]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/tsec/if_tsec_fdt.c
MFC r257991, r257992, 257993, 258504
[FreeBSD/stable/10.git] / sys / dev / tsec / if_tsec_fdt.c
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 static int tsec_fdt_probe(device_t dev);
72 static int tsec_fdt_attach(device_t dev);
73 static int tsec_fdt_detach(device_t dev);
74 static int tsec_setup_intr(struct tsec_softc *sc, struct resource **ires,
75     void **ihand, int *irid, driver_intr_t handler, const char *iname);
76 static void tsec_release_intr(struct tsec_softc *sc, struct resource *ires,
77     void *ihand, int irid, const char *iname);
78
79 static device_method_t tsec_methods[] = {
80         /* Device interface */
81         DEVMETHOD(device_probe,         tsec_fdt_probe),
82         DEVMETHOD(device_attach,        tsec_fdt_attach),
83         DEVMETHOD(device_detach,        tsec_fdt_detach),
84
85         DEVMETHOD(device_shutdown,      tsec_shutdown),
86         DEVMETHOD(device_suspend,       tsec_suspend),
87         DEVMETHOD(device_resume,        tsec_resume),
88
89         /* MII interface */
90         DEVMETHOD(miibus_readreg,       tsec_miibus_readreg),
91         DEVMETHOD(miibus_writereg,      tsec_miibus_writereg),
92         DEVMETHOD(miibus_statchg,       tsec_miibus_statchg),
93
94         DEVMETHOD_END
95 };
96
97 static driver_t tsec_fdt_driver = {
98         "tsec",
99         tsec_methods,
100         sizeof(struct tsec_softc),
101 };
102
103 DRIVER_MODULE(tsec, simplebus, tsec_fdt_driver, tsec_devclass, 0, 0);
104 MODULE_DEPEND(tsec, simplebus, 1, 1, 1);
105 MODULE_DEPEND(tsec, ether, 1, 1, 1);
106
107 static int
108 tsec_fdt_probe(device_t dev)
109 {
110         struct tsec_softc *sc;
111         uint32_t id;
112
113         if (ofw_bus_get_type(dev) == NULL ||
114             strcmp(ofw_bus_get_type(dev), "network") != 0)
115                 return (ENXIO);
116
117         if (!ofw_bus_is_compatible(dev, "gianfar"))
118                 return (ENXIO);
119
120         sc = device_get_softc(dev);
121
122         sc->sc_rrid = 0;
123         sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rrid,
124             RF_ACTIVE);
125         if (sc->sc_rres == NULL)
126                 return (ENXIO);
127
128         sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
129         sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
130
131         /* Check if we are eTSEC (enhanced TSEC) */
132         id = TSEC_READ(sc, TSEC_REG_ID);
133         sc->is_etsec = ((id >> 16) == TSEC_ETSEC_ID) ? 1 : 0;
134         id |= TSEC_READ(sc, TSEC_REG_ID2);
135
136         bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid, sc->sc_rres);
137
138         if (id == 0) {
139                 device_printf(dev, "could not identify TSEC type\n");
140                 return (ENXIO);
141         }
142
143         if (sc->is_etsec)
144                 device_set_desc(dev, "Enhanced Three-Speed Ethernet Controller");
145         else
146                 device_set_desc(dev, "Three-Speed Ethernet Controller");
147
148         return (BUS_PROBE_DEFAULT);
149 }
150
151 static int
152 tsec_fdt_attach(device_t dev)
153 {
154         struct tsec_softc *sc;
155         phandle_t phy;
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         /* Get phy address from fdt */
163         if (OF_getencprop(sc->node, "phy-handle", &phy, sizeof(phy)) <= 0) {
164                 device_printf(dev, "PHY not found in device tree");
165                 return (ENXIO);
166         }
167
168         phy = OF_xref_phandle(phy);
169         OF_decode_addr(OF_parent(phy), 0, &sc->phy_bst, &sc->phy_bsh);
170         OF_getencprop(phy, "reg", &sc->phyaddr, sizeof(sc->phyaddr));
171
172         /* Init timer */
173         callout_init(&sc->tsec_callout, 1);
174
175         /* Init locks */
176         mtx_init(&sc->transmit_lock, device_get_nameunit(dev), "TSEC TX lock",
177             MTX_DEF);
178         mtx_init(&sc->receive_lock, device_get_nameunit(dev), "TSEC RX lock",
179             MTX_DEF);
180         mtx_init(&sc->ic_lock, device_get_nameunit(dev), "TSEC IC lock",
181             MTX_DEF);
182
183         /* Allocate IO memory for TSEC registers */
184         sc->sc_rrid = 0;
185         sc->sc_rres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->sc_rrid,
186             RF_ACTIVE);
187         if (sc->sc_rres == NULL) {
188                 device_printf(dev, "could not allocate IO memory range!\n");
189                 goto fail1;
190         }
191         sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
192         sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
193
194         /* TSEC attach */
195         if (tsec_attach(sc) != 0) {
196                 device_printf(dev, "could not be configured\n");
197                 goto fail2;
198         }
199
200         /* Set up interrupts (TX/RX/ERR) */
201         sc->sc_transmit_irid = TSEC_RID_TXIRQ;
202         error = tsec_setup_intr(sc, &sc->sc_transmit_ires,
203             &sc->sc_transmit_ihand, &sc->sc_transmit_irid,
204             tsec_transmit_intr, "TX");
205         if (error)
206                 goto fail2;
207
208         sc->sc_receive_irid = TSEC_RID_RXIRQ;
209         error = tsec_setup_intr(sc, &sc->sc_receive_ires,
210             &sc->sc_receive_ihand, &sc->sc_receive_irid,
211             tsec_receive_intr, "RX");
212         if (error)
213                 goto fail3;
214
215         sc->sc_error_irid = TSEC_RID_ERRIRQ;
216         error = tsec_setup_intr(sc, &sc->sc_error_ires,
217             &sc->sc_error_ihand, &sc->sc_error_irid,
218             tsec_error_intr, "ERR");
219         if (error)
220                 goto fail4;
221
222         return (0);
223
224 fail4:
225         tsec_release_intr(sc, sc->sc_receive_ires, sc->sc_receive_ihand,
226             sc->sc_receive_irid, "RX");
227 fail3:
228         tsec_release_intr(sc, sc->sc_transmit_ires, sc->sc_transmit_ihand,
229             sc->sc_transmit_irid, "TX");
230 fail2:
231         bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid, sc->sc_rres);
232 fail1:
233         mtx_destroy(&sc->receive_lock);
234         mtx_destroy(&sc->transmit_lock);
235         return (ENXIO);
236 }
237
238 static int
239 tsec_setup_intr(struct tsec_softc *sc, struct resource **ires, void **ihand,
240     int *irid, driver_intr_t handler, const char *iname)
241 {
242         int error;
243
244         *ires = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, irid, RF_ACTIVE);
245         if (*ires == NULL) {
246                 device_printf(sc->dev, "could not allocate %s IRQ\n", iname);
247                 return (ENXIO);
248         }
249         error = bus_setup_intr(sc->dev, *ires, INTR_TYPE_NET | INTR_MPSAFE,
250             NULL, handler, sc, ihand);
251         if (error) {
252                 device_printf(sc->dev, "failed to set up %s IRQ\n", iname);
253                 if (bus_release_resource(sc->dev, SYS_RES_IRQ, *irid, *ires))
254                         device_printf(sc->dev, "could not release %s IRQ\n", iname);
255                 *ires = NULL;
256                 return (error);
257         }
258         return (0);
259 }
260
261 static void
262 tsec_release_intr(struct tsec_softc *sc, struct resource *ires, void *ihand,
263     int irid, const char *iname)
264 {
265         int error;
266
267         if (ires == NULL)
268                 return;
269
270         error = bus_teardown_intr(sc->dev, ires, ihand);
271         if (error)
272                 device_printf(sc->dev, "bus_teardown_intr() failed for %s intr"
273                     ", error %d\n", iname, error);
274
275         error = bus_release_resource(sc->dev, SYS_RES_IRQ, irid, ires);
276         if (error)
277                 device_printf(sc->dev, "bus_release_resource() failed for %s "
278                     "intr, error %d\n", iname, error);
279 }
280
281 static int
282 tsec_fdt_detach(device_t dev)
283 {
284         struct tsec_softc *sc;
285         int error;
286
287         sc = device_get_softc(dev);
288
289         /* Wait for stopping watchdog */
290         callout_drain(&sc->tsec_callout);
291
292         /* Stop and release all interrupts */
293         tsec_release_intr(sc, sc->sc_transmit_ires, sc->sc_transmit_ihand,
294             sc->sc_transmit_irid, "TX");
295         tsec_release_intr(sc, sc->sc_receive_ires, sc->sc_receive_ihand,
296             sc->sc_receive_irid, "RX");
297         tsec_release_intr(sc, sc->sc_error_ires, sc->sc_error_ihand,
298             sc->sc_error_irid, "ERR");
299
300         /* TSEC detach */
301         tsec_detach(sc);
302
303         /* Free IO memory handler */
304         if (sc->sc_rres) {
305                 error = bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_rrid,
306                     sc->sc_rres);
307                 if (error)
308                         device_printf(dev, "bus_release_resource() failed for"
309                             " IO memory, error %d\n", error);
310         }
311
312         /* Destroy locks */
313         mtx_destroy(&sc->receive_lock);
314         mtx_destroy(&sc->transmit_lock);
315         mtx_destroy(&sc->ic_lock);
316         return (0);
317 }
318
319 void
320 tsec_get_hwaddr(struct tsec_softc *sc, uint8_t *addr)
321 {
322         union {
323                 uint32_t reg[2];
324                 uint8_t addr[6];
325         } hw;
326         int i;
327
328         hw.reg[0] = hw.reg[1] = 0;
329
330         /* Retrieve the hardware address from the device tree. */
331         i = OF_getprop(sc->node, "local-mac-address", (void *)hw.addr, 6);
332         if (i == 6 && (hw.reg[0] != 0 || hw.reg[1] != 0)) {
333                 bcopy(hw.addr, addr, 6);
334                 return;
335         }
336
337         /* Also try the mac-address property, which is second-best */
338         i = OF_getprop(sc->node, "mac-address", (void *)hw.addr, 6);
339         if (i == 6 && (hw.reg[0] != 0 || hw.reg[1] != 0)) {
340                 bcopy(hw.addr, addr, 6);
341                 return;
342         }
343
344         /*
345          * Fall back -- use the currently programmed address in the hope that
346          * it was set be firmware...
347          */
348         hw.reg[0] = TSEC_READ(sc, TSEC_REG_MACSTNADDR1);
349         hw.reg[1] = TSEC_READ(sc, TSEC_REG_MACSTNADDR2);
350         for (i = 0; i < 6; i++)
351                 addr[5-i] = hw.addr[i];
352 }