1252190Srpaulo/*-
2252190Srpaulo * Copyright (c) 2013 Bjoern A. Zeeb
3252190Srpaulo * All rights reserved.
4252190Srpaulo *
5252190Srpaulo * This software was developed by SRI International and the University of
6252190Srpaulo * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-11-C-0249)
7252190Srpaulo * ("MRC2"), as part of the DARPA MRC research programme.
8252190Srpaulo *
9252190Srpaulo * Redistribution and use in source and binary forms, with or without
10252190Srpaulo * modification, are permitted provided that the following conditions
11252190Srpaulo * are met:
12252190Srpaulo * 1. Redistributions of source code must retain the above copyright
13252190Srpaulo *    notice, this list of conditions and the following disclaimer.
14252190Srpaulo * 2. Redistributions in binary form must reproduce the above copyright
15252190Srpaulo *    notice, this list of conditions and the following disclaimer in the
16252190Srpaulo *    documentation and/or other materials provided with the distribution.
17252190Srpaulo *
18252190Srpaulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19252190Srpaulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20252190Srpaulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21252190Srpaulo * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22252190Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23252190Srpaulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24252190Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25252190Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26252190Srpaulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27252190Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28252190Srpaulo * SUCH DAMAGE.
29252190Srpaulo */
30252190Srpaulo
31252190Srpaulo#include <sys/cdefs.h>
32252190Srpaulo__FBSDID("$FreeBSD$");
33252190Srpaulo
34252190Srpaulo#include <sys/param.h>
35252190Srpaulo#include <sys/bus.h>
36252190Srpaulo#include <sys/kernel.h>
37252190Srpaulo#include <sys/module.h>
38252190Srpaulo#include <sys/rman.h>
39252190Srpaulo#include <sys/socket.h>
40252190Srpaulo#include <sys/systm.h>
41252190Srpaulo
42252190Srpaulo#include <machine/bus.h>
43252190Srpaulo#include <machine/resource.h>
44252190Srpaulo
45252190Srpaulo#include <net/ethernet.h>
46252190Srpaulo#include <net/if.h>
47252190Srpaulo#include <net/if_media.h>
48252190Srpaulo#include <net/if_var.h>
49252190Srpaulo
50252190Srpaulo#include <dev/mii/mii.h>
51252190Srpaulo#include <dev/mii/miivar.h>
52252190Srpaulo
53252190Srpaulo
54252190Srpaulo#include <dev/fdt/fdt_common.h>
55252190Srpaulo#include <dev/ofw/openfirm.h>
56252190Srpaulo#include <dev/ofw/ofw_bus.h>
57252190Srpaulo#include <dev/ofw/ofw_bus_subr.h>
58252190Srpaulo
59252190Srpaulo#include <dev/altera/atse/if_atsereg.h>
60252190Srpaulo
61252190Srpaulo/* "device miibus" required.  See GENERIC if you get errors here. */
62252190Srpaulo#include "miibus_if.h"
63252190Srpaulo
64252190Srpaulostatic int
65252190Srpauloatse_probe_fdt(device_t dev)
66252190Srpaulo{
67252190Srpaulo
68252190Srpaulo	if (!ofw_bus_status_okay(dev))
69252190Srpaulo		return (ENXIO);
70252190Srpaulo
71252190Srpaulo	if (ofw_bus_is_compatible(dev, "altera,atse")) {
72252190Srpaulo		device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore");
73252190Srpaulo		return (BUS_PROBE_DEFAULT);
74252190Srpaulo	}
75252190Srpaulo        return (ENXIO);
76252190Srpaulo}
77252190Srpaulo
78252190Srpaulostatic int
79252190Srpauloatse_attach_fdt(device_t dev)
80252190Srpaulo{
81252190Srpaulo	struct atse_softc *sc;
82252190Srpaulo	int error;
83252190Srpaulo
84252190Srpaulo	sc = device_get_softc(dev);
85252190Srpaulo	sc->atse_dev = dev;
86252190Srpaulo	sc->atse_unit = device_get_unit(dev);
87
88	/*
89	 * FDT has the list of our resources.  Given we are using multiple
90	 * memory regions and possibly multiple interrupts, we need to attach
91	 * them in the order specified in .dts:
92	 * MAC, RX and RXC FIFO, TX and TXC FIFO; RX INTR, TX INTR.
93	 */
94
95	/* MAC: Avalon-MM, atse management register region. */
96	sc->atse_mem_rid = 0;
97	sc->atse_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
98	    &sc->atse_mem_rid, RF_ACTIVE);
99	if (sc->atse_mem_res == NULL) {
100		device_printf(dev, "failed to map memory for ctrl region\n");
101		error = ENXIO;
102		goto err;
103	}
104	if (bootverbose)
105		device_printf(sc->atse_dev, "MAC ctrl region at mem %p-%p\n",
106		    (void *)rman_get_start(sc->atse_mem_res),
107		    (void *)(rman_get_start(sc->atse_mem_res) +
108		    rman_get_size(sc->atse_mem_res)));
109
110	/*
111	 * RX and RXC FIFO memory regions.
112	 * 0x00: 2 * 32bit FIFO data,
113	 * 0x20: 8 * 32bit FIFO ctrl, Avalon-ST Sink to Avalon-MM R-Slave.
114	 */
115	sc->atse_rx_mem_rid = 1;
116	sc->atse_rx_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
117	    &sc->atse_rx_mem_rid, RF_ACTIVE);
118	if (sc->atse_rx_mem_res == NULL) {
119		device_printf(dev, "failed to map memory for RX FIFO\n");
120		error = ENXIO;
121		goto err;
122	}
123	if (bootverbose)
124		device_printf(sc->atse_dev, "RX FIFO at mem %p-%p\n",
125		    (void *)rman_get_start(sc->atse_rx_mem_res),
126		    (void *)(rman_get_start(sc->atse_rx_mem_res) +
127		    rman_get_size(sc->atse_rx_mem_res)));
128
129	sc->atse_rxc_mem_rid = 2;
130	sc->atse_rxc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
131	    &sc->atse_rxc_mem_rid, RF_ACTIVE);
132	if (sc->atse_rxc_mem_res == NULL) {
133		device_printf(dev, "failed to map memory for RXC FIFO\n");
134		error = ENXIO;
135		goto err;
136	}
137	if (bootverbose)
138		device_printf(sc->atse_dev, "RXC FIFO at mem %p-%p\n",
139		    (void *)rman_get_start(sc->atse_rxc_mem_res),
140		    (void *)(rman_get_start(sc->atse_rxc_mem_res) +
141		    rman_get_size(sc->atse_rxc_mem_res)));
142
143	/*
144	 * TX and TXC FIFO memory regions.
145	 * 0x00: 2 * 32bit FIFO data,
146	 * 0x20: 8 * 32bit FIFO ctrl, Avalon-MM W-Slave to Avalon-ST Source.
147	 */
148	sc->atse_tx_mem_rid = 3;
149	sc->atse_tx_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
150	    &sc->atse_tx_mem_rid, RF_ACTIVE);
151	if (sc->atse_tx_mem_res == NULL) {
152		device_printf(dev, "failed to map memory for TX FIFO\n");
153		error = ENXIO;
154		goto err;
155	}
156	if (bootverbose)
157		device_printf(sc->atse_dev, "TX FIFO at mem %p-%p\n",
158		    (void *)rman_get_start(sc->atse_tx_mem_res),
159		    (void *)(rman_get_start(sc->atse_tx_mem_res) +
160		    rman_get_size(sc->atse_tx_mem_res)));
161
162	sc->atse_txc_mem_rid = 4;
163	sc->atse_txc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
164	    &sc->atse_txc_mem_rid, RF_ACTIVE);
165	if (sc->atse_txc_mem_res == NULL) {
166		device_printf(dev, "failed to map memory for TXC FIFO\n");
167		error = ENXIO;
168		goto err;
169	}
170	if (bootverbose)
171		device_printf(sc->atse_dev, "TXC FIFO at mem %p-%p\n",
172		    (void *)rman_get_start(sc->atse_txc_mem_res),
173		    (void *)(rman_get_start(sc->atse_txc_mem_res) +
174		    rman_get_size(sc->atse_txc_mem_res)));
175
176	/* (Optional) RX and TX IRQ. */
177	sc->atse_rx_irq_rid = 0;
178	sc->atse_rx_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
179	    &sc->atse_rx_irq_rid, RF_ACTIVE | RF_SHAREABLE);
180	sc->atse_tx_irq_rid = 1;
181	sc->atse_tx_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
182	    &sc->atse_tx_irq_rid, RF_ACTIVE | RF_SHAREABLE);
183
184	error = atse_attach(dev);
185	if (error)
186		goto err;
187
188	return (0);
189
190err:
191	/* Cleanup. */
192	atse_detach_resources(dev);
193
194	return (error);
195}
196
197static device_method_t atse_methods_fdt[] = {
198	/* Device interface */
199	DEVMETHOD(device_probe,		atse_probe_fdt),
200	DEVMETHOD(device_attach,	atse_attach_fdt),
201	DEVMETHOD(device_detach,	atse_detach_dev),
202
203	/* MII interface */
204	DEVMETHOD(miibus_readreg,	atse_miibus_readreg),
205	DEVMETHOD(miibus_writereg,	atse_miibus_writereg),
206	DEVMETHOD(miibus_statchg,	atse_miibus_statchg),
207
208	DEVMETHOD_END
209};
210
211static driver_t atse_driver_fdt = {
212	"atse",
213	atse_methods_fdt,
214	sizeof(struct atse_softc)
215};
216
217DRIVER_MODULE(atse, simplebus, atse_driver_fdt, atse_devclass, 0, 0);
218DRIVER_MODULE(miibus, atse, miibus_driver, miibus_devclass, 0, 0);
219
220/* end */
221