1248dd603SAdrian Chadd /*- 2*718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*718cf2ccSPedro F. Giffuni * 4248dd603SAdrian Chadd * Copyright (c) 2013 Luiz Otavio O Souza. 5248dd603SAdrian Chadd * Copyright (c) 2011-2012 Stefan Bethke. 6248dd603SAdrian Chadd * Copyright (c) 2012 Adrian Chadd. 7248dd603SAdrian Chadd * All rights reserved. 8248dd603SAdrian Chadd * 9248dd603SAdrian Chadd * Redistribution and use in source and binary forms, with or without 10248dd603SAdrian Chadd * modification, are permitted provided that the following conditions 11248dd603SAdrian Chadd * are met: 12248dd603SAdrian Chadd * 1. Redistributions of source code must retain the above copyright 13248dd603SAdrian Chadd * notice, this list of conditions and the following disclaimer. 14248dd603SAdrian Chadd * 2. Redistributions in binary form must reproduce the above copyright 15248dd603SAdrian Chadd * notice, this list of conditions and the following disclaimer in the 16248dd603SAdrian Chadd * documentation and/or other materials provided with the distribution. 17248dd603SAdrian Chadd * 18248dd603SAdrian Chadd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19248dd603SAdrian Chadd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20248dd603SAdrian Chadd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21248dd603SAdrian Chadd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22248dd603SAdrian Chadd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23248dd603SAdrian Chadd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24248dd603SAdrian Chadd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25248dd603SAdrian Chadd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26248dd603SAdrian Chadd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27248dd603SAdrian Chadd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28248dd603SAdrian Chadd * SUCH DAMAGE. 29248dd603SAdrian Chadd * 30248dd603SAdrian Chadd * $FreeBSD$ 31248dd603SAdrian Chadd */ 32248dd603SAdrian Chadd 33248dd603SAdrian Chadd #include <sys/param.h> 34248dd603SAdrian Chadd #include <sys/bus.h> 35248dd603SAdrian Chadd #include <sys/kernel.h> 36248dd603SAdrian Chadd #include <sys/lock.h> 37248dd603SAdrian Chadd #include <sys/mutex.h> 38248dd603SAdrian Chadd #include <sys/systm.h> 39248dd603SAdrian Chadd #include <sys/socket.h> 40248dd603SAdrian Chadd 41248dd603SAdrian Chadd #include <net/if.h> 42248dd603SAdrian Chadd 43248dd603SAdrian Chadd #include <dev/mii/mii.h> 44248dd603SAdrian Chadd 45248dd603SAdrian Chadd #include <dev/etherswitch/etherswitch.h> 46248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_phy.h> 47248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_reg.h> 48248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_var.h> 49248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip17x_vlans.h> 50248dd603SAdrian Chadd #include <dev/etherswitch/ip17x/ip175c.h> 51248dd603SAdrian Chadd 52248dd603SAdrian Chadd /* 53248dd603SAdrian Chadd * IP175C specific functions. 54248dd603SAdrian Chadd */ 55248dd603SAdrian Chadd 56248dd603SAdrian Chadd /* 57248dd603SAdrian Chadd * Reset the switch. 58248dd603SAdrian Chadd */ 59248dd603SAdrian Chadd static int 60248dd603SAdrian Chadd ip175c_reset(struct ip17x_softc *sc) 61248dd603SAdrian Chadd { 62248dd603SAdrian Chadd uint32_t data; 63248dd603SAdrian Chadd 64248dd603SAdrian Chadd /* Reset all the switch settings. */ 65248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, IP175C_RESET_PHY, IP175C_RESET_REG, 66248dd603SAdrian Chadd 0x175c)) 67248dd603SAdrian Chadd return (-1); 686a7a25afSLuiz Otavio O Souza DELAY(2000); 69248dd603SAdrian Chadd 70248dd603SAdrian Chadd /* Force IP175C mode. */ 71248dd603SAdrian Chadd data = ip17x_readphy(sc->sc_dev, IP175C_MODE_PHY, IP175C_MODE_REG); 72248dd603SAdrian Chadd if (data == 0x175a) { 73248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, IP175C_MODE_PHY, IP175C_MODE_REG, 74248dd603SAdrian Chadd 0x175c)) 75248dd603SAdrian Chadd return (-1); 76248dd603SAdrian Chadd } 77248dd603SAdrian Chadd 78248dd603SAdrian Chadd return (0); 79248dd603SAdrian Chadd } 80248dd603SAdrian Chadd 81248dd603SAdrian Chadd static int 82248dd603SAdrian Chadd ip175c_port_vlan_setup(struct ip17x_softc *sc) 83248dd603SAdrian Chadd { 84248dd603SAdrian Chadd struct ip17x_vlan *v; 85248dd603SAdrian Chadd uint32_t ports[IP175X_NUM_PORTS], reg[IP175X_NUM_PORTS/2]; 86248dd603SAdrian Chadd int i, err, phy; 87248dd603SAdrian Chadd 88248dd603SAdrian Chadd KASSERT(sc->cpuport == 5, ("cpuport != 5 not supported for IP175C")); 89248dd603SAdrian Chadd KASSERT(sc->numports == 6, ("numports != 6 not supported for IP175C")); 90248dd603SAdrian Chadd 91248dd603SAdrian Chadd /* Build the port access masks. */ 92248dd603SAdrian Chadd memset(ports, 0, sizeof(ports)); 93248dd603SAdrian Chadd for (i = 0; i < sc->info.es_nports; i++) { 94248dd603SAdrian Chadd phy = sc->portphy[i]; 95248dd603SAdrian Chadd v = &sc->vlan[i]; 96248dd603SAdrian Chadd ports[phy] = v->ports; 97248dd603SAdrian Chadd } 98248dd603SAdrian Chadd 99248dd603SAdrian Chadd /* Move the cpuport bit to its correct place. */ 100248dd603SAdrian Chadd for (i = 0; i < sc->numports; i++) { 101248dd603SAdrian Chadd if (ports[i] & (1 << sc->cpuport)) { 102248dd603SAdrian Chadd ports[i] |= (1 << 7); 103248dd603SAdrian Chadd ports[i] &= ~(1 << sc->cpuport); 104248dd603SAdrian Chadd } 105248dd603SAdrian Chadd } 106248dd603SAdrian Chadd 107248dd603SAdrian Chadd /* And now build the switch register data. */ 108248dd603SAdrian Chadd memset(reg, 0, sizeof(reg)); 109248dd603SAdrian Chadd for (i = 0; i < (sc->numports / 2); i++) 110248dd603SAdrian Chadd reg[i] = ports[i * 2] << 8 | ports[i * 2 + 1]; 111248dd603SAdrian Chadd 112248dd603SAdrian Chadd /* Update the switch resgisters. */ 113248dd603SAdrian Chadd err = ip17x_writephy(sc->sc_dev, 29, 19, reg[0]); 114248dd603SAdrian Chadd if (err == 0) 115248dd603SAdrian Chadd err = ip17x_writephy(sc->sc_dev, 29, 20, reg[1]); 116248dd603SAdrian Chadd if (err == 0) 117248dd603SAdrian Chadd err = ip17x_updatephy(sc->sc_dev, 29, 21, 0xff00, reg[2]); 118248dd603SAdrian Chadd if (err == 0) 119248dd603SAdrian Chadd err = ip17x_updatephy(sc->sc_dev, 30, 18, 0x00ff, reg[2]); 120248dd603SAdrian Chadd return (err); 121248dd603SAdrian Chadd } 122248dd603SAdrian Chadd 123248dd603SAdrian Chadd static int 124248dd603SAdrian Chadd ip175c_dot1q_vlan_setup(struct ip17x_softc *sc) 125248dd603SAdrian Chadd { 126248dd603SAdrian Chadd struct ip17x_vlan *v; 127248dd603SAdrian Chadd uint32_t data; 128248dd603SAdrian Chadd uint32_t vlans[IP17X_MAX_VLANS]; 129248dd603SAdrian Chadd int i, j; 130248dd603SAdrian Chadd 131248dd603SAdrian Chadd KASSERT(sc->cpuport == 5, ("cpuport != 5 not supported for IP175C")); 132248dd603SAdrian Chadd KASSERT(sc->numports == 6, ("numports != 6 not supported for IP175C")); 133248dd603SAdrian Chadd 134248dd603SAdrian Chadd /* Add and strip VLAN tags. */ 135248dd603SAdrian Chadd data = (sc->addtag & ~(1 << IP175X_CPU_PORT)) << 11; 136248dd603SAdrian Chadd data |= (sc->striptag & ~(1 << IP175X_CPU_PORT)) << 6; 137248dd603SAdrian Chadd if (sc->addtag & (1 << IP175X_CPU_PORT)) 138248dd603SAdrian Chadd data |= (1 << 1); 139248dd603SAdrian Chadd if (sc->striptag & (1 << IP175X_CPU_PORT)) 140248dd603SAdrian Chadd data |= (1 << 0); 141248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, 29, 23, data)) 142248dd603SAdrian Chadd return (-1); 143248dd603SAdrian Chadd 144248dd603SAdrian Chadd /* Set the VID_IDX_SEL to 0. */ 145248dd603SAdrian Chadd if (ip17x_updatephy(sc->sc_dev, 30, 9, 0x70, 0)) 146248dd603SAdrian Chadd return (-1); 147248dd603SAdrian Chadd 148248dd603SAdrian Chadd /* Calculate the port masks. */ 149248dd603SAdrian Chadd memset(vlans, 0, sizeof(vlans)); 150248dd603SAdrian Chadd for (i = 0; i < IP17X_MAX_VLANS; i++) { 151248dd603SAdrian Chadd v = &sc->vlan[i]; 152cc320e37SLuiz Otavio O Souza if ((v->vlanid & ETHERSWITCH_VID_VALID) == 0) 153248dd603SAdrian Chadd continue; 154cc320e37SLuiz Otavio O Souza vlans[v->vlanid & ETHERSWITCH_VID_MASK] = v->ports; 155248dd603SAdrian Chadd } 156248dd603SAdrian Chadd 157248dd603SAdrian Chadd for (j = 0, i = 1; i <= IP17X_MAX_VLANS / 2; i++) { 158248dd603SAdrian Chadd data = vlans[j++] & 0x3f; 159248dd603SAdrian Chadd data |= (vlans[j++] & 0x3f) << 8; 160248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, 30, i, data)) 161248dd603SAdrian Chadd return (-1); 162248dd603SAdrian Chadd } 163248dd603SAdrian Chadd 164248dd603SAdrian Chadd /* Port default VLAN ID. */ 165248dd603SAdrian Chadd for (i = 0; i < sc->numports; i++) { 166248dd603SAdrian Chadd if (i == IP175X_CPU_PORT) { 167248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, 29, 30, sc->pvid[i])) 168248dd603SAdrian Chadd return (-1); 169248dd603SAdrian Chadd } else { 170248dd603SAdrian Chadd if (ip17x_writephy(sc->sc_dev, 29, 24 + i, sc->pvid[i])) 171248dd603SAdrian Chadd return (-1); 172248dd603SAdrian Chadd } 173248dd603SAdrian Chadd } 174248dd603SAdrian Chadd 175248dd603SAdrian Chadd return (0); 176248dd603SAdrian Chadd } 177248dd603SAdrian Chadd 178248dd603SAdrian Chadd /* 179248dd603SAdrian Chadd * Set the Switch configuration. 180248dd603SAdrian Chadd */ 181248dd603SAdrian Chadd static int 182248dd603SAdrian Chadd ip175c_hw_setup(struct ip17x_softc *sc) 183248dd603SAdrian Chadd { 184248dd603SAdrian Chadd 185248dd603SAdrian Chadd switch (sc->vlan_mode) { 186248dd603SAdrian Chadd case ETHERSWITCH_VLAN_PORT: 187248dd603SAdrian Chadd return (ip175c_port_vlan_setup(sc)); 188248dd603SAdrian Chadd break; 189248dd603SAdrian Chadd case ETHERSWITCH_VLAN_DOT1Q: 190248dd603SAdrian Chadd return (ip175c_dot1q_vlan_setup(sc)); 191248dd603SAdrian Chadd break; 192248dd603SAdrian Chadd } 193248dd603SAdrian Chadd return (-1); 194248dd603SAdrian Chadd } 195248dd603SAdrian Chadd 196248dd603SAdrian Chadd /* 197248dd603SAdrian Chadd * Set the switch VLAN mode. 198248dd603SAdrian Chadd */ 199248dd603SAdrian Chadd static int 200248dd603SAdrian Chadd ip175c_set_vlan_mode(struct ip17x_softc *sc, uint32_t mode) 201248dd603SAdrian Chadd { 202248dd603SAdrian Chadd 203248dd603SAdrian Chadd switch (mode) { 204248dd603SAdrian Chadd case ETHERSWITCH_VLAN_DOT1Q: 205248dd603SAdrian Chadd /* Enable VLAN tag processing. */ 206248dd603SAdrian Chadd ip17x_updatephy(sc->sc_dev, 30, 9, 0x80, 0x80); 207248dd603SAdrian Chadd sc->vlan_mode = mode; 208248dd603SAdrian Chadd break; 209248dd603SAdrian Chadd case ETHERSWITCH_VLAN_PORT: 210248dd603SAdrian Chadd default: 211248dd603SAdrian Chadd /* Disable VLAN tag processing. */ 212248dd603SAdrian Chadd ip17x_updatephy(sc->sc_dev, 30, 9, 0x80, 0); 213248dd603SAdrian Chadd sc->vlan_mode = ETHERSWITCH_VLAN_PORT; 214248dd603SAdrian Chadd break; 21574b8d63dSPedro F. Giffuni } 216248dd603SAdrian Chadd 217248dd603SAdrian Chadd /* Reset vlans. */ 218248dd603SAdrian Chadd ip17x_reset_vlans(sc, sc->vlan_mode); 219248dd603SAdrian Chadd 220248dd603SAdrian Chadd /* Update switch configuration. */ 221248dd603SAdrian Chadd ip175c_hw_setup(sc); 222248dd603SAdrian Chadd 223248dd603SAdrian Chadd return (0); 224248dd603SAdrian Chadd } 225248dd603SAdrian Chadd 226248dd603SAdrian Chadd /* 227248dd603SAdrian Chadd * Get the switch VLAN mode. 228248dd603SAdrian Chadd */ 229248dd603SAdrian Chadd static int 230248dd603SAdrian Chadd ip175c_get_vlan_mode(struct ip17x_softc *sc) 231248dd603SAdrian Chadd { 232248dd603SAdrian Chadd 233248dd603SAdrian Chadd return (sc->vlan_mode); 234248dd603SAdrian Chadd } 235248dd603SAdrian Chadd 236248dd603SAdrian Chadd void 237248dd603SAdrian Chadd ip175c_attach(struct ip17x_softc *sc) 238248dd603SAdrian Chadd { 2392567d713SMichael Zhilin uint32_t data; 2402567d713SMichael Zhilin 2412567d713SMichael Zhilin data = ip17x_readphy(sc->sc_dev, IP175C_MII_PHY, IP175C_MII_CTL_REG); 2422567d713SMichael Zhilin device_printf(sc->sc_dev, "MII: %x\n", data); 2432567d713SMichael Zhilin /* check mii1 interface if disabled then phy4 and mac4 hold on switch */ 2442567d713SMichael Zhilin if((data & (1 << IP175C_MII_MII1_RMII_EN)) == 0) 2452567d713SMichael Zhilin sc->phymask |= 0x10; 246248dd603SAdrian Chadd 247248dd603SAdrian Chadd sc->hal.ip17x_reset = ip175c_reset; 248248dd603SAdrian Chadd sc->hal.ip17x_hw_setup = ip175c_hw_setup; 249248dd603SAdrian Chadd sc->hal.ip17x_get_vlan_mode = ip175c_get_vlan_mode; 250248dd603SAdrian Chadd sc->hal.ip17x_set_vlan_mode = ip175c_set_vlan_mode; 251248dd603SAdrian Chadd 252248dd603SAdrian Chadd /* Defaults for IP175C. */ 253248dd603SAdrian Chadd sc->cpuport = IP175X_CPU_PORT; 254248dd603SAdrian Chadd sc->numports = IP175X_NUM_PORTS; 255248dd603SAdrian Chadd sc->info.es_vlan_caps = ETHERSWITCH_VLAN_PORT | ETHERSWITCH_VLAN_DOT1Q; 256248dd603SAdrian Chadd 257248dd603SAdrian Chadd device_printf(sc->sc_dev, "type: IP175C\n"); 258248dd603SAdrian Chadd } 259