blob: f11033ce3b9b74f191ddbdf1f5bb90f37bd6e9d5 [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>
Andre Przywaraf20f9462020-07-06 01:40:34 +010032#include <wait_bit.h>
Simon Glassbcee8d62019-12-06 21:41:35 -070033#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +010034#include <asm-generic/gpio.h>
35#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +053036
Amit Singh Tomara29710c2016-07-06 17:59:44 +053037#define MDIO_CMD_MII_BUSY BIT(0)
38#define MDIO_CMD_MII_WRITE BIT(1)
39
40#define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
41#define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
42#define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
43#define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
44
45#define CONFIG_TX_DESCR_NUM 32
46#define CONFIG_RX_DESCR_NUM 32
Hans de Goede40694372016-07-27 17:31:17 +020047#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
48
49/*
50 * The datasheet says that each descriptor can transfers up to 4096 bytes
51 * But later, the register documentation reduces that value to 2048,
52 * using 2048 cause strange behaviours and even BSP driver use 2047
53 */
54#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomara29710c2016-07-06 17:59:44 +053055
56#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
57#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
58
59#define H3_EPHY_DEFAULT_VALUE 0x58000
60#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
61#define H3_EPHY_ADDR_SHIFT 20
62#define REG_PHY_ADDR_MASK GENMASK(4, 0)
63#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
64#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
65#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
66
67#define SC_RMII_EN BIT(13)
68#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
69#define SC_ETCS_MASK GENMASK(1, 0)
70#define SC_ETCS_EXT_GMII 0x1
71#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng9b16ede2018-11-23 00:37:48 +010072#define SC_ETXDC_MASK GENMASK(12, 10)
73#define SC_ETXDC_OFFSET 10
74#define SC_ERXDC_MASK GENMASK(9, 5)
75#define SC_ERXDC_OFFSET 5
Amit Singh Tomara29710c2016-07-06 17:59:44 +053076
77#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
78
79#define AHB_GATE_OFFSET_EPHY 0
80
Lothar Feltenc6a21d62018-07-13 10:45:27 +020081/* IO mux settings */
82#define SUN8I_IOMUX_H3 2
Lothar Feltene46d73f2018-07-13 10:45:28 +020083#define SUN8I_IOMUX_R40 5
Lothar Feltenc6a21d62018-07-13 10:45:27 +020084#define SUN8I_IOMUX 4
Amit Singh Tomara29710c2016-07-06 17:59:44 +053085
86/* H3/A64 EMAC Register's offset */
87#define EMAC_CTL0 0x00
88#define EMAC_CTL1 0x04
89#define EMAC_INT_STA 0x08
90#define EMAC_INT_EN 0x0c
91#define EMAC_TX_CTL0 0x10
92#define EMAC_TX_CTL1 0x14
93#define EMAC_TX_FLOW_CTL 0x1c
94#define EMAC_TX_DMA_DESC 0x20
95#define EMAC_RX_CTL0 0x24
96#define EMAC_RX_CTL1 0x28
97#define EMAC_RX_DMA_DESC 0x34
98#define EMAC_MII_CMD 0x48
99#define EMAC_MII_DATA 0x4c
100#define EMAC_ADDR0_HIGH 0x50
101#define EMAC_ADDR0_LOW 0x54
102#define EMAC_TX_DMA_STA 0xb0
103#define EMAC_TX_CUR_DESC 0xb4
104#define EMAC_TX_CUR_BUF 0xb8
105#define EMAC_RX_DMA_STA 0xc0
106#define EMAC_RX_CUR_DESC 0xc4
107
108DECLARE_GLOBAL_DATA_PTR;
109
110enum emac_variant {
111 A83T_EMAC = 1,
112 H3_EMAC,
113 A64_EMAC,
Lothar Feltene46d73f2018-07-13 10:45:28 +0200114 R40_GMAC,
Samuel Holland99ac8612020-05-07 18:10:51 -0500115 H6_EMAC,
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530116};
117
118struct emac_dma_desc {
119 u32 status;
120 u32 st;
121 u32 buf_addr;
122 u32 next;
123} __aligned(ARCH_DMA_MINALIGN);
124
125struct emac_eth_dev {
126 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
127 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
128 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
129 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
130
131 u32 interface;
132 u32 phyaddr;
133 u32 link;
134 u32 speed;
135 u32 duplex;
136 u32 phy_configured;
137 u32 tx_currdescnum;
138 u32 rx_currdescnum;
139 u32 addr;
140 u32 tx_slot;
141 bool use_internal_phy;
142
143 enum emac_variant variant;
144 void *mac_reg;
145 phys_addr_t sysctl_reg;
146 struct phy_device *phydev;
147 struct mii_dev *bus;
Jagan Tekid3a2c052019-02-28 00:26:58 +0530148 struct clk tx_clk;
Jagan Teki23484532019-02-28 00:27:00 +0530149 struct clk ephy_clk;
Jagan Tekid3a2c052019-02-28 00:26:58 +0530150 struct reset_ctl tx_rst;
Jagan Teki23484532019-02-28 00:27:00 +0530151 struct reset_ctl ephy_rst;
Simon Glassbcee8d62019-12-06 21:41:35 -0700152#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100153 struct gpio_desc reset_gpio;
154#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530155};
156
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100157
158struct sun8i_eth_pdata {
159 struct eth_pdata eth_pdata;
160 u32 reset_delays[3];
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100161 int tx_delay_ps;
162 int rx_delay_ps;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100163};
164
165
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530166static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
167{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100168 struct udevice *dev = bus->priv;
169 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywaraf20f9462020-07-06 01:40:34 +0100170 u32 mii_cmd;
171 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530172
Andre Przywaraf20f9462020-07-06 01:40:34 +0100173 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530174 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywaraf20f9462020-07-06 01:40:34 +0100175 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530176 MDIO_CMD_MII_PHY_ADDR_MASK;
177
Andre Przywaraf20f9462020-07-06 01:40:34 +0100178 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530179
Andre Przywaraf20f9462020-07-06 01:40:34 +0100180 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530181
Andre Przywaraf20f9462020-07-06 01:40:34 +0100182 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
183 MDIO_CMD_MII_BUSY, false,
184 CONFIG_MDIO_TIMEOUT, true);
185 if (ret < 0)
186 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530187
Andre Przywaraf20f9462020-07-06 01:40:34 +0100188 return readl(priv->mac_reg + EMAC_MII_DATA);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530189}
190
191static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
192 u16 val)
193{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100194 struct udevice *dev = bus->priv;
195 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywaraf20f9462020-07-06 01:40:34 +0100196 u32 mii_cmd;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530197
Andre Przywaraf20f9462020-07-06 01:40:34 +0100198 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530199 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywaraf20f9462020-07-06 01:40:34 +0100200 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530201 MDIO_CMD_MII_PHY_ADDR_MASK;
202
Andre Przywaraf20f9462020-07-06 01:40:34 +0100203 mii_cmd |= MDIO_CMD_MII_WRITE;
204 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530205
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530206 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywaraf20f9462020-07-06 01:40:34 +0100207 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530208
Andre Przywaraf20f9462020-07-06 01:40:34 +0100209 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
210 MDIO_CMD_MII_BUSY, false,
211 CONFIG_MDIO_TIMEOUT, true);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530212}
213
214static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
215{
216 u32 macid_lo, macid_hi;
217
218 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
219 (mac_id[3] << 24);
220 macid_hi = mac_id[4] + (mac_id[5] << 8);
221
222 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
223 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
224
225 return 0;
226}
227
228static void sun8i_adjust_link(struct emac_eth_dev *priv,
229 struct phy_device *phydev)
230{
231 u32 v;
232
233 v = readl(priv->mac_reg + EMAC_CTL0);
234
235 if (phydev->duplex)
236 v |= BIT(0);
237 else
238 v &= ~BIT(0);
239
240 v &= ~0x0C;
241
242 switch (phydev->speed) {
243 case 1000:
244 break;
245 case 100:
246 v |= BIT(2);
247 v |= BIT(3);
248 break;
249 case 10:
250 v |= BIT(3);
251 break;
252 }
253 writel(v, priv->mac_reg + EMAC_CTL0);
254}
255
256static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
257{
258 if (priv->use_internal_phy) {
259 /* H3 based SoC's that has an Internal 100MBit PHY
260 * needs to be configured and powered up before use
261 */
262 *reg &= ~H3_EPHY_DEFAULT_MASK;
263 *reg |= H3_EPHY_DEFAULT_VALUE;
264 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
265 *reg &= ~H3_EPHY_SHUTDOWN;
266 *reg |= H3_EPHY_SELECT;
267 } else
268 /* This is to select External Gigabit PHY on
269 * the boards with H3 SoC.
270 */
271 *reg &= ~H3_EPHY_SELECT;
272
273 return 0;
274}
275
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100276static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
277 struct emac_eth_dev *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530278{
279 int ret;
280 u32 reg;
281
Jagan Teki695f6042019-02-28 00:26:51 +0530282 if (priv->variant == R40_GMAC) {
283 /* Select RGMII for R40 */
284 reg = readl(priv->sysctl_reg + 0x164);
Samuel Hollandabdbefb2020-05-07 18:10:50 -0500285 reg |= SC_ETCS_INT_GMII |
286 SC_EPIT |
287 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530288
Jagan Teki695f6042019-02-28 00:26:51 +0530289 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200290 return 0;
Jagan Teki695f6042019-02-28 00:26:51 +0530291 }
292
293 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200294
Samuel Holland99ac8612020-05-07 18:10:51 -0500295 if (priv->variant == H3_EMAC || priv->variant == H6_EMAC) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530296 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
297 if (ret)
298 return ret;
299 }
300
301 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland99ac8612020-05-07 18:10:51 -0500302 if (priv->variant == H3_EMAC ||
303 priv->variant == A64_EMAC ||
304 priv->variant == H6_EMAC)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530305 reg &= ~SC_RMII_EN;
306
307 switch (priv->interface) {
308 case PHY_INTERFACE_MODE_MII:
309 /* default */
310 break;
311 case PHY_INTERFACE_MODE_RGMII:
312 reg |= SC_EPIT | SC_ETCS_INT_GMII;
313 break;
314 case PHY_INTERFACE_MODE_RMII:
315 if (priv->variant == H3_EMAC ||
Samuel Holland99ac8612020-05-07 18:10:51 -0500316 priv->variant == A64_EMAC ||
317 priv->variant == H6_EMAC) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530318 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
319 break;
320 }
321 /* RMII not supported on A83T */
322 default:
323 debug("%s: Invalid PHY interface\n", __func__);
324 return -EINVAL;
325 }
326
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100327 if (pdata->tx_delay_ps)
328 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
329 & SC_ETXDC_MASK;
330
331 if (pdata->rx_delay_ps)
332 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
333 & SC_ERXDC_MASK;
334
Andre Przywara12afd952018-04-04 01:31:16 +0100335 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530336
337 return 0;
338}
339
340static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
341{
342 struct phy_device *phydev;
343
344 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
345 if (!phydev)
346 return -ENODEV;
347
348 phy_connect_dev(phydev, dev);
349
350 priv->phydev = phydev;
351 phy_config(priv->phydev);
352
353 return 0;
354}
355
356static void rx_descs_init(struct emac_eth_dev *priv)
357{
358 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
359 char *rxbuffs = &priv->rxbuffer[0];
360 struct emac_dma_desc *desc_p;
361 u32 idx;
362
363 /* flush Rx buffers */
364 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
365 RX_TOTAL_BUFSIZE);
366
367 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
368 desc_p = &desc_table_p[idx];
369 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
370 ;
371 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Hans de Goede40694372016-07-27 17:31:17 +0200372 desc_p->st |= CONFIG_ETH_RXSIZE;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530373 desc_p->status = BIT(31);
374 }
375
376 /* Correcting the last pointer of the chain */
377 desc_p->next = (uintptr_t)&desc_table_p[0];
378
379 flush_dcache_range((uintptr_t)priv->rx_chain,
380 (uintptr_t)priv->rx_chain +
381 sizeof(priv->rx_chain));
382
383 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
384 priv->rx_currdescnum = 0;
385}
386
387static void tx_descs_init(struct emac_eth_dev *priv)
388{
389 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
390 char *txbuffs = &priv->txbuffer[0];
391 struct emac_dma_desc *desc_p;
392 u32 idx;
393
394 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
395 desc_p = &desc_table_p[idx];
396 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
397 ;
398 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Andre Przywarac35380c2020-07-06 01:40:33 +0100399 desc_p->status = 0;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530400 desc_p->st = 0;
401 }
402
403 /* Correcting the last pointer of the chain */
404 desc_p->next = (uintptr_t)&desc_table_p[0];
405
406 /* Flush all Tx buffer descriptors */
407 flush_dcache_range((uintptr_t)priv->tx_chain,
408 (uintptr_t)priv->tx_chain +
409 sizeof(priv->tx_chain));
410
411 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
412 priv->tx_currdescnum = 0;
413}
414
415static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
416{
417 u32 reg, v;
418 int timeout = 100;
Andre Przywara2808cf62020-07-06 01:40:32 +0100419 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530420
421 reg = readl((priv->mac_reg + EMAC_CTL1));
422
423 if (!(reg & 0x1)) {
424 /* Soft reset MAC */
425 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
426 do {
427 reg = readl(priv->mac_reg + EMAC_CTL1);
428 } while ((reg & 0x01) != 0 && (--timeout));
429 if (!timeout) {
430 printf("%s: Timeout\n", __func__);
431 return -1;
432 }
433 }
434
435 /* Rewrite mac address after reset */
436 _sun8i_write_hwaddr(priv, enetaddr);
437
438 v = readl(priv->mac_reg + EMAC_TX_CTL1);
439 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
440 v |= BIT(1);
441 writel(v, priv->mac_reg + EMAC_TX_CTL1);
442
443 v = readl(priv->mac_reg + EMAC_RX_CTL1);
444 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
445 * complete frame has been written to RX DMA FIFO
446 */
447 v |= BIT(1);
448 writel(v, priv->mac_reg + EMAC_RX_CTL1);
449
450 /* DMA */
451 writel(8 << 24, priv->mac_reg + EMAC_CTL1);
452
453 /* Initialize rx/tx descriptors */
454 rx_descs_init(priv);
455 tx_descs_init(priv);
456
457 /* PHY Start Up */
Andre Przywara2808cf62020-07-06 01:40:32 +0100458 ret = phy_startup(priv->phydev);
459 if (ret)
460 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530461
462 sun8i_adjust_link(priv, priv->phydev);
463
464 /* Start RX DMA */
465 v = readl(priv->mac_reg + EMAC_RX_CTL1);
466 v |= BIT(30);
467 writel(v, priv->mac_reg + EMAC_RX_CTL1);
468 /* Start TX DMA */
469 v = readl(priv->mac_reg + EMAC_TX_CTL1);
470 v |= BIT(30);
471 writel(v, priv->mac_reg + EMAC_TX_CTL1);
472
473 /* Enable RX/TX */
474 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
475 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
476
477 return 0;
478}
479
480static int parse_phy_pins(struct udevice *dev)
481{
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200482 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530483 int offset;
484 const char *pin_name;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100485 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530486
Simon Glasse160f7d2017-01-17 16:52:55 -0700487 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530488 "pinctrl-0");
489 if (offset < 0) {
490 printf("WARNING: emac: cannot find pinctrl-0 node\n");
491 return offset;
492 }
493
494 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywarac0341172018-04-04 01:31:15 +0100495 "drive-strength", ~0);
496 if (drive != ~0) {
497 if (drive <= 10)
498 drive = SUN4I_PINCTRL_10_MA;
499 else if (drive <= 20)
500 drive = SUN4I_PINCTRL_20_MA;
501 else if (drive <= 30)
502 drive = SUN4I_PINCTRL_30_MA;
503 else
504 drive = SUN4I_PINCTRL_40_MA;
Andre Przywarac0341172018-04-04 01:31:15 +0100505 }
506
507 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
508 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywarac0341172018-04-04 01:31:15 +0100509 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
510 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100511
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530512 for (i = 0; ; i++) {
513 int pin;
514
Simon Glassb02e4042016-10-02 17:59:28 -0600515 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100516 "pins", i, NULL);
517 if (!pin_name)
518 break;
Andre Przywarac0341172018-04-04 01:31:15 +0100519
520 pin = sunxi_name_to_gpio(pin_name);
521 if (pin < 0)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530522 continue;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530523
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200524 if (priv->variant == H3_EMAC)
525 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Samuel Holland99ac8612020-05-07 18:10:51 -0500526 else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC)
Lothar Feltene46d73f2018-07-13 10:45:28 +0200527 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200528 else
529 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
530
Andre Przywarac0341172018-04-04 01:31:15 +0100531 if (drive != ~0)
532 sunxi_gpio_set_drv(pin, drive);
533 if (pull != ~0)
534 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530535 }
536
537 if (!i) {
Andre Przywarac0341172018-04-04 01:31:15 +0100538 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530539 return -2;
540 }
541
542 return 0;
543}
544
545static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
546{
547 u32 status, desc_num = priv->rx_currdescnum;
548 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
549 int length = -EAGAIN;
550 int good_packet = 1;
551 uintptr_t desc_start = (uintptr_t)desc_p;
552 uintptr_t desc_end = desc_start +
553 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
554
555 ulong data_start = (uintptr_t)desc_p->buf_addr;
556 ulong data_end;
557
558 /* Invalidate entire buffer descriptor */
559 invalidate_dcache_range(desc_start, desc_end);
560
561 status = desc_p->status;
562
563 /* Check for DMA own bit */
564 if (!(status & BIT(31))) {
565 length = (desc_p->status >> 16) & 0x3FFF;
566
567 if (length < 0x40) {
568 good_packet = 0;
569 debug("RX: Bad Packet (runt)\n");
570 }
571
572 data_end = data_start + length;
573 /* Invalidate received data */
574 invalidate_dcache_range(rounddown(data_start,
575 ARCH_DMA_MINALIGN),
576 roundup(data_end,
577 ARCH_DMA_MINALIGN));
578 if (good_packet) {
Hans de Goede40694372016-07-27 17:31:17 +0200579 if (length > CONFIG_ETH_RXSIZE) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530580 printf("Received packet is too big (len=%d)\n",
581 length);
582 return -EMSGSIZE;
583 }
584 *packetp = (uchar *)(ulong)desc_p->buf_addr;
585 return length;
586 }
587 }
588
589 return length;
590}
591
592static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
593 int len)
594{
595 u32 v, desc_num = priv->tx_currdescnum;
596 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
597 uintptr_t desc_start = (uintptr_t)desc_p;
598 uintptr_t desc_end = desc_start +
599 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
600
601 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
602 uintptr_t data_end = data_start +
603 roundup(len, ARCH_DMA_MINALIGN);
604
605 /* Invalidate entire buffer descriptor */
606 invalidate_dcache_range(desc_start, desc_end);
607
608 desc_p->st = len;
609 /* Mandatory undocumented bit */
610 desc_p->st |= BIT(24);
611
612 memcpy((void *)data_start, packet, len);
613
614 /* Flush data to be sent */
615 flush_dcache_range(data_start, data_end);
616
617 /* frame end */
618 desc_p->st |= BIT(30);
619 desc_p->st |= BIT(31);
620
621 /*frame begin */
622 desc_p->st |= BIT(29);
623 desc_p->status = BIT(31);
624
625 /*Descriptors st and status field has changed, so FLUSH it */
626 flush_dcache_range(desc_start, desc_end);
627
628 /* Move to next Descriptor and wrap around */
629 if (++desc_num >= CONFIG_TX_DESCR_NUM)
630 desc_num = 0;
631 priv->tx_currdescnum = desc_num;
632
633 /* Start the DMA */
634 v = readl(priv->mac_reg + EMAC_TX_CTL1);
635 v |= BIT(31);/* mandatory */
636 v |= BIT(30);/* mandatory */
637 writel(v, priv->mac_reg + EMAC_TX_CTL1);
638
639 return 0;
640}
641
642static int sun8i_eth_write_hwaddr(struct udevice *dev)
643{
644 struct eth_pdata *pdata = dev_get_platdata(dev);
645 struct emac_eth_dev *priv = dev_get_priv(dev);
646
647 return _sun8i_write_hwaddr(priv, pdata->enetaddr);
648}
649
Sean Andersonef043692020-09-15 10:45:00 -0400650static int sun8i_emac_board_setup(struct udevice *dev,
651 struct emac_eth_dev *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530652{
Jagan Tekid3a2c052019-02-28 00:26:58 +0530653 int ret;
654
655 ret = clk_enable(&priv->tx_clk);
656 if (ret) {
657 dev_err(dev, "failed to enable TX clock\n");
658 return ret;
659 }
660
661 if (reset_valid(&priv->tx_rst)) {
662 ret = reset_deassert(&priv->tx_rst);
663 if (ret) {
664 dev_err(dev, "failed to deassert TX reset\n");
665 goto err_tx_clk;
666 }
667 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530668
Jagan Teki23484532019-02-28 00:27:00 +0530669 /* Only H3/H5 have clock controls for internal EPHY */
670 if (clk_valid(&priv->ephy_clk)) {
671 ret = clk_enable(&priv->ephy_clk);
672 if (ret) {
673 dev_err(dev, "failed to enable EPHY TX clock\n");
674 return ret;
675 }
676 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530677
Jagan Teki23484532019-02-28 00:27:00 +0530678 if (reset_valid(&priv->ephy_rst)) {
679 ret = reset_deassert(&priv->ephy_rst);
680 if (ret) {
681 dev_err(dev, "failed to deassert EPHY TX clock\n");
682 return ret;
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200683 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530684 }
685
Jagan Tekid3a2c052019-02-28 00:26:58 +0530686 return 0;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530687
Jagan Tekid3a2c052019-02-28 00:26:58 +0530688err_tx_clk:
689 clk_disable(&priv->tx_clk);
690 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530691}
692
Simon Glassbcee8d62019-12-06 21:41:35 -0700693#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100694static int sun8i_mdio_reset(struct mii_dev *bus)
695{
696 struct udevice *dev = bus->priv;
697 struct emac_eth_dev *priv = dev_get_priv(dev);
698 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
699 int ret;
700
701 if (!dm_gpio_is_valid(&priv->reset_gpio))
702 return 0;
703
704 /* reset the phy */
705 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
706 if (ret)
707 return ret;
708
709 udelay(pdata->reset_delays[0]);
710
711 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
712 if (ret)
713 return ret;
714
715 udelay(pdata->reset_delays[1]);
716
717 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
718 if (ret)
719 return ret;
720
721 udelay(pdata->reset_delays[2]);
722
723 return 0;
724}
725#endif
726
727static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530728{
729 struct mii_dev *bus = mdio_alloc();
730
731 if (!bus) {
732 debug("Failed to allocate MDIO bus\n");
733 return -ENOMEM;
734 }
735
736 bus->read = sun8i_mdio_read;
737 bus->write = sun8i_mdio_write;
738 snprintf(bus->name, sizeof(bus->name), name);
739 bus->priv = (void *)priv;
Simon Glassbcee8d62019-12-06 21:41:35 -0700740#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100741 bus->reset = sun8i_mdio_reset;
742#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530743
744 return mdio_register(bus);
745}
746
747static int sun8i_emac_eth_start(struct udevice *dev)
748{
749 struct eth_pdata *pdata = dev_get_platdata(dev);
750
751 return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
752}
753
754static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
755{
756 struct emac_eth_dev *priv = dev_get_priv(dev);
757
758 return _sun8i_emac_eth_send(priv, packet, length);
759}
760
761static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
762{
763 struct emac_eth_dev *priv = dev_get_priv(dev);
764
765 return _sun8i_eth_recv(priv, packetp);
766}
767
768static int _sun8i_free_pkt(struct emac_eth_dev *priv)
769{
770 u32 desc_num = priv->rx_currdescnum;
771 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
772 uintptr_t desc_start = (uintptr_t)desc_p;
773 uintptr_t desc_end = desc_start +
774 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
775
776 /* Make the current descriptor valid again */
777 desc_p->status |= BIT(31);
778
779 /* Flush Status field of descriptor */
780 flush_dcache_range(desc_start, desc_end);
781
782 /* Move to next desc and wrap-around condition. */
783 if (++desc_num >= CONFIG_RX_DESCR_NUM)
784 desc_num = 0;
785 priv->rx_currdescnum = desc_num;
786
787 return 0;
788}
789
790static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
791 int length)
792{
793 struct emac_eth_dev *priv = dev_get_priv(dev);
794
795 return _sun8i_free_pkt(priv);
796}
797
798static void sun8i_emac_eth_stop(struct udevice *dev)
799{
800 struct emac_eth_dev *priv = dev_get_priv(dev);
801
802 /* Stop Rx/Tx transmitter */
803 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
804 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
805
806 /* Stop TX DMA */
807 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
808
809 phy_shutdown(priv->phydev);
810}
811
812static int sun8i_emac_eth_probe(struct udevice *dev)
813{
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100814 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
815 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530816 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekid3a2c052019-02-28 00:26:58 +0530817 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530818
819 priv->mac_reg = (void *)pdata->iobase;
820
Sean Andersonef043692020-09-15 10:45:00 -0400821 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekid3a2c052019-02-28 00:26:58 +0530822 if (ret)
823 return ret;
824
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100825 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530826
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100827 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530828 priv->bus = miiphy_get_dev_by_name(dev->name);
829
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530830 return sun8i_phy_init(priv, dev);
831}
832
833static const struct eth_ops sun8i_emac_eth_ops = {
834 .start = sun8i_emac_eth_start,
835 .write_hwaddr = sun8i_eth_write_hwaddr,
836 .send = sun8i_emac_eth_send,
837 .recv = sun8i_emac_eth_recv,
838 .free_pkt = sun8i_eth_free_pkt,
839 .stop = sun8i_emac_eth_stop,
840};
841
Sean Andersonef043692020-09-15 10:45:00 -0400842static int sun8i_get_ephy_nodes(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki23484532019-02-28 00:27:00 +0530843{
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200844 int emac_node, ephy_node, ret, ephy_handle;
845
846 emac_node = fdt_path_offset(gd->fdt_blob,
847 "/soc/ethernet@1c30000");
848 if (emac_node < 0) {
849 debug("failed to get emac node\n");
850 return emac_node;
851 }
852 ephy_handle = fdtdec_lookup_phandle(gd->fdt_blob,
853 emac_node, "phy-handle");
Jagan Teki23484532019-02-28 00:27:00 +0530854
855 /* look for mdio-mux node for internal PHY node */
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200856 ephy_node = fdt_path_offset(gd->fdt_blob,
857 "/soc/ethernet@1c30000/mdio-mux/mdio@1/ethernet-phy@1");
858 if (ephy_node < 0) {
Jagan Teki23484532019-02-28 00:27:00 +0530859 debug("failed to get mdio-mux with internal PHY\n");
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200860 return ephy_node;
Jagan Teki23484532019-02-28 00:27:00 +0530861 }
862
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200863 /* This is not the phy we are looking for */
864 if (ephy_node != ephy_handle)
865 return 0;
866
867 ret = fdt_node_check_compatible(gd->fdt_blob, ephy_node,
Jagan Teki23484532019-02-28 00:27:00 +0530868 "allwinner,sun8i-h3-mdio-internal");
869 if (ret < 0) {
870 debug("failed to find mdio-internal node\n");
871 return ret;
872 }
873
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200874 ret = clk_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki23484532019-02-28 00:27:00 +0530875 &priv->ephy_clk);
876 if (ret) {
877 dev_err(dev, "failed to get EPHY TX clock\n");
878 return ret;
879 }
880
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200881 ret = reset_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki23484532019-02-28 00:27:00 +0530882 &priv->ephy_rst);
883 if (ret) {
884 dev_err(dev, "failed to get EPHY TX reset\n");
885 return ret;
886 }
887
888 priv->use_internal_phy = true;
889
890 return 0;
891}
892
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530893static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
894{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100895 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
896 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530897 struct emac_eth_dev *priv = dev_get_priv(dev);
898 const char *phy_mode;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100899 const fdt32_t *reg;
Simon Glasse160f7d2017-01-17 16:52:55 -0700900 int node = dev_of_offset(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530901 int offset = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -0700902#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100903 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100904#endif
Jagan Tekid3a2c052019-02-28 00:26:58 +0530905 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530906
Masahiro Yamada25484932020-07-17 14:36:48 +0900907 pdata->iobase = dev_read_addr(dev);
Andre Przywara12afd952018-04-04 01:31:16 +0100908 if (pdata->iobase == FDT_ADDR_T_NONE) {
909 debug("%s: Cannot find MAC base address\n", __func__);
910 return -EINVAL;
911 }
912
Lothar Feltene46d73f2018-07-13 10:45:28 +0200913 priv->variant = dev_get_driver_data(dev);
914
915 if (!priv->variant) {
916 printf("%s: Missing variant\n", __func__);
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100917 return -EINVAL;
Andre Przywara12afd952018-04-04 01:31:16 +0100918 }
Lothar Feltene46d73f2018-07-13 10:45:28 +0200919
Jagan Tekid3a2c052019-02-28 00:26:58 +0530920 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
921 if (ret) {
922 dev_err(dev, "failed to get TX clock\n");
923 return ret;
924 }
925
926 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
927 if (ret && ret != -ENOENT) {
928 dev_err(dev, "failed to get TX reset\n");
929 return ret;
930 }
931
Jagan Teki695f6042019-02-28 00:26:51 +0530932 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
933 if (offset < 0) {
934 debug("%s: cannot find syscon node\n", __func__);
935 return -EINVAL;
936 }
937
938 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
939 if (!reg) {
940 debug("%s: cannot find reg property in syscon node\n",
941 __func__);
942 return -EINVAL;
943 }
944 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
945 offset, reg);
946 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
947 debug("%s: Cannot find syscon base address\n", __func__);
948 return -EINVAL;
Andre Przywara12afd952018-04-04 01:31:16 +0100949 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530950
951 pdata->phy_interface = -1;
952 priv->phyaddr = -1;
953 priv->use_internal_phy = false;
954
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100955 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywara12afd952018-04-04 01:31:16 +0100956 if (offset < 0) {
957 debug("%s: Cannot find PHY address\n", __func__);
958 return -EINVAL;
959 }
960 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530961
Simon Glasse160f7d2017-01-17 16:52:55 -0700962 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530963
964 if (phy_mode)
965 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
966 printf("phy interface%d\n", pdata->phy_interface);
967
968 if (pdata->phy_interface == -1) {
969 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
970 return -EINVAL;
971 }
972
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530973 if (priv->variant == H3_EMAC) {
Sean Andersonef043692020-09-15 10:45:00 -0400974 ret = sun8i_get_ephy_nodes(dev, priv);
Jagan Teki23484532019-02-28 00:27:00 +0530975 if (ret)
976 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530977 }
978
979 priv->interface = pdata->phy_interface;
980
981 if (!priv->use_internal_phy)
982 parse_phy_pins(dev);
983
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100984 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
985 "allwinner,tx-delay-ps", 0);
986 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
987 printf("%s: Invalid TX delay value %d\n", __func__,
988 sun8i_pdata->tx_delay_ps);
989
990 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
991 "allwinner,rx-delay-ps", 0);
992 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
993 printf("%s: Invalid RX delay value %d\n", __func__,
994 sun8i_pdata->rx_delay_ps);
995
Simon Glassbcee8d62019-12-06 21:41:35 -0700996#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glassda409cc2017-05-17 17:18:09 -0600997 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100998 "snps,reset-active-low"))
999 reset_flags |= GPIOD_ACTIVE_LOW;
1000
1001 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
1002 &priv->reset_gpio, reset_flags);
1003
1004 if (ret == 0) {
Simon Glassda409cc2017-05-17 17:18:09 -06001005 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +01001006 "snps,reset-delays-us",
1007 sun8i_pdata->reset_delays, 3);
1008 } else if (ret == -ENOENT) {
1009 ret = 0;
1010 }
1011#endif
1012
Amit Singh Tomara29710c2016-07-06 17:59:44 +05301013 return 0;
1014}
1015
1016static const struct udevice_id sun8i_emac_eth_ids[] = {
1017 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
1018 {.compatible = "allwinner,sun50i-a64-emac",
1019 .data = (uintptr_t)A64_EMAC },
1020 {.compatible = "allwinner,sun8i-a83t-emac",
1021 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene46d73f2018-07-13 10:45:28 +02001022 {.compatible = "allwinner,sun8i-r40-gmac",
1023 .data = (uintptr_t)R40_GMAC },
Samuel Holland99ac8612020-05-07 18:10:51 -05001024 {.compatible = "allwinner,sun50i-h6-emac",
1025 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomara29710c2016-07-06 17:59:44 +05301026 { }
1027};
1028
1029U_BOOT_DRIVER(eth_sun8i_emac) = {
1030 .name = "eth_sun8i_emac",
1031 .id = UCLASS_ETH,
1032 .of_match = sun8i_emac_eth_ids,
1033 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
1034 .probe = sun8i_emac_eth_probe,
1035 .ops = &sun8i_emac_eth_ops,
1036 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +01001037 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomara29710c2016-07-06 17:59:44 +05301038 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1039};