if_ate.c revision 155324
1/*- 2 * Copyright (c) 2006 M. Warner Losh. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25/* TODO: (in no order) 26 * 27 * 5) Setup RX buffers in ateinit_locked 28 * 8) Need to sync busdma goo in atestop 29 * 9) atestop should maybe free the mbufs? 30 * 10) On Rx, how do we get a new mbuf? 31 * 32 * 1) detach 33 * 2) Free dma setup 34 * 3) Turn on the clock in pmc and turn on pins? Turn off? 35 */ 36 37#include <sys/cdefs.h> 38__FBSDID("$FreeBSD: head/sys/arm/at91/if_ate.c 155324 2006-02-04 23:32:13Z imp $"); 39 40#include <sys/param.h> 41#include <sys/systm.h> 42#include <sys/bus.h> 43#include <sys/kernel.h> 44#include <sys/mbuf.h> 45#include <sys/malloc.h> 46#include <sys/module.h> 47#include <sys/rman.h> 48#include <sys/socket.h> 49#include <sys/sockio.h> 50#include <machine/bus.h> 51 52#include <net/ethernet.h> 53#include <net/if.h> 54#include <net/if_arp.h> 55#include <net/if_dl.h> 56#include <net/if_media.h> 57#include <net/if_mib.h> 58#include <net/if_types.h> 59 60#ifdef INET 61#include <netinet/in.h> 62#include <netinet/in_systm.h> 63#include <netinet/in_var.h> 64#include <netinet/ip.h> 65#endif 66 67#include <net/bpf.h> 68#include <net/bpfdesc.h> 69 70#include <dev/mii/mii.h> 71#include <dev/mii/miivar.h> 72#include <arm/at91/if_atereg.h> 73 74#include "miibus_if.h" 75 76#define ATE_MAX_TX_BUFFERS 2 /* We have ping-pong tx buffers */ 77#define ATE_MAX_RX_BUFFERS 8 78 79struct ate_softc 80{ 81 struct ifnet *ifp; /* ifnet pointer */ 82 struct mtx sc_mtx; /* basically a perimeter lock */ 83 device_t dev; /* Myself */ 84 device_t miibus; /* My child miibus */ 85 void *intrhand; /* Interrupt handle */ 86 struct resource *irq_res; /* IRQ resource */ 87 struct resource *mem_res; /* Memory resource */ 88 struct callout tick_ch; /* Tick callout */ 89 bus_dma_tag_t mtag; /* bus dma tag for mbufs */ 90 bus_dmamap_t tx_map[ATE_MAX_TX_BUFFERS]; 91 bus_dma_tag_t rxtag; 92 bus_dmamap_t rx_map[ATE_MAX_RX_BUFFERS]; 93 bus_dma_tag_t rx_desc_tag; 94 bus_dmamap_t rx_desc_map; 95 int txcur; /* current tx map pointer */ 96 struct mbuf *sent_mbuf[ATE_MAX_TX_BUFFERS]; /* Sent mbufs */ 97 struct mbuf *rx_mbuf[ATE_MAX_RX_BUFFERS]; /* RX mbufs */ 98 bus_addr_t rx_desc_phys; 99 eth_rx_desc_t *rx_descs; 100 struct ifmib_iso_8802_3 mibdata; /* stuff for network mgmt */ 101}; 102 103static inline uint32_t 104RD4(struct ate_softc *sc, bus_size_t off) 105{ 106 return bus_read_4(sc->mem_res, off); 107} 108 109static inline void 110WR4(struct ate_softc *sc, bus_size_t off, uint32_t val) 111{ 112 bus_write_4(sc->mem_res, off, val); 113} 114 115#define ATE_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 116#define ATE_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 117#define ATE_LOCK_INIT(_sc) \ 118 mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->dev), \ 119 MTX_NETWORK_LOCK, MTX_DEF) 120#define ATE_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx); 121#define ATE_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED); 122#define ATE_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED); 123 124static devclass_t ate_devclass; 125 126/* ifnet entry points */ 127 128static void ateinit_locked(void *); 129static void atestart_locked(struct ifnet *); 130 131static void ateinit(void *); 132static void atestart(struct ifnet *); 133static void atestop(struct ate_softc *); 134static void atewatchdog(struct ifnet *); 135static int ateioctl(struct ifnet * ifp, u_long, caddr_t); 136 137/* bus entry points */ 138 139static int ate_probe(device_t dev); 140static int ate_attach(device_t dev); 141static int ate_detach(device_t dev); 142static void ate_intr(void *); 143 144/* helper routines */ 145static int ate_activate(device_t dev); 146static void ate_deactivate(device_t dev); 147static int ate_ifmedia_upd(struct ifnet *ifp); 148static void ate_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr); 149static void ate_get_mac(struct ate_softc *sc, u_char *eaddr); 150 151/* 152 * The AT91 family of products has the ethernet called EMAC. However, 153 * it isn't self identifying. It is anticipated that the parent bus 154 * code will take care to only add ate devices where they really are. As 155 * such, we do nothing here to identify the device and just set its name. 156 */ 157static int 158ate_probe(device_t dev) 159{ 160 device_set_desc(dev, "EMAC"); 161 return (0); 162} 163 164static int 165ate_attach(device_t dev) 166{ 167 struct ate_softc *sc = device_get_softc(dev); 168 struct ifnet *ifp = NULL; 169 int err; 170 u_char eaddr[6]; 171 172 sc->dev = dev; 173 err = ate_activate(dev); 174 if (err) 175 goto out; 176 177 /* calling atestop before ifp is set is OK */ 178 atestop(sc); 179 ATE_LOCK_INIT(sc); 180 callout_init_mtx(&sc->tick_ch, &sc->sc_mtx, 0); 181 182 ate_get_mac(sc, eaddr); 183 184 if (mii_phy_probe(dev, &sc->miibus, ate_ifmedia_upd, ate_ifmedia_sts)) { 185 device_printf(dev, "Cannot find my PHY.\n"); 186 err = ENXIO; 187 goto out; 188 } 189 190 sc->ifp = ifp = if_alloc(IFT_ETHER); 191 ifp->if_softc = sc; 192 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 193 ifp->if_mtu = ETHERMTU; 194 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 195 ifp->if_start = atestart; 196 ifp->if_ioctl = ateioctl; 197 ifp->if_watchdog = atewatchdog; 198 ifp->if_init = ateinit; 199 ifp->if_baudrate = 10000000; 200 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 201 ifp->if_snd.ifq_maxlen = IFQ_MAXLEN; 202 IFQ_SET_READY(&ifp->if_snd); 203 ifp->if_timer = 0; 204 ifp->if_linkmib = &sc->mibdata; 205 ifp->if_linkmiblen = sizeof(sc->mibdata); 206 sc->mibdata.dot3Compliance = DOT3COMPLIANCE_COLLS; 207 208 ether_ifattach(ifp, eaddr); 209 210 /* 211 * Activate the interrupt 212 */ 213 err = bus_setup_intr(dev, sc->irq_res, INTR_TYPE_NET | INTR_MPSAFE, 214 ate_intr, sc, &sc->intrhand); 215 if (err) { 216 ether_ifdetach(ifp); 217 ATE_LOCK_DESTROY(sc); 218 } 219out:; 220 if (err) 221 ate_deactivate(dev); 222 if (err && ifp) 223 if_free(ifp); 224 return (err); 225} 226 227static int 228ate_detach(device_t dev) 229{ 230 return EBUSY; /* XXX TODO(1) */ 231} 232 233static void 234ate_getaddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 235{ 236 struct ate_softc *sc; 237 238 if (error != 0) 239 return; 240 sc = (struct ate_softc *)arg; 241 sc->rx_desc_phys = segs[0].ds_addr; 242} 243 244/* 245 * Compute the multicast filter for this device using the standard 246 * algorithm. I wonder why this isn't in ether somewhere as a lot 247 * of different MAC chips use this method (or the reverse the bits) 248 * method. 249 */ 250static void 251ate_setmcast(struct ate_softc *sc) 252{ 253 uint32_t index; 254 uint32_t mcaf[2]; 255 u_char *af = (u_char *) mcaf; 256 struct ifmultiaddr *ifma; 257 258 mcaf[0] = 0; 259 mcaf[1] = 0; 260 261 IF_ADDR_LOCK(sc->ifp); 262 TAILQ_FOREACH(ifma, &sc->ifp->if_multiaddrs, ifma_link) { 263 if (ifma->ifma_addr->sa_family != AF_LINK) 264 continue; 265 index = ether_crc32_be(LLADDR((struct sockaddr_dl *) 266 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 267 af[index >> 3] |= 1 << (index & 7); 268 } 269 IF_ADDR_UNLOCK(sc->ifp); 270 271 /* 272 * Write the hash to the hash register. This card can also 273 * accept unicast packets as well as multicast packets using this 274 * register for easier bridging operations, but we don't take 275 * advantage of that. Locks here are to avoid LOR with the 276 * IF_ADDR_LOCK, but might not be strictly necessary. 277 */ 278 ATE_LOCK(sc); 279 WR4(sc, ETH_HSL, mcaf[0]); 280 WR4(sc, ETH_HSH, mcaf[1]); 281 ATE_UNLOCK(sc); 282} 283 284static int 285ate_activate(device_t dev) 286{ 287 struct ate_softc *sc; 288 int rid, err, i; 289 290 sc = device_get_softc(dev); 291 rid = 0; 292 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 293 RF_ACTIVE); 294 if (sc->mem_res == NULL) 295 goto errout; 296 rid = 0; 297 sc->irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 298 RF_ACTIVE); 299 if (sc->mem_res == NULL) 300 goto errout; 301 302 /* 303 * Allocate DMA tags and maps 304 */ 305 err = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 306 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, 307 busdma_lock_mutex, &sc->sc_mtx, &sc->mtag); 308 if (err != 0) 309 goto errout; 310 for (i = 0; i < ATE_MAX_TX_BUFFERS; i++) { 311 err = bus_dmamap_create(sc->mtag, 0, &sc->tx_map[i]); 312 if (err != 0) 313 goto errout; 314 } 315 /* 316 * Allocate our Rx buffers. This chip has a rx structure that's filled 317 * in 318 */ 319 320 /* 321 * Allocate DMA tags and maps for RX. 322 */ 323 err = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 324 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, 325 busdma_lock_mutex, &sc->sc_mtx, &sc->rxtag); 326 if (err != 0) 327 goto errout; 328 for (i = 0; i < ATE_MAX_RX_BUFFERS; i++) { 329 err = bus_dmamap_create(sc->rxtag, 0, &sc->rx_map[i]); 330 if (err != 0) 331 goto errout; 332 } 333 334 /* Dma TAG and MAP for the rx descriptors. */ 335 err = bus_dma_tag_create(NULL, sizeof(eth_rx_desc_t), 0, 336 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 337 ATE_MAX_RX_BUFFERS * sizeof(eth_rx_desc_t), 1, 338 ATE_MAX_RX_BUFFERS * sizeof(eth_rx_desc_t), 0, busdma_lock_mutex, 339 &sc->sc_mtx, &sc->rx_desc_tag); 340 if (err != 0) 341 goto errout; 342 if (bus_dmamem_alloc(sc->rx_desc_tag, (void **)&sc->rx_descs, M_WAITOK, 343 &sc->rx_desc_map) != 0) 344 goto errout; 345 if (bus_dmamap_load(sc->rx_desc_tag, sc->rx_desc_map, 346 sc->rx_descs, ATE_MAX_RX_BUFFERS * sizeof(eth_rx_desc_t), 347 ate_getaddr, sc, 0) != 0) 348 goto errout; 349 /* XXX TODO(5) Put this in ateinit_locked? */ 350 for (i = 0; i < ATE_MAX_RX_BUFFERS; i++) { 351 bus_dma_segment_t seg; 352 int nsegs; 353 354 sc->rx_mbuf[i] = m_getcl(M_WAITOK, MT_DATA, M_PKTHDR); 355 sc->rx_mbuf[i]->m_len = sc->rx_mbuf[i]->m_pkthdr.len = 356 MCLBYTES; 357 if (bus_dmamap_load_mbuf_sg(sc->rxtag, sc->rx_map[i], 358 sc->rx_mbuf[i], &seg, &nsegs, 0) != 0) 359 goto errout; 360 /* 361 * For the last buffer, set the wrap bit so the controller 362 * restarts from the first descriptor. 363 */ 364 if (i == ATE_MAX_RX_BUFFERS - 1) 365 seg.ds_addr |= 1 << 1; 366 sc->rx_descs[i].addr = seg.ds_addr; 367 sc->rx_descs[i].status = 0; 368 bus_dmamap_sync(sc->rxtag, sc->rx_map[i], BUS_DMASYNC_PREWRITE); 369 } 370 bus_dmamap_sync(sc->rx_desc_tag, sc->rx_desc_map, BUS_DMASYNC_PREWRITE); 371 /* Write the descriptor queue address. */ 372 WR4(sc, ETH_RBQP, sc->rx_desc_phys); 373 return (0); 374errout: 375 ate_deactivate(dev); 376 return (ENOMEM); 377} 378 379static void 380ate_deactivate(device_t dev) 381{ 382 struct ate_softc *sc; 383 384 sc = device_get_softc(dev); 385 /* XXX TODO(2) teardown busdma junk, below from fxp -- customize */ 386#if 0 387 if (sc->fxp_mtag) { 388 for (i = 0; i < FXP_NRFABUFS; i++) { 389 rxp = &sc->fxp_desc.rx_list[i]; 390 if (rxp->rx_mbuf != NULL) { 391 bus_dmamap_sync(sc->fxp_mtag, rxp->rx_map, 392 BUS_DMASYNC_POSTREAD); 393 bus_dmamap_unload(sc->fxp_mtag, rxp->rx_map); 394 m_freem(rxp->rx_mbuf); 395 } 396 bus_dmamap_destroy(sc->fxp_mtag, rxp->rx_map); 397 } 398 bus_dmamap_destroy(sc->fxp_mtag, sc->spare_map); 399 for (i = 0; i < FXP_NTXCB; i++) { 400 txp = &sc->fxp_desc.tx_list[i]; 401 if (txp->tx_mbuf != NULL) { 402 bus_dmamap_sync(sc->fxp_mtag, txp->tx_map, 403 BUS_DMASYNC_POSTWRITE); 404 bus_dmamap_unload(sc->fxp_mtag, txp->tx_map); 405 m_freem(txp->tx_mbuf); 406 } 407 bus_dmamap_destroy(sc->fxp_mtag, txp->tx_map); 408 } 409 bus_dma_tag_destroy(sc->fxp_mtag); 410 } 411 if (sc->fxp_stag) 412 bus_dma_tag_destroy(sc->fxp_stag); 413 if (sc->cbl_tag) 414 bus_dma_tag_destroy(sc->cbl_tag); 415 if (sc->mcs_tag) 416 bus_dma_tag_destroy(sc->mcs_tag); 417#endif 418 if (sc->intrhand) 419 bus_teardown_intr(dev, sc->irq_res, sc->intrhand); 420 sc->intrhand = 0; 421 bus_generic_detach(sc->dev); 422 if (sc->miibus) 423 device_delete_child(sc->dev, sc->miibus); 424 if (sc->mem_res) 425 bus_release_resource(dev, SYS_RES_IOPORT, 426 rman_get_rid(sc->mem_res), sc->mem_res); 427 sc->mem_res = 0; 428 if (sc->irq_res) 429 bus_release_resource(dev, SYS_RES_IRQ, 430 rman_get_rid(sc->irq_res), sc->irq_res); 431 sc->irq_res = 0; 432 return; 433} 434 435/* 436 * Change media according to request. 437 */ 438static int 439ate_ifmedia_upd(struct ifnet *ifp) 440{ 441 struct ate_softc *sc = ifp->if_softc; 442 struct mii_data *mii; 443 444 mii = device_get_softc(sc->miibus); 445 ATE_LOCK(sc); 446 mii_mediachg(mii); 447 ATE_UNLOCK(sc); 448 return (0); 449} 450 451/* 452 * Notify the world which media we're using. 453 */ 454static void 455ate_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 456{ 457 struct ate_softc *sc = ifp->if_softc; 458 struct mii_data *mii; 459 460 mii = device_get_softc(sc->miibus); 461 ATE_LOCK(sc); 462 mii_pollstat(mii); 463 ifmr->ifm_active = mii->mii_media_active; 464 ifmr->ifm_status = mii->mii_media_status; 465 ATE_UNLOCK(sc); 466} 467 468static void 469ate_tick(void *xsc) 470{ 471 struct ate_softc *sc = xsc; 472 struct mii_data *mii; 473 int active; 474 475 /* 476 * The KB920x boot loader tests ETH_SR & ETH_SR_LINK and will ask 477 * the MII if there's a link if this bit is clear. Not sure if we 478 * should do the same thing here or not. 479 */ 480 ATE_ASSERT_LOCKED(sc); 481 if (sc->miibus != NULL) { 482 mii = device_get_softc(sc->miibus); 483 active = mii->mii_media_active; 484 mii_tick(mii); 485 if (mii->mii_media_status & IFM_ACTIVE && 486 active != mii->mii_media_active) { 487 /* 488 * The speed and full/half-duplex state needs 489 * to be reflected in the ETH_CFG register, it 490 * seems. 491 */ 492 if (IFM_SUBTYPE(mii->mii_media_active) == IFM_10_T) 493 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) & 494 ~ETH_CFG_SPD); 495 else 496 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) | 497 ETH_CFG_SPD); 498 if (mii->mii_media_active & IFM_FDX) 499 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) | 500 ETH_CFG_FD); 501 else 502 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) & 503 ~ETH_CFG_FD); 504 } 505 } 506 507 /* 508 * Update the stats as best we can. When we're done, clear 509 * the status counters and start over. We're supposed to read these 510 * registers often enough that they won't overflow. Hopefully 511 * once a second is often enough. Some don't map well to 512 * the dot3Stats mib, so for those we just count them as general 513 * errors. Stats for iframes, ibutes, oframes and obytes are 514 * collected elsewhere. These registers zero on a read to prevent 515 * races. 516 */ 517 sc->mibdata.dot3StatsAlignmentErrors += RD4(sc, ETH_ALE); 518 sc->mibdata.dot3StatsFCSErrors += RD4(sc, ETH_SEQE); 519 sc->mibdata.dot3StatsSingleCollisionFrames += RD4(sc, ETH_SCOL); 520 sc->mibdata.dot3StatsMultipleCollisionFrames += RD4(sc, ETH_MCOL); 521 sc->mibdata.dot3StatsSQETestErrors += RD4(sc, ETH_SQEE); 522 sc->mibdata.dot3StatsDeferredTransmissions += RD4(sc, ETH_DTE); 523 sc->mibdata.dot3StatsLateCollisions += RD4(sc, ETH_LCOL); 524 sc->mibdata.dot3StatsExcessiveCollisions += RD4(sc, ETH_ECOL); 525 sc->mibdata.dot3StatsCarrierSenseErrors += RD4(sc, ETH_CSE); 526 sc->mibdata.dot3StatsFrameTooLongs += RD4(sc, ETH_ELR); 527 sc->mibdata.dot3StatsInternalMacReceiveErrors += RD4(sc, ETH_DRFC); 528 /* 529 * not sure where to lump these, so count them against the errors 530 * for the interface. 531 */ 532 sc->ifp->if_oerrors += RD4(sc, ETH_CSE) + RD4(sc, ETH_TUE); 533 sc->ifp->if_ierrors += RD4(sc, ETH_CDE) + RD4(sc, ETH_RJB) + 534 RD4(sc, ETH_USF); 535 536 /* 537 * Schedule another timeout one second from now. 538 */ 539 callout_reset(&sc->tick_ch, hz, ate_tick, sc); 540} 541 542static void 543ate_get_mac(struct ate_softc *sc, u_char *eaddr) 544{ 545 uint32_t low, high; 546 547 /* 548 * The KB920x loaders will setup the MAC with an address, if one 549 * is set in the loader. The TSC loader will also set the MAC address 550 * in a similar way. Grab the MAC address from the SA1[HL] registers. 551 */ 552 low = RD4(sc, ETH_SA1L); 553 high = RD4(sc, ETH_SA1H); 554 eaddr[0] = (high >> 8) & 0xff; 555 eaddr[1] = high & 0xff; 556 eaddr[2] = (low >> 24) & 0xff; 557 eaddr[3] = (low >> 16) & 0xff; 558 eaddr[4] = (low >> 8) & 0xff; 559 eaddr[5] = low & 0xff; 560} 561 562static void 563ate_intr(void *xsc) 564{ 565 struct ate_softc *sc = xsc; 566 int status; 567 int i; 568 569 status = RD4(sc, ETH_ISR); 570 if (status == 0) 571 return; 572 printf("IT IS %x\n", RD4(sc, ETH_RSR)); 573 if (status & ETH_ISR_RCOM) { 574 bus_dmamap_sync(sc->rx_desc_tag, sc->rx_desc_map, 575 BUS_DMASYNC_POSTREAD); 576 for (i = 0; i < ATE_MAX_RX_BUFFERS; i++) { 577 if (sc->rx_descs[i].addr & ETH_CPU_OWNER) { 578 struct mbuf *mb = sc->rx_mbuf[i]; 579 bus_dma_segment_t seg; 580 int rx_stat = sc->rx_descs[i].status; 581 int nsegs; 582 583 printf("GOT ONE\n"); 584 bus_dmamap_sync(sc->rxtag, 585 sc->rx_map[i], BUS_DMASYNC_POSTREAD); 586 bus_dmamap_unload(sc->rxtag, 587 sc->rx_map[i]); 588 WR4(sc, ETH_RSR, RD4(sc, ETH_RSR)); 589 /* 590 * Allocate a new buffer to replace this one. 591 * if we cannot, then we drop this packet 592 * and keep the old buffer we had. 593 */ 594 sc->rx_mbuf[i] = m_getcl(M_DONTWAIT, MT_DATA, 595 M_PKTHDR); 596 if (!sc->rx_mbuf[i]) { 597 sc->rx_mbuf[i] = mb; 598 sc->rx_descs[i].addr &= ~ETH_CPU_OWNER; 599 bus_dmamap_sync(sc->rx_desc_tag, 600 sc->rx_desc_map, 601 BUS_DMASYNC_PREWRITE); 602 continue; 603 } 604 if (bus_dmamap_load_mbuf_sg(sc->rxtag, 605 sc->rx_map[i], 606 sc->rx_mbuf[i], &seg, &nsegs, 0) != 0) { 607 sc->rx_mbuf[i] = mb; 608 sc->rx_descs[i].addr &= ~ETH_CPU_OWNER; 609 bus_dmamap_sync(sc->rx_desc_tag, 610 sc->rx_desc_map, 611 BUS_DMASYNC_PREWRITE); 612 continue; 613 } 614 /* 615 * For the last buffer, set the wrap bit so 616 * the controller restarts from the first 617 * descriptor. 618 */ 619 if (i == ATE_MAX_RX_BUFFERS - 1) 620 seg.ds_addr |= 1 << 1; 621 sc->rx_descs[i].addr = seg.ds_addr; 622 sc->rx_descs[i].status = 0; 623 mb->m_len = rx_stat & ETH_LEN_MASK; 624 (*sc->ifp->if_input)(sc->ifp, mb); 625 break; 626 } 627 } 628 } 629 if (status & ETH_ISR_TCOM) { 630 if (sc->sent_mbuf[0]) 631 m_freem(sc->sent_mbuf[0]); 632 if (sc->sent_mbuf[1]) { 633 if (RD4(sc, ETH_TSR) & ETH_TSR_IDLE) { 634 m_freem(sc->sent_mbuf[1]); 635 sc->txcur = 0; 636 sc->sent_mbuf[0] = sc->sent_mbuf[1] = NULL; 637 } else { 638 sc->sent_mbuf[0] = sc->sent_mbuf[1]; 639 sc->sent_mbuf[1] = NULL; 640 sc->txcur = 1; 641 } 642 } else { 643 sc->sent_mbuf[0] = NULL; 644 sc->txcur = 0; 645 } 646 } 647 if (status & ETH_ISR_RBNA) { 648 /* Workaround Errata #11 */ 649 WR4(sc, ETH_CTL, RD4(sc, ETH_CTL) &~ ETH_CTL_RE); 650 WR4(sc, ETH_CTL, RD4(sc, ETH_CTL) | ETH_CTL_RE); 651 } 652} 653 654/* 655 * Reset and initialize the chip 656 */ 657static void 658ateinit_locked(void *xsc) 659{ 660 struct ate_softc *sc = xsc; 661 struct ifnet *ifp = sc->ifp; 662 663 ATE_ASSERT_LOCKED(sc); 664 665 /* 666 * XXX TODO(3) 667 * we need to turn on the EMAC clock in the pmc. With the 668 * default boot loader, this is already turned on. However, we 669 * need to think about how best to turn it on/off as the interface 670 * is brought up/down, as well as dealing with the mii bus... 671 * 672 * We also need to multiplex the pins correctly. 673 */ 674 675 /* 676 * There are two different ways that the mii bus is connected 677 * to this chip. Select the right one based on a compile-time 678 * option. 679 */ 680#ifdef ATE_USE_RMII 681 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) | ETH_CFG_RMII); 682#else 683 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) & ~ETH_CFG_RMII); 684#endif 685 /* 686 * Turn on the multicast hash, and write 0's to it. 687 */ 688 WR4(sc, ETH_CFG, RD4(sc, ETH_CFG) | ETH_CFG_MTI); 689 WR4(sc, ETH_HSH, 0); 690 WR4(sc, ETH_HSL, 0); 691 692 WR4(sc, ETH_CTL, RD4(sc, ETH_CTL) | ETH_CTL_TE | ETH_CTL_RE); 693 WR4(sc, ETH_IER, /*ETH_ISR_RCOM | ETH_ISR_TCOM | ETH_ISR_RBNA*/ 694 0xffffffff); 695 696 /* 697 * Boot loader fills in MAC address. If that's not the case, then 698 * we should set SA1L and SA1H here to the appropriate value. Note: 699 * the byte order is big endian, not little endian, so we have some 700 * swapping to do. Again, if we need it (which I don't think we do). 701 */ 702 703 ate_setmcast(sc); 704 705 /* 706 * Set 'running' flag, and clear output active flag 707 * and attempt to start the output 708 */ 709 ifp->if_drv_flags |= IFF_DRV_RUNNING; 710 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 711 atestart_locked(ifp); 712 713 callout_reset(&sc->tick_ch, hz, ate_tick, sc); 714} 715 716/* 717 * dequeu packets and transmit 718 */ 719static void 720atestart_locked(struct ifnet *ifp) 721{ 722 struct ate_softc *sc = ifp->if_softc; 723 struct mbuf *m, *mdefrag; 724 bus_dma_segment_t segs[1]; 725 int nseg; 726 727 ATE_ASSERT_LOCKED(sc); 728 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) 729 return; 730 731outloop: 732 /* 733 * check to see if there's room to put another packet into the 734 * xmit queue. The EMAC chip has a ping-pong buffer for xmit 735 * packets. We use OACTIVE to indicate "we can stuff more into 736 * our buffers (clear) or not (set)." 737 */ 738 if (!(RD4(sc, ETH_TSR) & ETH_TSR_BNQ)) { 739 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 740 return; 741 } 742 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 743 if (m == 0) { 744 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 745 return; 746 } 747 mdefrag = m_defrag(m, M_DONTWAIT); 748 if (mdefrag == NULL) { 749 m_freem(m); 750 return; 751 } 752 m = mdefrag; 753 754 if (bus_dmamap_load_mbuf_sg(sc->mtag, sc->tx_map[sc->txcur], m, segs, 755 &nseg, 0) != 0) { 756 m_free(m); 757 goto outloop; 758 } 759 bus_dmamap_sync(sc->mtag, sc->tx_map[sc->txcur], BUS_DMASYNC_PREWRITE); 760 sc->sent_mbuf[sc->txcur] = m; 761 sc->txcur++; 762 if (sc->txcur >= ATE_MAX_TX_BUFFERS) 763 sc->txcur = 0; 764 765 /* 766 * tell the hardware to xmit the packet. 767 */ 768 WR4(sc, ETH_TAR, segs[0].ds_addr); 769 WR4(sc, ETH_TCR, segs[0].ds_len); 770 771 /* 772 * Tap off here if there is a bpf listener. 773 */ 774 BPF_MTAP(ifp, m); 775 776 /* 777 * Once we've queued one packet, we'll do the rest via the ISR, 778 * save off a pointer. 779 */ 780 sc->sent_mbuf[1] = m; 781} 782 783static void 784ateinit(void *xsc) 785{ 786 struct ate_softc *sc = xsc; 787 ATE_LOCK(sc); 788 ateinit_locked(sc); 789 ATE_UNLOCK(sc); 790} 791 792static void 793atestart(struct ifnet *ifp) 794{ 795 struct ate_softc *sc = ifp->if_softc; 796 ATE_LOCK(sc); 797 atestart_locked(ifp); 798 ATE_UNLOCK(sc); 799} 800 801/* 802 * Turn off interrupts, and stop the nic. Can be called with sc->ifp NULL 803 * so be careful. 804 */ 805static void 806atestop(struct ate_softc *sc) 807{ 808 struct ifnet *ifp = sc->ifp; 809 810 if (ifp) { 811 ifp->if_timer = 0; 812 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 813 } 814 815 callout_stop(&sc->tick_ch); 816 817 /* 818 * Enable some parts of the MAC that are needed always (like the 819 * MII bus. This turns off the RE and TE bits, which will remain 820 * off until atestart() is called to turn them on. With RE and TE 821 * turned off, there's no DMA to worry about after this write. 822 */ 823 WR4(sc, ETH_CTL, ETH_CTL_MPE); 824 825 /* 826 * Turn off all the configured options and revert to defaults. 827 */ 828 WR4(sc, ETH_CFG, ETH_CFG_CLK_32); 829 830 /* 831 * Turn off all the interrupts, and ack any pending ones by reading 832 * the ISR. 833 */ 834 WR4(sc, ETH_IDR, 0xffffffff); 835 RD4(sc, ETH_ISR); 836 837 /* 838 * Clear out the Transmit and Receiver Status registers of any 839 * errors they may be reporting 840 */ 841 WR4(sc, ETH_TSR, 0xffffffff); 842 WR4(sc, ETH_RSR, 0xffffffff); 843 844 /* 845 * XXX TODO(8) 846 * need to worry about the busdma resources? Yes, I think we need 847 * to sync and unload them. We may also need to release the mbufs 848 * that are assocaited with RX and TX operations. 849 */ 850 851 /* 852 * XXX we should power down the EMAC if it isn't in use, after 853 * putting it into loopback mode. This saves about 400uA according 854 * to the datasheet. 855 */ 856} 857 858static void 859atewatchdog(struct ifnet *ifp) 860{ 861 struct ate_softc *sc = ifp->if_softc; 862 863 ATE_LOCK(sc); 864 device_printf(sc->dev, "Device timeout\n"); 865 ifp->if_oerrors++; 866 ateinit_locked(sc); 867 ATE_UNLOCK(sc); 868} 869 870static int 871ateioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 872{ 873 struct ate_softc *sc = ifp->if_softc; 874 int error = 0; 875 876 switch (cmd) { 877 case SIOCSIFFLAGS: 878 ATE_LOCK(sc); 879 if ((ifp->if_flags & IFF_UP) == 0 && 880 ifp->if_drv_flags & IFF_DRV_RUNNING) { 881 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 882 atestop(sc); 883 } else { 884 /* reinitialize card on any parameter change */ 885 ateinit_locked(sc); 886 } 887 ATE_UNLOCK(sc); 888 break; 889 890 case SIOCADDMULTI: 891 case SIOCDELMULTI: 892 /* update multicast filter list. */ 893 ate_setmcast(sc); 894 error = 0; 895 break; 896 897 default: 898 error = ether_ioctl(ifp, cmd, data); 899 break; 900 } 901 return (error); 902} 903 904static void 905ate_child_detached(device_t dev, device_t child) 906{ 907 struct ate_softc *sc; 908 909 sc = device_get_softc(dev); 910 if (child == sc->miibus) 911 sc->miibus = NULL; 912} 913 914/* 915 * MII bus support routines. 916 */ 917static int 918ate_miibus_readreg(device_t dev, int phy, int reg) 919{ 920 struct ate_softc *sc; 921 int val; 922 923 /* 924 * XXX if we implement agressive power savings, then we need 925 * XXX to make sure that the clock to the emac is on here 926 */ 927 928 if (phy != 0) 929 return (0xffff); 930 sc = device_get_softc(dev); 931 DELAY(1); /* Hangs w/o this delay really 30.5us atm */ 932 WR4(sc, ETH_MAN, ETH_MAN_REG_RD(phy, reg)); 933 while ((RD4(sc, ETH_SR) & ETH_SR_IDLE) == 0) 934 continue; 935 val = RD4(sc, ETH_MAN) & ETH_MAN_VALUE_MASK; 936 937 return (val); 938} 939 940static void 941ate_miibus_writereg(device_t dev, int phy, int reg, int data) 942{ 943 struct ate_softc *sc; 944 945 /* 946 * XXX if we implement agressive power savings, then we need 947 * XXX to make sure that the clock to the emac is on here 948 */ 949 950 sc = device_get_softc(dev); 951 WR4(sc, ETH_MAN, ETH_MAN_REG_WR(phy, reg, data)); 952 while ((RD4(sc, ETH_SR) & ETH_SR_IDLE) == 0) 953 continue; 954 return; 955} 956 957static device_method_t ate_methods[] = { 958 /* Device interface */ 959 DEVMETHOD(device_probe, ate_probe), 960 DEVMETHOD(device_attach, ate_attach), 961 DEVMETHOD(device_detach, ate_detach), 962 963 /* Bus interface */ 964 DEVMETHOD(bus_child_detached, ate_child_detached), 965 966 /* MII interface */ 967 DEVMETHOD(miibus_readreg, ate_miibus_readreg), 968 DEVMETHOD(miibus_writereg, ate_miibus_writereg), 969 970 { 0, 0 } 971}; 972 973static driver_t ate_driver = { 974 "ate", 975 ate_methods, 976 sizeof(struct ate_softc), 977}; 978 979DRIVER_MODULE(ate, atmelarm, ate_driver, ate_devclass, 0, 0); 980DRIVER_MODULE(miibus, ate, miibus_driver, miibus_devclass, 0, 0); 981MODULE_DEPEND(ate, miibus, 1, 1, 1); 982MODULE_DEPEND(ate, ether, 1, 1, 1); 983