2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2015-2016 Hiroki Mori.
5 * Copyright (c) 2011-2012 Stefan Bethke.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include "opt_etherswitch.h"
34 #include <sys/param.h>
36 #include <sys/errno.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/socket.h>
43 #include <sys/sockio.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
48 #include <net/if_var.h>
49 #include <net/ethernet.h>
50 #include <net/if_media.h>
51 #include <net/if_types.h>
53 #include <machine/bus.h>
54 #include <dev/iicbus/iic.h>
55 #include <dev/iicbus/iiconf.h>
56 #include <dev/iicbus/iicbus.h>
57 #include <dev/mii/mii.h>
58 #include <dev/mii/miivar.h>
59 #include <dev/mdio/mdio.h>
61 #include <dev/etherswitch/etherswitch.h>
62 #include <dev/etherswitch/rtl8366/rtl8366rbvar.h>
65 #include "iicbus_if.h"
66 #include "miibus_if.h"
67 #include "etherswitch_if.h"
70 struct rtl8366rb_softc {
71 struct mtx sc_mtx; /* serialize access to softc */
72 int smi_acquired; /* serialize access to SMI/I2C bus */
73 struct mtx callout_mtx; /* serialize callout */
75 int vid[RTL8366_NUM_VLANS];
76 char *ifname[RTL8366_NUM_PHYS];
77 device_t miibus[RTL8366_NUM_PHYS];
78 struct ifnet *ifp[RTL8366_NUM_PHYS];
79 struct callout callout_tick;
80 etherswitch_info_t info;
86 #define RTL_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx)
87 #define RTL_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx)
88 #define RTL_LOCK_ASSERT(_sc, _what) mtx_assert(&(_s)c->sc_mtx, (_what))
89 #define RTL_TRYLOCK(_sc) mtx_trylock(&(_sc)->sc_mtx)
94 #define RTL_SMI_ACQUIRED 1
95 #define RTL_SMI_ACQUIRED_ASSERT(_sc) \
96 KASSERT((_sc)->smi_acquired == RTL_SMI_ACQUIRED, ("smi must be acquired @%s", __FUNCTION__))
99 #define DPRINTF(dev, args...) device_printf(dev, args)
100 #define DEVERR(dev, err, fmt, args...) do { \
101 if (err != 0) device_printf(dev, fmt, err, args); \
103 #define DEBUG_INCRVAR(var) do { \
107 static int callout_blocked = 0;
108 static int iic_select_retries = 0;
109 static int phy_access_retries = 0;
110 static SYSCTL_NODE(_debug, OID_AUTO, rtl8366rb, CTLFLAG_RD, 0, "rtl8366rb");
111 SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, callout_blocked, CTLFLAG_RW, &callout_blocked, 0,
112 "number of times the callout couldn't acquire the bus");
113 SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, iic_select_retries, CTLFLAG_RW, &iic_select_retries, 0,
114 "number of times the I2C bus selection had to be retried");
115 SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, phy_access_retries, CTLFLAG_RW, &phy_access_retries, 0,
116 "number of times PHY register access had to be retried");
118 #define DPRINTF(dev, args...)
119 #define DEVERR(dev, err, fmt, args...)
120 #define DEBUG_INCRVAR(var)
123 static int smi_probe(device_t dev);
124 static int smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep);
125 static int smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep);
126 static int smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep);
127 static void rtl8366rb_tick(void *arg);
128 static int rtl8366rb_ifmedia_upd(struct ifnet *);
129 static void rtl8366rb_ifmedia_sts(struct ifnet *, struct ifmediareq *);
132 rtl8366rb_identify(driver_t *driver, device_t parent)
135 struct iicbus_ivar *devi;
137 if (device_find_child(parent, "rtl8366rb", -1) == NULL) {
138 child = BUS_ADD_CHILD(parent, 0, "rtl8366rb", -1);
139 devi = IICBUS_IVAR(child);
140 devi->addr = RTL8366_IIC_ADDR;
145 rtl8366rb_probe(device_t dev)
147 struct rtl8366rb_softc *sc;
149 sc = device_get_softc(dev);
151 bzero(sc, sizeof(*sc));
152 if (smi_probe(dev) != 0)
154 if (sc->chip_type == RTL8366RB)
155 device_set_desc(dev, "RTL8366RB Ethernet Switch Controller");
157 device_set_desc(dev, "RTL8366SR Ethernet Switch Controller");
158 return (BUS_PROBE_DEFAULT);
162 rtl8366rb_init(device_t dev)
164 struct rtl8366rb_softc *sc;
167 sc = device_get_softc(dev);
169 /* Initialisation for TL-WR1043ND */
170 #ifdef RTL8366_SOFT_RESET
171 smi_rmw(dev, RTL8366_RCR,
172 RTL8366_RCR_SOFT_RESET,
173 RTL8366_RCR_SOFT_RESET, RTL_WAITOK);
175 smi_rmw(dev, RTL8366_RCR,
176 RTL8366_RCR_HARD_RESET,
177 RTL8366_RCR_HARD_RESET, RTL_WAITOK);
179 /* hard reset not return ack */
181 /* Enable 16 VLAN mode */
182 smi_rmw(dev, RTL8366_SGCR,
183 RTL8366_SGCR_EN_VLAN | RTL8366_SGCR_EN_VLAN_4KTB,
184 RTL8366_SGCR_EN_VLAN, RTL_WAITOK);
185 /* Initialize our vlan table. */
186 for (i = 0; i <= 1; i++)
187 sc->vid[i] = (i + 1) | ETHERSWITCH_VID_VALID;
188 /* Remove port 0 from VLAN 1. */
189 smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 0),
190 (1 << 0), 0, RTL_WAITOK);
191 /* Add port 0 untagged and port 5 tagged to VLAN 2. */
192 smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 1),
193 ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT)
194 | ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT),
195 ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT
196 | ((1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT)),
198 /* Set PVID 2 for port 0. */
199 smi_rmw(dev, RTL8366_PVCR_REG(0),
200 RTL8366_PVCR_VAL(0, RTL8366_PVCR_PORT_MASK),
201 RTL8366_PVCR_VAL(0, 1), RTL_WAITOK);
205 rtl8366rb_attach(device_t dev)
207 struct rtl8366rb_softc *sc;
213 sc = device_get_softc(dev);
216 mtx_init(&sc->sc_mtx, "rtl8366rb", NULL, MTX_DEF);
217 sc->smi_acquired = 0;
218 mtx_init(&sc->callout_mtx, "rtl8366rbcallout", NULL, MTX_DEF);
221 smi_read(dev, RTL8366_CVCR, &rev, RTL_WAITOK);
222 device_printf(dev, "rev. %d\n", rev & 0x000f);
225 (void) resource_int_value(device_get_name(dev), device_get_unit(dev),
226 "phy4cpu", &sc->phy4cpu);
228 sc->numphys = sc->phy4cpu ? RTL8366_NUM_PHYS - 1 : RTL8366_NUM_PHYS;
230 sc->info.es_nports = sc->numphys + 1;
231 sc->info.es_nvlangroups = RTL8366_NUM_VLANS;
232 sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q;
233 if (sc->chip_type == RTL8366RB)
234 sprintf(sc->info.es_name, "Realtek RTL8366RB");
236 sprintf(sc->info.es_name, "Realtek RTL8366SR");
238 /* attach miibus and phys */
239 /* PHYs need an interface, so we generate a dummy one */
240 for (i = 0; i < sc->numphys; i++) {
241 sc->ifp[i] = if_alloc(IFT_ETHER);
242 if (sc->ifp[i] == NULL) {
243 device_printf(dev, "couldn't allocate ifnet structure\n");
248 sc->ifp[i]->if_softc = sc;
249 sc->ifp[i]->if_flags |= IFF_UP | IFF_BROADCAST | IFF_DRV_RUNNING
251 snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(dev));
252 sc->ifname[i] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK);
253 bcopy(name, sc->ifname[i], strlen(name)+1);
254 if_initname(sc->ifp[i], sc->ifname[i], i);
255 err = mii_attach(dev, &sc->miibus[i], sc->ifp[i], rtl8366rb_ifmedia_upd, \
256 rtl8366rb_ifmedia_sts, BMSR_DEFCAPMASK, \
257 i, MII_OFFSET_ANY, 0);
259 device_printf(dev, "attaching PHY %d failed\n", i);
264 bus_generic_probe(dev);
265 bus_enumerate_hinted_children(dev);
266 err = bus_generic_attach(dev);
270 callout_init_mtx(&sc->callout_tick, &sc->callout_mtx, 0);
277 rtl8366rb_detach(device_t dev)
279 struct rtl8366rb_softc *sc;
282 sc = device_get_softc(dev);
284 for (i=0; i < sc->numphys; i++) {
286 device_delete_child(dev, sc->miibus[i]);
287 if (sc->ifp[i] != NULL)
289 free(sc->ifname[i], M_DEVBUF);
291 bus_generic_detach(dev);
292 callout_drain(&sc->callout_tick);
293 mtx_destroy(&sc->callout_mtx);
294 mtx_destroy(&sc->sc_mtx);
300 rtl8366rb_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active)
302 *media_active = IFM_ETHER;
303 *media_status = IFM_AVALID;
304 if ((portstatus & RTL8366_PLSR_LINK) != 0)
305 *media_status |= IFM_ACTIVE;
307 *media_active |= IFM_NONE;
310 switch (portstatus & RTL8366_PLSR_SPEED_MASK) {
311 case RTL8366_PLSR_SPEED_10:
312 *media_active |= IFM_10_T;
314 case RTL8366_PLSR_SPEED_100:
315 *media_active |= IFM_100_TX;
317 case RTL8366_PLSR_SPEED_1000:
318 *media_active |= IFM_1000_T;
321 if ((portstatus & RTL8366_PLSR_FULLDUPLEX) != 0)
322 *media_active |= IFM_FDX;
324 *media_active |= IFM_HDX;
325 if ((portstatus & RTL8366_PLSR_TXPAUSE) != 0)
326 *media_active |= IFM_ETH_TXPAUSE;
327 if ((portstatus & RTL8366_PLSR_RXPAUSE) != 0)
328 *media_active |= IFM_ETH_RXPAUSE;
332 rtl833rb_miipollstat(struct rtl8366rb_softc *sc)
335 struct mii_data *mii;
336 struct mii_softc *miisc;
340 for (i = 0; i < sc->numphys; i++) {
341 mii = device_get_softc(sc->miibus[i]);
343 if (smi_read(sc->dev, RTL8366_PLSR_BASE + i/2, &value, RTL_NOWAIT) != 0) {
344 DEBUG_INCRVAR(callout_blocked);
347 portstatus = value & 0xff;
349 portstatus = (value >> 8) & 0xff;
351 rtl8366rb_update_ifmedia(portstatus, &mii->mii_media_status, &mii->mii_media_active);
352 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) {
353 if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != miisc->mii_inst)
355 mii_phy_update(miisc, MII_POLLSTAT);
361 rtl8366rb_tick(void *arg)
363 struct rtl8366rb_softc *sc;
367 rtl833rb_miipollstat(sc);
368 callout_reset(&sc->callout_tick, hz, rtl8366rb_tick, sc);
372 smi_probe(device_t dev)
374 struct rtl8366rb_softc *sc;
375 device_t iicbus, iicha;
381 sc = device_get_softc(dev);
383 iicbus = device_get_parent(dev);
384 iicha = device_get_parent(iicbus);
386 for (i = 0; i < 2; ++i) {
387 iicbus_reset(iicbus, IIC_FASTEST, RTL8366_IIC_ADDR, NULL);
391 * we go directly to the host adapter because iicbus.c
392 * only issues a stop on a bus that was successfully started.
395 err = iicbus_request_bus(iicbus, dev, IIC_WAIT);
398 err = iicbus_start(iicbus, RTL8366_IIC_ADDR | RTL_IICBUS_READ, RTL_IICBUS_TIMEOUT);
402 bytes[0] = RTL8366RB_CIR & 0xff;
403 bytes[1] = (RTL8366RB_CIR >> 8) & 0xff;
405 bytes[0] = RTL8366SR_CIR & 0xff;
406 bytes[1] = (RTL8366SR_CIR >> 8) & 0xff;
408 err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT);
411 err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0);
414 chipid = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff);
415 if (i == 0 && chipid == RTL8366RB_CIR_ID8366RB) {
416 DPRINTF(dev, "chip id 0x%04x\n", chipid);
417 sc->chip_type = RTL8366RB;
421 if (i == 1 && chipid == RTL8366SR_CIR_ID8366SR) {
422 DPRINTF(dev, "chip id 0x%04x\n", chipid);
423 sc->chip_type = RTL8366SR;
429 iicbus_release_bus(iicbus, dev);
436 iicbus_release_bus(iicbus, dev);
437 return (err == 0 ? 0 : ENXIO);
441 smi_acquire(struct rtl8366rb_softc *sc, int sleep)
444 if (sleep == RTL_WAITOK)
447 if (RTL_TRYLOCK(sc) == 0)
448 return (EWOULDBLOCK);
449 if (sc->smi_acquired == RTL_SMI_ACQUIRED)
452 r = iicbus_request_bus(device_get_parent(sc->dev), sc->dev, \
453 sleep == RTL_WAITOK ? IIC_WAIT : IIC_DONTWAIT);
455 sc->smi_acquired = RTL_SMI_ACQUIRED;
462 smi_release(struct rtl8366rb_softc *sc, int sleep)
464 if (sleep == RTL_WAITOK)
467 if (RTL_TRYLOCK(sc) == 0)
468 return (EWOULDBLOCK);
469 RTL_SMI_ACQUIRED_ASSERT(sc);
470 iicbus_release_bus(device_get_parent(sc->dev), sc->dev);
471 sc->smi_acquired = 0;
477 smi_select(device_t dev, int op, int sleep)
479 struct rtl8366rb_softc *sc;
482 struct iicbus_ivar *devi;
485 sc = device_get_softc(dev);
487 iicbus = device_get_parent(dev);
488 devi = IICBUS_IVAR(dev);
491 RTL_SMI_ACQUIRED_ASSERT((struct rtl8366rb_softc *)device_get_softc(dev));
493 if (sc->chip_type == RTL8366SR) { // RTL8366SR work around
494 // this is same work around at probe
496 IICBUS_STOP(device_get_parent(device_get_parent(dev)));
499 * The chip does not use clock stretching when it is busy,
500 * instead ignoring the command. Retry a few times.
502 for (i = RTL_IICBUS_RETRIES; i--; ) {
503 err = iicbus_start(iicbus, slave | op, RTL_IICBUS_TIMEOUT);
504 if (err != IIC_ENOACK)
506 if (sleep == RTL_WAITOK) {
507 DEBUG_INCRVAR(iic_select_retries);
508 pause("smi_select", RTL_IICBUS_RETRY_SLEEP);
516 smi_read_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t *data, int sleep)
523 iicbus = device_get_parent(sc->dev);
525 RTL_SMI_ACQUIRED_ASSERT(sc);
526 bytes[0] = addr & 0xff;
527 bytes[1] = (addr >> 8) & 0xff;
528 err = smi_select(sc->dev, RTL_IICBUS_READ, sleep);
531 err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT);
534 err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0);
537 *data = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff);
545 smi_write_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t data, int sleep)
552 iicbus = device_get_parent(sc->dev);
554 RTL_SMI_ACQUIRED_ASSERT(sc);
555 bytes[0] = addr & 0xff;
556 bytes[1] = (addr >> 8) & 0xff;
557 bytes[2] = data & 0xff;
558 bytes[3] = (data >> 8) & 0xff;
560 err = smi_select(sc->dev, RTL_IICBUS_WRITE, sleep);
562 err = iicbus_write(iicbus, bytes, 4, &xferd, RTL_IICBUS_TIMEOUT);
569 smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep)
571 struct rtl8366rb_softc *sc;
574 sc = device_get_softc(dev);
576 err = smi_acquire(sc, sleep);
579 err = smi_read_locked(sc, addr, data, sleep);
580 smi_release(sc, sleep);
581 DEVERR(dev, err, "smi_read()=%d: addr=%04x\n", addr);
582 return (err == 0 ? 0 : EIO);
586 smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep)
588 struct rtl8366rb_softc *sc;
591 sc = device_get_softc(dev);
593 err = smi_acquire(sc, sleep);
596 err = smi_write_locked(sc, addr, data, sleep);
597 smi_release(sc, sleep);
598 DEVERR(dev, err, "smi_write()=%d: addr=%04x\n", addr);
599 return (err == 0 ? 0 : EIO);
603 smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep)
605 struct rtl8366rb_softc *sc;
609 sc = device_get_softc(dev);
611 err = smi_acquire(sc, sleep);
615 err = smi_read_locked(sc, addr, &oldv, sleep);
620 err = smi_write_locked(sc, addr, newv, sleep);
623 smi_release(sc, sleep);
624 DEVERR(dev, err, "smi_rmw()=%d: addr=%04x\n", addr);
625 return (err == 0 ? 0 : EIO);
628 static etherswitch_info_t *
629 rtl_getinfo(device_t dev)
631 struct rtl8366rb_softc *sc;
633 sc = device_get_softc(dev);
639 rtl_readreg(device_t dev, int reg)
645 smi_read(dev, reg, &data, RTL_WAITOK);
650 rtl_writereg(device_t dev, int reg, int value)
652 return (smi_write(dev, reg, value, RTL_WAITOK));
656 rtl_getport(device_t dev, etherswitch_port_t *p)
658 struct rtl8366rb_softc *sc;
660 struct mii_data *mii;
661 struct ifmediareq *ifmr;
665 sc = device_get_softc(dev);
669 if (p->es_port < 0 || p->es_port >= (sc->numphys + 1))
671 if (sc->phy4cpu && p->es_port == sc->numphys) {
672 vlangroup = RTL8366_PVCR_GET(p->es_port + 1,
673 rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port + 1)));
675 vlangroup = RTL8366_PVCR_GET(p->es_port,
676 rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port)));
678 p->es_pvid = sc->vid[vlangroup] & ETHERSWITCH_VID_MASK;
680 if (p->es_port < sc->numphys) {
681 mii = device_get_softc(sc->miibus[p->es_port]);
682 ifm = &mii->mii_media;
683 err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCGIFMEDIA);
687 /* fill in fixed values for CPU port */
688 p->es_flags |= ETHERSWITCH_PORT_CPU;
689 smi_read(dev, RTL8366_PLSR_BASE + (RTL8366_NUM_PHYS)/2, &v, RTL_WAITOK);
690 v = v >> (8 * ((RTL8366_NUM_PHYS) % 2));
691 rtl8366rb_update_ifmedia(v, &ifmr->ifm_status, &ifmr->ifm_active);
692 ifmr->ifm_current = ifmr->ifm_active;
694 ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID;
695 /* Return our static media list. */
696 if (ifmr->ifm_count > 0) {
698 ifmr->ifm_ulist[0] = IFM_MAKEWORD(IFM_ETHER, IFM_1000_T,
707 rtl_setport(device_t dev, etherswitch_port_t *p)
709 struct rtl8366rb_softc *sc;
710 int i, err, vlangroup;
712 struct mii_data *mii;
715 sc = device_get_softc(dev);
717 if (p->es_port < 0 || p->es_port >= (sc->numphys + 1))
720 for (i = 0; i < RTL8366_NUM_VLANS; i++) {
721 if ((sc->vid[i] & ETHERSWITCH_VID_MASK) == p->es_pvid) {
728 if (sc->phy4cpu && p->es_port == sc->numphys) {
729 port = p->es_port + 1;
733 err = smi_rmw(dev, RTL8366_PVCR_REG(port),
734 RTL8366_PVCR_VAL(port, RTL8366_PVCR_PORT_MASK),
735 RTL8366_PVCR_VAL(port, vlangroup), RTL_WAITOK);
739 if (p->es_port == sc->numphys)
741 mii = device_get_softc(sc->miibus[p->es_port]);
742 ifm = &mii->mii_media;
743 err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCSIFMEDIA);
748 rtl_getvgroup(device_t dev, etherswitch_vlangroup_t *vg)
750 struct rtl8366rb_softc *sc;
753 int member, untagged;
755 sc = device_get_softc(dev);
757 for (i=0; i<RTL8366_VMCR_MULT; i++)
758 vmcr[i] = rtl_readreg(dev, RTL8366_VMCR(i, vg->es_vlangroup));
760 vg->es_vid = sc->vid[vg->es_vlangroup];
761 member = RTL8366_VMCR_MEMBER(vmcr);
762 untagged = RTL8366_VMCR_UNTAG(vmcr);
764 vg->es_member_ports = ((member & 0x20) >> 1) | (member & 0x0f);
765 vg->es_untagged_ports = ((untagged & 0x20) >> 1) | (untagged & 0x0f);
767 vg->es_member_ports = member;
768 vg->es_untagged_ports = untagged;
770 vg->es_fid = RTL8366_VMCR_FID(vmcr);
775 rtl_setvgroup(device_t dev, etherswitch_vlangroup_t *vg)
777 struct rtl8366rb_softc *sc;
779 int member, untagged;
781 sc = device_get_softc(dev);
783 g = vg->es_vlangroup;
785 sc->vid[g] = vg->es_vid;
786 /* VLAN group disabled ? */
787 if (vg->es_member_ports == 0 && vg->es_untagged_ports == 0 && vg->es_vid == 0)
789 sc->vid[g] |= ETHERSWITCH_VID_VALID;
790 rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_DOT1Q_REG, g),
791 (vg->es_vid << RTL8366_VMCR_DOT1Q_VID_SHIFT) & RTL8366_VMCR_DOT1Q_VID_MASK);
793 /* add space at phy4 */
794 member = (vg->es_member_ports & 0x0f) |
795 ((vg->es_member_ports & 0x10) << 1);
796 untagged = (vg->es_untagged_ports & 0x0f) |
797 ((vg->es_untagged_ports & 0x10) << 1);
799 member = vg->es_member_ports;
800 untagged = vg->es_untagged_ports;
802 if (sc->chip_type == RTL8366RB) {
803 rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g),
804 ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) |
805 ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK));
806 rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_FID_REG, g),
809 rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g),
810 ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) |
811 ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK) |
812 ((vg->es_fid << RTL8366_VMCR_FID_FID_SHIFT) & RTL8366_VMCR_FID_FID_MASK));
818 rtl_getconf(device_t dev, etherswitch_conf_t *conf)
821 /* Return the VLAN mode. */
822 conf->cmd = ETHERSWITCH_CONF_VLAN_MODE;
823 conf->vlan_mode = ETHERSWITCH_VLAN_DOT1Q;
829 rtl_readphy(device_t dev, int phy, int reg)
831 struct rtl8366rb_softc *sc;
835 sc = device_get_softc(dev);
839 if (phy < 0 || phy >= RTL8366_NUM_PHYS)
841 if (reg < 0 || reg >= RTL8366_NUM_PHY_REG)
844 err = smi_acquire(sc, sleep);
847 for (i = RTL_IICBUS_RETRIES; i--; ) {
848 err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_READ, sleep);
850 err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), 0, sleep);
852 err = smi_read_locked(sc, RTL8366_PADR, &data, sleep);
855 DEBUG_INCRVAR(phy_access_retries);
856 DPRINTF(dev, "rtl_readphy(): chip not responsive, retrying %d more times\n", i);
857 pause("rtl_readphy", RTL_IICBUS_RETRY_SLEEP);
859 smi_release(sc, sleep);
860 DEVERR(dev, err, "rtl_readphy()=%d: phy=%d.%02x\n", phy, reg);
865 rtl_writephy(device_t dev, int phy, int reg, int data)
867 struct rtl8366rb_softc *sc;
870 sc = device_get_softc(dev);
872 if (phy < 0 || phy >= RTL8366_NUM_PHYS)
874 if (reg < 0 || reg >= RTL8366_NUM_PHY_REG)
877 err = smi_acquire(sc, sleep);
880 for (i = RTL_IICBUS_RETRIES; i--; ) {
881 err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_WRITE, sleep);
883 err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), data, sleep);
887 DEBUG_INCRVAR(phy_access_retries);
888 DPRINTF(dev, "rtl_writephy(): chip not responsive, retrying %d more tiems\n", i);
889 pause("rtl_writephy", RTL_IICBUS_RETRY_SLEEP);
891 smi_release(sc, sleep);
892 DEVERR(dev, err, "rtl_writephy()=%d: phy=%d.%02x\n", phy, reg);
893 return (err == 0 ? 0 : EIO);
897 rtl8366rb_ifmedia_upd(struct ifnet *ifp)
899 struct rtl8366rb_softc *sc;
900 struct mii_data *mii;
903 mii = device_get_softc(sc->miibus[ifp->if_dunit]);
910 rtl8366rb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
912 struct rtl8366rb_softc *sc;
913 struct mii_data *mii;
916 mii = device_get_softc(sc->miibus[ifp->if_dunit]);
919 ifmr->ifm_active = mii->mii_media_active;
920 ifmr->ifm_status = mii->mii_media_status;
924 static device_method_t rtl8366rb_methods[] = {
925 /* Device interface */
926 DEVMETHOD(device_identify, rtl8366rb_identify),
927 DEVMETHOD(device_probe, rtl8366rb_probe),
928 DEVMETHOD(device_attach, rtl8366rb_attach),
929 DEVMETHOD(device_detach, rtl8366rb_detach),
932 DEVMETHOD(bus_add_child, device_add_child_ordered),
935 DEVMETHOD(miibus_readreg, rtl_readphy),
936 DEVMETHOD(miibus_writereg, rtl_writephy),
939 DEVMETHOD(mdio_readreg, rtl_readphy),
940 DEVMETHOD(mdio_writereg, rtl_writephy),
942 /* etherswitch interface */
943 DEVMETHOD(etherswitch_getconf, rtl_getconf),
944 DEVMETHOD(etherswitch_getinfo, rtl_getinfo),
945 DEVMETHOD(etherswitch_readreg, rtl_readreg),
946 DEVMETHOD(etherswitch_writereg, rtl_writereg),
947 DEVMETHOD(etherswitch_readphyreg, rtl_readphy),
948 DEVMETHOD(etherswitch_writephyreg, rtl_writephy),
949 DEVMETHOD(etherswitch_getport, rtl_getport),
950 DEVMETHOD(etherswitch_setport, rtl_setport),
951 DEVMETHOD(etherswitch_getvgroup, rtl_getvgroup),
952 DEVMETHOD(etherswitch_setvgroup, rtl_setvgroup),
957 DEFINE_CLASS_0(rtl8366rb, rtl8366rb_driver, rtl8366rb_methods,
958 sizeof(struct rtl8366rb_softc));
959 static devclass_t rtl8366rb_devclass;
961 DRIVER_MODULE(rtl8366rb, iicbus, rtl8366rb_driver, rtl8366rb_devclass, 0, 0);
962 DRIVER_MODULE(miibus, rtl8366rb, miibus_driver, miibus_devclass, 0, 0);
963 DRIVER_MODULE(mdio, rtl8366rb, mdio_driver, mdio_devclass, 0, 0);
964 DRIVER_MODULE(etherswitch, rtl8366rb, etherswitch_driver, etherswitch_devclass, 0, 0);
965 MODULE_VERSION(rtl8366rb, 1);
966 MODULE_DEPEND(rtl8366rb, iicbus, 1, 1, 1); /* XXX which versions? */
967 MODULE_DEPEND(rtl8366rb, miibus, 1, 1, 1); /* XXX which versions? */
968 MODULE_DEPEND(rtl8366rb, etherswitch, 1, 1, 1); /* XXX which versions? */