blob: 5943a5e3f0a81cb88e8d1bfd2b6b1ff97eebb488 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060016#include <asm/global_data.h>
Samuel Holland06feb812021-09-11 16:50:47 -050017#include <asm/gpio.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053018#include <asm/io.h>
19#include <asm/arch/clock.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053020#include <common.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053021#include <clk.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053022#include <dm.h>
23#include <fdt_support.h>
Simon Glass9bc15642020-02-03 07:36:16 -070024#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060025#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060026#include <linux/delay.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053027#include <linux/err.h>
28#include <malloc.h>
29#include <miiphy.h>
30#include <net.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053031#include <reset.h>
Andre Przywara26e549b2018-04-04 01:31:15 +010032#include <dt-bindings/pinctrl/sun4i-a10.h>
Andre Przywara0dd619b2020-07-06 01:40:34 +010033#include <wait_bit.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053034
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053035#define MDIO_CMD_MII_BUSY BIT(0)
36#define MDIO_CMD_MII_WRITE BIT(1)
37
38#define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
39#define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
40#define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
41#define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
Andre Przywarab41f2472020-07-06 01:40:45 +010042#define MDIO_CMD_MII_CLK_CSR_DIV_16 0x0
43#define MDIO_CMD_MII_CLK_CSR_DIV_32 0x1
44#define MDIO_CMD_MII_CLK_CSR_DIV_64 0x2
45#define MDIO_CMD_MII_CLK_CSR_DIV_128 0x3
46#define MDIO_CMD_MII_CLK_CSR_SHIFT 20
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053047
48#define CONFIG_TX_DESCR_NUM 32
49#define CONFIG_RX_DESCR_NUM 32
Hans de Goedefcdb3b32016-07-27 17:31:17 +020050#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
51
52/*
53 * The datasheet says that each descriptor can transfers up to 4096 bytes
54 * But later, the register documentation reduces that value to 2048,
55 * using 2048 cause strange behaviours and even BSP driver use 2047
56 */
57#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053058
59#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
60#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
61
62#define H3_EPHY_DEFAULT_VALUE 0x58000
63#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
64#define H3_EPHY_ADDR_SHIFT 20
65#define REG_PHY_ADDR_MASK GENMASK(4, 0)
66#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
67#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
68#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
69
70#define SC_RMII_EN BIT(13)
71#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
72#define SC_ETCS_MASK GENMASK(1, 0)
73#define SC_ETCS_EXT_GMII 0x1
74#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng525dc442018-11-23 00:37:48 +010075#define SC_ETXDC_MASK GENMASK(12, 10)
76#define SC_ETXDC_OFFSET 10
77#define SC_ERXDC_MASK GENMASK(9, 5)
78#define SC_ERXDC_OFFSET 5
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053079
80#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
81
82#define AHB_GATE_OFFSET_EPHY 0
83
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020084/* IO mux settings */
85#define SUN8I_IOMUX_H3 2
Andre Przywara882a9232021-01-11 21:11:49 +010086#define SUN8I_IOMUX_R40 5
87#define SUN8I_IOMUX_H6 5
88#define SUN8I_IOMUX_H616 2
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020089#define SUN8I_IOMUX 4
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053090
91/* H3/A64 EMAC Register's offset */
92#define EMAC_CTL0 0x00
Andre Przywarae6e29cc2020-07-06 01:40:36 +010093#define EMAC_CTL0_FULL_DUPLEX BIT(0)
94#define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
95#define EMAC_CTL0_SPEED_10 (0x2 << 2)
96#define EMAC_CTL0_SPEED_100 (0x3 << 2)
97#define EMAC_CTL0_SPEED_1000 (0x0 << 2)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053098#define EMAC_CTL1 0x04
Andre Przywarae6e29cc2020-07-06 01:40:36 +010099#define EMAC_CTL1_SOFT_RST BIT(0)
100#define EMAC_CTL1_BURST_LEN_SHIFT 24
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530101#define EMAC_INT_STA 0x08
102#define EMAC_INT_EN 0x0c
103#define EMAC_TX_CTL0 0x10
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100104#define EMAC_TX_CTL0_TX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530105#define EMAC_TX_CTL1 0x14
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100106#define EMAC_TX_CTL1_TX_MD BIT(1)
107#define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
108#define EMAC_TX_CTL1_TX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530109#define EMAC_TX_FLOW_CTL 0x1c
110#define EMAC_TX_DMA_DESC 0x20
111#define EMAC_RX_CTL0 0x24
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100112#define EMAC_RX_CTL0_RX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530113#define EMAC_RX_CTL1 0x28
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100114#define EMAC_RX_CTL1_RX_MD BIT(1)
Andre Przywara59422822020-07-06 01:40:43 +0100115#define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
116#define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100117#define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
118#define EMAC_RX_CTL1_RX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530119#define EMAC_RX_DMA_DESC 0x34
120#define EMAC_MII_CMD 0x48
121#define EMAC_MII_DATA 0x4c
122#define EMAC_ADDR0_HIGH 0x50
123#define EMAC_ADDR0_LOW 0x54
124#define EMAC_TX_DMA_STA 0xb0
125#define EMAC_TX_CUR_DESC 0xb4
126#define EMAC_TX_CUR_BUF 0xb8
127#define EMAC_RX_DMA_STA 0xc0
128#define EMAC_RX_CUR_DESC 0xc4
129
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100130#define EMAC_DESC_OWN_DMA BIT(31)
131#define EMAC_DESC_LAST_DESC BIT(30)
132#define EMAC_DESC_FIRST_DESC BIT(29)
133#define EMAC_DESC_CHAIN_SECOND BIT(24)
134
Andre Przywara59422822020-07-06 01:40:43 +0100135#define EMAC_DESC_RX_ERROR_MASK 0x400068db
136
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530137DECLARE_GLOBAL_DATA_PTR;
138
139enum emac_variant {
140 A83T_EMAC = 1,
141 H3_EMAC,
142 A64_EMAC,
Lothar Feltene8cbced2018-07-13 10:45:28 +0200143 R40_GMAC,
Samuel Holland3386e9a2020-05-07 18:10:51 -0500144 H6_EMAC,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530145};
146
147struct emac_dma_desc {
148 u32 status;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100149 u32 ctl_size;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530150 u32 buf_addr;
151 u32 next;
152} __aligned(ARCH_DMA_MINALIGN);
153
154struct emac_eth_dev {
155 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
156 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
157 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
158 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
159
160 u32 interface;
161 u32 phyaddr;
162 u32 link;
163 u32 speed;
164 u32 duplex;
165 u32 phy_configured;
166 u32 tx_currdescnum;
167 u32 rx_currdescnum;
168 u32 addr;
169 u32 tx_slot;
170 bool use_internal_phy;
171
172 enum emac_variant variant;
173 void *mac_reg;
174 phys_addr_t sysctl_reg;
175 struct phy_device *phydev;
176 struct mii_dev *bus;
Jagan Tekicb63d282019-02-28 00:26:58 +0530177 struct clk tx_clk;
Jagan Teki727ed792019-02-28 00:27:00 +0530178 struct clk ephy_clk;
Jagan Tekicb63d282019-02-28 00:26:58 +0530179 struct reset_ctl tx_rst;
Jagan Teki727ed792019-02-28 00:27:00 +0530180 struct reset_ctl ephy_rst;
Simon Glassfa4689a2019-12-06 21:41:35 -0700181#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100182 struct gpio_desc reset_gpio;
183#endif
184};
185
186
187struct sun8i_eth_pdata {
188 struct eth_pdata eth_pdata;
189 u32 reset_delays[3];
Icenowy Zheng525dc442018-11-23 00:37:48 +0100190 int tx_delay_ps;
191 int rx_delay_ps;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530192};
193
Philipp Tomsich3297b552017-02-22 19:46:41 +0100194
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530195static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
196{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100197 struct udevice *dev = bus->priv;
198 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100199 u32 mii_cmd;
200 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530201
Andre Przywara0dd619b2020-07-06 01:40:34 +0100202 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530203 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100204 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530205 MDIO_CMD_MII_PHY_ADDR_MASK;
206
Andre Przywarab41f2472020-07-06 01:40:45 +0100207 /*
208 * The EMAC clock is either 200 or 300 MHz, so we need a divider
209 * of 128 to get the MDIO frequency below the required 2.5 MHz.
210 */
Heinrich Schuchardt6ffc0232021-06-03 07:52:41 +0000211 if (!priv->use_internal_phy)
212 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
213 MDIO_CMD_MII_CLK_CSR_SHIFT;
Andre Przywarab41f2472020-07-06 01:40:45 +0100214
Andre Przywara0dd619b2020-07-06 01:40:34 +0100215 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530216
Andre Przywara0dd619b2020-07-06 01:40:34 +0100217 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530218
Andre Przywara0dd619b2020-07-06 01:40:34 +0100219 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
220 MDIO_CMD_MII_BUSY, false,
221 CONFIG_MDIO_TIMEOUT, true);
222 if (ret < 0)
223 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530224
Andre Przywara0dd619b2020-07-06 01:40:34 +0100225 return readl(priv->mac_reg + EMAC_MII_DATA);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530226}
227
228static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
229 u16 val)
230{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100231 struct udevice *dev = bus->priv;
232 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100233 u32 mii_cmd;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530234
Andre Przywara0dd619b2020-07-06 01:40:34 +0100235 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530236 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100237 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530238 MDIO_CMD_MII_PHY_ADDR_MASK;
239
Andre Przywarab41f2472020-07-06 01:40:45 +0100240 /*
241 * The EMAC clock is either 200 or 300 MHz, so we need a divider
242 * of 128 to get the MDIO frequency below the required 2.5 MHz.
243 */
Heinrich Schuchardt6ffc0232021-06-03 07:52:41 +0000244 if (!priv->use_internal_phy)
245 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
246 MDIO_CMD_MII_CLK_CSR_SHIFT;
Andre Przywarab41f2472020-07-06 01:40:45 +0100247
Andre Przywara0dd619b2020-07-06 01:40:34 +0100248 mii_cmd |= MDIO_CMD_MII_WRITE;
249 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530250
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530251 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100252 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530253
Andre Przywara0dd619b2020-07-06 01:40:34 +0100254 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
255 MDIO_CMD_MII_BUSY, false,
256 CONFIG_MDIO_TIMEOUT, true);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530257}
258
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530259static int sun8i_eth_write_hwaddr(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530260{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530261 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700262 struct eth_pdata *pdata = dev_get_plat(dev);
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530263 uchar *mac_id = pdata->enetaddr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530264 u32 macid_lo, macid_hi;
265
266 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
267 (mac_id[3] << 24);
268 macid_hi = mac_id[4] + (mac_id[5] << 8);
269
270 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
271 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
272
273 return 0;
274}
275
276static void sun8i_adjust_link(struct emac_eth_dev *priv,
277 struct phy_device *phydev)
278{
279 u32 v;
280
281 v = readl(priv->mac_reg + EMAC_CTL0);
282
283 if (phydev->duplex)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100284 v |= EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530285 else
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100286 v &= ~EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530287
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100288 v &= ~EMAC_CTL0_SPEED_MASK;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530289
290 switch (phydev->speed) {
291 case 1000:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100292 v |= EMAC_CTL0_SPEED_1000;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530293 break;
294 case 100:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100295 v |= EMAC_CTL0_SPEED_100;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530296 break;
297 case 10:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100298 v |= EMAC_CTL0_SPEED_10;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530299 break;
300 }
301 writel(v, priv->mac_reg + EMAC_CTL0);
302}
303
Andre Przywara15651d82021-01-11 21:11:45 +0100304static u32 sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 reg)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530305{
306 if (priv->use_internal_phy) {
307 /* H3 based SoC's that has an Internal 100MBit PHY
308 * needs to be configured and powered up before use
309 */
Andre Przywara15651d82021-01-11 21:11:45 +0100310 reg &= ~H3_EPHY_DEFAULT_MASK;
311 reg |= H3_EPHY_DEFAULT_VALUE;
312 reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
313 reg &= ~H3_EPHY_SHUTDOWN;
314 return reg | H3_EPHY_SELECT;
315 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530316
Andre Przywara15651d82021-01-11 21:11:45 +0100317 /* This is to select External Gigabit PHY on those boards with
318 * an internal PHY. Does not hurt on other SoCs. Linux does
319 * it as well.
320 */
321 return reg & ~H3_EPHY_SELECT;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530322}
323
Icenowy Zheng525dc442018-11-23 00:37:48 +0100324static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
325 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530326{
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530327 u32 reg;
328
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530329 if (priv->variant == R40_GMAC) {
330 /* Select RGMII for R40 */
331 reg = readl(priv->sysctl_reg + 0x164);
Samuel Holland97f2cf12020-05-07 18:10:50 -0500332 reg |= SC_ETCS_INT_GMII |
333 SC_EPIT |
334 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530335
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530336 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200337 return 0;
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530338 }
339
340 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200341
Andre Przywara15651d82021-01-11 21:11:45 +0100342 reg = sun8i_emac_set_syscon_ephy(priv, reg);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530343
344 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500345 if (priv->variant == H3_EMAC ||
346 priv->variant == A64_EMAC ||
347 priv->variant == H6_EMAC)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530348 reg &= ~SC_RMII_EN;
349
350 switch (priv->interface) {
351 case PHY_INTERFACE_MODE_MII:
352 /* default */
353 break;
354 case PHY_INTERFACE_MODE_RGMII:
Andre Przywara43bb1582020-11-14 17:37:46 +0000355 case PHY_INTERFACE_MODE_RGMII_ID:
356 case PHY_INTERFACE_MODE_RGMII_RXID:
357 case PHY_INTERFACE_MODE_RGMII_TXID:
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530358 reg |= SC_EPIT | SC_ETCS_INT_GMII;
359 break;
360 case PHY_INTERFACE_MODE_RMII:
361 if (priv->variant == H3_EMAC ||
Samuel Holland3386e9a2020-05-07 18:10:51 -0500362 priv->variant == A64_EMAC ||
363 priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530364 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
365 break;
366 }
367 /* RMII not supported on A83T */
368 default:
369 debug("%s: Invalid PHY interface\n", __func__);
370 return -EINVAL;
371 }
372
Icenowy Zheng525dc442018-11-23 00:37:48 +0100373 if (pdata->tx_delay_ps)
374 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
375 & SC_ETXDC_MASK;
376
377 if (pdata->rx_delay_ps)
378 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
379 & SC_ERXDC_MASK;
380
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100381 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530382
383 return 0;
384}
385
386static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
387{
388 struct phy_device *phydev;
389
390 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
391 if (!phydev)
392 return -ENODEV;
393
394 phy_connect_dev(phydev, dev);
395
396 priv->phydev = phydev;
397 phy_config(priv->phydev);
398
399 return 0;
400}
401
Andre Przywara2e7dd262020-07-06 01:40:40 +0100402#define cache_clean_descriptor(desc) \
Wolfgang Denk62fb2b42021-09-27 17:42:39 +0200403 flush_dcache_range((uintptr_t)(desc), \
Andre Przywara2e7dd262020-07-06 01:40:40 +0100404 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
405
406#define cache_inv_descriptor(desc) \
407 invalidate_dcache_range((uintptr_t)(desc), \
408 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
409
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530410static void rx_descs_init(struct emac_eth_dev *priv)
411{
412 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
413 char *rxbuffs = &priv->rxbuffer[0];
414 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100415 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530416
Andre Przywara7408b092020-07-06 01:40:37 +0100417 /*
418 * Make sure we don't have dirty cache lines around, which could
419 * be cleaned to DRAM *after* the MAC has already written data to it.
420 */
421 invalidate_dcache_range((uintptr_t)desc_table_p,
422 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
423 invalidate_dcache_range((uintptr_t)rxbuffs,
424 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530425
Andre Przywara4ab675e2020-07-06 01:40:41 +0100426 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
427 desc_p = &desc_table_p[i];
428 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
429 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywara7408b092020-07-06 01:40:37 +0100430 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100431 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530432 }
433
434 /* Correcting the last pointer of the chain */
435 desc_p->next = (uintptr_t)&desc_table_p[0];
436
437 flush_dcache_range((uintptr_t)priv->rx_chain,
438 (uintptr_t)priv->rx_chain +
439 sizeof(priv->rx_chain));
440
441 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
442 priv->rx_currdescnum = 0;
443}
444
445static void tx_descs_init(struct emac_eth_dev *priv)
446{
447 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
448 char *txbuffs = &priv->txbuffer[0];
449 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100450 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530451
Andre Przywara4ab675e2020-07-06 01:40:41 +0100452 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
453 desc_p = &desc_table_p[i];
454 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
455 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100456 desc_p->ctl_size = 0;
Andre Przywaradf6f2712020-07-06 01:40:33 +0100457 desc_p->status = 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530458 }
459
460 /* Correcting the last pointer of the chain */
461 desc_p->next = (uintptr_t)&desc_table_p[0];
462
Andre Przywara8cd89602020-07-06 01:40:38 +0100463 /* Flush the first TX buffer descriptor we will tell the MAC about. */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100464 cache_clean_descriptor(desc_table_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530465
466 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
467 priv->tx_currdescnum = 0;
468}
469
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530470static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530471{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530472 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara874145f2020-07-06 01:40:32 +0100473 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530474
Andre Przywara6bdc70e2020-07-06 01:40:42 +0100475 /* Soft reset MAC */
476 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
477 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
478 EMAC_CTL1_SOFT_RST, false, 10, true);
479 if (ret) {
480 printf("%s: Timeout\n", __func__);
481 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530482 }
483
484 /* Rewrite mac address after reset */
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530485 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530486
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100487 /* transmission starts after the full frame arrived in TX DMA FIFO */
488 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530489
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100490 /*
491 * RX DMA reads data from RX DMA FIFO to host memory after a
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530492 * complete frame has been written to RX DMA FIFO
493 */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100494 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530495
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100496 /* DMA burst length */
497 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530498
499 /* Initialize rx/tx descriptors */
500 rx_descs_init(priv);
501 tx_descs_init(priv);
502
503 /* PHY Start Up */
Andre Przywara874145f2020-07-06 01:40:32 +0100504 ret = phy_startup(priv->phydev);
505 if (ret)
506 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530507
508 sun8i_adjust_link(priv, priv->phydev);
509
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100510 /* Start RX/TX DMA */
Andre Przywara59422822020-07-06 01:40:43 +0100511 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
512 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100513 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530514
515 /* Enable RX/TX */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100516 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
517 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530518
519 return 0;
520}
521
522static int parse_phy_pins(struct udevice *dev)
523{
524 int offset;
525 const char *pin_name;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100526 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Andre Przywara882a9232021-01-11 21:11:49 +0100527 u32 iomux;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530528
Simon Glassdd79d6e2017-01-17 16:52:55 -0700529 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530530 "pinctrl-0");
531 if (offset < 0) {
532 printf("WARNING: emac: cannot find pinctrl-0 node\n");
533 return offset;
534 }
535
536 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywara26e549b2018-04-04 01:31:15 +0100537 "drive-strength", ~0);
538 if (drive != ~0) {
539 if (drive <= 10)
540 drive = SUN4I_PINCTRL_10_MA;
541 else if (drive <= 20)
542 drive = SUN4I_PINCTRL_20_MA;
543 else if (drive <= 30)
544 drive = SUN4I_PINCTRL_30_MA;
545 else
546 drive = SUN4I_PINCTRL_40_MA;
Andre Przywara26e549b2018-04-04 01:31:15 +0100547 }
548
549 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
550 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywara26e549b2018-04-04 01:31:15 +0100551 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
552 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100553
Andre Przywara882a9232021-01-11 21:11:49 +0100554 /*
555 * The GPIO pinmux value is an integration choice, so depends on the
556 * SoC, not the EMAC variant.
557 */
Andre Przywara9643c832021-04-16 00:53:17 +0100558 if (IS_ENABLED(CONFIG_MACH_SUNXI_H3_H5))
Andre Przywara882a9232021-01-11 21:11:49 +0100559 iomux = SUN8I_IOMUX_H3;
560 else if (IS_ENABLED(CONFIG_MACH_SUN8I_R40))
561 iomux = SUN8I_IOMUX_R40;
562 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
563 iomux = SUN8I_IOMUX_H6;
564 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H616))
565 iomux = SUN8I_IOMUX_H616;
Andre Przywara9643c832021-04-16 00:53:17 +0100566 else if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T))
Andre Przywara882a9232021-01-11 21:11:49 +0100567 iomux = SUN8I_IOMUX;
Andre Przywara9643c832021-04-16 00:53:17 +0100568 else if (IS_ENABLED(CONFIG_MACH_SUN50I))
569 iomux = SUN8I_IOMUX;
570 else
571 BUILD_BUG_ON_MSG(1, "missing pinmux value for Ethernet pins");
Andre Przywara882a9232021-01-11 21:11:49 +0100572
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530573 for (i = 0; ; i++) {
574 int pin;
575
Simon Glassb0ea7402016-10-02 17:59:28 -0600576 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100577 "pins", i, NULL);
578 if (!pin_name)
579 break;
Andre Przywara26e549b2018-04-04 01:31:15 +0100580
581 pin = sunxi_name_to_gpio(pin_name);
582 if (pin < 0)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530583 continue;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530584
Andre Przywara882a9232021-01-11 21:11:49 +0100585 sunxi_gpio_set_cfgpin(pin, iomux);
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200586
Andre Przywara26e549b2018-04-04 01:31:15 +0100587 if (drive != ~0)
588 sunxi_gpio_set_drv(pin, drive);
589 if (pull != ~0)
590 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530591 }
592
593 if (!i) {
Andre Przywara26e549b2018-04-04 01:31:15 +0100594 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530595 return -2;
596 }
597
598 return 0;
599}
600
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530601static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530602{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530603 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530604 u32 status, desc_num = priv->rx_currdescnum;
605 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Andre Przywara59422822020-07-06 01:40:43 +0100606 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
607 int length;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530608
609 /* Invalidate entire buffer descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100610 cache_inv_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530611
612 status = desc_p->status;
613
614 /* Check for DMA own bit */
Andre Przywara59422822020-07-06 01:40:43 +0100615 if (status & EMAC_DESC_OWN_DMA)
616 return -EAGAIN;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530617
Andre Przywara59422822020-07-06 01:40:43 +0100618 length = (status >> 16) & 0x3fff;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530619
Andre Przywara59422822020-07-06 01:40:43 +0100620 /* make sure we read from DRAM, not our cache */
621 invalidate_dcache_range(data_start,
622 data_start + roundup(length, ARCH_DMA_MINALIGN));
623
624 if (status & EMAC_DESC_RX_ERROR_MASK) {
625 debug("RX: packet error: 0x%x\n",
626 status & EMAC_DESC_RX_ERROR_MASK);
627 return 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530628 }
Andre Przywara59422822020-07-06 01:40:43 +0100629 if (length < 0x40) {
630 debug("RX: Bad Packet (runt)\n");
631 return 0;
632 }
633
634 if (length > CONFIG_ETH_RXSIZE) {
635 debug("RX: Too large packet (%d bytes)\n", length);
636 return 0;
637 }
638
639 *packetp = (uchar *)(ulong)desc_p->buf_addr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530640
641 return length;
642}
643
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530644static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530645{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530646 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100647 u32 desc_num = priv->tx_currdescnum;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530648 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530649 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
650 uintptr_t data_end = data_start +
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530651 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530652
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100653 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530654
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530655 memcpy((void *)data_start, packet, length);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530656
657 /* Flush data to be sent */
658 flush_dcache_range(data_start, data_end);
659
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100660 /* frame begin and end */
661 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
662 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530663
Andre Przywara2e7dd262020-07-06 01:40:40 +0100664 /* make sure the MAC reads the actual data from DRAM */
665 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530666
667 /* Move to next Descriptor and wrap around */
668 if (++desc_num >= CONFIG_TX_DESCR_NUM)
669 desc_num = 0;
670 priv->tx_currdescnum = desc_num;
671
672 /* Start the DMA */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100673 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
674
675 /*
676 * Since we copied the data above, we return here without waiting
677 * for the packet to be actually send out.
678 */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530679
680 return 0;
681}
682
Sean Anderson4702aa22020-09-15 10:45:00 -0400683static int sun8i_emac_board_setup(struct udevice *dev,
684 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530685{
Jagan Tekicb63d282019-02-28 00:26:58 +0530686 int ret;
687
688 ret = clk_enable(&priv->tx_clk);
689 if (ret) {
690 dev_err(dev, "failed to enable TX clock\n");
691 return ret;
692 }
693
694 if (reset_valid(&priv->tx_rst)) {
695 ret = reset_deassert(&priv->tx_rst);
696 if (ret) {
697 dev_err(dev, "failed to deassert TX reset\n");
698 goto err_tx_clk;
699 }
700 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530701
Jagan Teki727ed792019-02-28 00:27:00 +0530702 /* Only H3/H5 have clock controls for internal EPHY */
703 if (clk_valid(&priv->ephy_clk)) {
704 ret = clk_enable(&priv->ephy_clk);
705 if (ret) {
706 dev_err(dev, "failed to enable EPHY TX clock\n");
707 return ret;
708 }
709 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530710
Jagan Teki727ed792019-02-28 00:27:00 +0530711 if (reset_valid(&priv->ephy_rst)) {
712 ret = reset_deassert(&priv->ephy_rst);
713 if (ret) {
714 dev_err(dev, "failed to deassert EPHY TX clock\n");
715 return ret;
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200716 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530717 }
718
Jagan Tekicb63d282019-02-28 00:26:58 +0530719 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200720
Jagan Tekicb63d282019-02-28 00:26:58 +0530721err_tx_clk:
722 clk_disable(&priv->tx_clk);
723 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530724}
725
Simon Glassfa4689a2019-12-06 21:41:35 -0700726#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100727static int sun8i_mdio_reset(struct mii_dev *bus)
728{
729 struct udevice *dev = bus->priv;
730 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700731 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100732 int ret;
733
734 if (!dm_gpio_is_valid(&priv->reset_gpio))
735 return 0;
736
737 /* reset the phy */
738 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
739 if (ret)
740 return ret;
741
742 udelay(pdata->reset_delays[0]);
743
744 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
745 if (ret)
746 return ret;
747
748 udelay(pdata->reset_delays[1]);
749
750 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
751 if (ret)
752 return ret;
753
754 udelay(pdata->reset_delays[2]);
755
756 return 0;
757}
758#endif
759
760static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530761{
762 struct mii_dev *bus = mdio_alloc();
763
764 if (!bus) {
765 debug("Failed to allocate MDIO bus\n");
766 return -ENOMEM;
767 }
768
769 bus->read = sun8i_mdio_read;
770 bus->write = sun8i_mdio_write;
771 snprintf(bus->name, sizeof(bus->name), name);
772 bus->priv = (void *)priv;
Simon Glassfa4689a2019-12-06 21:41:35 -0700773#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100774 bus->reset = sun8i_mdio_reset;
775#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530776
777 return mdio_register(bus);
778}
779
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530780static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
781 int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530782{
783 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530784 u32 desc_num = priv->rx_currdescnum;
785 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530786
Andre Przywara2e7dd262020-07-06 01:40:40 +0100787 /* give the current descriptor back to the MAC */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100788 desc_p->status |= EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530789
790 /* Flush Status field of descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100791 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530792
793 /* Move to next desc and wrap-around condition. */
794 if (++desc_num >= CONFIG_RX_DESCR_NUM)
795 desc_num = 0;
796 priv->rx_currdescnum = desc_num;
797
798 return 0;
799}
800
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530801static void sun8i_emac_eth_stop(struct udevice *dev)
802{
803 struct emac_eth_dev *priv = dev_get_priv(dev);
804
805 /* Stop Rx/Tx transmitter */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100806 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
807 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530808
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100809 /* Stop RX/TX DMA */
810 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
811 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530812
813 phy_shutdown(priv->phydev);
814}
815
816static int sun8i_emac_eth_probe(struct udevice *dev)
817{
Simon Glassfa20e932020-12-03 16:55:20 -0700818 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Icenowy Zheng525dc442018-11-23 00:37:48 +0100819 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530820 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530821 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530822
823 priv->mac_reg = (void *)pdata->iobase;
824
Sean Anderson4702aa22020-09-15 10:45:00 -0400825 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekicb63d282019-02-28 00:26:58 +0530826 if (ret)
827 return ret;
828
Icenowy Zheng525dc442018-11-23 00:37:48 +0100829 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530830
Philipp Tomsich3297b552017-02-22 19:46:41 +0100831 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530832 priv->bus = miiphy_get_dev_by_name(dev->name);
833
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530834 return sun8i_phy_init(priv, dev);
835}
836
837static const struct eth_ops sun8i_emac_eth_ops = {
838 .start = sun8i_emac_eth_start,
839 .write_hwaddr = sun8i_eth_write_hwaddr,
840 .send = sun8i_emac_eth_send,
841 .recv = sun8i_emac_eth_recv,
842 .free_pkt = sun8i_eth_free_pkt,
843 .stop = sun8i_emac_eth_stop,
844};
845
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530846static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki727ed792019-02-28 00:27:00 +0530847{
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530848 struct ofnode_phandle_args phandle;
849 int ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530850
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530851 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
852 NULL, 0, 0, &phandle);
853 if (ret)
854 return ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530855
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530856 /* If the PHY node is not a child of the internal MDIO bus, we are
857 * using some external PHY.
858 */
859 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
860 "allwinner,sun8i-h3-mdio-internal"))
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200861 return 0;
862
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530863 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
Jagan Teki727ed792019-02-28 00:27:00 +0530864 if (ret) {
865 dev_err(dev, "failed to get EPHY TX clock\n");
866 return ret;
867 }
868
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530869 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
Jagan Teki727ed792019-02-28 00:27:00 +0530870 if (ret) {
871 dev_err(dev, "failed to get EPHY TX reset\n");
872 return ret;
873 }
874
875 priv->use_internal_phy = true;
876
877 return 0;
878}
879
Simon Glassaad29ae2020-12-03 16:55:21 -0700880static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530881{
Simon Glassfa20e932020-12-03 16:55:20 -0700882 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100883 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530884 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100885 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700886 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530887 int offset = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -0700888#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100889 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100890#endif
Jagan Tekicb63d282019-02-28 00:26:58 +0530891 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530892
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900893 pdata->iobase = dev_read_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100894 if (pdata->iobase == FDT_ADDR_T_NONE) {
895 debug("%s: Cannot find MAC base address\n", __func__);
896 return -EINVAL;
897 }
898
Lothar Feltene8cbced2018-07-13 10:45:28 +0200899 priv->variant = dev_get_driver_data(dev);
900
901 if (!priv->variant) {
902 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100903 return -EINVAL;
904 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200905
Jagan Tekicb63d282019-02-28 00:26:58 +0530906 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
907 if (ret) {
908 dev_err(dev, "failed to get TX clock\n");
909 return ret;
910 }
911
912 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
913 if (ret && ret != -ENOENT) {
914 dev_err(dev, "failed to get TX reset\n");
915 return ret;
916 }
917
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530918 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
919 if (offset < 0) {
920 debug("%s: cannot find syscon node\n", __func__);
921 return -EINVAL;
922 }
923
924 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
925 if (!reg) {
926 debug("%s: cannot find reg property in syscon node\n",
927 __func__);
928 return -EINVAL;
929 }
930 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
931 offset, reg);
932 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
933 debug("%s: Cannot find syscon base address\n", __func__);
934 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100935 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530936
937 pdata->phy_interface = -1;
938 priv->phyaddr = -1;
939 priv->use_internal_phy = false;
940
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100941 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100942 if (offset < 0) {
943 debug("%s: Cannot find PHY address\n", __func__);
944 return -EINVAL;
945 }
946 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530947
Marek BehĂșnbc194772022-04-07 00:33:01 +0200948 pdata->phy_interface = dev_read_phy_mode(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530949 printf("phy interface%d\n", pdata->phy_interface);
Marek BehĂșn48631e42022-04-07 00:33:03 +0200950 if (pdata->phy_interface == PHY_INTERFACE_MODE_NA)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530951 return -EINVAL;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530952
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530953 if (priv->variant == H3_EMAC) {
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530954 ret = sun8i_handle_internal_phy(dev, priv);
Jagan Teki727ed792019-02-28 00:27:00 +0530955 if (ret)
956 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530957 }
958
959 priv->interface = pdata->phy_interface;
960
961 if (!priv->use_internal_phy)
962 parse_phy_pins(dev);
963
Icenowy Zheng525dc442018-11-23 00:37:48 +0100964 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
965 "allwinner,tx-delay-ps", 0);
966 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
967 printf("%s: Invalid TX delay value %d\n", __func__,
968 sun8i_pdata->tx_delay_ps);
969
970 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
971 "allwinner,rx-delay-ps", 0);
972 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
973 printf("%s: Invalid RX delay value %d\n", __func__,
974 sun8i_pdata->rx_delay_ps);
975
Simon Glassfa4689a2019-12-06 21:41:35 -0700976#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass7a494432017-05-17 17:18:09 -0600977 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100978 "snps,reset-active-low"))
979 reset_flags |= GPIOD_ACTIVE_LOW;
980
981 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
982 &priv->reset_gpio, reset_flags);
983
984 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600985 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100986 "snps,reset-delays-us",
987 sun8i_pdata->reset_delays, 3);
988 } else if (ret == -ENOENT) {
989 ret = 0;
990 }
991#endif
992
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530993 return 0;
994}
995
996static const struct udevice_id sun8i_emac_eth_ids[] = {
997 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
998 {.compatible = "allwinner,sun50i-a64-emac",
999 .data = (uintptr_t)A64_EMAC },
1000 {.compatible = "allwinner,sun8i-a83t-emac",
1001 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene8cbced2018-07-13 10:45:28 +02001002 {.compatible = "allwinner,sun8i-r40-gmac",
1003 .data = (uintptr_t)R40_GMAC },
Samuel Holland3386e9a2020-05-07 18:10:51 -05001004 {.compatible = "allwinner,sun50i-h6-emac",
1005 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301006 { }
1007};
1008
1009U_BOOT_DRIVER(eth_sun8i_emac) = {
1010 .name = "eth_sun8i_emac",
1011 .id = UCLASS_ETH,
1012 .of_match = sun8i_emac_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001013 .of_to_plat = sun8i_emac_eth_of_to_plat,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301014 .probe = sun8i_emac_eth_probe,
1015 .ops = &sun8i_emac_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001016 .priv_auto = sizeof(struct emac_eth_dev),
Simon Glass71fa5b42020-12-03 16:55:18 -07001017 .plat_auto = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301018 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1019};