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