2 * (C) Copyright 2015 Sjoerd Simons <sjoerd.simons@collabora.co.uk>
4 * SPDX-License-Identifier: GPL-2.0+
6 * Rockchip GMAC ethernet IP driver for U-Boot
15 #include <asm/arch/periph.h>
16 #include <asm/arch/clock.h>
17 #include <asm/arch/grf_rk3288.h>
18 #include <dm/pinctrl.h>
19 #include <dt-bindings/clock/rk3288-cru.h>
20 #include "designware.h"
22 DECLARE_GLOBAL_DATA_PTR;
25 * Platform data for the gmac
27 * dw_eth_pdata: Required platform data for designware driver (must be first)
29 struct gmac_rockchip_platdata {
30 struct dw_eth_pdata dw_eth_pdata;
35 static int gmac_rockchip_ofdata_to_platdata(struct udevice *dev)
37 struct gmac_rockchip_platdata *pdata = dev_get_platdata(dev);
39 pdata->tx_delay = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
41 pdata->rx_delay = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
44 return designware_eth_ofdata_to_platdata(dev);
47 static int gmac_rockchip_fix_mac_speed(struct dw_eth_dev *priv)
49 struct rk3288_grf *grf;
52 switch (priv->phydev->speed) {
54 clk = GMAC_CLK_SEL_2_5M;
57 clk = GMAC_CLK_SEL_25M;
60 clk = GMAC_CLK_SEL_125M;
63 debug("Unknown phy speed: %d\n", priv->phydev->speed);
67 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
68 rk_clrsetreg(&grf->soc_con1,
69 GMAC_CLK_SEL_MASK << GMAC_CLK_SEL_SHIFT,
70 clk << GMAC_CLK_SEL_SHIFT);
75 static int gmac_rockchip_probe(struct udevice *dev)
77 struct gmac_rockchip_platdata *pdata = dev_get_platdata(dev);
78 struct rk3288_grf *grf;
82 ret = clk_get_by_index(dev, 0, &clk);
86 /* Since mac_clk is fed by an external clock we can use 0 here */
87 ret = clk_set_rate(&clk, 0);
91 /* Set to RGMII mode */
92 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
93 rk_clrsetreg(&grf->soc_con1,
94 RMII_MODE_MASK << RMII_MODE_SHIFT |
95 GMAC_PHY_INTF_SEL_MASK << GMAC_PHY_INTF_SEL_SHIFT,
96 GMAC_PHY_INTF_SEL_RGMII << GMAC_PHY_INTF_SEL_SHIFT);
98 rk_clrsetreg(&grf->soc_con3,
99 RXCLK_DLY_ENA_GMAC_MASK << RXCLK_DLY_ENA_GMAC_SHIFT |
100 TXCLK_DLY_ENA_GMAC_MASK << TXCLK_DLY_ENA_GMAC_SHIFT |
101 CLK_RX_DL_CFG_GMAC_MASK << CLK_RX_DL_CFG_GMAC_SHIFT |
102 CLK_TX_DL_CFG_GMAC_MASK << CLK_TX_DL_CFG_GMAC_SHIFT,
103 RXCLK_DLY_ENA_GMAC_ENABLE << RXCLK_DLY_ENA_GMAC_SHIFT |
104 TXCLK_DLY_ENA_GMAC_ENABLE << TXCLK_DLY_ENA_GMAC_SHIFT |
105 pdata->rx_delay << CLK_RX_DL_CFG_GMAC_SHIFT |
106 pdata->tx_delay << CLK_TX_DL_CFG_GMAC_SHIFT);
108 return designware_eth_probe(dev);
111 static int gmac_rockchip_eth_start(struct udevice *dev)
113 struct eth_pdata *pdata = dev_get_platdata(dev);
114 struct dw_eth_dev *priv = dev_get_priv(dev);
117 ret = designware_eth_init(priv, pdata->enetaddr);
120 ret = gmac_rockchip_fix_mac_speed(priv);
123 ret = designware_eth_enable(priv);
130 const struct eth_ops gmac_rockchip_eth_ops = {
131 .start = gmac_rockchip_eth_start,
132 .send = designware_eth_send,
133 .recv = designware_eth_recv,
134 .free_pkt = designware_eth_free_pkt,
135 .stop = designware_eth_stop,
136 .write_hwaddr = designware_eth_write_hwaddr,
139 static const struct udevice_id rockchip_gmac_ids[] = {
140 { .compatible = "rockchip,rk3288-gmac" },
144 U_BOOT_DRIVER(eth_gmac_rockchip) = {
145 .name = "gmac_rockchip",
147 .of_match = rockchip_gmac_ids,
148 .ofdata_to_platdata = gmac_rockchip_ofdata_to_platdata,
149 .probe = gmac_rockchip_probe,
150 .ops = &gmac_rockchip_eth_ops,
151 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
152 .platdata_auto_alloc_size = sizeof(struct gmac_rockchip_platdata),
153 .flags = DM_FLAG_ALLOC_PRIV_DMA,