xref: /freebsd/sys/dev/altera/atse/if_atse_fdt.c (revision 38a52bd3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 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$");
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/rman.h>
41 #include <sys/socket.h>
42 #include <sys/systm.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/fdt/fdt_common.h>
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ofw/ofw_bus.h>
58 #include <dev/ofw/ofw_bus_subr.h>
59 
60 #include <dev/altera/atse/if_atsereg.h>
61 
62 /* "device miibus" required.  See GENERIC if you get errors here. */
63 #include "miibus_if.h"
64 
65 static int
66 atse_probe_fdt(device_t dev)
67 {
68 
69 	if (!ofw_bus_status_okay(dev))
70 		return (ENXIO);
71 
72 	if (!ofw_bus_is_compatible(dev, "altera,atse")) {
73        		return (ENXIO);
74 	}
75 
76 	device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore");
77 
78 	return (BUS_PROBE_DEFAULT);
79 }
80 
81 static int
82 atse_attach_fdt(device_t dev)
83 {
84 	struct atse_softc *sc;
85 	int error;
86 
87 	sc = device_get_softc(dev);
88 	sc->atse_dev = dev;
89 	sc->atse_unit = device_get_unit(dev);
90 
91 	/*
92 	 * FDT has the list of our resources.  Given we are using multiple
93 	 * memory regions and possibly multiple interrupts, we need to attach
94 	 * them in the order specified in .dts:
95 	 * MAC, RX and RXC FIFO, TX and TXC FIFO; RX INTR, TX INTR.
96 	 */
97 
98 	/* MAC: Avalon-MM, atse management register region. */
99 	sc->atse_mem_rid = 0;
100 	sc->atse_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
101 	    &sc->atse_mem_rid, RF_ACTIVE);
102 	if (sc->atse_mem_res == NULL) {
103 		device_printf(dev, "failed to map memory for ctrl region\n");
104 		/* Cleanup. */
105 		atse_detach_resources(dev);
106 
107 		return (ENXIO);
108 	}
109 	if (bootverbose)
110 		device_printf(sc->atse_dev, "MAC ctrl region at mem %p-%p\n",
111 		    (void *)rman_get_start(sc->atse_mem_res),
112 		    (void *)(rman_get_start(sc->atse_mem_res) +
113 		    rman_get_size(sc->atse_mem_res)));
114 
115 	error = atse_attach(dev);
116 	if (error) {
117 		/* Cleanup. */
118 		atse_detach_resources(dev);
119 
120 		return (error);
121 	}
122 
123 	return (0);
124 }
125 
126 static device_method_t atse_methods_fdt[] = {
127 	/* Device interface */
128 	DEVMETHOD(device_probe,		atse_probe_fdt),
129 	DEVMETHOD(device_attach,	atse_attach_fdt),
130 	DEVMETHOD(device_detach,	atse_detach_dev),
131 
132 	/* MII interface */
133 	DEVMETHOD(miibus_readreg,	atse_miibus_readreg),
134 	DEVMETHOD(miibus_writereg,	atse_miibus_writereg),
135 	DEVMETHOD(miibus_statchg,	atse_miibus_statchg),
136 
137 	DEVMETHOD_END
138 };
139 
140 static driver_t atse_driver_fdt = {
141 	"atse",
142 	atse_methods_fdt,
143 	sizeof(struct atse_softc)
144 };
145 
146 DRIVER_MODULE(atse, simplebus, atse_driver_fdt, 0, 0);
147 DRIVER_MODULE(miibus, atse, miibus_driver, 0, 0);
148