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