1 /* $NetBSD: we.c,v 1.14 2008/03/12 14:31:11 cube Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet 42 * adapters. 43 * 44 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved. 45 * 46 * Copyright (C) 1993, David Greenman. This software may be used, modified, 47 * copied, distributed, and sold, in both source and binary form provided that 48 * the above copyright and these terms are retained. Under no circumstances is 49 * the author responsible for the proper functioning of this software, nor does 50 * the author assume any responsibility for damages incurred with its use. 51 */ 52 53 /* 54 * Device driver for the Western Digital/SMC 8003 and 8013 series, 55 * and the SMC Elite Ultra (8216). 56 */ 57 58 #include <sys/cdefs.h> 59 __KERNEL_RCSID(0, "$NetBSD: we.c,v 1.14 2008/03/12 14:31:11 cube Exp $"); 60 61 #include <sys/param.h> 62 #include <sys/systm.h> 63 #include <sys/device.h> 64 #include <sys/socket.h> 65 #include <sys/mbuf.h> 66 #include <sys/syslog.h> 67 68 #include <net/if.h> 69 #include <net/if_dl.h> 70 #include <net/if_types.h> 71 #include <net/if_media.h> 72 73 #include <net/if_ether.h> 74 75 #include <sys/bus.h> 76 #include <sys/bswap.h> 77 #include <sys/intr.h> 78 79 #include <dev/isa/isareg.h> 80 #include <dev/isa/isavar.h> 81 82 #include <dev/ic/dp8390reg.h> 83 #include <dev/ic/dp8390var.h> 84 #include <dev/ic/wereg.h> 85 #include <dev/ic/wevar.h> 86 87 #ifndef __BUS_SPACE_HAS_STREAM_METHODS 88 #define bus_space_read_region_stream_2 bus_space_read_region_2 89 #define bus_space_write_stream_2 bus_space_write_2 90 #define bus_space_write_region_stream_2 bus_space_write_region_2 91 #endif 92 93 static void we_set_media(struct we_softc *, int); 94 95 static void we_media_init(struct dp8390_softc *); 96 97 static int we_mediachange(struct dp8390_softc *); 98 static void we_mediastatus(struct dp8390_softc *, struct ifmediareq *); 99 100 static void we_recv_int(struct dp8390_softc *); 101 static void we_init_card(struct dp8390_softc *); 102 static int we_write_mbuf(struct dp8390_softc *, struct mbuf *, int); 103 static int we_ring_copy(struct dp8390_softc *, int, void *, u_short); 104 static void we_read_hdr(struct dp8390_softc *, int, struct dp8390_ring *); 105 static int we_test_mem(struct dp8390_softc *); 106 107 static inline void we_readmem(struct we_softc *, int, u_int8_t *, int); 108 109 /* 110 * Delay needed when switching 16-bit access to shared memory. 111 */ 112 #define WE_DELAY(wsc) delay(3) 113 114 /* 115 * Enable card RAM, and 16-bit access. 116 */ 117 #define WE_MEM_ENABLE(wsc) \ 118 if (((wsc)->sc_flags & WE_16BIT_NOTOGGLE) == 0) { \ 119 if ((wsc)->sc_flags & WE_16BIT_ENABLE) \ 120 bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \ 121 WE_LAAR, (wsc)->sc_laar_proto | WE_LAAR_M16EN); \ 122 bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \ 123 WE_MSR, wsc->sc_msr_proto | WE_MSR_MENB); \ 124 WE_DELAY((wsc)); \ 125 } 126 127 /* 128 * Disable card RAM, and 16-bit access. 129 */ 130 #define WE_MEM_DISABLE(wsc) \ 131 if (((wsc)->sc_flags & WE_16BIT_NOTOGGLE) == 0) { \ 132 bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \ 133 WE_MSR, (wsc)->sc_msr_proto); \ 134 if ((wsc)->sc_flags & WE_16BIT_ENABLE) \ 135 bus_space_write_1((wsc)->sc_asict, (wsc)->sc_asich, \ 136 WE_LAAR, (wsc)->sc_laar_proto); \ 137 WE_DELAY((wsc)); \ 138 } 139 140 int 141 we_config(device_t self, struct we_softc *wsc, const char *typestr) 142 { 143 struct dp8390_softc *sc = &wsc->sc_dp8390; 144 u_int8_t x; 145 int i, forced_16bit = 0; 146 147 /* 148 * Allow user to override 16-bit mode. 8-bit takes precedence. 149 */ 150 if (device_cfdata(self)->cf_flags & DP8390_FORCE_16BIT_MODE) { 151 wsc->sc_flags |= WE_16BIT_ENABLE; 152 forced_16bit = 1; 153 } 154 if (device_cfdata(self)->cf_flags & DP8390_FORCE_8BIT_MODE) 155 wsc->sc_flags &= ~WE_16BIT_ENABLE; 156 157 /* Registers are linear. */ 158 for (i = 0; i < 16; i++) 159 sc->sc_reg_map[i] = i; 160 161 /* Now we can use the NIC_{GET,PUT}() macros. */ 162 163 aprint_normal_dev(self, "%s Ethernet (%s-bit)\n", 164 typestr, wsc->sc_flags & WE_16BIT_ENABLE ? "16" : "8"); 165 166 /* Get station address from EEPROM. */ 167 for (i = 0; i < ETHER_ADDR_LEN; i++) 168 sc->sc_enaddr[i] = bus_space_read_1(wsc->sc_asict, 169 wsc->sc_asich, WE_PROM + i); 170 171 /* 172 * Set upper address bits and 8/16 bit access to shared memory. 173 */ 174 if (sc->is790) { 175 wsc->sc_laar_proto = 176 bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR) & 177 ~WE_LAAR_M16EN; 178 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR, 179 wsc->sc_laar_proto | (wsc->sc_flags & WE_16BIT_ENABLE ? WE_LAAR_M16EN : 0)); 180 } else if ((wsc->sc_type & WE_SOFTCONFIG) || 181 #ifdef TOSH_ETHER 182 (wsc->sc_type == WE_TYPE_TOSHIBA1) || 183 (wsc->sc_type == WE_TYPE_TOSHIBA4) || 184 #endif 185 (forced_16bit) || 186 (wsc->sc_type == WE_TYPE_WD8013EBT)) { 187 wsc->sc_laar_proto = (wsc->sc_maddr >> 19) & WE_LAAR_ADDRHI; 188 if (wsc->sc_flags & WE_16BIT_ENABLE) 189 wsc->sc_laar_proto |= WE_LAAR_L16EN; 190 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_LAAR, 191 wsc->sc_laar_proto | (wsc->sc_flags & WE_16BIT_ENABLE ? WE_LAAR_M16EN : 0)); 192 } 193 194 /* 195 * Set address and enable interface shared memory. 196 */ 197 if (sc->is790) { 198 /* XXX MAGIC CONSTANTS XXX */ 199 x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, 0x04); 200 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 0x04, x | 0x80); 201 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 0x0b, 202 ((wsc->sc_maddr >> 13) & 0x0f) | 203 ((wsc->sc_maddr >> 11) & 0x40) | 204 (bus_space_read_1(wsc->sc_asict, wsc->sc_asich, 0x0b) & 0xb0)); 205 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 0x04, x); 206 wsc->sc_msr_proto = 0x00; 207 sc->cr_proto = 0x00; 208 } else { 209 #ifdef TOSH_ETHER 210 if (wsc->sc_type == WE_TYPE_TOSHIBA1 || 211 wsc->sc_type == WE_TYPE_TOSHIBA4) { 212 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 213 WE_MSR + 1, 214 ((wsc->sc_maddr >> 8) & 0xe0) | 0x04); 215 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, 216 WE_MSR + 2, 217 ((wsc->sc_maddr >> 16) & 0x0f)); 218 wsc->sc_msr_proto = WE_MSR_POW; 219 } else 220 #endif 221 wsc->sc_msr_proto = (wsc->sc_maddr >> 13) & 222 WE_MSR_ADDR; 223 224 sc->cr_proto = ED_CR_RD2; 225 } 226 227 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_MSR, 228 wsc->sc_msr_proto | WE_MSR_MENB); 229 WE_DELAY(wsc); 230 231 /* 232 * DCR gets: 233 * 234 * FIFO threshold to 8, No auto-init Remote DMA, 235 * byte order=80x86. 236 * 237 * 16-bit cards also get word-wide DMA transfers. 238 */ 239 sc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | 240 (wsc->sc_flags & WE_16BIT_ENABLE ? ED_DCR_WTS : 0); 241 242 sc->test_mem = we_test_mem; 243 sc->ring_copy = we_ring_copy; 244 sc->write_mbuf = we_write_mbuf; 245 sc->read_hdr = we_read_hdr; 246 sc->recv_int = we_recv_int; 247 sc->init_card = we_init_card; 248 249 sc->sc_mediachange = we_mediachange; 250 sc->sc_mediastatus = we_mediastatus; 251 252 sc->mem_start = 0; 253 /* sc->mem_size has to be set by frontend */ 254 255 sc->sc_flags = device_cfdata(self)->cf_flags; 256 257 /* Do generic parts of attach. */ 258 if (wsc->sc_type & WE_SOFTCONFIG) 259 sc->sc_media_init = we_media_init; 260 else 261 sc->sc_media_init = dp8390_media_init; 262 if (dp8390_config(sc)) { 263 aprint_error_dev(self, "configuration failed\n"); 264 return (1); 265 } 266 267 /* 268 * Disable 16-bit access to shared memory - we leave it disabled 269 * so that: 270 * 271 * (1) machines reboot properly when the board is set to 272 * 16-bit mode and there are conflicting 8-bit devices 273 * within the same 128k address space as this board's 274 * shared memory, and 275 * 276 * (2) so that other 8-bit devices with shared memory 277 * in this same 128k address space will work. 278 */ 279 WE_MEM_DISABLE(wsc); 280 281 return (0); 282 } 283 284 static int 285 we_test_mem(struct dp8390_softc *sc) 286 { 287 struct we_softc *wsc = (struct we_softc *)sc; 288 bus_space_tag_t memt = sc->sc_buft; 289 bus_space_handle_t memh = sc->sc_bufh; 290 bus_size_t memsize = sc->mem_size; 291 int i; 292 293 if (wsc->sc_flags & WE_16BIT_ENABLE) 294 bus_space_set_region_2(memt, memh, 0, 0, memsize >> 1); 295 else 296 bus_space_set_region_1(memt, memh, 0, 0, memsize); 297 298 if (wsc->sc_flags & WE_16BIT_ENABLE) { 299 for (i = 0; i < memsize; i += 2) { 300 if (bus_space_read_2(memt, memh, i) != 0) 301 goto fail; 302 } 303 } else { 304 for (i = 0; i < memsize; i++) { 305 if (bus_space_read_1(memt, memh, i) != 0) 306 goto fail; 307 } 308 } 309 310 return (0); 311 312 fail: 313 aprint_error_dev(sc->sc_dev, 314 "failed to clear shared memory at offset 0x%x\n", i); 315 WE_MEM_DISABLE(wsc); 316 return (1); 317 } 318 319 /* 320 * Given a NIC memory source address and a host memory destination address, 321 * copy 'len' from NIC to host using shared memory. The 'len' is rounded 322 * up to a word - ok as long as mbufs are word-sized. 323 */ 324 static inline void 325 we_readmem(struct we_softc *wsc, int from, u_int8_t *to, int len) 326 { 327 bus_space_tag_t memt = wsc->sc_dp8390.sc_buft; 328 bus_space_handle_t memh = wsc->sc_dp8390.sc_bufh; 329 330 if (len & 1) 331 ++len; 332 333 if (wsc->sc_flags & WE_16BIT_ENABLE) 334 bus_space_read_region_stream_2(memt, memh, from, 335 (u_int16_t *)to, len >> 1); 336 else 337 bus_space_read_region_1(memt, memh, from, 338 to, len); 339 } 340 341 static int 342 we_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf) 343 { 344 struct we_softc *wsc = (struct we_softc *)sc; 345 bus_space_tag_t memt = wsc->sc_dp8390.sc_buft; 346 bus_space_handle_t memh = wsc->sc_dp8390.sc_bufh; 347 u_int8_t *data, savebyte[2]; 348 int savelen, len, leftover; 349 #ifdef DIAGNOSTIC 350 u_int8_t *lim; 351 #endif 352 353 savelen = m->m_pkthdr.len; 354 355 WE_MEM_ENABLE(wsc); 356 357 /* 358 * 8-bit boards are simple; no alignment tricks are necessary. 359 */ 360 if ((wsc->sc_flags & WE_16BIT_ENABLE) == 0) { 361 for (; m != NULL; buf += m->m_len, m = m->m_next) 362 bus_space_write_region_1(memt, memh, 363 buf, mtod(m, u_int8_t *), m->m_len); 364 if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) { 365 bus_space_set_region_1(memt, memh, 366 buf, 0, ETHER_MIN_LEN - ETHER_CRC_LEN - savelen); 367 savelen = ETHER_MIN_LEN - ETHER_CRC_LEN; 368 } 369 goto out; 370 } 371 372 /* Start out with no leftover data. */ 373 leftover = 0; 374 savebyte[0] = savebyte[1] = 0; 375 376 for (; m != NULL; m = m->m_next) { 377 len = m->m_len; 378 if (len == 0) 379 continue; 380 data = mtod(m, u_int8_t *); 381 #ifdef DIAGNOSTIC 382 lim = data + len; 383 #endif 384 while (len > 0) { 385 if (leftover) { 386 /* 387 * Data left over (from mbuf or realignment). 388 * Buffer the next byte, and write it and 389 * the leftover data out. 390 */ 391 savebyte[1] = *data++; 392 len--; 393 bus_space_write_stream_2(memt, memh, buf, 394 *(u_int16_t *)savebyte); 395 buf += 2; 396 leftover = 0; 397 } else if (BUS_SPACE_ALIGNED_POINTER(data, u_int16_t) 398 == 0) { 399 /* 400 * Unaligned dta; buffer the next byte. 401 */ 402 savebyte[0] = *data++; 403 len--; 404 leftover = 1; 405 } else { 406 /* 407 * Aligned data; output contiguous words as 408 * much as we can, then buffer the remaining 409 * byte, if any. 410 */ 411 leftover = len & 1; 412 len &= ~1; 413 bus_space_write_region_stream_2(memt, memh, 414 buf, (u_int16_t *)data, len >> 1); 415 data += len; 416 buf += len; 417 if (leftover) 418 savebyte[0] = *data++; 419 len = 0; 420 } 421 } 422 if (len < 0) 423 panic("we_write_mbuf: negative len"); 424 #ifdef DIAGNOSTIC 425 if (data != lim) 426 panic("we_write_mbuf: data != lim"); 427 #endif 428 } 429 if (leftover) { 430 savebyte[1] = 0; 431 bus_space_write_stream_2(memt, memh, buf, 432 *(u_int16_t *)savebyte); 433 buf += 2; 434 } 435 if (savelen < ETHER_MIN_LEN - ETHER_CRC_LEN) { 436 bus_space_set_region_2(memt, memh, 437 buf, 0, (ETHER_MIN_LEN - ETHER_CRC_LEN - savelen) >> 1); 438 savelen = ETHER_MIN_LEN - ETHER_CRC_LEN; 439 } 440 441 out: 442 WE_MEM_DISABLE(wsc); 443 444 return (savelen); 445 } 446 447 static int 448 we_ring_copy(struct dp8390_softc *sc, int src, void *dstv, u_short amount) 449 { 450 char *dst = dstv; 451 struct we_softc *wsc = (struct we_softc *)sc; 452 u_short tmp_amount; 453 454 /* Does copy wrap to lower addr in ring buffer? */ 455 if (src + amount > sc->mem_end) { 456 tmp_amount = sc->mem_end - src; 457 458 /* Copy amount up to end of NIC memory. */ 459 we_readmem(wsc, src, dst, tmp_amount); 460 461 amount -= tmp_amount; 462 src = sc->mem_ring; 463 dst += tmp_amount; 464 } 465 466 we_readmem(wsc, src, dst, amount); 467 468 return (src + amount); 469 } 470 471 static void 472 we_read_hdr(struct dp8390_softc *sc, int packet_ptr, 473 struct dp8390_ring *packet_hdrp) 474 { 475 struct we_softc *wsc = (struct we_softc *)sc; 476 477 we_readmem(wsc, packet_ptr, (u_int8_t *)packet_hdrp, 478 sizeof(struct dp8390_ring)); 479 #if BYTE_ORDER == BIG_ENDIAN 480 packet_hdrp->count = bswap16(packet_hdrp->count); 481 #endif 482 } 483 484 static void 485 we_recv_int(struct dp8390_softc *sc) 486 { 487 struct we_softc *wsc = (struct we_softc *)sc; 488 489 WE_MEM_ENABLE(wsc); 490 dp8390_rint(sc); 491 WE_MEM_DISABLE(wsc); 492 } 493 494 static void 495 we_media_init(struct dp8390_softc *sc) 496 { 497 struct we_softc *wsc = (void *) sc; 498 int defmedia = IFM_ETHER; 499 u_int8_t x; 500 501 if (sc->is790) { 502 x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR); 503 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR, 504 x | WE790_HWR_SWH); 505 if (bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE790_GCR) & 506 WE790_GCR_GPOUT) 507 defmedia |= IFM_10_2; 508 else 509 defmedia |= IFM_10_5; 510 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE790_HWR, 511 x & ~WE790_HWR_SWH); 512 } else { 513 x = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR); 514 if (x & WE_IRR_OUT2) 515 defmedia |= IFM_10_2; 516 else 517 defmedia |= IFM_10_5; 518 } 519 520 ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus); 521 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_2, 0, NULL); 522 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_5, 0, NULL); 523 ifmedia_set(&sc->sc_media, defmedia); 524 } 525 526 static int 527 we_mediachange(struct dp8390_softc *sc) 528 { 529 530 /* 531 * Current media is already set up. Just reset the interface 532 * to let the new value take hold. The new media will be 533 * set up in we_init_card() called via dp8390_init(). 534 */ 535 dp8390_reset(sc); 536 return (0); 537 } 538 539 static void 540 we_mediastatus(struct dp8390_softc *sc, struct ifmediareq *ifmr) 541 { 542 struct ifmedia *ifm = &sc->sc_media; 543 544 /* 545 * The currently selected media is always the active media. 546 */ 547 ifmr->ifm_active = ifm->ifm_cur->ifm_media; 548 } 549 550 static void 551 we_init_card(struct dp8390_softc *sc) 552 { 553 struct we_softc *wsc = (struct we_softc *)sc; 554 struct ifmedia *ifm = &sc->sc_media; 555 556 if (wsc->sc_init_hook) 557 (*wsc->sc_init_hook)(wsc); 558 559 we_set_media(wsc, ifm->ifm_cur->ifm_media); 560 } 561 562 static void 563 we_set_media(struct we_softc *wsc, int media) 564 { 565 struct dp8390_softc *sc = &wsc->sc_dp8390; 566 bus_space_tag_t asict = wsc->sc_asict; 567 bus_space_handle_t asich = wsc->sc_asich; 568 u_int8_t hwr, gcr, irr; 569 570 if (sc->is790) { 571 hwr = bus_space_read_1(asict, asich, WE790_HWR); 572 bus_space_write_1(asict, asich, WE790_HWR, 573 hwr | WE790_HWR_SWH); 574 gcr = bus_space_read_1(asict, asich, WE790_GCR); 575 if (IFM_SUBTYPE(media) == IFM_10_2) 576 gcr |= WE790_GCR_GPOUT; 577 else 578 gcr &= ~WE790_GCR_GPOUT; 579 bus_space_write_1(asict, asich, WE790_GCR, 580 gcr | WE790_GCR_LIT); 581 bus_space_write_1(asict, asich, WE790_HWR, 582 hwr & ~WE790_HWR_SWH); 583 return; 584 } 585 586 irr = bus_space_read_1(wsc->sc_asict, wsc->sc_asich, WE_IRR); 587 if (IFM_SUBTYPE(media) == IFM_10_2) 588 irr |= WE_IRR_OUT2; 589 else 590 irr &= ~WE_IRR_OUT2; 591 bus_space_write_1(wsc->sc_asict, wsc->sc_asich, WE_IRR, irr); 592 } 593