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