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 | |
| 379 | static void rx_descs_init(struct emac_eth_dev *priv) |
| 380 | { |
| 381 | struct emac_dma_desc *desc_table_p = &priv->rx_chain[0]; |
| 382 | char *rxbuffs = &priv->rxbuffer[0]; |
| 383 | struct emac_dma_desc *desc_p; |
| 384 | u32 idx; |
| 385 | |
Andre Przywara | 7408b09 | 2020-07-06 01:40:37 +0100 | [diff] [blame^] | 386 | /* |
| 387 | * Make sure we don't have dirty cache lines around, which could |
| 388 | * be cleaned to DRAM *after* the MAC has already written data to it. |
| 389 | */ |
| 390 | invalidate_dcache_range((uintptr_t)desc_table_p, |
| 391 | (uintptr_t)desc_table_p + sizeof(priv->rx_chain)); |
| 392 | invalidate_dcache_range((uintptr_t)rxbuffs, |
| 393 | (uintptr_t)rxbuffs + sizeof(priv->rxbuffer)); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 394 | |
| 395 | for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) { |
| 396 | desc_p = &desc_table_p[idx]; |
| 397 | desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE] |
| 398 | ; |
| 399 | desc_p->next = (uintptr_t)&desc_table_p[idx + 1]; |
Andre Przywara | 7408b09 | 2020-07-06 01:40:37 +0100 | [diff] [blame^] | 400 | desc_p->ctl_size = CONFIG_ETH_RXSIZE; |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 401 | desc_p->status = EMAC_DESC_OWN_DMA; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | /* Correcting the last pointer of the chain */ |
| 405 | desc_p->next = (uintptr_t)&desc_table_p[0]; |
| 406 | |
| 407 | flush_dcache_range((uintptr_t)priv->rx_chain, |
| 408 | (uintptr_t)priv->rx_chain + |
| 409 | sizeof(priv->rx_chain)); |
| 410 | |
| 411 | writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC)); |
| 412 | priv->rx_currdescnum = 0; |
| 413 | } |
| 414 | |
| 415 | static void tx_descs_init(struct emac_eth_dev *priv) |
| 416 | { |
| 417 | struct emac_dma_desc *desc_table_p = &priv->tx_chain[0]; |
| 418 | char *txbuffs = &priv->txbuffer[0]; |
| 419 | struct emac_dma_desc *desc_p; |
| 420 | u32 idx; |
| 421 | |
| 422 | for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) { |
| 423 | desc_p = &desc_table_p[idx]; |
| 424 | desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE] |
| 425 | ; |
| 426 | desc_p->next = (uintptr_t)&desc_table_p[idx + 1]; |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 427 | desc_p->ctl_size = 0; |
Andre Przywara | df6f271 | 2020-07-06 01:40:33 +0100 | [diff] [blame] | 428 | desc_p->status = 0; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* Correcting the last pointer of the chain */ |
| 432 | desc_p->next = (uintptr_t)&desc_table_p[0]; |
| 433 | |
| 434 | /* Flush all Tx buffer descriptors */ |
| 435 | flush_dcache_range((uintptr_t)priv->tx_chain, |
| 436 | (uintptr_t)priv->tx_chain + |
| 437 | sizeof(priv->tx_chain)); |
| 438 | |
| 439 | writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC); |
| 440 | priv->tx_currdescnum = 0; |
| 441 | } |
| 442 | |
Andre Przywara | f58d83c | 2020-10-21 23:21:42 +0530 | [diff] [blame] | 443 | static int sun8i_emac_eth_start(struct udevice *dev) |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 444 | { |
Andre Przywara | f58d83c | 2020-10-21 23:21:42 +0530 | [diff] [blame] | 445 | struct emac_eth_dev *priv = dev_get_priv(dev); |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 446 | u32 reg; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 447 | int timeout = 100; |
Andre Przywara | 874145f | 2020-07-06 01:40:32 +0100 | [diff] [blame] | 448 | int ret; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 449 | |
| 450 | reg = readl((priv->mac_reg + EMAC_CTL1)); |
| 451 | |
| 452 | if (!(reg & 0x1)) { |
| 453 | /* Soft reset MAC */ |
| 454 | setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1); |
| 455 | do { |
| 456 | reg = readl(priv->mac_reg + EMAC_CTL1); |
| 457 | } while ((reg & 0x01) != 0 && (--timeout)); |
| 458 | if (!timeout) { |
| 459 | printf("%s: Timeout\n", __func__); |
| 460 | return -1; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | /* Rewrite mac address after reset */ |
Andre Przywara | f58d83c | 2020-10-21 23:21:42 +0530 | [diff] [blame] | 465 | sun8i_eth_write_hwaddr(dev); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 466 | |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 467 | /* transmission starts after the full frame arrived in TX DMA FIFO */ |
| 468 | 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] | 469 | |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 470 | /* |
| 471 | * 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] | 472 | * complete frame has been written to RX DMA FIFO |
| 473 | */ |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 474 | 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] | 475 | |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 476 | /* DMA burst length */ |
| 477 | 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] | 478 | |
| 479 | /* Initialize rx/tx descriptors */ |
| 480 | rx_descs_init(priv); |
| 481 | tx_descs_init(priv); |
| 482 | |
| 483 | /* PHY Start Up */ |
Andre Przywara | 874145f | 2020-07-06 01:40:32 +0100 | [diff] [blame] | 484 | ret = phy_startup(priv->phydev); |
| 485 | if (ret) |
| 486 | return ret; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 487 | |
| 488 | sun8i_adjust_link(priv, priv->phydev); |
| 489 | |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 490 | /* Start RX/TX DMA */ |
| 491 | setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN); |
| 492 | 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] | 493 | |
| 494 | /* Enable RX/TX */ |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 495 | setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN); |
| 496 | 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] | 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int parse_phy_pins(struct udevice *dev) |
| 502 | { |
Lothar Felten | acb9a5b | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 503 | struct emac_eth_dev *priv = dev_get_priv(dev); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 504 | int offset; |
| 505 | const char *pin_name; |
Andre Przywara | 94f3bbd | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 506 | int drive, pull = SUN4I_PINCTRL_NO_PULL, i; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 507 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 508 | offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev), |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 509 | "pinctrl-0"); |
| 510 | if (offset < 0) { |
| 511 | printf("WARNING: emac: cannot find pinctrl-0 node\n"); |
| 512 | return offset; |
| 513 | } |
| 514 | |
| 515 | drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0, |
Andre Przywara | 26e549b | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 516 | "drive-strength", ~0); |
| 517 | if (drive != ~0) { |
| 518 | if (drive <= 10) |
| 519 | drive = SUN4I_PINCTRL_10_MA; |
| 520 | else if (drive <= 20) |
| 521 | drive = SUN4I_PINCTRL_20_MA; |
| 522 | else if (drive <= 30) |
| 523 | drive = SUN4I_PINCTRL_30_MA; |
| 524 | else |
| 525 | drive = SUN4I_PINCTRL_40_MA; |
Andre Przywara | 26e549b | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL)) |
| 529 | pull = SUN4I_PINCTRL_PULL_UP; |
Andre Przywara | 26e549b | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 530 | else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL)) |
| 531 | pull = SUN4I_PINCTRL_PULL_DOWN; |
Andre Przywara | 94f3bbd | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 532 | |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 533 | for (i = 0; ; i++) { |
| 534 | int pin; |
| 535 | |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 536 | pin_name = fdt_stringlist_get(gd->fdt_blob, offset, |
Andre Przywara | 94f3bbd | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 537 | "pins", i, NULL); |
| 538 | if (!pin_name) |
| 539 | break; |
Andre Przywara | 26e549b | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 540 | |
| 541 | pin = sunxi_name_to_gpio(pin_name); |
| 542 | if (pin < 0) |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 543 | continue; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 544 | |
Lothar Felten | acb9a5b | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 545 | if (priv->variant == H3_EMAC) |
| 546 | sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3); |
Samuel Holland | 3386e9a | 2020-05-07 18:10:51 -0500 | [diff] [blame] | 547 | else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC) |
Lothar Felten | e8cbced | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 548 | sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40); |
Lothar Felten | acb9a5b | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 549 | else |
| 550 | sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX); |
| 551 | |
Andre Przywara | 26e549b | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 552 | if (drive != ~0) |
| 553 | sunxi_gpio_set_drv(pin, drive); |
| 554 | if (pull != ~0) |
| 555 | sunxi_gpio_set_pull(pin, pull); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | if (!i) { |
Andre Przywara | 26e549b | 2018-04-04 01:31:15 +0100 | [diff] [blame] | 559 | printf("WARNING: emac: cannot find pins property\n"); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 560 | return -2; |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
Andre Przywara | f58d83c | 2020-10-21 23:21:42 +0530 | [diff] [blame] | 566 | 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] | 567 | { |
Andre Przywara | f58d83c | 2020-10-21 23:21:42 +0530 | [diff] [blame] | 568 | struct emac_eth_dev *priv = dev_get_priv(dev); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 569 | u32 status, desc_num = priv->rx_currdescnum; |
| 570 | struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num]; |
| 571 | int length = -EAGAIN; |
| 572 | int good_packet = 1; |
| 573 | uintptr_t desc_start = (uintptr_t)desc_p; |
| 574 | uintptr_t desc_end = desc_start + |
| 575 | roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN); |
| 576 | |
| 577 | ulong data_start = (uintptr_t)desc_p->buf_addr; |
| 578 | ulong data_end; |
| 579 | |
| 580 | /* Invalidate entire buffer descriptor */ |
| 581 | invalidate_dcache_range(desc_start, desc_end); |
| 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]; |
| 619 | uintptr_t desc_start = (uintptr_t)desc_p; |
| 620 | uintptr_t desc_end = desc_start + |
| 621 | roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN); |
| 622 | |
| 623 | uintptr_t data_start = (uintptr_t)desc_p->buf_addr; |
| 624 | uintptr_t data_end = data_start + |
Andre Przywara | f58d83c | 2020-10-21 23:21:42 +0530 | [diff] [blame] | 625 | roundup(length, ARCH_DMA_MINALIGN); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 626 | |
| 627 | /* Invalidate entire buffer descriptor */ |
| 628 | invalidate_dcache_range(desc_start, desc_end); |
| 629 | |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 630 | desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 631 | |
Andre Przywara | f58d83c | 2020-10-21 23:21:42 +0530 | [diff] [blame] | 632 | memcpy((void *)data_start, packet, length); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 633 | |
| 634 | /* Flush data to be sent */ |
| 635 | flush_dcache_range(data_start, data_end); |
| 636 | |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 637 | /* frame begin and end */ |
| 638 | desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC; |
| 639 | desc_p->status = EMAC_DESC_OWN_DMA; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 640 | |
| 641 | /*Descriptors st and status field has changed, so FLUSH it */ |
| 642 | flush_dcache_range(desc_start, desc_end); |
| 643 | |
| 644 | /* Move to next Descriptor and wrap around */ |
| 645 | if (++desc_num >= CONFIG_TX_DESCR_NUM) |
| 646 | desc_num = 0; |
| 647 | priv->tx_currdescnum = desc_num; |
| 648 | |
| 649 | /* Start the DMA */ |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 650 | setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START); |
| 651 | |
| 652 | /* |
| 653 | * Since we copied the data above, we return here without waiting |
| 654 | * for the packet to be actually send out. |
| 655 | */ |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
Sean Anderson | 4702aa2 | 2020-09-15 10:45:00 -0400 | [diff] [blame] | 660 | static int sun8i_emac_board_setup(struct udevice *dev, |
| 661 | struct emac_eth_dev *priv) |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 662 | { |
Jagan Teki | cb63d28 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 663 | int ret; |
| 664 | |
| 665 | ret = clk_enable(&priv->tx_clk); |
| 666 | if (ret) { |
| 667 | dev_err(dev, "failed to enable TX clock\n"); |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | if (reset_valid(&priv->tx_rst)) { |
| 672 | ret = reset_deassert(&priv->tx_rst); |
| 673 | if (ret) { |
| 674 | dev_err(dev, "failed to deassert TX reset\n"); |
| 675 | goto err_tx_clk; |
| 676 | } |
| 677 | } |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 678 | |
Jagan Teki | 727ed79 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 679 | /* Only H3/H5 have clock controls for internal EPHY */ |
| 680 | if (clk_valid(&priv->ephy_clk)) { |
| 681 | ret = clk_enable(&priv->ephy_clk); |
| 682 | if (ret) { |
| 683 | dev_err(dev, "failed to enable EPHY TX clock\n"); |
| 684 | return ret; |
| 685 | } |
| 686 | } |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 687 | |
Jagan Teki | 727ed79 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 688 | if (reset_valid(&priv->ephy_rst)) { |
| 689 | ret = reset_deassert(&priv->ephy_rst); |
| 690 | if (ret) { |
| 691 | dev_err(dev, "failed to deassert EPHY TX clock\n"); |
| 692 | return ret; |
Lothar Felten | acb9a5b | 2018-07-13 10:45:27 +0200 | [diff] [blame] | 693 | } |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 694 | } |
| 695 | |
Jagan Teki | cb63d28 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 696 | return 0; |
Lothar Felten | e8cbced | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 697 | |
Jagan Teki | cb63d28 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 698 | err_tx_clk: |
| 699 | clk_disable(&priv->tx_clk); |
| 700 | return ret; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 701 | } |
| 702 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 703 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Philipp Tomsich | 3297b55 | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 704 | static int sun8i_mdio_reset(struct mii_dev *bus) |
| 705 | { |
| 706 | struct udevice *dev = bus->priv; |
| 707 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 708 | struct sun8i_eth_pdata *pdata = dev_get_platdata(dev); |
| 709 | int ret; |
| 710 | |
| 711 | if (!dm_gpio_is_valid(&priv->reset_gpio)) |
| 712 | return 0; |
| 713 | |
| 714 | /* reset the phy */ |
| 715 | ret = dm_gpio_set_value(&priv->reset_gpio, 0); |
| 716 | if (ret) |
| 717 | return ret; |
| 718 | |
| 719 | udelay(pdata->reset_delays[0]); |
| 720 | |
| 721 | ret = dm_gpio_set_value(&priv->reset_gpio, 1); |
| 722 | if (ret) |
| 723 | return ret; |
| 724 | |
| 725 | udelay(pdata->reset_delays[1]); |
| 726 | |
| 727 | ret = dm_gpio_set_value(&priv->reset_gpio, 0); |
| 728 | if (ret) |
| 729 | return ret; |
| 730 | |
| 731 | udelay(pdata->reset_delays[2]); |
| 732 | |
| 733 | return 0; |
| 734 | } |
| 735 | #endif |
| 736 | |
| 737 | static int sun8i_mdio_init(const char *name, struct udevice *priv) |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 738 | { |
| 739 | struct mii_dev *bus = mdio_alloc(); |
| 740 | |
| 741 | if (!bus) { |
| 742 | debug("Failed to allocate MDIO bus\n"); |
| 743 | return -ENOMEM; |
| 744 | } |
| 745 | |
| 746 | bus->read = sun8i_mdio_read; |
| 747 | bus->write = sun8i_mdio_write; |
| 748 | snprintf(bus->name, sizeof(bus->name), name); |
| 749 | bus->priv = (void *)priv; |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 750 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Philipp Tomsich | 3297b55 | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 751 | bus->reset = sun8i_mdio_reset; |
| 752 | #endif |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 753 | |
| 754 | return mdio_register(bus); |
| 755 | } |
| 756 | |
Andre Przywara | f58d83c | 2020-10-21 23:21:42 +0530 | [diff] [blame] | 757 | static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet, |
| 758 | int length) |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 759 | { |
| 760 | struct emac_eth_dev *priv = dev_get_priv(dev); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 761 | u32 desc_num = priv->rx_currdescnum; |
| 762 | struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num]; |
| 763 | uintptr_t desc_start = (uintptr_t)desc_p; |
| 764 | uintptr_t desc_end = desc_start + |
| 765 | roundup(sizeof(u32), ARCH_DMA_MINALIGN); |
| 766 | |
| 767 | /* Make the current descriptor valid again */ |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 768 | desc_p->status |= EMAC_DESC_OWN_DMA; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 769 | |
| 770 | /* Flush Status field of descriptor */ |
| 771 | flush_dcache_range(desc_start, desc_end); |
| 772 | |
| 773 | /* Move to next desc and wrap-around condition. */ |
| 774 | if (++desc_num >= CONFIG_RX_DESCR_NUM) |
| 775 | desc_num = 0; |
| 776 | priv->rx_currdescnum = desc_num; |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 781 | static void sun8i_emac_eth_stop(struct udevice *dev) |
| 782 | { |
| 783 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 784 | |
| 785 | /* Stop Rx/Tx transmitter */ |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 786 | clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN); |
| 787 | 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] | 788 | |
Andre Przywara | e6e29cc | 2020-07-06 01:40:36 +0100 | [diff] [blame] | 789 | /* Stop RX/TX DMA */ |
| 790 | clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN); |
| 791 | 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] | 792 | |
| 793 | phy_shutdown(priv->phydev); |
| 794 | } |
| 795 | |
| 796 | static int sun8i_emac_eth_probe(struct udevice *dev) |
| 797 | { |
Icenowy Zheng | 525dc44 | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 798 | struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev); |
| 799 | struct eth_pdata *pdata = &sun8i_pdata->eth_pdata; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 800 | struct emac_eth_dev *priv = dev_get_priv(dev); |
Jagan Teki | cb63d28 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 801 | int ret; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 802 | |
| 803 | priv->mac_reg = (void *)pdata->iobase; |
| 804 | |
Sean Anderson | 4702aa2 | 2020-09-15 10:45:00 -0400 | [diff] [blame] | 805 | ret = sun8i_emac_board_setup(dev, priv); |
Jagan Teki | cb63d28 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 806 | if (ret) |
| 807 | return ret; |
| 808 | |
Icenowy Zheng | 525dc44 | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 809 | sun8i_emac_set_syscon(sun8i_pdata, priv); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 810 | |
Philipp Tomsich | 3297b55 | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 811 | sun8i_mdio_init(dev->name, dev); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 812 | priv->bus = miiphy_get_dev_by_name(dev->name); |
| 813 | |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 814 | return sun8i_phy_init(priv, dev); |
| 815 | } |
| 816 | |
| 817 | static const struct eth_ops sun8i_emac_eth_ops = { |
| 818 | .start = sun8i_emac_eth_start, |
| 819 | .write_hwaddr = sun8i_eth_write_hwaddr, |
| 820 | .send = sun8i_emac_eth_send, |
| 821 | .recv = sun8i_emac_eth_recv, |
| 822 | .free_pkt = sun8i_eth_free_pkt, |
| 823 | .stop = sun8i_emac_eth_stop, |
| 824 | }; |
| 825 | |
Sean Anderson | 4702aa2 | 2020-09-15 10:45:00 -0400 | [diff] [blame] | 826 | 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] | 827 | { |
Emmanuel Vadot | 98be8be | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 828 | int emac_node, ephy_node, ret, ephy_handle; |
| 829 | |
| 830 | emac_node = fdt_path_offset(gd->fdt_blob, |
| 831 | "/soc/ethernet@1c30000"); |
| 832 | if (emac_node < 0) { |
| 833 | debug("failed to get emac node\n"); |
| 834 | return emac_node; |
| 835 | } |
| 836 | ephy_handle = fdtdec_lookup_phandle(gd->fdt_blob, |
| 837 | emac_node, "phy-handle"); |
Jagan Teki | 727ed79 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 838 | |
| 839 | /* look for mdio-mux node for internal PHY node */ |
Emmanuel Vadot | 98be8be | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 840 | ephy_node = fdt_path_offset(gd->fdt_blob, |
| 841 | "/soc/ethernet@1c30000/mdio-mux/mdio@1/ethernet-phy@1"); |
| 842 | if (ephy_node < 0) { |
Jagan Teki | 727ed79 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 843 | debug("failed to get mdio-mux with internal PHY\n"); |
Emmanuel Vadot | 98be8be | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 844 | return ephy_node; |
Jagan Teki | 727ed79 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 845 | } |
| 846 | |
Emmanuel Vadot | 98be8be | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 847 | /* This is not the phy we are looking for */ |
| 848 | if (ephy_node != ephy_handle) |
| 849 | return 0; |
| 850 | |
| 851 | ret = fdt_node_check_compatible(gd->fdt_blob, ephy_node, |
Jagan Teki | 727ed79 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 852 | "allwinner,sun8i-h3-mdio-internal"); |
| 853 | if (ret < 0) { |
| 854 | debug("failed to find mdio-internal node\n"); |
| 855 | return ret; |
| 856 | } |
| 857 | |
Emmanuel Vadot | 98be8be | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 858 | ret = clk_get_by_index_nodev(offset_to_ofnode(ephy_node), 0, |
Jagan Teki | 727ed79 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 859 | &priv->ephy_clk); |
| 860 | if (ret) { |
| 861 | dev_err(dev, "failed to get EPHY TX clock\n"); |
| 862 | return ret; |
| 863 | } |
| 864 | |
Emmanuel Vadot | 98be8be | 2019-07-19 22:26:38 +0200 | [diff] [blame] | 865 | ret = reset_get_by_index_nodev(offset_to_ofnode(ephy_node), 0, |
Jagan Teki | 727ed79 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 866 | &priv->ephy_rst); |
| 867 | if (ret) { |
| 868 | dev_err(dev, "failed to get EPHY TX reset\n"); |
| 869 | return ret; |
| 870 | } |
| 871 | |
| 872 | priv->use_internal_phy = true; |
| 873 | |
| 874 | return 0; |
| 875 | } |
| 876 | |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 877 | static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev) |
| 878 | { |
Philipp Tomsich | 3297b55 | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 879 | struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev); |
| 880 | struct eth_pdata *pdata = &sun8i_pdata->eth_pdata; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 881 | struct emac_eth_dev *priv = dev_get_priv(dev); |
| 882 | const char *phy_mode; |
Andre Przywara | 94f3bbd | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 883 | const fdt32_t *reg; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 884 | int node = dev_of_offset(dev); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 885 | int offset = 0; |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 886 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Philipp Tomsich | 3297b55 | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 887 | int reset_flags = GPIOD_IS_OUT; |
Philipp Tomsich | 3297b55 | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 888 | #endif |
Jagan Teki | cb63d28 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 889 | int ret; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 890 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 891 | pdata->iobase = dev_read_addr(dev); |
Andre Przywara | ba3a96d | 2018-04-04 01:31:16 +0100 | [diff] [blame] | 892 | if (pdata->iobase == FDT_ADDR_T_NONE) { |
| 893 | debug("%s: Cannot find MAC base address\n", __func__); |
| 894 | return -EINVAL; |
| 895 | } |
| 896 | |
Lothar Felten | e8cbced | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 897 | priv->variant = dev_get_driver_data(dev); |
| 898 | |
| 899 | if (!priv->variant) { |
| 900 | printf("%s: Missing variant\n", __func__); |
Andre Przywara | 94f3bbd | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 901 | return -EINVAL; |
| 902 | } |
Lothar Felten | e8cbced | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 903 | |
Jagan Teki | cb63d28 | 2019-02-28 00:26:58 +0530 | [diff] [blame] | 904 | ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk); |
| 905 | if (ret) { |
| 906 | dev_err(dev, "failed to get TX clock\n"); |
| 907 | return ret; |
| 908 | } |
| 909 | |
| 910 | ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst); |
| 911 | if (ret && ret != -ENOENT) { |
| 912 | dev_err(dev, "failed to get TX reset\n"); |
| 913 | return ret; |
| 914 | } |
| 915 | |
Jagan Teki | 1cfc64c | 2019-02-28 00:26:51 +0530 | [diff] [blame] | 916 | offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon"); |
| 917 | if (offset < 0) { |
| 918 | debug("%s: cannot find syscon node\n", __func__); |
| 919 | return -EINVAL; |
| 920 | } |
| 921 | |
| 922 | reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL); |
| 923 | if (!reg) { |
| 924 | debug("%s: cannot find reg property in syscon node\n", |
| 925 | __func__); |
| 926 | return -EINVAL; |
| 927 | } |
| 928 | priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob, |
| 929 | offset, reg); |
| 930 | if (priv->sysctl_reg == FDT_ADDR_T_NONE) { |
| 931 | debug("%s: Cannot find syscon base address\n", __func__); |
| 932 | return -EINVAL; |
Andre Przywara | ba3a96d | 2018-04-04 01:31:16 +0100 | [diff] [blame] | 933 | } |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 934 | |
| 935 | pdata->phy_interface = -1; |
| 936 | priv->phyaddr = -1; |
| 937 | priv->use_internal_phy = false; |
| 938 | |
Andre Przywara | 94f3bbd | 2018-04-04 01:31:20 +0100 | [diff] [blame] | 939 | offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle"); |
Andre Przywara | ba3a96d | 2018-04-04 01:31:16 +0100 | [diff] [blame] | 940 | if (offset < 0) { |
| 941 | debug("%s: Cannot find PHY address\n", __func__); |
| 942 | return -EINVAL; |
| 943 | } |
| 944 | priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 945 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 946 | phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL); |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 947 | |
| 948 | if (phy_mode) |
| 949 | pdata->phy_interface = phy_get_interface_by_name(phy_mode); |
| 950 | printf("phy interface%d\n", pdata->phy_interface); |
| 951 | |
| 952 | if (pdata->phy_interface == -1) { |
| 953 | debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode); |
| 954 | return -EINVAL; |
| 955 | } |
| 956 | |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 957 | if (priv->variant == H3_EMAC) { |
Sean Anderson | 4702aa2 | 2020-09-15 10:45:00 -0400 | [diff] [blame] | 958 | ret = sun8i_get_ephy_nodes(dev, priv); |
Jagan Teki | 727ed79 | 2019-02-28 00:27:00 +0530 | [diff] [blame] | 959 | if (ret) |
| 960 | return ret; |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | priv->interface = pdata->phy_interface; |
| 964 | |
| 965 | if (!priv->use_internal_phy) |
| 966 | parse_phy_pins(dev); |
| 967 | |
Icenowy Zheng | 525dc44 | 2018-11-23 00:37:48 +0100 | [diff] [blame] | 968 | sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node, |
| 969 | "allwinner,tx-delay-ps", 0); |
| 970 | if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700) |
| 971 | printf("%s: Invalid TX delay value %d\n", __func__, |
| 972 | sun8i_pdata->tx_delay_ps); |
| 973 | |
| 974 | sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node, |
| 975 | "allwinner,rx-delay-ps", 0); |
| 976 | if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100) |
| 977 | printf("%s: Invalid RX delay value %d\n", __func__, |
| 978 | sun8i_pdata->rx_delay_ps); |
| 979 | |
Simon Glass | fa4689a | 2019-12-06 21:41:35 -0700 | [diff] [blame] | 980 | #if CONFIG_IS_ENABLED(DM_GPIO) |
Simon Glass | 7a49443 | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 981 | if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev), |
Philipp Tomsich | 3297b55 | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 982 | "snps,reset-active-low")) |
| 983 | reset_flags |= GPIOD_ACTIVE_LOW; |
| 984 | |
| 985 | ret = gpio_request_by_name(dev, "snps,reset-gpio", 0, |
| 986 | &priv->reset_gpio, reset_flags); |
| 987 | |
| 988 | if (ret == 0) { |
Simon Glass | 7a49443 | 2017-05-17 17:18:09 -0600 | [diff] [blame] | 989 | ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev), |
Philipp Tomsich | 3297b55 | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 990 | "snps,reset-delays-us", |
| 991 | sun8i_pdata->reset_delays, 3); |
| 992 | } else if (ret == -ENOENT) { |
| 993 | ret = 0; |
| 994 | } |
| 995 | #endif |
| 996 | |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 997 | return 0; |
| 998 | } |
| 999 | |
| 1000 | static const struct udevice_id sun8i_emac_eth_ids[] = { |
| 1001 | {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC }, |
| 1002 | {.compatible = "allwinner,sun50i-a64-emac", |
| 1003 | .data = (uintptr_t)A64_EMAC }, |
| 1004 | {.compatible = "allwinner,sun8i-a83t-emac", |
| 1005 | .data = (uintptr_t)A83T_EMAC }, |
Lothar Felten | e8cbced | 2018-07-13 10:45:28 +0200 | [diff] [blame] | 1006 | {.compatible = "allwinner,sun8i-r40-gmac", |
| 1007 | .data = (uintptr_t)R40_GMAC }, |
Samuel Holland | 3386e9a | 2020-05-07 18:10:51 -0500 | [diff] [blame] | 1008 | {.compatible = "allwinner,sun50i-h6-emac", |
| 1009 | .data = (uintptr_t)H6_EMAC }, |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 1010 | { } |
| 1011 | }; |
| 1012 | |
| 1013 | U_BOOT_DRIVER(eth_sun8i_emac) = { |
| 1014 | .name = "eth_sun8i_emac", |
| 1015 | .id = UCLASS_ETH, |
| 1016 | .of_match = sun8i_emac_eth_ids, |
| 1017 | .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata, |
| 1018 | .probe = sun8i_emac_eth_probe, |
| 1019 | .ops = &sun8i_emac_eth_ops, |
| 1020 | .priv_auto_alloc_size = sizeof(struct emac_eth_dev), |
Philipp Tomsich | 3297b55 | 2017-02-22 19:46:41 +0100 | [diff] [blame] | 1021 | .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata), |
Amit Singh Tomar | d194c0e | 2016-07-06 17:59:44 +0530 | [diff] [blame] | 1022 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 1023 | }; |