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