rk_gmac.c revision 1.12 1 1.12 jmcneill /* $NetBSD: rk_gmac.c,v 1.12 2019/05/05 19:10:05 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 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.12 jmcneill __KERNEL_RCSID(0, "$NetBSD: rk_gmac.c,v 1.12 2019/05/05 19:10:05 jmcneill 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.1 jmcneill
40 1.1 jmcneill #include <net/if.h>
41 1.1 jmcneill #include <net/if_ether.h>
42 1.1 jmcneill #include <net/if_media.h>
43 1.1 jmcneill
44 1.1 jmcneill #include <dev/mii/miivar.h>
45 1.1 jmcneill
46 1.1 jmcneill #include <dev/ic/dwc_gmac_var.h>
47 1.1 jmcneill #include <dev/ic/dwc_gmac_reg.h>
48 1.1 jmcneill
49 1.1 jmcneill #include <dev/fdt/fdtvar.h>
50 1.5 jmcneill #include <dev/fdt/syscon.h>
51 1.1 jmcneill
52 1.8 jmcneill #define RK_GMAC_TXDLY_DEFAULT 0x30
53 1.8 jmcneill #define RK_GMAC_RXDLY_DEFAULT 0x10
54 1.8 jmcneill
55 1.8 jmcneill enum rk_gmac_type {
56 1.8 jmcneill GMAC_RK3328 = 1,
57 1.8 jmcneill GMAC_RK3399
58 1.8 jmcneill };
59 1.8 jmcneill
60 1.8 jmcneill static const struct of_compat_data compat_data[] = {
61 1.8 jmcneill { "rockchip,rk3328-gmac", GMAC_RK3328 },
62 1.8 jmcneill { "rockchip,rk3399-gmac", GMAC_RK3399 },
63 1.8 jmcneill { NULL }
64 1.8 jmcneill };
65 1.8 jmcneill
66 1.8 jmcneill struct rk_gmac_softc {
67 1.8 jmcneill struct dwc_gmac_softc sc_base;
68 1.8 jmcneill struct syscon *sc_syscon;
69 1.8 jmcneill enum rk_gmac_type sc_type;
70 1.8 jmcneill };
71 1.8 jmcneill
72 1.8 jmcneill /*
73 1.8 jmcneill * RK3328 specific
74 1.8 jmcneill */
75 1.8 jmcneill
76 1.1 jmcneill #define RK3328_GRF_MAC_CON0 0x0900
77 1.1 jmcneill #define RK3328_GRF_MAC_CON0_RXDLY __BITS(13,7)
78 1.1 jmcneill #define RK3328_GRF_MAC_CON0_TXDLY __BITS(6,0)
79 1.1 jmcneill
80 1.1 jmcneill #define RK3328_GRF_MAC_CON1 0x0904
81 1.1 jmcneill #define RK3328_GRF_MAC_CON1_CLKSEL __BITS(12,11)
82 1.1 jmcneill #define RK3328_GRF_MAC_CON1_CLKSEL_125M 0
83 1.1 jmcneill #define RK3328_GRF_MAC_CON1_CLKSEL_2_5M 2
84 1.1 jmcneill #define RK3328_GRF_MAC_CON1_CLKSEL_25M 3
85 1.1 jmcneill #define RK3328_GRF_MAC_CON1_MODE __BIT(9)
86 1.1 jmcneill #define RK3328_GRF_MAC_CON1_SEL __BITS(6,4)
87 1.1 jmcneill #define RK3328_GRF_MAC_CON1_SEL_RGMII 1
88 1.1 jmcneill #define RK3328_GRF_MAC_CON1_RXDLY_EN __BIT(1)
89 1.1 jmcneill #define RK3328_GRF_MAC_CON1_TXDLY_EN __BIT(0)
90 1.1 jmcneill
91 1.1 jmcneill static void
92 1.1 jmcneill rk3328_gmac_set_mode_rgmii(struct dwc_gmac_softc *sc, u_int tx_delay, u_int rx_delay)
93 1.1 jmcneill {
94 1.1 jmcneill struct rk_gmac_softc * const rk_sc = (struct rk_gmac_softc *)sc;
95 1.3 jmcneill uint32_t write_mask, write_val;
96 1.1 jmcneill
97 1.5 jmcneill syscon_lock(rk_sc->sc_syscon);
98 1.5 jmcneill
99 1.3 jmcneill write_mask = (RK3328_GRF_MAC_CON1_MODE | RK3328_GRF_MAC_CON1_SEL) << 16;
100 1.3 jmcneill write_val = __SHIFTIN(RK3328_GRF_MAC_CON1_SEL_RGMII, RK3328_GRF_MAC_CON1_SEL);
101 1.5 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3328_GRF_MAC_CON1, write_mask | write_val);
102 1.1 jmcneill
103 1.3 jmcneill #if notyet
104 1.3 jmcneill write_mask = (RK3328_GRF_MAC_CON0_TXDLY | RK3328_GRF_MAC_CON0_RXDLY) << 16;
105 1.3 jmcneill write_val = __SHIFTIN(tx_delay, RK3328_GRF_MAC_CON0_TXDLY) |
106 1.3 jmcneill __SHIFTIN(rx_delay, RK3328_GRF_MAC_CON0_RXDLY);
107 1.5 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3328_GRF_MAC_CON0, write_mask | write_val);
108 1.3 jmcneill
109 1.3 jmcneill write_mask = (RK3328_GRF_MAC_CON1_RXDLY_EN | RK3328_GRF_MAC_CON1_TXDLY_EN) << 16;
110 1.3 jmcneill write_val = RK3328_GRF_MAC_CON1_RXDLY_EN | RK3328_GRF_MAC_CON1_TXDLY_EN;
111 1.5 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3328_GRF_MAC_CON1, write_mask | write_val);
112 1.3 jmcneill #endif
113 1.5 jmcneill
114 1.5 jmcneill syscon_unlock(rk_sc->sc_syscon);
115 1.1 jmcneill }
116 1.1 jmcneill
117 1.1 jmcneill static void
118 1.1 jmcneill rk3328_gmac_set_speed_rgmii(struct dwc_gmac_softc *sc, int speed)
119 1.1 jmcneill {
120 1.1 jmcneill struct rk_gmac_softc * const rk_sc = (struct rk_gmac_softc *)sc;
121 1.7 christos #if 0
122 1.1 jmcneill u_int clksel;
123 1.1 jmcneill
124 1.1 jmcneill switch (speed) {
125 1.1 jmcneill case IFM_10_T:
126 1.1 jmcneill clksel = RK3328_GRF_MAC_CON1_CLKSEL_2_5M;
127 1.1 jmcneill break;
128 1.1 jmcneill case IFM_100_TX:
129 1.1 jmcneill clksel = RK3328_GRF_MAC_CON1_CLKSEL_25M;
130 1.1 jmcneill break;
131 1.1 jmcneill default:
132 1.1 jmcneill clksel = RK3328_GRF_MAC_CON1_CLKSEL_125M;
133 1.1 jmcneill break;
134 1.1 jmcneill }
135 1.7 christos #endif
136 1.1 jmcneill
137 1.5 jmcneill syscon_lock(rk_sc->sc_syscon);
138 1.5 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3328_GRF_MAC_CON1,
139 1.1 jmcneill (RK3328_GRF_MAC_CON1_CLKSEL << 16) |
140 1.1 jmcneill __SHIFTIN(RK3328_GRF_MAC_CON1_CLKSEL_125M, RK3328_GRF_MAC_CON1_CLKSEL));
141 1.5 jmcneill syscon_unlock(rk_sc->sc_syscon);
142 1.1 jmcneill }
143 1.1 jmcneill
144 1.8 jmcneill /*
145 1.8 jmcneill * RK3399 specific
146 1.8 jmcneill */
147 1.8 jmcneill
148 1.8 jmcneill #define RK3399_GRF_SOC_CON5 0x0c214
149 1.8 jmcneill #define RK3399_GRF_SOC_CON5_GMAC_PHY_INTF_SEL __BITS(11,9)
150 1.8 jmcneill #define RK3399_GRF_SOC_CON5_GMAC_FLOWCTRL __BIT(8)
151 1.8 jmcneill #define RK3399_GRF_SOC_CON5_GMAC_SPEED __BIT(7)
152 1.8 jmcneill #define RK3399_GRF_SOC_CON5_RMII_MODE __BIT(6)
153 1.8 jmcneill #define RK3399_GRF_SOC_CON5_GMAC_CLK_SEL __BITS(5,4)
154 1.8 jmcneill #define RK3399_GRF_SOC_CON5_GMAC_CLK_SEL_125M 0
155 1.10 msaitoh #define RK3399_GRF_SOC_CON5_GMAC_CLK_SEL_25M 3
156 1.8 jmcneill #define RK3399_GRF_SOC_CON5_GMAC_CLK_SEL_2_5M 2
157 1.8 jmcneill #define RK3399_GRF_SOC_CON5_RMII_CLK_SEL __BIT(3)
158 1.8 jmcneill #define RK3399_GRF_SOC_CON6 0x0c218
159 1.8 jmcneill #define RK3399_GRF_SOC_CON6_GMAC_RXCLK_DLY_ENA __BIT(15)
160 1.8 jmcneill #define RK3399_GRF_SOC_CON6_GMAC_CLK_RX_DL_CFG __BITS(14,8)
161 1.8 jmcneill #define RK3399_GRF_SOC_CON6_GMAC_TXCLK_DLY_ENA __BIT(7)
162 1.8 jmcneill #define RK3399_GRF_SOC_CON6_GMAC_CLK_TX_DL_CFG __BITS(6,0)
163 1.8 jmcneill
164 1.8 jmcneill static void
165 1.8 jmcneill rk3399_gmac_set_mode_rgmii(struct dwc_gmac_softc *sc, u_int tx_delay, u_int rx_delay)
166 1.8 jmcneill {
167 1.8 jmcneill struct rk_gmac_softc * const rk_sc = (struct rk_gmac_softc *)sc;
168 1.8 jmcneill
169 1.8 jmcneill const uint32_t con5_mask =
170 1.8 jmcneill (RK3399_GRF_SOC_CON5_RMII_MODE | RK3399_GRF_SOC_CON5_GMAC_PHY_INTF_SEL) << 16;
171 1.8 jmcneill const uint32_t con5 = __SHIFTIN(1, RK3399_GRF_SOC_CON5_GMAC_PHY_INTF_SEL);
172 1.8 jmcneill
173 1.12 jmcneill #if notyet
174 1.8 jmcneill const uint32_t con6_mask =
175 1.11 msaitoh (RK3399_GRF_SOC_CON6_GMAC_RXCLK_DLY_ENA |
176 1.11 msaitoh RK3399_GRF_SOC_CON6_GMAC_TXCLK_DLY_ENA |
177 1.11 msaitoh RK3399_GRF_SOC_CON6_GMAC_CLK_RX_DL_CFG |
178 1.11 msaitoh RK3399_GRF_SOC_CON6_GMAC_CLK_TX_DL_CFG) << 16;
179 1.8 jmcneill const uint32_t con6 =
180 1.11 msaitoh (tx_delay ? RK3399_GRF_SOC_CON6_GMAC_TXCLK_DLY_ENA : 0) |
181 1.11 msaitoh (rx_delay ? RK3399_GRF_SOC_CON6_GMAC_RXCLK_DLY_ENA : 0) |
182 1.8 jmcneill __SHIFTIN(rx_delay, RK3399_GRF_SOC_CON6_GMAC_CLK_RX_DL_CFG) |
183 1.8 jmcneill __SHIFTIN(tx_delay, RK3399_GRF_SOC_CON6_GMAC_CLK_TX_DL_CFG);
184 1.12 jmcneill #endif
185 1.8 jmcneill
186 1.8 jmcneill syscon_lock(rk_sc->sc_syscon);
187 1.8 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3399_GRF_SOC_CON5, con5 | con5_mask);
188 1.12 jmcneill #if notyet
189 1.8 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3399_GRF_SOC_CON6, con6 | con6_mask);
190 1.12 jmcneill #endif
191 1.8 jmcneill syscon_unlock(rk_sc->sc_syscon);
192 1.8 jmcneill }
193 1.8 jmcneill
194 1.8 jmcneill static void
195 1.8 jmcneill rk3399_gmac_set_speed_rgmii(struct dwc_gmac_softc *sc, int speed)
196 1.8 jmcneill {
197 1.8 jmcneill struct rk_gmac_softc * const rk_sc = (struct rk_gmac_softc *)sc;
198 1.8 jmcneill u_int clksel;
199 1.8 jmcneill
200 1.8 jmcneill switch (speed) {
201 1.8 jmcneill case IFM_10_T:
202 1.8 jmcneill clksel = RK3399_GRF_SOC_CON5_GMAC_CLK_SEL_2_5M;
203 1.8 jmcneill break;
204 1.8 jmcneill case IFM_100_TX:
205 1.8 jmcneill clksel = RK3399_GRF_SOC_CON5_GMAC_CLK_SEL_25M;
206 1.8 jmcneill break;
207 1.8 jmcneill default:
208 1.8 jmcneill clksel = RK3399_GRF_SOC_CON5_GMAC_CLK_SEL_125M;
209 1.8 jmcneill break;
210 1.8 jmcneill }
211 1.8 jmcneill
212 1.8 jmcneill const uint32_t con5_mask =
213 1.8 jmcneill RK3399_GRF_SOC_CON5_GMAC_CLK_SEL << 16;
214 1.8 jmcneill const uint32_t con5 =
215 1.8 jmcneill __SHIFTIN(clksel, RK3399_GRF_SOC_CON5_GMAC_CLK_SEL);
216 1.8 jmcneill
217 1.8 jmcneill syscon_lock(rk_sc->sc_syscon);
218 1.8 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3399_GRF_SOC_CON5, con5 | con5_mask);
219 1.8 jmcneill syscon_unlock(rk_sc->sc_syscon);
220 1.8 jmcneill }
221 1.8 jmcneill
222 1.8 jmcneill static int
223 1.8 jmcneill rk_gmac_reset(const int phandle)
224 1.8 jmcneill {
225 1.8 jmcneill struct fdtbus_gpio_pin *pin_reset;
226 1.8 jmcneill const u_int *reset_delay_us;
227 1.8 jmcneill bool reset_active_low;
228 1.8 jmcneill int len;
229 1.8 jmcneill
230 1.8 jmcneill if (!of_hasprop(phandle, "snps,reset-gpio"))
231 1.8 jmcneill return 0;
232 1.8 jmcneill
233 1.8 jmcneill pin_reset = fdtbus_gpio_acquire(phandle, "snps,reset-gpio", GPIO_PIN_OUTPUT);
234 1.8 jmcneill if (pin_reset == NULL)
235 1.8 jmcneill return ENOENT;
236 1.8 jmcneill
237 1.8 jmcneill reset_delay_us = fdtbus_get_prop(phandle, "snps,reset-delays-us", &len);
238 1.8 jmcneill if (reset_delay_us == NULL || len != 12)
239 1.8 jmcneill return ENXIO;
240 1.8 jmcneill
241 1.8 jmcneill reset_active_low = of_hasprop(phandle, "snps,reset-active-low");
242 1.8 jmcneill
243 1.8 jmcneill fdtbus_gpio_write_raw(pin_reset, reset_active_low ? 1 : 0);
244 1.8 jmcneill delay(be32toh(reset_delay_us[0]));
245 1.8 jmcneill fdtbus_gpio_write_raw(pin_reset, reset_active_low ? 0 : 1);
246 1.8 jmcneill delay(be32toh(reset_delay_us[1]));
247 1.8 jmcneill fdtbus_gpio_write_raw(pin_reset, reset_active_low ? 1 : 0);
248 1.8 jmcneill delay(be32toh(reset_delay_us[2]));
249 1.8 jmcneill
250 1.8 jmcneill return 0;
251 1.8 jmcneill }
252 1.8 jmcneill
253 1.8 jmcneill static int
254 1.8 jmcneill rk_gmac_intr(void *arg)
255 1.8 jmcneill {
256 1.8 jmcneill return dwc_gmac_intr(arg);
257 1.8 jmcneill }
258 1.8 jmcneill
259 1.1 jmcneill static int
260 1.1 jmcneill rk_gmac_setup_clocks(int phandle)
261 1.1 jmcneill {
262 1.1 jmcneill static const char * const clknames[] = {
263 1.1 jmcneill #if 0
264 1.1 jmcneill "stmmaceth",
265 1.1 jmcneill "mac_clk_rx",
266 1.1 jmcneill "mac_clk_tx",
267 1.1 jmcneill "clk_mac_ref",
268 1.1 jmcneill "clk_mac_refout",
269 1.1 jmcneill "aclk_mac",
270 1.1 jmcneill "pclk_mac"
271 1.1 jmcneill #else
272 1.1 jmcneill "stmmaceth",
273 1.1 jmcneill "aclk_mac",
274 1.1 jmcneill "pclk_mac",
275 1.1 jmcneill "mac_clk_tx",
276 1.3 jmcneill "mac_clk_rx"
277 1.1 jmcneill #endif
278 1.1 jmcneill };
279 1.1 jmcneill static const char * const rstnames[] = {
280 1.1 jmcneill "stmmaceth"
281 1.1 jmcneill };
282 1.1 jmcneill struct fdtbus_reset *rst;
283 1.1 jmcneill struct clk *clk;
284 1.1 jmcneill int error, n;
285 1.1 jmcneill
286 1.1 jmcneill fdtbus_clock_assign(phandle);
287 1.1 jmcneill
288 1.1 jmcneill for (n = 0; n < __arraycount(clknames); n++) {
289 1.1 jmcneill clk = fdtbus_clock_get(phandle, clknames[n]);
290 1.1 jmcneill if (clk == NULL) {
291 1.1 jmcneill aprint_error(": couldn't get %s clock\n", clknames[n]);
292 1.1 jmcneill return ENXIO;
293 1.1 jmcneill }
294 1.1 jmcneill error = clk_enable(clk);
295 1.1 jmcneill if (error != 0) {
296 1.1 jmcneill aprint_error(": couldn't enable %s clock: %d\n",
297 1.1 jmcneill clknames[n], error);
298 1.1 jmcneill return error;
299 1.1 jmcneill }
300 1.1 jmcneill }
301 1.1 jmcneill
302 1.1 jmcneill for (n = 0; n < __arraycount(rstnames); n++) {
303 1.1 jmcneill rst = fdtbus_reset_get(phandle, rstnames[n]);
304 1.1 jmcneill if (rst == NULL) {
305 1.1 jmcneill aprint_error(": couldn't get %s reset\n", rstnames[n]);
306 1.1 jmcneill return ENXIO;
307 1.1 jmcneill }
308 1.1 jmcneill error = fdtbus_reset_deassert(rst);
309 1.1 jmcneill if (error != 0) {
310 1.1 jmcneill aprint_error(": couldn't de-assert %s reset: %d\n",
311 1.1 jmcneill rstnames[n], error);
312 1.1 jmcneill return error;
313 1.1 jmcneill }
314 1.1 jmcneill }
315 1.1 jmcneill
316 1.1 jmcneill delay(5000);
317 1.1 jmcneill
318 1.1 jmcneill return 0;
319 1.1 jmcneill }
320 1.1 jmcneill
321 1.1 jmcneill static int
322 1.1 jmcneill rk_gmac_match(device_t parent, cfdata_t cf, void *aux)
323 1.1 jmcneill {
324 1.1 jmcneill struct fdt_attach_args * const faa = aux;
325 1.1 jmcneill
326 1.8 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
327 1.1 jmcneill }
328 1.1 jmcneill
329 1.1 jmcneill static void
330 1.1 jmcneill rk_gmac_attach(device_t parent, device_t self, void *aux)
331 1.1 jmcneill {
332 1.1 jmcneill struct rk_gmac_softc * const rk_sc = device_private(self);
333 1.1 jmcneill struct dwc_gmac_softc * const sc = &rk_sc->sc_base;
334 1.1 jmcneill struct fdt_attach_args * const faa = aux;
335 1.1 jmcneill const int phandle = faa->faa_phandle;
336 1.1 jmcneill const char *phy_mode;
337 1.1 jmcneill char intrstr[128];
338 1.5 jmcneill bus_addr_t addr;
339 1.5 jmcneill bus_size_t size;
340 1.1 jmcneill u_int tx_delay, rx_delay;
341 1.1 jmcneill
342 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
343 1.1 jmcneill aprint_error(": couldn't get registers\n");
344 1.1 jmcneill return;
345 1.1 jmcneill }
346 1.1 jmcneill
347 1.8 jmcneill rk_sc->sc_type = of_search_compatible(phandle, compat_data)->data;
348 1.8 jmcneill
349 1.5 jmcneill rk_sc->sc_syscon = fdtbus_syscon_acquire(phandle, "rockchip,grf");
350 1.5 jmcneill if (rk_sc->sc_syscon == NULL) {
351 1.5 jmcneill aprint_error(": couldn't get grf syscon\n");
352 1.1 jmcneill return;
353 1.1 jmcneill }
354 1.1 jmcneill
355 1.1 jmcneill if (of_getprop_uint32(phandle, "tx_delay", &tx_delay) != 0)
356 1.1 jmcneill tx_delay = RK_GMAC_TXDLY_DEFAULT;
357 1.1 jmcneill
358 1.1 jmcneill if (of_getprop_uint32(phandle, "rx_delay", &rx_delay) != 0)
359 1.1 jmcneill rx_delay = RK_GMAC_RXDLY_DEFAULT;
360 1.1 jmcneill
361 1.1 jmcneill sc->sc_dev = self;
362 1.1 jmcneill sc->sc_bst = faa->faa_bst;
363 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
364 1.1 jmcneill aprint_error(": couldn't map registers\n");
365 1.1 jmcneill return;
366 1.1 jmcneill }
367 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
368 1.1 jmcneill
369 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
370 1.1 jmcneill aprint_error(": failed to decode interrupt\n");
371 1.1 jmcneill return;
372 1.1 jmcneill }
373 1.1 jmcneill
374 1.1 jmcneill if (rk_gmac_setup_clocks(phandle) != 0)
375 1.1 jmcneill return;
376 1.1 jmcneill
377 1.1 jmcneill if (rk_gmac_reset(phandle) != 0)
378 1.1 jmcneill aprint_error_dev(self, "PHY reset failed\n");
379 1.1 jmcneill
380 1.6 jmcneill /* Rock64 seems to need more time for the reset to complete */
381 1.6 jmcneill delay(100000);
382 1.6 jmcneill
383 1.3 jmcneill #if notyet
384 1.1 jmcneill if (of_hasprop(phandle, "snps,force_thresh_dma_mode"))
385 1.1 jmcneill sc->sc_flags |= DWC_GMAC_FORCE_THRESH_DMA_MODE;
386 1.3 jmcneill #endif
387 1.1 jmcneill
388 1.1 jmcneill phy_mode = fdtbus_get_string(phandle, "phy-mode");
389 1.1 jmcneill if (phy_mode == NULL) {
390 1.1 jmcneill aprint_error(": missing 'phy-mode' property\n");
391 1.1 jmcneill return;
392 1.1 jmcneill }
393 1.1 jmcneill
394 1.8 jmcneill switch (rk_sc->sc_type) {
395 1.8 jmcneill case GMAC_RK3328:
396 1.8 jmcneill if (strcmp(phy_mode, "rgmii") == 0) {
397 1.8 jmcneill rk3328_gmac_set_mode_rgmii(sc, tx_delay, rx_delay);
398 1.8 jmcneill
399 1.8 jmcneill sc->sc_set_speed = rk3328_gmac_set_speed_rgmii;
400 1.8 jmcneill } else {
401 1.8 jmcneill aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
402 1.8 jmcneill return;
403 1.8 jmcneill }
404 1.8 jmcneill break;
405 1.8 jmcneill case GMAC_RK3399:
406 1.8 jmcneill if (strcmp(phy_mode, "rgmii") == 0) {
407 1.8 jmcneill rk3399_gmac_set_mode_rgmii(sc, tx_delay, rx_delay);
408 1.8 jmcneill
409 1.8 jmcneill sc->sc_set_speed = rk3399_gmac_set_speed_rgmii;
410 1.8 jmcneill } else {
411 1.8 jmcneill aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
412 1.8 jmcneill return;
413 1.8 jmcneill }
414 1.8 jmcneill break;
415 1.1 jmcneill }
416 1.1 jmcneill
417 1.1 jmcneill aprint_naive("\n");
418 1.1 jmcneill aprint_normal(": GMAC\n");
419 1.1 jmcneill
420 1.9 martin if (dwc_gmac_attach(sc, MII_PHY_ANY, GMAC_MII_CLK_150_250M_DIV102) != 0)
421 1.4 jmcneill return;
422 1.4 jmcneill
423 1.1 jmcneill if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0, rk_gmac_intr, sc) == NULL) {
424 1.1 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n", intrstr);
425 1.1 jmcneill return;
426 1.1 jmcneill }
427 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
428 1.1 jmcneill }
429 1.1 jmcneill
430 1.1 jmcneill CFATTACH_DECL_NEW(rk_gmac, sizeof(struct rk_gmac_softc),
431 1.1 jmcneill rk_gmac_match, rk_gmac_attach, NULL, NULL);
432