2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012,2013 Bjoern A. Zeeb
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-11-C-0249)
9 * ("MRC2"), as part of the DARPA MRC research programme.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_device_polling.h"
38 #include <sys/param.h>
39 #include <sys/kernel.h>
41 #include <sys/module.h>
43 #include <sys/socket.h>
44 #include <sys/types.h>
46 #include <machine/bus.h>
47 #include <machine/resource.h>
49 #include <net/ethernet.h>
51 #include <net/if_media.h>
52 #include <net/if_var.h>
54 #include <dev/mii/mii.h>
55 #include <dev/mii/miivar.h>
57 #include <dev/altera/atse/if_atsereg.h>
59 /* "device miibus" required. See GENERIC if you get errors here. */
60 #include "miibus_if.h"
62 MODULE_DEPEND(atse, ether, 1, 1, 1);
63 MODULE_DEPEND(atse, miibus, 1, 1, 1);
66 * Device routines for interacting with nexus (probe, attach, detach) & helpers.
67 * XXX We should add suspend/resume later.
70 atse_resource_int(device_t dev, const char *resname, int *v)
74 error = resource_int_value(device_get_name(dev), device_get_unit(dev),
77 /* If it does not exist, we fail, so not ingoring ENOENT. */
78 device_printf(dev, "could not fetch '%s' hint\n", resname);
86 atse_resource_long(device_t dev, const char *resname, long *v)
90 error = resource_long_value(device_get_name(dev), device_get_unit(dev),
93 /* If it does not exist, we fail, so not ingoring ENOENT. */
94 device_printf(dev, "could not fetch '%s' hint\n", resname);
102 atse_probe_nexus(device_t dev)
104 struct resource *res;
109 * It is almost impossible to properly probe this device. We must
110 * rely on hints being set correctly. So try to get hints and
111 * one memory mapping. Must cleanup and do again in attach but
112 * should not probe successfully if not able to attach later.
114 error = atse_resource_int(dev, "rx_irq", &rid);
115 error += atse_resource_long(dev, "rx_maddr", &l);
116 error += atse_resource_long(dev, "rx_msize", &l);
117 error += atse_resource_long(dev, "rxc_maddr", &l);
118 error += atse_resource_long(dev, "rxc_msize", &l);
119 error += atse_resource_int(dev, "tx_irq", &rid);
120 error += atse_resource_long(dev, "tx_maddr", &l);
121 error += atse_resource_long(dev, "tx_msize", &l);
122 error += atse_resource_long(dev, "txc_maddr", &l);
123 error += atse_resource_long(dev, "txc_msize", &l);
128 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
131 bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
134 device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore");
135 return (BUS_PROBE_NOWILDCARD);
139 atse_attach_nexus(device_t dev)
141 struct atse_softc *sc;
144 sc = device_get_softc(dev);
146 sc->atse_unit = device_get_unit(dev);
148 /* Get RX and TX IRQ and FIFO information from hints. */
149 error = atse_resource_int(dev, "rx_irq", &sc->atse_rx_irq);
150 error += atse_resource_long(dev, "rx_maddr", &sc->atse_rx_maddr);
151 error += atse_resource_long(dev, "rx_msize", &sc->atse_rx_msize);
152 error += atse_resource_long(dev, "rxc_maddr", &sc->atse_rxc_maddr);
153 error += atse_resource_long(dev, "rxc_msize", &sc->atse_rxc_msize);
154 error += atse_resource_int(dev, "tx_irq", &sc->atse_tx_irq);
155 error += atse_resource_long(dev, "tx_maddr", &sc->atse_tx_maddr);
156 error += atse_resource_long(dev, "tx_msize", &sc->atse_tx_msize);
157 error += atse_resource_long(dev, "txc_maddr", &sc->atse_txc_maddr);
158 error += atse_resource_long(dev, "txc_msize", &sc->atse_txc_msize);
162 /* Avalon-MM, atse management register region. */
163 sc->atse_mem_rid = 0;
164 sc->atse_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
165 &sc->atse_mem_rid, RF_ACTIVE);
166 if (sc->atse_mem_res == NULL) {
167 device_printf(dev, "failed to map memory for ctrl region\n");
172 * (Optional) RX IRQ and memory mapped regions.
173 * 0x00: 2 * 32bit FIFO data,
174 * 0x20: 8 * 32bit FIFO ctrl, Avalon-ST Sink to Avalon-MM R-Slave.
176 sc->atse_rx_irq_rid = 0;
177 sc->atse_rx_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
178 &sc->atse_rx_irq_rid, sc->atse_rx_irq, sc->atse_rx_irq, 1,
179 RF_ACTIVE | RF_SHAREABLE);
181 sc->atse_rx_mem_rid = 0;
182 sc->atse_rx_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
183 &sc->atse_rx_mem_rid, sc->atse_rx_maddr, sc->atse_rx_maddr +
184 sc->atse_rx_msize, sc->atse_rx_msize, RF_ACTIVE);
185 if (sc->atse_rx_mem_res == NULL) {
186 device_printf(dev, "failed to map memory for RX\n");
189 sc->atse_rxc_mem_rid = 0;
190 sc->atse_rxc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
191 &sc->atse_rxc_mem_rid, sc->atse_rxc_maddr, sc->atse_rxc_maddr +
192 sc->atse_rxc_msize, sc->atse_rxc_msize, RF_ACTIVE);
193 if (sc->atse_rxc_mem_res == NULL) {
194 device_printf(dev, "failed to map memory for RX control\n");
199 * (Optional) TX IRQ and memory mapped regions.
200 * 0x00: 2 * 32bit FIFO data,
201 * 0x20: 8 * 32bit FIFO ctrl, Avalon-MM W-Slave to Avalon-ST Source.
203 sc->atse_tx_irq_rid = 0;
204 sc->atse_tx_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
205 &sc->atse_tx_irq_rid, sc->atse_tx_irq, sc->atse_tx_irq, 1,
206 RF_ACTIVE | RF_SHAREABLE);
208 sc->atse_tx_mem_rid = 0;
209 sc->atse_tx_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
210 &sc->atse_tx_mem_rid, sc->atse_tx_maddr, sc->atse_tx_maddr +
211 sc->atse_tx_msize, sc->atse_tx_msize, RF_ACTIVE);
212 if (sc->atse_tx_mem_res == NULL) {
213 device_printf(dev, "failed to map memory for TX\n");
216 sc->atse_txc_mem_rid = 0;
217 sc->atse_txc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
218 &sc->atse_txc_mem_rid, sc->atse_txc_maddr, sc->atse_txc_maddr +
219 sc->atse_txc_msize, sc->atse_txc_msize, RF_ACTIVE);
220 if (sc->atse_txc_mem_res == NULL) {
221 device_printf(dev, "failed to map memory for TX control\n");
225 error = atse_attach(dev);
233 atse_detach_resources(dev);
238 static device_method_t atse_methods_nexus[] = {
239 /* Device interface */
240 DEVMETHOD(device_probe, atse_probe_nexus),
241 DEVMETHOD(device_attach, atse_attach_nexus),
242 DEVMETHOD(device_detach, atse_detach_dev),
245 DEVMETHOD(miibus_readreg, atse_miibus_readreg),
246 DEVMETHOD(miibus_writereg, atse_miibus_writereg),
247 DEVMETHOD(miibus_statchg, atse_miibus_statchg),
252 static driver_t atse_driver_nexus = {
255 sizeof(struct atse_softc)
258 DRIVER_MODULE(atse, nexus, atse_driver_nexus, atse_devclass, 0, 0);
259 DRIVER_MODULE(miibus, atse, miibus_driver, miibus_devclass, 0, 0);