if_atse_fdt.c revision 257269
1/*-
2 * Copyright (c) 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: head/sys/dev/altera/atse/if_atse_fdt.c 257269 2013-10-28 20:15:59Z brooks $");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38#include <sys/rman.h>
39#include <sys/socket.h>
40#include <sys/systm.h>
41
42#include <machine/bus.h>
43#include <machine/resource.h>
44
45#include <net/ethernet.h>
46#include <net/if.h>
47#include <net/if_media.h>
48#include <net/if_var.h>
49
50#include <dev/mii/mii.h>
51#include <dev/mii/miivar.h>
52
53
54#include <dev/fdt/fdt_common.h>
55#include <dev/ofw/openfirm.h>
56#include <dev/ofw/ofw_bus.h>
57#include <dev/ofw/ofw_bus_subr.h>
58
59#include <dev/altera/atse/if_atsereg.h>
60
61/* "device miibus" required.  See GENERIC if you get errors here. */
62#include "miibus_if.h"
63
64static int
65atse_probe_fdt(device_t dev)
66{
67
68	if (ofw_bus_is_compatible(dev, "altera,atse")) {
69		device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore");
70		return (BUS_PROBE_DEFAULT);
71	}
72        return (ENXIO);
73}
74
75static int
76atse_attach_fdt(device_t dev)
77{
78	struct atse_softc *sc;
79	int error;
80
81	sc = device_get_softc(dev);
82	sc->atse_dev = dev;
83	sc->atse_unit = device_get_unit(dev);
84
85	/*
86	 * FDT has the list of our resources.  Given we are using multiple
87	 * memory regions and possibly multiple interrupts, we need to attach
88	 * them in the order specified in .dts:
89	 * MAC, RX and RXC FIFO, TX and TXC FIFO; RX INTR, TX INTR.
90	 */
91
92	/* MAC: Avalon-MM, atse management register region. */
93	sc->atse_mem_rid = 0;
94	sc->atse_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
95	    &sc->atse_mem_rid, RF_ACTIVE);
96	if (sc->atse_mem_res == NULL) {
97		device_printf(dev, "failed to map memory for ctrl region\n");
98		error = ENXIO;
99		goto err;
100	}
101	if (bootverbose)
102		device_printf(sc->atse_dev, "MAC ctrl region at mem %p-%p\n",
103		    (void *)rman_get_start(sc->atse_mem_res),
104		    (void *)(rman_get_start(sc->atse_mem_res) +
105		    rman_get_size(sc->atse_mem_res)));
106
107	/*
108	 * RX and RXC FIFO memory regions.
109	 * 0x00: 2 * 32bit FIFO data,
110	 * 0x20: 8 * 32bit FIFO ctrl, Avalon-ST Sink to Avalon-MM R-Slave.
111	 */
112	sc->atse_rx_mem_rid = 1;
113	sc->atse_rx_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
114	    &sc->atse_rx_mem_rid, RF_ACTIVE);
115	if (sc->atse_rx_mem_res == NULL) {
116		device_printf(dev, "failed to map memory for RX FIFO\n");
117		error = ENXIO;
118		goto err;
119	}
120	if (bootverbose)
121		device_printf(sc->atse_dev, "RX FIFO at mem %p-%p\n",
122		    (void *)rman_get_start(sc->atse_rx_mem_res),
123		    (void *)(rman_get_start(sc->atse_rx_mem_res) +
124		    rman_get_size(sc->atse_rx_mem_res)));
125
126	sc->atse_rxc_mem_rid = 2;
127	sc->atse_rxc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
128	    &sc->atse_rxc_mem_rid, RF_ACTIVE);
129	if (sc->atse_rxc_mem_res == NULL) {
130		device_printf(dev, "failed to map memory for RXC FIFO\n");
131		error = ENXIO;
132		goto err;
133	}
134	if (bootverbose)
135		device_printf(sc->atse_dev, "RXC FIFO at mem %p-%p\n",
136		    (void *)rman_get_start(sc->atse_rxc_mem_res),
137		    (void *)(rman_get_start(sc->atse_rxc_mem_res) +
138		    rman_get_size(sc->atse_rxc_mem_res)));
139
140	/*
141	 * TX and TXC FIFO memory regions.
142	 * 0x00: 2 * 32bit FIFO data,
143	 * 0x20: 8 * 32bit FIFO ctrl, Avalon-MM W-Slave to Avalon-ST Source.
144	 */
145	sc->atse_tx_mem_rid = 3;
146	sc->atse_tx_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
147	    &sc->atse_tx_mem_rid, RF_ACTIVE);
148	if (sc->atse_tx_mem_res == NULL) {
149		device_printf(dev, "failed to map memory for TX FIFO\n");
150		error = ENXIO;
151		goto err;
152	}
153	if (bootverbose)
154		device_printf(sc->atse_dev, "TX FIFO at mem %p-%p\n",
155		    (void *)rman_get_start(sc->atse_tx_mem_res),
156		    (void *)(rman_get_start(sc->atse_tx_mem_res) +
157		    rman_get_size(sc->atse_tx_mem_res)));
158
159	sc->atse_txc_mem_rid = 4;
160	sc->atse_txc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
161	    &sc->atse_txc_mem_rid, RF_ACTIVE);
162	if (sc->atse_txc_mem_res == NULL) {
163		device_printf(dev, "failed to map memory for TXC FIFO\n");
164		error = ENXIO;
165		goto err;
166	}
167	if (bootverbose)
168		device_printf(sc->atse_dev, "TXC FIFO at mem %p-%p\n",
169		    (void *)rman_get_start(sc->atse_txc_mem_res),
170		    (void *)(rman_get_start(sc->atse_txc_mem_res) +
171		    rman_get_size(sc->atse_txc_mem_res)));
172
173	/* (Optional) RX and TX IRQ. */
174	sc->atse_rx_irq_rid = 0;
175	sc->atse_rx_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
176	    &sc->atse_rx_irq_rid, RF_ACTIVE | RF_SHAREABLE);
177	sc->atse_tx_irq_rid = 1;
178	sc->atse_tx_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
179	    &sc->atse_tx_irq_rid, RF_ACTIVE | RF_SHAREABLE);
180
181	error = atse_attach(dev);
182	if (error)
183		goto err;
184
185	return (0);
186
187err:
188	/* Cleanup. */
189	atse_detach_resources(dev);
190
191	return (error);
192}
193
194static device_method_t atse_methods_fdt[] = {
195	/* Device interface */
196	DEVMETHOD(device_probe,		atse_probe_fdt),
197	DEVMETHOD(device_attach,	atse_attach_fdt),
198	DEVMETHOD(device_detach,	atse_detach_dev),
199
200	/* MII interface */
201	DEVMETHOD(miibus_readreg,	atse_miibus_readreg),
202	DEVMETHOD(miibus_writereg,	atse_miibus_writereg),
203	DEVMETHOD(miibus_statchg,	atse_miibus_statchg),
204
205	DEVMETHOD_END
206};
207
208static driver_t atse_driver_fdt = {
209	"atse",
210	atse_methods_fdt,
211	sizeof(struct atse_softc)
212};
213
214DRIVER_MODULE(atse, simplebus, atse_driver_fdt, atse_devclass, 0, 0);
215DRIVER_MODULE(miibus, atse, miibus_driver, miibus_devclass, 0, 0);
216
217/* end */
218