1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2011-2012 Stefan Bethke. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/bus.h> 33 #include <sys/errno.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/socket.h> 37 #include <sys/sockio.h> 38 #include <sys/sysctl.h> 39 #include <sys/systm.h> 40 41 #include <net/if.h> 42 #include <net/if_arp.h> 43 #include <net/ethernet.h> 44 #include <net/if_dl.h> 45 #include <net/if_media.h> 46 #include <net/if_types.h> 47 48 #include <machine/bus.h> 49 #include <dev/iicbus/iic.h> 50 #include <dev/iicbus/iiconf.h> 51 #include <dev/iicbus/iicbus.h> 52 #include <dev/mii/mii.h> 53 #include <dev/mii/miivar.h> 54 #include <dev/mdio/mdio.h> 55 56 #include <dev/etherswitch/etherswitch.h> 57 58 #include <dev/etherswitch/arswitch/arswitchreg.h> 59 #include <dev/etherswitch/arswitch/arswitchvar.h> 60 #include <dev/etherswitch/arswitch/arswitch_reg.h> 61 62 #include "mdio_if.h" 63 #include "miibus_if.h" 64 #include "etherswitch_if.h" 65 66 static inline void 67 arswitch_split_setpage(device_t dev, uint32_t addr, uint16_t *phy, 68 uint16_t *reg) 69 { 70 struct arswitch_softc *sc = device_get_softc(dev); 71 uint16_t page; 72 73 page = (addr >> 9) & 0x1ff; 74 *phy = (addr >> 6) & 0x7; 75 *reg = (addr >> 1) & 0x1f; 76 77 if (sc->page != page) { 78 MDIO_WRITEREG(device_get_parent(dev), 0x18, 0, page); 79 DELAY(2000); 80 sc->page = page; 81 } 82 } 83 84 /* 85 * Read half a register. Some of the registers define control bits, and 86 * the sequence of half-word accesses matters. The register addresses 87 * are word-even (mod 4). 88 */ 89 static inline int 90 arswitch_readreg16(device_t dev, int addr) 91 { 92 uint16_t phy, reg; 93 94 arswitch_split_setpage(dev, addr, &phy, ®); 95 return (MDIO_READREG(device_get_parent(dev), 0x10 | phy, reg)); 96 } 97 98 /* 99 * Write half a register. See above! 100 */ 101 static inline int 102 arswitch_writereg16(device_t dev, int addr, int data) 103 { 104 uint16_t phy, reg; 105 106 arswitch_split_setpage(dev, addr, &phy, ®); 107 return (MDIO_WRITEREG(device_get_parent(dev), 0x10 | phy, reg, data)); 108 } 109 110 void 111 arswitch_writedbg(device_t dev, int phy, uint16_t dbg_addr, 112 uint16_t dbg_data) 113 { 114 (void) MDIO_WRITEREG(device_get_parent(dev), phy, 115 MII_ATH_DBG_ADDR, dbg_addr); 116 (void) MDIO_WRITEREG(device_get_parent(dev), phy, 117 MII_ATH_DBG_DATA, dbg_data); 118 } 119 120 void 121 arswitch_writemmd(device_t dev, int phy, uint16_t dbg_addr, 122 uint16_t dbg_data) 123 { 124 (void) MDIO_WRITEREG(device_get_parent(dev), phy, 125 MII_ATH_MMD_ADDR, dbg_addr); 126 (void) MDIO_WRITEREG(device_get_parent(dev), phy, 127 MII_ATH_MMD_DATA, dbg_data); 128 } 129 130 static uint32_t 131 arswitch_reg_read32(device_t dev, int phy, int reg) 132 { 133 uint16_t lo, hi; 134 lo = MDIO_READREG(device_get_parent(dev), phy, reg); 135 hi = MDIO_READREG(device_get_parent(dev), phy, reg + 1); 136 137 return (hi << 16) | lo; 138 } 139 140 static int 141 arswitch_reg_write32(device_t dev, int phy, int reg, uint32_t value) 142 { 143 struct arswitch_softc *sc; 144 int r; 145 uint16_t lo, hi; 146 147 sc = device_get_softc(dev); 148 lo = value & 0xffff; 149 hi = (uint16_t) (value >> 16); 150 151 if (sc->mii_lo_first) { 152 r = MDIO_WRITEREG(device_get_parent(dev), 153 phy, reg, lo); 154 r |= MDIO_WRITEREG(device_get_parent(dev), 155 phy, reg + 1, hi); 156 } else { 157 r = MDIO_WRITEREG(device_get_parent(dev), 158 phy, reg + 1, hi); 159 r |= MDIO_WRITEREG(device_get_parent(dev), 160 phy, reg, lo); 161 } 162 163 return r; 164 } 165 166 int 167 arswitch_readreg(device_t dev, int addr) 168 { 169 uint16_t phy, reg; 170 171 arswitch_split_setpage(dev, addr, &phy, ®); 172 return arswitch_reg_read32(dev, 0x10 | phy, reg); 173 } 174 175 int 176 arswitch_writereg(device_t dev, int addr, int value) 177 { 178 uint16_t phy, reg; 179 180 arswitch_split_setpage(dev, addr, &phy, ®); 181 return (arswitch_reg_write32(dev, 0x10 | phy, reg, value)); 182 } 183 184 /* 185 * Read/write 16 bit values in the switch register space. 186 * 187 * Some of the registers are control registers (eg the MDIO 188 * data versus control space) and so need to be treated 189 * differently. 190 */ 191 int 192 arswitch_readreg_lsb(device_t dev, int addr) 193 { 194 195 return (arswitch_readreg16(dev, addr)); 196 } 197 198 int 199 arswitch_readreg_msb(device_t dev, int addr) 200 { 201 202 return (arswitch_readreg16(dev, addr + 2) << 16); 203 } 204 205 int 206 arswitch_writereg_lsb(device_t dev, int addr, int data) 207 { 208 209 return (arswitch_writereg16(dev, addr, data & 0xffff)); 210 } 211 212 int 213 arswitch_writereg_msb(device_t dev, int addr, int data) 214 { 215 216 return (arswitch_writereg16(dev, addr + 2, (data >> 16) & 0xffff)); 217 } 218 219 int 220 arswitch_modifyreg(device_t dev, int addr, int mask, int set) 221 { 222 int value; 223 uint16_t phy, reg; 224 225 ARSWITCH_LOCK_ASSERT((struct arswitch_softc *)device_get_softc(dev), 226 MA_OWNED); 227 228 arswitch_split_setpage(dev, addr, &phy, ®); 229 230 value = arswitch_reg_read32(dev, 0x10 | phy, reg); 231 value &= ~mask; 232 value |= set; 233 return (arswitch_reg_write32(dev, 0x10 | phy, reg, value)); 234 } 235 236 int 237 arswitch_waitreg(device_t dev, int addr, int mask, int val, int timeout) 238 { 239 struct arswitch_softc *sc = device_get_softc(dev); 240 int err, v; 241 uint16_t phy, reg; 242 243 ARSWITCH_LOCK_ASSERT(sc, MA_OWNED); 244 245 arswitch_split_setpage(dev, addr, &phy, ®); 246 247 err = -1; 248 while (1) { 249 v = arswitch_reg_read32(dev, 0x10 | phy, reg); 250 v &= mask; 251 if (v == val) { 252 err = 0; 253 break; 254 } 255 if (!timeout) 256 break; 257 DELAY(1); 258 timeout--; 259 } 260 if (err != 0) { 261 DPRINTF(sc, ARSWITCH_DBG_ANY, 262 "%s: waitreg failed; addr=0x%08x, mask=0x%08x, val=0x%08x\n", 263 __func__, addr, mask, val); 264 } 265 return (err); 266 } 267