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