blob: 04008d2b198e0657e4f9a5cff74da054e67c7764 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sjoerd Simons0125bcf2017-01-11 11:46:11 +01002/*
3 * (C) Copyright 2015 Sjoerd Simons <sjoerd.simons@collabora.co.uk>
4 *
Sjoerd Simons0125bcf2017-01-11 11:46:11 +01005 * Rockchip GMAC ethernet IP driver for U-Boot
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <clk.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060012#include <net.h>
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010013#include <phy.h>
14#include <syscon.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010016#include <asm/io.h>
Kever Yang15f09a12019-03-28 11:01:23 +080017#include <asm/arch-rockchip/periph.h>
18#include <asm/arch-rockchip/clock.h>
19#include <asm/arch-rockchip/hardware.h>
Heiko Stuebnerb0791182019-07-24 01:20:29 +020020#include <asm/arch-rockchip/grf_px30.h>
Kever Yang15f09a12019-03-28 11:01:23 +080021#include <asm/arch-rockchip/grf_rk322x.h>
22#include <asm/arch-rockchip/grf_rk3288.h>
David Wu0ed8f1e2019-11-26 09:39:49 +080023#include <asm/arch-rk3308/grf_rk3308.h>
Kever Yang15f09a12019-03-28 11:01:23 +080024#include <asm/arch-rockchip/grf_rk3328.h>
25#include <asm/arch-rockchip/grf_rk3368.h>
26#include <asm/arch-rockchip/grf_rk3399.h>
27#include <asm/arch-rockchip/grf_rv1108.h>
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010028#include <dm/pinctrl.h>
29#include <dt-bindings/clock/rk3288-cru.h>
Simon Glasscd93d622020-05-10 11:40:13 -060030#include <linux/bitops.h>
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010031#include "designware.h"
32
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +020033DECLARE_GLOBAL_DATA_PTR;
34#define DELAY_ENABLE(soc, tx, rx) \
35 (((tx) ? soc##_TXCLK_DLY_ENA_GMAC_ENABLE : soc##_TXCLK_DLY_ENA_GMAC_DISABLE) | \
36 ((rx) ? soc##_RXCLK_DLY_ENA_GMAC_ENABLE : soc##_RXCLK_DLY_ENA_GMAC_DISABLE))
37
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010038/*
39 * Platform data for the gmac
40 *
41 * dw_eth_pdata: Required platform data for designware driver (must be first)
42 */
Simon Glass8a8d24b2020-12-03 16:55:23 -070043struct gmac_rockchip_plat {
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010044 struct dw_eth_pdata dw_eth_pdata;
David Wucaf74612018-01-13 14:01:12 +080045 bool clock_input;
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010046 int tx_delay;
47 int rx_delay;
48};
49
Philipp Tomsich1f08aa12017-03-24 19:24:26 +010050struct rk_gmac_ops {
51 int (*fix_mac_speed)(struct dw_eth_dev *priv);
Simon Glass8a8d24b2020-12-03 16:55:23 -070052 void (*set_to_rmii)(struct gmac_rockchip_plat *pdata);
53 void (*set_to_rgmii)(struct gmac_rockchip_plat *pdata);
Philipp Tomsich1f08aa12017-03-24 19:24:26 +010054};
55
56
Simon Glassd1998a92020-12-03 16:55:21 -070057static int gmac_rockchip_of_to_plat(struct udevice *dev)
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010058{
Simon Glass8a8d24b2020-12-03 16:55:23 -070059 struct gmac_rockchip_plat *pdata = dev_get_plat(dev);
David Wucaf74612018-01-13 14:01:12 +080060 const char *string;
61
62 string = dev_read_string(dev, "clock_in_out");
63 if (!strcmp(string, "input"))
64 pdata->clock_input = true;
65 else
66 pdata->clock_input = false;
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010067
Philipp Tomsich1f08aa12017-03-24 19:24:26 +010068 /* Check the new naming-style first... */
Philipp Tomsich7ad326a2017-06-07 18:46:01 +020069 pdata->tx_delay = dev_read_u32_default(dev, "tx_delay", -ENOENT);
70 pdata->rx_delay = dev_read_u32_default(dev, "rx_delay", -ENOENT);
Philipp Tomsich1f08aa12017-03-24 19:24:26 +010071
72 /* ... and fall back to the old naming style or default, if necessary */
73 if (pdata->tx_delay == -ENOENT)
Philipp Tomsich7ad326a2017-06-07 18:46:01 +020074 pdata->tx_delay = dev_read_u32_default(dev, "tx-delay", 0x30);
Philipp Tomsich1f08aa12017-03-24 19:24:26 +010075 if (pdata->rx_delay == -ENOENT)
Philipp Tomsich7ad326a2017-06-07 18:46:01 +020076 pdata->rx_delay = dev_read_u32_default(dev, "rx-delay", 0x10);
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010077
Simon Glassd1998a92020-12-03 16:55:21 -070078 return designware_eth_of_to_plat(dev);
Sjoerd Simons0125bcf2017-01-11 11:46:11 +010079}
80
Heiko Stuebnerb0791182019-07-24 01:20:29 +020081static int px30_gmac_fix_mac_speed(struct dw_eth_dev *priv)
82{
83 struct px30_grf *grf;
84 struct clk clk_speed;
85 int speed, ret;
86 enum {
87 PX30_GMAC_SPEED_SHIFT = 0x2,
88 PX30_GMAC_SPEED_MASK = BIT(2),
89 PX30_GMAC_SPEED_10M = 0,
90 PX30_GMAC_SPEED_100M = BIT(2),
91 };
92
93 ret = clk_get_by_name(priv->phydev->dev, "clk_mac_speed",
94 &clk_speed);
95 if (ret)
96 return ret;
97
98 switch (priv->phydev->speed) {
99 case 10:
100 speed = PX30_GMAC_SPEED_10M;
101 ret = clk_set_rate(&clk_speed, 2500000);
102 if (ret)
103 return ret;
104 break;
105 case 100:
106 speed = PX30_GMAC_SPEED_100M;
107 ret = clk_set_rate(&clk_speed, 25000000);
108 if (ret)
109 return ret;
110 break;
111 default:
112 debug("Unknown phy speed: %d\n", priv->phydev->speed);
113 return -EINVAL;
114 }
115
116 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
117 rk_clrsetreg(&grf->mac_con1, PX30_GMAC_SPEED_MASK, speed);
118
119 return 0;
120}
121
David Wud12d7c02018-01-13 14:05:30 +0800122static int rk3228_gmac_fix_mac_speed(struct dw_eth_dev *priv)
123{
124 struct rk322x_grf *grf;
125 int clk;
126 enum {
127 RK3228_GMAC_CLK_SEL_SHIFT = 8,
128 RK3228_GMAC_CLK_SEL_MASK = GENMASK(9, 8),
129 RK3228_GMAC_CLK_SEL_125M = 0 << 8,
130 RK3228_GMAC_CLK_SEL_25M = 3 << 8,
131 RK3228_GMAC_CLK_SEL_2_5M = 2 << 8,
132 };
133
134 switch (priv->phydev->speed) {
135 case 10:
136 clk = RK3228_GMAC_CLK_SEL_2_5M;
137 break;
138 case 100:
139 clk = RK3228_GMAC_CLK_SEL_25M;
140 break;
141 case 1000:
142 clk = RK3228_GMAC_CLK_SEL_125M;
143 break;
144 default:
145 debug("Unknown phy speed: %d\n", priv->phydev->speed);
146 return -EINVAL;
147 }
148
149 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
150 rk_clrsetreg(&grf->mac_con[1], RK3228_GMAC_CLK_SEL_MASK, clk);
151
152 return 0;
153}
154
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100155static int rk3288_gmac_fix_mac_speed(struct dw_eth_dev *priv)
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100156{
157 struct rk3288_grf *grf;
158 int clk;
159
160 switch (priv->phydev->speed) {
161 case 10:
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100162 clk = RK3288_GMAC_CLK_SEL_2_5M;
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100163 break;
164 case 100:
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100165 clk = RK3288_GMAC_CLK_SEL_25M;
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100166 break;
167 case 1000:
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100168 clk = RK3288_GMAC_CLK_SEL_125M;
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100169 break;
170 default:
171 debug("Unknown phy speed: %d\n", priv->phydev->speed);
172 return -EINVAL;
173 }
174
175 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100176 rk_clrsetreg(&grf->soc_con1, RK3288_GMAC_CLK_SEL_MASK, clk);
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100177
178 return 0;
179}
180
David Wu0ed8f1e2019-11-26 09:39:49 +0800181static int rk3308_gmac_fix_mac_speed(struct dw_eth_dev *priv)
182{
183 struct rk3308_grf *grf;
184 struct clk clk_speed;
185 int speed, ret;
186 enum {
187 RK3308_GMAC_SPEED_SHIFT = 0x0,
188 RK3308_GMAC_SPEED_MASK = BIT(0),
189 RK3308_GMAC_SPEED_10M = 0,
190 RK3308_GMAC_SPEED_100M = BIT(0),
191 };
192
193 ret = clk_get_by_name(priv->phydev->dev, "clk_mac_speed",
194 &clk_speed);
195 if (ret)
196 return ret;
197
198 switch (priv->phydev->speed) {
199 case 10:
200 speed = RK3308_GMAC_SPEED_10M;
201 ret = clk_set_rate(&clk_speed, 2500000);
202 if (ret)
203 return ret;
204 break;
205 case 100:
206 speed = RK3308_GMAC_SPEED_100M;
207 ret = clk_set_rate(&clk_speed, 25000000);
208 if (ret)
209 return ret;
210 break;
211 default:
212 debug("Unknown phy speed: %d\n", priv->phydev->speed);
213 return -EINVAL;
214 }
215
216 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
217 rk_clrsetreg(&grf->mac_con0, RK3308_GMAC_SPEED_MASK, speed);
218
219 return 0;
220}
221
David Wub3d2a6d2018-01-13 14:03:04 +0800222static int rk3328_gmac_fix_mac_speed(struct dw_eth_dev *priv)
223{
224 struct rk3328_grf_regs *grf;
225 int clk;
226 enum {
227 RK3328_GMAC_CLK_SEL_SHIFT = 11,
228 RK3328_GMAC_CLK_SEL_MASK = GENMASK(12, 11),
229 RK3328_GMAC_CLK_SEL_125M = 0 << 11,
230 RK3328_GMAC_CLK_SEL_25M = 3 << 11,
231 RK3328_GMAC_CLK_SEL_2_5M = 2 << 11,
232 };
233
234 switch (priv->phydev->speed) {
235 case 10:
236 clk = RK3328_GMAC_CLK_SEL_2_5M;
237 break;
238 case 100:
239 clk = RK3328_GMAC_CLK_SEL_25M;
240 break;
241 case 1000:
242 clk = RK3328_GMAC_CLK_SEL_125M;
243 break;
244 default:
245 debug("Unknown phy speed: %d\n", priv->phydev->speed);
246 return -EINVAL;
247 }
248
249 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
250 rk_clrsetreg(&grf->mac_con[1], RK3328_GMAC_CLK_SEL_MASK, clk);
251
252 return 0;
253}
254
Philipp Tomsich793f2fd2017-07-25 17:02:51 +0200255static int rk3368_gmac_fix_mac_speed(struct dw_eth_dev *priv)
256{
257 struct rk3368_grf *grf;
258 int clk;
259 enum {
260 RK3368_GMAC_CLK_SEL_2_5M = 2 << 4,
261 RK3368_GMAC_CLK_SEL_25M = 3 << 4,
262 RK3368_GMAC_CLK_SEL_125M = 0 << 4,
263 RK3368_GMAC_CLK_SEL_MASK = GENMASK(5, 4),
264 };
265
266 switch (priv->phydev->speed) {
267 case 10:
268 clk = RK3368_GMAC_CLK_SEL_2_5M;
269 break;
270 case 100:
271 clk = RK3368_GMAC_CLK_SEL_25M;
272 break;
273 case 1000:
274 clk = RK3368_GMAC_CLK_SEL_125M;
275 break;
276 default:
277 debug("Unknown phy speed: %d\n", priv->phydev->speed);
278 return -EINVAL;
279 }
280
281 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
282 rk_clrsetreg(&grf->soc_con15, RK3368_GMAC_CLK_SEL_MASK, clk);
283
284 return 0;
285}
286
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100287static int rk3399_gmac_fix_mac_speed(struct dw_eth_dev *priv)
288{
289 struct rk3399_grf_regs *grf;
290 int clk;
291
292 switch (priv->phydev->speed) {
293 case 10:
294 clk = RK3399_GMAC_CLK_SEL_2_5M;
295 break;
296 case 100:
297 clk = RK3399_GMAC_CLK_SEL_25M;
298 break;
299 case 1000:
300 clk = RK3399_GMAC_CLK_SEL_125M;
301 break;
302 default:
303 debug("Unknown phy speed: %d\n", priv->phydev->speed);
304 return -EINVAL;
305 }
306
307 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
308 rk_clrsetreg(&grf->soc_con5, RK3399_GMAC_CLK_SEL_MASK, clk);
309
310 return 0;
311}
312
David Wucaf74612018-01-13 14:01:12 +0800313static int rv1108_set_rmii_speed(struct dw_eth_dev *priv)
314{
315 struct rv1108_grf *grf;
316 int clk, speed;
317 enum {
318 RV1108_GMAC_SPEED_MASK = BIT(2),
319 RV1108_GMAC_SPEED_10M = 0 << 2,
320 RV1108_GMAC_SPEED_100M = 1 << 2,
321 RV1108_GMAC_CLK_SEL_MASK = BIT(7),
322 RV1108_GMAC_CLK_SEL_2_5M = 0 << 7,
323 RV1108_GMAC_CLK_SEL_25M = 1 << 7,
324 };
325
326 switch (priv->phydev->speed) {
327 case 10:
328 clk = RV1108_GMAC_CLK_SEL_2_5M;
329 speed = RV1108_GMAC_SPEED_10M;
330 break;
331 case 100:
332 clk = RV1108_GMAC_CLK_SEL_25M;
333 speed = RV1108_GMAC_SPEED_100M;
334 break;
335 default:
336 debug("Unknown phy speed: %d\n", priv->phydev->speed);
337 return -EINVAL;
338 }
339
340 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
341 rk_clrsetreg(&grf->gmac_con0,
342 RV1108_GMAC_CLK_SEL_MASK | RV1108_GMAC_SPEED_MASK,
343 clk | speed);
344
345 return 0;
346}
347
Simon Glass8a8d24b2020-12-03 16:55:23 -0700348static void px30_gmac_set_to_rmii(struct gmac_rockchip_plat *pdata)
Heiko Stuebnerb0791182019-07-24 01:20:29 +0200349{
350 struct px30_grf *grf;
351 enum {
352 PX30_GMAC_PHY_INTF_SEL_SHIFT = 4,
353 PX30_GMAC_PHY_INTF_SEL_MASK = GENMASK(4, 6),
354 PX30_GMAC_PHY_INTF_SEL_RMII = BIT(6),
355 };
356
357 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
358
359 rk_clrsetreg(&grf->mac_con1,
360 PX30_GMAC_PHY_INTF_SEL_MASK,
361 PX30_GMAC_PHY_INTF_SEL_RMII);
362}
363
Simon Glass8a8d24b2020-12-03 16:55:23 -0700364static void rk3228_gmac_set_to_rgmii(struct gmac_rockchip_plat *pdata)
David Wud12d7c02018-01-13 14:05:30 +0800365{
366 struct rk322x_grf *grf;
367 enum {
368 RK3228_RMII_MODE_SHIFT = 10,
369 RK3228_RMII_MODE_MASK = BIT(10),
370
371 RK3228_GMAC_PHY_INTF_SEL_SHIFT = 4,
372 RK3228_GMAC_PHY_INTF_SEL_MASK = GENMASK(6, 4),
373 RK3228_GMAC_PHY_INTF_SEL_RGMII = BIT(4),
374
375 RK3228_RXCLK_DLY_ENA_GMAC_MASK = BIT(1),
376 RK3228_RXCLK_DLY_ENA_GMAC_DISABLE = 0,
377 RK3228_RXCLK_DLY_ENA_GMAC_ENABLE = BIT(1),
378
379 RK3228_TXCLK_DLY_ENA_GMAC_MASK = BIT(0),
380 RK3228_TXCLK_DLY_ENA_GMAC_DISABLE = 0,
381 RK3228_TXCLK_DLY_ENA_GMAC_ENABLE = BIT(0),
382 };
383 enum {
384 RK3228_CLK_RX_DL_CFG_GMAC_SHIFT = 0x7,
385 RK3228_CLK_RX_DL_CFG_GMAC_MASK = GENMASK(13, 7),
386
387 RK3228_CLK_TX_DL_CFG_GMAC_SHIFT = 0x0,
388 RK3228_CLK_TX_DL_CFG_GMAC_MASK = GENMASK(6, 0),
389 };
390
391 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
392 rk_clrsetreg(&grf->mac_con[1],
393 RK3228_RMII_MODE_MASK |
394 RK3228_GMAC_PHY_INTF_SEL_MASK |
395 RK3228_RXCLK_DLY_ENA_GMAC_MASK |
396 RK3228_TXCLK_DLY_ENA_GMAC_MASK,
397 RK3228_GMAC_PHY_INTF_SEL_RGMII |
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200398 DELAY_ENABLE(RK3228, pdata->tx_delay, pdata->rx_delay));
David Wud12d7c02018-01-13 14:05:30 +0800399
400 rk_clrsetreg(&grf->mac_con[0],
401 RK3228_CLK_RX_DL_CFG_GMAC_MASK |
402 RK3228_CLK_TX_DL_CFG_GMAC_MASK,
403 pdata->rx_delay << RK3228_CLK_RX_DL_CFG_GMAC_SHIFT |
404 pdata->tx_delay << RK3228_CLK_TX_DL_CFG_GMAC_SHIFT);
405}
406
Simon Glass8a8d24b2020-12-03 16:55:23 -0700407static void rk3288_gmac_set_to_rgmii(struct gmac_rockchip_plat *pdata)
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100408{
409 struct rk3288_grf *grf;
410
411 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
412 rk_clrsetreg(&grf->soc_con1,
413 RK3288_RMII_MODE_MASK | RK3288_GMAC_PHY_INTF_SEL_MASK,
414 RK3288_GMAC_PHY_INTF_SEL_RGMII);
415
416 rk_clrsetreg(&grf->soc_con3,
417 RK3288_RXCLK_DLY_ENA_GMAC_MASK |
418 RK3288_TXCLK_DLY_ENA_GMAC_MASK |
419 RK3288_CLK_RX_DL_CFG_GMAC_MASK |
420 RK3288_CLK_TX_DL_CFG_GMAC_MASK,
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200421 DELAY_ENABLE(RK3288, pdata->rx_delay, pdata->tx_delay) |
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100422 pdata->rx_delay << RK3288_CLK_RX_DL_CFG_GMAC_SHIFT |
423 pdata->tx_delay << RK3288_CLK_TX_DL_CFG_GMAC_SHIFT);
424}
425
Simon Glass8a8d24b2020-12-03 16:55:23 -0700426static void rk3308_gmac_set_to_rmii(struct gmac_rockchip_plat *pdata)
David Wu0ed8f1e2019-11-26 09:39:49 +0800427{
428 struct rk3308_grf *grf;
429 enum {
430 RK3308_GMAC_PHY_INTF_SEL_SHIFT = 2,
431 RK3308_GMAC_PHY_INTF_SEL_MASK = GENMASK(4, 2),
432 RK3308_GMAC_PHY_INTF_SEL_RMII = BIT(4),
433 };
434
435 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
436
437 rk_clrsetreg(&grf->mac_con0,
438 RK3308_GMAC_PHY_INTF_SEL_MASK,
439 RK3308_GMAC_PHY_INTF_SEL_RMII);
440}
441
Simon Glass8a8d24b2020-12-03 16:55:23 -0700442static void rk3328_gmac_set_to_rgmii(struct gmac_rockchip_plat *pdata)
David Wub3d2a6d2018-01-13 14:03:04 +0800443{
444 struct rk3328_grf_regs *grf;
445 enum {
446 RK3328_RMII_MODE_SHIFT = 9,
447 RK3328_RMII_MODE_MASK = BIT(9),
448
449 RK3328_GMAC_PHY_INTF_SEL_SHIFT = 4,
450 RK3328_GMAC_PHY_INTF_SEL_MASK = GENMASK(6, 4),
451 RK3328_GMAC_PHY_INTF_SEL_RGMII = BIT(4),
452
453 RK3328_RXCLK_DLY_ENA_GMAC_MASK = BIT(1),
454 RK3328_RXCLK_DLY_ENA_GMAC_DISABLE = 0,
455 RK3328_RXCLK_DLY_ENA_GMAC_ENABLE = BIT(1),
456
457 RK3328_TXCLK_DLY_ENA_GMAC_MASK = BIT(0),
458 RK3328_TXCLK_DLY_ENA_GMAC_DISABLE = 0,
459 RK3328_TXCLK_DLY_ENA_GMAC_ENABLE = BIT(0),
460 };
461 enum {
462 RK3328_CLK_RX_DL_CFG_GMAC_SHIFT = 0x7,
463 RK3328_CLK_RX_DL_CFG_GMAC_MASK = GENMASK(13, 7),
464
465 RK3328_CLK_TX_DL_CFG_GMAC_SHIFT = 0x0,
466 RK3328_CLK_TX_DL_CFG_GMAC_MASK = GENMASK(6, 0),
467 };
468
469 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
470 rk_clrsetreg(&grf->mac_con[1],
471 RK3328_RMII_MODE_MASK |
472 RK3328_GMAC_PHY_INTF_SEL_MASK |
473 RK3328_RXCLK_DLY_ENA_GMAC_MASK |
474 RK3328_TXCLK_DLY_ENA_GMAC_MASK,
475 RK3328_GMAC_PHY_INTF_SEL_RGMII |
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200476 DELAY_ENABLE(RK3328, pdata->tx_delay, pdata->rx_delay));
David Wub3d2a6d2018-01-13 14:03:04 +0800477
478 rk_clrsetreg(&grf->mac_con[0],
479 RK3328_CLK_RX_DL_CFG_GMAC_MASK |
480 RK3328_CLK_TX_DL_CFG_GMAC_MASK,
481 pdata->rx_delay << RK3328_CLK_RX_DL_CFG_GMAC_SHIFT |
482 pdata->tx_delay << RK3328_CLK_TX_DL_CFG_GMAC_SHIFT);
483}
484
Simon Glass8a8d24b2020-12-03 16:55:23 -0700485static void rk3368_gmac_set_to_rgmii(struct gmac_rockchip_plat *pdata)
Philipp Tomsich793f2fd2017-07-25 17:02:51 +0200486{
487 struct rk3368_grf *grf;
488 enum {
489 RK3368_GMAC_PHY_INTF_SEL_RGMII = 1 << 9,
490 RK3368_GMAC_PHY_INTF_SEL_MASK = GENMASK(11, 9),
491 RK3368_RMII_MODE_MASK = BIT(6),
492 RK3368_RMII_MODE = BIT(6),
493 };
494 enum {
495 RK3368_RXCLK_DLY_ENA_GMAC_MASK = BIT(15),
496 RK3368_RXCLK_DLY_ENA_GMAC_DISABLE = 0,
497 RK3368_RXCLK_DLY_ENA_GMAC_ENABLE = BIT(15),
498 RK3368_TXCLK_DLY_ENA_GMAC_MASK = BIT(7),
499 RK3368_TXCLK_DLY_ENA_GMAC_DISABLE = 0,
500 RK3368_TXCLK_DLY_ENA_GMAC_ENABLE = BIT(7),
501 RK3368_CLK_RX_DL_CFG_GMAC_SHIFT = 8,
502 RK3368_CLK_RX_DL_CFG_GMAC_MASK = GENMASK(14, 8),
503 RK3368_CLK_TX_DL_CFG_GMAC_SHIFT = 0,
504 RK3368_CLK_TX_DL_CFG_GMAC_MASK = GENMASK(6, 0),
505 };
506
507 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
508 rk_clrsetreg(&grf->soc_con15,
509 RK3368_RMII_MODE_MASK | RK3368_GMAC_PHY_INTF_SEL_MASK,
510 RK3368_GMAC_PHY_INTF_SEL_RGMII);
511
512 rk_clrsetreg(&grf->soc_con16,
513 RK3368_RXCLK_DLY_ENA_GMAC_MASK |
514 RK3368_TXCLK_DLY_ENA_GMAC_MASK |
515 RK3368_CLK_RX_DL_CFG_GMAC_MASK |
516 RK3368_CLK_TX_DL_CFG_GMAC_MASK,
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200517 DELAY_ENABLE(RK3368, pdata->tx_delay, pdata->rx_delay) |
Philipp Tomsich793f2fd2017-07-25 17:02:51 +0200518 pdata->rx_delay << RK3368_CLK_RX_DL_CFG_GMAC_SHIFT |
519 pdata->tx_delay << RK3368_CLK_TX_DL_CFG_GMAC_SHIFT);
520}
521
Simon Glass8a8d24b2020-12-03 16:55:23 -0700522static void rk3399_gmac_set_to_rgmii(struct gmac_rockchip_plat *pdata)
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100523{
524 struct rk3399_grf_regs *grf;
525
526 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
527
528 rk_clrsetreg(&grf->soc_con5,
529 RK3399_GMAC_PHY_INTF_SEL_MASK,
530 RK3399_GMAC_PHY_INTF_SEL_RGMII);
531
532 rk_clrsetreg(&grf->soc_con6,
533 RK3399_RXCLK_DLY_ENA_GMAC_MASK |
534 RK3399_TXCLK_DLY_ENA_GMAC_MASK |
535 RK3399_CLK_RX_DL_CFG_GMAC_MASK |
536 RK3399_CLK_TX_DL_CFG_GMAC_MASK,
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200537 DELAY_ENABLE(RK3399, pdata->tx_delay, pdata->rx_delay) |
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100538 pdata->rx_delay << RK3399_CLK_RX_DL_CFG_GMAC_SHIFT |
539 pdata->tx_delay << RK3399_CLK_TX_DL_CFG_GMAC_SHIFT);
540}
541
Simon Glass8a8d24b2020-12-03 16:55:23 -0700542static void rv1108_gmac_set_to_rmii(struct gmac_rockchip_plat *pdata)
David Wucaf74612018-01-13 14:01:12 +0800543{
544 struct rv1108_grf *grf;
545
546 enum {
547 RV1108_GMAC_PHY_INTF_SEL_MASK = GENMASK(6, 4),
548 RV1108_GMAC_PHY_INTF_SEL_RMII = 4 << 4,
549 };
550
551 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
552 rk_clrsetreg(&grf->gmac_con0,
553 RV1108_GMAC_PHY_INTF_SEL_MASK,
554 RV1108_GMAC_PHY_INTF_SEL_RMII);
555}
556
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100557static int gmac_rockchip_probe(struct udevice *dev)
558{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700559 struct gmac_rockchip_plat *pdata = dev_get_plat(dev);
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100560 struct rk_gmac_ops *ops =
561 (struct rk_gmac_ops *)dev_get_driver_data(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700562 struct dw_eth_pdata *dw_pdata = dev_get_plat(dev);
David Wucaf74612018-01-13 14:01:12 +0800563 struct eth_pdata *eth_pdata = &dw_pdata->eth_pdata;
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100564 struct clk clk;
David Wucaf74612018-01-13 14:01:12 +0800565 ulong rate;
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100566 int ret;
567
Sean Anderson6e33eba2021-06-11 00:16:07 -0400568 ret = clk_set_defaults(dev, CLK_DEFAULTS_PRE);
Heiko Stuebnerb0791182019-07-24 01:20:29 +0200569 if (ret)
570 debug("%s clk_set_defaults failed %d\n", __func__, ret);
571
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100572 ret = clk_get_by_index(dev, 0, &clk);
573 if (ret)
574 return ret;
575
David Wucaf74612018-01-13 14:01:12 +0800576 switch (eth_pdata->phy_interface) {
577 case PHY_INTERFACE_MODE_RGMII:
David Wucaf74612018-01-13 14:01:12 +0800578 /* Set to RGMII mode */
579 if (ops->set_to_rgmii)
580 ops->set_to_rgmii(pdata);
581 else
582 return -EPERM;
583
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200584 /*
585 * If the gmac clock is from internal pll, need to set and
586 * check the return value for gmac clock at RGMII mode. If
587 * the gmac clock is from external source, the clock rate
588 * is not set, because of it is bypassed.
589 */
590
David Wucaf74612018-01-13 14:01:12 +0800591 if (!pdata->clock_input) {
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200592 rate = clk_set_rate(&clk, 125000000);
593 if (rate != 125000000)
David Wucaf74612018-01-13 14:01:12 +0800594 return -EINVAL;
595 }
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200596 break;
David Wucaf74612018-01-13 14:01:12 +0800597
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200598 case PHY_INTERFACE_MODE_RGMII_ID:
599 /* Set to RGMII mode */
600 if (ops->set_to_rgmii) {
601 pdata->tx_delay = 0;
602 pdata->rx_delay = 0;
603 ops->set_to_rgmii(pdata);
604 } else
605 return -EPERM;
606
607 if (!pdata->clock_input) {
608 rate = clk_set_rate(&clk, 125000000);
609 if (rate != 125000000)
610 return -EINVAL;
611 }
612 break;
613
614 case PHY_INTERFACE_MODE_RMII:
David Wucaf74612018-01-13 14:01:12 +0800615 /* Set to RMII mode */
616 if (ops->set_to_rmii)
617 ops->set_to_rmii(pdata);
618 else
619 return -EPERM;
620
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200621 if (!pdata->clock_input) {
622 rate = clk_set_rate(&clk, 50000000);
623 if (rate != 50000000)
624 return -EINVAL;
625 }
David Wucaf74612018-01-13 14:01:12 +0800626 break;
Janine Hagemann3d1bd5b2018-08-28 08:25:05 +0200627
628 case PHY_INTERFACE_MODE_RGMII_RXID:
629 /* Set to RGMII_RXID mode */
630 if (ops->set_to_rgmii) {
631 pdata->tx_delay = 0;
632 ops->set_to_rgmii(pdata);
633 } else
634 return -EPERM;
635
636 if (!pdata->clock_input) {
637 rate = clk_set_rate(&clk, 125000000);
638 if (rate != 125000000)
639 return -EINVAL;
640 }
641 break;
642
643 case PHY_INTERFACE_MODE_RGMII_TXID:
644 /* Set to RGMII_TXID mode */
645 if (ops->set_to_rgmii) {
646 pdata->rx_delay = 0;
647 ops->set_to_rgmii(pdata);
648 } else
649 return -EPERM;
650
651 if (!pdata->clock_input) {
652 rate = clk_set_rate(&clk, 125000000);
653 if (rate != 125000000)
654 return -EINVAL;
655 }
656 break;
657
David Wucaf74612018-01-13 14:01:12 +0800658 default:
659 debug("NO interface defined!\n");
660 return -ENXIO;
661 }
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100662
663 return designware_eth_probe(dev);
664}
665
666static int gmac_rockchip_eth_start(struct udevice *dev)
667{
Simon Glassc69cda22020-12-03 16:55:20 -0700668 struct eth_pdata *pdata = dev_get_plat(dev);
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100669 struct dw_eth_dev *priv = dev_get_priv(dev);
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100670 struct rk_gmac_ops *ops =
671 (struct rk_gmac_ops *)dev_get_driver_data(dev);
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100672 int ret;
673
674 ret = designware_eth_init(priv, pdata->enetaddr);
675 if (ret)
676 return ret;
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100677 ret = ops->fix_mac_speed(priv);
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100678 if (ret)
679 return ret;
680 ret = designware_eth_enable(priv);
681 if (ret)
682 return ret;
683
684 return 0;
685}
686
687const struct eth_ops gmac_rockchip_eth_ops = {
688 .start = gmac_rockchip_eth_start,
689 .send = designware_eth_send,
690 .recv = designware_eth_recv,
691 .free_pkt = designware_eth_free_pkt,
692 .stop = designware_eth_stop,
693 .write_hwaddr = designware_eth_write_hwaddr,
694};
695
Heiko Stuebnerb0791182019-07-24 01:20:29 +0200696const struct rk_gmac_ops px30_gmac_ops = {
697 .fix_mac_speed = px30_gmac_fix_mac_speed,
698 .set_to_rmii = px30_gmac_set_to_rmii,
699};
700
David Wud12d7c02018-01-13 14:05:30 +0800701const struct rk_gmac_ops rk3228_gmac_ops = {
702 .fix_mac_speed = rk3228_gmac_fix_mac_speed,
703 .set_to_rgmii = rk3228_gmac_set_to_rgmii,
704};
705
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100706const struct rk_gmac_ops rk3288_gmac_ops = {
707 .fix_mac_speed = rk3288_gmac_fix_mac_speed,
708 .set_to_rgmii = rk3288_gmac_set_to_rgmii,
709};
710
David Wu0ed8f1e2019-11-26 09:39:49 +0800711const struct rk_gmac_ops rk3308_gmac_ops = {
712 .fix_mac_speed = rk3308_gmac_fix_mac_speed,
713 .set_to_rmii = rk3308_gmac_set_to_rmii,
714};
715
David Wub3d2a6d2018-01-13 14:03:04 +0800716const struct rk_gmac_ops rk3328_gmac_ops = {
717 .fix_mac_speed = rk3328_gmac_fix_mac_speed,
718 .set_to_rgmii = rk3328_gmac_set_to_rgmii,
719};
720
Philipp Tomsich793f2fd2017-07-25 17:02:51 +0200721const struct rk_gmac_ops rk3368_gmac_ops = {
722 .fix_mac_speed = rk3368_gmac_fix_mac_speed,
723 .set_to_rgmii = rk3368_gmac_set_to_rgmii,
724};
725
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100726const struct rk_gmac_ops rk3399_gmac_ops = {
727 .fix_mac_speed = rk3399_gmac_fix_mac_speed,
728 .set_to_rgmii = rk3399_gmac_set_to_rgmii,
729};
730
David Wucaf74612018-01-13 14:01:12 +0800731const struct rk_gmac_ops rv1108_gmac_ops = {
732 .fix_mac_speed = rv1108_set_rmii_speed,
733 .set_to_rmii = rv1108_gmac_set_to_rmii,
734};
735
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100736static const struct udevice_id rockchip_gmac_ids[] = {
Heiko Stuebnerb0791182019-07-24 01:20:29 +0200737 { .compatible = "rockchip,px30-gmac",
738 .data = (ulong)&px30_gmac_ops },
David Wud12d7c02018-01-13 14:05:30 +0800739 { .compatible = "rockchip,rk3228-gmac",
740 .data = (ulong)&rk3228_gmac_ops },
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100741 { .compatible = "rockchip,rk3288-gmac",
742 .data = (ulong)&rk3288_gmac_ops },
David Wu0ed8f1e2019-11-26 09:39:49 +0800743 { .compatible = "rockchip,rk3308-mac",
744 .data = (ulong)&rk3308_gmac_ops },
David Wub3d2a6d2018-01-13 14:03:04 +0800745 { .compatible = "rockchip,rk3328-gmac",
746 .data = (ulong)&rk3328_gmac_ops },
Philipp Tomsich793f2fd2017-07-25 17:02:51 +0200747 { .compatible = "rockchip,rk3368-gmac",
748 .data = (ulong)&rk3368_gmac_ops },
Philipp Tomsich1f08aa12017-03-24 19:24:26 +0100749 { .compatible = "rockchip,rk3399-gmac",
750 .data = (ulong)&rk3399_gmac_ops },
David Wucaf74612018-01-13 14:01:12 +0800751 { .compatible = "rockchip,rv1108-gmac",
752 .data = (ulong)&rv1108_gmac_ops },
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100753 { }
754};
755
756U_BOOT_DRIVER(eth_gmac_rockchip) = {
757 .name = "gmac_rockchip",
758 .id = UCLASS_ETH,
759 .of_match = rockchip_gmac_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700760 .of_to_plat = gmac_rockchip_of_to_plat,
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100761 .probe = gmac_rockchip_probe,
762 .ops = &gmac_rockchip_eth_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700763 .priv_auto = sizeof(struct dw_eth_dev),
Simon Glass8a8d24b2020-12-03 16:55:23 -0700764 .plat_auto = sizeof(struct gmac_rockchip_plat),
Sjoerd Simons0125bcf2017-01-11 11:46:11 +0100765 .flags = DM_FLAG_ALLOC_PRIV_DMA,
766};