meson_dwmac.c revision 1.16 1 1.16 skrll /* $NetBSD: meson_dwmac.c,v 1.16 2024/10/13 08:55:24 skrll Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill
31 1.16 skrll __KERNEL_RCSID(0, "$NetBSD: meson_dwmac.c,v 1.16 2024/10/13 08:55:24 skrll Exp $");
32 1.1 jmcneill
33 1.1 jmcneill #include <sys/param.h>
34 1.1 jmcneill #include <sys/bus.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/intr.h>
37 1.1 jmcneill #include <sys/systm.h>
38 1.1 jmcneill #include <sys/gpio.h>
39 1.6 msaitoh #include <sys/rndsource.h>
40 1.1 jmcneill
41 1.1 jmcneill #include <net/if.h>
42 1.1 jmcneill #include <net/if_ether.h>
43 1.1 jmcneill #include <net/if_media.h>
44 1.1 jmcneill
45 1.1 jmcneill #include <dev/mii/miivar.h>
46 1.1 jmcneill
47 1.1 jmcneill #include <dev/ic/dwc_gmac_var.h>
48 1.1 jmcneill #include <dev/ic/dwc_gmac_reg.h>
49 1.1 jmcneill
50 1.1 jmcneill #include <dev/fdt/fdtvar.h>
51 1.1 jmcneill
52 1.4 jmcneill #define PRG_ETHERNET_ADDR0 0x00
53 1.4 jmcneill #define CLKGEN_ENABLE __BIT(12)
54 1.5 jmcneill #define RMII_CLK_I_INVERTED __BIT(11)
55 1.4 jmcneill #define PHY_CLK_ENABLE __BIT(10)
56 1.4 jmcneill #define MP2_CLK_OUT_DIV __BITS(9,7)
57 1.4 jmcneill #define TX_CLK_DELAY __BITS(6,5)
58 1.4 jmcneill #define PHY_INTERFACE_SEL __BIT(0)
59 1.1 jmcneill
60 1.11 thorpej static const struct device_compatible_entry compat_data[] = {
61 1.11 thorpej { .compat = "amlogic,meson8b-dwmac" },
62 1.11 thorpej { .compat = "amlogic,meson-gx-dwmac" },
63 1.11 thorpej { .compat = "amlogic,meson-gxbb-dwmac" },
64 1.11 thorpej { .compat = "amlogic,meson-axg-dwmac" },
65 1.11 thorpej DEVICE_COMPAT_EOL
66 1.1 jmcneill };
67 1.1 jmcneill
68 1.1 jmcneill static int
69 1.14 jdc meson_dwmac_reset_eth(const int phandle)
70 1.1 jmcneill {
71 1.1 jmcneill struct fdtbus_gpio_pin *pin_reset;
72 1.1 jmcneill const u_int *reset_delay_us;
73 1.1 jmcneill bool reset_active_low;
74 1.1 jmcneill int len, val;
75 1.1 jmcneill
76 1.13 jdc pin_reset = fdtbus_gpio_acquire(phandle, "snps,reset-gpio",
77 1.13 jdc GPIO_PIN_OUTPUT);
78 1.14 jdc if (pin_reset == NULL)
79 1.14 jdc return ENXIO;
80 1.13 jdc
81 1.14 jdc reset_delay_us = fdtbus_get_prop(phandle, "snps,reset-delays-us", &len);
82 1.14 jdc if (reset_delay_us == NULL || len != 12)
83 1.14 jdc return ENXIO;
84 1.13 jdc
85 1.14 jdc reset_active_low = of_hasprop(phandle, "snps,reset-active-low");
86 1.13 jdc
87 1.14 jdc val = reset_active_low ? 1 : 0;
88 1.13 jdc
89 1.14 jdc fdtbus_gpio_write(pin_reset, val);
90 1.14 jdc delay(be32toh(reset_delay_us[0]));
91 1.14 jdc fdtbus_gpio_write(pin_reset, !val);
92 1.14 jdc delay(be32toh(reset_delay_us[1]));
93 1.14 jdc fdtbus_gpio_write(pin_reset, val);
94 1.14 jdc delay(be32toh(reset_delay_us[2]));
95 1.13 jdc
96 1.14 jdc return 0;
97 1.14 jdc }
98 1.14 jdc
99 1.14 jdc static int
100 1.14 jdc meson_dwmac_reset_phy(const int phandle)
101 1.14 jdc {
102 1.14 jdc struct fdtbus_gpio_pin *pin_reset;
103 1.14 jdc const u_int *reset_assert_us, *reset_deassert_us, *reset_gpios;
104 1.14 jdc bool reset_active_low;
105 1.14 jdc int len, val;
106 1.13 jdc
107 1.13 jdc pin_reset = fdtbus_gpio_acquire(phandle, "reset-gpios",
108 1.13 jdc GPIO_PIN_OUTPUT);
109 1.14 jdc if (pin_reset == NULL)
110 1.14 jdc return ENXIO;
111 1.1 jmcneill
112 1.14 jdc reset_assert_us = fdtbus_get_prop(phandle, "reset-assert-us", &len);
113 1.14 jdc if (reset_assert_us == NULL || len != 4)
114 1.14 jdc return ENXIO;
115 1.14 jdc reset_deassert_us = fdtbus_get_prop(phandle, "reset-deassert-us", &len);
116 1.14 jdc if (reset_deassert_us == NULL || len != 4)
117 1.14 jdc return ENXIO;
118 1.14 jdc reset_gpios = fdtbus_get_prop(phandle, "reset-gpios", &len);
119 1.14 jdc if (reset_gpios == NULL || len != 12)
120 1.14 jdc return ENXIO;
121 1.14 jdc
122 1.14 jdc reset_active_low = be32toh(reset_gpios[2]);
123 1.14 jdc
124 1.14 jdc val = reset_active_low ? 1 : 0;
125 1.14 jdc
126 1.14 jdc fdtbus_gpio_write(pin_reset, val);
127 1.14 jdc delay(be32toh(reset_assert_us[0]));
128 1.14 jdc fdtbus_gpio_write(pin_reset, !val);
129 1.14 jdc delay(be32toh(reset_deassert_us[0]));
130 1.1 jmcneill
131 1.14 jdc return 0;
132 1.1 jmcneill }
133 1.1 jmcneill
134 1.4 jmcneill static void
135 1.4 jmcneill meson_dwmac_set_mode_rgmii(int phandle, bus_space_tag_t bst,
136 1.4 jmcneill bus_space_handle_t bsh, struct clk *clkin)
137 1.4 jmcneill {
138 1.4 jmcneill u_int tx_delay;
139 1.4 jmcneill uint32_t val;
140 1.4 jmcneill
141 1.9 ryo #define DIV_ROUND_OFF(x, y) (((x) + (y) / 2) / (y))
142 1.9 ryo const u_int div = DIV_ROUND_OFF(clk_get_rate(clkin), 250000000);
143 1.4 jmcneill
144 1.4 jmcneill if (of_getprop_uint32(phandle, "amlogic,tx-delay-ns", &tx_delay) != 0)
145 1.4 jmcneill tx_delay = 2;
146 1.4 jmcneill
147 1.4 jmcneill val = bus_space_read_4(bst, bsh, PRG_ETHERNET_ADDR0);
148 1.4 jmcneill val |= PHY_INTERFACE_SEL;
149 1.4 jmcneill val &= ~TX_CLK_DELAY;
150 1.4 jmcneill val |= __SHIFTIN((tx_delay >> 1), TX_CLK_DELAY);
151 1.4 jmcneill val &= ~MP2_CLK_OUT_DIV;
152 1.4 jmcneill val |= __SHIFTIN(div, MP2_CLK_OUT_DIV);
153 1.4 jmcneill val |= PHY_CLK_ENABLE;
154 1.4 jmcneill val |= CLKGEN_ENABLE;
155 1.4 jmcneill bus_space_write_4(bst, bsh, PRG_ETHERNET_ADDR0, val);
156 1.4 jmcneill }
157 1.4 jmcneill
158 1.5 jmcneill static void
159 1.5 jmcneill meson_dwmac_set_mode_rmii(int phandle, bus_space_tag_t bst,
160 1.5 jmcneill bus_space_handle_t bsh)
161 1.5 jmcneill {
162 1.5 jmcneill uint32_t val;
163 1.5 jmcneill
164 1.5 jmcneill val = bus_space_read_4(bst, bsh, PRG_ETHERNET_ADDR0);
165 1.5 jmcneill val &= ~PHY_INTERFACE_SEL;
166 1.5 jmcneill val |= RMII_CLK_I_INVERTED;
167 1.5 jmcneill val &= ~TX_CLK_DELAY;
168 1.5 jmcneill val |= CLKGEN_ENABLE;
169 1.5 jmcneill bus_space_write_4(bst, bsh, PRG_ETHERNET_ADDR0, val);
170 1.5 jmcneill }
171 1.5 jmcneill
172 1.1 jmcneill static int
173 1.1 jmcneill meson_dwmac_intr(void *arg)
174 1.1 jmcneill {
175 1.1 jmcneill struct dwc_gmac_softc * const sc = arg;
176 1.1 jmcneill
177 1.1 jmcneill return dwc_gmac_intr(sc);
178 1.1 jmcneill }
179 1.1 jmcneill
180 1.1 jmcneill static int
181 1.1 jmcneill meson_dwmac_match(device_t parent, cfdata_t cf, void *aux)
182 1.1 jmcneill {
183 1.1 jmcneill struct fdt_attach_args * const faa = aux;
184 1.1 jmcneill
185 1.11 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
186 1.1 jmcneill }
187 1.1 jmcneill
188 1.1 jmcneill static void
189 1.1 jmcneill meson_dwmac_attach(device_t parent, device_t self, void *aux)
190 1.1 jmcneill {
191 1.1 jmcneill struct dwc_gmac_softc * const sc = device_private(self);
192 1.1 jmcneill struct fdt_attach_args * const faa = aux;
193 1.1 jmcneill const int phandle = faa->faa_phandle;
194 1.9 ryo int miiclk, phandle_phy, phy = MII_PHY_ANY;
195 1.9 ryo u_int miiclk_rate;
196 1.4 jmcneill bus_space_handle_t prgeth_bsh;
197 1.1 jmcneill struct fdtbus_reset *rst_gmac;
198 1.4 jmcneill struct clk *clk_gmac, *clk_in[2];
199 1.1 jmcneill const char *phy_mode;
200 1.1 jmcneill char intrstr[128];
201 1.4 jmcneill bus_addr_t addr[2];
202 1.4 jmcneill bus_size_t size[2];
203 1.1 jmcneill
204 1.4 jmcneill if (fdtbus_get_reg(phandle, 0, &addr[0], &size[0]) != 0 ||
205 1.4 jmcneill fdtbus_get_reg(phandle, 1, &addr[1], &size[1]) != 0) {
206 1.1 jmcneill aprint_error(": couldn't get registers\n");
207 1.1 jmcneill return;
208 1.1 jmcneill }
209 1.1 jmcneill
210 1.1 jmcneill sc->sc_dev = self;
211 1.1 jmcneill sc->sc_bst = faa->faa_bst;
212 1.4 jmcneill if (bus_space_map(sc->sc_bst, addr[0], size[0], 0, &sc->sc_bsh) != 0 ||
213 1.4 jmcneill bus_space_map(sc->sc_bst, addr[1], size[1], 0, &prgeth_bsh) != 0) {
214 1.1 jmcneill aprint_error(": couldn't map registers\n");
215 1.1 jmcneill return;
216 1.1 jmcneill }
217 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
218 1.1 jmcneill
219 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
220 1.1 jmcneill aprint_error(": failed to decode interrupt\n");
221 1.1 jmcneill return;
222 1.1 jmcneill }
223 1.1 jmcneill
224 1.1 jmcneill clk_gmac = fdtbus_clock_get(phandle, "stmmaceth");
225 1.4 jmcneill clk_in[0] = fdtbus_clock_get(phandle, "clkin0");
226 1.4 jmcneill clk_in[1] = fdtbus_clock_get(phandle, "clkin1");
227 1.4 jmcneill if (clk_gmac == NULL || clk_in[0] == NULL || clk_in[1] == NULL) {
228 1.1 jmcneill aprint_error(": couldn't get clocks\n");
229 1.1 jmcneill return;
230 1.1 jmcneill }
231 1.1 jmcneill
232 1.1 jmcneill rst_gmac = fdtbus_reset_get(phandle, "stmmaceth");
233 1.1 jmcneill
234 1.1 jmcneill phy_mode = fdtbus_get_string(phandle, "phy-mode");
235 1.1 jmcneill if (phy_mode == NULL) {
236 1.1 jmcneill aprint_error(": missing 'phy-mode' property\n");
237 1.1 jmcneill return;
238 1.1 jmcneill }
239 1.9 ryo phandle_phy = fdtbus_get_phandle(phandle, "phy-handle");
240 1.9 ryo if (phandle_phy > 0) {
241 1.9 ryo of_getprop_uint32(phandle_phy, "reg", &phy);
242 1.9 ryo } else {
243 1.9 ryo phandle_phy = phandle;
244 1.9 ryo }
245 1.4 jmcneill
246 1.12 jmcneill if (strncmp(phy_mode, "rgmii", 5) == 0) {
247 1.4 jmcneill meson_dwmac_set_mode_rgmii(phandle, sc->sc_bst, prgeth_bsh, clk_in[0]);
248 1.5 jmcneill } else if (strcmp(phy_mode, "rmii") == 0) {
249 1.5 jmcneill meson_dwmac_set_mode_rmii(phandle, sc->sc_bst, prgeth_bsh);
250 1.1 jmcneill } else {
251 1.1 jmcneill aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
252 1.1 jmcneill return;
253 1.1 jmcneill }
254 1.1 jmcneill
255 1.1 jmcneill if (clk_enable(clk_gmac) != 0) {
256 1.1 jmcneill aprint_error(": couldn't enable clock\n");
257 1.1 jmcneill return;
258 1.1 jmcneill }
259 1.1 jmcneill
260 1.1 jmcneill if (rst_gmac != NULL && fdtbus_reset_deassert(rst_gmac) != 0) {
261 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
262 1.1 jmcneill return;
263 1.1 jmcneill }
264 1.1 jmcneill
265 1.1 jmcneill aprint_naive("\n");
266 1.1 jmcneill aprint_normal(": Gigabit Ethernet Controller\n");
267 1.1 jmcneill
268 1.10 ryo if (fdtbus_intr_establish_xname(phandle, 0, IPL_NET,
269 1.15 skrll FDT_INTR_MPSAFE, meson_dwmac_intr, sc,
270 1.10 ryo device_xname(sc->sc_dev)) == NULL) {
271 1.1 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n", intrstr);
272 1.1 jmcneill return;
273 1.1 jmcneill }
274 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
275 1.1 jmcneill
276 1.14 jdc /*
277 1.14 jdc * Depending on the DTS, we need to check either the "snps,...",
278 1.14 jdc * properties on the ethernet node, or the "reset-..."
279 1.14 jdc * properties on the phy node for the MAC reset information.
280 1.14 jdc */
281 1.14 jdc
282 1.14 jdc if (of_hasprop(phandle, "snps,reset-gpio")) {
283 1.14 jdc if (meson_dwmac_reset_eth(phandle) != 0)
284 1.16 skrll aprint_error_dev(self, "reset failed\n");
285 1.14 jdc } else {
286 1.14 jdc if (meson_dwmac_reset_phy(phandle_phy) != 0)
287 1.14 jdc aprint_error_dev(self, "PHY reset failed\n");
288 1.14 jdc }
289 1.1 jmcneill
290 1.9 ryo miiclk_rate = clk_get_rate(clk_gmac);
291 1.9 ryo if (miiclk_rate > 250 * 1000 * 1000)
292 1.9 ryo miiclk = GMAC_MII_CLK_250_300M_DIV124;
293 1.9 ryo else if (miiclk_rate > 150 * 1000 * 1000)
294 1.9 ryo miiclk = GMAC_MII_CLK_150_250M_DIV102;
295 1.9 ryo else if (miiclk_rate > 100 * 1000 * 1000)
296 1.9 ryo miiclk = GMAC_MII_CLK_100_150M_DIV62;
297 1.9 ryo else if (miiclk_rate > 60 * 1000 * 1000)
298 1.9 ryo miiclk = GMAC_MII_CLK_60_100M_DIV42;
299 1.9 ryo else if (miiclk_rate > 35 * 1000 * 1000)
300 1.9 ryo miiclk = GMAC_MII_CLK_35_60M_DIV26;
301 1.9 ryo else
302 1.9 ryo miiclk = GMAC_MII_CLK_25_35M_DIV16;
303 1.9 ryo
304 1.9 ryo dwc_gmac_attach(sc, phy, miiclk);
305 1.1 jmcneill }
306 1.1 jmcneill
307 1.1 jmcneill CFATTACH_DECL_NEW(meson_dwmac, sizeof(struct dwc_gmac_softc),
308 1.1 jmcneill meson_dwmac_match, meson_dwmac_attach, NULL, NULL);
309