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