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!
14 #include <asm/arch/clock.h>
15 #include <asm/arch/gpio.h>
18 #include <fdt_support.h>
19 #include <linux/err.h>
23 #include <dt-bindings/pinctrl/sun4i-a10.h>
25 #include <asm-generic/gpio.h>
28 #define MDIO_CMD_MII_BUSY BIT(0)
29 #define MDIO_CMD_MII_WRITE BIT(1)
31 #define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
32 #define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
33 #define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
34 #define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
36 #define CONFIG_TX_DESCR_NUM 32
37 #define CONFIG_RX_DESCR_NUM 32
38 #define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
41 * The datasheet says that each descriptor can transfers up to 4096 bytes
42 * But later, the register documentation reduces that value to 2048,
43 * using 2048 cause strange behaviours and even BSP driver use 2047
45 #define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
47 #define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
48 #define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
50 #define H3_EPHY_DEFAULT_VALUE 0x58000
51 #define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
52 #define H3_EPHY_ADDR_SHIFT 20
53 #define REG_PHY_ADDR_MASK GENMASK(4, 0)
54 #define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
55 #define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
56 #define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
58 #define SC_RMII_EN BIT(13)
59 #define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
60 #define SC_ETCS_MASK GENMASK(1, 0)
61 #define SC_ETCS_EXT_GMII 0x1
62 #define SC_ETCS_INT_GMII 0x2
63 #define SC_ETXDC_MASK GENMASK(12, 10)
64 #define SC_ETXDC_OFFSET 10
65 #define SC_ERXDC_MASK GENMASK(9, 5)
66 #define SC_ERXDC_OFFSET 5
68 #define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
70 #define AHB_GATE_OFFSET_EPHY 0
73 #define SUN8I_IOMUX_H3 2
74 #define SUN8I_IOMUX_R40 5
77 /* H3/A64 EMAC Register's offset */
78 #define EMAC_CTL0 0x00
79 #define EMAC_CTL1 0x04
80 #define EMAC_INT_STA 0x08
81 #define EMAC_INT_EN 0x0c
82 #define EMAC_TX_CTL0 0x10
83 #define EMAC_TX_CTL1 0x14
84 #define EMAC_TX_FLOW_CTL 0x1c
85 #define EMAC_TX_DMA_DESC 0x20
86 #define EMAC_RX_CTL0 0x24
87 #define EMAC_RX_CTL1 0x28
88 #define EMAC_RX_DMA_DESC 0x34
89 #define EMAC_MII_CMD 0x48
90 #define EMAC_MII_DATA 0x4c
91 #define EMAC_ADDR0_HIGH 0x50
92 #define EMAC_ADDR0_LOW 0x54
93 #define EMAC_TX_DMA_STA 0xb0
94 #define EMAC_TX_CUR_DESC 0xb4
95 #define EMAC_TX_CUR_BUF 0xb8
96 #define EMAC_RX_DMA_STA 0xc0
97 #define EMAC_RX_CUR_DESC 0xc4
99 DECLARE_GLOBAL_DATA_PTR;
108 struct emac_dma_desc {
113 } __aligned(ARCH_DMA_MINALIGN);
115 struct emac_eth_dev {
116 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
117 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
118 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
119 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
131 bool use_internal_phy;
133 enum emac_variant variant;
135 phys_addr_t sysctl_reg;
136 struct phy_device *phydev;
138 #ifdef CONFIG_DM_GPIO
139 struct gpio_desc reset_gpio;
144 struct sun8i_eth_pdata {
145 struct eth_pdata eth_pdata;
152 static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
154 struct udevice *dev = bus->priv;
155 struct emac_eth_dev *priv = dev_get_priv(dev);
158 int timeout = CONFIG_MDIO_TIMEOUT;
160 miiaddr &= ~MDIO_CMD_MII_WRITE;
161 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
162 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
163 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
165 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
167 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
168 MDIO_CMD_MII_PHY_ADDR_MASK;
170 miiaddr |= MDIO_CMD_MII_BUSY;
172 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
174 start = get_timer(0);
175 while (get_timer(start) < timeout) {
176 if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY))
177 return readl(priv->mac_reg + EMAC_MII_DATA);
184 static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
187 struct udevice *dev = bus->priv;
188 struct emac_eth_dev *priv = dev_get_priv(dev);
191 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
193 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
194 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
195 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
197 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
198 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
199 MDIO_CMD_MII_PHY_ADDR_MASK;
201 miiaddr |= MDIO_CMD_MII_WRITE;
202 miiaddr |= MDIO_CMD_MII_BUSY;
204 writel(val, priv->mac_reg + EMAC_MII_DATA);
205 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
207 start = get_timer(0);
208 while (get_timer(start) < timeout) {
209 if (!(readl(priv->mac_reg + EMAC_MII_CMD) &
210 MDIO_CMD_MII_BUSY)) {
220 static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
222 u32 macid_lo, macid_hi;
224 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
226 macid_hi = mac_id[4] + (mac_id[5] << 8);
228 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
229 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
234 static void sun8i_adjust_link(struct emac_eth_dev *priv,
235 struct phy_device *phydev)
239 v = readl(priv->mac_reg + EMAC_CTL0);
248 switch (phydev->speed) {
259 writel(v, priv->mac_reg + EMAC_CTL0);
262 static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
264 if (priv->use_internal_phy) {
265 /* H3 based SoC's that has an Internal 100MBit PHY
266 * needs to be configured and powered up before use
268 *reg &= ~H3_EPHY_DEFAULT_MASK;
269 *reg |= H3_EPHY_DEFAULT_VALUE;
270 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
271 *reg &= ~H3_EPHY_SHUTDOWN;
272 *reg |= H3_EPHY_SELECT;
274 /* This is to select External Gigabit PHY on
275 * the boards with H3 SoC.
277 *reg &= ~H3_EPHY_SELECT;
282 static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
283 struct emac_eth_dev *priv)
288 reg = readl(priv->sysctl_reg + 0x30);
290 if (priv->variant == R40_GMAC)
293 if (priv->variant == H3_EMAC) {
294 ret = sun8i_emac_set_syscon_ephy(priv, ®);
299 reg &= ~(SC_ETCS_MASK | SC_EPIT);
300 if (priv->variant == H3_EMAC || priv->variant == A64_EMAC)
303 switch (priv->interface) {
304 case PHY_INTERFACE_MODE_MII:
307 case PHY_INTERFACE_MODE_RGMII:
308 reg |= SC_EPIT | SC_ETCS_INT_GMII;
310 case PHY_INTERFACE_MODE_RMII:
311 if (priv->variant == H3_EMAC ||
312 priv->variant == A64_EMAC) {
313 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
316 /* RMII not supported on A83T */
318 debug("%s: Invalid PHY interface\n", __func__);
322 if (pdata->tx_delay_ps)
323 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
326 if (pdata->rx_delay_ps)
327 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
330 writel(reg, priv->sysctl_reg + 0x30);
335 static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
337 struct phy_device *phydev;
339 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
343 phy_connect_dev(phydev, dev);
345 priv->phydev = phydev;
346 phy_config(priv->phydev);
351 static void rx_descs_init(struct emac_eth_dev *priv)
353 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
354 char *rxbuffs = &priv->rxbuffer[0];
355 struct emac_dma_desc *desc_p;
358 /* flush Rx buffers */
359 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
362 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
363 desc_p = &desc_table_p[idx];
364 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
366 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
367 desc_p->st |= CONFIG_ETH_RXSIZE;
368 desc_p->status = BIT(31);
371 /* Correcting the last pointer of the chain */
372 desc_p->next = (uintptr_t)&desc_table_p[0];
374 flush_dcache_range((uintptr_t)priv->rx_chain,
375 (uintptr_t)priv->rx_chain +
376 sizeof(priv->rx_chain));
378 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
379 priv->rx_currdescnum = 0;
382 static void tx_descs_init(struct emac_eth_dev *priv)
384 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
385 char *txbuffs = &priv->txbuffer[0];
386 struct emac_dma_desc *desc_p;
389 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
390 desc_p = &desc_table_p[idx];
391 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
393 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
394 desc_p->status = (1 << 31);
398 /* Correcting the last pointer of the chain */
399 desc_p->next = (uintptr_t)&desc_table_p[0];
401 /* Flush all Tx buffer descriptors */
402 flush_dcache_range((uintptr_t)priv->tx_chain,
403 (uintptr_t)priv->tx_chain +
404 sizeof(priv->tx_chain));
406 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
407 priv->tx_currdescnum = 0;
410 static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
415 reg = readl((priv->mac_reg + EMAC_CTL1));
419 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
421 reg = readl(priv->mac_reg + EMAC_CTL1);
422 } while ((reg & 0x01) != 0 && (--timeout));
424 printf("%s: Timeout\n", __func__);
429 /* Rewrite mac address after reset */
430 _sun8i_write_hwaddr(priv, enetaddr);
432 v = readl(priv->mac_reg + EMAC_TX_CTL1);
433 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
435 writel(v, priv->mac_reg + EMAC_TX_CTL1);
437 v = readl(priv->mac_reg + EMAC_RX_CTL1);
438 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
439 * complete frame has been written to RX DMA FIFO
442 writel(v, priv->mac_reg + EMAC_RX_CTL1);
445 writel(8 << 24, priv->mac_reg + EMAC_CTL1);
447 /* Initialize rx/tx descriptors */
452 phy_startup(priv->phydev);
454 sun8i_adjust_link(priv, priv->phydev);
457 v = readl(priv->mac_reg + EMAC_RX_CTL1);
459 writel(v, priv->mac_reg + EMAC_RX_CTL1);
461 v = readl(priv->mac_reg + EMAC_TX_CTL1);
463 writel(v, priv->mac_reg + EMAC_TX_CTL1);
466 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
467 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
472 static int parse_phy_pins(struct udevice *dev)
474 struct emac_eth_dev *priv = dev_get_priv(dev);
476 const char *pin_name;
477 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
479 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
482 printf("WARNING: emac: cannot find pinctrl-0 node\n");
486 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
487 "drive-strength", ~0);
490 drive = SUN4I_PINCTRL_10_MA;
491 else if (drive <= 20)
492 drive = SUN4I_PINCTRL_20_MA;
493 else if (drive <= 30)
494 drive = SUN4I_PINCTRL_30_MA;
496 drive = SUN4I_PINCTRL_40_MA;
499 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
500 pull = SUN4I_PINCTRL_PULL_UP;
501 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
502 pull = SUN4I_PINCTRL_PULL_DOWN;
507 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
512 pin = sunxi_name_to_gpio(pin_name);
516 if (priv->variant == H3_EMAC)
517 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
518 else if (priv->variant == R40_GMAC)
519 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
521 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
524 sunxi_gpio_set_drv(pin, drive);
526 sunxi_gpio_set_pull(pin, pull);
530 printf("WARNING: emac: cannot find pins property\n");
537 static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
539 u32 status, desc_num = priv->rx_currdescnum;
540 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
541 int length = -EAGAIN;
543 uintptr_t desc_start = (uintptr_t)desc_p;
544 uintptr_t desc_end = desc_start +
545 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
547 ulong data_start = (uintptr_t)desc_p->buf_addr;
550 /* Invalidate entire buffer descriptor */
551 invalidate_dcache_range(desc_start, desc_end);
553 status = desc_p->status;
555 /* Check for DMA own bit */
556 if (!(status & BIT(31))) {
557 length = (desc_p->status >> 16) & 0x3FFF;
561 debug("RX: Bad Packet (runt)\n");
564 data_end = data_start + length;
565 /* Invalidate received data */
566 invalidate_dcache_range(rounddown(data_start,
571 if (length > CONFIG_ETH_RXSIZE) {
572 printf("Received packet is too big (len=%d)\n",
576 *packetp = (uchar *)(ulong)desc_p->buf_addr;
584 static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
587 u32 v, desc_num = priv->tx_currdescnum;
588 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
589 uintptr_t desc_start = (uintptr_t)desc_p;
590 uintptr_t desc_end = desc_start +
591 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
593 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
594 uintptr_t data_end = data_start +
595 roundup(len, ARCH_DMA_MINALIGN);
597 /* Invalidate entire buffer descriptor */
598 invalidate_dcache_range(desc_start, desc_end);
601 /* Mandatory undocumented bit */
602 desc_p->st |= BIT(24);
604 memcpy((void *)data_start, packet, len);
606 /* Flush data to be sent */
607 flush_dcache_range(data_start, data_end);
610 desc_p->st |= BIT(30);
611 desc_p->st |= BIT(31);
614 desc_p->st |= BIT(29);
615 desc_p->status = BIT(31);
617 /*Descriptors st and status field has changed, so FLUSH it */
618 flush_dcache_range(desc_start, desc_end);
620 /* Move to next Descriptor and wrap around */
621 if (++desc_num >= CONFIG_TX_DESCR_NUM)
623 priv->tx_currdescnum = desc_num;
626 v = readl(priv->mac_reg + EMAC_TX_CTL1);
627 v |= BIT(31);/* mandatory */
628 v |= BIT(30);/* mandatory */
629 writel(v, priv->mac_reg + EMAC_TX_CTL1);
634 static int sun8i_eth_write_hwaddr(struct udevice *dev)
636 struct eth_pdata *pdata = dev_get_platdata(dev);
637 struct emac_eth_dev *priv = dev_get_priv(dev);
639 return _sun8i_write_hwaddr(priv, pdata->enetaddr);
642 static void sun8i_emac_board_setup(struct emac_eth_dev *priv)
644 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
646 if (priv->variant == H3_EMAC) {
647 /* Only H3/H5 have clock controls for internal EPHY */
648 if (priv->use_internal_phy) {
649 /* Set clock gating for ephy */
650 setbits_le32(&ccm->bus_gate4,
651 BIT(AHB_GATE_OFFSET_EPHY));
654 setbits_le32(&ccm->ahb_reset2_cfg,
655 BIT(AHB_RESET_OFFSET_EPHY));
659 if (priv->variant == R40_GMAC) {
660 /* Set clock gating for emac */
661 setbits_le32(&ccm->ahb_reset1_cfg, BIT(AHB_RESET_OFFSET_GMAC));
664 setbits_le32(&ccm->ahb_gate1, BIT(AHB_GATE_OFFSET_GMAC));
666 /* Select RGMII for R40 */
667 setbits_le32(&ccm->gmac_clk_cfg,
668 CCM_GMAC_CTRL_TX_CLK_SRC_INT_RGMII |
669 CCM_GMAC_CTRL_GPIT_RGMII);
670 setbits_le32(&ccm->gmac_clk_cfg,
671 CCM_GMAC_CTRL_TX_CLK_DELAY(CONFIG_GMAC_TX_DELAY));
673 /* Set clock gating for emac */
674 setbits_le32(&ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_GMAC));
677 setbits_le32(&ccm->ahb_reset0_cfg, BIT(AHB_RESET_OFFSET_GMAC));
681 #if defined(CONFIG_DM_GPIO)
682 static int sun8i_mdio_reset(struct mii_dev *bus)
684 struct udevice *dev = bus->priv;
685 struct emac_eth_dev *priv = dev_get_priv(dev);
686 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
689 if (!dm_gpio_is_valid(&priv->reset_gpio))
693 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
697 udelay(pdata->reset_delays[0]);
699 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
703 udelay(pdata->reset_delays[1]);
705 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
709 udelay(pdata->reset_delays[2]);
715 static int sun8i_mdio_init(const char *name, struct udevice *priv)
717 struct mii_dev *bus = mdio_alloc();
720 debug("Failed to allocate MDIO bus\n");
724 bus->read = sun8i_mdio_read;
725 bus->write = sun8i_mdio_write;
726 snprintf(bus->name, sizeof(bus->name), name);
727 bus->priv = (void *)priv;
728 #if defined(CONFIG_DM_GPIO)
729 bus->reset = sun8i_mdio_reset;
732 return mdio_register(bus);
735 static int sun8i_emac_eth_start(struct udevice *dev)
737 struct eth_pdata *pdata = dev_get_platdata(dev);
739 return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
742 static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
744 struct emac_eth_dev *priv = dev_get_priv(dev);
746 return _sun8i_emac_eth_send(priv, packet, length);
749 static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
751 struct emac_eth_dev *priv = dev_get_priv(dev);
753 return _sun8i_eth_recv(priv, packetp);
756 static int _sun8i_free_pkt(struct emac_eth_dev *priv)
758 u32 desc_num = priv->rx_currdescnum;
759 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
760 uintptr_t desc_start = (uintptr_t)desc_p;
761 uintptr_t desc_end = desc_start +
762 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
764 /* Make the current descriptor valid again */
765 desc_p->status |= BIT(31);
767 /* Flush Status field of descriptor */
768 flush_dcache_range(desc_start, desc_end);
770 /* Move to next desc and wrap-around condition. */
771 if (++desc_num >= CONFIG_RX_DESCR_NUM)
773 priv->rx_currdescnum = desc_num;
778 static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
781 struct emac_eth_dev *priv = dev_get_priv(dev);
783 return _sun8i_free_pkt(priv);
786 static void sun8i_emac_eth_stop(struct udevice *dev)
788 struct emac_eth_dev *priv = dev_get_priv(dev);
790 /* Stop Rx/Tx transmitter */
791 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
792 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
795 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
797 phy_shutdown(priv->phydev);
800 static int sun8i_emac_eth_probe(struct udevice *dev)
802 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
803 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
804 struct emac_eth_dev *priv = dev_get_priv(dev);
806 priv->mac_reg = (void *)pdata->iobase;
808 sun8i_emac_board_setup(priv);
809 sun8i_emac_set_syscon(sun8i_pdata, priv);
811 sun8i_mdio_init(dev->name, dev);
812 priv->bus = miiphy_get_dev_by_name(dev->name);
814 return sun8i_phy_init(priv, dev);
817 static const struct eth_ops sun8i_emac_eth_ops = {
818 .start = sun8i_emac_eth_start,
819 .write_hwaddr = sun8i_eth_write_hwaddr,
820 .send = sun8i_emac_eth_send,
821 .recv = sun8i_emac_eth_recv,
822 .free_pkt = sun8i_eth_free_pkt,
823 .stop = sun8i_emac_eth_stop,
826 static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
828 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
829 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
830 struct emac_eth_dev *priv = dev_get_priv(dev);
831 const char *phy_mode;
833 int node = dev_of_offset(dev);
835 #ifdef CONFIG_DM_GPIO
836 int reset_flags = GPIOD_IS_OUT;
840 pdata->iobase = devfdt_get_addr(dev);
841 if (pdata->iobase == FDT_ADDR_T_NONE) {
842 debug("%s: Cannot find MAC base address\n", __func__);
846 priv->variant = dev_get_driver_data(dev);
848 if (!priv->variant) {
849 printf("%s: Missing variant\n", __func__);
853 if (priv->variant != R40_GMAC) {
854 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
856 debug("%s: cannot find syscon node\n", __func__);
859 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
861 debug("%s: cannot find reg property in syscon node\n",
865 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
867 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
868 debug("%s: Cannot find syscon base address\n",
874 pdata->phy_interface = -1;
876 priv->use_internal_phy = false;
878 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
880 debug("%s: Cannot find PHY address\n", __func__);
883 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
885 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
888 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
889 printf("phy interface%d\n", pdata->phy_interface);
891 if (pdata->phy_interface == -1) {
892 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
896 if (priv->variant == H3_EMAC) {
897 int parent = fdt_parent_offset(gd->fdt_blob, offset);
900 !fdt_node_check_compatible(gd->fdt_blob, parent,
901 "allwinner,sun8i-h3-mdio-internal"))
902 priv->use_internal_phy = true;
905 priv->interface = pdata->phy_interface;
907 if (!priv->use_internal_phy)
910 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
911 "allwinner,tx-delay-ps", 0);
912 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
913 printf("%s: Invalid TX delay value %d\n", __func__,
914 sun8i_pdata->tx_delay_ps);
916 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
917 "allwinner,rx-delay-ps", 0);
918 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
919 printf("%s: Invalid RX delay value %d\n", __func__,
920 sun8i_pdata->rx_delay_ps);
922 #ifdef CONFIG_DM_GPIO
923 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
924 "snps,reset-active-low"))
925 reset_flags |= GPIOD_ACTIVE_LOW;
927 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
928 &priv->reset_gpio, reset_flags);
931 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
932 "snps,reset-delays-us",
933 sun8i_pdata->reset_delays, 3);
934 } else if (ret == -ENOENT) {
942 static const struct udevice_id sun8i_emac_eth_ids[] = {
943 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
944 {.compatible = "allwinner,sun50i-a64-emac",
945 .data = (uintptr_t)A64_EMAC },
946 {.compatible = "allwinner,sun8i-a83t-emac",
947 .data = (uintptr_t)A83T_EMAC },
948 {.compatible = "allwinner,sun8i-r40-gmac",
949 .data = (uintptr_t)R40_GMAC },
953 U_BOOT_DRIVER(eth_sun8i_emac) = {
954 .name = "eth_sun8i_emac",
956 .of_match = sun8i_emac_eth_ids,
957 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
958 .probe = sun8i_emac_eth_probe,
959 .ops = &sun8i_emac_eth_ops,
960 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
961 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
962 .flags = DM_FLAG_ALLOC_PRIV_DMA,