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