Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:28:25 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
45 #include <sys/module.h>
46 #include <sys/bus.h>
47
48 #include <machine/bus.h>
49 #include <machine/resource.h>
50 #include <sys/rman.h>
51  
52 #include <net/ethernet.h>
53 #include <net/if.h> 
54 #include <net/if_arp.h>
55 #include <net/if_media.h>
56
57 #include <machine/clock.h>
58
59 #include <dev/ep/if_epreg.h>
60 #include <dev/ep/if_epvar.h>
61
62 static const char *ep_pccard_identify(u_short id);
63
64 /*
65  * Initialize the device - called from Slot manager.
66  */
67 static int
68 ep_pccard_probe(device_t dev)
69 {
70         struct ep_softc *       sc = device_get_softc(dev);
71         struct ep_board *       epb = &sc->epb;
72         const char *            desc;
73         int                     error;
74
75         error = ep_alloc(dev);
76         if (error)
77                 return error;
78
79         /*
80          * XXX - Certain (newer?) 3Com cards need epb->cmd_off ==
81          * 2. Sadly, you need to have a correct cmd_off in order to
82          * identify the card.  So we have to hit it with both and
83          * cross our virtual fingers.  There's got to be a better way
84          * to do this.  jyoung@accessus.net 09/11/1999 
85          */
86
87         epb->cmd_off = 0;
88         epb->prod_id = get_e(sc, EEPROM_PROD_ID);
89         if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
90                 if (bootverbose) 
91                         device_printf(dev, "Pass 1 of 2 detection "
92                             "failed (nonfatal)\n");
93                 epb->cmd_off = 2;
94                 epb->prod_id = get_e(sc, EEPROM_PROD_ID);
95                 if ((desc = ep_pccard_identify(epb->prod_id)) == NULL) {
96                         device_printf(dev, "Unit failed to come ready or "
97                             "product ID unknown! (id 0x%x)\n", epb->prod_id);
98                         ep_free(dev);
99                         return (ENXIO);
100                 }
101         }
102         device_set_desc(dev, desc);
103
104         /*
105          * For some reason the 3c574 needs this.
106          */
107         ep_get_macaddr(sc, (u_char *)&sc->arpcom.ac_enaddr);
108
109         ep_free(dev);
110         return (0);
111 }
112
113 static const char *
114 ep_pccard_identify(u_short id)
115 {
116         /* Determine device type and associated MII capabilities  */
117         switch (id) {
118         case 0x6055: /* 3C556 */
119                 return ("3Com 3C556");
120         case 0x4057: /* 3C574 */
121                 return ("3Com 3C574");
122         case 0x4b57: /* 3C574B */
123                 return ("3Com 3C574B, Megahertz 3CCFE574BT or "
124                     "Fast Etherlink 3C574-TX");
125         case 0x2b57: /* 3CXSH572BT */
126                 return ("3Com OfficeConnect 572BT");
127         case 0x9058: /* 3C589 */
128                 return ("3Com Etherlink III 3C589");
129         case 0x2056: /* 3C562/3C563 */
130                 return ("3Com 3C562D/3C563D");
131         }
132         return (NULL);
133 }
134
135 static int
136 ep_pccard_card_attach(struct ep_board *epb)
137 {
138         /* Determine device type and associated MII capabilities  */
139         switch (epb->prod_id) {
140         case 0x6055: /* 3C556 */
141         case 0x2b57: /* 3C572BT */
142         case 0x4057: /* 3C574 */
143         case 0x4b57: /* 3C574B */
144                 epb->mii_trans = 1;
145                 return (1);
146         case 0x2056: /* 3C562D/3C563D */
147         case 0x9058: /* 3C589 */
148                 epb->mii_trans = 0;
149                 return (1);
150         }
151         return (0);
152 }
153
154 static int
155 ep_pccard_attach(device_t dev)
156 {
157         struct ep_softc *       sc = device_get_softc(dev);
158         int                     error = 0;
159
160         if ((error = ep_alloc(dev))) {
161                 device_printf(dev, "ep_alloc() failed! (%d)\n", error);
162                 goto bad;
163         }
164
165         sc->epb.cmd_off = 0;
166         sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
167         if (!ep_pccard_card_attach(&sc->epb)) {
168                 sc->epb.cmd_off = 2;
169                 sc->epb.prod_id = get_e(sc, EEPROM_PROD_ID);
170                 sc->epb.res_cfg = get_e(sc, EEPROM_RESOURCE_CFG);
171                 if (!ep_pccard_card_attach(&sc->epb)) {
172                         device_printf(dev,
173                             "Probe found ID, attach failed so ignore card!\n");
174                         error = ENXIO;
175                         goto bad;
176                 }
177         }
178
179         /* ROM size = 0, ROM base = 0 */
180         /* For now, ignore AUTO SELECT feature of 3C589B and later. */
181         outw(BASE + EP_W0_ADDRESS_CFG, get_e(sc, EEPROM_ADDR_CFG) & 0xc000);
182
183         /* Fake IRQ must be 3 */
184         outw(BASE + EP_W0_RESOURCE_CFG, (sc->epb.res_cfg & 0x0fff) | 0x3000);
185
186         outw(BASE + EP_W0_PRODUCT_ID, sc->epb.prod_id);
187
188         if (sc->epb.mii_trans) {
189                 /*
190                  * turn on the MII transciever
191                  */
192                 GO_WINDOW(3);
193                 outw(BASE + EP_W3_OPTIONS, 0x8040);
194                 DELAY(1000);
195                 outw(BASE + EP_W3_OPTIONS, 0xc040);
196                 outw(BASE + EP_COMMAND, RX_RESET);
197                 outw(BASE + EP_COMMAND, TX_RESET);
198                 while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
199                 DELAY(1000);
200                 outw(BASE + EP_W3_OPTIONS, 0x8040);
201         } else {
202                 ep_get_media(sc);
203         }
204
205         if ((error = ep_attach(sc))) {
206                 device_printf(dev, "ep_attach() failed! (%d)\n", error);
207                 goto bad;
208         }
209
210         if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
211                                     sc, &sc->ep_intrhand))) {
212                 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
213                 goto bad;
214         }
215
216         return (0);
217 bad:
218         ep_free(dev);
219         return (error);
220 }
221
222 static int
223 ep_pccard_detach(device_t dev)
224 {
225         struct ep_softc *sc = device_get_softc(dev);
226
227         if (sc->gone) {
228                 device_printf(dev, "already unloaded\n");
229                 return (0);
230         }
231         sc->arpcom.ac_if.if_flags &= ~IFF_RUNNING; 
232         ether_ifdetach(&sc->arpcom.ac_if, ETHER_BPF_SUPPORTED);
233         sc->gone = 1;
234         bus_teardown_intr(dev, sc->irq, sc->ep_intrhand);
235         ep_free(dev);
236         return (0);
237 }
238
239 static device_method_t ep_pccard_methods[] = {
240         /* Device interface */
241         DEVMETHOD(device_probe,         ep_pccard_probe),
242         DEVMETHOD(device_attach,        ep_pccard_attach),
243         DEVMETHOD(device_detach,        ep_pccard_detach),
244
245         { 0, 0 }
246 };
247
248 static driver_t ep_pccard_driver = {
249         "ep",
250         ep_pccard_methods,
251         sizeof(struct ep_softc),
252 };
253
254 extern devclass_t ep_devclass;
255
256 DRIVER_MODULE(ep, pccard, ep_pccard_driver, ep_devclass, 0, 0);