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