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),
498 driver_t smc_driver = {
501 sizeof(struct smc_softc),
504 DRIVER_MODULE(miibus, smc, miibus_driver, miibus_devclass, 0, 0);
507 smc_start(struct ifnet *ifp)
509 struct smc_softc *sc;
513 smc_start_locked(ifp);
518 smc_start_locked(struct ifnet *ifp)
520 struct smc_softc *sc;
522 u_int len, npages, spin_count;
525 SMC_ASSERT_LOCKED(sc);
527 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
529 if (IFQ_IS_EMPTY(&ifp->if_snd))
533 * Grab the next packet. If it's too big, drop it.
535 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
536 len = m_length(m, NULL);
538 if (len > ETHER_MAX_LEN - ETHER_CRC_LEN) {
539 if_printf(ifp, "large packet discarded\n");
540 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
542 return; /* XXX readcheck? */
546 * Flag that we're busy.
548 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
552 * Work out how many 256 byte "pages" we need. We have to include the
553 * control data for the packet in this calculation.
555 npages = (len + PKT_CTRL_DATA_LEN) >> 8;
562 smc_select_bank(sc, 2);
564 smc_write_2(sc, MMUCR, MMUCR_CMD_TX_ALLOC | npages);
567 * Spin briefly to see if the allocation succeeds.
569 spin_count = TX_ALLOC_WAIT_TIME;
571 if (smc_read_1(sc, IST) & ALLOC_INT) {
572 smc_write_1(sc, ACK, ALLOC_INT);
575 } while (--spin_count);
578 * If the allocation is taking too long, unmask the alloc interrupt
581 if (spin_count == 0) {
582 sc->smc_mask |= ALLOC_INT;
583 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
584 smc_write_1(sc, MSK, sc->smc_mask);
588 taskqueue_enqueue(sc->smc_tq, &sc->smc_tx);
592 smc_task_tx(void *context, int pending)
595 struct smc_softc *sc;
602 ifp = (struct ifnet *)context;
607 if (sc->smc_pending == NULL) {
612 m = m0 = sc->smc_pending;
613 sc->smc_pending = NULL;
614 smc_select_bank(sc, 2);
617 * Check the allocation result.
619 packet = smc_read_1(sc, ARR);
622 * If the allocation failed, requeue the packet and retry.
624 if (packet & ARR_FAILED) {
625 IFQ_DRV_PREPEND(&ifp->if_snd, m);
626 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
627 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
628 smc_start_locked(ifp);
634 * Tell the device to write to our packet number.
636 smc_write_1(sc, PNR, packet);
637 smc_write_2(sc, PTR, 0 | PTR_AUTO_INCR);
640 * Tell the device how long the packet is (including control data).
642 len = m_length(m, 0);
643 len += PKT_CTRL_DATA_LEN;
644 smc_write_2(sc, DATA0, 0);
645 smc_write_2(sc, DATA0, len);
648 * Push the data out to the device.
652 for (; m != NULL; m = m->m_next) {
653 data = mtod(m, uint8_t *);
654 smc_write_multi_2(sc, DATA0, (uint16_t *)data, m->m_len / 2);
659 * Push out the control byte and and the odd byte if needed.
661 if ((len & 1) != 0 && data != NULL)
662 smc_write_2(sc, DATA0, (CTRL_ODD << 8) | data[last_len - 1]);
664 smc_write_2(sc, DATA0, 0);
667 * Unmask the TX empty interrupt.
669 sc->smc_mask |= TX_EMPTY_INT;
670 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
671 smc_write_1(sc, MSK, sc->smc_mask);
674 * Enqueue the packet.
677 smc_write_2(sc, MMUCR, MMUCR_CMD_ENQUEUE);
678 callout_reset(&sc->smc_watchdog, hz * 2, smc_watchdog, sc);
683 if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
684 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
691 * See if there's anything else to do.
697 smc_task_rx(void *context, int pending)
699 u_int packet, status, len;
702 struct smc_softc *sc;
703 struct mbuf *m, *mhead, *mtail;
706 ifp = (struct ifnet *)context;
708 mhead = mtail = NULL;
712 packet = smc_read_1(sc, FIFO_RX);
713 while ((packet & FIFO_EMPTY) == 0) {
715 * Grab an mbuf and attach a cluster.
717 MGETHDR(m, M_NOWAIT, MT_DATA);
721 if (!(MCLGET(m, M_NOWAIT))) {
727 * Point to the start of the packet.
729 smc_select_bank(sc, 2);
730 smc_write_1(sc, PNR, packet);
731 smc_write_2(sc, PTR, 0 | PTR_READ | PTR_RCV | PTR_AUTO_INCR);
734 * Grab status and packet length.
736 status = smc_read_2(sc, DATA0);
737 len = smc_read_2(sc, DATA0) & RX_LEN_MASK;
739 if (status & RX_ODDFRM)
745 if (status & (RX_TOOSHORT | RX_TOOLNG | RX_BADCRC | RX_ALGNERR)) {
747 smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE);
748 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
754 * Set the mbuf up the way we want it.
756 m->m_pkthdr.rcvif = ifp;
757 m->m_pkthdr.len = m->m_len = len + 2; /* XXX: Is this right? */
758 m_adj(m, ETHER_ALIGN);
761 * Pull the packet out of the device. Make sure we're in the
762 * right bank first as things may have changed while we were
763 * allocating our mbuf.
765 smc_select_bank(sc, 2);
766 smc_write_1(sc, PNR, packet);
767 smc_write_2(sc, PTR, 4 | PTR_READ | PTR_RCV | PTR_AUTO_INCR);
768 data = mtod(m, uint8_t *);
769 smc_read_multi_2(sc, DATA0, (uint16_t *)data, len >> 1);
772 *data = smc_read_1(sc, DATA0);
776 * Tell the device we're done.
779 smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE);
791 packet = smc_read_1(sc, FIFO_RX);
794 sc->smc_mask |= RCV_INT;
795 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
796 smc_write_1(sc, MSK, sc->smc_mask);
800 while (mhead != NULL) {
802 mhead = mhead->m_next;
804 if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
805 (*ifp->if_input)(ifp, m);
809 #ifdef DEVICE_POLLING
811 smc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
813 struct smc_softc *sc;
818 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
824 if (cmd == POLL_AND_CHECK_STATUS)
825 taskqueue_enqueue(sc->smc_tq, &sc->smc_intr);
831 smc_intr(void *context)
833 struct smc_softc *sc;
836 sc = (struct smc_softc *)context;
839 * Save current bank and restore later in this function
841 curbank = (smc_read_2(sc, BSR) & BSR_BANK_MASK);
844 * Block interrupts in order to let smc_task_intr to kick in
846 smc_select_bank(sc, 2);
847 smc_write_1(sc, MSK, 0);
850 smc_select_bank(sc, curbank);
852 taskqueue_enqueue(sc->smc_tq, &sc->smc_intr);
853 return (FILTER_HANDLED);
857 smc_task_intr(void *context, int pending)
859 struct smc_softc *sc;
861 u_int status, packet, counter, tcr;
864 ifp = (struct ifnet *)context;
869 smc_select_bank(sc, 2);
872 * Find out what interrupts are flagged.
874 status = smc_read_1(sc, IST) & sc->smc_mask;
879 if (status & TX_INT) {
881 * Kill off the packet if there is one and re-enable transmit.
883 packet = smc_read_1(sc, FIFO_TX);
884 if ((packet & FIFO_EMPTY) == 0) {
885 callout_stop(&sc->smc_watchdog);
886 smc_select_bank(sc, 2);
887 smc_write_1(sc, PNR, packet);
888 smc_write_2(sc, PTR, 0 | PTR_READ |
890 smc_select_bank(sc, 0);
891 tcr = smc_read_2(sc, EPHSR);
893 if ((tcr & EPHSR_TX_SUC) == 0)
894 device_printf(sc->smc_dev,
897 smc_select_bank(sc, 2);
899 smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE_PKT);
901 smc_select_bank(sc, 0);
902 tcr = smc_read_2(sc, TCR);
903 tcr |= TCR_TXENA | TCR_PAD_EN;
904 smc_write_2(sc, TCR, tcr);
905 smc_select_bank(sc, 2);
906 taskqueue_enqueue(sc->smc_tq, &sc->smc_tx);
912 smc_write_1(sc, ACK, TX_INT);
918 if (status & RCV_INT) {
919 smc_write_1(sc, ACK, RCV_INT);
920 sc->smc_mask &= ~RCV_INT;
921 taskqueue_enqueue(sc->smc_tq, &sc->smc_rx);
927 if (status & ALLOC_INT) {
928 smc_write_1(sc, ACK, ALLOC_INT);
929 sc->smc_mask &= ~ALLOC_INT;
930 taskqueue_enqueue(sc->smc_tq, &sc->smc_tx);
936 if (status & RX_OVRN_INT) {
937 smc_write_1(sc, ACK, RX_OVRN_INT);
938 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
944 if (status & TX_EMPTY_INT) {
945 smc_write_1(sc, ACK, TX_EMPTY_INT);
946 sc->smc_mask &= ~TX_EMPTY_INT;
947 callout_stop(&sc->smc_watchdog);
950 * Update collision stats.
952 smc_select_bank(sc, 0);
953 counter = smc_read_2(sc, ECR);
954 smc_select_bank(sc, 2);
955 if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
956 ((counter & ECR_SNGLCOL_MASK) >> ECR_SNGLCOL_SHIFT) +
957 ((counter & ECR_MULCOL_MASK) >> ECR_MULCOL_SHIFT));
960 * See if there are any packets to transmit.
962 taskqueue_enqueue(sc->smc_tq, &sc->smc_tx);
966 * Update the interrupt mask.
968 smc_select_bank(sc, 2);
969 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
970 smc_write_1(sc, MSK, sc->smc_mask);
976 smc_mii_bitbang_read(device_t dev)
978 struct smc_softc *sc;
981 sc = device_get_softc(dev);
983 SMC_ASSERT_LOCKED(sc);
984 KASSERT((smc_read_2(sc, BSR) & BSR_BANK_MASK) == 3,
985 ("%s: smc_mii_bitbang_read called with bank %d (!= 3)",
986 device_get_nameunit(sc->smc_dev),
987 smc_read_2(sc, BSR) & BSR_BANK_MASK));
989 val = smc_read_2(sc, MGMT);
990 smc_barrier(sc, MGMT, 2,
991 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
997 smc_mii_bitbang_write(device_t dev, uint32_t val)
999 struct smc_softc *sc;
1001 sc = device_get_softc(dev);
1003 SMC_ASSERT_LOCKED(sc);
1004 KASSERT((smc_read_2(sc, BSR) & BSR_BANK_MASK) == 3,
1005 ("%s: smc_mii_bitbang_write called with bank %d (!= 3)",
1006 device_get_nameunit(sc->smc_dev),
1007 smc_read_2(sc, BSR) & BSR_BANK_MASK));
1009 smc_write_2(sc, MGMT, val);
1010 smc_barrier(sc, MGMT, 2,
1011 BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
1015 smc_miibus_readreg(device_t dev, int phy, int reg)
1017 struct smc_softc *sc;
1020 sc = device_get_softc(dev);
1024 smc_select_bank(sc, 3);
1026 val = mii_bitbang_readreg(dev, &smc_mii_bitbang_ops, phy, reg);
1033 smc_miibus_writereg(device_t dev, int phy, int reg, int data)
1035 struct smc_softc *sc;
1037 sc = device_get_softc(dev);
1041 smc_select_bank(sc, 3);
1043 mii_bitbang_writereg(dev, &smc_mii_bitbang_ops, phy, reg, data);
1050 smc_miibus_statchg(device_t dev)
1052 struct smc_softc *sc;
1053 struct mii_data *mii;
1056 sc = device_get_softc(dev);
1057 mii = device_get_softc(sc->smc_miibus);
1061 smc_select_bank(sc, 0);
1062 tcr = smc_read_2(sc, TCR);
1064 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
1069 smc_write_2(sc, TCR, tcr);
1075 smc_mii_ifmedia_upd(struct ifnet *ifp)
1077 struct smc_softc *sc;
1078 struct mii_data *mii;
1081 if (sc->smc_miibus == NULL)
1084 mii = device_get_softc(sc->smc_miibus);
1085 return (mii_mediachg(mii));
1089 smc_mii_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1091 struct smc_softc *sc;
1092 struct mii_data *mii;
1095 if (sc->smc_miibus == NULL)
1098 mii = device_get_softc(sc->smc_miibus);
1100 ifmr->ifm_active = mii->mii_media_active;
1101 ifmr->ifm_status = mii->mii_media_status;
1105 smc_mii_tick(void *context)
1107 struct smc_softc *sc;
1109 sc = (struct smc_softc *)context;
1111 if (sc->smc_miibus == NULL)
1116 mii_tick(device_get_softc(sc->smc_miibus));
1117 callout_reset(&sc->smc_mii_tick_ch, hz, smc_mii_tick, sc);
1121 smc_mii_mediachg(struct smc_softc *sc)
1124 if (sc->smc_miibus == NULL)
1126 mii_mediachg(device_get_softc(sc->smc_miibus));
1130 smc_mii_mediaioctl(struct smc_softc *sc, struct ifreq *ifr, u_long command)
1132 struct mii_data *mii;
1134 if (sc->smc_miibus == NULL)
1137 mii = device_get_softc(sc->smc_miibus);
1138 return (ifmedia_ioctl(sc->smc_ifp, ifr, &mii->mii_media, command));
1142 smc_reset(struct smc_softc *sc)
1146 SMC_ASSERT_LOCKED(sc);
1148 smc_select_bank(sc, 2);
1151 * Mask all interrupts.
1153 smc_write_1(sc, MSK, 0);
1156 * Tell the device to reset.
1158 smc_select_bank(sc, 0);
1159 smc_write_2(sc, RCR, RCR_SOFT_RST);
1162 * Set up the configuration register.
1164 smc_select_bank(sc, 1);
1165 smc_write_2(sc, CR, CR_EPH_POWER_EN);
1169 * Turn off transmit and receive.
1171 smc_select_bank(sc, 0);
1172 smc_write_2(sc, TCR, 0);
1173 smc_write_2(sc, RCR, 0);
1176 * Set up the control register.
1178 smc_select_bank(sc, 1);
1179 ctr = smc_read_2(sc, CTR);
1180 ctr |= CTR_LE_ENABLE | CTR_AUTO_RELEASE;
1181 smc_write_2(sc, CTR, ctr);
1186 smc_select_bank(sc, 2);
1188 smc_write_2(sc, MMUCR, MMUCR_CMD_MMU_RESET);
1192 smc_enable(struct smc_softc *sc)
1196 SMC_ASSERT_LOCKED(sc);
1200 * Set up the receive/PHY control register.
1202 smc_select_bank(sc, 0);
1203 smc_write_2(sc, RPCR, RPCR_ANEG | (RPCR_LED_LINK_ANY << RPCR_LSA_SHIFT)
1204 | (RPCR_LED_ACT_ANY << RPCR_LSB_SHIFT));
1207 * Set up the transmit and receive control registers.
1209 smc_write_2(sc, TCR, TCR_TXENA | TCR_PAD_EN);
1210 smc_write_2(sc, RCR, RCR_RXEN | RCR_STRIP_CRC);
1213 * Set up the interrupt mask.
1215 smc_select_bank(sc, 2);
1216 sc->smc_mask = EPH_INT | RX_OVRN_INT | RCV_INT | TX_INT;
1217 if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1218 smc_write_1(sc, MSK, sc->smc_mask);
1222 smc_stop(struct smc_softc *sc)
1225 SMC_ASSERT_LOCKED(sc);
1228 * Turn off callouts.
1230 callout_stop(&sc->smc_watchdog);
1231 callout_stop(&sc->smc_mii_tick_ch);
1234 * Mask all interrupts.
1236 smc_select_bank(sc, 2);
1238 smc_write_1(sc, MSK, 0);
1239 #ifdef DEVICE_POLLING
1240 ether_poll_deregister(sc->smc_ifp);
1241 sc->smc_ifp->if_capenable &= ~IFCAP_POLLING;
1245 * Disable transmit and receive.
1247 smc_select_bank(sc, 0);
1248 smc_write_2(sc, TCR, 0);
1249 smc_write_2(sc, RCR, 0);
1251 sc->smc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1255 smc_watchdog(void *arg)
1257 struct smc_softc *sc;
1259 sc = (struct smc_softc *)arg;
1260 device_printf(sc->smc_dev, "watchdog timeout\n");
1261 taskqueue_enqueue(sc->smc_tq, &sc->smc_intr);
1265 smc_init(void *context)
1267 struct smc_softc *sc;
1269 sc = (struct smc_softc *)context;
1271 smc_init_locked(sc);
1276 smc_init_locked(struct smc_softc *sc)
1280 SMC_ASSERT_LOCKED(sc);
1282 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1288 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1289 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1291 smc_start_locked(ifp);
1293 if (sc->smc_mii_tick != NULL)
1294 callout_reset(&sc->smc_mii_tick_ch, hz, sc->smc_mii_tick, sc);
1296 #ifdef DEVICE_POLLING
1298 ether_poll_register(smc_poll, ifp);
1300 ifp->if_capenable |= IFCAP_POLLING;
1305 smc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1307 struct smc_softc *sc;
1315 if ((ifp->if_flags & IFF_UP) == 0 &&
1316 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1322 if (sc->smc_mii_mediachg != NULL)
1323 sc->smc_mii_mediachg(sc);
1339 if (sc->smc_mii_mediaioctl == NULL) {
1343 sc->smc_mii_mediaioctl(sc, (struct ifreq *)data, cmd);
1347 error = ether_ioctl(ifp, cmd, data);