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