xref: /freebsd/sys/dev/dpaa/if_dtsec_fdt.c (revision 439df1f0d43a9e92ea02a4162c7f119bdce16f93)
1 /*-
2  * Copyright (c) 2012 Semihalf.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/module.h>
35 #include <sys/socket.h>
36 
37 #include <powerpc/mpc85xx/mpc85xx.h>
38 
39 #include <net/if.h>
40 #include <net/if_media.h>
41 
42 #include <dev/mii/mii.h>
43 #include <dev/mii/miivar.h>
44 
45 #include <dev/fdt/fdt_common.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/ofw/openfirm.h>
49 
50 #include "miibus_if.h"
51 
52 #include <contrib/ncsw/inc/Peripherals/fm_port_ext.h>
53 #include <contrib/ncsw/inc/xx_ext.h>
54 
55 #include "if_dtsec.h"
56 #include "fman.h"
57 
58 
59 static int	dtsec_fdt_probe(device_t dev);
60 static int	dtsec_fdt_attach(device_t dev);
61 
62 static device_method_t dtsec_methods[] = {
63 	/* Device interface */
64 	DEVMETHOD(device_probe,		dtsec_fdt_probe),
65 	DEVMETHOD(device_attach,	dtsec_fdt_attach),
66 	DEVMETHOD(device_detach,	dtsec_detach),
67 
68 	DEVMETHOD(device_shutdown,	dtsec_shutdown),
69 	DEVMETHOD(device_suspend,	dtsec_suspend),
70 	DEVMETHOD(device_resume,	dtsec_resume),
71 
72 	/* Bus interface */
73 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
74 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
75 
76 	/* MII interface */
77 	DEVMETHOD(miibus_readreg,	dtsec_miibus_readreg),
78 	DEVMETHOD(miibus_writereg,	dtsec_miibus_writereg),
79 	DEVMETHOD(miibus_statchg,	dtsec_miibus_statchg),
80 
81 	{ 0, 0 }
82 };
83 
84 static driver_t dtsec_driver = {
85 	"dtsec",
86 	dtsec_methods,
87 	sizeof(struct dtsec_softc),
88 };
89 
90 static devclass_t dtsec_devclass;
91 DRIVER_MODULE(dtsec, dpaa, dtsec_driver, dtsec_devclass, 0, 0);
92 DRIVER_MODULE(miibus, dtsec, miibus_driver, miibus_devclass, 0, 0);
93 MODULE_DEPEND(dtsec, ether, 1, 1, 1);
94 MODULE_DEPEND(dtsec, miibus, 1, 1, 1);
95 
96 static int
97 dtsec_fdt_probe(device_t dev)
98 {
99 
100 	if (!ofw_bus_is_compatible(dev, "fsl,dpa-ethernet"))
101 		return (ENXIO);
102 
103 	device_set_desc(dev, "Freescale Data Path Triple Speed Ethernet "
104 	    "Controller");
105 
106 	return (BUS_PROBE_DEFAULT);
107 }
108 
109 static int
110 find_mdio(phandle_t phy_node, device_t mac, device_t *mdio_dev)
111 {
112 	device_t bus;
113 
114 	while (phy_node > 0) {
115 		if (ofw_bus_node_is_compatible(phy_node, "fsl,fman-mdio"))
116 			break;
117 		phy_node = OF_parent(phy_node);
118 	}
119 
120 	if (phy_node <= 0)
121 		return (ENOENT);
122 
123 	if (fman_get_dev(&bus) < 0)
124 		return (ENOENT);
125 
126 	*mdio_dev = ofw_bus_find_child_device_by_phandle(bus, phy_node);
127 
128 	return (0);
129 }
130 
131 static int
132 dtsec_fdt_attach(device_t dev)
133 {
134 	struct dtsec_softc *sc;
135 	phandle_t node, enet_node, phy_node;
136 	phandle_t fman_rxtx_node[2];
137 	char phy_type[6];
138 
139 	sc = device_get_softc(dev);
140 	node = ofw_bus_get_node(dev);
141 
142 	if (OF_getprop(node, "fsl,fman-mac", (void *)&enet_node,
143 	    sizeof(enet_node)) == -1) {
144 		device_printf(dev, "Could not load fsl,fman-mac property "
145 		    "from DTS\n");
146 		return (ENXIO);
147 	}
148 
149 	enet_node = OF_instance_to_package(enet_node);
150 
151 	if (OF_getprop(enet_node, "local-mac-address",
152 	    (void *)sc->sc_mac_addr, 6) == -1) {
153 		device_printf(dev,
154 		    "Could not load local-mac-addr property from DTS\n");
155 		return (ENXIO);
156 	}
157 
158 	/* Get link speed */
159 	if (fdt_is_compatible(enet_node, "fsl,fman-1g-mac") != 0)
160 		sc->sc_eth_dev_type = ETH_DTSEC;
161 	else if (fdt_is_compatible(enet_node, "fsl,fman-10g-mac") != 0)
162 		sc->sc_eth_dev_type = ETH_10GSEC;
163 	else
164 		return(ENXIO);
165 
166 	/* Get MAC memory offset in SoC */
167 	if (OF_getprop(enet_node, "reg", (void *)&sc->sc_mac_mem_offset,
168 	    sizeof(sc->sc_mac_mem_offset)) <= 0)
169 		return (ENXIO);
170 
171 	/* Get PHY address */
172 	if (OF_getprop(enet_node, "phy-handle", (void *)&phy_node,
173 	    sizeof(phy_node)) <= 0)
174 		return (ENXIO);
175 
176 	phy_node = OF_instance_to_package(phy_node);
177 
178 	if (OF_getprop(phy_node, "reg", (void *)&sc->sc_phy_addr,
179 	    sizeof(sc->sc_phy_addr)) <= 0)
180 		return (ENXIO);
181 
182 	if (find_mdio(phy_node, dev, &sc->sc_mdio) != 0)
183 		return (ENXIO);
184 
185 	/* Get PHY connection type */
186 	if (OF_getprop(enet_node, "phy-connection-type", (void *)phy_type,
187 	    sizeof(phy_type)) <= 0)
188 		return (ENXIO);
189 
190 	if (!strcmp(phy_type, "sgmii"))
191 		sc->sc_mac_enet_mode = e_ENET_MODE_SGMII_1000;
192 	else if (!strcmp(phy_type, "rgmii"))
193 		sc->sc_mac_enet_mode = e_ENET_MODE_RGMII_1000;
194 	else if (!strcmp(phy_type, "xgmii"))
195 		/* We set 10 Gigabit mode flag however we don't support it */
196 		sc->sc_mac_enet_mode = e_ENET_MODE_XGMII_10000;
197 	else
198 		return (ENXIO);
199 
200 	/* Get RX/TX port handles */
201 	if (OF_getprop(enet_node, "fsl,port-handles", (void *)fman_rxtx_node,
202 	    sizeof(fman_rxtx_node)) <= 0)
203 		return (ENXIO);
204 
205 	if (fman_rxtx_node[0] == 0)
206 		return (ENXIO);
207 
208 	if (fman_rxtx_node[1] == 0)
209 		return (ENXIO);
210 
211 	fman_rxtx_node[0] = OF_instance_to_package(fman_rxtx_node[0]);
212 	fman_rxtx_node[1] = OF_instance_to_package(fman_rxtx_node[1]);
213 
214 	if (fdt_is_compatible(fman_rxtx_node[0], "fsl,fman-port-1g-rx") == 0)
215 		return (ENXIO);
216 
217 	if (fdt_is_compatible(fman_rxtx_node[1], "fsl,fman-port-1g-tx") == 0)
218 		return (ENXIO);
219 
220 	/* Get RX port HW id */
221 	if (OF_getprop(fman_rxtx_node[0], "reg", (void *)&sc->sc_port_rx_hw_id,
222 	    sizeof(sc->sc_port_rx_hw_id)) <= 0)
223 		return (ENXIO);
224 
225 	/* Get TX port HW id */
226 	if (OF_getprop(fman_rxtx_node[1], "reg", (void *)&sc->sc_port_tx_hw_id,
227 	    sizeof(sc->sc_port_tx_hw_id)) <= 0)
228 		return (ENXIO);
229 
230 	/* Get QMan channel */
231 	if (OF_getprop(fman_rxtx_node[1], "fsl,qman-channel-id",
232 	    (void *)&sc->sc_port_tx_qman_chan,
233 	    sizeof(sc->sc_port_tx_qman_chan)) <= 0)
234 		return (ENXIO);
235 
236 	return (dtsec_attach(dev));
237 }
238