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