Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / netif / ep / if_ep_isa.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_isa.c,v 1.8.2.1 2000/12/16 03:47:57 nyan Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/socket.h>
37
38 #include <sys/module.h>
39 #include <sys/bus.h>
40
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 #include <sys/rman.h> 
44
45 #include <net/if.h>
46 #include <net/if_arp.h>
47 #include <net/if_media.h> 
48
49 #include <machine/clock.h>
50
51 #include <isa/isavar.h>
52 #include <isa/pnpvar.h>
53
54 #include <dev/ep/if_epreg.h>
55 #include <dev/ep/if_epvar.h>
56 #include <i386/isa/elink.h>
57
58 static u_int16_t        get_eeprom_data (int, int);
59
60 static void             ep_isa_identify (driver_t *, device_t);
61 static int              ep_isa_probe    (device_t);
62 static int              ep_isa_attach   (device_t);
63
64 struct isa_ident {
65         u_int32_t       id;
66         char *          name;
67 };
68 const char * ep_isa_match_id (u_int32_t, struct isa_ident *);
69
70 #define ISA_ID_3C509_XXX   0x0506d509
71 #define ISA_ID_3C509_TP    0x506d5090
72 #define ISA_ID_3C509_BNC   0x506d5091
73 #define ISA_ID_3C509_COMBO 0x506d5094
74 #define ISA_ID_3C509_TPO   0x506d5095
75 #define ISA_ID_3C509_TPC   0x506d5098
76 #ifdef PC98
77 #define ISA_ID_3C569B_COMBO 0x506d5694
78 #define ISA_ID_3C569B_TPO   0x506d5695
79 #endif
80
81 static struct isa_ident ep_isa_devs[] = {
82         { ISA_ID_3C509_TP,      "3Com 3C509-TP EtherLink III" },
83         { ISA_ID_3C509_BNC,     "3Com 3C509-BNC EtherLink III" },
84         { ISA_ID_3C509_COMBO,   "3Com 3C509-Combo EtherLink III" },
85         { ISA_ID_3C509_TPO,     "3Com 3C509-TPO EtherLink III" },
86         { ISA_ID_3C509_TPC,     "3Com 3C509-TPC EtherLink III" },
87 #ifdef PC98
88         { ISA_ID_3C569B_COMBO,  "3Com 3C569B-J-Combo EtherLink III" },
89         { ISA_ID_3C569B_TPO,    "3Com 3C569B-J-TPO EtherLink III" },
90 #endif
91         { 0,                    NULL },
92 };
93
94 static struct isa_pnp_id ep_ids[] = {
95         { 0x90506d50,   "3Com 3C509B-TP EtherLink III (PnP)" }, /* TCM5090 */
96         { 0x91506d50,   "3Com 3C509B-BNC EtherLink III (PnP)" },/* TCM5091 */
97         { 0x94506d50,   "3Com 3C509B-Combo EtherLink III (PnP)" },/* TCM5094 */
98         { 0x95506d50,   "3Com 3C509B-TPO EtherLink III (PnP)" },/* TCM5095 */
99         { 0x98506d50,   "3Com 3C509B-TPC EtherLink III (PnP)" },/* TCM5098 */
100         { 0xf780d041,   NULL }, /* PNP80f7 */
101         { 0,            NULL },
102 };
103
104 /*
105  * We get eeprom data from the id_port given an offset into the eeprom.
106  * Basically; after the ID_sequence is sent to all of the cards; they enter
107  * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
108  * the eeprom data.  We then read the port 16 times and with every read; the
109  * cards check for contention (ie: if one card writes a 0 bit and another
110  * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
111  * compares the data on the bus; if there is a difference then that card goes
112  * into ID_WAIT state again). In the meantime; one bit of data is returned in
113  * the AX register which is conveniently returned to us by inb().  Hence; we
114  * read 16 times getting one bit of data with each read.
115  */
116
117 static u_int16_t
118 get_eeprom_data(id_port, offset)
119         int     id_port;
120         int     offset;
121 {
122         int             i;
123         u_int16_t       data = 0;
124
125         outb(id_port, EEPROM_CMD_RD|offset);
126         DELAY(BIT_DELAY_MULTIPLE * 1000);
127         for (i = 0; i < 16; i++) {
128                 DELAY(50);
129                 data = (data << 1) | (inw(id_port) & 1);
130         }
131         return (data);
132 }
133
134 const char *
135 ep_isa_match_id (id, isa_devs)
136         u_int32_t             id;
137         struct isa_ident *      isa_devs;
138 {
139         struct isa_ident *      i = isa_devs;
140         while(i->name != NULL) {
141                if (id == i->id)
142                       return (i->name);
143                i++;
144         }
145         /*
146          * If we see a card that is likely to be a 3c509
147          * return something so that it will work; be annoying
148          * so that the user will tell us about it though.
149          */
150         if ((id >> 4) == ISA_ID_3C509_XXX) {
151                 return ("Unknown 3c509; notify maintainer!");
152         }
153         return (NULL);
154 }
155
156 static void
157 ep_isa_identify (driver_t *driver, device_t parent)
158 {
159         int             tag = EP_LAST_TAG;
160         int             found = 0;
161         int             i;
162         int             j;
163         const char *    desc;
164         u_int16_t       data;
165         u_int32_t       irq;
166         u_int32_t       ioport;
167         u_int32_t       isa_id;
168         device_t        child;
169
170         outb(ELINK_ID_PORT, 0);
171         outb(ELINK_ID_PORT, 0);
172         elink_idseq(ELINK_509_POLY);
173         elink_reset();
174
175         DELAY(DELAY_MULTIPLE * 10000);
176
177         for (i = 0; i < EP_MAX_BOARDS; i++) {
178
179                 outb(ELINK_ID_PORT, 0);
180                 outb(ELINK_ID_PORT, 0);
181                 elink_idseq(ELINK_509_POLY);
182                 DELAY(400);
183
184                 /* For the first probe, clear all
185                  * board's tag registers.
186                  * Otherwise kill off already-found
187                  * boards. -- linux 3c509.c
188                  */
189                 if (i == 0) {
190                         outb(ELINK_ID_PORT, 0xd0);
191                 } else {
192                         outb(ELINK_ID_PORT, 0xd8);
193                 }
194
195                 /* Get out of loop if we're out of cards. */
196                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
197                 if (data != MFG_ID) {
198                         break;
199                 }
200
201                 /* resolve contention using the Ethernet address */
202                 for (j = 0; j < 3; j++) {
203                         (void)get_eeprom_data(ELINK_ID_PORT, j);
204                 }
205
206                 /*
207                  * Construct an 'isa_id' in 'EISA'
208                  * format.
209                  */
210                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_MFG_ID);
211                 isa_id = (htons(data) << 16);
212                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_PROD_ID);
213                 isa_id |= htons(data);
214
215                 /* Find known ISA boards */
216                 desc = ep_isa_match_id(isa_id, ep_isa_devs);
217                 if (!desc) {
218                         if (bootverbose) {
219                                 device_printf(parent, "if_ep: unknown ID 0x%08x\n",
220                                                 isa_id);
221                         }
222                         continue;
223                 }
224
225                 /* Retreive IRQ */
226                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_RESOURCE_CFG);
227                 irq = (data >> 12);
228
229                 /* Retreive IOPORT */
230                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_ADDR_CFG);
231 #ifdef PC98
232                 ioport = (((data & 0x1f) * 0x100) + 0x40d0);
233 #else
234                 ioport = (((data & 0x1f) << 4) + 0x200);
235 #endif
236
237                 /* Test for an adapter with PnP support. */
238                 data = get_eeprom_data(ELINK_ID_PORT, EEPROM_CAP);
239                 if (data == CAP_ISA) {
240                         data = get_eeprom_data(ELINK_ID_PORT, EEPROM_INT_CONFIG_1);
241                         if (data & ICW1_IAS_PNP) {
242                                 if (bootverbose) {
243                                         device_printf(parent, "if_ep: <%s> at 0x%03x in PnP mode!\n",
244                                                       desc, ioport);
245                                 }
246                                 /* Set the adaptor tag so that the next card can be found. */
247                                 outb(ELINK_ID_PORT, tag--);
248                                 continue;
249                         }
250                 }
251
252                 /* Set the adaptor tag so that the next card can be found. */
253                 outb(ELINK_ID_PORT, tag--);
254
255                 /* Activate the adaptor at the EEPROM location. */
256                 outb(ELINK_ID_PORT, ACTIVATE_ADAPTER_TO_CONFIG);
257
258                 /* Test for an adapter in TEST mode. */
259                 outw(ioport + EP_COMMAND, WINDOW_SELECT | 0);
260                 data = inw(ioport + EP_W0_EEPROM_COMMAND);
261                 if (data & EEPROM_TST_MODE) {
262                         device_printf(parent, "if_ep: <%s> at port 0x%03x in TEST mode!  Erase pencil mark.\n",
263                                         desc, ioport);
264                         continue;
265                 }
266
267                 child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "ep", -1);
268                 device_set_desc_copy(child, desc);
269                 device_set_driver(child, driver);
270                 bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
271                 bus_set_resource(child, SYS_RES_IOPORT, 0, ioport, EP_IOSIZE);
272
273                 if (bootverbose) {
274                         device_printf(parent, "if_ep: <%s> at port 0x%03x-0x%03x irq %d\n",
275                                         desc, ioport, ioport + EP_IOSIZE, irq);
276                 }
277
278                 found++;
279         }
280
281         return;
282 }
283
284 static int
285 ep_isa_probe (device_t dev)
286 {
287         int     error = 0;
288
289         /* Check isapnp ids */
290         error = ISA_PNP_PROBE(device_get_parent(dev), dev, ep_ids);
291
292         /* If the card had a PnP ID that didn't match any we know about */
293         if (error == ENXIO) {
294                return (error);
295         }
296
297         /* If we had some other problem. */
298         if (!(error == 0 || error == ENOENT)) {
299                 return (error);
300         }
301
302         /* If we have the resources we need then we're good to go. */
303         if ((bus_get_resource_start(dev, SYS_RES_IOPORT, 0) != 0) &&
304             (bus_get_resource_start(dev, SYS_RES_IRQ, 0) != 0)) {
305                 return (0);
306         }
307
308         return (ENXIO);
309 }
310
311 static int
312 ep_isa_attach (device_t dev)
313 {
314         struct ep_softc *       sc = device_get_softc(dev);
315         int                     error = 0;
316
317         if ((error = ep_alloc(dev))) {
318                 device_printf(dev, "ep_alloc() failed! (%d)\n", error);
319                 goto bad;
320         }
321
322         ep_get_media(sc);
323
324         GO_WINDOW(0);
325         SET_IRQ(BASE, rman_get_start(sc->irq));
326
327         if ((error = ep_attach(sc))) {
328                 device_printf(dev, "ep_attach() failed! (%d)\n", error);
329                 goto bad;
330         }
331
332         if ((error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, ep_intr,
333                                    sc, &sc->ep_intrhand))) {
334                 device_printf(dev, "bus_setup_intr() failed! (%d)\n", error);
335                 goto bad;
336         }
337
338         return (0);
339 bad:
340         ep_free(dev);
341         return (error);
342 }
343
344 static device_method_t ep_isa_methods[] = {
345         /* Device interface */
346         DEVMETHOD(device_identify,      ep_isa_identify),
347         DEVMETHOD(device_probe,         ep_isa_probe),
348         DEVMETHOD(device_attach,        ep_isa_attach),
349
350         { 0, 0 }
351 };
352
353 static driver_t ep_isa_driver = {
354         "ep",
355         ep_isa_methods,
356         sizeof(struct ep_softc),
357 };
358
359 extern devclass_t ep_devclass;
360
361 DRIVER_MODULE(ep, isa, ep_isa_driver, ep_devclass, 0, 0);