blob: 5a19a44d067d614488c3b796fafffd1e766f9139 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05302/*
3 * (C) Copyright 2016
4 * Author: Amit Singh Tomar, amittomer25@gmail.com
5 *
Amit Singh Tomard194c0e2016-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 Glass63334482019-11-14 12:57:39 -070013#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <asm/cache.h>
Amit Singh Tomard194c0e2016-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 Tekicb63d282019-02-28 00:26:58 +053020#include <clk.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053021#include <dm.h>
22#include <fdt_support.h>
Simon Glass9bc15642020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060025#include <linux/delay.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053026#include <linux/err.h>
27#include <malloc.h>
28#include <miiphy.h>
29#include <net.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053030#include <reset.h>
Andre Przywara26e549b2018-04-04 01:31:15 +010031#include <dt-bindings/pinctrl/sun4i-a10.h>
Andre Przywara0dd619b2020-07-06 01:40:34 +010032#include <wait_bit.h>
Simon Glassfa4689a2019-12-06 21:41:35 -070033#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +010034#include <asm-generic/gpio.h>
35#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053036
Amit Singh Tomard194c0e2016-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 Goedefcdb3b32016-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 Tomard194c0e2016-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 Zheng525dc442018-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 Tomard194c0e2016-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 Feltenacb9a5b2018-07-13 10:45:27 +020081/* IO mux settings */
82#define SUN8I_IOMUX_H3 2
Lothar Feltene8cbced2018-07-13 10:45:28 +020083#define SUN8I_IOMUX_R40 5
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020084#define SUN8I_IOMUX 4
Amit Singh Tomard194c0e2016-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 Feltene8cbced2018-07-13 10:45:28 +0200114 R40_GMAC,
Samuel Holland3386e9a2020-05-07 18:10:51 -0500115 H6_EMAC,
Amit Singh Tomard194c0e2016-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 Tekicb63d282019-02-28 00:26:58 +0530148 struct clk tx_clk;
Jagan Teki727ed792019-02-28 00:27:00 +0530149 struct clk ephy_clk;
Jagan Tekicb63d282019-02-28 00:26:58 +0530150 struct reset_ctl tx_rst;
Jagan Teki727ed792019-02-28 00:27:00 +0530151 struct reset_ctl ephy_rst;
Simon Glassfa4689a2019-12-06 21:41:35 -0700152#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100153 struct gpio_desc reset_gpio;
154#endif
155};
156
157
158struct sun8i_eth_pdata {
159 struct eth_pdata eth_pdata;
160 u32 reset_delays[3];
Icenowy Zheng525dc442018-11-23 00:37:48 +0100161 int tx_delay_ps;
162 int rx_delay_ps;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530163};
164
Philipp Tomsich3297b552017-02-22 19:46:41 +0100165
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530166static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
167{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100168 struct udevice *dev = bus->priv;
169 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100170 u32 mii_cmd;
171 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530172
Andre Przywara0dd619b2020-07-06 01:40:34 +0100173 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530174 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100175 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530176 MDIO_CMD_MII_PHY_ADDR_MASK;
177
Andre Przywara0dd619b2020-07-06 01:40:34 +0100178 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530179
Andre Przywara0dd619b2020-07-06 01:40:34 +0100180 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530181
Andre Przywara0dd619b2020-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 Tomard194c0e2016-07-06 17:59:44 +0530187
Andre Przywara0dd619b2020-07-06 01:40:34 +0100188 return readl(priv->mac_reg + EMAC_MII_DATA);
Amit Singh Tomard194c0e2016-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 Tomsich3297b552017-02-22 19:46:41 +0100194 struct udevice *dev = bus->priv;
195 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100196 u32 mii_cmd;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530197
Andre Przywara0dd619b2020-07-06 01:40:34 +0100198 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530199 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100200 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530201 MDIO_CMD_MII_PHY_ADDR_MASK;
202
Andre Przywara0dd619b2020-07-06 01:40:34 +0100203 mii_cmd |= MDIO_CMD_MII_WRITE;
204 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530205
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530206 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100207 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530208
Andre Przywara0dd619b2020-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 Tomard194c0e2016-07-06 17:59:44 +0530212}
213
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530214static int sun8i_eth_write_hwaddr(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530215{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530216 struct emac_eth_dev *priv = dev_get_priv(dev);
217 struct eth_pdata *pdata = dev_get_platdata(dev);
218 uchar *mac_id = pdata->enetaddr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530219 u32 macid_lo, macid_hi;
220
221 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
222 (mac_id[3] << 24);
223 macid_hi = mac_id[4] + (mac_id[5] << 8);
224
225 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
226 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
227
228 return 0;
229}
230
231static void sun8i_adjust_link(struct emac_eth_dev *priv,
232 struct phy_device *phydev)
233{
234 u32 v;
235
236 v = readl(priv->mac_reg + EMAC_CTL0);
237
238 if (phydev->duplex)
239 v |= BIT(0);
240 else
241 v &= ~BIT(0);
242
243 v &= ~0x0C;
244
245 switch (phydev->speed) {
246 case 1000:
247 break;
248 case 100:
249 v |= BIT(2);
250 v |= BIT(3);
251 break;
252 case 10:
253 v |= BIT(3);
254 break;
255 }
256 writel(v, priv->mac_reg + EMAC_CTL0);
257}
258
259static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
260{
261 if (priv->use_internal_phy) {
262 /* H3 based SoC's that has an Internal 100MBit PHY
263 * needs to be configured and powered up before use
264 */
265 *reg &= ~H3_EPHY_DEFAULT_MASK;
266 *reg |= H3_EPHY_DEFAULT_VALUE;
267 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
268 *reg &= ~H3_EPHY_SHUTDOWN;
269 *reg |= H3_EPHY_SELECT;
270 } else
271 /* This is to select External Gigabit PHY on
272 * the boards with H3 SoC.
273 */
274 *reg &= ~H3_EPHY_SELECT;
275
276 return 0;
277}
278
Icenowy Zheng525dc442018-11-23 00:37:48 +0100279static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
280 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530281{
282 int ret;
283 u32 reg;
284
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530285 if (priv->variant == R40_GMAC) {
286 /* Select RGMII for R40 */
287 reg = readl(priv->sysctl_reg + 0x164);
Samuel Holland97f2cf12020-05-07 18:10:50 -0500288 reg |= SC_ETCS_INT_GMII |
289 SC_EPIT |
290 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530291
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530292 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200293 return 0;
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530294 }
295
296 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200297
Samuel Holland3386e9a2020-05-07 18:10:51 -0500298 if (priv->variant == H3_EMAC || priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530299 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
300 if (ret)
301 return ret;
302 }
303
304 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500305 if (priv->variant == H3_EMAC ||
306 priv->variant == A64_EMAC ||
307 priv->variant == H6_EMAC)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530308 reg &= ~SC_RMII_EN;
309
310 switch (priv->interface) {
311 case PHY_INTERFACE_MODE_MII:
312 /* default */
313 break;
314 case PHY_INTERFACE_MODE_RGMII:
315 reg |= SC_EPIT | SC_ETCS_INT_GMII;
316 break;
317 case PHY_INTERFACE_MODE_RMII:
318 if (priv->variant == H3_EMAC ||
Samuel Holland3386e9a2020-05-07 18:10:51 -0500319 priv->variant == A64_EMAC ||
320 priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530321 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
322 break;
323 }
324 /* RMII not supported on A83T */
325 default:
326 debug("%s: Invalid PHY interface\n", __func__);
327 return -EINVAL;
328 }
329
Icenowy Zheng525dc442018-11-23 00:37:48 +0100330 if (pdata->tx_delay_ps)
331 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
332 & SC_ETXDC_MASK;
333
334 if (pdata->rx_delay_ps)
335 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
336 & SC_ERXDC_MASK;
337
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100338 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530339
340 return 0;
341}
342
343static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
344{
345 struct phy_device *phydev;
346
347 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
348 if (!phydev)
349 return -ENODEV;
350
351 phy_connect_dev(phydev, dev);
352
353 priv->phydev = phydev;
354 phy_config(priv->phydev);
355
356 return 0;
357}
358
359static void rx_descs_init(struct emac_eth_dev *priv)
360{
361 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
362 char *rxbuffs = &priv->rxbuffer[0];
363 struct emac_dma_desc *desc_p;
364 u32 idx;
365
366 /* flush Rx buffers */
367 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
368 RX_TOTAL_BUFSIZE);
369
370 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
371 desc_p = &desc_table_p[idx];
372 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
373 ;
374 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Hans de Goedefcdb3b32016-07-27 17:31:17 +0200375 desc_p->st |= CONFIG_ETH_RXSIZE;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530376 desc_p->status = BIT(31);
377 }
378
379 /* Correcting the last pointer of the chain */
380 desc_p->next = (uintptr_t)&desc_table_p[0];
381
382 flush_dcache_range((uintptr_t)priv->rx_chain,
383 (uintptr_t)priv->rx_chain +
384 sizeof(priv->rx_chain));
385
386 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
387 priv->rx_currdescnum = 0;
388}
389
390static void tx_descs_init(struct emac_eth_dev *priv)
391{
392 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
393 char *txbuffs = &priv->txbuffer[0];
394 struct emac_dma_desc *desc_p;
395 u32 idx;
396
397 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
398 desc_p = &desc_table_p[idx];
399 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
400 ;
401 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Andre Przywaradf6f2712020-07-06 01:40:33 +0100402 desc_p->status = 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530403 desc_p->st = 0;
404 }
405
406 /* Correcting the last pointer of the chain */
407 desc_p->next = (uintptr_t)&desc_table_p[0];
408
409 /* Flush all Tx buffer descriptors */
410 flush_dcache_range((uintptr_t)priv->tx_chain,
411 (uintptr_t)priv->tx_chain +
412 sizeof(priv->tx_chain));
413
414 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
415 priv->tx_currdescnum = 0;
416}
417
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530418static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530419{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530420 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530421 u32 reg, v;
422 int timeout = 100;
Andre Przywara874145f2020-07-06 01:40:32 +0100423 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530424
425 reg = readl((priv->mac_reg + EMAC_CTL1));
426
427 if (!(reg & 0x1)) {
428 /* Soft reset MAC */
429 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
430 do {
431 reg = readl(priv->mac_reg + EMAC_CTL1);
432 } while ((reg & 0x01) != 0 && (--timeout));
433 if (!timeout) {
434 printf("%s: Timeout\n", __func__);
435 return -1;
436 }
437 }
438
439 /* Rewrite mac address after reset */
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530440 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530441
442 v = readl(priv->mac_reg + EMAC_TX_CTL1);
443 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
444 v |= BIT(1);
445 writel(v, priv->mac_reg + EMAC_TX_CTL1);
446
447 v = readl(priv->mac_reg + EMAC_RX_CTL1);
448 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
449 * complete frame has been written to RX DMA FIFO
450 */
451 v |= BIT(1);
452 writel(v, priv->mac_reg + EMAC_RX_CTL1);
453
454 /* DMA */
455 writel(8 << 24, priv->mac_reg + EMAC_CTL1);
456
457 /* Initialize rx/tx descriptors */
458 rx_descs_init(priv);
459 tx_descs_init(priv);
460
461 /* PHY Start Up */
Andre Przywara874145f2020-07-06 01:40:32 +0100462 ret = phy_startup(priv->phydev);
463 if (ret)
464 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530465
466 sun8i_adjust_link(priv, priv->phydev);
467
468 /* Start RX DMA */
469 v = readl(priv->mac_reg + EMAC_RX_CTL1);
470 v |= BIT(30);
471 writel(v, priv->mac_reg + EMAC_RX_CTL1);
472 /* Start TX DMA */
473 v = readl(priv->mac_reg + EMAC_TX_CTL1);
474 v |= BIT(30);
475 writel(v, priv->mac_reg + EMAC_TX_CTL1);
476
477 /* Enable RX/TX */
478 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
479 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
480
481 return 0;
482}
483
484static int parse_phy_pins(struct udevice *dev)
485{
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200486 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530487 int offset;
488 const char *pin_name;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100489 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530490
Simon Glassdd79d6e2017-01-17 16:52:55 -0700491 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530492 "pinctrl-0");
493 if (offset < 0) {
494 printf("WARNING: emac: cannot find pinctrl-0 node\n");
495 return offset;
496 }
497
498 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywara26e549b2018-04-04 01:31:15 +0100499 "drive-strength", ~0);
500 if (drive != ~0) {
501 if (drive <= 10)
502 drive = SUN4I_PINCTRL_10_MA;
503 else if (drive <= 20)
504 drive = SUN4I_PINCTRL_20_MA;
505 else if (drive <= 30)
506 drive = SUN4I_PINCTRL_30_MA;
507 else
508 drive = SUN4I_PINCTRL_40_MA;
Andre Przywara26e549b2018-04-04 01:31:15 +0100509 }
510
511 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
512 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywara26e549b2018-04-04 01:31:15 +0100513 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
514 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100515
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530516 for (i = 0; ; i++) {
517 int pin;
518
Simon Glassb0ea7402016-10-02 17:59:28 -0600519 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100520 "pins", i, NULL);
521 if (!pin_name)
522 break;
Andre Przywara26e549b2018-04-04 01:31:15 +0100523
524 pin = sunxi_name_to_gpio(pin_name);
525 if (pin < 0)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530526 continue;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530527
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200528 if (priv->variant == H3_EMAC)
529 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500530 else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC)
Lothar Feltene8cbced2018-07-13 10:45:28 +0200531 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200532 else
533 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
534
Andre Przywara26e549b2018-04-04 01:31:15 +0100535 if (drive != ~0)
536 sunxi_gpio_set_drv(pin, drive);
537 if (pull != ~0)
538 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530539 }
540
541 if (!i) {
Andre Przywara26e549b2018-04-04 01:31:15 +0100542 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530543 return -2;
544 }
545
546 return 0;
547}
548
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530549static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530550{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530551 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530552 u32 status, desc_num = priv->rx_currdescnum;
553 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
554 int length = -EAGAIN;
555 int good_packet = 1;
556 uintptr_t desc_start = (uintptr_t)desc_p;
557 uintptr_t desc_end = desc_start +
558 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
559
560 ulong data_start = (uintptr_t)desc_p->buf_addr;
561 ulong data_end;
562
563 /* Invalidate entire buffer descriptor */
564 invalidate_dcache_range(desc_start, desc_end);
565
566 status = desc_p->status;
567
568 /* Check for DMA own bit */
569 if (!(status & BIT(31))) {
570 length = (desc_p->status >> 16) & 0x3FFF;
571
572 if (length < 0x40) {
573 good_packet = 0;
574 debug("RX: Bad Packet (runt)\n");
575 }
576
577 data_end = data_start + length;
578 /* Invalidate received data */
579 invalidate_dcache_range(rounddown(data_start,
580 ARCH_DMA_MINALIGN),
581 roundup(data_end,
582 ARCH_DMA_MINALIGN));
583 if (good_packet) {
Hans de Goedefcdb3b32016-07-27 17:31:17 +0200584 if (length > CONFIG_ETH_RXSIZE) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530585 printf("Received packet is too big (len=%d)\n",
586 length);
587 return -EMSGSIZE;
588 }
589 *packetp = (uchar *)(ulong)desc_p->buf_addr;
590 return length;
591 }
592 }
593
594 return length;
595}
596
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530597static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530598{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530599 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530600 u32 v, desc_num = priv->tx_currdescnum;
601 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
602 uintptr_t desc_start = (uintptr_t)desc_p;
603 uintptr_t desc_end = desc_start +
604 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
605
606 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
607 uintptr_t data_end = data_start +
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530608 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530609
610 /* Invalidate entire buffer descriptor */
611 invalidate_dcache_range(desc_start, desc_end);
612
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530613 desc_p->st = length;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530614 /* Mandatory undocumented bit */
615 desc_p->st |= BIT(24);
616
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530617 memcpy((void *)data_start, packet, length);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530618
619 /* Flush data to be sent */
620 flush_dcache_range(data_start, data_end);
621
622 /* frame end */
623 desc_p->st |= BIT(30);
624 desc_p->st |= BIT(31);
625
626 /*frame begin */
627 desc_p->st |= BIT(29);
628 desc_p->status = BIT(31);
629
630 /*Descriptors st and status field has changed, so FLUSH it */
631 flush_dcache_range(desc_start, desc_end);
632
633 /* Move to next Descriptor and wrap around */
634 if (++desc_num >= CONFIG_TX_DESCR_NUM)
635 desc_num = 0;
636 priv->tx_currdescnum = desc_num;
637
638 /* Start the DMA */
639 v = readl(priv->mac_reg + EMAC_TX_CTL1);
640 v |= BIT(31);/* mandatory */
641 v |= BIT(30);/* mandatory */
642 writel(v, priv->mac_reg + EMAC_TX_CTL1);
643
644 return 0;
645}
646
Sean Anderson4702aa22020-09-15 10:45:00 -0400647static int sun8i_emac_board_setup(struct udevice *dev,
648 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530649{
Jagan Tekicb63d282019-02-28 00:26:58 +0530650 int ret;
651
652 ret = clk_enable(&priv->tx_clk);
653 if (ret) {
654 dev_err(dev, "failed to enable TX clock\n");
655 return ret;
656 }
657
658 if (reset_valid(&priv->tx_rst)) {
659 ret = reset_deassert(&priv->tx_rst);
660 if (ret) {
661 dev_err(dev, "failed to deassert TX reset\n");
662 goto err_tx_clk;
663 }
664 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530665
Jagan Teki727ed792019-02-28 00:27:00 +0530666 /* Only H3/H5 have clock controls for internal EPHY */
667 if (clk_valid(&priv->ephy_clk)) {
668 ret = clk_enable(&priv->ephy_clk);
669 if (ret) {
670 dev_err(dev, "failed to enable EPHY TX clock\n");
671 return ret;
672 }
673 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530674
Jagan Teki727ed792019-02-28 00:27:00 +0530675 if (reset_valid(&priv->ephy_rst)) {
676 ret = reset_deassert(&priv->ephy_rst);
677 if (ret) {
678 dev_err(dev, "failed to deassert EPHY TX clock\n");
679 return ret;
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200680 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530681 }
682
Jagan Tekicb63d282019-02-28 00:26:58 +0530683 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200684
Jagan Tekicb63d282019-02-28 00:26:58 +0530685err_tx_clk:
686 clk_disable(&priv->tx_clk);
687 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530688}
689
Simon Glassfa4689a2019-12-06 21:41:35 -0700690#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100691static int sun8i_mdio_reset(struct mii_dev *bus)
692{
693 struct udevice *dev = bus->priv;
694 struct emac_eth_dev *priv = dev_get_priv(dev);
695 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
696 int ret;
697
698 if (!dm_gpio_is_valid(&priv->reset_gpio))
699 return 0;
700
701 /* reset the phy */
702 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
703 if (ret)
704 return ret;
705
706 udelay(pdata->reset_delays[0]);
707
708 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
709 if (ret)
710 return ret;
711
712 udelay(pdata->reset_delays[1]);
713
714 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
715 if (ret)
716 return ret;
717
718 udelay(pdata->reset_delays[2]);
719
720 return 0;
721}
722#endif
723
724static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530725{
726 struct mii_dev *bus = mdio_alloc();
727
728 if (!bus) {
729 debug("Failed to allocate MDIO bus\n");
730 return -ENOMEM;
731 }
732
733 bus->read = sun8i_mdio_read;
734 bus->write = sun8i_mdio_write;
735 snprintf(bus->name, sizeof(bus->name), name);
736 bus->priv = (void *)priv;
Simon Glassfa4689a2019-12-06 21:41:35 -0700737#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100738 bus->reset = sun8i_mdio_reset;
739#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530740
741 return mdio_register(bus);
742}
743
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530744static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
745 int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530746{
747 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530748 u32 desc_num = priv->rx_currdescnum;
749 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
750 uintptr_t desc_start = (uintptr_t)desc_p;
751 uintptr_t desc_end = desc_start +
752 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
753
754 /* Make the current descriptor valid again */
755 desc_p->status |= BIT(31);
756
757 /* Flush Status field of descriptor */
758 flush_dcache_range(desc_start, desc_end);
759
760 /* Move to next desc and wrap-around condition. */
761 if (++desc_num >= CONFIG_RX_DESCR_NUM)
762 desc_num = 0;
763 priv->rx_currdescnum = desc_num;
764
765 return 0;
766}
767
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530768static void sun8i_emac_eth_stop(struct udevice *dev)
769{
770 struct emac_eth_dev *priv = dev_get_priv(dev);
771
772 /* Stop Rx/Tx transmitter */
773 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
774 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
775
776 /* Stop TX DMA */
777 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
778
779 phy_shutdown(priv->phydev);
780}
781
782static int sun8i_emac_eth_probe(struct udevice *dev)
783{
Icenowy Zheng525dc442018-11-23 00:37:48 +0100784 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
785 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530786 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530787 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530788
789 priv->mac_reg = (void *)pdata->iobase;
790
Sean Anderson4702aa22020-09-15 10:45:00 -0400791 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekicb63d282019-02-28 00:26:58 +0530792 if (ret)
793 return ret;
794
Icenowy Zheng525dc442018-11-23 00:37:48 +0100795 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530796
Philipp Tomsich3297b552017-02-22 19:46:41 +0100797 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530798 priv->bus = miiphy_get_dev_by_name(dev->name);
799
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530800 return sun8i_phy_init(priv, dev);
801}
802
803static const struct eth_ops sun8i_emac_eth_ops = {
804 .start = sun8i_emac_eth_start,
805 .write_hwaddr = sun8i_eth_write_hwaddr,
806 .send = sun8i_emac_eth_send,
807 .recv = sun8i_emac_eth_recv,
808 .free_pkt = sun8i_eth_free_pkt,
809 .stop = sun8i_emac_eth_stop,
810};
811
Sean Anderson4702aa22020-09-15 10:45:00 -0400812static int sun8i_get_ephy_nodes(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki727ed792019-02-28 00:27:00 +0530813{
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200814 int emac_node, ephy_node, ret, ephy_handle;
815
816 emac_node = fdt_path_offset(gd->fdt_blob,
817 "/soc/ethernet@1c30000");
818 if (emac_node < 0) {
819 debug("failed to get emac node\n");
820 return emac_node;
821 }
822 ephy_handle = fdtdec_lookup_phandle(gd->fdt_blob,
823 emac_node, "phy-handle");
Jagan Teki727ed792019-02-28 00:27:00 +0530824
825 /* look for mdio-mux node for internal PHY node */
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200826 ephy_node = fdt_path_offset(gd->fdt_blob,
827 "/soc/ethernet@1c30000/mdio-mux/mdio@1/ethernet-phy@1");
828 if (ephy_node < 0) {
Jagan Teki727ed792019-02-28 00:27:00 +0530829 debug("failed to get mdio-mux with internal PHY\n");
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200830 return ephy_node;
Jagan Teki727ed792019-02-28 00:27:00 +0530831 }
832
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200833 /* This is not the phy we are looking for */
834 if (ephy_node != ephy_handle)
835 return 0;
836
837 ret = fdt_node_check_compatible(gd->fdt_blob, ephy_node,
Jagan Teki727ed792019-02-28 00:27:00 +0530838 "allwinner,sun8i-h3-mdio-internal");
839 if (ret < 0) {
840 debug("failed to find mdio-internal node\n");
841 return ret;
842 }
843
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200844 ret = clk_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki727ed792019-02-28 00:27:00 +0530845 &priv->ephy_clk);
846 if (ret) {
847 dev_err(dev, "failed to get EPHY TX clock\n");
848 return ret;
849 }
850
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200851 ret = reset_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki727ed792019-02-28 00:27:00 +0530852 &priv->ephy_rst);
853 if (ret) {
854 dev_err(dev, "failed to get EPHY TX reset\n");
855 return ret;
856 }
857
858 priv->use_internal_phy = true;
859
860 return 0;
861}
862
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530863static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
864{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100865 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
866 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530867 struct emac_eth_dev *priv = dev_get_priv(dev);
868 const char *phy_mode;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100869 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700870 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530871 int offset = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -0700872#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100873 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100874#endif
Jagan Tekicb63d282019-02-28 00:26:58 +0530875 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530876
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900877 pdata->iobase = dev_read_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100878 if (pdata->iobase == FDT_ADDR_T_NONE) {
879 debug("%s: Cannot find MAC base address\n", __func__);
880 return -EINVAL;
881 }
882
Lothar Feltene8cbced2018-07-13 10:45:28 +0200883 priv->variant = dev_get_driver_data(dev);
884
885 if (!priv->variant) {
886 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100887 return -EINVAL;
888 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200889
Jagan Tekicb63d282019-02-28 00:26:58 +0530890 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
891 if (ret) {
892 dev_err(dev, "failed to get TX clock\n");
893 return ret;
894 }
895
896 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
897 if (ret && ret != -ENOENT) {
898 dev_err(dev, "failed to get TX reset\n");
899 return ret;
900 }
901
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530902 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
903 if (offset < 0) {
904 debug("%s: cannot find syscon node\n", __func__);
905 return -EINVAL;
906 }
907
908 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
909 if (!reg) {
910 debug("%s: cannot find reg property in syscon node\n",
911 __func__);
912 return -EINVAL;
913 }
914 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
915 offset, reg);
916 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
917 debug("%s: Cannot find syscon base address\n", __func__);
918 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100919 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530920
921 pdata->phy_interface = -1;
922 priv->phyaddr = -1;
923 priv->use_internal_phy = false;
924
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100925 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100926 if (offset < 0) {
927 debug("%s: Cannot find PHY address\n", __func__);
928 return -EINVAL;
929 }
930 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530931
Simon Glassdd79d6e2017-01-17 16:52:55 -0700932 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530933
934 if (phy_mode)
935 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
936 printf("phy interface%d\n", pdata->phy_interface);
937
938 if (pdata->phy_interface == -1) {
939 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
940 return -EINVAL;
941 }
942
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530943 if (priv->variant == H3_EMAC) {
Sean Anderson4702aa22020-09-15 10:45:00 -0400944 ret = sun8i_get_ephy_nodes(dev, priv);
Jagan Teki727ed792019-02-28 00:27:00 +0530945 if (ret)
946 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530947 }
948
949 priv->interface = pdata->phy_interface;
950
951 if (!priv->use_internal_phy)
952 parse_phy_pins(dev);
953
Icenowy Zheng525dc442018-11-23 00:37:48 +0100954 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
955 "allwinner,tx-delay-ps", 0);
956 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
957 printf("%s: Invalid TX delay value %d\n", __func__,
958 sun8i_pdata->tx_delay_ps);
959
960 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
961 "allwinner,rx-delay-ps", 0);
962 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
963 printf("%s: Invalid RX delay value %d\n", __func__,
964 sun8i_pdata->rx_delay_ps);
965
Simon Glassfa4689a2019-12-06 21:41:35 -0700966#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass7a494432017-05-17 17:18:09 -0600967 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100968 "snps,reset-active-low"))
969 reset_flags |= GPIOD_ACTIVE_LOW;
970
971 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
972 &priv->reset_gpio, reset_flags);
973
974 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600975 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100976 "snps,reset-delays-us",
977 sun8i_pdata->reset_delays, 3);
978 } else if (ret == -ENOENT) {
979 ret = 0;
980 }
981#endif
982
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530983 return 0;
984}
985
986static const struct udevice_id sun8i_emac_eth_ids[] = {
987 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
988 {.compatible = "allwinner,sun50i-a64-emac",
989 .data = (uintptr_t)A64_EMAC },
990 {.compatible = "allwinner,sun8i-a83t-emac",
991 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene8cbced2018-07-13 10:45:28 +0200992 {.compatible = "allwinner,sun8i-r40-gmac",
993 .data = (uintptr_t)R40_GMAC },
Samuel Holland3386e9a2020-05-07 18:10:51 -0500994 {.compatible = "allwinner,sun50i-h6-emac",
995 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530996 { }
997};
998
999U_BOOT_DRIVER(eth_sun8i_emac) = {
1000 .name = "eth_sun8i_emac",
1001 .id = UCLASS_ETH,
1002 .of_match = sun8i_emac_eth_ids,
1003 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
1004 .probe = sun8i_emac_eth_probe,
1005 .ops = &sun8i_emac_eth_ops,
1006 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +01001007 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301008 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1009};