xref: /freebsd/sys/dev/altera/atse/if_atse_fdt.c (revision 4b9d6057)
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/param.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/rman.h>
38 #include <sys/socket.h>
39 #include <sys/systm.h>
40 
41 #include <machine/bus.h>
42 #include <machine/resource.h>
43 
44 #include <net/ethernet.h>
45 #include <net/if.h>
46 #include <net/if_media.h>
47 #include <net/if_var.h>
48 
49 #include <dev/mii/mii.h>
50 #include <dev/mii/miivar.h>
51 
52 #include <dev/fdt/fdt_common.h>
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.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 
62 static int
63 atse_probe_fdt(device_t dev)
64 {
65 
66 	if (!ofw_bus_status_okay(dev))
67 		return (ENXIO);
68 
69 	if (!ofw_bus_is_compatible(dev, "altera,atse")) {
70        		return (ENXIO);
71 	}
72 
73 	device_set_desc(dev, "Altera Triple-Speed Ethernet MegaCore");
74 
75 	return (BUS_PROBE_DEFAULT);
76 }
77 
78 static int
79 atse_attach_fdt(device_t dev)
80 {
81 	struct atse_softc *sc;
82 	int error;
83 
84 	sc = device_get_softc(dev);
85 	sc->atse_dev = dev;
86 	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 		/* Cleanup. */
102 		atse_detach_resources(dev);
103 
104 		return (ENXIO);
105 	}
106 	if (bootverbose)
107 		device_printf(sc->atse_dev, "MAC ctrl region at mem %p-%p\n",
108 		    (void *)rman_get_start(sc->atse_mem_res),
109 		    (void *)(rman_get_start(sc->atse_mem_res) +
110 		    rman_get_size(sc->atse_mem_res)));
111 
112 	error = atse_attach(dev);
113 	if (error) {
114 		/* Cleanup. */
115 		atse_detach_resources(dev);
116 
117 		return (error);
118 	}
119 
120 	return (0);
121 }
122 
123 static device_method_t atse_methods_fdt[] = {
124 	/* Device interface */
125 	DEVMETHOD(device_probe,		atse_probe_fdt),
126 	DEVMETHOD(device_attach,	atse_attach_fdt),
127 	DEVMETHOD(device_detach,	atse_detach_dev),
128 
129 	/* MII interface */
130 	DEVMETHOD(miibus_readreg,	atse_miibus_readreg),
131 	DEVMETHOD(miibus_writereg,	atse_miibus_writereg),
132 	DEVMETHOD(miibus_statchg,	atse_miibus_statchg),
133 
134 	DEVMETHOD_END
135 };
136 
137 static driver_t atse_driver_fdt = {
138 	"atse",
139 	atse_methods_fdt,
140 	sizeof(struct atse_softc)
141 };
142 
143 DRIVER_MODULE(atse, simplebus, atse_driver_fdt, 0, 0);
144 DRIVER_MODULE(miibus, atse, miibus_driver, 0, 0);
145