1 /*- 2 * Copyright (c) 2012 Semihalf. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 #include <sys/param.h> 29 #include <sys/systm.h> 30 #include <sys/kernel.h> 31 #include <sys/bus.h> 32 #include <sys/module.h> 33 #include <sys/rman.h> 34 #include <sys/socket.h> 35 36 #include <machine/bus.h> 37 38 #include <powerpc/mpc85xx/mpc85xx.h> 39 40 #include <net/if.h> 41 #include <net/if_media.h> 42 43 #include <dev/mii/mii.h> 44 #include <dev/mii/miivar.h> 45 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/ofw_bus_subr.h> 48 #include <dev/ofw/openfirm.h> 49 50 #include "miibus_if.h" 51 52 #include <contrib/ncsw/inc/Peripherals/fm_port_ext.h> 53 #include <contrib/ncsw/inc/xx_ext.h> 54 55 #include "if_dtsec.h" 56 #include "fman.h" 57 58 59 static int dtsec_fdt_probe(device_t dev); 60 static int dtsec_fdt_attach(device_t dev); 61 62 static device_method_t dtsec_methods[] = { 63 /* Device interface */ 64 DEVMETHOD(device_probe, dtsec_fdt_probe), 65 DEVMETHOD(device_attach, dtsec_fdt_attach), 66 DEVMETHOD(device_detach, dtsec_detach), 67 68 DEVMETHOD(device_shutdown, dtsec_shutdown), 69 DEVMETHOD(device_suspend, dtsec_suspend), 70 DEVMETHOD(device_resume, dtsec_resume), 71 72 /* Bus interface */ 73 DEVMETHOD(bus_print_child, bus_generic_print_child), 74 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 75 76 /* MII interface */ 77 DEVMETHOD(miibus_readreg, dtsec_miibus_readreg), 78 DEVMETHOD(miibus_writereg, dtsec_miibus_writereg), 79 DEVMETHOD(miibus_statchg, dtsec_miibus_statchg), 80 81 { 0, 0 } 82 }; 83 84 static driver_t dtsec_driver = { 85 "dtsec", 86 dtsec_methods, 87 sizeof(struct dtsec_softc), 88 }; 89 90 DRIVER_MODULE(dtsec, fman, dtsec_driver, 0, 0); 91 DRIVER_MODULE(miibus, dtsec, miibus_driver, 0, 0); 92 MODULE_DEPEND(dtsec, ether, 1, 1, 1); 93 MODULE_DEPEND(dtsec, miibus, 1, 1, 1); 94 95 static int 96 dtsec_fdt_probe(device_t dev) 97 { 98 99 if (!ofw_bus_status_okay(dev)) 100 return (ENXIO); 101 102 if (!ofw_bus_is_compatible(dev, "fsl,fman-dtsec") && 103 !ofw_bus_is_compatible(dev, "fsl,fman-xgec")) 104 return (ENXIO); 105 106 device_set_desc(dev, "Freescale Data Path Triple Speed Ethernet " 107 "Controller"); 108 109 return (BUS_PROBE_DEFAULT); 110 } 111 112 static int 113 dtsec_fdt_attach(device_t dev) 114 { 115 struct dtsec_softc *sc; 116 device_t phy_dev; 117 phandle_t enet_node, phy_node; 118 phandle_t fman_rxtx_node[2]; 119 char phy_type[6]; 120 pcell_t fman_tx_cell, mac_id; 121 int rid; 122 123 sc = device_get_softc(dev); 124 enet_node = ofw_bus_get_node(dev); 125 126 if (OF_getprop(enet_node, "local-mac-address", 127 (void *)sc->sc_mac_addr, 6) == -1) { 128 device_printf(dev, 129 "Could not load local-mac-addr property from DTS\n"); 130 return (ENXIO); 131 } 132 133 /* Get link speed */ 134 if (ofw_bus_is_compatible(dev, "fsl,fman-dtsec") != 0) 135 sc->sc_eth_dev_type = ETH_DTSEC; 136 else if (ofw_bus_is_compatible(dev, "fsl,fman-xgec") != 0) 137 sc->sc_eth_dev_type = ETH_10GSEC; 138 else 139 return(ENXIO); 140 141 /* Get PHY address */ 142 if (OF_getprop(enet_node, "phy-handle", (void *)&phy_node, 143 sizeof(phy_node)) <= 0) 144 return (ENXIO); 145 146 phy_node = OF_node_from_xref(phy_node); 147 148 if (OF_getprop(phy_node, "reg", (void *)&sc->sc_phy_addr, 149 sizeof(sc->sc_phy_addr)) <= 0) 150 return (ENXIO); 151 152 phy_dev = OF_device_from_xref(OF_parent(phy_node)); 153 154 if (phy_dev == NULL) { 155 device_printf(dev, "No PHY found.\n"); 156 return (ENXIO); 157 } 158 159 sc->sc_mdio = phy_dev; 160 161 /* Get MAC memory offset in SoC */ 162 rid = 0; 163 sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 164 if (sc->sc_mem == NULL) 165 return (ENXIO); 166 167 /* Get PHY connection type */ 168 if (OF_getprop(enet_node, "phy-connection-type", (void *)phy_type, 169 sizeof(phy_type)) <= 0) 170 return (ENXIO); 171 172 if (!strcmp(phy_type, "sgmii")) 173 sc->sc_mac_enet_mode = e_ENET_MODE_SGMII_1000; 174 else if (!strcmp(phy_type, "rgmii")) 175 sc->sc_mac_enet_mode = e_ENET_MODE_RGMII_1000; 176 else if (!strcmp(phy_type, "xgmii")) 177 /* We set 10 Gigabit mode flag however we don't support it */ 178 sc->sc_mac_enet_mode = e_ENET_MODE_XGMII_10000; 179 else 180 return (ENXIO); 181 182 if (OF_getencprop(enet_node, "cell-index", 183 (void *)&mac_id, sizeof(mac_id)) <= 0) 184 return (ENXIO); 185 sc->sc_eth_id = mac_id; 186 187 /* Get RX/TX port handles */ 188 if (OF_getprop(enet_node, "fsl,fman-ports", (void *)fman_rxtx_node, 189 sizeof(fman_rxtx_node)) <= 0) 190 return (ENXIO); 191 192 if (fman_rxtx_node[0] == 0) 193 return (ENXIO); 194 195 if (fman_rxtx_node[1] == 0) 196 return (ENXIO); 197 198 fman_rxtx_node[0] = OF_instance_to_package(fman_rxtx_node[0]); 199 fman_rxtx_node[1] = OF_instance_to_package(fman_rxtx_node[1]); 200 201 if (ofw_bus_node_is_compatible(fman_rxtx_node[0], 202 "fsl,fman-v2-port-rx") == 0) 203 return (ENXIO); 204 205 if (ofw_bus_node_is_compatible(fman_rxtx_node[1], 206 "fsl,fman-v2-port-tx") == 0) 207 return (ENXIO); 208 209 /* Get RX port HW id */ 210 if (OF_getprop(fman_rxtx_node[0], "reg", (void *)&sc->sc_port_rx_hw_id, 211 sizeof(sc->sc_port_rx_hw_id)) <= 0) 212 return (ENXIO); 213 214 /* Get TX port HW id */ 215 if (OF_getprop(fman_rxtx_node[1], "reg", (void *)&sc->sc_port_tx_hw_id, 216 sizeof(sc->sc_port_tx_hw_id)) <= 0) 217 return (ENXIO); 218 219 if (OF_getprop(fman_rxtx_node[1], "cell-index", &fman_tx_cell, 220 sizeof(fman_tx_cell)) <= 0) 221 return (ENXIO); 222 /* Get QMan channel */ 223 sc->sc_port_tx_qman_chan = fman_qman_channel_id(device_get_parent(dev), 224 fman_tx_cell); 225 226 return (dtsec_attach(dev)); 227 } 228