if_atse_nexus.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2012,2013 Bjoern A. Zeeb
5 * All rights reserved.
6 *
7 * This software was developed by SRI International and the University of
8 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-11-C-0249)
9 * ("MRC2"), as part of the DARPA MRC research programme.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/11/sys/dev/altera/atse/if_atse_nexus.c 330897 2018-03-14 03:19:51Z eadler $");
35
36#include "opt_device_polling.h"
37
38#include <sys/param.h>
39#include <sys/kernel.h>
40#include <sys/bus.h>
41#include <sys/module.h>
42#include <sys/rman.h>
43#include <sys/socket.h>
44#include <sys/types.h>
45
46#include <machine/bus.h>
47#include <machine/resource.h>
48
49#include <net/ethernet.h>
50#include <net/if.h>
51#include <net/if_media.h>
52#include <net/if_var.h>
53
54#include <dev/mii/mii.h>
55#include <dev/mii/miivar.h>
56
57#include <dev/altera/atse/if_atsereg.h>
58
59/* "device miibus" required.  See GENERIC if you get errors here. */
60#include "miibus_if.h"
61
62MODULE_DEPEND(atse, ether, 1, 1, 1);
63MODULE_DEPEND(atse, miibus, 1, 1, 1);
64
65/*
66 * Device routines for interacting with nexus (probe, attach, detach) & helpers.
67 * XXX We should add suspend/resume later.
68 */
69static int
70atse_resource_int(device_t dev, const char *resname, int *v)
71{
72	int error;
73
74	error = resource_int_value(device_get_name(dev), device_get_unit(dev),
75	    resname, v);
76	if (error != 0) {
77		/* If it does not exist, we fail, so not ingoring ENOENT. */
78		device_printf(dev, "could not fetch '%s' hint\n", resname);
79		return (error);
80	}
81
82	return (0);
83}
84
85static int
86atse_resource_long(device_t dev, const char *resname, long *v)
87{
88	int error;
89
90	error = resource_long_value(device_get_name(dev), device_get_unit(dev),
91	    resname, v);
92	if (error != 0) {
93		/* If it does not exist, we fail, so not ingoring ENOENT. */
94		device_printf(dev, "could not fetch '%s' hint\n", resname);
95		return (error);
96	}
97
98	return (0);
99}
100
101static int
102atse_probe_nexus(device_t dev)
103{
104	struct resource *res;
105	long l;
106	int error, rid;
107
108	/*
109	 * It is almost impossible to properly probe this device.  We must
110	 * rely on hints being set correctly.  So try to get hints and
111	 * one memory mapping.  Must cleanup and do again in attach but
112	 * should not probe successfully if not able to attach later.
113	 */
114	error = atse_resource_int(dev, "rx_irq", &rid);
115	error += atse_resource_long(dev, "rx_maddr", &l);
116	error += atse_resource_long(dev, "rx_msize", &l);
117	error += atse_resource_long(dev, "rxc_maddr", &l);
118	error += atse_resource_long(dev, "rxc_msize", &l);
119	error += atse_resource_int(dev, "tx_irq", &rid);
120	error += atse_resource_long(dev, "tx_maddr", &l);
121	error += atse_resource_long(dev, "tx_msize", &l);
122	error += atse_resource_long(dev, "txc_maddr", &l);
123	error += atse_resource_long(dev, "txc_msize", &l);
124	if (error != 0)
125		return (ENXIO);
126
127	rid = 0;
128	res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
129	if (res == NULL)
130		return (ENXIO);
131	bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
132
133	/* Success. */
134	device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore");
135	return (BUS_PROBE_NOWILDCARD);
136}
137
138static int
139atse_attach_nexus(device_t dev)
140{
141	struct atse_softc *sc;
142	int error;
143
144	sc = device_get_softc(dev);
145	sc->atse_dev = dev;
146	sc->atse_unit = device_get_unit(dev);
147
148	/* Get RX and TX IRQ and FIFO information from hints. */
149	error = atse_resource_int(dev, "rx_irq", &sc->atse_rx_irq);
150	error += atse_resource_long(dev, "rx_maddr", &sc->atse_rx_maddr);
151	error += atse_resource_long(dev, "rx_msize", &sc->atse_rx_msize);
152	error += atse_resource_long(dev, "rxc_maddr", &sc->atse_rxc_maddr);
153	error += atse_resource_long(dev, "rxc_msize", &sc->atse_rxc_msize);
154	error += atse_resource_int(dev, "tx_irq", &sc->atse_tx_irq);
155	error += atse_resource_long(dev, "tx_maddr", &sc->atse_tx_maddr);
156	error += atse_resource_long(dev, "tx_msize", &sc->atse_tx_msize);
157	error += atse_resource_long(dev, "txc_maddr", &sc->atse_txc_maddr);
158	error += atse_resource_long(dev, "txc_msize", &sc->atse_txc_msize);
159	if (error != 0)
160		return (error);
161
162	/* Avalon-MM, atse management register region. */
163	sc->atse_mem_rid = 0;
164	sc->atse_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
165	    &sc->atse_mem_rid, RF_ACTIVE);
166	if (sc->atse_mem_res == NULL) {
167		device_printf(dev, "failed to map memory for ctrl region\n");
168		return (ENXIO);
169	}
170
171	/*
172	 * (Optional) RX IRQ and memory mapped regions.
173	 * 0x00: 2 * 32bit FIFO data,
174	 * 0x20: 8 * 32bit FIFO ctrl, Avalon-ST Sink to Avalon-MM R-Slave.
175	 */
176	sc->atse_rx_irq_rid = 0;
177	sc->atse_rx_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
178	    &sc->atse_rx_irq_rid, sc->atse_rx_irq, sc->atse_rx_irq, 1,
179	    RF_ACTIVE | RF_SHAREABLE);
180
181	sc->atse_rx_mem_rid = 0;
182	sc->atse_rx_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
183	    &sc->atse_rx_mem_rid, sc->atse_rx_maddr, sc->atse_rx_maddr +
184	    sc->atse_rx_msize, sc->atse_rx_msize, RF_ACTIVE);
185	if (sc->atse_rx_mem_res == NULL) {
186		device_printf(dev, "failed to map memory for RX\n");
187		goto err;
188        }
189	sc->atse_rxc_mem_rid = 0;
190	sc->atse_rxc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
191	    &sc->atse_rxc_mem_rid, sc->atse_rxc_maddr, sc->atse_rxc_maddr +
192	    sc->atse_rxc_msize, sc->atse_rxc_msize, RF_ACTIVE);
193	if (sc->atse_rxc_mem_res == NULL) {
194		device_printf(dev, "failed to map memory for RX control\n");
195		goto err;
196        }
197
198	/*
199	 * (Optional) TX IRQ and memory mapped regions.
200	 * 0x00: 2 * 32bit FIFO data,
201	 * 0x20: 8 * 32bit FIFO ctrl, Avalon-MM W-Slave to Avalon-ST Source.
202	 */
203	sc->atse_tx_irq_rid = 0;
204	sc->atse_tx_irq_res = bus_alloc_resource(dev, SYS_RES_IRQ,
205	    &sc->atse_tx_irq_rid, sc->atse_tx_irq, sc->atse_tx_irq, 1,
206	    RF_ACTIVE | RF_SHAREABLE);
207
208	sc->atse_tx_mem_rid = 0;
209	sc->atse_tx_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
210	    &sc->atse_tx_mem_rid, sc->atse_tx_maddr, sc->atse_tx_maddr +
211	    sc->atse_tx_msize, sc->atse_tx_msize, RF_ACTIVE);
212	if (sc->atse_tx_mem_res == NULL) {
213		device_printf(dev, "failed to map memory for TX\n");
214		goto err;
215	}
216	sc->atse_txc_mem_rid = 0;
217	sc->atse_txc_mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY,
218	    &sc->atse_txc_mem_rid, sc->atse_txc_maddr, sc->atse_txc_maddr +
219	    sc->atse_txc_msize, sc->atse_txc_msize, RF_ACTIVE);
220	if (sc->atse_txc_mem_res == NULL) {
221		device_printf(dev, "failed to map memory for TX control\n");
222		goto err;
223	}
224
225	error = atse_attach(dev);
226	if (error)
227		goto err;
228
229	return (0);
230
231err:
232	/* Cleanup. */
233	atse_detach_resources(dev);
234
235	return (error);
236}
237
238static device_method_t atse_methods_nexus[] = {
239	/* Device interface */
240	DEVMETHOD(device_probe,		atse_probe_nexus),
241	DEVMETHOD(device_attach,	atse_attach_nexus),
242	DEVMETHOD(device_detach,	atse_detach_dev),
243
244	/* MII interface */
245	DEVMETHOD(miibus_readreg,	atse_miibus_readreg),
246	DEVMETHOD(miibus_writereg,	atse_miibus_writereg),
247	DEVMETHOD(miibus_statchg,	atse_miibus_statchg),
248
249	DEVMETHOD_END
250};
251
252static driver_t atse_driver_nexus = {
253	"atse",
254	atse_methods_nexus,
255	sizeof(struct atse_softc)
256};
257
258DRIVER_MODULE(atse, nexus, atse_driver_nexus, atse_devclass, 0, 0);
259DRIVER_MODULE(miibus, atse, miibus_driver, miibus_devclass, 0, 0);
260
261/* end */
262