blob: cf15e9f475befaf7481ae5506f26a244520822fd [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>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053016#include <asm/io.h>
17#include <asm/arch/clock.h>
18#include <asm/arch/gpio.h>
19#include <common.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053020#include <clk.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053021#include <dm.h>
22#include <fdt_support.h>
Simon Glass9bc15642020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060025#include <linux/delay.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053026#include <linux/err.h>
27#include <malloc.h>
28#include <miiphy.h>
29#include <net.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053030#include <reset.h>
Andre Przywara26e549b2018-04-04 01:31:15 +010031#include <dt-bindings/pinctrl/sun4i-a10.h>
Andre Przywara0dd619b2020-07-06 01:40:34 +010032#include <wait_bit.h>
Simon Glassfa4689a2019-12-06 21:41:35 -070033#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +010034#include <asm-generic/gpio.h>
35#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053036
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053037#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
Andre Przywarab41f2472020-07-06 01:40:45 +010044#define MDIO_CMD_MII_CLK_CSR_DIV_16 0x0
45#define MDIO_CMD_MII_CLK_CSR_DIV_32 0x1
46#define MDIO_CMD_MII_CLK_CSR_DIV_64 0x2
47#define MDIO_CMD_MII_CLK_CSR_DIV_128 0x3
48#define MDIO_CMD_MII_CLK_CSR_SHIFT 20
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053049
50#define CONFIG_TX_DESCR_NUM 32
51#define CONFIG_RX_DESCR_NUM 32
Hans de Goedefcdb3b32016-07-27 17:31:17 +020052#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
53
54/*
55 * The datasheet says that each descriptor can transfers up to 4096 bytes
56 * But later, the register documentation reduces that value to 2048,
57 * using 2048 cause strange behaviours and even BSP driver use 2047
58 */
59#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053060
61#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
62#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
63
64#define H3_EPHY_DEFAULT_VALUE 0x58000
65#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
66#define H3_EPHY_ADDR_SHIFT 20
67#define REG_PHY_ADDR_MASK GENMASK(4, 0)
68#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
69#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
70#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
71
72#define SC_RMII_EN BIT(13)
73#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
74#define SC_ETCS_MASK GENMASK(1, 0)
75#define SC_ETCS_EXT_GMII 0x1
76#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng525dc442018-11-23 00:37:48 +010077#define SC_ETXDC_MASK GENMASK(12, 10)
78#define SC_ETXDC_OFFSET 10
79#define SC_ERXDC_MASK GENMASK(9, 5)
80#define SC_ERXDC_OFFSET 5
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053081
82#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
83
84#define AHB_GATE_OFFSET_EPHY 0
85
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020086/* IO mux settings */
87#define SUN8I_IOMUX_H3 2
Lothar Feltene8cbced2018-07-13 10:45:28 +020088#define SUN8I_IOMUX_R40 5
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020089#define SUN8I_IOMUX 4
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053090
91/* H3/A64 EMAC Register's offset */
92#define EMAC_CTL0 0x00
Andre Przywarae6e29cc2020-07-06 01:40:36 +010093#define EMAC_CTL0_FULL_DUPLEX BIT(0)
94#define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
95#define EMAC_CTL0_SPEED_10 (0x2 << 2)
96#define EMAC_CTL0_SPEED_100 (0x3 << 2)
97#define EMAC_CTL0_SPEED_1000 (0x0 << 2)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053098#define EMAC_CTL1 0x04
Andre Przywarae6e29cc2020-07-06 01:40:36 +010099#define EMAC_CTL1_SOFT_RST BIT(0)
100#define EMAC_CTL1_BURST_LEN_SHIFT 24
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530101#define EMAC_INT_STA 0x08
102#define EMAC_INT_EN 0x0c
103#define EMAC_TX_CTL0 0x10
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100104#define EMAC_TX_CTL0_TX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530105#define EMAC_TX_CTL1 0x14
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100106#define EMAC_TX_CTL1_TX_MD BIT(1)
107#define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
108#define EMAC_TX_CTL1_TX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530109#define EMAC_TX_FLOW_CTL 0x1c
110#define EMAC_TX_DMA_DESC 0x20
111#define EMAC_RX_CTL0 0x24
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100112#define EMAC_RX_CTL0_RX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530113#define EMAC_RX_CTL1 0x28
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100114#define EMAC_RX_CTL1_RX_MD BIT(1)
Andre Przywara59422822020-07-06 01:40:43 +0100115#define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
116#define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100117#define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
118#define EMAC_RX_CTL1_RX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530119#define EMAC_RX_DMA_DESC 0x34
120#define EMAC_MII_CMD 0x48
121#define EMAC_MII_DATA 0x4c
122#define EMAC_ADDR0_HIGH 0x50
123#define EMAC_ADDR0_LOW 0x54
124#define EMAC_TX_DMA_STA 0xb0
125#define EMAC_TX_CUR_DESC 0xb4
126#define EMAC_TX_CUR_BUF 0xb8
127#define EMAC_RX_DMA_STA 0xc0
128#define EMAC_RX_CUR_DESC 0xc4
129
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100130#define EMAC_DESC_OWN_DMA BIT(31)
131#define EMAC_DESC_LAST_DESC BIT(30)
132#define EMAC_DESC_FIRST_DESC BIT(29)
133#define EMAC_DESC_CHAIN_SECOND BIT(24)
134
Andre Przywara59422822020-07-06 01:40:43 +0100135#define EMAC_DESC_RX_ERROR_MASK 0x400068db
136
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530137DECLARE_GLOBAL_DATA_PTR;
138
139enum emac_variant {
140 A83T_EMAC = 1,
141 H3_EMAC,
142 A64_EMAC,
Lothar Feltene8cbced2018-07-13 10:45:28 +0200143 R40_GMAC,
Samuel Holland3386e9a2020-05-07 18:10:51 -0500144 H6_EMAC,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530145};
146
147struct emac_dma_desc {
148 u32 status;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100149 u32 ctl_size;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530150 u32 buf_addr;
151 u32 next;
152} __aligned(ARCH_DMA_MINALIGN);
153
154struct emac_eth_dev {
155 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
156 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
157 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
158 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
159
160 u32 interface;
161 u32 phyaddr;
162 u32 link;
163 u32 speed;
164 u32 duplex;
165 u32 phy_configured;
166 u32 tx_currdescnum;
167 u32 rx_currdescnum;
168 u32 addr;
169 u32 tx_slot;
170 bool use_internal_phy;
171
172 enum emac_variant variant;
173 void *mac_reg;
174 phys_addr_t sysctl_reg;
175 struct phy_device *phydev;
176 struct mii_dev *bus;
Jagan Tekicb63d282019-02-28 00:26:58 +0530177 struct clk tx_clk;
Jagan Teki727ed792019-02-28 00:27:00 +0530178 struct clk ephy_clk;
Jagan Tekicb63d282019-02-28 00:26:58 +0530179 struct reset_ctl tx_rst;
Jagan Teki727ed792019-02-28 00:27:00 +0530180 struct reset_ctl ephy_rst;
Simon Glassfa4689a2019-12-06 21:41:35 -0700181#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100182 struct gpio_desc reset_gpio;
183#endif
184};
185
186
187struct sun8i_eth_pdata {
188 struct eth_pdata eth_pdata;
189 u32 reset_delays[3];
Icenowy Zheng525dc442018-11-23 00:37:48 +0100190 int tx_delay_ps;
191 int rx_delay_ps;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530192};
193
Philipp Tomsich3297b552017-02-22 19:46:41 +0100194
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530195static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
196{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100197 struct udevice *dev = bus->priv;
198 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100199 u32 mii_cmd;
200 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530201
Andre Przywara0dd619b2020-07-06 01:40:34 +0100202 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530203 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100204 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530205 MDIO_CMD_MII_PHY_ADDR_MASK;
206
Andre Przywarab41f2472020-07-06 01:40:45 +0100207 /*
208 * The EMAC clock is either 200 or 300 MHz, so we need a divider
209 * of 128 to get the MDIO frequency below the required 2.5 MHz.
210 */
211 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 << MDIO_CMD_MII_CLK_CSR_SHIFT;
212
Andre Przywara0dd619b2020-07-06 01:40:34 +0100213 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530214
Andre Przywara0dd619b2020-07-06 01:40:34 +0100215 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530216
Andre Przywara0dd619b2020-07-06 01:40:34 +0100217 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
218 MDIO_CMD_MII_BUSY, false,
219 CONFIG_MDIO_TIMEOUT, true);
220 if (ret < 0)
221 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530222
Andre Przywara0dd619b2020-07-06 01:40:34 +0100223 return readl(priv->mac_reg + EMAC_MII_DATA);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530224}
225
226static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
227 u16 val)
228{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100229 struct udevice *dev = bus->priv;
230 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100231 u32 mii_cmd;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530232
Andre Przywara0dd619b2020-07-06 01:40:34 +0100233 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530234 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100235 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530236 MDIO_CMD_MII_PHY_ADDR_MASK;
237
Andre Przywarab41f2472020-07-06 01:40:45 +0100238 /*
239 * The EMAC clock is either 200 or 300 MHz, so we need a divider
240 * of 128 to get the MDIO frequency below the required 2.5 MHz.
241 */
242 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 << MDIO_CMD_MII_CLK_CSR_SHIFT;
243
Andre Przywara0dd619b2020-07-06 01:40:34 +0100244 mii_cmd |= MDIO_CMD_MII_WRITE;
245 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530246
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530247 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100248 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530249
Andre Przywara0dd619b2020-07-06 01:40:34 +0100250 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
251 MDIO_CMD_MII_BUSY, false,
252 CONFIG_MDIO_TIMEOUT, true);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530253}
254
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530255static int sun8i_eth_write_hwaddr(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530256{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530257 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700258 struct eth_pdata *pdata = dev_get_plat(dev);
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530259 uchar *mac_id = pdata->enetaddr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530260 u32 macid_lo, macid_hi;
261
262 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
263 (mac_id[3] << 24);
264 macid_hi = mac_id[4] + (mac_id[5] << 8);
265
266 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
267 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
268
269 return 0;
270}
271
272static void sun8i_adjust_link(struct emac_eth_dev *priv,
273 struct phy_device *phydev)
274{
275 u32 v;
276
277 v = readl(priv->mac_reg + EMAC_CTL0);
278
279 if (phydev->duplex)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100280 v |= EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530281 else
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100282 v &= ~EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530283
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100284 v &= ~EMAC_CTL0_SPEED_MASK;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530285
286 switch (phydev->speed) {
287 case 1000:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100288 v |= EMAC_CTL0_SPEED_1000;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530289 break;
290 case 100:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100291 v |= EMAC_CTL0_SPEED_100;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530292 break;
293 case 10:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100294 v |= EMAC_CTL0_SPEED_10;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530295 break;
296 }
297 writel(v, priv->mac_reg + EMAC_CTL0);
298}
299
300static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
301{
302 if (priv->use_internal_phy) {
303 /* H3 based SoC's that has an Internal 100MBit PHY
304 * needs to be configured and powered up before use
305 */
306 *reg &= ~H3_EPHY_DEFAULT_MASK;
307 *reg |= H3_EPHY_DEFAULT_VALUE;
308 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
309 *reg &= ~H3_EPHY_SHUTDOWN;
310 *reg |= H3_EPHY_SELECT;
311 } else
312 /* This is to select External Gigabit PHY on
313 * the boards with H3 SoC.
314 */
315 *reg &= ~H3_EPHY_SELECT;
316
317 return 0;
318}
319
Icenowy Zheng525dc442018-11-23 00:37:48 +0100320static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
321 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530322{
323 int ret;
324 u32 reg;
325
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530326 if (priv->variant == R40_GMAC) {
327 /* Select RGMII for R40 */
328 reg = readl(priv->sysctl_reg + 0x164);
Samuel Holland97f2cf12020-05-07 18:10:50 -0500329 reg |= SC_ETCS_INT_GMII |
330 SC_EPIT |
331 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530332
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530333 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200334 return 0;
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530335 }
336
337 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200338
Samuel Holland3386e9a2020-05-07 18:10:51 -0500339 if (priv->variant == H3_EMAC || priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530340 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
341 if (ret)
342 return ret;
343 }
344
345 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500346 if (priv->variant == H3_EMAC ||
347 priv->variant == A64_EMAC ||
348 priv->variant == H6_EMAC)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530349 reg &= ~SC_RMII_EN;
350
351 switch (priv->interface) {
352 case PHY_INTERFACE_MODE_MII:
353 /* default */
354 break;
355 case PHY_INTERFACE_MODE_RGMII:
356 reg |= SC_EPIT | SC_ETCS_INT_GMII;
357 break;
358 case PHY_INTERFACE_MODE_RMII:
359 if (priv->variant == H3_EMAC ||
Samuel Holland3386e9a2020-05-07 18:10:51 -0500360 priv->variant == A64_EMAC ||
361 priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530362 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
363 break;
364 }
365 /* RMII not supported on A83T */
366 default:
367 debug("%s: Invalid PHY interface\n", __func__);
368 return -EINVAL;
369 }
370
Icenowy Zheng525dc442018-11-23 00:37:48 +0100371 if (pdata->tx_delay_ps)
372 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
373 & SC_ETXDC_MASK;
374
375 if (pdata->rx_delay_ps)
376 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
377 & SC_ERXDC_MASK;
378
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100379 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530380
381 return 0;
382}
383
384static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
385{
386 struct phy_device *phydev;
387
388 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
389 if (!phydev)
390 return -ENODEV;
391
392 phy_connect_dev(phydev, dev);
393
394 priv->phydev = phydev;
395 phy_config(priv->phydev);
396
397 return 0;
398}
399
Andre Przywara2e7dd262020-07-06 01:40:40 +0100400#define cache_clean_descriptor(desc) \
401 flush_dcache_range((uintptr_t)(desc), \
402 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
403
404#define cache_inv_descriptor(desc) \
405 invalidate_dcache_range((uintptr_t)(desc), \
406 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
407
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530408static void rx_descs_init(struct emac_eth_dev *priv)
409{
410 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
411 char *rxbuffs = &priv->rxbuffer[0];
412 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100413 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530414
Andre Przywara7408b092020-07-06 01:40:37 +0100415 /*
416 * Make sure we don't have dirty cache lines around, which could
417 * be cleaned to DRAM *after* the MAC has already written data to it.
418 */
419 invalidate_dcache_range((uintptr_t)desc_table_p,
420 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
421 invalidate_dcache_range((uintptr_t)rxbuffs,
422 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530423
Andre Przywara4ab675e2020-07-06 01:40:41 +0100424 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
425 desc_p = &desc_table_p[i];
426 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
427 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywara7408b092020-07-06 01:40:37 +0100428 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100429 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530430 }
431
432 /* Correcting the last pointer of the chain */
433 desc_p->next = (uintptr_t)&desc_table_p[0];
434
435 flush_dcache_range((uintptr_t)priv->rx_chain,
436 (uintptr_t)priv->rx_chain +
437 sizeof(priv->rx_chain));
438
439 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
440 priv->rx_currdescnum = 0;
441}
442
443static void tx_descs_init(struct emac_eth_dev *priv)
444{
445 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
446 char *txbuffs = &priv->txbuffer[0];
447 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100448 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530449
Andre Przywara4ab675e2020-07-06 01:40:41 +0100450 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
451 desc_p = &desc_table_p[i];
452 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
453 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100454 desc_p->ctl_size = 0;
Andre Przywaradf6f2712020-07-06 01:40:33 +0100455 desc_p->status = 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530456 }
457
458 /* Correcting the last pointer of the chain */
459 desc_p->next = (uintptr_t)&desc_table_p[0];
460
Andre Przywara8cd89602020-07-06 01:40:38 +0100461 /* Flush the first TX buffer descriptor we will tell the MAC about. */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100462 cache_clean_descriptor(desc_table_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530463
464 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
465 priv->tx_currdescnum = 0;
466}
467
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530468static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530469{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530470 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara874145f2020-07-06 01:40:32 +0100471 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530472
Andre Przywara6bdc70e2020-07-06 01:40:42 +0100473 /* Soft reset MAC */
474 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
475 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
476 EMAC_CTL1_SOFT_RST, false, 10, true);
477 if (ret) {
478 printf("%s: Timeout\n", __func__);
479 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530480 }
481
482 /* Rewrite mac address after reset */
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530483 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530484
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100485 /* transmission starts after the full frame arrived in TX DMA FIFO */
486 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530487
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100488 /*
489 * RX DMA reads data from RX DMA FIFO to host memory after a
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530490 * complete frame has been written to RX DMA FIFO
491 */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100492 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530493
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100494 /* DMA burst length */
495 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530496
497 /* Initialize rx/tx descriptors */
498 rx_descs_init(priv);
499 tx_descs_init(priv);
500
501 /* PHY Start Up */
Andre Przywara874145f2020-07-06 01:40:32 +0100502 ret = phy_startup(priv->phydev);
503 if (ret)
504 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530505
506 sun8i_adjust_link(priv, priv->phydev);
507
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100508 /* Start RX/TX DMA */
Andre Przywara59422822020-07-06 01:40:43 +0100509 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
510 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100511 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530512
513 /* Enable RX/TX */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100514 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
515 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530516
517 return 0;
518}
519
520static int parse_phy_pins(struct udevice *dev)
521{
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200522 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530523 int offset;
524 const char *pin_name;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100525 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530526
Simon Glassdd79d6e2017-01-17 16:52:55 -0700527 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530528 "pinctrl-0");
529 if (offset < 0) {
530 printf("WARNING: emac: cannot find pinctrl-0 node\n");
531 return offset;
532 }
533
534 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywara26e549b2018-04-04 01:31:15 +0100535 "drive-strength", ~0);
536 if (drive != ~0) {
537 if (drive <= 10)
538 drive = SUN4I_PINCTRL_10_MA;
539 else if (drive <= 20)
540 drive = SUN4I_PINCTRL_20_MA;
541 else if (drive <= 30)
542 drive = SUN4I_PINCTRL_30_MA;
543 else
544 drive = SUN4I_PINCTRL_40_MA;
Andre Przywara26e549b2018-04-04 01:31:15 +0100545 }
546
547 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
548 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywara26e549b2018-04-04 01:31:15 +0100549 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
550 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100551
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530552 for (i = 0; ; i++) {
553 int pin;
554
Simon Glassb0ea7402016-10-02 17:59:28 -0600555 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100556 "pins", i, NULL);
557 if (!pin_name)
558 break;
Andre Przywara26e549b2018-04-04 01:31:15 +0100559
560 pin = sunxi_name_to_gpio(pin_name);
561 if (pin < 0)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530562 continue;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530563
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200564 if (priv->variant == H3_EMAC)
565 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500566 else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC)
Lothar Feltene8cbced2018-07-13 10:45:28 +0200567 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200568 else
569 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
570
Andre Przywara26e549b2018-04-04 01:31:15 +0100571 if (drive != ~0)
572 sunxi_gpio_set_drv(pin, drive);
573 if (pull != ~0)
574 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530575 }
576
577 if (!i) {
Andre Przywara26e549b2018-04-04 01:31:15 +0100578 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530579 return -2;
580 }
581
582 return 0;
583}
584
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530585static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530586{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530587 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530588 u32 status, desc_num = priv->rx_currdescnum;
589 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Andre Przywara59422822020-07-06 01:40:43 +0100590 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
591 int length;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530592
593 /* Invalidate entire buffer descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100594 cache_inv_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530595
596 status = desc_p->status;
597
598 /* Check for DMA own bit */
Andre Przywara59422822020-07-06 01:40:43 +0100599 if (status & EMAC_DESC_OWN_DMA)
600 return -EAGAIN;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530601
Andre Przywara59422822020-07-06 01:40:43 +0100602 length = (status >> 16) & 0x3fff;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530603
Andre Przywara59422822020-07-06 01:40:43 +0100604 /* make sure we read from DRAM, not our cache */
605 invalidate_dcache_range(data_start,
606 data_start + roundup(length, ARCH_DMA_MINALIGN));
607
608 if (status & EMAC_DESC_RX_ERROR_MASK) {
609 debug("RX: packet error: 0x%x\n",
610 status & EMAC_DESC_RX_ERROR_MASK);
611 return 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530612 }
Andre Przywara59422822020-07-06 01:40:43 +0100613 if (length < 0x40) {
614 debug("RX: Bad Packet (runt)\n");
615 return 0;
616 }
617
618 if (length > CONFIG_ETH_RXSIZE) {
619 debug("RX: Too large packet (%d bytes)\n", length);
620 return 0;
621 }
622
623 *packetp = (uchar *)(ulong)desc_p->buf_addr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530624
625 return length;
626}
627
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530628static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530629{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530630 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100631 u32 desc_num = priv->tx_currdescnum;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530632 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530633 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
634 uintptr_t data_end = data_start +
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530635 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530636
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100637 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530638
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530639 memcpy((void *)data_start, packet, length);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530640
641 /* Flush data to be sent */
642 flush_dcache_range(data_start, data_end);
643
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100644 /* frame begin and end */
645 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
646 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530647
Andre Przywara2e7dd262020-07-06 01:40:40 +0100648 /* make sure the MAC reads the actual data from DRAM */
649 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530650
651 /* Move to next Descriptor and wrap around */
652 if (++desc_num >= CONFIG_TX_DESCR_NUM)
653 desc_num = 0;
654 priv->tx_currdescnum = desc_num;
655
656 /* Start the DMA */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100657 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
658
659 /*
660 * Since we copied the data above, we return here without waiting
661 * for the packet to be actually send out.
662 */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530663
664 return 0;
665}
666
Sean Anderson4702aa22020-09-15 10:45:00 -0400667static int sun8i_emac_board_setup(struct udevice *dev,
668 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530669{
Jagan Tekicb63d282019-02-28 00:26:58 +0530670 int ret;
671
672 ret = clk_enable(&priv->tx_clk);
673 if (ret) {
674 dev_err(dev, "failed to enable TX clock\n");
675 return ret;
676 }
677
678 if (reset_valid(&priv->tx_rst)) {
679 ret = reset_deassert(&priv->tx_rst);
680 if (ret) {
681 dev_err(dev, "failed to deassert TX reset\n");
682 goto err_tx_clk;
683 }
684 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530685
Jagan Teki727ed792019-02-28 00:27:00 +0530686 /* Only H3/H5 have clock controls for internal EPHY */
687 if (clk_valid(&priv->ephy_clk)) {
688 ret = clk_enable(&priv->ephy_clk);
689 if (ret) {
690 dev_err(dev, "failed to enable EPHY TX clock\n");
691 return ret;
692 }
693 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530694
Jagan Teki727ed792019-02-28 00:27:00 +0530695 if (reset_valid(&priv->ephy_rst)) {
696 ret = reset_deassert(&priv->ephy_rst);
697 if (ret) {
698 dev_err(dev, "failed to deassert EPHY TX clock\n");
699 return ret;
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200700 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530701 }
702
Jagan Tekicb63d282019-02-28 00:26:58 +0530703 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200704
Jagan Tekicb63d282019-02-28 00:26:58 +0530705err_tx_clk:
706 clk_disable(&priv->tx_clk);
707 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530708}
709
Simon Glassfa4689a2019-12-06 21:41:35 -0700710#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100711static int sun8i_mdio_reset(struct mii_dev *bus)
712{
713 struct udevice *dev = bus->priv;
714 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700715 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100716 int ret;
717
718 if (!dm_gpio_is_valid(&priv->reset_gpio))
719 return 0;
720
721 /* reset the phy */
722 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
723 if (ret)
724 return ret;
725
726 udelay(pdata->reset_delays[0]);
727
728 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
729 if (ret)
730 return ret;
731
732 udelay(pdata->reset_delays[1]);
733
734 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
735 if (ret)
736 return ret;
737
738 udelay(pdata->reset_delays[2]);
739
740 return 0;
741}
742#endif
743
744static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530745{
746 struct mii_dev *bus = mdio_alloc();
747
748 if (!bus) {
749 debug("Failed to allocate MDIO bus\n");
750 return -ENOMEM;
751 }
752
753 bus->read = sun8i_mdio_read;
754 bus->write = sun8i_mdio_write;
755 snprintf(bus->name, sizeof(bus->name), name);
756 bus->priv = (void *)priv;
Simon Glassfa4689a2019-12-06 21:41:35 -0700757#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100758 bus->reset = sun8i_mdio_reset;
759#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530760
761 return mdio_register(bus);
762}
763
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530764static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
765 int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530766{
767 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530768 u32 desc_num = priv->rx_currdescnum;
769 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530770
Andre Przywara2e7dd262020-07-06 01:40:40 +0100771 /* give the current descriptor back to the MAC */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100772 desc_p->status |= EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530773
774 /* Flush Status field of descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100775 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530776
777 /* Move to next desc and wrap-around condition. */
778 if (++desc_num >= CONFIG_RX_DESCR_NUM)
779 desc_num = 0;
780 priv->rx_currdescnum = desc_num;
781
782 return 0;
783}
784
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530785static void sun8i_emac_eth_stop(struct udevice *dev)
786{
787 struct emac_eth_dev *priv = dev_get_priv(dev);
788
789 /* Stop Rx/Tx transmitter */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100790 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
791 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530792
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100793 /* Stop RX/TX DMA */
794 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
795 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530796
797 phy_shutdown(priv->phydev);
798}
799
800static int sun8i_emac_eth_probe(struct udevice *dev)
801{
Simon Glassfa20e932020-12-03 16:55:20 -0700802 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Icenowy Zheng525dc442018-11-23 00:37:48 +0100803 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530804 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530805 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530806
807 priv->mac_reg = (void *)pdata->iobase;
808
Sean Anderson4702aa22020-09-15 10:45:00 -0400809 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekicb63d282019-02-28 00:26:58 +0530810 if (ret)
811 return ret;
812
Icenowy Zheng525dc442018-11-23 00:37:48 +0100813 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530814
Philipp Tomsich3297b552017-02-22 19:46:41 +0100815 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530816 priv->bus = miiphy_get_dev_by_name(dev->name);
817
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530818 return sun8i_phy_init(priv, dev);
819}
820
821static const struct eth_ops sun8i_emac_eth_ops = {
822 .start = sun8i_emac_eth_start,
823 .write_hwaddr = sun8i_eth_write_hwaddr,
824 .send = sun8i_emac_eth_send,
825 .recv = sun8i_emac_eth_recv,
826 .free_pkt = sun8i_eth_free_pkt,
827 .stop = sun8i_emac_eth_stop,
828};
829
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530830static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki727ed792019-02-28 00:27:00 +0530831{
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530832 struct ofnode_phandle_args phandle;
833 int ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530834
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530835 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
836 NULL, 0, 0, &phandle);
837 if (ret)
838 return ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530839
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530840 /* If the PHY node is not a child of the internal MDIO bus, we are
841 * using some external PHY.
842 */
843 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
844 "allwinner,sun8i-h3-mdio-internal"))
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200845 return 0;
846
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530847 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
Jagan Teki727ed792019-02-28 00:27:00 +0530848 if (ret) {
849 dev_err(dev, "failed to get EPHY TX clock\n");
850 return ret;
851 }
852
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530853 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
Jagan Teki727ed792019-02-28 00:27:00 +0530854 if (ret) {
855 dev_err(dev, "failed to get EPHY TX reset\n");
856 return ret;
857 }
858
859 priv->use_internal_phy = true;
860
861 return 0;
862}
863
Simon Glassaad29ae2020-12-03 16:55:21 -0700864static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530865{
Simon Glassfa20e932020-12-03 16:55:20 -0700866 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100867 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530868 struct emac_eth_dev *priv = dev_get_priv(dev);
869 const char *phy_mode;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100870 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700871 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530872 int offset = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -0700873#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100874 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100875#endif
Jagan Tekicb63d282019-02-28 00:26:58 +0530876 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530877
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900878 pdata->iobase = dev_read_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100879 if (pdata->iobase == FDT_ADDR_T_NONE) {
880 debug("%s: Cannot find MAC base address\n", __func__);
881 return -EINVAL;
882 }
883
Lothar Feltene8cbced2018-07-13 10:45:28 +0200884 priv->variant = dev_get_driver_data(dev);
885
886 if (!priv->variant) {
887 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100888 return -EINVAL;
889 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200890
Jagan Tekicb63d282019-02-28 00:26:58 +0530891 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
892 if (ret) {
893 dev_err(dev, "failed to get TX clock\n");
894 return ret;
895 }
896
897 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
898 if (ret && ret != -ENOENT) {
899 dev_err(dev, "failed to get TX reset\n");
900 return ret;
901 }
902
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530903 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
904 if (offset < 0) {
905 debug("%s: cannot find syscon node\n", __func__);
906 return -EINVAL;
907 }
908
909 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
910 if (!reg) {
911 debug("%s: cannot find reg property in syscon node\n",
912 __func__);
913 return -EINVAL;
914 }
915 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
916 offset, reg);
917 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
918 debug("%s: Cannot find syscon base address\n", __func__);
919 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100920 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530921
922 pdata->phy_interface = -1;
923 priv->phyaddr = -1;
924 priv->use_internal_phy = false;
925
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100926 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100927 if (offset < 0) {
928 debug("%s: Cannot find PHY address\n", __func__);
929 return -EINVAL;
930 }
931 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530932
Simon Glassdd79d6e2017-01-17 16:52:55 -0700933 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530934
935 if (phy_mode)
936 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
937 printf("phy interface%d\n", pdata->phy_interface);
938
939 if (pdata->phy_interface == -1) {
940 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
941 return -EINVAL;
942 }
943
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530944 if (priv->variant == H3_EMAC) {
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530945 ret = sun8i_handle_internal_phy(dev, priv);
Jagan Teki727ed792019-02-28 00:27:00 +0530946 if (ret)
947 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530948 }
949
950 priv->interface = pdata->phy_interface;
951
952 if (!priv->use_internal_phy)
953 parse_phy_pins(dev);
954
Icenowy Zheng525dc442018-11-23 00:37:48 +0100955 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
956 "allwinner,tx-delay-ps", 0);
957 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
958 printf("%s: Invalid TX delay value %d\n", __func__,
959 sun8i_pdata->tx_delay_ps);
960
961 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
962 "allwinner,rx-delay-ps", 0);
963 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
964 printf("%s: Invalid RX delay value %d\n", __func__,
965 sun8i_pdata->rx_delay_ps);
966
Simon Glassfa4689a2019-12-06 21:41:35 -0700967#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass7a494432017-05-17 17:18:09 -0600968 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100969 "snps,reset-active-low"))
970 reset_flags |= GPIOD_ACTIVE_LOW;
971
972 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
973 &priv->reset_gpio, reset_flags);
974
975 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600976 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100977 "snps,reset-delays-us",
978 sun8i_pdata->reset_delays, 3);
979 } else if (ret == -ENOENT) {
980 ret = 0;
981 }
982#endif
983
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530984 return 0;
985}
986
987static const struct udevice_id sun8i_emac_eth_ids[] = {
988 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
989 {.compatible = "allwinner,sun50i-a64-emac",
990 .data = (uintptr_t)A64_EMAC },
991 {.compatible = "allwinner,sun8i-a83t-emac",
992 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene8cbced2018-07-13 10:45:28 +0200993 {.compatible = "allwinner,sun8i-r40-gmac",
994 .data = (uintptr_t)R40_GMAC },
Samuel Holland3386e9a2020-05-07 18:10:51 -0500995 {.compatible = "allwinner,sun50i-h6-emac",
996 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530997 { }
998};
999
1000U_BOOT_DRIVER(eth_sun8i_emac) = {
1001 .name = "eth_sun8i_emac",
1002 .id = UCLASS_ETH,
1003 .of_match = sun8i_emac_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001004 .of_to_plat = sun8i_emac_eth_of_to_plat,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301005 .probe = sun8i_emac_eth_probe,
1006 .ops = &sun8i_emac_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001007 .priv_auto = sizeof(struct emac_eth_dev),
Simon Glass71fa5b42020-12-03 16:55:18 -07001008 .plat_auto = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301009 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1010};