183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2a29710c5SAmit Singh Tomar /*
3a29710c5SAmit Singh Tomar * (C) Copyright 2016
4a29710c5SAmit Singh Tomar * Author: Amit Singh Tomar, amittomer25@gmail.com
5a29710c5SAmit Singh Tomar *
6a29710c5SAmit Singh Tomar * Ethernet driver for H3/A64/A83T based SoC's
7a29710c5SAmit Singh Tomar *
8a29710c5SAmit Singh Tomar * It is derived from the work done by
9a29710c5SAmit Singh Tomar * LABBE Corentin & Chen-Yu Tsai for Linux, THANKS!
10a29710c5SAmit Singh Tomar *
11a29710c5SAmit Singh Tomar */
12a29710c5SAmit Singh Tomar
13a29710c5SAmit Singh Tomar #include <asm/io.h>
14a29710c5SAmit Singh Tomar #include <asm/arch/clock.h>
15a29710c5SAmit Singh Tomar #include <asm/arch/gpio.h>
16a29710c5SAmit Singh Tomar #include <common.h>
17a29710c5SAmit Singh Tomar #include <dm.h>
18a29710c5SAmit Singh Tomar #include <fdt_support.h>
19a29710c5SAmit Singh Tomar #include <linux/err.h>
20a29710c5SAmit Singh Tomar #include <malloc.h>
21a29710c5SAmit Singh Tomar #include <miiphy.h>
22a29710c5SAmit Singh Tomar #include <net.h>
23c0341173SAndre Przywara #include <dt-bindings/pinctrl/sun4i-a10.h>
244d555ae3SPhilipp Tomsich #ifdef CONFIG_DM_GPIO
254d555ae3SPhilipp Tomsich #include <asm-generic/gpio.h>
264d555ae3SPhilipp Tomsich #endif
27a29710c5SAmit Singh Tomar
28a29710c5SAmit Singh Tomar #define MDIO_CMD_MII_BUSY BIT(0)
29a29710c5SAmit Singh Tomar #define MDIO_CMD_MII_WRITE BIT(1)
30a29710c5SAmit Singh Tomar
31a29710c5SAmit Singh Tomar #define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
32a29710c5SAmit Singh Tomar #define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
33a29710c5SAmit Singh Tomar #define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
34a29710c5SAmit Singh Tomar #define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
35a29710c5SAmit Singh Tomar
36a29710c5SAmit Singh Tomar #define CONFIG_TX_DESCR_NUM 32
37a29710c5SAmit Singh Tomar #define CONFIG_RX_DESCR_NUM 32
384069437dSHans de Goede #define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
394069437dSHans de Goede
404069437dSHans de Goede /*
414069437dSHans de Goede * The datasheet says that each descriptor can transfers up to 4096 bytes
424069437dSHans de Goede * But later, the register documentation reduces that value to 2048,
434069437dSHans de Goede * using 2048 cause strange behaviours and even BSP driver use 2047
444069437dSHans de Goede */
454069437dSHans de Goede #define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
46a29710c5SAmit Singh Tomar
47a29710c5SAmit Singh Tomar #define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
48a29710c5SAmit Singh Tomar #define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
49a29710c5SAmit Singh Tomar
50a29710c5SAmit Singh Tomar #define H3_EPHY_DEFAULT_VALUE 0x58000
51a29710c5SAmit Singh Tomar #define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
52a29710c5SAmit Singh Tomar #define H3_EPHY_ADDR_SHIFT 20
53a29710c5SAmit Singh Tomar #define REG_PHY_ADDR_MASK GENMASK(4, 0)
54a29710c5SAmit Singh Tomar #define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
55a29710c5SAmit Singh Tomar #define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
56a29710c5SAmit Singh Tomar #define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
57a29710c5SAmit Singh Tomar
58a29710c5SAmit Singh Tomar #define SC_RMII_EN BIT(13)
59a29710c5SAmit Singh Tomar #define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
60a29710c5SAmit Singh Tomar #define SC_ETCS_MASK GENMASK(1, 0)
61a29710c5SAmit Singh Tomar #define SC_ETCS_EXT_GMII 0x1
62a29710c5SAmit Singh Tomar #define SC_ETCS_INT_GMII 0x2
63*9b16ede4SIcenowy Zheng #define SC_ETXDC_MASK GENMASK(12, 10)
64*9b16ede4SIcenowy Zheng #define SC_ETXDC_OFFSET 10
65*9b16ede4SIcenowy Zheng #define SC_ERXDC_MASK GENMASK(9, 5)
66*9b16ede4SIcenowy Zheng #define SC_ERXDC_OFFSET 5
67a29710c5SAmit Singh Tomar
68a29710c5SAmit Singh Tomar #define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
69a29710c5SAmit Singh Tomar
70a29710c5SAmit Singh Tomar #define AHB_GATE_OFFSET_EPHY 0
71a29710c5SAmit Singh Tomar
72c6a21d66SLothar Felten /* IO mux settings */
73c6a21d66SLothar Felten #define SUN8I_IOMUX_H3 2
74e46d73faSLothar Felten #define SUN8I_IOMUX_R40 5
75c6a21d66SLothar Felten #define SUN8I_IOMUX 4
76a29710c5SAmit Singh Tomar
77a29710c5SAmit Singh Tomar /* H3/A64 EMAC Register's offset */
78a29710c5SAmit Singh Tomar #define EMAC_CTL0 0x00
79a29710c5SAmit Singh Tomar #define EMAC_CTL1 0x04
80a29710c5SAmit Singh Tomar #define EMAC_INT_STA 0x08
81a29710c5SAmit Singh Tomar #define EMAC_INT_EN 0x0c
82a29710c5SAmit Singh Tomar #define EMAC_TX_CTL0 0x10
83a29710c5SAmit Singh Tomar #define EMAC_TX_CTL1 0x14
84a29710c5SAmit Singh Tomar #define EMAC_TX_FLOW_CTL 0x1c
85a29710c5SAmit Singh Tomar #define EMAC_TX_DMA_DESC 0x20
86a29710c5SAmit Singh Tomar #define EMAC_RX_CTL0 0x24
87a29710c5SAmit Singh Tomar #define EMAC_RX_CTL1 0x28
88a29710c5SAmit Singh Tomar #define EMAC_RX_DMA_DESC 0x34
89a29710c5SAmit Singh Tomar #define EMAC_MII_CMD 0x48
90a29710c5SAmit Singh Tomar #define EMAC_MII_DATA 0x4c
91a29710c5SAmit Singh Tomar #define EMAC_ADDR0_HIGH 0x50
92a29710c5SAmit Singh Tomar #define EMAC_ADDR0_LOW 0x54
93a29710c5SAmit Singh Tomar #define EMAC_TX_DMA_STA 0xb0
94a29710c5SAmit Singh Tomar #define EMAC_TX_CUR_DESC 0xb4
95a29710c5SAmit Singh Tomar #define EMAC_TX_CUR_BUF 0xb8
96a29710c5SAmit Singh Tomar #define EMAC_RX_DMA_STA 0xc0
97a29710c5SAmit Singh Tomar #define EMAC_RX_CUR_DESC 0xc4
98a29710c5SAmit Singh Tomar
99a29710c5SAmit Singh Tomar DECLARE_GLOBAL_DATA_PTR;
100a29710c5SAmit Singh Tomar
101a29710c5SAmit Singh Tomar enum emac_variant {
102a29710c5SAmit Singh Tomar A83T_EMAC = 1,
103a29710c5SAmit Singh Tomar H3_EMAC,
104a29710c5SAmit Singh Tomar A64_EMAC,
105e46d73faSLothar Felten R40_GMAC,
106a29710c5SAmit Singh Tomar };
107a29710c5SAmit Singh Tomar
108a29710c5SAmit Singh Tomar struct emac_dma_desc {
109a29710c5SAmit Singh Tomar u32 status;
110a29710c5SAmit Singh Tomar u32 st;
111a29710c5SAmit Singh Tomar u32 buf_addr;
112a29710c5SAmit Singh Tomar u32 next;
113a29710c5SAmit Singh Tomar } __aligned(ARCH_DMA_MINALIGN);
114a29710c5SAmit Singh Tomar
115a29710c5SAmit Singh Tomar struct emac_eth_dev {
116a29710c5SAmit Singh Tomar struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
117a29710c5SAmit Singh Tomar struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
118a29710c5SAmit Singh Tomar char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
119a29710c5SAmit Singh Tomar char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
120a29710c5SAmit Singh Tomar
121a29710c5SAmit Singh Tomar u32 interface;
122a29710c5SAmit Singh Tomar u32 phyaddr;
123a29710c5SAmit Singh Tomar u32 link;
124a29710c5SAmit Singh Tomar u32 speed;
125a29710c5SAmit Singh Tomar u32 duplex;
126a29710c5SAmit Singh Tomar u32 phy_configured;
127a29710c5SAmit Singh Tomar u32 tx_currdescnum;
128a29710c5SAmit Singh Tomar u32 rx_currdescnum;
129a29710c5SAmit Singh Tomar u32 addr;
130a29710c5SAmit Singh Tomar u32 tx_slot;
131a29710c5SAmit Singh Tomar bool use_internal_phy;
132a29710c5SAmit Singh Tomar
133a29710c5SAmit Singh Tomar enum emac_variant variant;
134a29710c5SAmit Singh Tomar void *mac_reg;
135a29710c5SAmit Singh Tomar phys_addr_t sysctl_reg;
136a29710c5SAmit Singh Tomar struct phy_device *phydev;
137a29710c5SAmit Singh Tomar struct mii_dev *bus;
1384d555ae3SPhilipp Tomsich #ifdef CONFIG_DM_GPIO
1394d555ae3SPhilipp Tomsich struct gpio_desc reset_gpio;
1404d555ae3SPhilipp Tomsich #endif
141a29710c5SAmit Singh Tomar };
142a29710c5SAmit Singh Tomar
1434d555ae3SPhilipp Tomsich
1444d555ae3SPhilipp Tomsich struct sun8i_eth_pdata {
1454d555ae3SPhilipp Tomsich struct eth_pdata eth_pdata;
1464d555ae3SPhilipp Tomsich u32 reset_delays[3];
147*9b16ede4SIcenowy Zheng int tx_delay_ps;
148*9b16ede4SIcenowy Zheng int rx_delay_ps;
1494d555ae3SPhilipp Tomsich };
1504d555ae3SPhilipp Tomsich
1514d555ae3SPhilipp Tomsich
sun8i_mdio_read(struct mii_dev * bus,int addr,int devad,int reg)152a29710c5SAmit Singh Tomar static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
153a29710c5SAmit Singh Tomar {
1544d555ae3SPhilipp Tomsich struct udevice *dev = bus->priv;
1554d555ae3SPhilipp Tomsich struct emac_eth_dev *priv = dev_get_priv(dev);
156a29710c5SAmit Singh Tomar ulong start;
157a29710c5SAmit Singh Tomar u32 miiaddr = 0;
158a29710c5SAmit Singh Tomar int timeout = CONFIG_MDIO_TIMEOUT;
159a29710c5SAmit Singh Tomar
160a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_WRITE;
161a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
162a29710c5SAmit Singh Tomar miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
163a29710c5SAmit Singh Tomar MDIO_CMD_MII_PHY_REG_ADDR_MASK;
164a29710c5SAmit Singh Tomar
165a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
166a29710c5SAmit Singh Tomar
167a29710c5SAmit Singh Tomar miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
168a29710c5SAmit Singh Tomar MDIO_CMD_MII_PHY_ADDR_MASK;
169a29710c5SAmit Singh Tomar
170a29710c5SAmit Singh Tomar miiaddr |= MDIO_CMD_MII_BUSY;
171a29710c5SAmit Singh Tomar
172a29710c5SAmit Singh Tomar writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
173a29710c5SAmit Singh Tomar
174a29710c5SAmit Singh Tomar start = get_timer(0);
175a29710c5SAmit Singh Tomar while (get_timer(start) < timeout) {
176a29710c5SAmit Singh Tomar if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY))
177a29710c5SAmit Singh Tomar return readl(priv->mac_reg + EMAC_MII_DATA);
178a29710c5SAmit Singh Tomar udelay(10);
179a29710c5SAmit Singh Tomar };
180a29710c5SAmit Singh Tomar
181a29710c5SAmit Singh Tomar return -1;
182a29710c5SAmit Singh Tomar }
183a29710c5SAmit Singh Tomar
sun8i_mdio_write(struct mii_dev * bus,int addr,int devad,int reg,u16 val)184a29710c5SAmit Singh Tomar static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
185a29710c5SAmit Singh Tomar u16 val)
186a29710c5SAmit Singh Tomar {
1874d555ae3SPhilipp Tomsich struct udevice *dev = bus->priv;
1884d555ae3SPhilipp Tomsich struct emac_eth_dev *priv = dev_get_priv(dev);
189a29710c5SAmit Singh Tomar ulong start;
190a29710c5SAmit Singh Tomar u32 miiaddr = 0;
191a29710c5SAmit Singh Tomar int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
192a29710c5SAmit Singh Tomar
193a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
194a29710c5SAmit Singh Tomar miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
195a29710c5SAmit Singh Tomar MDIO_CMD_MII_PHY_REG_ADDR_MASK;
196a29710c5SAmit Singh Tomar
197a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
198a29710c5SAmit Singh Tomar miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
199a29710c5SAmit Singh Tomar MDIO_CMD_MII_PHY_ADDR_MASK;
200a29710c5SAmit Singh Tomar
201a29710c5SAmit Singh Tomar miiaddr |= MDIO_CMD_MII_WRITE;
202a29710c5SAmit Singh Tomar miiaddr |= MDIO_CMD_MII_BUSY;
203a29710c5SAmit Singh Tomar
204a29710c5SAmit Singh Tomar writel(val, priv->mac_reg + EMAC_MII_DATA);
2051deeecb6SPhilipp Tomsich writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
206a29710c5SAmit Singh Tomar
207a29710c5SAmit Singh Tomar start = get_timer(0);
208a29710c5SAmit Singh Tomar while (get_timer(start) < timeout) {
209a29710c5SAmit Singh Tomar if (!(readl(priv->mac_reg + EMAC_MII_CMD) &
210a29710c5SAmit Singh Tomar MDIO_CMD_MII_BUSY)) {
211a29710c5SAmit Singh Tomar ret = 0;
212a29710c5SAmit Singh Tomar break;
213a29710c5SAmit Singh Tomar }
214a29710c5SAmit Singh Tomar udelay(10);
215a29710c5SAmit Singh Tomar };
216a29710c5SAmit Singh Tomar
217a29710c5SAmit Singh Tomar return ret;
218a29710c5SAmit Singh Tomar }
219a29710c5SAmit Singh Tomar
_sun8i_write_hwaddr(struct emac_eth_dev * priv,u8 * mac_id)220a29710c5SAmit Singh Tomar static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
221a29710c5SAmit Singh Tomar {
222a29710c5SAmit Singh Tomar u32 macid_lo, macid_hi;
223a29710c5SAmit Singh Tomar
224a29710c5SAmit Singh Tomar macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
225a29710c5SAmit Singh Tomar (mac_id[3] << 24);
226a29710c5SAmit Singh Tomar macid_hi = mac_id[4] + (mac_id[5] << 8);
227a29710c5SAmit Singh Tomar
228a29710c5SAmit Singh Tomar writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
229a29710c5SAmit Singh Tomar writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
230a29710c5SAmit Singh Tomar
231a29710c5SAmit Singh Tomar return 0;
232a29710c5SAmit Singh Tomar }
233a29710c5SAmit Singh Tomar
sun8i_adjust_link(struct emac_eth_dev * priv,struct phy_device * phydev)234a29710c5SAmit Singh Tomar static void sun8i_adjust_link(struct emac_eth_dev *priv,
235a29710c5SAmit Singh Tomar struct phy_device *phydev)
236a29710c5SAmit Singh Tomar {
237a29710c5SAmit Singh Tomar u32 v;
238a29710c5SAmit Singh Tomar
239a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_CTL0);
240a29710c5SAmit Singh Tomar
241a29710c5SAmit Singh Tomar if (phydev->duplex)
242a29710c5SAmit Singh Tomar v |= BIT(0);
243a29710c5SAmit Singh Tomar else
244a29710c5SAmit Singh Tomar v &= ~BIT(0);
245a29710c5SAmit Singh Tomar
246a29710c5SAmit Singh Tomar v &= ~0x0C;
247a29710c5SAmit Singh Tomar
248a29710c5SAmit Singh Tomar switch (phydev->speed) {
249a29710c5SAmit Singh Tomar case 1000:
250a29710c5SAmit Singh Tomar break;
251a29710c5SAmit Singh Tomar case 100:
252a29710c5SAmit Singh Tomar v |= BIT(2);
253a29710c5SAmit Singh Tomar v |= BIT(3);
254a29710c5SAmit Singh Tomar break;
255a29710c5SAmit Singh Tomar case 10:
256a29710c5SAmit Singh Tomar v |= BIT(3);
257a29710c5SAmit Singh Tomar break;
258a29710c5SAmit Singh Tomar }
259a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_CTL0);
260a29710c5SAmit Singh Tomar }
261a29710c5SAmit Singh Tomar
sun8i_emac_set_syscon_ephy(struct emac_eth_dev * priv,u32 * reg)262a29710c5SAmit Singh Tomar static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
263a29710c5SAmit Singh Tomar {
264a29710c5SAmit Singh Tomar if (priv->use_internal_phy) {
265a29710c5SAmit Singh Tomar /* H3 based SoC's that has an Internal 100MBit PHY
266a29710c5SAmit Singh Tomar * needs to be configured and powered up before use
267a29710c5SAmit Singh Tomar */
268a29710c5SAmit Singh Tomar *reg &= ~H3_EPHY_DEFAULT_MASK;
269a29710c5SAmit Singh Tomar *reg |= H3_EPHY_DEFAULT_VALUE;
270a29710c5SAmit Singh Tomar *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
271a29710c5SAmit Singh Tomar *reg &= ~H3_EPHY_SHUTDOWN;
272a29710c5SAmit Singh Tomar *reg |= H3_EPHY_SELECT;
273a29710c5SAmit Singh Tomar } else
274a29710c5SAmit Singh Tomar /* This is to select External Gigabit PHY on
275a29710c5SAmit Singh Tomar * the boards with H3 SoC.
276a29710c5SAmit Singh Tomar */
277a29710c5SAmit Singh Tomar *reg &= ~H3_EPHY_SELECT;
278a29710c5SAmit Singh Tomar
279a29710c5SAmit Singh Tomar return 0;
280a29710c5SAmit Singh Tomar }
281a29710c5SAmit Singh Tomar
sun8i_emac_set_syscon(struct sun8i_eth_pdata * pdata,struct emac_eth_dev * priv)282*9b16ede4SIcenowy Zheng static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
283*9b16ede4SIcenowy Zheng struct emac_eth_dev *priv)
284a29710c5SAmit Singh Tomar {
285a29710c5SAmit Singh Tomar int ret;
286a29710c5SAmit Singh Tomar u32 reg;
287a29710c5SAmit Singh Tomar
28812afd957SAndre Przywara reg = readl(priv->sysctl_reg + 0x30);
289a29710c5SAmit Singh Tomar
290e46d73faSLothar Felten if (priv->variant == R40_GMAC)
291e46d73faSLothar Felten return 0;
292e46d73faSLothar Felten
293a29710c5SAmit Singh Tomar if (priv->variant == H3_EMAC) {
294a29710c5SAmit Singh Tomar ret = sun8i_emac_set_syscon_ephy(priv, ®);
295a29710c5SAmit Singh Tomar if (ret)
296a29710c5SAmit Singh Tomar return ret;
297a29710c5SAmit Singh Tomar }
298a29710c5SAmit Singh Tomar
299a29710c5SAmit Singh Tomar reg &= ~(SC_ETCS_MASK | SC_EPIT);
300a29710c5SAmit Singh Tomar if (priv->variant == H3_EMAC || priv->variant == A64_EMAC)
301a29710c5SAmit Singh Tomar reg &= ~SC_RMII_EN;
302a29710c5SAmit Singh Tomar
303a29710c5SAmit Singh Tomar switch (priv->interface) {
304a29710c5SAmit Singh Tomar case PHY_INTERFACE_MODE_MII:
305a29710c5SAmit Singh Tomar /* default */
306a29710c5SAmit Singh Tomar break;
307a29710c5SAmit Singh Tomar case PHY_INTERFACE_MODE_RGMII:
308a29710c5SAmit Singh Tomar reg |= SC_EPIT | SC_ETCS_INT_GMII;
309a29710c5SAmit Singh Tomar break;
310a29710c5SAmit Singh Tomar case PHY_INTERFACE_MODE_RMII:
311a29710c5SAmit Singh Tomar if (priv->variant == H3_EMAC ||
312a29710c5SAmit Singh Tomar priv->variant == A64_EMAC) {
313a29710c5SAmit Singh Tomar reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
314a29710c5SAmit Singh Tomar break;
315a29710c5SAmit Singh Tomar }
316a29710c5SAmit Singh Tomar /* RMII not supported on A83T */
317a29710c5SAmit Singh Tomar default:
318a29710c5SAmit Singh Tomar debug("%s: Invalid PHY interface\n", __func__);
319a29710c5SAmit Singh Tomar return -EINVAL;
320a29710c5SAmit Singh Tomar }
321a29710c5SAmit Singh Tomar
322*9b16ede4SIcenowy Zheng if (pdata->tx_delay_ps)
323*9b16ede4SIcenowy Zheng reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
324*9b16ede4SIcenowy Zheng & SC_ETXDC_MASK;
325*9b16ede4SIcenowy Zheng
326*9b16ede4SIcenowy Zheng if (pdata->rx_delay_ps)
327*9b16ede4SIcenowy Zheng reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
328*9b16ede4SIcenowy Zheng & SC_ERXDC_MASK;
329*9b16ede4SIcenowy Zheng
33012afd957SAndre Przywara writel(reg, priv->sysctl_reg + 0x30);
331a29710c5SAmit Singh Tomar
332a29710c5SAmit Singh Tomar return 0;
333a29710c5SAmit Singh Tomar }
334a29710c5SAmit Singh Tomar
sun8i_phy_init(struct emac_eth_dev * priv,void * dev)335a29710c5SAmit Singh Tomar static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
336a29710c5SAmit Singh Tomar {
337a29710c5SAmit Singh Tomar struct phy_device *phydev;
338a29710c5SAmit Singh Tomar
339a29710c5SAmit Singh Tomar phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
340a29710c5SAmit Singh Tomar if (!phydev)
341a29710c5SAmit Singh Tomar return -ENODEV;
342a29710c5SAmit Singh Tomar
343a29710c5SAmit Singh Tomar phy_connect_dev(phydev, dev);
344a29710c5SAmit Singh Tomar
345a29710c5SAmit Singh Tomar priv->phydev = phydev;
346a29710c5SAmit Singh Tomar phy_config(priv->phydev);
347a29710c5SAmit Singh Tomar
348a29710c5SAmit Singh Tomar return 0;
349a29710c5SAmit Singh Tomar }
350a29710c5SAmit Singh Tomar
rx_descs_init(struct emac_eth_dev * priv)351a29710c5SAmit Singh Tomar static void rx_descs_init(struct emac_eth_dev *priv)
352a29710c5SAmit Singh Tomar {
353a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
354a29710c5SAmit Singh Tomar char *rxbuffs = &priv->rxbuffer[0];
355a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p;
356a29710c5SAmit Singh Tomar u32 idx;
357a29710c5SAmit Singh Tomar
358a29710c5SAmit Singh Tomar /* flush Rx buffers */
359a29710c5SAmit Singh Tomar flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
360a29710c5SAmit Singh Tomar RX_TOTAL_BUFSIZE);
361a29710c5SAmit Singh Tomar
362a29710c5SAmit Singh Tomar for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
363a29710c5SAmit Singh Tomar desc_p = &desc_table_p[idx];
364a29710c5SAmit Singh Tomar desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
365a29710c5SAmit Singh Tomar ;
366a29710c5SAmit Singh Tomar desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
3674069437dSHans de Goede desc_p->st |= CONFIG_ETH_RXSIZE;
368a29710c5SAmit Singh Tomar desc_p->status = BIT(31);
369a29710c5SAmit Singh Tomar }
370a29710c5SAmit Singh Tomar
371a29710c5SAmit Singh Tomar /* Correcting the last pointer of the chain */
372a29710c5SAmit Singh Tomar desc_p->next = (uintptr_t)&desc_table_p[0];
373a29710c5SAmit Singh Tomar
374a29710c5SAmit Singh Tomar flush_dcache_range((uintptr_t)priv->rx_chain,
375a29710c5SAmit Singh Tomar (uintptr_t)priv->rx_chain +
376a29710c5SAmit Singh Tomar sizeof(priv->rx_chain));
377a29710c5SAmit Singh Tomar
378a29710c5SAmit Singh Tomar writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
379a29710c5SAmit Singh Tomar priv->rx_currdescnum = 0;
380a29710c5SAmit Singh Tomar }
381a29710c5SAmit Singh Tomar
tx_descs_init(struct emac_eth_dev * priv)382a29710c5SAmit Singh Tomar static void tx_descs_init(struct emac_eth_dev *priv)
383a29710c5SAmit Singh Tomar {
384a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
385a29710c5SAmit Singh Tomar char *txbuffs = &priv->txbuffer[0];
386a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p;
387a29710c5SAmit Singh Tomar u32 idx;
388a29710c5SAmit Singh Tomar
389a29710c5SAmit Singh Tomar for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
390a29710c5SAmit Singh Tomar desc_p = &desc_table_p[idx];
391a29710c5SAmit Singh Tomar desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
392a29710c5SAmit Singh Tomar ;
393a29710c5SAmit Singh Tomar desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
394a29710c5SAmit Singh Tomar desc_p->status = (1 << 31);
395a29710c5SAmit Singh Tomar desc_p->st = 0;
396a29710c5SAmit Singh Tomar }
397a29710c5SAmit Singh Tomar
398a29710c5SAmit Singh Tomar /* Correcting the last pointer of the chain */
399a29710c5SAmit Singh Tomar desc_p->next = (uintptr_t)&desc_table_p[0];
400a29710c5SAmit Singh Tomar
401a29710c5SAmit Singh Tomar /* Flush all Tx buffer descriptors */
402a29710c5SAmit Singh Tomar flush_dcache_range((uintptr_t)priv->tx_chain,
403a29710c5SAmit Singh Tomar (uintptr_t)priv->tx_chain +
404a29710c5SAmit Singh Tomar sizeof(priv->tx_chain));
405a29710c5SAmit Singh Tomar
406a29710c5SAmit Singh Tomar writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
407a29710c5SAmit Singh Tomar priv->tx_currdescnum = 0;
408a29710c5SAmit Singh Tomar }
409a29710c5SAmit Singh Tomar
_sun8i_emac_eth_init(struct emac_eth_dev * priv,u8 * enetaddr)410a29710c5SAmit Singh Tomar static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
411a29710c5SAmit Singh Tomar {
412a29710c5SAmit Singh Tomar u32 reg, v;
413a29710c5SAmit Singh Tomar int timeout = 100;
414a29710c5SAmit Singh Tomar
415a29710c5SAmit Singh Tomar reg = readl((priv->mac_reg + EMAC_CTL1));
416a29710c5SAmit Singh Tomar
417a29710c5SAmit Singh Tomar if (!(reg & 0x1)) {
418a29710c5SAmit Singh Tomar /* Soft reset MAC */
419a29710c5SAmit Singh Tomar setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
420a29710c5SAmit Singh Tomar do {
421a29710c5SAmit Singh Tomar reg = readl(priv->mac_reg + EMAC_CTL1);
422a29710c5SAmit Singh Tomar } while ((reg & 0x01) != 0 && (--timeout));
423a29710c5SAmit Singh Tomar if (!timeout) {
424a29710c5SAmit Singh Tomar printf("%s: Timeout\n", __func__);
425a29710c5SAmit Singh Tomar return -1;
426a29710c5SAmit Singh Tomar }
427a29710c5SAmit Singh Tomar }
428a29710c5SAmit Singh Tomar
429a29710c5SAmit Singh Tomar /* Rewrite mac address after reset */
430a29710c5SAmit Singh Tomar _sun8i_write_hwaddr(priv, enetaddr);
431a29710c5SAmit Singh Tomar
432a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_TX_CTL1);
433a29710c5SAmit Singh Tomar /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
434a29710c5SAmit Singh Tomar v |= BIT(1);
435a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_TX_CTL1);
436a29710c5SAmit Singh Tomar
437a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_RX_CTL1);
438a29710c5SAmit Singh Tomar /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
439a29710c5SAmit Singh Tomar * complete frame has been written to RX DMA FIFO
440a29710c5SAmit Singh Tomar */
441a29710c5SAmit Singh Tomar v |= BIT(1);
442a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_RX_CTL1);
443a29710c5SAmit Singh Tomar
444a29710c5SAmit Singh Tomar /* DMA */
445a29710c5SAmit Singh Tomar writel(8 << 24, priv->mac_reg + EMAC_CTL1);
446a29710c5SAmit Singh Tomar
447a29710c5SAmit Singh Tomar /* Initialize rx/tx descriptors */
448a29710c5SAmit Singh Tomar rx_descs_init(priv);
449a29710c5SAmit Singh Tomar tx_descs_init(priv);
450a29710c5SAmit Singh Tomar
451a29710c5SAmit Singh Tomar /* PHY Start Up */
4522d53018aSSamuel Holland phy_startup(priv->phydev);
453a29710c5SAmit Singh Tomar
454a29710c5SAmit Singh Tomar sun8i_adjust_link(priv, priv->phydev);
455a29710c5SAmit Singh Tomar
456a29710c5SAmit Singh Tomar /* Start RX DMA */
457a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_RX_CTL1);
458a29710c5SAmit Singh Tomar v |= BIT(30);
459a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_RX_CTL1);
460a29710c5SAmit Singh Tomar /* Start TX DMA */
461a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_TX_CTL1);
462a29710c5SAmit Singh Tomar v |= BIT(30);
463a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_TX_CTL1);
464a29710c5SAmit Singh Tomar
465a29710c5SAmit Singh Tomar /* Enable RX/TX */
466a29710c5SAmit Singh Tomar setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
467a29710c5SAmit Singh Tomar setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
468a29710c5SAmit Singh Tomar
469a29710c5SAmit Singh Tomar return 0;
470a29710c5SAmit Singh Tomar }
471a29710c5SAmit Singh Tomar
parse_phy_pins(struct udevice * dev)472a29710c5SAmit Singh Tomar static int parse_phy_pins(struct udevice *dev)
473a29710c5SAmit Singh Tomar {
474c6a21d66SLothar Felten struct emac_eth_dev *priv = dev_get_priv(dev);
475a29710c5SAmit Singh Tomar int offset;
476a29710c5SAmit Singh Tomar const char *pin_name;
477ecd0cec0SAndre Przywara int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
478a29710c5SAmit Singh Tomar
479e160f7d4SSimon Glass offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
480a29710c5SAmit Singh Tomar "pinctrl-0");
481a29710c5SAmit Singh Tomar if (offset < 0) {
482a29710c5SAmit Singh Tomar printf("WARNING: emac: cannot find pinctrl-0 node\n");
483a29710c5SAmit Singh Tomar return offset;
484a29710c5SAmit Singh Tomar }
485a29710c5SAmit Singh Tomar
486a29710c5SAmit Singh Tomar drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
487c0341173SAndre Przywara "drive-strength", ~0);
488c0341173SAndre Przywara if (drive != ~0) {
489c0341173SAndre Przywara if (drive <= 10)
490c0341173SAndre Przywara drive = SUN4I_PINCTRL_10_MA;
491c0341173SAndre Przywara else if (drive <= 20)
492c0341173SAndre Przywara drive = SUN4I_PINCTRL_20_MA;
493c0341173SAndre Przywara else if (drive <= 30)
494c0341173SAndre Przywara drive = SUN4I_PINCTRL_30_MA;
495c0341173SAndre Przywara else
496c0341173SAndre Przywara drive = SUN4I_PINCTRL_40_MA;
497c0341173SAndre Przywara }
498c0341173SAndre Przywara
499c0341173SAndre Przywara if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
500c0341173SAndre Przywara pull = SUN4I_PINCTRL_PULL_UP;
501c0341173SAndre Przywara else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
502c0341173SAndre Przywara pull = SUN4I_PINCTRL_PULL_DOWN;
503ecd0cec0SAndre Przywara
504a29710c5SAmit Singh Tomar for (i = 0; ; i++) {
505a29710c5SAmit Singh Tomar int pin;
506a29710c5SAmit Singh Tomar
507b02e4044SSimon Glass pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
508c0341173SAndre Przywara "pins", i, NULL);
509b02e4044SSimon Glass if (!pin_name)
510a29710c5SAmit Singh Tomar break;
511c0341173SAndre Przywara
512c0341173SAndre Przywara pin = sunxi_name_to_gpio(pin_name);
513c0341173SAndre Przywara if (pin < 0)
514a29710c5SAmit Singh Tomar continue;
515a29710c5SAmit Singh Tomar
516c6a21d66SLothar Felten if (priv->variant == H3_EMAC)
517c6a21d66SLothar Felten sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
518e46d73faSLothar Felten else if (priv->variant == R40_GMAC)
519e46d73faSLothar Felten sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
520c6a21d66SLothar Felten else
521c6a21d66SLothar Felten sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
522c6a21d66SLothar Felten
523c0341173SAndre Przywara if (drive != ~0)
524a29710c5SAmit Singh Tomar sunxi_gpio_set_drv(pin, drive);
525c0341173SAndre Przywara if (pull != ~0)
526a29710c5SAmit Singh Tomar sunxi_gpio_set_pull(pin, pull);
527a29710c5SAmit Singh Tomar }
528a29710c5SAmit Singh Tomar
529a29710c5SAmit Singh Tomar if (!i) {
530c0341173SAndre Przywara printf("WARNING: emac: cannot find pins property\n");
531a29710c5SAmit Singh Tomar return -2;
532a29710c5SAmit Singh Tomar }
533a29710c5SAmit Singh Tomar
534a29710c5SAmit Singh Tomar return 0;
535a29710c5SAmit Singh Tomar }
536a29710c5SAmit Singh Tomar
_sun8i_eth_recv(struct emac_eth_dev * priv,uchar ** packetp)537a29710c5SAmit Singh Tomar static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
538a29710c5SAmit Singh Tomar {
539a29710c5SAmit Singh Tomar u32 status, desc_num = priv->rx_currdescnum;
540a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
541a29710c5SAmit Singh Tomar int length = -EAGAIN;
542a29710c5SAmit Singh Tomar int good_packet = 1;
543a29710c5SAmit Singh Tomar uintptr_t desc_start = (uintptr_t)desc_p;
544a29710c5SAmit Singh Tomar uintptr_t desc_end = desc_start +
545a29710c5SAmit Singh Tomar roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
546a29710c5SAmit Singh Tomar
547a29710c5SAmit Singh Tomar ulong data_start = (uintptr_t)desc_p->buf_addr;
548a29710c5SAmit Singh Tomar ulong data_end;
549a29710c5SAmit Singh Tomar
550a29710c5SAmit Singh Tomar /* Invalidate entire buffer descriptor */
551a29710c5SAmit Singh Tomar invalidate_dcache_range(desc_start, desc_end);
552a29710c5SAmit Singh Tomar
553a29710c5SAmit Singh Tomar status = desc_p->status;
554a29710c5SAmit Singh Tomar
555a29710c5SAmit Singh Tomar /* Check for DMA own bit */
556a29710c5SAmit Singh Tomar if (!(status & BIT(31))) {
557a29710c5SAmit Singh Tomar length = (desc_p->status >> 16) & 0x3FFF;
558a29710c5SAmit Singh Tomar
559a29710c5SAmit Singh Tomar if (length < 0x40) {
560a29710c5SAmit Singh Tomar good_packet = 0;
561a29710c5SAmit Singh Tomar debug("RX: Bad Packet (runt)\n");
562a29710c5SAmit Singh Tomar }
563a29710c5SAmit Singh Tomar
564a29710c5SAmit Singh Tomar data_end = data_start + length;
565a29710c5SAmit Singh Tomar /* Invalidate received data */
566a29710c5SAmit Singh Tomar invalidate_dcache_range(rounddown(data_start,
567a29710c5SAmit Singh Tomar ARCH_DMA_MINALIGN),
568a29710c5SAmit Singh Tomar roundup(data_end,
569a29710c5SAmit Singh Tomar ARCH_DMA_MINALIGN));
570a29710c5SAmit Singh Tomar if (good_packet) {
5714069437dSHans de Goede if (length > CONFIG_ETH_RXSIZE) {
572a29710c5SAmit Singh Tomar printf("Received packet is too big (len=%d)\n",
573a29710c5SAmit Singh Tomar length);
574a29710c5SAmit Singh Tomar return -EMSGSIZE;
575a29710c5SAmit Singh Tomar }
576a29710c5SAmit Singh Tomar *packetp = (uchar *)(ulong)desc_p->buf_addr;
577a29710c5SAmit Singh Tomar return length;
578a29710c5SAmit Singh Tomar }
579a29710c5SAmit Singh Tomar }
580a29710c5SAmit Singh Tomar
581a29710c5SAmit Singh Tomar return length;
582a29710c5SAmit Singh Tomar }
583a29710c5SAmit Singh Tomar
_sun8i_emac_eth_send(struct emac_eth_dev * priv,void * packet,int len)584a29710c5SAmit Singh Tomar static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
585a29710c5SAmit Singh Tomar int len)
586a29710c5SAmit Singh Tomar {
587a29710c5SAmit Singh Tomar u32 v, desc_num = priv->tx_currdescnum;
588a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
589a29710c5SAmit Singh Tomar uintptr_t desc_start = (uintptr_t)desc_p;
590a29710c5SAmit Singh Tomar uintptr_t desc_end = desc_start +
591a29710c5SAmit Singh Tomar roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
592a29710c5SAmit Singh Tomar
593a29710c5SAmit Singh Tomar uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
594a29710c5SAmit Singh Tomar uintptr_t data_end = data_start +
595a29710c5SAmit Singh Tomar roundup(len, ARCH_DMA_MINALIGN);
596a29710c5SAmit Singh Tomar
597a29710c5SAmit Singh Tomar /* Invalidate entire buffer descriptor */
598a29710c5SAmit Singh Tomar invalidate_dcache_range(desc_start, desc_end);
599a29710c5SAmit Singh Tomar
600a29710c5SAmit Singh Tomar desc_p->st = len;
601a29710c5SAmit Singh Tomar /* Mandatory undocumented bit */
602a29710c5SAmit Singh Tomar desc_p->st |= BIT(24);
603a29710c5SAmit Singh Tomar
604a29710c5SAmit Singh Tomar memcpy((void *)data_start, packet, len);
605a29710c5SAmit Singh Tomar
606a29710c5SAmit Singh Tomar /* Flush data to be sent */
607a29710c5SAmit Singh Tomar flush_dcache_range(data_start, data_end);
608a29710c5SAmit Singh Tomar
609a29710c5SAmit Singh Tomar /* frame end */
610a29710c5SAmit Singh Tomar desc_p->st |= BIT(30);
611a29710c5SAmit Singh Tomar desc_p->st |= BIT(31);
612a29710c5SAmit Singh Tomar
613a29710c5SAmit Singh Tomar /*frame begin */
614a29710c5SAmit Singh Tomar desc_p->st |= BIT(29);
615a29710c5SAmit Singh Tomar desc_p->status = BIT(31);
616a29710c5SAmit Singh Tomar
617a29710c5SAmit Singh Tomar /*Descriptors st and status field has changed, so FLUSH it */
618a29710c5SAmit Singh Tomar flush_dcache_range(desc_start, desc_end);
619a29710c5SAmit Singh Tomar
620a29710c5SAmit Singh Tomar /* Move to next Descriptor and wrap around */
621a29710c5SAmit Singh Tomar if (++desc_num >= CONFIG_TX_DESCR_NUM)
622a29710c5SAmit Singh Tomar desc_num = 0;
623a29710c5SAmit Singh Tomar priv->tx_currdescnum = desc_num;
624a29710c5SAmit Singh Tomar
625a29710c5SAmit Singh Tomar /* Start the DMA */
626a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_TX_CTL1);
627a29710c5SAmit Singh Tomar v |= BIT(31);/* mandatory */
628a29710c5SAmit Singh Tomar v |= BIT(30);/* mandatory */
629a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_TX_CTL1);
630a29710c5SAmit Singh Tomar
631a29710c5SAmit Singh Tomar return 0;
632a29710c5SAmit Singh Tomar }
633a29710c5SAmit Singh Tomar
sun8i_eth_write_hwaddr(struct udevice * dev)634a29710c5SAmit Singh Tomar static int sun8i_eth_write_hwaddr(struct udevice *dev)
635a29710c5SAmit Singh Tomar {
636a29710c5SAmit Singh Tomar struct eth_pdata *pdata = dev_get_platdata(dev);
637a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
638a29710c5SAmit Singh Tomar
639a29710c5SAmit Singh Tomar return _sun8i_write_hwaddr(priv, pdata->enetaddr);
640a29710c5SAmit Singh Tomar }
641a29710c5SAmit Singh Tomar
sun8i_emac_board_setup(struct emac_eth_dev * priv)642a29710c5SAmit Singh Tomar static void sun8i_emac_board_setup(struct emac_eth_dev *priv)
643a29710c5SAmit Singh Tomar {
644a29710c5SAmit Singh Tomar struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
645a29710c5SAmit Singh Tomar
646c6a21d66SLothar Felten if (priv->variant == H3_EMAC) {
64713ae2a40SChen-Yu Tsai /* Only H3/H5 have clock controls for internal EPHY */
648a29710c5SAmit Singh Tomar if (priv->use_internal_phy) {
649a29710c5SAmit Singh Tomar /* Set clock gating for ephy */
650c6a21d66SLothar Felten setbits_le32(&ccm->bus_gate4,
651c6a21d66SLothar Felten BIT(AHB_GATE_OFFSET_EPHY));
652a29710c5SAmit Singh Tomar
653a29710c5SAmit Singh Tomar /* Deassert EPHY */
654c6a21d66SLothar Felten setbits_le32(&ccm->ahb_reset2_cfg,
655c6a21d66SLothar Felten BIT(AHB_RESET_OFFSET_EPHY));
656a29710c5SAmit Singh Tomar }
657c6a21d66SLothar Felten }
658a29710c5SAmit Singh Tomar
659e46d73faSLothar Felten if (priv->variant == R40_GMAC) {
660e46d73faSLothar Felten /* Set clock gating for emac */
661e46d73faSLothar Felten setbits_le32(&ccm->ahb_reset1_cfg, BIT(AHB_RESET_OFFSET_GMAC));
662e46d73faSLothar Felten
663e46d73faSLothar Felten /* De-assert EMAC */
664e46d73faSLothar Felten setbits_le32(&ccm->ahb_gate1, BIT(AHB_GATE_OFFSET_GMAC));
665e46d73faSLothar Felten
666e46d73faSLothar Felten /* Select RGMII for R40 */
667e46d73faSLothar Felten setbits_le32(&ccm->gmac_clk_cfg,
668e46d73faSLothar Felten CCM_GMAC_CTRL_TX_CLK_SRC_INT_RGMII |
669e46d73faSLothar Felten CCM_GMAC_CTRL_GPIT_RGMII);
670e46d73faSLothar Felten setbits_le32(&ccm->gmac_clk_cfg,
671e46d73faSLothar Felten CCM_GMAC_CTRL_TX_CLK_DELAY(CONFIG_GMAC_TX_DELAY));
672e46d73faSLothar Felten } else {
673a29710c5SAmit Singh Tomar /* Set clock gating for emac */
674a29710c5SAmit Singh Tomar setbits_le32(&ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_GMAC));
675a29710c5SAmit Singh Tomar
676a29710c5SAmit Singh Tomar /* De-assert EMAC */
677a29710c5SAmit Singh Tomar setbits_le32(&ccm->ahb_reset0_cfg, BIT(AHB_RESET_OFFSET_GMAC));
678a29710c5SAmit Singh Tomar }
679e46d73faSLothar Felten }
680a29710c5SAmit Singh Tomar
6814d555ae3SPhilipp Tomsich #if defined(CONFIG_DM_GPIO)
sun8i_mdio_reset(struct mii_dev * bus)6824d555ae3SPhilipp Tomsich static int sun8i_mdio_reset(struct mii_dev *bus)
6834d555ae3SPhilipp Tomsich {
6844d555ae3SPhilipp Tomsich struct udevice *dev = bus->priv;
6854d555ae3SPhilipp Tomsich struct emac_eth_dev *priv = dev_get_priv(dev);
6864d555ae3SPhilipp Tomsich struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
6874d555ae3SPhilipp Tomsich int ret;
6884d555ae3SPhilipp Tomsich
6894d555ae3SPhilipp Tomsich if (!dm_gpio_is_valid(&priv->reset_gpio))
6904d555ae3SPhilipp Tomsich return 0;
6914d555ae3SPhilipp Tomsich
6924d555ae3SPhilipp Tomsich /* reset the phy */
6934d555ae3SPhilipp Tomsich ret = dm_gpio_set_value(&priv->reset_gpio, 0);
6944d555ae3SPhilipp Tomsich if (ret)
6954d555ae3SPhilipp Tomsich return ret;
6964d555ae3SPhilipp Tomsich
6974d555ae3SPhilipp Tomsich udelay(pdata->reset_delays[0]);
6984d555ae3SPhilipp Tomsich
6994d555ae3SPhilipp Tomsich ret = dm_gpio_set_value(&priv->reset_gpio, 1);
7004d555ae3SPhilipp Tomsich if (ret)
7014d555ae3SPhilipp Tomsich return ret;
7024d555ae3SPhilipp Tomsich
7034d555ae3SPhilipp Tomsich udelay(pdata->reset_delays[1]);
7044d555ae3SPhilipp Tomsich
7054d555ae3SPhilipp Tomsich ret = dm_gpio_set_value(&priv->reset_gpio, 0);
7064d555ae3SPhilipp Tomsich if (ret)
7074d555ae3SPhilipp Tomsich return ret;
7084d555ae3SPhilipp Tomsich
7094d555ae3SPhilipp Tomsich udelay(pdata->reset_delays[2]);
7104d555ae3SPhilipp Tomsich
7114d555ae3SPhilipp Tomsich return 0;
7124d555ae3SPhilipp Tomsich }
7134d555ae3SPhilipp Tomsich #endif
7144d555ae3SPhilipp Tomsich
sun8i_mdio_init(const char * name,struct udevice * priv)7154d555ae3SPhilipp Tomsich static int sun8i_mdio_init(const char *name, struct udevice *priv)
716a29710c5SAmit Singh Tomar {
717a29710c5SAmit Singh Tomar struct mii_dev *bus = mdio_alloc();
718a29710c5SAmit Singh Tomar
719a29710c5SAmit Singh Tomar if (!bus) {
720a29710c5SAmit Singh Tomar debug("Failed to allocate MDIO bus\n");
721a29710c5SAmit Singh Tomar return -ENOMEM;
722a29710c5SAmit Singh Tomar }
723a29710c5SAmit Singh Tomar
724a29710c5SAmit Singh Tomar bus->read = sun8i_mdio_read;
725a29710c5SAmit Singh Tomar bus->write = sun8i_mdio_write;
726a29710c5SAmit Singh Tomar snprintf(bus->name, sizeof(bus->name), name);
727a29710c5SAmit Singh Tomar bus->priv = (void *)priv;
7284d555ae3SPhilipp Tomsich #if defined(CONFIG_DM_GPIO)
7294d555ae3SPhilipp Tomsich bus->reset = sun8i_mdio_reset;
7304d555ae3SPhilipp Tomsich #endif
731a29710c5SAmit Singh Tomar
732a29710c5SAmit Singh Tomar return mdio_register(bus);
733a29710c5SAmit Singh Tomar }
734a29710c5SAmit Singh Tomar
sun8i_emac_eth_start(struct udevice * dev)735a29710c5SAmit Singh Tomar static int sun8i_emac_eth_start(struct udevice *dev)
736a29710c5SAmit Singh Tomar {
737a29710c5SAmit Singh Tomar struct eth_pdata *pdata = dev_get_platdata(dev);
738a29710c5SAmit Singh Tomar
739a29710c5SAmit Singh Tomar return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
740a29710c5SAmit Singh Tomar }
741a29710c5SAmit Singh Tomar
sun8i_emac_eth_send(struct udevice * dev,void * packet,int length)742a29710c5SAmit Singh Tomar static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
743a29710c5SAmit Singh Tomar {
744a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
745a29710c5SAmit Singh Tomar
746a29710c5SAmit Singh Tomar return _sun8i_emac_eth_send(priv, packet, length);
747a29710c5SAmit Singh Tomar }
748a29710c5SAmit Singh Tomar
sun8i_emac_eth_recv(struct udevice * dev,int flags,uchar ** packetp)749a29710c5SAmit Singh Tomar static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
750a29710c5SAmit Singh Tomar {
751a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
752a29710c5SAmit Singh Tomar
753a29710c5SAmit Singh Tomar return _sun8i_eth_recv(priv, packetp);
754a29710c5SAmit Singh Tomar }
755a29710c5SAmit Singh Tomar
_sun8i_free_pkt(struct emac_eth_dev * priv)756a29710c5SAmit Singh Tomar static int _sun8i_free_pkt(struct emac_eth_dev *priv)
757a29710c5SAmit Singh Tomar {
758a29710c5SAmit Singh Tomar u32 desc_num = priv->rx_currdescnum;
759a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
760a29710c5SAmit Singh Tomar uintptr_t desc_start = (uintptr_t)desc_p;
761a29710c5SAmit Singh Tomar uintptr_t desc_end = desc_start +
762a29710c5SAmit Singh Tomar roundup(sizeof(u32), ARCH_DMA_MINALIGN);
763a29710c5SAmit Singh Tomar
764a29710c5SAmit Singh Tomar /* Make the current descriptor valid again */
765a29710c5SAmit Singh Tomar desc_p->status |= BIT(31);
766a29710c5SAmit Singh Tomar
767a29710c5SAmit Singh Tomar /* Flush Status field of descriptor */
768a29710c5SAmit Singh Tomar flush_dcache_range(desc_start, desc_end);
769a29710c5SAmit Singh Tomar
770a29710c5SAmit Singh Tomar /* Move to next desc and wrap-around condition. */
771a29710c5SAmit Singh Tomar if (++desc_num >= CONFIG_RX_DESCR_NUM)
772a29710c5SAmit Singh Tomar desc_num = 0;
773a29710c5SAmit Singh Tomar priv->rx_currdescnum = desc_num;
774a29710c5SAmit Singh Tomar
775a29710c5SAmit Singh Tomar return 0;
776a29710c5SAmit Singh Tomar }
777a29710c5SAmit Singh Tomar
sun8i_eth_free_pkt(struct udevice * dev,uchar * packet,int length)778a29710c5SAmit Singh Tomar static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
779a29710c5SAmit Singh Tomar int length)
780a29710c5SAmit Singh Tomar {
781a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
782a29710c5SAmit Singh Tomar
783a29710c5SAmit Singh Tomar return _sun8i_free_pkt(priv);
784a29710c5SAmit Singh Tomar }
785a29710c5SAmit Singh Tomar
sun8i_emac_eth_stop(struct udevice * dev)786a29710c5SAmit Singh Tomar static void sun8i_emac_eth_stop(struct udevice *dev)
787a29710c5SAmit Singh Tomar {
788a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
789a29710c5SAmit Singh Tomar
790a29710c5SAmit Singh Tomar /* Stop Rx/Tx transmitter */
791a29710c5SAmit Singh Tomar clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
792a29710c5SAmit Singh Tomar clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
793a29710c5SAmit Singh Tomar
794a29710c5SAmit Singh Tomar /* Stop TX DMA */
795a29710c5SAmit Singh Tomar clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
796a29710c5SAmit Singh Tomar
797a29710c5SAmit Singh Tomar phy_shutdown(priv->phydev);
798a29710c5SAmit Singh Tomar }
799a29710c5SAmit Singh Tomar
sun8i_emac_eth_probe(struct udevice * dev)800a29710c5SAmit Singh Tomar static int sun8i_emac_eth_probe(struct udevice *dev)
801a29710c5SAmit Singh Tomar {
802*9b16ede4SIcenowy Zheng struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
803*9b16ede4SIcenowy Zheng struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
804a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
805a29710c5SAmit Singh Tomar
806a29710c5SAmit Singh Tomar priv->mac_reg = (void *)pdata->iobase;
807a29710c5SAmit Singh Tomar
808a29710c5SAmit Singh Tomar sun8i_emac_board_setup(priv);
809*9b16ede4SIcenowy Zheng sun8i_emac_set_syscon(sun8i_pdata, priv);
810a29710c5SAmit Singh Tomar
8114d555ae3SPhilipp Tomsich sun8i_mdio_init(dev->name, dev);
812a29710c5SAmit Singh Tomar priv->bus = miiphy_get_dev_by_name(dev->name);
813a29710c5SAmit Singh Tomar
814a29710c5SAmit Singh Tomar return sun8i_phy_init(priv, dev);
815a29710c5SAmit Singh Tomar }
816a29710c5SAmit Singh Tomar
817a29710c5SAmit Singh Tomar static const struct eth_ops sun8i_emac_eth_ops = {
818a29710c5SAmit Singh Tomar .start = sun8i_emac_eth_start,
819a29710c5SAmit Singh Tomar .write_hwaddr = sun8i_eth_write_hwaddr,
820a29710c5SAmit Singh Tomar .send = sun8i_emac_eth_send,
821a29710c5SAmit Singh Tomar .recv = sun8i_emac_eth_recv,
822a29710c5SAmit Singh Tomar .free_pkt = sun8i_eth_free_pkt,
823a29710c5SAmit Singh Tomar .stop = sun8i_emac_eth_stop,
824a29710c5SAmit Singh Tomar };
825a29710c5SAmit Singh Tomar
sun8i_emac_eth_ofdata_to_platdata(struct udevice * dev)826a29710c5SAmit Singh Tomar static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
827a29710c5SAmit Singh Tomar {
8284d555ae3SPhilipp Tomsich struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
8294d555ae3SPhilipp Tomsich struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
830a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
831a29710c5SAmit Singh Tomar const char *phy_mode;
832ecd0cec0SAndre Przywara const fdt32_t *reg;
833e160f7d4SSimon Glass int node = dev_of_offset(dev);
834a29710c5SAmit Singh Tomar int offset = 0;
8354d555ae3SPhilipp Tomsich #ifdef CONFIG_DM_GPIO
8364d555ae3SPhilipp Tomsich int reset_flags = GPIOD_IS_OUT;
8374d555ae3SPhilipp Tomsich int ret = 0;
8384d555ae3SPhilipp Tomsich #endif
839a29710c5SAmit Singh Tomar
84012afd957SAndre Przywara pdata->iobase = devfdt_get_addr(dev);
84112afd957SAndre Przywara if (pdata->iobase == FDT_ADDR_T_NONE) {
84212afd957SAndre Przywara debug("%s: Cannot find MAC base address\n", __func__);
84312afd957SAndre Przywara return -EINVAL;
84412afd957SAndre Przywara }
84512afd957SAndre Przywara
846e46d73faSLothar Felten priv->variant = dev_get_driver_data(dev);
847e46d73faSLothar Felten
848e46d73faSLothar Felten if (!priv->variant) {
849e46d73faSLothar Felten printf("%s: Missing variant\n", __func__);
850e46d73faSLothar Felten return -EINVAL;
851e46d73faSLothar Felten }
852e46d73faSLothar Felten
853e46d73faSLothar Felten if (priv->variant != R40_GMAC) {
85412afd957SAndre Przywara offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
85512afd957SAndre Przywara if (offset < 0) {
85612afd957SAndre Przywara debug("%s: cannot find syscon node\n", __func__);
85712afd957SAndre Przywara return -EINVAL;
85812afd957SAndre Przywara }
85912afd957SAndre Przywara reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
86012afd957SAndre Przywara if (!reg) {
86112afd957SAndre Przywara debug("%s: cannot find reg property in syscon node\n",
86212afd957SAndre Przywara __func__);
86312afd957SAndre Przywara return -EINVAL;
86412afd957SAndre Przywara }
86512afd957SAndre Przywara priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
86612afd957SAndre Przywara offset, reg);
86712afd957SAndre Przywara if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
868e46d73faSLothar Felten debug("%s: Cannot find syscon base address\n",
869e46d73faSLothar Felten __func__);
87012afd957SAndre Przywara return -EINVAL;
87112afd957SAndre Przywara }
872e46d73faSLothar Felten }
873a29710c5SAmit Singh Tomar
874a29710c5SAmit Singh Tomar pdata->phy_interface = -1;
875a29710c5SAmit Singh Tomar priv->phyaddr = -1;
876a29710c5SAmit Singh Tomar priv->use_internal_phy = false;
877a29710c5SAmit Singh Tomar
878ecd0cec0SAndre Przywara offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
87912afd957SAndre Przywara if (offset < 0) {
88012afd957SAndre Przywara debug("%s: Cannot find PHY address\n", __func__);
88112afd957SAndre Przywara return -EINVAL;
88212afd957SAndre Przywara }
88312afd957SAndre Przywara priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
884a29710c5SAmit Singh Tomar
885e160f7d4SSimon Glass phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
886a29710c5SAmit Singh Tomar
887a29710c5SAmit Singh Tomar if (phy_mode)
888a29710c5SAmit Singh Tomar pdata->phy_interface = phy_get_interface_by_name(phy_mode);
889a29710c5SAmit Singh Tomar printf("phy interface%d\n", pdata->phy_interface);
890a29710c5SAmit Singh Tomar
891a29710c5SAmit Singh Tomar if (pdata->phy_interface == -1) {
892a29710c5SAmit Singh Tomar debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
893a29710c5SAmit Singh Tomar return -EINVAL;
894a29710c5SAmit Singh Tomar }
895a29710c5SAmit Singh Tomar
896a29710c5SAmit Singh Tomar if (priv->variant == H3_EMAC) {
89712afd957SAndre Przywara int parent = fdt_parent_offset(gd->fdt_blob, offset);
89812afd957SAndre Przywara
89912afd957SAndre Przywara if (parent >= 0 &&
90012afd957SAndre Przywara !fdt_node_check_compatible(gd->fdt_blob, parent,
90112afd957SAndre Przywara "allwinner,sun8i-h3-mdio-internal"))
90212afd957SAndre Przywara priv->use_internal_phy = true;
90312afd957SAndre Przywara }
904a29710c5SAmit Singh Tomar
905a29710c5SAmit Singh Tomar priv->interface = pdata->phy_interface;
906a29710c5SAmit Singh Tomar
907a29710c5SAmit Singh Tomar if (!priv->use_internal_phy)
908a29710c5SAmit Singh Tomar parse_phy_pins(dev);
909a29710c5SAmit Singh Tomar
910*9b16ede4SIcenowy Zheng sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
911*9b16ede4SIcenowy Zheng "allwinner,tx-delay-ps", 0);
912*9b16ede4SIcenowy Zheng if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
913*9b16ede4SIcenowy Zheng printf("%s: Invalid TX delay value %d\n", __func__,
914*9b16ede4SIcenowy Zheng sun8i_pdata->tx_delay_ps);
915*9b16ede4SIcenowy Zheng
916*9b16ede4SIcenowy Zheng sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
917*9b16ede4SIcenowy Zheng "allwinner,rx-delay-ps", 0);
918*9b16ede4SIcenowy Zheng if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
919*9b16ede4SIcenowy Zheng printf("%s: Invalid RX delay value %d\n", __func__,
920*9b16ede4SIcenowy Zheng sun8i_pdata->rx_delay_ps);
921*9b16ede4SIcenowy Zheng
9224d555ae3SPhilipp Tomsich #ifdef CONFIG_DM_GPIO
923da409cccSSimon Glass if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
9244d555ae3SPhilipp Tomsich "snps,reset-active-low"))
9254d555ae3SPhilipp Tomsich reset_flags |= GPIOD_ACTIVE_LOW;
9264d555ae3SPhilipp Tomsich
9274d555ae3SPhilipp Tomsich ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
9284d555ae3SPhilipp Tomsich &priv->reset_gpio, reset_flags);
9294d555ae3SPhilipp Tomsich
9304d555ae3SPhilipp Tomsich if (ret == 0) {
931da409cccSSimon Glass ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
9324d555ae3SPhilipp Tomsich "snps,reset-delays-us",
9334d555ae3SPhilipp Tomsich sun8i_pdata->reset_delays, 3);
9344d555ae3SPhilipp Tomsich } else if (ret == -ENOENT) {
9354d555ae3SPhilipp Tomsich ret = 0;
9364d555ae3SPhilipp Tomsich }
9374d555ae3SPhilipp Tomsich #endif
9384d555ae3SPhilipp Tomsich
939a29710c5SAmit Singh Tomar return 0;
940a29710c5SAmit Singh Tomar }
941a29710c5SAmit Singh Tomar
942a29710c5SAmit Singh Tomar static const struct udevice_id sun8i_emac_eth_ids[] = {
943a29710c5SAmit Singh Tomar {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
944a29710c5SAmit Singh Tomar {.compatible = "allwinner,sun50i-a64-emac",
945a29710c5SAmit Singh Tomar .data = (uintptr_t)A64_EMAC },
946a29710c5SAmit Singh Tomar {.compatible = "allwinner,sun8i-a83t-emac",
947a29710c5SAmit Singh Tomar .data = (uintptr_t)A83T_EMAC },
948e46d73faSLothar Felten {.compatible = "allwinner,sun8i-r40-gmac",
949e46d73faSLothar Felten .data = (uintptr_t)R40_GMAC },
950a29710c5SAmit Singh Tomar { }
951a29710c5SAmit Singh Tomar };
952a29710c5SAmit Singh Tomar
953a29710c5SAmit Singh Tomar U_BOOT_DRIVER(eth_sun8i_emac) = {
954a29710c5SAmit Singh Tomar .name = "eth_sun8i_emac",
955a29710c5SAmit Singh Tomar .id = UCLASS_ETH,
956a29710c5SAmit Singh Tomar .of_match = sun8i_emac_eth_ids,
957a29710c5SAmit Singh Tomar .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
958a29710c5SAmit Singh Tomar .probe = sun8i_emac_eth_probe,
959a29710c5SAmit Singh Tomar .ops = &sun8i_emac_eth_ops,
960a29710c5SAmit Singh Tomar .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
9614d555ae3SPhilipp Tomsich .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
962a29710c5SAmit Singh Tomar .flags = DM_FLAG_ALLOC_PRIV_DMA,
963a29710c5SAmit Singh Tomar };
964