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