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