eddd0ea9c99b8f7e83feed6c6d619b0014fee5e5
[dragonfly.git] / sys / dev / netif / fe / if_fe_pccard.c
1 /*
2  * All Rights Reserved, Copyright (C) Fujitsu Limited 1995
3  *
4  * This software may be used, modified, copied, distributed, and sold, in
5  * both source and binary form provided that the above copyright, these
6  * terms and the following disclaimer are retained.  The name of the author
7  * and/or the contributor may not be used to endorse or promote products
8  * derived from this software without specific prior written permission.
9  *
10  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND
11  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
12  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE
14  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
15  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
16  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION.
17  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
18  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
19  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20  * SUCH DAMAGE.
21  *
22  * $FreeBSD: src/sys/dev/fe/if_fe_pccard.c,v 1.2.2.1 2000/09/22 10:01:47 nyan Exp $
23  */
24
25 #include "opt_fe.h"
26 #include "opt_inet.h"
27 #include "opt_ipx.h"
28
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <machine/clock.h>
35
36 #include <net/ethernet.h>
37 #include <net/if.h>
38 #include <net/if_mib.h>
39 #include <net/if_media.h>
40
41 #include <netinet/in.h>
42 #include <netinet/if_ether.h>
43
44 #include "mb86960.h"
45 #include "if_fereg.h"
46 #include "if_fevar.h"
47
48 #include <bus/pccard/pccardvar.h>
49
50 #include "card_if.h"
51 #include "pccarddevs.h"
52
53 /*
54  *      PC-Card (PCMCIA) specific code.
55  */
56 static int fe_pccard_probe(device_t);
57 static int fe_pccard_attach(device_t);
58 static int fe_pccard_detach(device_t);
59 static int fe_pccard_match(device_t);
60
61 static const struct fe_pccard_product {
62         struct pccard_product mpp_product;
63         u_int32_t mpp_ioalign;                  /* required alignment */
64         int mpp_enet_maddr;
65 } fe_pccard_products[] = {
66         { PCMCIA_CARD(TDK, LAK_CD021BX, 0), 0, -1 }, 
67         { PCMCIA_CARD(TDK, LAK_CF010, 0), 0, -1 }, 
68 #if 0 /* XXX 86960-based? */
69         { PCMCIA_CARD(TDK, LAK_DFL9610, 1), 0, -1 }, 
70 #endif
71         { PCMCIA_CARD(CONTEC, CNETPC, 0), 0, -1 },
72         { PCMCIA_CARD(FUJITSU, LA501, 0), 0x20, -1 },
73         { PCMCIA_CARD(FUJITSU, LA10S, 0), 0, -1 },
74         { PCMCIA_CARD(RATOC, REX_R280, 0), 0, 0x1fc },
75         { { NULL } }
76 };
77
78 static int
79 fe_pccard_match(device_t dev)
80 {
81         const struct pccard_product *pp;
82
83         if ((pp = pccard_product_lookup(dev,
84             (const struct pccard_product *)fe_pccard_products,
85             sizeof(fe_pccard_products[0]), NULL)) != NULL) {
86                 if (pp->pp_name != NULL)
87                         device_set_desc(dev, pp->pp_name);
88                 return 0;
89         }
90         return ENXIO;
91 }
92
93 static device_method_t fe_pccard_methods[] = {
94         /* Device interface */
95         DEVMETHOD(device_probe,         pccard_compat_probe),
96         DEVMETHOD(device_attach,        pccard_compat_attach),
97         DEVMETHOD(device_detach,        fe_pccard_detach),
98
99         /* Card interface */
100         DEVMETHOD(card_compat_match,    fe_pccard_match),
101         DEVMETHOD(card_compat_probe,    fe_pccard_probe),
102         DEVMETHOD(card_compat_attach,   fe_pccard_attach),
103
104         DEVMETHOD_END
105 };
106
107 static driver_t fe_pccard_driver = {
108         "fe",
109         fe_pccard_methods,
110         sizeof (struct fe_softc)
111 };
112
113 DRIVER_MODULE(if_fe, pccard, fe_pccard_driver, fe_devclass, NULL, NULL);
114
115
116 static int fe_probe_mbh(device_t);
117 static int fe_probe_tdk(device_t);
118
119 /*
120  *      Initialize the device - called from Slot manager.
121  */
122 static int
123 fe_pccard_probe(device_t dev)
124 {
125         struct fe_softc *sc;
126         int i, error;
127         uint8_t sum;
128         const uint8_t *ether_addr;
129
130         /* Prepare for the device probe process.  */
131         sc = device_get_softc(dev);
132         sc->sc_unit = device_get_unit(dev);
133
134         ether_addr = pccard_get_ether(dev);
135         for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
136                 sum |= ether_addr[i];
137         if (sum)
138                 bcopy(ether_addr, sc->sc_enaddr, ETHER_ADDR_LEN);
139
140         /* Probe for supported cards.  */
141         if ((error = fe_probe_mbh(dev)) == 0)
142                 goto end;
143         fe_release_resource(dev);
144
145         if ((error = fe_probe_tdk(dev)) == 0)
146                 goto end;
147         fe_release_resource(dev);
148
149 end:
150         if (error == 0)
151                 error = fe_alloc_irq(dev, 0);
152
153         fe_release_resource(dev);
154         return (error);
155 }
156
157 static int
158 fe_pccard_attach(device_t dev)
159 {
160         struct fe_softc *sc = device_get_softc(dev);
161
162         if (sc->port_used)
163                 fe_alloc_port(dev, sc->port_used);
164         fe_alloc_irq(dev, 0);
165
166         return fe_attach(dev);
167 }
168
169 /*
170  *      feunload - unload the driver and clear the table.
171  *      XXX TODO:
172  *      This is usually called when the card is ejected, but
173  *      can be caused by a modunload of a controller driver.
174  *      The idea is to reset the driver's view of the device
175  *      and ensure that any driver entry points such as
176  *      read and write do not hang.
177  */
178 static int
179 fe_pccard_detach(device_t dev)
180 {
181         struct fe_softc *sc = device_get_softc(dev);
182         struct ifnet *ifp = &sc->arpcom.ac_if;
183
184         lwkt_serialize_enter(ifp->if_serializer);
185         fe_stop(sc);
186         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
187         lwkt_serialize_exit(ifp->if_serializer);
188
189         ether_ifdetach(ifp);
190         fe_release_resource(dev);
191
192         return 0;
193 }
194
195
196 /*
197  * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
198  * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
199  */
200 static void
201 fe_init_mbh(struct fe_softc *sc)
202 {
203         /* Minimal initialization of 86960.  */
204         DELAY(200);
205         fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
206         DELAY(200);
207
208         /* Disable all interrupts.  */
209         fe_outb(sc, FE_DLCR2, 0);
210         fe_outb(sc, FE_DLCR3, 0);
211
212         /* Enable master interrupt flag.  */
213         fe_outb(sc, FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE);
214 }
215
216 static int
217 fe_probe_mbh(device_t dev)
218 {
219         struct fe_softc *sc = device_get_softc(dev);
220
221         static struct fe_simple_probe_struct probe_table [] = {
222                 { FE_DLCR2, 0x58, 0x00 },
223                 { FE_DLCR4, 0x08, 0x00 },
224                 { FE_DLCR6, 0xFF, 0xB6 },
225                 { 0 }
226         };
227
228         /* MBH10302 occupies 32 I/O addresses. */
229         if (fe_alloc_port(dev, 32))
230                 return ENXIO;
231
232         /* Ethernet MAC address should *NOT* have been given by pccardd,
233            if this is a true MBH10302; i.e., Ethernet address must be
234            "all-zero" upon entry.  */
235         if (sc->sc_enaddr[0] || sc->sc_enaddr[1] || sc->sc_enaddr[2] ||
236             sc->sc_enaddr[3] || sc->sc_enaddr[4] || sc->sc_enaddr[5])
237                 return ENXIO;
238
239         /* Fill the softc struct with default values.  */
240         fe_softc_defaults(sc);
241
242         /*
243          * See if MBH10302 is on its address.
244          * I'm not sure the following probe code works.  FIXME.
245          */
246         if (!fe_simple_probe(sc, probe_table))
247                 return ENXIO;
248
249         /* Get our station address from EEPROM.  */
250         fe_inblk(sc, FE_MBH10, sc->sc_enaddr, ETHER_ADDR_LEN);
251
252         /* Make sure we got a valid station address.  */
253         if (!valid_Ether_p(sc->sc_enaddr, 0))
254                 return ENXIO;
255
256         /* Determine the card type.  */
257         sc->type = FE_TYPE_MBH;
258         sc->typestr = "MBH10302 (PCMCIA)";
259
260         /* We seems to need our own IDENT bits...  FIXME.  */
261         sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
262
263         /* Setup hooks.  We need a special initialization procedure.  */
264         sc->init = fe_init_mbh;
265
266         return 0;
267 }
268
269 /*
270  * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
271  * by MASUI Kenji <masui@cs.titech.ac.jp>
272  *
273  * (Contec uses TDK Ethenet chip -- hosokawa)
274  *
275  * This version of fe_probe_tdk has been rewrote to handle
276  * *generic* PC card implementation of Fujitsu MB8696x family.  The
277  * name _tdk is just for a historical reason. :-)
278  */
279 static int
280 fe_probe_tdk (device_t dev)
281 {
282         struct fe_softc *sc = device_get_softc(dev);
283
284         static struct fe_simple_probe_struct probe_table [] = {
285                 { FE_DLCR2, 0x50, 0x00 },
286                 { FE_DLCR4, 0x08, 0x00 },
287             /*  { FE_DLCR5, 0x80, 0x00 },       Does not work well.  */
288                 { 0 }
289         };
290
291         /* C-NET(PC)C occupies 16 I/O addresses. */
292         if (fe_alloc_port(dev, 16))
293                 return ENXIO;
294
295         /* Fill the softc struct with default values.  */
296         fe_softc_defaults(sc);
297
298         /*
299          * See if C-NET(PC)C is on its address.
300          */
301         if (!fe_simple_probe(sc, probe_table))
302                 return ENXIO;
303
304         /* Determine the card type.  */
305         sc->type = FE_TYPE_TDK;
306         sc->typestr = "Generic MB8696x/78Q837x Ethernet (PCMCIA)";
307
308         /* Make sure we got a valid station address.  */
309         if (!valid_Ether_p(sc->sc_enaddr, 0))
310                 return ENXIO;
311
312         return 0;
313 }