1a29710c5SAmit Singh Tomar /*
2a29710c5SAmit Singh Tomar * (C) Copyright 2016
3a29710c5SAmit Singh Tomar * Author: Amit Singh Tomar, amittomer25@gmail.com
4a29710c5SAmit Singh Tomar *
5a29710c5SAmit Singh Tomar * SPDX-License-Identifier: GPL-2.0+
6a29710c5SAmit Singh Tomar *
7a29710c5SAmit Singh Tomar * Ethernet driver for H3/A64/A83T based SoC's
8a29710c5SAmit Singh Tomar *
9a29710c5SAmit Singh Tomar * It is derived from the work done by
10a29710c5SAmit Singh Tomar * LABBE Corentin & Chen-Yu Tsai for Linux, THANKS!
11a29710c5SAmit Singh Tomar *
12a29710c5SAmit Singh Tomar */
13a29710c5SAmit Singh Tomar
14a29710c5SAmit Singh Tomar #include <asm/io.h>
15a29710c5SAmit Singh Tomar #include <asm/arch/clock.h>
16a29710c5SAmit Singh Tomar #include <asm/arch/gpio.h>
17a29710c5SAmit Singh Tomar #include <common.h>
18a29710c5SAmit Singh Tomar #include <dm.h>
19a29710c5SAmit Singh Tomar #include <fdt_support.h>
20a29710c5SAmit Singh Tomar #include <linux/err.h>
21a29710c5SAmit Singh Tomar #include <malloc.h>
22a29710c5SAmit Singh Tomar #include <miiphy.h>
23a29710c5SAmit Singh Tomar #include <net.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
63a29710c5SAmit Singh Tomar
64a29710c5SAmit Singh Tomar #define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
65a29710c5SAmit Singh Tomar
66a29710c5SAmit Singh Tomar #define AHB_GATE_OFFSET_EPHY 0
67a29710c5SAmit Singh Tomar
687b82a229SAndre Przywara #if defined(CONFIG_MACH_SUNXI_H3_H5)
69a29710c5SAmit Singh Tomar #define SUN8I_GPD8_GMAC 2
70a29710c5SAmit Singh Tomar #else
71a29710c5SAmit Singh Tomar #define SUN8I_GPD8_GMAC 4
72a29710c5SAmit Singh Tomar #endif
73a29710c5SAmit Singh Tomar
74a29710c5SAmit Singh Tomar /* H3/A64 EMAC Register's offset */
75a29710c5SAmit Singh Tomar #define EMAC_CTL0 0x00
76a29710c5SAmit Singh Tomar #define EMAC_CTL1 0x04
77a29710c5SAmit Singh Tomar #define EMAC_INT_STA 0x08
78a29710c5SAmit Singh Tomar #define EMAC_INT_EN 0x0c
79a29710c5SAmit Singh Tomar #define EMAC_TX_CTL0 0x10
80a29710c5SAmit Singh Tomar #define EMAC_TX_CTL1 0x14
81a29710c5SAmit Singh Tomar #define EMAC_TX_FLOW_CTL 0x1c
82a29710c5SAmit Singh Tomar #define EMAC_TX_DMA_DESC 0x20
83a29710c5SAmit Singh Tomar #define EMAC_RX_CTL0 0x24
84a29710c5SAmit Singh Tomar #define EMAC_RX_CTL1 0x28
85a29710c5SAmit Singh Tomar #define EMAC_RX_DMA_DESC 0x34
86a29710c5SAmit Singh Tomar #define EMAC_MII_CMD 0x48
87a29710c5SAmit Singh Tomar #define EMAC_MII_DATA 0x4c
88a29710c5SAmit Singh Tomar #define EMAC_ADDR0_HIGH 0x50
89a29710c5SAmit Singh Tomar #define EMAC_ADDR0_LOW 0x54
90a29710c5SAmit Singh Tomar #define EMAC_TX_DMA_STA 0xb0
91a29710c5SAmit Singh Tomar #define EMAC_TX_CUR_DESC 0xb4
92a29710c5SAmit Singh Tomar #define EMAC_TX_CUR_BUF 0xb8
93a29710c5SAmit Singh Tomar #define EMAC_RX_DMA_STA 0xc0
94a29710c5SAmit Singh Tomar #define EMAC_RX_CUR_DESC 0xc4
95a29710c5SAmit Singh Tomar
96a29710c5SAmit Singh Tomar DECLARE_GLOBAL_DATA_PTR;
97a29710c5SAmit Singh Tomar
98a29710c5SAmit Singh Tomar enum emac_variant {
99a29710c5SAmit Singh Tomar A83T_EMAC = 1,
100a29710c5SAmit Singh Tomar H3_EMAC,
101a29710c5SAmit Singh Tomar A64_EMAC,
102a29710c5SAmit Singh Tomar };
103a29710c5SAmit Singh Tomar
104a29710c5SAmit Singh Tomar struct emac_dma_desc {
105a29710c5SAmit Singh Tomar u32 status;
106a29710c5SAmit Singh Tomar u32 st;
107a29710c5SAmit Singh Tomar u32 buf_addr;
108a29710c5SAmit Singh Tomar u32 next;
109a29710c5SAmit Singh Tomar } __aligned(ARCH_DMA_MINALIGN);
110a29710c5SAmit Singh Tomar
111a29710c5SAmit Singh Tomar struct emac_eth_dev {
112a29710c5SAmit Singh Tomar struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
113a29710c5SAmit Singh Tomar struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
114a29710c5SAmit Singh Tomar char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
115a29710c5SAmit Singh Tomar char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
116a29710c5SAmit Singh Tomar
117a29710c5SAmit Singh Tomar u32 interface;
118a29710c5SAmit Singh Tomar u32 phyaddr;
119a29710c5SAmit Singh Tomar u32 link;
120a29710c5SAmit Singh Tomar u32 speed;
121a29710c5SAmit Singh Tomar u32 duplex;
122a29710c5SAmit Singh Tomar u32 phy_configured;
123a29710c5SAmit Singh Tomar u32 tx_currdescnum;
124a29710c5SAmit Singh Tomar u32 rx_currdescnum;
125a29710c5SAmit Singh Tomar u32 addr;
126a29710c5SAmit Singh Tomar u32 tx_slot;
127a29710c5SAmit Singh Tomar bool use_internal_phy;
128a29710c5SAmit Singh Tomar
129a29710c5SAmit Singh Tomar enum emac_variant variant;
130a29710c5SAmit Singh Tomar void *mac_reg;
131a29710c5SAmit Singh Tomar phys_addr_t sysctl_reg;
132a29710c5SAmit Singh Tomar struct phy_device *phydev;
133a29710c5SAmit Singh Tomar struct mii_dev *bus;
1344d555ae3SPhilipp Tomsich #ifdef CONFIG_DM_GPIO
1354d555ae3SPhilipp Tomsich struct gpio_desc reset_gpio;
1364d555ae3SPhilipp Tomsich #endif
137a29710c5SAmit Singh Tomar };
138a29710c5SAmit Singh Tomar
1394d555ae3SPhilipp Tomsich
1404d555ae3SPhilipp Tomsich struct sun8i_eth_pdata {
1414d555ae3SPhilipp Tomsich struct eth_pdata eth_pdata;
1424d555ae3SPhilipp Tomsich u32 reset_delays[3];
1434d555ae3SPhilipp Tomsich };
1444d555ae3SPhilipp Tomsich
1454d555ae3SPhilipp Tomsich
sun8i_mdio_read(struct mii_dev * bus,int addr,int devad,int reg)146a29710c5SAmit Singh Tomar static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
147a29710c5SAmit Singh Tomar {
1484d555ae3SPhilipp Tomsich struct udevice *dev = bus->priv;
1494d555ae3SPhilipp Tomsich struct emac_eth_dev *priv = dev_get_priv(dev);
150a29710c5SAmit Singh Tomar ulong start;
151a29710c5SAmit Singh Tomar u32 miiaddr = 0;
152a29710c5SAmit Singh Tomar int timeout = CONFIG_MDIO_TIMEOUT;
153a29710c5SAmit Singh Tomar
154a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_WRITE;
155a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
156a29710c5SAmit Singh Tomar miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
157a29710c5SAmit Singh Tomar MDIO_CMD_MII_PHY_REG_ADDR_MASK;
158a29710c5SAmit Singh Tomar
159a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
160a29710c5SAmit Singh Tomar
161a29710c5SAmit Singh Tomar miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
162a29710c5SAmit Singh Tomar MDIO_CMD_MII_PHY_ADDR_MASK;
163a29710c5SAmit Singh Tomar
164a29710c5SAmit Singh Tomar miiaddr |= MDIO_CMD_MII_BUSY;
165a29710c5SAmit Singh Tomar
166a29710c5SAmit Singh Tomar writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
167a29710c5SAmit Singh Tomar
168a29710c5SAmit Singh Tomar start = get_timer(0);
169a29710c5SAmit Singh Tomar while (get_timer(start) < timeout) {
170a29710c5SAmit Singh Tomar if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY))
171a29710c5SAmit Singh Tomar return readl(priv->mac_reg + EMAC_MII_DATA);
172a29710c5SAmit Singh Tomar udelay(10);
173a29710c5SAmit Singh Tomar };
174a29710c5SAmit Singh Tomar
175a29710c5SAmit Singh Tomar return -1;
176a29710c5SAmit Singh Tomar }
177a29710c5SAmit Singh Tomar
sun8i_mdio_write(struct mii_dev * bus,int addr,int devad,int reg,u16 val)178a29710c5SAmit Singh Tomar static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
179a29710c5SAmit Singh Tomar u16 val)
180a29710c5SAmit Singh Tomar {
1814d555ae3SPhilipp Tomsich struct udevice *dev = bus->priv;
1824d555ae3SPhilipp Tomsich struct emac_eth_dev *priv = dev_get_priv(dev);
183a29710c5SAmit Singh Tomar ulong start;
184a29710c5SAmit Singh Tomar u32 miiaddr = 0;
185a29710c5SAmit Singh Tomar int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
186a29710c5SAmit Singh Tomar
187a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
188a29710c5SAmit Singh Tomar miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
189a29710c5SAmit Singh Tomar MDIO_CMD_MII_PHY_REG_ADDR_MASK;
190a29710c5SAmit Singh Tomar
191a29710c5SAmit Singh Tomar miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
192a29710c5SAmit Singh Tomar miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
193a29710c5SAmit Singh Tomar MDIO_CMD_MII_PHY_ADDR_MASK;
194a29710c5SAmit Singh Tomar
195a29710c5SAmit Singh Tomar miiaddr |= MDIO_CMD_MII_WRITE;
196a29710c5SAmit Singh Tomar miiaddr |= MDIO_CMD_MII_BUSY;
197a29710c5SAmit Singh Tomar
198a29710c5SAmit Singh Tomar writel(val, priv->mac_reg + EMAC_MII_DATA);
1991deeecb6SPhilipp Tomsich writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
200a29710c5SAmit Singh Tomar
201a29710c5SAmit Singh Tomar start = get_timer(0);
202a29710c5SAmit Singh Tomar while (get_timer(start) < timeout) {
203a29710c5SAmit Singh Tomar if (!(readl(priv->mac_reg + EMAC_MII_CMD) &
204a29710c5SAmit Singh Tomar MDIO_CMD_MII_BUSY)) {
205a29710c5SAmit Singh Tomar ret = 0;
206a29710c5SAmit Singh Tomar break;
207a29710c5SAmit Singh Tomar }
208a29710c5SAmit Singh Tomar udelay(10);
209a29710c5SAmit Singh Tomar };
210a29710c5SAmit Singh Tomar
211a29710c5SAmit Singh Tomar return ret;
212a29710c5SAmit Singh Tomar }
213a29710c5SAmit Singh Tomar
_sun8i_write_hwaddr(struct emac_eth_dev * priv,u8 * mac_id)214a29710c5SAmit Singh Tomar static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
215a29710c5SAmit Singh Tomar {
216a29710c5SAmit Singh Tomar u32 macid_lo, macid_hi;
217a29710c5SAmit Singh Tomar
218a29710c5SAmit Singh Tomar macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
219a29710c5SAmit Singh Tomar (mac_id[3] << 24);
220a29710c5SAmit Singh Tomar macid_hi = mac_id[4] + (mac_id[5] << 8);
221a29710c5SAmit Singh Tomar
222a29710c5SAmit Singh Tomar writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
223a29710c5SAmit Singh Tomar writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
224a29710c5SAmit Singh Tomar
225a29710c5SAmit Singh Tomar return 0;
226a29710c5SAmit Singh Tomar }
227a29710c5SAmit Singh Tomar
sun8i_adjust_link(struct emac_eth_dev * priv,struct phy_device * phydev)228a29710c5SAmit Singh Tomar static void sun8i_adjust_link(struct emac_eth_dev *priv,
229a29710c5SAmit Singh Tomar struct phy_device *phydev)
230a29710c5SAmit Singh Tomar {
231a29710c5SAmit Singh Tomar u32 v;
232a29710c5SAmit Singh Tomar
233a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_CTL0);
234a29710c5SAmit Singh Tomar
235a29710c5SAmit Singh Tomar if (phydev->duplex)
236a29710c5SAmit Singh Tomar v |= BIT(0);
237a29710c5SAmit Singh Tomar else
238a29710c5SAmit Singh Tomar v &= ~BIT(0);
239a29710c5SAmit Singh Tomar
240a29710c5SAmit Singh Tomar v &= ~0x0C;
241a29710c5SAmit Singh Tomar
242a29710c5SAmit Singh Tomar switch (phydev->speed) {
243a29710c5SAmit Singh Tomar case 1000:
244a29710c5SAmit Singh Tomar break;
245a29710c5SAmit Singh Tomar case 100:
246a29710c5SAmit Singh Tomar v |= BIT(2);
247a29710c5SAmit Singh Tomar v |= BIT(3);
248a29710c5SAmit Singh Tomar break;
249a29710c5SAmit Singh Tomar case 10:
250a29710c5SAmit Singh Tomar v |= BIT(3);
251a29710c5SAmit Singh Tomar break;
252a29710c5SAmit Singh Tomar }
253a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_CTL0);
254a29710c5SAmit Singh Tomar }
255a29710c5SAmit Singh Tomar
sun8i_emac_set_syscon_ephy(struct emac_eth_dev * priv,u32 * reg)256a29710c5SAmit Singh Tomar static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
257a29710c5SAmit Singh Tomar {
258a29710c5SAmit Singh Tomar if (priv->use_internal_phy) {
259a29710c5SAmit Singh Tomar /* H3 based SoC's that has an Internal 100MBit PHY
260a29710c5SAmit Singh Tomar * needs to be configured and powered up before use
261a29710c5SAmit Singh Tomar */
262a29710c5SAmit Singh Tomar *reg &= ~H3_EPHY_DEFAULT_MASK;
263a29710c5SAmit Singh Tomar *reg |= H3_EPHY_DEFAULT_VALUE;
264a29710c5SAmit Singh Tomar *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
265a29710c5SAmit Singh Tomar *reg &= ~H3_EPHY_SHUTDOWN;
266a29710c5SAmit Singh Tomar *reg |= H3_EPHY_SELECT;
267a29710c5SAmit Singh Tomar } else
268a29710c5SAmit Singh Tomar /* This is to select External Gigabit PHY on
269a29710c5SAmit Singh Tomar * the boards with H3 SoC.
270a29710c5SAmit Singh Tomar */
271a29710c5SAmit Singh Tomar *reg &= ~H3_EPHY_SELECT;
272a29710c5SAmit Singh Tomar
273a29710c5SAmit Singh Tomar return 0;
274a29710c5SAmit Singh Tomar }
275a29710c5SAmit Singh Tomar
sun8i_emac_set_syscon(struct emac_eth_dev * priv)276a29710c5SAmit Singh Tomar static int sun8i_emac_set_syscon(struct emac_eth_dev *priv)
277a29710c5SAmit Singh Tomar {
278a29710c5SAmit Singh Tomar int ret;
279a29710c5SAmit Singh Tomar u32 reg;
280a29710c5SAmit Singh Tomar
281a29710c5SAmit Singh Tomar reg = readl(priv->sysctl_reg);
282a29710c5SAmit Singh Tomar
283a29710c5SAmit Singh Tomar if (priv->variant == H3_EMAC) {
284a29710c5SAmit Singh Tomar ret = sun8i_emac_set_syscon_ephy(priv, ®);
285a29710c5SAmit Singh Tomar if (ret)
286a29710c5SAmit Singh Tomar return ret;
287a29710c5SAmit Singh Tomar }
288a29710c5SAmit Singh Tomar
289a29710c5SAmit Singh Tomar reg &= ~(SC_ETCS_MASK | SC_EPIT);
290a29710c5SAmit Singh Tomar if (priv->variant == H3_EMAC || priv->variant == A64_EMAC)
291a29710c5SAmit Singh Tomar reg &= ~SC_RMII_EN;
292a29710c5SAmit Singh Tomar
293a29710c5SAmit Singh Tomar switch (priv->interface) {
294a29710c5SAmit Singh Tomar case PHY_INTERFACE_MODE_MII:
295a29710c5SAmit Singh Tomar /* default */
296a29710c5SAmit Singh Tomar break;
297a29710c5SAmit Singh Tomar case PHY_INTERFACE_MODE_RGMII:
298a29710c5SAmit Singh Tomar reg |= SC_EPIT | SC_ETCS_INT_GMII;
299a29710c5SAmit Singh Tomar break;
300a29710c5SAmit Singh Tomar case PHY_INTERFACE_MODE_RMII:
301a29710c5SAmit Singh Tomar if (priv->variant == H3_EMAC ||
302a29710c5SAmit Singh Tomar priv->variant == A64_EMAC) {
303a29710c5SAmit Singh Tomar reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
304a29710c5SAmit Singh Tomar break;
305a29710c5SAmit Singh Tomar }
306a29710c5SAmit Singh Tomar /* RMII not supported on A83T */
307a29710c5SAmit Singh Tomar default:
308a29710c5SAmit Singh Tomar debug("%s: Invalid PHY interface\n", __func__);
309a29710c5SAmit Singh Tomar return -EINVAL;
310a29710c5SAmit Singh Tomar }
311a29710c5SAmit Singh Tomar
312a29710c5SAmit Singh Tomar writel(reg, priv->sysctl_reg);
313a29710c5SAmit Singh Tomar
314a29710c5SAmit Singh Tomar return 0;
315a29710c5SAmit Singh Tomar }
316a29710c5SAmit Singh Tomar
sun8i_phy_init(struct emac_eth_dev * priv,void * dev)317a29710c5SAmit Singh Tomar static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
318a29710c5SAmit Singh Tomar {
319a29710c5SAmit Singh Tomar struct phy_device *phydev;
320a29710c5SAmit Singh Tomar
321a29710c5SAmit Singh Tomar phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
322a29710c5SAmit Singh Tomar if (!phydev)
323a29710c5SAmit Singh Tomar return -ENODEV;
324a29710c5SAmit Singh Tomar
325a29710c5SAmit Singh Tomar phy_connect_dev(phydev, dev);
326a29710c5SAmit Singh Tomar
327a29710c5SAmit Singh Tomar priv->phydev = phydev;
328a29710c5SAmit Singh Tomar phy_config(priv->phydev);
329a29710c5SAmit Singh Tomar
330a29710c5SAmit Singh Tomar return 0;
331a29710c5SAmit Singh Tomar }
332a29710c5SAmit Singh Tomar
rx_descs_init(struct emac_eth_dev * priv)333a29710c5SAmit Singh Tomar static void rx_descs_init(struct emac_eth_dev *priv)
334a29710c5SAmit Singh Tomar {
335a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
336a29710c5SAmit Singh Tomar char *rxbuffs = &priv->rxbuffer[0];
337a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p;
338a29710c5SAmit Singh Tomar u32 idx;
339a29710c5SAmit Singh Tomar
340a29710c5SAmit Singh Tomar /* flush Rx buffers */
341a29710c5SAmit Singh Tomar flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
342a29710c5SAmit Singh Tomar RX_TOTAL_BUFSIZE);
343a29710c5SAmit Singh Tomar
344a29710c5SAmit Singh Tomar for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
345a29710c5SAmit Singh Tomar desc_p = &desc_table_p[idx];
346a29710c5SAmit Singh Tomar desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
347a29710c5SAmit Singh Tomar ;
348a29710c5SAmit Singh Tomar desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
3494069437dSHans de Goede desc_p->st |= CONFIG_ETH_RXSIZE;
350a29710c5SAmit Singh Tomar desc_p->status = BIT(31);
351a29710c5SAmit Singh Tomar }
352a29710c5SAmit Singh Tomar
353a29710c5SAmit Singh Tomar /* Correcting the last pointer of the chain */
354a29710c5SAmit Singh Tomar desc_p->next = (uintptr_t)&desc_table_p[0];
355a29710c5SAmit Singh Tomar
356a29710c5SAmit Singh Tomar flush_dcache_range((uintptr_t)priv->rx_chain,
357a29710c5SAmit Singh Tomar (uintptr_t)priv->rx_chain +
358a29710c5SAmit Singh Tomar sizeof(priv->rx_chain));
359a29710c5SAmit Singh Tomar
360a29710c5SAmit Singh Tomar writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
361a29710c5SAmit Singh Tomar priv->rx_currdescnum = 0;
362a29710c5SAmit Singh Tomar }
363a29710c5SAmit Singh Tomar
tx_descs_init(struct emac_eth_dev * priv)364a29710c5SAmit Singh Tomar static void tx_descs_init(struct emac_eth_dev *priv)
365a29710c5SAmit Singh Tomar {
366a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
367a29710c5SAmit Singh Tomar char *txbuffs = &priv->txbuffer[0];
368a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p;
369a29710c5SAmit Singh Tomar u32 idx;
370a29710c5SAmit Singh Tomar
371a29710c5SAmit Singh Tomar for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
372a29710c5SAmit Singh Tomar desc_p = &desc_table_p[idx];
373a29710c5SAmit Singh Tomar desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
374a29710c5SAmit Singh Tomar ;
375a29710c5SAmit Singh Tomar desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
376a29710c5SAmit Singh Tomar desc_p->status = (1 << 31);
377a29710c5SAmit Singh Tomar desc_p->st = 0;
378a29710c5SAmit Singh Tomar }
379a29710c5SAmit Singh Tomar
380a29710c5SAmit Singh Tomar /* Correcting the last pointer of the chain */
381a29710c5SAmit Singh Tomar desc_p->next = (uintptr_t)&desc_table_p[0];
382a29710c5SAmit Singh Tomar
383a29710c5SAmit Singh Tomar /* Flush all Tx buffer descriptors */
384a29710c5SAmit Singh Tomar flush_dcache_range((uintptr_t)priv->tx_chain,
385a29710c5SAmit Singh Tomar (uintptr_t)priv->tx_chain +
386a29710c5SAmit Singh Tomar sizeof(priv->tx_chain));
387a29710c5SAmit Singh Tomar
388a29710c5SAmit Singh Tomar writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
389a29710c5SAmit Singh Tomar priv->tx_currdescnum = 0;
390a29710c5SAmit Singh Tomar }
391a29710c5SAmit Singh Tomar
_sun8i_emac_eth_init(struct emac_eth_dev * priv,u8 * enetaddr)392a29710c5SAmit Singh Tomar static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
393a29710c5SAmit Singh Tomar {
394a29710c5SAmit Singh Tomar u32 reg, v;
395a29710c5SAmit Singh Tomar int timeout = 100;
396a29710c5SAmit Singh Tomar
397a29710c5SAmit Singh Tomar reg = readl((priv->mac_reg + EMAC_CTL1));
398a29710c5SAmit Singh Tomar
399a29710c5SAmit Singh Tomar if (!(reg & 0x1)) {
400a29710c5SAmit Singh Tomar /* Soft reset MAC */
401a29710c5SAmit Singh Tomar setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
402a29710c5SAmit Singh Tomar do {
403a29710c5SAmit Singh Tomar reg = readl(priv->mac_reg + EMAC_CTL1);
404a29710c5SAmit Singh Tomar } while ((reg & 0x01) != 0 && (--timeout));
405a29710c5SAmit Singh Tomar if (!timeout) {
406a29710c5SAmit Singh Tomar printf("%s: Timeout\n", __func__);
407a29710c5SAmit Singh Tomar return -1;
408a29710c5SAmit Singh Tomar }
409a29710c5SAmit Singh Tomar }
410a29710c5SAmit Singh Tomar
411a29710c5SAmit Singh Tomar /* Rewrite mac address after reset */
412a29710c5SAmit Singh Tomar _sun8i_write_hwaddr(priv, enetaddr);
413a29710c5SAmit Singh Tomar
414a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_TX_CTL1);
415a29710c5SAmit Singh Tomar /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
416a29710c5SAmit Singh Tomar v |= BIT(1);
417a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_TX_CTL1);
418a29710c5SAmit Singh Tomar
419a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_RX_CTL1);
420a29710c5SAmit Singh Tomar /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
421a29710c5SAmit Singh Tomar * complete frame has been written to RX DMA FIFO
422a29710c5SAmit Singh Tomar */
423a29710c5SAmit Singh Tomar v |= BIT(1);
424a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_RX_CTL1);
425a29710c5SAmit Singh Tomar
426a29710c5SAmit Singh Tomar /* DMA */
427a29710c5SAmit Singh Tomar writel(8 << 24, priv->mac_reg + EMAC_CTL1);
428a29710c5SAmit Singh Tomar
429a29710c5SAmit Singh Tomar /* Initialize rx/tx descriptors */
430a29710c5SAmit Singh Tomar rx_descs_init(priv);
431a29710c5SAmit Singh Tomar tx_descs_init(priv);
432a29710c5SAmit Singh Tomar
433a29710c5SAmit Singh Tomar /* PHY Start Up */
434a29710c5SAmit Singh Tomar genphy_parse_link(priv->phydev);
435a29710c5SAmit Singh Tomar
436a29710c5SAmit Singh Tomar sun8i_adjust_link(priv, priv->phydev);
437a29710c5SAmit Singh Tomar
438a29710c5SAmit Singh Tomar /* Start RX DMA */
439a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_RX_CTL1);
440a29710c5SAmit Singh Tomar v |= BIT(30);
441a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_RX_CTL1);
442a29710c5SAmit Singh Tomar /* Start TX DMA */
443a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_TX_CTL1);
444a29710c5SAmit Singh Tomar v |= BIT(30);
445a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_TX_CTL1);
446a29710c5SAmit Singh Tomar
447a29710c5SAmit Singh Tomar /* Enable RX/TX */
448a29710c5SAmit Singh Tomar setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
449a29710c5SAmit Singh Tomar setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
450a29710c5SAmit Singh Tomar
451a29710c5SAmit Singh Tomar return 0;
452a29710c5SAmit Singh Tomar }
453a29710c5SAmit Singh Tomar
parse_phy_pins(struct udevice * dev)454a29710c5SAmit Singh Tomar static int parse_phy_pins(struct udevice *dev)
455a29710c5SAmit Singh Tomar {
456a29710c5SAmit Singh Tomar int offset;
457a29710c5SAmit Singh Tomar const char *pin_name;
458a29710c5SAmit Singh Tomar int drive, pull, i;
459a29710c5SAmit Singh Tomar
460e160f7d4SSimon Glass offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
461a29710c5SAmit Singh Tomar "pinctrl-0");
462a29710c5SAmit Singh Tomar if (offset < 0) {
463a29710c5SAmit Singh Tomar printf("WARNING: emac: cannot find pinctrl-0 node\n");
464a29710c5SAmit Singh Tomar return offset;
465a29710c5SAmit Singh Tomar }
466a29710c5SAmit Singh Tomar
467a29710c5SAmit Singh Tomar drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
468a29710c5SAmit Singh Tomar "allwinner,drive", 4);
469a29710c5SAmit Singh Tomar pull = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
470a29710c5SAmit Singh Tomar "allwinner,pull", 0);
471a29710c5SAmit Singh Tomar for (i = 0; ; i++) {
472a29710c5SAmit Singh Tomar int pin;
473a29710c5SAmit Singh Tomar
474b02e4044SSimon Glass pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
475b02e4044SSimon Glass "allwinner,pins", i, NULL);
476b02e4044SSimon Glass if (!pin_name)
477a29710c5SAmit Singh Tomar break;
478a29710c5SAmit Singh Tomar if (pin_name[0] != 'P')
479a29710c5SAmit Singh Tomar continue;
480a29710c5SAmit Singh Tomar pin = (pin_name[1] - 'A') << 5;
481a29710c5SAmit Singh Tomar if (pin >= 26 << 5)
482a29710c5SAmit Singh Tomar continue;
483a29710c5SAmit Singh Tomar pin += simple_strtol(&pin_name[2], NULL, 10);
484a29710c5SAmit Singh Tomar
485a29710c5SAmit Singh Tomar sunxi_gpio_set_cfgpin(pin, SUN8I_GPD8_GMAC);
486a29710c5SAmit Singh Tomar sunxi_gpio_set_drv(pin, drive);
487a29710c5SAmit Singh Tomar sunxi_gpio_set_pull(pin, pull);
488a29710c5SAmit Singh Tomar }
489a29710c5SAmit Singh Tomar
490a29710c5SAmit Singh Tomar if (!i) {
491a29710c5SAmit Singh Tomar printf("WARNING: emac: cannot find allwinner,pins property\n");
492a29710c5SAmit Singh Tomar return -2;
493a29710c5SAmit Singh Tomar }
494a29710c5SAmit Singh Tomar
495a29710c5SAmit Singh Tomar return 0;
496a29710c5SAmit Singh Tomar }
497a29710c5SAmit Singh Tomar
_sun8i_eth_recv(struct emac_eth_dev * priv,uchar ** packetp)498a29710c5SAmit Singh Tomar static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
499a29710c5SAmit Singh Tomar {
500a29710c5SAmit Singh Tomar u32 status, desc_num = priv->rx_currdescnum;
501a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
502a29710c5SAmit Singh Tomar int length = -EAGAIN;
503a29710c5SAmit Singh Tomar int good_packet = 1;
504a29710c5SAmit Singh Tomar uintptr_t desc_start = (uintptr_t)desc_p;
505a29710c5SAmit Singh Tomar uintptr_t desc_end = desc_start +
506a29710c5SAmit Singh Tomar roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
507a29710c5SAmit Singh Tomar
508a29710c5SAmit Singh Tomar ulong data_start = (uintptr_t)desc_p->buf_addr;
509a29710c5SAmit Singh Tomar ulong data_end;
510a29710c5SAmit Singh Tomar
511a29710c5SAmit Singh Tomar /* Invalidate entire buffer descriptor */
512a29710c5SAmit Singh Tomar invalidate_dcache_range(desc_start, desc_end);
513a29710c5SAmit Singh Tomar
514a29710c5SAmit Singh Tomar status = desc_p->status;
515a29710c5SAmit Singh Tomar
516a29710c5SAmit Singh Tomar /* Check for DMA own bit */
517a29710c5SAmit Singh Tomar if (!(status & BIT(31))) {
518a29710c5SAmit Singh Tomar length = (desc_p->status >> 16) & 0x3FFF;
519a29710c5SAmit Singh Tomar
520a29710c5SAmit Singh Tomar if (length < 0x40) {
521a29710c5SAmit Singh Tomar good_packet = 0;
522a29710c5SAmit Singh Tomar debug("RX: Bad Packet (runt)\n");
523a29710c5SAmit Singh Tomar }
524a29710c5SAmit Singh Tomar
525a29710c5SAmit Singh Tomar data_end = data_start + length;
526a29710c5SAmit Singh Tomar /* Invalidate received data */
527a29710c5SAmit Singh Tomar invalidate_dcache_range(rounddown(data_start,
528a29710c5SAmit Singh Tomar ARCH_DMA_MINALIGN),
529a29710c5SAmit Singh Tomar roundup(data_end,
530a29710c5SAmit Singh Tomar ARCH_DMA_MINALIGN));
531a29710c5SAmit Singh Tomar if (good_packet) {
5324069437dSHans de Goede if (length > CONFIG_ETH_RXSIZE) {
533a29710c5SAmit Singh Tomar printf("Received packet is too big (len=%d)\n",
534a29710c5SAmit Singh Tomar length);
535a29710c5SAmit Singh Tomar return -EMSGSIZE;
536a29710c5SAmit Singh Tomar }
537a29710c5SAmit Singh Tomar *packetp = (uchar *)(ulong)desc_p->buf_addr;
538a29710c5SAmit Singh Tomar return length;
539a29710c5SAmit Singh Tomar }
540a29710c5SAmit Singh Tomar }
541a29710c5SAmit Singh Tomar
542a29710c5SAmit Singh Tomar return length;
543a29710c5SAmit Singh Tomar }
544a29710c5SAmit Singh Tomar
_sun8i_emac_eth_send(struct emac_eth_dev * priv,void * packet,int len)545a29710c5SAmit Singh Tomar static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
546a29710c5SAmit Singh Tomar int len)
547a29710c5SAmit Singh Tomar {
548a29710c5SAmit Singh Tomar u32 v, desc_num = priv->tx_currdescnum;
549a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
550a29710c5SAmit Singh Tomar uintptr_t desc_start = (uintptr_t)desc_p;
551a29710c5SAmit Singh Tomar uintptr_t desc_end = desc_start +
552a29710c5SAmit Singh Tomar roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
553a29710c5SAmit Singh Tomar
554a29710c5SAmit Singh Tomar uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
555a29710c5SAmit Singh Tomar uintptr_t data_end = data_start +
556a29710c5SAmit Singh Tomar roundup(len, ARCH_DMA_MINALIGN);
557a29710c5SAmit Singh Tomar
558a29710c5SAmit Singh Tomar /* Invalidate entire buffer descriptor */
559a29710c5SAmit Singh Tomar invalidate_dcache_range(desc_start, desc_end);
560a29710c5SAmit Singh Tomar
561a29710c5SAmit Singh Tomar desc_p->st = len;
562a29710c5SAmit Singh Tomar /* Mandatory undocumented bit */
563a29710c5SAmit Singh Tomar desc_p->st |= BIT(24);
564a29710c5SAmit Singh Tomar
565a29710c5SAmit Singh Tomar memcpy((void *)data_start, packet, len);
566a29710c5SAmit Singh Tomar
567a29710c5SAmit Singh Tomar /* Flush data to be sent */
568a29710c5SAmit Singh Tomar flush_dcache_range(data_start, data_end);
569a29710c5SAmit Singh Tomar
570a29710c5SAmit Singh Tomar /* frame end */
571a29710c5SAmit Singh Tomar desc_p->st |= BIT(30);
572a29710c5SAmit Singh Tomar desc_p->st |= BIT(31);
573a29710c5SAmit Singh Tomar
574a29710c5SAmit Singh Tomar /*frame begin */
575a29710c5SAmit Singh Tomar desc_p->st |= BIT(29);
576a29710c5SAmit Singh Tomar desc_p->status = BIT(31);
577a29710c5SAmit Singh Tomar
578a29710c5SAmit Singh Tomar /*Descriptors st and status field has changed, so FLUSH it */
579a29710c5SAmit Singh Tomar flush_dcache_range(desc_start, desc_end);
580a29710c5SAmit Singh Tomar
581a29710c5SAmit Singh Tomar /* Move to next Descriptor and wrap around */
582a29710c5SAmit Singh Tomar if (++desc_num >= CONFIG_TX_DESCR_NUM)
583a29710c5SAmit Singh Tomar desc_num = 0;
584a29710c5SAmit Singh Tomar priv->tx_currdescnum = desc_num;
585a29710c5SAmit Singh Tomar
586a29710c5SAmit Singh Tomar /* Start the DMA */
587a29710c5SAmit Singh Tomar v = readl(priv->mac_reg + EMAC_TX_CTL1);
588a29710c5SAmit Singh Tomar v |= BIT(31);/* mandatory */
589a29710c5SAmit Singh Tomar v |= BIT(30);/* mandatory */
590a29710c5SAmit Singh Tomar writel(v, priv->mac_reg + EMAC_TX_CTL1);
591a29710c5SAmit Singh Tomar
592a29710c5SAmit Singh Tomar return 0;
593a29710c5SAmit Singh Tomar }
594a29710c5SAmit Singh Tomar
sun8i_eth_write_hwaddr(struct udevice * dev)595a29710c5SAmit Singh Tomar static int sun8i_eth_write_hwaddr(struct udevice *dev)
596a29710c5SAmit Singh Tomar {
597a29710c5SAmit Singh Tomar struct eth_pdata *pdata = dev_get_platdata(dev);
598a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
599a29710c5SAmit Singh Tomar
600a29710c5SAmit Singh Tomar return _sun8i_write_hwaddr(priv, pdata->enetaddr);
601a29710c5SAmit Singh Tomar }
602a29710c5SAmit Singh Tomar
sun8i_emac_board_setup(struct emac_eth_dev * priv)603a29710c5SAmit Singh Tomar static void sun8i_emac_board_setup(struct emac_eth_dev *priv)
604a29710c5SAmit Singh Tomar {
605a29710c5SAmit Singh Tomar struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
606a29710c5SAmit Singh Tomar
607a29710c5SAmit Singh Tomar if (priv->use_internal_phy) {
608a29710c5SAmit Singh Tomar /* Set clock gating for ephy */
609a29710c5SAmit Singh Tomar setbits_le32(&ccm->bus_gate4, BIT(AHB_GATE_OFFSET_EPHY));
610a29710c5SAmit Singh Tomar
611a29710c5SAmit Singh Tomar /* Deassert EPHY */
612a29710c5SAmit Singh Tomar setbits_le32(&ccm->ahb_reset2_cfg, BIT(AHB_RESET_OFFSET_EPHY));
613a29710c5SAmit Singh Tomar }
614a29710c5SAmit Singh Tomar
615a29710c5SAmit Singh Tomar /* Set clock gating for emac */
616a29710c5SAmit Singh Tomar setbits_le32(&ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_GMAC));
617a29710c5SAmit Singh Tomar
618a29710c5SAmit Singh Tomar /* De-assert EMAC */
619a29710c5SAmit Singh Tomar setbits_le32(&ccm->ahb_reset0_cfg, BIT(AHB_RESET_OFFSET_GMAC));
620a29710c5SAmit Singh Tomar }
621a29710c5SAmit Singh Tomar
6224d555ae3SPhilipp Tomsich #if defined(CONFIG_DM_GPIO)
sun8i_mdio_reset(struct mii_dev * bus)6234d555ae3SPhilipp Tomsich static int sun8i_mdio_reset(struct mii_dev *bus)
6244d555ae3SPhilipp Tomsich {
6254d555ae3SPhilipp Tomsich struct udevice *dev = bus->priv;
6264d555ae3SPhilipp Tomsich struct emac_eth_dev *priv = dev_get_priv(dev);
6274d555ae3SPhilipp Tomsich struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
6284d555ae3SPhilipp Tomsich int ret;
6294d555ae3SPhilipp Tomsich
6304d555ae3SPhilipp Tomsich if (!dm_gpio_is_valid(&priv->reset_gpio))
6314d555ae3SPhilipp Tomsich return 0;
6324d555ae3SPhilipp Tomsich
6334d555ae3SPhilipp Tomsich /* reset the phy */
6344d555ae3SPhilipp Tomsich ret = dm_gpio_set_value(&priv->reset_gpio, 0);
6354d555ae3SPhilipp Tomsich if (ret)
6364d555ae3SPhilipp Tomsich return ret;
6374d555ae3SPhilipp Tomsich
6384d555ae3SPhilipp Tomsich udelay(pdata->reset_delays[0]);
6394d555ae3SPhilipp Tomsich
6404d555ae3SPhilipp Tomsich ret = dm_gpio_set_value(&priv->reset_gpio, 1);
6414d555ae3SPhilipp Tomsich if (ret)
6424d555ae3SPhilipp Tomsich return ret;
6434d555ae3SPhilipp Tomsich
6444d555ae3SPhilipp Tomsich udelay(pdata->reset_delays[1]);
6454d555ae3SPhilipp Tomsich
6464d555ae3SPhilipp Tomsich ret = dm_gpio_set_value(&priv->reset_gpio, 0);
6474d555ae3SPhilipp Tomsich if (ret)
6484d555ae3SPhilipp Tomsich return ret;
6494d555ae3SPhilipp Tomsich
6504d555ae3SPhilipp Tomsich udelay(pdata->reset_delays[2]);
6514d555ae3SPhilipp Tomsich
6524d555ae3SPhilipp Tomsich return 0;
6534d555ae3SPhilipp Tomsich }
6544d555ae3SPhilipp Tomsich #endif
6554d555ae3SPhilipp Tomsich
sun8i_mdio_init(const char * name,struct udevice * priv)6564d555ae3SPhilipp Tomsich static int sun8i_mdio_init(const char *name, struct udevice *priv)
657a29710c5SAmit Singh Tomar {
658a29710c5SAmit Singh Tomar struct mii_dev *bus = mdio_alloc();
659a29710c5SAmit Singh Tomar
660a29710c5SAmit Singh Tomar if (!bus) {
661a29710c5SAmit Singh Tomar debug("Failed to allocate MDIO bus\n");
662a29710c5SAmit Singh Tomar return -ENOMEM;
663a29710c5SAmit Singh Tomar }
664a29710c5SAmit Singh Tomar
665a29710c5SAmit Singh Tomar bus->read = sun8i_mdio_read;
666a29710c5SAmit Singh Tomar bus->write = sun8i_mdio_write;
667a29710c5SAmit Singh Tomar snprintf(bus->name, sizeof(bus->name), name);
668a29710c5SAmit Singh Tomar bus->priv = (void *)priv;
6694d555ae3SPhilipp Tomsich #if defined(CONFIG_DM_GPIO)
6704d555ae3SPhilipp Tomsich bus->reset = sun8i_mdio_reset;
6714d555ae3SPhilipp Tomsich #endif
672a29710c5SAmit Singh Tomar
673a29710c5SAmit Singh Tomar return mdio_register(bus);
674a29710c5SAmit Singh Tomar }
675a29710c5SAmit Singh Tomar
sun8i_emac_eth_start(struct udevice * dev)676a29710c5SAmit Singh Tomar static int sun8i_emac_eth_start(struct udevice *dev)
677a29710c5SAmit Singh Tomar {
678a29710c5SAmit Singh Tomar struct eth_pdata *pdata = dev_get_platdata(dev);
679a29710c5SAmit Singh Tomar
680a29710c5SAmit Singh Tomar return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
681a29710c5SAmit Singh Tomar }
682a29710c5SAmit Singh Tomar
sun8i_emac_eth_send(struct udevice * dev,void * packet,int length)683a29710c5SAmit Singh Tomar static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
684a29710c5SAmit Singh Tomar {
685a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
686a29710c5SAmit Singh Tomar
687a29710c5SAmit Singh Tomar return _sun8i_emac_eth_send(priv, packet, length);
688a29710c5SAmit Singh Tomar }
689a29710c5SAmit Singh Tomar
sun8i_emac_eth_recv(struct udevice * dev,int flags,uchar ** packetp)690a29710c5SAmit Singh Tomar static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
691a29710c5SAmit Singh Tomar {
692a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
693a29710c5SAmit Singh Tomar
694a29710c5SAmit Singh Tomar return _sun8i_eth_recv(priv, packetp);
695a29710c5SAmit Singh Tomar }
696a29710c5SAmit Singh Tomar
_sun8i_free_pkt(struct emac_eth_dev * priv)697a29710c5SAmit Singh Tomar static int _sun8i_free_pkt(struct emac_eth_dev *priv)
698a29710c5SAmit Singh Tomar {
699a29710c5SAmit Singh Tomar u32 desc_num = priv->rx_currdescnum;
700a29710c5SAmit Singh Tomar struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
701a29710c5SAmit Singh Tomar uintptr_t desc_start = (uintptr_t)desc_p;
702a29710c5SAmit Singh Tomar uintptr_t desc_end = desc_start +
703a29710c5SAmit Singh Tomar roundup(sizeof(u32), ARCH_DMA_MINALIGN);
704a29710c5SAmit Singh Tomar
705a29710c5SAmit Singh Tomar /* Make the current descriptor valid again */
706a29710c5SAmit Singh Tomar desc_p->status |= BIT(31);
707a29710c5SAmit Singh Tomar
708a29710c5SAmit Singh Tomar /* Flush Status field of descriptor */
709a29710c5SAmit Singh Tomar flush_dcache_range(desc_start, desc_end);
710a29710c5SAmit Singh Tomar
711a29710c5SAmit Singh Tomar /* Move to next desc and wrap-around condition. */
712a29710c5SAmit Singh Tomar if (++desc_num >= CONFIG_RX_DESCR_NUM)
713a29710c5SAmit Singh Tomar desc_num = 0;
714a29710c5SAmit Singh Tomar priv->rx_currdescnum = desc_num;
715a29710c5SAmit Singh Tomar
716a29710c5SAmit Singh Tomar return 0;
717a29710c5SAmit Singh Tomar }
718a29710c5SAmit Singh Tomar
sun8i_eth_free_pkt(struct udevice * dev,uchar * packet,int length)719a29710c5SAmit Singh Tomar static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
720a29710c5SAmit Singh Tomar int length)
721a29710c5SAmit Singh Tomar {
722a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
723a29710c5SAmit Singh Tomar
724a29710c5SAmit Singh Tomar return _sun8i_free_pkt(priv);
725a29710c5SAmit Singh Tomar }
726a29710c5SAmit Singh Tomar
sun8i_emac_eth_stop(struct udevice * dev)727a29710c5SAmit Singh Tomar static void sun8i_emac_eth_stop(struct udevice *dev)
728a29710c5SAmit Singh Tomar {
729a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
730a29710c5SAmit Singh Tomar
731a29710c5SAmit Singh Tomar /* Stop Rx/Tx transmitter */
732a29710c5SAmit Singh Tomar clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
733a29710c5SAmit Singh Tomar clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
734a29710c5SAmit Singh Tomar
735a29710c5SAmit Singh Tomar /* Stop TX DMA */
736a29710c5SAmit Singh Tomar clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
737a29710c5SAmit Singh Tomar
738a29710c5SAmit Singh Tomar phy_shutdown(priv->phydev);
739a29710c5SAmit Singh Tomar }
740a29710c5SAmit Singh Tomar
sun8i_emac_eth_probe(struct udevice * dev)741a29710c5SAmit Singh Tomar static int sun8i_emac_eth_probe(struct udevice *dev)
742a29710c5SAmit Singh Tomar {
743a29710c5SAmit Singh Tomar struct eth_pdata *pdata = dev_get_platdata(dev);
744a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
745a29710c5SAmit Singh Tomar
746a29710c5SAmit Singh Tomar priv->mac_reg = (void *)pdata->iobase;
747a29710c5SAmit Singh Tomar
748a29710c5SAmit Singh Tomar sun8i_emac_board_setup(priv);
749a85ba87dSChen-Yu Tsai sun8i_emac_set_syscon(priv);
750a29710c5SAmit Singh Tomar
7514d555ae3SPhilipp Tomsich sun8i_mdio_init(dev->name, dev);
752a29710c5SAmit Singh Tomar priv->bus = miiphy_get_dev_by_name(dev->name);
753a29710c5SAmit Singh Tomar
754a29710c5SAmit Singh Tomar return sun8i_phy_init(priv, dev);
755a29710c5SAmit Singh Tomar }
756a29710c5SAmit Singh Tomar
757a29710c5SAmit Singh Tomar static const struct eth_ops sun8i_emac_eth_ops = {
758a29710c5SAmit Singh Tomar .start = sun8i_emac_eth_start,
759a29710c5SAmit Singh Tomar .write_hwaddr = sun8i_eth_write_hwaddr,
760a29710c5SAmit Singh Tomar .send = sun8i_emac_eth_send,
761a29710c5SAmit Singh Tomar .recv = sun8i_emac_eth_recv,
762a29710c5SAmit Singh Tomar .free_pkt = sun8i_eth_free_pkt,
763a29710c5SAmit Singh Tomar .stop = sun8i_emac_eth_stop,
764a29710c5SAmit Singh Tomar };
765a29710c5SAmit Singh Tomar
sun8i_emac_eth_ofdata_to_platdata(struct udevice * dev)766a29710c5SAmit Singh Tomar static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
767a29710c5SAmit Singh Tomar {
7684d555ae3SPhilipp Tomsich struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
7694d555ae3SPhilipp Tomsich struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
770a29710c5SAmit Singh Tomar struct emac_eth_dev *priv = dev_get_priv(dev);
771a29710c5SAmit Singh Tomar const char *phy_mode;
772e160f7d4SSimon Glass int node = dev_of_offset(dev);
773a29710c5SAmit Singh Tomar int offset = 0;
7744d555ae3SPhilipp Tomsich #ifdef CONFIG_DM_GPIO
7754d555ae3SPhilipp Tomsich int reset_flags = GPIOD_IS_OUT;
7764d555ae3SPhilipp Tomsich int ret = 0;
7774d555ae3SPhilipp Tomsich #endif
778a29710c5SAmit Singh Tomar
779a821c4afSSimon Glass pdata->iobase = devfdt_get_addr_name(dev, "emac");
780a821c4afSSimon Glass priv->sysctl_reg = devfdt_get_addr_name(dev, "syscon");
781a29710c5SAmit Singh Tomar
782a29710c5SAmit Singh Tomar pdata->phy_interface = -1;
783a29710c5SAmit Singh Tomar priv->phyaddr = -1;
784a29710c5SAmit Singh Tomar priv->use_internal_phy = false;
785a29710c5SAmit Singh Tomar
786e160f7d4SSimon Glass offset = fdtdec_lookup_phandle(gd->fdt_blob, node,
787a29710c5SAmit Singh Tomar "phy");
788a29710c5SAmit Singh Tomar if (offset > 0)
789a29710c5SAmit Singh Tomar priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg",
790a29710c5SAmit Singh Tomar -1);
791a29710c5SAmit Singh Tomar
792e160f7d4SSimon Glass phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
793a29710c5SAmit Singh Tomar
794a29710c5SAmit Singh Tomar if (phy_mode)
795a29710c5SAmit Singh Tomar pdata->phy_interface = phy_get_interface_by_name(phy_mode);
796a29710c5SAmit Singh Tomar printf("phy interface%d\n", pdata->phy_interface);
797a29710c5SAmit Singh Tomar
798a29710c5SAmit Singh Tomar if (pdata->phy_interface == -1) {
799a29710c5SAmit Singh Tomar debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
800a29710c5SAmit Singh Tomar return -EINVAL;
801a29710c5SAmit Singh Tomar }
802a29710c5SAmit Singh Tomar
803a29710c5SAmit Singh Tomar priv->variant = dev_get_driver_data(dev);
804a29710c5SAmit Singh Tomar
805a29710c5SAmit Singh Tomar if (!priv->variant) {
806a29710c5SAmit Singh Tomar printf("%s: Missing variant '%s'\n", __func__,
807a29710c5SAmit Singh Tomar (char *)priv->variant);
808a29710c5SAmit Singh Tomar return -EINVAL;
809a29710c5SAmit Singh Tomar }
810a29710c5SAmit Singh Tomar
811a29710c5SAmit Singh Tomar if (priv->variant == H3_EMAC) {
812e160f7d4SSimon Glass if (fdt_getprop(gd->fdt_blob, node,
813a29710c5SAmit Singh Tomar "allwinner,use-internal-phy", NULL))
814a29710c5SAmit Singh Tomar priv->use_internal_phy = true;
815a29710c5SAmit Singh Tomar }
816a29710c5SAmit Singh Tomar
817a29710c5SAmit Singh Tomar priv->interface = pdata->phy_interface;
818a29710c5SAmit Singh Tomar
819a29710c5SAmit Singh Tomar if (!priv->use_internal_phy)
820a29710c5SAmit Singh Tomar parse_phy_pins(dev);
821a29710c5SAmit Singh Tomar
8224d555ae3SPhilipp Tomsich #ifdef CONFIG_DM_GPIO
823*da409cccSSimon Glass if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
8244d555ae3SPhilipp Tomsich "snps,reset-active-low"))
8254d555ae3SPhilipp Tomsich reset_flags |= GPIOD_ACTIVE_LOW;
8264d555ae3SPhilipp Tomsich
8274d555ae3SPhilipp Tomsich ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
8284d555ae3SPhilipp Tomsich &priv->reset_gpio, reset_flags);
8294d555ae3SPhilipp Tomsich
8304d555ae3SPhilipp Tomsich if (ret == 0) {
831*da409cccSSimon Glass ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
8324d555ae3SPhilipp Tomsich "snps,reset-delays-us",
8334d555ae3SPhilipp Tomsich sun8i_pdata->reset_delays, 3);
8344d555ae3SPhilipp Tomsich } else if (ret == -ENOENT) {
8354d555ae3SPhilipp Tomsich ret = 0;
8364d555ae3SPhilipp Tomsich }
8374d555ae3SPhilipp Tomsich #endif
8384d555ae3SPhilipp Tomsich
839a29710c5SAmit Singh Tomar return 0;
840a29710c5SAmit Singh Tomar }
841a29710c5SAmit Singh Tomar
842a29710c5SAmit Singh Tomar static const struct udevice_id sun8i_emac_eth_ids[] = {
843a29710c5SAmit Singh Tomar {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
844a29710c5SAmit Singh Tomar {.compatible = "allwinner,sun50i-a64-emac",
845a29710c5SAmit Singh Tomar .data = (uintptr_t)A64_EMAC },
846a29710c5SAmit Singh Tomar {.compatible = "allwinner,sun8i-a83t-emac",
847a29710c5SAmit Singh Tomar .data = (uintptr_t)A83T_EMAC },
848a29710c5SAmit Singh Tomar { }
849a29710c5SAmit Singh Tomar };
850a29710c5SAmit Singh Tomar
851a29710c5SAmit Singh Tomar U_BOOT_DRIVER(eth_sun8i_emac) = {
852a29710c5SAmit Singh Tomar .name = "eth_sun8i_emac",
853a29710c5SAmit Singh Tomar .id = UCLASS_ETH,
854a29710c5SAmit Singh Tomar .of_match = sun8i_emac_eth_ids,
855a29710c5SAmit Singh Tomar .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
856a29710c5SAmit Singh Tomar .probe = sun8i_emac_eth_probe,
857a29710c5SAmit Singh Tomar .ops = &sun8i_emac_eth_ops,
858a29710c5SAmit Singh Tomar .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
8594d555ae3SPhilipp Tomsich .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
860a29710c5SAmit Singh Tomar .flags = DM_FLAG_ALLOC_PRIV_DMA,
861a29710c5SAmit Singh Tomar };
862