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