if_atse_nexus.c revision 256752
1/*- 2 * Copyright (c) 2012,2013 Bjoern A. Zeeb 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-11-C-0249) 7 * ("MRC2"), as part of the DARPA MRC 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: head/sys/dev/altera/atse/if_atse_nexus.c 256752 2013-10-18 20:44:19Z brooks $"); 33 34#include "opt_device_polling.h" 35 36#include <sys/param.h> 37#include <sys/kernel.h> 38#include <sys/bus.h> 39#include <sys/module.h> 40#include <sys/rman.h> 41#include <sys/socket.h> 42#include <sys/types.h> 43 44#include <machine/bus.h> 45#include <machine/resource.h> 46 47#include <net/ethernet.h> 48#include <net/if.h> 49#include <net/if_media.h> 50 51#include <dev/mii/mii.h> 52#include <dev/mii/miivar.h> 53 54#include <dev/altera/atse/if_atsereg.h> 55 56/* "device miibus" required. See GENERIC if you get errors here. */ 57#include "miibus_if.h" 58 59MODULE_DEPEND(atse, ether, 1, 1, 1); 60MODULE_DEPEND(atse, miibus, 1, 1, 1); 61 62/* 63 * Device routines for interacting with nexus (probe, attach, detach) & helpers. 64 * XXX We should add suspend/resume later. 65 */ 66static int 67atse_resource_int(device_t dev, const char *resname, int *v) 68{ 69 int error; 70 71 error = resource_int_value(device_get_name(dev), device_get_unit(dev), 72 resname, v); 73 if (error != 0) { 74 /* If it does not exist, we fail, so not ingoring ENOENT. */ 75 device_printf(dev, "could not fetch '%s' hint\n", resname); 76 return (error); 77 } 78 79 return (0); 80} 81 82static int 83atse_resource_long(device_t dev, const char *resname, long *v) 84{ 85 int error; 86 87 error = resource_long_value(device_get_name(dev), device_get_unit(dev), 88 resname, v); 89 if (error != 0) { 90 /* If it does not exist, we fail, so not ingoring ENOENT. */ 91 device_printf(dev, "could not fetch '%s' hint\n", resname); 92 return (error); 93 } 94 95 return (0); 96} 97 98static int 99atse_probe_nexus(device_t dev) 100{ 101 struct resource *res; 102 long l; 103 int error, rid; 104 105 /* 106 * It is almost impossible to properly probe this device. We must 107 * rely on hints being set correctly. So try to get hints and 108 * one memory mapping. Must cleanup and do again in attach but 109 * should not probe successfully if not able to attach later. 110 */ 111 error = atse_resource_int(dev, "rx_irq", &rid); 112 error += atse_resource_long(dev, "rx_maddr", &l); 113 error += atse_resource_long(dev, "rx_msize", &l); 114 error += atse_resource_long(dev, "rxc_maddr", &l); 115 error += atse_resource_long(dev, "rxc_msize", &l); 116 error += atse_resource_int(dev, "tx_irq", &rid); 117 error += atse_resource_long(dev, "tx_maddr", &l); 118 error += atse_resource_long(dev, "tx_msize", &l); 119 error += atse_resource_long(dev, "txc_maddr", &l); 120 error += atse_resource_long(dev, "txc_msize", &l); 121 if (error != 0) 122 return (ENXIO); 123 124 rid = 0; 125 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE); 126 if (res == NULL) 127 return (ENXIO); 128 bus_release_resource(dev, SYS_RES_MEMORY, rid, res); 129 130 /* Success. */ 131 device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore"); 132 return (BUS_PROBE_DEFAULT); 133} 134 135static int 136atse_attach_nexus(device_t dev) 137{ 138 struct atse_softc *sc; 139 int error; 140 141 sc = device_get_softc(dev); 142 sc->atse_dev = dev; 143 sc->atse_unit = device_get_unit(dev); 144 145 /* Get RX and TX IRQ and FIFO information from hints. */ 146 error = atse_resource_int(dev, "rx_irq", &sc->atse_rx_irq); 147 error += atse_resource_long(dev, "rx_maddr", &sc->atse_rx_maddr); 148 error += atse_resource_long(dev, "rx_msize", &sc->atse_rx_msize); 149 error += atse_resource_long(dev, "rxc_maddr", &sc->atse_rxc_maddr); 150 error += atse_resource_long(dev, "rxc_msize", &sc->atse_rxc_msize); 151 error += atse_resource_int(dev, "tx_irq", &sc->atse_tx_irq); 152 error += atse_resource_long(dev, "tx_maddr", &sc->atse_tx_maddr); 153 error += atse_resource_long(dev, "tx_msize", &sc->atse_tx_msize); 154 error += atse_resource_long(dev, "txc_maddr", &sc->atse_txc_maddr); 155 error += atse_resource_long(dev, "txc_msize", &sc->atse_txc_msize); 156 if (error != 0) 157 return (error); 158 159 /* Avalon-MM, atse management register region. */ 160 sc->atse_mem_rid = 0; 161 sc->atse_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 162 &sc->atse_mem_rid, RF_ACTIVE); 163 if (sc->atse_mem_res == NULL) { 164 device_printf(dev, "failed to map memory for ctrl region\n"); 165 return (ENXIO); 166 } 167 168 /* 169 * (Optional) RX IRQ and memory mapped regions. 170 * 0x00: 2 * 32bit FIFO data, 171 * 0x20: 8 * 32bit FIFO ctrl, Avalon-ST Sink to Avalon-MM R-Slave. 172 */ 173 sc->atse_rx_irq_rid = 0; 174 sc->atse_rx_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, 175 &sc->atse_rx_irq_rid, sc->atse_rx_irq, sc->atse_rx_irq, 1, 176 RF_ACTIVE | RF_SHAREABLE); 177 178 sc->atse_rx_mem_rid = 0; 179 sc->atse_rx_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 180 &sc->atse_rx_mem_rid, sc->atse_rx_maddr, sc->atse_rx_maddr + 181 sc->atse_rx_msize, sc->atse_rx_msize, RF_ACTIVE); 182 if (sc->atse_rx_mem_res == NULL) { 183 device_printf(dev, "failed to map memory for RX\n"); 184 goto err; 185 } 186 sc->atse_rxc_mem_rid = 0; 187 sc->atse_rxc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 188 &sc->atse_rxc_mem_rid, sc->atse_rxc_maddr, sc->atse_rxc_maddr + 189 sc->atse_rxc_msize, sc->atse_rxc_msize, RF_ACTIVE); 190 if (sc->atse_rxc_mem_res == NULL) { 191 device_printf(dev, "failed to map memory for RX control\n"); 192 goto err; 193 } 194 195 /* 196 * (Optional) TX IRQ and memory mapped regions. 197 * 0x00: 2 * 32bit FIFO data, 198 * 0x20: 8 * 32bit FIFO ctrl, Avalon-MM W-Slave to Avalon-ST Source. 199 */ 200 sc->atse_tx_irq_rid = 0; 201 sc->atse_tx_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, 202 &sc->atse_tx_irq_rid, sc->atse_tx_irq, sc->atse_tx_irq, 1, 203 RF_ACTIVE | RF_SHAREABLE); 204 205 sc->atse_tx_mem_rid = 0; 206 sc->atse_tx_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 207 &sc->atse_tx_mem_rid, sc->atse_tx_maddr, sc->atse_tx_maddr + 208 sc->atse_tx_msize, sc->atse_tx_msize, RF_ACTIVE); 209 if (sc->atse_tx_mem_res == NULL) { 210 device_printf(dev, "failed to map memory for TX\n"); 211 goto err; 212 } 213 sc->atse_txc_mem_rid = 0; 214 sc->atse_txc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, 215 &sc->atse_txc_mem_rid, sc->atse_txc_maddr, sc->atse_txc_maddr + 216 sc->atse_txc_msize, sc->atse_txc_msize, RF_ACTIVE); 217 if (sc->atse_txc_mem_res == NULL) { 218 device_printf(dev, "failed to map memory for TX control\n"); 219 goto err; 220 } 221 222 error = atse_attach(dev); 223 if (error) 224 goto err; 225 226 return (0); 227 228err: 229 /* Cleanup. */ 230 atse_detach_resources(dev); 231 232 return (error); 233} 234 235static device_method_t atse_methods_nexus[] = { 236 /* Device interface */ 237 DEVMETHOD(device_probe, atse_probe_nexus), 238 DEVMETHOD(device_attach, atse_attach_nexus), 239 DEVMETHOD(device_detach, atse_detach_dev), 240 241 /* MII interface */ 242 DEVMETHOD(miibus_readreg, atse_miibus_readreg), 243 DEVMETHOD(miibus_writereg, atse_miibus_writereg), 244 DEVMETHOD(miibus_statchg, atse_miibus_statchg), 245 246 DEVMETHOD_END 247}; 248 249static driver_t atse_driver_nexus = { 250 "atse", 251 atse_methods_nexus, 252 sizeof(struct atse_softc) 253}; 254 255DRIVER_MODULE(atse, nexus, atse_driver_nexus, atse_devclass, 0, 0); 256DRIVER_MODULE(miibus, atse, miibus_driver, miibus_devclass, 0, 0); 257 258/* end */ 259