blob: 1dae81c7bf8940b42b80f4aad47bd541fec94313 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Amit Singh Tomara29710c2016-07-06 17:59:44 +05302/*
3 * (C) Copyright 2016
4 * Author: Amit Singh Tomar, amittomer25@gmail.com
5 *
Amit Singh Tomara29710c2016-07-06 17:59:44 +05306 * Ethernet driver for H3/A64/A83T based SoC's
7 *
8 * It is derived from the work done by
9 * LABBE Corentin & Chen-Yu Tsai for Linux, THANKS!
10 *
11*/
12
Simon Glass1eb69ae2019-11-14 12:57:39 -070013#include <cpu_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060015#include <asm/cache.h>
Amit Singh Tomara29710c2016-07-06 17:59:44 +053016#include <asm/io.h>
17#include <asm/arch/clock.h>
18#include <asm/arch/gpio.h>
19#include <common.h>
Jagan Tekid3a2c052019-02-28 00:26:58 +053020#include <clk.h>
Amit Singh Tomara29710c2016-07-06 17:59:44 +053021#include <dm.h>
22#include <fdt_support.h>
Simon Glass336d4612020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060025#include <linux/delay.h>
Amit Singh Tomara29710c2016-07-06 17:59:44 +053026#include <linux/err.h>
27#include <malloc.h>
28#include <miiphy.h>
29#include <net.h>
Jagan Tekid3a2c052019-02-28 00:26:58 +053030#include <reset.h>
Andre Przywarac0341172018-04-04 01:31:15 +010031#include <dt-bindings/pinctrl/sun4i-a10.h>
Simon Glassbcee8d62019-12-06 21:41:35 -070032#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +010033#include <asm-generic/gpio.h>
34#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +053035
Amit Singh Tomara29710c2016-07-06 17:59:44 +053036#define MDIO_CMD_MII_BUSY BIT(0)
37#define MDIO_CMD_MII_WRITE BIT(1)
38
39#define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
40#define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
41#define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
42#define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
43
44#define CONFIG_TX_DESCR_NUM 32
45#define CONFIG_RX_DESCR_NUM 32
Hans de Goede40694372016-07-27 17:31:17 +020046#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
47
48/*
49 * The datasheet says that each descriptor can transfers up to 4096 bytes
50 * But later, the register documentation reduces that value to 2048,
51 * using 2048 cause strange behaviours and even BSP driver use 2047
52 */
53#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomara29710c2016-07-06 17:59:44 +053054
55#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
56#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
57
58#define H3_EPHY_DEFAULT_VALUE 0x58000
59#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
60#define H3_EPHY_ADDR_SHIFT 20
61#define REG_PHY_ADDR_MASK GENMASK(4, 0)
62#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
63#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
64#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
65
66#define SC_RMII_EN BIT(13)
67#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
68#define SC_ETCS_MASK GENMASK(1, 0)
69#define SC_ETCS_EXT_GMII 0x1
70#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng9b16ede2018-11-23 00:37:48 +010071#define SC_ETXDC_MASK GENMASK(12, 10)
72#define SC_ETXDC_OFFSET 10
73#define SC_ERXDC_MASK GENMASK(9, 5)
74#define SC_ERXDC_OFFSET 5
Amit Singh Tomara29710c2016-07-06 17:59:44 +053075
76#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
77
78#define AHB_GATE_OFFSET_EPHY 0
79
Lothar Feltenc6a21d62018-07-13 10:45:27 +020080/* IO mux settings */
81#define SUN8I_IOMUX_H3 2
Lothar Feltene46d73f2018-07-13 10:45:28 +020082#define SUN8I_IOMUX_R40 5
Lothar Feltenc6a21d62018-07-13 10:45:27 +020083#define SUN8I_IOMUX 4
Amit Singh Tomara29710c2016-07-06 17:59:44 +053084
85/* H3/A64 EMAC Register's offset */
86#define EMAC_CTL0 0x00
87#define EMAC_CTL1 0x04
88#define EMAC_INT_STA 0x08
89#define EMAC_INT_EN 0x0c
90#define EMAC_TX_CTL0 0x10
91#define EMAC_TX_CTL1 0x14
92#define EMAC_TX_FLOW_CTL 0x1c
93#define EMAC_TX_DMA_DESC 0x20
94#define EMAC_RX_CTL0 0x24
95#define EMAC_RX_CTL1 0x28
96#define EMAC_RX_DMA_DESC 0x34
97#define EMAC_MII_CMD 0x48
98#define EMAC_MII_DATA 0x4c
99#define EMAC_ADDR0_HIGH 0x50
100#define EMAC_ADDR0_LOW 0x54
101#define EMAC_TX_DMA_STA 0xb0
102#define EMAC_TX_CUR_DESC 0xb4
103#define EMAC_TX_CUR_BUF 0xb8
104#define EMAC_RX_DMA_STA 0xc0
105#define EMAC_RX_CUR_DESC 0xc4
106
107DECLARE_GLOBAL_DATA_PTR;
108
109enum emac_variant {
110 A83T_EMAC = 1,
111 H3_EMAC,
112 A64_EMAC,
Lothar Feltene46d73f2018-07-13 10:45:28 +0200113 R40_GMAC,
Samuel Holland99ac8612020-05-07 18:10:51 -0500114 H6_EMAC,
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530115};
116
117struct emac_dma_desc {
118 u32 status;
119 u32 st;
120 u32 buf_addr;
121 u32 next;
122} __aligned(ARCH_DMA_MINALIGN);
123
124struct emac_eth_dev {
125 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
126 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
127 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
128 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
129
130 u32 interface;
131 u32 phyaddr;
132 u32 link;
133 u32 speed;
134 u32 duplex;
135 u32 phy_configured;
136 u32 tx_currdescnum;
137 u32 rx_currdescnum;
138 u32 addr;
139 u32 tx_slot;
140 bool use_internal_phy;
141
142 enum emac_variant variant;
143 void *mac_reg;
144 phys_addr_t sysctl_reg;
145 struct phy_device *phydev;
146 struct mii_dev *bus;
Jagan Tekid3a2c052019-02-28 00:26:58 +0530147 struct clk tx_clk;
Jagan Teki23484532019-02-28 00:27:00 +0530148 struct clk ephy_clk;
Jagan Tekid3a2c052019-02-28 00:26:58 +0530149 struct reset_ctl tx_rst;
Jagan Teki23484532019-02-28 00:27:00 +0530150 struct reset_ctl ephy_rst;
Simon Glassbcee8d62019-12-06 21:41:35 -0700151#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100152 struct gpio_desc reset_gpio;
153#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530154};
155
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100156
157struct sun8i_eth_pdata {
158 struct eth_pdata eth_pdata;
159 u32 reset_delays[3];
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100160 int tx_delay_ps;
161 int rx_delay_ps;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100162};
163
164
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530165static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
166{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100167 struct udevice *dev = bus->priv;
168 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530169 ulong start;
170 u32 miiaddr = 0;
171 int timeout = CONFIG_MDIO_TIMEOUT;
172
173 miiaddr &= ~MDIO_CMD_MII_WRITE;
174 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
175 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
176 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
177
178 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
179
180 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
181 MDIO_CMD_MII_PHY_ADDR_MASK;
182
183 miiaddr |= MDIO_CMD_MII_BUSY;
184
185 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
186
187 start = get_timer(0);
188 while (get_timer(start) < timeout) {
189 if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY))
190 return readl(priv->mac_reg + EMAC_MII_DATA);
191 udelay(10);
192 };
193
194 return -1;
195}
196
197static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
198 u16 val)
199{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100200 struct udevice *dev = bus->priv;
201 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530202 ulong start;
203 u32 miiaddr = 0;
204 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
205
206 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
207 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
208 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
209
210 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
211 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
212 MDIO_CMD_MII_PHY_ADDR_MASK;
213
214 miiaddr |= MDIO_CMD_MII_WRITE;
215 miiaddr |= MDIO_CMD_MII_BUSY;
216
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530217 writel(val, priv->mac_reg + EMAC_MII_DATA);
Philipp Tomsich1deeecb2016-11-16 01:40:27 +0000218 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530219
220 start = get_timer(0);
221 while (get_timer(start) < timeout) {
222 if (!(readl(priv->mac_reg + EMAC_MII_CMD) &
223 MDIO_CMD_MII_BUSY)) {
224 ret = 0;
225 break;
226 }
227 udelay(10);
228 };
229
230 return ret;
231}
232
233static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
234{
235 u32 macid_lo, macid_hi;
236
237 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
238 (mac_id[3] << 24);
239 macid_hi = mac_id[4] + (mac_id[5] << 8);
240
241 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
242 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
243
244 return 0;
245}
246
247static void sun8i_adjust_link(struct emac_eth_dev *priv,
248 struct phy_device *phydev)
249{
250 u32 v;
251
252 v = readl(priv->mac_reg + EMAC_CTL0);
253
254 if (phydev->duplex)
255 v |= BIT(0);
256 else
257 v &= ~BIT(0);
258
259 v &= ~0x0C;
260
261 switch (phydev->speed) {
262 case 1000:
263 break;
264 case 100:
265 v |= BIT(2);
266 v |= BIT(3);
267 break;
268 case 10:
269 v |= BIT(3);
270 break;
271 }
272 writel(v, priv->mac_reg + EMAC_CTL0);
273}
274
275static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
276{
277 if (priv->use_internal_phy) {
278 /* H3 based SoC's that has an Internal 100MBit PHY
279 * needs to be configured and powered up before use
280 */
281 *reg &= ~H3_EPHY_DEFAULT_MASK;
282 *reg |= H3_EPHY_DEFAULT_VALUE;
283 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
284 *reg &= ~H3_EPHY_SHUTDOWN;
285 *reg |= H3_EPHY_SELECT;
286 } else
287 /* This is to select External Gigabit PHY on
288 * the boards with H3 SoC.
289 */
290 *reg &= ~H3_EPHY_SELECT;
291
292 return 0;
293}
294
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100295static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
296 struct emac_eth_dev *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530297{
298 int ret;
299 u32 reg;
300
Jagan Teki695f6042019-02-28 00:26:51 +0530301 if (priv->variant == R40_GMAC) {
302 /* Select RGMII for R40 */
303 reg = readl(priv->sysctl_reg + 0x164);
Samuel Hollandabdbefb2020-05-07 18:10:50 -0500304 reg |= SC_ETCS_INT_GMII |
305 SC_EPIT |
306 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530307
Jagan Teki695f6042019-02-28 00:26:51 +0530308 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200309 return 0;
Jagan Teki695f6042019-02-28 00:26:51 +0530310 }
311
312 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200313
Samuel Holland99ac8612020-05-07 18:10:51 -0500314 if (priv->variant == H3_EMAC || priv->variant == H6_EMAC) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530315 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
316 if (ret)
317 return ret;
318 }
319
320 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland99ac8612020-05-07 18:10:51 -0500321 if (priv->variant == H3_EMAC ||
322 priv->variant == A64_EMAC ||
323 priv->variant == H6_EMAC)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530324 reg &= ~SC_RMII_EN;
325
326 switch (priv->interface) {
327 case PHY_INTERFACE_MODE_MII:
328 /* default */
329 break;
330 case PHY_INTERFACE_MODE_RGMII:
331 reg |= SC_EPIT | SC_ETCS_INT_GMII;
332 break;
333 case PHY_INTERFACE_MODE_RMII:
334 if (priv->variant == H3_EMAC ||
Samuel Holland99ac8612020-05-07 18:10:51 -0500335 priv->variant == A64_EMAC ||
336 priv->variant == H6_EMAC) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530337 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
338 break;
339 }
340 /* RMII not supported on A83T */
341 default:
342 debug("%s: Invalid PHY interface\n", __func__);
343 return -EINVAL;
344 }
345
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100346 if (pdata->tx_delay_ps)
347 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
348 & SC_ETXDC_MASK;
349
350 if (pdata->rx_delay_ps)
351 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
352 & SC_ERXDC_MASK;
353
Andre Przywara12afd952018-04-04 01:31:16 +0100354 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530355
356 return 0;
357}
358
359static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
360{
361 struct phy_device *phydev;
362
363 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
364 if (!phydev)
365 return -ENODEV;
366
367 phy_connect_dev(phydev, dev);
368
369 priv->phydev = phydev;
370 phy_config(priv->phydev);
371
372 return 0;
373}
374
375static void rx_descs_init(struct emac_eth_dev *priv)
376{
377 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
378 char *rxbuffs = &priv->rxbuffer[0];
379 struct emac_dma_desc *desc_p;
380 u32 idx;
381
382 /* flush Rx buffers */
383 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
384 RX_TOTAL_BUFSIZE);
385
386 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
387 desc_p = &desc_table_p[idx];
388 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
389 ;
390 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Hans de Goede40694372016-07-27 17:31:17 +0200391 desc_p->st |= CONFIG_ETH_RXSIZE;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530392 desc_p->status = BIT(31);
393 }
394
395 /* Correcting the last pointer of the chain */
396 desc_p->next = (uintptr_t)&desc_table_p[0];
397
398 flush_dcache_range((uintptr_t)priv->rx_chain,
399 (uintptr_t)priv->rx_chain +
400 sizeof(priv->rx_chain));
401
402 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
403 priv->rx_currdescnum = 0;
404}
405
406static void tx_descs_init(struct emac_eth_dev *priv)
407{
408 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
409 char *txbuffs = &priv->txbuffer[0];
410 struct emac_dma_desc *desc_p;
411 u32 idx;
412
413 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
414 desc_p = &desc_table_p[idx];
415 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
416 ;
417 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
418 desc_p->status = (1 << 31);
419 desc_p->st = 0;
420 }
421
422 /* Correcting the last pointer of the chain */
423 desc_p->next = (uintptr_t)&desc_table_p[0];
424
425 /* Flush all Tx buffer descriptors */
426 flush_dcache_range((uintptr_t)priv->tx_chain,
427 (uintptr_t)priv->tx_chain +
428 sizeof(priv->tx_chain));
429
430 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
431 priv->tx_currdescnum = 0;
432}
433
434static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
435{
436 u32 reg, v;
437 int timeout = 100;
438
439 reg = readl((priv->mac_reg + EMAC_CTL1));
440
441 if (!(reg & 0x1)) {
442 /* Soft reset MAC */
443 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
444 do {
445 reg = readl(priv->mac_reg + EMAC_CTL1);
446 } while ((reg & 0x01) != 0 && (--timeout));
447 if (!timeout) {
448 printf("%s: Timeout\n", __func__);
449 return -1;
450 }
451 }
452
453 /* Rewrite mac address after reset */
454 _sun8i_write_hwaddr(priv, enetaddr);
455
456 v = readl(priv->mac_reg + EMAC_TX_CTL1);
457 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
458 v |= BIT(1);
459 writel(v, priv->mac_reg + EMAC_TX_CTL1);
460
461 v = readl(priv->mac_reg + EMAC_RX_CTL1);
462 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
463 * complete frame has been written to RX DMA FIFO
464 */
465 v |= BIT(1);
466 writel(v, priv->mac_reg + EMAC_RX_CTL1);
467
468 /* DMA */
469 writel(8 << 24, priv->mac_reg + EMAC_CTL1);
470
471 /* Initialize rx/tx descriptors */
472 rx_descs_init(priv);
473 tx_descs_init(priv);
474
475 /* PHY Start Up */
Samuel Holland2d530182018-01-27 23:53:20 -0600476 phy_startup(priv->phydev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530477
478 sun8i_adjust_link(priv, priv->phydev);
479
480 /* Start RX DMA */
481 v = readl(priv->mac_reg + EMAC_RX_CTL1);
482 v |= BIT(30);
483 writel(v, priv->mac_reg + EMAC_RX_CTL1);
484 /* Start TX DMA */
485 v = readl(priv->mac_reg + EMAC_TX_CTL1);
486 v |= BIT(30);
487 writel(v, priv->mac_reg + EMAC_TX_CTL1);
488
489 /* Enable RX/TX */
490 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
491 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
492
493 return 0;
494}
495
496static int parse_phy_pins(struct udevice *dev)
497{
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200498 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530499 int offset;
500 const char *pin_name;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100501 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530502
Simon Glasse160f7d2017-01-17 16:52:55 -0700503 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530504 "pinctrl-0");
505 if (offset < 0) {
506 printf("WARNING: emac: cannot find pinctrl-0 node\n");
507 return offset;
508 }
509
510 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywarac0341172018-04-04 01:31:15 +0100511 "drive-strength", ~0);
512 if (drive != ~0) {
513 if (drive <= 10)
514 drive = SUN4I_PINCTRL_10_MA;
515 else if (drive <= 20)
516 drive = SUN4I_PINCTRL_20_MA;
517 else if (drive <= 30)
518 drive = SUN4I_PINCTRL_30_MA;
519 else
520 drive = SUN4I_PINCTRL_40_MA;
Andre Przywarac0341172018-04-04 01:31:15 +0100521 }
522
523 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
524 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywarac0341172018-04-04 01:31:15 +0100525 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
526 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100527
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530528 for (i = 0; ; i++) {
529 int pin;
530
Simon Glassb02e4042016-10-02 17:59:28 -0600531 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100532 "pins", i, NULL);
533 if (!pin_name)
534 break;
Andre Przywarac0341172018-04-04 01:31:15 +0100535
536 pin = sunxi_name_to_gpio(pin_name);
537 if (pin < 0)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530538 continue;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530539
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200540 if (priv->variant == H3_EMAC)
541 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Samuel Holland99ac8612020-05-07 18:10:51 -0500542 else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC)
Lothar Feltene46d73f2018-07-13 10:45:28 +0200543 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200544 else
545 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
546
Andre Przywarac0341172018-04-04 01:31:15 +0100547 if (drive != ~0)
548 sunxi_gpio_set_drv(pin, drive);
549 if (pull != ~0)
550 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530551 }
552
553 if (!i) {
Andre Przywarac0341172018-04-04 01:31:15 +0100554 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530555 return -2;
556 }
557
558 return 0;
559}
560
561static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
562{
563 u32 status, desc_num = priv->rx_currdescnum;
564 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
565 int length = -EAGAIN;
566 int good_packet = 1;
567 uintptr_t desc_start = (uintptr_t)desc_p;
568 uintptr_t desc_end = desc_start +
569 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
570
571 ulong data_start = (uintptr_t)desc_p->buf_addr;
572 ulong data_end;
573
574 /* Invalidate entire buffer descriptor */
575 invalidate_dcache_range(desc_start, desc_end);
576
577 status = desc_p->status;
578
579 /* Check for DMA own bit */
580 if (!(status & BIT(31))) {
581 length = (desc_p->status >> 16) & 0x3FFF;
582
583 if (length < 0x40) {
584 good_packet = 0;
585 debug("RX: Bad Packet (runt)\n");
586 }
587
588 data_end = data_start + length;
589 /* Invalidate received data */
590 invalidate_dcache_range(rounddown(data_start,
591 ARCH_DMA_MINALIGN),
592 roundup(data_end,
593 ARCH_DMA_MINALIGN));
594 if (good_packet) {
Hans de Goede40694372016-07-27 17:31:17 +0200595 if (length > CONFIG_ETH_RXSIZE) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530596 printf("Received packet is too big (len=%d)\n",
597 length);
598 return -EMSGSIZE;
599 }
600 *packetp = (uchar *)(ulong)desc_p->buf_addr;
601 return length;
602 }
603 }
604
605 return length;
606}
607
608static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
609 int len)
610{
611 u32 v, desc_num = priv->tx_currdescnum;
612 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
613 uintptr_t desc_start = (uintptr_t)desc_p;
614 uintptr_t desc_end = desc_start +
615 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
616
617 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
618 uintptr_t data_end = data_start +
619 roundup(len, ARCH_DMA_MINALIGN);
620
621 /* Invalidate entire buffer descriptor */
622 invalidate_dcache_range(desc_start, desc_end);
623
624 desc_p->st = len;
625 /* Mandatory undocumented bit */
626 desc_p->st |= BIT(24);
627
628 memcpy((void *)data_start, packet, len);
629
630 /* Flush data to be sent */
631 flush_dcache_range(data_start, data_end);
632
633 /* frame end */
634 desc_p->st |= BIT(30);
635 desc_p->st |= BIT(31);
636
637 /*frame begin */
638 desc_p->st |= BIT(29);
639 desc_p->status = BIT(31);
640
641 /*Descriptors st and status field has changed, so FLUSH it */
642 flush_dcache_range(desc_start, desc_end);
643
644 /* Move to next Descriptor and wrap around */
645 if (++desc_num >= CONFIG_TX_DESCR_NUM)
646 desc_num = 0;
647 priv->tx_currdescnum = desc_num;
648
649 /* Start the DMA */
650 v = readl(priv->mac_reg + EMAC_TX_CTL1);
651 v |= BIT(31);/* mandatory */
652 v |= BIT(30);/* mandatory */
653 writel(v, priv->mac_reg + EMAC_TX_CTL1);
654
655 return 0;
656}
657
658static int sun8i_eth_write_hwaddr(struct udevice *dev)
659{
660 struct eth_pdata *pdata = dev_get_platdata(dev);
661 struct emac_eth_dev *priv = dev_get_priv(dev);
662
663 return _sun8i_write_hwaddr(priv, pdata->enetaddr);
664}
665
Sean Andersonef043692020-09-15 10:45:00 -0400666static int sun8i_emac_board_setup(struct udevice *dev,
667 struct emac_eth_dev *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530668{
Jagan Tekid3a2c052019-02-28 00:26:58 +0530669 int ret;
670
671 ret = clk_enable(&priv->tx_clk);
672 if (ret) {
673 dev_err(dev, "failed to enable TX clock\n");
674 return ret;
675 }
676
677 if (reset_valid(&priv->tx_rst)) {
678 ret = reset_deassert(&priv->tx_rst);
679 if (ret) {
680 dev_err(dev, "failed to deassert TX reset\n");
681 goto err_tx_clk;
682 }
683 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530684
Jagan Teki23484532019-02-28 00:27:00 +0530685 /* Only H3/H5 have clock controls for internal EPHY */
686 if (clk_valid(&priv->ephy_clk)) {
687 ret = clk_enable(&priv->ephy_clk);
688 if (ret) {
689 dev_err(dev, "failed to enable EPHY TX clock\n");
690 return ret;
691 }
692 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530693
Jagan Teki23484532019-02-28 00:27:00 +0530694 if (reset_valid(&priv->ephy_rst)) {
695 ret = reset_deassert(&priv->ephy_rst);
696 if (ret) {
697 dev_err(dev, "failed to deassert EPHY TX clock\n");
698 return ret;
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200699 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530700 }
701
Jagan Tekid3a2c052019-02-28 00:26:58 +0530702 return 0;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530703
Jagan Tekid3a2c052019-02-28 00:26:58 +0530704err_tx_clk:
705 clk_disable(&priv->tx_clk);
706 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530707}
708
Simon Glassbcee8d62019-12-06 21:41:35 -0700709#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100710static int sun8i_mdio_reset(struct mii_dev *bus)
711{
712 struct udevice *dev = bus->priv;
713 struct emac_eth_dev *priv = dev_get_priv(dev);
714 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
715 int ret;
716
717 if (!dm_gpio_is_valid(&priv->reset_gpio))
718 return 0;
719
720 /* reset the phy */
721 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
722 if (ret)
723 return ret;
724
725 udelay(pdata->reset_delays[0]);
726
727 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
728 if (ret)
729 return ret;
730
731 udelay(pdata->reset_delays[1]);
732
733 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
734 if (ret)
735 return ret;
736
737 udelay(pdata->reset_delays[2]);
738
739 return 0;
740}
741#endif
742
743static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530744{
745 struct mii_dev *bus = mdio_alloc();
746
747 if (!bus) {
748 debug("Failed to allocate MDIO bus\n");
749 return -ENOMEM;
750 }
751
752 bus->read = sun8i_mdio_read;
753 bus->write = sun8i_mdio_write;
754 snprintf(bus->name, sizeof(bus->name), name);
755 bus->priv = (void *)priv;
Simon Glassbcee8d62019-12-06 21:41:35 -0700756#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100757 bus->reset = sun8i_mdio_reset;
758#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530759
760 return mdio_register(bus);
761}
762
763static int sun8i_emac_eth_start(struct udevice *dev)
764{
765 struct eth_pdata *pdata = dev_get_platdata(dev);
766
767 return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
768}
769
770static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
771{
772 struct emac_eth_dev *priv = dev_get_priv(dev);
773
774 return _sun8i_emac_eth_send(priv, packet, length);
775}
776
777static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
778{
779 struct emac_eth_dev *priv = dev_get_priv(dev);
780
781 return _sun8i_eth_recv(priv, packetp);
782}
783
784static int _sun8i_free_pkt(struct emac_eth_dev *priv)
785{
786 u32 desc_num = priv->rx_currdescnum;
787 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
788 uintptr_t desc_start = (uintptr_t)desc_p;
789 uintptr_t desc_end = desc_start +
790 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
791
792 /* Make the current descriptor valid again */
793 desc_p->status |= BIT(31);
794
795 /* Flush Status field of descriptor */
796 flush_dcache_range(desc_start, desc_end);
797
798 /* Move to next desc and wrap-around condition. */
799 if (++desc_num >= CONFIG_RX_DESCR_NUM)
800 desc_num = 0;
801 priv->rx_currdescnum = desc_num;
802
803 return 0;
804}
805
806static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
807 int length)
808{
809 struct emac_eth_dev *priv = dev_get_priv(dev);
810
811 return _sun8i_free_pkt(priv);
812}
813
814static void sun8i_emac_eth_stop(struct udevice *dev)
815{
816 struct emac_eth_dev *priv = dev_get_priv(dev);
817
818 /* Stop Rx/Tx transmitter */
819 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
820 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
821
822 /* Stop TX DMA */
823 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
824
825 phy_shutdown(priv->phydev);
826}
827
828static int sun8i_emac_eth_probe(struct udevice *dev)
829{
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100830 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
831 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530832 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekid3a2c052019-02-28 00:26:58 +0530833 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530834
835 priv->mac_reg = (void *)pdata->iobase;
836
Sean Andersonef043692020-09-15 10:45:00 -0400837 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekid3a2c052019-02-28 00:26:58 +0530838 if (ret)
839 return ret;
840
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100841 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530842
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100843 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530844 priv->bus = miiphy_get_dev_by_name(dev->name);
845
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530846 return sun8i_phy_init(priv, dev);
847}
848
849static const struct eth_ops sun8i_emac_eth_ops = {
850 .start = sun8i_emac_eth_start,
851 .write_hwaddr = sun8i_eth_write_hwaddr,
852 .send = sun8i_emac_eth_send,
853 .recv = sun8i_emac_eth_recv,
854 .free_pkt = sun8i_eth_free_pkt,
855 .stop = sun8i_emac_eth_stop,
856};
857
Sean Andersonef043692020-09-15 10:45:00 -0400858static int sun8i_get_ephy_nodes(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki23484532019-02-28 00:27:00 +0530859{
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200860 int emac_node, ephy_node, ret, ephy_handle;
861
862 emac_node = fdt_path_offset(gd->fdt_blob,
863 "/soc/ethernet@1c30000");
864 if (emac_node < 0) {
865 debug("failed to get emac node\n");
866 return emac_node;
867 }
868 ephy_handle = fdtdec_lookup_phandle(gd->fdt_blob,
869 emac_node, "phy-handle");
Jagan Teki23484532019-02-28 00:27:00 +0530870
871 /* look for mdio-mux node for internal PHY node */
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200872 ephy_node = fdt_path_offset(gd->fdt_blob,
873 "/soc/ethernet@1c30000/mdio-mux/mdio@1/ethernet-phy@1");
874 if (ephy_node < 0) {
Jagan Teki23484532019-02-28 00:27:00 +0530875 debug("failed to get mdio-mux with internal PHY\n");
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200876 return ephy_node;
Jagan Teki23484532019-02-28 00:27:00 +0530877 }
878
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200879 /* This is not the phy we are looking for */
880 if (ephy_node != ephy_handle)
881 return 0;
882
883 ret = fdt_node_check_compatible(gd->fdt_blob, ephy_node,
Jagan Teki23484532019-02-28 00:27:00 +0530884 "allwinner,sun8i-h3-mdio-internal");
885 if (ret < 0) {
886 debug("failed to find mdio-internal node\n");
887 return ret;
888 }
889
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200890 ret = clk_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki23484532019-02-28 00:27:00 +0530891 &priv->ephy_clk);
892 if (ret) {
893 dev_err(dev, "failed to get EPHY TX clock\n");
894 return ret;
895 }
896
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200897 ret = reset_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki23484532019-02-28 00:27:00 +0530898 &priv->ephy_rst);
899 if (ret) {
900 dev_err(dev, "failed to get EPHY TX reset\n");
901 return ret;
902 }
903
904 priv->use_internal_phy = true;
905
906 return 0;
907}
908
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530909static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
910{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100911 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
912 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530913 struct emac_eth_dev *priv = dev_get_priv(dev);
914 const char *phy_mode;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100915 const fdt32_t *reg;
Simon Glasse160f7d2017-01-17 16:52:55 -0700916 int node = dev_of_offset(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530917 int offset = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -0700918#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100919 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100920#endif
Jagan Tekid3a2c052019-02-28 00:26:58 +0530921 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530922
Masahiro Yamada25484932020-07-17 14:36:48 +0900923 pdata->iobase = dev_read_addr(dev);
Andre Przywara12afd952018-04-04 01:31:16 +0100924 if (pdata->iobase == FDT_ADDR_T_NONE) {
925 debug("%s: Cannot find MAC base address\n", __func__);
926 return -EINVAL;
927 }
928
Lothar Feltene46d73f2018-07-13 10:45:28 +0200929 priv->variant = dev_get_driver_data(dev);
930
931 if (!priv->variant) {
932 printf("%s: Missing variant\n", __func__);
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100933 return -EINVAL;
Andre Przywara12afd952018-04-04 01:31:16 +0100934 }
Lothar Feltene46d73f2018-07-13 10:45:28 +0200935
Jagan Tekid3a2c052019-02-28 00:26:58 +0530936 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
937 if (ret) {
938 dev_err(dev, "failed to get TX clock\n");
939 return ret;
940 }
941
942 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
943 if (ret && ret != -ENOENT) {
944 dev_err(dev, "failed to get TX reset\n");
945 return ret;
946 }
947
Jagan Teki695f6042019-02-28 00:26:51 +0530948 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
949 if (offset < 0) {
950 debug("%s: cannot find syscon node\n", __func__);
951 return -EINVAL;
952 }
953
954 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
955 if (!reg) {
956 debug("%s: cannot find reg property in syscon node\n",
957 __func__);
958 return -EINVAL;
959 }
960 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
961 offset, reg);
962 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
963 debug("%s: Cannot find syscon base address\n", __func__);
964 return -EINVAL;
Andre Przywara12afd952018-04-04 01:31:16 +0100965 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530966
967 pdata->phy_interface = -1;
968 priv->phyaddr = -1;
969 priv->use_internal_phy = false;
970
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100971 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywara12afd952018-04-04 01:31:16 +0100972 if (offset < 0) {
973 debug("%s: Cannot find PHY address\n", __func__);
974 return -EINVAL;
975 }
976 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530977
Simon Glasse160f7d2017-01-17 16:52:55 -0700978 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530979
980 if (phy_mode)
981 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
982 printf("phy interface%d\n", pdata->phy_interface);
983
984 if (pdata->phy_interface == -1) {
985 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
986 return -EINVAL;
987 }
988
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530989 if (priv->variant == H3_EMAC) {
Sean Andersonef043692020-09-15 10:45:00 -0400990 ret = sun8i_get_ephy_nodes(dev, priv);
Jagan Teki23484532019-02-28 00:27:00 +0530991 if (ret)
992 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530993 }
994
995 priv->interface = pdata->phy_interface;
996
997 if (!priv->use_internal_phy)
998 parse_phy_pins(dev);
999
Icenowy Zheng9b16ede2018-11-23 00:37:48 +01001000 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
1001 "allwinner,tx-delay-ps", 0);
1002 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
1003 printf("%s: Invalid TX delay value %d\n", __func__,
1004 sun8i_pdata->tx_delay_ps);
1005
1006 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
1007 "allwinner,rx-delay-ps", 0);
1008 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
1009 printf("%s: Invalid RX delay value %d\n", __func__,
1010 sun8i_pdata->rx_delay_ps);
1011
Simon Glassbcee8d62019-12-06 21:41:35 -07001012#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glassda409cc2017-05-17 17:18:09 -06001013 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +01001014 "snps,reset-active-low"))
1015 reset_flags |= GPIOD_ACTIVE_LOW;
1016
1017 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
1018 &priv->reset_gpio, reset_flags);
1019
1020 if (ret == 0) {
Simon Glassda409cc2017-05-17 17:18:09 -06001021 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +01001022 "snps,reset-delays-us",
1023 sun8i_pdata->reset_delays, 3);
1024 } else if (ret == -ENOENT) {
1025 ret = 0;
1026 }
1027#endif
1028
Amit Singh Tomara29710c2016-07-06 17:59:44 +05301029 return 0;
1030}
1031
1032static const struct udevice_id sun8i_emac_eth_ids[] = {
1033 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
1034 {.compatible = "allwinner,sun50i-a64-emac",
1035 .data = (uintptr_t)A64_EMAC },
1036 {.compatible = "allwinner,sun8i-a83t-emac",
1037 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene46d73f2018-07-13 10:45:28 +02001038 {.compatible = "allwinner,sun8i-r40-gmac",
1039 .data = (uintptr_t)R40_GMAC },
Samuel Holland99ac8612020-05-07 18:10:51 -05001040 {.compatible = "allwinner,sun50i-h6-emac",
1041 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomara29710c2016-07-06 17:59:44 +05301042 { }
1043};
1044
1045U_BOOT_DRIVER(eth_sun8i_emac) = {
1046 .name = "eth_sun8i_emac",
1047 .id = UCLASS_ETH,
1048 .of_match = sun8i_emac_eth_ids,
1049 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
1050 .probe = sun8i_emac_eth_probe,
1051 .ops = &sun8i_emac_eth_ops,
1052 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +01001053 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomara29710c2016-07-06 17:59:44 +05301054 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1055};