blob: c9f9e839bad7ac276491f08d810f68c81683e24c [file] [log] [blame]
Sjoerd Simons54c46f92017-01-11 11:46:11 +01001/*
2 * (C) Copyright 2015 Sjoerd Simons <sjoerd.simons@collabora.co.uk>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 *
6 * Rockchip GMAC ethernet IP driver for U-Boot
7 */
8
9#include <common.h>
10#include <dm.h>
11#include <clk.h>
12#include <phy.h>
13#include <syscon.h>
14#include <asm/io.h>
15#include <asm/arch/periph.h>
16#include <asm/arch/clock.h>
Philipp Tomsich99cac582017-03-24 19:24:26 +010017#include <asm/arch/hardware.h>
Sjoerd Simons54c46f92017-01-11 11:46:11 +010018#include <asm/arch/grf_rk3288.h>
Philipp Tomsich99cac582017-03-24 19:24:26 +010019#include <asm/arch/grf_rk3399.h>
Sjoerd Simons54c46f92017-01-11 11:46:11 +010020#include <dm/pinctrl.h>
21#include <dt-bindings/clock/rk3288-cru.h>
22#include "designware.h"
23
24DECLARE_GLOBAL_DATA_PTR;
25
26/*
27 * Platform data for the gmac
28 *
29 * dw_eth_pdata: Required platform data for designware driver (must be first)
30 */
31struct gmac_rockchip_platdata {
32 struct dw_eth_pdata dw_eth_pdata;
33 int tx_delay;
34 int rx_delay;
35};
36
Philipp Tomsich99cac582017-03-24 19:24:26 +010037struct rk_gmac_ops {
38 int (*fix_mac_speed)(struct dw_eth_dev *priv);
39 void (*set_to_rgmii)(struct gmac_rockchip_platdata *pdata);
40};
41
42
Sjoerd Simons54c46f92017-01-11 11:46:11 +010043static int gmac_rockchip_ofdata_to_platdata(struct udevice *dev)
44{
45 struct gmac_rockchip_platdata *pdata = dev_get_platdata(dev);
46
Philipp Tomsich99cac582017-03-24 19:24:26 +010047 /* Check the new naming-style first... */
Philipp Tomsich150005b2017-06-07 18:46:01 +020048 pdata->tx_delay = dev_read_u32_default(dev, "tx_delay", -ENOENT);
49 pdata->rx_delay = dev_read_u32_default(dev, "rx_delay", -ENOENT);
Philipp Tomsich99cac582017-03-24 19:24:26 +010050
51 /* ... and fall back to the old naming style or default, if necessary */
52 if (pdata->tx_delay == -ENOENT)
Philipp Tomsich150005b2017-06-07 18:46:01 +020053 pdata->tx_delay = dev_read_u32_default(dev, "tx-delay", 0x30);
Philipp Tomsich99cac582017-03-24 19:24:26 +010054 if (pdata->rx_delay == -ENOENT)
Philipp Tomsich150005b2017-06-07 18:46:01 +020055 pdata->rx_delay = dev_read_u32_default(dev, "rx-delay", 0x10);
Sjoerd Simons54c46f92017-01-11 11:46:11 +010056
57 return designware_eth_ofdata_to_platdata(dev);
58}
59
Philipp Tomsich99cac582017-03-24 19:24:26 +010060static int rk3288_gmac_fix_mac_speed(struct dw_eth_dev *priv)
Sjoerd Simons54c46f92017-01-11 11:46:11 +010061{
62 struct rk3288_grf *grf;
63 int clk;
64
65 switch (priv->phydev->speed) {
66 case 10:
Philipp Tomsich99cac582017-03-24 19:24:26 +010067 clk = RK3288_GMAC_CLK_SEL_2_5M;
Sjoerd Simons54c46f92017-01-11 11:46:11 +010068 break;
69 case 100:
Philipp Tomsich99cac582017-03-24 19:24:26 +010070 clk = RK3288_GMAC_CLK_SEL_25M;
Sjoerd Simons54c46f92017-01-11 11:46:11 +010071 break;
72 case 1000:
Philipp Tomsich99cac582017-03-24 19:24:26 +010073 clk = RK3288_GMAC_CLK_SEL_125M;
Sjoerd Simons54c46f92017-01-11 11:46:11 +010074 break;
75 default:
76 debug("Unknown phy speed: %d\n", priv->phydev->speed);
77 return -EINVAL;
78 }
79
80 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Philipp Tomsich99cac582017-03-24 19:24:26 +010081 rk_clrsetreg(&grf->soc_con1, RK3288_GMAC_CLK_SEL_MASK, clk);
82
83 return 0;
84}
85
86static int rk3399_gmac_fix_mac_speed(struct dw_eth_dev *priv)
87{
88 struct rk3399_grf_regs *grf;
89 int clk;
90
91 switch (priv->phydev->speed) {
92 case 10:
93 clk = RK3399_GMAC_CLK_SEL_2_5M;
94 break;
95 case 100:
96 clk = RK3399_GMAC_CLK_SEL_25M;
97 break;
98 case 1000:
99 clk = RK3399_GMAC_CLK_SEL_125M;
100 break;
101 default:
102 debug("Unknown phy speed: %d\n", priv->phydev->speed);
103 return -EINVAL;
104 }
105
106 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
107 rk_clrsetreg(&grf->soc_con5, RK3399_GMAC_CLK_SEL_MASK, clk);
Sjoerd Simons54c46f92017-01-11 11:46:11 +0100108
109 return 0;
110}
111
Philipp Tomsich99cac582017-03-24 19:24:26 +0100112static void rk3288_gmac_set_to_rgmii(struct gmac_rockchip_platdata *pdata)
113{
114 struct rk3288_grf *grf;
115
116 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
117 rk_clrsetreg(&grf->soc_con1,
118 RK3288_RMII_MODE_MASK | RK3288_GMAC_PHY_INTF_SEL_MASK,
119 RK3288_GMAC_PHY_INTF_SEL_RGMII);
120
121 rk_clrsetreg(&grf->soc_con3,
122 RK3288_RXCLK_DLY_ENA_GMAC_MASK |
123 RK3288_TXCLK_DLY_ENA_GMAC_MASK |
124 RK3288_CLK_RX_DL_CFG_GMAC_MASK |
125 RK3288_CLK_TX_DL_CFG_GMAC_MASK,
126 RK3288_RXCLK_DLY_ENA_GMAC_ENABLE |
127 RK3288_TXCLK_DLY_ENA_GMAC_ENABLE |
128 pdata->rx_delay << RK3288_CLK_RX_DL_CFG_GMAC_SHIFT |
129 pdata->tx_delay << RK3288_CLK_TX_DL_CFG_GMAC_SHIFT);
130}
131
132static void rk3399_gmac_set_to_rgmii(struct gmac_rockchip_platdata *pdata)
133{
134 struct rk3399_grf_regs *grf;
135
136 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
137
138 rk_clrsetreg(&grf->soc_con5,
139 RK3399_GMAC_PHY_INTF_SEL_MASK,
140 RK3399_GMAC_PHY_INTF_SEL_RGMII);
141
142 rk_clrsetreg(&grf->soc_con6,
143 RK3399_RXCLK_DLY_ENA_GMAC_MASK |
144 RK3399_TXCLK_DLY_ENA_GMAC_MASK |
145 RK3399_CLK_RX_DL_CFG_GMAC_MASK |
146 RK3399_CLK_TX_DL_CFG_GMAC_MASK,
147 RK3399_RXCLK_DLY_ENA_GMAC_ENABLE |
148 RK3399_TXCLK_DLY_ENA_GMAC_ENABLE |
149 pdata->rx_delay << RK3399_CLK_RX_DL_CFG_GMAC_SHIFT |
150 pdata->tx_delay << RK3399_CLK_TX_DL_CFG_GMAC_SHIFT);
151}
152
Sjoerd Simons54c46f92017-01-11 11:46:11 +0100153static int gmac_rockchip_probe(struct udevice *dev)
154{
155 struct gmac_rockchip_platdata *pdata = dev_get_platdata(dev);
Philipp Tomsich99cac582017-03-24 19:24:26 +0100156 struct rk_gmac_ops *ops =
157 (struct rk_gmac_ops *)dev_get_driver_data(dev);
Sjoerd Simons54c46f92017-01-11 11:46:11 +0100158 struct clk clk;
159 int ret;
160
161 ret = clk_get_by_index(dev, 0, &clk);
162 if (ret)
163 return ret;
164
165 /* Since mac_clk is fed by an external clock we can use 0 here */
166 ret = clk_set_rate(&clk, 0);
167 if (ret)
168 return ret;
169
170 /* Set to RGMII mode */
Philipp Tomsich99cac582017-03-24 19:24:26 +0100171 ops->set_to_rgmii(pdata);
Sjoerd Simons54c46f92017-01-11 11:46:11 +0100172
173 return designware_eth_probe(dev);
174}
175
176static int gmac_rockchip_eth_start(struct udevice *dev)
177{
178 struct eth_pdata *pdata = dev_get_platdata(dev);
179 struct dw_eth_dev *priv = dev_get_priv(dev);
Philipp Tomsich99cac582017-03-24 19:24:26 +0100180 struct rk_gmac_ops *ops =
181 (struct rk_gmac_ops *)dev_get_driver_data(dev);
Sjoerd Simons54c46f92017-01-11 11:46:11 +0100182 int ret;
183
184 ret = designware_eth_init(priv, pdata->enetaddr);
185 if (ret)
186 return ret;
Philipp Tomsich99cac582017-03-24 19:24:26 +0100187 ret = ops->fix_mac_speed(priv);
Sjoerd Simons54c46f92017-01-11 11:46:11 +0100188 if (ret)
189 return ret;
190 ret = designware_eth_enable(priv);
191 if (ret)
192 return ret;
193
194 return 0;
195}
196
197const struct eth_ops gmac_rockchip_eth_ops = {
198 .start = gmac_rockchip_eth_start,
199 .send = designware_eth_send,
200 .recv = designware_eth_recv,
201 .free_pkt = designware_eth_free_pkt,
202 .stop = designware_eth_stop,
203 .write_hwaddr = designware_eth_write_hwaddr,
204};
205
Philipp Tomsich99cac582017-03-24 19:24:26 +0100206const struct rk_gmac_ops rk3288_gmac_ops = {
207 .fix_mac_speed = rk3288_gmac_fix_mac_speed,
208 .set_to_rgmii = rk3288_gmac_set_to_rgmii,
209};
210
211const struct rk_gmac_ops rk3399_gmac_ops = {
212 .fix_mac_speed = rk3399_gmac_fix_mac_speed,
213 .set_to_rgmii = rk3399_gmac_set_to_rgmii,
214};
215
Sjoerd Simons54c46f92017-01-11 11:46:11 +0100216static const struct udevice_id rockchip_gmac_ids[] = {
Philipp Tomsich99cac582017-03-24 19:24:26 +0100217 { .compatible = "rockchip,rk3288-gmac",
218 .data = (ulong)&rk3288_gmac_ops },
219 { .compatible = "rockchip,rk3399-gmac",
220 .data = (ulong)&rk3399_gmac_ops },
Sjoerd Simons54c46f92017-01-11 11:46:11 +0100221 { }
222};
223
224U_BOOT_DRIVER(eth_gmac_rockchip) = {
225 .name = "gmac_rockchip",
226 .id = UCLASS_ETH,
227 .of_match = rockchip_gmac_ids,
228 .ofdata_to_platdata = gmac_rockchip_ofdata_to_platdata,
229 .probe = gmac_rockchip_probe,
230 .ops = &gmac_rockchip_eth_ops,
231 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
232 .platdata_auto_alloc_size = sizeof(struct gmac_rockchip_platdata),
233 .flags = DM_FLAG_ALLOC_PRIV_DMA,
234};