Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 |
| 4 | * Author: Amit Singh Tomar, amittomer25@gmail.com |
| 5 | * |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 6 | * 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 Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 13 | #include <cpu_func.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 15 | #include <asm/cache.h> |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 16 | #include <asm/io.h> |
| 17 | #include <asm/arch/clock.h> |
| 18 | #include <asm/arch/gpio.h> |
| 19 | #include <common.h> |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 20 | #include <clk.h> |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 21 | #include <dm.h> |
| 22 | #include <fdt_support.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 23 | #include <dm/device_compat.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 24 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 25 | #include <linux/delay.h> |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 26 | #include <linux/err.h> |
| 27 | #include <malloc.h> |
| 28 | #include <miiphy.h> |
| 29 | #include <net.h> |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 30 | #include <reset.h> |
Andre Przywara | c034117 | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 31 | #include <dt-bindings/pinctrl/sun4i-a10.h> |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 32 | #include <wait_bit.h> |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 33 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 34 | #include <asm-generic/gpio.h> |
| 35 | #endif |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 36 | |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 37 | #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 Goede | 4069437 | 2016-07-27 17:31:17 +0200 | [diff] [blame] | 47 | #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 Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 55 | |
| 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 Zheng | 9b16ede | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 72 | #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 Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 76 | |
| 77 | #define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ) |
| 78 | |
| 79 | #define AHB_GATE_OFFSET_EPHY 0 |
| 80 | |
Lothar Felten | c6a21d6 | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 81 | /* IO mux settings */ |
| 82 | #define SUN8I_IOMUX_H3 2 |
Lothar Felten | e46d73f | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 83 | #define SUN8I_IOMUX_R40 5 |
Lothar Felten | c6a21d6 | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 84 | #define SUN8I_IOMUX 4 |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 85 | |
| 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 | |
| 108 | DECLARE_GLOBAL_DATA_PTR; |
| 109 | |
| 110 | enum emac_variant { |
| 111 | A83T_EMAC = 1, |
| 112 | H3_EMAC, |
| 113 | A64_EMAC, |
Lothar Felten | e46d73f | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 114 | R40_GMAC, |
Samuel Holland | 99ac861 | 2020-05-07 18:10:51 -0500 | [diff] [blame] | 115 | H6_EMAC, |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | struct emac_dma_desc { |
| 119 | u32 status; |
| 120 | u32 st; |
| 121 | u32 buf_addr; |
| 122 | u32 next; |
| 123 | } __aligned(ARCH_DMA_MINALIGN); |
| 124 | |
| 125 | struct 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 Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 148 | struct clk tx_clk; |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 149 | struct clk ephy_clk; |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 150 | struct reset_ctl tx_rst; |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 151 | struct reset_ctl ephy_rst; |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 152 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 153 | struct gpio_desc reset_gpio; |
| 154 | #endif |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 155 | }; |
| 156 | |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 157 | |
| 158 | struct sun8i_eth_pdata { |
| 159 | struct eth_pdata eth_pdata; |
| 160 | u32 reset_delays[3]; |
Icenowy Zheng | 9b16ede | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 161 | int tx_delay_ps; |
| 162 | int rx_delay_ps; |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 166 | static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg) |
| 167 | { |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 168 | struct udevice *dev = bus->priv; |
| 169 | struct emac_eth_dev *priv = dev_get_priv(dev); |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 170 | u32 mii_cmd; |
| 171 | int ret; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 172 | |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 173 | mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) & |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 174 | MDIO_CMD_MII_PHY_REG_ADDR_MASK; |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 175 | mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) & |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 176 | MDIO_CMD_MII_PHY_ADDR_MASK; |
| 177 | |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 178 | mii_cmd |= MDIO_CMD_MII_BUSY; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 179 | |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 180 | writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 181 | |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 182 | 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 Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 187 | |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 188 | return readl(priv->mac_reg + EMAC_MII_DATA); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg, |
| 192 | u16 val) |
| 193 | { |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 194 | struct udevice *dev = bus->priv; |
| 195 | struct emac_eth_dev *priv = dev_get_priv(dev); |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 196 | u32 mii_cmd; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 197 | |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 198 | mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) & |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 199 | MDIO_CMD_MII_PHY_REG_ADDR_MASK; |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 200 | mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) & |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 201 | MDIO_CMD_MII_PHY_ADDR_MASK; |
| 202 | |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 203 | mii_cmd |= MDIO_CMD_MII_WRITE; |
| 204 | mii_cmd |= MDIO_CMD_MII_BUSY; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 205 | |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 206 | writel(val, priv->mac_reg + EMAC_MII_DATA); |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 207 | writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 208 | |
Andre Przywara | f20f946 | 2020-07-06 01:40:34 +0100 | [diff] [blame^] | 209 | return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD, |
| 210 | MDIO_CMD_MII_BUSY, false, |
| 211 | CONFIG_MDIO_TIMEOUT, true); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id) |
| 215 | { |
| 216 | u32 macid_lo, macid_hi; |
| 217 | |
| 218 | macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) + |
| 219 | (mac_id[3] << 24); |
| 220 | macid_hi = mac_id[4] + (mac_id[5] << 8); |
| 221 | |
| 222 | writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH); |
| 223 | writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW); |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | static void sun8i_adjust_link(struct emac_eth_dev *priv, |
| 229 | struct phy_device *phydev) |
| 230 | { |
| 231 | u32 v; |
| 232 | |
| 233 | v = readl(priv->mac_reg + EMAC_CTL0); |
| 234 | |
| 235 | if (phydev->duplex) |
| 236 | v |= BIT(0); |
| 237 | else |
| 238 | v &= ~BIT(0); |
| 239 | |
| 240 | v &= ~0x0C; |
| 241 | |
| 242 | switch (phydev->speed) { |
| 243 | case 1000: |
| 244 | break; |
| 245 | case 100: |
| 246 | v |= BIT(2); |
| 247 | v |= BIT(3); |
| 248 | break; |
| 249 | case 10: |
| 250 | v |= BIT(3); |
| 251 | break; |
| 252 | } |
| 253 | writel(v, priv->mac_reg + EMAC_CTL0); |
| 254 | } |
| 255 | |
| 256 | static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg) |
| 257 | { |
| 258 | if (priv->use_internal_phy) { |
| 259 | /* H3 based SoC's that has an Internal 100MBit PHY |
| 260 | * needs to be configured and powered up before use |
| 261 | */ |
| 262 | *reg &= ~H3_EPHY_DEFAULT_MASK; |
| 263 | *reg |= H3_EPHY_DEFAULT_VALUE; |
| 264 | *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT; |
| 265 | *reg &= ~H3_EPHY_SHUTDOWN; |
| 266 | *reg |= H3_EPHY_SELECT; |
| 267 | } else |
| 268 | /* This is to select External Gigabit PHY on |
| 269 | * the boards with H3 SoC. |
| 270 | */ |
| 271 | *reg &= ~H3_EPHY_SELECT; |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Icenowy Zheng | 9b16ede | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 276 | static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata, |
| 277 | struct emac_eth_dev *priv) |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 278 | { |
| 279 | int ret; |
| 280 | u32 reg; |
| 281 | |
Jagan Teki | 695f604 | 2019-02-28 00:26:51 +0530 | [diff] [blame] | 282 | if (priv->variant == R40_GMAC) { |
| 283 | /* Select RGMII for R40 */ |
| 284 | reg = readl(priv->sysctl_reg + 0x164); |
Samuel Holland | abdbefb | 2020-05-07 18:10:50 -0500 | [diff] [blame] | 285 | reg |= SC_ETCS_INT_GMII | |
| 286 | SC_EPIT | |
| 287 | (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 288 | |
Jagan Teki | 695f604 | 2019-02-28 00:26:51 +0530 | [diff] [blame] | 289 | writel(reg, priv->sysctl_reg + 0x164); |
Lothar Felten | e46d73f | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 290 | return 0; |
Jagan Teki | 695f604 | 2019-02-28 00:26:51 +0530 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | reg = readl(priv->sysctl_reg + 0x30); |
Lothar Felten | e46d73f | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 294 | |
Samuel Holland | 99ac861 | 2020-05-07 18:10:51 -0500 | [diff] [blame] | 295 | if (priv->variant == H3_EMAC || priv->variant == H6_EMAC) { |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 296 | ret = sun8i_emac_set_syscon_ephy(priv, ®); |
| 297 | if (ret) |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | reg &= ~(SC_ETCS_MASK | SC_EPIT); |
Samuel Holland | 99ac861 | 2020-05-07 18:10:51 -0500 | [diff] [blame] | 302 | if (priv->variant == H3_EMAC || |
| 303 | priv->variant == A64_EMAC || |
| 304 | priv->variant == H6_EMAC) |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 305 | reg &= ~SC_RMII_EN; |
| 306 | |
| 307 | switch (priv->interface) { |
| 308 | case PHY_INTERFACE_MODE_MII: |
| 309 | /* default */ |
| 310 | break; |
| 311 | case PHY_INTERFACE_MODE_RGMII: |
| 312 | reg |= SC_EPIT | SC_ETCS_INT_GMII; |
| 313 | break; |
| 314 | case PHY_INTERFACE_MODE_RMII: |
| 315 | if (priv->variant == H3_EMAC || |
Samuel Holland | 99ac861 | 2020-05-07 18:10:51 -0500 | [diff] [blame] | 316 | priv->variant == A64_EMAC || |
| 317 | priv->variant == H6_EMAC) { |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 318 | reg |= SC_RMII_EN | SC_ETCS_EXT_GMII; |
| 319 | break; |
| 320 | } |
| 321 | /* RMII not supported on A83T */ |
| 322 | default: |
| 323 | debug("%s: Invalid PHY interface\n", __func__); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
Icenowy Zheng | 9b16ede | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 327 | if (pdata->tx_delay_ps) |
| 328 | reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET) |
| 329 | & SC_ETXDC_MASK; |
| 330 | |
| 331 | if (pdata->rx_delay_ps) |
| 332 | reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET) |
| 333 | & SC_ERXDC_MASK; |
| 334 | |
Andre Przywara | 12afd95 | 2018-04-04 01:31:16 +0100 | [diff] [blame] | 335 | writel(reg, priv->sysctl_reg + 0x30); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev) |
| 341 | { |
| 342 | struct phy_device *phydev; |
| 343 | |
| 344 | phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface); |
| 345 | if (!phydev) |
| 346 | return -ENODEV; |
| 347 | |
| 348 | phy_connect_dev(phydev, dev); |
| 349 | |
| 350 | priv->phydev = phydev; |
| 351 | phy_config(priv->phydev); |
| 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static void rx_descs_init(struct emac_eth_dev *priv) |
| 357 | { |
| 358 | struct emac_dma_desc *desc_table_p = &priv->rx_chain[0]; |
| 359 | char *rxbuffs = &priv->rxbuffer[0]; |
| 360 | struct emac_dma_desc *desc_p; |
| 361 | u32 idx; |
| 362 | |
| 363 | /* flush Rx buffers */ |
| 364 | flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs + |
| 365 | RX_TOTAL_BUFSIZE); |
| 366 | |
| 367 | for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) { |
| 368 | desc_p = &desc_table_p[idx]; |
| 369 | desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE] |
| 370 | ; |
| 371 | desc_p->next = (uintptr_t)&desc_table_p[idx + 1]; |
Hans de Goede | 4069437 | 2016-07-27 17:31:17 +0200 | [diff] [blame] | 372 | desc_p->st |= CONFIG_ETH_RXSIZE; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 373 | desc_p->status = BIT(31); |
| 374 | } |
| 375 | |
| 376 | /* Correcting the last pointer of the chain */ |
| 377 | desc_p->next = (uintptr_t)&desc_table_p[0]; |
| 378 | |
| 379 | flush_dcache_range((uintptr_t)priv->rx_chain, |
| 380 | (uintptr_t)priv->rx_chain + |
| 381 | sizeof(priv->rx_chain)); |
| 382 | |
| 383 | writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC)); |
| 384 | priv->rx_currdescnum = 0; |
| 385 | } |
| 386 | |
| 387 | static void tx_descs_init(struct emac_eth_dev *priv) |
| 388 | { |
| 389 | struct emac_dma_desc *desc_table_p = &priv->tx_chain[0]; |
| 390 | char *txbuffs = &priv->txbuffer[0]; |
| 391 | struct emac_dma_desc *desc_p; |
| 392 | u32 idx; |
| 393 | |
| 394 | for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) { |
| 395 | desc_p = &desc_table_p[idx]; |
| 396 | desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE] |
| 397 | ; |
| 398 | desc_p->next = (uintptr_t)&desc_table_p[idx + 1]; |
Andre Przywara | c35380c | 2020-07-06 01:40:33 +0100 | [diff] [blame] | 399 | desc_p->status = 0; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 400 | desc_p->st = 0; |
| 401 | } |
| 402 | |
| 403 | /* Correcting the last pointer of the chain */ |
| 404 | desc_p->next = (uintptr_t)&desc_table_p[0]; |
| 405 | |
| 406 | /* Flush all Tx buffer descriptors */ |
| 407 | flush_dcache_range((uintptr_t)priv->tx_chain, |
| 408 | (uintptr_t)priv->tx_chain + |
| 409 | sizeof(priv->tx_chain)); |
| 410 | |
| 411 | writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC); |
| 412 | priv->tx_currdescnum = 0; |
| 413 | } |
| 414 | |
| 415 | static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr) |
| 416 | { |
| 417 | u32 reg, v; |
| 418 | int timeout = 100; |
Andre Przywara | 2808cf6 | 2020-07-06 01:40:32 +0100 | [diff] [blame] | 419 | int ret; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 420 | |
| 421 | reg = readl((priv->mac_reg + EMAC_CTL1)); |
| 422 | |
| 423 | if (!(reg & 0x1)) { |
| 424 | /* Soft reset MAC */ |
| 425 | setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1); |
| 426 | do { |
| 427 | reg = readl(priv->mac_reg + EMAC_CTL1); |
| 428 | } while ((reg & 0x01) != 0 && (--timeout)); |
| 429 | if (!timeout) { |
| 430 | printf("%s: Timeout\n", __func__); |
| 431 | return -1; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | /* Rewrite mac address after reset */ |
| 436 | _sun8i_write_hwaddr(priv, enetaddr); |
| 437 | |
| 438 | v = readl(priv->mac_reg + EMAC_TX_CTL1); |
| 439 | /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/ |
| 440 | v |= BIT(1); |
| 441 | writel(v, priv->mac_reg + EMAC_TX_CTL1); |
| 442 | |
| 443 | v = readl(priv->mac_reg + EMAC_RX_CTL1); |
| 444 | /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a |
| 445 | * complete frame has been written to RX DMA FIFO |
| 446 | */ |
| 447 | v |= BIT(1); |
| 448 | writel(v, priv->mac_reg + EMAC_RX_CTL1); |
| 449 | |
| 450 | /* DMA */ |
| 451 | writel(8 << 24, priv->mac_reg + EMAC_CTL1); |
| 452 | |
| 453 | /* Initialize rx/tx descriptors */ |
| 454 | rx_descs_init(priv); |
| 455 | tx_descs_init(priv); |
| 456 | |
| 457 | /* PHY Start Up */ |
Andre Przywara | 2808cf6 | 2020-07-06 01:40:32 +0100 | [diff] [blame] | 458 | ret = phy_startup(priv->phydev); |
| 459 | if (ret) |
| 460 | return ret; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 461 | |
| 462 | sun8i_adjust_link(priv, priv->phydev); |
| 463 | |
| 464 | /* Start RX DMA */ |
| 465 | v = readl(priv->mac_reg + EMAC_RX_CTL1); |
| 466 | v |= BIT(30); |
| 467 | writel(v, priv->mac_reg + EMAC_RX_CTL1); |
| 468 | /* Start TX DMA */ |
| 469 | v = readl(priv->mac_reg + EMAC_TX_CTL1); |
| 470 | v |= BIT(30); |
| 471 | writel(v, priv->mac_reg + EMAC_TX_CTL1); |
| 472 | |
| 473 | /* Enable RX/TX */ |
| 474 | setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31)); |
| 475 | setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31)); |
| 476 | |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int parse_phy_pins(struct udevice *dev) |
| 481 | { |
Lothar Felten | c6a21d6 | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 482 | struct emac_eth_dev *priv = dev_get_priv(dev); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 483 | int offset; |
| 484 | const char *pin_name; |
Andre Przywara | ecd0cec | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 485 | int drive, pull = SUN4I_PINCTRL_NO_PULL, i; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 486 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 487 | offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev), |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 488 | "pinctrl-0"); |
| 489 | if (offset < 0) { |
| 490 | printf("WARNING: emac: cannot find pinctrl-0 node\n"); |
| 491 | return offset; |
| 492 | } |
| 493 | |
| 494 | drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0, |
Andre Przywara | c034117 | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 495 | "drive-strength", ~0); |
| 496 | if (drive != ~0) { |
| 497 | if (drive <= 10) |
| 498 | drive = SUN4I_PINCTRL_10_MA; |
| 499 | else if (drive <= 20) |
| 500 | drive = SUN4I_PINCTRL_20_MA; |
| 501 | else if (drive <= 30) |
| 502 | drive = SUN4I_PINCTRL_30_MA; |
| 503 | else |
| 504 | drive = SUN4I_PINCTRL_40_MA; |
Andre Przywara | c034117 | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL)) |
| 508 | pull = SUN4I_PINCTRL_PULL_UP; |
Andre Przywara | c034117 | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 509 | else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL)) |
| 510 | pull = SUN4I_PINCTRL_PULL_DOWN; |
Andre Przywara | ecd0cec | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 511 | |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 512 | for (i = 0; ; i++) { |
| 513 | int pin; |
| 514 | |
Simon Glass | b02e404 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 515 | pin_name = fdt_stringlist_get(gd->fdt_blob, offset, |
Andre Przywara | ecd0cec | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 516 | "pins", i, NULL); |
| 517 | if (!pin_name) |
| 518 | break; |
Andre Przywara | c034117 | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 519 | |
| 520 | pin = sunxi_name_to_gpio(pin_name); |
| 521 | if (pin < 0) |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 522 | continue; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 523 | |
Lothar Felten | c6a21d6 | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 524 | if (priv->variant == H3_EMAC) |
| 525 | sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3); |
Samuel Holland | 99ac861 | 2020-05-07 18:10:51 -0500 | [diff] [blame] | 526 | else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC) |
Lothar Felten | e46d73f | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 527 | sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40); |
Lothar Felten | c6a21d6 | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 528 | else |
| 529 | sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX); |
| 530 | |
Andre Przywara | c034117 | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 531 | if (drive != ~0) |
| 532 | sunxi_gpio_set_drv(pin, drive); |
| 533 | if (pull != ~0) |
| 534 | sunxi_gpio_set_pull(pin, pull); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | if (!i) { |
Andre Przywara | c034117 | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 538 | printf("WARNING: emac: cannot find pins property\n"); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 539 | return -2; |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp) |
| 546 | { |
| 547 | u32 status, desc_num = priv->rx_currdescnum; |
| 548 | struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num]; |
| 549 | int length = -EAGAIN; |
| 550 | int good_packet = 1; |
| 551 | uintptr_t desc_start = (uintptr_t)desc_p; |
| 552 | uintptr_t desc_end = desc_start + |
| 553 | roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN); |
| 554 | |
| 555 | ulong data_start = (uintptr_t)desc_p->buf_addr; |
| 556 | ulong data_end; |
| 557 | |
| 558 | /* Invalidate entire buffer descriptor */ |
| 559 | invalidate_dcache_range(desc_start, desc_end); |
| 560 | |
| 561 | status = desc_p->status; |
| 562 | |
| 563 | /* Check for DMA own bit */ |
| 564 | if (!(status & BIT(31))) { |
| 565 | length = (desc_p->status >> 16) & 0x3FFF; |
| 566 | |
| 567 | if (length < 0x40) { |
| 568 | good_packet = 0; |
| 569 | debug("RX: Bad Packet (runt)\n"); |
| 570 | } |
| 571 | |
| 572 | data_end = data_start + length; |
| 573 | /* Invalidate received data */ |
| 574 | invalidate_dcache_range(rounddown(data_start, |
| 575 | ARCH_DMA_MINALIGN), |
| 576 | roundup(data_end, |
| 577 | ARCH_DMA_MINALIGN)); |
| 578 | if (good_packet) { |
Hans de Goede | 4069437 | 2016-07-27 17:31:17 +0200 | [diff] [blame] | 579 | if (length > CONFIG_ETH_RXSIZE) { |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 580 | printf("Received packet is too big (len=%d)\n", |
| 581 | length); |
| 582 | return -EMSGSIZE; |
| 583 | } |
| 584 | *packetp = (uchar *)(ulong)desc_p->buf_addr; |
| 585 | return length; |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | return length; |
| 590 | } |
| 591 | |
| 592 | static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet, |
| 593 | int len) |
| 594 | { |
| 595 | u32 v, desc_num = priv->tx_currdescnum; |
| 596 | struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num]; |
| 597 | uintptr_t desc_start = (uintptr_t)desc_p; |
| 598 | uintptr_t desc_end = desc_start + |
| 599 | roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN); |
| 600 | |
| 601 | uintptr_t data_start = (uintptr_t)desc_p->buf_addr; |
| 602 | uintptr_t data_end = data_start + |
| 603 | roundup(len, ARCH_DMA_MINALIGN); |
| 604 | |
| 605 | /* Invalidate entire buffer descriptor */ |
| 606 | invalidate_dcache_range(desc_start, desc_end); |
| 607 | |
| 608 | desc_p->st = len; |
| 609 | /* Mandatory undocumented bit */ |
| 610 | desc_p->st |= BIT(24); |
| 611 | |
| 612 | memcpy((void *)data_start, packet, len); |
| 613 | |
| 614 | /* Flush data to be sent */ |
| 615 | flush_dcache_range(data_start, data_end); |
| 616 | |
| 617 | /* frame end */ |
| 618 | desc_p->st |= BIT(30); |
| 619 | desc_p->st |= BIT(31); |
| 620 | |
| 621 | /*frame begin */ |
| 622 | desc_p->st |= BIT(29); |
| 623 | desc_p->status = BIT(31); |
| 624 | |
| 625 | /*Descriptors st and status field has changed, so FLUSH it */ |
| 626 | flush_dcache_range(desc_start, desc_end); |
| 627 | |
| 628 | /* Move to next Descriptor and wrap around */ |
| 629 | if (++desc_num >= CONFIG_TX_DESCR_NUM) |
| 630 | desc_num = 0; |
| 631 | priv->tx_currdescnum = desc_num; |
| 632 | |
| 633 | /* Start the DMA */ |
| 634 | v = readl(priv->mac_reg + EMAC_TX_CTL1); |
| 635 | v |= BIT(31);/* mandatory */ |
| 636 | v |= BIT(30);/* mandatory */ |
| 637 | writel(v, priv->mac_reg + EMAC_TX_CTL1); |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | static int sun8i_eth_write_hwaddr(struct udevice *dev) |
| 643 | { |
| 644 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 645 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 646 | |
| 647 | return _sun8i_write_hwaddr(priv, pdata->enetaddr); |
| 648 | } |
| 649 | |
Sean Anderson | ef04369 | 2020-09-15 10:45:00 -0400 | [diff] [blame] | 650 | static int sun8i_emac_board_setup(struct udevice *dev, |
| 651 | struct emac_eth_dev *priv) |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 652 | { |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 653 | int ret; |
| 654 | |
| 655 | ret = clk_enable(&priv->tx_clk); |
| 656 | if (ret) { |
| 657 | dev_err(dev, "failed to enable TX clock\n"); |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | if (reset_valid(&priv->tx_rst)) { |
| 662 | ret = reset_deassert(&priv->tx_rst); |
| 663 | if (ret) { |
| 664 | dev_err(dev, "failed to deassert TX reset\n"); |
| 665 | goto err_tx_clk; |
| 666 | } |
| 667 | } |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 668 | |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 669 | /* Only H3/H5 have clock controls for internal EPHY */ |
| 670 | if (clk_valid(&priv->ephy_clk)) { |
| 671 | ret = clk_enable(&priv->ephy_clk); |
| 672 | if (ret) { |
| 673 | dev_err(dev, "failed to enable EPHY TX clock\n"); |
| 674 | return ret; |
| 675 | } |
| 676 | } |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 677 | |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 678 | if (reset_valid(&priv->ephy_rst)) { |
| 679 | ret = reset_deassert(&priv->ephy_rst); |
| 680 | if (ret) { |
| 681 | dev_err(dev, "failed to deassert EPHY TX clock\n"); |
| 682 | return ret; |
Lothar Felten | c6a21d6 | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 683 | } |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 684 | } |
| 685 | |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 686 | return 0; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 687 | |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 688 | err_tx_clk: |
| 689 | clk_disable(&priv->tx_clk); |
| 690 | return ret; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 691 | } |
| 692 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 693 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 694 | static int sun8i_mdio_reset(struct mii_dev *bus) |
| 695 | { |
| 696 | struct udevice *dev = bus->priv; |
| 697 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 698 | struct sun8i_eth_pdata *pdata = dev_get_platdata(dev); |
| 699 | int ret; |
| 700 | |
| 701 | if (!dm_gpio_is_valid(&priv->reset_gpio)) |
| 702 | return 0; |
| 703 | |
| 704 | /* reset the phy */ |
| 705 | ret = dm_gpio_set_value(&priv->reset_gpio, 0); |
| 706 | if (ret) |
| 707 | return ret; |
| 708 | |
| 709 | udelay(pdata->reset_delays[0]); |
| 710 | |
| 711 | ret = dm_gpio_set_value(&priv->reset_gpio, 1); |
| 712 | if (ret) |
| 713 | return ret; |
| 714 | |
| 715 | udelay(pdata->reset_delays[1]); |
| 716 | |
| 717 | ret = dm_gpio_set_value(&priv->reset_gpio, 0); |
| 718 | if (ret) |
| 719 | return ret; |
| 720 | |
| 721 | udelay(pdata->reset_delays[2]); |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | #endif |
| 726 | |
| 727 | static int sun8i_mdio_init(const char *name, struct udevice *priv) |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 728 | { |
| 729 | struct mii_dev *bus = mdio_alloc(); |
| 730 | |
| 731 | if (!bus) { |
| 732 | debug("Failed to allocate MDIO bus\n"); |
| 733 | return -ENOMEM; |
| 734 | } |
| 735 | |
| 736 | bus->read = sun8i_mdio_read; |
| 737 | bus->write = sun8i_mdio_write; |
| 738 | snprintf(bus->name, sizeof(bus->name), name); |
| 739 | bus->priv = (void *)priv; |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 740 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 741 | bus->reset = sun8i_mdio_reset; |
| 742 | #endif |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 743 | |
| 744 | return mdio_register(bus); |
| 745 | } |
| 746 | |
| 747 | static int sun8i_emac_eth_start(struct udevice *dev) |
| 748 | { |
| 749 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 750 | |
| 751 | return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr); |
| 752 | } |
| 753 | |
| 754 | static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length) |
| 755 | { |
| 756 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 757 | |
| 758 | return _sun8i_emac_eth_send(priv, packet, length); |
| 759 | } |
| 760 | |
| 761 | static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 762 | { |
| 763 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 764 | |
| 765 | return _sun8i_eth_recv(priv, packetp); |
| 766 | } |
| 767 | |
| 768 | static int _sun8i_free_pkt(struct emac_eth_dev *priv) |
| 769 | { |
| 770 | u32 desc_num = priv->rx_currdescnum; |
| 771 | struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num]; |
| 772 | uintptr_t desc_start = (uintptr_t)desc_p; |
| 773 | uintptr_t desc_end = desc_start + |
| 774 | roundup(sizeof(u32), ARCH_DMA_MINALIGN); |
| 775 | |
| 776 | /* Make the current descriptor valid again */ |
| 777 | desc_p->status |= BIT(31); |
| 778 | |
| 779 | /* Flush Status field of descriptor */ |
| 780 | flush_dcache_range(desc_start, desc_end); |
| 781 | |
| 782 | /* Move to next desc and wrap-around condition. */ |
| 783 | if (++desc_num >= CONFIG_RX_DESCR_NUM) |
| 784 | desc_num = 0; |
| 785 | priv->rx_currdescnum = desc_num; |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet, |
| 791 | int length) |
| 792 | { |
| 793 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 794 | |
| 795 | return _sun8i_free_pkt(priv); |
| 796 | } |
| 797 | |
| 798 | static void sun8i_emac_eth_stop(struct udevice *dev) |
| 799 | { |
| 800 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 801 | |
| 802 | /* Stop Rx/Tx transmitter */ |
| 803 | clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31)); |
| 804 | clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31)); |
| 805 | |
| 806 | /* Stop TX DMA */ |
| 807 | clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30)); |
| 808 | |
| 809 | phy_shutdown(priv->phydev); |
| 810 | } |
| 811 | |
| 812 | static int sun8i_emac_eth_probe(struct udevice *dev) |
| 813 | { |
Icenowy Zheng | 9b16ede | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 814 | struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev); |
| 815 | struct eth_pdata *pdata = &sun8i_pdata->eth_pdata; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 816 | struct emac_eth_dev *priv = dev_get_priv(dev); |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 817 | int ret; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 818 | |
| 819 | priv->mac_reg = (void *)pdata->iobase; |
| 820 | |
Sean Anderson | ef04369 | 2020-09-15 10:45:00 -0400 | [diff] [blame] | 821 | ret = sun8i_emac_board_setup(dev, priv); |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 822 | if (ret) |
| 823 | return ret; |
| 824 | |
Icenowy Zheng | 9b16ede | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 825 | sun8i_emac_set_syscon(sun8i_pdata, priv); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 826 | |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 827 | sun8i_mdio_init(dev->name, dev); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 828 | priv->bus = miiphy_get_dev_by_name(dev->name); |
| 829 | |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 830 | return sun8i_phy_init(priv, dev); |
| 831 | } |
| 832 | |
| 833 | static const struct eth_ops sun8i_emac_eth_ops = { |
| 834 | .start = sun8i_emac_eth_start, |
| 835 | .write_hwaddr = sun8i_eth_write_hwaddr, |
| 836 | .send = sun8i_emac_eth_send, |
| 837 | .recv = sun8i_emac_eth_recv, |
| 838 | .free_pkt = sun8i_eth_free_pkt, |
| 839 | .stop = sun8i_emac_eth_stop, |
| 840 | }; |
| 841 | |
Sean Anderson | ef04369 | 2020-09-15 10:45:00 -0400 | [diff] [blame] | 842 | static int sun8i_get_ephy_nodes(struct udevice *dev, struct emac_eth_dev *priv) |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 843 | { |
Emmanuel Vadot | d53e522 | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 844 | int emac_node, ephy_node, ret, ephy_handle; |
| 845 | |
| 846 | emac_node = fdt_path_offset(gd->fdt_blob, |
| 847 | "/soc/ethernet@1c30000"); |
| 848 | if (emac_node < 0) { |
| 849 | debug("failed to get emac node\n"); |
| 850 | return emac_node; |
| 851 | } |
| 852 | ephy_handle = fdtdec_lookup_phandle(gd->fdt_blob, |
| 853 | emac_node, "phy-handle"); |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 854 | |
| 855 | /* look for mdio-mux node for internal PHY node */ |
Emmanuel Vadot | d53e522 | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 856 | ephy_node = fdt_path_offset(gd->fdt_blob, |
| 857 | "/soc/ethernet@1c30000/mdio-mux/mdio@1/ethernet-phy@1"); |
| 858 | if (ephy_node < 0) { |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 859 | debug("failed to get mdio-mux with internal PHY\n"); |
Emmanuel Vadot | d53e522 | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 860 | return ephy_node; |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 861 | } |
| 862 | |
Emmanuel Vadot | d53e522 | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 863 | /* This is not the phy we are looking for */ |
| 864 | if (ephy_node != ephy_handle) |
| 865 | return 0; |
| 866 | |
| 867 | ret = fdt_node_check_compatible(gd->fdt_blob, ephy_node, |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 868 | "allwinner,sun8i-h3-mdio-internal"); |
| 869 | if (ret < 0) { |
| 870 | debug("failed to find mdio-internal node\n"); |
| 871 | return ret; |
| 872 | } |
| 873 | |
Emmanuel Vadot | d53e522 | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 874 | ret = clk_get_by_index_nodev(offset_to_ofnode(ephy_node), 0, |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 875 | &priv->ephy_clk); |
| 876 | if (ret) { |
| 877 | dev_err(dev, "failed to get EPHY TX clock\n"); |
| 878 | return ret; |
| 879 | } |
| 880 | |
Emmanuel Vadot | d53e522 | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 881 | ret = reset_get_by_index_nodev(offset_to_ofnode(ephy_node), 0, |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 882 | &priv->ephy_rst); |
| 883 | if (ret) { |
| 884 | dev_err(dev, "failed to get EPHY TX reset\n"); |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | priv->use_internal_phy = true; |
| 889 | |
| 890 | return 0; |
| 891 | } |
| 892 | |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 893 | static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev) |
| 894 | { |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 895 | struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev); |
| 896 | struct eth_pdata *pdata = &sun8i_pdata->eth_pdata; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 897 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 898 | const char *phy_mode; |
Andre Przywara | ecd0cec | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 899 | const fdt32_t *reg; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 900 | int node = dev_of_offset(dev); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 901 | int offset = 0; |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 902 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 903 | int reset_flags = GPIOD_IS_OUT; |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 904 | #endif |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 905 | int ret; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 906 | |
Masahiro Yamada | 2548493 | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 907 | pdata->iobase = dev_read_addr(dev); |
Andre Przywara | 12afd95 | 2018-04-04 01:31:16 +0100 | [diff] [blame] | 908 | if (pdata->iobase == FDT_ADDR_T_NONE) { |
| 909 | debug("%s: Cannot find MAC base address\n", __func__); |
| 910 | return -EINVAL; |
| 911 | } |
| 912 | |
Lothar Felten | e46d73f | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 913 | priv->variant = dev_get_driver_data(dev); |
| 914 | |
| 915 | if (!priv->variant) { |
| 916 | printf("%s: Missing variant\n", __func__); |
Andre Przywara | ecd0cec | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 917 | return -EINVAL; |
Andre Przywara | 12afd95 | 2018-04-04 01:31:16 +0100 | [diff] [blame] | 918 | } |
Lothar Felten | e46d73f | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 919 | |
Jagan Teki | d3a2c05 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 920 | ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk); |
| 921 | if (ret) { |
| 922 | dev_err(dev, "failed to get TX clock\n"); |
| 923 | return ret; |
| 924 | } |
| 925 | |
| 926 | ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst); |
| 927 | if (ret && ret != -ENOENT) { |
| 928 | dev_err(dev, "failed to get TX reset\n"); |
| 929 | return ret; |
| 930 | } |
| 931 | |
Jagan Teki | 695f604 | 2019-02-28 00:26:51 +0530 | [diff] [blame] | 932 | offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon"); |
| 933 | if (offset < 0) { |
| 934 | debug("%s: cannot find syscon node\n", __func__); |
| 935 | return -EINVAL; |
| 936 | } |
| 937 | |
| 938 | reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL); |
| 939 | if (!reg) { |
| 940 | debug("%s: cannot find reg property in syscon node\n", |
| 941 | __func__); |
| 942 | return -EINVAL; |
| 943 | } |
| 944 | priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob, |
| 945 | offset, reg); |
| 946 | if (priv->sysctl_reg == FDT_ADDR_T_NONE) { |
| 947 | debug("%s: Cannot find syscon base address\n", __func__); |
| 948 | return -EINVAL; |
Andre Przywara | 12afd95 | 2018-04-04 01:31:16 +0100 | [diff] [blame] | 949 | } |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 950 | |
| 951 | pdata->phy_interface = -1; |
| 952 | priv->phyaddr = -1; |
| 953 | priv->use_internal_phy = false; |
| 954 | |
Andre Przywara | ecd0cec | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 955 | offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle"); |
Andre Przywara | 12afd95 | 2018-04-04 01:31:16 +0100 | [diff] [blame] | 956 | if (offset < 0) { |
| 957 | debug("%s: Cannot find PHY address\n", __func__); |
| 958 | return -EINVAL; |
| 959 | } |
| 960 | priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 961 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 962 | phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL); |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 963 | |
| 964 | if (phy_mode) |
| 965 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 966 | printf("phy interface%d\n", pdata->phy_interface); |
| 967 | |
| 968 | if (pdata->phy_interface == -1) { |
| 969 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 970 | return -EINVAL; |
| 971 | } |
| 972 | |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 973 | if (priv->variant == H3_EMAC) { |
Sean Anderson | ef04369 | 2020-09-15 10:45:00 -0400 | [diff] [blame] | 974 | ret = sun8i_get_ephy_nodes(dev, priv); |
Jagan Teki | 2348453 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 975 | if (ret) |
| 976 | return ret; |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | priv->interface = pdata->phy_interface; |
| 980 | |
| 981 | if (!priv->use_internal_phy) |
| 982 | parse_phy_pins(dev); |
| 983 | |
Icenowy Zheng | 9b16ede | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 984 | sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node, |
| 985 | "allwinner,tx-delay-ps", 0); |
| 986 | if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700) |
| 987 | printf("%s: Invalid TX delay value %d\n", __func__, |
| 988 | sun8i_pdata->tx_delay_ps); |
| 989 | |
| 990 | sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node, |
| 991 | "allwinner,rx-delay-ps", 0); |
| 992 | if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100) |
| 993 | printf("%s: Invalid RX delay value %d\n", __func__, |
| 994 | sun8i_pdata->rx_delay_ps); |
| 995 | |
Simon Glass | bcee8d6 | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 996 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Simon Glass | da409cc | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 997 | if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev), |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 998 | "snps,reset-active-low")) |
| 999 | reset_flags |= GPIOD_ACTIVE_LOW; |
| 1000 | |
| 1001 | ret = gpio_request_by_name(dev, "snps,reset-gpio", 0, |
| 1002 | &priv->reset_gpio, reset_flags); |
| 1003 | |
| 1004 | if (ret == 0) { |
Simon Glass | da409cc | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 1005 | ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev), |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 1006 | "snps,reset-delays-us", |
| 1007 | sun8i_pdata->reset_delays, 3); |
| 1008 | } else if (ret == -ENOENT) { |
| 1009 | ret = 0; |
| 1010 | } |
| 1011 | #endif |
| 1012 | |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 1013 | return 0; |
| 1014 | } |
| 1015 | |
| 1016 | static const struct udevice_id sun8i_emac_eth_ids[] = { |
| 1017 | {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC }, |
| 1018 | {.compatible = "allwinner,sun50i-a64-emac", |
| 1019 | .data = (uintptr_t)A64_EMAC }, |
| 1020 | {.compatible = "allwinner,sun8i-a83t-emac", |
| 1021 | .data = (uintptr_t)A83T_EMAC }, |
Lothar Felten | e46d73f | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 1022 | {.compatible = "allwinner,sun8i-r40-gmac", |
| 1023 | .data = (uintptr_t)R40_GMAC }, |
Samuel Holland | 99ac861 | 2020-05-07 18:10:51 -0500 | [diff] [blame] | 1024 | {.compatible = "allwinner,sun50i-h6-emac", |
| 1025 | .data = (uintptr_t)H6_EMAC }, |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 1026 | { } |
| 1027 | }; |
| 1028 | |
| 1029 | U_BOOT_DRIVER(eth_sun8i_emac) = { |
| 1030 | .name = "eth_sun8i_emac", |
| 1031 | .id = UCLASS_ETH, |
| 1032 | .of_match = sun8i_emac_eth_ids, |
| 1033 | .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata, |
| 1034 | .probe = sun8i_emac_eth_probe, |
| 1035 | .ops = &sun8i_emac_eth_ops, |
| 1036 | .priv_auto_alloc_size = sizeof(struct emac_eth_dev), |
Philipp Tomsich | 4d555ae | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 1037 | .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata), |
Amit Singh Tomar | a29710c | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 1038 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 1039 | }; |