blob: c567b9e0827417ae3ae2c45889cb4306283a562d [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
44
45#define CONFIG_TX_DESCR_NUM 32
46#define CONFIG_RX_DESCR_NUM 32
Hans de Goedefcdb3b32016-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 Tomard194c0e2016-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 Zheng525dc442018-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 Tomard194c0e2016-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 Feltenacb9a5b2018-07-13 10:45:27 +020081/* IO mux settings */
82#define SUN8I_IOMUX_H3 2
Lothar Feltene8cbced2018-07-13 10:45:28 +020083#define SUN8I_IOMUX_R40 5
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020084#define SUN8I_IOMUX 4
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053085
86/* H3/A64 EMAC Register's offset */
87#define EMAC_CTL0 0x00
Andre Przywarae6e29cc2020-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 Tomard194c0e2016-07-06 17:59:44 +053093#define EMAC_CTL1 0x04
Andre Przywarae6e29cc2020-07-06 01:40:36 +010094#define EMAC_CTL1_SOFT_RST BIT(0)
95#define EMAC_CTL1_BURST_LEN_SHIFT 24
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053096#define EMAC_INT_STA 0x08
97#define EMAC_INT_EN 0x0c
98#define EMAC_TX_CTL0 0x10
Andre Przywarae6e29cc2020-07-06 01:40:36 +010099#define EMAC_TX_CTL0_TX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530100#define EMAC_TX_CTL1 0x14
Andre Przywarae6e29cc2020-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 Tomard194c0e2016-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 Przywarae6e29cc2020-07-06 01:40:36 +0100107#define EMAC_RX_CTL0_RX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530108#define EMAC_RX_CTL1 0x28
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100109#define EMAC_RX_CTL1_RX_MD BIT(1)
110#define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
111#define EMAC_RX_CTL1_RX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530112#define EMAC_RX_DMA_DESC 0x34
113#define EMAC_MII_CMD 0x48
114#define EMAC_MII_DATA 0x4c
115#define EMAC_ADDR0_HIGH 0x50
116#define EMAC_ADDR0_LOW 0x54
117#define EMAC_TX_DMA_STA 0xb0
118#define EMAC_TX_CUR_DESC 0xb4
119#define EMAC_TX_CUR_BUF 0xb8
120#define EMAC_RX_DMA_STA 0xc0
121#define EMAC_RX_CUR_DESC 0xc4
122
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100123#define EMAC_DESC_OWN_DMA BIT(31)
124#define EMAC_DESC_LAST_DESC BIT(30)
125#define EMAC_DESC_FIRST_DESC BIT(29)
126#define EMAC_DESC_CHAIN_SECOND BIT(24)
127
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530128DECLARE_GLOBAL_DATA_PTR;
129
130enum emac_variant {
131 A83T_EMAC = 1,
132 H3_EMAC,
133 A64_EMAC,
Lothar Feltene8cbced2018-07-13 10:45:28 +0200134 R40_GMAC,
Samuel Holland3386e9a2020-05-07 18:10:51 -0500135 H6_EMAC,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530136};
137
138struct emac_dma_desc {
139 u32 status;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100140 u32 ctl_size;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530141 u32 buf_addr;
142 u32 next;
143} __aligned(ARCH_DMA_MINALIGN);
144
145struct emac_eth_dev {
146 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
147 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
148 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
149 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
150
151 u32 interface;
152 u32 phyaddr;
153 u32 link;
154 u32 speed;
155 u32 duplex;
156 u32 phy_configured;
157 u32 tx_currdescnum;
158 u32 rx_currdescnum;
159 u32 addr;
160 u32 tx_slot;
161 bool use_internal_phy;
162
163 enum emac_variant variant;
164 void *mac_reg;
165 phys_addr_t sysctl_reg;
166 struct phy_device *phydev;
167 struct mii_dev *bus;
Jagan Tekicb63d282019-02-28 00:26:58 +0530168 struct clk tx_clk;
Jagan Teki727ed792019-02-28 00:27:00 +0530169 struct clk ephy_clk;
Jagan Tekicb63d282019-02-28 00:26:58 +0530170 struct reset_ctl tx_rst;
Jagan Teki727ed792019-02-28 00:27:00 +0530171 struct reset_ctl ephy_rst;
Simon Glassfa4689a2019-12-06 21:41:35 -0700172#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100173 struct gpio_desc reset_gpio;
174#endif
175};
176
177
178struct sun8i_eth_pdata {
179 struct eth_pdata eth_pdata;
180 u32 reset_delays[3];
Icenowy Zheng525dc442018-11-23 00:37:48 +0100181 int tx_delay_ps;
182 int rx_delay_ps;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530183};
184
Philipp Tomsich3297b552017-02-22 19:46:41 +0100185
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530186static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
187{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100188 struct udevice *dev = bus->priv;
189 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100190 u32 mii_cmd;
191 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530192
Andre Przywara0dd619b2020-07-06 01:40:34 +0100193 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530194 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100195 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530196 MDIO_CMD_MII_PHY_ADDR_MASK;
197
Andre Przywara0dd619b2020-07-06 01:40:34 +0100198 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530199
Andre Przywara0dd619b2020-07-06 01:40:34 +0100200 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530201
Andre Przywara0dd619b2020-07-06 01:40:34 +0100202 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
203 MDIO_CMD_MII_BUSY, false,
204 CONFIG_MDIO_TIMEOUT, true);
205 if (ret < 0)
206 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530207
Andre Przywara0dd619b2020-07-06 01:40:34 +0100208 return readl(priv->mac_reg + EMAC_MII_DATA);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530209}
210
211static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
212 u16 val)
213{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100214 struct udevice *dev = bus->priv;
215 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100216 u32 mii_cmd;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530217
Andre Przywara0dd619b2020-07-06 01:40:34 +0100218 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530219 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100220 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530221 MDIO_CMD_MII_PHY_ADDR_MASK;
222
Andre Przywara0dd619b2020-07-06 01:40:34 +0100223 mii_cmd |= MDIO_CMD_MII_WRITE;
224 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530225
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530226 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100227 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530228
Andre Przywara0dd619b2020-07-06 01:40:34 +0100229 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
230 MDIO_CMD_MII_BUSY, false,
231 CONFIG_MDIO_TIMEOUT, true);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530232}
233
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530234static int sun8i_eth_write_hwaddr(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530235{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530236 struct emac_eth_dev *priv = dev_get_priv(dev);
237 struct eth_pdata *pdata = dev_get_platdata(dev);
238 uchar *mac_id = pdata->enetaddr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530239 u32 macid_lo, macid_hi;
240
241 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
242 (mac_id[3] << 24);
243 macid_hi = mac_id[4] + (mac_id[5] << 8);
244
245 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
246 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
247
248 return 0;
249}
250
251static void sun8i_adjust_link(struct emac_eth_dev *priv,
252 struct phy_device *phydev)
253{
254 u32 v;
255
256 v = readl(priv->mac_reg + EMAC_CTL0);
257
258 if (phydev->duplex)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100259 v |= EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530260 else
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100261 v &= ~EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530262
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100263 v &= ~EMAC_CTL0_SPEED_MASK;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530264
265 switch (phydev->speed) {
266 case 1000:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100267 v |= EMAC_CTL0_SPEED_1000;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530268 break;
269 case 100:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100270 v |= EMAC_CTL0_SPEED_100;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530271 break;
272 case 10:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100273 v |= EMAC_CTL0_SPEED_10;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530274 break;
275 }
276 writel(v, priv->mac_reg + EMAC_CTL0);
277}
278
279static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
280{
281 if (priv->use_internal_phy) {
282 /* H3 based SoC's that has an Internal 100MBit PHY
283 * needs to be configured and powered up before use
284 */
285 *reg &= ~H3_EPHY_DEFAULT_MASK;
286 *reg |= H3_EPHY_DEFAULT_VALUE;
287 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
288 *reg &= ~H3_EPHY_SHUTDOWN;
289 *reg |= H3_EPHY_SELECT;
290 } else
291 /* This is to select External Gigabit PHY on
292 * the boards with H3 SoC.
293 */
294 *reg &= ~H3_EPHY_SELECT;
295
296 return 0;
297}
298
Icenowy Zheng525dc442018-11-23 00:37:48 +0100299static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
300 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530301{
302 int ret;
303 u32 reg;
304
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530305 if (priv->variant == R40_GMAC) {
306 /* Select RGMII for R40 */
307 reg = readl(priv->sysctl_reg + 0x164);
Samuel Holland97f2cf12020-05-07 18:10:50 -0500308 reg |= SC_ETCS_INT_GMII |
309 SC_EPIT |
310 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530311
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530312 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200313 return 0;
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530314 }
315
316 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200317
Samuel Holland3386e9a2020-05-07 18:10:51 -0500318 if (priv->variant == H3_EMAC || priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530319 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
320 if (ret)
321 return ret;
322 }
323
324 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500325 if (priv->variant == H3_EMAC ||
326 priv->variant == A64_EMAC ||
327 priv->variant == H6_EMAC)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530328 reg &= ~SC_RMII_EN;
329
330 switch (priv->interface) {
331 case PHY_INTERFACE_MODE_MII:
332 /* default */
333 break;
334 case PHY_INTERFACE_MODE_RGMII:
335 reg |= SC_EPIT | SC_ETCS_INT_GMII;
336 break;
337 case PHY_INTERFACE_MODE_RMII:
338 if (priv->variant == H3_EMAC ||
Samuel Holland3386e9a2020-05-07 18:10:51 -0500339 priv->variant == A64_EMAC ||
340 priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530341 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
342 break;
343 }
344 /* RMII not supported on A83T */
345 default:
346 debug("%s: Invalid PHY interface\n", __func__);
347 return -EINVAL;
348 }
349
Icenowy Zheng525dc442018-11-23 00:37:48 +0100350 if (pdata->tx_delay_ps)
351 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
352 & SC_ETXDC_MASK;
353
354 if (pdata->rx_delay_ps)
355 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
356 & SC_ERXDC_MASK;
357
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100358 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530359
360 return 0;
361}
362
363static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
364{
365 struct phy_device *phydev;
366
367 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
368 if (!phydev)
369 return -ENODEV;
370
371 phy_connect_dev(phydev, dev);
372
373 priv->phydev = phydev;
374 phy_config(priv->phydev);
375
376 return 0;
377}
378
379static void rx_descs_init(struct emac_eth_dev *priv)
380{
381 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
382 char *rxbuffs = &priv->rxbuffer[0];
383 struct emac_dma_desc *desc_p;
384 u32 idx;
385
Andre Przywara7408b092020-07-06 01:40:37 +0100386 /*
387 * Make sure we don't have dirty cache lines around, which could
388 * be cleaned to DRAM *after* the MAC has already written data to it.
389 */
390 invalidate_dcache_range((uintptr_t)desc_table_p,
391 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
392 invalidate_dcache_range((uintptr_t)rxbuffs,
393 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530394
395 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
396 desc_p = &desc_table_p[idx];
397 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
398 ;
399 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Andre Przywara7408b092020-07-06 01:40:37 +0100400 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100401 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530402 }
403
404 /* Correcting the last pointer of the chain */
405 desc_p->next = (uintptr_t)&desc_table_p[0];
406
407 flush_dcache_range((uintptr_t)priv->rx_chain,
408 (uintptr_t)priv->rx_chain +
409 sizeof(priv->rx_chain));
410
411 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
412 priv->rx_currdescnum = 0;
413}
414
415static void tx_descs_init(struct emac_eth_dev *priv)
416{
417 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
418 char *txbuffs = &priv->txbuffer[0];
419 struct emac_dma_desc *desc_p;
420 u32 idx;
421
422 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
423 desc_p = &desc_table_p[idx];
424 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
425 ;
426 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100427 desc_p->ctl_size = 0;
Andre Przywaradf6f2712020-07-06 01:40:33 +0100428 desc_p->status = 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530429 }
430
431 /* Correcting the last pointer of the chain */
432 desc_p->next = (uintptr_t)&desc_table_p[0];
433
434 /* Flush all Tx buffer descriptors */
435 flush_dcache_range((uintptr_t)priv->tx_chain,
436 (uintptr_t)priv->tx_chain +
437 sizeof(priv->tx_chain));
438
439 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
440 priv->tx_currdescnum = 0;
441}
442
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530443static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530444{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530445 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100446 u32 reg;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530447 int timeout = 100;
Andre Przywara874145f2020-07-06 01:40:32 +0100448 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530449
450 reg = readl((priv->mac_reg + EMAC_CTL1));
451
452 if (!(reg & 0x1)) {
453 /* Soft reset MAC */
454 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
455 do {
456 reg = readl(priv->mac_reg + EMAC_CTL1);
457 } while ((reg & 0x01) != 0 && (--timeout));
458 if (!timeout) {
459 printf("%s: Timeout\n", __func__);
460 return -1;
461 }
462 }
463
464 /* Rewrite mac address after reset */
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530465 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530466
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100467 /* transmission starts after the full frame arrived in TX DMA FIFO */
468 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530469
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100470 /*
471 * RX DMA reads data from RX DMA FIFO to host memory after a
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530472 * complete frame has been written to RX DMA FIFO
473 */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100474 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530475
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100476 /* DMA burst length */
477 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530478
479 /* Initialize rx/tx descriptors */
480 rx_descs_init(priv);
481 tx_descs_init(priv);
482
483 /* PHY Start Up */
Andre Przywara874145f2020-07-06 01:40:32 +0100484 ret = phy_startup(priv->phydev);
485 if (ret)
486 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530487
488 sun8i_adjust_link(priv, priv->phydev);
489
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100490 /* Start RX/TX DMA */
491 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
492 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530493
494 /* Enable RX/TX */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100495 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
496 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530497
498 return 0;
499}
500
501static int parse_phy_pins(struct udevice *dev)
502{
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200503 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530504 int offset;
505 const char *pin_name;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100506 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530507
Simon Glassdd79d6e2017-01-17 16:52:55 -0700508 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530509 "pinctrl-0");
510 if (offset < 0) {
511 printf("WARNING: emac: cannot find pinctrl-0 node\n");
512 return offset;
513 }
514
515 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywara26e549b2018-04-04 01:31:15 +0100516 "drive-strength", ~0);
517 if (drive != ~0) {
518 if (drive <= 10)
519 drive = SUN4I_PINCTRL_10_MA;
520 else if (drive <= 20)
521 drive = SUN4I_PINCTRL_20_MA;
522 else if (drive <= 30)
523 drive = SUN4I_PINCTRL_30_MA;
524 else
525 drive = SUN4I_PINCTRL_40_MA;
Andre Przywara26e549b2018-04-04 01:31:15 +0100526 }
527
528 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
529 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywara26e549b2018-04-04 01:31:15 +0100530 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
531 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100532
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530533 for (i = 0; ; i++) {
534 int pin;
535
Simon Glassb0ea7402016-10-02 17:59:28 -0600536 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100537 "pins", i, NULL);
538 if (!pin_name)
539 break;
Andre Przywara26e549b2018-04-04 01:31:15 +0100540
541 pin = sunxi_name_to_gpio(pin_name);
542 if (pin < 0)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530543 continue;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530544
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200545 if (priv->variant == H3_EMAC)
546 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500547 else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC)
Lothar Feltene8cbced2018-07-13 10:45:28 +0200548 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200549 else
550 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
551
Andre Przywara26e549b2018-04-04 01:31:15 +0100552 if (drive != ~0)
553 sunxi_gpio_set_drv(pin, drive);
554 if (pull != ~0)
555 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530556 }
557
558 if (!i) {
Andre Przywara26e549b2018-04-04 01:31:15 +0100559 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530560 return -2;
561 }
562
563 return 0;
564}
565
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530566static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530567{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530568 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530569 u32 status, desc_num = priv->rx_currdescnum;
570 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
571 int length = -EAGAIN;
572 int good_packet = 1;
573 uintptr_t desc_start = (uintptr_t)desc_p;
574 uintptr_t desc_end = desc_start +
575 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
576
577 ulong data_start = (uintptr_t)desc_p->buf_addr;
578 ulong data_end;
579
580 /* Invalidate entire buffer descriptor */
581 invalidate_dcache_range(desc_start, desc_end);
582
583 status = desc_p->status;
584
585 /* Check for DMA own bit */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100586 if (!(status & EMAC_DESC_OWN_DMA)) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530587 length = (desc_p->status >> 16) & 0x3FFF;
588
589 if (length < 0x40) {
590 good_packet = 0;
591 debug("RX: Bad Packet (runt)\n");
592 }
593
594 data_end = data_start + length;
595 /* Invalidate received data */
596 invalidate_dcache_range(rounddown(data_start,
597 ARCH_DMA_MINALIGN),
598 roundup(data_end,
599 ARCH_DMA_MINALIGN));
600 if (good_packet) {
Hans de Goedefcdb3b32016-07-27 17:31:17 +0200601 if (length > CONFIG_ETH_RXSIZE) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530602 printf("Received packet is too big (len=%d)\n",
603 length);
604 return -EMSGSIZE;
605 }
606 *packetp = (uchar *)(ulong)desc_p->buf_addr;
607 return length;
608 }
609 }
610
611 return length;
612}
613
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530614static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530615{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530616 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100617 u32 desc_num = priv->tx_currdescnum;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530618 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
619 uintptr_t desc_start = (uintptr_t)desc_p;
620 uintptr_t desc_end = desc_start +
621 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
622
623 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
624 uintptr_t data_end = data_start +
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530625 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530626
627 /* Invalidate entire buffer descriptor */
628 invalidate_dcache_range(desc_start, desc_end);
629
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100630 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530631
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530632 memcpy((void *)data_start, packet, length);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530633
634 /* Flush data to be sent */
635 flush_dcache_range(data_start, data_end);
636
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100637 /* frame begin and end */
638 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
639 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530640
641 /*Descriptors st and status field has changed, so FLUSH it */
642 flush_dcache_range(desc_start, desc_end);
643
644 /* Move to next Descriptor and wrap around */
645 if (++desc_num >= CONFIG_TX_DESCR_NUM)
646 desc_num = 0;
647 priv->tx_currdescnum = desc_num;
648
649 /* Start the DMA */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100650 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
651
652 /*
653 * Since we copied the data above, we return here without waiting
654 * for the packet to be actually send out.
655 */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530656
657 return 0;
658}
659
Sean Anderson4702aa22020-09-15 10:45:00 -0400660static int sun8i_emac_board_setup(struct udevice *dev,
661 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530662{
Jagan Tekicb63d282019-02-28 00:26:58 +0530663 int ret;
664
665 ret = clk_enable(&priv->tx_clk);
666 if (ret) {
667 dev_err(dev, "failed to enable TX clock\n");
668 return ret;
669 }
670
671 if (reset_valid(&priv->tx_rst)) {
672 ret = reset_deassert(&priv->tx_rst);
673 if (ret) {
674 dev_err(dev, "failed to deassert TX reset\n");
675 goto err_tx_clk;
676 }
677 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530678
Jagan Teki727ed792019-02-28 00:27:00 +0530679 /* Only H3/H5 have clock controls for internal EPHY */
680 if (clk_valid(&priv->ephy_clk)) {
681 ret = clk_enable(&priv->ephy_clk);
682 if (ret) {
683 dev_err(dev, "failed to enable EPHY TX clock\n");
684 return ret;
685 }
686 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530687
Jagan Teki727ed792019-02-28 00:27:00 +0530688 if (reset_valid(&priv->ephy_rst)) {
689 ret = reset_deassert(&priv->ephy_rst);
690 if (ret) {
691 dev_err(dev, "failed to deassert EPHY TX clock\n");
692 return ret;
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200693 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530694 }
695
Jagan Tekicb63d282019-02-28 00:26:58 +0530696 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200697
Jagan Tekicb63d282019-02-28 00:26:58 +0530698err_tx_clk:
699 clk_disable(&priv->tx_clk);
700 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530701}
702
Simon Glassfa4689a2019-12-06 21:41:35 -0700703#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100704static int sun8i_mdio_reset(struct mii_dev *bus)
705{
706 struct udevice *dev = bus->priv;
707 struct emac_eth_dev *priv = dev_get_priv(dev);
708 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
709 int ret;
710
711 if (!dm_gpio_is_valid(&priv->reset_gpio))
712 return 0;
713
714 /* reset the phy */
715 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
716 if (ret)
717 return ret;
718
719 udelay(pdata->reset_delays[0]);
720
721 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
722 if (ret)
723 return ret;
724
725 udelay(pdata->reset_delays[1]);
726
727 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
728 if (ret)
729 return ret;
730
731 udelay(pdata->reset_delays[2]);
732
733 return 0;
734}
735#endif
736
737static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530738{
739 struct mii_dev *bus = mdio_alloc();
740
741 if (!bus) {
742 debug("Failed to allocate MDIO bus\n");
743 return -ENOMEM;
744 }
745
746 bus->read = sun8i_mdio_read;
747 bus->write = sun8i_mdio_write;
748 snprintf(bus->name, sizeof(bus->name), name);
749 bus->priv = (void *)priv;
Simon Glassfa4689a2019-12-06 21:41:35 -0700750#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100751 bus->reset = sun8i_mdio_reset;
752#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530753
754 return mdio_register(bus);
755}
756
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530757static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
758 int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530759{
760 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530761 u32 desc_num = priv->rx_currdescnum;
762 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
763 uintptr_t desc_start = (uintptr_t)desc_p;
764 uintptr_t desc_end = desc_start +
765 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
766
767 /* Make the current descriptor valid again */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100768 desc_p->status |= EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530769
770 /* Flush Status field of descriptor */
771 flush_dcache_range(desc_start, desc_end);
772
773 /* Move to next desc and wrap-around condition. */
774 if (++desc_num >= CONFIG_RX_DESCR_NUM)
775 desc_num = 0;
776 priv->rx_currdescnum = desc_num;
777
778 return 0;
779}
780
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530781static void sun8i_emac_eth_stop(struct udevice *dev)
782{
783 struct emac_eth_dev *priv = dev_get_priv(dev);
784
785 /* Stop Rx/Tx transmitter */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100786 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
787 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530788
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100789 /* Stop RX/TX DMA */
790 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
791 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530792
793 phy_shutdown(priv->phydev);
794}
795
796static int sun8i_emac_eth_probe(struct udevice *dev)
797{
Icenowy Zheng525dc442018-11-23 00:37:48 +0100798 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
799 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530800 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530801 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530802
803 priv->mac_reg = (void *)pdata->iobase;
804
Sean Anderson4702aa22020-09-15 10:45:00 -0400805 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekicb63d282019-02-28 00:26:58 +0530806 if (ret)
807 return ret;
808
Icenowy Zheng525dc442018-11-23 00:37:48 +0100809 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530810
Philipp Tomsich3297b552017-02-22 19:46:41 +0100811 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530812 priv->bus = miiphy_get_dev_by_name(dev->name);
813
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530814 return sun8i_phy_init(priv, dev);
815}
816
817static const struct eth_ops sun8i_emac_eth_ops = {
818 .start = sun8i_emac_eth_start,
819 .write_hwaddr = sun8i_eth_write_hwaddr,
820 .send = sun8i_emac_eth_send,
821 .recv = sun8i_emac_eth_recv,
822 .free_pkt = sun8i_eth_free_pkt,
823 .stop = sun8i_emac_eth_stop,
824};
825
Sean Anderson4702aa22020-09-15 10:45:00 -0400826static int sun8i_get_ephy_nodes(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki727ed792019-02-28 00:27:00 +0530827{
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200828 int emac_node, ephy_node, ret, ephy_handle;
829
830 emac_node = fdt_path_offset(gd->fdt_blob,
831 "/soc/ethernet@1c30000");
832 if (emac_node < 0) {
833 debug("failed to get emac node\n");
834 return emac_node;
835 }
836 ephy_handle = fdtdec_lookup_phandle(gd->fdt_blob,
837 emac_node, "phy-handle");
Jagan Teki727ed792019-02-28 00:27:00 +0530838
839 /* look for mdio-mux node for internal PHY node */
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200840 ephy_node = fdt_path_offset(gd->fdt_blob,
841 "/soc/ethernet@1c30000/mdio-mux/mdio@1/ethernet-phy@1");
842 if (ephy_node < 0) {
Jagan Teki727ed792019-02-28 00:27:00 +0530843 debug("failed to get mdio-mux with internal PHY\n");
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200844 return ephy_node;
Jagan Teki727ed792019-02-28 00:27:00 +0530845 }
846
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200847 /* This is not the phy we are looking for */
848 if (ephy_node != ephy_handle)
849 return 0;
850
851 ret = fdt_node_check_compatible(gd->fdt_blob, ephy_node,
Jagan Teki727ed792019-02-28 00:27:00 +0530852 "allwinner,sun8i-h3-mdio-internal");
853 if (ret < 0) {
854 debug("failed to find mdio-internal node\n");
855 return ret;
856 }
857
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200858 ret = clk_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki727ed792019-02-28 00:27:00 +0530859 &priv->ephy_clk);
860 if (ret) {
861 dev_err(dev, "failed to get EPHY TX clock\n");
862 return ret;
863 }
864
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200865 ret = reset_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki727ed792019-02-28 00:27:00 +0530866 &priv->ephy_rst);
867 if (ret) {
868 dev_err(dev, "failed to get EPHY TX reset\n");
869 return ret;
870 }
871
872 priv->use_internal_phy = true;
873
874 return 0;
875}
876
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530877static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
878{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100879 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
880 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530881 struct emac_eth_dev *priv = dev_get_priv(dev);
882 const char *phy_mode;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100883 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700884 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530885 int offset = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -0700886#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100887 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100888#endif
Jagan Tekicb63d282019-02-28 00:26:58 +0530889 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530890
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900891 pdata->iobase = dev_read_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100892 if (pdata->iobase == FDT_ADDR_T_NONE) {
893 debug("%s: Cannot find MAC base address\n", __func__);
894 return -EINVAL;
895 }
896
Lothar Feltene8cbced2018-07-13 10:45:28 +0200897 priv->variant = dev_get_driver_data(dev);
898
899 if (!priv->variant) {
900 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100901 return -EINVAL;
902 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200903
Jagan Tekicb63d282019-02-28 00:26:58 +0530904 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
905 if (ret) {
906 dev_err(dev, "failed to get TX clock\n");
907 return ret;
908 }
909
910 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
911 if (ret && ret != -ENOENT) {
912 dev_err(dev, "failed to get TX reset\n");
913 return ret;
914 }
915
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530916 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
917 if (offset < 0) {
918 debug("%s: cannot find syscon node\n", __func__);
919 return -EINVAL;
920 }
921
922 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
923 if (!reg) {
924 debug("%s: cannot find reg property in syscon node\n",
925 __func__);
926 return -EINVAL;
927 }
928 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
929 offset, reg);
930 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
931 debug("%s: Cannot find syscon base address\n", __func__);
932 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100933 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530934
935 pdata->phy_interface = -1;
936 priv->phyaddr = -1;
937 priv->use_internal_phy = false;
938
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100939 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100940 if (offset < 0) {
941 debug("%s: Cannot find PHY address\n", __func__);
942 return -EINVAL;
943 }
944 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530945
Simon Glassdd79d6e2017-01-17 16:52:55 -0700946 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530947
948 if (phy_mode)
949 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
950 printf("phy interface%d\n", pdata->phy_interface);
951
952 if (pdata->phy_interface == -1) {
953 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
954 return -EINVAL;
955 }
956
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530957 if (priv->variant == H3_EMAC) {
Sean Anderson4702aa22020-09-15 10:45:00 -0400958 ret = sun8i_get_ephy_nodes(dev, priv);
Jagan Teki727ed792019-02-28 00:27:00 +0530959 if (ret)
960 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530961 }
962
963 priv->interface = pdata->phy_interface;
964
965 if (!priv->use_internal_phy)
966 parse_phy_pins(dev);
967
Icenowy Zheng525dc442018-11-23 00:37:48 +0100968 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
969 "allwinner,tx-delay-ps", 0);
970 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
971 printf("%s: Invalid TX delay value %d\n", __func__,
972 sun8i_pdata->tx_delay_ps);
973
974 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
975 "allwinner,rx-delay-ps", 0);
976 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
977 printf("%s: Invalid RX delay value %d\n", __func__,
978 sun8i_pdata->rx_delay_ps);
979
Simon Glassfa4689a2019-12-06 21:41:35 -0700980#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass7a494432017-05-17 17:18:09 -0600981 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100982 "snps,reset-active-low"))
983 reset_flags |= GPIOD_ACTIVE_LOW;
984
985 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
986 &priv->reset_gpio, reset_flags);
987
988 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600989 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100990 "snps,reset-delays-us",
991 sun8i_pdata->reset_delays, 3);
992 } else if (ret == -ENOENT) {
993 ret = 0;
994 }
995#endif
996
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530997 return 0;
998}
999
1000static const struct udevice_id sun8i_emac_eth_ids[] = {
1001 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
1002 {.compatible = "allwinner,sun50i-a64-emac",
1003 .data = (uintptr_t)A64_EMAC },
1004 {.compatible = "allwinner,sun8i-a83t-emac",
1005 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene8cbced2018-07-13 10:45:28 +02001006 {.compatible = "allwinner,sun8i-r40-gmac",
1007 .data = (uintptr_t)R40_GMAC },
Samuel Holland3386e9a2020-05-07 18:10:51 -05001008 {.compatible = "allwinner,sun50i-h6-emac",
1009 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301010 { }
1011};
1012
1013U_BOOT_DRIVER(eth_sun8i_emac) = {
1014 .name = "eth_sun8i_emac",
1015 .id = UCLASS_ETH,
1016 .of_match = sun8i_emac_eth_ids,
1017 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
1018 .probe = sun8i_emac_eth_probe,
1019 .ops = &sun8i_emac_eth_ops,
1020 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +01001021 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301022 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1023};