blob: 0f6b6bb537e0955d30900e45bf9f38c0fb3c0a0d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05302/*
3 * (C) Copyright 2016
4 * Author: Amit Singh Tomar, amittomer25@gmail.com
5 *
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05306 * Ethernet driver for H3/A64/A83T based SoC's
7 *
8 * It is derived from the work done by
9 * LABBE Corentin & Chen-Yu Tsai for Linux, THANKS!
10 *
11*/
12
Simon Glass63334482019-11-14 12:57:39 -070013#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <asm/cache.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053016#include <asm/io.h>
17#include <asm/arch/clock.h>
18#include <asm/arch/gpio.h>
19#include <common.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053020#include <clk.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053021#include <dm.h>
22#include <fdt_support.h>
Simon Glass9bc15642020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060024#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060025#include <linux/delay.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053026#include <linux/err.h>
27#include <malloc.h>
28#include <miiphy.h>
29#include <net.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053030#include <reset.h>
Andre Przywara26e549b2018-04-04 01:31:15 +010031#include <dt-bindings/pinctrl/sun4i-a10.h>
Andre Przywara0dd619b2020-07-06 01:40:34 +010032#include <wait_bit.h>
Simon Glassfa4689a2019-12-06 21:41:35 -070033#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +010034#include <asm-generic/gpio.h>
35#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053036
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053037#define MDIO_CMD_MII_BUSY BIT(0)
38#define MDIO_CMD_MII_WRITE BIT(1)
39
40#define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
41#define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
42#define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
43#define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
Andre Przywarab41f2472020-07-06 01:40:45 +010044#define MDIO_CMD_MII_CLK_CSR_DIV_16 0x0
45#define MDIO_CMD_MII_CLK_CSR_DIV_32 0x1
46#define MDIO_CMD_MII_CLK_CSR_DIV_64 0x2
47#define MDIO_CMD_MII_CLK_CSR_DIV_128 0x3
48#define MDIO_CMD_MII_CLK_CSR_SHIFT 20
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053049
50#define CONFIG_TX_DESCR_NUM 32
51#define CONFIG_RX_DESCR_NUM 32
Hans de Goedefcdb3b32016-07-27 17:31:17 +020052#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
53
54/*
55 * The datasheet says that each descriptor can transfers up to 4096 bytes
56 * But later, the register documentation reduces that value to 2048,
57 * using 2048 cause strange behaviours and even BSP driver use 2047
58 */
59#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053060
61#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
62#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
63
64#define H3_EPHY_DEFAULT_VALUE 0x58000
65#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
66#define H3_EPHY_ADDR_SHIFT 20
67#define REG_PHY_ADDR_MASK GENMASK(4, 0)
68#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
69#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
70#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
71
72#define SC_RMII_EN BIT(13)
73#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
74#define SC_ETCS_MASK GENMASK(1, 0)
75#define SC_ETCS_EXT_GMII 0x1
76#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng525dc442018-11-23 00:37:48 +010077#define SC_ETXDC_MASK GENMASK(12, 10)
78#define SC_ETXDC_OFFSET 10
79#define SC_ERXDC_MASK GENMASK(9, 5)
80#define SC_ERXDC_OFFSET 5
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053081
82#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
83
84#define AHB_GATE_OFFSET_EPHY 0
85
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020086/* IO mux settings */
87#define SUN8I_IOMUX_H3 2
Andre Przywara882a9232021-01-11 21:11:49 +010088#define SUN8I_IOMUX_R40 5
89#define SUN8I_IOMUX_H6 5
90#define SUN8I_IOMUX_H616 2
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020091#define SUN8I_IOMUX 4
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053092
93/* H3/A64 EMAC Register's offset */
94#define EMAC_CTL0 0x00
Andre Przywarae6e29cc2020-07-06 01:40:36 +010095#define EMAC_CTL0_FULL_DUPLEX BIT(0)
96#define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
97#define EMAC_CTL0_SPEED_10 (0x2 << 2)
98#define EMAC_CTL0_SPEED_100 (0x3 << 2)
99#define EMAC_CTL0_SPEED_1000 (0x0 << 2)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530100#define EMAC_CTL1 0x04
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100101#define EMAC_CTL1_SOFT_RST BIT(0)
102#define EMAC_CTL1_BURST_LEN_SHIFT 24
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530103#define EMAC_INT_STA 0x08
104#define EMAC_INT_EN 0x0c
105#define EMAC_TX_CTL0 0x10
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100106#define EMAC_TX_CTL0_TX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530107#define EMAC_TX_CTL1 0x14
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100108#define EMAC_TX_CTL1_TX_MD BIT(1)
109#define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
110#define EMAC_TX_CTL1_TX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530111#define EMAC_TX_FLOW_CTL 0x1c
112#define EMAC_TX_DMA_DESC 0x20
113#define EMAC_RX_CTL0 0x24
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100114#define EMAC_RX_CTL0_RX_EN BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530115#define EMAC_RX_CTL1 0x28
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100116#define EMAC_RX_CTL1_RX_MD BIT(1)
Andre Przywara59422822020-07-06 01:40:43 +0100117#define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
118#define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100119#define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
120#define EMAC_RX_CTL1_RX_DMA_START BIT(31)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530121#define EMAC_RX_DMA_DESC 0x34
122#define EMAC_MII_CMD 0x48
123#define EMAC_MII_DATA 0x4c
124#define EMAC_ADDR0_HIGH 0x50
125#define EMAC_ADDR0_LOW 0x54
126#define EMAC_TX_DMA_STA 0xb0
127#define EMAC_TX_CUR_DESC 0xb4
128#define EMAC_TX_CUR_BUF 0xb8
129#define EMAC_RX_DMA_STA 0xc0
130#define EMAC_RX_CUR_DESC 0xc4
131
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100132#define EMAC_DESC_OWN_DMA BIT(31)
133#define EMAC_DESC_LAST_DESC BIT(30)
134#define EMAC_DESC_FIRST_DESC BIT(29)
135#define EMAC_DESC_CHAIN_SECOND BIT(24)
136
Andre Przywara59422822020-07-06 01:40:43 +0100137#define EMAC_DESC_RX_ERROR_MASK 0x400068db
138
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530139DECLARE_GLOBAL_DATA_PTR;
140
141enum emac_variant {
142 A83T_EMAC = 1,
143 H3_EMAC,
144 A64_EMAC,
Lothar Feltene8cbced2018-07-13 10:45:28 +0200145 R40_GMAC,
Samuel Holland3386e9a2020-05-07 18:10:51 -0500146 H6_EMAC,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530147};
148
149struct emac_dma_desc {
150 u32 status;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100151 u32 ctl_size;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530152 u32 buf_addr;
153 u32 next;
154} __aligned(ARCH_DMA_MINALIGN);
155
156struct emac_eth_dev {
157 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
158 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
159 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
160 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
161
162 u32 interface;
163 u32 phyaddr;
164 u32 link;
165 u32 speed;
166 u32 duplex;
167 u32 phy_configured;
168 u32 tx_currdescnum;
169 u32 rx_currdescnum;
170 u32 addr;
171 u32 tx_slot;
172 bool use_internal_phy;
173
174 enum emac_variant variant;
175 void *mac_reg;
176 phys_addr_t sysctl_reg;
177 struct phy_device *phydev;
178 struct mii_dev *bus;
Jagan Tekicb63d282019-02-28 00:26:58 +0530179 struct clk tx_clk;
Jagan Teki727ed792019-02-28 00:27:00 +0530180 struct clk ephy_clk;
Jagan Tekicb63d282019-02-28 00:26:58 +0530181 struct reset_ctl tx_rst;
Jagan Teki727ed792019-02-28 00:27:00 +0530182 struct reset_ctl ephy_rst;
Simon Glassfa4689a2019-12-06 21:41:35 -0700183#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100184 struct gpio_desc reset_gpio;
185#endif
186};
187
188
189struct sun8i_eth_pdata {
190 struct eth_pdata eth_pdata;
191 u32 reset_delays[3];
Icenowy Zheng525dc442018-11-23 00:37:48 +0100192 int tx_delay_ps;
193 int rx_delay_ps;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530194};
195
Philipp Tomsich3297b552017-02-22 19:46:41 +0100196
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530197static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
198{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100199 struct udevice *dev = bus->priv;
200 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100201 u32 mii_cmd;
202 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530203
Andre Przywara0dd619b2020-07-06 01:40:34 +0100204 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530205 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
Andre Przywara0dd619b2020-07-06 01:40:34 +0100206 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530207 MDIO_CMD_MII_PHY_ADDR_MASK;
208
Andre Przywarab41f2472020-07-06 01:40:45 +0100209 /*
210 * The EMAC clock is either 200 or 300 MHz, so we need a divider
211 * of 128 to get the MDIO frequency below the required 2.5 MHz.
212 */
213 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 << MDIO_CMD_MII_CLK_CSR_SHIFT;
214
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 */
244 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 << MDIO_CMD_MII_CLK_CSR_SHIFT;
245
Andre Przywara0dd619b2020-07-06 01:40:34 +0100246 mii_cmd |= MDIO_CMD_MII_WRITE;
247 mii_cmd |= MDIO_CMD_MII_BUSY;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530248
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530249 writel(val, priv->mac_reg + EMAC_MII_DATA);
Andre Przywara0dd619b2020-07-06 01:40:34 +0100250 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530251
Andre Przywara0dd619b2020-07-06 01:40:34 +0100252 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
253 MDIO_CMD_MII_BUSY, false,
254 CONFIG_MDIO_TIMEOUT, true);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530255}
256
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530257static int sun8i_eth_write_hwaddr(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530258{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530259 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700260 struct eth_pdata *pdata = dev_get_plat(dev);
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530261 uchar *mac_id = pdata->enetaddr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530262 u32 macid_lo, macid_hi;
263
264 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
265 (mac_id[3] << 24);
266 macid_hi = mac_id[4] + (mac_id[5] << 8);
267
268 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
269 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
270
271 return 0;
272}
273
274static void sun8i_adjust_link(struct emac_eth_dev *priv,
275 struct phy_device *phydev)
276{
277 u32 v;
278
279 v = readl(priv->mac_reg + EMAC_CTL0);
280
281 if (phydev->duplex)
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100282 v |= EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530283 else
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100284 v &= ~EMAC_CTL0_FULL_DUPLEX;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530285
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100286 v &= ~EMAC_CTL0_SPEED_MASK;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530287
288 switch (phydev->speed) {
289 case 1000:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100290 v |= EMAC_CTL0_SPEED_1000;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530291 break;
292 case 100:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100293 v |= EMAC_CTL0_SPEED_100;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530294 break;
295 case 10:
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100296 v |= EMAC_CTL0_SPEED_10;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530297 break;
298 }
299 writel(v, priv->mac_reg + EMAC_CTL0);
300}
301
Andre Przywara15651d82021-01-11 21:11:45 +0100302static u32 sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 reg)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530303{
304 if (priv->use_internal_phy) {
305 /* H3 based SoC's that has an Internal 100MBit PHY
306 * needs to be configured and powered up before use
307 */
Andre Przywara15651d82021-01-11 21:11:45 +0100308 reg &= ~H3_EPHY_DEFAULT_MASK;
309 reg |= H3_EPHY_DEFAULT_VALUE;
310 reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
311 reg &= ~H3_EPHY_SHUTDOWN;
312 return reg | H3_EPHY_SELECT;
313 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530314
Andre Przywara15651d82021-01-11 21:11:45 +0100315 /* This is to select External Gigabit PHY on those boards with
316 * an internal PHY. Does not hurt on other SoCs. Linux does
317 * it as well.
318 */
319 return reg & ~H3_EPHY_SELECT;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530320}
321
Icenowy Zheng525dc442018-11-23 00:37:48 +0100322static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
323 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530324{
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530325 u32 reg;
326
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530327 if (priv->variant == R40_GMAC) {
328 /* Select RGMII for R40 */
329 reg = readl(priv->sysctl_reg + 0x164);
Samuel Holland97f2cf12020-05-07 18:10:50 -0500330 reg |= SC_ETCS_INT_GMII |
331 SC_EPIT |
332 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530333
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530334 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200335 return 0;
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530336 }
337
338 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200339
Andre Przywara15651d82021-01-11 21:11:45 +0100340 reg = sun8i_emac_set_syscon_ephy(priv, reg);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530341
342 reg &= ~(SC_ETCS_MASK | SC_EPIT);
Samuel Holland3386e9a2020-05-07 18:10:51 -0500343 if (priv->variant == H3_EMAC ||
344 priv->variant == A64_EMAC ||
345 priv->variant == H6_EMAC)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530346 reg &= ~SC_RMII_EN;
347
348 switch (priv->interface) {
349 case PHY_INTERFACE_MODE_MII:
350 /* default */
351 break;
352 case PHY_INTERFACE_MODE_RGMII:
Andre Przywara43bb1582020-11-14 17:37:46 +0000353 case PHY_INTERFACE_MODE_RGMII_ID:
354 case PHY_INTERFACE_MODE_RGMII_RXID:
355 case PHY_INTERFACE_MODE_RGMII_TXID:
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530356 reg |= SC_EPIT | SC_ETCS_INT_GMII;
357 break;
358 case PHY_INTERFACE_MODE_RMII:
359 if (priv->variant == H3_EMAC ||
Samuel Holland3386e9a2020-05-07 18:10:51 -0500360 priv->variant == A64_EMAC ||
361 priv->variant == H6_EMAC) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530362 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
363 break;
364 }
365 /* RMII not supported on A83T */
366 default:
367 debug("%s: Invalid PHY interface\n", __func__);
368 return -EINVAL;
369 }
370
Icenowy Zheng525dc442018-11-23 00:37:48 +0100371 if (pdata->tx_delay_ps)
372 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
373 & SC_ETXDC_MASK;
374
375 if (pdata->rx_delay_ps)
376 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
377 & SC_ERXDC_MASK;
378
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100379 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530380
381 return 0;
382}
383
384static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
385{
386 struct phy_device *phydev;
387
388 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
389 if (!phydev)
390 return -ENODEV;
391
392 phy_connect_dev(phydev, dev);
393
394 priv->phydev = phydev;
395 phy_config(priv->phydev);
396
397 return 0;
398}
399
Andre Przywara2e7dd262020-07-06 01:40:40 +0100400#define cache_clean_descriptor(desc) \
401 flush_dcache_range((uintptr_t)(desc), \
402 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
403
404#define cache_inv_descriptor(desc) \
405 invalidate_dcache_range((uintptr_t)(desc), \
406 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
407
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530408static void rx_descs_init(struct emac_eth_dev *priv)
409{
410 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
411 char *rxbuffs = &priv->rxbuffer[0];
412 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100413 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530414
Andre Przywara7408b092020-07-06 01:40:37 +0100415 /*
416 * Make sure we don't have dirty cache lines around, which could
417 * be cleaned to DRAM *after* the MAC has already written data to it.
418 */
419 invalidate_dcache_range((uintptr_t)desc_table_p,
420 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
421 invalidate_dcache_range((uintptr_t)rxbuffs,
422 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530423
Andre Przywara4ab675e2020-07-06 01:40:41 +0100424 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
425 desc_p = &desc_table_p[i];
426 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
427 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywara7408b092020-07-06 01:40:37 +0100428 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100429 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530430 }
431
432 /* Correcting the last pointer of the chain */
433 desc_p->next = (uintptr_t)&desc_table_p[0];
434
435 flush_dcache_range((uintptr_t)priv->rx_chain,
436 (uintptr_t)priv->rx_chain +
437 sizeof(priv->rx_chain));
438
439 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
440 priv->rx_currdescnum = 0;
441}
442
443static void tx_descs_init(struct emac_eth_dev *priv)
444{
445 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
446 char *txbuffs = &priv->txbuffer[0];
447 struct emac_dma_desc *desc_p;
Andre Przywara4ab675e2020-07-06 01:40:41 +0100448 int i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530449
Andre Przywara4ab675e2020-07-06 01:40:41 +0100450 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
451 desc_p = &desc_table_p[i];
452 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
453 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100454 desc_p->ctl_size = 0;
Andre Przywaradf6f2712020-07-06 01:40:33 +0100455 desc_p->status = 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530456 }
457
458 /* Correcting the last pointer of the chain */
459 desc_p->next = (uintptr_t)&desc_table_p[0];
460
Andre Przywara8cd89602020-07-06 01:40:38 +0100461 /* Flush the first TX buffer descriptor we will tell the MAC about. */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100462 cache_clean_descriptor(desc_table_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530463
464 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
465 priv->tx_currdescnum = 0;
466}
467
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530468static int sun8i_emac_eth_start(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530469{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530470 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywara874145f2020-07-06 01:40:32 +0100471 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530472
Andre Przywara6bdc70e2020-07-06 01:40:42 +0100473 /* Soft reset MAC */
474 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
475 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
476 EMAC_CTL1_SOFT_RST, false, 10, true);
477 if (ret) {
478 printf("%s: Timeout\n", __func__);
479 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530480 }
481
482 /* Rewrite mac address after reset */
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530483 sun8i_eth_write_hwaddr(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530484
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100485 /* transmission starts after the full frame arrived in TX DMA FIFO */
486 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530487
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100488 /*
489 * RX DMA reads data from RX DMA FIFO to host memory after a
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530490 * complete frame has been written to RX DMA FIFO
491 */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100492 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530493
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100494 /* DMA burst length */
495 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530496
497 /* Initialize rx/tx descriptors */
498 rx_descs_init(priv);
499 tx_descs_init(priv);
500
501 /* PHY Start Up */
Andre Przywara874145f2020-07-06 01:40:32 +0100502 ret = phy_startup(priv->phydev);
503 if (ret)
504 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530505
506 sun8i_adjust_link(priv, priv->phydev);
507
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100508 /* Start RX/TX DMA */
Andre Przywara59422822020-07-06 01:40:43 +0100509 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
510 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100511 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530512
513 /* Enable RX/TX */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100514 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
515 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530516
517 return 0;
518}
519
520static int parse_phy_pins(struct udevice *dev)
521{
522 int offset;
523 const char *pin_name;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100524 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Andre Przywara882a9232021-01-11 21:11:49 +0100525 u32 iomux;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530526
Simon Glassdd79d6e2017-01-17 16:52:55 -0700527 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530528 "pinctrl-0");
529 if (offset < 0) {
530 printf("WARNING: emac: cannot find pinctrl-0 node\n");
531 return offset;
532 }
533
534 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywara26e549b2018-04-04 01:31:15 +0100535 "drive-strength", ~0);
536 if (drive != ~0) {
537 if (drive <= 10)
538 drive = SUN4I_PINCTRL_10_MA;
539 else if (drive <= 20)
540 drive = SUN4I_PINCTRL_20_MA;
541 else if (drive <= 30)
542 drive = SUN4I_PINCTRL_30_MA;
543 else
544 drive = SUN4I_PINCTRL_40_MA;
Andre Przywara26e549b2018-04-04 01:31:15 +0100545 }
546
547 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
548 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywara26e549b2018-04-04 01:31:15 +0100549 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
550 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100551
Andre Przywara882a9232021-01-11 21:11:49 +0100552 /*
553 * The GPIO pinmux value is an integration choice, so depends on the
554 * SoC, not the EMAC variant.
555 */
556 if (IS_ENABLED(CONFIG_MACH_SUN8I_H3))
557 iomux = SUN8I_IOMUX_H3;
558 else if (IS_ENABLED(CONFIG_MACH_SUN8I_R40))
559 iomux = SUN8I_IOMUX_R40;
560 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
561 iomux = SUN8I_IOMUX_H6;
562 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H616))
563 iomux = SUN8I_IOMUX_H616;
564 else
565 iomux = SUN8I_IOMUX;
566
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530567 for (i = 0; ; i++) {
568 int pin;
569
Simon Glassb0ea7402016-10-02 17:59:28 -0600570 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100571 "pins", i, NULL);
572 if (!pin_name)
573 break;
Andre Przywara26e549b2018-04-04 01:31:15 +0100574
575 pin = sunxi_name_to_gpio(pin_name);
576 if (pin < 0)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530577 continue;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530578
Andre Przywara882a9232021-01-11 21:11:49 +0100579 sunxi_gpio_set_cfgpin(pin, iomux);
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200580
Andre Przywara26e549b2018-04-04 01:31:15 +0100581 if (drive != ~0)
582 sunxi_gpio_set_drv(pin, drive);
583 if (pull != ~0)
584 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530585 }
586
587 if (!i) {
Andre Przywara26e549b2018-04-04 01:31:15 +0100588 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530589 return -2;
590 }
591
592 return 0;
593}
594
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530595static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530596{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530597 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530598 u32 status, desc_num = priv->rx_currdescnum;
599 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Andre Przywara59422822020-07-06 01:40:43 +0100600 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
601 int length;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530602
603 /* Invalidate entire buffer descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100604 cache_inv_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530605
606 status = desc_p->status;
607
608 /* Check for DMA own bit */
Andre Przywara59422822020-07-06 01:40:43 +0100609 if (status & EMAC_DESC_OWN_DMA)
610 return -EAGAIN;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530611
Andre Przywara59422822020-07-06 01:40:43 +0100612 length = (status >> 16) & 0x3fff;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530613
Andre Przywara59422822020-07-06 01:40:43 +0100614 /* make sure we read from DRAM, not our cache */
615 invalidate_dcache_range(data_start,
616 data_start + roundup(length, ARCH_DMA_MINALIGN));
617
618 if (status & EMAC_DESC_RX_ERROR_MASK) {
619 debug("RX: packet error: 0x%x\n",
620 status & EMAC_DESC_RX_ERROR_MASK);
621 return 0;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530622 }
Andre Przywara59422822020-07-06 01:40:43 +0100623 if (length < 0x40) {
624 debug("RX: Bad Packet (runt)\n");
625 return 0;
626 }
627
628 if (length > CONFIG_ETH_RXSIZE) {
629 debug("RX: Too large packet (%d bytes)\n", length);
630 return 0;
631 }
632
633 *packetp = (uchar *)(ulong)desc_p->buf_addr;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530634
635 return length;
636}
637
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530638static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530639{
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530640 struct emac_eth_dev *priv = dev_get_priv(dev);
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100641 u32 desc_num = priv->tx_currdescnum;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530642 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530643 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
644 uintptr_t data_end = data_start +
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530645 roundup(length, ARCH_DMA_MINALIGN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530646
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100647 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530648
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530649 memcpy((void *)data_start, packet, length);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530650
651 /* Flush data to be sent */
652 flush_dcache_range(data_start, data_end);
653
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100654 /* frame begin and end */
655 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
656 desc_p->status = EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530657
Andre Przywara2e7dd262020-07-06 01:40:40 +0100658 /* make sure the MAC reads the actual data from DRAM */
659 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530660
661 /* Move to next Descriptor and wrap around */
662 if (++desc_num >= CONFIG_TX_DESCR_NUM)
663 desc_num = 0;
664 priv->tx_currdescnum = desc_num;
665
666 /* Start the DMA */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100667 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
668
669 /*
670 * Since we copied the data above, we return here without waiting
671 * for the packet to be actually send out.
672 */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530673
674 return 0;
675}
676
Sean Anderson4702aa22020-09-15 10:45:00 -0400677static int sun8i_emac_board_setup(struct udevice *dev,
678 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530679{
Jagan Tekicb63d282019-02-28 00:26:58 +0530680 int ret;
681
682 ret = clk_enable(&priv->tx_clk);
683 if (ret) {
684 dev_err(dev, "failed to enable TX clock\n");
685 return ret;
686 }
687
688 if (reset_valid(&priv->tx_rst)) {
689 ret = reset_deassert(&priv->tx_rst);
690 if (ret) {
691 dev_err(dev, "failed to deassert TX reset\n");
692 goto err_tx_clk;
693 }
694 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530695
Jagan Teki727ed792019-02-28 00:27:00 +0530696 /* Only H3/H5 have clock controls for internal EPHY */
697 if (clk_valid(&priv->ephy_clk)) {
698 ret = clk_enable(&priv->ephy_clk);
699 if (ret) {
700 dev_err(dev, "failed to enable EPHY TX clock\n");
701 return ret;
702 }
703 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530704
Jagan Teki727ed792019-02-28 00:27:00 +0530705 if (reset_valid(&priv->ephy_rst)) {
706 ret = reset_deassert(&priv->ephy_rst);
707 if (ret) {
708 dev_err(dev, "failed to deassert EPHY TX clock\n");
709 return ret;
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200710 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530711 }
712
Jagan Tekicb63d282019-02-28 00:26:58 +0530713 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200714
Jagan Tekicb63d282019-02-28 00:26:58 +0530715err_tx_clk:
716 clk_disable(&priv->tx_clk);
717 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530718}
719
Simon Glassfa4689a2019-12-06 21:41:35 -0700720#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100721static int sun8i_mdio_reset(struct mii_dev *bus)
722{
723 struct udevice *dev = bus->priv;
724 struct emac_eth_dev *priv = dev_get_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700725 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100726 int ret;
727
728 if (!dm_gpio_is_valid(&priv->reset_gpio))
729 return 0;
730
731 /* reset the phy */
732 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
733 if (ret)
734 return ret;
735
736 udelay(pdata->reset_delays[0]);
737
738 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
739 if (ret)
740 return ret;
741
742 udelay(pdata->reset_delays[1]);
743
744 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
745 if (ret)
746 return ret;
747
748 udelay(pdata->reset_delays[2]);
749
750 return 0;
751}
752#endif
753
754static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530755{
756 struct mii_dev *bus = mdio_alloc();
757
758 if (!bus) {
759 debug("Failed to allocate MDIO bus\n");
760 return -ENOMEM;
761 }
762
763 bus->read = sun8i_mdio_read;
764 bus->write = sun8i_mdio_write;
765 snprintf(bus->name, sizeof(bus->name), name);
766 bus->priv = (void *)priv;
Simon Glassfa4689a2019-12-06 21:41:35 -0700767#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100768 bus->reset = sun8i_mdio_reset;
769#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530770
771 return mdio_register(bus);
772}
773
Andre Przywaraf58d83c2020-10-21 23:21:42 +0530774static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
775 int length)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530776{
777 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530778 u32 desc_num = priv->rx_currdescnum;
779 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530780
Andre Przywara2e7dd262020-07-06 01:40:40 +0100781 /* give the current descriptor back to the MAC */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100782 desc_p->status |= EMAC_DESC_OWN_DMA;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530783
784 /* Flush Status field of descriptor */
Andre Przywara2e7dd262020-07-06 01:40:40 +0100785 cache_clean_descriptor(desc_p);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530786
787 /* Move to next desc and wrap-around condition. */
788 if (++desc_num >= CONFIG_RX_DESCR_NUM)
789 desc_num = 0;
790 priv->rx_currdescnum = desc_num;
791
792 return 0;
793}
794
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530795static void sun8i_emac_eth_stop(struct udevice *dev)
796{
797 struct emac_eth_dev *priv = dev_get_priv(dev);
798
799 /* Stop Rx/Tx transmitter */
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100800 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
801 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530802
Andre Przywarae6e29cc2020-07-06 01:40:36 +0100803 /* Stop RX/TX DMA */
804 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
805 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530806
807 phy_shutdown(priv->phydev);
808}
809
810static int sun8i_emac_eth_probe(struct udevice *dev)
811{
Simon Glassfa20e932020-12-03 16:55:20 -0700812 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Icenowy Zheng525dc442018-11-23 00:37:48 +0100813 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530814 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530815 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530816
817 priv->mac_reg = (void *)pdata->iobase;
818
Sean Anderson4702aa22020-09-15 10:45:00 -0400819 ret = sun8i_emac_board_setup(dev, priv);
Jagan Tekicb63d282019-02-28 00:26:58 +0530820 if (ret)
821 return ret;
822
Icenowy Zheng525dc442018-11-23 00:37:48 +0100823 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530824
Philipp Tomsich3297b552017-02-22 19:46:41 +0100825 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530826 priv->bus = miiphy_get_dev_by_name(dev->name);
827
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530828 return sun8i_phy_init(priv, dev);
829}
830
831static const struct eth_ops sun8i_emac_eth_ops = {
832 .start = sun8i_emac_eth_start,
833 .write_hwaddr = sun8i_eth_write_hwaddr,
834 .send = sun8i_emac_eth_send,
835 .recv = sun8i_emac_eth_recv,
836 .free_pkt = sun8i_eth_free_pkt,
837 .stop = sun8i_emac_eth_stop,
838};
839
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530840static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
Jagan Teki727ed792019-02-28 00:27:00 +0530841{
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530842 struct ofnode_phandle_args phandle;
843 int ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530844
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530845 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
846 NULL, 0, 0, &phandle);
847 if (ret)
848 return ret;
Jagan Teki727ed792019-02-28 00:27:00 +0530849
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530850 /* If the PHY node is not a child of the internal MDIO bus, we are
851 * using some external PHY.
852 */
853 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
854 "allwinner,sun8i-h3-mdio-internal"))
Emmanuel Vadot98be8be2019-07-19 22:26:38 +0200855 return 0;
856
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530857 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
Jagan Teki727ed792019-02-28 00:27:00 +0530858 if (ret) {
859 dev_err(dev, "failed to get EPHY TX clock\n");
860 return ret;
861 }
862
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530863 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
Jagan Teki727ed792019-02-28 00:27:00 +0530864 if (ret) {
865 dev_err(dev, "failed to get EPHY TX reset\n");
866 return ret;
867 }
868
869 priv->use_internal_phy = true;
870
871 return 0;
872}
873
Simon Glassaad29ae2020-12-03 16:55:21 -0700874static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530875{
Simon Glassfa20e932020-12-03 16:55:20 -0700876 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
Philipp Tomsich3297b552017-02-22 19:46:41 +0100877 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530878 struct emac_eth_dev *priv = dev_get_priv(dev);
879 const char *phy_mode;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100880 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700881 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530882 int offset = 0;
Simon Glassfa4689a2019-12-06 21:41:35 -0700883#if CONFIG_IS_ENABLED(DM_GPIO)
Philipp Tomsich3297b552017-02-22 19:46:41 +0100884 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100885#endif
Jagan Tekicb63d282019-02-28 00:26:58 +0530886 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530887
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900888 pdata->iobase = dev_read_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100889 if (pdata->iobase == FDT_ADDR_T_NONE) {
890 debug("%s: Cannot find MAC base address\n", __func__);
891 return -EINVAL;
892 }
893
Lothar Feltene8cbced2018-07-13 10:45:28 +0200894 priv->variant = dev_get_driver_data(dev);
895
896 if (!priv->variant) {
897 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100898 return -EINVAL;
899 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200900
Jagan Tekicb63d282019-02-28 00:26:58 +0530901 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
902 if (ret) {
903 dev_err(dev, "failed to get TX clock\n");
904 return ret;
905 }
906
907 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
908 if (ret && ret != -ENOENT) {
909 dev_err(dev, "failed to get TX reset\n");
910 return ret;
911 }
912
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530913 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
914 if (offset < 0) {
915 debug("%s: cannot find syscon node\n", __func__);
916 return -EINVAL;
917 }
918
919 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
920 if (!reg) {
921 debug("%s: cannot find reg property in syscon node\n",
922 __func__);
923 return -EINVAL;
924 }
925 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
926 offset, reg);
927 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
928 debug("%s: Cannot find syscon base address\n", __func__);
929 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100930 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530931
932 pdata->phy_interface = -1;
933 priv->phyaddr = -1;
934 priv->use_internal_phy = false;
935
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100936 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100937 if (offset < 0) {
938 debug("%s: Cannot find PHY address\n", __func__);
939 return -EINVAL;
940 }
941 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530942
Simon Glassdd79d6e2017-01-17 16:52:55 -0700943 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530944
945 if (phy_mode)
946 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
947 printf("phy interface%d\n", pdata->phy_interface);
948
949 if (pdata->phy_interface == -1) {
950 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
951 return -EINVAL;
952 }
953
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530954 if (priv->variant == H3_EMAC) {
Andre Przywarab3ce85c2020-10-21 23:27:32 +0530955 ret = sun8i_handle_internal_phy(dev, priv);
Jagan Teki727ed792019-02-28 00:27:00 +0530956 if (ret)
957 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530958 }
959
960 priv->interface = pdata->phy_interface;
961
962 if (!priv->use_internal_phy)
963 parse_phy_pins(dev);
964
Icenowy Zheng525dc442018-11-23 00:37:48 +0100965 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
966 "allwinner,tx-delay-ps", 0);
967 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
968 printf("%s: Invalid TX delay value %d\n", __func__,
969 sun8i_pdata->tx_delay_ps);
970
971 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
972 "allwinner,rx-delay-ps", 0);
973 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
974 printf("%s: Invalid RX delay value %d\n", __func__,
975 sun8i_pdata->rx_delay_ps);
976
Simon Glassfa4689a2019-12-06 21:41:35 -0700977#if CONFIG_IS_ENABLED(DM_GPIO)
Simon Glass7a494432017-05-17 17:18:09 -0600978 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100979 "snps,reset-active-low"))
980 reset_flags |= GPIOD_ACTIVE_LOW;
981
982 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
983 &priv->reset_gpio, reset_flags);
984
985 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600986 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100987 "snps,reset-delays-us",
988 sun8i_pdata->reset_delays, 3);
989 } else if (ret == -ENOENT) {
990 ret = 0;
991 }
992#endif
993
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530994 return 0;
995}
996
997static const struct udevice_id sun8i_emac_eth_ids[] = {
998 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
999 {.compatible = "allwinner,sun50i-a64-emac",
1000 .data = (uintptr_t)A64_EMAC },
1001 {.compatible = "allwinner,sun8i-a83t-emac",
1002 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene8cbced2018-07-13 10:45:28 +02001003 {.compatible = "allwinner,sun8i-r40-gmac",
1004 .data = (uintptr_t)R40_GMAC },
Samuel Holland3386e9a2020-05-07 18:10:51 -05001005 {.compatible = "allwinner,sun50i-h6-emac",
1006 .data = (uintptr_t)H6_EMAC },
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301007 { }
1008};
1009
1010U_BOOT_DRIVER(eth_sun8i_emac) = {
1011 .name = "eth_sun8i_emac",
1012 .id = UCLASS_ETH,
1013 .of_match = sun8i_emac_eth_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -07001014 .of_to_plat = sun8i_emac_eth_of_to_plat,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301015 .probe = sun8i_emac_eth_probe,
1016 .ops = &sun8i_emac_eth_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001017 .priv_auto = sizeof(struct emac_eth_dev),
Simon Glass71fa5b42020-12-03 16:55:18 -07001018 .plat_auto = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301019 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1020};