Do a major clean-up of the BUSDMA architecture. A large number of
[dragonfly.git] / sys / dev / netif / ep / if_ep_pccard.c
1 /*
2  * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Herb Peyerl.
16  * 4. The name of Herb Peyerl may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * $FreeBSD: src/sys/dev/ep/if_ep_pccard.c,v 1.12.2.2 2000/08/08 23:55:02 peter Exp $
31  * $DragonFly: src/sys/dev/netif/ep/if_ep_pccard.c,v 1.11 2006/10/25 20:55:56 dillon Exp $
32  */
33
34 /*
35  * Pccard support for 3C589 by:
36  *              HAMADA Naoki
37  *              nao@tom-yam.or.jp
38  */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/module.h>
45 #include <sys/bus.h>
46 #include <sys/rman.h>
47  
48 #include <net/ethernet.h>
49 #include <net/if.h> 
50 #include <net/if_arp.h>
51 #include <net/if_media.h>
52
53 #include <machine/clock.h>
54
55 #include "if_epreg.h"
56 #include "if_epvar.h"
57
58 #include <bus/pccard/pccardvar.h>
59 #include <bus/pccard/pccarddevs.h>
60
61 #include "card_if.h"
62
63 static const char *ep_pccard_identify(u_short id);
64
65 /*
66  * Initialize the device - called from Slot manager.
67  */
68 static int
69 ep_pccard_probe(device_t dev)
70 {
71         struct ep_softc *       sc = device_get_softc(dev);
72         struct ep_board *       epb = &sc->epb;
73         const char *            desc;
74         int                     error;
75
76         error = ep_alloc(dev);
77         if (error)
78                 return error;
79
80         /*
81          * XXX - Certain (newer?) 3Com cards need epb->cmd_off ==
82          * 2. Sadly, you need to have a correct cmd_off in order to
83          * identify the card.  So we have to hit it with both and
84          * cross our virtual fingers.  There's got to be a better way
85          * to do this.  jyoung@accessus.net 09/11/1999 
86          */
87
88         epb->cmd_off = 0;
89         epb->prod_id = get_e(sc, EEPROM_PROD_ID);
90         if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
91                 if (bootverbose) 
92                         device_printf(dev, "Pass 1 of 2 detection "
93                             "failed (nonfatal)\n");
94                 epb->cmd_off = 2;
95                 epb->prod_id = get_e(sc, EEPROM_PROD_ID);
96                 if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
97                         device_printf(dev, "Unit failed to come ready or "
98                             "product ID unknown! (id 0x%x)\n", epb->prod_id);
99                         ep_free(dev);
100                         return (ENXIO);
101                 }
102         }
103         device_set_desc(dev, desc);
104
105         /*
106          * For some reason the 3c574 needs this.
107          */
108         ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr);
109
110         ep_free(dev);
111         return (0);
112 }
113
114 static const char *
115 ep_pccard_identify(u_short id)
116 {
117         /* Determine device type and associated MII capabilities  */
118         switch (id) {
119         case 0x6055: /* 3C556 */
120                 return ("3Com 3C556");
121         case 0x4057: /* 3C574 */
122                 return ("3Com 3C574");
123         case 0x4b57: /* 3C574B */
124                 return ("3Com 3C574B, Megahertz 3CCFE574BT or "
125                     "Fast Etherlink 3C574-TX");
126         case 0x2b57: /* 3CXSH572BT */
127                 return ("3Com OfficeConnect 572BT");
128         case 0x9058: /* 3C589 */
129                 return ("3Com Etherlink III 3C589");
130         case 0x2056: /* 3C562/3C563 */
131                 return ("3Com 3C562D/3C563D");
132         }
133         return (NULL);
134 }
135
136 static int
137 ep_pccard_card_attach(struct ep_board *epb)
138 {
139         /* Determine device type and associated MII capabilities  */
140         switch (epb->prod_id) {
141         case 0x6055: /* 3C556 */
142         case 0x2b57: /* 3C572BT */
143         case 0x4057: /* 3C574 */
144         case 0x4b57: /* 3C574B */
145                 epb->mii_trans = 1;
146                 return (1);
147         case 0x2056: /* 3C562D/3C563D */
148         case 0x9058: /* 3C589 */
149                 epb->mii_trans = 0;
150                 return (1);
151         }
152         return (0);
153 }
154
155 static int
156 ep_pccard_attach(device_t dev)
157 {
158         struct ep_softc *       sc = device_get_softc(dev);
159         int                     error = 0;
160
161         if ((error = ep_alloc(dev))) {
162                 device_printf(dev, "ep_alloc() failed! (%d)\n", error);
163                 goto bad;
164         }
165
166         sc->epb.cmd_off = 0;
167         sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
168         if (!ep_pccard_card_attach(&sc->epb)) {
169                 sc->epb.cmd_off = 2;
170                 sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
171                 sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
172                 if (!ep_pccard_card_attach(&sc->epb)) {
173                         device_printf(dev,
174                             "Probe found ID, attach failed so ignore card!\n");
175                         error = ENXIO;
176                         goto bad;
177                 }
178         }
179
180         /* ROM size = 0, ROM base = 0 */
181         /* For now, ignore AUTO SELECT feature of 3C589B and later. */
182         outw(BASE + EP_W0_ADDRESS_CFG, get_e(sc, EEPROM_ADDR_CFG) & 0xc000);
183
184         /* Fake IRQ must be 3 */
185         outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb.res_cfg & 0x0fff) | 0x3000);
186
187         outw(BASE + EP_W0_PRODUCT_ID, sc->epb.prod_id);
188
189         if (sc->epb.mii_trans) {
190                 /*
191                  * turn on the MII transciever
192                  */
193                 GO_WINDOW(3);
194                 outw(BASE + EP_W3_OPTIONS, 0x8040);
195                 DELAY(1000);
196                 outw(BASE + EP_W3_OPTIONS, 0xc040);
197                 outw(BASE + EP_COMMAND, RX_RESET);
198                 outw(BASE + EP_COMMAND, TX_RESET);
199                 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
200                 DELAY(1000);
201                 outw(BASE + EP_W3_OPTIONS, 0x8040);
202         } else {
203                 ep_get_media(sc);
204         }
205
206         if ((error = ep_attach(sc))) {
207                 device_printf(dev, "ep_attach() failed! (%d)\n", error);
208                 goto bad;
209         }
210
211         error = bus_setup_intr(dev, sc->irq, INTR_NETSAFE, ep_intr,
212                                     sc, &sc->ep_intrhand, 
213                                     sc->arpcom.ac_if.if_serializer);
214         if (error) {
215                 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
216                 goto bad;
217         }
218
219         return (0);
220 bad:
221         ep_free(dev);
222         return (error);
223 }
224
225 static int
226 ep_pccard_detach(device_t dev)
227 {
228         struct ep_softc *sc = device_get_softc(dev);
229         struct ifnet *ifp = &sc->arpcom.ac_if;
230
231         lwkt_serialize_enter(ifp->if_serializer);
232
233         if (sc->gone) {
234                 device_printf(dev, "already unloaded\n");
235                 lwkt_serialize_exit(ifp->if_serializer);
236                 return (0);
237         }
238         ifp->if_flags &= ~IFF_RUNNING; 
239         sc->gone = 1;
240         bus_teardown_intr(dev, sc->irq, sc->ep_intrhand);
241
242         lwkt_serialize_exit(ifp->if_serializer);
243
244         ether_ifdetach(&sc->arpcom.ac_if);
245         ep_free(dev);
246         return (0);
247 }
248
249 static const struct pccard_product ep_pccard_products[] = {
250         PCMCIA_CARD(3COM, 3C1, 0),
251         PCMCIA_CARD(3COM, 3C562, 0),
252         PCMCIA_CARD(3COM, 3C574, 0),    /* ROADRUNNER */
253         PCMCIA_CARD(3COM, 3C589, 0),
254         PCMCIA_CARD(3COM, 3CCFEM556BI, 0),      /* ROADRUNNER */
255         PCMCIA_CARD(3COM, 3CXEM556, 0),
256         PCMCIA_CARD(3COM, 3CXEM556INT, 0),
257         {NULL}
258 };
259
260 static int
261 ep_pccard_match(device_t dev)
262 {
263         const struct pccard_product *pp;
264
265         if ((pp = pccard_product_lookup(dev, ep_pccard_products,
266                     sizeof(ep_pccard_products[0]), NULL)) != NULL) {
267                 if (pp->pp_name != NULL)
268                         device_set_desc(dev, pp->pp_name);
269                 return 0;
270         }
271         return EIO;
272 }
273
274 static device_method_t ep_pccard_methods[] = {
275         /* Device interface */
276         DEVMETHOD(device_probe, pccard_compat_probe),
277         DEVMETHOD(device_attach, pccard_compat_attach),
278         DEVMETHOD(device_detach, ep_pccard_detach),
279
280         /* Card interface */
281         DEVMETHOD(card_compat_match, ep_pccard_match),
282         DEVMETHOD(card_compat_probe, ep_pccard_probe),
283         DEVMETHOD(card_compat_attach, ep_pccard_attach),
284
285         { 0, 0 }
286 };
287
288 static driver_t ep_pccard_driver = {
289         "ep",
290         ep_pccard_methods,
291         sizeof(struct ep_softc),
292 };
293
294 extern devclass_t ep_devclass;
295
296 DRIVER_MODULE(if_ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);