xref: /freebsd/sys/dev/neta/if_mvneta_fdt.c (revision 093cf790569775b80662926efea6d9d3464bde94)
1 /*
2  * Copyright (c) 2017 Stormshield.
3  * Copyright (c) 2017 Semihalf.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
24  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "opt_platform.h"
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/rman.h>
38 #include <sys/socket.h>
39 #include <sys/taskqueue.h>
40 
41 #include <net/ethernet.h>
42 #include <net/if.h>
43 #include <net/if_media.h>
44 
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <netinet/tcp_lro.h>
48 
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 
55 #include <dev/mii/mii.h>
56 #include <dev/mii/miivar.h>
57 
58 #include "if_mvnetareg.h"
59 #include "if_mvnetavar.h"
60 
61 #ifdef MVNETA_DEBUG
62 #define	STATIC /* nothing */
63 #else
64 #define	STATIC static
65 #endif
66 
67 #define	PHY_MODE_MAXLEN	10
68 #define	INBAND_STATUS_MAXLEN 16
69 
70 static int mvneta_fdt_probe(device_t);
71 static int mvneta_fdt_attach(device_t);
72 STATIC boolean_t mvneta_find_ethernet_prop_switch(phandle_t, phandle_t);
73 
74 static device_method_t mvneta_fdt_methods[] = {
75 	/* Device interface */
76 	DEVMETHOD(device_probe,		mvneta_fdt_probe),
77 	DEVMETHOD(device_attach,	mvneta_fdt_attach),
78 
79 	/* End */
80 	DEVMETHOD_END
81 };
82 
83 DEFINE_CLASS_1(mvneta, mvneta_fdt_driver, mvneta_fdt_methods,
84     sizeof(struct mvneta_softc), mvneta_driver);
85 
86 static devclass_t mvneta_fdt_devclass;
87 
88 DRIVER_MODULE(mvneta, ofwbus, mvneta_fdt_driver, mvneta_fdt_devclass, 0, 0);
89 DRIVER_MODULE(mvneta, simplebus, mvneta_fdt_driver, mvneta_fdt_devclass, 0, 0);
90 
91 static int mvneta_fdt_phy_acquire(device_t);
92 
93 static struct ofw_compat_data compat_data[] = {
94 	{"marvell,armada-370-neta",	true},
95 	{"marvell,armada-3700-neta",	true},
96 	{NULL,				false}
97 };
98 
99 SIMPLEBUS_PNP_INFO(compat_data);
100 
101 static int
102 mvneta_fdt_probe(device_t dev)
103 {
104 
105 	if (!ofw_bus_status_okay(dev))
106 		return (ENXIO);
107 
108 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
109 		return (ENXIO);
110 
111 	device_set_desc(dev, "NETA controller");
112 	return (BUS_PROBE_DEFAULT);
113 }
114 
115 static int
116 mvneta_fdt_attach(device_t dev)
117 {
118 	struct mvneta_softc *sc;
119 	uint32_t tx_csum_limit;
120 	int err;
121 
122 	sc = device_get_softc(dev);
123 
124 	/* Try to fetch PHY information from FDT */
125 	err = mvneta_fdt_phy_acquire(dev);
126 	if (err != 0)
127 		return (err);
128 
129 	if (ofw_bus_is_compatible(dev, "marvell,armada-370-neta")) {
130 		tx_csum_limit = MVNETA_A370_MAX_CSUM_MTU;
131 	} else {
132 		tx_csum_limit = MVNETA_A3700_MAX_CSUM_MTU;
133 	}
134 
135 	if (ofw_bus_has_prop(dev, "tx-csum-limit")) {
136 		err = OF_getencprop(ofw_bus_get_node(dev), "tx-csum-limit",
137 			    &tx_csum_limit, sizeof(tx_csum_limit));
138 		if (err <= 0) {
139 			device_printf(dev,
140 				"Failed to acquire tx-csum-limit property\n");
141 			return (ENXIO);
142 		}
143 	}
144 	sc->tx_csum_limit = tx_csum_limit;
145 
146 	return (mvneta_attach(dev));
147 }
148 
149 static int
150 mvneta_fdt_phy_acquire(device_t dev)
151 {
152 	struct mvneta_softc *sc;
153 	phandle_t node, child, phy_handle;
154 	char phymode[PHY_MODE_MAXLEN];
155 	char managed[INBAND_STATUS_MAXLEN];
156 	char *name;
157 
158 	sc = device_get_softc(dev);
159 	node = ofw_bus_get_node(dev);
160 
161 	/* PHY mode is crucial */
162 	if (OF_getprop(node, "phy-mode", phymode, sizeof(phymode)) <= 0) {
163 		device_printf(dev, "Failed to acquire PHY mode from FDT.\n");
164 		return (ENXIO);
165 	}
166 
167 	if (strncmp(phymode, "rgmii-id", 8) == 0)
168 		sc->phy_mode = MVNETA_PHY_RGMII_ID;
169 	else if (strncmp(phymode, "rgmii", 5) == 0)
170 		sc->phy_mode = MVNETA_PHY_RGMII;
171 	else if (strncmp(phymode, "sgmii", 5) == 0)
172 		sc->phy_mode = MVNETA_PHY_SGMII;
173 	else if (strncmp(phymode, "qsgmii", 6) == 0)
174 		sc->phy_mode = MVNETA_PHY_QSGMII;
175 	else
176 		sc->phy_mode = MVNETA_PHY_SGMII;
177 
178 	/* Check if in-band link status will be used */
179 	if (OF_getprop(node, "managed", managed, sizeof(managed)) > 0) {
180 		if (strncmp(managed, "in-band-status", 14) == 0) {
181 			sc->use_inband_status = TRUE;
182 			device_printf(dev, "Use in-band link status.\n");
183 			return (0);
184 		}
185 	}
186 
187 	if (OF_getencprop(node, "phy", (void *)&phy_handle,
188 	    sizeof(phy_handle)) <= 0) {
189 		/* Test for fixed-link (present i.e. in 388-gp) */
190 		for (child = OF_child(node); child != 0; child = OF_peer(child)) {
191 			if (OF_getprop_alloc(child,
192 			    "name", (void **)&name) <= 0) {
193 				continue;
194 			}
195 			if (strncmp(name, "fixed-link", 10) == 0) {
196 				free(name, M_OFWPROP);
197 				if (OF_getencprop(child, "speed",
198 				    &sc->phy_speed, sizeof(sc->phy_speed)) <= 0) {
199 					if (bootverbose) {
200 						device_printf(dev,
201 						    "No PHY information.\n");
202 					}
203 					return (ENXIO);
204 				}
205 				if (OF_hasprop(child, "full-duplex"))
206 					sc->phy_fdx = TRUE;
207 				else
208 					sc->phy_fdx = FALSE;
209 
210 				/* Keep this flag just for the record */
211 				sc->phy_addr = MII_PHY_ANY;
212 
213 				return (0);
214 			}
215 			free(name, M_OFWPROP);
216 		}
217 		if (bootverbose) {
218 			device_printf(dev,
219 			    "Could not find PHY information in FDT.\n");
220 		}
221 		return (ENXIO);
222 	} else {
223 		phy_handle = OF_instance_to_package(phy_handle);
224 		if (OF_getencprop(phy_handle, "reg", &sc->phy_addr,
225 		    sizeof(sc->phy_addr)) <= 0) {
226 			device_printf(dev,
227 			    "Could not find PHY address in FDT.\n");
228 			return (ENXIO);
229 		}
230 	}
231 
232 	return (0);
233 }
234 
235 int
236 mvneta_fdt_mac_address(struct mvneta_softc *sc, uint8_t *addr)
237 {
238 	phandle_t node;
239 	uint8_t lmac[ETHER_ADDR_LEN];
240 	uint8_t zeromac[] = {[0 ... (ETHER_ADDR_LEN - 1)] = 0};
241 	int len;
242 
243 	/*
244 	 * Retrieve hw address from the device tree.
245 	 */
246 	node = ofw_bus_get_node(sc->dev);
247 	if (node == 0)
248 		return (ENXIO);
249 
250 	len = OF_getprop(node, "local-mac-address", (void *)lmac, sizeof(lmac));
251 	if (len != ETHER_ADDR_LEN)
252 		return (ENOENT);
253 
254 	if (memcmp(lmac, zeromac, ETHER_ADDR_LEN) == 0) {
255 		/* Invalid MAC address (all zeros) */
256 		return (EINVAL);
257 	}
258 	memcpy(addr, lmac, ETHER_ADDR_LEN);
259 
260 	return (0);
261 }
262 
263 STATIC boolean_t
264 mvneta_find_ethernet_prop_switch(phandle_t ethernet, phandle_t node)
265 {
266 	boolean_t ret;
267 	phandle_t child, switch_eth_handle, switch_eth;
268 
269 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
270 		if (OF_getencprop(child, "ethernet", (void*)&switch_eth_handle,
271 		    sizeof(switch_eth_handle)) > 0) {
272 			if (switch_eth_handle > 0) {
273 				switch_eth = OF_node_from_xref(
274 				    switch_eth_handle);
275 
276 				if (switch_eth == ethernet)
277 					return (true);
278 			}
279 		}
280 
281 		ret = mvneta_find_ethernet_prop_switch(ethernet, child);
282 		if (ret != 0)
283 			return (ret);
284 	}
285 
286 	return (false);
287 }
288 
289 boolean_t
290 mvneta_has_switch_fdt(device_t self)
291 {
292 	phandle_t node;
293 
294 	node = ofw_bus_get_node(self);
295 
296 	return mvneta_find_ethernet_prop_switch(node, OF_finddevice("/"));
297 }
298