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