xref: /freebsd/sys/dev/neta/if_mvneta_fdt.c (revision d93a896e)
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 #define	PHY_MODE_MAXLEN	10
62 #define	INBAND_STATUS_MAXLEN 16
63 
64 static int mvneta_fdt_probe(device_t);
65 static int mvneta_fdt_attach(device_t);
66 
67 static device_method_t mvneta_fdt_methods[] = {
68 	/* Device interface */
69 	DEVMETHOD(device_probe,		mvneta_fdt_probe),
70 	DEVMETHOD(device_attach,	mvneta_fdt_attach),
71 
72 	/* End */
73 	DEVMETHOD_END
74 };
75 
76 DEFINE_CLASS_1(mvneta, mvneta_fdt_driver, mvneta_fdt_methods,
77     sizeof(struct mvneta_softc), mvneta_driver);
78 
79 static devclass_t mvneta_fdt_devclass;
80 
81 DRIVER_MODULE(mvneta, ofwbus, mvneta_fdt_driver, mvneta_fdt_devclass, 0, 0);
82 DRIVER_MODULE(mvneta, simplebus, mvneta_fdt_driver, mvneta_fdt_devclass, 0, 0);
83 
84 static int mvneta_fdt_phy_acquire(device_t);
85 
86 static int
87 mvneta_fdt_probe(device_t dev)
88 {
89 
90 	if (!ofw_bus_status_okay(dev))
91 		return (ENXIO);
92 
93 	if (!ofw_bus_is_compatible(dev, "marvell,armada-370-neta"))
94 		return (ENXIO);
95 
96 	device_set_desc(dev, "NETA controller");
97 	return (BUS_PROBE_DEFAULT);
98 }
99 
100 static int
101 mvneta_fdt_attach(device_t dev)
102 {
103 	int err;
104 
105 	/* Try to fetch PHY information from FDT */
106 	err = mvneta_fdt_phy_acquire(dev);
107 	if (err != 0)
108 		return (err);
109 
110 	return (mvneta_attach(dev));
111 }
112 
113 static int
114 mvneta_fdt_phy_acquire(device_t dev)
115 {
116 	struct mvneta_softc *sc;
117 	phandle_t node, child, phy_handle;
118 	char phymode[PHY_MODE_MAXLEN];
119 	char managed[INBAND_STATUS_MAXLEN];
120 	char *name;
121 
122 	sc = device_get_softc(dev);
123 	node = ofw_bus_get_node(dev);
124 
125 	/* PHY mode is crucial */
126 	if (OF_getprop(node, "phy-mode", phymode, sizeof(phymode)) <= 0) {
127 		device_printf(dev, "Failed to acquire PHY mode from FDT.\n");
128 		return (ENXIO);
129 	}
130 
131 	if (strncmp(phymode, "rgmii-id", 8) == 0)
132 		sc->phy_mode = MVNETA_PHY_RGMII_ID;
133 	else if (strncmp(phymode, "rgmii", 5) == 0)
134 		sc->phy_mode = MVNETA_PHY_RGMII;
135 	else if (strncmp(phymode, "sgmii", 5) == 0)
136 		sc->phy_mode = MVNETA_PHY_SGMII;
137 	else if (strncmp(phymode, "qsgmii", 6) == 0)
138 		sc->phy_mode = MVNETA_PHY_QSGMII;
139 	else
140 		sc->phy_mode = MVNETA_PHY_SGMII;
141 
142 	/* Check if in-band link status will be used */
143 	if (OF_getprop(node, "managed", managed, sizeof(managed)) > 0) {
144 		if (strncmp(managed, "in-band-status", 14) == 0) {
145 			sc->use_inband_status = TRUE;
146 			device_printf(dev, "Use in-band link status.\n");
147 			return (0);
148 		}
149 	}
150 
151 	if (OF_getencprop(node, "phy", (void *)&phy_handle,
152 	    sizeof(phy_handle)) <= 0) {
153 		/* Test for fixed-link (present i.e. in 388-gp) */
154 		for (child = OF_child(node); child != 0; child = OF_peer(child)) {
155 			if (OF_getprop_alloc(child,
156 			    "name", 1, (void **)&name) <= 0) {
157 				continue;
158 			}
159 			if (strncmp(name, "fixed-link", 10) == 0) {
160 				free(name, M_OFWPROP);
161 				if (OF_getencprop(child, "speed",
162 				    &sc->phy_speed, sizeof(sc->phy_speed)) <= 0) {
163 					if (bootverbose) {
164 						device_printf(dev,
165 						    "No PHY information.\n");
166 					}
167 					return (ENXIO);
168 				}
169 				if (OF_hasprop(child, "full-duplex"))
170 					sc->phy_fdx = TRUE;
171 				else
172 					sc->phy_fdx = FALSE;
173 
174 				/* Keep this flag just for the record */
175 				sc->phy_addr = MII_PHY_ANY;
176 
177 				return (0);
178 			}
179 			free(name, M_OFWPROP);
180 		}
181 		if (bootverbose) {
182 			device_printf(dev,
183 			    "Could not find PHY information in FDT.\n");
184 		}
185 		return (ENXIO);
186 	} else {
187 		phy_handle = OF_instance_to_package(phy_handle);
188 		if (OF_getencprop(phy_handle, "reg", &sc->phy_addr,
189 		    sizeof(sc->phy_addr)) <= 0) {
190 			device_printf(dev,
191 			    "Could not find PHY address in FDT.\n");
192 			return (ENXIO);
193 		}
194 	}
195 
196 	return (0);
197 }
198 
199 int
200 mvneta_fdt_mac_address(struct mvneta_softc *sc, uint8_t *addr)
201 {
202 	phandle_t node;
203 	uint8_t lmac[ETHER_ADDR_LEN];
204 	uint8_t zeromac[] = {[0 ... (ETHER_ADDR_LEN - 1)] = 0};
205 	int len;
206 
207 	/*
208 	 * Retrieve hw address from the device tree.
209 	 */
210 	node = ofw_bus_get_node(sc->dev);
211 	if (node == 0)
212 		return (ENXIO);
213 
214 	len = OF_getprop(node, "local-mac-address", (void *)lmac, sizeof(lmac));
215 	if (len != ETHER_ADDR_LEN)
216 		return (ENOENT);
217 
218 	if (memcmp(lmac, zeromac, ETHER_ADDR_LEN) == 0) {
219 		/* Invalid MAC address (all zeros) */
220 		return (EINVAL);
221 	}
222 	memcpy(addr, lmac, ETHER_ADDR_LEN);
223 
224 	return (0);
225 }
226