1 /*- 2 * Copyright (c) 2014,2019 Ruslan Bukin <br@bsdpad.com> 3 * All rights reserved. 4 * 5 * This software was developed by SRI International and the University of 6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) 7 * ("CTSRD"), as part of the DARPA CRASH research programme. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 /* 35 * Micrel KSZ8081/KSZ9021/KSZ9031 Gigabit Ethernet Transceiver 36 */ 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/kernel.h> 41 #include <sys/socket.h> 42 #include <sys/errno.h> 43 #include <sys/module.h> 44 #include <sys/bus.h> 45 #include <sys/malloc.h> 46 47 #include <machine/bus.h> 48 49 #include <net/if.h> 50 #include <net/if_media.h> 51 52 #include <dev/mii/mii.h> 53 #include <dev/mii/miivar.h> 54 #include "miidevs.h" 55 56 #include "miibus_if.h" 57 58 #include <dev/fdt/fdt_common.h> 59 #include <dev/ofw/openfirm.h> 60 #include <dev/ofw/ofw_bus.h> 61 #include <dev/ofw/ofw_bus_subr.h> 62 #include <dev/mii/mii_fdt.h> 63 64 #define MII_KSZPHY_EXTREG 0x0b 65 #define KSZPHY_EXTREG_WRITE (1 << 15) 66 #define MII_KSZPHY_EXTREG_WRITE 0x0c 67 #define MII_KSZPHY_EXTREG_READ 0x0d 68 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104 69 #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105 70 #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106 71 /* KSZ9031 */ 72 #define MII_KSZ9031_MMD_ACCESS_CTRL 0x0d 73 #define MII_KSZ9031_MMD_ACCESS_DATA 0x0e 74 #define MII_KSZ9031_MMD_DATA_NOINC (1 << 14) 75 #define MII_KSZ9031_CONTROL_PAD_SKEW 0x4 76 #define MII_KSZ9031_RX_DATA_PAD_SKEW 0x5 77 #define MII_KSZ9031_TX_DATA_PAD_SKEW 0x6 78 #define MII_KSZ9031_CLOCK_PAD_SKEW 0x8 79 80 #define MII_KSZ8081_PHYCTL2 0x1f 81 82 #define PS_TO_REG(p) ((p) / 200) 83 84 static int micphy_probe(device_t); 85 static int micphy_attach(device_t); 86 static void micphy_reset(struct mii_softc *); 87 static int micphy_service(struct mii_softc *, struct mii_data *, int); 88 89 static device_method_t micphy_methods[] = { 90 /* device interface */ 91 DEVMETHOD(device_probe, micphy_probe), 92 DEVMETHOD(device_attach, micphy_attach), 93 DEVMETHOD(device_detach, mii_phy_detach), 94 DEVMETHOD(device_shutdown, bus_generic_shutdown), 95 DEVMETHOD_END 96 }; 97 98 static driver_t micphy_driver = { 99 "micphy", 100 micphy_methods, 101 sizeof(struct mii_softc) 102 }; 103 104 DRIVER_MODULE(micphy, miibus, micphy_driver, 0, 0); 105 106 static const struct mii_phydesc micphys[] = { 107 MII_PHY_DESC(MICREL, KSZ8081), 108 MII_PHY_DESC(MICREL, KSZ9021), 109 MII_PHY_DESC(MICREL, KSZ9031), 110 MII_PHY_END 111 }; 112 113 static const struct mii_phy_funcs micphy_funcs = { 114 micphy_service, 115 ukphy_status, 116 micphy_reset 117 }; 118 119 static uint32_t 120 ksz9031_read(struct mii_softc *sc, uint32_t devaddr, uint32_t reg) 121 { 122 /* Set up device address and register. */ 123 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_CTRL, devaddr); 124 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_DATA, reg); 125 126 /* Select register data for MMD and read the value. */ 127 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_CTRL, 128 MII_KSZ9031_MMD_DATA_NOINC | devaddr); 129 130 return (PHY_READ(sc, MII_KSZ9031_MMD_ACCESS_DATA)); 131 } 132 133 static void 134 ksz9031_write(struct mii_softc *sc, uint32_t devaddr, uint32_t reg, 135 uint32_t val) 136 { 137 138 /* Set up device address and register. */ 139 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_CTRL, devaddr); 140 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_DATA, reg); 141 142 /* Select register data for MMD and write the value. */ 143 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_CTRL, 144 MII_KSZ9031_MMD_DATA_NOINC | devaddr); 145 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_DATA, val); 146 } 147 148 static uint32_t 149 ksz9021_read(struct mii_softc *sc, uint32_t reg) 150 { 151 152 PHY_WRITE(sc, MII_KSZPHY_EXTREG, reg); 153 154 return (PHY_READ(sc, MII_KSZPHY_EXTREG_READ)); 155 } 156 157 static void 158 ksz9021_write(struct mii_softc *sc, uint32_t reg, uint32_t val) 159 { 160 161 PHY_WRITE(sc, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | reg); 162 PHY_WRITE(sc, MII_KSZPHY_EXTREG_WRITE, val); 163 } 164 165 static void 166 ksz90x1_load_values(struct mii_softc *sc, phandle_t node, 167 uint32_t dev, uint32_t reg, char *field1, uint32_t f1mask, int f1off, 168 char *field2, uint32_t f2mask, int f2off, char *field3, uint32_t f3mask, 169 int f3off, char *field4, uint32_t f4mask, int f4off) 170 { 171 pcell_t dts_value[1]; 172 int len; 173 int val; 174 175 if (sc->mii_mpd_model == MII_MODEL_MICREL_KSZ9031) 176 val = ksz9031_read(sc, dev, reg); 177 else 178 val = ksz9021_read(sc, reg); 179 180 if ((len = OF_getproplen(node, field1)) > 0) { 181 OF_getencprop(node, field1, dts_value, len); 182 val &= ~(f1mask << f1off); 183 val |= (PS_TO_REG(dts_value[0]) & f1mask) << f1off; 184 } 185 186 if (field2 != NULL && (len = OF_getproplen(node, field2)) > 0) { 187 OF_getencprop(node, field2, dts_value, len); 188 val &= ~(f2mask << f2off); 189 val |= (PS_TO_REG(dts_value[0]) & f2mask) << f2off; 190 } 191 192 if (field3 != NULL && (len = OF_getproplen(node, field3)) > 0) { 193 OF_getencprop(node, field3, dts_value, len); 194 val &= ~(f3mask << f3off); 195 val |= (PS_TO_REG(dts_value[0]) & f3mask) << f3off; 196 } 197 198 if (field4 != NULL && (len = OF_getproplen(node, field4)) > 0) { 199 OF_getencprop(node, field4, dts_value, len); 200 val &= ~(f4mask << f4off); 201 val |= (PS_TO_REG(dts_value[0]) & f4mask) << f4off; 202 } 203 204 if (sc->mii_mpd_model == MII_MODEL_MICREL_KSZ9031) 205 ksz9031_write(sc, dev, reg, val); 206 else 207 ksz9021_write(sc, reg, val); 208 } 209 210 static void 211 ksz9031_load_values(struct mii_softc *sc, phandle_t node) 212 { 213 214 ksz90x1_load_values(sc, node, 2, MII_KSZ9031_CONTROL_PAD_SKEW, 215 "txen-skew-ps", 0xf, 0, "rxdv-skew-ps", 0xf, 4, 216 NULL, 0, 0, NULL, 0, 0); 217 ksz90x1_load_values(sc, node, 2, MII_KSZ9031_RX_DATA_PAD_SKEW, 218 "rxd0-skew-ps", 0xf, 0, "rxd1-skew-ps", 0xf, 4, 219 "rxd2-skew-ps", 0xf, 8, "rxd3-skew-ps", 0xf, 12); 220 ksz90x1_load_values(sc, node, 2, MII_KSZ9031_TX_DATA_PAD_SKEW, 221 "txd0-skew-ps", 0xf, 0, "txd1-skew-ps", 0xf, 4, 222 "txd2-skew-ps", 0xf, 8, "txd3-skew-ps", 0xf, 12); 223 ksz90x1_load_values(sc, node, 2, MII_KSZ9031_CLOCK_PAD_SKEW, 224 "rxc-skew-ps", 0x1f, 0, "txc-skew-ps", 0x1f, 5, 225 NULL, 0, 0, NULL, 0, 0); 226 } 227 228 static void 229 ksz9021_load_values(struct mii_softc *sc, phandle_t node) 230 { 231 232 ksz90x1_load_values(sc, node, 0, MII_KSZPHY_CLK_CONTROL_PAD_SKEW, 233 "txen-skew-ps", 0xf, 0, "txc-skew-ps", 0xf, 4, 234 "rxdv-skew-ps", 0xf, 8, "rxc-skew-ps", 0xf, 12); 235 ksz90x1_load_values(sc, node, 0, MII_KSZPHY_RX_DATA_PAD_SKEW, 236 "rxd0-skew-ps", 0xf, 0, "rxd1-skew-ps", 0xf, 4, 237 "rxd2-skew-ps", 0xf, 8, "rxd3-skew-ps", 0xf, 12); 238 ksz90x1_load_values(sc, node, 0, MII_KSZPHY_TX_DATA_PAD_SKEW, 239 "txd0-skew-ps", 0xf, 0, "txd1-skew-ps", 0xf, 4, 240 "txd2-skew-ps", 0xf, 8, "txd3-skew-ps", 0xf, 12); 241 } 242 243 static int 244 micphy_probe(device_t dev) 245 { 246 247 return (mii_phy_dev_probe(dev, micphys, BUS_PROBE_DEFAULT)); 248 } 249 250 static int 251 micphy_attach(device_t dev) 252 { 253 mii_fdt_phy_config_t *cfg; 254 struct mii_softc *sc; 255 phandle_t node; 256 device_t miibus; 257 device_t parent; 258 259 sc = device_get_softc(dev); 260 261 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &micphy_funcs, 1); 262 mii_phy_setmedia(sc); 263 264 /* Nothing further to configure for 8081 model. */ 265 if (sc->mii_mpd_model == MII_MODEL_MICREL_KSZ8081) 266 return (0); 267 268 miibus = device_get_parent(dev); 269 parent = device_get_parent(miibus); 270 271 if ((node = ofw_bus_get_node(parent)) == -1) 272 return (ENXIO); 273 274 cfg = mii_fdt_get_config(dev); 275 276 if (sc->mii_mpd_model == MII_MODEL_MICREL_KSZ9031) 277 ksz9031_load_values(sc, cfg->phynode); 278 else 279 ksz9021_load_values(sc, cfg->phynode); 280 281 return (0); 282 } 283 284 static void 285 micphy_reset(struct mii_softc *sc) 286 { 287 int reg; 288 289 /* 290 * The 8081 has no "sticky bits" that survive a soft reset; several bits 291 * in the Phy Control Register 2 must be preserved across the reset. 292 * These bits are set up by the bootloader; they control how the phy 293 * interfaces to the board (such as clock frequency and LED behavior). 294 */ 295 if (sc->mii_mpd_model == MII_MODEL_MICREL_KSZ8081) 296 reg = PHY_READ(sc, MII_KSZ8081_PHYCTL2); 297 mii_phy_reset(sc); 298 if (sc->mii_mpd_model == MII_MODEL_MICREL_KSZ8081) 299 PHY_WRITE(sc, MII_KSZ8081_PHYCTL2, reg); 300 } 301 302 static int 303 micphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 304 { 305 306 switch (cmd) { 307 case MII_POLLSTAT: 308 break; 309 310 case MII_MEDIACHG: 311 mii_phy_setmedia(sc); 312 break; 313 314 case MII_TICK: 315 if (mii_phy_tick(sc) == EJUSTRETURN) 316 return (0); 317 break; 318 } 319 320 /* Update the media status. */ 321 PHY_STATUS(sc); 322 323 /* Callback if something changed. */ 324 mii_phy_update(sc, cmd); 325 return (0); 326 } 327