xref: /freebsd/sys/dev/neta/if_mvneta_fdt.c (revision 4b9d605768acabc460aa6dcfe8a1f8db35b16794)
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 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/rman.h>
36 #include <sys/socket.h>
37 #include <sys/taskqueue.h>
38 
39 #include <net/ethernet.h>
40 #include <net/if.h>
41 #include <net/if_media.h>
42 
43 #include <netinet/in.h>
44 #include <netinet/ip.h>
45 #include <netinet/tcp_lro.h>
46 
47 #include <machine/bus.h>
48 #include <machine/resource.h>
49 
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 
53 #include <dev/mii/mii.h>
54 #include <dev/mii/miivar.h>
55 
56 #include "if_mvnetareg.h"
57 #include "if_mvnetavar.h"
58 
59 #ifdef MVNETA_DEBUG
60 #define	STATIC /* nothing */
61 #else
62 #define	STATIC static
63 #endif
64 
65 #define	PHY_MODE_MAXLEN	10
66 #define	INBAND_STATUS_MAXLEN 16
67 
68 static int mvneta_fdt_probe(device_t);
69 static int mvneta_fdt_attach(device_t);
70 STATIC boolean_t mvneta_find_ethernet_prop_switch(phandle_t, phandle_t);
71 
72 static device_method_t mvneta_fdt_methods[] = {
73 	/* Device interface */
74 	DEVMETHOD(device_probe,		mvneta_fdt_probe),
75 	DEVMETHOD(device_attach,	mvneta_fdt_attach),
76 
77 	/* End */
78 	DEVMETHOD_END
79 };
80 
81 DEFINE_CLASS_1(mvneta, mvneta_fdt_driver, mvneta_fdt_methods,
82     sizeof(struct mvneta_softc), mvneta_driver);
83 
84 DRIVER_MODULE(mvneta, ofwbus, mvneta_fdt_driver, 0, 0);
85 DRIVER_MODULE(mvneta, simplebus, mvneta_fdt_driver, 0, 0);
86 
87 static int mvneta_fdt_phy_acquire(device_t);
88 
89 static struct ofw_compat_data compat_data[] = {
90 	{"marvell,armada-370-neta",	true},
91 	{"marvell,armada-3700-neta",	true},
92 	{NULL,				false}
93 };
94 
95 SIMPLEBUS_PNP_INFO(compat_data);
96 
97 static int
98 mvneta_fdt_probe(device_t dev)
99 {
100 
101 	if (!ofw_bus_status_okay(dev))
102 		return (ENXIO);
103 
104 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
105 		return (ENXIO);
106 
107 	device_set_desc(dev, "NETA controller");
108 	return (BUS_PROBE_DEFAULT);
109 }
110 
111 static int
112 mvneta_fdt_attach(device_t dev)
113 {
114 	struct mvneta_softc *sc;
115 	uint32_t tx_csum_limit;
116 	int err;
117 
118 	sc = device_get_softc(dev);
119 
120 	/* Try to fetch PHY information from FDT */
121 	err = mvneta_fdt_phy_acquire(dev);
122 	if (err != 0)
123 		return (err);
124 
125 	if (ofw_bus_is_compatible(dev, "marvell,armada-370-neta")) {
126 		tx_csum_limit = MVNETA_A370_MAX_CSUM_MTU;
127 	} else {
128 		tx_csum_limit = MVNETA_A3700_MAX_CSUM_MTU;
129 	}
130 
131 	if (ofw_bus_has_prop(dev, "tx-csum-limit")) {
132 		err = OF_getencprop(ofw_bus_get_node(dev), "tx-csum-limit",
133 			    &tx_csum_limit, sizeof(tx_csum_limit));
134 		if (err <= 0) {
135 			device_printf(dev,
136 				"Failed to acquire tx-csum-limit property\n");
137 			return (ENXIO);
138 		}
139 	}
140 	sc->tx_csum_limit = tx_csum_limit;
141 
142 	return (mvneta_attach(dev));
143 }
144 
145 static int
146 mvneta_fdt_phy_acquire(device_t dev)
147 {
148 	struct mvneta_softc *sc;
149 	phandle_t node, child, phy_handle;
150 	char phymode[PHY_MODE_MAXLEN];
151 	char managed[INBAND_STATUS_MAXLEN];
152 	char *name;
153 
154 	sc = device_get_softc(dev);
155 	node = ofw_bus_get_node(dev);
156 
157 	/* PHY mode is crucial */
158 	if (OF_getprop(node, "phy-mode", phymode, sizeof(phymode)) <= 0) {
159 		device_printf(dev, "Failed to acquire PHY mode from FDT.\n");
160 		return (ENXIO);
161 	}
162 
163 	if (strncmp(phymode, "rgmii-id", 8) == 0)
164 		sc->phy_mode = MVNETA_PHY_RGMII_ID;
165 	else if (strncmp(phymode, "rgmii", 5) == 0)
166 		sc->phy_mode = MVNETA_PHY_RGMII;
167 	else if (strncmp(phymode, "sgmii", 5) == 0)
168 		sc->phy_mode = MVNETA_PHY_SGMII;
169 	else if (strncmp(phymode, "qsgmii", 6) == 0)
170 		sc->phy_mode = MVNETA_PHY_QSGMII;
171 	else
172 		sc->phy_mode = MVNETA_PHY_SGMII;
173 
174 	/* Check if in-band link status will be used */
175 	if (OF_getprop(node, "managed", managed, sizeof(managed)) > 0) {
176 		if (strncmp(managed, "in-band-status", 14) == 0) {
177 			sc->use_inband_status = TRUE;
178 			device_printf(dev, "Use in-band link status.\n");
179 			return (0);
180 		}
181 	}
182 
183 	if (OF_getencprop(node, "phy", (void *)&phy_handle,
184 	    sizeof(phy_handle)) <= 0) {
185 		/* Test for fixed-link (present i.e. in 388-gp) */
186 		for (child = OF_child(node); child != 0; child = OF_peer(child)) {
187 			if (OF_getprop_alloc(child,
188 			    "name", (void **)&name) <= 0) {
189 				continue;
190 			}
191 			if (strncmp(name, "fixed-link", 10) == 0) {
192 				free(name, M_OFWPROP);
193 				if (OF_getencprop(child, "speed",
194 				    &sc->phy_speed, sizeof(sc->phy_speed)) <= 0) {
195 					if (bootverbose) {
196 						device_printf(dev,
197 						    "No PHY information.\n");
198 					}
199 					return (ENXIO);
200 				}
201 				if (OF_hasprop(child, "full-duplex"))
202 					sc->phy_fdx = TRUE;
203 				else
204 					sc->phy_fdx = FALSE;
205 
206 				/* Keep this flag just for the record */
207 				sc->phy_addr = MII_PHY_ANY;
208 
209 				return (0);
210 			}
211 			free(name, M_OFWPROP);
212 		}
213 		if (bootverbose) {
214 			device_printf(dev,
215 			    "Could not find PHY information in FDT.\n");
216 		}
217 		return (ENXIO);
218 	} else {
219 		phy_handle = OF_instance_to_package(phy_handle);
220 		if (OF_getencprop(phy_handle, "reg", &sc->phy_addr,
221 		    sizeof(sc->phy_addr)) <= 0) {
222 			device_printf(dev,
223 			    "Could not find PHY address in FDT.\n");
224 			return (ENXIO);
225 		}
226 	}
227 
228 	return (0);
229 }
230 
231 int
232 mvneta_fdt_mac_address(struct mvneta_softc *sc, uint8_t *addr)
233 {
234 	phandle_t node;
235 	uint8_t lmac[ETHER_ADDR_LEN];
236 	uint8_t zeromac[] = {[0 ... (ETHER_ADDR_LEN - 1)] = 0};
237 	int len;
238 
239 	/*
240 	 * Retrieve hw address from the device tree.
241 	 */
242 	node = ofw_bus_get_node(sc->dev);
243 	if (node == 0)
244 		return (ENXIO);
245 
246 	len = OF_getprop(node, "local-mac-address", (void *)lmac, sizeof(lmac));
247 	if (len != ETHER_ADDR_LEN)
248 		return (ENOENT);
249 
250 	if (memcmp(lmac, zeromac, ETHER_ADDR_LEN) == 0) {
251 		/* Invalid MAC address (all zeros) */
252 		return (EINVAL);
253 	}
254 	memcpy(addr, lmac, ETHER_ADDR_LEN);
255 
256 	return (0);
257 }
258 
259 STATIC boolean_t
260 mvneta_find_ethernet_prop_switch(phandle_t ethernet, phandle_t node)
261 {
262 	boolean_t ret;
263 	phandle_t child, switch_eth_handle, switch_eth;
264 
265 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
266 		if (OF_getencprop(child, "ethernet", (void*)&switch_eth_handle,
267 		    sizeof(switch_eth_handle)) > 0) {
268 			if (switch_eth_handle > 0) {
269 				switch_eth = OF_node_from_xref(
270 				    switch_eth_handle);
271 
272 				if (switch_eth == ethernet)
273 					return (true);
274 			}
275 		}
276 
277 		ret = mvneta_find_ethernet_prop_switch(ethernet, child);
278 		if (ret != 0)
279 			return (ret);
280 	}
281 
282 	return (false);
283 }
284 
285 boolean_t
286 mvneta_has_switch_fdt(device_t self)
287 {
288 	phandle_t node;
289 
290 	node = ofw_bus_get_node(self);
291 
292 	return mvneta_find_ethernet_prop_switch(node, OF_finddevice("/"));
293 }
294