1 /* $NetBSD: dm9000.c,v 1.1 2010/09/08 22:01:29 ahoka Exp $ */ 2 3 /* 4 * Copyright (c) 2009 Paul Fleischer 5 * All rights reserved. 6 * 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. The name of the company nor the name of the author may be used to 13 * endorse or promote products derived from this software without specific 14 * prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 /* based on sys/dev/ic/cs89x0.c */ 30 /* 31 * Copyright (c) 2004 Christopher Gilbert 32 * All rights reserved. 33 * 34 * 1. Redistributions of source code must retain the above copyright 35 * notice, this list of conditions and the following disclaimer. 36 * 2. Redistributions in binary form must reproduce the above copyright 37 * notice, this list of conditions and the following disclaimer in the 38 * documentation and/or other materials provided with the distribution. 39 * 3. The name of the company nor the name of the author may be used to 40 * endorse or promote products derived from this software without specific 41 * prior written permission. 42 * 43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 44 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 45 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 46 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 47 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 48 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 49 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 53 * SUCH DAMAGE. 54 */ 55 56 /* 57 * Copyright 1997 58 * Digital Equipment Corporation. All rights reserved. 59 * 60 * This software is furnished under license and may be used and 61 * copied only in accordance with the following terms and conditions. 62 * Subject to these conditions, you may download, copy, install, 63 * use, modify and distribute this software in source and/or binary 64 * form. No title or ownership is transferred hereby. 65 * 66 * 1) Any source code used, modified or distributed must reproduce 67 * and retain this copyright notice and list of conditions as 68 * they appear in the source file. 69 * 70 * 2) No right is granted to use any trade name, trademark, or logo of 71 * Digital Equipment Corporation. Neither the "Digital Equipment 72 * Corporation" name nor any trademark or logo of Digital Equipment 73 * Corporation may be used to endorse or promote products derived 74 * from this software without the prior written permission of 75 * Digital Equipment Corporation. 76 * 77 * 3) This software is provided "AS-IS" and any express or implied 78 * warranties, including but not limited to, any implied warranties 79 * of merchantability, fitness for a particular purpose, or 80 * non-infringement are disclaimed. In no event shall DIGITAL be 81 * liable for any damages whatsoever, and in particular, DIGITAL 82 * shall not be liable for special, indirect, consequential, or 83 * incidental damages or damages for lost profits, loss of 84 * revenue or loss of use, whether such damages arise in contract, 85 * negligence, tort, under statute, in equity, at law or otherwise, 86 * even if advised of the possibility of such damage. 87 */ 88 89 #include <sys/cdefs.h> 90 91 #include <sys/param.h> 92 #include <sys/systm.h> 93 #include <sys/mbuf.h> 94 #include <sys/syslog.h> 95 #include <sys/socket.h> 96 #include <sys/device.h> 97 #include <sys/malloc.h> 98 #include <sys/ioctl.h> 99 #include <sys/errno.h> 100 101 #include <net/if.h> 102 #include <net/if_ether.h> 103 #include <net/if_media.h> 104 #ifdef INET 105 #include <netinet/in.h> 106 #include <netinet/if_inarp.h> 107 #endif 108 109 #include <net/bpf.h> 110 #include <net/bpfdesc.h> 111 112 #include <sys/bus.h> 113 #include <sys/intr.h> 114 115 #include <dev/ic/dm9000var.h> 116 #include <dev/ic/dm9000reg.h> 117 118 #if 1 119 #undef DM9000_DEBUG 120 #undef DM9000_TX_DEBUG 121 #undef DM9000_TX_DATA_DEBUG 122 #undef DM9000_RX_DEBUG 123 #undef DM9000_RX_DATA_DEBUG 124 #else 125 #define DM9000_DEBUG 126 #define DM9000_TX_DEBUG 127 #define DM9000_TX_DATA_DEBUG 128 #define DM9000_RX_DEBUG 129 #define DM9000_RX_DATA_DEBUG 130 #endif 131 132 #ifdef DM9000_DEBUG 133 #define DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0) 134 #else 135 #define DPRINTF(s) do {} while (/*CONSTCOND*/0) 136 #endif 137 138 #ifdef DM9000_TX_DEBUG 139 #define TX_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0) 140 #else 141 #define TX_DPRINTF(s) do {} while (/*CONSTCOND*/0) 142 #endif 143 144 #ifdef DM9000_RX_DEBUG 145 #define RX_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0) 146 #else 147 #define RX_DPRINTF(s) do {} while (/*CONSTCOND*/0) 148 #endif 149 150 #ifdef DM9000_RX_DATA_DEBUG 151 #define RX_DATA_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0) 152 #else 153 #define RX_DATA_DPRINTF(s) do {} while (/*CONSTCOND*/0) 154 #endif 155 156 #ifdef DM9000_TX_DATA_DEBUG 157 #define TX_DATA_DPRINTF(s) do {printf s; } while (/*CONSTCOND*/0) 158 #else 159 #define TX_DATA_DPRINTF(s) do {} while (/*CONSTCOND*/0) 160 #endif 161 162 163 uint16_t dme_phy_read(struct dme_softc *sc, int reg); 164 void dme_phy_write(struct dme_softc *sc, int reg, uint16_t value); 165 166 /*** Methods registered in struct ifnet ***/ 167 void dme_start_output(struct ifnet *ifp); 168 int dme_init(struct ifnet *ifp); 169 int dme_ioctl(struct ifnet *ifp, u_long cmd, void *data); 170 void dme_stop(struct ifnet *ifp, int disable); 171 172 int dme_mediachange(struct ifnet *ifp); 173 void dme_mediastatus(struct ifnet *ufp, struct ifmediareq *ifmr); 174 175 /*** Internal methods ***/ 176 177 /* Prepare data to be transmitted (i.e. dequeue and load it into the DM9000) */ 178 void dme_prepare(struct dme_softc *sc, struct ifnet *ifp); 179 180 /* Transmit prepared data */ 181 void dme_transmit(struct dme_softc *sc); 182 183 /* Receive data */ 184 void dme_receive(struct dme_softc *sc, struct ifnet *ifp); 185 186 /* Software Initialize/Reset of the DM9000 */ 187 void dme_reset(struct dme_softc *sc); 188 189 uint16_t 190 dme_phy_read(struct dme_softc *sc, int reg) 191 { 192 uint16_t val; 193 /* Select Register to read*/ 194 dme_write(sc, DM9000_EPAR, DM9000_EPAR_INT_PHY + 195 (reg & DM9000_EPAR_EROA_MASK)); 196 /* Select read operation (DM9000_EPCR_ERPRR) from the PHY */ 197 dme_write(sc, DM9000_EPCR, DM9000_EPCR_ERPRR + DM9000_EPCR_EPOS_PHY); 198 199 /* Wait until access to PHY has completed */ 200 while (dme_read(sc, DM9000_EPCR) & DM9000_EPCR_ERRE); 201 202 /* XXX: The delay is probably not necessary as we just busy-waited */ 203 delay(200); 204 205 /* Reset ERPRR-bit */ 206 dme_write(sc, DM9000_EPCR, DM9000_EPCR_EPOS_PHY); 207 208 val = dme_read(sc, DM9000_EPDRL); 209 val += dme_read(sc, DM9000_EPDRH) << 8; 210 211 return val; 212 } 213 214 void 215 dme_phy_write(struct dme_softc *sc, int reg, uint16_t value) 216 { 217 /* Select Register to write*/ 218 dme_write(sc, DM9000_EPAR, DM9000_EPAR_INT_PHY + 219 (reg & DM9000_EPAR_EROA_MASK)); 220 221 /* Write data to the two data registers */ 222 dme_write(sc, DM9000_EPDRL, value & 0xFF); 223 dme_write(sc, DM9000_EPDRH, (value >> 8) & 0xFF); 224 225 /* Select write operation (DM9000_EPCR_ERPRW) from the PHY */ 226 dme_write(sc, DM9000_EPCR, DM9000_EPCR_ERPRW + DM9000_EPCR_EPOS_PHY); 227 228 /* Wait until access to PHY has completed */ 229 while(dme_read(sc, DM9000_EPCR) & DM9000_EPCR_ERRE); 230 231 232 /* XXX: The delay is probably not necessary as we just busy-waited */ 233 delay(200); 234 235 /* Reset ERPRR-bit */ 236 dme_write(sc, DM9000_EPCR, DM9000_EPCR_EPOS_PHY); 237 } 238 239 int 240 dme_attach(struct dme_softc *sc, uint8_t *enaddr) 241 { 242 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 243 uint8_t b[2]; 244 245 dme_read_c(sc, DM9000_VID0, b, 2); 246 #if BYTE_ORDER == BIG_ENDIAN 247 sc->sc_vendor_id = (b[0] << 8) | b[1]; 248 #else 249 sc->sc_vendor_id = b[0] | (b[1] << 8); 250 #endif 251 dme_read_c(sc, DM9000_PID0, b, 2); 252 #if BYTE_ORDER == BIG_ENDIAN 253 sc->sc_product_id = (b[0] << 8) | b[1]; 254 #else 255 sc->sc_product_id = b[0] | (b[1] << 8); 256 #endif 257 /* TODO: Check the vendor ID as well */ 258 if (sc->sc_product_id != 0x9000) { 259 panic("dme_attach: product id mismatch (0x%hx != 0x9000)", 260 sc->sc_product_id); 261 } 262 263 #if 0 264 { 265 /* Force 10Mbps to test dme_phy_write */ 266 uint16_t bmcr; 267 bmcr = dme_phy_read(sc, DM9000_PHY_BMCR); 268 bmcr &= ~DM9000_PHY_BMCR_AUTO_NEG_EN; 269 bmcr &= ~DM9000_PHY_BMCR_SPEED_SELECT; /* select 100Mbps */ 270 dme_phy_write(sc, DM9000_PHY_BMCR, bmcr); 271 } 272 #endif 273 /* Initialize ifnet structure. */ 274 strlcpy(ifp->if_xname, device_xname(sc->sc_dev), IFNAMSIZ); 275 ifp->if_softc = sc; 276 ifp->if_start = dme_start_output; 277 ifp->if_init = dme_init; 278 ifp->if_ioctl = dme_ioctl; 279 ifp->if_stop = dme_stop; 280 ifp->if_watchdog = NULL; /* no watchdog at this stage */ 281 ifp->if_flags = IFF_SIMPLEX | IFF_NOTRAILERS | 282 IFF_BROADCAST; /* No multicast support for now */ 283 IFQ_SET_READY(&ifp->if_snd); 284 285 /* Initialize ifmedia structures. */ 286 ifmedia_init(&sc->sc_media, 0, dme_mediachange, dme_mediastatus); 287 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_10_T|IFM_100_TX, 0, NULL); 288 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_10_T|IFM_100_TX); 289 290 if (enaddr != NULL) 291 memcpy(sc->sc_enaddr, enaddr, sizeof(sc->sc_enaddr)); 292 293 /* Configure DM9000 with the MAC address */ 294 dme_write_c(sc, DM9000_PAB0, sc->sc_enaddr, 6); 295 296 #ifdef DM9000_DEBUG 297 { 298 uint8_t macAddr[6]; 299 dme_read_c(sc, DM9000_PAB0, macAddr, 6); 300 printf("DM9000 configured with MAC address: "); 301 for (int i = 0; i < 6; i++) { 302 printf("%02X:", macAddr[i]); 303 } 304 printf("\n"); 305 } 306 #endif 307 308 if_attach(ifp); 309 ether_ifattach(ifp, sc->sc_enaddr); 310 311 #ifdef DM9000_DEBUG 312 { 313 uint8_t network_state; 314 network_state = dme_read(sc, DM9000_NSR); 315 printf("DM9000 Link status: "); 316 if (network_state & DM9000_NSR_LINKST) { 317 if (network_state & DM9000_NSR_SPEED) 318 printf("10Mbps"); 319 else 320 printf("100Mbps"); 321 } else { 322 printf("Down"); 323 } 324 printf("\n"); 325 } 326 #endif 327 328 sc->io_mode = (dme_read(sc, DM9000_ISR) & 329 DM9000_IOMODE_MASK) >> DM9000_IOMODE_SHIFT; 330 if (sc->io_mode != DM9000_MODE_16BIT ) 331 panic("DM9000: Only 16-bit mode is supported!\n"); 332 #ifdef DM9000_DEBUG 333 printf("DM9000 Operation Mode: "); 334 switch( sc->io_mode) { 335 case DM9000_MODE_16BIT: 336 printf("16-bit mode"); 337 break; 338 case DM9000_MODE_32BIT: 339 printf("32-bit mode"); 340 break; 341 case DM9000_MODE_8BIT: 342 printf("8-bit mode"); 343 break; 344 case 3: 345 printf("Invalid mode"); 346 break; 347 } 348 printf("\n"); 349 #endif 350 351 return 0; 352 } 353 354 int dme_intr(void *arg) 355 { 356 struct dme_softc *sc = arg; 357 struct ifnet *ifp = &sc->sc_ethercom.ec_if; 358 uint8_t status; 359 360 /* Disable interrupts */ 361 dme_write(sc, DM9000_IMR, DM9000_IMR_PAR ); 362 363 status = dme_read(sc, DM9000_ISR); 364 dme_write(sc, DM9000_ISR, status); 365 366 if (status & DM9000_ISR_PRS) { 367 if (ifp->if_flags & IFF_RUNNING ) 368 dme_receive(sc, ifp); 369 } 370 if (status & DM9000_ISR_PTS) { 371 uint8_t nsr; 372 uint8_t tx_status = 0x01; /* Initialize to an error value */ 373 374 /* A packet has been transmitted */ 375 sc->txbusy = 0; 376 377 nsr = dme_read(sc, DM9000_NSR); 378 379 if (nsr & DM9000_NSR_TX1END) { 380 tx_status = dme_read(sc, DM9000_TSR1); 381 TX_DPRINTF(("dme_intr: Sent using channel 0\n")); 382 } else if (nsr & DM9000_NSR_TX2END) { 383 tx_status = dme_read(sc, DM9000_TSR2); 384 TX_DPRINTF(("dme_intr: Sent using channel 1\n")); 385 } 386 387 if (tx_status == 0x0) { 388 /* Frame successfully sent */ 389 ifp->if_opackets++; 390 } else { 391 ifp->if_oerrors++; 392 } 393 394 /* If we have nothing ready to transmit, prepare something */ 395 if (!sc->txready) { 396 dme_prepare(sc, ifp); 397 } 398 399 if (sc->txready) 400 dme_transmit(sc); 401 402 /* Prepare the next frame */ 403 dme_prepare(sc, ifp); 404 405 } 406 #ifdef notyet 407 if (status & DM9000_ISR_LNKCHNG) { 408 } 409 #endif 410 411 #ifdef DIAGNOSTIC 412 sc->sc_inside_interrupt = false; 413 #endif 414 415 /* Enable interrupts again */ 416 dme_write(sc, DM9000_IMR, DM9000_IMR_PAR | DM9000_IMR_PRM | 417 DM9000_IMR_PTM); 418 419 return 1; 420 } 421 422 void 423 dme_start_output(struct ifnet *ifp) 424 { 425 struct dme_softc *sc; 426 427 sc = ifp->if_softc; 428 429 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) { 430 printf("No output\n"); 431 return; 432 } 433 434 if (sc->txbusy && sc->txready) { 435 panic("DM9000: Internal error, trying to send without" 436 " any empty queue\n"); 437 } 438 439 dme_prepare(sc, ifp); 440 441 if (sc->txbusy == 0) { 442 /* We are ready to transmit right away */ 443 dme_transmit(sc); 444 dme_prepare(sc, ifp); /* Prepare next one */ 445 } else { 446 /* We need to wait until the current packet has 447 * been transmitted. 448 */ 449 ifp->if_flags |= IFF_OACTIVE; 450 } 451 } 452 453 void 454 dme_prepare(struct dme_softc *sc, struct ifnet *ifp) 455 { 456 struct mbuf *buf; 457 struct mbuf *bufChain; 458 uint16_t length; 459 uint8_t *write_ptr; 460 461 TX_DPRINTF(("dme_prepare: Entering\n")); 462 463 if (sc->txready) 464 panic("dme_prepare: Someone called us with txready set\n"); 465 466 IFQ_DEQUEUE(&ifp->if_snd, bufChain); 467 if (bufChain == NULL) { 468 TX_DPRINTF(("dme_prepare: Nothing to transmit\n")); 469 ifp->if_flags &= ~IFF_OACTIVE; /* Clear OACTIVE bit */ 470 return; /* Nothing to transmit */ 471 } 472 473 /* Element has now been removed from the queue, so we better send it */ 474 475 if (ifp->if_bpf) 476 bpf_mtap(ifp, bufChain); 477 478 479 length = 0; 480 481 /* XXX: This support 16-bit I/O mode only. */ 482 /* XXX: This code must be factored out, such that architecture 483 dependant versions can be supplied */ 484 485 int left_over_count = 0; /* Number of bytes from previous mbuf, which 486 need to be written with the next.*/ 487 uint16_t left_over_buf = 0; 488 489 /* Setup the DM9000 to accept the writes, and then write each buf in 490 the chain. */ 491 492 TX_DATA_DPRINTF(("dme_prepare: Writing data: ")); 493 bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->dme_io, DM9000_MWCMD); 494 for (buf = bufChain; buf != NULL; buf = buf->m_next) { 495 int to_write = buf->m_len; 496 497 length += to_write; 498 499 write_ptr = buf->m_data; 500 while (to_write > 0 || 501 (buf->m_next == NULL && left_over_count > 0) 502 ) { 503 if (left_over_count > 0) { 504 uint8_t b = 0; 505 DPRINTF(("dme_prepare: " 506 "Writing left over byte\n")); 507 508 if (to_write > 0) { 509 b = *write_ptr; 510 to_write--; 511 write_ptr++; 512 513 DPRINTF(("Took single byte\n")); 514 } else { 515 DPRINTF(("Leftover in last run\n")); 516 length++; 517 } 518 519 /* Does shift direction depend on endianess? */ 520 left_over_buf = left_over_buf | (b << 8); 521 522 bus_space_write_2(sc->sc_iot, sc->sc_ioh, 523 sc->dme_data, left_over_buf); 524 TX_DATA_DPRINTF(("%02X ", left_over_buf)); 525 left_over_count = 0; 526 } else if ((long)write_ptr % 2 != 0) { 527 /* Misaligned data */ 528 DPRINTF(("dme_prepare: " 529 "Detected misaligned data\n")); 530 left_over_buf = *write_ptr; 531 left_over_count = 1; 532 write_ptr++; 533 to_write--; 534 } else { 535 int i; 536 uint16_t *dptr = (uint16_t*)write_ptr; 537 538 /* A block of aligned data. */ 539 for(i = 0; i < to_write/2; i++) { 540 /* buf will be half-word aligned 541 * all the time 542 */ 543 bus_space_write_2(sc->sc_iot, 544 sc->sc_ioh, sc->dme_data, *dptr); 545 TX_DATA_DPRINTF(("%02X %02X ", 546 *dptr & 0xFF, (*dptr>>8) & 0xFF)); 547 dptr++; 548 } 549 550 write_ptr += i*2; 551 if (to_write % 2 != 0) { 552 DPRINTF(("dme_prepare: " 553 "to_write %% 2: %d\n", 554 to_write % 2)); 555 left_over_count = 1; 556 /* XXX: Does this depend on 557 * the endianess? 558 */ 559 left_over_buf = *write_ptr; 560 561 write_ptr++; 562 to_write--; 563 DPRINTF(("dme_prepare: " 564 "to_write (after): %d\n", 565 to_write)); 566 DPRINTF(("dme_prepare: i*2: %d\n", 567 i*2)); 568 } 569 to_write -= i*2; 570 } 571 } /* while(...) */ 572 } /* for(...) */ 573 574 TX_DATA_DPRINTF(("\n")); 575 576 if (length % 2 == 1) { 577 panic("dme_prepare: length is not a word-length"); 578 } 579 580 sc->txready_length = length; 581 sc->txready = 1; 582 583 TX_DPRINTF(("dme_prepare: txbusy: %d\ndme_prepare: " 584 "txready: %d, txready_length: %d\n", 585 sc->txbusy, sc->txready, sc->txready_length)); 586 587 m_freem(bufChain); 588 589 TX_DPRINTF(("dme_prepare: Leaving\n")); 590 } 591 592 int 593 dme_init(struct ifnet *ifp) 594 { 595 int s; 596 struct dme_softc *sc = ifp->if_softc; 597 598 dme_stop(ifp, 0); 599 600 s = splnet(); 601 602 dme_reset(sc); 603 604 sc->sc_ethercom.ec_if.if_flags |= IFF_RUNNING; 605 sc->sc_ethercom.ec_if.if_flags &= ~IFF_OACTIVE; 606 sc->sc_ethercom.ec_if.if_timer = 0; 607 608 splx(s); 609 610 return 0; 611 } 612 613 int 614 dme_ioctl(struct ifnet *ifp, u_long cmd, void *data) 615 { 616 struct dme_softc *sc = ifp->if_softc; 617 struct ifreq *ifr = data; 618 int s, error = 0; 619 620 s = splnet(); 621 622 switch(cmd) { 623 case SIOCGIFMEDIA: 624 case SIOCSIFMEDIA: 625 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd); 626 break; 627 default: 628 error = ether_ioctl(ifp, cmd, data); 629 break; 630 } 631 632 splx(s); 633 return error; 634 } 635 636 void 637 dme_stop(struct ifnet *ifp, int disable) 638 { 639 struct dme_softc *sc = ifp->if_softc; 640 641 /* Not quite sure what to do when called with disable == 0 */ 642 if (disable) { 643 /* Disable RX */ 644 dme_write(sc, DM9000_RCR, 0x0); 645 } 646 647 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 648 ifp->if_timer = 0; 649 } 650 651 int 652 dme_mediachange(struct ifnet *ifp) 653 { 654 /* TODO: Make this function do something useful. */ 655 return 0; 656 } 657 658 void 659 dme_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 660 { 661 /* TODO: Make this function do something useful. */ 662 struct dme_softc *sc = ifp->if_softc; 663 ifmr->ifm_active = sc->sc_media.ifm_cur->ifm_media; 664 665 if (ifp->if_flags & IFF_UP) { 666 ifmr->ifm_status = IFM_AVALID | IFM_ACTIVE; 667 } else { 668 ifmr->ifm_status = 0; 669 } 670 } 671 672 void 673 dme_transmit(struct dme_softc *sc) 674 { 675 uint8_t status; 676 677 TX_DPRINTF(("dme_transmit: PRE: txready: %d, txbusy: %d\n", 678 sc->txready, sc->txbusy)); 679 680 dme_write(sc, DM9000_TXPLL, sc->txready_length & 0xff); 681 dme_write(sc, DM9000_TXPLH, (sc->txready_length >> 8) & 0xff ); 682 683 /* Request to send the packet */ 684 status = dme_read(sc, DM9000_ISR); 685 686 dme_write(sc, DM9000_TCR, DM9000_TCR_TXREQ); 687 688 sc->txready = 0; 689 sc->txbusy = 1; 690 sc->txready_length = 0; 691 } 692 693 void 694 dme_receive(struct dme_softc *sc, struct ifnet *ifp) 695 { 696 uint8_t ready = 0x01; 697 698 DPRINTF(("inside dme_receive\n")); 699 700 while (ready == 0x01) { 701 /* Packet received, retrieve it */ 702 703 /* Read without address increment to get the ready byte without moving past it. */ 704 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 705 sc->dme_io, DM9000_MRCMDX); 706 /* Dummy ready */ 707 ready = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data); 708 ready = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->dme_data); 709 ready &= 0x03; /* we only want bits 1:0 */ 710 if (ready == 0x01) { 711 uint8_t rx_status; 712 713 uint16_t data; 714 uint16_t frame_length; 715 uint16_t i; 716 struct mbuf *m; 717 uint16_t *buf; 718 int pad; 719 720 /* TODO: Add support for 8-bit and 721 * 32-bit transfer modes. 722 */ 723 724 /* Read with address increment. */ 725 bus_space_write_1(sc->sc_iot, sc->sc_ioh, 726 sc->dme_io, DM9000_MRCMD); 727 data = bus_space_read_2(sc->sc_iot, sc->sc_ioh, 728 sc->dme_data); 729 730 rx_status = data & 0xFF; 731 frame_length = bus_space_read_2(sc->sc_iot, 732 sc->sc_ioh, sc->dme_data); 733 734 RX_DPRINTF(("dme_receive: " 735 "rx_statux: 0x%x, frame_length: %d\n", 736 rx_status, frame_length)); 737 738 739 MGETHDR(m, M_DONTWAIT, MT_DATA); 740 m->m_pkthdr.rcvif = ifp; 741 /* Ensure that we always allocate an even number of 742 * bytes in order to avoid writing beyond the buffer 743 */ 744 m->m_pkthdr.len = frame_length + (frame_length % 2); 745 pad = ALIGN(sizeof(struct ether_header)) - 746 sizeof(struct ether_header); 747 /* All our frames have the CRC attached */ 748 m->m_flags |= M_HASFCS; 749 if (m->m_pkthdr.len + pad > MHLEN ) 750 MCLGET(m, M_DONTWAIT); 751 752 m->m_data += pad; 753 m->m_len = frame_length + (frame_length % 2); 754 buf = mtod(m, uint16_t*); 755 756 RX_DPRINTF(("dme_receive: ")); 757 758 for(i=0; i< frame_length; i+=2 ) { 759 data = bus_space_read_2(sc->sc_iot, 760 sc->sc_ioh, sc->dme_data); 761 if ( (frame_length % 2 != 0) && 762 (i == frame_length-1) ) { 763 data = data & 0xff; 764 RX_DPRINTF((" L ")); 765 } 766 *buf = data; 767 buf++; 768 RX_DATA_DPRINTF(("%02X %02X ", data & 0xff, 769 (data>>8) & 0xff)); 770 } 771 772 RX_DATA_DPRINTF(("\n")); 773 RX_DPRINTF(("Read %d bytes\n", i)); 774 775 if (rx_status & (DM9000_RSR_CE | DM9000_RSR_PLE)) { 776 /* Error while receiving the packet, 777 * discard it and keep track of counters 778 */ 779 ifp->if_ierrors++; 780 RX_DPRINTF(("dme_receive: " 781 "Error reciving packet\n")); 782 } else if (rx_status & DM9000_RSR_LCS) { 783 ifp->if_collisions++; 784 } else { 785 if (ifp->if_bpf) 786 bpf_mtap(ifp, m); 787 ifp->if_ipackets++; 788 (*ifp->if_input)(ifp, m); 789 } 790 791 } else if (ready != 0x00) { 792 /* Should this be logged somehow? */ 793 DPRINTF(("DM9000: Resetting chip\n")); 794 dme_reset(sc); 795 } 796 } 797 } 798 799 void 800 dme_reset(struct dme_softc *sc) 801 { 802 uint8_t var; 803 804 /* Enable PHY */ 805 var = dme_read(sc, DM9000_GPCR); 806 dme_write(sc, DM9000_GPCR, var | DM9000_GPCR_GPIO0_OUT); 807 var = dme_read(sc, DM9000_GPR); 808 dme_write(sc, DM9000_GPR, var & ~DM9000_GPR_PHY_PWROFF); 809 810 /* Reset the DM9000 twice, as describe din section 5.2 of the 811 * Application Notes 812 */ 813 dme_write(sc, DM9000_NCR, 814 DM9000_NCR_RST | DM9000_NCR_LBK_MAC_INTERNAL); 815 816 delay(20); 817 dme_write(sc, DM9000_NCR, 0x0); 818 dme_write(sc, DM9000_NCR, 819 DM9000_NCR_RST | DM9000_NCR_LBK_MAC_INTERNAL); 820 821 delay(20); 822 dme_write(sc, DM9000_NCR, 0x0); 823 824 /* Select internal PHY, no wakeup event, no collosion mode, 825 * normal loopback mode, and no full duplex mode 826 */ 827 dme_write(sc, DM9000_NCR, DM9000_NCR_LBK_NORMAL ); 828 829 /* Will clear TX1END, TX2END, and WAKEST fields by reading DM9000_NSR*/ 830 dme_read(sc, DM9000_NSR); 831 832 /* Enable wraparound of read/write pointer, packet received latch, 833 * and packet transmitted latch. 834 */ 835 dme_write(sc, DM9000_IMR, 836 DM9000_IMR_PAR | DM9000_IMR_PRM | DM9000_IMR_PTM); 837 838 /* Enable RX without watchdog */ 839 dme_write(sc, DM9000_RCR, DM9000_RCR_RXEN | DM9000_RCR_WTDIS); 840 841 sc->txbusy = 0; 842 sc->txready = 0; 843 } 844