1 /* $OpenBSD: ne2000.c,v 1.14 2004/01/07 00:34:25 fgsch Exp $ */ 2 /* $NetBSD: ne2000.c,v 1.12 1998/06/10 01:15:50 thorpej Exp $ */ 3 4 /*- 5 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 6 * All rights reserved. 7 * 8 * This code is derived from software contributed to The NetBSD Foundation 9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 10 * NASA Ames Research Center. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the NetBSD 23 * Foundation, Inc. and its contributors. 24 * 4. Neither the name of The NetBSD Foundation nor the names of its 25 * contributors may be used to endorse or promote products derived 26 * from this software without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38 * POSSIBILITY OF SUCH DAMAGE. 39 */ 40 41 /* 42 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet 43 * adapters. 44 * 45 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved. 46 * 47 * Copyright (C) 1993, David Greenman. This software may be used, modified, 48 * copied, distributed, and sold, in both source and binary form provided that 49 * the above copyright and these terms are retained. Under no circumstances is 50 * the author responsible for the proper functioning of this software, nor does 51 * the author assume any responsibility for damages incurred with its use. 52 */ 53 54 /* 55 * Common code shared by all NE2000-compatible Ethernet interfaces. 56 */ 57 58 #include <sys/param.h> 59 #include <sys/systm.h> 60 #include <sys/device.h> 61 #include <sys/socket.h> 62 #include <sys/mbuf.h> 63 #include <sys/syslog.h> 64 65 #include <net/if.h> 66 #include <net/if_dl.h> 67 #include <net/if_types.h> 68 #include <net/if_media.h> 69 70 #ifdef __NetBSD__ 71 #include <net/if_ether.h> 72 #else 73 #include <netinet/in.h> 74 #include <netinet/if_ether.h> 75 #endif 76 77 #include <machine/bus.h> 78 79 #ifndef __BUS_SPACE_HAS_STREAM_METHODS 80 #define bus_space_write_stream_2 bus_space_write_2 81 #define bus_space_write_multi_stream_2 bus_space_write_multi_2 82 #define bus_space_read_multi_stream_2 bus_space_read_multi_2 83 #endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 84 85 #include <dev/ic/dp8390reg.h> 86 #include <dev/ic/dp8390var.h> 87 88 #include <dev/ic/ne2000reg.h> 89 #include <dev/ic/ne2000var.h> 90 91 int ne2000_write_mbuf(struct dp8390_softc *, struct mbuf *, int); 92 int ne2000_ring_copy(struct dp8390_softc *, int, caddr_t, u_short); 93 void ne2000_read_hdr(struct dp8390_softc *, int, struct dp8390_ring *); 94 int ne2000_test_mem(struct dp8390_softc *); 95 96 void ne2000_writemem(bus_space_tag_t, bus_space_handle_t, 97 bus_space_tag_t, bus_space_handle_t, u_int8_t *, int, size_t, int); 98 void ne2000_readmem(bus_space_tag_t, bus_space_handle_t, 99 bus_space_tag_t, bus_space_handle_t, int, u_int8_t *, size_t, int); 100 101 struct cfdriver ne_cd = { 102 NULL, "ne", DV_IFNET 103 }; 104 105 int 106 ne2000_attach(nsc, myea) 107 struct ne2000_softc *nsc; 108 u_int8_t *myea; 109 { 110 struct dp8390_softc *dsc = &nsc->sc_dp8390; 111 bus_space_tag_t nict = dsc->sc_regt; 112 bus_space_handle_t nich = dsc->sc_regh; 113 bus_space_tag_t asict = nsc->sc_asict; 114 bus_space_handle_t asich = nsc->sc_asich; 115 u_int8_t romdata[16]; 116 int memsize, i, useword; 117 118 /* 119 * Detect it again unless caller specified it; this gives us 120 * the memory size. 121 */ 122 if (nsc->sc_type == 0) { 123 nsc->sc_type = ne2000_detect(nsc); 124 if (nsc->sc_type == 0) { 125 printf("%s: where did the card go?\n", 126 dsc->sc_dev.dv_xname); 127 return (1); 128 } 129 } 130 131 useword = NE2000_USE_WORD(nsc); 132 133 dsc->cr_proto = ED_CR_RD2; 134 if (nsc->sc_type == NE2000_TYPE_AX88190) { 135 dsc->rcr_proto = ED_RCR_INTT; 136 dsc->sc_flags |= DP8390_DO_AX88190_WORKAROUND; 137 } else 138 dsc->rcr_proto = 0; 139 140 /* 141 * DCR gets: 142 * 143 * FIFO threshold to 8, No auto-init Remote DMA, 144 * byte order=80x86. 145 * 146 * NE1000 gets byte-wide DMA, NE2000 gets word-wide DMA. 147 */ 148 dsc->dcr_reg = ED_DCR_FT1 | ED_DCR_LS | (useword ? ED_DCR_WTS : 0); 149 150 dsc->test_mem = ne2000_test_mem; 151 dsc->ring_copy = ne2000_ring_copy; 152 dsc->write_mbuf = ne2000_write_mbuf; 153 dsc->read_hdr = ne2000_read_hdr; 154 155 /* Registers are linear. */ 156 for (i = 0; i < 16; i++) 157 dsc->sc_reg_map[i] = i; 158 159 /* 160 * 8k of memory for NE1000, 16k otherwise. 161 */ 162 switch (nsc->sc_type) { 163 case NE2000_TYPE_NE1000: 164 memsize = 8192; 165 break; 166 case NE2000_TYPE_NE2000: 167 case NE2000_TYPE_AX88190: /* XXX really? */ 168 case NE2000_TYPE_DL10019: 169 case NE2000_TYPE_DL10022: 170 memsize = 8192 * 2; 171 break; 172 } 173 174 /* 175 * NIC memory doens't start at zero on an NE board. 176 * The start address is tied to the bus width. 177 * (It happens to be computed the same way as mem size.) 178 */ 179 dsc->mem_start = memsize; 180 181 #ifdef GWETHER 182 { 183 int x, mstart = 0; 184 int8_t pbuf0[ED_PAGE_SIZE], pbuf[ED_PAGE_SIZE], 185 tbuf[ED_PAGE_SIZE]; 186 187 for (i = 0; i < ED_PAGE_SIZE; i++) 188 pbuf0[i] = 0; 189 190 /* Search for the start of RAM. */ 191 for (x = 1; x < 256; x++) { 192 ne2000_writemem(nict, nich, asict, asich, pbuf0, 193 x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword); 194 ne2000_readmem(nict, nich, asict, asich, 195 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword); 196 if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) { 197 for (i = 0; i < ED_PAGE_SIZE; i++) 198 pbuf[i] = 255 - x; 199 ne2000_writemem(nict, nich, asict, asich, 200 pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE, 201 useword); 202 ne2000_readmem(nict, nich, asict, asich, 203 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, 204 useword); 205 if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) { 206 mstart = x << ED_PAGE_SHIFT; 207 memsize = ED_PAGE_SIZE; 208 break; 209 } 210 } 211 } 212 213 if (mstart == 0) { 214 printf("%s: cannot find start of RAM\n", 215 dsc->sc_dev.dv_xname); 216 return; 217 } 218 219 /* Search for the end of RAM. */ 220 for (++x; x < 256; x++) { 221 ne2000_writemem(nict, nich, asict, asich, pbuf0, 222 x << ED_PAGE_SHIFT, ED_PAGE_SIZE, useword); 223 ne2000_readmem(nict, nich, asict, asich, 224 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, useword); 225 if (bcmp(pbuf0, tbuf, ED_PAGE_SIZE) == 0) { 226 for (i = 0; i < ED_PAGE_SIZE; i++) 227 pbuf[i] = 255 - x; 228 ne2000_writemem(nict, nich, asict, asich, 229 pbuf, x << ED_PAGE_SHIFT, ED_PAGE_SIZE, 230 useword); 231 ne2000_readmem(nict, nich, asict, asich, 232 x << ED_PAGE_SHIFT, tbuf, ED_PAGE_SIZE, 233 useword); 234 if (bcmp(pbuf, tbuf, ED_PAGE_SIZE) == 0) 235 memsize += ED_PAGE_SIZE; 236 else 237 break; 238 } else 239 break; 240 } 241 242 printf("%s: RAM start 0x%x, size %d\n", 243 dsc->sc_dev.dv_xname, mstart, memsize); 244 245 dsc->mem_start = mstart; 246 } 247 #endif /* GWETHER */ 248 249 dsc->mem_size = memsize; 250 251 if (myea == NULL) { 252 /* Read the station address. */ 253 if (nsc->sc_type == NE2000_TYPE_AX88190) { 254 /* Select page 0 registers. */ 255 bus_space_write_1(nict, nich, ED_P0_CR, 256 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA); 257 /* Select word transfer. */ 258 bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_WTS); 259 ne2000_readmem(nict, nich, asict, asich, 260 NE2000_AX88190_NODEID_OFFSET, 261 dsc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN, useword); 262 } else { 263 ne2000_readmem(nict, nich, asict, asich, 0, romdata, 264 sizeof(romdata), useword); 265 for (i = 0; i < ETHER_ADDR_LEN; i++) 266 #ifdef __NetBSD__ 267 dsc->sc_enaddr[i] = 268 romdata[i * (useword ? 2 : 1)]; 269 #else 270 dsc->sc_arpcom.ac_enaddr[i] = 271 romdata[i * (useword ? 2 : 1)]; 272 #endif 273 } 274 } else 275 bcopy(myea, dsc->sc_arpcom.ac_enaddr, ETHER_ADDR_LEN); 276 277 /* Clear any pending interrupts that might have occurred above. */ 278 bus_space_write_1(nict, nich, ED_P0_ISR, 0xff); 279 280 if (dsc->sc_media_init == NULL) 281 dsc->sc_media_init = dp8390_media_init; 282 283 if (dp8390_config(dsc)) { 284 printf("%s: setup failed\n", dsc->sc_dev.dv_xname); 285 return (1); 286 } 287 288 /* 289 * We need to compute mem_ring a bit differently; override the 290 * value set up in dp8390_config(). 291 */ 292 dsc->mem_ring = 293 dsc->mem_start + ((dsc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT); 294 295 return (0); 296 } 297 298 /* 299 * Detect an NE-2000 or compatible. Returns a model code. 300 */ 301 int 302 ne2000_detect(nsc) 303 struct ne2000_softc *nsc; 304 { 305 struct dp8390_softc *dsc = &nsc->sc_dp8390; 306 bus_space_tag_t nict = dsc->sc_regt; 307 bus_space_handle_t nich = dsc->sc_regh; 308 bus_space_tag_t asict = nsc->sc_asict; 309 bus_space_handle_t asich = nsc->sc_asich; 310 static u_int8_t test_pattern[32] = "THIS is A memory TEST pattern"; 311 u_int8_t test_buffer[32], tmp; 312 int i, rv = 0; 313 314 /* Reset the board. */ 315 #ifdef GWETHER 316 bus_space_write_1(asict, asich, NE2000_ASIC_RESET, 0); 317 delay(200); 318 #endif /* GWETHER */ 319 tmp = bus_space_read_1(asict, asich, NE2000_ASIC_RESET); 320 delay(10000); 321 322 /* 323 * I don't know if this is necessary; probably cruft leftover from 324 * Clarkson packet driver code. Doesn't do a thing on the boards I've 325 * tested. -DG [note that a outb(0x84, 0) seems to work here, and is 326 * non-invasive...but some boards don't seem to reset and I don't have 327 * complete documentation on what the 'right' thing to do is...so we do 328 * the invasive thing for now. Yuck.] 329 */ 330 bus_space_write_1(asict, asich, NE2000_ASIC_RESET, tmp); 331 delay(5000); 332 333 /* 334 * This is needed because some NE clones apparently don't reset the 335 * NIC properly (or the NIC chip doesn't reset fully on power-up). 336 * XXX - this makes the probe invasive! Done against my better 337 * judgement. -DLG 338 */ 339 bus_space_write_1(nict, nich, ED_P0_CR, 340 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STP); 341 342 delay(5000); 343 344 /* 345 * Generic probe routine for testing for the existence of a DS8390. 346 * Must be performed after the NIC has just been reset. This 347 * works by looking at certain register values that are guaranteed 348 * to be initialized a certain way after power-up or reset. 349 * 350 * Specifically: 351 * 352 * Register reset bits set bits 353 * -------- ---------- -------- 354 * CR TXP, STA RD2, STP 355 * ISR RST 356 * IMR <all> 357 * DCR LAS 358 * TCR LB1, LB0 359 * 360 * We only look at CR and ISR, however, since looking at the others 361 * would require changing register pages, which would be intrusive 362 * if this isn't an 8390. 363 */ 364 365 tmp = bus_space_read_1(nict, nich, ED_P0_CR); 366 if ((tmp & (ED_CR_RD2 | ED_CR_TXP | ED_CR_STA | ED_CR_STP)) != 367 (ED_CR_RD2 | ED_CR_STP)) 368 goto out; 369 370 tmp = bus_space_read_1(nict, nich, ED_P0_ISR); 371 if ((tmp & ED_ISR_RST) != ED_ISR_RST) 372 goto out; 373 374 bus_space_write_1(nict, nich, 375 ED_P0_CR, ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA); 376 377 for (i = 0; i < 100; i++) { 378 if ((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RST) == 379 ED_ISR_RST) { 380 /* Ack the reset bit. */ 381 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RST); 382 break; 383 } 384 delay(100); 385 } 386 387 #if 0 388 /* XXX */ 389 if (i == 100) 390 goto out; 391 #endif 392 393 /* 394 * Test the ability to read and write to the NIC memory. This has 395 * the side effect of determining if this is an NE1000 or an NE2000. 396 */ 397 398 /* 399 * This prevents packets from being stored in the NIC memory when 400 * the readmem routine turns on the start bit in the CR. 401 */ 402 bus_space_write_1(nict, nich, ED_P0_RCR, ED_RCR_MON); 403 404 /* Temporarily initialize DCR for byte operations. */ 405 bus_space_write_1(nict, nich, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS); 406 407 bus_space_write_1(nict, nich, ED_P0_PSTART, 8192 >> ED_PAGE_SHIFT); 408 bus_space_write_1(nict, nich, ED_P0_PSTOP, 16384 >> ED_PAGE_SHIFT); 409 410 /* 411 * Write a test pattern in byte mode. If this fails, then there 412 * probably isn't any memory at 8k - which likely means that the 413 * board is an NE2000. 414 */ 415 ne2000_writemem(nict, nich, asict, asich, test_pattern, 8192, 416 sizeof(test_pattern), 0); 417 ne2000_readmem(nict, nich, asict, asich, 8192, test_buffer, 418 sizeof(test_buffer), 0); 419 420 if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) { 421 /* not an NE1000 - try NE2000 */ 422 bus_space_write_1(nict, nich, ED_P0_DCR, 423 ED_DCR_WTS | ED_DCR_FT1 | ED_DCR_LS); 424 bus_space_write_1(nict, nich, ED_P0_PSTART, 425 16384 >> ED_PAGE_SHIFT); 426 bus_space_write_1(nict, nich, ED_P0_PSTOP, 427 32768 >> ED_PAGE_SHIFT); 428 429 /* 430 * Write the test pattern in word mode. If this also fails, 431 * then we don't know what this board is. 432 */ 433 ne2000_writemem(nict, nich, asict, asich, test_pattern, 16384, 434 sizeof(test_pattern), 1); 435 ne2000_readmem(nict, nich, asict, asich, 16384, test_buffer, 436 sizeof(test_buffer), 1); 437 438 if (bcmp(test_pattern, test_buffer, sizeof(test_pattern))) 439 goto out; /* not an NE2000 either */ 440 441 rv = NE2000_TYPE_NE2000; 442 } else { 443 /* We're an NE1000. */ 444 rv = NE2000_TYPE_NE1000; 445 } 446 447 /* Clear any pending interrupts that might have occurred above. */ 448 bus_space_write_1(nict, nich, ED_P0_ISR, 0xff); 449 450 out: 451 return (rv); 452 } 453 454 /* 455 * Write an mbuf chain to the destination NIC memory address using programmed 456 * I/O. 457 */ 458 int 459 ne2000_write_mbuf(sc, m, buf) 460 struct dp8390_softc *sc; 461 struct mbuf *m; 462 int buf; 463 { 464 struct ne2000_softc *nsc = (struct ne2000_softc *)sc; 465 bus_space_tag_t nict = sc->sc_regt; 466 bus_space_handle_t nich = sc->sc_regh; 467 bus_space_tag_t asict = nsc->sc_asict; 468 bus_space_handle_t asich = nsc->sc_asich; 469 int savelen; 470 int maxwait = 100; /* about 120us */ 471 472 savelen = m->m_pkthdr.len; 473 474 /* Select page 0 registers. */ 475 bus_space_write_1(nict, nich, ED_P0_CR, 476 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA); 477 478 /* Reset remote DMA complete flag. */ 479 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC); 480 481 /* Set up DMA byte count. */ 482 bus_space_write_1(nict, nich, ED_P0_RBCR0, savelen); 483 bus_space_write_1(nict, nich, ED_P0_RBCR1, savelen >> 8); 484 485 /* Set up destination address in NIC mem. */ 486 bus_space_write_1(nict, nich, ED_P0_RSAR0, buf); 487 bus_space_write_1(nict, nich, ED_P0_RSAR1, buf >> 8); 488 489 /* Set remote DMA write. */ 490 bus_space_write_1(nict, nich, 491 ED_P0_CR, ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA); 492 493 /* 494 * Transfer the mbuf chain to the NIC memory. NE2000 cards 495 * require that data be transferred as words, and only words, 496 * so that case requires some extra code to patch over odd-length 497 * mbufs. 498 */ 499 if (nsc->sc_type == NE2000_TYPE_NE1000) { 500 /* NE1000s are easy. */ 501 for (; m != 0; m = m->m_next) { 502 if (m->m_len) { 503 bus_space_write_multi_1(asict, asich, 504 NE2000_ASIC_DATA, mtod(m, u_int8_t *), 505 m->m_len); 506 } 507 } 508 } else { 509 /* NE2000s are a bit trickier. */ 510 u_int8_t *data, savebyte[2]; 511 int l, leftover; 512 #ifdef DIAGNOSTIC 513 u_int8_t *lim; 514 #endif 515 /* Start out with no leftover data. */ 516 leftover = 0; 517 savebyte[0] = savebyte[1] = 0; 518 519 for (; m != 0; m = m->m_next) { 520 l = m->m_len; 521 if (l == 0) 522 continue; 523 data = mtod(m, u_int8_t *); 524 #ifdef DIAGNOSTIC 525 lim = data + l; 526 #endif 527 while (l > 0) { 528 if (leftover) { 529 /* 530 * Data left over (from mbuf or 531 * realignment). Buffer the next 532 * byte, and write it and the 533 * leftover data out. 534 */ 535 savebyte[1] = *data++; 536 l--; 537 #ifdef __NetBSD__ 538 bus_space_write_stream_2(asict, asich, 539 NE2000_ASIC_DATA, 540 *(u_int16_t *)savebyte); 541 #else 542 bus_space_write_raw_multi_2(asict, 543 asich, NE2000_ASIC_DATA, 544 savebyte, 2); 545 #endif 546 leftover = 0; 547 #ifdef i386 548 #define ALIGNED_POINTER(p,t) 1 549 #endif 550 #ifdef alpha 551 #define ALIGNED_POINTER(p,t) ((((u_long)(p)) & (sizeof(t)-1)) == 0) 552 #endif 553 } else if (ALIGNED_POINTER(data, 554 u_int16_t) == 0) { 555 /* 556 * Unaligned data; buffer the next 557 * byte. 558 */ 559 savebyte[0] = *data++; 560 l--; 561 leftover = 1; 562 } else { 563 /* 564 * Aligned data; output contiguous 565 * words as much as we can, then 566 * buffer the remaining byte, if any. 567 */ 568 leftover = l & 1; 569 l &= ~1; 570 #ifdef __NetBSD__ 571 bus_space_write_multi_stream_2(asict, 572 asich, NE2000_ASIC_DATA, 573 (u_int16_t *)data, l >> 1); 574 #else 575 bus_space_write_raw_multi_2(asict, 576 asich, NE2000_ASIC_DATA, data, l); 577 #endif 578 data += l; 579 if (leftover) 580 savebyte[0] = *data++; 581 l = 0; 582 } 583 } 584 if (l < 0) 585 panic("ne2000_write_mbuf: negative len"); 586 #ifdef DIAGNOSTIC 587 if (data != lim) 588 panic("ne2000_write_mbuf: data != lim"); 589 #endif 590 } 591 if (leftover) { 592 savebyte[1] = 0; 593 #ifdef __NetBSD__ 594 bus_space_write_stream_2(asict, asich, NE2000_ASIC_DATA, 595 *(u_int16_t *)savebyte); 596 #else 597 bus_space_write_raw_multi_2(asict, asich, 598 NE2000_ASIC_DATA, savebyte, 2); 599 #endif 600 } 601 } 602 603 /* 604 * Wait for remote DMA to complete. This is necessary because on the 605 * transmit side, data is handled internally by the NIC in bursts, and 606 * we can't start another remote DMA until this one completes. Not 607 * waiting causes really bad things to happen - like the NIC wedging 608 * the bus. 609 */ 610 while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) != 611 ED_ISR_RDC) && --maxwait); 612 613 if (maxwait == 0) { 614 log(LOG_WARNING, 615 "%s: remote transmit DMA failed to complete\n", 616 sc->sc_dev.dv_xname); 617 dp8390_reset(sc); 618 } 619 620 return (savelen); 621 } 622 623 /* 624 * Given a source and destination address, copy 'amount' of a packet from 625 * the ring buffer into a linear destination buffer. Takes into account 626 * ring-wrap. 627 */ 628 int 629 ne2000_ring_copy(sc, src, dst, amount) 630 struct dp8390_softc *sc; 631 int src; 632 caddr_t dst; 633 u_short amount; 634 { 635 struct ne2000_softc *nsc = (struct ne2000_softc *)sc; 636 bus_space_tag_t nict = sc->sc_regt; 637 bus_space_handle_t nich = sc->sc_regh; 638 bus_space_tag_t asict = nsc->sc_asict; 639 bus_space_handle_t asich = nsc->sc_asich; 640 u_short tmp_amount; 641 int useword = NE2000_USE_WORD(nsc); 642 643 /* Does copy wrap to lower addr in ring buffer? */ 644 if (src + amount > sc->mem_end) { 645 tmp_amount = sc->mem_end - src; 646 647 /* Copy amount up to end of NIC memory. */ 648 ne2000_readmem(nict, nich, asict, asich, src, 649 (u_int8_t *)dst, tmp_amount, useword); 650 651 amount -= tmp_amount; 652 src = sc->mem_ring; 653 dst += tmp_amount; 654 } 655 656 ne2000_readmem(nict, nich, asict, asich, src, (u_int8_t *)dst, 657 amount, useword); 658 659 return (src + amount); 660 } 661 662 void 663 ne2000_read_hdr(sc, buf, hdr) 664 struct dp8390_softc *sc; 665 int buf; 666 struct dp8390_ring *hdr; 667 { 668 struct ne2000_softc *nsc = (struct ne2000_softc *)sc; 669 670 ne2000_readmem(sc->sc_regt, sc->sc_regh, nsc->sc_asict, nsc->sc_asich, 671 buf, (u_int8_t *)hdr, sizeof(struct dp8390_ring), 672 NE2000_USE_WORD(nsc)); 673 #if BYTE_ORDER == BIG_ENDIAN 674 hdr->count = swap16(hdr->count); 675 #endif 676 } 677 678 int 679 ne2000_test_mem(sc) 680 struct dp8390_softc *sc; 681 { 682 683 /* Noop. */ 684 return (0); 685 } 686 687 /* 688 * Given a NIC memory source address and a host memory destination address, 689 * copy 'amount' from NIC to host using programmed i/o. The 'amount' is 690 * rounded up to a word - ok as long as mbufs are word sized. 691 */ 692 void 693 ne2000_readmem(nict, nich, asict, asich, src, dst, amount, useword) 694 bus_space_tag_t nict; 695 bus_space_handle_t nich; 696 bus_space_tag_t asict; 697 bus_space_handle_t asich; 698 int src; 699 u_int8_t *dst; 700 size_t amount; 701 int useword; 702 { 703 704 /* Select page 0 registers. */ 705 bus_space_write_1(nict, nich, ED_P0_CR, 706 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA); 707 708 /* Round up to a word. */ 709 if (amount & 1) 710 ++amount; 711 712 /* Set up DMA byte count. */ 713 bus_space_write_1(nict, nich, ED_P0_RBCR0, amount); 714 bus_space_write_1(nict, nich, ED_P0_RBCR1, amount >> 8); 715 716 /* Set up source address in NIC mem. */ 717 bus_space_write_1(nict, nich, ED_P0_RSAR0, src); 718 bus_space_write_1(nict, nich, ED_P0_RSAR1, src >> 8); 719 720 bus_space_write_1(nict, nich, ED_P0_CR, 721 ED_CR_RD0 | ED_CR_PAGE_0 | ED_CR_STA); 722 723 if (useword) 724 #ifdef __NetBSD__ 725 bus_space_read_multi_stream_2(asict, asich, NE2000_ASIC_DATA, 726 (u_int16_t *)dst, amount >> 1); 727 #else 728 bus_space_read_raw_multi_2(asict, asich, NE2000_ASIC_DATA, 729 dst, amount); 730 #endif 731 else 732 bus_space_read_multi_1(asict, asich, NE2000_ASIC_DATA, 733 dst, amount); 734 } 735 736 /* 737 * Stripped down routine for writing a linear buffer to NIC memory. Only 738 * used in the probe routine to test the memory. 'len' must be even. 739 */ 740 void 741 ne2000_writemem(nict, nich, asict, asich, src, dst, len, useword) 742 bus_space_tag_t nict; 743 bus_space_handle_t nich; 744 bus_space_tag_t asict; 745 bus_space_handle_t asich; 746 u_int8_t *src; 747 int dst; 748 size_t len; 749 int useword; 750 { 751 int maxwait = 100; /* about 120us */ 752 753 /* Select page 0 registers. */ 754 bus_space_write_1(nict, nich, ED_P0_CR, 755 ED_CR_RD2 | ED_CR_PAGE_0 | ED_CR_STA); 756 757 /* Reset remote DMA complete flag. */ 758 bus_space_write_1(nict, nich, ED_P0_ISR, ED_ISR_RDC); 759 760 /* Set up DMA byte count. */ 761 bus_space_write_1(nict, nich, ED_P0_RBCR0, len); 762 bus_space_write_1(nict, nich, ED_P0_RBCR1, len >> 8); 763 764 /* Set up destination address in NIC mem. */ 765 bus_space_write_1(nict, nich, ED_P0_RSAR0, dst); 766 bus_space_write_1(nict, nich, ED_P0_RSAR1, dst >> 8); 767 768 /* Set remote DMA write. */ 769 bus_space_write_1(nict, nich, ED_P0_CR, 770 ED_CR_RD1 | ED_CR_PAGE_0 | ED_CR_STA); 771 772 if (useword) 773 #ifdef __NetBSD__ 774 bus_space_write_multi_stream_2(asict, asich, NE2000_ASIC_DATA, 775 (u_int16_t *)src, len >> 1); 776 #else 777 bus_space_write_raw_multi_2(asict, asich, NE2000_ASIC_DATA, 778 src, len); 779 #endif 780 else 781 bus_space_write_multi_1(asict, asich, NE2000_ASIC_DATA, 782 src, len); 783 784 /* 785 * Wait for remote DMA to complete. This is necessary because on the 786 * transmit side, data is handled internally by the NIC in bursts, and 787 * we can't start another remote DMA until this one completes. Not 788 * waiting causes really bad things to happen - like the NIC wedging 789 * the bus. 790 */ 791 while (((bus_space_read_1(nict, nich, ED_P0_ISR) & ED_ISR_RDC) != 792 ED_ISR_RDC) && --maxwait); 793 } 794 795 int 796 ne2000_detach(sc, flags) 797 struct ne2000_softc *sc; 798 int flags; 799 { 800 return (dp8390_detach(&sc->sc_dp8390, flags)); 801 } 802