blob: b23faa228e0d053142c6d2228af7a91b0ee15f2c [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>
19#include <asm/arch/clock.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053020#include <common.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053021#include <clk.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053022#include <dm.h>
23#include <fdt_support.h>
Simon Glass9bc15642020-02-03 07:36:16 -070024#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060026#include <linux/delay.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053027#include <linux/err.h>
28#include <malloc.h>
29#include <miiphy.h>
30#include <net.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053031#include <reset.h>
Andre Przywara0dd619b2020-07-06 01:40:34 +010032#include <wait_bit.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053033
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053034#define MDIO_CMD_MII_BUSY BIT(0)
35#define MDIO_CMD_MII_WRITE BIT(1)
36
37#define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
38#define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
39#define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
40#define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
Andre Przywarab41f2472020-07-06 01:40:45 +010041#define MDIO_CMD_MII_CLK_CSR_DIV_16 0x0
42#define MDIO_CMD_MII_CLK_CSR_DIV_32 0x1
43#define MDIO_CMD_MII_CLK_CSR_DIV_64 0x2
44#define MDIO_CMD_MII_CLK_CSR_DIV_128 0x3
45#define MDIO_CMD_MII_CLK_CSR_SHIFT 20
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053046
47#define CONFIG_TX_DESCR_NUM 32
48#define CONFIG_RX_DESCR_NUM 32
Hans de Goedefcdb3b32016-07-27 17:31:17 +020049#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
50
51/*
52 * The datasheet says that each descriptor can transfers up to 4096 bytes
53 * But later, the register documentation reduces that value to 2048,
54 * using 2048 cause strange behaviours and even BSP driver use 2047
55 */
56#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053057
58#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
59#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
60
61#define H3_EPHY_DEFAULT_VALUE 0x58000
62#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
63#define H3_EPHY_ADDR_SHIFT 20
64#define REG_PHY_ADDR_MASK GENMASK(4, 0)
65#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
66#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
67#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
68
69#define SC_RMII_EN BIT(13)
70#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
71#define SC_ETCS_MASK GENMASK(1, 0)
72#define SC_ETCS_EXT_GMII 0x1
73#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng525dc442018-11-23 00:37:48 +010074#define SC_ETXDC_MASK GENMASK(12, 10)
75#define SC_ETXDC_OFFSET 10
76#define SC_ERXDC_MASK GENMASK(9, 5)
77#define SC_ERXDC_OFFSET 5
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053078
79#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
80
81#define AHB_GATE_OFFSET_EPHY 0
82
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053083/* H3/A64 EMAC Register's offset */
84#define EMAC_CTL0 0x00
Andre Przywarae6e29cc2020-07-06 01:40:36 +010085#define EMAC_CTL0_FULL_DUPLEX BIT(0)
86#define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
87#define EMAC_CTL0_SPEED_10 (0x2 << 2)
88#define EMAC_CTL0_SPEED_100 (0x3 << 2)
89#define EMAC_CTL0_SPEED_1000 (0x0 << 2)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053090#define EMAC_CTL1 0x04
Andre Przywarae6e29cc2020-07-06 01:40:36 +010091#define EMAC_CTL1_SOFT_RST BIT(0)
92#define EMAC_CTL1_BURST_LEN_SHIFT 24
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053093#define EMAC_INT_STA 0x08
94#define EMAC_INT_EN 0x0c
95#define EMAC_TX_CTL0 0x10
Andre Przywarae6e29cc2020-07-06 01:40:36 +010096#define EMAC_TX_CTL0_TX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053097#define EMAC_TX_CTL1 0x14
Andre Przywarae6e29cc2020-07-06 01:40:36 +010098#define EMAC_TX_CTL1_TX_MD BIT(1)
99#define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
100#define EMAC_TX_CTL1_TX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530101#define EMAC_TX_FLOW_CTL 0x1c
102#define EMAC_TX_DMA_DESC 0x20
103#define EMAC_RX_CTL0 0x24
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100104#define EMAC_RX_CTL0_RX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530105#define EMAC_RX_CTL1 0x28
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100106#define EMAC_RX_CTL1_RX_MD BIT(1)
Andre Przywara59422822020-07-06 01:40:43 +0100107#define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
108#define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100109#define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
110#define EMAC_RX_CTL1_RX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530111#define EMAC_RX_DMA_DESC 0x34
112#define EMAC_MII_CMD 0x48
113#define EMAC_MII_DATA 0x4c
114#define EMAC_ADDR0_HIGH 0x50
115#define EMAC_ADDR0_LOW 0x54
116#define EMAC_TX_DMA_STA 0xb0
117#define EMAC_TX_CUR_DESC 0xb4
118#define EMAC_TX_CUR_BUF 0xb8
119#define EMAC_RX_DMA_STA 0xc0
120#define EMAC_RX_CUR_DESC 0xc4
121
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100122#define EMAC_DESC_OWN_DMA BIT(31)
123#define EMAC_DESC_LAST_DESC BIT(30)
124#define EMAC_DESC_FIRST_DESC BIT(29)
125#define EMAC_DESC_CHAIN_SECOND BIT(24)
126
Andre Przywara59422822020-07-06 01:40:43 +0100127#define EMAC_DESC_RX_ERROR_MASK 0x400068db
128
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530129DECLARE_GLOBAL_DATA_PTR;
130
131enum emac_variant {
132 A83T_EMAC = 1,
133 H3_EMAC,
134 A64_EMAC,
Lothar Feltene8cbced2018-07-13 10:45:28 +0200135 R40_GMAC,
Samuel Holland3386e9a2020-05-07 18:10:51 -0500136 H6_EMAC,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530137};
138
139struct emac_dma_desc {
140 u32 status;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100141 u32 ctl_size;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530142 u32 buf_addr;
143 u32 next;
144} __aligned(ARCH_DMA_MINALIGN);
145
146struct emac_eth_dev {
147 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
148 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
149 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
150 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
151
152 u32 interface;
153 u32 phyaddr;
154 u32 link;
155 u32 speed;
156 u32 duplex;
157 u32 phy_configured;
158 u32 tx_currdescnum;
159 u32 rx_currdescnum;
160 u32 addr;
161 u32 tx_slot;
162 bool use_internal_phy;
163
164 enum emac_variant variant;
165 void *mac_reg;
166 phys_addr_t sysctl_reg;
167 struct phy_device *phydev;
168 struct mii_dev *bus;
Jagan Tekicb63d282019-02-28 00:26:58 +0530169 struct clk tx_clk;
Jagan Teki727ed792019-02-28 00:27:00 +0530170 struct clk ephy_clk;
Jagan Tekicb63d282019-02-28 00:26:58 +0530171 struct reset_ctl tx_rst;
Jagan Teki727ed792019-02-28 00:27:00 +0530172 struct reset_ctl ephy_rst;
Simon Glassfa4689a2019-12-06 21:41:35 -0700173#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100174 struct gpio_desc reset_gpio;
175#endif
176};
177
178
179struct sun8i_eth_pdata {
180 struct eth_pdata eth_pdata;
181 u32 reset_delays[3];
Icenowy Zheng525dc442018-11-23 00:37:48 +0100182 int tx_delay_ps;
183 int rx_delay_ps;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530184};
185
Philipp Tomsich3297b552017-02-22 19:46:41 +0100186
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530187static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
188{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100189 struct udevice *dev = bus->priv;
190 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100191 u32 mii_cmd;
192 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530193
Andre Przywara0dd619b2020-07-06 01:40:34 +0100194 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530195 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100196 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530197 MDIO_CMD_MII_PHY_ADDR_MASK;
198
Andre Przywarab41f2472020-07-06 01:40:45 +0100199 /*
200 * The EMAC clock is either 200 or 300 MHz, so we need a divider
201 * of 128 to get the MDIO frequency below the required 2.5 MHz.
202 */
Heinrich Schuchardt6ffc0232021-06-03 07:52:41 +0000203 if (!priv->use_internal_phy)
204 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
205 MDIO_CMD_MII_CLK_CSR_SHIFT;
Andre Przywarab41f2472020-07-06 01:40:45 +0100206
Andre Przywara0dd619b2020-07-06 01:40:34 +0100207 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530208
Andre Przywara0dd619b2020-07-06 01:40:34 +0100209 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530210
Andre Przywara0dd619b2020-07-06 01:40:34 +0100211 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
212 MDIO_CMD_MII_BUSY, false,
213 CONFIG_MDIO_TIMEOUT, true);
214 if (ret < 0)
215 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530216
Andre Przywara0dd619b2020-07-06 01:40:34 +0100217 return readl(priv->mac_reg + EMAC_MII_DATA);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530218}
219
220static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
221 u16 val)
222{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100223 struct udevice *dev = bus->priv;
224 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100225 u32 mii_cmd;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530226
Andre Przywara0dd619b2020-07-06 01:40:34 +0100227 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530228 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100229 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530230 MDIO_CMD_MII_PHY_ADDR_MASK;
231
Andre Przywarab41f2472020-07-06 01:40:45 +0100232 /*
233 * The EMAC clock is either 200 or 300 MHz, so we need a divider
234 * of 128 to get the MDIO frequency below the required 2.5 MHz.
235 */
Heinrich Schuchardt6ffc0232021-06-03 07:52:41 +0000236 if (!priv->use_internal_phy)
237 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
238 MDIO_CMD_MII_CLK_CSR_SHIFT;
Andre Przywarab41f2472020-07-06 01:40:45 +0100239
Andre Przywara0dd619b2020-07-06 01:40:34 +0100240 mii_cmd |= MDIO_CMD_MII_WRITE;
241 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530242
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530243 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100244 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530245
Andre Przywara0dd619b2020-07-06 01:40:34 +0100246 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
247 MDIO_CMD_MII_BUSY, false,
248 CONFIG_MDIO_TIMEOUT, true);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530249}
250
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530251static int sun8i_eth_write_hwaddr(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530252{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530253 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700254 struct eth_pdata *pdata = dev_get_plat(dev);
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530255 uchar *mac_id = pdata->enetaddr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530256 u32 macid_lo, macid_hi;
257
258 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
259 (mac_id[3] << 24);
260 macid_hi = mac_id[4] + (mac_id[5] << 8);
261
262 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
263 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
264
265 return 0;
266}
267
268static void sun8i_adjust_link(struct emac_eth_dev *priv,
269 struct phy_device *phydev)
270{
271 u32 v;
272
273 v = readl(priv->mac_reg + EMAC_CTL0);
274
275 if (phydev->duplex)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100276 v |= EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530277 else
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100278 v &= ~EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530279
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100280 v &= ~EMAC_CTL0_SPEED_MASK;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530281
282 switch (phydev->speed) {
283 case 1000:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100284 v |= EMAC_CTL0_SPEED_1000;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530285 break;
286 case 100:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100287 v |= EMAC_CTL0_SPEED_100;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530288 break;
289 case 10:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100290 v |= EMAC_CTL0_SPEED_10;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530291 break;
292 }
293 writel(v, priv->mac_reg + EMAC_CTL0);
294}
295
Andre Przywara15651d82021-01-11 21:11:45 +0100296static u32 sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 reg)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530297{
298 if (priv->use_internal_phy) {
299 /* H3 based SoC's that has an Internal 100MBit PHY
300 * needs to be configured and powered up before use
301 */
Andre Przywara15651d82021-01-11 21:11:45 +0100302 reg &= ~H3_EPHY_DEFAULT_MASK;
303 reg |= H3_EPHY_DEFAULT_VALUE;
304 reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
305 reg &= ~H3_EPHY_SHUTDOWN;
306 return reg | H3_EPHY_SELECT;
307 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530308
Andre Przywara15651d82021-01-11 21:11:45 +0100309 /* This is to select External Gigabit PHY on those boards with
310 * an internal PHY. Does not hurt on other SoCs. Linux does
311 * it as well.
312 */
313 return reg & ~H3_EPHY_SELECT;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530314}
315
Icenowy Zheng525dc442018-11-23 00:37:48 +0100316static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
317 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530318{
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530319 u32 reg;
320
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530321 if (priv->variant == R40_GMAC) {
322 /* Select RGMII for R40 */
323 reg = readl(priv->sysctl_reg + 0x164);
Samuel Holland97f2cf12020-05-07 18:10:50 -0500324 reg |= SC_ETCS_INT_GMII |
325 SC_EPIT |
326 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530327
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530328 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200329 return 0;
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530330 }
331
332 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200333
Andre Przywara15651d82021-01-11 21:11:45 +0100334 reg = sun8i_emac_set_syscon_ephy(priv, reg);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530335
336 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500337 if (priv->variant == H3_EMAC ||
338 priv->variant == A64_EMAC ||
339 priv->variant == H6_EMAC)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530340 reg &= ~SC_RMII_EN;
341
342 switch (priv->interface) {
343 case PHY_INTERFACE_MODE_MII:
344 /* default */
345 break;
346 case PHY_INTERFACE_MODE_RGMII:
Andre Przywara43bb1582020-11-14 17:37:46 +0000347 case PHY_INTERFACE_MODE_RGMII_ID:
348 case PHY_INTERFACE_MODE_RGMII_RXID:
349 case PHY_INTERFACE_MODE_RGMII_TXID:
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530350 reg |= SC_EPIT | SC_ETCS_INT_GMII;
351 break;
352 case PHY_INTERFACE_MODE_RMII:
353 if (priv->variant == H3_EMAC ||
Samuel Holland3386e9a2020-05-07 18:10:51 -0500354 priv->variant == A64_EMAC ||
355 priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530356 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
357 break;
358 }
359 /* RMII not supported on A83T */
360 default:
361 debug("%s: Invalid PHY interface\n", __func__);
362 return -EINVAL;
363 }
364
Icenowy Zheng525dc442018-11-23 00:37:48 +0100365 if (pdata->tx_delay_ps)
366 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
367 & SC_ETXDC_MASK;
368
369 if (pdata->rx_delay_ps)
370 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
371 & SC_ERXDC_MASK;
372
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100373 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530374
375 return 0;
376}
377
378static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
379{
380 struct phy_device *phydev;
381
382 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
383 if (!phydev)
384 return -ENODEV;
385
386 phy_connect_dev(phydev, dev);
387
388 priv->phydev = phydev;
389 phy_config(priv->phydev);
390
391 return 0;
392}
393
Andre Przywara2e7dd262020-07-06 01:40:40 +0100394#define cache_clean_descriptor(desc) \
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200395 flush_dcache_range((uintptr_t)(desc), \
Andre Przywara2e7dd262020-07-06 01:40:40 +0100396 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
397
398#define cache_inv_descriptor(desc) \
399 invalidate_dcache_range((uintptr_t)(desc), \
400 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
401
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530402static void rx_descs_init(struct emac_eth_dev *priv)
403{
404 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
405 char *rxbuffs = &priv->rxbuffer[0];
406 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100407 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530408
Andre Przywara7408b092020-07-06 01:40:37 +0100409 /*
410 * Make sure we don't have dirty cache lines around, which could
411 * be cleaned to DRAM *after* the MAC has already written data to it.
412 */
413 invalidate_dcache_range((uintptr_t)desc_table_p,
414 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
415 invalidate_dcache_range((uintptr_t)rxbuffs,
416 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530417
Andre Przywara4ab675e2020-07-06 01:40:41 +0100418 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
419 desc_p = &desc_table_p[i];
420 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
421 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywara7408b092020-07-06 01:40:37 +0100422 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100423 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530424 }
425
426 /* Correcting the last pointer of the chain */
427 desc_p->next = (uintptr_t)&desc_table_p[0];
428
429 flush_dcache_range((uintptr_t)priv->rx_chain,
430 (uintptr_t)priv->rx_chain +
431 sizeof(priv->rx_chain));
432
433 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
434 priv->rx_currdescnum = 0;
435}
436
437static void tx_descs_init(struct emac_eth_dev *priv)
438{
439 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
440 char *txbuffs = &priv->txbuffer[0];
441 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100442 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530443
Andre Przywara4ab675e2020-07-06 01:40:41 +0100444 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
445 desc_p = &desc_table_p[i];
446 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
447 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100448 desc_p->ctl_size = 0;
Andre Przywaradf6f2712020-07-06 01:40:33 +0100449 desc_p->status = 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530450 }
451
452 /* Correcting the last pointer of the chain */
453 desc_p->next = (uintptr_t)&desc_table_p[0];
454
Andre Przywara8cd89602020-07-06 01:40:38 +0100455 /* Flush the first TX buffer descriptor we will tell the MAC about. */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100456 cache_clean_descriptor(desc_table_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530457
458 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
459 priv->tx_currdescnum = 0;
460}
461
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530462static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530463{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530464 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara874145f2020-07-06 01:40:32 +0100465 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530466
Andre Przywara6bdc70e2020-07-06 01:40:42 +0100467 /* Soft reset MAC */
468 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
469 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
470 EMAC_CTL1_SOFT_RST, false, 10, true);
471 if (ret) {
472 printf("%s: Timeout\n", __func__);
473 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530474 }
475
476 /* Rewrite mac address after reset */
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530477 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530478
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100479 /* transmission starts after the full frame arrived in TX DMA FIFO */
480 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530481
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100482 /*
483 * RX DMA reads data from RX DMA FIFO to host memory after a
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530484 * complete frame has been written to RX DMA FIFO
485 */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100486 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530487
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100488 /* DMA burst length */
489 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530490
491 /* Initialize rx/tx descriptors */
492 rx_descs_init(priv);
493 tx_descs_init(priv);
494
495 /* PHY Start Up */
Andre Przywara874145f2020-07-06 01:40:32 +0100496 ret = phy_startup(priv->phydev);
497 if (ret)
498 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530499
500 sun8i_adjust_link(priv, priv->phydev);
501
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100502 /* Start RX/TX DMA */
Andre Przywara59422822020-07-06 01:40:43 +0100503 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
504 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100505 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530506
507 /* Enable RX/TX */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100508 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
509 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530510
511 return 0;
512}
513
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530514static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530515{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530516 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530517 u32 status, desc_num = priv->rx_currdescnum;
518 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Andre Przywara59422822020-07-06 01:40:43 +0100519 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
520 int length;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530521
522 /* Invalidate entire buffer descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100523 cache_inv_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530524
525 status = desc_p->status;
526
527 /* Check for DMA own bit */
Andre Przywara59422822020-07-06 01:40:43 +0100528 if (status & EMAC_DESC_OWN_DMA)
529 return -EAGAIN;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530530
Andre Przywara59422822020-07-06 01:40:43 +0100531 length = (status >> 16) & 0x3fff;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530532
Andre Przywara59422822020-07-06 01:40:43 +0100533 /* make sure we read from DRAM, not our cache */
534 invalidate_dcache_range(data_start,
535 data_start + roundup(length, ARCH_DMA_MINALIGN));
536
537 if (status & EMAC_DESC_RX_ERROR_MASK) {
538 debug("RX: packet error: 0x%x\n",
539 status & EMAC_DESC_RX_ERROR_MASK);
540 return 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530541 }
Andre Przywara59422822020-07-06 01:40:43 +0100542 if (length < 0x40) {
543 debug("RX: Bad Packet (runt)\n");
544 return 0;
545 }
546
547 if (length > CONFIG_ETH_RXSIZE) {
548 debug("RX: Too large packet (%d bytes)\n", length);
549 return 0;
550 }
551
552 *packetp = (uchar *)(ulong)desc_p->buf_addr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530553
554 return length;
555}
556
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530557static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530558{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530559 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100560 u32 desc_num = priv->tx_currdescnum;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530561 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530562 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
563 uintptr_t data_end = data_start +
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530564 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530565
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100566 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530567
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530568 memcpy((void *)data_start, packet, length);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530569
570 /* Flush data to be sent */
571 flush_dcache_range(data_start, data_end);
572
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100573 /* frame begin and end */
574 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
575 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530576
Andre Przywara2e7dd262020-07-06 01:40:40 +0100577 /* make sure the MAC reads the actual data from DRAM */
578 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530579
580 /* Move to next Descriptor and wrap around */
581 if (++desc_num >= CONFIG_TX_DESCR_NUM)
582 desc_num = 0;
583 priv->tx_currdescnum = desc_num;
584
585 /* Start the DMA */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100586 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
587
588 /*
589 * Since we copied the data above, we return here without waiting
590 * for the packet to be actually send out.
591 */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530592
593 return 0;
594}
595
Sean Anderson4702aa22020-09-15 10:45:00 -0400596static int sun8i_emac_board_setup(struct udevice *dev,
597 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530598{
Jagan Tekicb63d282019-02-28 00:26:58 +0530599 int ret;
600
601 ret = clk_enable(&priv->tx_clk);
602 if (ret) {
603 dev_err(dev, "failed to enable TX clock\n");
604 return ret;
605 }
606
607 if (reset_valid(&priv->tx_rst)) {
608 ret = reset_deassert(&priv->tx_rst);
609 if (ret) {
610 dev_err(dev, "failed to deassert TX reset\n");
611 goto err_tx_clk;
612 }
613 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530614
Jagan Teki727ed792019-02-28 00:27:00 +0530615 /* Only H3/H5 have clock controls for internal EPHY */
616 if (clk_valid(&priv->ephy_clk)) {
617 ret = clk_enable(&priv->ephy_clk);
618 if (ret) {
619 dev_err(dev, "failed to enable EPHY TX clock\n");
620 return ret;
621 }
622 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530623
Jagan Teki727ed792019-02-28 00:27:00 +0530624 if (reset_valid(&priv->ephy_rst)) {
625 ret = reset_deassert(&priv->ephy_rst);
626 if (ret) {
627 dev_err(dev, "failed to deassert EPHY TX clock\n");
628 return ret;
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200629 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530630 }
631
Jagan Tekicb63d282019-02-28 00:26:58 +0530632 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200633
Jagan Tekicb63d282019-02-28 00:26:58 +0530634err_tx_clk:
635 clk_disable(&priv->tx_clk);
636 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530637}
638
Simon Glassfa4689a2019-12-06 21:41:35 -0700639#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100640static int sun8i_mdio_reset(struct mii_dev *bus)
641{
642 struct udevice *dev = bus->priv;
643 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700644 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100645 int ret;
646
647 if (!dm_gpio_is_valid(&priv->reset_gpio))
648 return 0;
649
650 /* reset the phy */
651 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
652 if (ret)
653 return ret;
654
655 udelay(pdata->reset_delays[0]);
656
657 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
658 if (ret)
659 return ret;
660
661 udelay(pdata->reset_delays[1]);
662
663 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
664 if (ret)
665 return ret;
666
667 udelay(pdata->reset_delays[2]);
668
669 return 0;
670}
671#endif
672
673static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530674{
675 struct mii_dev *bus = mdio_alloc();
676
677 if (!bus) {
678 debug("Failed to allocate MDIO bus\n");
679 return -ENOMEM;
680 }
681
682 bus->read = sun8i_mdio_read;
683 bus->write = sun8i_mdio_write;
684 snprintf(bus->name, sizeof(bus->name), name);
685 bus->priv = (void *)priv;
Simon Glassfa4689a2019-12-06 21:41:35 -0700686#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100687 bus->reset = sun8i_mdio_reset;
688#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530689
690 return mdio_register(bus);
691}
692
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530693static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
694 int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530695{
696 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530697 u32 desc_num = priv->rx_currdescnum;
698 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530699
Andre Przywara2e7dd262020-07-06 01:40:40 +0100700 /* give the current descriptor back to the MAC */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100701 desc_p->status |= EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530702
703 /* Flush Status field of descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100704 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530705
706 /* Move to next desc and wrap-around condition. */
707 if (++desc_num >= CONFIG_RX_DESCR_NUM)
708 desc_num = 0;
709 priv->rx_currdescnum = desc_num;
710
711 return 0;
712}
713
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530714static void sun8i_emac_eth_stop(struct udevice *dev)
715{
716 struct emac_eth_dev *priv = dev_get_priv(dev);
717
718 /* Stop Rx/Tx transmitter */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100719 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
720 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530721
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100722 /* Stop RX/TX DMA */
723 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
724 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530725
726 phy_shutdown(priv->phydev);
727}
728
729static int sun8i_emac_eth_probe(struct udevice *dev)
730{
Simon Glassfa20e932020-12-03 16:55:20 -0700731 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Icenowy Zheng525dc442018-11-23 00:37:48 +0100732 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530733 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530734 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530735
736 priv->mac_reg = (void *)pdata->iobase;
737
Sean Anderson4702aa22020-09-15 10:45:00 -0400738 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekicb63d282019-02-28 00:26:58 +0530739 if (ret)
740 return ret;
741
Icenowy Zheng525dc442018-11-23 00:37:48 +0100742 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530743
Philipp Tomsich3297b552017-02-22 19:46:41 +0100744 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530745 priv->bus = miiphy_get_dev_by_name(dev->name);
746
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530747 return sun8i_phy_init(priv, dev);
748}
749
750static const struct eth_ops sun8i_emac_eth_ops = {
751 .start = sun8i_emac_eth_start,
752 .write_hwaddr = sun8i_eth_write_hwaddr,
753 .send = sun8i_emac_eth_send,
754 .recv = sun8i_emac_eth_recv,
755 .free_pkt = sun8i_eth_free_pkt,
756 .stop = sun8i_emac_eth_stop,
757};
758
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530759static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki727ed792019-02-28 00:27:00 +0530760{
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530761 struct ofnode_phandle_args phandle;
762 int ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530763
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530764 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
765 NULL, 0, 0, &phandle);
766 if (ret)
767 return ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530768
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530769 /* If the PHY node is not a child of the internal MDIO bus, we are
770 * using some external PHY.
771 */
772 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
773 "allwinner,sun8i-h3-mdio-internal"))
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200774 return 0;
775
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530776 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
Jagan Teki727ed792019-02-28 00:27:00 +0530777 if (ret) {
778 dev_err(dev, "failed to get EPHY TX clock\n");
779 return ret;
780 }
781
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530782 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
Jagan Teki727ed792019-02-28 00:27:00 +0530783 if (ret) {
784 dev_err(dev, "failed to get EPHY TX reset\n");
785 return ret;
786 }
787
788 priv->use_internal_phy = true;
789
790 return 0;
791}
792
Simon Glassaad29ae2020-12-03 16:55:21 -0700793static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530794{
Simon Glassfa20e932020-12-03 16:55:20 -0700795 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100796 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530797 struct emac_eth_dev *priv = dev_get_priv(dev);
798 const char *phy_mode;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100799 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700800 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530801 int offset = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -0700802#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100803 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100804#endif
Jagan Tekicb63d282019-02-28 00:26:58 +0530805 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530806
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900807 pdata->iobase = dev_read_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100808 if (pdata->iobase == FDT_ADDR_T_NONE) {
809 debug("%s: Cannot find MAC base address\n", __func__);
810 return -EINVAL;
811 }
812
Lothar Feltene8cbced2018-07-13 10:45:28 +0200813 priv->variant = dev_get_driver_data(dev);
814
815 if (!priv->variant) {
816 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100817 return -EINVAL;
818 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200819
Jagan Tekicb63d282019-02-28 00:26:58 +0530820 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
821 if (ret) {
822 dev_err(dev, "failed to get TX clock\n");
823 return ret;
824 }
825
826 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
827 if (ret && ret != -ENOENT) {
828 dev_err(dev, "failed to get TX reset\n");
829 return ret;
830 }
831
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530832 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
833 if (offset < 0) {
834 debug("%s: cannot find syscon node\n", __func__);
835 return -EINVAL;
836 }
837
838 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
839 if (!reg) {
840 debug("%s: cannot find reg property in syscon node\n",
841 __func__);
842 return -EINVAL;
843 }
844 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
845 offset, reg);
846 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
847 debug("%s: Cannot find syscon base address\n", __func__);
848 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100849 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530850
851 pdata->phy_interface = -1;
852 priv->phyaddr = -1;
853 priv->use_internal_phy = false;
854
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100855 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100856 if (offset < 0) {
857 debug("%s: Cannot find PHY address\n", __func__);
858 return -EINVAL;
859 }
860 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530861
Simon Glassdd79d6e2017-01-17 16:52:55 -0700862 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530863
864 if (phy_mode)
865 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
866 printf("phy interface%d\n", pdata->phy_interface);
867
868 if (pdata->phy_interface == -1) {
869 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
870 return -EINVAL;
871 }
872
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530873 if (priv->variant == H3_EMAC) {
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530874 ret = sun8i_handle_internal_phy(dev, priv);
Jagan Teki727ed792019-02-28 00:27:00 +0530875 if (ret)
876 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530877 }
878
879 priv->interface = pdata->phy_interface;
880
Icenowy Zheng525dc442018-11-23 00:37:48 +0100881 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
882 "allwinner,tx-delay-ps", 0);
883 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
884 printf("%s: Invalid TX delay value %d\n", __func__,
885 sun8i_pdata->tx_delay_ps);
886
887 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
888 "allwinner,rx-delay-ps", 0);
889 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
890 printf("%s: Invalid RX delay value %d\n", __func__,
891 sun8i_pdata->rx_delay_ps);
892
Simon Glassfa4689a2019-12-06 21:41:35 -0700893#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass7a494432017-05-17 17:18:09 -0600894 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100895 "snps,reset-active-low"))
896 reset_flags |= GPIOD_ACTIVE_LOW;
897
898 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
899 &priv->reset_gpio, reset_flags);
900
901 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600902 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100903 "snps,reset-delays-us",
904 sun8i_pdata->reset_delays, 3);
905 } else if (ret == -ENOENT) {
906 ret = 0;
907 }
908#endif
909
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530910 return 0;
911}
912
913static const struct udevice_id sun8i_emac_eth_ids[] = {
914 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
915 {.compatible = "allwinner,sun50i-a64-emac",
916 .data = (uintptr_t)A64_EMAC },
917 {.compatible = "allwinner,sun8i-a83t-emac",
918 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene8cbced2018-07-13 10:45:28 +0200919 {.compatible = "allwinner,sun8i-r40-gmac",
920 .data = (uintptr_t)R40_GMAC },
Samuel Holland3386e9a2020-05-07 18:10:51 -0500921 {.compatible = "allwinner,sun50i-h6-emac",
922 .data = (uintptr_t)H6_EMAC },
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};