blob: 2b39ad81b6612c071f0f8f4ccf9d442fe246cd0b [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Amit Singh Tomara29710c2016-07-06 17:59:44 +05302/*
3 * (C) Copyright 2016
4 * Author: Amit Singh Tomar, amittomer25@gmail.com
5 *
Amit Singh Tomara29710c2016-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 Glass1eb69ae2019-11-14 12:57:39 -070013#include <cpu_func.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass90526e92020-05-10 11:39:56 -060015#include <asm/cache.h>
Amit Singh Tomara29710c2016-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 Tekid3a2c052019-02-28 00:26:58 +053020#include <clk.h>
Amit Singh Tomara29710c2016-07-06 17:59:44 +053021#include <dm.h>
22#include <fdt_support.h>
Simon Glass336d4612020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060025#include <linux/delay.h>
Amit Singh Tomara29710c2016-07-06 17:59:44 +053026#include <linux/err.h>
27#include <malloc.h>
28#include <miiphy.h>
29#include <net.h>
Jagan Tekid3a2c052019-02-28 00:26:58 +053030#include <reset.h>
Andre Przywarac0341172018-04-04 01:31:15 +010031#include <dt-bindings/pinctrl/sun4i-a10.h>
Andre Przywaraf20f9462020-07-06 01:40:34 +010032#include <wait_bit.h>
Simon Glassbcee8d62019-12-06 21:41:35 -070033#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +010034#include <asm-generic/gpio.h>
35#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +053036
Amit Singh Tomara29710c2016-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
44
45#define CONFIG_TX_DESCR_NUM 32
46#define CONFIG_RX_DESCR_NUM 32
Hans de Goede40694372016-07-27 17:31:17 +020047#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
48
49/*
50 * The datasheet says that each descriptor can transfers up to 4096 bytes
51 * But later, the register documentation reduces that value to 2048,
52 * using 2048 cause strange behaviours and even BSP driver use 2047
53 */
54#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomara29710c2016-07-06 17:59:44 +053055
56#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
57#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
58
59#define H3_EPHY_DEFAULT_VALUE 0x58000
60#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
61#define H3_EPHY_ADDR_SHIFT 20
62#define REG_PHY_ADDR_MASK GENMASK(4, 0)
63#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
64#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
65#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
66
67#define SC_RMII_EN BIT(13)
68#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
69#define SC_ETCS_MASK GENMASK(1, 0)
70#define SC_ETCS_EXT_GMII 0x1
71#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng9b16ede2018-11-23 00:37:48 +010072#define SC_ETXDC_MASK GENMASK(12, 10)
73#define SC_ETXDC_OFFSET 10
74#define SC_ERXDC_MASK GENMASK(9, 5)
75#define SC_ERXDC_OFFSET 5
Amit Singh Tomara29710c2016-07-06 17:59:44 +053076
77#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
78
79#define AHB_GATE_OFFSET_EPHY 0
80
Lothar Feltenc6a21d62018-07-13 10:45:27 +020081/* IO mux settings */
82#define SUN8I_IOMUX_H3 2
Lothar Feltene46d73f2018-07-13 10:45:28 +020083#define SUN8I_IOMUX_R40 5
Lothar Feltenc6a21d62018-07-13 10:45:27 +020084#define SUN8I_IOMUX 4
Amit Singh Tomara29710c2016-07-06 17:59:44 +053085
86/* H3/A64 EMAC Register's offset */
87#define EMAC_CTL0 0x00
Andre Przywara4fe86412020-07-06 01:40:36 +010088#define EMAC_CTL0_FULL_DUPLEX BIT(0)
89#define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
90#define EMAC_CTL0_SPEED_10 (0x2 << 2)
91#define EMAC_CTL0_SPEED_100 (0x3 << 2)
92#define EMAC_CTL0_SPEED_1000 (0x0 << 2)
Amit Singh Tomara29710c2016-07-06 17:59:44 +053093#define EMAC_CTL1 0x04
Andre Przywara4fe86412020-07-06 01:40:36 +010094#define EMAC_CTL1_SOFT_RST BIT(0)
95#define EMAC_CTL1_BURST_LEN_SHIFT 24
Amit Singh Tomara29710c2016-07-06 17:59:44 +053096#define EMAC_INT_STA 0x08
97#define EMAC_INT_EN 0x0c
98#define EMAC_TX_CTL0 0x10
Andre Przywara4fe86412020-07-06 01:40:36 +010099#define EMAC_TX_CTL0_TX_EN BIT(31)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530100#define EMAC_TX_CTL1 0x14
Andre Przywara4fe86412020-07-06 01:40:36 +0100101#define EMAC_TX_CTL1_TX_MD BIT(1)
102#define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
103#define EMAC_TX_CTL1_TX_DMA_START BIT(31)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530104#define EMAC_TX_FLOW_CTL 0x1c
105#define EMAC_TX_DMA_DESC 0x20
106#define EMAC_RX_CTL0 0x24
Andre Przywara4fe86412020-07-06 01:40:36 +0100107#define EMAC_RX_CTL0_RX_EN BIT(31)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530108#define EMAC_RX_CTL1 0x28
Andre Przywara4fe86412020-07-06 01:40:36 +0100109#define EMAC_RX_CTL1_RX_MD BIT(1)
Andre Przywara7edcb4e2020-07-06 01:40:43 +0100110#define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
111#define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
Andre Przywara4fe86412020-07-06 01:40:36 +0100112#define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
113#define EMAC_RX_CTL1_RX_DMA_START BIT(31)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530114#define EMAC_RX_DMA_DESC 0x34
115#define EMAC_MII_CMD 0x48
116#define EMAC_MII_DATA 0x4c
117#define EMAC_ADDR0_HIGH 0x50
118#define EMAC_ADDR0_LOW 0x54
119#define EMAC_TX_DMA_STA 0xb0
120#define EMAC_TX_CUR_DESC 0xb4
121#define EMAC_TX_CUR_BUF 0xb8
122#define EMAC_RX_DMA_STA 0xc0
123#define EMAC_RX_CUR_DESC 0xc4
124
Andre Przywara4fe86412020-07-06 01:40:36 +0100125#define EMAC_DESC_OWN_DMA BIT(31)
126#define EMAC_DESC_LAST_DESC BIT(30)
127#define EMAC_DESC_FIRST_DESC BIT(29)
128#define EMAC_DESC_CHAIN_SECOND BIT(24)
129
Andre Przywara7edcb4e2020-07-06 01:40:43 +0100130#define EMAC_DESC_RX_ERROR_MASK 0x400068db
131
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530132DECLARE_GLOBAL_DATA_PTR;
133
134enum emac_variant {
135 A83T_EMAC = 1,
136 H3_EMAC,
137 A64_EMAC,
Lothar Feltene46d73f2018-07-13 10:45:28 +0200138 R40_GMAC,
Samuel Holland99ac8612020-05-07 18:10:51 -0500139 H6_EMAC,
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530140};
141
142struct emac_dma_desc {
143 u32 status;
Andre Przywara4fe86412020-07-06 01:40:36 +0100144 u32 ctl_size;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530145 u32 buf_addr;
146 u32 next;
147} __aligned(ARCH_DMA_MINALIGN);
148
149struct emac_eth_dev {
150 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
151 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
152 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
153 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
154
155 u32 interface;
156 u32 phyaddr;
157 u32 link;
158 u32 speed;
159 u32 duplex;
160 u32 phy_configured;
161 u32 tx_currdescnum;
162 u32 rx_currdescnum;
163 u32 addr;
164 u32 tx_slot;
165 bool use_internal_phy;
166
167 enum emac_variant variant;
168 void *mac_reg;
169 phys_addr_t sysctl_reg;
170 struct phy_device *phydev;
171 struct mii_dev *bus;
Jagan Tekid3a2c052019-02-28 00:26:58 +0530172 struct clk tx_clk;
Jagan Teki23484532019-02-28 00:27:00 +0530173 struct clk ephy_clk;
Jagan Tekid3a2c052019-02-28 00:26:58 +0530174 struct reset_ctl tx_rst;
Jagan Teki23484532019-02-28 00:27:00 +0530175 struct reset_ctl ephy_rst;
Simon Glassbcee8d62019-12-06 21:41:35 -0700176#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100177 struct gpio_desc reset_gpio;
178#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530179};
180
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100181
182struct sun8i_eth_pdata {
183 struct eth_pdata eth_pdata;
184 u32 reset_delays[3];
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100185 int tx_delay_ps;
186 int rx_delay_ps;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100187};
188
189
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530190static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
191{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100192 struct udevice *dev = bus->priv;
193 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywaraf20f9462020-07-06 01:40:34 +0100194 u32 mii_cmd;
195 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530196
Andre Przywaraf20f9462020-07-06 01:40:34 +0100197 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530198 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywaraf20f9462020-07-06 01:40:34 +0100199 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530200 MDIO_CMD_MII_PHY_ADDR_MASK;
201
Andre Przywaraf20f9462020-07-06 01:40:34 +0100202 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530203
Andre Przywaraf20f9462020-07-06 01:40:34 +0100204 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530205
Andre Przywaraf20f9462020-07-06 01:40:34 +0100206 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
207 MDIO_CMD_MII_BUSY, false,
208 CONFIG_MDIO_TIMEOUT, true);
209 if (ret < 0)
210 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530211
Andre Przywaraf20f9462020-07-06 01:40:34 +0100212 return readl(priv->mac_reg + EMAC_MII_DATA);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530213}
214
215static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
216 u16 val)
217{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100218 struct udevice *dev = bus->priv;
219 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywaraf20f9462020-07-06 01:40:34 +0100220 u32 mii_cmd;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530221
Andre Przywaraf20f9462020-07-06 01:40:34 +0100222 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530223 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywaraf20f9462020-07-06 01:40:34 +0100224 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530225 MDIO_CMD_MII_PHY_ADDR_MASK;
226
Andre Przywaraf20f9462020-07-06 01:40:34 +0100227 mii_cmd |= MDIO_CMD_MII_WRITE;
228 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530229
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530230 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywaraf20f9462020-07-06 01:40:34 +0100231 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530232
Andre Przywaraf20f9462020-07-06 01:40:34 +0100233 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
234 MDIO_CMD_MII_BUSY, false,
235 CONFIG_MDIO_TIMEOUT, true);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530236}
237
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530238static int sun8i_eth_write_hwaddr(struct udevice *dev)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530239{
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530240 struct emac_eth_dev *priv = dev_get_priv(dev);
241 struct eth_pdata *pdata = dev_get_platdata(dev);
242 uchar *mac_id = pdata->enetaddr;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530243 u32 macid_lo, macid_hi;
244
245 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
246 (mac_id[3] << 24);
247 macid_hi = mac_id[4] + (mac_id[5] << 8);
248
249 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
250 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
251
252 return 0;
253}
254
255static void sun8i_adjust_link(struct emac_eth_dev *priv,
256 struct phy_device *phydev)
257{
258 u32 v;
259
260 v = readl(priv->mac_reg + EMAC_CTL0);
261
262 if (phydev->duplex)
Andre Przywara4fe86412020-07-06 01:40:36 +0100263 v |= EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530264 else
Andre Przywara4fe86412020-07-06 01:40:36 +0100265 v &= ~EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530266
Andre Przywara4fe86412020-07-06 01:40:36 +0100267 v &= ~EMAC_CTL0_SPEED_MASK;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530268
269 switch (phydev->speed) {
270 case 1000:
Andre Przywara4fe86412020-07-06 01:40:36 +0100271 v |= EMAC_CTL0_SPEED_1000;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530272 break;
273 case 100:
Andre Przywara4fe86412020-07-06 01:40:36 +0100274 v |= EMAC_CTL0_SPEED_100;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530275 break;
276 case 10:
Andre Przywara4fe86412020-07-06 01:40:36 +0100277 v |= EMAC_CTL0_SPEED_10;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530278 break;
279 }
280 writel(v, priv->mac_reg + EMAC_CTL0);
281}
282
283static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
284{
285 if (priv->use_internal_phy) {
286 /* H3 based SoC's that has an Internal 100MBit PHY
287 * needs to be configured and powered up before use
288 */
289 *reg &= ~H3_EPHY_DEFAULT_MASK;
290 *reg |= H3_EPHY_DEFAULT_VALUE;
291 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
292 *reg &= ~H3_EPHY_SHUTDOWN;
293 *reg |= H3_EPHY_SELECT;
294 } else
295 /* This is to select External Gigabit PHY on
296 * the boards with H3 SoC.
297 */
298 *reg &= ~H3_EPHY_SELECT;
299
300 return 0;
301}
302
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100303static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
304 struct emac_eth_dev *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530305{
306 int ret;
307 u32 reg;
308
Jagan Teki695f6042019-02-28 00:26:51 +0530309 if (priv->variant == R40_GMAC) {
310 /* Select RGMII for R40 */
311 reg = readl(priv->sysctl_reg + 0x164);
Samuel Hollandabdbefb2020-05-07 18:10:50 -0500312 reg |= SC_ETCS_INT_GMII |
313 SC_EPIT |
314 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530315
Jagan Teki695f6042019-02-28 00:26:51 +0530316 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200317 return 0;
Jagan Teki695f6042019-02-28 00:26:51 +0530318 }
319
320 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200321
Samuel Holland99ac8612020-05-07 18:10:51 -0500322 if (priv->variant == H3_EMAC || priv->variant == H6_EMAC) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530323 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
324 if (ret)
325 return ret;
326 }
327
328 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland99ac8612020-05-07 18:10:51 -0500329 if (priv->variant == H3_EMAC ||
330 priv->variant == A64_EMAC ||
331 priv->variant == H6_EMAC)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530332 reg &= ~SC_RMII_EN;
333
334 switch (priv->interface) {
335 case PHY_INTERFACE_MODE_MII:
336 /* default */
337 break;
338 case PHY_INTERFACE_MODE_RGMII:
339 reg |= SC_EPIT | SC_ETCS_INT_GMII;
340 break;
341 case PHY_INTERFACE_MODE_RMII:
342 if (priv->variant == H3_EMAC ||
Samuel Holland99ac8612020-05-07 18:10:51 -0500343 priv->variant == A64_EMAC ||
344 priv->variant == H6_EMAC) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530345 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
346 break;
347 }
348 /* RMII not supported on A83T */
349 default:
350 debug("%s: Invalid PHY interface\n", __func__);
351 return -EINVAL;
352 }
353
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100354 if (pdata->tx_delay_ps)
355 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
356 & SC_ETXDC_MASK;
357
358 if (pdata->rx_delay_ps)
359 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
360 & SC_ERXDC_MASK;
361
Andre Przywara12afd952018-04-04 01:31:16 +0100362 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530363
364 return 0;
365}
366
367static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
368{
369 struct phy_device *phydev;
370
371 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
372 if (!phydev)
373 return -ENODEV;
374
375 phy_connect_dev(phydev, dev);
376
377 priv->phydev = phydev;
378 phy_config(priv->phydev);
379
380 return 0;
381}
382
Andre Przywara8c274ec2020-07-06 01:40:40 +0100383#define cache_clean_descriptor(desc) \
384 flush_dcache_range((uintptr_t)(desc), \
385 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
386
387#define cache_inv_descriptor(desc) \
388 invalidate_dcache_range((uintptr_t)(desc), \
389 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
390
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530391static void rx_descs_init(struct emac_eth_dev *priv)
392{
393 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
394 char *rxbuffs = &priv->rxbuffer[0];
395 struct emac_dma_desc *desc_p;
Andre Przywara09501ff2020-07-06 01:40:41 +0100396 int i;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530397
Andre Przywara69853122020-07-06 01:40:37 +0100398 /*
399 * Make sure we don't have dirty cache lines around, which could
400 * be cleaned to DRAM *after* the MAC has already written data to it.
401 */
402 invalidate_dcache_range((uintptr_t)desc_table_p,
403 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
404 invalidate_dcache_range((uintptr_t)rxbuffs,
405 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530406
Andre Przywara09501ff2020-07-06 01:40:41 +0100407 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
408 desc_p = &desc_table_p[i];
409 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
410 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywara69853122020-07-06 01:40:37 +0100411 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
Andre Przywara4fe86412020-07-06 01:40:36 +0100412 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530413 }
414
415 /* Correcting the last pointer of the chain */
416 desc_p->next = (uintptr_t)&desc_table_p[0];
417
418 flush_dcache_range((uintptr_t)priv->rx_chain,
419 (uintptr_t)priv->rx_chain +
420 sizeof(priv->rx_chain));
421
422 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
423 priv->rx_currdescnum = 0;
424}
425
426static void tx_descs_init(struct emac_eth_dev *priv)
427{
428 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
429 char *txbuffs = &priv->txbuffer[0];
430 struct emac_dma_desc *desc_p;
Andre Przywara09501ff2020-07-06 01:40:41 +0100431 int i;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530432
Andre Przywara09501ff2020-07-06 01:40:41 +0100433 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
434 desc_p = &desc_table_p[i];
435 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
436 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywara4fe86412020-07-06 01:40:36 +0100437 desc_p->ctl_size = 0;
Andre Przywarac35380c2020-07-06 01:40:33 +0100438 desc_p->status = 0;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530439 }
440
441 /* Correcting the last pointer of the chain */
442 desc_p->next = (uintptr_t)&desc_table_p[0];
443
Andre Przywaraed909de2020-07-06 01:40:38 +0100444 /* Flush the first TX buffer descriptor we will tell the MAC about. */
Andre Przywara8c274ec2020-07-06 01:40:40 +0100445 cache_clean_descriptor(desc_table_p);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530446
447 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
448 priv->tx_currdescnum = 0;
449}
450
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530451static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530452{
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530453 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara2808cf62020-07-06 01:40:32 +0100454 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530455
Andre Przywara2c5600c2020-07-06 01:40:42 +0100456 /* Soft reset MAC */
457 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
458 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
459 EMAC_CTL1_SOFT_RST, false, 10, true);
460 if (ret) {
461 printf("%s: Timeout\n", __func__);
462 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530463 }
464
465 /* Rewrite mac address after reset */
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530466 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530467
Andre Przywara4fe86412020-07-06 01:40:36 +0100468 /* transmission starts after the full frame arrived in TX DMA FIFO */
469 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530470
Andre Przywara4fe86412020-07-06 01:40:36 +0100471 /*
472 * RX DMA reads data from RX DMA FIFO to host memory after a
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530473 * complete frame has been written to RX DMA FIFO
474 */
Andre Przywara4fe86412020-07-06 01:40:36 +0100475 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530476
Andre Przywara4fe86412020-07-06 01:40:36 +0100477 /* DMA burst length */
478 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530479
480 /* Initialize rx/tx descriptors */
481 rx_descs_init(priv);
482 tx_descs_init(priv);
483
484 /* PHY Start Up */
Andre Przywara2808cf62020-07-06 01:40:32 +0100485 ret = phy_startup(priv->phydev);
486 if (ret)
487 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530488
489 sun8i_adjust_link(priv, priv->phydev);
490
Andre Przywara4fe86412020-07-06 01:40:36 +0100491 /* Start RX/TX DMA */
Andre Przywara7edcb4e2020-07-06 01:40:43 +0100492 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
493 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
Andre Przywara4fe86412020-07-06 01:40:36 +0100494 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530495
496 /* Enable RX/TX */
Andre Przywara4fe86412020-07-06 01:40:36 +0100497 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
498 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530499
500 return 0;
501}
502
503static int parse_phy_pins(struct udevice *dev)
504{
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200505 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530506 int offset;
507 const char *pin_name;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100508 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530509
Simon Glasse160f7d2017-01-17 16:52:55 -0700510 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530511 "pinctrl-0");
512 if (offset < 0) {
513 printf("WARNING: emac: cannot find pinctrl-0 node\n");
514 return offset;
515 }
516
517 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywarac0341172018-04-04 01:31:15 +0100518 "drive-strength", ~0);
519 if (drive != ~0) {
520 if (drive <= 10)
521 drive = SUN4I_PINCTRL_10_MA;
522 else if (drive <= 20)
523 drive = SUN4I_PINCTRL_20_MA;
524 else if (drive <= 30)
525 drive = SUN4I_PINCTRL_30_MA;
526 else
527 drive = SUN4I_PINCTRL_40_MA;
Andre Przywarac0341172018-04-04 01:31:15 +0100528 }
529
530 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
531 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywarac0341172018-04-04 01:31:15 +0100532 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
533 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100534
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530535 for (i = 0; ; i++) {
536 int pin;
537
Simon Glassb02e4042016-10-02 17:59:28 -0600538 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100539 "pins", i, NULL);
540 if (!pin_name)
541 break;
Andre Przywarac0341172018-04-04 01:31:15 +0100542
543 pin = sunxi_name_to_gpio(pin_name);
544 if (pin < 0)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530545 continue;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530546
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200547 if (priv->variant == H3_EMAC)
548 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Samuel Holland99ac8612020-05-07 18:10:51 -0500549 else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC)
Lothar Feltene46d73f2018-07-13 10:45:28 +0200550 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200551 else
552 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
553
Andre Przywarac0341172018-04-04 01:31:15 +0100554 if (drive != ~0)
555 sunxi_gpio_set_drv(pin, drive);
556 if (pull != ~0)
557 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530558 }
559
560 if (!i) {
Andre Przywarac0341172018-04-04 01:31:15 +0100561 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530562 return -2;
563 }
564
565 return 0;
566}
567
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530568static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530569{
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530570 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530571 u32 status, desc_num = priv->rx_currdescnum;
572 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Andre Przywara7edcb4e2020-07-06 01:40:43 +0100573 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
574 int length;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530575
576 /* Invalidate entire buffer descriptor */
Andre Przywara8c274ec2020-07-06 01:40:40 +0100577 cache_inv_descriptor(desc_p);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530578
579 status = desc_p->status;
580
581 /* Check for DMA own bit */
Andre Przywara7edcb4e2020-07-06 01:40:43 +0100582 if (status & EMAC_DESC_OWN_DMA)
583 return -EAGAIN;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530584
Andre Przywara7edcb4e2020-07-06 01:40:43 +0100585 length = (status >> 16) & 0x3fff;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530586
Andre Przywara7edcb4e2020-07-06 01:40:43 +0100587 /* make sure we read from DRAM, not our cache */
588 invalidate_dcache_range(data_start,
589 data_start + roundup(length, ARCH_DMA_MINALIGN));
590
591 if (status & EMAC_DESC_RX_ERROR_MASK) {
592 debug("RX: packet error: 0x%x\n",
593 status & EMAC_DESC_RX_ERROR_MASK);
594 return 0;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530595 }
Andre Przywara7edcb4e2020-07-06 01:40:43 +0100596 if (length < 0x40) {
597 debug("RX: Bad Packet (runt)\n");
598 return 0;
599 }
600
601 if (length > CONFIG_ETH_RXSIZE) {
602 debug("RX: Too large packet (%d bytes)\n", length);
603 return 0;
604 }
605
606 *packetp = (uchar *)(ulong)desc_p->buf_addr;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530607
608 return length;
609}
610
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530611static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530612{
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530613 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara4fe86412020-07-06 01:40:36 +0100614 u32 desc_num = priv->tx_currdescnum;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530615 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530616 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
617 uintptr_t data_end = data_start +
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530618 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530619
Andre Przywara4fe86412020-07-06 01:40:36 +0100620 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530621
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530622 memcpy((void *)data_start, packet, length);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530623
624 /* Flush data to be sent */
625 flush_dcache_range(data_start, data_end);
626
Andre Przywara4fe86412020-07-06 01:40:36 +0100627 /* frame begin and end */
628 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
629 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530630
Andre Przywara8c274ec2020-07-06 01:40:40 +0100631 /* make sure the MAC reads the actual data from DRAM */
632 cache_clean_descriptor(desc_p);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530633
634 /* Move to next Descriptor and wrap around */
635 if (++desc_num >= CONFIG_TX_DESCR_NUM)
636 desc_num = 0;
637 priv->tx_currdescnum = desc_num;
638
639 /* Start the DMA */
Andre Przywara4fe86412020-07-06 01:40:36 +0100640 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
641
642 /*
643 * Since we copied the data above, we return here without waiting
644 * for the packet to be actually send out.
645 */
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530646
647 return 0;
648}
649
Sean Andersonef043692020-09-15 10:45:00 -0400650static int sun8i_emac_board_setup(struct udevice *dev,
651 struct emac_eth_dev *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530652{
Jagan Tekid3a2c052019-02-28 00:26:58 +0530653 int ret;
654
655 ret = clk_enable(&priv->tx_clk);
656 if (ret) {
657 dev_err(dev, "failed to enable TX clock\n");
658 return ret;
659 }
660
661 if (reset_valid(&priv->tx_rst)) {
662 ret = reset_deassert(&priv->tx_rst);
663 if (ret) {
664 dev_err(dev, "failed to deassert TX reset\n");
665 goto err_tx_clk;
666 }
667 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530668
Jagan Teki23484532019-02-28 00:27:00 +0530669 /* Only H3/H5 have clock controls for internal EPHY */
670 if (clk_valid(&priv->ephy_clk)) {
671 ret = clk_enable(&priv->ephy_clk);
672 if (ret) {
673 dev_err(dev, "failed to enable EPHY TX clock\n");
674 return ret;
675 }
676 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530677
Jagan Teki23484532019-02-28 00:27:00 +0530678 if (reset_valid(&priv->ephy_rst)) {
679 ret = reset_deassert(&priv->ephy_rst);
680 if (ret) {
681 dev_err(dev, "failed to deassert EPHY TX clock\n");
682 return ret;
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200683 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530684 }
685
Jagan Tekid3a2c052019-02-28 00:26:58 +0530686 return 0;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530687
Jagan Tekid3a2c052019-02-28 00:26:58 +0530688err_tx_clk:
689 clk_disable(&priv->tx_clk);
690 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530691}
692
Simon Glassbcee8d62019-12-06 21:41:35 -0700693#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100694static int sun8i_mdio_reset(struct mii_dev *bus)
695{
696 struct udevice *dev = bus->priv;
697 struct emac_eth_dev *priv = dev_get_priv(dev);
698 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
699 int ret;
700
701 if (!dm_gpio_is_valid(&priv->reset_gpio))
702 return 0;
703
704 /* reset the phy */
705 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
706 if (ret)
707 return ret;
708
709 udelay(pdata->reset_delays[0]);
710
711 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
712 if (ret)
713 return ret;
714
715 udelay(pdata->reset_delays[1]);
716
717 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
718 if (ret)
719 return ret;
720
721 udelay(pdata->reset_delays[2]);
722
723 return 0;
724}
725#endif
726
727static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530728{
729 struct mii_dev *bus = mdio_alloc();
730
731 if (!bus) {
732 debug("Failed to allocate MDIO bus\n");
733 return -ENOMEM;
734 }
735
736 bus->read = sun8i_mdio_read;
737 bus->write = sun8i_mdio_write;
738 snprintf(bus->name, sizeof(bus->name), name);
739 bus->priv = (void *)priv;
Simon Glassbcee8d62019-12-06 21:41:35 -0700740#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100741 bus->reset = sun8i_mdio_reset;
742#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530743
744 return mdio_register(bus);
745}
746
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530747static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
748 int length)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530749{
750 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530751 u32 desc_num = priv->rx_currdescnum;
752 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530753
Andre Przywara8c274ec2020-07-06 01:40:40 +0100754 /* give the current descriptor back to the MAC */
Andre Przywara4fe86412020-07-06 01:40:36 +0100755 desc_p->status |= EMAC_DESC_OWN_DMA;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530756
757 /* Flush Status field of descriptor */
Andre Przywara8c274ec2020-07-06 01:40:40 +0100758 cache_clean_descriptor(desc_p);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530759
760 /* Move to next desc and wrap-around condition. */
761 if (++desc_num >= CONFIG_RX_DESCR_NUM)
762 desc_num = 0;
763 priv->rx_currdescnum = desc_num;
764
765 return 0;
766}
767
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530768static void sun8i_emac_eth_stop(struct udevice *dev)
769{
770 struct emac_eth_dev *priv = dev_get_priv(dev);
771
772 /* Stop Rx/Tx transmitter */
Andre Przywara4fe86412020-07-06 01:40:36 +0100773 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
774 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530775
Andre Przywara4fe86412020-07-06 01:40:36 +0100776 /* Stop RX/TX DMA */
777 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
778 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530779
780 phy_shutdown(priv->phydev);
781}
782
783static int sun8i_emac_eth_probe(struct udevice *dev)
784{
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100785 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
786 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530787 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekid3a2c052019-02-28 00:26:58 +0530788 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530789
790 priv->mac_reg = (void *)pdata->iobase;
791
Sean Andersonef043692020-09-15 10:45:00 -0400792 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekid3a2c052019-02-28 00:26:58 +0530793 if (ret)
794 return ret;
795
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100796 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530797
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100798 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530799 priv->bus = miiphy_get_dev_by_name(dev->name);
800
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530801 return sun8i_phy_init(priv, dev);
802}
803
804static const struct eth_ops sun8i_emac_eth_ops = {
805 .start = sun8i_emac_eth_start,
806 .write_hwaddr = sun8i_eth_write_hwaddr,
807 .send = sun8i_emac_eth_send,
808 .recv = sun8i_emac_eth_recv,
809 .free_pkt = sun8i_eth_free_pkt,
810 .stop = sun8i_emac_eth_stop,
811};
812
Sean Andersonef043692020-09-15 10:45:00 -0400813static int sun8i_get_ephy_nodes(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki23484532019-02-28 00:27:00 +0530814{
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200815 int emac_node, ephy_node, ret, ephy_handle;
816
817 emac_node = fdt_path_offset(gd->fdt_blob,
818 "/soc/ethernet@1c30000");
819 if (emac_node < 0) {
820 debug("failed to get emac node\n");
821 return emac_node;
822 }
823 ephy_handle = fdtdec_lookup_phandle(gd->fdt_blob,
824 emac_node, "phy-handle");
Jagan Teki23484532019-02-28 00:27:00 +0530825
826 /* look for mdio-mux node for internal PHY node */
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200827 ephy_node = fdt_path_offset(gd->fdt_blob,
828 "/soc/ethernet@1c30000/mdio-mux/mdio@1/ethernet-phy@1");
829 if (ephy_node < 0) {
Jagan Teki23484532019-02-28 00:27:00 +0530830 debug("failed to get mdio-mux with internal PHY\n");
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200831 return ephy_node;
Jagan Teki23484532019-02-28 00:27:00 +0530832 }
833
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200834 /* This is not the phy we are looking for */
835 if (ephy_node != ephy_handle)
836 return 0;
837
838 ret = fdt_node_check_compatible(gd->fdt_blob, ephy_node,
Jagan Teki23484532019-02-28 00:27:00 +0530839 "allwinner,sun8i-h3-mdio-internal");
840 if (ret < 0) {
841 debug("failed to find mdio-internal node\n");
842 return ret;
843 }
844
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200845 ret = clk_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki23484532019-02-28 00:27:00 +0530846 &priv->ephy_clk);
847 if (ret) {
848 dev_err(dev, "failed to get EPHY TX clock\n");
849 return ret;
850 }
851
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200852 ret = reset_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki23484532019-02-28 00:27:00 +0530853 &priv->ephy_rst);
854 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
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530864static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
865{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100866 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
867 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530868 struct emac_eth_dev *priv = dev_get_priv(dev);
869 const char *phy_mode;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100870 const fdt32_t *reg;
Simon Glasse160f7d2017-01-17 16:52:55 -0700871 int node = dev_of_offset(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530872 int offset = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -0700873#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100874 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100875#endif
Jagan Tekid3a2c052019-02-28 00:26:58 +0530876 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530877
Masahiro Yamada25484932020-07-17 14:36:48 +0900878 pdata->iobase = dev_read_addr(dev);
Andre Przywara12afd952018-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 Feltene46d73f2018-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 Przywaraecd0cec2018-04-04 01:31:20 +0100888 return -EINVAL;
Andre Przywara12afd952018-04-04 01:31:16 +0100889 }
Lothar Feltene46d73f2018-07-13 10:45:28 +0200890
Jagan Tekid3a2c052019-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 Teki695f6042019-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 Przywara12afd952018-04-04 01:31:16 +0100920 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530921
922 pdata->phy_interface = -1;
923 priv->phyaddr = -1;
924 priv->use_internal_phy = false;
925
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100926 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywara12afd952018-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 Tomara29710c2016-07-06 17:59:44 +0530932
Simon Glasse160f7d2017-01-17 16:52:55 -0700933 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomara29710c2016-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 Tomara29710c2016-07-06 17:59:44 +0530944 if (priv->variant == H3_EMAC) {
Sean Andersonef043692020-09-15 10:45:00 -0400945 ret = sun8i_get_ephy_nodes(dev, priv);
Jagan Teki23484532019-02-28 00:27:00 +0530946 if (ret)
947 return ret;
Amit Singh Tomara29710c2016-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 Zheng9b16ede2018-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 Glassbcee8d62019-12-06 21:41:35 -0700967#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glassda409cc2017-05-17 17:18:09 -0600968 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-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 Glassda409cc2017-05-17 17:18:09 -0600976 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-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 Tomara29710c2016-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 Feltene46d73f2018-07-13 10:45:28 +0200993 {.compatible = "allwinner,sun8i-r40-gmac",
994 .data = (uintptr_t)R40_GMAC },
Samuel Holland99ac8612020-05-07 18:10:51 -0500995 {.compatible = "allwinner,sun50i-h6-emac",
996 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomara29710c2016-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,
1004 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
1005 .probe = sun8i_emac_eth_probe,
1006 .ops = &sun8i_emac_eth_ops,
1007 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +01001008 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomara29710c2016-07-06 17:59:44 +05301009 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1010};