1 /*- 2 * Copyright (c) 2014 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 KSZ9021 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 63 #define MII_KSZPHY_EXTREG 0x0b 64 #define KSZPHY_EXTREG_WRITE (1 << 15) 65 #define MII_KSZPHY_EXTREG_WRITE 0x0c 66 #define MII_KSZPHY_EXTREG_READ 0x0d 67 #define MII_KSZPHY_CLK_CONTROL_PAD_SKEW 0x104 68 #define MII_KSZPHY_RX_DATA_PAD_SKEW 0x105 69 #define MII_KSZPHY_TX_DATA_PAD_SKEW 0x106 70 /* KSZ9031 */ 71 #define MII_KSZ9031_MMD_ACCESS_CTRL 0x0d 72 #define MII_KSZ9031_MMD_ACCESS_DATA 0x0e 73 #define MII_KSZ9031_MMD_DATA_NOINC (1 << 14) 74 #define MII_KSZ9031_CONTROL_PAD_SKEW 0x4 75 #define MII_KSZ9031_RX_DATA_PAD_SKEW 0x5 76 #define MII_KSZ9031_TX_DATA_PAD_SKEW 0x6 77 #define MII_KSZ9031_CLOCK_PAD_SKEW 0x8 78 79 #define PS_TO_REG(p) ((p) / 200) 80 81 static int micphy_probe(device_t); 82 static int micphy_attach(device_t); 83 static int micphy_service(struct mii_softc *, struct mii_data *, int); 84 85 static device_method_t micphy_methods[] = { 86 /* device interface */ 87 DEVMETHOD(device_probe, micphy_probe), 88 DEVMETHOD(device_attach, micphy_attach), 89 DEVMETHOD(device_detach, mii_phy_detach), 90 DEVMETHOD(device_shutdown, bus_generic_shutdown), 91 DEVMETHOD_END 92 }; 93 94 static devclass_t micphy_devclass; 95 96 static driver_t micphy_driver = { 97 "micphy", 98 micphy_methods, 99 sizeof(struct mii_softc) 100 }; 101 102 DRIVER_MODULE(micphy, miibus, micphy_driver, micphy_devclass, 0, 0); 103 104 static const struct mii_phydesc micphys[] = { 105 MII_PHY_DESC(MICREL, KSZ9021), 106 MII_PHY_DESC(MICREL, KSZ9031), 107 MII_PHY_END 108 }; 109 110 static const struct mii_phy_funcs micphy_funcs = { 111 micphy_service, 112 ukphy_status, 113 mii_phy_reset 114 }; 115 116 static uint32_t 117 ksz9031_read(struct mii_softc *sc, uint32_t devaddr, uint32_t reg) 118 { 119 /* Set up device address and register. */ 120 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_CTRL, devaddr); 121 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_DATA, reg); 122 123 /* Select register data for MMD and read the value. */ 124 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_CTRL, 125 MII_KSZ9031_MMD_DATA_NOINC | devaddr); 126 127 return (PHY_READ(sc, MII_KSZ9031_MMD_ACCESS_DATA)); 128 } 129 130 static void 131 ksz9031_write(struct mii_softc *sc, uint32_t devaddr, uint32_t reg, 132 uint32_t val) 133 { 134 135 /* Set up device address and register. */ 136 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_CTRL, devaddr); 137 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_DATA, reg); 138 139 /* Select register data for MMD and write the value. */ 140 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_CTRL, 141 MII_KSZ9031_MMD_DATA_NOINC | devaddr); 142 PHY_WRITE(sc, MII_KSZ9031_MMD_ACCESS_DATA, val); 143 } 144 145 static uint32_t 146 ksz9021_read(struct mii_softc *sc, uint32_t reg) 147 { 148 149 PHY_WRITE(sc, MII_KSZPHY_EXTREG, reg); 150 151 return (PHY_READ(sc, MII_KSZPHY_EXTREG_READ)); 152 } 153 154 static void 155 ksz9021_write(struct mii_softc *sc, uint32_t reg, uint32_t val) 156 { 157 158 PHY_WRITE(sc, MII_KSZPHY_EXTREG, KSZPHY_EXTREG_WRITE | reg); 159 PHY_WRITE(sc, MII_KSZPHY_EXTREG_WRITE, val); 160 } 161 162 static void 163 ksz90x1_load_values(struct mii_softc *sc, phandle_t node, 164 uint32_t dev, uint32_t reg, char *field1, uint32_t f1mask, int f1off, 165 char *field2, uint32_t f2mask, int f2off, char *field3, uint32_t f3mask, 166 int f3off, char *field4, uint32_t f4mask, int f4off) 167 { 168 pcell_t dts_value[1]; 169 int len; 170 int val; 171 172 if (sc->mii_mpd_model == MII_MODEL_MICREL_KSZ9031) 173 val = ksz9031_read(sc, dev, reg); 174 else 175 val = ksz9021_read(sc, reg); 176 177 if ((len = OF_getproplen(node, field1)) > 0) { 178 OF_getencprop(node, field1, dts_value, len); 179 val &= ~(f1mask << f1off); 180 val |= (PS_TO_REG(dts_value[0]) & f1mask) << f1off; 181 } 182 183 if (field2 != NULL && (len = OF_getproplen(node, field2)) > 0) { 184 OF_getencprop(node, field2, dts_value, len); 185 val &= ~(f2mask << f2off); 186 val |= (PS_TO_REG(dts_value[0]) & f2mask) << f2off; 187 } 188 189 if (field3 != NULL && (len = OF_getproplen(node, field3)) > 0) { 190 OF_getencprop(node, field3, dts_value, len); 191 val &= ~(f3mask << f3off); 192 val |= (PS_TO_REG(dts_value[0]) & f3mask) << f3off; 193 } 194 195 if (field4 != NULL && (len = OF_getproplen(node, field4)) > 0) { 196 OF_getencprop(node, field4, dts_value, len); 197 val &= ~(f4mask << f4off); 198 val |= (PS_TO_REG(dts_value[0]) & f4mask) << f4off; 199 } 200 201 if (sc->mii_mpd_model == MII_MODEL_MICREL_KSZ9031) 202 ksz9031_write(sc, dev, reg, val); 203 else 204 ksz9021_write(sc, reg, val); 205 } 206 207 static void 208 ksz9031_load_values(struct mii_softc *sc, phandle_t node) 209 { 210 211 ksz90x1_load_values(sc, node, 2, MII_KSZ9031_CONTROL_PAD_SKEW, 212 "txen-skew-ps", 0xf, 0, "rxdv-skew-ps", 0xf, 4, 213 NULL, 0, 0, NULL, 0, 0); 214 ksz90x1_load_values(sc, node, 2, MII_KSZ9031_RX_DATA_PAD_SKEW, 215 "rxd0-skew-ps", 0xf, 0, "rxd1-skew-ps", 0xf, 4, 216 "rxd2-skew-ps", 0xf, 8, "rxd3-skew-ps", 0xf, 12); 217 ksz90x1_load_values(sc, node, 2, MII_KSZ9031_TX_DATA_PAD_SKEW, 218 "txd0-skew-ps", 0xf, 0, "txd1-skew-ps", 0xf, 4, 219 "txd2-skew-ps", 0xf, 8, "txd3-skew-ps", 0xf, 12); 220 ksz90x1_load_values(sc, node, 2, MII_KSZ9031_CLOCK_PAD_SKEW, 221 "rxc-skew-ps", 0x1f, 0, "txc-skew-ps", 0x1f, 5, 222 NULL, 0, 0, NULL, 0, 0); 223 } 224 225 static void 226 ksz9021_load_values(struct mii_softc *sc, phandle_t node) 227 { 228 229 ksz90x1_load_values(sc, node, 0, MII_KSZPHY_CLK_CONTROL_PAD_SKEW, 230 "txen-skew-ps", 0xf, 0, "txc-skew-ps", 0xf, 4, 231 "rxdv-skew-ps", 0xf, 8, "rxc-skew-ps", 0xf, 12); 232 ksz90x1_load_values(sc, node, 0, MII_KSZPHY_RX_DATA_PAD_SKEW, 233 "rxd0-skew-ps", 0xf, 0, "rxd1-skew-ps", 0xf, 4, 234 "rxd2-skew-ps", 0xf, 8, "rxd3-skew-ps", 0xf, 12); 235 ksz90x1_load_values(sc, node, 0, MII_KSZPHY_TX_DATA_PAD_SKEW, 236 "txd0-skew-ps", 0xf, 0, "txd1-skew-ps", 0xf, 4, 237 "txd2-skew-ps", 0xf, 8, "txd3-skew-ps", 0xf, 12); 238 } 239 240 static int 241 micphy_probe(device_t dev) 242 { 243 244 return (mii_phy_dev_probe(dev, micphys, BUS_PROBE_DEFAULT)); 245 } 246 247 static int 248 micphy_attach(device_t dev) 249 { 250 struct mii_softc *sc; 251 phandle_t node; 252 device_t miibus; 253 device_t parent; 254 255 sc = device_get_softc(dev); 256 257 mii_phy_dev_attach(dev, MIIF_NOMANPAUSE, &micphy_funcs, 1); 258 mii_phy_setmedia(sc); 259 260 miibus = device_get_parent(dev); 261 parent = device_get_parent(miibus); 262 263 if ((node = ofw_bus_get_node(parent)) == -1) 264 return (ENXIO); 265 266 if (sc->mii_mpd_model == MII_MODEL_MICREL_KSZ9031) 267 ksz9031_load_values(sc, node); 268 else 269 ksz9021_load_values(sc, node); 270 271 return (0); 272 } 273 274 static int 275 micphy_service(struct mii_softc *sc, struct mii_data *mii, int cmd) 276 { 277 278 switch (cmd) { 279 case MII_POLLSTAT: 280 break; 281 282 case MII_MEDIACHG: 283 mii_phy_setmedia(sc); 284 break; 285 286 case MII_TICK: 287 if (mii_phy_tick(sc) == EJUSTRETURN) 288 return (0); 289 break; 290 } 291 292 /* Update the media status. */ 293 PHY_STATUS(sc); 294 295 /* Callback if something changed. */ 296 mii_phy_update(sc, cmd); 297 return (0); 298 } 299