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