]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/fe/if_fe_pccard.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / dev / 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  */
23
24 #include <sys/cdefs.h>
25 __FBSDID("$FreeBSD$");
26
27 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/socket.h>
30 #include <sys/systm.h>
31 #include <sys/module.h>
32
33 #include <sys/bus.h>
34 #include <machine/bus.h>
35 #include <sys/rman.h>
36
37 #include <net/ethernet.h>
38 #include <net/if.h>
39 #include <net/if_mib.h>
40 #include <net/if_media.h>
41
42 #include <netinet/in.h>
43 #include <netinet/if_ether.h>
44
45 #include <dev/fe/mb86960.h>
46 #include <dev/fe/if_fereg.h>
47 #include <dev/fe/if_fevar.h>
48
49 #include <dev/pccard/pccardvar.h>
50 #include <dev/pccard/pccard_cis.h>
51
52 #include "card_if.h"
53 #include "pccarddevs.h"
54
55 /*
56  *      PC Card (PCMCIA) specific code.
57  */
58 static int fe_pccard_probe(device_t);
59 static int fe_pccard_attach(device_t);
60 static int fe_pccard_detach(device_t);
61
62 static const struct fe_pccard_product {
63         struct pccard_product mpp_product;
64         int mpp_flags;
65 #define MPP_MBH10302 1
66 #define MPP_ANYFUNC 2
67 } fe_pccard_products[] = {
68         /* These need to be first */
69         { PCMCIA_CARD(FUJITSU2, FMV_J181), MPP_MBH10302 },
70         { PCMCIA_CARD(FUJITSU2, FMV_J182), 0 },
71         { PCMCIA_CARD(FUJITSU2, FMV_J182A), 0 },
72         { PCMCIA_CARD(FUJITSU2, ITCFJ182A), 0 },
73         /* These need to be second */
74         { PCMCIA_CARD(TDK, LAK_CD021BX), 0 }, 
75         { PCMCIA_CARD(TDK, LAK_CF010), 0 }, 
76 #if 0 /* XXX 86960-based? */
77         { PCMCIA_CARD(TDK, LAK_DFL9610), 0 }, 
78 #endif
79         { PCMCIA_CARD(CONTEC, CNETPC), 0 },
80         { PCMCIA_CARD(FUJITSU, LA501), 0 },
81         { PCMCIA_CARD(FUJITSU, LA10S), 0 },
82         { PCMCIA_CARD(FUJITSU, NE200T), MPP_MBH10302 },/* Sold by Eagle */
83         { PCMCIA_CARD(RATOC, REX_R280), 0 },
84         { PCMCIA_CARD(XIRCOM, CE), MPP_ANYFUNC },
85         { { NULL } }
86 };
87
88 static int
89 fe_pccard_probe(device_t dev)
90 {
91         int             error;
92         uint32_t        fcn = PCCARD_FUNCTION_UNSPEC;
93         const struct fe_pccard_product *pp;
94
95         if ((pp = (const struct fe_pccard_product *)pccard_product_lookup(dev,
96             (const struct pccard_product *)fe_pccard_products,
97             sizeof(fe_pccard_products[0]), NULL)) != NULL) {
98                 if (pp->mpp_product.pp_name != NULL)
99                         device_set_desc(dev, pp->mpp_product.pp_name);
100                 if (pp->mpp_flags & MPP_ANYFUNC)
101                         return (0);
102                 /* Make sure we're a network function */
103                 error = pccard_get_function(dev, &fcn);
104                 if (error != 0)
105                         return (error);
106                 if (fcn != PCCARD_FUNCTION_NETWORK)
107                         return (ENXIO);
108                 return (0);
109         }
110         return (ENXIO);
111 }
112
113 static device_method_t fe_pccard_methods[] = {
114         /* Device interface */
115         DEVMETHOD(device_probe,         fe_pccard_probe),
116         DEVMETHOD(device_attach,        fe_pccard_attach),
117         DEVMETHOD(device_detach,        fe_pccard_detach),
118
119         { 0, 0 }
120 };
121
122 static driver_t fe_pccard_driver = {
123         "fe",
124         fe_pccard_methods,
125         sizeof (struct fe_softc)
126 };
127
128 DRIVER_MODULE(fe, pccard, fe_pccard_driver, fe_devclass, 0, 0);
129
130 static int fe_probe_mbh(device_t, const struct fe_pccard_product *);
131 static int fe_probe_tdk(device_t, const struct fe_pccard_product *);
132 /*
133  *      Initialize the device - called from Slot manager.
134  */
135 static int
136 fe_pccard_attach(device_t dev)
137 {
138         struct fe_softc *sc;
139         const struct fe_pccard_product *pp;
140         int error;
141
142         /* Prepare for the device probe process.  */
143         sc = device_get_softc(dev);
144         sc->sc_unit = device_get_unit(dev);
145
146         pp = (const struct fe_pccard_product *) pccard_product_lookup(dev,
147             (const struct pccard_product *)fe_pccard_products,
148             sizeof(fe_pccard_products[0]), NULL);
149         if (pp == NULL)
150                 return (ENXIO);
151
152         if (pp->mpp_flags & MPP_MBH10302)
153                 error = fe_probe_mbh(dev, pp);
154         else
155                 error = fe_probe_tdk(dev, pp);
156         if (error != 0) {
157                 fe_release_resource(dev);
158                 return (error);
159         }
160         error = fe_alloc_irq(dev, 0);
161         if (error != 0) {
162                 fe_release_resource(dev);
163                 return (error);
164         }
165         return (fe_attach(dev));
166 }
167
168 /*
169  *      feunload - unload the driver and clear the table.
170  */
171 static int
172 fe_pccard_detach(device_t dev)
173 {
174         struct fe_softc *sc = device_get_softc(dev);
175         struct ifnet *ifp = sc->ifp;
176
177         FE_LOCK(sc);
178         fe_stop(sc);
179         FE_UNLOCK(sc);
180         callout_drain(&sc->timer);
181         ether_ifdetach(ifp);
182         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
183         if_free(ifp);
184         fe_release_resource(dev);
185         mtx_destroy(&sc->lock);
186
187         return 0;
188 }
189
190
191 /*
192  * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
193  * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
194  */
195 static void
196 fe_init_mbh(struct fe_softc *sc)
197 {
198         /* Minimal initialization of 86960.  */
199         DELAY(200);
200         fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
201         DELAY(200);
202
203         /* Disable all interrupts.  */
204         fe_outb(sc, FE_DLCR2, 0);
205         fe_outb(sc, FE_DLCR3, 0);
206
207         /* Enable master interrupt flag.  */
208         fe_outb(sc, FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE);
209 }
210
211 static int
212 fe_probe_mbh(device_t dev, const struct fe_pccard_product *pp)
213 {
214         struct fe_softc *sc = device_get_softc(dev);
215
216         static struct fe_simple_probe_struct probe_table [] = {
217                 { FE_DLCR2, 0x58, 0x00 },
218                 { FE_DLCR4, 0x08, 0x00 },
219                 { FE_DLCR6, 0xFF, 0xB6 },
220                 { 0 }
221         };
222
223         /* MBH10302 occupies 32 I/O addresses. */
224         if (fe_alloc_port(dev, 32))
225                 return ENXIO;
226
227         /* Fill the softc struct with default values.  */
228         fe_softc_defaults(sc);
229
230         /*
231          * See if MBH10302 is on its address.
232          * I'm not sure the following probe code works.  FIXME.
233          */
234         if (!fe_simple_probe(sc, probe_table))
235                 return ENXIO;
236
237         /* Get our station address from EEPROM.  */
238         fe_inblk(sc, FE_MBH10, sc->enaddr, ETHER_ADDR_LEN);
239
240         /* Make sure we got a valid station address.  */
241         if (!fe_valid_Ether_p(sc->enaddr, 0))
242                 return ENXIO;
243
244         /* Determine the card type.  */
245         sc->type = FE_TYPE_MBH;
246         sc->typestr = "MBH10302 (PCMCIA)";
247
248         /* We seems to need our own IDENT bits...  FIXME.  */
249         sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
250
251         /* Setup hooks.  We need a special initialization procedure.  */
252         sc->init = fe_init_mbh;
253
254         return 0;
255 }
256
257 static int
258 sn_pccard_xircom_mac(const struct pccard_tuple *tuple, void *argp)
259 {
260         uint8_t *enaddr = argp;
261         int i;
262
263 #if 1
264         /*
265          * We fail to map the CIS twice, for reasons unknown.  We
266          * may fix this in the future by loading the CIS with a sane
267          * CIS from userland.
268          */
269         static uint8_t defaultmac[ETHER_ADDR_LEN] = {
270                 0x00, 0x80, 0xc7, 0xed, 0x16, 0x7b};
271
272         /* Copy the MAC ADDR and return success */
273         for (i = 0; i < ETHER_ADDR_LEN; i++)
274                 enaddr[i] = defaultmac[i];
275 #else
276         /* FUNCE is not after FUNCID, so we gotta go find it */
277         if (tuple->code != 0x22)
278                 return (0);
279
280         /* Make sure this is a sane node */
281         if (tuple->length < ETHER_ADDR_LEN + 3)
282                 return (0);
283
284         /* Copy the MAC ADDR and return success */
285         for (i = 0; i < ETHER_ADDR_LEN; i++)
286                 enaddr[i] = pccard_tuple_read_1(tuple, i + 3);
287 #endif
288         return (1);
289 }
290
291 /*
292  * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
293  * by MASUI Kenji <masui@cs.titech.ac.jp>
294  *
295  * (Contec uses TDK Ethenet chip -- hosokawa)
296  *
297  * This version of fe_probe_tdk has been rewrote to handle
298  * *generic* PC Card implementation of Fujitsu MB8696x family.  The
299  * name _tdk is just for a historical reason. :-)
300  */
301 static int
302 fe_probe_tdk (device_t dev, const struct fe_pccard_product *pp)
303 {
304         struct fe_softc *sc = device_get_softc(dev);
305
306         static struct fe_simple_probe_struct probe_table [] = {
307                 { FE_DLCR2, 0x10, 0x00 },
308                 { FE_DLCR4, 0x08, 0x00 },
309             /*  { FE_DLCR5, 0x80, 0x00 },       Does not work well.  */
310                 { 0 }
311         };
312
313
314         /* C-NET(PC)C occupies 16 I/O addresses. */
315         if (fe_alloc_port(dev, 16))
316                 return ENXIO;
317
318         /* Fill the softc struct with default values.  */
319         fe_softc_defaults(sc);
320
321         /*
322          * See if C-NET(PC)C is on its address.
323          */
324         if (!fe_simple_probe(sc, probe_table))
325                 return ENXIO;
326
327         /* Determine the card type.  */
328         sc->type = FE_TYPE_TDK;
329         sc->typestr = "Generic MB8696x/78Q837x Ethernet (PCMCIA)";
330
331         pccard_get_ether(dev, sc->enaddr);
332
333         /* Make sure we got a valid station address.  */
334         if (!fe_valid_Ether_p(sc->enaddr, 0)) {
335                 pccard_cis_scan(dev, sn_pccard_xircom_mac, sc->enaddr);
336         }
337
338         /* Make sure we got a valid station address.  */
339         if (!fe_valid_Ether_p(sc->enaddr, 0))
340                 return ENXIO;
341
342         return 0;
343 }