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