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) { 137a05a6804SWarner 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 257723da5d9SJohn Baldwin bus_identify_children(dev); 258a043e8c7SAdrian Chadd bus_enumerate_hinted_children(dev); 259*18250ec6SJohn Baldwin bus_attach_children(dev); 260a043e8c7SAdrian Chadd 261a043e8c7SAdrian Chadd callout_init_mtx(&sc->callout_tick, &sc->callout_mtx, 0); 262a043e8c7SAdrian Chadd rtl8366rb_tick(sc); 263a043e8c7SAdrian Chadd 264a043e8c7SAdrian Chadd return (err); 265a043e8c7SAdrian Chadd } 266a043e8c7SAdrian Chadd 267a043e8c7SAdrian Chadd static int 268a043e8c7SAdrian Chadd rtl8366rb_detach(device_t dev) 269a043e8c7SAdrian Chadd { 270477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 271a043e8c7SAdrian Chadd int i; 272a043e8c7SAdrian Chadd 273477e3effSMichael Zhilin sc = device_get_softc(dev); 274477e3effSMichael Zhilin 2755a4380b5SMichael Zhilin for (i=0; i < sc->numphys; i++) { 276a043e8c7SAdrian Chadd if (sc->miibus[i]) 277a043e8c7SAdrian Chadd device_delete_child(dev, sc->miibus[i]); 278a043e8c7SAdrian Chadd if (sc->ifp[i] != NULL) 279a043e8c7SAdrian Chadd if_free(sc->ifp[i]); 280a043e8c7SAdrian Chadd free(sc->ifname[i], M_DEVBUF); 281a043e8c7SAdrian Chadd } 282a043e8c7SAdrian Chadd bus_generic_detach(dev); 283a043e8c7SAdrian Chadd callout_drain(&sc->callout_tick); 284a043e8c7SAdrian Chadd mtx_destroy(&sc->callout_mtx); 285a043e8c7SAdrian Chadd mtx_destroy(&sc->sc_mtx); 286a043e8c7SAdrian Chadd 287a043e8c7SAdrian Chadd return (0); 288a043e8c7SAdrian Chadd } 289a043e8c7SAdrian Chadd 290a043e8c7SAdrian Chadd static void 291a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active) 292a043e8c7SAdrian Chadd { 293a043e8c7SAdrian Chadd *media_active = IFM_ETHER; 294a043e8c7SAdrian Chadd *media_status = IFM_AVALID; 295477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_LINK) != 0) 296a043e8c7SAdrian Chadd *media_status |= IFM_ACTIVE; 297a043e8c7SAdrian Chadd else { 298a043e8c7SAdrian Chadd *media_active |= IFM_NONE; 299a043e8c7SAdrian Chadd return; 300a043e8c7SAdrian Chadd } 301477e3effSMichael Zhilin switch (portstatus & RTL8366_PLSR_SPEED_MASK) { 302477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_10: 303a043e8c7SAdrian Chadd *media_active |= IFM_10_T; 304a043e8c7SAdrian Chadd break; 305477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_100: 306a043e8c7SAdrian Chadd *media_active |= IFM_100_TX; 307a043e8c7SAdrian Chadd break; 308477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_1000: 309a043e8c7SAdrian Chadd *media_active |= IFM_1000_T; 310a043e8c7SAdrian Chadd break; 311a043e8c7SAdrian Chadd } 312477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_FULLDUPLEX) != 0) 313a043e8c7SAdrian Chadd *media_active |= IFM_FDX; 314a043e8c7SAdrian Chadd else 315a043e8c7SAdrian Chadd *media_active |= IFM_HDX; 316477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_TXPAUSE) != 0) 317a043e8c7SAdrian Chadd *media_active |= IFM_ETH_TXPAUSE; 318477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_RXPAUSE) != 0) 319a043e8c7SAdrian Chadd *media_active |= IFM_ETH_RXPAUSE; 320a043e8c7SAdrian Chadd } 321a043e8c7SAdrian Chadd 322a043e8c7SAdrian Chadd static void 323a043e8c7SAdrian Chadd rtl833rb_miipollstat(struct rtl8366rb_softc *sc) 324a043e8c7SAdrian Chadd { 325a043e8c7SAdrian Chadd int i; 326a043e8c7SAdrian Chadd struct mii_data *mii; 327a043e8c7SAdrian Chadd struct mii_softc *miisc; 328a043e8c7SAdrian Chadd uint16_t value; 329a043e8c7SAdrian Chadd int portstatus; 330a043e8c7SAdrian Chadd 3315a4380b5SMichael Zhilin for (i = 0; i < sc->numphys; i++) { 332a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[i]); 333a043e8c7SAdrian Chadd if ((i % 2) == 0) { 334477e3effSMichael Zhilin if (smi_read(sc->dev, RTL8366_PLSR_BASE + i/2, &value, RTL_NOWAIT) != 0) { 335a043e8c7SAdrian Chadd DEBUG_INCRVAR(callout_blocked); 336a043e8c7SAdrian Chadd return; 337a043e8c7SAdrian Chadd } 338a043e8c7SAdrian Chadd portstatus = value & 0xff; 339a043e8c7SAdrian Chadd } else { 340a043e8c7SAdrian Chadd portstatus = (value >> 8) & 0xff; 341a043e8c7SAdrian Chadd } 342a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(portstatus, &mii->mii_media_status, &mii->mii_media_active); 343a043e8c7SAdrian Chadd LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 344a043e8c7SAdrian Chadd if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != miisc->mii_inst) 345a043e8c7SAdrian Chadd continue; 346a043e8c7SAdrian Chadd mii_phy_update(miisc, MII_POLLSTAT); 347a043e8c7SAdrian Chadd } 348a043e8c7SAdrian Chadd } 349a043e8c7SAdrian Chadd } 350a043e8c7SAdrian Chadd 351a043e8c7SAdrian Chadd static void 352a043e8c7SAdrian Chadd rtl8366rb_tick(void *arg) 353a043e8c7SAdrian Chadd { 354477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 355477e3effSMichael Zhilin 356477e3effSMichael Zhilin sc = arg; 357a043e8c7SAdrian Chadd 358a043e8c7SAdrian Chadd rtl833rb_miipollstat(sc); 359a043e8c7SAdrian Chadd callout_reset(&sc->callout_tick, hz, rtl8366rb_tick, sc); 360a043e8c7SAdrian Chadd } 361a043e8c7SAdrian Chadd 362a043e8c7SAdrian Chadd static int 363a043e8c7SAdrian Chadd smi_probe(device_t dev) 364a043e8c7SAdrian Chadd { 365477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 366a043e8c7SAdrian Chadd device_t iicbus, iicha; 367477e3effSMichael Zhilin int err, i, j; 368a043e8c7SAdrian Chadd uint16_t chipid; 369a043e8c7SAdrian Chadd char bytes[2]; 370a043e8c7SAdrian Chadd int xferd; 371a043e8c7SAdrian Chadd 372477e3effSMichael Zhilin sc = device_get_softc(dev); 373477e3effSMichael Zhilin 374a043e8c7SAdrian Chadd iicbus = device_get_parent(dev); 375a043e8c7SAdrian Chadd iicha = device_get_parent(iicbus); 376477e3effSMichael Zhilin 377477e3effSMichael Zhilin for (i = 0; i < 2; ++i) { 378477e3effSMichael Zhilin iicbus_reset(iicbus, IIC_FASTEST, RTL8366_IIC_ADDR, NULL); 379477e3effSMichael Zhilin for (j=3; j--; ) { 380a043e8c7SAdrian Chadd IICBUS_STOP(iicha); 381a043e8c7SAdrian Chadd /* 382a043e8c7SAdrian Chadd * we go directly to the host adapter because iicbus.c 383a043e8c7SAdrian Chadd * only issues a stop on a bus that was successfully started. 384a043e8c7SAdrian Chadd */ 385a043e8c7SAdrian Chadd } 386a043e8c7SAdrian Chadd err = iicbus_request_bus(iicbus, dev, IIC_WAIT); 387a043e8c7SAdrian Chadd if (err != 0) 388a043e8c7SAdrian Chadd goto out; 389477e3effSMichael Zhilin err = iicbus_start(iicbus, RTL8366_IIC_ADDR | RTL_IICBUS_READ, RTL_IICBUS_TIMEOUT); 390a043e8c7SAdrian Chadd if (err != 0) 391a043e8c7SAdrian Chadd goto out; 392477e3effSMichael Zhilin if (i == 0) { 393477e3effSMichael Zhilin bytes[0] = RTL8366RB_CIR & 0xff; 394477e3effSMichael Zhilin bytes[1] = (RTL8366RB_CIR >> 8) & 0xff; 395477e3effSMichael Zhilin } else { 396477e3effSMichael Zhilin bytes[0] = RTL8366SR_CIR & 0xff; 397477e3effSMichael Zhilin bytes[1] = (RTL8366SR_CIR >> 8) & 0xff; 398477e3effSMichael Zhilin } 399a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT); 400a043e8c7SAdrian Chadd if (err != 0) 401a043e8c7SAdrian Chadd goto out; 402a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0); 403a043e8c7SAdrian Chadd if (err != 0) 404a043e8c7SAdrian Chadd goto out; 405a043e8c7SAdrian Chadd chipid = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff); 406477e3effSMichael Zhilin if (i == 0 && chipid == RTL8366RB_CIR_ID8366RB) { 407a043e8c7SAdrian Chadd DPRINTF(dev, "chip id 0x%04x\n", chipid); 4085a4380b5SMichael Zhilin sc->chip_type = RTL8366RB; 409477e3effSMichael Zhilin err = 0; 410477e3effSMichael Zhilin break; 411477e3effSMichael Zhilin } 412477e3effSMichael Zhilin if (i == 1 && chipid == RTL8366SR_CIR_ID8366SR) { 413477e3effSMichael Zhilin DPRINTF(dev, "chip id 0x%04x\n", chipid); 4145a4380b5SMichael Zhilin sc->chip_type = RTL8366SR; 415477e3effSMichael Zhilin err = 0; 416477e3effSMichael Zhilin break; 417477e3effSMichael Zhilin } 418477e3effSMichael Zhilin if (i == 0) { 419477e3effSMichael Zhilin iicbus_stop(iicbus); 420477e3effSMichael Zhilin iicbus_release_bus(iicbus, dev); 421477e3effSMichael Zhilin } 422477e3effSMichael Zhilin } 423477e3effSMichael Zhilin if (i == 2) 424a043e8c7SAdrian Chadd err = ENXIO; 425a043e8c7SAdrian Chadd out: 426a043e8c7SAdrian Chadd iicbus_stop(iicbus); 427a043e8c7SAdrian Chadd iicbus_release_bus(iicbus, dev); 428a043e8c7SAdrian Chadd return (err == 0 ? 0 : ENXIO); 429a043e8c7SAdrian Chadd } 430a043e8c7SAdrian Chadd 431a043e8c7SAdrian Chadd static int 432a043e8c7SAdrian Chadd smi_acquire(struct rtl8366rb_softc *sc, int sleep) 433a043e8c7SAdrian Chadd { 434a043e8c7SAdrian Chadd int r = 0; 435a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) 436a043e8c7SAdrian Chadd RTL_LOCK(sc); 437a043e8c7SAdrian Chadd else 438a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0) 439a043e8c7SAdrian Chadd return (EWOULDBLOCK); 440a043e8c7SAdrian Chadd if (sc->smi_acquired == RTL_SMI_ACQUIRED) 441a043e8c7SAdrian Chadd r = EBUSY; 442a043e8c7SAdrian Chadd else { 443a043e8c7SAdrian Chadd r = iicbus_request_bus(device_get_parent(sc->dev), sc->dev, \ 444a043e8c7SAdrian Chadd sleep == RTL_WAITOK ? IIC_WAIT : IIC_DONTWAIT); 445a043e8c7SAdrian Chadd if (r == 0) 446a043e8c7SAdrian Chadd sc->smi_acquired = RTL_SMI_ACQUIRED; 447a043e8c7SAdrian Chadd } 448a043e8c7SAdrian Chadd RTL_UNLOCK(sc); 449a043e8c7SAdrian Chadd return (r); 450a043e8c7SAdrian Chadd } 451a043e8c7SAdrian Chadd 452a043e8c7SAdrian Chadd static int 453a043e8c7SAdrian Chadd smi_release(struct rtl8366rb_softc *sc, int sleep) 454a043e8c7SAdrian Chadd { 455a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) 456a043e8c7SAdrian Chadd RTL_LOCK(sc); 457a043e8c7SAdrian Chadd else 458a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0) 459a043e8c7SAdrian Chadd return (EWOULDBLOCK); 460a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 461a043e8c7SAdrian Chadd iicbus_release_bus(device_get_parent(sc->dev), sc->dev); 462a043e8c7SAdrian Chadd sc->smi_acquired = 0; 463a043e8c7SAdrian Chadd RTL_UNLOCK(sc); 464a043e8c7SAdrian Chadd return (0); 465a043e8c7SAdrian Chadd } 466a043e8c7SAdrian Chadd 467a043e8c7SAdrian Chadd static int 468a043e8c7SAdrian Chadd smi_select(device_t dev, int op, int sleep) 469a043e8c7SAdrian Chadd { 470477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 471a043e8c7SAdrian Chadd int err, i; 472477e3effSMichael Zhilin device_t iicbus; 473477e3effSMichael Zhilin struct iicbus_ivar *devi; 474477e3effSMichael Zhilin int slave; 475477e3effSMichael Zhilin 476477e3effSMichael Zhilin sc = device_get_softc(dev); 477477e3effSMichael Zhilin 478477e3effSMichael Zhilin iicbus = device_get_parent(dev); 479477e3effSMichael Zhilin devi = IICBUS_IVAR(dev); 480477e3effSMichael Zhilin slave = devi->addr; 481a043e8c7SAdrian Chadd 482a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT((struct rtl8366rb_softc *)device_get_softc(dev)); 483477e3effSMichael Zhilin 4845a4380b5SMichael Zhilin if (sc->chip_type == RTL8366SR) { // RTL8366SR work around 485477e3effSMichael Zhilin // this is same work around at probe 486477e3effSMichael Zhilin for (int i=3; i--; ) 487477e3effSMichael Zhilin IICBUS_STOP(device_get_parent(device_get_parent(dev))); 488477e3effSMichael Zhilin } 489a043e8c7SAdrian Chadd /* 490a043e8c7SAdrian Chadd * The chip does not use clock stretching when it is busy, 491a043e8c7SAdrian Chadd * instead ignoring the command. Retry a few times. 492a043e8c7SAdrian Chadd */ 493a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 494a043e8c7SAdrian Chadd err = iicbus_start(iicbus, slave | op, RTL_IICBUS_TIMEOUT); 495a043e8c7SAdrian Chadd if (err != IIC_ENOACK) 496a043e8c7SAdrian Chadd break; 497a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) { 498a043e8c7SAdrian Chadd DEBUG_INCRVAR(iic_select_retries); 499a043e8c7SAdrian Chadd pause("smi_select", RTL_IICBUS_RETRY_SLEEP); 500a043e8c7SAdrian Chadd } else 501a043e8c7SAdrian Chadd break; 502a043e8c7SAdrian Chadd } 503a043e8c7SAdrian Chadd return (err); 504a043e8c7SAdrian Chadd } 505a043e8c7SAdrian Chadd 506a043e8c7SAdrian Chadd static int 507a043e8c7SAdrian Chadd smi_read_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t *data, int sleep) 508a043e8c7SAdrian Chadd { 509a043e8c7SAdrian Chadd int err; 510477e3effSMichael Zhilin device_t iicbus; 511a043e8c7SAdrian Chadd char bytes[2]; 512a043e8c7SAdrian Chadd int xferd; 513a043e8c7SAdrian Chadd 514477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev); 515477e3effSMichael Zhilin 516a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 517a043e8c7SAdrian Chadd bytes[0] = addr & 0xff; 518a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff; 519a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_READ, sleep); 520a043e8c7SAdrian Chadd if (err != 0) 521a043e8c7SAdrian Chadd goto out; 522a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT); 523a043e8c7SAdrian Chadd if (err != 0) 524a043e8c7SAdrian Chadd goto out; 525a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0); 526a043e8c7SAdrian Chadd if (err != 0) 527a043e8c7SAdrian Chadd goto out; 528a043e8c7SAdrian Chadd *data = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff); 529a043e8c7SAdrian Chadd 530a043e8c7SAdrian Chadd out: 531a043e8c7SAdrian Chadd iicbus_stop(iicbus); 532a043e8c7SAdrian Chadd return (err); 533a043e8c7SAdrian Chadd } 534a043e8c7SAdrian Chadd 535a043e8c7SAdrian Chadd static int 536a043e8c7SAdrian Chadd smi_write_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t data, int sleep) 537a043e8c7SAdrian Chadd { 538a043e8c7SAdrian Chadd int err; 539477e3effSMichael Zhilin device_t iicbus; 540a043e8c7SAdrian Chadd char bytes[4]; 541a043e8c7SAdrian Chadd int xferd; 542a043e8c7SAdrian Chadd 543477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev); 544477e3effSMichael Zhilin 545a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 546a043e8c7SAdrian Chadd bytes[0] = addr & 0xff; 547a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff; 548a043e8c7SAdrian Chadd bytes[2] = data & 0xff; 549a043e8c7SAdrian Chadd bytes[3] = (data >> 8) & 0xff; 550a043e8c7SAdrian Chadd 551a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_WRITE, sleep); 552a043e8c7SAdrian Chadd if (err == 0) 553a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 4, &xferd, RTL_IICBUS_TIMEOUT); 554a043e8c7SAdrian Chadd iicbus_stop(iicbus); 555a043e8c7SAdrian Chadd 556a043e8c7SAdrian Chadd return (err); 557a043e8c7SAdrian Chadd } 558a043e8c7SAdrian Chadd 559a043e8c7SAdrian Chadd static int 560a043e8c7SAdrian Chadd smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep) 561a043e8c7SAdrian Chadd { 562477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 563a043e8c7SAdrian Chadd int err; 564a043e8c7SAdrian Chadd 565477e3effSMichael Zhilin sc = device_get_softc(dev); 566477e3effSMichael Zhilin 567a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 568a043e8c7SAdrian Chadd if (err != 0) 569a043e8c7SAdrian Chadd return (EBUSY); 570a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, data, sleep); 571a043e8c7SAdrian Chadd smi_release(sc, sleep); 572a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_read()=%d: addr=%04x\n", addr); 573a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 574a043e8c7SAdrian Chadd } 575a043e8c7SAdrian Chadd 576a043e8c7SAdrian Chadd static int 577a043e8c7SAdrian Chadd smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep) 578a043e8c7SAdrian Chadd { 579477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 580a043e8c7SAdrian Chadd int err; 581a043e8c7SAdrian Chadd 582477e3effSMichael Zhilin sc = device_get_softc(dev); 583477e3effSMichael Zhilin 584a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 585a043e8c7SAdrian Chadd if (err != 0) 586a043e8c7SAdrian Chadd return (EBUSY); 587a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, data, sleep); 588a043e8c7SAdrian Chadd smi_release(sc, sleep); 589a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_write()=%d: addr=%04x\n", addr); 590a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 591a043e8c7SAdrian Chadd } 592a043e8c7SAdrian Chadd 593a043e8c7SAdrian Chadd static int 594a043e8c7SAdrian Chadd smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep) 595a043e8c7SAdrian Chadd { 596477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 597a043e8c7SAdrian Chadd int err; 598a043e8c7SAdrian Chadd uint16_t oldv, newv; 599a043e8c7SAdrian Chadd 600477e3effSMichael Zhilin sc = device_get_softc(dev); 601477e3effSMichael Zhilin 602a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 603a043e8c7SAdrian Chadd if (err != 0) 604a043e8c7SAdrian Chadd return (EBUSY); 605a043e8c7SAdrian Chadd if (err == 0) { 606a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, &oldv, sleep); 607a043e8c7SAdrian Chadd if (err == 0) { 608a043e8c7SAdrian Chadd newv = oldv & ~mask; 609a043e8c7SAdrian Chadd newv |= data & mask; 610a043e8c7SAdrian Chadd if (newv != oldv) 611a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, newv, sleep); 612a043e8c7SAdrian Chadd } 613a043e8c7SAdrian Chadd } 614a043e8c7SAdrian Chadd smi_release(sc, sleep); 615a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_rmw()=%d: addr=%04x\n", addr); 616a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 617a043e8c7SAdrian Chadd } 618a043e8c7SAdrian Chadd 619a043e8c7SAdrian Chadd static etherswitch_info_t * 620a043e8c7SAdrian Chadd rtl_getinfo(device_t dev) 621a043e8c7SAdrian Chadd { 622477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 623477e3effSMichael Zhilin 624477e3effSMichael Zhilin sc = device_get_softc(dev); 625477e3effSMichael Zhilin 626477e3effSMichael Zhilin return (&sc->info); 627a043e8c7SAdrian Chadd } 628a043e8c7SAdrian Chadd 629a043e8c7SAdrian Chadd static int 630a043e8c7SAdrian Chadd rtl_readreg(device_t dev, int reg) 631a043e8c7SAdrian Chadd { 632477e3effSMichael Zhilin uint16_t data; 633477e3effSMichael Zhilin 634477e3effSMichael Zhilin data = 0; 635a043e8c7SAdrian Chadd 636a043e8c7SAdrian Chadd smi_read(dev, reg, &data, RTL_WAITOK); 637a043e8c7SAdrian Chadd return (data); 638a043e8c7SAdrian Chadd } 639a043e8c7SAdrian Chadd 640a043e8c7SAdrian Chadd static int 641a043e8c7SAdrian Chadd rtl_writereg(device_t dev, int reg, int value) 642a043e8c7SAdrian Chadd { 643a043e8c7SAdrian Chadd return (smi_write(dev, reg, value, RTL_WAITOK)); 644a043e8c7SAdrian Chadd } 645a043e8c7SAdrian Chadd 646a043e8c7SAdrian Chadd static int 647a043e8c7SAdrian Chadd rtl_getport(device_t dev, etherswitch_port_t *p) 648a043e8c7SAdrian Chadd { 649a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 650a043e8c7SAdrian Chadd struct ifmedia *ifm; 651a043e8c7SAdrian Chadd struct mii_data *mii; 652477e3effSMichael Zhilin struct ifmediareq *ifmr; 653a043e8c7SAdrian Chadd uint16_t v; 654a3219359SAdrian Chadd int err, vlangroup; 655a043e8c7SAdrian Chadd 656a3219359SAdrian Chadd sc = device_get_softc(dev); 657477e3effSMichael Zhilin 658477e3effSMichael Zhilin ifmr = &p->es_ifmr; 659477e3effSMichael Zhilin 6605a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1)) 661477e3effSMichael Zhilin return (ENXIO); 6625a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) { 6635a4380b5SMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port + 1, 6645a4380b5SMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port + 1))); 6655a4380b5SMichael Zhilin } else { 666477e3effSMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port, 667477e3effSMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port))); 6685a4380b5SMichael Zhilin } 669bfae9329SLuiz Otavio O Souza p->es_pvid = sc->vid[vlangroup] & ETHERSWITCH_VID_MASK; 670a043e8c7SAdrian Chadd 6715a4380b5SMichael Zhilin if (p->es_port < sc->numphys) { 672a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]); 673a043e8c7SAdrian Chadd ifm = &mii->mii_media; 674a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCGIFMEDIA); 675a043e8c7SAdrian Chadd if (err) 676a043e8c7SAdrian Chadd return (err); 677a043e8c7SAdrian Chadd } else { 678a043e8c7SAdrian Chadd /* fill in fixed values for CPU port */ 679dddab089SLuiz Otavio O Souza p->es_flags |= ETHERSWITCH_PORT_CPU; 680477e3effSMichael Zhilin smi_read(dev, RTL8366_PLSR_BASE + (RTL8366_NUM_PHYS)/2, &v, RTL_WAITOK); 681477e3effSMichael Zhilin v = v >> (8 * ((RTL8366_NUM_PHYS) % 2)); 682a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(v, &ifmr->ifm_status, &ifmr->ifm_active); 683a043e8c7SAdrian Chadd ifmr->ifm_current = ifmr->ifm_active; 684a043e8c7SAdrian Chadd ifmr->ifm_mask = 0; 685a043e8c7SAdrian Chadd ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 68628b07d23SLuiz Otavio O Souza /* Return our static media list. */ 68728b07d23SLuiz Otavio O Souza if (ifmr->ifm_count > 0) { 68828b07d23SLuiz Otavio O Souza ifmr->ifm_count = 1; 68928b07d23SLuiz Otavio O Souza ifmr->ifm_ulist[0] = IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 69028b07d23SLuiz Otavio O Souza IFM_FDX, 0); 69128b07d23SLuiz Otavio O Souza } else 69228b07d23SLuiz Otavio O Souza ifmr->ifm_count = 0; 693a043e8c7SAdrian Chadd } 694a043e8c7SAdrian Chadd return (0); 695a043e8c7SAdrian Chadd } 696a043e8c7SAdrian Chadd 697a043e8c7SAdrian Chadd static int 698a043e8c7SAdrian Chadd rtl_setport(device_t dev, etherswitch_port_t *p) 699a043e8c7SAdrian Chadd { 700a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 701477e3effSMichael Zhilin int i, err, vlangroup; 702a043e8c7SAdrian Chadd struct ifmedia *ifm; 703a043e8c7SAdrian Chadd struct mii_data *mii; 7045a4380b5SMichael Zhilin int port; 705a043e8c7SAdrian Chadd 706a3219359SAdrian Chadd sc = device_get_softc(dev); 707477e3effSMichael Zhilin 7085a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1)) 709477e3effSMichael Zhilin return (ENXIO); 710a3219359SAdrian Chadd vlangroup = -1; 711477e3effSMichael Zhilin for (i = 0; i < RTL8366_NUM_VLANS; i++) { 712bfae9329SLuiz Otavio O Souza if ((sc->vid[i] & ETHERSWITCH_VID_MASK) == p->es_pvid) { 713a3219359SAdrian Chadd vlangroup = i; 714a3219359SAdrian Chadd break; 715a3219359SAdrian Chadd } 716a3219359SAdrian Chadd } 717a3219359SAdrian Chadd if (vlangroup == -1) 718a3219359SAdrian Chadd return (ENXIO); 7195a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) { 7205a4380b5SMichael Zhilin port = p->es_port + 1; 7215a4380b5SMichael Zhilin } else { 7225a4380b5SMichael Zhilin port = p->es_port; 7235a4380b5SMichael Zhilin } 7245a4380b5SMichael Zhilin err = smi_rmw(dev, RTL8366_PVCR_REG(port), 7255a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, RTL8366_PVCR_PORT_MASK), 7265a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, vlangroup), RTL_WAITOK); 727a043e8c7SAdrian Chadd if (err) 728a043e8c7SAdrian Chadd return (err); 7295a4380b5SMichael Zhilin /* CPU Port */ 7305a4380b5SMichael Zhilin if (p->es_port == sc->numphys) 731dddab089SLuiz Otavio O Souza return (0); 732a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]); 733a043e8c7SAdrian Chadd ifm = &mii->mii_media; 734a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCSIFMEDIA); 735a043e8c7SAdrian Chadd return (err); 736a043e8c7SAdrian Chadd } 737a043e8c7SAdrian Chadd 738a043e8c7SAdrian Chadd static int 739a043e8c7SAdrian Chadd rtl_getvgroup(device_t dev, etherswitch_vlangroup_t *vg) 740a043e8c7SAdrian Chadd { 741bfae9329SLuiz Otavio O Souza struct rtl8366rb_softc *sc; 742a043e8c7SAdrian Chadd uint16_t vmcr[3]; 743a043e8c7SAdrian Chadd int i; 7445a4380b5SMichael Zhilin int member, untagged; 745a043e8c7SAdrian Chadd 746bfae9329SLuiz Otavio O Souza sc = device_get_softc(dev); 747477e3effSMichael Zhilin 748477e3effSMichael Zhilin for (i=0; i<RTL8366_VMCR_MULT; i++) 749477e3effSMichael Zhilin vmcr[i] = rtl_readreg(dev, RTL8366_VMCR(i, vg->es_vlangroup)); 750477e3effSMichael Zhilin 751bfae9329SLuiz Otavio O Souza vg->es_vid = sc->vid[vg->es_vlangroup]; 7525a4380b5SMichael Zhilin member = RTL8366_VMCR_MEMBER(vmcr); 7535a4380b5SMichael Zhilin untagged = RTL8366_VMCR_UNTAG(vmcr); 7545a4380b5SMichael Zhilin if (sc->phy4cpu) { 7555a4380b5SMichael Zhilin vg->es_member_ports = ((member & 0x20) >> 1) | (member & 0x0f); 7565a4380b5SMichael Zhilin vg->es_untagged_ports = ((untagged & 0x20) >> 1) | (untagged & 0x0f); 7575a4380b5SMichael Zhilin } else { 7585a4380b5SMichael Zhilin vg->es_member_ports = member; 7595a4380b5SMichael Zhilin vg->es_untagged_ports = untagged; 7605a4380b5SMichael Zhilin } 761477e3effSMichael Zhilin vg->es_fid = RTL8366_VMCR_FID(vmcr); 762a043e8c7SAdrian Chadd return (0); 763a043e8c7SAdrian Chadd } 764a043e8c7SAdrian Chadd 765a043e8c7SAdrian Chadd static int 766a043e8c7SAdrian Chadd rtl_setvgroup(device_t dev, etherswitch_vlangroup_t *vg) 767a043e8c7SAdrian Chadd { 768a3219359SAdrian Chadd struct rtl8366rb_softc *sc; 769477e3effSMichael Zhilin int g; 7705a4380b5SMichael Zhilin int member, untagged; 771a043e8c7SAdrian Chadd 772a3219359SAdrian Chadd sc = device_get_softc(dev); 773477e3effSMichael Zhilin 774477e3effSMichael Zhilin g = vg->es_vlangroup; 775477e3effSMichael Zhilin 776a3219359SAdrian Chadd sc->vid[g] = vg->es_vid; 777bfae9329SLuiz Otavio O Souza /* VLAN group disabled ? */ 778bfae9329SLuiz Otavio O Souza if (vg->es_member_ports == 0 && vg->es_untagged_ports == 0 && vg->es_vid == 0) 779bfae9329SLuiz Otavio O Souza return (0); 780bfae9329SLuiz Otavio O Souza sc->vid[g] |= ETHERSWITCH_VID_VALID; 781477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_DOT1Q_REG, g), 782477e3effSMichael Zhilin (vg->es_vid << RTL8366_VMCR_DOT1Q_VID_SHIFT) & RTL8366_VMCR_DOT1Q_VID_MASK); 7835a4380b5SMichael Zhilin if (sc->phy4cpu) { 7845a4380b5SMichael Zhilin /* add space at phy4 */ 7855a4380b5SMichael Zhilin member = (vg->es_member_ports & 0x0f) | 7865a4380b5SMichael Zhilin ((vg->es_member_ports & 0x10) << 1); 7875a4380b5SMichael Zhilin untagged = (vg->es_untagged_ports & 0x0f) | 7885a4380b5SMichael Zhilin ((vg->es_untagged_ports & 0x10) << 1); 7895a4380b5SMichael Zhilin } else { 7905a4380b5SMichael Zhilin member = vg->es_member_ports; 7915a4380b5SMichael Zhilin untagged = vg->es_untagged_ports; 7925a4380b5SMichael Zhilin } 7935a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) { 794477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g), 7955a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) | 7965a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK)); 797477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_FID_REG, g), 798a043e8c7SAdrian Chadd vg->es_fid); 799477e3effSMichael Zhilin } else { 800477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g), 8015a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) | 8025a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK) | 803477e3effSMichael Zhilin ((vg->es_fid << RTL8366_VMCR_FID_FID_SHIFT) & RTL8366_VMCR_FID_FID_MASK)); 804477e3effSMichael Zhilin } 805a043e8c7SAdrian Chadd return (0); 806a043e8c7SAdrian Chadd } 807a043e8c7SAdrian Chadd 808a043e8c7SAdrian Chadd static int 809bfae9329SLuiz Otavio O Souza rtl_getconf(device_t dev, etherswitch_conf_t *conf) 810bfae9329SLuiz Otavio O Souza { 811bfae9329SLuiz Otavio O Souza 812bfae9329SLuiz Otavio O Souza /* Return the VLAN mode. */ 813bfae9329SLuiz Otavio O Souza conf->cmd = ETHERSWITCH_CONF_VLAN_MODE; 814bfae9329SLuiz Otavio O Souza conf->vlan_mode = ETHERSWITCH_VLAN_DOT1Q; 815bfae9329SLuiz Otavio O Souza 816bfae9329SLuiz Otavio O Souza return (0); 817bfae9329SLuiz Otavio O Souza } 818bfae9329SLuiz Otavio O Souza 819bfae9329SLuiz Otavio O Souza static int 820a043e8c7SAdrian Chadd rtl_readphy(device_t dev, int phy, int reg) 821a043e8c7SAdrian Chadd { 822477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 823477e3effSMichael Zhilin uint16_t data; 824a043e8c7SAdrian Chadd int err, i, sleep; 825a043e8c7SAdrian Chadd 826477e3effSMichael Zhilin sc = device_get_softc(dev); 827477e3effSMichael Zhilin 828477e3effSMichael Zhilin data = 0; 829477e3effSMichael Zhilin 830477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS) 831a043e8c7SAdrian Chadd return (ENXIO); 832477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG) 833a043e8c7SAdrian Chadd return (ENXIO); 834a043e8c7SAdrian Chadd sleep = RTL_WAITOK; 835a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 836a043e8c7SAdrian Chadd if (err != 0) 837a043e8c7SAdrian Chadd return (EBUSY); 838a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 839477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_READ, sleep); 840a043e8c7SAdrian Chadd if (err == 0) 841477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), 0, sleep); 842a043e8c7SAdrian Chadd if (err == 0) { 843477e3effSMichael Zhilin err = smi_read_locked(sc, RTL8366_PADR, &data, sleep); 844a043e8c7SAdrian Chadd break; 845a043e8c7SAdrian Chadd } 846a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries); 847a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_readphy(): chip not responsive, retrying %d more times\n", i); 848a043e8c7SAdrian Chadd pause("rtl_readphy", RTL_IICBUS_RETRY_SLEEP); 849a043e8c7SAdrian Chadd } 850a043e8c7SAdrian Chadd smi_release(sc, sleep); 851a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_readphy()=%d: phy=%d.%02x\n", phy, reg); 852a043e8c7SAdrian Chadd return (data); 853a043e8c7SAdrian Chadd } 854a043e8c7SAdrian Chadd 855a043e8c7SAdrian Chadd static int 856a043e8c7SAdrian Chadd rtl_writephy(device_t dev, int phy, int reg, int data) 857a043e8c7SAdrian Chadd { 858477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 859a043e8c7SAdrian Chadd int err, i, sleep; 860a043e8c7SAdrian Chadd 861477e3effSMichael Zhilin sc = device_get_softc(dev); 862477e3effSMichael Zhilin 863477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS) 864a043e8c7SAdrian Chadd return (ENXIO); 865477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG) 866a043e8c7SAdrian Chadd return (ENXIO); 867a043e8c7SAdrian Chadd sleep = RTL_WAITOK; 868a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 869a043e8c7SAdrian Chadd if (err != 0) 870a043e8c7SAdrian Chadd return (EBUSY); 871a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 872477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_WRITE, sleep); 873a043e8c7SAdrian Chadd if (err == 0) 874477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), data, sleep); 875a043e8c7SAdrian Chadd if (err == 0) { 876a043e8c7SAdrian Chadd break; 877a043e8c7SAdrian Chadd } 878a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries); 879a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_writephy(): chip not responsive, retrying %d more tiems\n", i); 880a043e8c7SAdrian Chadd pause("rtl_writephy", RTL_IICBUS_RETRY_SLEEP); 881a043e8c7SAdrian Chadd } 882a043e8c7SAdrian Chadd smi_release(sc, sleep); 883a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_writephy()=%d: phy=%d.%02x\n", phy, reg); 884a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 885a043e8c7SAdrian Chadd } 886a043e8c7SAdrian Chadd 887a043e8c7SAdrian Chadd static int 8882e6a8c1aSJustin Hibbits rtl8366rb_ifmedia_upd(if_t ifp) 889a043e8c7SAdrian Chadd { 890477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 891477e3effSMichael Zhilin struct mii_data *mii; 892477e3effSMichael Zhilin 8932e6a8c1aSJustin Hibbits sc = if_getsoftc(ifp); 8942e6a8c1aSJustin Hibbits mii = device_get_softc(sc->miibus[if_getdunit(ifp)]); 895a043e8c7SAdrian Chadd 896a043e8c7SAdrian Chadd mii_mediachg(mii); 897a043e8c7SAdrian Chadd return (0); 898a043e8c7SAdrian Chadd } 899a043e8c7SAdrian Chadd 900a043e8c7SAdrian Chadd static void 9012e6a8c1aSJustin Hibbits rtl8366rb_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr) 902a043e8c7SAdrian Chadd { 903477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 904477e3effSMichael Zhilin struct mii_data *mii; 905477e3effSMichael Zhilin 9062e6a8c1aSJustin Hibbits sc = if_getsoftc(ifp); 9072e6a8c1aSJustin Hibbits mii = device_get_softc(sc->miibus[if_getdunit(ifp)]); 908a043e8c7SAdrian Chadd 909a043e8c7SAdrian Chadd mii_pollstat(mii); 910a043e8c7SAdrian Chadd ifmr->ifm_active = mii->mii_media_active; 911a043e8c7SAdrian Chadd ifmr->ifm_status = mii->mii_media_status; 912a043e8c7SAdrian Chadd } 913a043e8c7SAdrian Chadd 914a043e8c7SAdrian Chadd 915a043e8c7SAdrian Chadd static device_method_t rtl8366rb_methods[] = { 916a043e8c7SAdrian Chadd /* Device interface */ 917a043e8c7SAdrian Chadd DEVMETHOD(device_identify, rtl8366rb_identify), 918a043e8c7SAdrian Chadd DEVMETHOD(device_probe, rtl8366rb_probe), 919a043e8c7SAdrian Chadd DEVMETHOD(device_attach, rtl8366rb_attach), 920a043e8c7SAdrian Chadd DEVMETHOD(device_detach, rtl8366rb_detach), 921a043e8c7SAdrian Chadd 922a043e8c7SAdrian Chadd /* bus interface */ 923a043e8c7SAdrian Chadd DEVMETHOD(bus_add_child, device_add_child_ordered), 924a043e8c7SAdrian Chadd 925a043e8c7SAdrian Chadd /* MII interface */ 926a043e8c7SAdrian Chadd DEVMETHOD(miibus_readreg, rtl_readphy), 927a043e8c7SAdrian Chadd DEVMETHOD(miibus_writereg, rtl_writephy), 928a043e8c7SAdrian Chadd 9295a4380b5SMichael Zhilin /* MDIO interface */ 9305a4380b5SMichael Zhilin DEVMETHOD(mdio_readreg, rtl_readphy), 9315a4380b5SMichael Zhilin DEVMETHOD(mdio_writereg, rtl_writephy), 9325a4380b5SMichael Zhilin 933a043e8c7SAdrian Chadd /* etherswitch interface */ 934bfae9329SLuiz Otavio O Souza DEVMETHOD(etherswitch_getconf, rtl_getconf), 935a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getinfo, rtl_getinfo), 936a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readreg, rtl_readreg), 937a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writereg, rtl_writereg), 938a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readphyreg, rtl_readphy), 939a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writephyreg, rtl_writephy), 940a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getport, rtl_getport), 941a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setport, rtl_setport), 942a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getvgroup, rtl_getvgroup), 943a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setvgroup, rtl_setvgroup), 944a043e8c7SAdrian Chadd 945a043e8c7SAdrian Chadd DEVMETHOD_END 946a043e8c7SAdrian Chadd }; 947a043e8c7SAdrian Chadd 948a043e8c7SAdrian Chadd DEFINE_CLASS_0(rtl8366rb, rtl8366rb_driver, rtl8366rb_methods, 949a043e8c7SAdrian Chadd sizeof(struct rtl8366rb_softc)); 950a043e8c7SAdrian Chadd 95142726c2fSJohn Baldwin DRIVER_MODULE(rtl8366rb, iicbus, rtl8366rb_driver, 0, 0); 9523e38757dSJohn Baldwin DRIVER_MODULE(miibus, rtl8366rb, miibus_driver, 0, 0); 9538933f7d6SJohn Baldwin DRIVER_MODULE(mdio, rtl8366rb, mdio_driver, 0, 0); 954829a13faSJohn Baldwin DRIVER_MODULE(etherswitch, rtl8366rb, etherswitch_driver, 0, 0); 955a043e8c7SAdrian Chadd MODULE_VERSION(rtl8366rb, 1); 956a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, iicbus, 1, 1, 1); /* XXX which versions? */ 957a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, miibus, 1, 1, 1); /* XXX which versions? */ 958a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, etherswitch, 1, 1, 1); /* XXX which versions? */ 959