blob: c4557e57988dc56755e549e9a91d865a19f08811 [file] [log] [blame]
Jonas Karlman098ee4f2023-10-01 19:17:19 +00001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright Contributors to the U-Boot project.
4 *
5 * rk_gmac_ops ported from linux drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c
6 *
7 * Ported code is intentionally left as close as possible with linux counter
8 * part in order to simplify future porting of fixes and support for other SoCs.
9 */
10
Jonas Karlman098ee4f2023-10-01 19:17:19 +000011#include <clk.h>
12#include <dm.h>
13#include <dm/device_compat.h>
14#include <net.h>
15#include <phy.h>
16#include <regmap.h>
17#include <reset.h>
18#include <syscon.h>
19#include <asm/gpio.h>
20#include <linux/delay.h>
21
22#include "dwc_eth_qos.h"
23
24struct rk_gmac_ops {
25 const char *compatible;
26 int (*set_to_rgmii)(struct udevice *dev,
27 int tx_delay, int rx_delay);
28 int (*set_to_rmii)(struct udevice *dev);
29 int (*set_gmac_speed)(struct udevice *dev);
Jonas Karlman1b615702023-10-01 19:17:20 +000030 void (*set_clock_selection)(struct udevice *dev, bool enable);
Jonas Karlman098ee4f2023-10-01 19:17:19 +000031 u32 regs[3];
32};
33
34struct rockchip_platform_data {
35 struct reset_ctl_bulk resets;
36 const struct rk_gmac_ops *ops;
37 int id;
Jonas Karlman1b615702023-10-01 19:17:20 +000038 bool clock_input;
Jonas Karlman098ee4f2023-10-01 19:17:19 +000039 struct regmap *grf;
Jonas Karlman1b615702023-10-01 19:17:20 +000040 struct regmap *php_grf;
Jonas Karlman098ee4f2023-10-01 19:17:19 +000041};
42
43#define HIWORD_UPDATE(val, mask, shift) \
44 ((val) << (shift) | (mask) << ((shift) + 16))
45
46#define GRF_BIT(nr) (BIT(nr) | BIT((nr) + 16))
47#define GRF_CLR_BIT(nr) (BIT((nr) + 16))
48
49#define RK3568_GRF_GMAC0_CON0 0x0380
50#define RK3568_GRF_GMAC0_CON1 0x0384
51#define RK3568_GRF_GMAC1_CON0 0x0388
52#define RK3568_GRF_GMAC1_CON1 0x038c
53
54/* RK3568_GRF_GMAC0_CON1 && RK3568_GRF_GMAC1_CON1 */
55#define RK3568_GMAC_PHY_INTF_SEL_RGMII \
56 (GRF_BIT(4) | GRF_CLR_BIT(5) | GRF_CLR_BIT(6))
57#define RK3568_GMAC_PHY_INTF_SEL_RMII \
58 (GRF_CLR_BIT(4) | GRF_CLR_BIT(5) | GRF_BIT(6))
59#define RK3568_GMAC_FLOW_CTRL GRF_BIT(3)
60#define RK3568_GMAC_FLOW_CTRL_CLR GRF_CLR_BIT(3)
61#define RK3568_GMAC_RXCLK_DLY_ENABLE GRF_BIT(1)
62#define RK3568_GMAC_RXCLK_DLY_DISABLE GRF_CLR_BIT(1)
63#define RK3568_GMAC_TXCLK_DLY_ENABLE GRF_BIT(0)
64#define RK3568_GMAC_TXCLK_DLY_DISABLE GRF_CLR_BIT(0)
65
66/* RK3568_GRF_GMAC0_CON0 && RK3568_GRF_GMAC1_CON0 */
67#define RK3568_GMAC_CLK_RX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 8)
68#define RK3568_GMAC_CLK_TX_DL_CFG(val) HIWORD_UPDATE(val, 0x7F, 0)
69
70static int rk3568_set_to_rgmii(struct udevice *dev,
71 int tx_delay, int rx_delay)
72{
73 struct eth_pdata *pdata = dev_get_plat(dev);
74 struct rockchip_platform_data *data = pdata->priv_pdata;
75 u32 con0, con1;
76
77 con0 = (data->id == 1) ? RK3568_GRF_GMAC1_CON0 :
78 RK3568_GRF_GMAC0_CON0;
79 con1 = (data->id == 1) ? RK3568_GRF_GMAC1_CON1 :
80 RK3568_GRF_GMAC0_CON1;
81
82 regmap_write(data->grf, con0,
83 RK3568_GMAC_CLK_RX_DL_CFG(rx_delay) |
84 RK3568_GMAC_CLK_TX_DL_CFG(tx_delay));
85
86 regmap_write(data->grf, con1,
87 RK3568_GMAC_PHY_INTF_SEL_RGMII |
88 RK3568_GMAC_RXCLK_DLY_ENABLE |
89 RK3568_GMAC_TXCLK_DLY_ENABLE);
90
91 return 0;
92}
93
94static int rk3568_set_to_rmii(struct udevice *dev)
95{
96 struct eth_pdata *pdata = dev_get_plat(dev);
97 struct rockchip_platform_data *data = pdata->priv_pdata;
98 u32 con1;
99
100 con1 = (data->id == 1) ? RK3568_GRF_GMAC1_CON1 :
101 RK3568_GRF_GMAC0_CON1;
102 regmap_write(data->grf, con1, RK3568_GMAC_PHY_INTF_SEL_RMII);
103
104 return 0;
105}
106
107static int rk3568_set_gmac_speed(struct udevice *dev)
108{
109 struct eqos_priv *eqos = dev_get_priv(dev);
110 ulong rate;
111 int ret;
112
113 switch (eqos->phy->speed) {
114 case SPEED_10:
115 rate = 2500000;
116 break;
117 case SPEED_100:
118 rate = 25000000;
119 break;
120 case SPEED_1000:
121 rate = 125000000;
122 break;
123 default:
124 return -EINVAL;
125 }
126
127 ret = clk_set_rate(&eqos->clk_tx, rate);
128 if (ret < 0)
129 return ret;
130
131 return 0;
132}
133
Jonas Karlman1b615702023-10-01 19:17:20 +0000134/* sys_grf */
135#define RK3588_GRF_GMAC_CON7 0x031c
136#define RK3588_GRF_GMAC_CON8 0x0320
137#define RK3588_GRF_GMAC_CON9 0x0324
138
139#define RK3588_GMAC_RXCLK_DLY_ENABLE(id) GRF_BIT(2 * (id) + 3)
140#define RK3588_GMAC_RXCLK_DLY_DISABLE(id) GRF_CLR_BIT(2 * (id) + 3)
141#define RK3588_GMAC_TXCLK_DLY_ENABLE(id) GRF_BIT(2 * (id) + 2)
142#define RK3588_GMAC_TXCLK_DLY_DISABLE(id) GRF_CLR_BIT(2 * (id) + 2)
143
144#define RK3588_GMAC_CLK_RX_DL_CFG(val) HIWORD_UPDATE(val, 0xFF, 8)
145#define RK3588_GMAC_CLK_TX_DL_CFG(val) HIWORD_UPDATE(val, 0xFF, 0)
146
147/* php_grf */
148#define RK3588_GRF_GMAC_CON0 0x0008
149#define RK3588_GRF_CLK_CON1 0x0070
150
151#define RK3588_GMAC_PHY_INTF_SEL_RGMII(id) \
152 (GRF_BIT(3 + (id) * 6) | GRF_CLR_BIT(4 + (id) * 6) | GRF_CLR_BIT(5 + (id) * 6))
153#define RK3588_GMAC_PHY_INTF_SEL_RMII(id) \
154 (GRF_CLR_BIT(3 + (id) * 6) | GRF_CLR_BIT(4 + (id) * 6) | GRF_BIT(5 + (id) * 6))
155
156#define RK3588_GMAC_CLK_RMII_MODE(id) GRF_BIT(5 * (id))
157#define RK3588_GMAC_CLK_RGMII_MODE(id) GRF_CLR_BIT(5 * (id))
158
159#define RK3588_GMAC_CLK_SELET_CRU(id) GRF_BIT(5 * (id) + 4)
160#define RK3588_GMAC_CLK_SELET_IO(id) GRF_CLR_BIT(5 * (id) + 4)
161
162#define RK3588_GMAC_CLK_RMII_DIV2(id) GRF_BIT(5 * (id) + 2)
163#define RK3588_GMAC_CLK_RMII_DIV20(id) GRF_CLR_BIT(5 * (id) + 2)
164
165#define RK3588_GMAC_CLK_RGMII_DIV1(id) \
166 (GRF_CLR_BIT(5 * (id) + 2) | GRF_CLR_BIT(5 * (id) + 3))
167#define RK3588_GMAC_CLK_RGMII_DIV5(id) \
168 (GRF_BIT(5 * (id) + 2) | GRF_BIT(5 * (id) + 3))
169#define RK3588_GMAC_CLK_RGMII_DIV50(id) \
170 (GRF_CLR_BIT(5 * (id) + 2) | GRF_BIT(5 * (id) + 3))
171
172#define RK3588_GMAC_CLK_RMII_GATE(id) GRF_BIT(5 * (id) + 1)
173#define RK3588_GMAC_CLK_RMII_NOGATE(id) GRF_CLR_BIT(5 * (id) + 1)
174
175static int rk3588_set_to_rgmii(struct udevice *dev,
176 int tx_delay, int rx_delay)
177{
178 struct eth_pdata *pdata = dev_get_plat(dev);
179 struct rockchip_platform_data *data = pdata->priv_pdata;
180 u32 offset_con, id = data->id;
181
182 offset_con = data->id == 1 ? RK3588_GRF_GMAC_CON9 :
183 RK3588_GRF_GMAC_CON8;
184
185 regmap_write(data->php_grf, RK3588_GRF_GMAC_CON0,
186 RK3588_GMAC_PHY_INTF_SEL_RGMII(id));
187
188 regmap_write(data->php_grf, RK3588_GRF_CLK_CON1,
189 RK3588_GMAC_CLK_RGMII_MODE(id));
190
191 regmap_write(data->grf, RK3588_GRF_GMAC_CON7,
192 RK3588_GMAC_RXCLK_DLY_ENABLE(id) |
193 RK3588_GMAC_TXCLK_DLY_ENABLE(id));
194
195 regmap_write(data->grf, offset_con,
196 RK3588_GMAC_CLK_RX_DL_CFG(rx_delay) |
197 RK3588_GMAC_CLK_TX_DL_CFG(tx_delay));
198
199 return 0;
200}
201
202static int rk3588_set_to_rmii(struct udevice *dev)
203{
204 struct eth_pdata *pdata = dev_get_plat(dev);
205 struct rockchip_platform_data *data = pdata->priv_pdata;
206
207 regmap_write(data->php_grf, RK3588_GRF_GMAC_CON0,
208 RK3588_GMAC_PHY_INTF_SEL_RMII(data->id));
209
210 regmap_write(data->php_grf, RK3588_GRF_CLK_CON1,
211 RK3588_GMAC_CLK_RMII_MODE(data->id));
212
213 return 0;
214}
215
216static int rk3588_set_gmac_speed(struct udevice *dev)
217{
218 struct eqos_priv *eqos = dev_get_priv(dev);
219 struct eth_pdata *pdata = dev_get_plat(dev);
220 struct rockchip_platform_data *data = pdata->priv_pdata;
221 u32 val = 0, id = data->id;
222
223 switch (eqos->phy->speed) {
224 case SPEED_10:
225 if (pdata->phy_interface == PHY_INTERFACE_MODE_RMII)
226 val = RK3588_GMAC_CLK_RMII_DIV20(id);
227 else
228 val = RK3588_GMAC_CLK_RGMII_DIV50(id);
229 break;
230 case SPEED_100:
231 if (pdata->phy_interface == PHY_INTERFACE_MODE_RMII)
232 val = RK3588_GMAC_CLK_RMII_DIV2(id);
233 else
234 val = RK3588_GMAC_CLK_RGMII_DIV5(id);
235 break;
236 case SPEED_1000:
237 if (pdata->phy_interface != PHY_INTERFACE_MODE_RMII)
238 val = RK3588_GMAC_CLK_RGMII_DIV1(id);
239 else
240 return -EINVAL;
241 break;
242 default:
243 return -EINVAL;
244 }
245
246 regmap_write(data->php_grf, RK3588_GRF_CLK_CON1, val);
247
248 return 0;
249}
250
251static void rk3588_set_clock_selection(struct udevice *dev, bool enable)
252{
253 struct eth_pdata *pdata = dev_get_plat(dev);
254 struct rockchip_platform_data *data = pdata->priv_pdata;
255
256 u32 val = data->clock_input ? RK3588_GMAC_CLK_SELET_IO(data->id) :
257 RK3588_GMAC_CLK_SELET_CRU(data->id);
258
259 val |= enable ? RK3588_GMAC_CLK_RMII_NOGATE(data->id) :
260 RK3588_GMAC_CLK_RMII_GATE(data->id);
261
262 regmap_write(data->php_grf, RK3588_GRF_CLK_CON1, val);
263}
264
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000265static const struct rk_gmac_ops rk_gmac_ops[] = {
266 {
267 .compatible = "rockchip,rk3568-gmac",
268 .set_to_rgmii = rk3568_set_to_rgmii,
269 .set_to_rmii = rk3568_set_to_rmii,
270 .set_gmac_speed = rk3568_set_gmac_speed,
271 .regs = {
272 0xfe2a0000, /* gmac0 */
273 0xfe010000, /* gmac1 */
274 0x0, /* sentinel */
275 },
276 },
Jonas Karlman1b615702023-10-01 19:17:20 +0000277 {
278 .compatible = "rockchip,rk3588-gmac",
279 .set_to_rgmii = rk3588_set_to_rgmii,
280 .set_to_rmii = rk3588_set_to_rmii,
281 .set_gmac_speed = rk3588_set_gmac_speed,
282 .set_clock_selection = rk3588_set_clock_selection,
283 .regs = {
284 0xfe1b0000, /* gmac0 */
285 0xfe1c0000, /* gmac1 */
286 0x0, /* sentinel */
287 },
288 },
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000289 { }
290};
291
292static const struct rk_gmac_ops *get_rk_gmac_ops(struct udevice *dev)
293{
294 const struct rk_gmac_ops *ops = rk_gmac_ops;
295
296 while (ops->compatible) {
297 if (device_is_compatible(dev, ops->compatible))
298 return ops;
299 ops++;
300 }
301
302 return NULL;
303}
304
305static int eqos_probe_resources_rk(struct udevice *dev)
306{
307 struct eqos_priv *eqos = dev_get_priv(dev);
308 struct eth_pdata *pdata = dev_get_plat(dev);
309 struct rockchip_platform_data *data;
Jonas Karlman1b615702023-10-01 19:17:20 +0000310 const char *clock_in_out;
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000311 int reset_flags = GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE;
312 int ret;
313
314 data = calloc(1, sizeof(struct rockchip_platform_data));
315 if (!data)
316 return -ENOMEM;
317
318 data->ops = get_rk_gmac_ops(dev);
319 if (!data->ops) {
320 ret = -EINVAL;
321 goto err_free;
322 }
323
324 for (int i = 0; data->ops->regs[i]; i++) {
325 if (data->ops->regs[i] == (u32)eqos->regs) {
326 data->id = i;
327 break;
328 }
329 }
330
331 pdata->priv_pdata = data;
332 pdata->phy_interface = eqos->config->interface(dev);
333 pdata->max_speed = eqos->max_speed;
334
335 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) {
336 pr_err("Invalid PHY interface\n");
337 ret = -EINVAL;
338 goto err_free;
339 }
340
341 data->grf = syscon_regmap_lookup_by_phandle(dev, "rockchip,grf");
342 if (IS_ERR(data->grf)) {
343 dev_err(dev, "Missing rockchip,grf property\n");
344 ret = -EINVAL;
345 goto err_free;
346 }
347
Jonas Karlman1b615702023-10-01 19:17:20 +0000348 if (device_is_compatible(dev, "rockchip,rk3588-gmac")) {
349 data->php_grf =
350 syscon_regmap_lookup_by_phandle(dev, "rockchip,php-grf");
351 if (IS_ERR(data->php_grf)) {
352 dev_err(dev, "Missing rockchip,php-grf property\n");
353 ret = -EINVAL;
354 goto err_free;
355 }
356 }
357
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000358 ret = reset_get_bulk(dev, &data->resets);
359 if (ret < 0)
360 goto err_free;
361
362 reset_assert_bulk(&data->resets);
363
364 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
365 if (ret) {
366 dev_dbg(dev, "clk_get_by_name(stmmaceth) failed: %d", ret);
367 goto err_release_resets;
368 }
369
Jonas Karlman1b615702023-10-01 19:17:20 +0000370 if (device_is_compatible(dev, "rockchip,rk3568-gmac")) {
371 ret = clk_get_by_name(dev, "clk_mac_speed", &eqos->clk_tx);
372 if (ret) {
373 dev_dbg(dev, "clk_get_by_name(clk_mac_speed) failed: %d", ret);
Sean Andersond318eb32023-12-16 14:38:42 -0500374 goto err_release_resets;
Jonas Karlman1b615702023-10-01 19:17:20 +0000375 }
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000376 }
377
Jonas Karlman1b615702023-10-01 19:17:20 +0000378 clock_in_out = dev_read_string(dev, "clock_in_out");
379 if (clock_in_out && !strcmp(clock_in_out, "input"))
380 data->clock_input = true;
381 else
382 data->clock_input = false;
383
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000384 /* snps,reset props are deprecated, do bare minimum to support them */
385 if (dev_read_bool(dev, "snps,reset-active-low"))
386 reset_flags |= GPIOD_ACTIVE_LOW;
387
388 dev_read_u32_array(dev, "snps,reset-delays-us", eqos->reset_delays, 3);
389
390 gpio_request_by_name(dev, "snps,reset-gpio", 0,
391 &eqos->phy_reset_gpio, reset_flags);
392
393 return 0;
394
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000395err_release_resets:
396 reset_release_bulk(&data->resets);
397err_free:
398 free(data);
399
400 return ret;
401}
402
403static int eqos_remove_resources_rk(struct udevice *dev)
404{
405 struct eqos_priv *eqos = dev_get_priv(dev);
406 struct eth_pdata *pdata = dev_get_plat(dev);
407 struct rockchip_platform_data *data = pdata->priv_pdata;
408
409 if (dm_gpio_is_valid(&eqos->phy_reset_gpio))
410 dm_gpio_free(dev, &eqos->phy_reset_gpio);
411
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000412 reset_release_bulk(&data->resets);
413 free(data);
414
415 return 0;
416}
417
418static int eqos_stop_resets_rk(struct udevice *dev)
419{
420 struct eth_pdata *pdata = dev_get_plat(dev);
421 struct rockchip_platform_data *data = pdata->priv_pdata;
422
423 return reset_assert_bulk(&data->resets);
424}
425
426static int eqos_start_resets_rk(struct udevice *dev)
427{
428 struct eth_pdata *pdata = dev_get_plat(dev);
429 struct rockchip_platform_data *data = pdata->priv_pdata;
430
431 return reset_deassert_bulk(&data->resets);
432}
433
434static int eqos_stop_clks_rk(struct udevice *dev)
435{
Jonas Karlman1b615702023-10-01 19:17:20 +0000436 struct eth_pdata *pdata = dev_get_plat(dev);
437 struct rockchip_platform_data *data = pdata->priv_pdata;
438
439 if (data->ops->set_clock_selection)
440 data->ops->set_clock_selection(dev, false);
441
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000442 return 0;
443}
444
445static int eqos_start_clks_rk(struct udevice *dev)
446{
447 struct eqos_priv *eqos = dev_get_priv(dev);
448 struct eth_pdata *pdata = dev_get_plat(dev);
449 struct rockchip_platform_data *data = pdata->priv_pdata;
450 int tx_delay, rx_delay, ret;
451
452 if (dm_gpio_is_valid(&eqos->phy_reset_gpio)) {
453 udelay(eqos->reset_delays[1]);
454
455 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
456 if (ret < 0)
457 return ret;
458
459 udelay(eqos->reset_delays[2]);
460 }
461
Jonas Karlman1b615702023-10-01 19:17:20 +0000462 if (data->ops->set_clock_selection)
463 data->ops->set_clock_selection(dev, true);
464
Jonas Karlman098ee4f2023-10-01 19:17:19 +0000465 tx_delay = dev_read_u32_default(dev, "tx_delay", 0x30);
466 rx_delay = dev_read_u32_default(dev, "rx_delay", 0x10);
467
468 switch (pdata->phy_interface) {
469 case PHY_INTERFACE_MODE_RGMII:
470 return data->ops->set_to_rgmii(dev, tx_delay, rx_delay);
471 case PHY_INTERFACE_MODE_RGMII_ID:
472 return data->ops->set_to_rgmii(dev, 0, 0);
473 case PHY_INTERFACE_MODE_RGMII_RXID:
474 return data->ops->set_to_rgmii(dev, tx_delay, 0);
475 case PHY_INTERFACE_MODE_RGMII_TXID:
476 return data->ops->set_to_rgmii(dev, 0, rx_delay);
477 case PHY_INTERFACE_MODE_RMII:
478 return data->ops->set_to_rmii(dev);
479 }
480
481 return -EINVAL;
482}
483
484static int eqos_set_tx_clk_speed_rk(struct udevice *dev)
485{
486 struct eth_pdata *pdata = dev_get_plat(dev);
487 struct rockchip_platform_data *data = pdata->priv_pdata;
488
489 return data->ops->set_gmac_speed(dev);
490}
491
492static ulong eqos_get_tick_clk_rate_rk(struct udevice *dev)
493{
494 struct eqos_priv *eqos = dev_get_priv(dev);
495
496 return clk_get_rate(&eqos->clk_master_bus);
497}
498
499static struct eqos_ops eqos_rockchip_ops = {
500 .eqos_inval_desc = eqos_inval_desc_generic,
501 .eqos_flush_desc = eqos_flush_desc_generic,
502 .eqos_inval_buffer = eqos_inval_buffer_generic,
503 .eqos_flush_buffer = eqos_flush_buffer_generic,
504 .eqos_probe_resources = eqos_probe_resources_rk,
505 .eqos_remove_resources = eqos_remove_resources_rk,
506 .eqos_stop_resets = eqos_stop_resets_rk,
507 .eqos_start_resets = eqos_start_resets_rk,
508 .eqos_stop_clks = eqos_stop_clks_rk,
509 .eqos_start_clks = eqos_start_clks_rk,
510 .eqos_calibrate_pads = eqos_null_ops,
511 .eqos_disable_calibration = eqos_null_ops,
512 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_rk,
513 .eqos_get_enetaddr = eqos_null_ops,
514 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_rk,
515};
516
517struct eqos_config eqos_rockchip_config = {
518 .reg_access_always_ok = false,
519 .mdio_wait = 10,
520 .swr_wait = 50,
521 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
522 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_100_150,
523 .axi_bus_width = EQOS_AXI_WIDTH_64,
524 .interface = dev_read_phy_mode,
525 .ops = &eqos_rockchip_ops,
526};