Merge from vendor branch GCC:
[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  * $DragonFly: src/sys/dev/netif/fe/if_fe_pccard.c,v 1.12 2006/11/07 06:43:23 dillon Exp $
24  */
25
26 #include "opt_fe.h"
27 #include "opt_inet.h"
28 #include "opt_ipx.h"
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/socket.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <machine/clock.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 <machine_base/isa/ic/mb86960.h>
46 #include "if_fereg.h"
47 #include "if_fevar.h"
48
49 #include <bus/pccard/pccardvar.h>
50 #include <bus/pccard/pccarddevs.h>
51 #include <bus/pccard/cardinfo.h>
52
53 #include "card_if.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 static int fe_pccard_match(device_t);
62
63 static const struct fe_pccard_product {
64         struct pccard_product mpp_product;
65         u_int32_t mpp_ioalign;                  /* required alignment */
66         int mpp_enet_maddr;
67 } fe_pccard_products[] = {
68         { PCMCIA_CARD(TDK, LAK_CD021BX, 0), 0, -1 }, 
69         { PCMCIA_CARD(TDK, LAK_CF010, 0), 0, -1 }, 
70 #if 0 /* XXX 86960-based? */
71         { PCMCIA_CARD(TDK, LAK_DFL9610, 1), 0, -1 }, 
72 #endif
73         { PCMCIA_CARD(CONTEC, CNETPC, 0), 0, -1 },
74         { PCMCIA_CARD(FUJITSU, LA501, 0), 0x20, -1 },
75         { PCMCIA_CARD(FUJITSU, LA10S, 0), 0, -1 },
76         { PCMCIA_CARD(RATOC, REX_R280, 0), 0, 0x1fc },
77         { { NULL } }
78 };
79
80 static int
81 fe_pccard_match(device_t dev)
82 {
83         const struct pccard_product *pp;
84
85         if ((pp = pccard_product_lookup(dev,
86             (const struct pccard_product *)fe_pccard_products,
87             sizeof(fe_pccard_products[0]), NULL)) != NULL) {
88                 if (pp->pp_name != NULL)
89                         device_set_desc(dev, pp->pp_name);
90                 return 0;
91         }
92         return ENXIO;
93 }
94
95 static device_method_t fe_pccard_methods[] = {
96         /* Device interface */
97         DEVMETHOD(device_probe,         pccard_compat_probe),
98         DEVMETHOD(device_attach,        pccard_compat_attach),
99         DEVMETHOD(device_detach,        fe_pccard_detach),
100
101         /* Card interface */
102         DEVMETHOD(card_compat_match,    fe_pccard_match),
103         DEVMETHOD(card_compat_probe,    fe_pccard_probe),
104         DEVMETHOD(card_compat_attach,   fe_pccard_attach),
105
106         { 0, 0 }
107 };
108
109 static driver_t fe_pccard_driver = {
110         "fe",
111         fe_pccard_methods,
112         sizeof (struct fe_softc)
113 };
114
115 DRIVER_MODULE(if_fe, pccard, fe_pccard_driver, fe_devclass, 0, 0);
116
117
118 static int fe_probe_mbh(device_t);
119 static int fe_probe_tdk(device_t);
120
121 /*
122  *      Initialize the device - called from Slot manager.
123  */
124 static int
125 fe_pccard_probe(device_t dev)
126 {
127         struct fe_softc *sc;
128         int i, error;
129         uint8_t sum;
130         uint8_t *ether_addr;
131
132         /* Prepare for the device probe process.  */
133         sc = device_get_softc(dev);
134         sc->sc_unit = device_get_unit(dev);
135
136         ether_addr = pccard_get_ether(dev);
137         for (i = 0, sum = 0; i < ETHER_ADDR_LEN; i++)
138                 sum |= ether_addr[i];
139         if (sum)
140                 bcopy(ether_addr, sc->sc_enaddr, ETHER_ADDR_LEN);
141
142         /* Probe for supported cards.  */
143         if ((error = fe_probe_mbh(dev)) == 0)
144                 goto end;
145         fe_release_resource(dev);
146
147         if ((error = fe_probe_tdk(dev)) == 0)
148                 goto end;
149         fe_release_resource(dev);
150
151 end:
152         if (error == 0)
153                 error = fe_alloc_irq(dev, 0);
154
155         fe_release_resource(dev);
156         return (error);
157 }
158
159 static int
160 fe_pccard_attach(device_t dev)
161 {
162         struct fe_softc *sc = device_get_softc(dev);
163
164         if (sc->port_used)
165                 fe_alloc_port(dev, sc->port_used);
166         fe_alloc_irq(dev, 0);
167
168         return fe_attach(dev);
169 }
170
171 /*
172  *      feunload - unload the driver and clear the table.
173  *      XXX TODO:
174  *      This is usually called when the card is ejected, but
175  *      can be caused by a modunload of a controller driver.
176  *      The idea is to reset the driver's view of the device
177  *      and ensure that any driver entry points such as
178  *      read and write do not hang.
179  */
180 static int
181 fe_pccard_detach(device_t dev)
182 {
183         struct fe_softc *sc = device_get_softc(dev);
184         struct ifnet *ifp = &sc->arpcom.ac_if;
185
186         lwkt_serialize_enter(ifp->if_serializer);
187         fe_stop(sc);
188         bus_teardown_intr(dev, sc->irq_res, sc->irq_handle);
189         lwkt_serialize_exit(ifp->if_serializer);
190
191         ether_ifdetach(ifp);
192         fe_release_resource(dev);
193
194         return 0;
195 }
196
197
198 /*
199  * Probe and initialization for Fujitsu MBH10302 PCMCIA Ethernet interface.
200  * Note that this is for 10302 only; MBH10304 is handled by fe_probe_tdk().
201  */
202 static void
203 fe_init_mbh(struct fe_softc *sc)
204 {
205         /* Minimal initialization of 86960.  */
206         DELAY(200);
207         fe_outb(sc, FE_DLCR6, sc->proto_dlcr6 | FE_D6_DLC_DISABLE);
208         DELAY(200);
209
210         /* Disable all interrupts.  */
211         fe_outb(sc, FE_DLCR2, 0);
212         fe_outb(sc, FE_DLCR3, 0);
213
214         /* Enable master interrupt flag.  */
215         fe_outb(sc, FE_MBH0, FE_MBH0_MAGIC | FE_MBH0_INTR_ENABLE);
216 }
217
218 static int
219 fe_probe_mbh(device_t dev)
220 {
221         struct fe_softc *sc = device_get_softc(dev);
222
223         static struct fe_simple_probe_struct probe_table [] = {
224                 { FE_DLCR2, 0x58, 0x00 },
225                 { FE_DLCR4, 0x08, 0x00 },
226                 { FE_DLCR6, 0xFF, 0xB6 },
227                 { 0 }
228         };
229
230         /* MBH10302 occupies 32 I/O addresses. */
231         if (fe_alloc_port(dev, 32))
232                 return ENXIO;
233
234         /* Ethernet MAC address should *NOT* have been given by pccardd,
235            if this is a true MBH10302; i.e., Ethernet address must be
236            "all-zero" upon entry.  */
237         if (sc->sc_enaddr[0] || sc->sc_enaddr[1] || sc->sc_enaddr[2] ||
238             sc->sc_enaddr[3] || sc->sc_enaddr[4] || sc->sc_enaddr[5])
239                 return ENXIO;
240
241         /* Fill the softc struct with default values.  */
242         fe_softc_defaults(sc);
243
244         /*
245          * See if MBH10302 is on its address.
246          * I'm not sure the following probe code works.  FIXME.
247          */
248         if (!fe_simple_probe(sc, probe_table))
249                 return ENXIO;
250
251         /* Get our station address from EEPROM.  */
252         fe_inblk(sc, FE_MBH10, sc->sc_enaddr, ETHER_ADDR_LEN);
253
254         /* Make sure we got a valid station address.  */
255         if (!valid_Ether_p(sc->sc_enaddr, 0))
256                 return ENXIO;
257
258         /* Determine the card type.  */
259         sc->type = FE_TYPE_MBH;
260         sc->typestr = "MBH10302 (PCMCIA)";
261
262         /* We seems to need our own IDENT bits...  FIXME.  */
263         sc->proto_dlcr7 = FE_D7_BYTSWP_LH | FE_D7_IDENT_NICE;
264
265         /* Setup hooks.  We need a special initialization procedure.  */
266         sc->init = fe_init_mbh;
267
268         return 0;
269 }
270
271 /*
272  * Probe and initialization for TDK/CONTEC PCMCIA Ethernet interface.
273  * by MASUI Kenji <masui@cs.titech.ac.jp>
274  *
275  * (Contec uses TDK Ethenet chip -- hosokawa)
276  *
277  * This version of fe_probe_tdk has been rewrote to handle
278  * *generic* PC card implementation of Fujitsu MB8696x family.  The
279  * name _tdk is just for a historical reason. :-)
280  */
281 static int
282 fe_probe_tdk (device_t dev)
283 {
284         struct fe_softc *sc = device_get_softc(dev);
285
286         static struct fe_simple_probe_struct probe_table [] = {
287                 { FE_DLCR2, 0x50, 0x00 },
288                 { FE_DLCR4, 0x08, 0x00 },
289             /*  { FE_DLCR5, 0x80, 0x00 },       Does not work well.  */
290                 { 0 }
291         };
292
293         /* C-NET(PC)C occupies 16 I/O addresses. */
294         if (fe_alloc_port(dev, 16))
295                 return ENXIO;
296
297         /* Fill the softc struct with default values.  */
298         fe_softc_defaults(sc);
299
300         /*
301          * See if C-NET(PC)C is on its address.
302          */
303         if (!fe_simple_probe(sc, probe_table))
304                 return ENXIO;
305
306         /* Determine the card type.  */
307         sc->type = FE_TYPE_TDK;
308         sc->typestr = "Generic MB8696x/78Q837x Ethernet (PCMCIA)";
309
310         /* Make sure we got a valid station address.  */
311         if (!valid_Ether_p(sc->sc_enaddr, 0))
312                 return ENXIO;
313
314         return 0;
315 }