2 * Copyright (c) 2012,2013 Bjoern A. Zeeb
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.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_device_polling.h"
36 #include <sys/param.h>
37 #include <sys/kernel.h>
39 #include <sys/module.h>
41 #include <sys/socket.h>
42 #include <sys/types.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
47 #include <net/ethernet.h>
49 #include <net/if_media.h>
51 #include <dev/mii/mii.h>
52 #include <dev/mii/miivar.h>
54 #include <dev/altera/atse/if_atsereg.h>
56 /* "device miibus" required. See GENERIC if you get errors here. */
57 #include "miibus_if.h"
59 MODULE_DEPEND(atse, ether, 1, 1, 1);
60 MODULE_DEPEND(atse, miibus, 1, 1, 1);
63 * Device routines for interacting with nexus (probe, attach, detach) & helpers.
64 * XXX We should add suspend/resume later.
67 atse_resource_int(device_t dev, const char *resname, int *v)
71 error = resource_int_value(device_get_name(dev), device_get_unit(dev),
74 /* If it does not exist, we fail, so not ingoring ENOENT. */
75 device_printf(dev, "could not fetch '%s' hint\n", resname);
83 atse_resource_long(device_t dev, const char *resname, long *v)
87 error = resource_long_value(device_get_name(dev), device_get_unit(dev),
90 /* If it does not exist, we fail, so not ingoring ENOENT. */
91 device_printf(dev, "could not fetch '%s' hint\n", resname);
99 atse_probe_nexus(device_t dev)
101 struct resource *res;
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.
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);
125 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
128 bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
131 device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore");
132 return (BUS_PROBE_DEFAULT);
136 atse_attach_nexus(device_t dev)
138 struct atse_softc *sc;
141 sc = device_get_softc(dev);
143 sc->atse_unit = device_get_unit(dev);
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);
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");
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.
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);
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");
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");
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.
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);
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");
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");
222 error = atse_attach(dev);
230 atse_detach_resources(dev);
235 static 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),
242 DEVMETHOD(miibus_readreg, atse_miibus_readreg),
243 DEVMETHOD(miibus_writereg, atse_miibus_writereg),
244 DEVMETHOD(miibus_statchg, atse_miibus_statchg),
249 static driver_t atse_driver_nexus = {
252 sizeof(struct atse_softc)
255 DRIVER_MODULE(atse, nexus, atse_driver_nexus, atse_devclass, 0, 0);
256 DRIVER_MODULE(miibus, atse, miibus_driver, miibus_devclass, 0, 0);