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