1 /* $NetBSD: if_gm.c,v 1.49 2016/12/15 09:28:03 ozaki-r Exp $ */ 2 3 /*- 4 * Copyright (c) 2000 Tsubai Masanari. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: if_gm.c,v 1.49 2016/12/15 09:28:03 ozaki-r Exp $"); 31 32 #include "opt_inet.h" 33 34 #include <sys/param.h> 35 #include <sys/device.h> 36 #include <sys/ioctl.h> 37 #include <sys/kernel.h> 38 #include <sys/mbuf.h> 39 #include <sys/socket.h> 40 #include <sys/systm.h> 41 #include <sys/callout.h> 42 43 #include <sys/rndsource.h> 44 45 #include <uvm/uvm_extern.h> 46 47 #include <net/if.h> 48 #include <net/if_ether.h> 49 #include <net/if_media.h> 50 51 #include <net/bpf.h> 52 53 #ifdef INET 54 #include <netinet/in.h> 55 #include <netinet/if_inarp.h> 56 #endif 57 58 #include <dev/mii/mii.h> 59 #include <dev/mii/miivar.h> 60 61 #include <dev/pci/pcivar.h> 62 #include <dev/pci/pcireg.h> 63 #include <dev/pci/pcidevs.h> 64 65 #include <dev/ofw/openfirm.h> 66 #include <macppc/dev/if_gmreg.h> 67 #include <machine/pio.h> 68 69 #define NTXBUF 4 70 #define NRXBUF 32 71 72 struct gmac_softc { 73 device_t sc_dev; 74 struct ethercom sc_ethercom; 75 vaddr_t sc_reg; 76 struct gmac_dma *sc_txlist; 77 struct gmac_dma *sc_rxlist; 78 int sc_txnext; 79 int sc_rxlast; 80 void *sc_txbuf[NTXBUF]; 81 void *sc_rxbuf[NRXBUF]; 82 struct mii_data sc_mii; 83 struct callout sc_tick_ch; 84 char sc_laddr[6]; 85 86 krndsource_t sc_rnd_source; /* random source */ 87 }; 88 89 #define sc_if sc_ethercom.ec_if 90 91 int gmac_match(device_t, cfdata_t, void *); 92 void gmac_attach(device_t, device_t, void *); 93 94 static inline u_int gmac_read_reg(struct gmac_softc *, int); 95 static inline void gmac_write_reg(struct gmac_softc *, int, u_int); 96 97 static inline void gmac_start_txdma(struct gmac_softc *); 98 static inline void gmac_start_rxdma(struct gmac_softc *); 99 static inline void gmac_stop_txdma(struct gmac_softc *); 100 static inline void gmac_stop_rxdma(struct gmac_softc *); 101 102 int gmac_intr(void *); 103 void gmac_tint(struct gmac_softc *); 104 void gmac_rint(struct gmac_softc *); 105 struct mbuf * gmac_get(struct gmac_softc *, void *, int); 106 void gmac_start(struct ifnet *); 107 int gmac_put(struct gmac_softc *, void *, struct mbuf *); 108 109 void gmac_stop(struct gmac_softc *); 110 void gmac_reset(struct gmac_softc *); 111 void gmac_init(struct gmac_softc *); 112 void gmac_init_mac(struct gmac_softc *); 113 void gmac_setladrf(struct gmac_softc *); 114 115 int gmac_ioctl(struct ifnet *, u_long, void *); 116 void gmac_watchdog(struct ifnet *); 117 118 int gmac_mii_readreg(device_t, int, int); 119 void gmac_mii_writereg(device_t, int, int, int); 120 void gmac_mii_statchg(struct ifnet *); 121 void gmac_mii_tick(void *); 122 123 CFATTACH_DECL_NEW(gm, sizeof(struct gmac_softc), 124 gmac_match, gmac_attach, NULL, NULL); 125 126 int 127 gmac_match(device_t parent, cfdata_t match, void *aux) 128 { 129 struct pci_attach_args *pa = aux; 130 131 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE && 132 (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC || 133 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2 || 134 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC3)) 135 return 1; 136 137 return 0; 138 } 139 140 void 141 gmac_attach(device_t parent, device_t self, void *aux) 142 { 143 struct gmac_softc * const sc = device_private(self); 144 struct pci_attach_args * const pa = aux; 145 struct ifnet * const ifp = &sc->sc_if; 146 struct mii_data * const mii = &sc->sc_mii; 147 pci_intr_handle_t ih; 148 const char *intrstr = NULL; 149 const char * const xname = device_xname(self); 150 int node, i; 151 char *p; 152 struct gmac_dma *dp; 153 u_int32_t reg[10]; 154 u_char laddr[6]; 155 char buf[PCI_INTRSTR_LEN]; 156 157 sc->sc_dev = self; 158 159 node = pcidev_to_ofdev(pa->pa_pc, pa->pa_tag); 160 if (node == 0) { 161 printf(": cannot find gmac node\n"); 162 return; 163 } 164 165 OF_getprop(node, "local-mac-address", laddr, sizeof laddr); 166 OF_getprop(node, "assigned-addresses", reg, sizeof reg); 167 168 memcpy(sc->sc_laddr, laddr, sizeof laddr); 169 sc->sc_reg = reg[2]; 170 171 if (pci_intr_map(pa, &ih)) { 172 printf(": unable to map interrupt\n"); 173 return; 174 } 175 intrstr = pci_intr_string(pa->pa_pc, ih, buf, sizeof(buf)); 176 177 if (pci_intr_establish(pa->pa_pc, ih, IPL_NET, gmac_intr, sc) == NULL) { 178 printf(": unable to establish interrupt"); 179 if (intrstr) 180 printf(" at %s", intrstr); 181 printf("\n"); 182 return; 183 } 184 185 /* Setup packet buffers and DMA descriptors. */ 186 p = malloc((NRXBUF + NTXBUF) * 2048 + 3 * 0x800, M_DEVBUF, M_NOWAIT); 187 if (p == NULL) { 188 printf(": cannot malloc buffers\n"); 189 return; 190 } 191 p = (void *)roundup((vaddr_t)p, 0x800); 192 memset(p, 0, 2048 * (NRXBUF + NTXBUF) + 2 * 0x800); 193 194 sc->sc_rxlist = (void *)p; 195 p += 0x800; 196 sc->sc_txlist = (void *)p; 197 p += 0x800; 198 199 dp = sc->sc_rxlist; 200 for (i = 0; i < NRXBUF; i++) { 201 sc->sc_rxbuf[i] = p; 202 dp->address = htole32(vtophys((vaddr_t)p)); 203 dp->cmd = htole32(GMAC_OWN); 204 dp++; 205 p += 2048; 206 } 207 208 dp = sc->sc_txlist; 209 for (i = 0; i < NTXBUF; i++) { 210 sc->sc_txbuf[i] = p; 211 dp->address = htole32(vtophys((vaddr_t)p)); 212 dp++; 213 p += 2048; 214 } 215 216 aprint_normal(": Ethernet address %s\n", ether_sprintf(laddr)); 217 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 218 219 callout_init(&sc->sc_tick_ch, 0); 220 221 gmac_reset(sc); 222 gmac_init_mac(sc); 223 224 memcpy(ifp->if_xname, xname, IFNAMSIZ); 225 ifp->if_softc = sc; 226 ifp->if_ioctl = gmac_ioctl; 227 ifp->if_start = gmac_start; 228 ifp->if_watchdog = gmac_watchdog; 229 ifp->if_flags = 230 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST; 231 IFQ_SET_READY(&ifp->if_snd); 232 233 mii->mii_ifp = ifp; 234 mii->mii_readreg = gmac_mii_readreg; 235 mii->mii_writereg = gmac_mii_writereg; 236 mii->mii_statchg = gmac_mii_statchg; 237 238 sc->sc_ethercom.ec_mii = mii; 239 ifmedia_init(&mii->mii_media, 0, ether_mediachange, ether_mediastatus); 240 mii_attach(self, mii, 0xffffffff, MII_PHY_ANY, MII_OFFSET_ANY, 0); 241 242 /* Choose a default media. */ 243 if (LIST_FIRST(&mii->mii_phys) == NULL) { 244 ifmedia_add(&mii->mii_media, IFM_ETHER|IFM_NONE, 0, NULL); 245 ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_NONE); 246 } else 247 ifmedia_set(&mii->mii_media, IFM_ETHER|IFM_AUTO); 248 249 if_attach(ifp); 250 if_deferred_start_init(ifp, NULL); 251 ether_ifattach(ifp, laddr); 252 rnd_attach_source(&sc->sc_rnd_source, xname, RND_TYPE_NET, 253 RND_FLAG_DEFAULT); 254 } 255 256 u_int 257 gmac_read_reg(struct gmac_softc *sc, int reg) 258 { 259 return in32rb(sc->sc_reg + reg); 260 } 261 262 void 263 gmac_write_reg(struct gmac_softc *sc, int reg, u_int val) 264 { 265 out32rb(sc->sc_reg + reg, val); 266 } 267 268 void 269 gmac_start_txdma(struct gmac_softc *sc) 270 { 271 u_int x; 272 273 x = gmac_read_reg(sc, GMAC_TXDMACONFIG); 274 x |= 1; 275 gmac_write_reg(sc, GMAC_TXDMACONFIG, x); 276 x = gmac_read_reg(sc, GMAC_TXMACCONFIG); 277 x |= 1; 278 gmac_write_reg(sc, GMAC_TXMACCONFIG, x); 279 } 280 281 void 282 gmac_start_rxdma(struct gmac_softc *sc) 283 { 284 u_int x; 285 286 x = gmac_read_reg(sc, GMAC_RXDMACONFIG); 287 x |= 1; 288 gmac_write_reg(sc, GMAC_RXDMACONFIG, x); 289 x = gmac_read_reg(sc, GMAC_RXMACCONFIG); 290 x |= 1; 291 gmac_write_reg(sc, GMAC_RXMACCONFIG, x); 292 } 293 294 void 295 gmac_stop_txdma(struct gmac_softc *sc) 296 { 297 u_int x; 298 299 x = gmac_read_reg(sc, GMAC_TXDMACONFIG); 300 x &= ~1; 301 gmac_write_reg(sc, GMAC_TXDMACONFIG, x); 302 x = gmac_read_reg(sc, GMAC_TXMACCONFIG); 303 x &= ~1; 304 gmac_write_reg(sc, GMAC_TXMACCONFIG, x); 305 } 306 307 void 308 gmac_stop_rxdma(struct gmac_softc *sc) 309 { 310 u_int x; 311 312 x = gmac_read_reg(sc, GMAC_RXDMACONFIG); 313 x &= ~1; 314 gmac_write_reg(sc, GMAC_RXDMACONFIG, x); 315 x = gmac_read_reg(sc, GMAC_RXMACCONFIG); 316 x &= ~1; 317 gmac_write_reg(sc, GMAC_RXMACCONFIG, x); 318 } 319 320 int 321 gmac_intr(void *v) 322 { 323 struct gmac_softc *sc = v; 324 u_int status; 325 326 status = gmac_read_reg(sc, GMAC_STATUS) & 0xff; 327 if (status == 0) 328 return 0; 329 330 if (status & GMAC_INT_RXDONE) 331 gmac_rint(sc); 332 333 if (status & GMAC_INT_TXEMPTY) 334 gmac_tint(sc); 335 336 rnd_add_uint32(&sc->sc_rnd_source, status); 337 return 1; 338 } 339 340 void 341 gmac_tint(struct gmac_softc *sc) 342 { 343 struct ifnet *ifp = &sc->sc_if; 344 345 ifp->if_flags &= ~IFF_OACTIVE; 346 ifp->if_timer = 0; 347 if_schedule_deferred_start(ifp); 348 } 349 350 void 351 gmac_rint(struct gmac_softc *sc) 352 { 353 struct ifnet *ifp = &sc->sc_if; 354 volatile struct gmac_dma *dp; 355 struct mbuf *m; 356 int i, j, len; 357 u_int cmd; 358 359 for (i = sc->sc_rxlast;; i++) { 360 if (i == NRXBUF) 361 i = 0; 362 363 dp = &sc->sc_rxlist[i]; 364 cmd = le32toh(dp->cmd); 365 if (cmd & GMAC_OWN) 366 break; 367 len = (cmd >> 16) & GMAC_LEN_MASK; 368 len -= 4; /* CRC */ 369 370 if (le32toh(dp->cmd_hi) & 0x40000000) { 371 ifp->if_ierrors++; 372 goto next; 373 } 374 375 m = gmac_get(sc, sc->sc_rxbuf[i], len); 376 if (m == NULL) { 377 ifp->if_ierrors++; 378 goto next; 379 } 380 381 if_percpuq_enqueue(ifp->if_percpuq, m); 382 383 next: 384 dp->cmd_hi = 0; 385 __asm volatile ("sync"); 386 dp->cmd = htole32(GMAC_OWN); 387 } 388 sc->sc_rxlast = i; 389 390 /* XXX Make sure free buffers have GMAC_OWN. */ 391 i++; 392 for (j = 1; j < NRXBUF; j++) { 393 if (i == NRXBUF) 394 i = 0; 395 dp = &sc->sc_rxlist[i++]; 396 dp->cmd = htole32(GMAC_OWN); 397 } 398 } 399 400 struct mbuf * 401 gmac_get(struct gmac_softc *sc, void *pkt, int totlen) 402 { 403 struct mbuf *m; 404 struct mbuf *top, **mp; 405 int len; 406 407 MGETHDR(m, M_DONTWAIT, MT_DATA); 408 if (m == 0) 409 return 0; 410 m_set_rcvif(m, &sc->sc_if); 411 m->m_pkthdr.len = totlen; 412 len = MHLEN; 413 top = 0; 414 mp = ⊤ 415 416 while (totlen > 0) { 417 if (top) { 418 MGET(m, M_DONTWAIT, MT_DATA); 419 if (m == 0) { 420 m_freem(top); 421 return 0; 422 } 423 len = MLEN; 424 } 425 if (totlen >= MINCLSIZE) { 426 MCLGET(m, M_DONTWAIT); 427 if ((m->m_flags & M_EXT) == 0) { 428 m_free(m); 429 m_freem(top); 430 return 0; 431 } 432 len = MCLBYTES; 433 } 434 m->m_len = len = min(totlen, len); 435 memcpy(mtod(m, void *), pkt, len); 436 pkt += len; 437 totlen -= len; 438 *mp = m; 439 mp = &m->m_next; 440 } 441 442 return top; 443 } 444 445 void 446 gmac_start(struct ifnet *ifp) 447 { 448 struct gmac_softc *sc = ifp->if_softc; 449 struct mbuf *m; 450 void *buff; 451 int i, tlen; 452 volatile struct gmac_dma *dp; 453 454 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 455 return; 456 457 for (;;) { 458 if (ifp->if_flags & IFF_OACTIVE) 459 break; 460 461 IFQ_DEQUEUE(&ifp->if_snd, m); 462 if (m == 0) 463 break; 464 465 /* 5 seconds to watch for failing to transmit */ 466 ifp->if_timer = 5; 467 ifp->if_opackets++; /* # of pkts */ 468 469 i = sc->sc_txnext; 470 buff = sc->sc_txbuf[i]; 471 tlen = gmac_put(sc, buff, m); 472 473 dp = &sc->sc_txlist[i]; 474 dp->cmd_hi = 0; 475 dp->address_hi = 0; 476 dp->cmd = htole32(tlen | GMAC_OWN | GMAC_SOP); 477 478 i++; 479 if (i == NTXBUF) 480 i = 0; 481 __asm volatile ("sync"); 482 483 gmac_write_reg(sc, GMAC_TXDMAKICK, i); 484 sc->sc_txnext = i; 485 486 /* 487 * If BPF is listening on this interface, let it see the 488 * packet before we commit it to the wire. 489 */ 490 bpf_mtap(ifp, m); 491 m_freem(m); 492 493 i++; 494 if (i == NTXBUF) 495 i = 0; 496 if (i == gmac_read_reg(sc, GMAC_TXDMACOMPLETE)) { 497 ifp->if_flags |= IFF_OACTIVE; 498 break; 499 } 500 } 501 } 502 503 int 504 gmac_put(struct gmac_softc *sc, void *buff, struct mbuf *m) 505 { 506 int len, tlen = 0; 507 508 for (; m; m = m->m_next) { 509 len = m->m_len; 510 if (len == 0) 511 continue; 512 memcpy(buff, mtod(m, void *), len); 513 buff += len; 514 tlen += len; 515 } 516 if (tlen > 2048) 517 panic("%s: gmac_put packet overflow", device_xname(sc->sc_dev)); 518 519 return tlen; 520 } 521 522 void 523 gmac_reset(struct gmac_softc *sc) 524 { 525 int i, s; 526 527 s = splnet(); 528 529 gmac_stop_txdma(sc); 530 gmac_stop_rxdma(sc); 531 532 gmac_write_reg(sc, GMAC_SOFTWARERESET, 3); 533 for (i = 10; i > 0; i--) { 534 delay(300000); /* XXX long delay */ 535 if ((gmac_read_reg(sc, GMAC_SOFTWARERESET) & 3) == 0) 536 break; 537 } 538 if (i == 0) 539 aprint_error_dev(sc->sc_dev, "reset timeout\n"); 540 541 sc->sc_txnext = 0; 542 sc->sc_rxlast = 0; 543 for (i = 0; i < NRXBUF; i++) 544 sc->sc_rxlist[i].cmd = htole32(GMAC_OWN); 545 __asm volatile ("sync"); 546 547 gmac_write_reg(sc, GMAC_TXDMADESCBASEHI, 0); 548 gmac_write_reg(sc, GMAC_TXDMADESCBASELO, 549 vtophys((vaddr_t)sc->sc_txlist)); 550 gmac_write_reg(sc, GMAC_RXDMADESCBASEHI, 0); 551 gmac_write_reg(sc, GMAC_RXDMADESCBASELO, 552 vtophys((vaddr_t)sc->sc_rxlist)); 553 gmac_write_reg(sc, GMAC_RXDMAKICK, NRXBUF); 554 555 splx(s); 556 } 557 558 void 559 gmac_stop(struct gmac_softc *sc) 560 { 561 struct ifnet *ifp = &sc->sc_if; 562 int s; 563 564 s = splnet(); 565 566 callout_stop(&sc->sc_tick_ch); 567 mii_down(&sc->sc_mii); 568 569 gmac_stop_txdma(sc); 570 gmac_stop_rxdma(sc); 571 572 gmac_write_reg(sc, GMAC_INTMASK, 0xffffffff); 573 574 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING); 575 ifp->if_timer = 0; 576 577 splx(s); 578 } 579 580 void 581 gmac_init_mac(struct gmac_softc *sc) 582 { 583 int i, tb; 584 char *laddr = sc->sc_laddr; 585 586 if ((mfpvr() >> 16) == MPC601) 587 tb = mfrtcl(); 588 else 589 tb = mftbl(); 590 gmac_write_reg(sc, GMAC_RANDOMSEED, tb); 591 592 /* init-mii */ 593 gmac_write_reg(sc, GMAC_DATAPATHMODE, 4); 594 gmac_mii_writereg(&sc->sc_dev, 0, 0, 0x1000); 595 596 gmac_write_reg(sc, GMAC_TXDMACONFIG, 0xffc00); 597 gmac_write_reg(sc, GMAC_RXDMACONFIG, 0); 598 gmac_write_reg(sc, GMAC_MACPAUSE, 0x1bf0); 599 gmac_write_reg(sc, GMAC_INTERPACKETGAP0, 0); 600 gmac_write_reg(sc, GMAC_INTERPACKETGAP1, 8); 601 gmac_write_reg(sc, GMAC_INTERPACKETGAP2, 4); 602 gmac_write_reg(sc, GMAC_MINFRAMESIZE, ETHER_MIN_LEN); 603 gmac_write_reg(sc, GMAC_MAXFRAMESIZE, ETHER_MAX_LEN); 604 gmac_write_reg(sc, GMAC_PASIZE, 7); 605 gmac_write_reg(sc, GMAC_JAMSIZE, 4); 606 gmac_write_reg(sc, GMAC_ATTEMPTLIMIT,0x10); 607 gmac_write_reg(sc, GMAC_MACCNTLTYPE, 0x8808); 608 609 gmac_write_reg(sc, GMAC_MACADDRESS0, (laddr[4] << 8) | laddr[5]); 610 gmac_write_reg(sc, GMAC_MACADDRESS1, (laddr[2] << 8) | laddr[3]); 611 gmac_write_reg(sc, GMAC_MACADDRESS2, (laddr[0] << 8) | laddr[1]); 612 gmac_write_reg(sc, GMAC_MACADDRESS3, 0); 613 gmac_write_reg(sc, GMAC_MACADDRESS4, 0); 614 gmac_write_reg(sc, GMAC_MACADDRESS5, 0); 615 gmac_write_reg(sc, GMAC_MACADDRESS6, 1); 616 gmac_write_reg(sc, GMAC_MACADDRESS7, 0xc200); 617 gmac_write_reg(sc, GMAC_MACADDRESS8, 0x0180); 618 gmac_write_reg(sc, GMAC_MACADDRFILT0, 0); 619 gmac_write_reg(sc, GMAC_MACADDRFILT1, 0); 620 gmac_write_reg(sc, GMAC_MACADDRFILT2, 0); 621 gmac_write_reg(sc, GMAC_MACADDRFILT2_1MASK, 0); 622 gmac_write_reg(sc, GMAC_MACADDRFILT0MASK, 0); 623 624 for (i = 0; i < 0x6c; i += 4) 625 gmac_write_reg(sc, GMAC_HASHTABLE0 + i, 0); 626 627 gmac_write_reg(sc, GMAC_SLOTTIME, 0x40); 628 629 if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) { 630 gmac_write_reg(sc, GMAC_TXMACCONFIG, 6); 631 gmac_write_reg(sc, GMAC_XIFCONFIG, 1); 632 } else { 633 gmac_write_reg(sc, GMAC_TXMACCONFIG, 0); 634 gmac_write_reg(sc, GMAC_XIFCONFIG, 5); 635 } 636 637 if (0) /* g-bit? */ 638 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3); 639 else 640 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0); 641 } 642 643 void 644 gmac_setladrf(struct gmac_softc *sc) 645 { 646 struct ifnet *ifp = &sc->sc_if; 647 struct ether_multi *enm; 648 struct ether_multistep step; 649 struct ethercom *ec = &sc->sc_ethercom; 650 u_int32_t crc; 651 u_int32_t hash[16]; 652 u_int v; 653 int i; 654 655 /* Clear hash table */ 656 for (i = 0; i < 16; i++) 657 hash[i] = 0; 658 659 /* Get current RX configuration */ 660 v = gmac_read_reg(sc, GMAC_RXMACCONFIG); 661 662 if ((ifp->if_flags & IFF_PROMISC) != 0) { 663 /* Turn on promiscuous mode; turn off the hash filter */ 664 v |= GMAC_RXMAC_PR; 665 v &= ~GMAC_RXMAC_HEN; 666 ifp->if_flags |= IFF_ALLMULTI; 667 goto chipit; 668 } 669 670 /* Turn off promiscuous mode; turn on the hash filter */ 671 v &= ~GMAC_RXMAC_PR; 672 v |= GMAC_RXMAC_HEN; 673 674 /* 675 * Set up multicast address filter by passing all multicast addresses 676 * through a crc generator, and then using the high order 8 bits as an 677 * index into the 256 bit logical address filter. The high order bit 678 * selects the word, while the rest of the bits select the bit within 679 * the word. 680 */ 681 682 ETHER_FIRST_MULTI(step, ec, enm); 683 while (enm != NULL) { 684 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6)) { 685 /* 686 * We must listen to a range of multicast addresses. 687 * For now, just accept all multicasts, rather than 688 * trying to set only those filter bits needed to match 689 * the range. (At this time, the only use of address 690 * ranges is for IP multicast routing, for which the 691 * range is big enough to require all bits set.) 692 */ 693 for (i = 0; i < 16; i++) 694 hash[i] = 0xffff; 695 ifp->if_flags |= IFF_ALLMULTI; 696 goto chipit; 697 } 698 699 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN); 700 701 /* Just want the 8 most significant bits. */ 702 crc >>= 24; 703 704 /* Set the corresponding bit in the filter. */ 705 hash[crc >> 4] |= 1 << (crc & 0xf); 706 707 ETHER_NEXT_MULTI(step, enm); 708 } 709 710 ifp->if_flags &= ~IFF_ALLMULTI; 711 712 chipit: 713 /* Now load the hash table into the chip */ 714 for (i = 0; i < 16; i++) 715 gmac_write_reg(sc, GMAC_HASHTABLE0 + i * 4, hash[i]); 716 717 gmac_write_reg(sc, GMAC_RXMACCONFIG, v); 718 } 719 720 void 721 gmac_init(struct gmac_softc *sc) 722 { 723 struct ifnet *ifp = &sc->sc_if; 724 725 gmac_stop_txdma(sc); 726 gmac_stop_rxdma(sc); 727 728 gmac_init_mac(sc); 729 gmac_setladrf(sc); 730 731 gmac_start_txdma(sc); 732 gmac_start_rxdma(sc); 733 734 gmac_write_reg(sc, GMAC_INTMASK, ~(GMAC_INT_TXEMPTY | GMAC_INT_RXDONE)); 735 736 ifp->if_flags |= IFF_RUNNING; 737 ifp->if_flags &= ~IFF_OACTIVE; 738 ifp->if_timer = 0; 739 740 callout_reset(&sc->sc_tick_ch, 1, gmac_mii_tick, sc); 741 742 gmac_start(ifp); 743 } 744 745 int 746 gmac_ioctl(struct ifnet *ifp, unsigned long cmd, void *data) 747 { 748 struct gmac_softc *sc = ifp->if_softc; 749 struct ifaddr *ifa = (struct ifaddr *)data; 750 struct ifreq *ifr = (struct ifreq *)data; 751 int s, error = 0; 752 753 s = splnet(); 754 755 switch (cmd) { 756 757 case SIOCINITIFADDR: 758 ifp->if_flags |= IFF_UP; 759 760 gmac_init(sc); 761 switch (ifa->ifa_addr->sa_family) { 762 #ifdef INET 763 case AF_INET: 764 arp_ifinit(ifp, ifa); 765 break; 766 #endif 767 default: 768 break; 769 } 770 break; 771 772 case SIOCSIFFLAGS: 773 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 774 break; 775 /* XXX see the comment in ed_ioctl() about code re-use */ 776 if ((ifp->if_flags & IFF_UP) == 0 && 777 (ifp->if_flags & IFF_RUNNING) != 0) { 778 /* 779 * If interface is marked down and it is running, then 780 * stop it. 781 */ 782 gmac_stop(sc); 783 ifp->if_flags &= ~IFF_RUNNING; 784 } else if ((ifp->if_flags & IFF_UP) != 0 && 785 (ifp->if_flags & IFF_RUNNING) == 0) { 786 /* 787 * If interface is marked up and it is stopped, then 788 * start it. 789 */ 790 gmac_init(sc); 791 } else { 792 /* 793 * Reset the interface to pick up changes in any other 794 * flags that affect hardware registers. 795 */ 796 gmac_reset(sc); 797 gmac_init(sc); 798 } 799 #ifdef GMAC_DEBUG 800 if (ifp->if_flags & IFF_DEBUG) 801 sc->sc_flags |= GMAC_DEBUGFLAG; 802 #endif 803 break; 804 805 case SIOCADDMULTI: 806 case SIOCDELMULTI: 807 case SIOCGIFMEDIA: 808 case SIOCSIFMEDIA: 809 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 810 /* 811 * Multicast list has changed; set the hardware filter 812 * accordingly. 813 */ 814 if (ifp->if_flags & IFF_RUNNING) { 815 gmac_init(sc); 816 /* gmac_setladrf(sc); */ 817 } 818 error = 0; 819 } 820 break; 821 default: 822 error = ether_ioctl(ifp, cmd, data); 823 break; 824 } 825 826 splx(s); 827 return error; 828 } 829 830 void 831 gmac_watchdog(struct ifnet *ifp) 832 { 833 struct gmac_softc *sc = ifp->if_softc; 834 835 printf("%s: device timeout\n", ifp->if_xname); 836 ifp->if_oerrors++; 837 838 gmac_reset(sc); 839 gmac_init(sc); 840 } 841 842 int 843 gmac_mii_readreg(device_t self, int phy, int reg) 844 { 845 struct gmac_softc *sc = device_private(self); 846 int i; 847 848 gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT, 849 0x60020000 | (phy << 23) | (reg << 18)); 850 851 for (i = 1000; i >= 0; i -= 10) { 852 if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000) 853 break; 854 delay(10); 855 } 856 if (i < 0) { 857 aprint_error_dev(sc->sc_dev, "gmac_mii_readreg: timeout\n"); 858 return 0; 859 } 860 861 return gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0xffff; 862 } 863 864 void 865 gmac_mii_writereg(device_t self, int phy, int reg, int val) 866 { 867 struct gmac_softc *sc = device_private(self); 868 int i; 869 870 gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT, 871 0x50020000 | (phy << 23) | (reg << 18) | (val & 0xffff)); 872 873 for (i = 1000; i >= 0; i -= 10) { 874 if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000) 875 break; 876 delay(10); 877 } 878 if (i < 0) 879 aprint_error_dev(sc->sc_dev, "gmac_mii_writereg: timeout\n"); 880 } 881 882 void 883 gmac_mii_statchg(struct ifnet *ifp) 884 { 885 struct gmac_softc *sc = ifp->if_softc; 886 887 gmac_stop_txdma(sc); 888 gmac_stop_rxdma(sc); 889 890 if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) { 891 gmac_write_reg(sc, GMAC_TXMACCONFIG, 6); 892 gmac_write_reg(sc, GMAC_XIFCONFIG, 1); 893 } else { 894 gmac_write_reg(sc, GMAC_TXMACCONFIG, 0); 895 gmac_write_reg(sc, GMAC_XIFCONFIG, 5); 896 } 897 898 if (0) /* g-bit? */ 899 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3); 900 else 901 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0); 902 903 gmac_start_txdma(sc); 904 gmac_start_rxdma(sc); 905 } 906 907 void 908 gmac_mii_tick(void *v) 909 { 910 struct gmac_softc *sc = v; 911 int s; 912 913 s = splnet(); 914 mii_tick(&sc->sc_mii); 915 splx(s); 916 917 callout_reset(&sc->sc_tick_ch, hz, gmac_mii_tick, sc); 918 } 919