1 /*- 2 * Copyright (C) 2007-2008 Semihalf, Rafal Jaworowski 3 * Copyright (C) 2006-2007 Semihalf, Piotr Kruszynski 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 18 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 20 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * From: FreeBSD: head/sys/dev/tsec/if_tsec_ocp.c 188712 2009-02-17 14:59:47Z raj 27 */ 28 29 /* 30 * FDT 'simple-bus' attachment for Freescale TSEC controller. 31 */ 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/endian.h> 38 #include <sys/lock.h> 39 #include <sys/mbuf.h> 40 #include <sys/mutex.h> 41 #include <sys/kernel.h> 42 #include <sys/module.h> 43 #include <sys/socket.h> 44 #include <sys/sysctl.h> 45 46 #include <sys/bus.h> 47 #include <machine/bus.h> 48 #include <sys/rman.h> 49 #include <machine/resource.h> 50 51 #include <net/ethernet.h> 52 #include <net/if.h> 53 #include <net/if_media.h> 54 55 #include <dev/fdt/fdt_common.h> 56 #include <dev/mii/mii.h> 57 #include <dev/mii/miivar.h> 58 #include <dev/ofw/ofw_bus.h> 59 #include <dev/ofw/ofw_bus_subr.h> 60 #include <dev/ofw/openfirm.h> 61 62 #include <dev/tsec/if_tsec.h> 63 #include <dev/tsec/if_tsecreg.h> 64 65 #include "miibus_if.h" 66 67 #define TSEC_RID_TXIRQ 0 68 #define TSEC_RID_RXIRQ 1 69 #define TSEC_RID_ERRIRQ 2 70 71 static int tsec_fdt_probe(device_t dev); 72 static int tsec_fdt_attach(device_t dev); 73 static int tsec_fdt_detach(device_t dev); 74 static int tsec_setup_intr(struct tsec_softc *sc, struct resource **ires, 75 void **ihand, int *irid, driver_intr_t handler, const char *iname); 76 static void tsec_release_intr(struct tsec_softc *sc, struct resource *ires, 77 void *ihand, int irid, const char *iname); 78 79 static device_method_t tsec_methods[] = { 80 /* Device interface */ 81 DEVMETHOD(device_probe, tsec_fdt_probe), 82 DEVMETHOD(device_attach, tsec_fdt_attach), 83 DEVMETHOD(device_detach, tsec_fdt_detach), 84 85 DEVMETHOD(device_shutdown, tsec_shutdown), 86 DEVMETHOD(device_suspend, tsec_suspend), 87 DEVMETHOD(device_resume, tsec_resume), 88 89 /* MII interface */ 90 DEVMETHOD(miibus_readreg, tsec_miibus_readreg), 91 DEVMETHOD(miibus_writereg, tsec_miibus_writereg), 92 DEVMETHOD(miibus_statchg, tsec_miibus_statchg), 93 94 DEVMETHOD_END 95 }; 96 97 static driver_t tsec_fdt_driver = { 98 "tsec", 99 tsec_methods, 100 sizeof(struct tsec_softc), 101 }; 102 103 DRIVER_MODULE(tsec, simplebus, tsec_fdt_driver, tsec_devclass, 0, 0); 104 105 static int 106 tsec_fdt_probe(device_t dev) 107 { 108 struct tsec_softc *sc; 109 uint32_t id; 110 111 if (!ofw_bus_status_okay(dev)) 112 return (ENXIO); 113 114 if (ofw_bus_get_type(dev) == NULL || 115 strcmp(ofw_bus_get_type(dev), "network") != 0) 116 return (ENXIO); 117 118 if (!ofw_bus_is_compatible(dev, "gianfar")) 119 return (ENXIO); 120 121 sc = device_get_softc(dev); 122 123 sc->sc_rrid = 0; 124 sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rrid, 125 RF_ACTIVE); 126 if (sc->sc_rres == NULL) 127 return (ENXIO); 128 129 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 130 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 131 132 /* Check if we are eTSEC (enhanced TSEC) */ 133 id = TSEC_READ(sc, TSEC_REG_ID); 134 sc->is_etsec = ((id >> 16) == TSEC_ETSEC_ID) ? 1 : 0; 135 id |= TSEC_READ(sc, TSEC_REG_ID2); 136 137 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid, sc->sc_rres); 138 139 if (id == 0) { 140 device_printf(dev, "could not identify TSEC type\n"); 141 return (ENXIO); 142 } 143 144 if (sc->is_etsec) 145 device_set_desc(dev, "Enhanced Three-Speed Ethernet Controller"); 146 else 147 device_set_desc(dev, "Three-Speed Ethernet Controller"); 148 149 return (BUS_PROBE_DEFAULT); 150 } 151 152 static int 153 tsec_fdt_attach(device_t dev) 154 { 155 struct tsec_softc *sc; 156 phandle_t phy; 157 int error = 0; 158 159 sc = device_get_softc(dev); 160 sc->dev = dev; 161 sc->node = ofw_bus_get_node(dev); 162 163 /* Get phy address from fdt */ 164 if (OF_getencprop(sc->node, "phy-handle", &phy, sizeof(phy)) <= 0) { 165 device_printf(dev, "PHY not found in device tree"); 166 return (ENXIO); 167 } 168 169 phy = OF_xref_phandle(phy); 170 OF_decode_addr(OF_parent(phy), 0, &sc->phy_bst, &sc->phy_bsh); 171 OF_getencprop(phy, "reg", &sc->phyaddr, sizeof(sc->phyaddr)); 172 173 /* Init timer */ 174 callout_init(&sc->tsec_callout, 1); 175 176 /* Init locks */ 177 mtx_init(&sc->transmit_lock, device_get_nameunit(dev), "TSEC TX lock", 178 MTX_DEF); 179 mtx_init(&sc->receive_lock, device_get_nameunit(dev), "TSEC RX lock", 180 MTX_DEF); 181 mtx_init(&sc->ic_lock, device_get_nameunit(dev), "TSEC IC lock", 182 MTX_DEF); 183 184 /* Allocate IO memory for TSEC registers */ 185 sc->sc_rrid = 0; 186 sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rrid, 187 RF_ACTIVE); 188 if (sc->sc_rres == NULL) { 189 device_printf(dev, "could not allocate IO memory range!\n"); 190 goto fail1; 191 } 192 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 193 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 194 195 /* TSEC attach */ 196 if (tsec_attach(sc) != 0) { 197 device_printf(dev, "could not be configured\n"); 198 goto fail2; 199 } 200 201 /* Set up interrupts (TX/RX/ERR) */ 202 sc->sc_transmit_irid = TSEC_RID_TXIRQ; 203 error = tsec_setup_intr(sc, &sc->sc_transmit_ires, 204 &sc->sc_transmit_ihand, &sc->sc_transmit_irid, 205 tsec_transmit_intr, "TX"); 206 if (error) 207 goto fail2; 208 209 sc->sc_receive_irid = TSEC_RID_RXIRQ; 210 error = tsec_setup_intr(sc, &sc->sc_receive_ires, 211 &sc->sc_receive_ihand, &sc->sc_receive_irid, 212 tsec_receive_intr, "RX"); 213 if (error) 214 goto fail3; 215 216 sc->sc_error_irid = TSEC_RID_ERRIRQ; 217 error = tsec_setup_intr(sc, &sc->sc_error_ires, 218 &sc->sc_error_ihand, &sc->sc_error_irid, 219 tsec_error_intr, "ERR"); 220 if (error) 221 goto fail4; 222 223 return (0); 224 225 fail4: 226 tsec_release_intr(sc, sc->sc_receive_ires, sc->sc_receive_ihand, 227 sc->sc_receive_irid, "RX"); 228 fail3: 229 tsec_release_intr(sc, sc->sc_transmit_ires, sc->sc_transmit_ihand, 230 sc->sc_transmit_irid, "TX"); 231 fail2: 232 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid, sc->sc_rres); 233 fail1: 234 mtx_destroy(&sc->receive_lock); 235 mtx_destroy(&sc->transmit_lock); 236 return (ENXIO); 237 } 238 239 static int 240 tsec_setup_intr(struct tsec_softc *sc, struct resource **ires, void **ihand, 241 int *irid, driver_intr_t handler, const char *iname) 242 { 243 int error; 244 245 *ires = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, irid, RF_ACTIVE); 246 if (*ires == NULL) { 247 device_printf(sc->dev, "could not allocate %s IRQ\n", iname); 248 return (ENXIO); 249 } 250 error = bus_setup_intr(sc->dev, *ires, INTR_TYPE_NET | INTR_MPSAFE, 251 NULL, handler, sc, ihand); 252 if (error) { 253 device_printf(sc->dev, "failed to set up %s IRQ\n", iname); 254 if (bus_release_resource(sc->dev, SYS_RES_IRQ, *irid, *ires)) 255 device_printf(sc->dev, "could not release %s IRQ\n", iname); 256 *ires = NULL; 257 return (error); 258 } 259 return (0); 260 } 261 262 static void 263 tsec_release_intr(struct tsec_softc *sc, struct resource *ires, void *ihand, 264 int irid, const char *iname) 265 { 266 int error; 267 268 if (ires == NULL) 269 return; 270 271 error = bus_teardown_intr(sc->dev, ires, ihand); 272 if (error) 273 device_printf(sc->dev, "bus_teardown_intr() failed for %s intr" 274 ", error %d\n", iname, error); 275 276 error = bus_release_resource(sc->dev, SYS_RES_IRQ, irid, ires); 277 if (error) 278 device_printf(sc->dev, "bus_release_resource() failed for %s " 279 "intr, error %d\n", iname, error); 280 } 281 282 static int 283 tsec_fdt_detach(device_t dev) 284 { 285 struct tsec_softc *sc; 286 int error; 287 288 sc = device_get_softc(dev); 289 290 /* Wait for stopping watchdog */ 291 callout_drain(&sc->tsec_callout); 292 293 /* Stop and release all interrupts */ 294 tsec_release_intr(sc, sc->sc_transmit_ires, sc->sc_transmit_ihand, 295 sc->sc_transmit_irid, "TX"); 296 tsec_release_intr(sc, sc->sc_receive_ires, sc->sc_receive_ihand, 297 sc->sc_receive_irid, "RX"); 298 tsec_release_intr(sc, sc->sc_error_ires, sc->sc_error_ihand, 299 sc->sc_error_irid, "ERR"); 300 301 /* TSEC detach */ 302 tsec_detach(sc); 303 304 /* Free IO memory handler */ 305 if (sc->sc_rres) { 306 error = bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid, 307 sc->sc_rres); 308 if (error) 309 device_printf(dev, "bus_release_resource() failed for" 310 " IO memory, error %d\n", error); 311 } 312 313 /* Destroy locks */ 314 mtx_destroy(&sc->receive_lock); 315 mtx_destroy(&sc->transmit_lock); 316 mtx_destroy(&sc->ic_lock); 317 return (0); 318 } 319 320 void 321 tsec_get_hwaddr(struct tsec_softc *sc, uint8_t *addr) 322 { 323 union { 324 uint32_t reg[2]; 325 uint8_t addr[6]; 326 } hw; 327 int i; 328 329 hw.reg[0] = hw.reg[1] = 0; 330 331 /* Retrieve the hardware address from the device tree. */ 332 i = OF_getprop(sc->node, "local-mac-address", (void *)hw.addr, 6); 333 if (i == 6 && (hw.reg[0] != 0 || hw.reg[1] != 0)) { 334 bcopy(hw.addr, addr, 6); 335 return; 336 } 337 338 /* Also try the mac-address property, which is second-best */ 339 i = OF_getprop(sc->node, "mac-address", (void *)hw.addr, 6); 340 if (i == 6 && (hw.reg[0] != 0 || hw.reg[1] != 0)) { 341 bcopy(hw.addr, addr, 6); 342 return; 343 } 344 345 /* 346 * Fall back -- use the currently programmed address in the hope that 347 * it was set be firmware... 348 */ 349 hw.reg[0] = TSEC_READ(sc, TSEC_REG_MACSTNADDR1); 350 hw.reg[1] = TSEC_READ(sc, TSEC_REG_MACSTNADDR2); 351 for (i = 0; i < 6; i++) 352 addr[5-i] = hw.addr[i]; 353 } 354