2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Benno Rice. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Driver for SMSC LAN91C111, may work for older variants.
34 #ifdef HAVE_KERNEL_OPTION_HEADERS
35 #include "opt_device_polling.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/kernel.h>
42 #include <sys/sockio.h>
43 #include <sys/malloc.h>
45 #include <sys/queue.h>
46 #include <sys/socket.h>
47 #include <sys/syslog.h>
48 #include <sys/taskqueue.h>
50 #include <sys/module.h>
53 #include <machine/bus.h>
54 #include <machine/resource.h>
57 #include <net/ethernet.h>
59 #include <net/if_var.h>
60 #include <net/if_arp.h>
61 #include <net/if_dl.h>
62 #include <net/if_types.h>
63 #include <net/if_mib.h>
64 #include <net/if_media.h>
67 #include <netinet/in.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip.h>
74 #include <net/bpfdesc.h>
76 #include <dev/smc/if_smcreg.h>
77 #include <dev/smc/if_smcvar.h>
79 #include <dev/mii/mii.h>
80 #include <dev/mii/mii_bitbang.h>
81 #include <dev/mii/miivar.h>
83 #include "miibus_if.h"
85 #define SMC_LOCK(sc) mtx_lock(&(sc)->smc_mtx)
86 #define SMC_UNLOCK(sc) mtx_unlock(&(sc)->smc_mtx)
87 #define SMC_ASSERT_LOCKED(sc) mtx_assert(&(sc)->smc_mtx, MA_OWNED)
89 #define SMC_INTR_PRIORITY 0
90 #define SMC_RX_PRIORITY 5
91 #define SMC_TX_PRIORITY 10
93 devclass_t smc_devclass;
95 static const char *smc_chip_ids[16] = {
97 /* 3 */ "SMSC LAN91C90 or LAN91C92",
98 /* 4 */ "SMSC LAN91C94",
99 /* 5 */ "SMSC LAN91C95",
100 /* 6 */ "SMSC LAN91C96",
101 /* 7 */ "SMSC LAN91C100",
102 /* 8 */ "SMSC LAN91C100FD",
103 /* 9 */ "SMSC LAN91C110FD or LAN91C111FD",
108 static void smc_init(void *);
109 static void smc_start(struct ifnet *);
110 static void smc_stop(struct smc_softc *);
111 static int smc_ioctl(struct ifnet *, u_long, caddr_t);
113 static void smc_init_locked(struct smc_softc *);
114 static void smc_start_locked(struct ifnet *);
115 static void smc_reset(struct smc_softc *);
116 static int smc_mii_ifmedia_upd(struct ifnet *);
117 static void smc_mii_ifmedia_sts(struct ifnet *, struct ifmediareq *);
118 static void smc_mii_tick(void *);
119 static void smc_mii_mediachg(struct smc_softc *);
120 static int smc_mii_mediaioctl(struct smc_softc *, struct ifreq *, u_long);
122 static void smc_task_intr(void *, int);
123 static void smc_task_rx(void *, int);
124 static void smc_task_tx(void *, int);
126 static driver_filter_t smc_intr;
127 static callout_func_t smc_watchdog;
128 #ifdef DEVICE_POLLING
129 static poll_handler_t smc_poll;
135 static uint32_t smc_mii_bitbang_read(device_t);
136 static void smc_mii_bitbang_write(device_t, uint32_t);
138 static const struct mii_bitbang_ops smc_mii_bitbang_ops = {
139 smc_mii_bitbang_read,
140 smc_mii_bitbang_write,
142 MGMT_MDO, /* MII_BIT_MDO */
143 MGMT_MDI, /* MII_BIT_MDI */
144 MGMT_MCLK, /* MII_BIT_MDC */
145 MGMT_MDOE, /* MII_BIT_DIR_HOST_PHY */
146 0, /* MII_BIT_DIR_PHY_HOST */
151 smc_select_bank(struct smc_softc *sc, uint16_t bank)
154 bus_barrier(sc->smc_reg, BSR, 2,
155 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
156 bus_write_2(sc->smc_reg, BSR, bank & BSR_BANK_MASK);
157 bus_barrier(sc->smc_reg, BSR, 2,
158 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
161 /* Never call this when not in bank 2. */
163 smc_mmu_wait(struct smc_softc *sc)
166 KASSERT((bus_read_2(sc->smc_reg, BSR) &
167 BSR_BANK_MASK) == 2, ("%s: smc_mmu_wait called when not in bank 2",
168 device_get_nameunit(sc->smc_dev)));
169 while (bus_read_2(sc->smc_reg, MMUCR) & MMUCR_BUSY)
173 static __inline uint8_t
174 smc_read_1(struct smc_softc *sc, bus_size_t offset)
177 return (bus_read_1(sc->smc_reg, offset));
181 smc_write_1(struct smc_softc *sc, bus_size_t offset, uint8_t val)
184 bus_write_1(sc->smc_reg, offset, val);
187 static __inline uint16_t
188 smc_read_2(struct smc_softc *sc, bus_size_t offset)
191 return (bus_read_2(sc->smc_reg, offset));
195 smc_write_2(struct smc_softc *sc, bus_size_t offset, uint16_t val)
198 bus_write_2(sc->smc_reg, offset, val);
202 smc_read_multi_2(struct smc_softc *sc, bus_size_t offset, uint16_t *datap,
206 bus_read_multi_2(sc->smc_reg, offset, datap, count);
210 smc_write_multi_2(struct smc_softc *sc, bus_size_t offset, uint16_t *datap,
214 bus_write_multi_2(sc->smc_reg, offset, datap, count);
218 smc_barrier(struct smc_softc *sc, bus_size_t offset, bus_size_t length,
222 bus_barrier(sc->smc_reg, offset, length, flags);
226 smc_probe(device_t dev)
228 int rid, type, error;
230 struct smc_softc *sc;
231 struct resource *reg;
233 sc = device_get_softc(dev);
235 type = SYS_RES_IOPORT;
239 type = SYS_RES_MEMORY;
241 reg = bus_alloc_resource_anywhere(dev, type, &rid, 16, RF_ACTIVE);
245 "could not allocate I/O resource for probe\n");
249 /* Check for the identification value in the BSR. */
250 val = bus_read_2(reg, BSR);
251 if ((val & BSR_IDENTIFY_MASK) != BSR_IDENTIFY) {
253 device_printf(dev, "identification value not in BSR\n");
259 * Try switching banks and make sure we still get the identification
262 bus_write_2(reg, BSR, 0);
263 val = bus_read_2(reg, BSR);
264 if ((val & BSR_IDENTIFY_MASK) != BSR_IDENTIFY) {
267 "identification value not in BSR after write\n");
274 bus_write_2(reg, BSR, 1);
275 val = bus_read_2(reg, BAR);
276 val = BAR_ADDRESS(val);
277 if (rman_get_start(reg) != val) {
279 device_printf(dev, "BAR address %x does not match "
280 "I/O resource address %lx\n", val,
281 rman_get_start(reg));
287 /* Compare REV against known chip revisions. */
288 bus_write_2(reg, BSR, 3);
289 val = bus_read_2(reg, REV);
290 val = (val & REV_CHIP_MASK) >> REV_CHIP_SHIFT;
291 if (smc_chip_ids[val] == NULL) {
293 device_printf(dev, "Unknown chip revision: %d\n", val);
298 device_set_desc(dev, smc_chip_ids[val]);
301 bus_release_resource(dev, type, rid, reg);
306 smc_attach(device_t dev)
310 u_char eaddr[ETHER_ADDR_LEN];
311 struct smc_softc *sc;
314 sc = device_get_softc(dev);
319 ifp = sc->smc_ifp = if_alloc(IFT_ETHER);
325 mtx_init(&sc->smc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
327 /* Set up watchdog callout. */
328 callout_init_mtx(&sc->smc_watchdog, &sc->smc_mtx, 0);
330 type = SYS_RES_IOPORT;
332 type = SYS_RES_MEMORY;
335 sc->smc_reg = bus_alloc_resource_anywhere(dev, type, &sc->smc_reg_rid,
337 if (sc->smc_reg == NULL) {
342 sc->smc_irq = bus_alloc_resource_anywhere(dev, SYS_RES_IRQ,
343 &sc->smc_irq_rid, 1, RF_ACTIVE | RF_SHAREABLE);
344 if (sc->smc_irq == NULL) {
353 smc_select_bank(sc, 3);
354 val = smc_read_2(sc, REV);
355 sc->smc_chip = (val & REV_CHIP_MASK) >> REV_CHIP_SHIFT;
356 sc->smc_rev = (val * REV_REV_MASK) >> REV_REV_SHIFT;
358 device_printf(dev, "revision %x\n", sc->smc_rev);
360 callout_init_mtx(&sc->smc_mii_tick_ch, &sc->smc_mtx,
361 CALLOUT_RETURNUNLOCKED);
362 if (sc->smc_chip >= REV_CHIP_91110FD) {
363 (void)mii_attach(dev, &sc->smc_miibus, ifp,
364 smc_mii_ifmedia_upd, smc_mii_ifmedia_sts, BMSR_DEFCAPMASK,
365 MII_PHY_ANY, MII_OFFSET_ANY, 0);
366 if (sc->smc_miibus != NULL) {
367 sc->smc_mii_tick = smc_mii_tick;
368 sc->smc_mii_mediachg = smc_mii_mediachg;
369 sc->smc_mii_mediaioctl = smc_mii_mediaioctl;
373 smc_select_bank(sc, 1);
374 eaddr[0] = smc_read_1(sc, IAR0);
375 eaddr[1] = smc_read_1(sc, IAR1);
376 eaddr[2] = smc_read_1(sc, IAR2);
377 eaddr[3] = smc_read_1(sc, IAR3);
378 eaddr[4] = smc_read_1(sc, IAR4);
379 eaddr[5] = smc_read_1(sc, IAR5);
381 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
383 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
384 ifp->if_init = smc_init;
385 ifp->if_ioctl = smc_ioctl;
386 ifp->if_start = smc_start;
387 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
388 IFQ_SET_READY(&ifp->if_snd);
390 ifp->if_capabilities = ifp->if_capenable = 0;
392 #ifdef DEVICE_POLLING
393 ifp->if_capabilities |= IFCAP_POLLING;
396 ether_ifattach(ifp, eaddr);
398 /* Set up taskqueue */
399 TASK_INIT(&sc->smc_intr, SMC_INTR_PRIORITY, smc_task_intr, ifp);
400 NET_TASK_INIT(&sc->smc_rx, SMC_RX_PRIORITY, smc_task_rx, ifp);
401 TASK_INIT(&sc->smc_tx, SMC_TX_PRIORITY, smc_task_tx, ifp);
402 sc->smc_tq = taskqueue_create_fast("smc_taskq", M_NOWAIT,
403 taskqueue_thread_enqueue, &sc->smc_tq);
404 taskqueue_start_threads(&sc->smc_tq, 1, PI_NET, "%s taskq",
405 device_get_nameunit(sc->smc_dev));
407 /* Mask all interrupts. */
409 smc_write_1(sc, MSK, 0);
411 /* Wire up interrupt */
412 error = bus_setup_intr(dev, sc->smc_irq,
413 INTR_TYPE_NET|INTR_MPSAFE, smc_intr, NULL, sc, &sc->smc_ih);
424 smc_detach(device_t dev)
427 struct smc_softc *sc;
429 sc = device_get_softc(dev);
434 if (sc->smc_ifp != NULL) {
435 ether_ifdetach(sc->smc_ifp);
438 callout_drain(&sc->smc_watchdog);
439 callout_drain(&sc->smc_mii_tick_ch);
441 #ifdef DEVICE_POLLING
442 if (sc->smc_ifp->if_capenable & IFCAP_POLLING)
443 ether_poll_deregister(sc->smc_ifp);
446 if (sc->smc_ih != NULL)
447 bus_teardown_intr(sc->smc_dev, sc->smc_irq, sc->smc_ih);
449 if (sc->smc_tq != NULL) {
450 taskqueue_drain(sc->smc_tq, &sc->smc_intr);
451 taskqueue_drain(sc->smc_tq, &sc->smc_rx);
452 taskqueue_drain(sc->smc_tq, &sc->smc_tx);
453 taskqueue_free(sc->smc_tq);
457 if (sc->smc_ifp != NULL) {
458 if_free(sc->smc_ifp);
461 if (sc->smc_miibus != NULL) {
462 device_delete_child(sc->smc_dev, sc->smc_miibus);
463 bus_generic_detach(sc->smc_dev);
466 if (sc->smc_reg != NULL) {
467 type = SYS_RES_IOPORT;
469 type = SYS_RES_MEMORY;
471 bus_release_resource(sc->smc_dev, type, sc->smc_reg_rid,
475 if (sc->smc_irq != NULL)
476 bus_release_resource(sc->smc_dev, SYS_RES_IRQ, sc->smc_irq_rid,
479 if (mtx_initialized(&sc->smc_mtx))
480 mtx_destroy(&sc->smc_mtx);
485 static device_method_t smc_methods[] = {
486 /* Device interface */
487 DEVMETHOD(device_attach, smc_attach),
488 DEVMETHOD(device_detach, smc_detach),
491 DEVMETHOD(miibus_readreg, smc_miibus_readreg),
492 DEVMETHOD(miibus_writereg, smc_miibus_writereg),
493 DEVMETHOD(miibus_statchg, smc_miibus_statchg),
497 driver_t smc_driver = {
500 sizeof(struct smc_softc),
503 DRIVER_MODULE(miibus, smc, miibus_driver, miibus_devclass, 0, 0);
506 smc_start(struct ifnet *ifp)
508 struct smc_softc *sc;
512 smc_start_locked(ifp);
517 smc_start_locked(struct ifnet *ifp)
519 struct smc_softc *sc;
521 u_int len, npages, spin_count;
524 SMC_ASSERT_LOCKED(sc);
526 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
528 if (IFQ_IS_EMPTY(&ifp->if_snd))
532 * Grab the next packet. If it's too big, drop it.
534 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
535 len = m_length(m, NULL);
537 if (len > ETHER_MAX_LEN - ETHER_CRC_LEN) {
538 if_printf(ifp, "large packet discarded\n");
539 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
541 return; /* XXX readcheck? */
545 * Flag that we're busy.
547 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
551 * Work out how many 256 byte "pages" we need. We have to include the
552 * control data for the packet in this calculation.
554 npages = (len + PKT_CTRL_DATA_LEN) >> 8;
561 smc_select_bank(sc, 2);
563 smc_write_2(sc, MMUCR, MMUCR_CMD_TX_ALLOC | npages);
566 * Spin briefly to see if the allocation succeeds.
568 spin_count = TX_ALLOC_WAIT_TIME;
570 if (smc_read_1(sc, IST) & ALLOC_INT) {
571 smc_write_1(sc, ACK, ALLOC_INT);
574 } while (--spin_count);
577 * If the allocation is taking too long, unmask the alloc interrupt
580 if (spin_count == 0) {
581 sc->smc_mask |= ALLOC_INT;
582 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
583 smc_write_1(sc, MSK, sc->smc_mask);
587 taskqueue_enqueue(sc->smc_tq, &sc->smc_tx);
591 smc_task_tx(void *context, int pending)
594 struct smc_softc *sc;
601 ifp = (struct ifnet *)context;
606 if (sc->smc_pending == NULL) {
611 m = m0 = sc->smc_pending;
612 sc->smc_pending = NULL;
613 smc_select_bank(sc, 2);
616 * Check the allocation result.
618 packet = smc_read_1(sc, ARR);
621 * If the allocation failed, requeue the packet and retry.
623 if (packet & ARR_FAILED) {
624 IFQ_DRV_PREPEND(&ifp->if_snd, m);
625 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
626 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
627 smc_start_locked(ifp);
633 * Tell the device to write to our packet number.
635 smc_write_1(sc, PNR, packet);
636 smc_write_2(sc, PTR, 0 | PTR_AUTO_INCR);
639 * Tell the device how long the packet is (including control data).
641 len = m_length(m, 0);
642 len += PKT_CTRL_DATA_LEN;
643 smc_write_2(sc, DATA0, 0);
644 smc_write_2(sc, DATA0, len);
647 * Push the data out to the device.
651 for (; m != NULL; m = m->m_next) {
652 data = mtod(m, uint8_t *);
653 smc_write_multi_2(sc, DATA0, (uint16_t *)data, m->m_len / 2);
658 * Push out the control byte and and the odd byte if needed.
660 if ((len & 1) != 0 && data != NULL)
661 smc_write_2(sc, DATA0, (CTRL_ODD << 8) | data[last_len - 1]);
663 smc_write_2(sc, DATA0, 0);
666 * Unmask the TX empty interrupt.
668 sc->smc_mask |= TX_EMPTY_INT;
669 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
670 smc_write_1(sc, MSK, sc->smc_mask);
673 * Enqueue the packet.
676 smc_write_2(sc, MMUCR, MMUCR_CMD_ENQUEUE);
677 callout_reset(&sc->smc_watchdog, hz * 2, smc_watchdog, sc);
682 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
683 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
690 * See if there's anything else to do.
696 smc_task_rx(void *context, int pending)
698 u_int packet, status, len;
701 struct smc_softc *sc;
702 struct mbuf *m, *mhead, *mtail;
705 ifp = (struct ifnet *)context;
707 mhead = mtail = NULL;
711 packet = smc_read_1(sc, FIFO_RX);
712 while ((packet & FIFO_EMPTY) == 0) {
714 * Grab an mbuf and attach a cluster.
716 MGETHDR(m, M_NOWAIT, MT_DATA);
720 if (!(MCLGET(m, M_NOWAIT))) {
726 * Point to the start of the packet.
728 smc_select_bank(sc, 2);
729 smc_write_1(sc, PNR, packet);
730 smc_write_2(sc, PTR, 0 | PTR_READ | PTR_RCV | PTR_AUTO_INCR);
733 * Grab status and packet length.
735 status = smc_read_2(sc, DATA0);
736 len = smc_read_2(sc, DATA0) & RX_LEN_MASK;
738 if (status & RX_ODDFRM)
744 if (status & (RX_TOOSHORT | RX_TOOLNG | RX_BADCRC | RX_ALGNERR)) {
746 smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE);
747 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
753 * Set the mbuf up the way we want it.
755 m->m_pkthdr.rcvif = ifp;
756 m->m_pkthdr.len = m->m_len = len + 2; /* XXX: Is this right? */
757 m_adj(m, ETHER_ALIGN);
760 * Pull the packet out of the device. Make sure we're in the
761 * right bank first as things may have changed while we were
762 * allocating our mbuf.
764 smc_select_bank(sc, 2);
765 smc_write_1(sc, PNR, packet);
766 smc_write_2(sc, PTR, 4 | PTR_READ | PTR_RCV | PTR_AUTO_INCR);
767 data = mtod(m, uint8_t *);
768 smc_read_multi_2(sc, DATA0, (uint16_t *)data, len >> 1);
771 *data = smc_read_1(sc, DATA0);
775 * Tell the device we're done.
778 smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE);
790 packet = smc_read_1(sc, FIFO_RX);
793 sc->smc_mask |= RCV_INT;
794 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
795 smc_write_1(sc, MSK, sc->smc_mask);
799 while (mhead != NULL) {
801 mhead = mhead->m_next;
803 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
804 (*ifp->if_input)(ifp, m);
808 #ifdef DEVICE_POLLING
810 smc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
812 struct smc_softc *sc;
817 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
823 if (cmd == POLL_AND_CHECK_STATUS)
824 taskqueue_enqueue(sc->smc_tq, &sc->smc_intr);
830 smc_intr(void *context)
832 struct smc_softc *sc;
835 sc = (struct smc_softc *)context;
838 * Save current bank and restore later in this function
840 curbank = (smc_read_2(sc, BSR) & BSR_BANK_MASK);
843 * Block interrupts in order to let smc_task_intr to kick in
845 smc_select_bank(sc, 2);
846 smc_write_1(sc, MSK, 0);
849 smc_select_bank(sc, curbank);
851 taskqueue_enqueue(sc->smc_tq, &sc->smc_intr);
852 return (FILTER_HANDLED);
856 smc_task_intr(void *context, int pending)
858 struct smc_softc *sc;
860 u_int status, packet, counter, tcr;
863 ifp = (struct ifnet *)context;
868 smc_select_bank(sc, 2);
871 * Find out what interrupts are flagged.
873 status = smc_read_1(sc, IST) & sc->smc_mask;
878 if (status & TX_INT) {
880 * Kill off the packet if there is one and re-enable transmit.
882 packet = smc_read_1(sc, FIFO_TX);
883 if ((packet & FIFO_EMPTY) == 0) {
884 callout_stop(&sc->smc_watchdog);
885 smc_select_bank(sc, 2);
886 smc_write_1(sc, PNR, packet);
887 smc_write_2(sc, PTR, 0 | PTR_READ |
889 smc_select_bank(sc, 0);
890 tcr = smc_read_2(sc, EPHSR);
892 if ((tcr & EPHSR_TX_SUC) == 0)
893 device_printf(sc->smc_dev,
896 smc_select_bank(sc, 2);
898 smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE_PKT);
900 smc_select_bank(sc, 0);
901 tcr = smc_read_2(sc, TCR);
902 tcr |= TCR_TXENA | TCR_PAD_EN;
903 smc_write_2(sc, TCR, tcr);
904 smc_select_bank(sc, 2);
905 taskqueue_enqueue(sc->smc_tq, &sc->smc_tx);
911 smc_write_1(sc, ACK, TX_INT);
917 if (status & RCV_INT) {
918 smc_write_1(sc, ACK, RCV_INT);
919 sc->smc_mask &= ~RCV_INT;
920 taskqueue_enqueue(sc->smc_tq, &sc->smc_rx);
926 if (status & ALLOC_INT) {
927 smc_write_1(sc, ACK, ALLOC_INT);
928 sc->smc_mask &= ~ALLOC_INT;
929 taskqueue_enqueue(sc->smc_tq, &sc->smc_tx);
935 if (status & RX_OVRN_INT) {
936 smc_write_1(sc, ACK, RX_OVRN_INT);
937 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
943 if (status & TX_EMPTY_INT) {
944 smc_write_1(sc, ACK, TX_EMPTY_INT);
945 sc->smc_mask &= ~TX_EMPTY_INT;
946 callout_stop(&sc->smc_watchdog);
949 * Update collision stats.
951 smc_select_bank(sc, 0);
952 counter = smc_read_2(sc, ECR);
953 smc_select_bank(sc, 2);
954 if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
955 ((counter & ECR_SNGLCOL_MASK) >> ECR_SNGLCOL_SHIFT) +
956 ((counter & ECR_MULCOL_MASK) >> ECR_MULCOL_SHIFT));
959 * See if there are any packets to transmit.
961 taskqueue_enqueue(sc->smc_tq, &sc->smc_tx);
965 * Update the interrupt mask.
967 smc_select_bank(sc, 2);
968 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
969 smc_write_1(sc, MSK, sc->smc_mask);
975 smc_mii_bitbang_read(device_t dev)
977 struct smc_softc *sc;
980 sc = device_get_softc(dev);
982 SMC_ASSERT_LOCKED(sc);
983 KASSERT((smc_read_2(sc, BSR) & BSR_BANK_MASK) == 3,
984 ("%s: smc_mii_bitbang_read called with bank %d (!= 3)",
985 device_get_nameunit(sc->smc_dev),
986 smc_read_2(sc, BSR) & BSR_BANK_MASK));
988 val = smc_read_2(sc, MGMT);
989 smc_barrier(sc, MGMT, 2,
990 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
996 smc_mii_bitbang_write(device_t dev, uint32_t val)
998 struct smc_softc *sc;
1000 sc = device_get_softc(dev);
1002 SMC_ASSERT_LOCKED(sc);
1003 KASSERT((smc_read_2(sc, BSR) & BSR_BANK_MASK) == 3,
1004 ("%s: smc_mii_bitbang_write called with bank %d (!= 3)",
1005 device_get_nameunit(sc->smc_dev),
1006 smc_read_2(sc, BSR) & BSR_BANK_MASK));
1008 smc_write_2(sc, MGMT, val);
1009 smc_barrier(sc, MGMT, 2,
1010 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
1014 smc_miibus_readreg(device_t dev, int phy, int reg)
1016 struct smc_softc *sc;
1019 sc = device_get_softc(dev);
1023 smc_select_bank(sc, 3);
1025 val = mii_bitbang_readreg(dev, &smc_mii_bitbang_ops, phy, reg);
1032 smc_miibus_writereg(device_t dev, int phy, int reg, int data)
1034 struct smc_softc *sc;
1036 sc = device_get_softc(dev);
1040 smc_select_bank(sc, 3);
1042 mii_bitbang_writereg(dev, &smc_mii_bitbang_ops, phy, reg, data);
1049 smc_miibus_statchg(device_t dev)
1051 struct smc_softc *sc;
1052 struct mii_data *mii;
1055 sc = device_get_softc(dev);
1056 mii = device_get_softc(sc->smc_miibus);
1060 smc_select_bank(sc, 0);
1061 tcr = smc_read_2(sc, TCR);
1063 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
1068 smc_write_2(sc, TCR, tcr);
1074 smc_mii_ifmedia_upd(struct ifnet *ifp)
1076 struct smc_softc *sc;
1077 struct mii_data *mii;
1080 if (sc->smc_miibus == NULL)
1083 mii = device_get_softc(sc->smc_miibus);
1084 return (mii_mediachg(mii));
1088 smc_mii_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1090 struct smc_softc *sc;
1091 struct mii_data *mii;
1094 if (sc->smc_miibus == NULL)
1097 mii = device_get_softc(sc->smc_miibus);
1099 ifmr->ifm_active = mii->mii_media_active;
1100 ifmr->ifm_status = mii->mii_media_status;
1104 smc_mii_tick(void *context)
1106 struct smc_softc *sc;
1108 sc = (struct smc_softc *)context;
1110 if (sc->smc_miibus == NULL)
1115 mii_tick(device_get_softc(sc->smc_miibus));
1116 callout_reset(&sc->smc_mii_tick_ch, hz, smc_mii_tick, sc);
1120 smc_mii_mediachg(struct smc_softc *sc)
1123 if (sc->smc_miibus == NULL)
1125 mii_mediachg(device_get_softc(sc->smc_miibus));
1129 smc_mii_mediaioctl(struct smc_softc *sc, struct ifreq *ifr, u_long command)
1131 struct mii_data *mii;
1133 if (sc->smc_miibus == NULL)
1136 mii = device_get_softc(sc->smc_miibus);
1137 return (ifmedia_ioctl(sc->smc_ifp, ifr, &mii->mii_media, command));
1141 smc_reset(struct smc_softc *sc)
1145 SMC_ASSERT_LOCKED(sc);
1147 smc_select_bank(sc, 2);
1150 * Mask all interrupts.
1152 smc_write_1(sc, MSK, 0);
1155 * Tell the device to reset.
1157 smc_select_bank(sc, 0);
1158 smc_write_2(sc, RCR, RCR_SOFT_RST);
1161 * Set up the configuration register.
1163 smc_select_bank(sc, 1);
1164 smc_write_2(sc, CR, CR_EPH_POWER_EN);
1168 * Turn off transmit and receive.
1170 smc_select_bank(sc, 0);
1171 smc_write_2(sc, TCR, 0);
1172 smc_write_2(sc, RCR, 0);
1175 * Set up the control register.
1177 smc_select_bank(sc, 1);
1178 ctr = smc_read_2(sc, CTR);
1179 ctr |= CTR_LE_ENABLE | CTR_AUTO_RELEASE;
1180 smc_write_2(sc, CTR, ctr);
1185 smc_select_bank(sc, 2);
1187 smc_write_2(sc, MMUCR, MMUCR_CMD_MMU_RESET);
1191 smc_enable(struct smc_softc *sc)
1195 SMC_ASSERT_LOCKED(sc);
1199 * Set up the receive/PHY control register.
1201 smc_select_bank(sc, 0);
1202 smc_write_2(sc, RPCR, RPCR_ANEG | (RPCR_LED_LINK_ANY << RPCR_LSA_SHIFT)
1203 | (RPCR_LED_ACT_ANY << RPCR_LSB_SHIFT));
1206 * Set up the transmit and receive control registers.
1208 smc_write_2(sc, TCR, TCR_TXENA | TCR_PAD_EN);
1209 smc_write_2(sc, RCR, RCR_RXEN | RCR_STRIP_CRC);
1212 * Set up the interrupt mask.
1214 smc_select_bank(sc, 2);
1215 sc->smc_mask = EPH_INT | RX_OVRN_INT | RCV_INT | TX_INT;
1216 if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1217 smc_write_1(sc, MSK, sc->smc_mask);
1221 smc_stop(struct smc_softc *sc)
1224 SMC_ASSERT_LOCKED(sc);
1227 * Turn off callouts.
1229 callout_stop(&sc->smc_watchdog);
1230 callout_stop(&sc->smc_mii_tick_ch);
1233 * Mask all interrupts.
1235 smc_select_bank(sc, 2);
1237 smc_write_1(sc, MSK, 0);
1238 #ifdef DEVICE_POLLING
1239 ether_poll_deregister(sc->smc_ifp);
1240 sc->smc_ifp->if_capenable &= ~IFCAP_POLLING;
1244 * Disable transmit and receive.
1246 smc_select_bank(sc, 0);
1247 smc_write_2(sc, TCR, 0);
1248 smc_write_2(sc, RCR, 0);
1250 sc->smc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1254 smc_watchdog(void *arg)
1256 struct smc_softc *sc;
1258 sc = (struct smc_softc *)arg;
1259 device_printf(sc->smc_dev, "watchdog timeout\n");
1260 taskqueue_enqueue(sc->smc_tq, &sc->smc_intr);
1264 smc_init(void *context)
1266 struct smc_softc *sc;
1268 sc = (struct smc_softc *)context;
1270 smc_init_locked(sc);
1275 smc_init_locked(struct smc_softc *sc)
1279 SMC_ASSERT_LOCKED(sc);
1281 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1287 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1288 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1290 smc_start_locked(ifp);
1292 if (sc->smc_mii_tick != NULL)
1293 callout_reset(&sc->smc_mii_tick_ch, hz, sc->smc_mii_tick, sc);
1295 #ifdef DEVICE_POLLING
1297 ether_poll_register(smc_poll, ifp);
1299 ifp->if_capenable |= IFCAP_POLLING;
1304 smc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1306 struct smc_softc *sc;
1314 if ((ifp->if_flags & IFF_UP) == 0 &&
1315 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1321 if (sc->smc_mii_mediachg != NULL)
1322 sc->smc_mii_mediachg(sc);
1338 if (sc->smc_mii_mediaioctl == NULL) {
1342 sc->smc_mii_mediaioctl(sc, (struct ifreq *)data, cmd);
1346 error = ether_ioctl(ifp, cmd, data);