xref: /freebsd/sys/dev/dpaa/if_dtsec_fdt.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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/rman.h>
36 #include <sys/socket.h>
37 
38 #include <machine/bus.h>
39 
40 #include <powerpc/mpc85xx/mpc85xx.h>
41 
42 #include <net/if.h>
43 #include <net/if_media.h>
44 
45 #include <dev/mii/mii.h>
46 #include <dev/mii/miivar.h>
47 
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
50 #include <dev/ofw/openfirm.h>
51 
52 #include "miibus_if.h"
53 
54 #include <contrib/ncsw/inc/Peripherals/fm_port_ext.h>
55 #include <contrib/ncsw/inc/xx_ext.h>
56 
57 #include "if_dtsec.h"
58 #include "fman.h"
59 
60 
61 static int	dtsec_fdt_probe(device_t dev);
62 static int	dtsec_fdt_attach(device_t dev);
63 
64 static device_method_t dtsec_methods[] = {
65 	/* Device interface */
66 	DEVMETHOD(device_probe,		dtsec_fdt_probe),
67 	DEVMETHOD(device_attach,	dtsec_fdt_attach),
68 	DEVMETHOD(device_detach,	dtsec_detach),
69 
70 	DEVMETHOD(device_shutdown,	dtsec_shutdown),
71 	DEVMETHOD(device_suspend,	dtsec_suspend),
72 	DEVMETHOD(device_resume,	dtsec_resume),
73 
74 	/* Bus interface */
75 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
76 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
77 
78 	/* MII interface */
79 	DEVMETHOD(miibus_readreg,	dtsec_miibus_readreg),
80 	DEVMETHOD(miibus_writereg,	dtsec_miibus_writereg),
81 	DEVMETHOD(miibus_statchg,	dtsec_miibus_statchg),
82 
83 	{ 0, 0 }
84 };
85 
86 static driver_t dtsec_driver = {
87 	"dtsec",
88 	dtsec_methods,
89 	sizeof(struct dtsec_softc),
90 };
91 
92 static devclass_t dtsec_devclass;
93 DRIVER_MODULE(dtsec, fman, dtsec_driver, dtsec_devclass, 0, 0);
94 DRIVER_MODULE(miibus, dtsec, miibus_driver, miibus_devclass, 0, 0);
95 MODULE_DEPEND(dtsec, ether, 1, 1, 1);
96 MODULE_DEPEND(dtsec, miibus, 1, 1, 1);
97 
98 static int
99 dtsec_fdt_probe(device_t dev)
100 {
101 
102 	if (!ofw_bus_is_compatible(dev, "fsl,fman-dtsec") &&
103 	    !ofw_bus_is_compatible(dev, "fsl,fman-xgec"))
104 		return (ENXIO);
105 
106 	device_set_desc(dev, "Freescale Data Path Triple Speed Ethernet "
107 	    "Controller");
108 
109 	return (BUS_PROBE_DEFAULT);
110 }
111 
112 static int
113 find_mdio(phandle_t phy_node, device_t mac, device_t *mdio_dev)
114 {
115 	device_t bus;
116 
117 	while (phy_node > 0) {
118 		if (ofw_bus_node_is_compatible(phy_node, "fsl,fman-mdio"))
119 			break;
120 		phy_node = OF_parent(phy_node);
121 	}
122 
123 	if (phy_node <= 0)
124 		return (ENOENT);
125 
126 	bus = device_get_parent(mac);
127 	*mdio_dev = ofw_bus_find_child_device_by_phandle(bus, phy_node);
128 
129 	return (0);
130 }
131 
132 static int
133 dtsec_fdt_attach(device_t dev)
134 {
135 	struct dtsec_softc *sc;
136 	phandle_t enet_node, phy_node;
137 	phandle_t fman_rxtx_node[2];
138 	char phy_type[6];
139 	pcell_t fman_tx_cell, mac_id;
140 	int rid;
141 
142 	sc = device_get_softc(dev);
143 	enet_node = ofw_bus_get_node(dev);
144 
145 	if (OF_getprop(enet_node, "local-mac-address",
146 	    (void *)sc->sc_mac_addr, 6) == -1) {
147 		device_printf(dev,
148 		    "Could not load local-mac-addr property from DTS\n");
149 		return (ENXIO);
150 	}
151 
152 	/* Get link speed */
153 	if (ofw_bus_is_compatible(dev, "fsl,fman-dtsec") != 0)
154 		sc->sc_eth_dev_type = ETH_DTSEC;
155 	else if (ofw_bus_is_compatible(dev, "fsl,fman-xgec") != 0)
156 		sc->sc_eth_dev_type = ETH_10GSEC;
157 	else
158 		return(ENXIO);
159 
160 	/* Get MAC memory offset in SoC */
161 	rid = 0;
162 	sc->sc_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
163 	if (sc->sc_mem == NULL)
164 		return (ENXIO);
165 
166 	/* Get PHY address */
167 	if (OF_getprop(enet_node, "phy-handle", (void *)&phy_node,
168 	    sizeof(phy_node)) <= 0)
169 		return (ENXIO);
170 
171 	phy_node = OF_instance_to_package(phy_node);
172 
173 	if (OF_getprop(phy_node, "reg", (void *)&sc->sc_phy_addr,
174 	    sizeof(sc->sc_phy_addr)) <= 0)
175 		return (ENXIO);
176 
177 	if (find_mdio(phy_node, dev, &sc->sc_mdio) != 0)
178 		return (ENXIO);
179 
180 	/* Get PHY connection type */
181 	if (OF_getprop(enet_node, "phy-connection-type", (void *)phy_type,
182 	    sizeof(phy_type)) <= 0)
183 		return (ENXIO);
184 
185 	if (!strcmp(phy_type, "sgmii"))
186 		sc->sc_mac_enet_mode = e_ENET_MODE_SGMII_1000;
187 	else if (!strcmp(phy_type, "rgmii"))
188 		sc->sc_mac_enet_mode = e_ENET_MODE_RGMII_1000;
189 	else if (!strcmp(phy_type, "xgmii"))
190 		/* We set 10 Gigabit mode flag however we don't support it */
191 		sc->sc_mac_enet_mode = e_ENET_MODE_XGMII_10000;
192 	else
193 		return (ENXIO);
194 
195 	if (OF_getencprop(enet_node, "cell-index",
196 	    (void *)&mac_id, sizeof(mac_id)) <= 0)
197 		return (ENXIO);
198 	sc->sc_eth_id = mac_id;
199 
200 	/* Get RX/TX port handles */
201 	if (OF_getprop(enet_node, "fsl,fman-ports", (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 (ofw_bus_node_is_compatible(fman_rxtx_node[0],
215 	    "fsl,fman-v2-port-rx") == 0)
216 		return (ENXIO);
217 
218 	if (ofw_bus_node_is_compatible(fman_rxtx_node[1],
219 	    "fsl,fman-v2-port-tx") == 0)
220 		return (ENXIO);
221 
222 	/* Get RX port HW id */
223 	if (OF_getprop(fman_rxtx_node[0], "reg", (void *)&sc->sc_port_rx_hw_id,
224 	    sizeof(sc->sc_port_rx_hw_id)) <= 0)
225 		return (ENXIO);
226 
227 	/* Get TX port HW id */
228 	if (OF_getprop(fman_rxtx_node[1], "reg", (void *)&sc->sc_port_tx_hw_id,
229 	    sizeof(sc->sc_port_tx_hw_id)) <= 0)
230 		return (ENXIO);
231 
232 	if (OF_getprop(fman_rxtx_node[1], "cell-index", &fman_tx_cell,
233 	    sizeof(fman_tx_cell)) <= 0)
234 		return (ENXIO);
235 	/* Get QMan channel */
236 	sc->sc_port_tx_qman_chan = fman_qman_channel_id(device_get_parent(dev),
237 	    fman_tx_cell);
238 
239 	return (dtsec_attach(dev));
240 }
241