1a043e8c7SAdrian Chadd /*- 2*718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*718cf2ccSPedro 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; 110a043e8c7SAdrian Chadd static SYSCTL_NODE(_debug, OID_AUTO, rtl8366rb, CTLFLAG_RD, 0, "rtl8366rb"); 111a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, callout_blocked, CTLFLAG_RW, &callout_blocked, 0, 112a043e8c7SAdrian Chadd "number of times the callout couldn't acquire the bus"); 113a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, iic_select_retries, CTLFLAG_RW, &iic_select_retries, 0, 114a043e8c7SAdrian Chadd "number of times the I2C bus selection had to be retried"); 115a043e8c7SAdrian Chadd SYSCTL_INT(_debug_rtl8366rb, OID_AUTO, phy_access_retries, CTLFLAG_RW, &phy_access_retries, 0, 116a043e8c7SAdrian Chadd "number of times PHY register access had to be retried"); 117a043e8c7SAdrian Chadd #else 118a043e8c7SAdrian Chadd #define DPRINTF(dev, args...) 119a043e8c7SAdrian Chadd #define DEVERR(dev, err, fmt, args...) 120a043e8c7SAdrian Chadd #define DEBUG_INCRVAR(var) 121a043e8c7SAdrian Chadd #endif 122a043e8c7SAdrian Chadd 123a043e8c7SAdrian Chadd static int smi_probe(device_t dev); 124a043e8c7SAdrian Chadd static int smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep); 125a043e8c7SAdrian Chadd static int smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep); 126a043e8c7SAdrian Chadd static int smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep); 127a043e8c7SAdrian Chadd static void rtl8366rb_tick(void *arg); 128a043e8c7SAdrian Chadd static int rtl8366rb_ifmedia_upd(struct ifnet *); 129a043e8c7SAdrian Chadd static void rtl8366rb_ifmedia_sts(struct ifnet *, struct ifmediareq *); 130a043e8c7SAdrian Chadd 131a043e8c7SAdrian Chadd static void 132a043e8c7SAdrian Chadd rtl8366rb_identify(driver_t *driver, device_t parent) 133a043e8c7SAdrian Chadd { 134a043e8c7SAdrian Chadd device_t child; 135a043e8c7SAdrian Chadd struct iicbus_ivar *devi; 136a043e8c7SAdrian Chadd 137a043e8c7SAdrian Chadd if (device_find_child(parent, "rtl8366rb", -1) == NULL) { 138a043e8c7SAdrian Chadd child = BUS_ADD_CHILD(parent, 0, "rtl8366rb", -1); 139a043e8c7SAdrian Chadd devi = IICBUS_IVAR(child); 140477e3effSMichael Zhilin devi->addr = RTL8366_IIC_ADDR; 141a043e8c7SAdrian Chadd } 142a043e8c7SAdrian Chadd } 143a043e8c7SAdrian Chadd 144a043e8c7SAdrian Chadd static int 145a043e8c7SAdrian Chadd rtl8366rb_probe(device_t dev) 146a043e8c7SAdrian Chadd { 147477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 148477e3effSMichael Zhilin 149477e3effSMichael Zhilin sc = device_get_softc(dev); 150477e3effSMichael Zhilin 151477e3effSMichael Zhilin bzero(sc, sizeof(*sc)); 152a043e8c7SAdrian Chadd if (smi_probe(dev) != 0) 153a043e8c7SAdrian Chadd return (ENXIO); 1545a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) 155a043e8c7SAdrian Chadd device_set_desc(dev, "RTL8366RB Ethernet Switch Controller"); 156477e3effSMichael Zhilin else 157477e3effSMichael Zhilin device_set_desc(dev, "RTL8366SR Ethernet Switch Controller"); 158a043e8c7SAdrian Chadd return (BUS_PROBE_DEFAULT); 159a043e8c7SAdrian Chadd } 160a043e8c7SAdrian Chadd 161a043e8c7SAdrian Chadd static void 162a043e8c7SAdrian Chadd rtl8366rb_init(device_t dev) 163a043e8c7SAdrian Chadd { 164bfae9329SLuiz Otavio O Souza struct rtl8366rb_softc *sc; 165477e3effSMichael Zhilin int i; 166477e3effSMichael Zhilin 167477e3effSMichael Zhilin sc = device_get_softc(dev); 168bfae9329SLuiz Otavio O Souza 169a043e8c7SAdrian Chadd /* Initialisation for TL-WR1043ND */ 170477e3effSMichael Zhilin #ifdef RTL8366_SOFT_RESET 171477e3effSMichael Zhilin smi_rmw(dev, RTL8366_RCR, 172477e3effSMichael Zhilin RTL8366_RCR_SOFT_RESET, 173477e3effSMichael Zhilin RTL8366_RCR_SOFT_RESET, RTL_WAITOK); 174477e3effSMichael Zhilin #else 175477e3effSMichael Zhilin smi_rmw(dev, RTL8366_RCR, 176477e3effSMichael Zhilin RTL8366_RCR_HARD_RESET, 177477e3effSMichael Zhilin RTL8366_RCR_HARD_RESET, RTL_WAITOK); 178477e3effSMichael Zhilin #endif 179477e3effSMichael Zhilin /* hard reset not return ack */ 180a043e8c7SAdrian Chadd DELAY(100000); 181a043e8c7SAdrian Chadd /* Enable 16 VLAN mode */ 182477e3effSMichael Zhilin smi_rmw(dev, RTL8366_SGCR, 183477e3effSMichael Zhilin RTL8366_SGCR_EN_VLAN | RTL8366_SGCR_EN_VLAN_4KTB, 184477e3effSMichael Zhilin RTL8366_SGCR_EN_VLAN, RTL_WAITOK); 185bfae9329SLuiz Otavio O Souza /* Initialize our vlan table. */ 186bfae9329SLuiz Otavio O Souza for (i = 0; i <= 1; i++) 187bfae9329SLuiz Otavio O Souza sc->vid[i] = (i + 1) | ETHERSWITCH_VID_VALID; 188bfae9329SLuiz Otavio O Souza /* Remove port 0 from VLAN 1. */ 189477e3effSMichael Zhilin smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 0), 190a043e8c7SAdrian Chadd (1 << 0), 0, RTL_WAITOK); 191bfae9329SLuiz Otavio O Souza /* Add port 0 untagged and port 5 tagged to VLAN 2. */ 192477e3effSMichael Zhilin smi_rmw(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, 1), 193477e3effSMichael Zhilin ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT) 194477e3effSMichael Zhilin | ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT), 195477e3effSMichael Zhilin ((1 << 5 | 1 << 0) << RTL8366_VMCR_MU_MEMBER_SHIFT 196477e3effSMichael Zhilin | ((1 << 0) << RTL8366_VMCR_MU_UNTAG_SHIFT)), 197a043e8c7SAdrian Chadd RTL_WAITOK); 198bfae9329SLuiz Otavio O Souza /* Set PVID 2 for port 0. */ 199477e3effSMichael Zhilin smi_rmw(dev, RTL8366_PVCR_REG(0), 200477e3effSMichael Zhilin RTL8366_PVCR_VAL(0, RTL8366_PVCR_PORT_MASK), 201477e3effSMichael Zhilin RTL8366_PVCR_VAL(0, 1), RTL_WAITOK); 202a043e8c7SAdrian Chadd } 203a043e8c7SAdrian Chadd 204a043e8c7SAdrian Chadd static int 205a043e8c7SAdrian Chadd rtl8366rb_attach(device_t dev) 206a043e8c7SAdrian Chadd { 207a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 208477e3effSMichael Zhilin uint16_t rev = 0; 209a043e8c7SAdrian Chadd char name[IFNAMSIZ]; 210a043e8c7SAdrian Chadd int err = 0; 211a043e8c7SAdrian Chadd int i; 212a043e8c7SAdrian Chadd 213a043e8c7SAdrian Chadd sc = device_get_softc(dev); 214477e3effSMichael Zhilin 215a043e8c7SAdrian Chadd sc->dev = dev; 216a043e8c7SAdrian Chadd mtx_init(&sc->sc_mtx, "rtl8366rb", NULL, MTX_DEF); 217a043e8c7SAdrian Chadd sc->smi_acquired = 0; 218a043e8c7SAdrian Chadd mtx_init(&sc->callout_mtx, "rtl8366rbcallout", NULL, MTX_DEF); 219a043e8c7SAdrian Chadd 220a043e8c7SAdrian Chadd rtl8366rb_init(dev); 221477e3effSMichael Zhilin smi_read(dev, RTL8366_CVCR, &rev, RTL_WAITOK); 222a043e8c7SAdrian Chadd device_printf(dev, "rev. %d\n", rev & 0x000f); 223a043e8c7SAdrian Chadd 2245a4380b5SMichael Zhilin sc->phy4cpu = 0; 2255a4380b5SMichael Zhilin (void) resource_int_value(device_get_name(dev), device_get_unit(dev), 2265a4380b5SMichael Zhilin "phy4cpu", &sc->phy4cpu); 2275a4380b5SMichael Zhilin 2285a4380b5SMichael Zhilin sc->numphys = sc->phy4cpu ? RTL8366_NUM_PHYS - 1 : RTL8366_NUM_PHYS; 2295a4380b5SMichael Zhilin 2305a4380b5SMichael Zhilin sc->info.es_nports = sc->numphys + 1; 231477e3effSMichael Zhilin sc->info.es_nvlangroups = RTL8366_NUM_VLANS; 232477e3effSMichael Zhilin sc->info.es_vlan_caps = ETHERSWITCH_VLAN_DOT1Q; 2335a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) 234477e3effSMichael Zhilin sprintf(sc->info.es_name, "Realtek RTL8366RB"); 235477e3effSMichael Zhilin else 236477e3effSMichael Zhilin sprintf(sc->info.es_name, "Realtek RTL8366SR"); 237477e3effSMichael Zhilin 238a043e8c7SAdrian Chadd /* attach miibus and phys */ 239a043e8c7SAdrian Chadd /* PHYs need an interface, so we generate a dummy one */ 2405a4380b5SMichael Zhilin for (i = 0; i < sc->numphys; i++) { 241a043e8c7SAdrian Chadd sc->ifp[i] = if_alloc(IFT_ETHER); 242a043e8c7SAdrian Chadd sc->ifp[i]->if_softc = sc; 243a043e8c7SAdrian Chadd sc->ifp[i]->if_flags |= IFF_UP | IFF_BROADCAST | IFF_DRV_RUNNING 244a043e8c7SAdrian Chadd | IFF_SIMPLEX; 245a043e8c7SAdrian Chadd snprintf(name, IFNAMSIZ, "%sport", device_get_nameunit(dev)); 246a043e8c7SAdrian Chadd sc->ifname[i] = malloc(strlen(name)+1, M_DEVBUF, M_WAITOK); 247a043e8c7SAdrian Chadd bcopy(name, sc->ifname[i], strlen(name)+1); 248a043e8c7SAdrian Chadd if_initname(sc->ifp[i], sc->ifname[i], i); 249a043e8c7SAdrian Chadd err = mii_attach(dev, &sc->miibus[i], sc->ifp[i], rtl8366rb_ifmedia_upd, \ 250a043e8c7SAdrian Chadd rtl8366rb_ifmedia_sts, BMSR_DEFCAPMASK, \ 251a043e8c7SAdrian Chadd i, MII_OFFSET_ANY, 0); 252a043e8c7SAdrian Chadd if (err != 0) { 253a043e8c7SAdrian Chadd device_printf(dev, "attaching PHY %d failed\n", i); 254a043e8c7SAdrian Chadd return (err); 255a043e8c7SAdrian Chadd } 256a043e8c7SAdrian Chadd } 257a043e8c7SAdrian Chadd 258a043e8c7SAdrian Chadd bus_generic_probe(dev); 259a043e8c7SAdrian Chadd bus_enumerate_hinted_children(dev); 260a043e8c7SAdrian Chadd err = bus_generic_attach(dev); 261a043e8c7SAdrian Chadd if (err != 0) 262a043e8c7SAdrian Chadd return (err); 263a043e8c7SAdrian Chadd 264a043e8c7SAdrian Chadd callout_init_mtx(&sc->callout_tick, &sc->callout_mtx, 0); 265a043e8c7SAdrian Chadd rtl8366rb_tick(sc); 266a043e8c7SAdrian Chadd 267a043e8c7SAdrian Chadd return (err); 268a043e8c7SAdrian Chadd } 269a043e8c7SAdrian Chadd 270a043e8c7SAdrian Chadd static int 271a043e8c7SAdrian Chadd rtl8366rb_detach(device_t dev) 272a043e8c7SAdrian Chadd { 273477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 274a043e8c7SAdrian Chadd int i; 275a043e8c7SAdrian Chadd 276477e3effSMichael Zhilin sc = device_get_softc(dev); 277477e3effSMichael Zhilin 2785a4380b5SMichael Zhilin for (i=0; i < sc->numphys; i++) { 279a043e8c7SAdrian Chadd if (sc->miibus[i]) 280a043e8c7SAdrian Chadd device_delete_child(dev, sc->miibus[i]); 281a043e8c7SAdrian Chadd if (sc->ifp[i] != NULL) 282a043e8c7SAdrian Chadd if_free(sc->ifp[i]); 283a043e8c7SAdrian Chadd free(sc->ifname[i], M_DEVBUF); 284a043e8c7SAdrian Chadd } 285a043e8c7SAdrian Chadd bus_generic_detach(dev); 286a043e8c7SAdrian Chadd callout_drain(&sc->callout_tick); 287a043e8c7SAdrian Chadd mtx_destroy(&sc->callout_mtx); 288a043e8c7SAdrian Chadd mtx_destroy(&sc->sc_mtx); 289a043e8c7SAdrian Chadd 290a043e8c7SAdrian Chadd return (0); 291a043e8c7SAdrian Chadd } 292a043e8c7SAdrian Chadd 293a043e8c7SAdrian Chadd static void 294a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(int portstatus, u_int *media_status, u_int *media_active) 295a043e8c7SAdrian Chadd { 296a043e8c7SAdrian Chadd *media_active = IFM_ETHER; 297a043e8c7SAdrian Chadd *media_status = IFM_AVALID; 298477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_LINK) != 0) 299a043e8c7SAdrian Chadd *media_status |= IFM_ACTIVE; 300a043e8c7SAdrian Chadd else { 301a043e8c7SAdrian Chadd *media_active |= IFM_NONE; 302a043e8c7SAdrian Chadd return; 303a043e8c7SAdrian Chadd } 304477e3effSMichael Zhilin switch (portstatus & RTL8366_PLSR_SPEED_MASK) { 305477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_10: 306a043e8c7SAdrian Chadd *media_active |= IFM_10_T; 307a043e8c7SAdrian Chadd break; 308477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_100: 309a043e8c7SAdrian Chadd *media_active |= IFM_100_TX; 310a043e8c7SAdrian Chadd break; 311477e3effSMichael Zhilin case RTL8366_PLSR_SPEED_1000: 312a043e8c7SAdrian Chadd *media_active |= IFM_1000_T; 313a043e8c7SAdrian Chadd break; 314a043e8c7SAdrian Chadd } 315477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_FULLDUPLEX) != 0) 316a043e8c7SAdrian Chadd *media_active |= IFM_FDX; 317a043e8c7SAdrian Chadd else 318a043e8c7SAdrian Chadd *media_active |= IFM_HDX; 319477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_TXPAUSE) != 0) 320a043e8c7SAdrian Chadd *media_active |= IFM_ETH_TXPAUSE; 321477e3effSMichael Zhilin if ((portstatus & RTL8366_PLSR_RXPAUSE) != 0) 322a043e8c7SAdrian Chadd *media_active |= IFM_ETH_RXPAUSE; 323a043e8c7SAdrian Chadd } 324a043e8c7SAdrian Chadd 325a043e8c7SAdrian Chadd static void 326a043e8c7SAdrian Chadd rtl833rb_miipollstat(struct rtl8366rb_softc *sc) 327a043e8c7SAdrian Chadd { 328a043e8c7SAdrian Chadd int i; 329a043e8c7SAdrian Chadd struct mii_data *mii; 330a043e8c7SAdrian Chadd struct mii_softc *miisc; 331a043e8c7SAdrian Chadd uint16_t value; 332a043e8c7SAdrian Chadd int portstatus; 333a043e8c7SAdrian Chadd 3345a4380b5SMichael Zhilin for (i = 0; i < sc->numphys; i++) { 335a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[i]); 336a043e8c7SAdrian Chadd if ((i % 2) == 0) { 337477e3effSMichael Zhilin if (smi_read(sc->dev, RTL8366_PLSR_BASE + i/2, &value, RTL_NOWAIT) != 0) { 338a043e8c7SAdrian Chadd DEBUG_INCRVAR(callout_blocked); 339a043e8c7SAdrian Chadd return; 340a043e8c7SAdrian Chadd } 341a043e8c7SAdrian Chadd portstatus = value & 0xff; 342a043e8c7SAdrian Chadd } else { 343a043e8c7SAdrian Chadd portstatus = (value >> 8) & 0xff; 344a043e8c7SAdrian Chadd } 345a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(portstatus, &mii->mii_media_status, &mii->mii_media_active); 346a043e8c7SAdrian Chadd LIST_FOREACH(miisc, &mii->mii_phys, mii_list) { 347a043e8c7SAdrian Chadd if (IFM_INST(mii->mii_media.ifm_cur->ifm_media) != miisc->mii_inst) 348a043e8c7SAdrian Chadd continue; 349a043e8c7SAdrian Chadd mii_phy_update(miisc, MII_POLLSTAT); 350a043e8c7SAdrian Chadd } 351a043e8c7SAdrian Chadd } 352a043e8c7SAdrian Chadd } 353a043e8c7SAdrian Chadd 354a043e8c7SAdrian Chadd static void 355a043e8c7SAdrian Chadd rtl8366rb_tick(void *arg) 356a043e8c7SAdrian Chadd { 357477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 358477e3effSMichael Zhilin 359477e3effSMichael Zhilin sc = arg; 360a043e8c7SAdrian Chadd 361a043e8c7SAdrian Chadd rtl833rb_miipollstat(sc); 362a043e8c7SAdrian Chadd callout_reset(&sc->callout_tick, hz, rtl8366rb_tick, sc); 363a043e8c7SAdrian Chadd } 364a043e8c7SAdrian Chadd 365a043e8c7SAdrian Chadd static int 366a043e8c7SAdrian Chadd smi_probe(device_t dev) 367a043e8c7SAdrian Chadd { 368477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 369a043e8c7SAdrian Chadd device_t iicbus, iicha; 370477e3effSMichael Zhilin int err, i, j; 371a043e8c7SAdrian Chadd uint16_t chipid; 372a043e8c7SAdrian Chadd char bytes[2]; 373a043e8c7SAdrian Chadd int xferd; 374a043e8c7SAdrian Chadd 375477e3effSMichael Zhilin sc = device_get_softc(dev); 376477e3effSMichael Zhilin 377a043e8c7SAdrian Chadd iicbus = device_get_parent(dev); 378a043e8c7SAdrian Chadd iicha = device_get_parent(iicbus); 379477e3effSMichael Zhilin 380477e3effSMichael Zhilin for (i = 0; i < 2; ++i) { 381477e3effSMichael Zhilin iicbus_reset(iicbus, IIC_FASTEST, RTL8366_IIC_ADDR, NULL); 382477e3effSMichael Zhilin for (j=3; j--; ) { 383a043e8c7SAdrian Chadd IICBUS_STOP(iicha); 384a043e8c7SAdrian Chadd /* 385a043e8c7SAdrian Chadd * we go directly to the host adapter because iicbus.c 386a043e8c7SAdrian Chadd * only issues a stop on a bus that was successfully started. 387a043e8c7SAdrian Chadd */ 388a043e8c7SAdrian Chadd } 389a043e8c7SAdrian Chadd err = iicbus_request_bus(iicbus, dev, IIC_WAIT); 390a043e8c7SAdrian Chadd if (err != 0) 391a043e8c7SAdrian Chadd goto out; 392477e3effSMichael Zhilin err = iicbus_start(iicbus, RTL8366_IIC_ADDR | RTL_IICBUS_READ, RTL_IICBUS_TIMEOUT); 393a043e8c7SAdrian Chadd if (err != 0) 394a043e8c7SAdrian Chadd goto out; 395477e3effSMichael Zhilin if (i == 0) { 396477e3effSMichael Zhilin bytes[0] = RTL8366RB_CIR & 0xff; 397477e3effSMichael Zhilin bytes[1] = (RTL8366RB_CIR >> 8) & 0xff; 398477e3effSMichael Zhilin } else { 399477e3effSMichael Zhilin bytes[0] = RTL8366SR_CIR & 0xff; 400477e3effSMichael Zhilin bytes[1] = (RTL8366SR_CIR >> 8) & 0xff; 401477e3effSMichael Zhilin } 402a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT); 403a043e8c7SAdrian Chadd if (err != 0) 404a043e8c7SAdrian Chadd goto out; 405a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0); 406a043e8c7SAdrian Chadd if (err != 0) 407a043e8c7SAdrian Chadd goto out; 408a043e8c7SAdrian Chadd chipid = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff); 409477e3effSMichael Zhilin if (i == 0 && chipid == RTL8366RB_CIR_ID8366RB) { 410a043e8c7SAdrian Chadd DPRINTF(dev, "chip id 0x%04x\n", chipid); 4115a4380b5SMichael Zhilin sc->chip_type = RTL8366RB; 412477e3effSMichael Zhilin err = 0; 413477e3effSMichael Zhilin break; 414477e3effSMichael Zhilin } 415477e3effSMichael Zhilin if (i == 1 && chipid == RTL8366SR_CIR_ID8366SR) { 416477e3effSMichael Zhilin DPRINTF(dev, "chip id 0x%04x\n", chipid); 4175a4380b5SMichael Zhilin sc->chip_type = RTL8366SR; 418477e3effSMichael Zhilin err = 0; 419477e3effSMichael Zhilin break; 420477e3effSMichael Zhilin } 421477e3effSMichael Zhilin if (i == 0) { 422477e3effSMichael Zhilin iicbus_stop(iicbus); 423477e3effSMichael Zhilin iicbus_release_bus(iicbus, dev); 424477e3effSMichael Zhilin } 425477e3effSMichael Zhilin } 426477e3effSMichael Zhilin if (i == 2) 427a043e8c7SAdrian Chadd err = ENXIO; 428a043e8c7SAdrian Chadd out: 429a043e8c7SAdrian Chadd iicbus_stop(iicbus); 430a043e8c7SAdrian Chadd iicbus_release_bus(iicbus, dev); 431a043e8c7SAdrian Chadd return (err == 0 ? 0 : ENXIO); 432a043e8c7SAdrian Chadd } 433a043e8c7SAdrian Chadd 434a043e8c7SAdrian Chadd static int 435a043e8c7SAdrian Chadd smi_acquire(struct rtl8366rb_softc *sc, int sleep) 436a043e8c7SAdrian Chadd { 437a043e8c7SAdrian Chadd int r = 0; 438a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) 439a043e8c7SAdrian Chadd RTL_LOCK(sc); 440a043e8c7SAdrian Chadd else 441a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0) 442a043e8c7SAdrian Chadd return (EWOULDBLOCK); 443a043e8c7SAdrian Chadd if (sc->smi_acquired == RTL_SMI_ACQUIRED) 444a043e8c7SAdrian Chadd r = EBUSY; 445a043e8c7SAdrian Chadd else { 446a043e8c7SAdrian Chadd r = iicbus_request_bus(device_get_parent(sc->dev), sc->dev, \ 447a043e8c7SAdrian Chadd sleep == RTL_WAITOK ? IIC_WAIT : IIC_DONTWAIT); 448a043e8c7SAdrian Chadd if (r == 0) 449a043e8c7SAdrian Chadd sc->smi_acquired = RTL_SMI_ACQUIRED; 450a043e8c7SAdrian Chadd } 451a043e8c7SAdrian Chadd RTL_UNLOCK(sc); 452a043e8c7SAdrian Chadd return (r); 453a043e8c7SAdrian Chadd } 454a043e8c7SAdrian Chadd 455a043e8c7SAdrian Chadd static int 456a043e8c7SAdrian Chadd smi_release(struct rtl8366rb_softc *sc, int sleep) 457a043e8c7SAdrian Chadd { 458a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) 459a043e8c7SAdrian Chadd RTL_LOCK(sc); 460a043e8c7SAdrian Chadd else 461a043e8c7SAdrian Chadd if (RTL_TRYLOCK(sc) == 0) 462a043e8c7SAdrian Chadd return (EWOULDBLOCK); 463a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 464a043e8c7SAdrian Chadd iicbus_release_bus(device_get_parent(sc->dev), sc->dev); 465a043e8c7SAdrian Chadd sc->smi_acquired = 0; 466a043e8c7SAdrian Chadd RTL_UNLOCK(sc); 467a043e8c7SAdrian Chadd return (0); 468a043e8c7SAdrian Chadd } 469a043e8c7SAdrian Chadd 470a043e8c7SAdrian Chadd static int 471a043e8c7SAdrian Chadd smi_select(device_t dev, int op, int sleep) 472a043e8c7SAdrian Chadd { 473477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 474a043e8c7SAdrian Chadd int err, i; 475477e3effSMichael Zhilin device_t iicbus; 476477e3effSMichael Zhilin struct iicbus_ivar *devi; 477477e3effSMichael Zhilin int slave; 478477e3effSMichael Zhilin 479477e3effSMichael Zhilin sc = device_get_softc(dev); 480477e3effSMichael Zhilin 481477e3effSMichael Zhilin iicbus = device_get_parent(dev); 482477e3effSMichael Zhilin devi = IICBUS_IVAR(dev); 483477e3effSMichael Zhilin slave = devi->addr; 484a043e8c7SAdrian Chadd 485a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT((struct rtl8366rb_softc *)device_get_softc(dev)); 486477e3effSMichael Zhilin 4875a4380b5SMichael Zhilin if (sc->chip_type == RTL8366SR) { // RTL8366SR work around 488477e3effSMichael Zhilin // this is same work around at probe 489477e3effSMichael Zhilin for (int i=3; i--; ) 490477e3effSMichael Zhilin IICBUS_STOP(device_get_parent(device_get_parent(dev))); 491477e3effSMichael Zhilin } 492a043e8c7SAdrian Chadd /* 493a043e8c7SAdrian Chadd * The chip does not use clock stretching when it is busy, 494a043e8c7SAdrian Chadd * instead ignoring the command. Retry a few times. 495a043e8c7SAdrian Chadd */ 496a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 497a043e8c7SAdrian Chadd err = iicbus_start(iicbus, slave | op, RTL_IICBUS_TIMEOUT); 498a043e8c7SAdrian Chadd if (err != IIC_ENOACK) 499a043e8c7SAdrian Chadd break; 500a043e8c7SAdrian Chadd if (sleep == RTL_WAITOK) { 501a043e8c7SAdrian Chadd DEBUG_INCRVAR(iic_select_retries); 502a043e8c7SAdrian Chadd pause("smi_select", RTL_IICBUS_RETRY_SLEEP); 503a043e8c7SAdrian Chadd } else 504a043e8c7SAdrian Chadd break; 505a043e8c7SAdrian Chadd } 506a043e8c7SAdrian Chadd return (err); 507a043e8c7SAdrian Chadd } 508a043e8c7SAdrian Chadd 509a043e8c7SAdrian Chadd static int 510a043e8c7SAdrian Chadd smi_read_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t *data, int sleep) 511a043e8c7SAdrian Chadd { 512a043e8c7SAdrian Chadd int err; 513477e3effSMichael Zhilin device_t iicbus; 514a043e8c7SAdrian Chadd char bytes[2]; 515a043e8c7SAdrian Chadd int xferd; 516a043e8c7SAdrian Chadd 517477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev); 518477e3effSMichael Zhilin 519a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 520a043e8c7SAdrian Chadd bytes[0] = addr & 0xff; 521a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff; 522a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_READ, sleep); 523a043e8c7SAdrian Chadd if (err != 0) 524a043e8c7SAdrian Chadd goto out; 525a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 2, &xferd, RTL_IICBUS_TIMEOUT); 526a043e8c7SAdrian Chadd if (err != 0) 527a043e8c7SAdrian Chadd goto out; 528a043e8c7SAdrian Chadd err = iicbus_read(iicbus, bytes, 2, &xferd, IIC_LAST_READ, 0); 529a043e8c7SAdrian Chadd if (err != 0) 530a043e8c7SAdrian Chadd goto out; 531a043e8c7SAdrian Chadd *data = ((bytes[1] & 0xff) << 8) | (bytes[0] & 0xff); 532a043e8c7SAdrian Chadd 533a043e8c7SAdrian Chadd out: 534a043e8c7SAdrian Chadd iicbus_stop(iicbus); 535a043e8c7SAdrian Chadd return (err); 536a043e8c7SAdrian Chadd } 537a043e8c7SAdrian Chadd 538a043e8c7SAdrian Chadd static int 539a043e8c7SAdrian Chadd smi_write_locked(struct rtl8366rb_softc *sc, uint16_t addr, uint16_t data, int sleep) 540a043e8c7SAdrian Chadd { 541a043e8c7SAdrian Chadd int err; 542477e3effSMichael Zhilin device_t iicbus; 543a043e8c7SAdrian Chadd char bytes[4]; 544a043e8c7SAdrian Chadd int xferd; 545a043e8c7SAdrian Chadd 546477e3effSMichael Zhilin iicbus = device_get_parent(sc->dev); 547477e3effSMichael Zhilin 548a043e8c7SAdrian Chadd RTL_SMI_ACQUIRED_ASSERT(sc); 549a043e8c7SAdrian Chadd bytes[0] = addr & 0xff; 550a043e8c7SAdrian Chadd bytes[1] = (addr >> 8) & 0xff; 551a043e8c7SAdrian Chadd bytes[2] = data & 0xff; 552a043e8c7SAdrian Chadd bytes[3] = (data >> 8) & 0xff; 553a043e8c7SAdrian Chadd 554a043e8c7SAdrian Chadd err = smi_select(sc->dev, RTL_IICBUS_WRITE, sleep); 555a043e8c7SAdrian Chadd if (err == 0) 556a043e8c7SAdrian Chadd err = iicbus_write(iicbus, bytes, 4, &xferd, RTL_IICBUS_TIMEOUT); 557a043e8c7SAdrian Chadd iicbus_stop(iicbus); 558a043e8c7SAdrian Chadd 559a043e8c7SAdrian Chadd return (err); 560a043e8c7SAdrian Chadd } 561a043e8c7SAdrian Chadd 562a043e8c7SAdrian Chadd static int 563a043e8c7SAdrian Chadd smi_read(device_t dev, uint16_t addr, uint16_t *data, int sleep) 564a043e8c7SAdrian Chadd { 565477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 566a043e8c7SAdrian Chadd int err; 567a043e8c7SAdrian Chadd 568477e3effSMichael Zhilin sc = device_get_softc(dev); 569477e3effSMichael Zhilin 570a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 571a043e8c7SAdrian Chadd if (err != 0) 572a043e8c7SAdrian Chadd return (EBUSY); 573a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, data, sleep); 574a043e8c7SAdrian Chadd smi_release(sc, sleep); 575a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_read()=%d: addr=%04x\n", addr); 576a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 577a043e8c7SAdrian Chadd } 578a043e8c7SAdrian Chadd 579a043e8c7SAdrian Chadd static int 580a043e8c7SAdrian Chadd smi_write(device_t dev, uint16_t addr, uint16_t data, int sleep) 581a043e8c7SAdrian Chadd { 582477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 583a043e8c7SAdrian Chadd int err; 584a043e8c7SAdrian Chadd 585477e3effSMichael Zhilin sc = device_get_softc(dev); 586477e3effSMichael Zhilin 587a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 588a043e8c7SAdrian Chadd if (err != 0) 589a043e8c7SAdrian Chadd return (EBUSY); 590a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, data, sleep); 591a043e8c7SAdrian Chadd smi_release(sc, sleep); 592a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_write()=%d: addr=%04x\n", addr); 593a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 594a043e8c7SAdrian Chadd } 595a043e8c7SAdrian Chadd 596a043e8c7SAdrian Chadd static int 597a043e8c7SAdrian Chadd smi_rmw(device_t dev, uint16_t addr, uint16_t mask, uint16_t data, int sleep) 598a043e8c7SAdrian Chadd { 599477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 600a043e8c7SAdrian Chadd int err; 601a043e8c7SAdrian Chadd uint16_t oldv, newv; 602a043e8c7SAdrian Chadd 603477e3effSMichael Zhilin sc = device_get_softc(dev); 604477e3effSMichael Zhilin 605a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 606a043e8c7SAdrian Chadd if (err != 0) 607a043e8c7SAdrian Chadd return (EBUSY); 608a043e8c7SAdrian Chadd if (err == 0) { 609a043e8c7SAdrian Chadd err = smi_read_locked(sc, addr, &oldv, sleep); 610a043e8c7SAdrian Chadd if (err == 0) { 611a043e8c7SAdrian Chadd newv = oldv & ~mask; 612a043e8c7SAdrian Chadd newv |= data & mask; 613a043e8c7SAdrian Chadd if (newv != oldv) 614a043e8c7SAdrian Chadd err = smi_write_locked(sc, addr, newv, sleep); 615a043e8c7SAdrian Chadd } 616a043e8c7SAdrian Chadd } 617a043e8c7SAdrian Chadd smi_release(sc, sleep); 618a043e8c7SAdrian Chadd DEVERR(dev, err, "smi_rmw()=%d: addr=%04x\n", addr); 619a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 620a043e8c7SAdrian Chadd } 621a043e8c7SAdrian Chadd 622a043e8c7SAdrian Chadd static etherswitch_info_t * 623a043e8c7SAdrian Chadd rtl_getinfo(device_t dev) 624a043e8c7SAdrian Chadd { 625477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 626477e3effSMichael Zhilin 627477e3effSMichael Zhilin sc = device_get_softc(dev); 628477e3effSMichael Zhilin 629477e3effSMichael Zhilin return (&sc->info); 630a043e8c7SAdrian Chadd } 631a043e8c7SAdrian Chadd 632a043e8c7SAdrian Chadd static int 633a043e8c7SAdrian Chadd rtl_readreg(device_t dev, int reg) 634a043e8c7SAdrian Chadd { 635477e3effSMichael Zhilin uint16_t data; 636477e3effSMichael Zhilin 637477e3effSMichael Zhilin data = 0; 638a043e8c7SAdrian Chadd 639a043e8c7SAdrian Chadd smi_read(dev, reg, &data, RTL_WAITOK); 640a043e8c7SAdrian Chadd return (data); 641a043e8c7SAdrian Chadd } 642a043e8c7SAdrian Chadd 643a043e8c7SAdrian Chadd static int 644a043e8c7SAdrian Chadd rtl_writereg(device_t dev, int reg, int value) 645a043e8c7SAdrian Chadd { 646a043e8c7SAdrian Chadd return (smi_write(dev, reg, value, RTL_WAITOK)); 647a043e8c7SAdrian Chadd } 648a043e8c7SAdrian Chadd 649a043e8c7SAdrian Chadd static int 650a043e8c7SAdrian Chadd rtl_getport(device_t dev, etherswitch_port_t *p) 651a043e8c7SAdrian Chadd { 652a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 653a043e8c7SAdrian Chadd struct ifmedia *ifm; 654a043e8c7SAdrian Chadd struct mii_data *mii; 655477e3effSMichael Zhilin struct ifmediareq *ifmr; 656a043e8c7SAdrian Chadd uint16_t v; 657a3219359SAdrian Chadd int err, vlangroup; 658a043e8c7SAdrian Chadd 659a3219359SAdrian Chadd sc = device_get_softc(dev); 660477e3effSMichael Zhilin 661477e3effSMichael Zhilin ifmr = &p->es_ifmr; 662477e3effSMichael Zhilin 6635a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1)) 664477e3effSMichael Zhilin return (ENXIO); 6655a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) { 6665a4380b5SMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port + 1, 6675a4380b5SMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port + 1))); 6685a4380b5SMichael Zhilin } else { 669477e3effSMichael Zhilin vlangroup = RTL8366_PVCR_GET(p->es_port, 670477e3effSMichael Zhilin rtl_readreg(dev, RTL8366_PVCR_REG(p->es_port))); 6715a4380b5SMichael Zhilin } 672bfae9329SLuiz Otavio O Souza p->es_pvid = sc->vid[vlangroup] & ETHERSWITCH_VID_MASK; 673a043e8c7SAdrian Chadd 6745a4380b5SMichael Zhilin if (p->es_port < sc->numphys) { 675a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]); 676a043e8c7SAdrian Chadd ifm = &mii->mii_media; 677a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCGIFMEDIA); 678a043e8c7SAdrian Chadd if (err) 679a043e8c7SAdrian Chadd return (err); 680a043e8c7SAdrian Chadd } else { 681a043e8c7SAdrian Chadd /* fill in fixed values for CPU port */ 682dddab089SLuiz Otavio O Souza p->es_flags |= ETHERSWITCH_PORT_CPU; 683477e3effSMichael Zhilin smi_read(dev, RTL8366_PLSR_BASE + (RTL8366_NUM_PHYS)/2, &v, RTL_WAITOK); 684477e3effSMichael Zhilin v = v >> (8 * ((RTL8366_NUM_PHYS) % 2)); 685a043e8c7SAdrian Chadd rtl8366rb_update_ifmedia(v, &ifmr->ifm_status, &ifmr->ifm_active); 686a043e8c7SAdrian Chadd ifmr->ifm_current = ifmr->ifm_active; 687a043e8c7SAdrian Chadd ifmr->ifm_mask = 0; 688a043e8c7SAdrian Chadd ifmr->ifm_status = IFM_ACTIVE | IFM_AVALID; 68928b07d23SLuiz Otavio O Souza /* Return our static media list. */ 69028b07d23SLuiz Otavio O Souza if (ifmr->ifm_count > 0) { 69128b07d23SLuiz Otavio O Souza ifmr->ifm_count = 1; 69228b07d23SLuiz Otavio O Souza ifmr->ifm_ulist[0] = IFM_MAKEWORD(IFM_ETHER, IFM_1000_T, 69328b07d23SLuiz Otavio O Souza IFM_FDX, 0); 69428b07d23SLuiz Otavio O Souza } else 69528b07d23SLuiz Otavio O Souza ifmr->ifm_count = 0; 696a043e8c7SAdrian Chadd } 697a043e8c7SAdrian Chadd return (0); 698a043e8c7SAdrian Chadd } 699a043e8c7SAdrian Chadd 700a043e8c7SAdrian Chadd static int 701a043e8c7SAdrian Chadd rtl_setport(device_t dev, etherswitch_port_t *p) 702a043e8c7SAdrian Chadd { 703a043e8c7SAdrian Chadd struct rtl8366rb_softc *sc; 704477e3effSMichael Zhilin int i, err, vlangroup; 705a043e8c7SAdrian Chadd struct ifmedia *ifm; 706a043e8c7SAdrian Chadd struct mii_data *mii; 7075a4380b5SMichael Zhilin int port; 708a043e8c7SAdrian Chadd 709a3219359SAdrian Chadd sc = device_get_softc(dev); 710477e3effSMichael Zhilin 7115a4380b5SMichael Zhilin if (p->es_port < 0 || p->es_port >= (sc->numphys + 1)) 712477e3effSMichael Zhilin return (ENXIO); 713a3219359SAdrian Chadd vlangroup = -1; 714477e3effSMichael Zhilin for (i = 0; i < RTL8366_NUM_VLANS; i++) { 715bfae9329SLuiz Otavio O Souza if ((sc->vid[i] & ETHERSWITCH_VID_MASK) == p->es_pvid) { 716a3219359SAdrian Chadd vlangroup = i; 717a3219359SAdrian Chadd break; 718a3219359SAdrian Chadd } 719a3219359SAdrian Chadd } 720a3219359SAdrian Chadd if (vlangroup == -1) 721a3219359SAdrian Chadd return (ENXIO); 7225a4380b5SMichael Zhilin if (sc->phy4cpu && p->es_port == sc->numphys) { 7235a4380b5SMichael Zhilin port = p->es_port + 1; 7245a4380b5SMichael Zhilin } else { 7255a4380b5SMichael Zhilin port = p->es_port; 7265a4380b5SMichael Zhilin } 7275a4380b5SMichael Zhilin err = smi_rmw(dev, RTL8366_PVCR_REG(port), 7285a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, RTL8366_PVCR_PORT_MASK), 7295a4380b5SMichael Zhilin RTL8366_PVCR_VAL(port, vlangroup), RTL_WAITOK); 730a043e8c7SAdrian Chadd if (err) 731a043e8c7SAdrian Chadd return (err); 7325a4380b5SMichael Zhilin /* CPU Port */ 7335a4380b5SMichael Zhilin if (p->es_port == sc->numphys) 734dddab089SLuiz Otavio O Souza return (0); 735a043e8c7SAdrian Chadd mii = device_get_softc(sc->miibus[p->es_port]); 736a043e8c7SAdrian Chadd ifm = &mii->mii_media; 737a043e8c7SAdrian Chadd err = ifmedia_ioctl(sc->ifp[p->es_port], &p->es_ifr, ifm, SIOCSIFMEDIA); 738a043e8c7SAdrian Chadd return (err); 739a043e8c7SAdrian Chadd } 740a043e8c7SAdrian Chadd 741a043e8c7SAdrian Chadd static int 742a043e8c7SAdrian Chadd rtl_getvgroup(device_t dev, etherswitch_vlangroup_t *vg) 743a043e8c7SAdrian Chadd { 744bfae9329SLuiz Otavio O Souza struct rtl8366rb_softc *sc; 745a043e8c7SAdrian Chadd uint16_t vmcr[3]; 746a043e8c7SAdrian Chadd int i; 7475a4380b5SMichael Zhilin int member, untagged; 748a043e8c7SAdrian Chadd 749bfae9329SLuiz Otavio O Souza sc = device_get_softc(dev); 750477e3effSMichael Zhilin 751477e3effSMichael Zhilin for (i=0; i<RTL8366_VMCR_MULT; i++) 752477e3effSMichael Zhilin vmcr[i] = rtl_readreg(dev, RTL8366_VMCR(i, vg->es_vlangroup)); 753477e3effSMichael Zhilin 754bfae9329SLuiz Otavio O Souza vg->es_vid = sc->vid[vg->es_vlangroup]; 7555a4380b5SMichael Zhilin member = RTL8366_VMCR_MEMBER(vmcr); 7565a4380b5SMichael Zhilin untagged = RTL8366_VMCR_UNTAG(vmcr); 7575a4380b5SMichael Zhilin if (sc->phy4cpu) { 7585a4380b5SMichael Zhilin vg->es_member_ports = ((member & 0x20) >> 1) | (member & 0x0f); 7595a4380b5SMichael Zhilin vg->es_untagged_ports = ((untagged & 0x20) >> 1) | (untagged & 0x0f); 7605a4380b5SMichael Zhilin } else { 7615a4380b5SMichael Zhilin vg->es_member_ports = member; 7625a4380b5SMichael Zhilin vg->es_untagged_ports = untagged; 7635a4380b5SMichael Zhilin } 764477e3effSMichael Zhilin vg->es_fid = RTL8366_VMCR_FID(vmcr); 765a043e8c7SAdrian Chadd return (0); 766a043e8c7SAdrian Chadd } 767a043e8c7SAdrian Chadd 768a043e8c7SAdrian Chadd static int 769a043e8c7SAdrian Chadd rtl_setvgroup(device_t dev, etherswitch_vlangroup_t *vg) 770a043e8c7SAdrian Chadd { 771a3219359SAdrian Chadd struct rtl8366rb_softc *sc; 772477e3effSMichael Zhilin int g; 7735a4380b5SMichael Zhilin int member, untagged; 774a043e8c7SAdrian Chadd 775a3219359SAdrian Chadd sc = device_get_softc(dev); 776477e3effSMichael Zhilin 777477e3effSMichael Zhilin g = vg->es_vlangroup; 778477e3effSMichael Zhilin 779a3219359SAdrian Chadd sc->vid[g] = vg->es_vid; 780bfae9329SLuiz Otavio O Souza /* VLAN group disabled ? */ 781bfae9329SLuiz Otavio O Souza if (vg->es_member_ports == 0 && vg->es_untagged_ports == 0 && vg->es_vid == 0) 782bfae9329SLuiz Otavio O Souza return (0); 783bfae9329SLuiz Otavio O Souza sc->vid[g] |= ETHERSWITCH_VID_VALID; 784477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_DOT1Q_REG, g), 785477e3effSMichael Zhilin (vg->es_vid << RTL8366_VMCR_DOT1Q_VID_SHIFT) & RTL8366_VMCR_DOT1Q_VID_MASK); 7865a4380b5SMichael Zhilin if (sc->phy4cpu) { 7875a4380b5SMichael Zhilin /* add space at phy4 */ 7885a4380b5SMichael Zhilin member = (vg->es_member_ports & 0x0f) | 7895a4380b5SMichael Zhilin ((vg->es_member_ports & 0x10) << 1); 7905a4380b5SMichael Zhilin untagged = (vg->es_untagged_ports & 0x0f) | 7915a4380b5SMichael Zhilin ((vg->es_untagged_ports & 0x10) << 1); 7925a4380b5SMichael Zhilin } else { 7935a4380b5SMichael Zhilin member = vg->es_member_ports; 7945a4380b5SMichael Zhilin untagged = vg->es_untagged_ports; 7955a4380b5SMichael Zhilin } 7965a4380b5SMichael Zhilin if (sc->chip_type == RTL8366RB) { 797477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g), 7985a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) | 7995a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK)); 800477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_FID_REG, g), 801a043e8c7SAdrian Chadd vg->es_fid); 802477e3effSMichael Zhilin } else { 803477e3effSMichael Zhilin rtl_writereg(dev, RTL8366_VMCR(RTL8366_VMCR_MU_REG, g), 8045a4380b5SMichael Zhilin ((member << RTL8366_VMCR_MU_MEMBER_SHIFT) & RTL8366_VMCR_MU_MEMBER_MASK) | 8055a4380b5SMichael Zhilin ((untagged << RTL8366_VMCR_MU_UNTAG_SHIFT) & RTL8366_VMCR_MU_UNTAG_MASK) | 806477e3effSMichael Zhilin ((vg->es_fid << RTL8366_VMCR_FID_FID_SHIFT) & RTL8366_VMCR_FID_FID_MASK)); 807477e3effSMichael Zhilin } 808a043e8c7SAdrian Chadd return (0); 809a043e8c7SAdrian Chadd } 810a043e8c7SAdrian Chadd 811a043e8c7SAdrian Chadd static int 812bfae9329SLuiz Otavio O Souza rtl_getconf(device_t dev, etherswitch_conf_t *conf) 813bfae9329SLuiz Otavio O Souza { 814bfae9329SLuiz Otavio O Souza 815bfae9329SLuiz Otavio O Souza /* Return the VLAN mode. */ 816bfae9329SLuiz Otavio O Souza conf->cmd = ETHERSWITCH_CONF_VLAN_MODE; 817bfae9329SLuiz Otavio O Souza conf->vlan_mode = ETHERSWITCH_VLAN_DOT1Q; 818bfae9329SLuiz Otavio O Souza 819bfae9329SLuiz Otavio O Souza return (0); 820bfae9329SLuiz Otavio O Souza } 821bfae9329SLuiz Otavio O Souza 822bfae9329SLuiz Otavio O Souza static int 823a043e8c7SAdrian Chadd rtl_readphy(device_t dev, int phy, int reg) 824a043e8c7SAdrian Chadd { 825477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 826477e3effSMichael Zhilin uint16_t data; 827a043e8c7SAdrian Chadd int err, i, sleep; 828a043e8c7SAdrian Chadd 829477e3effSMichael Zhilin sc = device_get_softc(dev); 830477e3effSMichael Zhilin 831477e3effSMichael Zhilin data = 0; 832477e3effSMichael Zhilin 833477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS) 834a043e8c7SAdrian Chadd return (ENXIO); 835477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG) 836a043e8c7SAdrian Chadd return (ENXIO); 837a043e8c7SAdrian Chadd sleep = RTL_WAITOK; 838a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 839a043e8c7SAdrian Chadd if (err != 0) 840a043e8c7SAdrian Chadd return (EBUSY); 841a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 842477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_READ, sleep); 843a043e8c7SAdrian Chadd if (err == 0) 844477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), 0, sleep); 845a043e8c7SAdrian Chadd if (err == 0) { 846477e3effSMichael Zhilin err = smi_read_locked(sc, RTL8366_PADR, &data, sleep); 847a043e8c7SAdrian Chadd break; 848a043e8c7SAdrian Chadd } 849a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries); 850a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_readphy(): chip not responsive, retrying %d more times\n", i); 851a043e8c7SAdrian Chadd pause("rtl_readphy", RTL_IICBUS_RETRY_SLEEP); 852a043e8c7SAdrian Chadd } 853a043e8c7SAdrian Chadd smi_release(sc, sleep); 854a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_readphy()=%d: phy=%d.%02x\n", phy, reg); 855a043e8c7SAdrian Chadd return (data); 856a043e8c7SAdrian Chadd } 857a043e8c7SAdrian Chadd 858a043e8c7SAdrian Chadd static int 859a043e8c7SAdrian Chadd rtl_writephy(device_t dev, int phy, int reg, int data) 860a043e8c7SAdrian Chadd { 861477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 862a043e8c7SAdrian Chadd int err, i, sleep; 863a043e8c7SAdrian Chadd 864477e3effSMichael Zhilin sc = device_get_softc(dev); 865477e3effSMichael Zhilin 866477e3effSMichael Zhilin if (phy < 0 || phy >= RTL8366_NUM_PHYS) 867a043e8c7SAdrian Chadd return (ENXIO); 868477e3effSMichael Zhilin if (reg < 0 || reg >= RTL8366_NUM_PHY_REG) 869a043e8c7SAdrian Chadd return (ENXIO); 870a043e8c7SAdrian Chadd sleep = RTL_WAITOK; 871a043e8c7SAdrian Chadd err = smi_acquire(sc, sleep); 872a043e8c7SAdrian Chadd if (err != 0) 873a043e8c7SAdrian Chadd return (EBUSY); 874a043e8c7SAdrian Chadd for (i = RTL_IICBUS_RETRIES; i--; ) { 875477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PACR, RTL8366_PACR_WRITE, sleep); 876a043e8c7SAdrian Chadd if (err == 0) 877477e3effSMichael Zhilin err = smi_write_locked(sc, RTL8366_PHYREG(phy, 0, reg), data, sleep); 878a043e8c7SAdrian Chadd if (err == 0) { 879a043e8c7SAdrian Chadd break; 880a043e8c7SAdrian Chadd } 881a043e8c7SAdrian Chadd DEBUG_INCRVAR(phy_access_retries); 882a043e8c7SAdrian Chadd DPRINTF(dev, "rtl_writephy(): chip not responsive, retrying %d more tiems\n", i); 883a043e8c7SAdrian Chadd pause("rtl_writephy", RTL_IICBUS_RETRY_SLEEP); 884a043e8c7SAdrian Chadd } 885a043e8c7SAdrian Chadd smi_release(sc, sleep); 886a043e8c7SAdrian Chadd DEVERR(dev, err, "rtl_writephy()=%d: phy=%d.%02x\n", phy, reg); 887a043e8c7SAdrian Chadd return (err == 0 ? 0 : EIO); 888a043e8c7SAdrian Chadd } 889a043e8c7SAdrian Chadd 890a043e8c7SAdrian Chadd static int 891a043e8c7SAdrian Chadd rtl8366rb_ifmedia_upd(struct ifnet *ifp) 892a043e8c7SAdrian Chadd { 893477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 894477e3effSMichael Zhilin struct mii_data *mii; 895477e3effSMichael Zhilin 896477e3effSMichael Zhilin sc = ifp->if_softc; 897477e3effSMichael Zhilin mii = device_get_softc(sc->miibus[ifp->if_dunit]); 898a043e8c7SAdrian Chadd 899a043e8c7SAdrian Chadd mii_mediachg(mii); 900a043e8c7SAdrian Chadd return (0); 901a043e8c7SAdrian Chadd } 902a043e8c7SAdrian Chadd 903a043e8c7SAdrian Chadd static void 904a043e8c7SAdrian Chadd rtl8366rb_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 905a043e8c7SAdrian Chadd { 906477e3effSMichael Zhilin struct rtl8366rb_softc *sc; 907477e3effSMichael Zhilin struct mii_data *mii; 908477e3effSMichael Zhilin 909477e3effSMichael Zhilin sc = ifp->if_softc; 910477e3effSMichael Zhilin mii = device_get_softc(sc->miibus[ifp->if_dunit]); 911a043e8c7SAdrian Chadd 912a043e8c7SAdrian Chadd mii_pollstat(mii); 913a043e8c7SAdrian Chadd ifmr->ifm_active = mii->mii_media_active; 914a043e8c7SAdrian Chadd ifmr->ifm_status = mii->mii_media_status; 915a043e8c7SAdrian Chadd } 916a043e8c7SAdrian Chadd 917a043e8c7SAdrian Chadd 918a043e8c7SAdrian Chadd static device_method_t rtl8366rb_methods[] = { 919a043e8c7SAdrian Chadd /* Device interface */ 920a043e8c7SAdrian Chadd DEVMETHOD(device_identify, rtl8366rb_identify), 921a043e8c7SAdrian Chadd DEVMETHOD(device_probe, rtl8366rb_probe), 922a043e8c7SAdrian Chadd DEVMETHOD(device_attach, rtl8366rb_attach), 923a043e8c7SAdrian Chadd DEVMETHOD(device_detach, rtl8366rb_detach), 924a043e8c7SAdrian Chadd 925a043e8c7SAdrian Chadd /* bus interface */ 926a043e8c7SAdrian Chadd DEVMETHOD(bus_add_child, device_add_child_ordered), 927a043e8c7SAdrian Chadd 928a043e8c7SAdrian Chadd /* MII interface */ 929a043e8c7SAdrian Chadd DEVMETHOD(miibus_readreg, rtl_readphy), 930a043e8c7SAdrian Chadd DEVMETHOD(miibus_writereg, rtl_writephy), 931a043e8c7SAdrian Chadd 9325a4380b5SMichael Zhilin /* MDIO interface */ 9335a4380b5SMichael Zhilin DEVMETHOD(mdio_readreg, rtl_readphy), 9345a4380b5SMichael Zhilin DEVMETHOD(mdio_writereg, rtl_writephy), 9355a4380b5SMichael Zhilin 936a043e8c7SAdrian Chadd /* etherswitch interface */ 937bfae9329SLuiz Otavio O Souza DEVMETHOD(etherswitch_getconf, rtl_getconf), 938a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getinfo, rtl_getinfo), 939a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readreg, rtl_readreg), 940a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writereg, rtl_writereg), 941a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_readphyreg, rtl_readphy), 942a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_writephyreg, rtl_writephy), 943a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getport, rtl_getport), 944a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setport, rtl_setport), 945a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_getvgroup, rtl_getvgroup), 946a043e8c7SAdrian Chadd DEVMETHOD(etherswitch_setvgroup, rtl_setvgroup), 947a043e8c7SAdrian Chadd 948a043e8c7SAdrian Chadd DEVMETHOD_END 949a043e8c7SAdrian Chadd }; 950a043e8c7SAdrian Chadd 951a043e8c7SAdrian Chadd DEFINE_CLASS_0(rtl8366rb, rtl8366rb_driver, rtl8366rb_methods, 952a043e8c7SAdrian Chadd sizeof(struct rtl8366rb_softc)); 953a043e8c7SAdrian Chadd static devclass_t rtl8366rb_devclass; 954a043e8c7SAdrian Chadd 955a043e8c7SAdrian Chadd DRIVER_MODULE(rtl8366rb, iicbus, rtl8366rb_driver, rtl8366rb_devclass, 0, 0); 956a043e8c7SAdrian Chadd DRIVER_MODULE(miibus, rtl8366rb, miibus_driver, miibus_devclass, 0, 0); 9575a4380b5SMichael Zhilin DRIVER_MODULE(mdio, rtl8366rb, mdio_driver, mdio_devclass, 0, 0); 958a043e8c7SAdrian Chadd DRIVER_MODULE(etherswitch, rtl8366rb, etherswitch_driver, etherswitch_devclass, 0, 0); 959a043e8c7SAdrian Chadd MODULE_VERSION(rtl8366rb, 1); 960a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, iicbus, 1, 1, 1); /* XXX which versions? */ 961a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, miibus, 1, 1, 1); /* XXX which versions? */ 962a043e8c7SAdrian Chadd MODULE_DEPEND(rtl8366rb, etherswitch, 1, 1, 1); /* XXX which versions? */ 963