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