1 /* $NetBSD: if_ate.c,v 1.27 2001/07/18 20:52:48 thorpej Exp $ */ 2 3 /* 4 * All Rights Reserved, Copyright (C) Fujitsu Limited 1995 5 * 6 * This software may be used, modified, copied, distributed, and sold, in 7 * both source and binary form provided that the above copyright, these 8 * terms and the following disclaimer are retained. The name of the author 9 * and/or the contributor may not be used to endorse or promote products 10 * derived from this software without specific prior written permission. 11 * 12 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND THE CONTRIBUTOR ``AS IS'' AND 13 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR THE CONTRIBUTOR BE LIABLE 16 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 18 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION. 19 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 20 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 22 * SUCH DAMAGE. 23 */ 24 25 /* 26 * Portions copyright (C) 1993, David Greenman. This software may be used, 27 * modified, copied, distributed, and sold, in both source and binary form 28 * provided that the above copyright and these terms are retained. Under no 29 * circumstances is the author responsible for the proper functioning of this 30 * software, nor does the author assume any responsibility for damages 31 * incurred with its use. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/device.h> 37 #include <sys/socket.h> 38 #include <sys/syslog.h> 39 40 #include <net/if.h> 41 #include <net/if_ether.h> 42 #include <net/if_media.h> 43 44 #include <machine/bus.h> 45 #include <machine/intr.h> 46 47 #include <dev/ic/mb86960reg.h> 48 #include <dev/ic/mb86960var.h> 49 #include <dev/ic/ate_subr.h> 50 51 #include <dev/isa/isavar.h> 52 #include <dev/isa/if_fereg.h> /* XXX */ 53 54 int ate_match __P((struct device *, struct cfdata *, void *)); 55 void ate_attach __P((struct device *, struct device *, void *)); 56 57 struct ate_softc { 58 struct mb86960_softc sc_mb86960; /* real "mb86960" softc */ 59 60 /* ISA-specific goo. */ 61 void *sc_ih; /* interrupt cookie */ 62 }; 63 64 struct cfattach ate_isa_ca = { 65 sizeof(struct ate_softc), ate_match, ate_attach 66 }; 67 68 #if NetBSD <= 199712 69 struct cfdriver ate_isa_cd = { 70 NULL, "ate", DV_IFNET 71 }; 72 #endif 73 74 struct fe_simple_probe_struct { 75 u_char port; /* Offset from the base I/O address. */ 76 u_char mask; /* Bits to be checked. */ 77 u_char bits; /* Values to be compared against. */ 78 }; 79 80 static __inline__ int fe_simple_probe __P((bus_space_tag_t, 81 bus_space_handle_t, struct fe_simple_probe_struct const *)); 82 static int ate_find __P((bus_space_tag_t, bus_space_handle_t, int *, 83 int *)); 84 static int ate_detect __P((bus_space_tag_t, bus_space_handle_t, 85 u_int8_t enaddr[ETHER_ADDR_LEN])); 86 87 static int const ate_iomap[8] = { 88 0x260, 0x280, 0x2A0, 0x240, 0x340, 0x320, 0x380, 0x300 89 }; 90 #define NATE_IOMAP (sizeof (ate_iomap) / sizeof (ate_iomap[0])) 91 #define ATE_NPORTS 0x20 92 93 /* 94 * Hardware probe routines. 95 */ 96 97 /* 98 * Determine if the device is present. 99 */ 100 int 101 ate_match(parent, match, aux) 102 struct device *parent; 103 struct cfdata *match; 104 void *aux; 105 { 106 struct isa_attach_args *ia = aux; 107 bus_space_tag_t iot = ia->ia_iot; 108 bus_space_handle_t ioh; 109 int i, iobase, irq, rv = 0; 110 u_int8_t myea[ETHER_ADDR_LEN]; 111 112 /* Disallow wildcarded values. */ 113 if (ia->ia_iobase == ISACF_PORT_DEFAULT) 114 return (0); 115 116 /* 117 * See if the sepcified address is valid for MB86965A JLI mode. 118 */ 119 for (i = 0; i < NATE_IOMAP; i++) 120 if (ate_iomap[i] == ia->ia_iobase) 121 break; 122 if (i == NATE_IOMAP) { 123 #ifdef ATE_DEBUG 124 printf("ate_match: unknown iobase 0x%x\n", ia->ia_iobase); 125 #endif 126 return (0); 127 } 128 129 /* Map i/o space. */ 130 if (bus_space_map(iot, ia->ia_iobase, ATE_NPORTS, 0, &ioh)) { 131 #ifdef ATE_DEBUG 132 printf("ate_match: couldn't map iospace 0x%x\n", 133 ia->ia_iobase); 134 #endif 135 return (0); 136 } 137 138 if (ate_find(iot, ioh, &iobase, &irq) == 0) { 139 #ifdef ATE_DEBUG 140 printf("ate_match: ate_find failed\n"); 141 #endif 142 goto out; 143 } 144 145 if (iobase != ia->ia_iobase) { 146 #ifdef ATE_DEBUG 147 printf("ate_match: unexpected iobase in board: 0x%x\n", 148 ia->ia_iobase); 149 #endif 150 goto out; 151 } 152 153 if (ate_detect(iot, ioh, myea) == 0) { /* XXX necessary? */ 154 #ifdef ATE_DEBUG 155 printf("ate_match: ate_detect failed\n"); 156 #endif 157 goto out; 158 } 159 160 if (ia->ia_irq != ISACF_IRQ_DEFAULT) { 161 if (ia->ia_irq != irq) { 162 printf("ate_match: irq mismatch; " 163 "kernel configured %d != board configured %d\n", 164 ia->ia_irq, irq); 165 goto out; 166 } 167 } else 168 ia->ia_irq = irq; 169 170 ia->ia_iosize = ATE_NPORTS; 171 ia->ia_msize = 0; 172 rv = 1; 173 174 out: 175 bus_space_unmap(iot, ioh, ATE_NPORTS); 176 return (rv); 177 } 178 179 /* 180 * Check for specific bits in specific registers have specific values. 181 */ 182 static __inline__ int 183 fe_simple_probe (iot, ioh, sp) 184 bus_space_tag_t iot; 185 bus_space_handle_t ioh; 186 struct fe_simple_probe_struct const *sp; 187 { 188 u_int8_t val; 189 struct fe_simple_probe_struct const *p; 190 191 for (p = sp; p->mask != 0; p++) { 192 val = bus_space_read_1(iot, ioh, p->port); 193 if ((val & p->mask) != p->bits) { 194 #ifdef ATE_DEBUG 195 printf("fe_simple_probe: %x & %x != %x\n", 196 val, p->mask, p->bits); 197 #endif 198 return (0); 199 } 200 } 201 202 return (1); 203 } 204 205 /* 206 * Hardware (vendor) specific probe routines. 207 */ 208 209 /* 210 * Probe and initialization for Allied-Telesis AT1700/RE2000 series. 211 */ 212 static int 213 ate_find(iot, ioh, iobase, irq) 214 bus_space_tag_t iot; 215 bus_space_handle_t ioh; 216 int *iobase, *irq; 217 { 218 u_char eeprom[FE_EEPROM_SIZE]; 219 int n; 220 221 static int const irqmap[4][4] = { 222 { 3, 4, 5, 9 }, 223 { 10, 11, 12, 15 }, 224 { 3, 11, 5, 15 }, 225 { 10, 11, 14, 15 }, 226 }; 227 static struct fe_simple_probe_struct const probe_table[] = { 228 { FE_DLCR2, 0x70, 0x00 }, 229 { FE_DLCR4, 0x08, 0x00 }, 230 { FE_DLCR5, 0x80, 0x00 }, 231 #if 0 232 { FE_BMPR16, 0x1B, 0x00 }, 233 { FE_BMPR17, 0x7F, 0x00 }, 234 #endif 235 { 0 } 236 }; 237 238 #if ATE_DEBUG >= 4 239 log(LOG_INFO, "ate_find: probe (0x%x) for ATI\n", iobase); 240 #if 0 241 fe_dump(LOG_INFO, sc); 242 #endif 243 #endif 244 245 /* 246 * We should test if MB86965A is on the base address now. 247 * Unfortunately, it is very hard to probe it reliably, since 248 * we have no way to reset the chip under software control. 249 * On cold boot, we could check the "signature" bit patterns 250 * described in the Fujitsu document. On warm boot, however, 251 * we can predict almost nothing about register values. 252 */ 253 if (!fe_simple_probe(iot, ioh, probe_table)) 254 return (0); 255 256 /* Check if our I/O address matches config info on 86965. */ 257 n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_ADDR) 258 >> FE_B19_ADDR_SHIFT; 259 *iobase = ate_iomap[n]; 260 261 /* 262 * We are now almost sure we have an AT1700 at the given 263 * address. So, read EEPROM through 86965. We have to write 264 * into LSI registers to read from EEPROM. I want to avoid it 265 * at this stage, but I cannot test the presense of the chip 266 * any further without reading EEPROM. FIXME. 267 */ 268 ate_read_eeprom(iot, ioh, eeprom); 269 270 /* Make sure that config info in EEPROM and 86965 agree. */ 271 if (eeprom[FE_EEPROM_CONF] != bus_space_read_1(iot, ioh, FE_BMPR19)) { 272 #ifdef DIAGNOSTIC 273 printf("ate_find: " 274 "incorrect configration in eeprom and chip\n"); 275 #endif 276 return (0); 277 } 278 279 /* 280 * Try to determine IRQ settings. 281 * Different models use different ranges of IRQs. 282 */ 283 n = (bus_space_read_1(iot, ioh, FE_BMPR19) & FE_B19_IRQ) 284 >> FE_B19_IRQ_SHIFT; 285 switch (eeprom[FE_ATI_EEP_REVISION] & 0xf0) { 286 case 0x30: 287 *irq = irqmap[3][n]; 288 break; 289 case 0x10: 290 case 0x50: 291 *irq = irqmap[2][n]; 292 break; 293 case 0x40: 294 case 0x60: 295 if (eeprom[FE_ATI_EEP_MAGIC] & 0x04) { 296 *irq = irqmap[1][n]; 297 break; 298 } 299 default: 300 *irq = irqmap[0][n]; 301 break; 302 } 303 304 return (1); 305 } 306 307 /* 308 * Determine type and ethernet address. 309 */ 310 static int 311 ate_detect(iot, ioh, enaddr) 312 bus_space_tag_t iot; 313 bus_space_handle_t ioh; 314 u_int8_t enaddr[ETHER_ADDR_LEN]; 315 { 316 u_char eeprom[FE_EEPROM_SIZE]; 317 int type; 318 319 /* Get our station address from EEPROM. */ 320 ate_read_eeprom(iot, ioh, eeprom); 321 memcpy(enaddr, eeprom + FE_ATI_EEP_ADDR, ETHER_ADDR_LEN); 322 323 /* Make sure we got a valid station address. */ 324 if ((enaddr[0] & 0x03) != 0x00 || 325 (enaddr[0] == 0x00 && enaddr[1] == 0x00 && enaddr[2] == 0x00)) { 326 #ifdef ATE_DEBUG 327 printf("fmv_detect: invalid ethernet address\n"); 328 #endif 329 return (0); 330 } 331 332 /* 333 * Determine the card type. 334 */ 335 switch (eeprom[FE_ATI_EEP_MODEL]) { 336 case FE_ATI_MODEL_AT1700T: 337 type = FE_TYPE_AT1700T; 338 break; 339 case FE_ATI_MODEL_AT1700BT: 340 type = FE_TYPE_AT1700BT; 341 break; 342 case FE_ATI_MODEL_AT1700FT: 343 type = FE_TYPE_AT1700FT; 344 break; 345 case FE_ATI_MODEL_AT1700AT: 346 type = FE_TYPE_AT1700AT; 347 break; 348 default: 349 type = FE_TYPE_RE2000; 350 break; 351 } 352 353 return (type); 354 } 355 356 void 357 ate_attach(parent, self, aux) 358 struct device *parent, *self; 359 void *aux; 360 { 361 struct ate_softc *isc = (struct ate_softc *)self; 362 struct mb86960_softc *sc = &isc->sc_mb86960; 363 struct isa_attach_args *ia = aux; 364 bus_space_tag_t iot = ia->ia_iot; 365 bus_space_handle_t ioh; 366 u_int8_t myea[ETHER_ADDR_LEN]; 367 const char *typestr; 368 int type; 369 370 printf("\n"); 371 372 /* Map i/o space. */ 373 if (bus_space_map(iot, ia->ia_iobase, ATE_NPORTS, 0, &ioh)) { 374 printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname); 375 return; 376 } 377 378 sc->sc_bst = iot; 379 sc->sc_bsh = ioh; 380 381 /* Determine the card type and get ethernet address. */ 382 type = ate_detect(iot, ioh, myea); 383 switch (type) { 384 case FE_TYPE_AT1700T: 385 typestr = "AT-1700T"; 386 break; 387 case FE_TYPE_AT1700BT: 388 typestr = "AT-1700BT"; 389 break; 390 case FE_TYPE_AT1700FT: 391 typestr = "AT-1700FT"; 392 break; 393 case FE_TYPE_AT1700AT: 394 typestr = "AT-1700AT"; 395 break; 396 case FE_TYPE_RE2000: 397 typestr = "unknown (RE-2000?)"; 398 break; 399 400 default: 401 /* Unknown card type: maybe a new model, but... */ 402 printf("%s: where did the card go?!\n", sc->sc_dev.dv_xname); 403 panic("unknown card"); 404 } 405 406 printf("%s: %s Ethernet\n", sc->sc_dev.dv_xname, typestr); 407 408 /* This interface is always enabled. */ 409 sc->sc_flags |= FE_FLAGS_ENABLED; 410 411 /* 412 * Do generic MB86960 attach. 413 */ 414 mb86960_attach(sc, MB86960_TYPE_86965, myea); 415 416 mb86960_config(sc, NULL, 0, 0); 417 418 /* Establish the interrupt handler. */ 419 isc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE, 420 IPL_NET, mb86960_intr, sc); 421 if (isc->sc_ih == NULL) 422 printf("%s: couldn't establish interrupt handler\n", 423 sc->sc_dev.dv_xname); 424 } 425