xref: /freebsd/sys/dev/tsec/if_tsec_fdt.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
1 /*-
2  * Copyright (C) 2007-2008 Semihalf, Rafal Jaworowski
3  * Copyright (C) 2006-2007 Semihalf, Piotr Kruszynski
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
18  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * From: FreeBSD: head/sys/dev/tsec/if_tsec_ocp.c 188712 2009-02-17 14:59:47Z raj
27  */
28 
29 /*
30  * FDT 'simple-bus' attachment for Freescale TSEC controller.
31  */
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/endian.h>
38 #include <sys/mbuf.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 #include <sys/socket.h>
42 #include <sys/sysctl.h>
43 
44 #include <sys/bus.h>
45 #include <machine/bus.h>
46 #include <sys/rman.h>
47 #include <machine/resource.h>
48 
49 #include <net/ethernet.h>
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_media.h>
53 #include <net/if_arp.h>
54 
55 #include <dev/fdt/fdt_common.h>
56 #include <dev/mii/mii.h>
57 #include <dev/mii/miivar.h>
58 #include <dev/ofw/ofw_bus.h>
59 #include <dev/ofw/ofw_bus_subr.h>
60 #include <dev/ofw/openfirm.h>
61 
62 #include <dev/tsec/if_tsec.h>
63 #include <dev/tsec/if_tsecreg.h>
64 
65 #include "miibus_if.h"
66 
67 #define	TSEC_RID_TXIRQ	0
68 #define	TSEC_RID_RXIRQ	1
69 #define	TSEC_RID_ERRIRQ	2
70 
71 extern struct tsec_softc *tsec0_sc;
72 
73 static int tsec_fdt_probe(device_t dev);
74 static int tsec_fdt_attach(device_t dev);
75 static int tsec_fdt_detach(device_t dev);
76 static int tsec_setup_intr(struct tsec_softc *sc, struct resource **ires,
77     void **ihand, int *irid, driver_intr_t handler, const char *iname);
78 static void tsec_release_intr(struct tsec_softc *sc, struct resource *ires,
79     void *ihand, int irid, const char *iname);
80 
81 static device_method_t tsec_methods[] = {
82 	/* Device interface */
83 	DEVMETHOD(device_probe,		tsec_fdt_probe),
84 	DEVMETHOD(device_attach,	tsec_fdt_attach),
85 	DEVMETHOD(device_detach,	tsec_fdt_detach),
86 
87 	DEVMETHOD(device_shutdown,	tsec_shutdown),
88 	DEVMETHOD(device_suspend,	tsec_suspend),
89 	DEVMETHOD(device_resume,	tsec_resume),
90 
91 	/* Bus interface */
92 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
93 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
94 
95 	/* MII interface */
96 	DEVMETHOD(miibus_readreg,	tsec_miibus_readreg),
97 	DEVMETHOD(miibus_writereg,	tsec_miibus_writereg),
98 	DEVMETHOD(miibus_statchg,	tsec_miibus_statchg),
99 	{ 0, 0 }
100 };
101 
102 static driver_t tsec_fdt_driver = {
103 	"tsec",
104 	tsec_methods,
105 	sizeof(struct tsec_softc),
106 };
107 
108 DRIVER_MODULE(tsec, simplebus, tsec_fdt_driver, tsec_devclass, 0, 0);
109 MODULE_DEPEND(tsec, simplebus, 1, 1, 1);
110 MODULE_DEPEND(tsec, ether, 1, 1, 1);
111 
112 static int
113 tsec_fdt_probe(device_t dev)
114 {
115 	struct tsec_softc *sc;
116 	uint32_t id;
117 
118 	if (!ofw_bus_is_compatible(dev, "gianfar"))
119 		return (ENXIO);
120 
121 	sc = device_get_softc(dev);
122 
123 	sc->sc_rrid = 0;
124 	sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rrid,
125 	    RF_ACTIVE);
126 	if (sc->sc_rres == NULL)
127 		return (ENXIO);
128 
129 	sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
130 	sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
131 
132 	/* Check if we are eTSEC (enhanced TSEC) */
133 	id = TSEC_READ(sc, TSEC_REG_ID);
134 	sc->is_etsec = ((id >> 16) == TSEC_ETSEC_ID) ? 1 : 0;
135 	id |= TSEC_READ(sc, TSEC_REG_ID2);
136 
137 	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid, sc->sc_rres);
138 
139 	if (id == 0) {
140 		device_printf(dev, "could not identify TSEC type\n");
141 		return (ENXIO);
142 	}
143 
144 	if (sc->is_etsec)
145 		device_set_desc(dev, "Enhanced Three-Speed Ethernet Controller");
146 	else
147 		device_set_desc(dev, "Three-Speed Ethernet Controller");
148 
149 	return (BUS_PROBE_DEFAULT);
150 }
151 
152 static int
153 tsec_fdt_attach(device_t dev)
154 {
155 	struct tsec_softc *sc;
156 	int error = 0;
157 
158 	sc = device_get_softc(dev);
159 	sc->dev = dev;
160 	sc->node = ofw_bus_get_node(dev);
161 
162 	/* XXX add comment on weird FSL's MII registers access design */
163 	if (device_get_unit(dev) == 0)
164 		tsec0_sc = sc;
165 
166 	/* Get phy address from fdt */
167 	if (fdt_get_phyaddr(sc->node, &sc->phyaddr) != 0)
168 		return (ENXIO);
169 
170 	/* Init timer */
171 	callout_init(&sc->tsec_callout, 1);
172 
173 	/* Init locks */
174 	mtx_init(&sc->transmit_lock, device_get_nameunit(dev), "TSEC TX lock",
175 	    MTX_DEF);
176 	mtx_init(&sc->receive_lock, device_get_nameunit(dev), "TSEC RX lock",
177 	    MTX_DEF);
178 	mtx_init(&sc->ic_lock, device_get_nameunit(dev), "TSEC IC lock",
179 	    MTX_DEF);
180 
181 	/* Allocate IO memory for TSEC registers */
182 	sc->sc_rrid = 0;
183 	sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rrid,
184 	    RF_ACTIVE);
185 	if (sc->sc_rres == NULL) {
186 		device_printf(dev, "could not allocate IO memory range!\n");
187 		goto fail1;
188 	}
189 	sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
190 	sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
191 
192 	/* TSEC attach */
193 	if (tsec_attach(sc) != 0) {
194 		device_printf(dev, "could not be configured\n");
195 		goto fail2;
196 	}
197 
198 	/* Set up interrupts (TX/RX/ERR) */
199 	sc->sc_transmit_irid = TSEC_RID_TXIRQ;
200 	error = tsec_setup_intr(sc, &sc->sc_transmit_ires,
201 	    &sc->sc_transmit_ihand, &sc->sc_transmit_irid,
202 	    tsec_transmit_intr, "TX");
203 	if (error)
204 		goto fail2;
205 
206 	sc->sc_receive_irid = TSEC_RID_RXIRQ;
207 	error = tsec_setup_intr(sc, &sc->sc_receive_ires,
208 	    &sc->sc_receive_ihand, &sc->sc_receive_irid,
209 	    tsec_receive_intr, "RX");
210 	if (error)
211 		goto fail3;
212 
213 	sc->sc_error_irid = TSEC_RID_ERRIRQ;
214 	error = tsec_setup_intr(sc, &sc->sc_error_ires,
215 	    &sc->sc_error_ihand, &sc->sc_error_irid,
216 	    tsec_error_intr, "ERR");
217 	if (error)
218 		goto fail4;
219 
220 	return (0);
221 
222 fail4:
223 	tsec_release_intr(sc, sc->sc_receive_ires, sc->sc_receive_ihand,
224 	    sc->sc_receive_irid, "RX");
225 fail3:
226 	tsec_release_intr(sc, sc->sc_transmit_ires, sc->sc_transmit_ihand,
227 	    sc->sc_transmit_irid, "TX");
228 fail2:
229 	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid, sc->sc_rres);
230 fail1:
231 	mtx_destroy(&sc->receive_lock);
232 	mtx_destroy(&sc->transmit_lock);
233 	return (ENXIO);
234 }
235 
236 static int
237 tsec_setup_intr(struct tsec_softc *sc, struct resource **ires, void **ihand,
238     int *irid, driver_intr_t handler, const char *iname)
239 {
240 	int error;
241 
242 	*ires = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, irid, RF_ACTIVE);
243 	if (*ires == NULL) {
244 		device_printf(sc->dev, "could not allocate %s IRQ\n", iname);
245 		return (ENXIO);
246 	}
247 	error = bus_setup_intr(sc->dev, *ires, INTR_TYPE_NET | INTR_MPSAFE,
248 	    NULL, handler, sc, ihand);
249 	if (error) {
250 		device_printf(sc->dev, "failed to set up %s IRQ\n", iname);
251 		if (bus_release_resource(sc->dev, SYS_RES_IRQ, *irid, *ires))
252 			device_printf(sc->dev, "could not release %s IRQ\n", iname);
253 		*ires = NULL;
254 		return (error);
255 	}
256 	return (0);
257 }
258 
259 static void
260 tsec_release_intr(struct tsec_softc *sc, struct resource *ires, void *ihand,
261     int irid, const char *iname)
262 {
263 	int error;
264 
265 	if (ires == NULL)
266 		return;
267 
268 	error = bus_teardown_intr(sc->dev, ires, ihand);
269 	if (error)
270 		device_printf(sc->dev, "bus_teardown_intr() failed for %s intr"
271 		    ", error %d\n", iname, error);
272 
273 	error = bus_release_resource(sc->dev, SYS_RES_IRQ, irid, ires);
274 	if (error)
275 		device_printf(sc->dev, "bus_release_resource() failed for %s "
276 		    "intr, error %d\n", iname, error);
277 }
278 
279 static int
280 tsec_fdt_detach(device_t dev)
281 {
282 	struct tsec_softc *sc;
283 	int error;
284 
285 	sc = device_get_softc(dev);
286 
287 	/* Wait for stopping watchdog */
288 	callout_drain(&sc->tsec_callout);
289 
290 	/* Stop and release all interrupts */
291 	tsec_release_intr(sc, sc->sc_transmit_ires, sc->sc_transmit_ihand,
292 	    sc->sc_transmit_irid, "TX");
293 	tsec_release_intr(sc, sc->sc_receive_ires, sc->sc_receive_ihand,
294 	    sc->sc_receive_irid, "RX");
295 	tsec_release_intr(sc, sc->sc_error_ires, sc->sc_error_ihand,
296 	    sc->sc_error_irid, "ERR");
297 
298 	/* TSEC detach */
299 	tsec_detach(sc);
300 
301 	/* Free IO memory handler */
302 	if (sc->sc_rres) {
303 		error = bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid,
304 		    sc->sc_rres);
305 		if (error)
306 			device_printf(dev, "bus_release_resource() failed for"
307 			    " IO memory, error %d\n", error);
308 	}
309 
310 	/* Destroy locks */
311 	mtx_destroy(&sc->receive_lock);
312 	mtx_destroy(&sc->transmit_lock);
313 	mtx_destroy(&sc->ic_lock);
314 	return (0);
315 }
316 
317 void
318 tsec_get_hwaddr(struct tsec_softc *sc, uint8_t *addr)
319 {
320 	union {
321 		uint32_t reg[2];
322 		uint8_t addr[6];
323 	} curmac;
324 	uint32_t a[6];
325 	uint8_t lma[6];
326 	int i;
327 
328 	/*
329 	 * Retrieve hw address from the device tree.
330 	 */
331 	i = OF_getprop(sc->node, "local-mac-address", (void *)lma, 6);
332 	if (i == 6) {
333 		bcopy(lma, addr, 6);
334 		return;
335 	}
336 
337 	/*
338 	 * Fall back -- use the currently programmed address in the hope that
339 	 * it was set be firmware...
340 	 */
341 	curmac.reg[0] = TSEC_READ(sc, TSEC_REG_MACSTNADDR1);
342 	curmac.reg[1] = TSEC_READ(sc, TSEC_REG_MACSTNADDR2);
343 	for (i = 0; i < 6; i++)
344 		a[5-i] = curmac.addr[i];
345 
346 	addr[0] = a[0];
347 	addr[1] = a[1];
348 	addr[2] = a[2];
349 	addr[3] = a[3];
350 	addr[4] = a[4];
351 	addr[5] = a[5];
352 }
353