1 // SPDX-License-Identifier: GPL-2.0+
4 * Author: Amit Singh Tomar, amittomer25@gmail.com
6 * Ethernet driver for H3/A64/A83T based SoC's
8 * It is derived from the work done by
9 * LABBE Corentin & Chen-Yu Tsai for Linux, THANKS!
15 #include <asm/cache.h>
16 #include <asm/global_data.h>
22 #include <fdt_support.h>
23 #include <dm/device_compat.h>
24 #include <linux/bitops.h>
25 #include <linux/delay.h>
26 #include <linux/err.h>
33 #define MDIO_CMD_MII_BUSY BIT(0)
34 #define MDIO_CMD_MII_WRITE BIT(1)
36 #define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
37 #define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
38 #define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
39 #define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
40 #define MDIO_CMD_MII_CLK_CSR_DIV_16 0x0
41 #define MDIO_CMD_MII_CLK_CSR_DIV_32 0x1
42 #define MDIO_CMD_MII_CLK_CSR_DIV_64 0x2
43 #define MDIO_CMD_MII_CLK_CSR_DIV_128 0x3
44 #define MDIO_CMD_MII_CLK_CSR_SHIFT 20
46 #define CONFIG_TX_DESCR_NUM 32
47 #define CONFIG_RX_DESCR_NUM 32
48 #define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
51 * The datasheet says that each descriptor can transfers up to 4096 bytes
52 * But later, the register documentation reduces that value to 2048,
53 * using 2048 cause strange behaviours and even BSP driver use 2047
55 #define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
57 #define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
58 #define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
60 #define H3_EPHY_DEFAULT_VALUE 0x58000
61 #define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
62 #define H3_EPHY_ADDR_SHIFT 20
63 #define REG_PHY_ADDR_MASK GENMASK(4, 0)
64 #define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
65 #define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
66 #define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
68 #define SC_RMII_EN BIT(13)
69 #define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
70 #define SC_ETCS_MASK GENMASK(1, 0)
71 #define SC_ETCS_EXT_GMII 0x1
72 #define SC_ETCS_INT_GMII 0x2
73 #define SC_ETXDC_MASK GENMASK(12, 10)
74 #define SC_ETXDC_OFFSET 10
75 #define SC_ERXDC_MASK GENMASK(9, 5)
76 #define SC_ERXDC_OFFSET 5
78 #define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
80 #define AHB_GATE_OFFSET_EPHY 0
82 /* H3/A64 EMAC Register's offset */
83 #define EMAC_CTL0 0x00
84 #define EMAC_CTL0_FULL_DUPLEX BIT(0)
85 #define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
86 #define EMAC_CTL0_SPEED_10 (0x2 << 2)
87 #define EMAC_CTL0_SPEED_100 (0x3 << 2)
88 #define EMAC_CTL0_SPEED_1000 (0x0 << 2)
89 #define EMAC_CTL1 0x04
90 #define EMAC_CTL1_SOFT_RST BIT(0)
91 #define EMAC_CTL1_BURST_LEN_SHIFT 24
92 #define EMAC_INT_STA 0x08
93 #define EMAC_INT_EN 0x0c
94 #define EMAC_TX_CTL0 0x10
95 #define EMAC_TX_CTL0_TX_EN BIT(31)
96 #define EMAC_TX_CTL1 0x14
97 #define EMAC_TX_CTL1_TX_MD BIT(1)
98 #define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
99 #define EMAC_TX_CTL1_TX_DMA_START BIT(31)
100 #define EMAC_TX_FLOW_CTL 0x1c
101 #define EMAC_TX_DMA_DESC 0x20
102 #define EMAC_RX_CTL0 0x24
103 #define EMAC_RX_CTL0_RX_EN BIT(31)
104 #define EMAC_RX_CTL1 0x28
105 #define EMAC_RX_CTL1_RX_MD BIT(1)
106 #define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
107 #define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
108 #define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
109 #define EMAC_RX_CTL1_RX_DMA_START BIT(31)
110 #define EMAC_RX_DMA_DESC 0x34
111 #define EMAC_MII_CMD 0x48
112 #define EMAC_MII_DATA 0x4c
113 #define EMAC_ADDR0_HIGH 0x50
114 #define EMAC_ADDR0_LOW 0x54
115 #define EMAC_TX_DMA_STA 0xb0
116 #define EMAC_TX_CUR_DESC 0xb4
117 #define EMAC_TX_CUR_BUF 0xb8
118 #define EMAC_RX_DMA_STA 0xc0
119 #define EMAC_RX_CUR_DESC 0xc4
121 #define EMAC_DESC_OWN_DMA BIT(31)
122 #define EMAC_DESC_LAST_DESC BIT(30)
123 #define EMAC_DESC_FIRST_DESC BIT(29)
124 #define EMAC_DESC_CHAIN_SECOND BIT(24)
126 #define EMAC_DESC_RX_ERROR_MASK 0x400068db
128 DECLARE_GLOBAL_DATA_PTR;
138 struct emac_dma_desc {
143 } __aligned(ARCH_DMA_MINALIGN);
145 struct emac_eth_dev {
146 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
147 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
148 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
149 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
161 bool use_internal_phy;
163 enum emac_variant variant;
165 phys_addr_t sysctl_reg;
166 struct phy_device *phydev;
170 struct reset_ctl tx_rst;
171 struct reset_ctl ephy_rst;
172 #if CONFIG_IS_ENABLED(DM_GPIO)
173 struct gpio_desc reset_gpio;
178 struct sun8i_eth_pdata {
179 struct eth_pdata eth_pdata;
186 static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
188 struct udevice *dev = bus->priv;
189 struct emac_eth_dev *priv = dev_get_priv(dev);
193 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
194 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
195 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
196 MDIO_CMD_MII_PHY_ADDR_MASK;
199 * The EMAC clock is either 200 or 300 MHz, so we need a divider
200 * of 128 to get the MDIO frequency below the required 2.5 MHz.
202 if (!priv->use_internal_phy)
203 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
204 MDIO_CMD_MII_CLK_CSR_SHIFT;
206 mii_cmd |= MDIO_CMD_MII_BUSY;
208 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
210 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
211 MDIO_CMD_MII_BUSY, false,
212 CONFIG_MDIO_TIMEOUT, true);
216 return readl(priv->mac_reg + EMAC_MII_DATA);
219 static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
222 struct udevice *dev = bus->priv;
223 struct emac_eth_dev *priv = dev_get_priv(dev);
226 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
227 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
228 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
229 MDIO_CMD_MII_PHY_ADDR_MASK;
232 * The EMAC clock is either 200 or 300 MHz, so we need a divider
233 * of 128 to get the MDIO frequency below the required 2.5 MHz.
235 if (!priv->use_internal_phy)
236 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
237 MDIO_CMD_MII_CLK_CSR_SHIFT;
239 mii_cmd |= MDIO_CMD_MII_WRITE;
240 mii_cmd |= MDIO_CMD_MII_BUSY;
242 writel(val, priv->mac_reg + EMAC_MII_DATA);
243 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
245 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
246 MDIO_CMD_MII_BUSY, false,
247 CONFIG_MDIO_TIMEOUT, true);
250 static int sun8i_eth_write_hwaddr(struct udevice *dev)
252 struct emac_eth_dev *priv = dev_get_priv(dev);
253 struct eth_pdata *pdata = dev_get_plat(dev);
254 uchar *mac_id = pdata->enetaddr;
255 u32 macid_lo, macid_hi;
257 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
259 macid_hi = mac_id[4] + (mac_id[5] << 8);
261 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
262 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
267 static void sun8i_adjust_link(struct emac_eth_dev *priv,
268 struct phy_device *phydev)
272 v = readl(priv->mac_reg + EMAC_CTL0);
275 v |= EMAC_CTL0_FULL_DUPLEX;
277 v &= ~EMAC_CTL0_FULL_DUPLEX;
279 v &= ~EMAC_CTL0_SPEED_MASK;
281 switch (phydev->speed) {
283 v |= EMAC_CTL0_SPEED_1000;
286 v |= EMAC_CTL0_SPEED_100;
289 v |= EMAC_CTL0_SPEED_10;
292 writel(v, priv->mac_reg + EMAC_CTL0);
295 static u32 sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 reg)
297 if (priv->use_internal_phy) {
298 /* H3 based SoC's that has an Internal 100MBit PHY
299 * needs to be configured and powered up before use
301 reg &= ~H3_EPHY_DEFAULT_MASK;
302 reg |= H3_EPHY_DEFAULT_VALUE;
303 reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
304 reg &= ~H3_EPHY_SHUTDOWN;
305 return reg | H3_EPHY_SELECT;
308 /* This is to select External Gigabit PHY on those boards with
309 * an internal PHY. Does not hurt on other SoCs. Linux does
312 return reg & ~H3_EPHY_SELECT;
315 static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
316 struct emac_eth_dev *priv)
320 if (priv->variant == R40_GMAC) {
321 /* Select RGMII for R40 */
322 reg = readl(priv->sysctl_reg + 0x164);
323 reg |= SC_ETCS_INT_GMII |
325 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
327 writel(reg, priv->sysctl_reg + 0x164);
331 reg = readl(priv->sysctl_reg + 0x30);
333 reg = sun8i_emac_set_syscon_ephy(priv, reg);
335 reg &= ~(SC_ETCS_MASK | SC_EPIT);
336 if (priv->variant == H3_EMAC ||
337 priv->variant == A64_EMAC ||
338 priv->variant == H6_EMAC)
341 switch (priv->interface) {
342 case PHY_INTERFACE_MODE_MII:
345 case PHY_INTERFACE_MODE_RGMII:
346 case PHY_INTERFACE_MODE_RGMII_ID:
347 case PHY_INTERFACE_MODE_RGMII_RXID:
348 case PHY_INTERFACE_MODE_RGMII_TXID:
349 reg |= SC_EPIT | SC_ETCS_INT_GMII;
351 case PHY_INTERFACE_MODE_RMII:
352 if (priv->variant == H3_EMAC ||
353 priv->variant == A64_EMAC ||
354 priv->variant == H6_EMAC) {
355 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
358 /* RMII not supported on A83T */
360 debug("%s: Invalid PHY interface\n", __func__);
364 if (pdata->tx_delay_ps)
365 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
368 if (pdata->rx_delay_ps)
369 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
372 writel(reg, priv->sysctl_reg + 0x30);
377 static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
379 struct phy_device *phydev;
381 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
385 priv->phydev = phydev;
386 phy_config(priv->phydev);
391 #define cache_clean_descriptor(desc) \
392 flush_dcache_range((uintptr_t)(desc), \
393 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
395 #define cache_inv_descriptor(desc) \
396 invalidate_dcache_range((uintptr_t)(desc), \
397 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
399 static void rx_descs_init(struct emac_eth_dev *priv)
401 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
402 char *rxbuffs = &priv->rxbuffer[0];
403 struct emac_dma_desc *desc_p;
407 * Make sure we don't have dirty cache lines around, which could
408 * be cleaned to DRAM *after* the MAC has already written data to it.
410 invalidate_dcache_range((uintptr_t)desc_table_p,
411 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
412 invalidate_dcache_range((uintptr_t)rxbuffs,
413 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
415 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
416 desc_p = &desc_table_p[i];
417 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
418 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
419 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
420 desc_p->status = EMAC_DESC_OWN_DMA;
423 /* Correcting the last pointer of the chain */
424 desc_p->next = (uintptr_t)&desc_table_p[0];
426 flush_dcache_range((uintptr_t)priv->rx_chain,
427 (uintptr_t)priv->rx_chain +
428 sizeof(priv->rx_chain));
430 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
431 priv->rx_currdescnum = 0;
434 static void tx_descs_init(struct emac_eth_dev *priv)
436 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
437 char *txbuffs = &priv->txbuffer[0];
438 struct emac_dma_desc *desc_p;
441 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
442 desc_p = &desc_table_p[i];
443 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
444 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
445 desc_p->ctl_size = 0;
449 /* Correcting the last pointer of the chain */
450 desc_p->next = (uintptr_t)&desc_table_p[0];
452 /* Flush the first TX buffer descriptor we will tell the MAC about. */
453 cache_clean_descriptor(desc_table_p);
455 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
456 priv->tx_currdescnum = 0;
459 static int sun8i_emac_eth_start(struct udevice *dev)
461 struct emac_eth_dev *priv = dev_get_priv(dev);
465 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
466 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
467 EMAC_CTL1_SOFT_RST, false, 10, true);
469 printf("%s: Timeout\n", __func__);
473 /* Rewrite mac address after reset */
474 sun8i_eth_write_hwaddr(dev);
476 /* transmission starts after the full frame arrived in TX DMA FIFO */
477 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
480 * RX DMA reads data from RX DMA FIFO to host memory after a
481 * complete frame has been written to RX DMA FIFO
483 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
485 /* DMA burst length */
486 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
488 /* Initialize rx/tx descriptors */
493 ret = phy_startup(priv->phydev);
497 sun8i_adjust_link(priv, priv->phydev);
499 /* Start RX/TX DMA */
500 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
501 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
502 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
505 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
506 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
511 static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
513 struct emac_eth_dev *priv = dev_get_priv(dev);
514 u32 status, desc_num = priv->rx_currdescnum;
515 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
516 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
519 /* Invalidate entire buffer descriptor */
520 cache_inv_descriptor(desc_p);
522 status = desc_p->status;
524 /* Check for DMA own bit */
525 if (status & EMAC_DESC_OWN_DMA)
528 length = (status >> 16) & 0x3fff;
530 /* make sure we read from DRAM, not our cache */
531 invalidate_dcache_range(data_start,
532 data_start + roundup(length, ARCH_DMA_MINALIGN));
534 if (status & EMAC_DESC_RX_ERROR_MASK) {
535 debug("RX: packet error: 0x%x\n",
536 status & EMAC_DESC_RX_ERROR_MASK);
540 debug("RX: Bad Packet (runt)\n");
544 if (length > CONFIG_ETH_RXSIZE) {
545 debug("RX: Too large packet (%d bytes)\n", length);
549 *packetp = (uchar *)(ulong)desc_p->buf_addr;
554 static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
556 struct emac_eth_dev *priv = dev_get_priv(dev);
557 u32 desc_num = priv->tx_currdescnum;
558 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
559 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
560 uintptr_t data_end = data_start +
561 roundup(length, ARCH_DMA_MINALIGN);
563 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
565 memcpy((void *)data_start, packet, length);
567 /* Flush data to be sent */
568 flush_dcache_range(data_start, data_end);
570 /* frame begin and end */
571 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
572 desc_p->status = EMAC_DESC_OWN_DMA;
574 /* make sure the MAC reads the actual data from DRAM */
575 cache_clean_descriptor(desc_p);
577 /* Move to next Descriptor and wrap around */
578 if (++desc_num >= CONFIG_TX_DESCR_NUM)
580 priv->tx_currdescnum = desc_num;
583 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
586 * Since we copied the data above, we return here without waiting
587 * for the packet to be actually send out.
593 static int sun8i_emac_board_setup(struct udevice *dev,
594 struct emac_eth_dev *priv)
598 ret = clk_enable(&priv->tx_clk);
600 dev_err(dev, "failed to enable TX clock\n");
604 if (reset_valid(&priv->tx_rst)) {
605 ret = reset_deassert(&priv->tx_rst);
607 dev_err(dev, "failed to deassert TX reset\n");
612 /* Only H3/H5 have clock controls for internal EPHY */
613 if (clk_valid(&priv->ephy_clk)) {
614 ret = clk_enable(&priv->ephy_clk);
616 dev_err(dev, "failed to enable EPHY TX clock\n");
621 if (reset_valid(&priv->ephy_rst)) {
622 ret = reset_deassert(&priv->ephy_rst);
624 dev_err(dev, "failed to deassert EPHY TX clock\n");
632 clk_disable(&priv->tx_clk);
636 #if CONFIG_IS_ENABLED(DM_GPIO)
637 static int sun8i_mdio_reset(struct mii_dev *bus)
639 struct udevice *dev = bus->priv;
640 struct emac_eth_dev *priv = dev_get_priv(dev);
641 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
644 if (!dm_gpio_is_valid(&priv->reset_gpio))
648 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
652 udelay(pdata->reset_delays[0]);
654 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
658 udelay(pdata->reset_delays[1]);
660 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
664 udelay(pdata->reset_delays[2]);
670 static int sun8i_mdio_init(const char *name, struct udevice *priv)
672 struct mii_dev *bus = mdio_alloc();
675 debug("Failed to allocate MDIO bus\n");
679 bus->read = sun8i_mdio_read;
680 bus->write = sun8i_mdio_write;
681 snprintf(bus->name, sizeof(bus->name), name);
682 bus->priv = (void *)priv;
683 #if CONFIG_IS_ENABLED(DM_GPIO)
684 bus->reset = sun8i_mdio_reset;
687 return mdio_register(bus);
690 static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
693 struct emac_eth_dev *priv = dev_get_priv(dev);
694 u32 desc_num = priv->rx_currdescnum;
695 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
697 /* give the current descriptor back to the MAC */
698 desc_p->status |= EMAC_DESC_OWN_DMA;
700 /* Flush Status field of descriptor */
701 cache_clean_descriptor(desc_p);
703 /* Move to next desc and wrap-around condition. */
704 if (++desc_num >= CONFIG_RX_DESCR_NUM)
706 priv->rx_currdescnum = desc_num;
711 static void sun8i_emac_eth_stop(struct udevice *dev)
713 struct emac_eth_dev *priv = dev_get_priv(dev);
715 /* Stop Rx/Tx transmitter */
716 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
717 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
720 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
721 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
723 phy_shutdown(priv->phydev);
726 static int sun8i_emac_eth_probe(struct udevice *dev)
728 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
729 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
730 struct emac_eth_dev *priv = dev_get_priv(dev);
733 priv->mac_reg = (void *)pdata->iobase;
735 ret = sun8i_emac_board_setup(dev, priv);
739 sun8i_emac_set_syscon(sun8i_pdata, priv);
741 sun8i_mdio_init(dev->name, dev);
742 priv->bus = miiphy_get_dev_by_name(dev->name);
744 return sun8i_phy_init(priv, dev);
747 static const struct eth_ops sun8i_emac_eth_ops = {
748 .start = sun8i_emac_eth_start,
749 .write_hwaddr = sun8i_eth_write_hwaddr,
750 .send = sun8i_emac_eth_send,
751 .recv = sun8i_emac_eth_recv,
752 .free_pkt = sun8i_eth_free_pkt,
753 .stop = sun8i_emac_eth_stop,
756 static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
758 struct ofnode_phandle_args phandle;
761 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
762 NULL, 0, 0, &phandle);
766 /* If the PHY node is not a child of the internal MDIO bus, we are
767 * using some external PHY.
769 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
770 "allwinner,sun8i-h3-mdio-internal"))
773 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
775 dev_err(dev, "failed to get EPHY TX clock\n");
779 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
781 dev_err(dev, "failed to get EPHY TX reset\n");
785 priv->use_internal_phy = true;
790 static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
792 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
793 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
794 struct emac_eth_dev *priv = dev_get_priv(dev);
796 int node = dev_of_offset(dev);
798 #if CONFIG_IS_ENABLED(DM_GPIO)
799 int reset_flags = GPIOD_IS_OUT;
803 pdata->iobase = dev_read_addr(dev);
804 if (pdata->iobase == FDT_ADDR_T_NONE) {
805 debug("%s: Cannot find MAC base address\n", __func__);
809 priv->variant = dev_get_driver_data(dev);
811 if (!priv->variant) {
812 printf("%s: Missing variant\n", __func__);
816 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
818 dev_err(dev, "failed to get TX clock\n");
822 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
823 if (ret && ret != -ENOENT) {
824 dev_err(dev, "failed to get TX reset\n");
828 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
830 debug("%s: cannot find syscon node\n", __func__);
834 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
836 debug("%s: cannot find reg property in syscon node\n",
840 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
842 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
843 debug("%s: Cannot find syscon base address\n", __func__);
847 pdata->phy_interface = -1;
849 priv->use_internal_phy = false;
851 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
853 debug("%s: Cannot find PHY address\n", __func__);
856 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
858 pdata->phy_interface = dev_read_phy_mode(dev);
859 debug("phy interface %d\n", pdata->phy_interface);
860 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
863 if (priv->variant == H3_EMAC) {
864 ret = sun8i_handle_internal_phy(dev, priv);
869 priv->interface = pdata->phy_interface;
871 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
872 "allwinner,tx-delay-ps", 0);
873 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
874 printf("%s: Invalid TX delay value %d\n", __func__,
875 sun8i_pdata->tx_delay_ps);
877 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
878 "allwinner,rx-delay-ps", 0);
879 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
880 printf("%s: Invalid RX delay value %d\n", __func__,
881 sun8i_pdata->rx_delay_ps);
883 #if CONFIG_IS_ENABLED(DM_GPIO)
884 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
885 "snps,reset-active-low"))
886 reset_flags |= GPIOD_ACTIVE_LOW;
888 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
889 &priv->reset_gpio, reset_flags);
892 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
893 "snps,reset-delays-us",
894 sun8i_pdata->reset_delays, 3);
895 } else if (ret == -ENOENT) {
903 static const struct udevice_id sun8i_emac_eth_ids[] = {
904 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
905 {.compatible = "allwinner,sun50i-a64-emac",
906 .data = (uintptr_t)A64_EMAC },
907 {.compatible = "allwinner,sun8i-a83t-emac",
908 .data = (uintptr_t)A83T_EMAC },
909 {.compatible = "allwinner,sun8i-r40-gmac",
910 .data = (uintptr_t)R40_GMAC },
911 {.compatible = "allwinner,sun50i-h6-emac",
912 .data = (uintptr_t)H6_EMAC },
916 U_BOOT_DRIVER(eth_sun8i_emac) = {
917 .name = "eth_sun8i_emac",
919 .of_match = sun8i_emac_eth_ids,
920 .of_to_plat = sun8i_emac_eth_of_to_plat,
921 .probe = sun8i_emac_eth_probe,
922 .ops = &sun8i_emac_eth_ops,
923 .priv_auto = sizeof(struct emac_eth_dev),
924 .plat_auto = sizeof(struct sun8i_eth_pdata),
925 .flags = DM_FLAG_ALLOC_PRIV_DMA,