1 // SPDX-License-Identifier: GPL-2.0+
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>
19 #include <asm/arch/clock.h>
23 #include <fdt_support.h>
24 #include <dm/device_compat.h>
25 #include <linux/bitops.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
32 #include <dt-bindings/pinctrl/sun4i-a10.h>
35 #define MDIO_CMD_MII_BUSY BIT(0)
36 #define MDIO_CMD_MII_WRITE BIT(1)
38 #define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
39 #define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
40 #define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
41 #define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
42 #define MDIO_CMD_MII_CLK_CSR_DIV_16 0x0
43 #define MDIO_CMD_MII_CLK_CSR_DIV_32 0x1
44 #define MDIO_CMD_MII_CLK_CSR_DIV_64 0x2
45 #define MDIO_CMD_MII_CLK_CSR_DIV_128 0x3
46 #define MDIO_CMD_MII_CLK_CSR_SHIFT 20
48 #define CONFIG_TX_DESCR_NUM 32
49 #define CONFIG_RX_DESCR_NUM 32
50 #define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
53 * The datasheet says that each descriptor can transfers up to 4096 bytes
54 * But later, the register documentation reduces that value to 2048,
55 * using 2048 cause strange behaviours and even BSP driver use 2047
57 #define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
59 #define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
60 #define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
62 #define H3_EPHY_DEFAULT_VALUE 0x58000
63 #define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
64 #define H3_EPHY_ADDR_SHIFT 20
65 #define REG_PHY_ADDR_MASK GENMASK(4, 0)
66 #define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
67 #define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
68 #define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
70 #define SC_RMII_EN BIT(13)
71 #define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
72 #define SC_ETCS_MASK GENMASK(1, 0)
73 #define SC_ETCS_EXT_GMII 0x1
74 #define SC_ETCS_INT_GMII 0x2
75 #define SC_ETXDC_MASK GENMASK(12, 10)
76 #define SC_ETXDC_OFFSET 10
77 #define SC_ERXDC_MASK GENMASK(9, 5)
78 #define SC_ERXDC_OFFSET 5
80 #define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
82 #define AHB_GATE_OFFSET_EPHY 0
85 #define SUN8I_IOMUX_H3 2
86 #define SUN8I_IOMUX_R40 5
87 #define SUN8I_IOMUX_H6 5
88 #define SUN8I_IOMUX_H616 2
91 /* H3/A64 EMAC Register's offset */
92 #define EMAC_CTL0 0x00
93 #define EMAC_CTL0_FULL_DUPLEX BIT(0)
94 #define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
95 #define EMAC_CTL0_SPEED_10 (0x2 << 2)
96 #define EMAC_CTL0_SPEED_100 (0x3 << 2)
97 #define EMAC_CTL0_SPEED_1000 (0x0 << 2)
98 #define EMAC_CTL1 0x04
99 #define EMAC_CTL1_SOFT_RST BIT(0)
100 #define EMAC_CTL1_BURST_LEN_SHIFT 24
101 #define EMAC_INT_STA 0x08
102 #define EMAC_INT_EN 0x0c
103 #define EMAC_TX_CTL0 0x10
104 #define EMAC_TX_CTL0_TX_EN BIT(31)
105 #define EMAC_TX_CTL1 0x14
106 #define EMAC_TX_CTL1_TX_MD BIT(1)
107 #define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
108 #define EMAC_TX_CTL1_TX_DMA_START BIT(31)
109 #define EMAC_TX_FLOW_CTL 0x1c
110 #define EMAC_TX_DMA_DESC 0x20
111 #define EMAC_RX_CTL0 0x24
112 #define EMAC_RX_CTL0_RX_EN BIT(31)
113 #define EMAC_RX_CTL1 0x28
114 #define EMAC_RX_CTL1_RX_MD BIT(1)
115 #define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
116 #define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
117 #define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
118 #define EMAC_RX_CTL1_RX_DMA_START BIT(31)
119 #define EMAC_RX_DMA_DESC 0x34
120 #define EMAC_MII_CMD 0x48
121 #define EMAC_MII_DATA 0x4c
122 #define EMAC_ADDR0_HIGH 0x50
123 #define EMAC_ADDR0_LOW 0x54
124 #define EMAC_TX_DMA_STA 0xb0
125 #define EMAC_TX_CUR_DESC 0xb4
126 #define EMAC_TX_CUR_BUF 0xb8
127 #define EMAC_RX_DMA_STA 0xc0
128 #define EMAC_RX_CUR_DESC 0xc4
130 #define EMAC_DESC_OWN_DMA BIT(31)
131 #define EMAC_DESC_LAST_DESC BIT(30)
132 #define EMAC_DESC_FIRST_DESC BIT(29)
133 #define EMAC_DESC_CHAIN_SECOND BIT(24)
135 #define EMAC_DESC_RX_ERROR_MASK 0x400068db
137 DECLARE_GLOBAL_DATA_PTR;
147 struct emac_dma_desc {
152 } __aligned(ARCH_DMA_MINALIGN);
154 struct emac_eth_dev {
155 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
156 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
157 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
158 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
170 bool use_internal_phy;
172 enum emac_variant variant;
174 phys_addr_t sysctl_reg;
175 struct phy_device *phydev;
179 struct reset_ctl tx_rst;
180 struct reset_ctl ephy_rst;
181 #if CONFIG_IS_ENABLED(DM_GPIO)
182 struct gpio_desc reset_gpio;
187 struct sun8i_eth_pdata {
188 struct eth_pdata eth_pdata;
195 static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
197 struct udevice *dev = bus->priv;
198 struct emac_eth_dev *priv = dev_get_priv(dev);
202 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
203 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
204 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
205 MDIO_CMD_MII_PHY_ADDR_MASK;
208 * The EMAC clock is either 200 or 300 MHz, so we need a divider
209 * of 128 to get the MDIO frequency below the required 2.5 MHz.
211 if (!priv->use_internal_phy)
212 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
213 MDIO_CMD_MII_CLK_CSR_SHIFT;
215 mii_cmd |= MDIO_CMD_MII_BUSY;
217 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
219 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
220 MDIO_CMD_MII_BUSY, false,
221 CONFIG_MDIO_TIMEOUT, true);
225 return readl(priv->mac_reg + EMAC_MII_DATA);
228 static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
231 struct udevice *dev = bus->priv;
232 struct emac_eth_dev *priv = dev_get_priv(dev);
235 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
236 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
237 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
238 MDIO_CMD_MII_PHY_ADDR_MASK;
241 * The EMAC clock is either 200 or 300 MHz, so we need a divider
242 * of 128 to get the MDIO frequency below the required 2.5 MHz.
244 if (!priv->use_internal_phy)
245 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
246 MDIO_CMD_MII_CLK_CSR_SHIFT;
248 mii_cmd |= MDIO_CMD_MII_WRITE;
249 mii_cmd |= MDIO_CMD_MII_BUSY;
251 writel(val, priv->mac_reg + EMAC_MII_DATA);
252 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
254 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
255 MDIO_CMD_MII_BUSY, false,
256 CONFIG_MDIO_TIMEOUT, true);
259 static int sun8i_eth_write_hwaddr(struct udevice *dev)
261 struct emac_eth_dev *priv = dev_get_priv(dev);
262 struct eth_pdata *pdata = dev_get_plat(dev);
263 uchar *mac_id = pdata->enetaddr;
264 u32 macid_lo, macid_hi;
266 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
268 macid_hi = mac_id[4] + (mac_id[5] << 8);
270 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
271 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
276 static void sun8i_adjust_link(struct emac_eth_dev *priv,
277 struct phy_device *phydev)
281 v = readl(priv->mac_reg + EMAC_CTL0);
284 v |= EMAC_CTL0_FULL_DUPLEX;
286 v &= ~EMAC_CTL0_FULL_DUPLEX;
288 v &= ~EMAC_CTL0_SPEED_MASK;
290 switch (phydev->speed) {
292 v |= EMAC_CTL0_SPEED_1000;
295 v |= EMAC_CTL0_SPEED_100;
298 v |= EMAC_CTL0_SPEED_10;
301 writel(v, priv->mac_reg + EMAC_CTL0);
304 static u32 sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 reg)
306 if (priv->use_internal_phy) {
307 /* H3 based SoC's that has an Internal 100MBit PHY
308 * needs to be configured and powered up before use
310 reg &= ~H3_EPHY_DEFAULT_MASK;
311 reg |= H3_EPHY_DEFAULT_VALUE;
312 reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
313 reg &= ~H3_EPHY_SHUTDOWN;
314 return reg | H3_EPHY_SELECT;
317 /* This is to select External Gigabit PHY on those boards with
318 * an internal PHY. Does not hurt on other SoCs. Linux does
321 return reg & ~H3_EPHY_SELECT;
324 static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
325 struct emac_eth_dev *priv)
329 if (priv->variant == R40_GMAC) {
330 /* Select RGMII for R40 */
331 reg = readl(priv->sysctl_reg + 0x164);
332 reg |= SC_ETCS_INT_GMII |
334 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
336 writel(reg, priv->sysctl_reg + 0x164);
340 reg = readl(priv->sysctl_reg + 0x30);
342 reg = sun8i_emac_set_syscon_ephy(priv, reg);
344 reg &= ~(SC_ETCS_MASK | SC_EPIT);
345 if (priv->variant == H3_EMAC ||
346 priv->variant == A64_EMAC ||
347 priv->variant == H6_EMAC)
350 switch (priv->interface) {
351 case PHY_INTERFACE_MODE_MII:
354 case PHY_INTERFACE_MODE_RGMII:
355 case PHY_INTERFACE_MODE_RGMII_ID:
356 case PHY_INTERFACE_MODE_RGMII_RXID:
357 case PHY_INTERFACE_MODE_RGMII_TXID:
358 reg |= SC_EPIT | SC_ETCS_INT_GMII;
360 case PHY_INTERFACE_MODE_RMII:
361 if (priv->variant == H3_EMAC ||
362 priv->variant == A64_EMAC ||
363 priv->variant == H6_EMAC) {
364 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
367 /* RMII not supported on A83T */
369 debug("%s: Invalid PHY interface\n", __func__);
373 if (pdata->tx_delay_ps)
374 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
377 if (pdata->rx_delay_ps)
378 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
381 writel(reg, priv->sysctl_reg + 0x30);
386 static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
388 struct phy_device *phydev;
390 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
394 phy_connect_dev(phydev, dev);
396 priv->phydev = phydev;
397 phy_config(priv->phydev);
402 #define cache_clean_descriptor(desc) \
403 flush_dcache_range((uintptr_t)(desc), \
404 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
406 #define cache_inv_descriptor(desc) \
407 invalidate_dcache_range((uintptr_t)(desc), \
408 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
410 static void rx_descs_init(struct emac_eth_dev *priv)
412 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
413 char *rxbuffs = &priv->rxbuffer[0];
414 struct emac_dma_desc *desc_p;
418 * Make sure we don't have dirty cache lines around, which could
419 * be cleaned to DRAM *after* the MAC has already written data to it.
421 invalidate_dcache_range((uintptr_t)desc_table_p,
422 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
423 invalidate_dcache_range((uintptr_t)rxbuffs,
424 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
426 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
427 desc_p = &desc_table_p[i];
428 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
429 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
430 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
431 desc_p->status = EMAC_DESC_OWN_DMA;
434 /* Correcting the last pointer of the chain */
435 desc_p->next = (uintptr_t)&desc_table_p[0];
437 flush_dcache_range((uintptr_t)priv->rx_chain,
438 (uintptr_t)priv->rx_chain +
439 sizeof(priv->rx_chain));
441 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
442 priv->rx_currdescnum = 0;
445 static void tx_descs_init(struct emac_eth_dev *priv)
447 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
448 char *txbuffs = &priv->txbuffer[0];
449 struct emac_dma_desc *desc_p;
452 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
453 desc_p = &desc_table_p[i];
454 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
455 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
456 desc_p->ctl_size = 0;
460 /* Correcting the last pointer of the chain */
461 desc_p->next = (uintptr_t)&desc_table_p[0];
463 /* Flush the first TX buffer descriptor we will tell the MAC about. */
464 cache_clean_descriptor(desc_table_p);
466 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
467 priv->tx_currdescnum = 0;
470 static int sun8i_emac_eth_start(struct udevice *dev)
472 struct emac_eth_dev *priv = dev_get_priv(dev);
476 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
477 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
478 EMAC_CTL1_SOFT_RST, false, 10, true);
480 printf("%s: Timeout\n", __func__);
484 /* Rewrite mac address after reset */
485 sun8i_eth_write_hwaddr(dev);
487 /* transmission starts after the full frame arrived in TX DMA FIFO */
488 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
491 * RX DMA reads data from RX DMA FIFO to host memory after a
492 * complete frame has been written to RX DMA FIFO
494 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
496 /* DMA burst length */
497 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
499 /* Initialize rx/tx descriptors */
504 ret = phy_startup(priv->phydev);
508 sun8i_adjust_link(priv, priv->phydev);
510 /* Start RX/TX DMA */
511 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
512 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
513 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
516 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
517 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
522 static int parse_phy_pins(struct udevice *dev)
525 const char *pin_name;
526 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
529 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
532 printf("WARNING: emac: cannot find pinctrl-0 node\n");
536 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
537 "drive-strength", ~0);
540 drive = SUN4I_PINCTRL_10_MA;
541 else if (drive <= 20)
542 drive = SUN4I_PINCTRL_20_MA;
543 else if (drive <= 30)
544 drive = SUN4I_PINCTRL_30_MA;
546 drive = SUN4I_PINCTRL_40_MA;
549 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
550 pull = SUN4I_PINCTRL_PULL_UP;
551 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
552 pull = SUN4I_PINCTRL_PULL_DOWN;
555 * The GPIO pinmux value is an integration choice, so depends on the
556 * SoC, not the EMAC variant.
558 if (IS_ENABLED(CONFIG_MACH_SUNXI_H3_H5))
559 iomux = SUN8I_IOMUX_H3;
560 else if (IS_ENABLED(CONFIG_MACH_SUN8I_R40))
561 iomux = SUN8I_IOMUX_R40;
562 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
563 iomux = SUN8I_IOMUX_H6;
564 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H616))
565 iomux = SUN8I_IOMUX_H616;
566 else if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T))
568 else if (IS_ENABLED(CONFIG_MACH_SUN50I))
571 BUILD_BUG_ON_MSG(1, "missing pinmux value for Ethernet pins");
576 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
581 pin = sunxi_name_to_gpio(pin_name);
585 sunxi_gpio_set_cfgpin(pin, iomux);
588 sunxi_gpio_set_drv(pin, drive);
590 sunxi_gpio_set_pull(pin, pull);
594 printf("WARNING: emac: cannot find pins property\n");
601 static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
603 struct emac_eth_dev *priv = dev_get_priv(dev);
604 u32 status, desc_num = priv->rx_currdescnum;
605 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
606 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
609 /* Invalidate entire buffer descriptor */
610 cache_inv_descriptor(desc_p);
612 status = desc_p->status;
614 /* Check for DMA own bit */
615 if (status & EMAC_DESC_OWN_DMA)
618 length = (status >> 16) & 0x3fff;
620 /* make sure we read from DRAM, not our cache */
621 invalidate_dcache_range(data_start,
622 data_start + roundup(length, ARCH_DMA_MINALIGN));
624 if (status & EMAC_DESC_RX_ERROR_MASK) {
625 debug("RX: packet error: 0x%x\n",
626 status & EMAC_DESC_RX_ERROR_MASK);
630 debug("RX: Bad Packet (runt)\n");
634 if (length > CONFIG_ETH_RXSIZE) {
635 debug("RX: Too large packet (%d bytes)\n", length);
639 *packetp = (uchar *)(ulong)desc_p->buf_addr;
644 static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
646 struct emac_eth_dev *priv = dev_get_priv(dev);
647 u32 desc_num = priv->tx_currdescnum;
648 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
649 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
650 uintptr_t data_end = data_start +
651 roundup(length, ARCH_DMA_MINALIGN);
653 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
655 memcpy((void *)data_start, packet, length);
657 /* Flush data to be sent */
658 flush_dcache_range(data_start, data_end);
660 /* frame begin and end */
661 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
662 desc_p->status = EMAC_DESC_OWN_DMA;
664 /* make sure the MAC reads the actual data from DRAM */
665 cache_clean_descriptor(desc_p);
667 /* Move to next Descriptor and wrap around */
668 if (++desc_num >= CONFIG_TX_DESCR_NUM)
670 priv->tx_currdescnum = desc_num;
673 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
676 * Since we copied the data above, we return here without waiting
677 * for the packet to be actually send out.
683 static int sun8i_emac_board_setup(struct udevice *dev,
684 struct emac_eth_dev *priv)
688 ret = clk_enable(&priv->tx_clk);
690 dev_err(dev, "failed to enable TX clock\n");
694 if (reset_valid(&priv->tx_rst)) {
695 ret = reset_deassert(&priv->tx_rst);
697 dev_err(dev, "failed to deassert TX reset\n");
702 /* Only H3/H5 have clock controls for internal EPHY */
703 if (clk_valid(&priv->ephy_clk)) {
704 ret = clk_enable(&priv->ephy_clk);
706 dev_err(dev, "failed to enable EPHY TX clock\n");
711 if (reset_valid(&priv->ephy_rst)) {
712 ret = reset_deassert(&priv->ephy_rst);
714 dev_err(dev, "failed to deassert EPHY TX clock\n");
722 clk_disable(&priv->tx_clk);
726 #if CONFIG_IS_ENABLED(DM_GPIO)
727 static int sun8i_mdio_reset(struct mii_dev *bus)
729 struct udevice *dev = bus->priv;
730 struct emac_eth_dev *priv = dev_get_priv(dev);
731 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
734 if (!dm_gpio_is_valid(&priv->reset_gpio))
738 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
742 udelay(pdata->reset_delays[0]);
744 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
748 udelay(pdata->reset_delays[1]);
750 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
754 udelay(pdata->reset_delays[2]);
760 static int sun8i_mdio_init(const char *name, struct udevice *priv)
762 struct mii_dev *bus = mdio_alloc();
765 debug("Failed to allocate MDIO bus\n");
769 bus->read = sun8i_mdio_read;
770 bus->write = sun8i_mdio_write;
771 snprintf(bus->name, sizeof(bus->name), name);
772 bus->priv = (void *)priv;
773 #if CONFIG_IS_ENABLED(DM_GPIO)
774 bus->reset = sun8i_mdio_reset;
777 return mdio_register(bus);
780 static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
783 struct emac_eth_dev *priv = dev_get_priv(dev);
784 u32 desc_num = priv->rx_currdescnum;
785 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
787 /* give the current descriptor back to the MAC */
788 desc_p->status |= EMAC_DESC_OWN_DMA;
790 /* Flush Status field of descriptor */
791 cache_clean_descriptor(desc_p);
793 /* Move to next desc and wrap-around condition. */
794 if (++desc_num >= CONFIG_RX_DESCR_NUM)
796 priv->rx_currdescnum = desc_num;
801 static void sun8i_emac_eth_stop(struct udevice *dev)
803 struct emac_eth_dev *priv = dev_get_priv(dev);
805 /* Stop Rx/Tx transmitter */
806 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
807 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
810 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
811 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
813 phy_shutdown(priv->phydev);
816 static int sun8i_emac_eth_probe(struct udevice *dev)
818 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
819 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
820 struct emac_eth_dev *priv = dev_get_priv(dev);
823 priv->mac_reg = (void *)pdata->iobase;
825 ret = sun8i_emac_board_setup(dev, priv);
829 sun8i_emac_set_syscon(sun8i_pdata, priv);
831 sun8i_mdio_init(dev->name, dev);
832 priv->bus = miiphy_get_dev_by_name(dev->name);
834 return sun8i_phy_init(priv, dev);
837 static const struct eth_ops sun8i_emac_eth_ops = {
838 .start = sun8i_emac_eth_start,
839 .write_hwaddr = sun8i_eth_write_hwaddr,
840 .send = sun8i_emac_eth_send,
841 .recv = sun8i_emac_eth_recv,
842 .free_pkt = sun8i_eth_free_pkt,
843 .stop = sun8i_emac_eth_stop,
846 static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
848 struct ofnode_phandle_args phandle;
851 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
852 NULL, 0, 0, &phandle);
856 /* If the PHY node is not a child of the internal MDIO bus, we are
857 * using some external PHY.
859 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
860 "allwinner,sun8i-h3-mdio-internal"))
863 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
865 dev_err(dev, "failed to get EPHY TX clock\n");
869 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
871 dev_err(dev, "failed to get EPHY TX reset\n");
875 priv->use_internal_phy = true;
880 static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
882 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
883 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
884 struct emac_eth_dev *priv = dev_get_priv(dev);
885 const char *phy_mode;
887 int node = dev_of_offset(dev);
889 #if CONFIG_IS_ENABLED(DM_GPIO)
890 int reset_flags = GPIOD_IS_OUT;
894 pdata->iobase = dev_read_addr(dev);
895 if (pdata->iobase == FDT_ADDR_T_NONE) {
896 debug("%s: Cannot find MAC base address\n", __func__);
900 priv->variant = dev_get_driver_data(dev);
902 if (!priv->variant) {
903 printf("%s: Missing variant\n", __func__);
907 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
909 dev_err(dev, "failed to get TX clock\n");
913 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
914 if (ret && ret != -ENOENT) {
915 dev_err(dev, "failed to get TX reset\n");
919 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
921 debug("%s: cannot find syscon node\n", __func__);
925 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
927 debug("%s: cannot find reg property in syscon node\n",
931 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
933 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
934 debug("%s: Cannot find syscon base address\n", __func__);
938 pdata->phy_interface = -1;
940 priv->use_internal_phy = false;
942 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
944 debug("%s: Cannot find PHY address\n", __func__);
947 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
949 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
952 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
953 printf("phy interface%d\n", pdata->phy_interface);
955 if (pdata->phy_interface == -1) {
956 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
960 if (priv->variant == H3_EMAC) {
961 ret = sun8i_handle_internal_phy(dev, priv);
966 priv->interface = pdata->phy_interface;
968 if (!priv->use_internal_phy)
971 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
972 "allwinner,tx-delay-ps", 0);
973 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
974 printf("%s: Invalid TX delay value %d\n", __func__,
975 sun8i_pdata->tx_delay_ps);
977 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
978 "allwinner,rx-delay-ps", 0);
979 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
980 printf("%s: Invalid RX delay value %d\n", __func__,
981 sun8i_pdata->rx_delay_ps);
983 #if CONFIG_IS_ENABLED(DM_GPIO)
984 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
985 "snps,reset-active-low"))
986 reset_flags |= GPIOD_ACTIVE_LOW;
988 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
989 &priv->reset_gpio, reset_flags);
992 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
993 "snps,reset-delays-us",
994 sun8i_pdata->reset_delays, 3);
995 } else if (ret == -ENOENT) {
1003 static const struct udevice_id sun8i_emac_eth_ids[] = {
1004 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
1005 {.compatible = "allwinner,sun50i-a64-emac",
1006 .data = (uintptr_t)A64_EMAC },
1007 {.compatible = "allwinner,sun8i-a83t-emac",
1008 .data = (uintptr_t)A83T_EMAC },
1009 {.compatible = "allwinner,sun8i-r40-gmac",
1010 .data = (uintptr_t)R40_GMAC },
1011 {.compatible = "allwinner,sun50i-h6-emac",
1012 .data = (uintptr_t)H6_EMAC },
1016 U_BOOT_DRIVER(eth_sun8i_emac) = {
1017 .name = "eth_sun8i_emac",
1019 .of_match = sun8i_emac_eth_ids,
1020 .of_to_plat = sun8i_emac_eth_of_to_plat,
1021 .probe = sun8i_emac_eth_probe,
1022 .ops = &sun8i_emac_eth_ops,
1023 .priv_auto = sizeof(struct emac_eth_dev),
1024 .plat_auto = sizeof(struct sun8i_eth_pdata),
1025 .flags = DM_FLAG_ALLOC_PRIV_DMA,