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