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