1a043e8c7SAdrian Chadd /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3718cf2ccSPedro F. Giffuni * 4477e3effSMichael Zhilin * Copyright (c) 2015-2016 Hiroki Mori. 5a043e8c7SAdrian Chadd * Copyright (c) 2011-2012 Stefan Bethke. 6a043e8c7SAdrian Chadd * All rights reserved. 7a043e8c7SAdrian Chadd * 8a043e8c7SAdrian Chadd * Redistribution and use in source and binary forms, with or without 9a043e8c7SAdrian Chadd * modification, are permitted provided that the following conditions 10a043e8c7SAdrian Chadd * are met: 11a043e8c7SAdrian Chadd * 1. Redistributions of source code must retain the above copyright 12a043e8c7SAdrian Chadd * notice, this list of conditions and the following disclaimer. 13a043e8c7SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright 14a043e8c7SAdrian Chadd * notice, this list of conditions and the following disclaimer in the 15a043e8c7SAdrian Chadd * documentation and/or other materials provided with the distribution. 16a043e8c7SAdrian Chadd * 17a043e8c7SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18a043e8c7SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19a043e8c7SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20a043e8c7SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21a043e8c7SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22a043e8c7SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23a043e8c7SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24a043e8c7SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25a043e8c7SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26a043e8c7SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27a043e8c7SAdrian Chadd * SUCH DAMAGE. 28a043e8c7SAdrian Chadd */ 29a043e8c7SAdrian Chadd 30477e3effSMichael Zhilin #include "opt_etherswitch.h" 31477e3effSMichael Zhilin 32a043e8c7SAdrian Chadd #include <sys/param.h> 33a043e8c7SAdrian Chadd #include <sys/bus.h> 34a043e8c7SAdrian Chadd #include <sys/errno.h> 35a043e8c7SAdrian Chadd #include <sys/kernel.h> 363d02237cSLuiz Otavio O Souza #include <sys/lock.h> 373d02237cSLuiz Otavio O Souza #include <sys/malloc.h> 38a043e8c7SAdrian Chadd #include <sys/module.h> 393d02237cSLuiz Otavio O Souza #include <sys/mutex.h> 40a043e8c7SAdrian Chadd #include <sys/socket.h> 41a043e8c7SAdrian Chadd #include <sys/sockio.h> 42a043e8c7SAdrian Chadd #include <sys/sysctl.h> 43a043e8c7SAdrian Chadd #include <sys/systm.h> 44a043e8c7SAdrian Chadd 45a043e8c7SAdrian Chadd #include <net/if.h> 463d02237cSLuiz Otavio O Souza #include <net/if_var.h> 47a043e8c7SAdrian Chadd #include <net/ethernet.h> 48a043e8c7SAdrian Chadd #include <net/if_media.h> 49a043e8c7SAdrian Chadd #include <net/if_types.h> 50a043e8c7SAdrian Chadd 51a043e8c7SAdrian Chadd #include <machine/bus.h> 52efce3748SRui Paulo #include <dev/iicbus/iic.h> 53a043e8c7SAdrian Chadd #include <dev/iicbus/iiconf.h> 54a043e8c7SAdrian Chadd #include <dev/iicbus/iicbus.h> 55a043e8c7SAdrian Chadd #include <dev/mii/mii.h> 56a043e8c7SAdrian Chadd #include <dev/mii/miivar.h> 575a4380b5SMichael Zhilin #include <dev/mdio/mdio.h> 58a043e8c7SAdrian Chadd 59a043e8c7SAdrian Chadd #include <dev/etherswitch/etherswitch.h> 60a043e8c7SAdrian Chadd #include <dev/etherswitch/rtl8366/rtl8366rbvar.h> 61a043e8c7SAdrian Chadd 625a4380b5SMichael Zhilin #include "mdio_if.h" 63a043e8c7SAdrian Chadd #include "iicbus_if.h" 64a043e8c7SAdrian Chadd #include "miibus_if.h" 65a043e8c7SAdrian Chadd #include "etherswitch_if.h" 66a043e8c7SAdrian Chadd 67a043e8c7SAdrian Chadd 68a043e8c7SAdrian Chadd struct rtl8366rb_softc { 69a043e8c7SAdrian Chadd struct mtx sc_mtx; /* serialize access to softc */ 70a043e8c7SAdrian Chadd int smi_acquired; /* serialize access to SMI/I2C bus */ 71a043e8c7SAdrian Chadd struct mtx callout_mtx; /* serialize callout */ 72a043e8c7SAdrian Chadd device_t dev; 73477e3effSMichael Zhilin int vid[RTL8366_NUM_VLANS]; 74477e3effSMichael Zhilin char *ifname[RTL8366_NUM_PHYS]; 75477e3effSMichael Zhilin device_t miibus[RTL8366_NUM_PHYS]; 762e6a8c1aSJustin Hibbits if_t ifp[RTL8366_NUM_PHYS]; 77a043e8c7SAdrian Chadd struct callout callout_tick; 78477e3effSMichael Zhilin etherswitch_info_t info; 795a4380b5SMichael Zhilin int chip_type; 805a4380b5SMichael Zhilin int phy4cpu; 815a4380b5SMichael Zhilin int numphys; 82a043e8c7SAdrian Chadd }; 83a043e8c7SAdrian Chadd 84a043e8c7SAdrian Chadd #define RTL_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 85a043e8c7SAdrian Chadd #define RTL_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 86a043e8c7SAdrian Chadd #define RTL_LOCK_ASSERT(_sc, _what) mtx_assert(&(_s)c->sc_mtx, (_what)) 87a043e8c7SAdrian Chadd #define RTL_TRYLOCK(_sc) mtx_trylock(&(_sc)->sc_mtx) 88a043e8c7SAdrian Chadd 89a043e8c7SAdrian Chadd #define RTL_WAITOK 0 90a043e8c7SAdrian Chadd #define RTL_NOWAIT 1 91a043e8c7SAdrian Chadd 92a043e8c7SAdrian Chadd #define RTL_SMI_ACQUIRED 1 93a043e8c7SAdrian Chadd #define RTL_SMI_ACQUIRED_ASSERT(_sc) \ 94a043e8c7SAdrian Chadd KASSERT((_sc)->smi_acquired == RTL_SMI_ACQUIRED, ("smi must be acquired @%s", __FUNCTION__)) 95a043e8c7SAdrian Chadd 96a043e8c7SAdrian Chadd #if defined(DEBUG) 97a043e8c7SAdrian Chadd #define DPRINTF(dev, args...) device_printf(dev, args) 98a043e8c7SAdrian Chadd #define DEVERR(dev, err, fmt, args...) do { \ 99a043e8c7SAdrian Chadd if (err != 0) device_printf(dev, fmt, err, args); \ 100a043e8c7SAdrian Chadd } while (0) 101a043e8c7SAdrian Chadd #define DEBUG_INCRVAR(var) do { \ 102a043e8c7SAdrian Chadd var++; \ 103a043e8c7SAdrian Chadd } while (0) 104a043e8c7SAdrian Chadd 105a043e8c7SAdrian Chadd static int callout_blocked = 0; 106a043e8c7SAdrian Chadd static int iic_select_retries = 0; 107a043e8c7SAdrian Chadd static int phy_access_retries = 0; 1087029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, rtl8366rb, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1097029da5cSPawel Biernacki "rtl8366rb"); 110a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, callout_blocked, CTLFLAG_RW, &callout_blocked, 0, 111a043e8c7SAdrian Chadd "number of times the callout couldn't acquire the bus"); 112a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, iic_select_retries, CTLFLAG_RW, &iic_select_retries, 0, 113a043e8c7SAdrian Chadd "number of times the I2C bus selection had to be retried"); 114a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, phy_access_retries, CTLFLAG_RW, &phy_access_retries, 0, 115a043e8c7SAdrian Chadd "number of times PHY register access had to be retried"); 116a043e8c7SAdrian Chadd #else 117a043e8c7SAdrian Chadd #define DPRINTF(dev, args...) 118a043e8c7SAdrian Chadd #define DEVERR(dev, err, fmt, args...) 119a043e8c7SAdrian Chadd #define DEBUG_INCRVAR(var) 120a043e8c7SAdrian Chadd #endif 121a043e8c7SAdrian Chadd 122a043e8c7SAdrian Chadd static int smi_probe(device_t dev); 123a043e8c7SAdrian Chadd static int smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep); 124a043e8c7SAdrian Chadd static int smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep); 125a043e8c7SAdrian Chadd static int smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep); 126a043e8c7SAdrian Chadd static void rtl8366rb_tick(void *arg); 1272e6a8c1aSJustin Hibbits static int rtl8366rb_ifmedia_upd(if_t); 1282e6a8c1aSJustin Hibbits static void rtl8366rb_ifmedia_sts(if_t, struct ifmediareq *); 129a043e8c7SAdrian Chadd 130a043e8c7SAdrian Chadd static void 131a043e8c7SAdrian Chadd rtl8366rb_identify(driver_t *driver, device_t parent) 132a043e8c7SAdrian Chadd { 133a043e8c7SAdrian Chadd device_t child; 134a043e8c7SAdrian Chadd struct iicbus_ivar *devi; 135a043e8c7SAdrian Chadd 136a043e8c7SAdrian Chadd if (device_find_child(parent, "rtl8366rb", -1) == NULL) { 137*a05a6804SWarner Losh child = BUS_ADD_CHILD(parent, 0, "rtl8366rb", DEVICE_UNIT_ANY); 138a043e8c7SAdrian Chadd devi = IICBUS_IVAR(child); 139477e3effSMichael Zhilin devi->addr = RTL8366_IIC_ADDR; 140a043e8c7SAdrian Chadd } 141a043e8c7SAdrian Chadd } 142a043e8c7SAdrian Chadd 143a043e8c7SAdrian Chadd static int 144a043e8c7SAdrian Chadd rtl8366rb_probe(device_t dev) 145a043e8c7SAdrian Chadd { 146477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 147477e3effSMichael Zhilin 148477e3effSMichael Zhilin sc = device_get_softc(dev); 149477e3effSMichael Zhilin 150477e3effSMichael Zhilin bzero(sc, sizeof(*sc)); 151a043e8c7SAdrian Chadd if (smi_probe(dev) != 0) 152a043e8c7SAdrian Chadd return (ENXIO); 1535a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) 154a043e8c7SAdrian Chadd device_set_desc(dev, "RTL8366RB Ethernet Switch Controller"); 155477e3effSMichael Zhilin else 156477e3effSMichael Zhilin device_set_desc(dev, "RTL8366SR Ethernet Switch Controller"); 157a043e8c7SAdrian Chadd return (BUS_PROBE_DEFAULT); 158a043e8c7SAdrian Chadd } 159a043e8c7SAdrian Chadd 160a043e8c7SAdrian Chadd static void 161a043e8c7SAdrian Chadd rtl8366rb_init(device_t dev) 162a043e8c7SAdrian Chadd { 163bfae9329SLuiz Otavio O Souza struct rtl8366rb_softc *sc; 164477e3effSMichael Zhilin int i; 165477e3effSMichael Zhilin 166477e3effSMichael Zhilin sc = device_get_softc(dev); 167bfae9329SLuiz Otavio O Souza 168a043e8c7SAdrian Chadd /* Initialisation for TL-WR1043ND */ 169477e3effSMichael Zhilin #ifdef RTL8366_SOFT_RESET 170477e3effSMichael Zhilin smi_rmw(dev, RTL8366_RCR, 171477e3effSMichael Zhilin RTL8366_RCR_SOFT_RESET, 172477e3effSMichael Zhilin RTL8366_RCR_SOFT_RESET, RTL_WAITOK); 173477e3effSMichael Zhilin #else 174477e3effSMichael Zhilin smi_rmw(dev, RTL8366_RCR, 175477e3effSMichael Zhilin RTL8366_RCR_HARD_RESET, 176477e3effSMichael Zhilin RTL8366_RCR_HARD_RESET, RTL_WAITOK); 177477e3effSMichael Zhilin #endif 178477e3effSMichael Zhilin /* hard reset not return ack */ 179a043e8c7SAdrian Chadd DELAY(100000); 180a043e8c7SAdrian Chadd /* Enable 16 VLAN mode */ 181477e3effSMichael Zhilin smi_rmw(dev, RTL8366_SGCR, 182477e3effSMichael Zhilin RTL8366_SGCR_EN_VLAN | RTL8366_SGCR_EN_VLAN_4KTB, 183477e3effSMichael Zhilin RTL8366_SGCR_EN_VLAN, RTL_WAITOK); 184bfae9329SLuiz Otavio O Souza /* Initialize our vlan table. */ 185bfae9329SLuiz Otavio O Souza for (i = 0; i <= 1; i++) 186bfae9329SLuiz Otavio O Souza sc->vid[i] = (i + 1) | ETHERSWITCH_VID_VALID; 187bfae9329SLuiz Otavio O Souza /* Remove port 0 from VLAN 1. */ 188477e3effSMichael Zhilin smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 0), 189a043e8c7SAdrian Chadd (1 << 0), 0, RTL_WAITOK); 190bfae9329SLuiz Otavio O Souza /* Add port 0 untagged and port 5 tagged to VLAN 2. */ 191477e3effSMichael Zhilin smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 1), 192477e3effSMichael Zhilin ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT) 193477e3effSMichael Zhilin | ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT), 194477e3effSMichael Zhilin ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT 195477e3effSMichael Zhilin | ((1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT)), 196a043e8c7SAdrian Chadd RTL_WAITOK); 197bfae9329SLuiz Otavio O Souza /* Set PVID 2 for port 0. */ 198477e3effSMichael Zhilin smi_rmw(dev, RTL8366_PVCR_REG(0), 199477e3effSMichael Zhilin RTL8366_PVCR_VAL(0, RTL8366_PVCR_PORT_MASK), 200477e3effSMichael Zhilin RTL8366_PVCR_VAL(0, 1), RTL_WAITOK); 201a043e8c7SAdrian Chadd } 202a043e8c7SAdrian Chadd 203a043e8c7SAdrian Chadd static int 204a043e8c7SAdrian Chadd rtl8366rb_attach(device_t dev) 205a043e8c7SAdrian Chadd { 206a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 207477e3effSMichael Zhilin uint16_t rev = 0; 208a043e8c7SAdrian Chadd char name[IFNAMSIZ]; 209a043e8c7SAdrian Chadd int err = 0; 210a043e8c7SAdrian Chadd int i; 211a043e8c7SAdrian Chadd 212a043e8c7SAdrian Chadd sc = device_get_softc(dev); 213477e3effSMichael Zhilin 214a043e8c7SAdrian Chadd sc->dev = dev; 215a043e8c7SAdrian Chadd mtx_init(&sc->sc_mtx, "rtl8366rb", NULL, MTX_DEF); 216a043e8c7SAdrian Chadd sc->smi_acquired = 0; 217a043e8c7SAdrian Chadd mtx_init(&sc->callout_mtx, "rtl8366rbcallout", NULL, MTX_DEF); 218a043e8c7SAdrian Chadd 219a043e8c7SAdrian Chadd rtl8366rb_init(dev); 220477e3effSMichael Zhilin smi_read(dev, RTL8366_CVCR, &rev, RTL_WAITOK); 221a043e8c7SAdrian Chadd device_printf(dev, "rev. %d\n", rev & 0x000f); 222a043e8c7SAdrian Chadd 2235a4380b5SMichael Zhilin sc->phy4cpu = 0; 2245a4380b5SMichael Zhilin (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 2255a4380b5SMichael Zhilin "phy4cpu", &sc->phy4cpu); 2265a4380b5SMichael Zhilin 2275a4380b5SMichael Zhilin sc->numphys = sc->phy4cpu ? RTL8366_NUM_PHYS - 1 : RTL8366_NUM_PHYS; 2285a4380b5SMichael Zhilin 2295a4380b5SMichael Zhilin sc->info.es_nports = sc->numphys + 1; 230477e3effSMichael Zhilin sc->info.es_nvlangroups = RTL8366_NUM_VLANS; 231477e3effSMichael Zhilin sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q; 2325a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) 233477e3effSMichael Zhilin sprintf(sc->info.es_name, "Realtek RTL8366RB"); 234477e3effSMichael Zhilin else 235477e3effSMichael Zhilin sprintf(sc->info.es_name, "Realtek RTL8366SR"); 236477e3effSMichael Zhilin 237a043e8c7SAdrian Chadd /* attach miibus and phys */ 238a043e8c7SAdrian Chadd /* PHYs need an interface, so we generate a dummy one */ 2395a4380b5SMichael Zhilin for (i = 0; i < sc->numphys; i++) { 240a043e8c7SAdrian Chadd sc->ifp[i] = if_alloc(IFT_ETHER); 2412e6a8c1aSJustin Hibbits if_setsoftc(sc->ifp[i], sc); 2422e6a8c1aSJustin Hibbits if_setflagbits(sc->ifp[i], IFF_UP | IFF_BROADCAST | IFF_DRV_RUNNING 2432e6a8c1aSJustin Hibbits | IFF_SIMPLEX, 0); 244a043e8c7SAdrian Chadd snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(dev)); 245a043e8c7SAdrian Chadd sc->ifname[i] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK); 246a043e8c7SAdrian Chadd bcopy(name, sc->ifname[i], strlen(name)+1); 247a043e8c7SAdrian Chadd if_initname(sc->ifp[i], sc->ifname[i], i); 248a043e8c7SAdrian Chadd err = mii_attach(dev, &sc->miibus[i], sc->ifp[i], rtl8366rb_ifmedia_upd, \ 249a043e8c7SAdrian Chadd rtl8366rb_ifmedia_sts, BMSR_DEFCAPMASK, \ 250a043e8c7SAdrian Chadd i, MII_OFFSET_ANY, 0); 251a043e8c7SAdrian Chadd if (err != 0) { 252a043e8c7SAdrian Chadd device_printf(dev, "attaching PHY %d failed\n", i); 253a043e8c7SAdrian Chadd return (err); 254a043e8c7SAdrian Chadd } 255a043e8c7SAdrian Chadd } 256a043e8c7SAdrian Chadd 257a043e8c7SAdrian Chadd bus_generic_probe(dev); 258a043e8c7SAdrian Chadd bus_enumerate_hinted_children(dev); 259a043e8c7SAdrian Chadd err = bus_generic_attach(dev); 260a043e8c7SAdrian Chadd if (err != 0) 261a043e8c7SAdrian Chadd return (err); 262a043e8c7SAdrian Chadd 263a043e8c7SAdrian Chadd callout_init_mtx(&sc->callout_tick, &sc->callout_mtx, 0); 264a043e8c7SAdrian Chadd rtl8366rb_tick(sc); 265a043e8c7SAdrian Chadd 266a043e8c7SAdrian Chadd return (err); 267a043e8c7SAdrian Chadd } 268a043e8c7SAdrian Chadd 269a043e8c7SAdrian Chadd static int 270a043e8c7SAdrian Chadd rtl8366rb_detach(device_t dev) 271a043e8c7SAdrian Chadd { 272477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 273a043e8c7SAdrian Chadd int i; 274a043e8c7SAdrian Chadd 275477e3effSMichael Zhilin sc = device_get_softc(dev); 276477e3effSMichael Zhilin 2775a4380b5SMichael Zhilin for (i=0; i < sc->numphys; i++) { 278a043e8c7SAdrian Chadd if (sc->miibus[i]) 279a043e8c7SAdrian Chadd device_delete_child(dev, sc->miibus[i]); 280a043e8c7SAdrian Chadd if (sc->ifp[i] != NULL) 281a043e8c7SAdrian Chadd if_free(sc->ifp[i]); 282a043e8c7SAdrian Chadd free(sc->ifname[i], M_DEVBUF); 283a043e8c7SAdrian Chadd } 284a043e8c7SAdrian Chadd bus_generic_detach(dev); 285a043e8c7SAdrian Chadd callout_drain(&sc->callout_tick); 286a043e8c7SAdrian Chadd mtx_destroy(&sc->callout_mtx); 287a043e8c7SAdrian Chadd mtx_destroy(&sc->sc_mtx); 288a043e8c7SAdrian Chadd 289a043e8c7SAdrian Chadd return (0); 290a043e8c7SAdrian Chadd } 291a043e8c7SAdrian Chadd 292a043e8c7SAdrian Chadd static void 293a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active) 294a043e8c7SAdrian Chadd { 295a043e8c7SAdrian Chadd *media_active = IFM_ETHER; 296a043e8c7SAdrian Chadd *media_status = IFM_AVALID; 297477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_LINK) != 0) 298a043e8c7SAdrian Chadd *media_status |= IFM_ACTIVE; 299a043e8c7SAdrian Chadd else { 300a043e8c7SAdrian Chadd *media_active |= IFM_NONE; 301a043e8c7SAdrian Chadd return; 302a043e8c7SAdrian Chadd } 303477e3effSMichael Zhilin switch (portstatus & RTL8366_PLSR_SPEED_MASK) { 304477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_10: 305a043e8c7SAdrian Chadd *media_active |= IFM_10_T; 306a043e8c7SAdrian Chadd break; 307477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_100: 308a043e8c7SAdrian Chadd *media_active |= IFM_100_TX; 309a043e8c7SAdrian Chadd break; 310477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_1000: 311a043e8c7SAdrian Chadd *media_active |= IFM_1000_T; 312a043e8c7SAdrian Chadd break; 313a043e8c7SAdrian Chadd } 314477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_FULLDUPLEX) != 0) 315a043e8c7SAdrian Chadd *media_active |= IFM_FDX; 316a043e8c7SAdrian Chadd else 317a043e8c7SAdrian Chadd *media_active |= IFM_HDX; 318477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_TXPAUSE) != 0) 319a043e8c7SAdrian Chadd *media_active |= IFM_ETH_TXPAUSE; 320477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_RXPAUSE) != 0) 321a043e8c7SAdrian Chadd *media_active |= IFM_ETH_RXPAUSE; 322a043e8c7SAdrian Chadd } 323a043e8c7SAdrian Chadd 324a043e8c7SAdrian Chadd static void 325a043e8c7SAdrian Chadd rtl833rb_miipollstat(struct rtl8366rb_softc *sc) 326a043e8c7SAdrian Chadd { 327a043e8c7SAdrian Chadd int i; 328a043e8c7SAdrian Chadd struct mii_data *mii; 329a043e8c7SAdrian Chadd struct mii_softc *miisc; 330a043e8c7SAdrian Chadd uint16_t value; 331a043e8c7SAdrian Chadd int portstatus; 332a043e8c7SAdrian Chadd 3335a4380b5SMichael Zhilin for (i = 0; i < sc->numphys; i++) { 334a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[i]); 335a043e8c7SAdrian Chadd if ((i % 2) == 0) { 336477e3effSMichael Zhilin if (smi_read(sc->dev, RTL8366_PLSR_BASE + i/2, &value, RTL_NOWAIT) != 0) { 337a043e8c7SAdrian Chadd DEBUG_INCRVAR(callout_blocked); 338a043e8c7SAdrian Chadd return; 339a043e8c7SAdrian Chadd } 340a043e8c7SAdrian Chadd portstatus = value & 0xff; 341a043e8c7SAdrian Chadd } else { 342a043e8c7SAdrian Chadd portstatus = (value >> 8) & 0xff; 343a043e8c7SAdrian Chadd } 344a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(portstatus, &mii->mii_media_status, &mii->mii_media_active); 345a043e8c7SAdrian Chadd LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 346a043e8c7SAdrian Chadd if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != miisc->mii_inst) 347a043e8c7SAdrian Chadd continue; 348a043e8c7SAdrian Chadd mii_phy_update(miisc, MII_POLLSTAT); 349a043e8c7SAdrian Chadd } 350a043e8c7SAdrian Chadd } 351a043e8c7SAdrian Chadd } 352a043e8c7SAdrian Chadd 353a043e8c7SAdrian Chadd static void 354a043e8c7SAdrian Chadd rtl8366rb_tick(void *arg) 355a043e8c7SAdrian Chadd { 356477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 357477e3effSMichael Zhilin 358477e3effSMichael Zhilin sc = arg; 359a043e8c7SAdrian Chadd 360a043e8c7SAdrian Chadd rtl833rb_miipollstat(sc); 361a043e8c7SAdrian Chadd callout_reset(&sc->callout_tick, hz, rtl8366rb_tick, sc); 362a043e8c7SAdrian Chadd } 363a043e8c7SAdrian Chadd 364a043e8c7SAdrian Chadd static int 365a043e8c7SAdrian Chadd smi_probe(device_t dev) 366a043e8c7SAdrian Chadd { 367477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 368a043e8c7SAdrian Chadd device_t iicbus, iicha; 369477e3effSMichael Zhilin int err, i, j; 370a043e8c7SAdrian Chadd uint16_t chipid; 371a043e8c7SAdrian Chadd char bytes[2]; 372a043e8c7SAdrian Chadd int xferd; 373a043e8c7SAdrian Chadd 374477e3effSMichael Zhilin sc = device_get_softc(dev); 375477e3effSMichael Zhilin 376a043e8c7SAdrian Chadd iicbus = device_get_parent(dev); 377a043e8c7SAdrian Chadd iicha = device_get_parent(iicbus); 378477e3effSMichael Zhilin 379477e3effSMichael Zhilin for (i = 0; i < 2; ++i) { 380477e3effSMichael Zhilin iicbus_reset(iicbus, IIC_FASTEST, RTL8366_IIC_ADDR, NULL); 381477e3effSMichael Zhilin for (j=3; j--; ) { 382a043e8c7SAdrian Chadd IICBUS_STOP(iicha); 383a043e8c7SAdrian Chadd /* 384a043e8c7SAdrian Chadd * we go directly to the host adapter because iicbus.c 385a043e8c7SAdrian Chadd * only issues a stop on a bus that was successfully started. 386a043e8c7SAdrian Chadd */ 387a043e8c7SAdrian Chadd } 388a043e8c7SAdrian Chadd err = iicbus_request_bus(iicbus, dev, IIC_WAIT); 389a043e8c7SAdrian Chadd if (err != 0) 390a043e8c7SAdrian Chadd goto out; 391477e3effSMichael Zhilin err = iicbus_start(iicbus, RTL8366_IIC_ADDR | RTL_IICBUS_READ, RTL_IICBUS_TIMEOUT); 392a043e8c7SAdrian Chadd if (err != 0) 393a043e8c7SAdrian Chadd goto out; 394477e3effSMichael Zhilin if (i == 0) { 395477e3effSMichael Zhilin bytes[0] = RTL8366RB_CIR & 0xff; 396477e3effSMichael Zhilin bytes[1] = (RTL8366RB_CIR >> 8) & 0xff; 397477e3effSMichael Zhilin } else { 398477e3effSMichael Zhilin bytes[0] = RTL8366SR_CIR & 0xff; 399477e3effSMichael Zhilin bytes[1] = (RTL8366SR_CIR >> 8) & 0xff; 400477e3effSMichael Zhilin } 401a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT); 402a043e8c7SAdrian Chadd if (err != 0) 403a043e8c7SAdrian Chadd goto out; 404a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0); 405a043e8c7SAdrian Chadd if (err != 0) 406a043e8c7SAdrian Chadd goto out; 407a043e8c7SAdrian Chadd chipid = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff); 408477e3effSMichael Zhilin if (i == 0 && chipid == RTL8366RB_CIR_ID8366RB) { 409a043e8c7SAdrian Chadd DPRINTF(dev, "chip id 0x%04x\n", chipid); 4105a4380b5SMichael Zhilin sc->chip_type = RTL8366RB; 411477e3effSMichael Zhilin err = 0; 412477e3effSMichael Zhilin break; 413477e3effSMichael Zhilin } 414477e3effSMichael Zhilin if (i == 1 && chipid == RTL8366SR_CIR_ID8366SR) { 415477e3effSMichael Zhilin DPRINTF(dev, "chip id 0x%04x\n", chipid); 4165a4380b5SMichael Zhilin sc->chip_type = RTL8366SR; 417477e3effSMichael Zhilin err = 0; 418477e3effSMichael Zhilin break; 419477e3effSMichael Zhilin } 420477e3effSMichael Zhilin if (i == 0) { 421477e3effSMichael Zhilin iicbus_stop(iicbus); 422477e3effSMichael Zhilin iicbus_release_bus(iicbus, dev); 423477e3effSMichael Zhilin } 424477e3effSMichael Zhilin } 425477e3effSMichael Zhilin if (i == 2) 426a043e8c7SAdrian Chadd err = ENXIO; 427a043e8c7SAdrian Chadd out: 428a043e8c7SAdrian Chadd iicbus_stop(iicbus); 429a043e8c7SAdrian Chadd iicbus_release_bus(iicbus, dev); 430a043e8c7SAdrian Chadd return (err == 0 ? 0 : ENXIO); 431a043e8c7SAdrian Chadd } 432a043e8c7SAdrian Chadd 433a043e8c7SAdrian Chadd static int 434a043e8c7SAdrian Chadd smi_acquire(struct rtl8366rb_softc *sc, int sleep) 435a043e8c7SAdrian Chadd { 436a043e8c7SAdrian Chadd int r = 0; 437a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) 438a043e8c7SAdrian Chadd RTL_LOCK(sc); 439a043e8c7SAdrian Chadd else 440a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0) 441a043e8c7SAdrian Chadd return (EWOULDBLOCK); 442a043e8c7SAdrian Chadd if (sc->smi_acquired == RTL_SMI_ACQUIRED) 443a043e8c7SAdrian Chadd r = EBUSY; 444a043e8c7SAdrian Chadd else { 445a043e8c7SAdrian Chadd r = iicbus_request_bus(device_get_parent(sc->dev), sc->dev, \ 446a043e8c7SAdrian Chadd sleep == RTL_WAITOK ? IIC_WAIT : IIC_DONTWAIT); 447a043e8c7SAdrian Chadd if (r == 0) 448a043e8c7SAdrian Chadd sc->smi_acquired = RTL_SMI_ACQUIRED; 449a043e8c7SAdrian Chadd } 450a043e8c7SAdrian Chadd RTL_UNLOCK(sc); 451a043e8c7SAdrian Chadd return (r); 452a043e8c7SAdrian Chadd } 453a043e8c7SAdrian Chadd 454a043e8c7SAdrian Chadd static int 455a043e8c7SAdrian Chadd smi_release(struct rtl8366rb_softc *sc, int sleep) 456a043e8c7SAdrian Chadd { 457a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) 458a043e8c7SAdrian Chadd RTL_LOCK(sc); 459a043e8c7SAdrian Chadd else 460a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0) 461a043e8c7SAdrian Chadd return (EWOULDBLOCK); 462a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 463a043e8c7SAdrian Chadd iicbus_release_bus(device_get_parent(sc->dev), sc->dev); 464a043e8c7SAdrian Chadd sc->smi_acquired = 0; 465a043e8c7SAdrian Chadd RTL_UNLOCK(sc); 466a043e8c7SAdrian Chadd return (0); 467a043e8c7SAdrian Chadd } 468a043e8c7SAdrian Chadd 469a043e8c7SAdrian Chadd static int 470a043e8c7SAdrian Chadd smi_select(device_t dev, int op, int sleep) 471a043e8c7SAdrian Chadd { 472477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 473a043e8c7SAdrian Chadd int err, i; 474477e3effSMichael Zhilin device_t iicbus; 475477e3effSMichael Zhilin struct iicbus_ivar *devi; 476477e3effSMichael Zhilin int slave; 477477e3effSMichael Zhilin 478477e3effSMichael Zhilin sc = device_get_softc(dev); 479477e3effSMichael Zhilin 480477e3effSMichael Zhilin iicbus = device_get_parent(dev); 481477e3effSMichael Zhilin devi = IICBUS_IVAR(dev); 482477e3effSMichael Zhilin slave = devi->addr; 483a043e8c7SAdrian Chadd 484a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT((struct rtl8366rb_softc *)device_get_softc(dev)); 485477e3effSMichael Zhilin 4865a4380b5SMichael Zhilin if (sc->chip_type == RTL8366SR) { // RTL8366SR work around 487477e3effSMichael Zhilin // this is same work around at probe 488477e3effSMichael Zhilin for (int i=3; i--; ) 489477e3effSMichael Zhilin IICBUS_STOP(device_get_parent(device_get_parent(dev))); 490477e3effSMichael Zhilin } 491a043e8c7SAdrian Chadd /* 492a043e8c7SAdrian Chadd * The chip does not use clock stretching when it is busy, 493a043e8c7SAdrian Chadd * instead ignoring the command. Retry a few times. 494a043e8c7SAdrian Chadd */ 495a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 496a043e8c7SAdrian Chadd err = iicbus_start(iicbus, slave | op, RTL_IICBUS_TIMEOUT); 497a043e8c7SAdrian Chadd if (err != IIC_ENOACK) 498a043e8c7SAdrian Chadd break; 499a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) { 500a043e8c7SAdrian Chadd DEBUG_INCRVAR(iic_select_retries); 501a043e8c7SAdrian Chadd pause("smi_select", RTL_IICBUS_RETRY_SLEEP); 502a043e8c7SAdrian Chadd } else 503a043e8c7SAdrian Chadd break; 504a043e8c7SAdrian Chadd } 505a043e8c7SAdrian Chadd return (err); 506a043e8c7SAdrian Chadd } 507a043e8c7SAdrian Chadd 508a043e8c7SAdrian Chadd static int 509a043e8c7SAdrian Chadd smi_read_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t *data, int sleep) 510a043e8c7SAdrian Chadd { 511a043e8c7SAdrian Chadd int err; 512477e3effSMichael Zhilin device_t iicbus; 513a043e8c7SAdrian Chadd char bytes[2]; 514a043e8c7SAdrian Chadd int xferd; 515a043e8c7SAdrian Chadd 516477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev); 517477e3effSMichael Zhilin 518a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 519a043e8c7SAdrian Chadd bytes[0] = addr & 0xff; 520a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff; 521a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_READ, sleep); 522a043e8c7SAdrian Chadd if (err != 0) 523a043e8c7SAdrian Chadd goto out; 524a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT); 525a043e8c7SAdrian Chadd if (err != 0) 526a043e8c7SAdrian Chadd goto out; 527a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0); 528a043e8c7SAdrian Chadd if (err != 0) 529a043e8c7SAdrian Chadd goto out; 530a043e8c7SAdrian Chadd *data = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff); 531a043e8c7SAdrian Chadd 532a043e8c7SAdrian Chadd out: 533a043e8c7SAdrian Chadd iicbus_stop(iicbus); 534a043e8c7SAdrian Chadd return (err); 535a043e8c7SAdrian Chadd } 536a043e8c7SAdrian Chadd 537a043e8c7SAdrian Chadd static int 538a043e8c7SAdrian Chadd smi_write_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t data, int sleep) 539a043e8c7SAdrian Chadd { 540a043e8c7SAdrian Chadd int err; 541477e3effSMichael Zhilin device_t iicbus; 542a043e8c7SAdrian Chadd char bytes[4]; 543a043e8c7SAdrian Chadd int xferd; 544a043e8c7SAdrian Chadd 545477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev); 546477e3effSMichael Zhilin 547a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 548a043e8c7SAdrian Chadd bytes[0] = addr & 0xff; 549a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff; 550a043e8c7SAdrian Chadd bytes[2] = data & 0xff; 551a043e8c7SAdrian Chadd bytes[3] = (data >> 8) & 0xff; 552a043e8c7SAdrian Chadd 553a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_WRITE, sleep); 554a043e8c7SAdrian Chadd if (err == 0) 555a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 4, &xferd, RTL_IICBUS_TIMEOUT); 556a043e8c7SAdrian Chadd iicbus_stop(iicbus); 557a043e8c7SAdrian Chadd 558a043e8c7SAdrian Chadd return (err); 559a043e8c7SAdrian Chadd } 560a043e8c7SAdrian Chadd 561a043e8c7SAdrian Chadd static int 562a043e8c7SAdrian Chadd smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep) 563a043e8c7SAdrian Chadd { 564477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 565a043e8c7SAdrian Chadd int err; 566a043e8c7SAdrian Chadd 567477e3effSMichael Zhilin sc = device_get_softc(dev); 568477e3effSMichael Zhilin 569a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 570a043e8c7SAdrian Chadd if (err != 0) 571a043e8c7SAdrian Chadd return (EBUSY); 572a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, data, sleep); 573a043e8c7SAdrian Chadd smi_release(sc, sleep); 574a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_read()=%d: addr=%04x\n", addr); 575a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 576a043e8c7SAdrian Chadd } 577a043e8c7SAdrian Chadd 578a043e8c7SAdrian Chadd static int 579a043e8c7SAdrian Chadd smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep) 580a043e8c7SAdrian Chadd { 581477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 582a043e8c7SAdrian Chadd int err; 583a043e8c7SAdrian Chadd 584477e3effSMichael Zhilin sc = device_get_softc(dev); 585477e3effSMichael Zhilin 586a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 587a043e8c7SAdrian Chadd if (err != 0) 588a043e8c7SAdrian Chadd return (EBUSY); 589a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, data, sleep); 590a043e8c7SAdrian Chadd smi_release(sc, sleep); 591a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_write()=%d: addr=%04x\n", addr); 592a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 593a043e8c7SAdrian Chadd } 594a043e8c7SAdrian Chadd 595a043e8c7SAdrian Chadd static int 596a043e8c7SAdrian Chadd smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep) 597a043e8c7SAdrian Chadd { 598477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 599a043e8c7SAdrian Chadd int err; 600a043e8c7SAdrian Chadd uint16_t oldv, newv; 601a043e8c7SAdrian Chadd 602477e3effSMichael Zhilin sc = device_get_softc(dev); 603477e3effSMichael Zhilin 604a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 605a043e8c7SAdrian Chadd if (err != 0) 606a043e8c7SAdrian Chadd return (EBUSY); 607a043e8c7SAdrian Chadd if (err == 0) { 608a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, &oldv, sleep); 609a043e8c7SAdrian Chadd if (err == 0) { 610a043e8c7SAdrian Chadd newv = oldv & ~mask; 611a043e8c7SAdrian Chadd newv |= data & mask; 612a043e8c7SAdrian Chadd if (newv != oldv) 613a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, newv, sleep); 614a043e8c7SAdrian Chadd } 615a043e8c7SAdrian Chadd } 616a043e8c7SAdrian Chadd smi_release(sc, sleep); 617a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_rmw()=%d: addr=%04x\n", addr); 618a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 619a043e8c7SAdrian Chadd } 620a043e8c7SAdrian Chadd 621a043e8c7SAdrian Chadd static etherswitch_info_t * 622a043e8c7SAdrian Chadd rtl_getinfo(device_t dev) 623a043e8c7SAdrian Chadd { 624477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 625477e3effSMichael Zhilin 626477e3effSMichael Zhilin sc = device_get_softc(dev); 627477e3effSMichael Zhilin 628477e3effSMichael Zhilin return (&sc->info); 629a043e8c7SAdrian Chadd } 630a043e8c7SAdrian Chadd 631a043e8c7SAdrian Chadd static int 632a043e8c7SAdrian Chadd rtl_readreg(device_t dev, int reg) 633a043e8c7SAdrian Chadd { 634477e3effSMichael Zhilin uint16_t data; 635477e3effSMichael Zhilin 636477e3effSMichael Zhilin data = 0; 637a043e8c7SAdrian Chadd 638a043e8c7SAdrian Chadd smi_read(dev, reg, &data, RTL_WAITOK); 639a043e8c7SAdrian Chadd return (data); 640a043e8c7SAdrian Chadd } 641a043e8c7SAdrian Chadd 642a043e8c7SAdrian Chadd static int 643a043e8c7SAdrian Chadd rtl_writereg(device_t dev, int reg, int value) 644a043e8c7SAdrian Chadd { 645a043e8c7SAdrian Chadd return (smi_write(dev, reg, value, RTL_WAITOK)); 646a043e8c7SAdrian Chadd } 647a043e8c7SAdrian Chadd 648a043e8c7SAdrian Chadd static int 649a043e8c7SAdrian Chadd rtl_getport(device_t dev, etherswitch_port_t *p) 650a043e8c7SAdrian Chadd { 651a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 652a043e8c7SAdrian Chadd struct ifmedia *ifm; 653a043e8c7SAdrian Chadd struct mii_data *mii; 654477e3effSMichael Zhilin struct ifmediareq *ifmr; 655a043e8c7SAdrian Chadd uint16_t v; 656a3219359SAdrian Chadd int err, vlangroup; 657a043e8c7SAdrian Chadd 658a3219359SAdrian Chadd sc = device_get_softc(dev); 659477e3effSMichael Zhilin 660477e3effSMichael Zhilin ifmr = &p->es_ifmr; 661477e3effSMichael Zhilin 6625a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1)) 663477e3effSMichael Zhilin return (ENXIO); 6645a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) { 6655a4380b5SMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port + 1, 6665a4380b5SMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port + 1))); 6675a4380b5SMichael Zhilin } else { 668477e3effSMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port, 669477e3effSMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port))); 6705a4380b5SMichael Zhilin } 671bfae9329SLuiz Otavio O Souza p->es_pvid = sc->vid[vlangroup] & ETHERSWITCH_VID_MASK; 672a043e8c7SAdrian Chadd 6735a4380b5SMichael Zhilin if (p->es_port < sc->numphys) { 674a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]); 675a043e8c7SAdrian Chadd ifm = &mii->mii_media; 676a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCGIFMEDIA); 677a043e8c7SAdrian Chadd if (err) 678a043e8c7SAdrian Chadd return (err); 679a043e8c7SAdrian Chadd } else { 680a043e8c7SAdrian Chadd /* fill in fixed values for CPU port */ 681dddab089SLuiz Otavio O Souza p->es_flags |= ETHERSWITCH_PORT_CPU; 682477e3effSMichael Zhilin smi_read(dev, RTL8366_PLSR_BASE + (RTL8366_NUM_PHYS)/2, &v, RTL_WAITOK); 683477e3effSMichael Zhilin v = v >> (8 * ((RTL8366_NUM_PHYS) % 2)); 684a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(v, &ifmr->ifm_status, &ifmr->ifm_active); 685a043e8c7SAdrian Chadd ifmr->ifm_current = ifmr->ifm_active; 686a043e8c7SAdrian Chadd ifmr->ifm_mask = 0; 687a043e8c7SAdrian Chadd ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 68828b07d23SLuiz Otavio O Souza /* Return our static media list. */ 68928b07d23SLuiz Otavio O Souza if (ifmr->ifm_count > 0) { 69028b07d23SLuiz Otavio O Souza ifmr->ifm_count = 1; 69128b07d23SLuiz Otavio O Souza ifmr->ifm_ulist[0] = IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 69228b07d23SLuiz Otavio O Souza IFM_FDX, 0); 69328b07d23SLuiz Otavio O Souza } else 69428b07d23SLuiz Otavio O Souza ifmr->ifm_count = 0; 695a043e8c7SAdrian Chadd } 696a043e8c7SAdrian Chadd return (0); 697a043e8c7SAdrian Chadd } 698a043e8c7SAdrian Chadd 699a043e8c7SAdrian Chadd static int 700a043e8c7SAdrian Chadd rtl_setport(device_t dev, etherswitch_port_t *p) 701a043e8c7SAdrian Chadd { 702a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 703477e3effSMichael Zhilin int i, err, vlangroup; 704a043e8c7SAdrian Chadd struct ifmedia *ifm; 705a043e8c7SAdrian Chadd struct mii_data *mii; 7065a4380b5SMichael Zhilin int port; 707a043e8c7SAdrian Chadd 708a3219359SAdrian Chadd sc = device_get_softc(dev); 709477e3effSMichael Zhilin 7105a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1)) 711477e3effSMichael Zhilin return (ENXIO); 712a3219359SAdrian Chadd vlangroup = -1; 713477e3effSMichael Zhilin for (i = 0; i < RTL8366_NUM_VLANS; i++) { 714bfae9329SLuiz Otavio O Souza if ((sc->vid[i] & ETHERSWITCH_VID_MASK) == p->es_pvid) { 715a3219359SAdrian Chadd vlangroup = i; 716a3219359SAdrian Chadd break; 717a3219359SAdrian Chadd } 718a3219359SAdrian Chadd } 719a3219359SAdrian Chadd if (vlangroup == -1) 720a3219359SAdrian Chadd return (ENXIO); 7215a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) { 7225a4380b5SMichael Zhilin port = p->es_port + 1; 7235a4380b5SMichael Zhilin } else { 7245a4380b5SMichael Zhilin port = p->es_port; 7255a4380b5SMichael Zhilin } 7265a4380b5SMichael Zhilin err = smi_rmw(dev, RTL8366_PVCR_REG(port), 7275a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, RTL8366_PVCR_PORT_MASK), 7285a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, vlangroup), RTL_WAITOK); 729a043e8c7SAdrian Chadd if (err) 730a043e8c7SAdrian Chadd return (err); 7315a4380b5SMichael Zhilin /* CPU Port */ 7325a4380b5SMichael Zhilin if (p->es_port == sc->numphys) 733dddab089SLuiz Otavio O Souza return (0); 734a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]); 735a043e8c7SAdrian Chadd ifm = &mii->mii_media; 736a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCSIFMEDIA); 737a043e8c7SAdrian Chadd return (err); 738a043e8c7SAdrian Chadd } 739a043e8c7SAdrian Chadd 740a043e8c7SAdrian Chadd static int 741a043e8c7SAdrian Chadd rtl_getvgroup(device_t dev, etherswitch_vlangroup_t *vg) 742a043e8c7SAdrian Chadd { 743bfae9329SLuiz Otavio O Souza struct rtl8366rb_softc *sc; 744a043e8c7SAdrian Chadd uint16_t vmcr[3]; 745a043e8c7SAdrian Chadd int i; 7465a4380b5SMichael Zhilin int member, untagged; 747a043e8c7SAdrian Chadd 748bfae9329SLuiz Otavio O Souza sc = device_get_softc(dev); 749477e3effSMichael Zhilin 750477e3effSMichael Zhilin for (i=0; i<RTL8366_VMCR_MULT; i++) 751477e3effSMichael Zhilin vmcr[i] = rtl_readreg(dev, RTL8366_VMCR(i, vg->es_vlangroup)); 752477e3effSMichael Zhilin 753bfae9329SLuiz Otavio O Souza vg->es_vid = sc->vid[vg->es_vlangroup]; 7545a4380b5SMichael Zhilin member = RTL8366_VMCR_MEMBER(vmcr); 7555a4380b5SMichael Zhilin untagged = RTL8366_VMCR_UNTAG(vmcr); 7565a4380b5SMichael Zhilin if (sc->phy4cpu) { 7575a4380b5SMichael Zhilin vg->es_member_ports = ((member & 0x20) >> 1) | (member & 0x0f); 7585a4380b5SMichael Zhilin vg->es_untagged_ports = ((untagged & 0x20) >> 1) | (untagged & 0x0f); 7595a4380b5SMichael Zhilin } else { 7605a4380b5SMichael Zhilin vg->es_member_ports = member; 7615a4380b5SMichael Zhilin vg->es_untagged_ports = untagged; 7625a4380b5SMichael Zhilin } 763477e3effSMichael Zhilin vg->es_fid = RTL8366_VMCR_FID(vmcr); 764a043e8c7SAdrian Chadd return (0); 765a043e8c7SAdrian Chadd } 766a043e8c7SAdrian Chadd 767a043e8c7SAdrian Chadd static int 768a043e8c7SAdrian Chadd rtl_setvgroup(device_t dev, etherswitch_vlangroup_t *vg) 769a043e8c7SAdrian Chadd { 770a3219359SAdrian Chadd struct rtl8366rb_softc *sc; 771477e3effSMichael Zhilin int g; 7725a4380b5SMichael Zhilin int member, untagged; 773a043e8c7SAdrian Chadd 774a3219359SAdrian Chadd sc = device_get_softc(dev); 775477e3effSMichael Zhilin 776477e3effSMichael Zhilin g = vg->es_vlangroup; 777477e3effSMichael Zhilin 778a3219359SAdrian Chadd sc->vid[g] = vg->es_vid; 779bfae9329SLuiz Otavio O Souza /* VLAN group disabled ? */ 780bfae9329SLuiz Otavio O Souza if (vg->es_member_ports == 0 && vg->es_untagged_ports == 0 && vg->es_vid == 0) 781bfae9329SLuiz Otavio O Souza return (0); 782bfae9329SLuiz Otavio O Souza sc->vid[g] |= ETHERSWITCH_VID_VALID; 783477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_DOT1Q_REG, g), 784477e3effSMichael Zhilin (vg->es_vid << RTL8366_VMCR_DOT1Q_VID_SHIFT) & RTL8366_VMCR_DOT1Q_VID_MASK); 7855a4380b5SMichael Zhilin if (sc->phy4cpu) { 7865a4380b5SMichael Zhilin /* add space at phy4 */ 7875a4380b5SMichael Zhilin member = (vg->es_member_ports & 0x0f) | 7885a4380b5SMichael Zhilin ((vg->es_member_ports & 0x10) << 1); 7895a4380b5SMichael Zhilin untagged = (vg->es_untagged_ports & 0x0f) | 7905a4380b5SMichael Zhilin ((vg->es_untagged_ports & 0x10) << 1); 7915a4380b5SMichael Zhilin } else { 7925a4380b5SMichael Zhilin member = vg->es_member_ports; 7935a4380b5SMichael Zhilin untagged = vg->es_untagged_ports; 7945a4380b5SMichael Zhilin } 7955a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) { 796477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g), 7975a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) | 7985a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK)); 799477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_FID_REG, g), 800a043e8c7SAdrian Chadd vg->es_fid); 801477e3effSMichael Zhilin } else { 802477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g), 8035a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) | 8045a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK) | 805477e3effSMichael Zhilin ((vg->es_fid << RTL8366_VMCR_FID_FID_SHIFT) & RTL8366_VMCR_FID_FID_MASK)); 806477e3effSMichael Zhilin } 807a043e8c7SAdrian Chadd return (0); 808a043e8c7SAdrian Chadd } 809a043e8c7SAdrian Chadd 810a043e8c7SAdrian Chadd static int 811bfae9329SLuiz Otavio O Souza rtl_getconf(device_t dev, etherswitch_conf_t *conf) 812bfae9329SLuiz Otavio O Souza { 813bfae9329SLuiz Otavio O Souza 814bfae9329SLuiz Otavio O Souza /* Return the VLAN mode. */ 815bfae9329SLuiz Otavio O Souza conf->cmd = ETHERSWITCH_CONF_VLAN_MODE; 816bfae9329SLuiz Otavio O Souza conf->vlan_mode = ETHERSWITCH_VLAN_DOT1Q; 817bfae9329SLuiz Otavio O Souza 818bfae9329SLuiz Otavio O Souza return (0); 819bfae9329SLuiz Otavio O Souza } 820bfae9329SLuiz Otavio O Souza 821bfae9329SLuiz Otavio O Souza static int 822a043e8c7SAdrian Chadd rtl_readphy(device_t dev, int phy, int reg) 823a043e8c7SAdrian Chadd { 824477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 825477e3effSMichael Zhilin uint16_t data; 826a043e8c7SAdrian Chadd int err, i, sleep; 827a043e8c7SAdrian Chadd 828477e3effSMichael Zhilin sc = device_get_softc(dev); 829477e3effSMichael Zhilin 830477e3effSMichael Zhilin data = 0; 831477e3effSMichael Zhilin 832477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS) 833a043e8c7SAdrian Chadd return (ENXIO); 834477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG) 835a043e8c7SAdrian Chadd return (ENXIO); 836a043e8c7SAdrian Chadd sleep = RTL_WAITOK; 837a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 838a043e8c7SAdrian Chadd if (err != 0) 839a043e8c7SAdrian Chadd return (EBUSY); 840a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 841477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_READ, sleep); 842a043e8c7SAdrian Chadd if (err == 0) 843477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), 0, sleep); 844a043e8c7SAdrian Chadd if (err == 0) { 845477e3effSMichael Zhilin err = smi_read_locked(sc, RTL8366_PADR, &data, sleep); 846a043e8c7SAdrian Chadd break; 847a043e8c7SAdrian Chadd } 848a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries); 849a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_readphy(): chip not responsive, retrying %d more times\n", i); 850a043e8c7SAdrian Chadd pause("rtl_readphy", RTL_IICBUS_RETRY_SLEEP); 851a043e8c7SAdrian Chadd } 852a043e8c7SAdrian Chadd smi_release(sc, sleep); 853a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_readphy()=%d: phy=%d.%02x\n", phy, reg); 854a043e8c7SAdrian Chadd return (data); 855a043e8c7SAdrian Chadd } 856a043e8c7SAdrian Chadd 857a043e8c7SAdrian Chadd static int 858a043e8c7SAdrian Chadd rtl_writephy(device_t dev, int phy, int reg, int data) 859a043e8c7SAdrian Chadd { 860477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 861a043e8c7SAdrian Chadd int err, i, sleep; 862a043e8c7SAdrian Chadd 863477e3effSMichael Zhilin sc = device_get_softc(dev); 864477e3effSMichael Zhilin 865477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS) 866a043e8c7SAdrian Chadd return (ENXIO); 867477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG) 868a043e8c7SAdrian Chadd return (ENXIO); 869a043e8c7SAdrian Chadd sleep = RTL_WAITOK; 870a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 871a043e8c7SAdrian Chadd if (err != 0) 872a043e8c7SAdrian Chadd return (EBUSY); 873a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 874477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_WRITE, sleep); 875a043e8c7SAdrian Chadd if (err == 0) 876477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), data, sleep); 877a043e8c7SAdrian Chadd if (err == 0) { 878a043e8c7SAdrian Chadd break; 879a043e8c7SAdrian Chadd } 880a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries); 881a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_writephy(): chip not responsive, retrying %d more tiems\n", i); 882a043e8c7SAdrian Chadd pause("rtl_writephy", RTL_IICBUS_RETRY_SLEEP); 883a043e8c7SAdrian Chadd } 884a043e8c7SAdrian Chadd smi_release(sc, sleep); 885a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_writephy()=%d: phy=%d.%02x\n", phy, reg); 886a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 887a043e8c7SAdrian Chadd } 888a043e8c7SAdrian Chadd 889a043e8c7SAdrian Chadd static int 8902e6a8c1aSJustin Hibbits rtl8366rb_ifmedia_upd(if_t ifp) 891a043e8c7SAdrian Chadd { 892477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 893477e3effSMichael Zhilin struct mii_data *mii; 894477e3effSMichael Zhilin 8952e6a8c1aSJustin Hibbits sc = if_getsoftc(ifp); 8962e6a8c1aSJustin Hibbits mii = device_get_softc(sc->miibus[if_getdunit(ifp)]); 897a043e8c7SAdrian Chadd 898a043e8c7SAdrian Chadd mii_mediachg(mii); 899a043e8c7SAdrian Chadd return (0); 900a043e8c7SAdrian Chadd } 901a043e8c7SAdrian Chadd 902a043e8c7SAdrian Chadd static void 9032e6a8c1aSJustin Hibbits rtl8366rb_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr) 904a043e8c7SAdrian Chadd { 905477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 906477e3effSMichael Zhilin struct mii_data *mii; 907477e3effSMichael Zhilin 9082e6a8c1aSJustin Hibbits sc = if_getsoftc(ifp); 9092e6a8c1aSJustin Hibbits mii = device_get_softc(sc->miibus[if_getdunit(ifp)]); 910a043e8c7SAdrian Chadd 911a043e8c7SAdrian Chadd mii_pollstat(mii); 912a043e8c7SAdrian Chadd ifmr->ifm_active = mii->mii_media_active; 913a043e8c7SAdrian Chadd ifmr->ifm_status = mii->mii_media_status; 914a043e8c7SAdrian Chadd } 915a043e8c7SAdrian Chadd 916a043e8c7SAdrian Chadd 917a043e8c7SAdrian Chadd static device_method_t rtl8366rb_methods[] = { 918a043e8c7SAdrian Chadd /* Device interface */ 919a043e8c7SAdrian Chadd DEVMETHOD(device_identify, rtl8366rb_identify), 920a043e8c7SAdrian Chadd DEVMETHOD(device_probe, rtl8366rb_probe), 921a043e8c7SAdrian Chadd DEVMETHOD(device_attach, rtl8366rb_attach), 922a043e8c7SAdrian Chadd DEVMETHOD(device_detach, rtl8366rb_detach), 923a043e8c7SAdrian Chadd 924a043e8c7SAdrian Chadd /* bus interface */ 925a043e8c7SAdrian Chadd DEVMETHOD(bus_add_child, device_add_child_ordered), 926a043e8c7SAdrian Chadd 927a043e8c7SAdrian Chadd /* MII interface */ 928a043e8c7SAdrian Chadd DEVMETHOD(miibus_readreg, rtl_readphy), 929a043e8c7SAdrian Chadd DEVMETHOD(miibus_writereg, rtl_writephy), 930a043e8c7SAdrian Chadd 9315a4380b5SMichael Zhilin /* MDIO interface */ 9325a4380b5SMichael Zhilin DEVMETHOD(mdio_readreg, rtl_readphy), 9335a4380b5SMichael Zhilin DEVMETHOD(mdio_writereg, rtl_writephy), 9345a4380b5SMichael Zhilin 935a043e8c7SAdrian Chadd /* etherswitch interface */ 936bfae9329SLuiz Otavio O Souza DEVMETHOD(etherswitch_getconf, rtl_getconf), 937a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getinfo, rtl_getinfo), 938a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readreg, rtl_readreg), 939a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writereg, rtl_writereg), 940a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readphyreg, rtl_readphy), 941a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writephyreg, rtl_writephy), 942a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getport, rtl_getport), 943a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setport, rtl_setport), 944a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getvgroup, rtl_getvgroup), 945a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setvgroup, rtl_setvgroup), 946a043e8c7SAdrian Chadd 947a043e8c7SAdrian Chadd DEVMETHOD_END 948a043e8c7SAdrian Chadd }; 949a043e8c7SAdrian Chadd 950a043e8c7SAdrian Chadd DEFINE_CLASS_0(rtl8366rb, rtl8366rb_driver, rtl8366rb_methods, 951a043e8c7SAdrian Chadd sizeof(struct rtl8366rb_softc)); 952a043e8c7SAdrian Chadd 95342726c2fSJohn Baldwin DRIVER_MODULE(rtl8366rb, iicbus, rtl8366rb_driver, 0, 0); 9543e38757dSJohn Baldwin DRIVER_MODULE(miibus, rtl8366rb, miibus_driver, 0, 0); 9558933f7d6SJohn Baldwin DRIVER_MODULE(mdio, rtl8366rb, mdio_driver, 0, 0); 956829a13faSJohn Baldwin DRIVER_MODULE(etherswitch, rtl8366rb, etherswitch_driver, 0, 0); 957a043e8c7SAdrian Chadd MODULE_VERSION(rtl8366rb, 1); 958a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, iicbus, 1, 1, 1); /* XXX which versions? */ 959a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, miibus, 1, 1, 1); /* XXX which versions? */ 960a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, etherswitch, 1, 1, 1); /* XXX which versions? */ 961