2 * Copyright (c) 2008 Benno Rice. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
29 * Driver for SMSC LAN91C111, may work for older variants.
32 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include "opt_device_polling.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/kernel.h>
40 #include <sys/sockio.h>
41 #include <sys/malloc.h>
43 #include <sys/queue.h>
44 #include <sys/socket.h>
45 #include <sys/syslog.h>
46 #include <sys/taskqueue.h>
48 #include <sys/module.h>
51 #include <machine/bus.h>
52 #include <machine/resource.h>
55 #include <net/ethernet.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <net/if_mib.h>
61 #include <net/if_media.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip.h>
71 #include <net/bpfdesc.h>
73 #include <dev/smc/if_smcreg.h>
74 #include <dev/smc/if_smcvar.h>
76 #include <dev/mii/mii.h>
77 #include <dev/mii/miivar.h>
79 #define SMC_LOCK(sc) mtx_lock(&(sc)->smc_mtx)
80 #define SMC_UNLOCK(sc) mtx_unlock(&(sc)->smc_mtx)
81 #define SMC_ASSERT_LOCKED(sc) mtx_assert(&(sc)->smc_mtx, MA_OWNED)
83 #define SMC_INTR_PRIORITY 0
84 #define SMC_RX_PRIORITY 5
85 #define SMC_TX_PRIORITY 10
87 devclass_t smc_devclass;
89 static const char *smc_chip_ids[16] = {
91 /* 3 */ "SMSC LAN91C90 or LAN91C92",
92 /* 4 */ "SMSC LAN91C94",
93 /* 5 */ "SMSC LAN91C95",
94 /* 6 */ "SMSC LAN91C96",
95 /* 7 */ "SMSC LAN91C100",
96 /* 8 */ "SMSC LAN91C100FD",
97 /* 9 */ "SMSC LAN91C110FD or LAN91C111FD",
102 static void smc_init(void *);
103 static void smc_start(struct ifnet *);
104 static void smc_stop(struct smc_softc *);
105 static int smc_ioctl(struct ifnet *, u_long, caddr_t);
107 static void smc_init_locked(struct smc_softc *);
108 static void smc_start_locked(struct ifnet *);
109 static void smc_reset(struct smc_softc *);
110 static int smc_mii_ifmedia_upd(struct ifnet *);
111 static void smc_mii_ifmedia_sts(struct ifnet *, struct ifmediareq *);
112 static void smc_mii_tick(void *);
113 static void smc_mii_mediachg(struct smc_softc *);
114 static int smc_mii_mediaioctl(struct smc_softc *, struct ifreq *, u_long);
116 static void smc_task_intr(void *, int);
117 static void smc_task_rx(void *, int);
118 static void smc_task_tx(void *, int);
120 static driver_filter_t smc_intr;
121 static timeout_t smc_watchdog;
122 #ifdef DEVICE_POLLING
123 static poll_handler_t smc_poll;
127 smc_select_bank(struct smc_softc *sc, uint16_t bank)
130 bus_write_2(sc->smc_reg, BSR, bank & BSR_BANK_MASK);
133 /* Never call this when not in bank 2. */
135 smc_mmu_wait(struct smc_softc *sc)
138 KASSERT((bus_read_2(sc->smc_reg, BSR) &
139 BSR_BANK_MASK) == 2, ("%s: smc_mmu_wait called when not in bank 2",
140 device_get_nameunit(sc->smc_dev)));
141 while (bus_read_2(sc->smc_reg, MMUCR) & MMUCR_BUSY)
145 static __inline uint8_t
146 smc_read_1(struct smc_softc *sc, bus_addr_t offset)
149 return (bus_read_1(sc->smc_reg, offset));
153 smc_write_1(struct smc_softc *sc, bus_addr_t offset, uint8_t val)
156 bus_write_1(sc->smc_reg, offset, val);
159 static __inline uint16_t
160 smc_read_2(struct smc_softc *sc, bus_addr_t offset)
163 return (bus_read_2(sc->smc_reg, offset));
167 smc_write_2(struct smc_softc *sc, bus_addr_t offset, uint16_t val)
170 bus_write_2(sc->smc_reg, offset, val);
174 smc_read_multi_2(struct smc_softc *sc, bus_addr_t offset, uint16_t *datap,
178 bus_read_multi_2(sc->smc_reg, offset, datap, count);
182 smc_write_multi_2(struct smc_softc *sc, bus_addr_t offset, uint16_t *datap,
186 bus_write_multi_2(sc->smc_reg, offset, datap, count);
190 smc_probe(device_t dev)
192 int rid, type, error;
194 struct smc_softc *sc;
195 struct resource *reg;
197 sc = device_get_softc(dev);
199 type = SYS_RES_IOPORT;
203 type = SYS_RES_MEMORY;
205 reg = bus_alloc_resource(dev, type, &rid, 0, ~0, 16, RF_ACTIVE);
209 "could not allocate I/O resource for probe\n");
213 /* Check for the identification value in the BSR. */
214 val = bus_read_2(reg, BSR);
215 if ((val & BSR_IDENTIFY_MASK) != BSR_IDENTIFY) {
217 device_printf(dev, "identification value not in BSR\n");
223 * Try switching banks and make sure we still get the identification
226 bus_write_2(reg, BSR, 0);
227 val = bus_read_2(reg, BSR);
228 if ((val & BSR_IDENTIFY_MASK) != BSR_IDENTIFY) {
231 "identification value not in BSR after write\n");
238 bus_write_2(reg, BSR, 1);
239 val = bus_read_2(reg, BAR);
240 val = BAR_ADDRESS(val);
241 if (rman_get_start(reg) != val) {
243 device_printf(dev, "BAR address %x does not match "
244 "I/O resource address %lx\n", val,
245 rman_get_start(reg));
251 /* Compare REV against known chip revisions. */
252 bus_write_2(reg, BSR, 3);
253 val = bus_read_2(reg, REV);
254 val = (val & REV_CHIP_MASK) >> REV_CHIP_SHIFT;
255 if (smc_chip_ids[val] == NULL) {
257 device_printf(dev, "Unknown chip revision: %d\n", val);
262 device_set_desc(dev, smc_chip_ids[val]);
265 bus_release_resource(dev, type, rid, reg);
270 smc_attach(device_t dev)
274 u_char eaddr[ETHER_ADDR_LEN];
275 struct smc_softc *sc;
278 sc = device_get_softc(dev);
283 ifp = sc->smc_ifp = if_alloc(IFT_ETHER);
289 mtx_init(&sc->smc_mtx, device_get_nameunit(dev), NULL, MTX_DEF);
291 /* Set up watchdog callout. */
292 callout_init_mtx(&sc->smc_watchdog, &sc->smc_mtx, 0);
294 type = SYS_RES_IOPORT;
296 type = SYS_RES_MEMORY;
299 sc->smc_reg = bus_alloc_resource(dev, type, &sc->smc_reg_rid, 0, ~0,
301 if (sc->smc_reg == NULL) {
306 sc->smc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &sc->smc_irq_rid, 0,
307 ~0, 1, RF_ACTIVE | RF_SHAREABLE);
308 if (sc->smc_irq == NULL) {
317 smc_select_bank(sc, 3);
318 val = smc_read_2(sc, REV);
319 sc->smc_chip = (val & REV_CHIP_MASK) >> REV_CHIP_SHIFT;
320 sc->smc_rev = (val * REV_REV_MASK) >> REV_REV_SHIFT;
322 device_printf(dev, "revision %x\n", sc->smc_rev);
324 callout_init_mtx(&sc->smc_mii_tick_ch, &sc->smc_mtx,
325 CALLOUT_RETURNUNLOCKED);
326 if (sc->smc_chip >= REV_CHIP_91110FD) {
327 (void)mii_attach(dev, &sc->smc_miibus, ifp,
328 smc_mii_ifmedia_upd, smc_mii_ifmedia_sts, BMSR_DEFCAPMASK,
329 MII_PHY_ANY, MII_OFFSET_ANY, 0);
330 if (sc->smc_miibus != NULL) {
331 sc->smc_mii_tick = smc_mii_tick;
332 sc->smc_mii_mediachg = smc_mii_mediachg;
333 sc->smc_mii_mediaioctl = smc_mii_mediaioctl;
337 smc_select_bank(sc, 1);
338 eaddr[0] = smc_read_1(sc, IAR0);
339 eaddr[1] = smc_read_1(sc, IAR1);
340 eaddr[2] = smc_read_1(sc, IAR2);
341 eaddr[3] = smc_read_1(sc, IAR3);
342 eaddr[4] = smc_read_1(sc, IAR4);
343 eaddr[5] = smc_read_1(sc, IAR5);
345 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
347 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
348 ifp->if_init = smc_init;
349 ifp->if_ioctl = smc_ioctl;
350 ifp->if_start = smc_start;
351 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
352 IFQ_SET_READY(&ifp->if_snd);
354 ifp->if_capabilities = ifp->if_capenable = 0;
356 #ifdef DEVICE_POLLING
357 ifp->if_capabilities |= IFCAP_POLLING;
360 ether_ifattach(ifp, eaddr);
362 /* Set up taskqueue */
363 TASK_INIT(&sc->smc_intr, SMC_INTR_PRIORITY, smc_task_intr, ifp);
364 TASK_INIT(&sc->smc_rx, SMC_RX_PRIORITY, smc_task_rx, ifp);
365 TASK_INIT(&sc->smc_tx, SMC_TX_PRIORITY, smc_task_tx, ifp);
366 sc->smc_tq = taskqueue_create_fast("smc_taskq", M_NOWAIT,
367 taskqueue_thread_enqueue, &sc->smc_tq);
368 taskqueue_start_threads(&sc->smc_tq, 1, PI_NET, "%s taskq",
369 device_get_nameunit(sc->smc_dev));
371 /* Mask all interrupts. */
373 smc_write_1(sc, MSK, 0);
375 /* Wire up interrupt */
376 error = bus_setup_intr(dev, sc->smc_irq,
377 INTR_TYPE_NET|INTR_MPSAFE, smc_intr, NULL, sc, &sc->smc_ih);
388 smc_detach(device_t dev)
391 struct smc_softc *sc;
393 sc = device_get_softc(dev);
398 if (sc->smc_ifp != NULL) {
399 ether_ifdetach(sc->smc_ifp);
402 callout_drain(&sc->smc_watchdog);
403 callout_drain(&sc->smc_mii_tick_ch);
405 #ifdef DEVICE_POLLING
406 if (sc->smc_ifp->if_capenable & IFCAP_POLLING)
407 ether_poll_deregister(sc->smc_ifp);
410 if (sc->smc_ih != NULL)
411 bus_teardown_intr(sc->smc_dev, sc->smc_irq, sc->smc_ih);
413 if (sc->smc_tq != NULL) {
414 taskqueue_drain(sc->smc_tq, &sc->smc_intr);
415 taskqueue_drain(sc->smc_tq, &sc->smc_rx);
416 taskqueue_drain(sc->smc_tq, &sc->smc_tx);
417 taskqueue_free(sc->smc_tq);
421 if (sc->smc_ifp != NULL) {
422 if_free(sc->smc_ifp);
425 if (sc->smc_miibus != NULL) {
426 device_delete_child(sc->smc_dev, sc->smc_miibus);
427 bus_generic_detach(sc->smc_dev);
430 if (sc->smc_reg != NULL) {
431 type = SYS_RES_IOPORT;
433 type = SYS_RES_MEMORY;
435 bus_release_resource(sc->smc_dev, type, sc->smc_reg_rid,
439 if (sc->smc_irq != NULL)
440 bus_release_resource(sc->smc_dev, SYS_RES_IRQ, sc->smc_irq_rid,
443 if (mtx_initialized(&sc->smc_mtx))
444 mtx_destroy(&sc->smc_mtx);
450 smc_start(struct ifnet *ifp)
452 struct smc_softc *sc;
456 smc_start_locked(ifp);
461 smc_start_locked(struct ifnet *ifp)
463 struct smc_softc *sc;
465 u_int len, npages, spin_count;
468 SMC_ASSERT_LOCKED(sc);
470 if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
472 if (IFQ_IS_EMPTY(&ifp->if_snd))
476 * Grab the next packet. If it's too big, drop it.
478 IFQ_DRV_DEQUEUE(&ifp->if_snd, m);
479 len = m_length(m, NULL);
481 if (len > ETHER_MAX_LEN - ETHER_CRC_LEN) {
482 if_printf(ifp, "large packet discarded\n");
485 return; /* XXX readcheck? */
489 * Flag that we're busy.
491 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
495 * Work out how many 256 byte "pages" we need. We have to include the
496 * control data for the packet in this calculation.
498 npages = (len * PKT_CTRL_DATA_LEN) >> 8;
505 smc_select_bank(sc, 2);
507 smc_write_2(sc, MMUCR, MMUCR_CMD_TX_ALLOC | npages);
510 * Spin briefly to see if the allocation succeeds.
512 spin_count = TX_ALLOC_WAIT_TIME;
514 if (smc_read_1(sc, IST) & ALLOC_INT) {
515 smc_write_1(sc, ACK, ALLOC_INT);
518 } while (--spin_count);
521 * If the allocation is taking too long, unmask the alloc interrupt
524 if (spin_count == 0) {
525 sc->smc_mask |= ALLOC_INT;
526 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
527 smc_write_1(sc, MSK, sc->smc_mask);
531 taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_tx);
535 smc_task_tx(void *context, int pending)
538 struct smc_softc *sc;
545 ifp = (struct ifnet *)context;
550 if (sc->smc_pending == NULL) {
555 m = m0 = sc->smc_pending;
556 sc->smc_pending = NULL;
557 smc_select_bank(sc, 2);
560 * Check the allocation result.
562 packet = smc_read_1(sc, ARR);
565 * If the allocation failed, requeue the packet and retry.
567 if (packet & ARR_FAILED) {
568 IFQ_DRV_PREPEND(&ifp->if_snd, m);
570 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
571 smc_start_locked(ifp);
577 * Tell the device to write to our packet number.
579 smc_write_1(sc, PNR, packet);
580 smc_write_2(sc, PTR, 0 | PTR_AUTO_INCR);
583 * Tell the device how long the packet is (including control data).
585 len = m_length(m, 0);
586 len += PKT_CTRL_DATA_LEN;
587 smc_write_2(sc, DATA0, 0);
588 smc_write_2(sc, DATA0, len);
591 * Push the data out to the device.
595 for (; m != NULL; m = m->m_next) {
596 data = mtod(m, uint8_t *);
597 smc_write_multi_2(sc, DATA0, (uint16_t *)data, m->m_len / 2);
602 * Push out the control byte and and the odd byte if needed.
604 if ((len & 1) != 0 && data != NULL)
605 smc_write_2(sc, DATA0, (CTRL_ODD << 8) | data[last_len - 1]);
607 smc_write_2(sc, DATA0, 0);
610 * Unmask the TX empty interrupt.
612 sc->smc_mask |= TX_EMPTY_INT;
613 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
614 smc_write_1(sc, MSK, sc->smc_mask);
617 * Enqueue the packet.
620 smc_write_2(sc, MMUCR, MMUCR_CMD_ENQUEUE);
621 callout_reset(&sc->smc_watchdog, hz * 2, smc_watchdog, sc);
627 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
634 * See if there's anything else to do.
640 smc_task_rx(void *context, int pending)
642 u_int packet, status, len;
645 struct smc_softc *sc;
646 struct mbuf *m, *mhead, *mtail;
649 ifp = (struct ifnet *)context;
651 mhead = mtail = NULL;
655 packet = smc_read_1(sc, FIFO_RX);
656 while ((packet & FIFO_EMPTY) == 0) {
658 * Grab an mbuf and attach a cluster.
660 MGETHDR(m, M_DONTWAIT, MT_DATA);
664 MCLGET(m, M_DONTWAIT);
665 if ((m->m_flags & M_EXT) == 0) {
671 * Point to the start of the packet.
673 smc_select_bank(sc, 2);
674 smc_write_1(sc, PNR, packet);
675 smc_write_2(sc, PTR, 0 | PTR_READ | PTR_RCV | PTR_AUTO_INCR);
678 * Grab status and packet length.
680 status = smc_read_2(sc, DATA0);
681 len = smc_read_2(sc, DATA0) & RX_LEN_MASK;
683 if (status & RX_ODDFRM)
689 if (status & (RX_TOOSHORT | RX_TOOLNG | RX_BADCRC | RX_ALGNERR)) {
691 smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE);
698 * Set the mbuf up the way we want it.
700 m->m_pkthdr.rcvif = ifp;
701 m->m_pkthdr.len = m->m_len = len + 2; /* XXX: Is this right? */
702 m_adj(m, ETHER_ALIGN);
705 * Pull the packet out of the device. Make sure we're in the
706 * right bank first as things may have changed while we were
707 * allocating our mbuf.
709 smc_select_bank(sc, 2);
710 smc_write_1(sc, PNR, packet);
711 smc_write_2(sc, PTR, 4 | PTR_READ | PTR_RCV | PTR_AUTO_INCR);
712 data = mtod(m, uint8_t *);
713 smc_read_multi_2(sc, DATA0, (uint16_t *)data, len >> 1);
716 *data = smc_read_1(sc, DATA0);
720 * Tell the device we're done.
723 smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE);
735 packet = smc_read_1(sc, FIFO_RX);
738 sc->smc_mask |= RCV_INT;
739 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
740 smc_write_1(sc, MSK, sc->smc_mask);
744 while (mhead != NULL) {
746 mhead = mhead->m_next;
749 (*ifp->if_input)(ifp, m);
753 #ifdef DEVICE_POLLING
755 smc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
757 struct smc_softc *sc;
762 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
768 if (cmd == POLL_AND_CHECK_STATUS)
769 taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_intr);
774 smc_intr(void *context)
776 struct smc_softc *sc;
778 sc = (struct smc_softc *)context;
779 taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_intr);
780 return (FILTER_HANDLED);
784 smc_task_intr(void *context, int pending)
786 struct smc_softc *sc;
788 u_int status, packet, counter, tcr;
791 ifp = (struct ifnet *)context;
796 smc_select_bank(sc, 2);
799 * Get the current mask, and then block all interrupts while we're
802 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
803 smc_write_1(sc, MSK, 0);
806 * Find out what interrupts are flagged.
808 status = smc_read_1(sc, IST) & sc->smc_mask;
813 if (status & TX_INT) {
815 * Kill off the packet if there is one and re-enable transmit.
817 packet = smc_read_1(sc, FIFO_TX);
818 if ((packet & FIFO_EMPTY) == 0) {
819 smc_write_1(sc, PNR, packet);
820 smc_write_2(sc, PTR, 0 | PTR_READ |
822 tcr = smc_read_2(sc, DATA0);
823 if ((tcr & EPHSR_TX_SUC) == 0)
824 device_printf(sc->smc_dev,
827 smc_write_2(sc, MMUCR, MMUCR_CMD_RELEASE_PKT);
829 smc_select_bank(sc, 0);
830 tcr = smc_read_2(sc, TCR);
831 tcr |= TCR_TXENA | TCR_PAD_EN;
832 smc_write_2(sc, TCR, tcr);
833 smc_select_bank(sc, 2);
834 taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_tx);
840 smc_write_1(sc, ACK, TX_INT);
846 if (status & RCV_INT) {
847 smc_write_1(sc, ACK, RCV_INT);
848 sc->smc_mask &= ~RCV_INT;
849 taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_rx);
855 if (status & ALLOC_INT) {
856 smc_write_1(sc, ACK, ALLOC_INT);
857 sc->smc_mask &= ~ALLOC_INT;
858 taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_tx);
864 if (status & RX_OVRN_INT) {
865 smc_write_1(sc, ACK, RX_OVRN_INT);
872 if (status & TX_EMPTY_INT) {
873 smc_write_1(sc, ACK, TX_EMPTY_INT);
874 sc->smc_mask &= ~TX_EMPTY_INT;
875 callout_stop(&sc->smc_watchdog);
878 * Update collision stats.
880 smc_select_bank(sc, 0);
881 counter = smc_read_2(sc, ECR);
882 smc_select_bank(sc, 2);
883 ifp->if_collisions +=
884 (counter & ECR_SNGLCOL_MASK) >> ECR_SNGLCOL_SHIFT;
885 ifp->if_collisions +=
886 (counter & ECR_MULCOL_MASK) >> ECR_MULCOL_SHIFT;
889 * See if there are any packets to transmit.
891 taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_tx);
895 * Update the interrupt mask.
897 if ((ifp->if_capenable & IFCAP_POLLING) == 0)
898 smc_write_1(sc, MSK, sc->smc_mask);
904 smc_mii_readbits(struct smc_softc *sc, int nbits)
906 u_int mgmt, mask, val;
908 SMC_ASSERT_LOCKED(sc);
909 KASSERT((smc_read_2(sc, BSR) & BSR_BANK_MASK) == 3,
910 ("%s: smc_mii_readbits called with bank %d (!= 3)",
911 device_get_nameunit(sc->smc_dev),
912 smc_read_2(sc, BSR) & BSR_BANK_MASK));
915 * Set up the MGMT (aka MII) register.
917 mgmt = smc_read_2(sc, MGMT) & ~(MGMT_MCLK | MGMT_MDOE | MGMT_MDO);
918 smc_write_2(sc, MGMT, mgmt);
923 for (mask = 1 << (nbits - 1), val = 0; mask; mask >>= 1) {
924 if (smc_read_2(sc, MGMT) & MGMT_MDI)
927 smc_write_2(sc, MGMT, mgmt);
929 smc_write_2(sc, MGMT, mgmt | MGMT_MCLK);
937 smc_mii_writebits(struct smc_softc *sc, u_int val, int nbits)
941 SMC_ASSERT_LOCKED(sc);
942 KASSERT((smc_read_2(sc, BSR) & BSR_BANK_MASK) == 3,
943 ("%s: smc_mii_writebits called with bank %d (!= 3)",
944 device_get_nameunit(sc->smc_dev),
945 smc_read_2(sc, BSR) & BSR_BANK_MASK));
948 * Set up the MGMT (aka MII) register).
950 mgmt = smc_read_2(sc, MGMT) & ~(MGMT_MCLK | MGMT_MDOE | MGMT_MDO);
956 for (mask = 1 << (nbits - 1); mask; mask >>= 1) {
962 smc_write_2(sc, MGMT, mgmt);
964 smc_write_2(sc, MGMT, mgmt | MGMT_MCLK);
970 smc_miibus_readreg(device_t dev, int phy, int reg)
972 struct smc_softc *sc;
975 sc = device_get_softc(dev);
979 smc_select_bank(sc, 3);
982 * Send out the idle pattern.
984 smc_mii_writebits(sc, 0xffffffff, 32);
987 * Start code + read opcode + phy address + phy register
989 smc_mii_writebits(sc, 6 << 10 | phy << 5 | reg, 14);
994 val = smc_mii_readbits(sc, 18);
997 * Reset the MDIO interface.
999 smc_write_2(sc, MGMT,
1000 smc_read_2(sc, MGMT) & ~(MGMT_MCLK | MGMT_MDOE | MGMT_MDO));
1007 smc_miibus_writereg(device_t dev, int phy, int reg, int data)
1009 struct smc_softc *sc;
1011 sc = device_get_softc(dev);
1015 smc_select_bank(sc, 3);
1018 * Send idle pattern.
1020 smc_mii_writebits(sc, 0xffffffff, 32);
1023 * Start code + write opcode + phy address + phy register + turnaround
1026 smc_mii_writebits(sc, 5 << 28 | phy << 23 | reg << 18 | 2 << 16 | data,
1030 * Reset MDIO interface.
1032 smc_write_2(sc, MGMT,
1033 smc_read_2(sc, MGMT) & ~(MGMT_MCLK | MGMT_MDOE | MGMT_MDO));
1040 smc_miibus_statchg(device_t dev)
1042 struct smc_softc *sc;
1043 struct mii_data *mii;
1046 sc = device_get_softc(dev);
1047 mii = device_get_softc(sc->smc_miibus);
1051 smc_select_bank(sc, 0);
1052 tcr = smc_read_2(sc, TCR);
1054 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
1059 smc_write_2(sc, TCR, tcr);
1065 smc_mii_ifmedia_upd(struct ifnet *ifp)
1067 struct smc_softc *sc;
1068 struct mii_data *mii;
1071 if (sc->smc_miibus == NULL)
1074 mii = device_get_softc(sc->smc_miibus);
1075 return (mii_mediachg(mii));
1079 smc_mii_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1081 struct smc_softc *sc;
1082 struct mii_data *mii;
1085 if (sc->smc_miibus == NULL)
1088 mii = device_get_softc(sc->smc_miibus);
1090 ifmr->ifm_active = mii->mii_media_active;
1091 ifmr->ifm_status = mii->mii_media_status;
1095 smc_mii_tick(void *context)
1097 struct smc_softc *sc;
1099 sc = (struct smc_softc *)context;
1101 if (sc->smc_miibus == NULL)
1106 mii_tick(device_get_softc(sc->smc_miibus));
1107 callout_reset(&sc->smc_mii_tick_ch, hz, smc_mii_tick, sc);
1111 smc_mii_mediachg(struct smc_softc *sc)
1114 if (sc->smc_miibus == NULL)
1116 mii_mediachg(device_get_softc(sc->smc_miibus));
1120 smc_mii_mediaioctl(struct smc_softc *sc, struct ifreq *ifr, u_long command)
1122 struct mii_data *mii;
1124 if (sc->smc_miibus == NULL)
1127 mii = device_get_softc(sc->smc_miibus);
1128 return (ifmedia_ioctl(sc->smc_ifp, ifr, &mii->mii_media, command));
1132 smc_reset(struct smc_softc *sc)
1136 SMC_ASSERT_LOCKED(sc);
1138 smc_select_bank(sc, 2);
1141 * Mask all interrupts.
1143 smc_write_1(sc, MSK, 0);
1146 * Tell the device to reset.
1148 smc_select_bank(sc, 0);
1149 smc_write_2(sc, RCR, RCR_SOFT_RST);
1152 * Set up the configuration register.
1154 smc_select_bank(sc, 1);
1155 smc_write_2(sc, CR, CR_EPH_POWER_EN);
1159 * Turn off transmit and receive.
1161 smc_select_bank(sc, 0);
1162 smc_write_2(sc, TCR, 0);
1163 smc_write_2(sc, RCR, 0);
1166 * Set up the control register.
1168 smc_select_bank(sc, 1);
1169 ctr = smc_read_2(sc, CTR);
1170 ctr |= CTR_LE_ENABLE | CTR_AUTO_RELEASE;
1171 smc_write_2(sc, CTR, ctr);
1176 smc_select_bank(sc, 2);
1178 smc_write_2(sc, MMUCR, MMUCR_CMD_MMU_RESET);
1182 smc_enable(struct smc_softc *sc)
1186 SMC_ASSERT_LOCKED(sc);
1190 * Set up the receive/PHY control register.
1192 smc_select_bank(sc, 0);
1193 smc_write_2(sc, RPCR, RPCR_ANEG | (RPCR_LED_LINK_ANY << RPCR_LSA_SHIFT)
1194 | (RPCR_LED_ACT_ANY << RPCR_LSB_SHIFT));
1197 * Set up the transmit and receive control registers.
1199 smc_write_2(sc, TCR, TCR_TXENA | TCR_PAD_EN);
1200 smc_write_2(sc, RCR, RCR_RXEN | RCR_STRIP_CRC);
1203 * Set up the interrupt mask.
1205 smc_select_bank(sc, 2);
1206 sc->smc_mask = EPH_INT | RX_OVRN_INT | RCV_INT | TX_INT;
1207 if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1208 smc_write_1(sc, MSK, sc->smc_mask);
1212 smc_stop(struct smc_softc *sc)
1215 SMC_ASSERT_LOCKED(sc);
1218 * Turn off callouts.
1220 callout_stop(&sc->smc_watchdog);
1221 callout_stop(&sc->smc_mii_tick_ch);
1224 * Mask all interrupts.
1226 smc_select_bank(sc, 2);
1228 smc_write_1(sc, MSK, 0);
1229 #ifdef DEVICE_POLLING
1230 ether_poll_deregister(sc->smc_ifp);
1231 sc->smc_ifp->if_capenable &= ~IFCAP_POLLING;
1232 sc->smc_ifp->if_capenable &= ~IFCAP_POLLING_NOCOUNT;
1236 * Disable transmit and receive.
1238 smc_select_bank(sc, 0);
1239 smc_write_2(sc, TCR, 0);
1240 smc_write_2(sc, RCR, 0);
1242 sc->smc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1246 smc_watchdog(void *arg)
1248 struct smc_softc *sc;
1250 sc = (struct smc_softc *)arg;
1251 device_printf(sc->smc_dev, "watchdog timeout\n");
1252 taskqueue_enqueue_fast(sc->smc_tq, &sc->smc_intr);
1256 smc_init(void *context)
1258 struct smc_softc *sc;
1260 sc = (struct smc_softc *)context;
1262 smc_init_locked(sc);
1267 smc_init_locked(struct smc_softc *sc)
1273 SMC_ASSERT_LOCKED(sc);
1278 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1279 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1281 smc_start_locked(ifp);
1283 if (sc->smc_mii_tick != NULL)
1284 callout_reset(&sc->smc_mii_tick_ch, hz, sc->smc_mii_tick, sc);
1286 #ifdef DEVICE_POLLING
1288 ether_poll_register(smc_poll, ifp);
1290 ifp->if_capenable |= IFCAP_POLLING;
1291 ifp->if_capenable |= IFCAP_POLLING_NOCOUNT;
1296 smc_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1298 struct smc_softc *sc;
1306 if ((ifp->if_flags & IFF_UP) == 0 &&
1307 (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1313 if (sc->smc_mii_mediachg != NULL)
1314 sc->smc_mii_mediachg(sc);
1330 if (sc->smc_mii_mediaioctl == NULL) {
1334 sc->smc_mii_mediaioctl(sc, (struct ifreq *)data, cmd);
1338 error = ether_ioctl(ifp, cmd, data);