blob: 7144f96aa301e52ec868ebcdfd43e255da38074f [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)
110#define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
111#define EMAC_RX_CTL1_RX_DMA_START BIT(31)
Amit Singh Tomara29710c2016-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 Przywara4fe86412020-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 Tomara29710c2016-07-06 17:59:44 +0530128DECLARE_GLOBAL_DATA_PTR;
129
130enum emac_variant {
131 A83T_EMAC = 1,
132 H3_EMAC,
133 A64_EMAC,
Lothar Feltene46d73f2018-07-13 10:45:28 +0200134 R40_GMAC,
Samuel Holland99ac8612020-05-07 18:10:51 -0500135 H6_EMAC,
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530136};
137
138struct emac_dma_desc {
139 u32 status;
Andre Przywara4fe86412020-07-06 01:40:36 +0100140 u32 ctl_size;
Amit Singh Tomara29710c2016-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 Tekid3a2c052019-02-28 00:26:58 +0530168 struct clk tx_clk;
Jagan Teki23484532019-02-28 00:27:00 +0530169 struct clk ephy_clk;
Jagan Tekid3a2c052019-02-28 00:26:58 +0530170 struct reset_ctl tx_rst;
Jagan Teki23484532019-02-28 00:27:00 +0530171 struct reset_ctl ephy_rst;
Simon Glassbcee8d62019-12-06 21:41:35 -0700172#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100173 struct gpio_desc reset_gpio;
174#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530175};
176
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100177
178struct sun8i_eth_pdata {
179 struct eth_pdata eth_pdata;
180 u32 reset_delays[3];
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100181 int tx_delay_ps;
182 int rx_delay_ps;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100183};
184
185
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530186static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
187{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100188 struct udevice *dev = bus->priv;
189 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywaraf20f9462020-07-06 01:40:34 +0100190 u32 mii_cmd;
191 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530192
Andre Przywaraf20f9462020-07-06 01:40:34 +0100193 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530194 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywaraf20f9462020-07-06 01:40:34 +0100195 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530196 MDIO_CMD_MII_PHY_ADDR_MASK;
197
Andre Przywaraf20f9462020-07-06 01:40:34 +0100198 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530199
Andre Przywaraf20f9462020-07-06 01:40:34 +0100200 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530201
Andre Przywaraf20f9462020-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 Tomara29710c2016-07-06 17:59:44 +0530207
Andre Przywaraf20f9462020-07-06 01:40:34 +0100208 return readl(priv->mac_reg + EMAC_MII_DATA);
Amit Singh Tomara29710c2016-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 Tomsich4d555ae2017-02-22 19:46:41 +0100214 struct udevice *dev = bus->priv;
215 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywaraf20f9462020-07-06 01:40:34 +0100216 u32 mii_cmd;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530217
Andre Przywaraf20f9462020-07-06 01:40:34 +0100218 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530219 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywaraf20f9462020-07-06 01:40:34 +0100220 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530221 MDIO_CMD_MII_PHY_ADDR_MASK;
222
Andre Przywaraf20f9462020-07-06 01:40:34 +0100223 mii_cmd |= MDIO_CMD_MII_WRITE;
224 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530225
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530226 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywaraf20f9462020-07-06 01:40:34 +0100227 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530228
Andre Przywaraf20f9462020-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 Tomara29710c2016-07-06 17:59:44 +0530232}
233
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530234static int sun8i_eth_write_hwaddr(struct udevice *dev)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530235{
Andre Przywaraa5b2a992020-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 Tomara29710c2016-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 Przywara4fe86412020-07-06 01:40:36 +0100259 v |= EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530260 else
Andre Przywara4fe86412020-07-06 01:40:36 +0100261 v &= ~EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530262
Andre Przywara4fe86412020-07-06 01:40:36 +0100263 v &= ~EMAC_CTL0_SPEED_MASK;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530264
265 switch (phydev->speed) {
266 case 1000:
Andre Przywara4fe86412020-07-06 01:40:36 +0100267 v |= EMAC_CTL0_SPEED_1000;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530268 break;
269 case 100:
Andre Przywara4fe86412020-07-06 01:40:36 +0100270 v |= EMAC_CTL0_SPEED_100;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530271 break;
272 case 10:
Andre Przywara4fe86412020-07-06 01:40:36 +0100273 v |= EMAC_CTL0_SPEED_10;
Amit Singh Tomara29710c2016-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 Zheng9b16ede2018-11-23 00:37:48 +0100299static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
300 struct emac_eth_dev *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530301{
302 int ret;
303 u32 reg;
304
Jagan Teki695f6042019-02-28 00:26:51 +0530305 if (priv->variant == R40_GMAC) {
306 /* Select RGMII for R40 */
307 reg = readl(priv->sysctl_reg + 0x164);
Samuel Hollandabdbefb2020-05-07 18:10:50 -0500308 reg |= SC_ETCS_INT_GMII |
309 SC_EPIT |
310 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530311
Jagan Teki695f6042019-02-28 00:26:51 +0530312 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200313 return 0;
Jagan Teki695f6042019-02-28 00:26:51 +0530314 }
315
316 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200317
Samuel Holland99ac8612020-05-07 18:10:51 -0500318 if (priv->variant == H3_EMAC || priv->variant == H6_EMAC) {
Amit Singh Tomara29710c2016-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 Holland99ac8612020-05-07 18:10:51 -0500325 if (priv->variant == H3_EMAC ||
326 priv->variant == A64_EMAC ||
327 priv->variant == H6_EMAC)
Amit Singh Tomara29710c2016-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 Holland99ac8612020-05-07 18:10:51 -0500339 priv->variant == A64_EMAC ||
340 priv->variant == H6_EMAC) {
Amit Singh Tomara29710c2016-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 Zheng9b16ede2018-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 Przywara12afd952018-04-04 01:31:16 +0100358 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomara29710c2016-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
386 /* flush Rx buffers */
387 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
388 RX_TOTAL_BUFSIZE);
389
390 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
391 desc_p = &desc_table_p[idx];
392 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
393 ;
394 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Andre Przywara4fe86412020-07-06 01:40:36 +0100395 desc_p->ctl_size |= CONFIG_ETH_RXSIZE;
396 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530397 }
398
399 /* Correcting the last pointer of the chain */
400 desc_p->next = (uintptr_t)&desc_table_p[0];
401
402 flush_dcache_range((uintptr_t)priv->rx_chain,
403 (uintptr_t)priv->rx_chain +
404 sizeof(priv->rx_chain));
405
406 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
407 priv->rx_currdescnum = 0;
408}
409
410static void tx_descs_init(struct emac_eth_dev *priv)
411{
412 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
413 char *txbuffs = &priv->txbuffer[0];
414 struct emac_dma_desc *desc_p;
415 u32 idx;
416
417 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
418 desc_p = &desc_table_p[idx];
419 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
420 ;
421 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Andre Przywara4fe86412020-07-06 01:40:36 +0100422 desc_p->ctl_size = 0;
Andre Przywarac35380c2020-07-06 01:40:33 +0100423 desc_p->status = 0;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530424 }
425
426 /* Correcting the last pointer of the chain */
427 desc_p->next = (uintptr_t)&desc_table_p[0];
428
429 /* Flush all Tx buffer descriptors */
430 flush_dcache_range((uintptr_t)priv->tx_chain,
431 (uintptr_t)priv->tx_chain +
432 sizeof(priv->tx_chain));
433
434 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
435 priv->tx_currdescnum = 0;
436}
437
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530438static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530439{
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530440 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara4fe86412020-07-06 01:40:36 +0100441 u32 reg;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530442 int timeout = 100;
Andre Przywara2808cf62020-07-06 01:40:32 +0100443 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530444
445 reg = readl((priv->mac_reg + EMAC_CTL1));
446
447 if (!(reg & 0x1)) {
448 /* Soft reset MAC */
449 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
450 do {
451 reg = readl(priv->mac_reg + EMAC_CTL1);
452 } while ((reg & 0x01) != 0 && (--timeout));
453 if (!timeout) {
454 printf("%s: Timeout\n", __func__);
455 return -1;
456 }
457 }
458
459 /* Rewrite mac address after reset */
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530460 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530461
Andre Przywara4fe86412020-07-06 01:40:36 +0100462 /* transmission starts after the full frame arrived in TX DMA FIFO */
463 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530464
Andre Przywara4fe86412020-07-06 01:40:36 +0100465 /*
466 * RX DMA reads data from RX DMA FIFO to host memory after a
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530467 * complete frame has been written to RX DMA FIFO
468 */
Andre Przywara4fe86412020-07-06 01:40:36 +0100469 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530470
Andre Przywara4fe86412020-07-06 01:40:36 +0100471 /* DMA burst length */
472 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530473
474 /* Initialize rx/tx descriptors */
475 rx_descs_init(priv);
476 tx_descs_init(priv);
477
478 /* PHY Start Up */
Andre Przywara2808cf62020-07-06 01:40:32 +0100479 ret = phy_startup(priv->phydev);
480 if (ret)
481 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530482
483 sun8i_adjust_link(priv, priv->phydev);
484
Andre Przywara4fe86412020-07-06 01:40:36 +0100485 /* Start RX/TX DMA */
486 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
487 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530488
489 /* Enable RX/TX */
Andre Przywara4fe86412020-07-06 01:40:36 +0100490 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
491 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530492
493 return 0;
494}
495
496static int parse_phy_pins(struct udevice *dev)
497{
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200498 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530499 int offset;
500 const char *pin_name;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100501 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530502
Simon Glasse160f7d2017-01-17 16:52:55 -0700503 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530504 "pinctrl-0");
505 if (offset < 0) {
506 printf("WARNING: emac: cannot find pinctrl-0 node\n");
507 return offset;
508 }
509
510 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywarac0341172018-04-04 01:31:15 +0100511 "drive-strength", ~0);
512 if (drive != ~0) {
513 if (drive <= 10)
514 drive = SUN4I_PINCTRL_10_MA;
515 else if (drive <= 20)
516 drive = SUN4I_PINCTRL_20_MA;
517 else if (drive <= 30)
518 drive = SUN4I_PINCTRL_30_MA;
519 else
520 drive = SUN4I_PINCTRL_40_MA;
Andre Przywarac0341172018-04-04 01:31:15 +0100521 }
522
523 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
524 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywarac0341172018-04-04 01:31:15 +0100525 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
526 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100527
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530528 for (i = 0; ; i++) {
529 int pin;
530
Simon Glassb02e4042016-10-02 17:59:28 -0600531 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100532 "pins", i, NULL);
533 if (!pin_name)
534 break;
Andre Przywarac0341172018-04-04 01:31:15 +0100535
536 pin = sunxi_name_to_gpio(pin_name);
537 if (pin < 0)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530538 continue;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530539
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200540 if (priv->variant == H3_EMAC)
541 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Samuel Holland99ac8612020-05-07 18:10:51 -0500542 else if (priv->variant == R40_GMAC || priv->variant == H6_EMAC)
Lothar Feltene46d73f2018-07-13 10:45:28 +0200543 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200544 else
545 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
546
Andre Przywarac0341172018-04-04 01:31:15 +0100547 if (drive != ~0)
548 sunxi_gpio_set_drv(pin, drive);
549 if (pull != ~0)
550 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530551 }
552
553 if (!i) {
Andre Przywarac0341172018-04-04 01:31:15 +0100554 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530555 return -2;
556 }
557
558 return 0;
559}
560
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530561static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530562{
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530563 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530564 u32 status, desc_num = priv->rx_currdescnum;
565 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
566 int length = -EAGAIN;
567 int good_packet = 1;
568 uintptr_t desc_start = (uintptr_t)desc_p;
569 uintptr_t desc_end = desc_start +
570 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
571
572 ulong data_start = (uintptr_t)desc_p->buf_addr;
573 ulong data_end;
574
575 /* Invalidate entire buffer descriptor */
576 invalidate_dcache_range(desc_start, desc_end);
577
578 status = desc_p->status;
579
580 /* Check for DMA own bit */
Andre Przywara4fe86412020-07-06 01:40:36 +0100581 if (!(status & EMAC_DESC_OWN_DMA)) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530582 length = (desc_p->status >> 16) & 0x3FFF;
583
584 if (length < 0x40) {
585 good_packet = 0;
586 debug("RX: Bad Packet (runt)\n");
587 }
588
589 data_end = data_start + length;
590 /* Invalidate received data */
591 invalidate_dcache_range(rounddown(data_start,
592 ARCH_DMA_MINALIGN),
593 roundup(data_end,
594 ARCH_DMA_MINALIGN));
595 if (good_packet) {
Hans de Goede40694372016-07-27 17:31:17 +0200596 if (length > CONFIG_ETH_RXSIZE) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530597 printf("Received packet is too big (len=%d)\n",
598 length);
599 return -EMSGSIZE;
600 }
601 *packetp = (uchar *)(ulong)desc_p->buf_addr;
602 return length;
603 }
604 }
605
606 return length;
607}
608
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530609static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530610{
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530611 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara4fe86412020-07-06 01:40:36 +0100612 u32 desc_num = priv->tx_currdescnum;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530613 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
614 uintptr_t desc_start = (uintptr_t)desc_p;
615 uintptr_t desc_end = desc_start +
616 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
617
618 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
619 uintptr_t data_end = data_start +
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530620 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530621
622 /* Invalidate entire buffer descriptor */
623 invalidate_dcache_range(desc_start, desc_end);
624
Andre Przywara4fe86412020-07-06 01:40:36 +0100625 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530626
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530627 memcpy((void *)data_start, packet, length);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530628
629 /* Flush data to be sent */
630 flush_dcache_range(data_start, data_end);
631
Andre Przywara4fe86412020-07-06 01:40:36 +0100632 /* frame begin and end */
633 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
634 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530635
636 /*Descriptors st and status field has changed, so FLUSH it */
637 flush_dcache_range(desc_start, desc_end);
638
639 /* Move to next Descriptor and wrap around */
640 if (++desc_num >= CONFIG_TX_DESCR_NUM)
641 desc_num = 0;
642 priv->tx_currdescnum = desc_num;
643
644 /* Start the DMA */
Andre Przywara4fe86412020-07-06 01:40:36 +0100645 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
646
647 /*
648 * Since we copied the data above, we return here without waiting
649 * for the packet to be actually send out.
650 */
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530651
652 return 0;
653}
654
Sean Andersonef043692020-09-15 10:45:00 -0400655static int sun8i_emac_board_setup(struct udevice *dev,
656 struct emac_eth_dev *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530657{
Jagan Tekid3a2c052019-02-28 00:26:58 +0530658 int ret;
659
660 ret = clk_enable(&priv->tx_clk);
661 if (ret) {
662 dev_err(dev, "failed to enable TX clock\n");
663 return ret;
664 }
665
666 if (reset_valid(&priv->tx_rst)) {
667 ret = reset_deassert(&priv->tx_rst);
668 if (ret) {
669 dev_err(dev, "failed to deassert TX reset\n");
670 goto err_tx_clk;
671 }
672 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530673
Jagan Teki23484532019-02-28 00:27:00 +0530674 /* Only H3/H5 have clock controls for internal EPHY */
675 if (clk_valid(&priv->ephy_clk)) {
676 ret = clk_enable(&priv->ephy_clk);
677 if (ret) {
678 dev_err(dev, "failed to enable EPHY TX clock\n");
679 return ret;
680 }
681 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530682
Jagan Teki23484532019-02-28 00:27:00 +0530683 if (reset_valid(&priv->ephy_rst)) {
684 ret = reset_deassert(&priv->ephy_rst);
685 if (ret) {
686 dev_err(dev, "failed to deassert EPHY TX clock\n");
687 return ret;
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200688 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530689 }
690
Jagan Tekid3a2c052019-02-28 00:26:58 +0530691 return 0;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530692
Jagan Tekid3a2c052019-02-28 00:26:58 +0530693err_tx_clk:
694 clk_disable(&priv->tx_clk);
695 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530696}
697
Simon Glassbcee8d62019-12-06 21:41:35 -0700698#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100699static int sun8i_mdio_reset(struct mii_dev *bus)
700{
701 struct udevice *dev = bus->priv;
702 struct emac_eth_dev *priv = dev_get_priv(dev);
703 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
704 int ret;
705
706 if (!dm_gpio_is_valid(&priv->reset_gpio))
707 return 0;
708
709 /* reset the phy */
710 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
711 if (ret)
712 return ret;
713
714 udelay(pdata->reset_delays[0]);
715
716 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
717 if (ret)
718 return ret;
719
720 udelay(pdata->reset_delays[1]);
721
722 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
723 if (ret)
724 return ret;
725
726 udelay(pdata->reset_delays[2]);
727
728 return 0;
729}
730#endif
731
732static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530733{
734 struct mii_dev *bus = mdio_alloc();
735
736 if (!bus) {
737 debug("Failed to allocate MDIO bus\n");
738 return -ENOMEM;
739 }
740
741 bus->read = sun8i_mdio_read;
742 bus->write = sun8i_mdio_write;
743 snprintf(bus->name, sizeof(bus->name), name);
744 bus->priv = (void *)priv;
Simon Glassbcee8d62019-12-06 21:41:35 -0700745#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100746 bus->reset = sun8i_mdio_reset;
747#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530748
749 return mdio_register(bus);
750}
751
Andre Przywaraa5b2a992020-10-21 23:21:42 +0530752static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
753 int length)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530754{
755 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530756 u32 desc_num = priv->rx_currdescnum;
757 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
758 uintptr_t desc_start = (uintptr_t)desc_p;
759 uintptr_t desc_end = desc_start +
760 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
761
762 /* Make the current descriptor valid again */
Andre Przywara4fe86412020-07-06 01:40:36 +0100763 desc_p->status |= EMAC_DESC_OWN_DMA;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530764
765 /* Flush Status field of descriptor */
766 flush_dcache_range(desc_start, desc_end);
767
768 /* Move to next desc and wrap-around condition. */
769 if (++desc_num >= CONFIG_RX_DESCR_NUM)
770 desc_num = 0;
771 priv->rx_currdescnum = desc_num;
772
773 return 0;
774}
775
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530776static void sun8i_emac_eth_stop(struct udevice *dev)
777{
778 struct emac_eth_dev *priv = dev_get_priv(dev);
779
780 /* Stop Rx/Tx transmitter */
Andre Przywara4fe86412020-07-06 01:40:36 +0100781 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
782 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530783
Andre Przywara4fe86412020-07-06 01:40:36 +0100784 /* Stop RX/TX DMA */
785 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
786 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530787
788 phy_shutdown(priv->phydev);
789}
790
791static int sun8i_emac_eth_probe(struct udevice *dev)
792{
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100793 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
794 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530795 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekid3a2c052019-02-28 00:26:58 +0530796 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530797
798 priv->mac_reg = (void *)pdata->iobase;
799
Sean Andersonef043692020-09-15 10:45:00 -0400800 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekid3a2c052019-02-28 00:26:58 +0530801 if (ret)
802 return ret;
803
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100804 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530805
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100806 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530807 priv->bus = miiphy_get_dev_by_name(dev->name);
808
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530809 return sun8i_phy_init(priv, dev);
810}
811
812static const struct eth_ops sun8i_emac_eth_ops = {
813 .start = sun8i_emac_eth_start,
814 .write_hwaddr = sun8i_eth_write_hwaddr,
815 .send = sun8i_emac_eth_send,
816 .recv = sun8i_emac_eth_recv,
817 .free_pkt = sun8i_eth_free_pkt,
818 .stop = sun8i_emac_eth_stop,
819};
820
Sean Andersonef043692020-09-15 10:45:00 -0400821static int sun8i_get_ephy_nodes(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki23484532019-02-28 00:27:00 +0530822{
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200823 int emac_node, ephy_node, ret, ephy_handle;
824
825 emac_node = fdt_path_offset(gd->fdt_blob,
826 "/soc/ethernet@1c30000");
827 if (emac_node < 0) {
828 debug("failed to get emac node\n");
829 return emac_node;
830 }
831 ephy_handle = fdtdec_lookup_phandle(gd->fdt_blob,
832 emac_node, "phy-handle");
Jagan Teki23484532019-02-28 00:27:00 +0530833
834 /* look for mdio-mux node for internal PHY node */
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200835 ephy_node = fdt_path_offset(gd->fdt_blob,
836 "/soc/ethernet@1c30000/mdio-mux/mdio@1/ethernet-phy@1");
837 if (ephy_node < 0) {
Jagan Teki23484532019-02-28 00:27:00 +0530838 debug("failed to get mdio-mux with internal PHY\n");
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200839 return ephy_node;
Jagan Teki23484532019-02-28 00:27:00 +0530840 }
841
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200842 /* This is not the phy we are looking for */
843 if (ephy_node != ephy_handle)
844 return 0;
845
846 ret = fdt_node_check_compatible(gd->fdt_blob, ephy_node,
Jagan Teki23484532019-02-28 00:27:00 +0530847 "allwinner,sun8i-h3-mdio-internal");
848 if (ret < 0) {
849 debug("failed to find mdio-internal node\n");
850 return ret;
851 }
852
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200853 ret = clk_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki23484532019-02-28 00:27:00 +0530854 &priv->ephy_clk);
855 if (ret) {
856 dev_err(dev, "failed to get EPHY TX clock\n");
857 return ret;
858 }
859
Emmanuel Vadotd53e5222019-07-19 22:26:38 +0200860 ret = reset_get_by_index_nodev(offset_to_ofnode(ephy_node), 0,
Jagan Teki23484532019-02-28 00:27:00 +0530861 &priv->ephy_rst);
862 if (ret) {
863 dev_err(dev, "failed to get EPHY TX reset\n");
864 return ret;
865 }
866
867 priv->use_internal_phy = true;
868
869 return 0;
870}
871
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530872static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
873{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100874 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
875 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530876 struct emac_eth_dev *priv = dev_get_priv(dev);
877 const char *phy_mode;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100878 const fdt32_t *reg;
Simon Glasse160f7d2017-01-17 16:52:55 -0700879 int node = dev_of_offset(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530880 int offset = 0;
Simon Glassbcee8d62019-12-06 21:41:35 -0700881#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100882 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100883#endif
Jagan Tekid3a2c052019-02-28 00:26:58 +0530884 int ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530885
Masahiro Yamada25484932020-07-17 14:36:48 +0900886 pdata->iobase = dev_read_addr(dev);
Andre Przywara12afd952018-04-04 01:31:16 +0100887 if (pdata->iobase == FDT_ADDR_T_NONE) {
888 debug("%s: Cannot find MAC base address\n", __func__);
889 return -EINVAL;
890 }
891
Lothar Feltene46d73f2018-07-13 10:45:28 +0200892 priv->variant = dev_get_driver_data(dev);
893
894 if (!priv->variant) {
895 printf("%s: Missing variant\n", __func__);
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100896 return -EINVAL;
Andre Przywara12afd952018-04-04 01:31:16 +0100897 }
Lothar Feltene46d73f2018-07-13 10:45:28 +0200898
Jagan Tekid3a2c052019-02-28 00:26:58 +0530899 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
900 if (ret) {
901 dev_err(dev, "failed to get TX clock\n");
902 return ret;
903 }
904
905 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
906 if (ret && ret != -ENOENT) {
907 dev_err(dev, "failed to get TX reset\n");
908 return ret;
909 }
910
Jagan Teki695f6042019-02-28 00:26:51 +0530911 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
912 if (offset < 0) {
913 debug("%s: cannot find syscon node\n", __func__);
914 return -EINVAL;
915 }
916
917 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
918 if (!reg) {
919 debug("%s: cannot find reg property in syscon node\n",
920 __func__);
921 return -EINVAL;
922 }
923 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
924 offset, reg);
925 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
926 debug("%s: Cannot find syscon base address\n", __func__);
927 return -EINVAL;
Andre Przywara12afd952018-04-04 01:31:16 +0100928 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530929
930 pdata->phy_interface = -1;
931 priv->phyaddr = -1;
932 priv->use_internal_phy = false;
933
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100934 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywara12afd952018-04-04 01:31:16 +0100935 if (offset < 0) {
936 debug("%s: Cannot find PHY address\n", __func__);
937 return -EINVAL;
938 }
939 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530940
Simon Glasse160f7d2017-01-17 16:52:55 -0700941 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530942
943 if (phy_mode)
944 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
945 printf("phy interface%d\n", pdata->phy_interface);
946
947 if (pdata->phy_interface == -1) {
948 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
949 return -EINVAL;
950 }
951
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530952 if (priv->variant == H3_EMAC) {
Sean Andersonef043692020-09-15 10:45:00 -0400953 ret = sun8i_get_ephy_nodes(dev, priv);
Jagan Teki23484532019-02-28 00:27:00 +0530954 if (ret)
955 return ret;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530956 }
957
958 priv->interface = pdata->phy_interface;
959
960 if (!priv->use_internal_phy)
961 parse_phy_pins(dev);
962
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100963 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
964 "allwinner,tx-delay-ps", 0);
965 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
966 printf("%s: Invalid TX delay value %d\n", __func__,
967 sun8i_pdata->tx_delay_ps);
968
969 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
970 "allwinner,rx-delay-ps", 0);
971 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
972 printf("%s: Invalid RX delay value %d\n", __func__,
973 sun8i_pdata->rx_delay_ps);
974
Simon Glassbcee8d62019-12-06 21:41:35 -0700975#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glassda409cc2017-05-17 17:18:09 -0600976 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100977 "snps,reset-active-low"))
978 reset_flags |= GPIOD_ACTIVE_LOW;
979
980 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
981 &priv->reset_gpio, reset_flags);
982
983 if (ret == 0) {
Simon Glassda409cc2017-05-17 17:18:09 -0600984 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100985 "snps,reset-delays-us",
986 sun8i_pdata->reset_delays, 3);
987 } else if (ret == -ENOENT) {
988 ret = 0;
989 }
990#endif
991
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530992 return 0;
993}
994
995static const struct udevice_id sun8i_emac_eth_ids[] = {
996 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
997 {.compatible = "allwinner,sun50i-a64-emac",
998 .data = (uintptr_t)A64_EMAC },
999 {.compatible = "allwinner,sun8i-a83t-emac",
1000 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene46d73f2018-07-13 10:45:28 +02001001 {.compatible = "allwinner,sun8i-r40-gmac",
1002 .data = (uintptr_t)R40_GMAC },
Samuel Holland99ac8612020-05-07 18:10:51 -05001003 {.compatible = "allwinner,sun50i-h6-emac",
1004 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomara29710c2016-07-06 17:59:44 +05301005 { }
1006};
1007
1008U_BOOT_DRIVER(eth_sun8i_emac) = {
1009 .name = "eth_sun8i_emac",
1010 .id = UCLASS_ETH,
1011 .of_match = sun8i_emac_eth_ids,
1012 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
1013 .probe = sun8i_emac_eth_probe,
1014 .ops = &sun8i_emac_eth_ops,
1015 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +01001016 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomara29710c2016-07-06 17:59:44 +05301017 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1018};