1 /* $NetBSD: if_gm.c,v 1.48 2016/12/08 01:12:00 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.48 2016/12/08 01:12:00 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 /* 382 * Check if there's a BPF listener on this interface. 383 * If so, hand off the raw packet to BPF. 384 */ 385 bpf_mtap(ifp, m); 386 if_percpuq_enqueue(ifp->if_percpuq, m); 387 ifp->if_ipackets++; 388 389 next: 390 dp->cmd_hi = 0; 391 __asm volatile ("sync"); 392 dp->cmd = htole32(GMAC_OWN); 393 } 394 sc->sc_rxlast = i; 395 396 /* XXX Make sure free buffers have GMAC_OWN. */ 397 i++; 398 for (j = 1; j < NRXBUF; j++) { 399 if (i == NRXBUF) 400 i = 0; 401 dp = &sc->sc_rxlist[i++]; 402 dp->cmd = htole32(GMAC_OWN); 403 } 404 } 405 406 struct mbuf * 407 gmac_get(struct gmac_softc *sc, void *pkt, int totlen) 408 { 409 struct mbuf *m; 410 struct mbuf *top, **mp; 411 int len; 412 413 MGETHDR(m, M_DONTWAIT, MT_DATA); 414 if (m == 0) 415 return 0; 416 m_set_rcvif(m, &sc->sc_if); 417 m->m_pkthdr.len = totlen; 418 len = MHLEN; 419 top = 0; 420 mp = ⊤ 421 422 while (totlen > 0) { 423 if (top) { 424 MGET(m, M_DONTWAIT, MT_DATA); 425 if (m == 0) { 426 m_freem(top); 427 return 0; 428 } 429 len = MLEN; 430 } 431 if (totlen >= MINCLSIZE) { 432 MCLGET(m, M_DONTWAIT); 433 if ((m->m_flags & M_EXT) == 0) { 434 m_free(m); 435 m_freem(top); 436 return 0; 437 } 438 len = MCLBYTES; 439 } 440 m->m_len = len = min(totlen, len); 441 memcpy(mtod(m, void *), pkt, len); 442 pkt += len; 443 totlen -= len; 444 *mp = m; 445 mp = &m->m_next; 446 } 447 448 return top; 449 } 450 451 void 452 gmac_start(struct ifnet *ifp) 453 { 454 struct gmac_softc *sc = ifp->if_softc; 455 struct mbuf *m; 456 void *buff; 457 int i, tlen; 458 volatile struct gmac_dma *dp; 459 460 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 461 return; 462 463 for (;;) { 464 if (ifp->if_flags & IFF_OACTIVE) 465 break; 466 467 IFQ_DEQUEUE(&ifp->if_snd, m); 468 if (m == 0) 469 break; 470 471 /* 5 seconds to watch for failing to transmit */ 472 ifp->if_timer = 5; 473 ifp->if_opackets++; /* # of pkts */ 474 475 i = sc->sc_txnext; 476 buff = sc->sc_txbuf[i]; 477 tlen = gmac_put(sc, buff, m); 478 479 dp = &sc->sc_txlist[i]; 480 dp->cmd_hi = 0; 481 dp->address_hi = 0; 482 dp->cmd = htole32(tlen | GMAC_OWN | GMAC_SOP); 483 484 i++; 485 if (i == NTXBUF) 486 i = 0; 487 __asm volatile ("sync"); 488 489 gmac_write_reg(sc, GMAC_TXDMAKICK, i); 490 sc->sc_txnext = i; 491 492 /* 493 * If BPF is listening on this interface, let it see the 494 * packet before we commit it to the wire. 495 */ 496 bpf_mtap(ifp, m); 497 m_freem(m); 498 499 i++; 500 if (i == NTXBUF) 501 i = 0; 502 if (i == gmac_read_reg(sc, GMAC_TXDMACOMPLETE)) { 503 ifp->if_flags |= IFF_OACTIVE; 504 break; 505 } 506 } 507 } 508 509 int 510 gmac_put(struct gmac_softc *sc, void *buff, struct mbuf *m) 511 { 512 int len, tlen = 0; 513 514 for (; m; m = m->m_next) { 515 len = m->m_len; 516 if (len == 0) 517 continue; 518 memcpy(buff, mtod(m, void *), len); 519 buff += len; 520 tlen += len; 521 } 522 if (tlen > 2048) 523 panic("%s: gmac_put packet overflow", device_xname(sc->sc_dev)); 524 525 return tlen; 526 } 527 528 void 529 gmac_reset(struct gmac_softc *sc) 530 { 531 int i, s; 532 533 s = splnet(); 534 535 gmac_stop_txdma(sc); 536 gmac_stop_rxdma(sc); 537 538 gmac_write_reg(sc, GMAC_SOFTWARERESET, 3); 539 for (i = 10; i > 0; i--) { 540 delay(300000); /* XXX long delay */ 541 if ((gmac_read_reg(sc, GMAC_SOFTWARERESET) & 3) == 0) 542 break; 543 } 544 if (i == 0) 545 aprint_error_dev(sc->sc_dev, "reset timeout\n"); 546 547 sc->sc_txnext = 0; 548 sc->sc_rxlast = 0; 549 for (i = 0; i < NRXBUF; i++) 550 sc->sc_rxlist[i].cmd = htole32(GMAC_OWN); 551 __asm volatile ("sync"); 552 553 gmac_write_reg(sc, GMAC_TXDMADESCBASEHI, 0); 554 gmac_write_reg(sc, GMAC_TXDMADESCBASELO, 555 vtophys((vaddr_t)sc->sc_txlist)); 556 gmac_write_reg(sc, GMAC_RXDMADESCBASEHI, 0); 557 gmac_write_reg(sc, GMAC_RXDMADESCBASELO, 558 vtophys((vaddr_t)sc->sc_rxlist)); 559 gmac_write_reg(sc, GMAC_RXDMAKICK, NRXBUF); 560 561 splx(s); 562 } 563 564 void 565 gmac_stop(struct gmac_softc *sc) 566 { 567 struct ifnet *ifp = &sc->sc_if; 568 int s; 569 570 s = splnet(); 571 572 callout_stop(&sc->sc_tick_ch); 573 mii_down(&sc->sc_mii); 574 575 gmac_stop_txdma(sc); 576 gmac_stop_rxdma(sc); 577 578 gmac_write_reg(sc, GMAC_INTMASK, 0xffffffff); 579 580 ifp->if_flags &= ~(IFF_UP | IFF_RUNNING); 581 ifp->if_timer = 0; 582 583 splx(s); 584 } 585 586 void 587 gmac_init_mac(struct gmac_softc *sc) 588 { 589 int i, tb; 590 char *laddr = sc->sc_laddr; 591 592 if ((mfpvr() >> 16) == MPC601) 593 tb = mfrtcl(); 594 else 595 tb = mftbl(); 596 gmac_write_reg(sc, GMAC_RANDOMSEED, tb); 597 598 /* init-mii */ 599 gmac_write_reg(sc, GMAC_DATAPATHMODE, 4); 600 gmac_mii_writereg(&sc->sc_dev, 0, 0, 0x1000); 601 602 gmac_write_reg(sc, GMAC_TXDMACONFIG, 0xffc00); 603 gmac_write_reg(sc, GMAC_RXDMACONFIG, 0); 604 gmac_write_reg(sc, GMAC_MACPAUSE, 0x1bf0); 605 gmac_write_reg(sc, GMAC_INTERPACKETGAP0, 0); 606 gmac_write_reg(sc, GMAC_INTERPACKETGAP1, 8); 607 gmac_write_reg(sc, GMAC_INTERPACKETGAP2, 4); 608 gmac_write_reg(sc, GMAC_MINFRAMESIZE, ETHER_MIN_LEN); 609 gmac_write_reg(sc, GMAC_MAXFRAMESIZE, ETHER_MAX_LEN); 610 gmac_write_reg(sc, GMAC_PASIZE, 7); 611 gmac_write_reg(sc, GMAC_JAMSIZE, 4); 612 gmac_write_reg(sc, GMAC_ATTEMPTLIMIT,0x10); 613 gmac_write_reg(sc, GMAC_MACCNTLTYPE, 0x8808); 614 615 gmac_write_reg(sc, GMAC_MACADDRESS0, (laddr[4] << 8) | laddr[5]); 616 gmac_write_reg(sc, GMAC_MACADDRESS1, (laddr[2] << 8) | laddr[3]); 617 gmac_write_reg(sc, GMAC_MACADDRESS2, (laddr[0] << 8) | laddr[1]); 618 gmac_write_reg(sc, GMAC_MACADDRESS3, 0); 619 gmac_write_reg(sc, GMAC_MACADDRESS4, 0); 620 gmac_write_reg(sc, GMAC_MACADDRESS5, 0); 621 gmac_write_reg(sc, GMAC_MACADDRESS6, 1); 622 gmac_write_reg(sc, GMAC_MACADDRESS7, 0xc200); 623 gmac_write_reg(sc, GMAC_MACADDRESS8, 0x0180); 624 gmac_write_reg(sc, GMAC_MACADDRFILT0, 0); 625 gmac_write_reg(sc, GMAC_MACADDRFILT1, 0); 626 gmac_write_reg(sc, GMAC_MACADDRFILT2, 0); 627 gmac_write_reg(sc, GMAC_MACADDRFILT2_1MASK, 0); 628 gmac_write_reg(sc, GMAC_MACADDRFILT0MASK, 0); 629 630 for (i = 0; i < 0x6c; i += 4) 631 gmac_write_reg(sc, GMAC_HASHTABLE0 + i, 0); 632 633 gmac_write_reg(sc, GMAC_SLOTTIME, 0x40); 634 635 if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) { 636 gmac_write_reg(sc, GMAC_TXMACCONFIG, 6); 637 gmac_write_reg(sc, GMAC_XIFCONFIG, 1); 638 } else { 639 gmac_write_reg(sc, GMAC_TXMACCONFIG, 0); 640 gmac_write_reg(sc, GMAC_XIFCONFIG, 5); 641 } 642 643 if (0) /* g-bit? */ 644 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3); 645 else 646 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0); 647 } 648 649 void 650 gmac_setladrf(struct gmac_softc *sc) 651 { 652 struct ifnet *ifp = &sc->sc_if; 653 struct ether_multi *enm; 654 struct ether_multistep step; 655 struct ethercom *ec = &sc->sc_ethercom; 656 u_int32_t crc; 657 u_int32_t hash[16]; 658 u_int v; 659 int i; 660 661 /* Clear hash table */ 662 for (i = 0; i < 16; i++) 663 hash[i] = 0; 664 665 /* Get current RX configuration */ 666 v = gmac_read_reg(sc, GMAC_RXMACCONFIG); 667 668 if ((ifp->if_flags & IFF_PROMISC) != 0) { 669 /* Turn on promiscuous mode; turn off the hash filter */ 670 v |= GMAC_RXMAC_PR; 671 v &= ~GMAC_RXMAC_HEN; 672 ifp->if_flags |= IFF_ALLMULTI; 673 goto chipit; 674 } 675 676 /* Turn off promiscuous mode; turn on the hash filter */ 677 v &= ~GMAC_RXMAC_PR; 678 v |= GMAC_RXMAC_HEN; 679 680 /* 681 * Set up multicast address filter by passing all multicast addresses 682 * through a crc generator, and then using the high order 8 bits as an 683 * index into the 256 bit logical address filter. The high order bit 684 * selects the word, while the rest of the bits select the bit within 685 * the word. 686 */ 687 688 ETHER_FIRST_MULTI(step, ec, enm); 689 while (enm != NULL) { 690 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6)) { 691 /* 692 * We must listen to a range of multicast addresses. 693 * For now, just accept all multicasts, rather than 694 * trying to set only those filter bits needed to match 695 * the range. (At this time, the only use of address 696 * ranges is for IP multicast routing, for which the 697 * range is big enough to require all bits set.) 698 */ 699 for (i = 0; i < 16; i++) 700 hash[i] = 0xffff; 701 ifp->if_flags |= IFF_ALLMULTI; 702 goto chipit; 703 } 704 705 crc = ether_crc32_le(enm->enm_addrlo, ETHER_ADDR_LEN); 706 707 /* Just want the 8 most significant bits. */ 708 crc >>= 24; 709 710 /* Set the corresponding bit in the filter. */ 711 hash[crc >> 4] |= 1 << (crc & 0xf); 712 713 ETHER_NEXT_MULTI(step, enm); 714 } 715 716 ifp->if_flags &= ~IFF_ALLMULTI; 717 718 chipit: 719 /* Now load the hash table into the chip */ 720 for (i = 0; i < 16; i++) 721 gmac_write_reg(sc, GMAC_HASHTABLE0 + i * 4, hash[i]); 722 723 gmac_write_reg(sc, GMAC_RXMACCONFIG, v); 724 } 725 726 void 727 gmac_init(struct gmac_softc *sc) 728 { 729 struct ifnet *ifp = &sc->sc_if; 730 731 gmac_stop_txdma(sc); 732 gmac_stop_rxdma(sc); 733 734 gmac_init_mac(sc); 735 gmac_setladrf(sc); 736 737 gmac_start_txdma(sc); 738 gmac_start_rxdma(sc); 739 740 gmac_write_reg(sc, GMAC_INTMASK, ~(GMAC_INT_TXEMPTY | GMAC_INT_RXDONE)); 741 742 ifp->if_flags |= IFF_RUNNING; 743 ifp->if_flags &= ~IFF_OACTIVE; 744 ifp->if_timer = 0; 745 746 callout_reset(&sc->sc_tick_ch, 1, gmac_mii_tick, sc); 747 748 gmac_start(ifp); 749 } 750 751 int 752 gmac_ioctl(struct ifnet *ifp, unsigned long cmd, void *data) 753 { 754 struct gmac_softc *sc = ifp->if_softc; 755 struct ifaddr *ifa = (struct ifaddr *)data; 756 struct ifreq *ifr = (struct ifreq *)data; 757 int s, error = 0; 758 759 s = splnet(); 760 761 switch (cmd) { 762 763 case SIOCINITIFADDR: 764 ifp->if_flags |= IFF_UP; 765 766 gmac_init(sc); 767 switch (ifa->ifa_addr->sa_family) { 768 #ifdef INET 769 case AF_INET: 770 arp_ifinit(ifp, ifa); 771 break; 772 #endif 773 default: 774 break; 775 } 776 break; 777 778 case SIOCSIFFLAGS: 779 if ((error = ifioctl_common(ifp, cmd, data)) != 0) 780 break; 781 /* XXX see the comment in ed_ioctl() about code re-use */ 782 if ((ifp->if_flags & IFF_UP) == 0 && 783 (ifp->if_flags & IFF_RUNNING) != 0) { 784 /* 785 * If interface is marked down and it is running, then 786 * stop it. 787 */ 788 gmac_stop(sc); 789 ifp->if_flags &= ~IFF_RUNNING; 790 } else if ((ifp->if_flags & IFF_UP) != 0 && 791 (ifp->if_flags & IFF_RUNNING) == 0) { 792 /* 793 * If interface is marked up and it is stopped, then 794 * start it. 795 */ 796 gmac_init(sc); 797 } else { 798 /* 799 * Reset the interface to pick up changes in any other 800 * flags that affect hardware registers. 801 */ 802 gmac_reset(sc); 803 gmac_init(sc); 804 } 805 #ifdef GMAC_DEBUG 806 if (ifp->if_flags & IFF_DEBUG) 807 sc->sc_flags |= GMAC_DEBUGFLAG; 808 #endif 809 break; 810 811 case SIOCADDMULTI: 812 case SIOCDELMULTI: 813 case SIOCGIFMEDIA: 814 case SIOCSIFMEDIA: 815 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 816 /* 817 * Multicast list has changed; set the hardware filter 818 * accordingly. 819 */ 820 if (ifp->if_flags & IFF_RUNNING) { 821 gmac_init(sc); 822 /* gmac_setladrf(sc); */ 823 } 824 error = 0; 825 } 826 break; 827 default: 828 error = ether_ioctl(ifp, cmd, data); 829 break; 830 } 831 832 splx(s); 833 return error; 834 } 835 836 void 837 gmac_watchdog(struct ifnet *ifp) 838 { 839 struct gmac_softc *sc = ifp->if_softc; 840 841 printf("%s: device timeout\n", ifp->if_xname); 842 ifp->if_oerrors++; 843 844 gmac_reset(sc); 845 gmac_init(sc); 846 } 847 848 int 849 gmac_mii_readreg(device_t self, int phy, int reg) 850 { 851 struct gmac_softc *sc = device_private(self); 852 int i; 853 854 gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT, 855 0x60020000 | (phy << 23) | (reg << 18)); 856 857 for (i = 1000; i >= 0; i -= 10) { 858 if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000) 859 break; 860 delay(10); 861 } 862 if (i < 0) { 863 aprint_error_dev(sc->sc_dev, "gmac_mii_readreg: timeout\n"); 864 return 0; 865 } 866 867 return gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0xffff; 868 } 869 870 void 871 gmac_mii_writereg(device_t self, int phy, int reg, int val) 872 { 873 struct gmac_softc *sc = device_private(self); 874 int i; 875 876 gmac_write_reg(sc, GMAC_MIFFRAMEOUTPUT, 877 0x50020000 | (phy << 23) | (reg << 18) | (val & 0xffff)); 878 879 for (i = 1000; i >= 0; i -= 10) { 880 if (gmac_read_reg(sc, GMAC_MIFFRAMEOUTPUT) & 0x10000) 881 break; 882 delay(10); 883 } 884 if (i < 0) 885 aprint_error_dev(sc->sc_dev, "gmac_mii_writereg: timeout\n"); 886 } 887 888 void 889 gmac_mii_statchg(struct ifnet *ifp) 890 { 891 struct gmac_softc *sc = ifp->if_softc; 892 893 gmac_stop_txdma(sc); 894 gmac_stop_rxdma(sc); 895 896 if (IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) { 897 gmac_write_reg(sc, GMAC_TXMACCONFIG, 6); 898 gmac_write_reg(sc, GMAC_XIFCONFIG, 1); 899 } else { 900 gmac_write_reg(sc, GMAC_TXMACCONFIG, 0); 901 gmac_write_reg(sc, GMAC_XIFCONFIG, 5); 902 } 903 904 if (0) /* g-bit? */ 905 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 3); 906 else 907 gmac_write_reg(sc, GMAC_MACCTRLCONFIG, 0); 908 909 gmac_start_txdma(sc); 910 gmac_start_rxdma(sc); 911 } 912 913 void 914 gmac_mii_tick(void *v) 915 { 916 struct gmac_softc *sc = v; 917 int s; 918 919 s = splnet(); 920 mii_tick(&sc->sc_mii); 921 splx(s); 922 923 callout_reset(&sc->sc_tick_ch, hz, gmac_mii_tick, sc); 924 } 925