xref: /freebsd/sys/dev/eqos/if_eqos_fdt.c (revision 62e8ccc3a489434af379c7f47da71545bc1e14ee)
1702b53ddSSøren Schmidt /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3702b53ddSSøren Schmidt  *
4702b53ddSSøren Schmidt  * Copyright (c) 2022 Soren Schmidt <sos@deepcore.dk>
5702b53ddSSøren Schmidt  * All rights reserved.
6702b53ddSSøren Schmidt  *
7702b53ddSSøren Schmidt  * Redistribution and use in source and binary forms, with or without
8702b53ddSSøren Schmidt  * modification, are permitted provided that the following conditions
9702b53ddSSøren Schmidt  * are met:
10702b53ddSSøren Schmidt  * 1. Redistributions of source code must retain the above copyright
11702b53ddSSøren Schmidt  *    notice, this list of conditions and the following disclaimer.
12702b53ddSSøren Schmidt  * 2. Redistributions in binary form must reproduce the above copyright
13702b53ddSSøren Schmidt  *    notice, this list of conditions and the following disclaimer in the
14702b53ddSSøren Schmidt  *    documentation and/or other materials provided with the distribution.
15702b53ddSSøren Schmidt  *
16702b53ddSSøren Schmidt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17702b53ddSSøren Schmidt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18702b53ddSSøren Schmidt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19702b53ddSSøren Schmidt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20702b53ddSSøren Schmidt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21702b53ddSSøren Schmidt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22702b53ddSSøren Schmidt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23702b53ddSSøren Schmidt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24702b53ddSSøren Schmidt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25702b53ddSSøren Schmidt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26702b53ddSSøren Schmidt  * SUCH DAMAGE.
27702b53ddSSøren Schmidt  *
28702b53ddSSøren Schmidt  * $Id: eqos_fdt.c 1049 2022-12-03 14:25:46Z sos $
29702b53ddSSøren Schmidt  */
30702b53ddSSøren Schmidt 
31702b53ddSSøren Schmidt #include "opt_platform.h"
32fdafd315SWarner Losh 
33702b53ddSSøren Schmidt #include <sys/param.h>
34702b53ddSSøren Schmidt #include <sys/bus.h>
35702b53ddSSøren Schmidt #include <sys/kernel.h>
36702b53ddSSøren Schmidt #include <sys/module.h>
37702b53ddSSøren Schmidt #include <sys/systm.h>
38702b53ddSSøren Schmidt #include <sys/endian.h>
39702b53ddSSøren Schmidt #include <sys/hash.h>
40702b53ddSSøren Schmidt #include <sys/gpio.h>
41702b53ddSSøren Schmidt #include <sys/rman.h>
42702b53ddSSøren Schmidt #include <sys/socket.h>
43702b53ddSSøren Schmidt #include <machine/bus.h>
44702b53ddSSøren Schmidt 
45702b53ddSSøren Schmidt #include <net/if.h>
46702b53ddSSøren Schmidt #include <net/if_media.h>
47702b53ddSSøren Schmidt #include <dev/mii/mii.h>
48702b53ddSSøren Schmidt #include <dev/mii/miivar.h>
49702b53ddSSøren Schmidt 
50702b53ddSSøren Schmidt #include <dev/ofw/ofw_bus.h>
51702b53ddSSøren Schmidt #include <dev/ofw/ofw_bus_subr.h>
52702b53ddSSøren Schmidt 
53be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
541f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
55b2f0caf1SEmmanuel Vadot #include <dev/regulator/regulator.h>
56*62e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
57702b53ddSSøren Schmidt 
58702b53ddSSøren Schmidt #include <dev/eqos/if_eqos_var.h>
59702b53ddSSøren Schmidt 
60702b53ddSSøren Schmidt #include "if_eqos_if.h"
61702b53ddSSøren Schmidt #include "syscon_if.h"
62702b53ddSSøren Schmidt #include "gpio_if.h"
63702b53ddSSøren Schmidt #include "rk_otp_if.h"
64702b53ddSSøren Schmidt 
65702b53ddSSøren Schmidt #define	RK356XGMAC0	0xfe2a0000
66702b53ddSSøren Schmidt #define	RK356XGMAC1	0xfe010000
67702b53ddSSøren Schmidt #define	RK3588GMAC0	0xfe1b0000
68702b53ddSSøren Schmidt #define	RK3588GMAC1	0xfe1c0000
69702b53ddSSøren Schmidt 
70702b53ddSSøren Schmidt #define	EQOS_GRF_GMAC0				0x0380
71702b53ddSSøren Schmidt #define	EQOS_GRF_GMAC1				0x0388
72702b53ddSSøren Schmidt #define	EQOS_CON0_OFFSET			0
73702b53ddSSøren Schmidt #define	EQOS_CON1_OFFSET			4
74702b53ddSSøren Schmidt 
75702b53ddSSøren Schmidt #define	EQOS_GMAC_PHY_INTF_SEL_RGMII		0x00fc0010
76702b53ddSSøren Schmidt #define	EQOS_GMAC_PHY_INTF_SEL_RMII		0x00fc0040
77702b53ddSSøren Schmidt #define	EQOS_GMAC_RXCLK_DLY_ENABLE		0x00020002
78702b53ddSSøren Schmidt #define	EQOS_GMAC_RXCLK_DLY_DISABLE		0x00020000
79702b53ddSSøren Schmidt #define	EQOS_GMAC_TXCLK_DLY_ENABLE		0x00010001
80702b53ddSSøren Schmidt #define	EQOS_GMAC_TXCLK_DLY_DISABLE		0x00010000
81702b53ddSSøren Schmidt #define	EQOS_GMAC_CLK_RX_DL_CFG(val)		(0x7f000000 | val << 8)
82702b53ddSSøren Schmidt #define	EQOS_GMAC_CLK_TX_DL_CFG(val)		(0x007f0000 | val)
83702b53ddSSøren Schmidt 
84702b53ddSSøren Schmidt #define	WR4(sc, o, v)		bus_write_4(sc->res[EQOS_RES_MEM], (o), (v))
85702b53ddSSøren Schmidt 
86702b53ddSSøren Schmidt static const struct ofw_compat_data compat_data[] = {
87702b53ddSSøren Schmidt 	{"snps,dwmac-4.20a",	1},
88702b53ddSSøren Schmidt 	{ NULL, 0 }
89702b53ddSSøren Schmidt };
90702b53ddSSøren Schmidt 
91702b53ddSSøren Schmidt 
92702b53ddSSøren Schmidt static int
eqos_phy_reset(device_t dev)93702b53ddSSøren Schmidt eqos_phy_reset(device_t dev)
94702b53ddSSøren Schmidt {
95702b53ddSSøren Schmidt 	pcell_t gpio_prop[4];
96702b53ddSSøren Schmidt 	pcell_t delay_prop[3];
97702b53ddSSøren Schmidt 	phandle_t node, gpio_node;
98702b53ddSSøren Schmidt 	device_t gpio;
99702b53ddSSøren Schmidt 	uint32_t pin, flags;
100702b53ddSSøren Schmidt 	uint32_t pin_value;
101702b53ddSSøren Schmidt 
102702b53ddSSøren Schmidt 	node = ofw_bus_get_node(dev);
103702b53ddSSøren Schmidt 	if (OF_getencprop(node, "snps,reset-gpio",
104702b53ddSSøren Schmidt 	    gpio_prop, sizeof(gpio_prop)) <= 0)
105702b53ddSSøren Schmidt 		return (0);
106702b53ddSSøren Schmidt 
107702b53ddSSøren Schmidt 	if (OF_getencprop(node, "snps,reset-delays-us",
108702b53ddSSøren Schmidt 	    delay_prop, sizeof(delay_prop)) <= 0) {
109702b53ddSSøren Schmidt 		device_printf(dev,
110702b53ddSSøren Schmidt 		    "Wrong property for snps,reset-delays-us");
111702b53ddSSøren Schmidt 		return (ENXIO);
112702b53ddSSøren Schmidt 	}
113702b53ddSSøren Schmidt 
114702b53ddSSøren Schmidt 	gpio_node = OF_node_from_xref(gpio_prop[0]);
115702b53ddSSøren Schmidt 	if ((gpio = OF_device_from_xref(gpio_prop[0])) == NULL) {
116702b53ddSSøren Schmidt 		device_printf(dev,
117702b53ddSSøren Schmidt 		    "Can't find gpio controller for phy reset\n");
118702b53ddSSøren Schmidt 		return (ENXIO);
119702b53ddSSøren Schmidt 	}
120702b53ddSSøren Schmidt 
121702b53ddSSøren Schmidt 	if (GPIO_MAP_GPIOS(gpio, node, gpio_node,
122702b53ddSSøren Schmidt 	    nitems(gpio_prop) - 1,
123702b53ddSSøren Schmidt 	    gpio_prop + 1, &pin, &flags) != 0) {
124702b53ddSSøren Schmidt 		device_printf(dev, "Can't map gpio for phy reset\n");
125702b53ddSSøren Schmidt 		return (ENXIO);
126702b53ddSSøren Schmidt 	}
127702b53ddSSøren Schmidt 
128702b53ddSSøren Schmidt 	pin_value = GPIO_PIN_LOW;
129702b53ddSSøren Schmidt 	if (OF_hasprop(node, "snps,reset-active-low"))
130702b53ddSSøren Schmidt 		pin_value = GPIO_PIN_HIGH;
131702b53ddSSøren Schmidt 
132702b53ddSSøren Schmidt 	GPIO_PIN_SETFLAGS(gpio, pin, GPIO_PIN_OUTPUT);
133702b53ddSSøren Schmidt 	GPIO_PIN_SET(gpio, pin, pin_value);
134702b53ddSSøren Schmidt 	DELAY(delay_prop[0]);
135702b53ddSSøren Schmidt 	GPIO_PIN_SET(gpio, pin, !pin_value);
136702b53ddSSøren Schmidt 	DELAY(delay_prop[1]);
137702b53ddSSøren Schmidt 	GPIO_PIN_SET(gpio, pin, pin_value);
138702b53ddSSøren Schmidt 	DELAY(delay_prop[2]);
139702b53ddSSøren Schmidt 
140702b53ddSSøren Schmidt 	return (0);
141702b53ddSSøren Schmidt }
142702b53ddSSøren Schmidt 
143702b53ddSSøren Schmidt static int
eqos_fdt_init(device_t dev)144702b53ddSSøren Schmidt eqos_fdt_init(device_t dev)
145702b53ddSSøren Schmidt {
146702b53ddSSøren Schmidt 	struct eqos_softc *sc = device_get_softc(dev);
147702b53ddSSøren Schmidt 	phandle_t node = ofw_bus_get_node(dev);
148702b53ddSSøren Schmidt 	hwreset_t eqos_reset;
149702b53ddSSøren Schmidt 	regulator_t eqos_supply;
150702b53ddSSøren Schmidt 	uint32_t rx_delay, tx_delay;
151702b53ddSSøren Schmidt 	uint8_t buffer[16];
15222029445SEmmanuel Vadot 	clk_t stmmaceth, mac_clk_rx, mac_clk_tx, aclk_mac, pclk_mac;
15322029445SEmmanuel Vadot 	uint64_t freq;
15422029445SEmmanuel Vadot 	int error;
155702b53ddSSøren Schmidt 
156702b53ddSSøren Schmidt 	if (OF_hasprop(node, "rockchip,grf") &&
157702b53ddSSøren Schmidt 	    syscon_get_by_ofw_property(dev, node, "rockchip,grf", &sc->grf)) {
158702b53ddSSøren Schmidt 		device_printf(dev, "cannot get grf driver handle\n");
159702b53ddSSøren Schmidt 		return (ENXIO);
160702b53ddSSøren Schmidt 	}
161702b53ddSSøren Schmidt 
162702b53ddSSøren Schmidt 	/* figure out if gmac0 or gmac1 offset */
163702b53ddSSøren Schmidt 	switch (rman_get_start(sc->res[EQOS_RES_MEM])) {
164702b53ddSSøren Schmidt 	case RK356XGMAC0:	/* RK356X gmac0 */
165702b53ddSSøren Schmidt 		sc->grf_offset = EQOS_GRF_GMAC0;
166702b53ddSSøren Schmidt 		break;
167702b53ddSSøren Schmidt 	case RK356XGMAC1:	/* RK356X gmac1 */
168702b53ddSSøren Schmidt 		sc->grf_offset = EQOS_GRF_GMAC1;
169702b53ddSSøren Schmidt 		break;
170702b53ddSSøren Schmidt 	case RK3588GMAC0:	/* RK3588 gmac0 */
171702b53ddSSøren Schmidt 	case RK3588GMAC1:	/* RK3588 gmac1 */
172702b53ddSSøren Schmidt 	default:
173702b53ddSSøren Schmidt 		device_printf(dev, "Unknown eqos address\n");
174702b53ddSSøren Schmidt 		return (ENXIO);
175702b53ddSSøren Schmidt 	}
176702b53ddSSøren Schmidt 
177702b53ddSSøren Schmidt 	if (hwreset_get_by_ofw_idx(dev, node, 0, &eqos_reset)) {
178702b53ddSSøren Schmidt 		device_printf(dev, "cannot get reset\n");
179702b53ddSSøren Schmidt 		return (ENXIO);
180702b53ddSSøren Schmidt 	}
181702b53ddSSøren Schmidt 	hwreset_assert(eqos_reset);
182702b53ddSSøren Schmidt 
18322029445SEmmanuel Vadot 	error = clk_set_assigned(dev, ofw_bus_get_node(dev));
18422029445SEmmanuel Vadot 	if (error != 0) {
18522029445SEmmanuel Vadot 		device_printf(dev, "clk_set_assigned failed\n");
18622029445SEmmanuel Vadot 		return (error);
18722029445SEmmanuel Vadot 	}
18822029445SEmmanuel Vadot 
18922029445SEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "stmmaceth", &stmmaceth) == 0) {
19022029445SEmmanuel Vadot 		error = clk_enable(stmmaceth);
19122029445SEmmanuel Vadot 		if (error != 0) {
19222029445SEmmanuel Vadot 			device_printf(dev, "could not enable main clock\n");
19322029445SEmmanuel Vadot 			return (error);
19422029445SEmmanuel Vadot 		}
19522029445SEmmanuel Vadot 		if (bootverbose) {
19622029445SEmmanuel Vadot 			clk_get_freq(stmmaceth, &freq);
19722029445SEmmanuel Vadot 			device_printf(dev, "MAC clock(%s) freq: %jd\n",
19822029445SEmmanuel Vadot 					clk_get_name(stmmaceth), (intmax_t)freq);
19922029445SEmmanuel Vadot 		}
20022029445SEmmanuel Vadot 	}
20122029445SEmmanuel Vadot 	else {
20222029445SEmmanuel Vadot 		device_printf(dev, "could not find clock stmmaceth\n");
20322029445SEmmanuel Vadot 	}
20422029445SEmmanuel Vadot 
20522029445SEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "mac_clk_rx", &mac_clk_rx) != 0) {
20622029445SEmmanuel Vadot 		device_printf(dev, "could not get mac_clk_rx clock\n");
20722029445SEmmanuel Vadot 		mac_clk_rx = NULL;
20822029445SEmmanuel Vadot 	}
20922029445SEmmanuel Vadot 
21022029445SEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "mac_clk_tx", &mac_clk_tx) != 0) {
21122029445SEmmanuel Vadot 		device_printf(dev, "could not get mac_clk_tx clock\n");
21222029445SEmmanuel Vadot 		mac_clk_tx = NULL;
21322029445SEmmanuel Vadot 	}
21422029445SEmmanuel Vadot 
21522029445SEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "aclk_mac", &aclk_mac) != 0) {
21622029445SEmmanuel Vadot 		device_printf(dev, "could not get aclk_mac clock\n");
21722029445SEmmanuel Vadot 		aclk_mac = NULL;
21822029445SEmmanuel Vadot 	}
21922029445SEmmanuel Vadot 
22022029445SEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "pclk_mac", &pclk_mac) != 0) {
22122029445SEmmanuel Vadot 		device_printf(dev, "could not get pclk_mac clock\n");
22222029445SEmmanuel Vadot 		pclk_mac = NULL;
22322029445SEmmanuel Vadot 	}
22422029445SEmmanuel Vadot 
22522029445SEmmanuel Vadot 	if (aclk_mac)
22622029445SEmmanuel Vadot 		clk_enable(aclk_mac);
22722029445SEmmanuel Vadot 	if (pclk_mac)
22822029445SEmmanuel Vadot 		clk_enable(pclk_mac);
22922029445SEmmanuel Vadot 	if (mac_clk_tx)
23022029445SEmmanuel Vadot 		clk_enable(mac_clk_tx);
23122029445SEmmanuel Vadot 
232702b53ddSSøren Schmidt 	sc->csr_clock = 125000000;
233702b53ddSSøren Schmidt 	sc->csr_clock_range = GMAC_MAC_MDIO_ADDRESS_CR_100_150;
234702b53ddSSøren Schmidt 
235702b53ddSSøren Schmidt 	if (OF_getencprop(node, "tx_delay", &tx_delay, sizeof(tx_delay)) <= 0)
236702b53ddSSøren Schmidt 		tx_delay = 0x30;
237702b53ddSSøren Schmidt 	if (OF_getencprop(node, "rx_delay", &rx_delay, sizeof(rx_delay)) <= 0)
238702b53ddSSøren Schmidt 		rx_delay = 0x10;
239702b53ddSSøren Schmidt 
240702b53ddSSøren Schmidt 	SYSCON_WRITE_4(sc->grf, sc->grf_offset + EQOS_CON0_OFFSET,
241702b53ddSSøren Schmidt 	    EQOS_GMAC_CLK_RX_DL_CFG(rx_delay) |
242702b53ddSSøren Schmidt 	    EQOS_GMAC_CLK_TX_DL_CFG(tx_delay));
243702b53ddSSøren Schmidt 	SYSCON_WRITE_4(sc->grf, sc->grf_offset + EQOS_CON1_OFFSET,
244702b53ddSSøren Schmidt 	    EQOS_GMAC_PHY_INTF_SEL_RGMII |
245702b53ddSSøren Schmidt 	    EQOS_GMAC_RXCLK_DLY_ENABLE |
246702b53ddSSøren Schmidt 	    EQOS_GMAC_TXCLK_DLY_ENABLE);
247702b53ddSSøren Schmidt 
248702b53ddSSøren Schmidt 	if (!regulator_get_by_ofw_property(dev, 0, "phy-supply",
249702b53ddSSøren Schmidt 	    &eqos_supply)) {
250702b53ddSSøren Schmidt 		if (regulator_enable(eqos_supply))
251702b53ddSSøren Schmidt 			device_printf(dev, "cannot enable 'phy' regulator\n");
252702b53ddSSøren Schmidt 	}
253702b53ddSSøren Schmidt 	else
254702b53ddSSøren Schmidt 		device_printf(dev, "no phy-supply property\n");
255702b53ddSSøren Schmidt 
256702b53ddSSøren Schmidt 	if (eqos_phy_reset(dev))
257702b53ddSSøren Schmidt 		return (ENXIO);
258702b53ddSSøren Schmidt 
259702b53ddSSøren Schmidt 	if (eqos_reset)
260702b53ddSSøren Schmidt 		hwreset_deassert(eqos_reset);
261702b53ddSSøren Schmidt 
262702b53ddSSøren Schmidt 	/* set the MAC address if we have OTP data handy */
263702b53ddSSøren Schmidt 	if (!RK_OTP_READ(dev, buffer, 0, sizeof(buffer))) {
264702b53ddSSøren Schmidt 		uint32_t mac;
265702b53ddSSøren Schmidt 
266702b53ddSSøren Schmidt 		mac = hash32_buf(buffer, sizeof(buffer), HASHINIT);
267702b53ddSSøren Schmidt 		WR4(sc, GMAC_MAC_ADDRESS0_LOW,
268702b53ddSSøren Schmidt 		    htobe32((mac & 0xffffff00) | 0x22));
269702b53ddSSøren Schmidt 
270702b53ddSSøren Schmidt 		mac = hash32_buf(buffer, sizeof(buffer), mac);
271702b53ddSSøren Schmidt 		WR4(sc, GMAC_MAC_ADDRESS0_HIGH,
272702b53ddSSøren Schmidt 		    htobe16((mac & 0x0000ffff) + (device_get_unit(dev) << 8)));
273702b53ddSSøren Schmidt 	}
274702b53ddSSøren Schmidt 
275702b53ddSSøren Schmidt 	return (0);
276702b53ddSSøren Schmidt }
277702b53ddSSøren Schmidt 
278702b53ddSSøren Schmidt static int
eqos_fdt_probe(device_t dev)279702b53ddSSøren Schmidt eqos_fdt_probe(device_t dev)
280702b53ddSSøren Schmidt {
281702b53ddSSøren Schmidt 
282702b53ddSSøren Schmidt 	if (!ofw_bus_status_okay(dev))
283702b53ddSSøren Schmidt 		return (ENXIO);
284702b53ddSSøren Schmidt         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
285702b53ddSSøren Schmidt 		return (ENXIO);
286702b53ddSSøren Schmidt 
287702b53ddSSøren Schmidt 	device_set_desc(dev, "DesignWare EQOS Gigabit ethernet");
288702b53ddSSøren Schmidt 
289702b53ddSSøren Schmidt 	return (BUS_PROBE_DEFAULT);
290702b53ddSSøren Schmidt }
291702b53ddSSøren Schmidt 
292702b53ddSSøren Schmidt 
293702b53ddSSøren Schmidt static device_method_t eqos_fdt_methods[] = {
294702b53ddSSøren Schmidt 	/* Device interface */
295702b53ddSSøren Schmidt 	DEVMETHOD(device_probe,		eqos_fdt_probe),
296702b53ddSSøren Schmidt 
297702b53ddSSøren Schmidt 	/* EQOS interface */
2985ba0691dSSøren Schmidt 	DEVMETHOD(if_eqos_init,		eqos_fdt_init),
299702b53ddSSøren Schmidt 
300702b53ddSSøren Schmidt 	DEVMETHOD_END
301702b53ddSSøren Schmidt };
302702b53ddSSøren Schmidt 
303702b53ddSSøren Schmidt DEFINE_CLASS_1(eqos, eqos_fdt_driver, eqos_fdt_methods,
304702b53ddSSøren Schmidt     sizeof(struct eqos_softc), eqos_driver);
305702b53ddSSøren Schmidt DRIVER_MODULE(eqos, simplebus, eqos_fdt_driver, 0, 0);
306702b53ddSSøren Schmidt MODULE_DEPEND(eqos, ether, 1, 1, 1);
307702b53ddSSøren Schmidt MODULE_DEPEND(eqos, miibus, 1, 1, 1);
308