1a043e8c7SAdrian Chadd /*- 2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 * $FreeBSD$ 30a043e8c7SAdrian Chadd */ 31a043e8c7SAdrian Chadd 32477e3effSMichael Zhilin #include "opt_etherswitch.h" 33477e3effSMichael Zhilin 34a043e8c7SAdrian Chadd #include <sys/param.h> 35a043e8c7SAdrian Chadd #include <sys/bus.h> 36a043e8c7SAdrian Chadd #include <sys/errno.h> 37a043e8c7SAdrian Chadd #include <sys/kernel.h> 383d02237cSLuiz Otavio O Souza #include <sys/lock.h> 393d02237cSLuiz Otavio O Souza #include <sys/malloc.h> 40a043e8c7SAdrian Chadd #include <sys/module.h> 413d02237cSLuiz Otavio O Souza #include <sys/mutex.h> 42a043e8c7SAdrian Chadd #include <sys/socket.h> 43a043e8c7SAdrian Chadd #include <sys/sockio.h> 44a043e8c7SAdrian Chadd #include <sys/sysctl.h> 45a043e8c7SAdrian Chadd #include <sys/systm.h> 46a043e8c7SAdrian Chadd 47a043e8c7SAdrian Chadd #include <net/if.h> 483d02237cSLuiz Otavio O Souza #include <net/if_var.h> 49a043e8c7SAdrian Chadd #include <net/ethernet.h> 50a043e8c7SAdrian Chadd #include <net/if_media.h> 51a043e8c7SAdrian Chadd #include <net/if_types.h> 52a043e8c7SAdrian Chadd 53a043e8c7SAdrian Chadd #include <machine/bus.h> 54efce3748SRui Paulo #include <dev/iicbus/iic.h> 55a043e8c7SAdrian Chadd #include <dev/iicbus/iiconf.h> 56a043e8c7SAdrian Chadd #include <dev/iicbus/iicbus.h> 57a043e8c7SAdrian Chadd #include <dev/mii/mii.h> 58a043e8c7SAdrian Chadd #include <dev/mii/miivar.h> 595a4380b5SMichael Zhilin #include <dev/mdio/mdio.h> 60a043e8c7SAdrian Chadd 61a043e8c7SAdrian Chadd #include <dev/etherswitch/etherswitch.h> 62a043e8c7SAdrian Chadd #include <dev/etherswitch/rtl8366/rtl8366rbvar.h> 63a043e8c7SAdrian Chadd 645a4380b5SMichael Zhilin #include "mdio_if.h" 65a043e8c7SAdrian Chadd #include "iicbus_if.h" 66a043e8c7SAdrian Chadd #include "miibus_if.h" 67a043e8c7SAdrian Chadd #include "etherswitch_if.h" 68a043e8c7SAdrian Chadd 69a043e8c7SAdrian Chadd 70a043e8c7SAdrian Chadd struct rtl8366rb_softc { 71a043e8c7SAdrian Chadd struct mtx sc_mtx; /* serialize access to softc */ 72a043e8c7SAdrian Chadd int smi_acquired; /* serialize access to SMI/I2C bus */ 73a043e8c7SAdrian Chadd struct mtx callout_mtx; /* serialize callout */ 74a043e8c7SAdrian Chadd device_t dev; 75477e3effSMichael Zhilin int vid[RTL8366_NUM_VLANS]; 76477e3effSMichael Zhilin char *ifname[RTL8366_NUM_PHYS]; 77477e3effSMichael Zhilin device_t miibus[RTL8366_NUM_PHYS]; 78477e3effSMichael Zhilin struct ifnet *ifp[RTL8366_NUM_PHYS]; 79a043e8c7SAdrian Chadd struct callout callout_tick; 80477e3effSMichael Zhilin etherswitch_info_t info; 815a4380b5SMichael Zhilin int chip_type; 825a4380b5SMichael Zhilin int phy4cpu; 835a4380b5SMichael Zhilin int numphys; 84a043e8c7SAdrian Chadd }; 85a043e8c7SAdrian Chadd 86a043e8c7SAdrian Chadd #define RTL_LOCK(_sc) mtx_lock(&(_sc)->sc_mtx) 87a043e8c7SAdrian Chadd #define RTL_UNLOCK(_sc) mtx_unlock(&(_sc)->sc_mtx) 88a043e8c7SAdrian Chadd #define RTL_LOCK_ASSERT(_sc, _what) mtx_assert(&(_s)c->sc_mtx, (_what)) 89a043e8c7SAdrian Chadd #define RTL_TRYLOCK(_sc) mtx_trylock(&(_sc)->sc_mtx) 90a043e8c7SAdrian Chadd 91a043e8c7SAdrian Chadd #define RTL_WAITOK 0 92a043e8c7SAdrian Chadd #define RTL_NOWAIT 1 93a043e8c7SAdrian Chadd 94a043e8c7SAdrian Chadd #define RTL_SMI_ACQUIRED 1 95a043e8c7SAdrian Chadd #define RTL_SMI_ACQUIRED_ASSERT(_sc) \ 96a043e8c7SAdrian Chadd KASSERT((_sc)->smi_acquired == RTL_SMI_ACQUIRED, ("smi must be acquired @%s", __FUNCTION__)) 97a043e8c7SAdrian Chadd 98a043e8c7SAdrian Chadd #if defined(DEBUG) 99a043e8c7SAdrian Chadd #define DPRINTF(dev, args...) device_printf(dev, args) 100a043e8c7SAdrian Chadd #define DEVERR(dev, err, fmt, args...) do { \ 101a043e8c7SAdrian Chadd if (err != 0) device_printf(dev, fmt, err, args); \ 102a043e8c7SAdrian Chadd } while (0) 103a043e8c7SAdrian Chadd #define DEBUG_INCRVAR(var) do { \ 104a043e8c7SAdrian Chadd var++; \ 105a043e8c7SAdrian Chadd } while (0) 106a043e8c7SAdrian Chadd 107a043e8c7SAdrian Chadd static int callout_blocked = 0; 108a043e8c7SAdrian Chadd static int iic_select_retries = 0; 109a043e8c7SAdrian Chadd static int phy_access_retries = 0; 1107029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, rtl8366rb, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1117029da5cSPawel Biernacki "rtl8366rb"); 112a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, callout_blocked, CTLFLAG_RW, &callout_blocked, 0, 113a043e8c7SAdrian Chadd "number of times the callout couldn't acquire the bus"); 114a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, iic_select_retries, CTLFLAG_RW, &iic_select_retries, 0, 115a043e8c7SAdrian Chadd "number of times the I2C bus selection had to be retried"); 116a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, phy_access_retries, CTLFLAG_RW, &phy_access_retries, 0, 117a043e8c7SAdrian Chadd "number of times PHY register access had to be retried"); 118a043e8c7SAdrian Chadd #else 119a043e8c7SAdrian Chadd #define DPRINTF(dev, args...) 120a043e8c7SAdrian Chadd #define DEVERR(dev, err, fmt, args...) 121a043e8c7SAdrian Chadd #define DEBUG_INCRVAR(var) 122a043e8c7SAdrian Chadd #endif 123a043e8c7SAdrian Chadd 124a043e8c7SAdrian Chadd static int smi_probe(device_t dev); 125a043e8c7SAdrian Chadd static int smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep); 126a043e8c7SAdrian Chadd static int smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep); 127a043e8c7SAdrian Chadd static int smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep); 128a043e8c7SAdrian Chadd static void rtl8366rb_tick(void *arg); 129a043e8c7SAdrian Chadd static int rtl8366rb_ifmedia_upd(struct ifnet *); 130a043e8c7SAdrian Chadd static void rtl8366rb_ifmedia_sts(struct ifnet *, struct ifmediareq *); 131a043e8c7SAdrian Chadd 132a043e8c7SAdrian Chadd static void 133a043e8c7SAdrian Chadd rtl8366rb_identify(driver_t *driver, device_t parent) 134a043e8c7SAdrian Chadd { 135a043e8c7SAdrian Chadd device_t child; 136a043e8c7SAdrian Chadd struct iicbus_ivar *devi; 137a043e8c7SAdrian Chadd 138a043e8c7SAdrian Chadd if (device_find_child(parent, "rtl8366rb", -1) == NULL) { 139a043e8c7SAdrian Chadd child = BUS_ADD_CHILD(parent, 0, "rtl8366rb", -1); 140a043e8c7SAdrian Chadd devi = IICBUS_IVAR(child); 141477e3effSMichael Zhilin devi->addr = RTL8366_IIC_ADDR; 142a043e8c7SAdrian Chadd } 143a043e8c7SAdrian Chadd } 144a043e8c7SAdrian Chadd 145a043e8c7SAdrian Chadd static int 146a043e8c7SAdrian Chadd rtl8366rb_probe(device_t dev) 147a043e8c7SAdrian Chadd { 148477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 149477e3effSMichael Zhilin 150477e3effSMichael Zhilin sc = device_get_softc(dev); 151477e3effSMichael Zhilin 152477e3effSMichael Zhilin bzero(sc, sizeof(*sc)); 153a043e8c7SAdrian Chadd if (smi_probe(dev) != 0) 154a043e8c7SAdrian Chadd return (ENXIO); 1555a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) 156a043e8c7SAdrian Chadd device_set_desc(dev, "RTL8366RB Ethernet Switch Controller"); 157477e3effSMichael Zhilin else 158477e3effSMichael Zhilin device_set_desc(dev, "RTL8366SR Ethernet Switch Controller"); 159a043e8c7SAdrian Chadd return (BUS_PROBE_DEFAULT); 160a043e8c7SAdrian Chadd } 161a043e8c7SAdrian Chadd 162a043e8c7SAdrian Chadd static void 163a043e8c7SAdrian Chadd rtl8366rb_init(device_t dev) 164a043e8c7SAdrian Chadd { 165bfae9329SLuiz Otavio O Souza struct rtl8366rb_softc *sc; 166477e3effSMichael Zhilin int i; 167477e3effSMichael Zhilin 168477e3effSMichael Zhilin sc = device_get_softc(dev); 169bfae9329SLuiz Otavio O Souza 170a043e8c7SAdrian Chadd /* Initialisation for TL-WR1043ND */ 171477e3effSMichael Zhilin #ifdef RTL8366_SOFT_RESET 172477e3effSMichael Zhilin smi_rmw(dev, RTL8366_RCR, 173477e3effSMichael Zhilin RTL8366_RCR_SOFT_RESET, 174477e3effSMichael Zhilin RTL8366_RCR_SOFT_RESET, RTL_WAITOK); 175477e3effSMichael Zhilin #else 176477e3effSMichael Zhilin smi_rmw(dev, RTL8366_RCR, 177477e3effSMichael Zhilin RTL8366_RCR_HARD_RESET, 178477e3effSMichael Zhilin RTL8366_RCR_HARD_RESET, RTL_WAITOK); 179477e3effSMichael Zhilin #endif 180477e3effSMichael Zhilin /* hard reset not return ack */ 181a043e8c7SAdrian Chadd DELAY(100000); 182a043e8c7SAdrian Chadd /* Enable 16 VLAN mode */ 183477e3effSMichael Zhilin smi_rmw(dev, RTL8366_SGCR, 184477e3effSMichael Zhilin RTL8366_SGCR_EN_VLAN | RTL8366_SGCR_EN_VLAN_4KTB, 185477e3effSMichael Zhilin RTL8366_SGCR_EN_VLAN, RTL_WAITOK); 186bfae9329SLuiz Otavio O Souza /* Initialize our vlan table. */ 187bfae9329SLuiz Otavio O Souza for (i = 0; i <= 1; i++) 188bfae9329SLuiz Otavio O Souza sc->vid[i] = (i + 1) | ETHERSWITCH_VID_VALID; 189bfae9329SLuiz Otavio O Souza /* Remove port 0 from VLAN 1. */ 190477e3effSMichael Zhilin smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 0), 191a043e8c7SAdrian Chadd (1 << 0), 0, RTL_WAITOK); 192bfae9329SLuiz Otavio O Souza /* Add port 0 untagged and port 5 tagged to VLAN 2. */ 193477e3effSMichael Zhilin smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 1), 194477e3effSMichael Zhilin ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT) 195477e3effSMichael Zhilin | ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT), 196477e3effSMichael Zhilin ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT 197477e3effSMichael Zhilin | ((1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT)), 198a043e8c7SAdrian Chadd RTL_WAITOK); 199bfae9329SLuiz Otavio O Souza /* Set PVID 2 for port 0. */ 200477e3effSMichael Zhilin smi_rmw(dev, RTL8366_PVCR_REG(0), 201477e3effSMichael Zhilin RTL8366_PVCR_VAL(0, RTL8366_PVCR_PORT_MASK), 202477e3effSMichael Zhilin RTL8366_PVCR_VAL(0, 1), RTL_WAITOK); 203a043e8c7SAdrian Chadd } 204a043e8c7SAdrian Chadd 205a043e8c7SAdrian Chadd static int 206a043e8c7SAdrian Chadd rtl8366rb_attach(device_t dev) 207a043e8c7SAdrian Chadd { 208a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 209477e3effSMichael Zhilin uint16_t rev = 0; 210a043e8c7SAdrian Chadd char name[IFNAMSIZ]; 211a043e8c7SAdrian Chadd int err = 0; 212a043e8c7SAdrian Chadd int i; 213a043e8c7SAdrian Chadd 214a043e8c7SAdrian Chadd sc = device_get_softc(dev); 215477e3effSMichael Zhilin 216a043e8c7SAdrian Chadd sc->dev = dev; 217a043e8c7SAdrian Chadd mtx_init(&sc->sc_mtx, "rtl8366rb", NULL, MTX_DEF); 218a043e8c7SAdrian Chadd sc->smi_acquired = 0; 219a043e8c7SAdrian Chadd mtx_init(&sc->callout_mtx, "rtl8366rbcallout", NULL, MTX_DEF); 220a043e8c7SAdrian Chadd 221a043e8c7SAdrian Chadd rtl8366rb_init(dev); 222477e3effSMichael Zhilin smi_read(dev, RTL8366_CVCR, &rev, RTL_WAITOK); 223a043e8c7SAdrian Chadd device_printf(dev, "rev. %d\n", rev & 0x000f); 224a043e8c7SAdrian Chadd 2255a4380b5SMichael Zhilin sc->phy4cpu = 0; 2265a4380b5SMichael Zhilin (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 2275a4380b5SMichael Zhilin "phy4cpu", &sc->phy4cpu); 2285a4380b5SMichael Zhilin 2295a4380b5SMichael Zhilin sc->numphys = sc->phy4cpu ? RTL8366_NUM_PHYS - 1 : RTL8366_NUM_PHYS; 2305a4380b5SMichael Zhilin 2315a4380b5SMichael Zhilin sc->info.es_nports = sc->numphys + 1; 232477e3effSMichael Zhilin sc->info.es_nvlangroups = RTL8366_NUM_VLANS; 233477e3effSMichael Zhilin sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q; 2345a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) 235477e3effSMichael Zhilin sprintf(sc->info.es_name, "Realtek RTL8366RB"); 236477e3effSMichael Zhilin else 237477e3effSMichael Zhilin sprintf(sc->info.es_name, "Realtek RTL8366SR"); 238477e3effSMichael Zhilin 239a043e8c7SAdrian Chadd /* attach miibus and phys */ 240a043e8c7SAdrian Chadd /* PHYs need an interface, so we generate a dummy one */ 2415a4380b5SMichael Zhilin for (i = 0; i < sc->numphys; i++) { 242a043e8c7SAdrian Chadd sc->ifp[i] = if_alloc(IFT_ETHER); 2430774131eSMichael Zhilin if (sc->ifp[i] == NULL) { 2441b8be09aSMichael Zhilin device_printf(dev, "couldn't allocate ifnet structure\n"); 2450774131eSMichael Zhilin err = ENOMEM; 2460774131eSMichael Zhilin break; 2470774131eSMichael Zhilin } 2480774131eSMichael Zhilin 249a043e8c7SAdrian Chadd sc->ifp[i]->if_softc = sc; 250a043e8c7SAdrian Chadd sc->ifp[i]->if_flags |= IFF_UP | IFF_BROADCAST | IFF_DRV_RUNNING 251a043e8c7SAdrian Chadd | IFF_SIMPLEX; 252a043e8c7SAdrian Chadd snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(dev)); 253a043e8c7SAdrian Chadd sc->ifname[i] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK); 254a043e8c7SAdrian Chadd bcopy(name, sc->ifname[i], strlen(name)+1); 255a043e8c7SAdrian Chadd if_initname(sc->ifp[i], sc->ifname[i], i); 256a043e8c7SAdrian Chadd err = mii_attach(dev, &sc->miibus[i], sc->ifp[i], rtl8366rb_ifmedia_upd, \ 257a043e8c7SAdrian Chadd rtl8366rb_ifmedia_sts, BMSR_DEFCAPMASK, \ 258a043e8c7SAdrian Chadd i, MII_OFFSET_ANY, 0); 259a043e8c7SAdrian Chadd if (err != 0) { 260a043e8c7SAdrian Chadd device_printf(dev, "attaching PHY %d failed\n", i); 261a043e8c7SAdrian Chadd return (err); 262a043e8c7SAdrian Chadd } 263a043e8c7SAdrian Chadd } 264a043e8c7SAdrian Chadd 265a043e8c7SAdrian Chadd bus_generic_probe(dev); 266a043e8c7SAdrian Chadd bus_enumerate_hinted_children(dev); 267a043e8c7SAdrian Chadd err = bus_generic_attach(dev); 268a043e8c7SAdrian Chadd if (err != 0) 269a043e8c7SAdrian Chadd return (err); 270a043e8c7SAdrian Chadd 271a043e8c7SAdrian Chadd callout_init_mtx(&sc->callout_tick, &sc->callout_mtx, 0); 272a043e8c7SAdrian Chadd rtl8366rb_tick(sc); 273a043e8c7SAdrian Chadd 274a043e8c7SAdrian Chadd return (err); 275a043e8c7SAdrian Chadd } 276a043e8c7SAdrian Chadd 277a043e8c7SAdrian Chadd static int 278a043e8c7SAdrian Chadd rtl8366rb_detach(device_t dev) 279a043e8c7SAdrian Chadd { 280477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 281a043e8c7SAdrian Chadd int i; 282a043e8c7SAdrian Chadd 283477e3effSMichael Zhilin sc = device_get_softc(dev); 284477e3effSMichael Zhilin 2855a4380b5SMichael Zhilin for (i=0; i < sc->numphys; i++) { 286a043e8c7SAdrian Chadd if (sc->miibus[i]) 287a043e8c7SAdrian Chadd device_delete_child(dev, sc->miibus[i]); 288a043e8c7SAdrian Chadd if (sc->ifp[i] != NULL) 289a043e8c7SAdrian Chadd if_free(sc->ifp[i]); 290a043e8c7SAdrian Chadd free(sc->ifname[i], M_DEVBUF); 291a043e8c7SAdrian Chadd } 292a043e8c7SAdrian Chadd bus_generic_detach(dev); 293a043e8c7SAdrian Chadd callout_drain(&sc->callout_tick); 294a043e8c7SAdrian Chadd mtx_destroy(&sc->callout_mtx); 295a043e8c7SAdrian Chadd mtx_destroy(&sc->sc_mtx); 296a043e8c7SAdrian Chadd 297a043e8c7SAdrian Chadd return (0); 298a043e8c7SAdrian Chadd } 299a043e8c7SAdrian Chadd 300a043e8c7SAdrian Chadd static void 301a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active) 302a043e8c7SAdrian Chadd { 303a043e8c7SAdrian Chadd *media_active = IFM_ETHER; 304a043e8c7SAdrian Chadd *media_status = IFM_AVALID; 305477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_LINK) != 0) 306a043e8c7SAdrian Chadd *media_status |= IFM_ACTIVE; 307a043e8c7SAdrian Chadd else { 308a043e8c7SAdrian Chadd *media_active |= IFM_NONE; 309a043e8c7SAdrian Chadd return; 310a043e8c7SAdrian Chadd } 311477e3effSMichael Zhilin switch (portstatus & RTL8366_PLSR_SPEED_MASK) { 312477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_10: 313a043e8c7SAdrian Chadd *media_active |= IFM_10_T; 314a043e8c7SAdrian Chadd break; 315477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_100: 316a043e8c7SAdrian Chadd *media_active |= IFM_100_TX; 317a043e8c7SAdrian Chadd break; 318477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_1000: 319a043e8c7SAdrian Chadd *media_active |= IFM_1000_T; 320a043e8c7SAdrian Chadd break; 321a043e8c7SAdrian Chadd } 322477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_FULLDUPLEX) != 0) 323a043e8c7SAdrian Chadd *media_active |= IFM_FDX; 324a043e8c7SAdrian Chadd else 325a043e8c7SAdrian Chadd *media_active |= IFM_HDX; 326477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_TXPAUSE) != 0) 327a043e8c7SAdrian Chadd *media_active |= IFM_ETH_TXPAUSE; 328477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_RXPAUSE) != 0) 329a043e8c7SAdrian Chadd *media_active |= IFM_ETH_RXPAUSE; 330a043e8c7SAdrian Chadd } 331a043e8c7SAdrian Chadd 332a043e8c7SAdrian Chadd static void 333a043e8c7SAdrian Chadd rtl833rb_miipollstat(struct rtl8366rb_softc *sc) 334a043e8c7SAdrian Chadd { 335a043e8c7SAdrian Chadd int i; 336a043e8c7SAdrian Chadd struct mii_data *mii; 337a043e8c7SAdrian Chadd struct mii_softc *miisc; 338a043e8c7SAdrian Chadd uint16_t value; 339a043e8c7SAdrian Chadd int portstatus; 340a043e8c7SAdrian Chadd 3415a4380b5SMichael Zhilin for (i = 0; i < sc->numphys; i++) { 342a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[i]); 343a043e8c7SAdrian Chadd if ((i % 2) == 0) { 344477e3effSMichael Zhilin if (smi_read(sc->dev, RTL8366_PLSR_BASE + i/2, &value, RTL_NOWAIT) != 0) { 345a043e8c7SAdrian Chadd DEBUG_INCRVAR(callout_blocked); 346a043e8c7SAdrian Chadd return; 347a043e8c7SAdrian Chadd } 348a043e8c7SAdrian Chadd portstatus = value & 0xff; 349a043e8c7SAdrian Chadd } else { 350a043e8c7SAdrian Chadd portstatus = (value >> 8) & 0xff; 351a043e8c7SAdrian Chadd } 352a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(portstatus, &mii->mii_media_status, &mii->mii_media_active); 353a043e8c7SAdrian Chadd LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 354a043e8c7SAdrian Chadd if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != miisc->mii_inst) 355a043e8c7SAdrian Chadd continue; 356a043e8c7SAdrian Chadd mii_phy_update(miisc, MII_POLLSTAT); 357a043e8c7SAdrian Chadd } 358a043e8c7SAdrian Chadd } 359a043e8c7SAdrian Chadd } 360a043e8c7SAdrian Chadd 361a043e8c7SAdrian Chadd static void 362a043e8c7SAdrian Chadd rtl8366rb_tick(void *arg) 363a043e8c7SAdrian Chadd { 364477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 365477e3effSMichael Zhilin 366477e3effSMichael Zhilin sc = arg; 367a043e8c7SAdrian Chadd 368a043e8c7SAdrian Chadd rtl833rb_miipollstat(sc); 369a043e8c7SAdrian Chadd callout_reset(&sc->callout_tick, hz, rtl8366rb_tick, sc); 370a043e8c7SAdrian Chadd } 371a043e8c7SAdrian Chadd 372a043e8c7SAdrian Chadd static int 373a043e8c7SAdrian Chadd smi_probe(device_t dev) 374a043e8c7SAdrian Chadd { 375477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 376a043e8c7SAdrian Chadd device_t iicbus, iicha; 377477e3effSMichael Zhilin int err, i, j; 378a043e8c7SAdrian Chadd uint16_t chipid; 379a043e8c7SAdrian Chadd char bytes[2]; 380a043e8c7SAdrian Chadd int xferd; 381a043e8c7SAdrian Chadd 382477e3effSMichael Zhilin sc = device_get_softc(dev); 383477e3effSMichael Zhilin 384a043e8c7SAdrian Chadd iicbus = device_get_parent(dev); 385a043e8c7SAdrian Chadd iicha = device_get_parent(iicbus); 386477e3effSMichael Zhilin 387477e3effSMichael Zhilin for (i = 0; i < 2; ++i) { 388477e3effSMichael Zhilin iicbus_reset(iicbus, IIC_FASTEST, RTL8366_IIC_ADDR, NULL); 389477e3effSMichael Zhilin for (j=3; j--; ) { 390a043e8c7SAdrian Chadd IICBUS_STOP(iicha); 391a043e8c7SAdrian Chadd /* 392a043e8c7SAdrian Chadd * we go directly to the host adapter because iicbus.c 393a043e8c7SAdrian Chadd * only issues a stop on a bus that was successfully started. 394a043e8c7SAdrian Chadd */ 395a043e8c7SAdrian Chadd } 396a043e8c7SAdrian Chadd err = iicbus_request_bus(iicbus, dev, IIC_WAIT); 397a043e8c7SAdrian Chadd if (err != 0) 398a043e8c7SAdrian Chadd goto out; 399477e3effSMichael Zhilin err = iicbus_start(iicbus, RTL8366_IIC_ADDR | RTL_IICBUS_READ, RTL_IICBUS_TIMEOUT); 400a043e8c7SAdrian Chadd if (err != 0) 401a043e8c7SAdrian Chadd goto out; 402477e3effSMichael Zhilin if (i == 0) { 403477e3effSMichael Zhilin bytes[0] = RTL8366RB_CIR & 0xff; 404477e3effSMichael Zhilin bytes[1] = (RTL8366RB_CIR >> 8) & 0xff; 405477e3effSMichael Zhilin } else { 406477e3effSMichael Zhilin bytes[0] = RTL8366SR_CIR & 0xff; 407477e3effSMichael Zhilin bytes[1] = (RTL8366SR_CIR >> 8) & 0xff; 408477e3effSMichael Zhilin } 409a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT); 410a043e8c7SAdrian Chadd if (err != 0) 411a043e8c7SAdrian Chadd goto out; 412a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0); 413a043e8c7SAdrian Chadd if (err != 0) 414a043e8c7SAdrian Chadd goto out; 415a043e8c7SAdrian Chadd chipid = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff); 416477e3effSMichael Zhilin if (i == 0 && chipid == RTL8366RB_CIR_ID8366RB) { 417a043e8c7SAdrian Chadd DPRINTF(dev, "chip id 0x%04x\n", chipid); 4185a4380b5SMichael Zhilin sc->chip_type = RTL8366RB; 419477e3effSMichael Zhilin err = 0; 420477e3effSMichael Zhilin break; 421477e3effSMichael Zhilin } 422477e3effSMichael Zhilin if (i == 1 && chipid == RTL8366SR_CIR_ID8366SR) { 423477e3effSMichael Zhilin DPRINTF(dev, "chip id 0x%04x\n", chipid); 4245a4380b5SMichael Zhilin sc->chip_type = RTL8366SR; 425477e3effSMichael Zhilin err = 0; 426477e3effSMichael Zhilin break; 427477e3effSMichael Zhilin } 428477e3effSMichael Zhilin if (i == 0) { 429477e3effSMichael Zhilin iicbus_stop(iicbus); 430477e3effSMichael Zhilin iicbus_release_bus(iicbus, dev); 431477e3effSMichael Zhilin } 432477e3effSMichael Zhilin } 433477e3effSMichael Zhilin if (i == 2) 434a043e8c7SAdrian Chadd err = ENXIO; 435a043e8c7SAdrian Chadd out: 436a043e8c7SAdrian Chadd iicbus_stop(iicbus); 437a043e8c7SAdrian Chadd iicbus_release_bus(iicbus, dev); 438a043e8c7SAdrian Chadd return (err == 0 ? 0 : ENXIO); 439a043e8c7SAdrian Chadd } 440a043e8c7SAdrian Chadd 441a043e8c7SAdrian Chadd static int 442a043e8c7SAdrian Chadd smi_acquire(struct rtl8366rb_softc *sc, int sleep) 443a043e8c7SAdrian Chadd { 444a043e8c7SAdrian Chadd int r = 0; 445a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) 446a043e8c7SAdrian Chadd RTL_LOCK(sc); 447a043e8c7SAdrian Chadd else 448a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0) 449a043e8c7SAdrian Chadd return (EWOULDBLOCK); 450a043e8c7SAdrian Chadd if (sc->smi_acquired == RTL_SMI_ACQUIRED) 451a043e8c7SAdrian Chadd r = EBUSY; 452a043e8c7SAdrian Chadd else { 453a043e8c7SAdrian Chadd r = iicbus_request_bus(device_get_parent(sc->dev), sc->dev, \ 454a043e8c7SAdrian Chadd sleep == RTL_WAITOK ? IIC_WAIT : IIC_DONTWAIT); 455a043e8c7SAdrian Chadd if (r == 0) 456a043e8c7SAdrian Chadd sc->smi_acquired = RTL_SMI_ACQUIRED; 457a043e8c7SAdrian Chadd } 458a043e8c7SAdrian Chadd RTL_UNLOCK(sc); 459a043e8c7SAdrian Chadd return (r); 460a043e8c7SAdrian Chadd } 461a043e8c7SAdrian Chadd 462a043e8c7SAdrian Chadd static int 463a043e8c7SAdrian Chadd smi_release(struct rtl8366rb_softc *sc, int sleep) 464a043e8c7SAdrian Chadd { 465a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) 466a043e8c7SAdrian Chadd RTL_LOCK(sc); 467a043e8c7SAdrian Chadd else 468a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0) 469a043e8c7SAdrian Chadd return (EWOULDBLOCK); 470a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 471a043e8c7SAdrian Chadd iicbus_release_bus(device_get_parent(sc->dev), sc->dev); 472a043e8c7SAdrian Chadd sc->smi_acquired = 0; 473a043e8c7SAdrian Chadd RTL_UNLOCK(sc); 474a043e8c7SAdrian Chadd return (0); 475a043e8c7SAdrian Chadd } 476a043e8c7SAdrian Chadd 477a043e8c7SAdrian Chadd static int 478a043e8c7SAdrian Chadd smi_select(device_t dev, int op, int sleep) 479a043e8c7SAdrian Chadd { 480477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 481a043e8c7SAdrian Chadd int err, i; 482477e3effSMichael Zhilin device_t iicbus; 483477e3effSMichael Zhilin struct iicbus_ivar *devi; 484477e3effSMichael Zhilin int slave; 485477e3effSMichael Zhilin 486477e3effSMichael Zhilin sc = device_get_softc(dev); 487477e3effSMichael Zhilin 488477e3effSMichael Zhilin iicbus = device_get_parent(dev); 489477e3effSMichael Zhilin devi = IICBUS_IVAR(dev); 490477e3effSMichael Zhilin slave = devi->addr; 491a043e8c7SAdrian Chadd 492a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT((struct rtl8366rb_softc *)device_get_softc(dev)); 493477e3effSMichael Zhilin 4945a4380b5SMichael Zhilin if (sc->chip_type == RTL8366SR) { // RTL8366SR work around 495477e3effSMichael Zhilin // this is same work around at probe 496477e3effSMichael Zhilin for (int i=3; i--; ) 497477e3effSMichael Zhilin IICBUS_STOP(device_get_parent(device_get_parent(dev))); 498477e3effSMichael Zhilin } 499a043e8c7SAdrian Chadd /* 500a043e8c7SAdrian Chadd * The chip does not use clock stretching when it is busy, 501a043e8c7SAdrian Chadd * instead ignoring the command. Retry a few times. 502a043e8c7SAdrian Chadd */ 503a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 504a043e8c7SAdrian Chadd err = iicbus_start(iicbus, slave | op, RTL_IICBUS_TIMEOUT); 505a043e8c7SAdrian Chadd if (err != IIC_ENOACK) 506a043e8c7SAdrian Chadd break; 507a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) { 508a043e8c7SAdrian Chadd DEBUG_INCRVAR(iic_select_retries); 509a043e8c7SAdrian Chadd pause("smi_select", RTL_IICBUS_RETRY_SLEEP); 510a043e8c7SAdrian Chadd } else 511a043e8c7SAdrian Chadd break; 512a043e8c7SAdrian Chadd } 513a043e8c7SAdrian Chadd return (err); 514a043e8c7SAdrian Chadd } 515a043e8c7SAdrian Chadd 516a043e8c7SAdrian Chadd static int 517a043e8c7SAdrian Chadd smi_read_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t *data, int sleep) 518a043e8c7SAdrian Chadd { 519a043e8c7SAdrian Chadd int err; 520477e3effSMichael Zhilin device_t iicbus; 521a043e8c7SAdrian Chadd char bytes[2]; 522a043e8c7SAdrian Chadd int xferd; 523a043e8c7SAdrian Chadd 524477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev); 525477e3effSMichael Zhilin 526a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 527a043e8c7SAdrian Chadd bytes[0] = addr & 0xff; 528a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff; 529a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_READ, sleep); 530a043e8c7SAdrian Chadd if (err != 0) 531a043e8c7SAdrian Chadd goto out; 532a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT); 533a043e8c7SAdrian Chadd if (err != 0) 534a043e8c7SAdrian Chadd goto out; 535a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0); 536a043e8c7SAdrian Chadd if (err != 0) 537a043e8c7SAdrian Chadd goto out; 538a043e8c7SAdrian Chadd *data = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff); 539a043e8c7SAdrian Chadd 540a043e8c7SAdrian Chadd out: 541a043e8c7SAdrian Chadd iicbus_stop(iicbus); 542a043e8c7SAdrian Chadd return (err); 543a043e8c7SAdrian Chadd } 544a043e8c7SAdrian Chadd 545a043e8c7SAdrian Chadd static int 546a043e8c7SAdrian Chadd smi_write_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t data, int sleep) 547a043e8c7SAdrian Chadd { 548a043e8c7SAdrian Chadd int err; 549477e3effSMichael Zhilin device_t iicbus; 550a043e8c7SAdrian Chadd char bytes[4]; 551a043e8c7SAdrian Chadd int xferd; 552a043e8c7SAdrian Chadd 553477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev); 554477e3effSMichael Zhilin 555a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 556a043e8c7SAdrian Chadd bytes[0] = addr & 0xff; 557a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff; 558a043e8c7SAdrian Chadd bytes[2] = data & 0xff; 559a043e8c7SAdrian Chadd bytes[3] = (data >> 8) & 0xff; 560a043e8c7SAdrian Chadd 561a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_WRITE, sleep); 562a043e8c7SAdrian Chadd if (err == 0) 563a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 4, &xferd, RTL_IICBUS_TIMEOUT); 564a043e8c7SAdrian Chadd iicbus_stop(iicbus); 565a043e8c7SAdrian Chadd 566a043e8c7SAdrian Chadd return (err); 567a043e8c7SAdrian Chadd } 568a043e8c7SAdrian Chadd 569a043e8c7SAdrian Chadd static int 570a043e8c7SAdrian Chadd smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep) 571a043e8c7SAdrian Chadd { 572477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 573a043e8c7SAdrian Chadd int err; 574a043e8c7SAdrian Chadd 575477e3effSMichael Zhilin sc = device_get_softc(dev); 576477e3effSMichael Zhilin 577a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 578a043e8c7SAdrian Chadd if (err != 0) 579a043e8c7SAdrian Chadd return (EBUSY); 580a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, data, sleep); 581a043e8c7SAdrian Chadd smi_release(sc, sleep); 582a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_read()=%d: addr=%04x\n", addr); 583a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 584a043e8c7SAdrian Chadd } 585a043e8c7SAdrian Chadd 586a043e8c7SAdrian Chadd static int 587a043e8c7SAdrian Chadd smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep) 588a043e8c7SAdrian Chadd { 589477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 590a043e8c7SAdrian Chadd int err; 591a043e8c7SAdrian Chadd 592477e3effSMichael Zhilin sc = device_get_softc(dev); 593477e3effSMichael Zhilin 594a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 595a043e8c7SAdrian Chadd if (err != 0) 596a043e8c7SAdrian Chadd return (EBUSY); 597a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, data, sleep); 598a043e8c7SAdrian Chadd smi_release(sc, sleep); 599a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_write()=%d: addr=%04x\n", addr); 600a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 601a043e8c7SAdrian Chadd } 602a043e8c7SAdrian Chadd 603a043e8c7SAdrian Chadd static int 604a043e8c7SAdrian Chadd smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep) 605a043e8c7SAdrian Chadd { 606477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 607a043e8c7SAdrian Chadd int err; 608a043e8c7SAdrian Chadd uint16_t oldv, newv; 609a043e8c7SAdrian Chadd 610477e3effSMichael Zhilin sc = device_get_softc(dev); 611477e3effSMichael Zhilin 612a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 613a043e8c7SAdrian Chadd if (err != 0) 614a043e8c7SAdrian Chadd return (EBUSY); 615a043e8c7SAdrian Chadd if (err == 0) { 616a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, &oldv, sleep); 617a043e8c7SAdrian Chadd if (err == 0) { 618a043e8c7SAdrian Chadd newv = oldv & ~mask; 619a043e8c7SAdrian Chadd newv |= data & mask; 620a043e8c7SAdrian Chadd if (newv != oldv) 621a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, newv, sleep); 622a043e8c7SAdrian Chadd } 623a043e8c7SAdrian Chadd } 624a043e8c7SAdrian Chadd smi_release(sc, sleep); 625a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_rmw()=%d: addr=%04x\n", addr); 626a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 627a043e8c7SAdrian Chadd } 628a043e8c7SAdrian Chadd 629a043e8c7SAdrian Chadd static etherswitch_info_t * 630a043e8c7SAdrian Chadd rtl_getinfo(device_t dev) 631a043e8c7SAdrian Chadd { 632477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 633477e3effSMichael Zhilin 634477e3effSMichael Zhilin sc = device_get_softc(dev); 635477e3effSMichael Zhilin 636477e3effSMichael Zhilin return (&sc->info); 637a043e8c7SAdrian Chadd } 638a043e8c7SAdrian Chadd 639a043e8c7SAdrian Chadd static int 640a043e8c7SAdrian Chadd rtl_readreg(device_t dev, int reg) 641a043e8c7SAdrian Chadd { 642477e3effSMichael Zhilin uint16_t data; 643477e3effSMichael Zhilin 644477e3effSMichael Zhilin data = 0; 645a043e8c7SAdrian Chadd 646a043e8c7SAdrian Chadd smi_read(dev, reg, &data, RTL_WAITOK); 647a043e8c7SAdrian Chadd return (data); 648a043e8c7SAdrian Chadd } 649a043e8c7SAdrian Chadd 650a043e8c7SAdrian Chadd static int 651a043e8c7SAdrian Chadd rtl_writereg(device_t dev, int reg, int value) 652a043e8c7SAdrian Chadd { 653a043e8c7SAdrian Chadd return (smi_write(dev, reg, value, RTL_WAITOK)); 654a043e8c7SAdrian Chadd } 655a043e8c7SAdrian Chadd 656a043e8c7SAdrian Chadd static int 657a043e8c7SAdrian Chadd rtl_getport(device_t dev, etherswitch_port_t *p) 658a043e8c7SAdrian Chadd { 659a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 660a043e8c7SAdrian Chadd struct ifmedia *ifm; 661a043e8c7SAdrian Chadd struct mii_data *mii; 662477e3effSMichael Zhilin struct ifmediareq *ifmr; 663a043e8c7SAdrian Chadd uint16_t v; 664a3219359SAdrian Chadd int err, vlangroup; 665a043e8c7SAdrian Chadd 666a3219359SAdrian Chadd sc = device_get_softc(dev); 667477e3effSMichael Zhilin 668477e3effSMichael Zhilin ifmr = &p->es_ifmr; 669477e3effSMichael Zhilin 6705a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1)) 671477e3effSMichael Zhilin return (ENXIO); 6725a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) { 6735a4380b5SMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port + 1, 6745a4380b5SMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port + 1))); 6755a4380b5SMichael Zhilin } else { 676477e3effSMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port, 677477e3effSMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port))); 6785a4380b5SMichael Zhilin } 679bfae9329SLuiz Otavio O Souza p->es_pvid = sc->vid[vlangroup] & ETHERSWITCH_VID_MASK; 680a043e8c7SAdrian Chadd 6815a4380b5SMichael Zhilin if (p->es_port < sc->numphys) { 682a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]); 683a043e8c7SAdrian Chadd ifm = &mii->mii_media; 684a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCGIFMEDIA); 685a043e8c7SAdrian Chadd if (err) 686a043e8c7SAdrian Chadd return (err); 687a043e8c7SAdrian Chadd } else { 688a043e8c7SAdrian Chadd /* fill in fixed values for CPU port */ 689dddab089SLuiz Otavio O Souza p->es_flags |= ETHERSWITCH_PORT_CPU; 690477e3effSMichael Zhilin smi_read(dev, RTL8366_PLSR_BASE + (RTL8366_NUM_PHYS)/2, &v, RTL_WAITOK); 691477e3effSMichael Zhilin v = v >> (8 * ((RTL8366_NUM_PHYS) % 2)); 692a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(v, &ifmr->ifm_status, &ifmr->ifm_active); 693a043e8c7SAdrian Chadd ifmr->ifm_current = ifmr->ifm_active; 694a043e8c7SAdrian Chadd ifmr->ifm_mask = 0; 695a043e8c7SAdrian Chadd ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 69628b07d23SLuiz Otavio O Souza /* Return our static media list. */ 69728b07d23SLuiz Otavio O Souza if (ifmr->ifm_count > 0) { 69828b07d23SLuiz Otavio O Souza ifmr->ifm_count = 1; 69928b07d23SLuiz Otavio O Souza ifmr->ifm_ulist[0] = IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 70028b07d23SLuiz Otavio O Souza IFM_FDX, 0); 70128b07d23SLuiz Otavio O Souza } else 70228b07d23SLuiz Otavio O Souza ifmr->ifm_count = 0; 703a043e8c7SAdrian Chadd } 704a043e8c7SAdrian Chadd return (0); 705a043e8c7SAdrian Chadd } 706a043e8c7SAdrian Chadd 707a043e8c7SAdrian Chadd static int 708a043e8c7SAdrian Chadd rtl_setport(device_t dev, etherswitch_port_t *p) 709a043e8c7SAdrian Chadd { 710a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 711477e3effSMichael Zhilin int i, err, vlangroup; 712a043e8c7SAdrian Chadd struct ifmedia *ifm; 713a043e8c7SAdrian Chadd struct mii_data *mii; 7145a4380b5SMichael Zhilin int port; 715a043e8c7SAdrian Chadd 716a3219359SAdrian Chadd sc = device_get_softc(dev); 717477e3effSMichael Zhilin 7185a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1)) 719477e3effSMichael Zhilin return (ENXIO); 720a3219359SAdrian Chadd vlangroup = -1; 721477e3effSMichael Zhilin for (i = 0; i < RTL8366_NUM_VLANS; i++) { 722bfae9329SLuiz Otavio O Souza if ((sc->vid[i] & ETHERSWITCH_VID_MASK) == p->es_pvid) { 723a3219359SAdrian Chadd vlangroup = i; 724a3219359SAdrian Chadd break; 725a3219359SAdrian Chadd } 726a3219359SAdrian Chadd } 727a3219359SAdrian Chadd if (vlangroup == -1) 728a3219359SAdrian Chadd return (ENXIO); 7295a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) { 7305a4380b5SMichael Zhilin port = p->es_port + 1; 7315a4380b5SMichael Zhilin } else { 7325a4380b5SMichael Zhilin port = p->es_port; 7335a4380b5SMichael Zhilin } 7345a4380b5SMichael Zhilin err = smi_rmw(dev, RTL8366_PVCR_REG(port), 7355a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, RTL8366_PVCR_PORT_MASK), 7365a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, vlangroup), RTL_WAITOK); 737a043e8c7SAdrian Chadd if (err) 738a043e8c7SAdrian Chadd return (err); 7395a4380b5SMichael Zhilin /* CPU Port */ 7405a4380b5SMichael Zhilin if (p->es_port == sc->numphys) 741dddab089SLuiz Otavio O Souza return (0); 742a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]); 743a043e8c7SAdrian Chadd ifm = &mii->mii_media; 744a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCSIFMEDIA); 745a043e8c7SAdrian Chadd return (err); 746a043e8c7SAdrian Chadd } 747a043e8c7SAdrian Chadd 748a043e8c7SAdrian Chadd static int 749a043e8c7SAdrian Chadd rtl_getvgroup(device_t dev, etherswitch_vlangroup_t *vg) 750a043e8c7SAdrian Chadd { 751bfae9329SLuiz Otavio O Souza struct rtl8366rb_softc *sc; 752a043e8c7SAdrian Chadd uint16_t vmcr[3]; 753a043e8c7SAdrian Chadd int i; 7545a4380b5SMichael Zhilin int member, untagged; 755a043e8c7SAdrian Chadd 756bfae9329SLuiz Otavio O Souza sc = device_get_softc(dev); 757477e3effSMichael Zhilin 758477e3effSMichael Zhilin for (i=0; i<RTL8366_VMCR_MULT; i++) 759477e3effSMichael Zhilin vmcr[i] = rtl_readreg(dev, RTL8366_VMCR(i, vg->es_vlangroup)); 760477e3effSMichael Zhilin 761bfae9329SLuiz Otavio O Souza vg->es_vid = sc->vid[vg->es_vlangroup]; 7625a4380b5SMichael Zhilin member = RTL8366_VMCR_MEMBER(vmcr); 7635a4380b5SMichael Zhilin untagged = RTL8366_VMCR_UNTAG(vmcr); 7645a4380b5SMichael Zhilin if (sc->phy4cpu) { 7655a4380b5SMichael Zhilin vg->es_member_ports = ((member & 0x20) >> 1) | (member & 0x0f); 7665a4380b5SMichael Zhilin vg->es_untagged_ports = ((untagged & 0x20) >> 1) | (untagged & 0x0f); 7675a4380b5SMichael Zhilin } else { 7685a4380b5SMichael Zhilin vg->es_member_ports = member; 7695a4380b5SMichael Zhilin vg->es_untagged_ports = untagged; 7705a4380b5SMichael Zhilin } 771477e3effSMichael Zhilin vg->es_fid = RTL8366_VMCR_FID(vmcr); 772a043e8c7SAdrian Chadd return (0); 773a043e8c7SAdrian Chadd } 774a043e8c7SAdrian Chadd 775a043e8c7SAdrian Chadd static int 776a043e8c7SAdrian Chadd rtl_setvgroup(device_t dev, etherswitch_vlangroup_t *vg) 777a043e8c7SAdrian Chadd { 778a3219359SAdrian Chadd struct rtl8366rb_softc *sc; 779477e3effSMichael Zhilin int g; 7805a4380b5SMichael Zhilin int member, untagged; 781a043e8c7SAdrian Chadd 782a3219359SAdrian Chadd sc = device_get_softc(dev); 783477e3effSMichael Zhilin 784477e3effSMichael Zhilin g = vg->es_vlangroup; 785477e3effSMichael Zhilin 786a3219359SAdrian Chadd sc->vid[g] = vg->es_vid; 787bfae9329SLuiz Otavio O Souza /* VLAN group disabled ? */ 788bfae9329SLuiz Otavio O Souza if (vg->es_member_ports == 0 && vg->es_untagged_ports == 0 && vg->es_vid == 0) 789bfae9329SLuiz Otavio O Souza return (0); 790bfae9329SLuiz Otavio O Souza sc->vid[g] |= ETHERSWITCH_VID_VALID; 791477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_DOT1Q_REG, g), 792477e3effSMichael Zhilin (vg->es_vid << RTL8366_VMCR_DOT1Q_VID_SHIFT) & RTL8366_VMCR_DOT1Q_VID_MASK); 7935a4380b5SMichael Zhilin if (sc->phy4cpu) { 7945a4380b5SMichael Zhilin /* add space at phy4 */ 7955a4380b5SMichael Zhilin member = (vg->es_member_ports & 0x0f) | 7965a4380b5SMichael Zhilin ((vg->es_member_ports & 0x10) << 1); 7975a4380b5SMichael Zhilin untagged = (vg->es_untagged_ports & 0x0f) | 7985a4380b5SMichael Zhilin ((vg->es_untagged_ports & 0x10) << 1); 7995a4380b5SMichael Zhilin } else { 8005a4380b5SMichael Zhilin member = vg->es_member_ports; 8015a4380b5SMichael Zhilin untagged = vg->es_untagged_ports; 8025a4380b5SMichael Zhilin } 8035a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) { 804477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g), 8055a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) | 8065a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK)); 807477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_FID_REG, g), 808a043e8c7SAdrian Chadd vg->es_fid); 809477e3effSMichael Zhilin } else { 810477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g), 8115a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) | 8125a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK) | 813477e3effSMichael Zhilin ((vg->es_fid << RTL8366_VMCR_FID_FID_SHIFT) & RTL8366_VMCR_FID_FID_MASK)); 814477e3effSMichael Zhilin } 815a043e8c7SAdrian Chadd return (0); 816a043e8c7SAdrian Chadd } 817a043e8c7SAdrian Chadd 818a043e8c7SAdrian Chadd static int 819bfae9329SLuiz Otavio O Souza rtl_getconf(device_t dev, etherswitch_conf_t *conf) 820bfae9329SLuiz Otavio O Souza { 821bfae9329SLuiz Otavio O Souza 822bfae9329SLuiz Otavio O Souza /* Return the VLAN mode. */ 823bfae9329SLuiz Otavio O Souza conf->cmd = ETHERSWITCH_CONF_VLAN_MODE; 824bfae9329SLuiz Otavio O Souza conf->vlan_mode = ETHERSWITCH_VLAN_DOT1Q; 825bfae9329SLuiz Otavio O Souza 826bfae9329SLuiz Otavio O Souza return (0); 827bfae9329SLuiz Otavio O Souza } 828bfae9329SLuiz Otavio O Souza 829bfae9329SLuiz Otavio O Souza static int 830a043e8c7SAdrian Chadd rtl_readphy(device_t dev, int phy, int reg) 831a043e8c7SAdrian Chadd { 832477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 833477e3effSMichael Zhilin uint16_t data; 834a043e8c7SAdrian Chadd int err, i, sleep; 835a043e8c7SAdrian Chadd 836477e3effSMichael Zhilin sc = device_get_softc(dev); 837477e3effSMichael Zhilin 838477e3effSMichael Zhilin data = 0; 839477e3effSMichael Zhilin 840477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS) 841a043e8c7SAdrian Chadd return (ENXIO); 842477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG) 843a043e8c7SAdrian Chadd return (ENXIO); 844a043e8c7SAdrian Chadd sleep = RTL_WAITOK; 845a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 846a043e8c7SAdrian Chadd if (err != 0) 847a043e8c7SAdrian Chadd return (EBUSY); 848a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 849477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_READ, sleep); 850a043e8c7SAdrian Chadd if (err == 0) 851477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), 0, sleep); 852a043e8c7SAdrian Chadd if (err == 0) { 853477e3effSMichael Zhilin err = smi_read_locked(sc, RTL8366_PADR, &data, sleep); 854a043e8c7SAdrian Chadd break; 855a043e8c7SAdrian Chadd } 856a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries); 857a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_readphy(): chip not responsive, retrying %d more times\n", i); 858a043e8c7SAdrian Chadd pause("rtl_readphy", RTL_IICBUS_RETRY_SLEEP); 859a043e8c7SAdrian Chadd } 860a043e8c7SAdrian Chadd smi_release(sc, sleep); 861a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_readphy()=%d: phy=%d.%02x\n", phy, reg); 862a043e8c7SAdrian Chadd return (data); 863a043e8c7SAdrian Chadd } 864a043e8c7SAdrian Chadd 865a043e8c7SAdrian Chadd static int 866a043e8c7SAdrian Chadd rtl_writephy(device_t dev, int phy, int reg, int data) 867a043e8c7SAdrian Chadd { 868477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 869a043e8c7SAdrian Chadd int err, i, sleep; 870a043e8c7SAdrian Chadd 871477e3effSMichael Zhilin sc = device_get_softc(dev); 872477e3effSMichael Zhilin 873477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS) 874a043e8c7SAdrian Chadd return (ENXIO); 875477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG) 876a043e8c7SAdrian Chadd return (ENXIO); 877a043e8c7SAdrian Chadd sleep = RTL_WAITOK; 878a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 879a043e8c7SAdrian Chadd if (err != 0) 880a043e8c7SAdrian Chadd return (EBUSY); 881a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 882477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_WRITE, sleep); 883a043e8c7SAdrian Chadd if (err == 0) 884477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), data, sleep); 885a043e8c7SAdrian Chadd if (err == 0) { 886a043e8c7SAdrian Chadd break; 887a043e8c7SAdrian Chadd } 888a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries); 889a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_writephy(): chip not responsive, retrying %d more tiems\n", i); 890a043e8c7SAdrian Chadd pause("rtl_writephy", RTL_IICBUS_RETRY_SLEEP); 891a043e8c7SAdrian Chadd } 892a043e8c7SAdrian Chadd smi_release(sc, sleep); 893a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_writephy()=%d: phy=%d.%02x\n", phy, reg); 894a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 895a043e8c7SAdrian Chadd } 896a043e8c7SAdrian Chadd 897a043e8c7SAdrian Chadd static int 898a043e8c7SAdrian Chadd rtl8366rb_ifmedia_upd(struct ifnet *ifp) 899a043e8c7SAdrian Chadd { 900477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 901477e3effSMichael Zhilin struct mii_data *mii; 902477e3effSMichael Zhilin 903477e3effSMichael Zhilin sc = ifp->if_softc; 904477e3effSMichael Zhilin mii = device_get_softc(sc->miibus[ifp->if_dunit]); 905a043e8c7SAdrian Chadd 906a043e8c7SAdrian Chadd mii_mediachg(mii); 907a043e8c7SAdrian Chadd return (0); 908a043e8c7SAdrian Chadd } 909a043e8c7SAdrian Chadd 910a043e8c7SAdrian Chadd static void 911a043e8c7SAdrian Chadd rtl8366rb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 912a043e8c7SAdrian Chadd { 913477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 914477e3effSMichael Zhilin struct mii_data *mii; 915477e3effSMichael Zhilin 916477e3effSMichael Zhilin sc = ifp->if_softc; 917477e3effSMichael Zhilin mii = device_get_softc(sc->miibus[ifp->if_dunit]); 918a043e8c7SAdrian Chadd 919a043e8c7SAdrian Chadd mii_pollstat(mii); 920a043e8c7SAdrian Chadd ifmr->ifm_active = mii->mii_media_active; 921a043e8c7SAdrian Chadd ifmr->ifm_status = mii->mii_media_status; 922a043e8c7SAdrian Chadd } 923a043e8c7SAdrian Chadd 924a043e8c7SAdrian Chadd 925a043e8c7SAdrian Chadd static device_method_t rtl8366rb_methods[] = { 926a043e8c7SAdrian Chadd /* Device interface */ 927a043e8c7SAdrian Chadd DEVMETHOD(device_identify, rtl8366rb_identify), 928a043e8c7SAdrian Chadd DEVMETHOD(device_probe, rtl8366rb_probe), 929a043e8c7SAdrian Chadd DEVMETHOD(device_attach, rtl8366rb_attach), 930a043e8c7SAdrian Chadd DEVMETHOD(device_detach, rtl8366rb_detach), 931a043e8c7SAdrian Chadd 932a043e8c7SAdrian Chadd /* bus interface */ 933a043e8c7SAdrian Chadd DEVMETHOD(bus_add_child, device_add_child_ordered), 934a043e8c7SAdrian Chadd 935a043e8c7SAdrian Chadd /* MII interface */ 936a043e8c7SAdrian Chadd DEVMETHOD(miibus_readreg, rtl_readphy), 937a043e8c7SAdrian Chadd DEVMETHOD(miibus_writereg, rtl_writephy), 938a043e8c7SAdrian Chadd 9395a4380b5SMichael Zhilin /* MDIO interface */ 9405a4380b5SMichael Zhilin DEVMETHOD(mdio_readreg, rtl_readphy), 9415a4380b5SMichael Zhilin DEVMETHOD(mdio_writereg, rtl_writephy), 9425a4380b5SMichael Zhilin 943a043e8c7SAdrian Chadd /* etherswitch interface */ 944bfae9329SLuiz Otavio O Souza DEVMETHOD(etherswitch_getconf, rtl_getconf), 945a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getinfo, rtl_getinfo), 946a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readreg, rtl_readreg), 947a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writereg, rtl_writereg), 948a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readphyreg, rtl_readphy), 949a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writephyreg, rtl_writephy), 950a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getport, rtl_getport), 951a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setport, rtl_setport), 952a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getvgroup, rtl_getvgroup), 953a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setvgroup, rtl_setvgroup), 954a043e8c7SAdrian Chadd 955a043e8c7SAdrian Chadd DEVMETHOD_END 956a043e8c7SAdrian Chadd }; 957a043e8c7SAdrian Chadd 958a043e8c7SAdrian Chadd DEFINE_CLASS_0(rtl8366rb, rtl8366rb_driver, rtl8366rb_methods, 959a043e8c7SAdrian Chadd sizeof(struct rtl8366rb_softc)); 960a043e8c7SAdrian Chadd static devclass_t rtl8366rb_devclass; 961a043e8c7SAdrian Chadd 962a043e8c7SAdrian Chadd DRIVER_MODULE(rtl8366rb, iicbus, rtl8366rb_driver, rtl8366rb_devclass, 0, 0); 9633e38757dSJohn Baldwin DRIVER_MODULE(miibus, rtl8366rb, miibus_driver, 0, 0); 964*8933f7d6SJohn Baldwin DRIVER_MODULE(mdio, rtl8366rb, mdio_driver, 0, 0); 965a043e8c7SAdrian Chadd DRIVER_MODULE(etherswitch, rtl8366rb, etherswitch_driver, etherswitch_devclass, 0, 0); 966a043e8c7SAdrian Chadd MODULE_VERSION(rtl8366rb, 1); 967a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, iicbus, 1, 1, 1); /* XXX which versions? */ 968a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, miibus, 1, 1, 1); /* XXX which versions? */ 969a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, etherswitch, 1, 1, 1); /* XXX which versions? */ 970