1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
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#include "opt_device_polling.h"
35
36#include <sys/param.h>
37#include <sys/kernel.h>
38#include <sys/bus.h>
39#include <sys/module.h>
40#include <sys/rman.h>
41#include <sys/socket.h>
42#include <sys/types.h>
43
44#include <machine/bus.h>
45#include <machine/resource.h>
46
47#include <net/ethernet.h>
48#include <net/if.h>
49#include <net/if_media.h>
50#include <net/if_var.h>
51
52#include <dev/mii/mii.h>
53#include <dev/mii/miivar.h>
54
55#include <dev/altera/atse/if_atsereg.h>
56
57/* "device miibus" required.  See GENERIC if you get errors here. */
58#include "miibus_if.h"
59
60MODULE_DEPEND(atse, ether, 1, 1, 1);
61MODULE_DEPEND(atse, miibus, 1, 1, 1);
62
63/*
64 * Device routines for interacting with nexus (probe, attach, detach) & helpers.
65 * XXX We should add suspend/resume later.
66 */
67static int __unused
68atse_resource_int(device_t dev, const char *resname, int *v)
69{
70	int error;
71
72	error = resource_int_value(device_get_name(dev), device_get_unit(dev),
73	    resname, v);
74	if (error != 0) {
75		/* If it does not exist, we fail, so not ingoring ENOENT. */
76		device_printf(dev, "could not fetch '%s' hint\n", resname);
77		return (error);
78	}
79
80	return (0);
81}
82
83static int __unused
84atse_resource_long(device_t dev, const char *resname, long *v)
85{
86	int error;
87
88	error = resource_long_value(device_get_name(dev), device_get_unit(dev),
89	    resname, v);
90	if (error != 0) {
91		/* If it does not exist, we fail, so not ingoring ENOENT. */
92		device_printf(dev, "could not fetch '%s' hint\n", resname);
93		return (error);
94	}
95
96	return (0);
97}
98
99static int
100atse_probe_nexus(device_t dev)
101{
102
103	device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore");
104
105	return (BUS_PROBE_NOWILDCARD);
106}
107
108static int
109atse_attach_nexus(device_t dev)
110{
111	struct atse_softc *sc;
112	int error;
113
114	sc = device_get_softc(dev);
115	sc->atse_dev = dev;
116	sc->atse_unit = device_get_unit(dev);
117
118	/* Avalon-MM, atse management register region. */
119	sc->atse_mem_rid = 0;
120	sc->atse_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
121	    &sc->atse_mem_rid, RF_ACTIVE);
122	if (sc->atse_mem_res == NULL) {
123		device_printf(dev, "failed to map memory for ctrl region\n");
124		return (ENXIO);
125	}
126
127	error = atse_attach(dev);
128	if (error) {
129		/* Cleanup. */
130		atse_detach_resources(dev);
131		return (error);
132	}
133
134	return (0);
135}
136
137static device_method_t atse_methods_nexus[] = {
138	/* Device interface */
139	DEVMETHOD(device_probe,		atse_probe_nexus),
140	DEVMETHOD(device_attach,	atse_attach_nexus),
141	DEVMETHOD(device_detach,	atse_detach_dev),
142
143	/* MII interface */
144	DEVMETHOD(miibus_readreg,	atse_miibus_readreg),
145	DEVMETHOD(miibus_writereg,	atse_miibus_writereg),
146	DEVMETHOD(miibus_statchg,	atse_miibus_statchg),
147
148	DEVMETHOD_END
149};
150
151static driver_t atse_driver_nexus = {
152	"atse",
153	atse_methods_nexus,
154	sizeof(struct atse_softc)
155};
156
157DRIVER_MODULE(atse, nexus, atse_driver_nexus, 0, 0);
158DRIVER_MODULE(miibus, atse, miibus_driver, 0, 0);
159