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