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