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