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