blob: 2344525bf523a8582f3e64a52ebd2c1a652ce5ec [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
Andre Przywara15651d82021-01-11 21:11:45 +0100300static u32 sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 reg)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530301{
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 */
Andre Przywara15651d82021-01-11 21:11:45 +0100306 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 return reg | H3_EPHY_SELECT;
311 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530312
Andre Przywara15651d82021-01-11 21:11:45 +0100313 /* This is to select External Gigabit PHY on those boards with
314 * an internal PHY. Does not hurt on other SoCs. Linux does
315 * it as well.
316 */
317 return reg & ~H3_EPHY_SELECT;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530318}
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{
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530323 u32 reg;
324
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530325 if (priv->variant == R40_GMAC) {
326 /* Select RGMII for R40 */
327 reg = readl(priv->sysctl_reg + 0x164);
Samuel Holland97f2cf12020-05-07 18:10:50 -0500328 reg |= SC_ETCS_INT_GMII |
329 SC_EPIT |
330 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530331
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530332 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200333 return 0;
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530334 }
335
336 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200337
Andre Przywara15651d82021-01-11 21:11:45 +0100338 reg = sun8i_emac_set_syscon_ephy(priv, reg);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530339
340 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500341 if (priv->variant == H3_EMAC ||
342 priv->variant == A64_EMAC ||
343 priv->variant == H6_EMAC)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530344 reg &= ~SC_RMII_EN;
345
346 switch (priv->interface) {
347 case PHY_INTERFACE_MODE_MII:
348 /* default */
349 break;
350 case PHY_INTERFACE_MODE_RGMII:
Andre Przywara43bb1582020-11-14 17:37:46 +0000351 case PHY_INTERFACE_MODE_RGMII_ID:
352 case PHY_INTERFACE_MODE_RGMII_RXID:
353 case PHY_INTERFACE_MODE_RGMII_TXID:
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530354 reg |= SC_EPIT | SC_ETCS_INT_GMII;
355 break;
356 case PHY_INTERFACE_MODE_RMII:
357 if (priv->variant == H3_EMAC ||
Samuel Holland3386e9a2020-05-07 18:10:51 -0500358 priv->variant == A64_EMAC ||
359 priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530360 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
361 break;
362 }
363 /* RMII not supported on A83T */
364 default:
365 debug("%s: Invalid PHY interface\n", __func__);
366 return -EINVAL;
367 }
368
Icenowy Zheng525dc442018-11-23 00:37:48 +0100369 if (pdata->tx_delay_ps)
370 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
371 & SC_ETXDC_MASK;
372
373 if (pdata->rx_delay_ps)
374 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
375 & SC_ERXDC_MASK;
376
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100377 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530378
379 return 0;
380}
381
382static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
383{
384 struct phy_device *phydev;
385
386 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
387 if (!phydev)
388 return -ENODEV;
389
390 phy_connect_dev(phydev, dev);
391
392 priv->phydev = phydev;
393 phy_config(priv->phydev);
394
395 return 0;
396}
397
Andre Przywara2e7dd262020-07-06 01:40:40 +0100398#define cache_clean_descriptor(desc) \
399 flush_dcache_range((uintptr_t)(desc), \
400 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
401
402#define cache_inv_descriptor(desc) \
403 invalidate_dcache_range((uintptr_t)(desc), \
404 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
405
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530406static void rx_descs_init(struct emac_eth_dev *priv)
407{
408 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
409 char *rxbuffs = &priv->rxbuffer[0];
410 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100411 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530412
Andre Przywara7408b092020-07-06 01:40:37 +0100413 /*
414 * Make sure we don't have dirty cache lines around, which could
415 * be cleaned to DRAM *after* the MAC has already written data to it.
416 */
417 invalidate_dcache_range((uintptr_t)desc_table_p,
418 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
419 invalidate_dcache_range((uintptr_t)rxbuffs,
420 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530421
Andre Przywara4ab675e2020-07-06 01:40:41 +0100422 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
423 desc_p = &desc_table_p[i];
424 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
425 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywara7408b092020-07-06 01:40:37 +0100426 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100427 desc_p->status = EMAC_DESC_OWN_DMA;
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
433 flush_dcache_range((uintptr_t)priv->rx_chain,
434 (uintptr_t)priv->rx_chain +
435 sizeof(priv->rx_chain));
436
437 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
438 priv->rx_currdescnum = 0;
439}
440
441static void tx_descs_init(struct emac_eth_dev *priv)
442{
443 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
444 char *txbuffs = &priv->txbuffer[0];
445 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100446 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530447
Andre Przywara4ab675e2020-07-06 01:40:41 +0100448 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
449 desc_p = &desc_table_p[i];
450 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
451 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100452 desc_p->ctl_size = 0;
Andre Przywaradf6f2712020-07-06 01:40:33 +0100453 desc_p->status = 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530454 }
455
456 /* Correcting the last pointer of the chain */
457 desc_p->next = (uintptr_t)&desc_table_p[0];
458
Andre Przywara8cd89602020-07-06 01:40:38 +0100459 /* Flush the first TX buffer descriptor we will tell the MAC about. */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100460 cache_clean_descriptor(desc_table_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530461
462 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
463 priv->tx_currdescnum = 0;
464}
465
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530466static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530467{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530468 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara874145f2020-07-06 01:40:32 +0100469 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530470
Andre Przywara6bdc70e2020-07-06 01:40:42 +0100471 /* Soft reset MAC */
472 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
473 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
474 EMAC_CTL1_SOFT_RST, false, 10, true);
475 if (ret) {
476 printf("%s: Timeout\n", __func__);
477 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530478 }
479
480 /* Rewrite mac address after reset */
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530481 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530482
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100483 /* transmission starts after the full frame arrived in TX DMA FIFO */
484 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530485
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100486 /*
487 * RX DMA reads data from RX DMA FIFO to host memory after a
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530488 * complete frame has been written to RX DMA FIFO
489 */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100490 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530491
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100492 /* DMA burst length */
493 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530494
495 /* Initialize rx/tx descriptors */
496 rx_descs_init(priv);
497 tx_descs_init(priv);
498
499 /* PHY Start Up */
Andre Przywara874145f2020-07-06 01:40:32 +0100500 ret = phy_startup(priv->phydev);
501 if (ret)
502 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530503
504 sun8i_adjust_link(priv, priv->phydev);
505
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100506 /* Start RX/TX DMA */
Andre Przywara59422822020-07-06 01:40:43 +0100507 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
508 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100509 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530510
511 /* Enable RX/TX */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100512 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
513 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530514
515 return 0;
516}
517
518static int parse_phy_pins(struct udevice *dev)
519{
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200520 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530521 int offset;
522 const char *pin_name;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100523 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530524
Simon Glassdd79d6e2017-01-17 16:52:55 -0700525 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530526 "pinctrl-0");
527 if (offset < 0) {
528 printf("WARNING: emac: cannot find pinctrl-0 node\n");
529 return offset;
530 }
531
532 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywara26e549b2018-04-04 01:31:15 +0100533 "drive-strength", ~0);
534 if (drive != ~0) {
535 if (drive <= 10)
536 drive = SUN4I_PINCTRL_10_MA;
537 else if (drive <= 20)
538 drive = SUN4I_PINCTRL_20_MA;
539 else if (drive <= 30)
540 drive = SUN4I_PINCTRL_30_MA;
541 else
542 drive = SUN4I_PINCTRL_40_MA;
Andre Przywara26e549b2018-04-04 01:31:15 +0100543 }
544
545 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
546 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywara26e549b2018-04-04 01:31:15 +0100547 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
548 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100549
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530550 for (i = 0; ; i++) {
551 int pin;
552
Simon Glassb0ea7402016-10-02 17:59:28 -0600553 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100554 "pins", i, NULL);
555 if (!pin_name)
556 break;
Andre Przywara26e549b2018-04-04 01:31:15 +0100557
558 pin = sunxi_name_to_gpio(pin_name);
559 if (pin < 0)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530560 continue;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530561
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200562 if (priv->variant == H3_EMAC)
563 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500564 else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC)
Lothar Feltene8cbced2018-07-13 10:45:28 +0200565 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200566 else
567 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
568
Andre Przywara26e549b2018-04-04 01:31:15 +0100569 if (drive != ~0)
570 sunxi_gpio_set_drv(pin, drive);
571 if (pull != ~0)
572 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530573 }
574
575 if (!i) {
Andre Przywara26e549b2018-04-04 01:31:15 +0100576 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530577 return -2;
578 }
579
580 return 0;
581}
582
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530583static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530584{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530585 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530586 u32 status, desc_num = priv->rx_currdescnum;
587 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Andre Przywara59422822020-07-06 01:40:43 +0100588 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
589 int length;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530590
591 /* Invalidate entire buffer descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100592 cache_inv_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530593
594 status = desc_p->status;
595
596 /* Check for DMA own bit */
Andre Przywara59422822020-07-06 01:40:43 +0100597 if (status & EMAC_DESC_OWN_DMA)
598 return -EAGAIN;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530599
Andre Przywara59422822020-07-06 01:40:43 +0100600 length = (status >> 16) & 0x3fff;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530601
Andre Przywara59422822020-07-06 01:40:43 +0100602 /* make sure we read from DRAM, not our cache */
603 invalidate_dcache_range(data_start,
604 data_start + roundup(length, ARCH_DMA_MINALIGN));
605
606 if (status & EMAC_DESC_RX_ERROR_MASK) {
607 debug("RX: packet error: 0x%x\n",
608 status & EMAC_DESC_RX_ERROR_MASK);
609 return 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530610 }
Andre Przywara59422822020-07-06 01:40:43 +0100611 if (length < 0x40) {
612 debug("RX: Bad Packet (runt)\n");
613 return 0;
614 }
615
616 if (length > CONFIG_ETH_RXSIZE) {
617 debug("RX: Too large packet (%d bytes)\n", length);
618 return 0;
619 }
620
621 *packetp = (uchar *)(ulong)desc_p->buf_addr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530622
623 return length;
624}
625
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530626static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530627{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530628 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100629 u32 desc_num = priv->tx_currdescnum;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530630 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530631 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
632 uintptr_t data_end = data_start +
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530633 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530634
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100635 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530636
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530637 memcpy((void *)data_start, packet, length);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530638
639 /* Flush data to be sent */
640 flush_dcache_range(data_start, data_end);
641
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100642 /* frame begin and end */
643 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
644 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530645
Andre Przywara2e7dd262020-07-06 01:40:40 +0100646 /* make sure the MAC reads the actual data from DRAM */
647 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530648
649 /* Move to next Descriptor and wrap around */
650 if (++desc_num >= CONFIG_TX_DESCR_NUM)
651 desc_num = 0;
652 priv->tx_currdescnum = desc_num;
653
654 /* Start the DMA */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100655 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
656
657 /*
658 * Since we copied the data above, we return here without waiting
659 * for the packet to be actually send out.
660 */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530661
662 return 0;
663}
664
Sean Anderson4702aa22020-09-15 10:45:00 -0400665static int sun8i_emac_board_setup(struct udevice *dev,
666 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530667{
Jagan Tekicb63d282019-02-28 00:26:58 +0530668 int ret;
669
670 ret = clk_enable(&priv->tx_clk);
671 if (ret) {
672 dev_err(dev, "failed to enable TX clock\n");
673 return ret;
674 }
675
676 if (reset_valid(&priv->tx_rst)) {
677 ret = reset_deassert(&priv->tx_rst);
678 if (ret) {
679 dev_err(dev, "failed to deassert TX reset\n");
680 goto err_tx_clk;
681 }
682 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530683
Jagan Teki727ed792019-02-28 00:27:00 +0530684 /* Only H3/H5 have clock controls for internal EPHY */
685 if (clk_valid(&priv->ephy_clk)) {
686 ret = clk_enable(&priv->ephy_clk);
687 if (ret) {
688 dev_err(dev, "failed to enable EPHY TX clock\n");
689 return ret;
690 }
691 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530692
Jagan Teki727ed792019-02-28 00:27:00 +0530693 if (reset_valid(&priv->ephy_rst)) {
694 ret = reset_deassert(&priv->ephy_rst);
695 if (ret) {
696 dev_err(dev, "failed to deassert EPHY TX clock\n");
697 return ret;
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200698 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530699 }
700
Jagan Tekicb63d282019-02-28 00:26:58 +0530701 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200702
Jagan Tekicb63d282019-02-28 00:26:58 +0530703err_tx_clk:
704 clk_disable(&priv->tx_clk);
705 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530706}
707
Simon Glassfa4689a2019-12-06 21:41:35 -0700708#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100709static int sun8i_mdio_reset(struct mii_dev *bus)
710{
711 struct udevice *dev = bus->priv;
712 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700713 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100714 int ret;
715
716 if (!dm_gpio_is_valid(&priv->reset_gpio))
717 return 0;
718
719 /* reset the phy */
720 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
721 if (ret)
722 return ret;
723
724 udelay(pdata->reset_delays[0]);
725
726 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
727 if (ret)
728 return ret;
729
730 udelay(pdata->reset_delays[1]);
731
732 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
733 if (ret)
734 return ret;
735
736 udelay(pdata->reset_delays[2]);
737
738 return 0;
739}
740#endif
741
742static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530743{
744 struct mii_dev *bus = mdio_alloc();
745
746 if (!bus) {
747 debug("Failed to allocate MDIO bus\n");
748 return -ENOMEM;
749 }
750
751 bus->read = sun8i_mdio_read;
752 bus->write = sun8i_mdio_write;
753 snprintf(bus->name, sizeof(bus->name), name);
754 bus->priv = (void *)priv;
Simon Glassfa4689a2019-12-06 21:41:35 -0700755#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100756 bus->reset = sun8i_mdio_reset;
757#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530758
759 return mdio_register(bus);
760}
761
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530762static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
763 int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530764{
765 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530766 u32 desc_num = priv->rx_currdescnum;
767 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530768
Andre Przywara2e7dd262020-07-06 01:40:40 +0100769 /* give the current descriptor back to the MAC */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100770 desc_p->status |= EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530771
772 /* Flush Status field of descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100773 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530774
775 /* Move to next desc and wrap-around condition. */
776 if (++desc_num >= CONFIG_RX_DESCR_NUM)
777 desc_num = 0;
778 priv->rx_currdescnum = desc_num;
779
780 return 0;
781}
782
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530783static void sun8i_emac_eth_stop(struct udevice *dev)
784{
785 struct emac_eth_dev *priv = dev_get_priv(dev);
786
787 /* Stop Rx/Tx transmitter */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100788 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
789 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530790
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100791 /* Stop RX/TX DMA */
792 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
793 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530794
795 phy_shutdown(priv->phydev);
796}
797
798static int sun8i_emac_eth_probe(struct udevice *dev)
799{
Simon Glassfa20e932020-12-03 16:55:20 -0700800 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Icenowy Zheng525dc442018-11-23 00:37:48 +0100801 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530802 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530803 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530804
805 priv->mac_reg = (void *)pdata->iobase;
806
Sean Anderson4702aa22020-09-15 10:45:00 -0400807 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekicb63d282019-02-28 00:26:58 +0530808 if (ret)
809 return ret;
810
Icenowy Zheng525dc442018-11-23 00:37:48 +0100811 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530812
Philipp Tomsich3297b552017-02-22 19:46:41 +0100813 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530814 priv->bus = miiphy_get_dev_by_name(dev->name);
815
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530816 return sun8i_phy_init(priv, dev);
817}
818
819static const struct eth_ops sun8i_emac_eth_ops = {
820 .start = sun8i_emac_eth_start,
821 .write_hwaddr = sun8i_eth_write_hwaddr,
822 .send = sun8i_emac_eth_send,
823 .recv = sun8i_emac_eth_recv,
824 .free_pkt = sun8i_eth_free_pkt,
825 .stop = sun8i_emac_eth_stop,
826};
827
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530828static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki727ed792019-02-28 00:27:00 +0530829{
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530830 struct ofnode_phandle_args phandle;
831 int ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530832
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530833 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
834 NULL, 0, 0, &phandle);
835 if (ret)
836 return ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530837
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530838 /* If the PHY node is not a child of the internal MDIO bus, we are
839 * using some external PHY.
840 */
841 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
842 "allwinner,sun8i-h3-mdio-internal"))
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200843 return 0;
844
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530845 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
Jagan Teki727ed792019-02-28 00:27:00 +0530846 if (ret) {
847 dev_err(dev, "failed to get EPHY TX clock\n");
848 return ret;
849 }
850
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530851 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
Jagan Teki727ed792019-02-28 00:27:00 +0530852 if (ret) {
853 dev_err(dev, "failed to get EPHY TX reset\n");
854 return ret;
855 }
856
857 priv->use_internal_phy = true;
858
859 return 0;
860}
861
Simon Glassaad29ae2020-12-03 16:55:21 -0700862static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530863{
Simon Glassfa20e932020-12-03 16:55:20 -0700864 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100865 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530866 struct emac_eth_dev *priv = dev_get_priv(dev);
867 const char *phy_mode;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100868 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700869 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530870 int offset = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -0700871#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100872 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100873#endif
Jagan Tekicb63d282019-02-28 00:26:58 +0530874 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530875
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900876 pdata->iobase = dev_read_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100877 if (pdata->iobase == FDT_ADDR_T_NONE) {
878 debug("%s: Cannot find MAC base address\n", __func__);
879 return -EINVAL;
880 }
881
Lothar Feltene8cbced2018-07-13 10:45:28 +0200882 priv->variant = dev_get_driver_data(dev);
883
884 if (!priv->variant) {
885 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100886 return -EINVAL;
887 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200888
Jagan Tekicb63d282019-02-28 00:26:58 +0530889 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
890 if (ret) {
891 dev_err(dev, "failed to get TX clock\n");
892 return ret;
893 }
894
895 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
896 if (ret && ret != -ENOENT) {
897 dev_err(dev, "failed to get TX reset\n");
898 return ret;
899 }
900
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530901 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
902 if (offset < 0) {
903 debug("%s: cannot find syscon node\n", __func__);
904 return -EINVAL;
905 }
906
907 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
908 if (!reg) {
909 debug("%s: cannot find reg property in syscon node\n",
910 __func__);
911 return -EINVAL;
912 }
913 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
914 offset, reg);
915 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
916 debug("%s: Cannot find syscon base address\n", __func__);
917 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100918 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530919
920 pdata->phy_interface = -1;
921 priv->phyaddr = -1;
922 priv->use_internal_phy = false;
923
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100924 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100925 if (offset < 0) {
926 debug("%s: Cannot find PHY address\n", __func__);
927 return -EINVAL;
928 }
929 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530930
Simon Glassdd79d6e2017-01-17 16:52:55 -0700931 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530932
933 if (phy_mode)
934 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
935 printf("phy interface%d\n", pdata->phy_interface);
936
937 if (pdata->phy_interface == -1) {
938 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
939 return -EINVAL;
940 }
941
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530942 if (priv->variant == H3_EMAC) {
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530943 ret = sun8i_handle_internal_phy(dev, priv);
Jagan Teki727ed792019-02-28 00:27:00 +0530944 if (ret)
945 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530946 }
947
948 priv->interface = pdata->phy_interface;
949
950 if (!priv->use_internal_phy)
951 parse_phy_pins(dev);
952
Icenowy Zheng525dc442018-11-23 00:37:48 +0100953 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
954 "allwinner,tx-delay-ps", 0);
955 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
956 printf("%s: Invalid TX delay value %d\n", __func__,
957 sun8i_pdata->tx_delay_ps);
958
959 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
960 "allwinner,rx-delay-ps", 0);
961 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
962 printf("%s: Invalid RX delay value %d\n", __func__,
963 sun8i_pdata->rx_delay_ps);
964
Simon Glassfa4689a2019-12-06 21:41:35 -0700965#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass7a494432017-05-17 17:18:09 -0600966 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100967 "snps,reset-active-low"))
968 reset_flags |= GPIOD_ACTIVE_LOW;
969
970 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
971 &priv->reset_gpio, reset_flags);
972
973 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600974 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100975 "snps,reset-delays-us",
976 sun8i_pdata->reset_delays, 3);
977 } else if (ret == -ENOENT) {
978 ret = 0;
979 }
980#endif
981
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530982 return 0;
983}
984
985static const struct udevice_id sun8i_emac_eth_ids[] = {
986 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
987 {.compatible = "allwinner,sun50i-a64-emac",
988 .data = (uintptr_t)A64_EMAC },
989 {.compatible = "allwinner,sun8i-a83t-emac",
990 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene8cbced2018-07-13 10:45:28 +0200991 {.compatible = "allwinner,sun8i-r40-gmac",
992 .data = (uintptr_t)R40_GMAC },
Samuel Holland3386e9a2020-05-07 18:10:51 -0500993 {.compatible = "allwinner,sun50i-h6-emac",
994 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530995 { }
996};
997
998U_BOOT_DRIVER(eth_sun8i_emac) = {
999 .name = "eth_sun8i_emac",
1000 .id = UCLASS_ETH,
1001 .of_match = sun8i_emac_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001002 .of_to_plat = sun8i_emac_eth_of_to_plat,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301003 .probe = sun8i_emac_eth_probe,
1004 .ops = &sun8i_emac_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001005 .priv_auto = sizeof(struct emac_eth_dev),
Simon Glass71fa5b42020-12-03 16:55:18 -07001006 .plat_auto = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301007 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1008};