rk_gmac.c revision 1.7 1 1.7 christos /* $NetBSD: rk_gmac.c,v 1.7 2018/07/16 23:11:47 christos 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.7 christos __KERNEL_RCSID(0, "$NetBSD: rk_gmac.c,v 1.7 2018/07/16 23:11:47 christos 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.1 jmcneill #define RK3328_GRF_MAC_CON0 0x0900
53 1.1 jmcneill #define RK3328_GRF_MAC_CON0_RXDLY __BITS(13,7)
54 1.1 jmcneill #define RK3328_GRF_MAC_CON0_TXDLY __BITS(6,0)
55 1.1 jmcneill
56 1.1 jmcneill #define RK3328_GRF_MAC_CON1 0x0904
57 1.1 jmcneill #define RK3328_GRF_MAC_CON1_CLKSEL __BITS(12,11)
58 1.1 jmcneill #define RK3328_GRF_MAC_CON1_CLKSEL_125M 0
59 1.1 jmcneill #define RK3328_GRF_MAC_CON1_CLKSEL_2_5M 2
60 1.1 jmcneill #define RK3328_GRF_MAC_CON1_CLKSEL_25M 3
61 1.1 jmcneill #define RK3328_GRF_MAC_CON1_MODE __BIT(9)
62 1.1 jmcneill #define RK3328_GRF_MAC_CON1_SEL __BITS(6,4)
63 1.1 jmcneill #define RK3328_GRF_MAC_CON1_SEL_RGMII 1
64 1.1 jmcneill #define RK3328_GRF_MAC_CON1_RXDLY_EN __BIT(1)
65 1.1 jmcneill #define RK3328_GRF_MAC_CON1_TXDLY_EN __BIT(0)
66 1.1 jmcneill
67 1.1 jmcneill #define RK_GMAC_TXDLY_DEFAULT 0x30
68 1.1 jmcneill #define RK_GMAC_RXDLY_DEFAULT 0x10
69 1.1 jmcneill
70 1.1 jmcneill static const char * compatible[] = {
71 1.1 jmcneill "rockchip,rk3328-gmac",
72 1.1 jmcneill NULL
73 1.1 jmcneill };
74 1.1 jmcneill
75 1.1 jmcneill struct rk_gmac_softc {
76 1.1 jmcneill struct dwc_gmac_softc sc_base;
77 1.5 jmcneill struct syscon *sc_syscon;
78 1.1 jmcneill };
79 1.1 jmcneill
80 1.1 jmcneill static int
81 1.1 jmcneill rk_gmac_reset(const int phandle)
82 1.1 jmcneill {
83 1.1 jmcneill struct fdtbus_gpio_pin *pin_reset;
84 1.1 jmcneill const u_int *reset_delay_us;
85 1.1 jmcneill bool reset_active_low;
86 1.2 jmcneill int len;
87 1.1 jmcneill
88 1.1 jmcneill if (!of_hasprop(phandle, "snps,reset-gpio"))
89 1.1 jmcneill return 0;
90 1.1 jmcneill
91 1.1 jmcneill pin_reset = fdtbus_gpio_acquire(phandle, "snps,reset-gpio", GPIO_PIN_OUTPUT);
92 1.1 jmcneill if (pin_reset == NULL)
93 1.1 jmcneill return ENOENT;
94 1.1 jmcneill
95 1.1 jmcneill reset_delay_us = fdtbus_get_prop(phandle, "snps,reset-delays-us", &len);
96 1.1 jmcneill if (reset_delay_us == NULL || len != 12)
97 1.1 jmcneill return ENXIO;
98 1.1 jmcneill
99 1.1 jmcneill reset_active_low = of_hasprop(phandle, "snps,reset-active-low");
100 1.1 jmcneill
101 1.2 jmcneill fdtbus_gpio_write_raw(pin_reset, reset_active_low ? 1 : 0);
102 1.2 jmcneill delay(be32toh(reset_delay_us[0]));
103 1.2 jmcneill fdtbus_gpio_write_raw(pin_reset, reset_active_low ? 0 : 1);
104 1.2 jmcneill delay(be32toh(reset_delay_us[1]));
105 1.2 jmcneill fdtbus_gpio_write_raw(pin_reset, reset_active_low ? 1 : 0);
106 1.2 jmcneill delay(be32toh(reset_delay_us[2]));
107 1.1 jmcneill
108 1.1 jmcneill return 0;
109 1.1 jmcneill }
110 1.1 jmcneill
111 1.1 jmcneill static int
112 1.1 jmcneill rk_gmac_intr(void *arg)
113 1.1 jmcneill {
114 1.1 jmcneill return dwc_gmac_intr(arg);
115 1.1 jmcneill }
116 1.1 jmcneill
117 1.1 jmcneill static void
118 1.1 jmcneill rk3328_gmac_set_mode_rgmii(struct dwc_gmac_softc *sc, u_int tx_delay, u_int rx_delay)
119 1.1 jmcneill {
120 1.1 jmcneill struct rk_gmac_softc * const rk_sc = (struct rk_gmac_softc *)sc;
121 1.3 jmcneill uint32_t write_mask, write_val;
122 1.1 jmcneill
123 1.5 jmcneill syscon_lock(rk_sc->sc_syscon);
124 1.5 jmcneill
125 1.3 jmcneill write_mask = (RK3328_GRF_MAC_CON1_MODE | RK3328_GRF_MAC_CON1_SEL) << 16;
126 1.3 jmcneill write_val = __SHIFTIN(RK3328_GRF_MAC_CON1_SEL_RGMII, RK3328_GRF_MAC_CON1_SEL);
127 1.5 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3328_GRF_MAC_CON1, write_mask | write_val);
128 1.1 jmcneill
129 1.3 jmcneill #if notyet
130 1.3 jmcneill write_mask = (RK3328_GRF_MAC_CON0_TXDLY | RK3328_GRF_MAC_CON0_RXDLY) << 16;
131 1.3 jmcneill write_val = __SHIFTIN(tx_delay, RK3328_GRF_MAC_CON0_TXDLY) |
132 1.3 jmcneill __SHIFTIN(rx_delay, RK3328_GRF_MAC_CON0_RXDLY);
133 1.5 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3328_GRF_MAC_CON0, write_mask | write_val);
134 1.3 jmcneill
135 1.3 jmcneill write_mask = (RK3328_GRF_MAC_CON1_RXDLY_EN | RK3328_GRF_MAC_CON1_TXDLY_EN) << 16;
136 1.3 jmcneill write_val = RK3328_GRF_MAC_CON1_RXDLY_EN | RK3328_GRF_MAC_CON1_TXDLY_EN;
137 1.5 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3328_GRF_MAC_CON1, write_mask | write_val);
138 1.3 jmcneill #endif
139 1.5 jmcneill
140 1.5 jmcneill syscon_unlock(rk_sc->sc_syscon);
141 1.1 jmcneill }
142 1.1 jmcneill
143 1.1 jmcneill static void
144 1.1 jmcneill rk3328_gmac_set_speed_rgmii(struct dwc_gmac_softc *sc, int speed)
145 1.1 jmcneill {
146 1.1 jmcneill struct rk_gmac_softc * const rk_sc = (struct rk_gmac_softc *)sc;
147 1.7 christos #if 0
148 1.1 jmcneill u_int clksel;
149 1.1 jmcneill
150 1.1 jmcneill switch (speed) {
151 1.1 jmcneill case IFM_10_T:
152 1.1 jmcneill clksel = RK3328_GRF_MAC_CON1_CLKSEL_2_5M;
153 1.1 jmcneill break;
154 1.1 jmcneill case IFM_100_TX:
155 1.1 jmcneill clksel = RK3328_GRF_MAC_CON1_CLKSEL_25M;
156 1.1 jmcneill break;
157 1.1 jmcneill default:
158 1.1 jmcneill clksel = RK3328_GRF_MAC_CON1_CLKSEL_125M;
159 1.1 jmcneill break;
160 1.1 jmcneill }
161 1.7 christos #endif
162 1.1 jmcneill
163 1.5 jmcneill syscon_lock(rk_sc->sc_syscon);
164 1.5 jmcneill syscon_write_4(rk_sc->sc_syscon, RK3328_GRF_MAC_CON1,
165 1.1 jmcneill (RK3328_GRF_MAC_CON1_CLKSEL << 16) |
166 1.1 jmcneill __SHIFTIN(RK3328_GRF_MAC_CON1_CLKSEL_125M, RK3328_GRF_MAC_CON1_CLKSEL));
167 1.5 jmcneill syscon_unlock(rk_sc->sc_syscon);
168 1.1 jmcneill }
169 1.1 jmcneill
170 1.1 jmcneill static int
171 1.1 jmcneill rk_gmac_setup_clocks(int phandle)
172 1.1 jmcneill {
173 1.1 jmcneill static const char * const clknames[] = {
174 1.1 jmcneill #if 0
175 1.1 jmcneill "stmmaceth",
176 1.1 jmcneill "mac_clk_rx",
177 1.1 jmcneill "mac_clk_tx",
178 1.1 jmcneill "clk_mac_ref",
179 1.1 jmcneill "clk_mac_refout",
180 1.1 jmcneill "aclk_mac",
181 1.1 jmcneill "pclk_mac"
182 1.1 jmcneill #else
183 1.1 jmcneill "stmmaceth",
184 1.1 jmcneill "aclk_mac",
185 1.1 jmcneill "pclk_mac",
186 1.1 jmcneill "mac_clk_tx",
187 1.3 jmcneill "mac_clk_rx"
188 1.1 jmcneill #endif
189 1.1 jmcneill };
190 1.1 jmcneill static const char * const rstnames[] = {
191 1.1 jmcneill "stmmaceth"
192 1.1 jmcneill };
193 1.1 jmcneill struct fdtbus_reset *rst;
194 1.1 jmcneill struct clk *clk;
195 1.1 jmcneill int error, n;
196 1.1 jmcneill
197 1.1 jmcneill fdtbus_clock_assign(phandle);
198 1.1 jmcneill
199 1.1 jmcneill for (n = 0; n < __arraycount(clknames); n++) {
200 1.1 jmcneill clk = fdtbus_clock_get(phandle, clknames[n]);
201 1.1 jmcneill if (clk == NULL) {
202 1.1 jmcneill aprint_error(": couldn't get %s clock\n", clknames[n]);
203 1.1 jmcneill return ENXIO;
204 1.1 jmcneill }
205 1.1 jmcneill error = clk_enable(clk);
206 1.1 jmcneill if (error != 0) {
207 1.1 jmcneill aprint_error(": couldn't enable %s clock: %d\n",
208 1.1 jmcneill clknames[n], error);
209 1.1 jmcneill return error;
210 1.1 jmcneill }
211 1.1 jmcneill }
212 1.1 jmcneill
213 1.1 jmcneill for (n = 0; n < __arraycount(rstnames); n++) {
214 1.1 jmcneill rst = fdtbus_reset_get(phandle, rstnames[n]);
215 1.1 jmcneill if (rst == NULL) {
216 1.1 jmcneill aprint_error(": couldn't get %s reset\n", rstnames[n]);
217 1.1 jmcneill return ENXIO;
218 1.1 jmcneill }
219 1.1 jmcneill error = fdtbus_reset_deassert(rst);
220 1.1 jmcneill if (error != 0) {
221 1.1 jmcneill aprint_error(": couldn't de-assert %s reset: %d\n",
222 1.1 jmcneill rstnames[n], error);
223 1.1 jmcneill return error;
224 1.1 jmcneill }
225 1.1 jmcneill }
226 1.1 jmcneill
227 1.1 jmcneill delay(5000);
228 1.1 jmcneill
229 1.1 jmcneill return 0;
230 1.1 jmcneill }
231 1.1 jmcneill
232 1.1 jmcneill static int
233 1.1 jmcneill rk_gmac_match(device_t parent, cfdata_t cf, void *aux)
234 1.1 jmcneill {
235 1.1 jmcneill struct fdt_attach_args * const faa = aux;
236 1.1 jmcneill
237 1.1 jmcneill return of_match_compatible(faa->faa_phandle, compatible);
238 1.1 jmcneill }
239 1.1 jmcneill
240 1.1 jmcneill static void
241 1.1 jmcneill rk_gmac_attach(device_t parent, device_t self, void *aux)
242 1.1 jmcneill {
243 1.1 jmcneill struct rk_gmac_softc * const rk_sc = device_private(self);
244 1.1 jmcneill struct dwc_gmac_softc * const sc = &rk_sc->sc_base;
245 1.1 jmcneill struct fdt_attach_args * const faa = aux;
246 1.1 jmcneill const int phandle = faa->faa_phandle;
247 1.1 jmcneill const char *phy_mode;
248 1.1 jmcneill char intrstr[128];
249 1.5 jmcneill bus_addr_t addr;
250 1.5 jmcneill bus_size_t size;
251 1.1 jmcneill u_int tx_delay, rx_delay;
252 1.1 jmcneill
253 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
254 1.1 jmcneill aprint_error(": couldn't get registers\n");
255 1.1 jmcneill return;
256 1.1 jmcneill }
257 1.1 jmcneill
258 1.5 jmcneill rk_sc->sc_syscon = fdtbus_syscon_acquire(phandle, "rockchip,grf");
259 1.5 jmcneill if (rk_sc->sc_syscon == NULL) {
260 1.5 jmcneill aprint_error(": couldn't get grf syscon\n");
261 1.1 jmcneill return;
262 1.1 jmcneill }
263 1.1 jmcneill
264 1.1 jmcneill if (of_getprop_uint32(phandle, "tx_delay", &tx_delay) != 0)
265 1.1 jmcneill tx_delay = RK_GMAC_TXDLY_DEFAULT;
266 1.1 jmcneill
267 1.1 jmcneill if (of_getprop_uint32(phandle, "rx_delay", &rx_delay) != 0)
268 1.1 jmcneill rx_delay = RK_GMAC_RXDLY_DEFAULT;
269 1.1 jmcneill
270 1.1 jmcneill sc->sc_dev = self;
271 1.1 jmcneill sc->sc_bst = faa->faa_bst;
272 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
273 1.1 jmcneill aprint_error(": couldn't map registers\n");
274 1.1 jmcneill return;
275 1.1 jmcneill }
276 1.1 jmcneill sc->sc_dmat = faa->faa_dmat;
277 1.1 jmcneill
278 1.1 jmcneill if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
279 1.1 jmcneill aprint_error(": failed to decode interrupt\n");
280 1.1 jmcneill return;
281 1.1 jmcneill }
282 1.1 jmcneill
283 1.1 jmcneill if (rk_gmac_setup_clocks(phandle) != 0)
284 1.1 jmcneill return;
285 1.1 jmcneill
286 1.1 jmcneill if (rk_gmac_reset(phandle) != 0)
287 1.1 jmcneill aprint_error_dev(self, "PHY reset failed\n");
288 1.1 jmcneill
289 1.6 jmcneill /* Rock64 seems to need more time for the reset to complete */
290 1.6 jmcneill delay(100000);
291 1.6 jmcneill
292 1.3 jmcneill #if notyet
293 1.1 jmcneill if (of_hasprop(phandle, "snps,force_thresh_dma_mode"))
294 1.1 jmcneill sc->sc_flags |= DWC_GMAC_FORCE_THRESH_DMA_MODE;
295 1.3 jmcneill #endif
296 1.1 jmcneill
297 1.1 jmcneill phy_mode = fdtbus_get_string(phandle, "phy-mode");
298 1.1 jmcneill if (phy_mode == NULL) {
299 1.1 jmcneill aprint_error(": missing 'phy-mode' property\n");
300 1.1 jmcneill return;
301 1.1 jmcneill }
302 1.1 jmcneill
303 1.1 jmcneill if (strcmp(phy_mode, "rgmii") == 0) {
304 1.1 jmcneill rk3328_gmac_set_mode_rgmii(sc, tx_delay, rx_delay);
305 1.1 jmcneill
306 1.1 jmcneill sc->sc_set_speed = rk3328_gmac_set_speed_rgmii;
307 1.1 jmcneill } else {
308 1.1 jmcneill aprint_error(": unsupported phy-mode '%s'\n", phy_mode);
309 1.1 jmcneill return;
310 1.1 jmcneill }
311 1.1 jmcneill
312 1.1 jmcneill aprint_naive("\n");
313 1.1 jmcneill aprint_normal(": GMAC\n");
314 1.1 jmcneill
315 1.4 jmcneill if (dwc_gmac_attach(sc, GMAC_MII_CLK_150_250M_DIV102) != 0)
316 1.4 jmcneill return;
317 1.4 jmcneill
318 1.1 jmcneill if (fdtbus_intr_establish(phandle, 0, IPL_NET, 0, rk_gmac_intr, sc) == NULL) {
319 1.1 jmcneill aprint_error_dev(self, "failed to establish interrupt on %s\n", intrstr);
320 1.1 jmcneill return;
321 1.1 jmcneill }
322 1.1 jmcneill aprint_normal_dev(self, "interrupting on %s\n", intrstr);
323 1.1 jmcneill }
324 1.1 jmcneill
325 1.1 jmcneill CFATTACH_DECL_NEW(rk_gmac, sizeof(struct rk_gmac_softc),
326 1.1 jmcneill rk_gmac_match, rk_gmac_attach, NULL, NULL);
327