1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2015 Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5 * Rockchip GMAC ethernet IP driver for U-Boot
14 #include <asm/arch/periph.h>
15 #include <asm/arch/clock.h>
16 #include <asm/arch/hardware.h>
17 #include <asm/arch/grf_rk322x.h>
18 #include <asm/arch/grf_rk3288.h>
19 #include <asm/arch/grf_rk3328.h>
20 #include <asm/arch/grf_rk3368.h>
21 #include <asm/arch/grf_rk3399.h>
22 #include <asm/arch/grf_rv1108.h>
23 #include <dm/pinctrl.h>
24 #include <dt-bindings/clock/rk3288-cru.h>
25 #include "designware.h"
27 DECLARE_GLOBAL_DATA_PTR;
28 #define DELAY_ENABLE(soc, tx, rx) \
29 (((tx) ? soc##_TXCLK_DLY_ENA_GMAC_ENABLE : soc##_TXCLK_DLY_ENA_GMAC_DISABLE) | \
30 ((rx) ? soc##_RXCLK_DLY_ENA_GMAC_ENABLE : soc##_RXCLK_DLY_ENA_GMAC_DISABLE))
33 * Platform data for the gmac
35 * dw_eth_pdata: Required platform data for designware driver (must be first)
37 struct gmac_rockchip_platdata {
38 struct dw_eth_pdata dw_eth_pdata;
45 int (*fix_mac_speed)(struct dw_eth_dev *priv);
46 void (*set_to_rmii)(struct gmac_rockchip_platdata *pdata);
47 void (*set_to_rgmii)(struct gmac_rockchip_platdata *pdata);
51 static int gmac_rockchip_ofdata_to_platdata(struct udevice *dev)
53 struct gmac_rockchip_platdata *pdata = dev_get_platdata(dev);
56 string = dev_read_string(dev, "clock_in_out");
57 if (!strcmp(string, "input"))
58 pdata->clock_input = true;
60 pdata->clock_input = false;
62 /* Check the new naming-style first... */
63 pdata->tx_delay = dev_read_u32_default(dev, "tx_delay", -ENOENT);
64 pdata->rx_delay = dev_read_u32_default(dev, "rx_delay", -ENOENT);
66 /* ... and fall back to the old naming style or default, if necessary */
67 if (pdata->tx_delay == -ENOENT)
68 pdata->tx_delay = dev_read_u32_default(dev, "tx-delay", 0x30);
69 if (pdata->rx_delay == -ENOENT)
70 pdata->rx_delay = dev_read_u32_default(dev, "rx-delay", 0x10);
72 return designware_eth_ofdata_to_platdata(dev);
75 static int rk3228_gmac_fix_mac_speed(struct dw_eth_dev *priv)
77 struct rk322x_grf *grf;
80 RK3228_GMAC_CLK_SEL_SHIFT = 8,
81 RK3228_GMAC_CLK_SEL_MASK = GENMASK(9, 8),
82 RK3228_GMAC_CLK_SEL_125M = 0 << 8,
83 RK3228_GMAC_CLK_SEL_25M = 3 << 8,
84 RK3228_GMAC_CLK_SEL_2_5M = 2 << 8,
87 switch (priv->phydev->speed) {
89 clk = RK3228_GMAC_CLK_SEL_2_5M;
92 clk = RK3228_GMAC_CLK_SEL_25M;
95 clk = RK3228_GMAC_CLK_SEL_125M;
98 debug("Unknown phy speed: %d\n", priv->phydev->speed);
102 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
103 rk_clrsetreg(&grf->mac_con[1], RK3228_GMAC_CLK_SEL_MASK, clk);
108 static int rk3288_gmac_fix_mac_speed(struct dw_eth_dev *priv)
110 struct rk3288_grf *grf;
113 switch (priv->phydev->speed) {
115 clk = RK3288_GMAC_CLK_SEL_2_5M;
118 clk = RK3288_GMAC_CLK_SEL_25M;
121 clk = RK3288_GMAC_CLK_SEL_125M;
124 debug("Unknown phy speed: %d\n", priv->phydev->speed);
128 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
129 rk_clrsetreg(&grf->soc_con1, RK3288_GMAC_CLK_SEL_MASK, clk);
134 static int rk3328_gmac_fix_mac_speed(struct dw_eth_dev *priv)
136 struct rk3328_grf_regs *grf;
139 RK3328_GMAC_CLK_SEL_SHIFT = 11,
140 RK3328_GMAC_CLK_SEL_MASK = GENMASK(12, 11),
141 RK3328_GMAC_CLK_SEL_125M = 0 << 11,
142 RK3328_GMAC_CLK_SEL_25M = 3 << 11,
143 RK3328_GMAC_CLK_SEL_2_5M = 2 << 11,
146 switch (priv->phydev->speed) {
148 clk = RK3328_GMAC_CLK_SEL_2_5M;
151 clk = RK3328_GMAC_CLK_SEL_25M;
154 clk = RK3328_GMAC_CLK_SEL_125M;
157 debug("Unknown phy speed: %d\n", priv->phydev->speed);
161 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
162 rk_clrsetreg(&grf->mac_con[1], RK3328_GMAC_CLK_SEL_MASK, clk);
167 static int rk3368_gmac_fix_mac_speed(struct dw_eth_dev *priv)
169 struct rk3368_grf *grf;
172 RK3368_GMAC_CLK_SEL_2_5M = 2 << 4,
173 RK3368_GMAC_CLK_SEL_25M = 3 << 4,
174 RK3368_GMAC_CLK_SEL_125M = 0 << 4,
175 RK3368_GMAC_CLK_SEL_MASK = GENMASK(5, 4),
178 switch (priv->phydev->speed) {
180 clk = RK3368_GMAC_CLK_SEL_2_5M;
183 clk = RK3368_GMAC_CLK_SEL_25M;
186 clk = RK3368_GMAC_CLK_SEL_125M;
189 debug("Unknown phy speed: %d\n", priv->phydev->speed);
193 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
194 rk_clrsetreg(&grf->soc_con15, RK3368_GMAC_CLK_SEL_MASK, clk);
199 static int rk3399_gmac_fix_mac_speed(struct dw_eth_dev *priv)
201 struct rk3399_grf_regs *grf;
204 switch (priv->phydev->speed) {
206 clk = RK3399_GMAC_CLK_SEL_2_5M;
209 clk = RK3399_GMAC_CLK_SEL_25M;
212 clk = RK3399_GMAC_CLK_SEL_125M;
215 debug("Unknown phy speed: %d\n", priv->phydev->speed);
219 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
220 rk_clrsetreg(&grf->soc_con5, RK3399_GMAC_CLK_SEL_MASK, clk);
225 static int rv1108_set_rmii_speed(struct dw_eth_dev *priv)
227 struct rv1108_grf *grf;
230 RV1108_GMAC_SPEED_MASK = BIT(2),
231 RV1108_GMAC_SPEED_10M = 0 << 2,
232 RV1108_GMAC_SPEED_100M = 1 << 2,
233 RV1108_GMAC_CLK_SEL_MASK = BIT(7),
234 RV1108_GMAC_CLK_SEL_2_5M = 0 << 7,
235 RV1108_GMAC_CLK_SEL_25M = 1 << 7,
238 switch (priv->phydev->speed) {
240 clk = RV1108_GMAC_CLK_SEL_2_5M;
241 speed = RV1108_GMAC_SPEED_10M;
244 clk = RV1108_GMAC_CLK_SEL_25M;
245 speed = RV1108_GMAC_SPEED_100M;
248 debug("Unknown phy speed: %d\n", priv->phydev->speed);
252 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
253 rk_clrsetreg(&grf->gmac_con0,
254 RV1108_GMAC_CLK_SEL_MASK | RV1108_GMAC_SPEED_MASK,
260 static void rk3228_gmac_set_to_rgmii(struct gmac_rockchip_platdata *pdata)
262 struct rk322x_grf *grf;
264 RK3228_RMII_MODE_SHIFT = 10,
265 RK3228_RMII_MODE_MASK = BIT(10),
267 RK3228_GMAC_PHY_INTF_SEL_SHIFT = 4,
268 RK3228_GMAC_PHY_INTF_SEL_MASK = GENMASK(6, 4),
269 RK3228_GMAC_PHY_INTF_SEL_RGMII = BIT(4),
271 RK3228_RXCLK_DLY_ENA_GMAC_MASK = BIT(1),
272 RK3228_RXCLK_DLY_ENA_GMAC_DISABLE = 0,
273 RK3228_RXCLK_DLY_ENA_GMAC_ENABLE = BIT(1),
275 RK3228_TXCLK_DLY_ENA_GMAC_MASK = BIT(0),
276 RK3228_TXCLK_DLY_ENA_GMAC_DISABLE = 0,
277 RK3228_TXCLK_DLY_ENA_GMAC_ENABLE = BIT(0),
280 RK3228_CLK_RX_DL_CFG_GMAC_SHIFT = 0x7,
281 RK3228_CLK_RX_DL_CFG_GMAC_MASK = GENMASK(13, 7),
283 RK3228_CLK_TX_DL_CFG_GMAC_SHIFT = 0x0,
284 RK3228_CLK_TX_DL_CFG_GMAC_MASK = GENMASK(6, 0),
287 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
288 rk_clrsetreg(&grf->mac_con[1],
289 RK3228_RMII_MODE_MASK |
290 RK3228_GMAC_PHY_INTF_SEL_MASK |
291 RK3228_RXCLK_DLY_ENA_GMAC_MASK |
292 RK3228_TXCLK_DLY_ENA_GMAC_MASK,
293 RK3228_GMAC_PHY_INTF_SEL_RGMII |
294 DELAY_ENABLE(RK3228, pdata->tx_delay, pdata->rx_delay));
296 rk_clrsetreg(&grf->mac_con[0],
297 RK3228_CLK_RX_DL_CFG_GMAC_MASK |
298 RK3228_CLK_TX_DL_CFG_GMAC_MASK,
299 pdata->rx_delay << RK3228_CLK_RX_DL_CFG_GMAC_SHIFT |
300 pdata->tx_delay << RK3228_CLK_TX_DL_CFG_GMAC_SHIFT);
303 static void rk3288_gmac_set_to_rgmii(struct gmac_rockchip_platdata *pdata)
305 struct rk3288_grf *grf;
307 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
308 rk_clrsetreg(&grf->soc_con1,
309 RK3288_RMII_MODE_MASK | RK3288_GMAC_PHY_INTF_SEL_MASK,
310 RK3288_GMAC_PHY_INTF_SEL_RGMII);
312 rk_clrsetreg(&grf->soc_con3,
313 RK3288_RXCLK_DLY_ENA_GMAC_MASK |
314 RK3288_TXCLK_DLY_ENA_GMAC_MASK |
315 RK3288_CLK_RX_DL_CFG_GMAC_MASK |
316 RK3288_CLK_TX_DL_CFG_GMAC_MASK,
317 DELAY_ENABLE(RK3288, pdata->rx_delay, pdata->tx_delay) |
318 pdata->rx_delay << RK3288_CLK_RX_DL_CFG_GMAC_SHIFT |
319 pdata->tx_delay << RK3288_CLK_TX_DL_CFG_GMAC_SHIFT);
322 static void rk3328_gmac_set_to_rgmii(struct gmac_rockchip_platdata *pdata)
324 struct rk3328_grf_regs *grf;
326 RK3328_RMII_MODE_SHIFT = 9,
327 RK3328_RMII_MODE_MASK = BIT(9),
329 RK3328_GMAC_PHY_INTF_SEL_SHIFT = 4,
330 RK3328_GMAC_PHY_INTF_SEL_MASK = GENMASK(6, 4),
331 RK3328_GMAC_PHY_INTF_SEL_RGMII = BIT(4),
333 RK3328_RXCLK_DLY_ENA_GMAC_MASK = BIT(1),
334 RK3328_RXCLK_DLY_ENA_GMAC_DISABLE = 0,
335 RK3328_RXCLK_DLY_ENA_GMAC_ENABLE = BIT(1),
337 RK3328_TXCLK_DLY_ENA_GMAC_MASK = BIT(0),
338 RK3328_TXCLK_DLY_ENA_GMAC_DISABLE = 0,
339 RK3328_TXCLK_DLY_ENA_GMAC_ENABLE = BIT(0),
342 RK3328_CLK_RX_DL_CFG_GMAC_SHIFT = 0x7,
343 RK3328_CLK_RX_DL_CFG_GMAC_MASK = GENMASK(13, 7),
345 RK3328_CLK_TX_DL_CFG_GMAC_SHIFT = 0x0,
346 RK3328_CLK_TX_DL_CFG_GMAC_MASK = GENMASK(6, 0),
349 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
350 rk_clrsetreg(&grf->mac_con[1],
351 RK3328_RMII_MODE_MASK |
352 RK3328_GMAC_PHY_INTF_SEL_MASK |
353 RK3328_RXCLK_DLY_ENA_GMAC_MASK |
354 RK3328_TXCLK_DLY_ENA_GMAC_MASK,
355 RK3328_GMAC_PHY_INTF_SEL_RGMII |
356 DELAY_ENABLE(RK3328, pdata->tx_delay, pdata->rx_delay));
358 rk_clrsetreg(&grf->mac_con[0],
359 RK3328_CLK_RX_DL_CFG_GMAC_MASK |
360 RK3328_CLK_TX_DL_CFG_GMAC_MASK,
361 pdata->rx_delay << RK3328_CLK_RX_DL_CFG_GMAC_SHIFT |
362 pdata->tx_delay << RK3328_CLK_TX_DL_CFG_GMAC_SHIFT);
365 static void rk3368_gmac_set_to_rgmii(struct gmac_rockchip_platdata *pdata)
367 struct rk3368_grf *grf;
369 RK3368_GMAC_PHY_INTF_SEL_RGMII = 1 << 9,
370 RK3368_GMAC_PHY_INTF_SEL_MASK = GENMASK(11, 9),
371 RK3368_RMII_MODE_MASK = BIT(6),
372 RK3368_RMII_MODE = BIT(6),
375 RK3368_RXCLK_DLY_ENA_GMAC_MASK = BIT(15),
376 RK3368_RXCLK_DLY_ENA_GMAC_DISABLE = 0,
377 RK3368_RXCLK_DLY_ENA_GMAC_ENABLE = BIT(15),
378 RK3368_TXCLK_DLY_ENA_GMAC_MASK = BIT(7),
379 RK3368_TXCLK_DLY_ENA_GMAC_DISABLE = 0,
380 RK3368_TXCLK_DLY_ENA_GMAC_ENABLE = BIT(7),
381 RK3368_CLK_RX_DL_CFG_GMAC_SHIFT = 8,
382 RK3368_CLK_RX_DL_CFG_GMAC_MASK = GENMASK(14, 8),
383 RK3368_CLK_TX_DL_CFG_GMAC_SHIFT = 0,
384 RK3368_CLK_TX_DL_CFG_GMAC_MASK = GENMASK(6, 0),
387 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
388 rk_clrsetreg(&grf->soc_con15,
389 RK3368_RMII_MODE_MASK | RK3368_GMAC_PHY_INTF_SEL_MASK,
390 RK3368_GMAC_PHY_INTF_SEL_RGMII);
392 rk_clrsetreg(&grf->soc_con16,
393 RK3368_RXCLK_DLY_ENA_GMAC_MASK |
394 RK3368_TXCLK_DLY_ENA_GMAC_MASK |
395 RK3368_CLK_RX_DL_CFG_GMAC_MASK |
396 RK3368_CLK_TX_DL_CFG_GMAC_MASK,
397 DELAY_ENABLE(RK3368, pdata->tx_delay, pdata->rx_delay) |
398 pdata->rx_delay << RK3368_CLK_RX_DL_CFG_GMAC_SHIFT |
399 pdata->tx_delay << RK3368_CLK_TX_DL_CFG_GMAC_SHIFT);
402 static void rk3399_gmac_set_to_rgmii(struct gmac_rockchip_platdata *pdata)
404 struct rk3399_grf_regs *grf;
406 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
408 rk_clrsetreg(&grf->soc_con5,
409 RK3399_GMAC_PHY_INTF_SEL_MASK,
410 RK3399_GMAC_PHY_INTF_SEL_RGMII);
412 rk_clrsetreg(&grf->soc_con6,
413 RK3399_RXCLK_DLY_ENA_GMAC_MASK |
414 RK3399_TXCLK_DLY_ENA_GMAC_MASK |
415 RK3399_CLK_RX_DL_CFG_GMAC_MASK |
416 RK3399_CLK_TX_DL_CFG_GMAC_MASK,
417 DELAY_ENABLE(RK3399, pdata->tx_delay, pdata->rx_delay) |
418 pdata->rx_delay << RK3399_CLK_RX_DL_CFG_GMAC_SHIFT |
419 pdata->tx_delay << RK3399_CLK_TX_DL_CFG_GMAC_SHIFT);
422 static void rv1108_gmac_set_to_rmii(struct gmac_rockchip_platdata *pdata)
424 struct rv1108_grf *grf;
427 RV1108_GMAC_PHY_INTF_SEL_MASK = GENMASK(6, 4),
428 RV1108_GMAC_PHY_INTF_SEL_RMII = 4 << 4,
431 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
432 rk_clrsetreg(&grf->gmac_con0,
433 RV1108_GMAC_PHY_INTF_SEL_MASK,
434 RV1108_GMAC_PHY_INTF_SEL_RMII);
437 static int gmac_rockchip_probe(struct udevice *dev)
439 struct gmac_rockchip_platdata *pdata = dev_get_platdata(dev);
440 struct rk_gmac_ops *ops =
441 (struct rk_gmac_ops *)dev_get_driver_data(dev);
442 struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
443 struct eth_pdata *eth_pdata = &dw_pdata->eth_pdata;
448 ret = clk_get_by_index(dev, 0, &clk);
452 switch (eth_pdata->phy_interface) {
453 case PHY_INTERFACE_MODE_RGMII:
454 /* Set to RGMII mode */
455 if (ops->set_to_rgmii)
456 ops->set_to_rgmii(pdata);
461 * If the gmac clock is from internal pll, need to set and
462 * check the return value for gmac clock at RGMII mode. If
463 * the gmac clock is from external source, the clock rate
464 * is not set, because of it is bypassed.
467 if (!pdata->clock_input) {
468 rate = clk_set_rate(&clk, 125000000);
469 if (rate != 125000000)
474 case PHY_INTERFACE_MODE_RGMII_ID:
475 /* Set to RGMII mode */
476 if (ops->set_to_rgmii) {
479 ops->set_to_rgmii(pdata);
483 if (!pdata->clock_input) {
484 rate = clk_set_rate(&clk, 125000000);
485 if (rate != 125000000)
490 case PHY_INTERFACE_MODE_RMII:
491 /* Set to RMII mode */
492 if (ops->set_to_rmii)
493 ops->set_to_rmii(pdata);
497 if (!pdata->clock_input) {
498 rate = clk_set_rate(&clk, 50000000);
499 if (rate != 50000000)
504 case PHY_INTERFACE_MODE_RGMII_RXID:
505 /* Set to RGMII_RXID mode */
506 if (ops->set_to_rgmii) {
508 ops->set_to_rgmii(pdata);
512 if (!pdata->clock_input) {
513 rate = clk_set_rate(&clk, 125000000);
514 if (rate != 125000000)
519 case PHY_INTERFACE_MODE_RGMII_TXID:
520 /* Set to RGMII_TXID mode */
521 if (ops->set_to_rgmii) {
523 ops->set_to_rgmii(pdata);
527 if (!pdata->clock_input) {
528 rate = clk_set_rate(&clk, 125000000);
529 if (rate != 125000000)
535 debug("NO interface defined!\n");
539 return designware_eth_probe(dev);
542 static int gmac_rockchip_eth_start(struct udevice *dev)
544 struct eth_pdata *pdata = dev_get_platdata(dev);
545 struct dw_eth_dev *priv = dev_get_priv(dev);
546 struct rk_gmac_ops *ops =
547 (struct rk_gmac_ops *)dev_get_driver_data(dev);
550 ret = designware_eth_init(priv, pdata->enetaddr);
553 ret = ops->fix_mac_speed(priv);
556 ret = designware_eth_enable(priv);
563 const struct eth_ops gmac_rockchip_eth_ops = {
564 .start = gmac_rockchip_eth_start,
565 .send = designware_eth_send,
566 .recv = designware_eth_recv,
567 .free_pkt = designware_eth_free_pkt,
568 .stop = designware_eth_stop,
569 .write_hwaddr = designware_eth_write_hwaddr,
572 const struct rk_gmac_ops rk3228_gmac_ops = {
573 .fix_mac_speed = rk3228_gmac_fix_mac_speed,
574 .set_to_rgmii = rk3228_gmac_set_to_rgmii,
577 const struct rk_gmac_ops rk3288_gmac_ops = {
578 .fix_mac_speed = rk3288_gmac_fix_mac_speed,
579 .set_to_rgmii = rk3288_gmac_set_to_rgmii,
582 const struct rk_gmac_ops rk3328_gmac_ops = {
583 .fix_mac_speed = rk3328_gmac_fix_mac_speed,
584 .set_to_rgmii = rk3328_gmac_set_to_rgmii,
587 const struct rk_gmac_ops rk3368_gmac_ops = {
588 .fix_mac_speed = rk3368_gmac_fix_mac_speed,
589 .set_to_rgmii = rk3368_gmac_set_to_rgmii,
592 const struct rk_gmac_ops rk3399_gmac_ops = {
593 .fix_mac_speed = rk3399_gmac_fix_mac_speed,
594 .set_to_rgmii = rk3399_gmac_set_to_rgmii,
597 const struct rk_gmac_ops rv1108_gmac_ops = {
598 .fix_mac_speed = rv1108_set_rmii_speed,
599 .set_to_rmii = rv1108_gmac_set_to_rmii,
602 static const struct udevice_id rockchip_gmac_ids[] = {
603 { .compatible = "rockchip,rk3228-gmac",
604 .data = (ulong)&rk3228_gmac_ops },
605 { .compatible = "rockchip,rk3288-gmac",
606 .data = (ulong)&rk3288_gmac_ops },
607 { .compatible = "rockchip,rk3328-gmac",
608 .data = (ulong)&rk3328_gmac_ops },
609 { .compatible = "rockchip,rk3368-gmac",
610 .data = (ulong)&rk3368_gmac_ops },
611 { .compatible = "rockchip,rk3399-gmac",
612 .data = (ulong)&rk3399_gmac_ops },
613 { .compatible = "rockchip,rv1108-gmac",
614 .data = (ulong)&rv1108_gmac_ops },
618 U_BOOT_DRIVER(eth_gmac_rockchip) = {
619 .name = "gmac_rockchip",
621 .of_match = rockchip_gmac_ids,
622 .ofdata_to_platdata = gmac_rockchip_ofdata_to_platdata,
623 .probe = gmac_rockchip_probe,
624 .ops = &gmac_rockchip_eth_ops,
625 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
626 .platdata_auto_alloc_size = sizeof(struct gmac_rockchip_platdata),
627 .flags = DM_FLAG_ALLOC_PRIV_DMA,