blob: 6fab34715de02e6bfdc793e903cb76224952ab87 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05302/*
3 * (C) Copyright 2016
4 * Author: Amit Singh Tomar, amittomer25@gmail.com
5 *
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05306 * 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 Glass63334482019-11-14 12:57:39 -070013#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Samuel Holland06feb812021-09-11 16:50:47 -050017#include <asm/gpio.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053018#include <asm/io.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053019#include <clk.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053020#include <dm.h>
21#include <fdt_support.h>
Simon Glass9bc15642020-02-03 07:36:16 -070022#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060023#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060024#include <linux/delay.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053025#include <linux/err.h>
26#include <malloc.h>
27#include <miiphy.h>
28#include <net.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053029#include <reset.h>
Andre Przywara0dd619b2020-07-06 01:40:34 +010030#include <wait_bit.h>
Andre Przywara493e8ba2022-06-08 14:56:56 +010031#include <power/regulator.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053032
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053033#define MDIO_CMD_MII_BUSY BIT(0)
34#define MDIO_CMD_MII_WRITE BIT(1)
35
36#define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
37#define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
38#define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
39#define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
Andre Przywarab41f2472020-07-06 01:40:45 +010040#define MDIO_CMD_MII_CLK_CSR_DIV_16 0x0
41#define MDIO_CMD_MII_CLK_CSR_DIV_32 0x1
42#define MDIO_CMD_MII_CLK_CSR_DIV_64 0x2
43#define MDIO_CMD_MII_CLK_CSR_DIV_128 0x3
44#define MDIO_CMD_MII_CLK_CSR_SHIFT 20
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053045
Tom Rini364d0022023-01-10 11:19:45 -050046#define CFG_TX_DESCR_NUM 32
47#define CFG_RX_DESCR_NUM 32
48#define CFG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
Hans de Goedefcdb3b32016-07-27 17:31:17 +020049
50/*
51 * The datasheet says that each descriptor can transfers up to 4096 bytes
52 * But later, the register documentation reduces that value to 2048,
53 * using 2048 cause strange behaviours and even BSP driver use 2047
54 */
Tom Rini364d0022023-01-10 11:19:45 -050055#define CFG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053056
Tom Rini364d0022023-01-10 11:19:45 -050057#define TX_TOTAL_BUFSIZE (CFG_ETH_BUFSIZE * CFG_TX_DESCR_NUM)
58#define RX_TOTAL_BUFSIZE (CFG_ETH_BUFSIZE * CFG_RX_DESCR_NUM)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053059
60#define H3_EPHY_DEFAULT_VALUE 0x58000
61#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
62#define H3_EPHY_ADDR_SHIFT 20
63#define REG_PHY_ADDR_MASK GENMASK(4, 0)
64#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
65#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
66#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
67
68#define SC_RMII_EN BIT(13)
69#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
70#define SC_ETCS_MASK GENMASK(1, 0)
71#define SC_ETCS_EXT_GMII 0x1
72#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng525dc442018-11-23 00:37:48 +010073#define SC_ETXDC_MASK GENMASK(12, 10)
74#define SC_ETXDC_OFFSET 10
75#define SC_ERXDC_MASK GENMASK(9, 5)
76#define SC_ERXDC_OFFSET 5
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053077
Tom Rini364d0022023-01-10 11:19:45 -050078#define CFG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053079
80#define AHB_GATE_OFFSET_EPHY 0
81
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053082/* H3/A64 EMAC Register's offset */
83#define EMAC_CTL0 0x00
Andre Przywarae6e29cc2020-07-06 01:40:36 +010084#define EMAC_CTL0_FULL_DUPLEX BIT(0)
85#define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
86#define EMAC_CTL0_SPEED_10 (0x2 << 2)
87#define EMAC_CTL0_SPEED_100 (0x3 << 2)
88#define EMAC_CTL0_SPEED_1000 (0x0 << 2)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053089#define EMAC_CTL1 0x04
Andre Przywarae6e29cc2020-07-06 01:40:36 +010090#define EMAC_CTL1_SOFT_RST BIT(0)
91#define EMAC_CTL1_BURST_LEN_SHIFT 24
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053092#define EMAC_INT_STA 0x08
93#define EMAC_INT_EN 0x0c
94#define EMAC_TX_CTL0 0x10
Andre Przywarae6e29cc2020-07-06 01:40:36 +010095#define EMAC_TX_CTL0_TX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053096#define EMAC_TX_CTL1 0x14
Andre Przywarae6e29cc2020-07-06 01:40:36 +010097#define EMAC_TX_CTL1_TX_MD BIT(1)
98#define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
99#define EMAC_TX_CTL1_TX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530100#define EMAC_TX_FLOW_CTL 0x1c
101#define EMAC_TX_DMA_DESC 0x20
102#define EMAC_RX_CTL0 0x24
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100103#define EMAC_RX_CTL0_RX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530104#define EMAC_RX_CTL1 0x28
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100105#define EMAC_RX_CTL1_RX_MD BIT(1)
Andre Przywara59422822020-07-06 01:40:43 +0100106#define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
107#define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100108#define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
109#define EMAC_RX_CTL1_RX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530110#define EMAC_RX_DMA_DESC 0x34
111#define EMAC_MII_CMD 0x48
112#define EMAC_MII_DATA 0x4c
113#define EMAC_ADDR0_HIGH 0x50
114#define EMAC_ADDR0_LOW 0x54
115#define EMAC_TX_DMA_STA 0xb0
116#define EMAC_TX_CUR_DESC 0xb4
117#define EMAC_TX_CUR_BUF 0xb8
118#define EMAC_RX_DMA_STA 0xc0
119#define EMAC_RX_CUR_DESC 0xc4
120
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100121#define EMAC_DESC_OWN_DMA BIT(31)
122#define EMAC_DESC_LAST_DESC BIT(30)
123#define EMAC_DESC_FIRST_DESC BIT(29)
124#define EMAC_DESC_CHAIN_SECOND BIT(24)
125
Andre Przywara59422822020-07-06 01:40:43 +0100126#define EMAC_DESC_RX_ERROR_MASK 0x400068db
127
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530128DECLARE_GLOBAL_DATA_PTR;
129
Samuel Hollanda8791622023-01-22 16:51:02 -0600130struct emac_variant {
Samuel Holland71b8ea32023-01-22 16:51:05 -0600131 uint syscon_offset;
Samuel Holland195bb2d2023-01-22 16:51:04 -0600132 bool soc_has_internal_phy;
Samuel Holland62a2a682023-01-22 16:51:03 -0600133 bool support_rmii;
Samuel Hollanda8791622023-01-22 16:51:02 -0600134};
135
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530136struct emac_dma_desc {
137 u32 status;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100138 u32 ctl_size;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530139 u32 buf_addr;
140 u32 next;
141} __aligned(ARCH_DMA_MINALIGN);
142
143struct emac_eth_dev {
Tom Rini364d0022023-01-10 11:19:45 -0500144 struct emac_dma_desc rx_chain[CFG_TX_DESCR_NUM];
145 struct emac_dma_desc tx_chain[CFG_RX_DESCR_NUM];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530146 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
147 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
148
149 u32 interface;
150 u32 phyaddr;
151 u32 link;
152 u32 speed;
153 u32 duplex;
154 u32 phy_configured;
155 u32 tx_currdescnum;
156 u32 rx_currdescnum;
157 u32 addr;
158 u32 tx_slot;
159 bool use_internal_phy;
160
Samuel Hollanda8791622023-01-22 16:51:02 -0600161 const struct emac_variant *variant;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530162 void *mac_reg;
Samuel Holland71b8ea32023-01-22 16:51:05 -0600163 void *sysctl_reg;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530164 struct phy_device *phydev;
165 struct mii_dev *bus;
Jagan Tekicb63d282019-02-28 00:26:58 +0530166 struct clk tx_clk;
Jagan Teki727ed792019-02-28 00:27:00 +0530167 struct clk ephy_clk;
Jagan Tekicb63d282019-02-28 00:26:58 +0530168 struct reset_ctl tx_rst;
Jagan Teki727ed792019-02-28 00:27:00 +0530169 struct reset_ctl ephy_rst;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100170 struct gpio_desc reset_gpio;
Andre Przywara493e8ba2022-06-08 14:56:56 +0100171 struct udevice *phy_reg;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100172};
173
174
175struct sun8i_eth_pdata {
176 struct eth_pdata eth_pdata;
177 u32 reset_delays[3];
Icenowy Zheng525dc442018-11-23 00:37:48 +0100178 int tx_delay_ps;
179 int rx_delay_ps;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530180};
181
Philipp Tomsich3297b552017-02-22 19:46:41 +0100182
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530183static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
184{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100185 struct udevice *dev = bus->priv;
186 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100187 u32 mii_cmd;
188 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530189
Andre Przywara0dd619b2020-07-06 01:40:34 +0100190 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530191 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100192 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530193 MDIO_CMD_MII_PHY_ADDR_MASK;
194
Andre Przywarab41f2472020-07-06 01:40:45 +0100195 /*
196 * The EMAC clock is either 200 or 300 MHz, so we need a divider
197 * of 128 to get the MDIO frequency below the required 2.5 MHz.
198 */
Heinrich Schuchardt6ffc0232021-06-03 07:52:41 +0000199 if (!priv->use_internal_phy)
200 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
201 MDIO_CMD_MII_CLK_CSR_SHIFT;
Andre Przywarab41f2472020-07-06 01:40:45 +0100202
Andre Przywara0dd619b2020-07-06 01:40:34 +0100203 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530204
Andre Przywara0dd619b2020-07-06 01:40:34 +0100205 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530206
Andre Przywara0dd619b2020-07-06 01:40:34 +0100207 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
208 MDIO_CMD_MII_BUSY, false,
Tom Rini364d0022023-01-10 11:19:45 -0500209 CFG_MDIO_TIMEOUT, true);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100210 if (ret < 0)
211 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530212
Andre Przywara0dd619b2020-07-06 01:40:34 +0100213 return readl(priv->mac_reg + EMAC_MII_DATA);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530214}
215
216static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
217 u16 val)
218{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100219 struct udevice *dev = bus->priv;
220 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100221 u32 mii_cmd;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530222
Andre Przywara0dd619b2020-07-06 01:40:34 +0100223 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530224 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100225 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530226 MDIO_CMD_MII_PHY_ADDR_MASK;
227
Andre Przywarab41f2472020-07-06 01:40:45 +0100228 /*
229 * The EMAC clock is either 200 or 300 MHz, so we need a divider
230 * of 128 to get the MDIO frequency below the required 2.5 MHz.
231 */
Heinrich Schuchardt6ffc0232021-06-03 07:52:41 +0000232 if (!priv->use_internal_phy)
233 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
234 MDIO_CMD_MII_CLK_CSR_SHIFT;
Andre Przywarab41f2472020-07-06 01:40:45 +0100235
Andre Przywara0dd619b2020-07-06 01:40:34 +0100236 mii_cmd |= MDIO_CMD_MII_WRITE;
237 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530238
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530239 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100240 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530241
Andre Przywara0dd619b2020-07-06 01:40:34 +0100242 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
243 MDIO_CMD_MII_BUSY, false,
Tom Rini364d0022023-01-10 11:19:45 -0500244 CFG_MDIO_TIMEOUT, true);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530245}
246
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530247static int sun8i_eth_write_hwaddr(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530248{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530249 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700250 struct eth_pdata *pdata = dev_get_plat(dev);
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530251 uchar *mac_id = pdata->enetaddr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530252 u32 macid_lo, macid_hi;
253
254 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
255 (mac_id[3] << 24);
256 macid_hi = mac_id[4] + (mac_id[5] << 8);
257
258 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
259 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
260
261 return 0;
262}
263
264static void sun8i_adjust_link(struct emac_eth_dev *priv,
265 struct phy_device *phydev)
266{
267 u32 v;
268
269 v = readl(priv->mac_reg + EMAC_CTL0);
270
271 if (phydev->duplex)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100272 v |= EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530273 else
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100274 v &= ~EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530275
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100276 v &= ~EMAC_CTL0_SPEED_MASK;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530277
278 switch (phydev->speed) {
279 case 1000:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100280 v |= EMAC_CTL0_SPEED_1000;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530281 break;
282 case 100:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100283 v |= EMAC_CTL0_SPEED_100;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530284 break;
285 case 10:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100286 v |= EMAC_CTL0_SPEED_10;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530287 break;
288 }
289 writel(v, priv->mac_reg + EMAC_CTL0);
290}
291
Andre Przywara15651d82021-01-11 21:11:45 +0100292static u32 sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 reg)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530293{
294 if (priv->use_internal_phy) {
295 /* H3 based SoC's that has an Internal 100MBit PHY
296 * needs to be configured and powered up before use
297 */
Andre Przywara15651d82021-01-11 21:11:45 +0100298 reg &= ~H3_EPHY_DEFAULT_MASK;
299 reg |= H3_EPHY_DEFAULT_VALUE;
300 reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
301 reg &= ~H3_EPHY_SHUTDOWN;
302 return reg | H3_EPHY_SELECT;
303 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530304
Andre Przywara15651d82021-01-11 21:11:45 +0100305 /* This is to select External Gigabit PHY on those boards with
306 * an internal PHY. Does not hurt on other SoCs. Linux does
307 * it as well.
308 */
309 return reg & ~H3_EPHY_SELECT;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530310}
311
Icenowy Zheng525dc442018-11-23 00:37:48 +0100312static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
313 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530314{
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530315 u32 reg;
316
Samuel Holland71b8ea32023-01-22 16:51:05 -0600317 reg = readl(priv->sysctl_reg);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200318
Andre Przywara15651d82021-01-11 21:11:45 +0100319 reg = sun8i_emac_set_syscon_ephy(priv, reg);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530320
321 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland62a2a682023-01-22 16:51:03 -0600322 if (priv->variant->support_rmii)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530323 reg &= ~SC_RMII_EN;
324
325 switch (priv->interface) {
326 case PHY_INTERFACE_MODE_MII:
327 /* default */
328 break;
329 case PHY_INTERFACE_MODE_RGMII:
Andre Przywara43bb1582020-11-14 17:37:46 +0000330 case PHY_INTERFACE_MODE_RGMII_ID:
331 case PHY_INTERFACE_MODE_RGMII_RXID:
332 case PHY_INTERFACE_MODE_RGMII_TXID:
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530333 reg |= SC_EPIT | SC_ETCS_INT_GMII;
334 break;
335 case PHY_INTERFACE_MODE_RMII:
Samuel Holland62a2a682023-01-22 16:51:03 -0600336 if (priv->variant->support_rmii) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530337 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
Samuel Holland62a2a682023-01-22 16:51:03 -0600338 break;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530339 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530340 default:
341 debug("%s: Invalid PHY interface\n", __func__);
342 return -EINVAL;
343 }
344
Icenowy Zheng525dc442018-11-23 00:37:48 +0100345 if (pdata->tx_delay_ps)
346 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
347 & SC_ETXDC_MASK;
348
349 if (pdata->rx_delay_ps)
350 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
351 & SC_ERXDC_MASK;
352
Samuel Holland71b8ea32023-01-22 16:51:05 -0600353 writel(reg, priv->sysctl_reg);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530354
355 return 0;
356}
357
358static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
359{
360 struct phy_device *phydev;
361
362 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
363 if (!phydev)
364 return -ENODEV;
365
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530366 priv->phydev = phydev;
367 phy_config(priv->phydev);
368
369 return 0;
370}
371
Andre Przywara2e7dd262020-07-06 01:40:40 +0100372#define cache_clean_descriptor(desc) \
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200373 flush_dcache_range((uintptr_t)(desc), \
Andre Przywara2e7dd262020-07-06 01:40:40 +0100374 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
375
376#define cache_inv_descriptor(desc) \
377 invalidate_dcache_range((uintptr_t)(desc), \
378 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
379
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530380static void rx_descs_init(struct emac_eth_dev *priv)
381{
382 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
383 char *rxbuffs = &priv->rxbuffer[0];
384 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100385 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530386
Andre Przywara7408b092020-07-06 01:40:37 +0100387 /*
388 * Make sure we don't have dirty cache lines around, which could
389 * be cleaned to DRAM *after* the MAC has already written data to it.
390 */
391 invalidate_dcache_range((uintptr_t)desc_table_p,
392 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
393 invalidate_dcache_range((uintptr_t)rxbuffs,
394 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530395
Tom Rini364d0022023-01-10 11:19:45 -0500396 for (i = 0; i < CFG_RX_DESCR_NUM; i++) {
Andre Przywara4ab675e2020-07-06 01:40:41 +0100397 desc_p = &desc_table_p[i];
Tom Rini364d0022023-01-10 11:19:45 -0500398 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CFG_ETH_BUFSIZE];
Andre Przywara4ab675e2020-07-06 01:40:41 +0100399 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Tom Rini364d0022023-01-10 11:19:45 -0500400 desc_p->ctl_size = CFG_ETH_RXSIZE;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100401 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530402 }
403
404 /* Correcting the last pointer of the chain */
405 desc_p->next = (uintptr_t)&desc_table_p[0];
406
407 flush_dcache_range((uintptr_t)priv->rx_chain,
408 (uintptr_t)priv->rx_chain +
409 sizeof(priv->rx_chain));
410
411 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
412 priv->rx_currdescnum = 0;
413}
414
415static void tx_descs_init(struct emac_eth_dev *priv)
416{
417 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
418 char *txbuffs = &priv->txbuffer[0];
419 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100420 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530421
Tom Rini364d0022023-01-10 11:19:45 -0500422 for (i = 0; i < CFG_TX_DESCR_NUM; i++) {
Andre Przywara4ab675e2020-07-06 01:40:41 +0100423 desc_p = &desc_table_p[i];
Tom Rini364d0022023-01-10 11:19:45 -0500424 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CFG_ETH_BUFSIZE];
Andre Przywara4ab675e2020-07-06 01:40:41 +0100425 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100426 desc_p->ctl_size = 0;
Andre Przywaradf6f2712020-07-06 01:40:33 +0100427 desc_p->status = 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530428 }
429
430 /* Correcting the last pointer of the chain */
431 desc_p->next = (uintptr_t)&desc_table_p[0];
432
Andre Przywara8cd89602020-07-06 01:40:38 +0100433 /* Flush the first TX buffer descriptor we will tell the MAC about. */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100434 cache_clean_descriptor(desc_table_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530435
436 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
437 priv->tx_currdescnum = 0;
438}
439
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530440static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530441{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530442 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara874145f2020-07-06 01:40:32 +0100443 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530444
Andre Przywara6bdc70e2020-07-06 01:40:42 +0100445 /* Soft reset MAC */
446 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
447 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
448 EMAC_CTL1_SOFT_RST, false, 10, true);
449 if (ret) {
450 printf("%s: Timeout\n", __func__);
451 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530452 }
453
454 /* Rewrite mac address after reset */
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530455 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530456
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100457 /* transmission starts after the full frame arrived in TX DMA FIFO */
458 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530459
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100460 /*
461 * RX DMA reads data from RX DMA FIFO to host memory after a
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530462 * complete frame has been written to RX DMA FIFO
463 */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100464 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530465
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100466 /* DMA burst length */
467 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530468
469 /* Initialize rx/tx descriptors */
470 rx_descs_init(priv);
471 tx_descs_init(priv);
472
473 /* PHY Start Up */
Andre Przywara874145f2020-07-06 01:40:32 +0100474 ret = phy_startup(priv->phydev);
475 if (ret)
476 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530477
478 sun8i_adjust_link(priv, priv->phydev);
479
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100480 /* Start RX/TX DMA */
Andre Przywara59422822020-07-06 01:40:43 +0100481 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
482 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100483 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530484
485 /* Enable RX/TX */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100486 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
487 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530488
489 return 0;
490}
491
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530492static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530493{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530494 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530495 u32 status, desc_num = priv->rx_currdescnum;
496 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Andre Przywara59422822020-07-06 01:40:43 +0100497 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
498 int length;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530499
500 /* Invalidate entire buffer descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100501 cache_inv_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530502
503 status = desc_p->status;
504
505 /* Check for DMA own bit */
Andre Przywara59422822020-07-06 01:40:43 +0100506 if (status & EMAC_DESC_OWN_DMA)
507 return -EAGAIN;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530508
Andre Przywara59422822020-07-06 01:40:43 +0100509 length = (status >> 16) & 0x3fff;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530510
Andre Przywara59422822020-07-06 01:40:43 +0100511 /* make sure we read from DRAM, not our cache */
512 invalidate_dcache_range(data_start,
513 data_start + roundup(length, ARCH_DMA_MINALIGN));
514
515 if (status & EMAC_DESC_RX_ERROR_MASK) {
516 debug("RX: packet error: 0x%x\n",
517 status & EMAC_DESC_RX_ERROR_MASK);
518 return 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530519 }
Andre Przywara59422822020-07-06 01:40:43 +0100520 if (length < 0x40) {
521 debug("RX: Bad Packet (runt)\n");
522 return 0;
523 }
524
Tom Rini364d0022023-01-10 11:19:45 -0500525 if (length > CFG_ETH_RXSIZE) {
Andre Przywara59422822020-07-06 01:40:43 +0100526 debug("RX: Too large packet (%d bytes)\n", length);
527 return 0;
528 }
529
530 *packetp = (uchar *)(ulong)desc_p->buf_addr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530531
532 return length;
533}
534
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530535static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530536{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530537 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100538 u32 desc_num = priv->tx_currdescnum;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530539 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530540 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
541 uintptr_t data_end = data_start +
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530542 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530543
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100544 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530545
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530546 memcpy((void *)data_start, packet, length);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530547
548 /* Flush data to be sent */
549 flush_dcache_range(data_start, data_end);
550
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100551 /* frame begin and end */
552 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
553 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530554
Andre Przywara2e7dd262020-07-06 01:40:40 +0100555 /* make sure the MAC reads the actual data from DRAM */
556 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530557
558 /* Move to next Descriptor and wrap around */
Tom Rini364d0022023-01-10 11:19:45 -0500559 if (++desc_num >= CFG_TX_DESCR_NUM)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530560 desc_num = 0;
561 priv->tx_currdescnum = desc_num;
562
563 /* Start the DMA */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100564 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
565
566 /*
567 * Since we copied the data above, we return here without waiting
568 * for the packet to be actually send out.
569 */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530570
571 return 0;
572}
573
Sean Anderson4702aa22020-09-15 10:45:00 -0400574static int sun8i_emac_board_setup(struct udevice *dev,
575 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530576{
Jagan Tekicb63d282019-02-28 00:26:58 +0530577 int ret;
578
579 ret = clk_enable(&priv->tx_clk);
580 if (ret) {
581 dev_err(dev, "failed to enable TX clock\n");
582 return ret;
583 }
584
585 if (reset_valid(&priv->tx_rst)) {
586 ret = reset_deassert(&priv->tx_rst);
587 if (ret) {
588 dev_err(dev, "failed to deassert TX reset\n");
589 goto err_tx_clk;
590 }
591 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530592
Jagan Teki727ed792019-02-28 00:27:00 +0530593 /* Only H3/H5 have clock controls for internal EPHY */
594 if (clk_valid(&priv->ephy_clk)) {
595 ret = clk_enable(&priv->ephy_clk);
596 if (ret) {
597 dev_err(dev, "failed to enable EPHY TX clock\n");
598 return ret;
599 }
600 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530601
Jagan Teki727ed792019-02-28 00:27:00 +0530602 if (reset_valid(&priv->ephy_rst)) {
603 ret = reset_deassert(&priv->ephy_rst);
604 if (ret) {
605 dev_err(dev, "failed to deassert EPHY TX clock\n");
606 return ret;
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200607 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530608 }
609
Jagan Tekicb63d282019-02-28 00:26:58 +0530610 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200611
Jagan Tekicb63d282019-02-28 00:26:58 +0530612err_tx_clk:
613 clk_disable(&priv->tx_clk);
614 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530615}
616
Philipp Tomsich3297b552017-02-22 19:46:41 +0100617static int sun8i_mdio_reset(struct mii_dev *bus)
618{
619 struct udevice *dev = bus->priv;
620 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700621 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100622 int ret;
623
624 if (!dm_gpio_is_valid(&priv->reset_gpio))
625 return 0;
626
627 /* reset the phy */
628 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
629 if (ret)
630 return ret;
631
632 udelay(pdata->reset_delays[0]);
633
634 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
635 if (ret)
636 return ret;
637
638 udelay(pdata->reset_delays[1]);
639
640 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
641 if (ret)
642 return ret;
643
644 udelay(pdata->reset_delays[2]);
645
646 return 0;
647}
Philipp Tomsich3297b552017-02-22 19:46:41 +0100648
649static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530650{
651 struct mii_dev *bus = mdio_alloc();
652
653 if (!bus) {
654 debug("Failed to allocate MDIO bus\n");
655 return -ENOMEM;
656 }
657
658 bus->read = sun8i_mdio_read;
659 bus->write = sun8i_mdio_write;
660 snprintf(bus->name, sizeof(bus->name), name);
661 bus->priv = (void *)priv;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100662 bus->reset = sun8i_mdio_reset;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530663
664 return mdio_register(bus);
665}
666
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530667static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
668 int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530669{
670 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530671 u32 desc_num = priv->rx_currdescnum;
672 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530673
Andre Przywara2e7dd262020-07-06 01:40:40 +0100674 /* give the current descriptor back to the MAC */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100675 desc_p->status |= EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530676
677 /* Flush Status field of descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100678 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530679
680 /* Move to next desc and wrap-around condition. */
Tom Rini364d0022023-01-10 11:19:45 -0500681 if (++desc_num >= CFG_RX_DESCR_NUM)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530682 desc_num = 0;
683 priv->rx_currdescnum = desc_num;
684
685 return 0;
686}
687
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530688static void sun8i_emac_eth_stop(struct udevice *dev)
689{
690 struct emac_eth_dev *priv = dev_get_priv(dev);
691
692 /* Stop Rx/Tx transmitter */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100693 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
694 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530695
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100696 /* Stop RX/TX DMA */
697 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
698 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530699
700 phy_shutdown(priv->phydev);
701}
702
703static int sun8i_emac_eth_probe(struct udevice *dev)
704{
Simon Glassfa20e932020-12-03 16:55:20 -0700705 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Icenowy Zheng525dc442018-11-23 00:37:48 +0100706 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530707 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530708 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530709
710 priv->mac_reg = (void *)pdata->iobase;
711
Sean Anderson4702aa22020-09-15 10:45:00 -0400712 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekicb63d282019-02-28 00:26:58 +0530713 if (ret)
714 return ret;
715
Icenowy Zheng525dc442018-11-23 00:37:48 +0100716 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530717
Andre Przywara493e8ba2022-06-08 14:56:56 +0100718 if (priv->phy_reg)
719 regulator_set_enable(priv->phy_reg, true);
720
Philipp Tomsich3297b552017-02-22 19:46:41 +0100721 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530722 priv->bus = miiphy_get_dev_by_name(dev->name);
723
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530724 return sun8i_phy_init(priv, dev);
725}
726
727static const struct eth_ops sun8i_emac_eth_ops = {
728 .start = sun8i_emac_eth_start,
729 .write_hwaddr = sun8i_eth_write_hwaddr,
730 .send = sun8i_emac_eth_send,
731 .recv = sun8i_emac_eth_recv,
732 .free_pkt = sun8i_eth_free_pkt,
733 .stop = sun8i_emac_eth_stop,
734};
735
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530736static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki727ed792019-02-28 00:27:00 +0530737{
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530738 struct ofnode_phandle_args phandle;
739 int ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530740
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530741 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
742 NULL, 0, 0, &phandle);
743 if (ret)
744 return ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530745
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530746 /* If the PHY node is not a child of the internal MDIO bus, we are
747 * using some external PHY.
748 */
749 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
750 "allwinner,sun8i-h3-mdio-internal"))
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200751 return 0;
752
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530753 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
Jagan Teki727ed792019-02-28 00:27:00 +0530754 if (ret) {
755 dev_err(dev, "failed to get EPHY TX clock\n");
756 return ret;
757 }
758
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530759 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
Jagan Teki727ed792019-02-28 00:27:00 +0530760 if (ret) {
761 dev_err(dev, "failed to get EPHY TX reset\n");
762 return ret;
763 }
764
765 priv->use_internal_phy = true;
766
767 return 0;
768}
769
Simon Glassaad29ae2020-12-03 16:55:21 -0700770static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530771{
Simon Glassfa20e932020-12-03 16:55:20 -0700772 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100773 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530774 struct emac_eth_dev *priv = dev_get_priv(dev);
Samuel Holland71b8ea32023-01-22 16:51:05 -0600775 phys_addr_t syscon_base;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100776 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700777 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530778 int offset = 0;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100779 int reset_flags = GPIOD_IS_OUT;
Jagan Tekicb63d282019-02-28 00:26:58 +0530780 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530781
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900782 pdata->iobase = dev_read_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100783 if (pdata->iobase == FDT_ADDR_T_NONE) {
784 debug("%s: Cannot find MAC base address\n", __func__);
785 return -EINVAL;
786 }
787
Samuel Hollanda8791622023-01-22 16:51:02 -0600788 priv->variant = (const void *)dev_get_driver_data(dev);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200789
790 if (!priv->variant) {
791 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100792 return -EINVAL;
793 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200794
Jagan Tekicb63d282019-02-28 00:26:58 +0530795 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
796 if (ret) {
797 dev_err(dev, "failed to get TX clock\n");
798 return ret;
799 }
800
801 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
802 if (ret && ret != -ENOENT) {
803 dev_err(dev, "failed to get TX reset\n");
804 return ret;
805 }
806
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530807 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
808 if (offset < 0) {
809 debug("%s: cannot find syscon node\n", __func__);
810 return -EINVAL;
811 }
812
813 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
814 if (!reg) {
815 debug("%s: cannot find reg property in syscon node\n",
816 __func__);
817 return -EINVAL;
818 }
Samuel Holland71b8ea32023-01-22 16:51:05 -0600819
820 syscon_base = fdt_translate_address((void *)gd->fdt_blob, offset, reg);
821 if (syscon_base == FDT_ADDR_T_NONE) {
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530822 debug("%s: Cannot find syscon base address\n", __func__);
823 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100824 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530825
Samuel Holland71b8ea32023-01-22 16:51:05 -0600826 priv->sysctl_reg = (void *)syscon_base + priv->variant->syscon_offset;
827
Andre Przywara493e8ba2022-06-08 14:56:56 +0100828 device_get_supply_regulator(dev, "phy-supply", &priv->phy_reg);
829
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530830 pdata->phy_interface = -1;
831 priv->phyaddr = -1;
832 priv->use_internal_phy = false;
833
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100834 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Maksim Kiseleveda99312024-01-20 19:26:24 +0300835 if (offset >= 0)
836 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530837
Marek BehĂșnbc194772022-04-07 00:33:01 +0200838 pdata->phy_interface = dev_read_phy_mode(dev);
Samuel Holland712cc892022-07-15 00:20:56 -0500839 debug("phy interface %d\n", pdata->phy_interface);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200840 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530841 return -EINVAL;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530842
Samuel Holland195bb2d2023-01-22 16:51:04 -0600843 if (priv->variant->soc_has_internal_phy) {
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530844 ret = sun8i_handle_internal_phy(dev, priv);
Jagan Teki727ed792019-02-28 00:27:00 +0530845 if (ret)
846 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530847 }
848
849 priv->interface = pdata->phy_interface;
850
Icenowy Zheng525dc442018-11-23 00:37:48 +0100851 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
852 "allwinner,tx-delay-ps", 0);
853 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
854 printf("%s: Invalid TX delay value %d\n", __func__,
855 sun8i_pdata->tx_delay_ps);
856
857 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
858 "allwinner,rx-delay-ps", 0);
859 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
860 printf("%s: Invalid RX delay value %d\n", __func__,
861 sun8i_pdata->rx_delay_ps);
862
Simon Glass7a494432017-05-17 17:18:09 -0600863 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100864 "snps,reset-active-low"))
865 reset_flags |= GPIOD_ACTIVE_LOW;
866
867 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
868 &priv->reset_gpio, reset_flags);
869
870 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600871 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100872 "snps,reset-delays-us",
873 sun8i_pdata->reset_delays, 3);
874 } else if (ret == -ENOENT) {
875 ret = 0;
876 }
Philipp Tomsich3297b552017-02-22 19:46:41 +0100877
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530878 return 0;
879}
880
Samuel Hollanda8791622023-01-22 16:51:02 -0600881static const struct emac_variant emac_variant_a83t = {
Samuel Holland71b8ea32023-01-22 16:51:05 -0600882 .syscon_offset = 0x30,
Samuel Hollanda8791622023-01-22 16:51:02 -0600883};
884
885static const struct emac_variant emac_variant_h3 = {
Samuel Holland71b8ea32023-01-22 16:51:05 -0600886 .syscon_offset = 0x30,
Samuel Holland195bb2d2023-01-22 16:51:04 -0600887 .soc_has_internal_phy = true,
Samuel Holland62a2a682023-01-22 16:51:03 -0600888 .support_rmii = true,
Samuel Hollanda8791622023-01-22 16:51:02 -0600889};
890
891static const struct emac_variant emac_variant_r40 = {
Samuel Holland71b8ea32023-01-22 16:51:05 -0600892 .syscon_offset = 0x164,
Samuel Hollanda8791622023-01-22 16:51:02 -0600893};
894
Michael Walle1b3b9a42024-05-13 22:56:09 +0200895static const struct emac_variant emac_variant_v3s = {
896 .syscon_offset = 0x30,
897 .soc_has_internal_phy = true,
898};
899
Samuel Hollanda8791622023-01-22 16:51:02 -0600900static const struct emac_variant emac_variant_a64 = {
Samuel Holland71b8ea32023-01-22 16:51:05 -0600901 .syscon_offset = 0x30,
Samuel Holland62a2a682023-01-22 16:51:03 -0600902 .support_rmii = true,
Samuel Hollanda8791622023-01-22 16:51:02 -0600903};
904
905static const struct emac_variant emac_variant_h6 = {
Samuel Holland71b8ea32023-01-22 16:51:05 -0600906 .syscon_offset = 0x30,
Samuel Holland62a2a682023-01-22 16:51:03 -0600907 .support_rmii = true,
Samuel Hollanda8791622023-01-22 16:51:02 -0600908};
909
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530910static const struct udevice_id sun8i_emac_eth_ids[] = {
Samuel Hollanda8791622023-01-22 16:51:02 -0600911 { .compatible = "allwinner,sun8i-a83t-emac",
912 .data = (ulong)&emac_variant_a83t },
913 { .compatible = "allwinner,sun8i-h3-emac",
914 .data = (ulong)&emac_variant_h3 },
915 { .compatible = "allwinner,sun8i-r40-gmac",
916 .data = (ulong)&emac_variant_r40 },
Michael Walle1b3b9a42024-05-13 22:56:09 +0200917 { .compatible = "allwinner,sun8i-v3s-emac",
918 .data = (ulong)&emac_variant_v3s },
Samuel Hollanda8791622023-01-22 16:51:02 -0600919 { .compatible = "allwinner,sun50i-a64-emac",
920 .data = (ulong)&emac_variant_a64 },
921 { .compatible = "allwinner,sun50i-h6-emac",
922 .data = (ulong)&emac_variant_h6 },
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530923 { }
924};
925
926U_BOOT_DRIVER(eth_sun8i_emac) = {
927 .name = "eth_sun8i_emac",
928 .id = UCLASS_ETH,
929 .of_match = sun8i_emac_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700930 .of_to_plat = sun8i_emac_eth_of_to_plat,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530931 .probe = sun8i_emac_eth_probe,
932 .ops = &sun8i_emac_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700933 .priv_auto = sizeof(struct emac_eth_dev),
Simon Glass71fa5b42020-12-03 16:55:18 -0700934 .plat_auto = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530935 .flags = DM_FLAG_ALLOC_PRIV_DMA,
936};