blob: a7fb7ac4055fad6aadd959d46768b7d347aeb68a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Amit Singh Tomara29710c2016-07-06 17:59:44 +05302/*
3 * (C) Copyright 2016
4 * Author: Amit Singh Tomar, amittomer25@gmail.com
5 *
Amit Singh Tomara29710c2016-07-06 17:59:44 +05306 * Ethernet driver for H3/A64/A83T based SoC's
7 *
8 * It is derived from the work done by
9 * LABBE Corentin & Chen-Yu Tsai for Linux, THANKS!
10 *
11*/
12
13#include <asm/io.h>
14#include <asm/arch/clock.h>
15#include <asm/arch/gpio.h>
16#include <common.h>
17#include <dm.h>
18#include <fdt_support.h>
19#include <linux/err.h>
20#include <malloc.h>
21#include <miiphy.h>
22#include <net.h>
Andre Przywarac0341172018-04-04 01:31:15 +010023#include <dt-bindings/pinctrl/sun4i-a10.h>
Philipp Tomsich4d555ae2017-02-22 19:46:41 +010024#ifdef CONFIG_DM_GPIO
25#include <asm-generic/gpio.h>
26#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +053027
Amit Singh Tomara29710c2016-07-06 17:59:44 +053028#define MDIO_CMD_MII_BUSY BIT(0)
29#define MDIO_CMD_MII_WRITE BIT(1)
30
31#define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
32#define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
33#define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
34#define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
35
36#define CONFIG_TX_DESCR_NUM 32
37#define CONFIG_RX_DESCR_NUM 32
Hans de Goede40694372016-07-27 17:31:17 +020038#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
39
40/*
41 * The datasheet says that each descriptor can transfers up to 4096 bytes
42 * But later, the register documentation reduces that value to 2048,
43 * using 2048 cause strange behaviours and even BSP driver use 2047
44 */
45#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomara29710c2016-07-06 17:59:44 +053046
47#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
48#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
49
50#define H3_EPHY_DEFAULT_VALUE 0x58000
51#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
52#define H3_EPHY_ADDR_SHIFT 20
53#define REG_PHY_ADDR_MASK GENMASK(4, 0)
54#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
55#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
56#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
57
58#define SC_RMII_EN BIT(13)
59#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
60#define SC_ETCS_MASK GENMASK(1, 0)
61#define SC_ETCS_EXT_GMII 0x1
62#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng9b16ede2018-11-23 00:37:48 +010063#define SC_ETXDC_MASK GENMASK(12, 10)
64#define SC_ETXDC_OFFSET 10
65#define SC_ERXDC_MASK GENMASK(9, 5)
66#define SC_ERXDC_OFFSET 5
Amit Singh Tomara29710c2016-07-06 17:59:44 +053067
68#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
69
70#define AHB_GATE_OFFSET_EPHY 0
71
Lothar Feltenc6a21d62018-07-13 10:45:27 +020072/* IO mux settings */
73#define SUN8I_IOMUX_H3 2
Lothar Feltene46d73f2018-07-13 10:45:28 +020074#define SUN8I_IOMUX_R40 5
Lothar Feltenc6a21d62018-07-13 10:45:27 +020075#define SUN8I_IOMUX 4
Amit Singh Tomara29710c2016-07-06 17:59:44 +053076
77/* H3/A64 EMAC Register's offset */
78#define EMAC_CTL0 0x00
79#define EMAC_CTL1 0x04
80#define EMAC_INT_STA 0x08
81#define EMAC_INT_EN 0x0c
82#define EMAC_TX_CTL0 0x10
83#define EMAC_TX_CTL1 0x14
84#define EMAC_TX_FLOW_CTL 0x1c
85#define EMAC_TX_DMA_DESC 0x20
86#define EMAC_RX_CTL0 0x24
87#define EMAC_RX_CTL1 0x28
88#define EMAC_RX_DMA_DESC 0x34
89#define EMAC_MII_CMD 0x48
90#define EMAC_MII_DATA 0x4c
91#define EMAC_ADDR0_HIGH 0x50
92#define EMAC_ADDR0_LOW 0x54
93#define EMAC_TX_DMA_STA 0xb0
94#define EMAC_TX_CUR_DESC 0xb4
95#define EMAC_TX_CUR_BUF 0xb8
96#define EMAC_RX_DMA_STA 0xc0
97#define EMAC_RX_CUR_DESC 0xc4
98
99DECLARE_GLOBAL_DATA_PTR;
100
101enum emac_variant {
102 A83T_EMAC = 1,
103 H3_EMAC,
104 A64_EMAC,
Lothar Feltene46d73f2018-07-13 10:45:28 +0200105 R40_GMAC,
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530106};
107
108struct emac_dma_desc {
109 u32 status;
110 u32 st;
111 u32 buf_addr;
112 u32 next;
113} __aligned(ARCH_DMA_MINALIGN);
114
115struct emac_eth_dev {
116 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
117 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
118 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
119 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
120
121 u32 interface;
122 u32 phyaddr;
123 u32 link;
124 u32 speed;
125 u32 duplex;
126 u32 phy_configured;
127 u32 tx_currdescnum;
128 u32 rx_currdescnum;
129 u32 addr;
130 u32 tx_slot;
131 bool use_internal_phy;
132
133 enum emac_variant variant;
134 void *mac_reg;
135 phys_addr_t sysctl_reg;
136 struct phy_device *phydev;
137 struct mii_dev *bus;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100138#ifdef CONFIG_DM_GPIO
139 struct gpio_desc reset_gpio;
140#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530141};
142
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100143
144struct sun8i_eth_pdata {
145 struct eth_pdata eth_pdata;
146 u32 reset_delays[3];
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100147 int tx_delay_ps;
148 int rx_delay_ps;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100149};
150
151
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530152static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
153{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100154 struct udevice *dev = bus->priv;
155 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530156 ulong start;
157 u32 miiaddr = 0;
158 int timeout = CONFIG_MDIO_TIMEOUT;
159
160 miiaddr &= ~MDIO_CMD_MII_WRITE;
161 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
162 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
163 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
164
165 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
166
167 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
168 MDIO_CMD_MII_PHY_ADDR_MASK;
169
170 miiaddr |= MDIO_CMD_MII_BUSY;
171
172 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
173
174 start = get_timer(0);
175 while (get_timer(start) < timeout) {
176 if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY))
177 return readl(priv->mac_reg + EMAC_MII_DATA);
178 udelay(10);
179 };
180
181 return -1;
182}
183
184static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
185 u16 val)
186{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100187 struct udevice *dev = bus->priv;
188 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530189 ulong start;
190 u32 miiaddr = 0;
191 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
192
193 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
194 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
195 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
196
197 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
198 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
199 MDIO_CMD_MII_PHY_ADDR_MASK;
200
201 miiaddr |= MDIO_CMD_MII_WRITE;
202 miiaddr |= MDIO_CMD_MII_BUSY;
203
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530204 writel(val, priv->mac_reg + EMAC_MII_DATA);
Philipp Tomsich1deeecb2016-11-16 01:40:27 +0000205 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530206
207 start = get_timer(0);
208 while (get_timer(start) < timeout) {
209 if (!(readl(priv->mac_reg + EMAC_MII_CMD) &
210 MDIO_CMD_MII_BUSY)) {
211 ret = 0;
212 break;
213 }
214 udelay(10);
215 };
216
217 return ret;
218}
219
220static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
221{
222 u32 macid_lo, macid_hi;
223
224 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
225 (mac_id[3] << 24);
226 macid_hi = mac_id[4] + (mac_id[5] << 8);
227
228 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
229 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
230
231 return 0;
232}
233
234static void sun8i_adjust_link(struct emac_eth_dev *priv,
235 struct phy_device *phydev)
236{
237 u32 v;
238
239 v = readl(priv->mac_reg + EMAC_CTL0);
240
241 if (phydev->duplex)
242 v |= BIT(0);
243 else
244 v &= ~BIT(0);
245
246 v &= ~0x0C;
247
248 switch (phydev->speed) {
249 case 1000:
250 break;
251 case 100:
252 v |= BIT(2);
253 v |= BIT(3);
254 break;
255 case 10:
256 v |= BIT(3);
257 break;
258 }
259 writel(v, priv->mac_reg + EMAC_CTL0);
260}
261
262static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
263{
264 if (priv->use_internal_phy) {
265 /* H3 based SoC's that has an Internal 100MBit PHY
266 * needs to be configured and powered up before use
267 */
268 *reg &= ~H3_EPHY_DEFAULT_MASK;
269 *reg |= H3_EPHY_DEFAULT_VALUE;
270 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
271 *reg &= ~H3_EPHY_SHUTDOWN;
272 *reg |= H3_EPHY_SELECT;
273 } else
274 /* This is to select External Gigabit PHY on
275 * the boards with H3 SoC.
276 */
277 *reg &= ~H3_EPHY_SELECT;
278
279 return 0;
280}
281
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100282static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
283 struct emac_eth_dev *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530284{
285 int ret;
286 u32 reg;
287
Jagan Teki695f6042019-02-28 00:26:51 +0530288 if (priv->variant == R40_GMAC) {
289 /* Select RGMII for R40 */
290 reg = readl(priv->sysctl_reg + 0x164);
291 reg |= CCM_GMAC_CTRL_TX_CLK_SRC_INT_RGMII |
292 CCM_GMAC_CTRL_GPIT_RGMII |
293 CCM_GMAC_CTRL_TX_CLK_DELAY(CONFIG_GMAC_TX_DELAY);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530294
Jagan Teki695f6042019-02-28 00:26:51 +0530295 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200296 return 0;
Jagan Teki695f6042019-02-28 00:26:51 +0530297 }
298
299 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200300
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530301 if (priv->variant == H3_EMAC) {
302 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
303 if (ret)
304 return ret;
305 }
306
307 reg &= ~(SC_ETCS_MASK | SC_EPIT);
308 if (priv->variant == H3_EMAC || priv->variant == A64_EMAC)
309 reg &= ~SC_RMII_EN;
310
311 switch (priv->interface) {
312 case PHY_INTERFACE_MODE_MII:
313 /* default */
314 break;
315 case PHY_INTERFACE_MODE_RGMII:
316 reg |= SC_EPIT | SC_ETCS_INT_GMII;
317 break;
318 case PHY_INTERFACE_MODE_RMII:
319 if (priv->variant == H3_EMAC ||
320 priv->variant == A64_EMAC) {
321 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
322 break;
323 }
324 /* RMII not supported on A83T */
325 default:
326 debug("%s: Invalid PHY interface\n", __func__);
327 return -EINVAL;
328 }
329
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100330 if (pdata->tx_delay_ps)
331 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
332 & SC_ETXDC_MASK;
333
334 if (pdata->rx_delay_ps)
335 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
336 & SC_ERXDC_MASK;
337
Andre Przywara12afd952018-04-04 01:31:16 +0100338 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530339
340 return 0;
341}
342
343static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
344{
345 struct phy_device *phydev;
346
347 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
348 if (!phydev)
349 return -ENODEV;
350
351 phy_connect_dev(phydev, dev);
352
353 priv->phydev = phydev;
354 phy_config(priv->phydev);
355
356 return 0;
357}
358
359static void rx_descs_init(struct emac_eth_dev *priv)
360{
361 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
362 char *rxbuffs = &priv->rxbuffer[0];
363 struct emac_dma_desc *desc_p;
364 u32 idx;
365
366 /* flush Rx buffers */
367 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
368 RX_TOTAL_BUFSIZE);
369
370 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
371 desc_p = &desc_table_p[idx];
372 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
373 ;
374 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Hans de Goede40694372016-07-27 17:31:17 +0200375 desc_p->st |= CONFIG_ETH_RXSIZE;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530376 desc_p->status = BIT(31);
377 }
378
379 /* Correcting the last pointer of the chain */
380 desc_p->next = (uintptr_t)&desc_table_p[0];
381
382 flush_dcache_range((uintptr_t)priv->rx_chain,
383 (uintptr_t)priv->rx_chain +
384 sizeof(priv->rx_chain));
385
386 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
387 priv->rx_currdescnum = 0;
388}
389
390static void tx_descs_init(struct emac_eth_dev *priv)
391{
392 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
393 char *txbuffs = &priv->txbuffer[0];
394 struct emac_dma_desc *desc_p;
395 u32 idx;
396
397 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
398 desc_p = &desc_table_p[idx];
399 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
400 ;
401 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
402 desc_p->status = (1 << 31);
403 desc_p->st = 0;
404 }
405
406 /* Correcting the last pointer of the chain */
407 desc_p->next = (uintptr_t)&desc_table_p[0];
408
409 /* Flush all Tx buffer descriptors */
410 flush_dcache_range((uintptr_t)priv->tx_chain,
411 (uintptr_t)priv->tx_chain +
412 sizeof(priv->tx_chain));
413
414 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
415 priv->tx_currdescnum = 0;
416}
417
418static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
419{
420 u32 reg, v;
421 int timeout = 100;
422
423 reg = readl((priv->mac_reg + EMAC_CTL1));
424
425 if (!(reg & 0x1)) {
426 /* Soft reset MAC */
427 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
428 do {
429 reg = readl(priv->mac_reg + EMAC_CTL1);
430 } while ((reg & 0x01) != 0 && (--timeout));
431 if (!timeout) {
432 printf("%s: Timeout\n", __func__);
433 return -1;
434 }
435 }
436
437 /* Rewrite mac address after reset */
438 _sun8i_write_hwaddr(priv, enetaddr);
439
440 v = readl(priv->mac_reg + EMAC_TX_CTL1);
441 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
442 v |= BIT(1);
443 writel(v, priv->mac_reg + EMAC_TX_CTL1);
444
445 v = readl(priv->mac_reg + EMAC_RX_CTL1);
446 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
447 * complete frame has been written to RX DMA FIFO
448 */
449 v |= BIT(1);
450 writel(v, priv->mac_reg + EMAC_RX_CTL1);
451
452 /* DMA */
453 writel(8 << 24, priv->mac_reg + EMAC_CTL1);
454
455 /* Initialize rx/tx descriptors */
456 rx_descs_init(priv);
457 tx_descs_init(priv);
458
459 /* PHY Start Up */
Samuel Holland2d530182018-01-27 23:53:20 -0600460 phy_startup(priv->phydev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530461
462 sun8i_adjust_link(priv, priv->phydev);
463
464 /* Start RX DMA */
465 v = readl(priv->mac_reg + EMAC_RX_CTL1);
466 v |= BIT(30);
467 writel(v, priv->mac_reg + EMAC_RX_CTL1);
468 /* Start TX DMA */
469 v = readl(priv->mac_reg + EMAC_TX_CTL1);
470 v |= BIT(30);
471 writel(v, priv->mac_reg + EMAC_TX_CTL1);
472
473 /* Enable RX/TX */
474 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
475 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
476
477 return 0;
478}
479
480static int parse_phy_pins(struct udevice *dev)
481{
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200482 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530483 int offset;
484 const char *pin_name;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100485 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530486
Simon Glasse160f7d2017-01-17 16:52:55 -0700487 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530488 "pinctrl-0");
489 if (offset < 0) {
490 printf("WARNING: emac: cannot find pinctrl-0 node\n");
491 return offset;
492 }
493
494 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywarac0341172018-04-04 01:31:15 +0100495 "drive-strength", ~0);
496 if (drive != ~0) {
497 if (drive <= 10)
498 drive = SUN4I_PINCTRL_10_MA;
499 else if (drive <= 20)
500 drive = SUN4I_PINCTRL_20_MA;
501 else if (drive <= 30)
502 drive = SUN4I_PINCTRL_30_MA;
503 else
504 drive = SUN4I_PINCTRL_40_MA;
Andre Przywarac0341172018-04-04 01:31:15 +0100505 }
506
507 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
508 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywarac0341172018-04-04 01:31:15 +0100509 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
510 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100511
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530512 for (i = 0; ; i++) {
513 int pin;
514
Simon Glassb02e4042016-10-02 17:59:28 -0600515 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100516 "pins", i, NULL);
517 if (!pin_name)
518 break;
Andre Przywarac0341172018-04-04 01:31:15 +0100519
520 pin = sunxi_name_to_gpio(pin_name);
521 if (pin < 0)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530522 continue;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530523
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200524 if (priv->variant == H3_EMAC)
525 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Lothar Feltene46d73f2018-07-13 10:45:28 +0200526 else if (priv->variant == R40_GMAC)
527 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200528 else
529 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
530
Andre Przywarac0341172018-04-04 01:31:15 +0100531 if (drive != ~0)
532 sunxi_gpio_set_drv(pin, drive);
533 if (pull != ~0)
534 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530535 }
536
537 if (!i) {
Andre Przywarac0341172018-04-04 01:31:15 +0100538 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530539 return -2;
540 }
541
542 return 0;
543}
544
545static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
546{
547 u32 status, desc_num = priv->rx_currdescnum;
548 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
549 int length = -EAGAIN;
550 int good_packet = 1;
551 uintptr_t desc_start = (uintptr_t)desc_p;
552 uintptr_t desc_end = desc_start +
553 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
554
555 ulong data_start = (uintptr_t)desc_p->buf_addr;
556 ulong data_end;
557
558 /* Invalidate entire buffer descriptor */
559 invalidate_dcache_range(desc_start, desc_end);
560
561 status = desc_p->status;
562
563 /* Check for DMA own bit */
564 if (!(status & BIT(31))) {
565 length = (desc_p->status >> 16) & 0x3FFF;
566
567 if (length < 0x40) {
568 good_packet = 0;
569 debug("RX: Bad Packet (runt)\n");
570 }
571
572 data_end = data_start + length;
573 /* Invalidate received data */
574 invalidate_dcache_range(rounddown(data_start,
575 ARCH_DMA_MINALIGN),
576 roundup(data_end,
577 ARCH_DMA_MINALIGN));
578 if (good_packet) {
Hans de Goede40694372016-07-27 17:31:17 +0200579 if (length > CONFIG_ETH_RXSIZE) {
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530580 printf("Received packet is too big (len=%d)\n",
581 length);
582 return -EMSGSIZE;
583 }
584 *packetp = (uchar *)(ulong)desc_p->buf_addr;
585 return length;
586 }
587 }
588
589 return length;
590}
591
592static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
593 int len)
594{
595 u32 v, desc_num = priv->tx_currdescnum;
596 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
597 uintptr_t desc_start = (uintptr_t)desc_p;
598 uintptr_t desc_end = desc_start +
599 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
600
601 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
602 uintptr_t data_end = data_start +
603 roundup(len, ARCH_DMA_MINALIGN);
604
605 /* Invalidate entire buffer descriptor */
606 invalidate_dcache_range(desc_start, desc_end);
607
608 desc_p->st = len;
609 /* Mandatory undocumented bit */
610 desc_p->st |= BIT(24);
611
612 memcpy((void *)data_start, packet, len);
613
614 /* Flush data to be sent */
615 flush_dcache_range(data_start, data_end);
616
617 /* frame end */
618 desc_p->st |= BIT(30);
619 desc_p->st |= BIT(31);
620
621 /*frame begin */
622 desc_p->st |= BIT(29);
623 desc_p->status = BIT(31);
624
625 /*Descriptors st and status field has changed, so FLUSH it */
626 flush_dcache_range(desc_start, desc_end);
627
628 /* Move to next Descriptor and wrap around */
629 if (++desc_num >= CONFIG_TX_DESCR_NUM)
630 desc_num = 0;
631 priv->tx_currdescnum = desc_num;
632
633 /* Start the DMA */
634 v = readl(priv->mac_reg + EMAC_TX_CTL1);
635 v |= BIT(31);/* mandatory */
636 v |= BIT(30);/* mandatory */
637 writel(v, priv->mac_reg + EMAC_TX_CTL1);
638
639 return 0;
640}
641
642static int sun8i_eth_write_hwaddr(struct udevice *dev)
643{
644 struct eth_pdata *pdata = dev_get_platdata(dev);
645 struct emac_eth_dev *priv = dev_get_priv(dev);
646
647 return _sun8i_write_hwaddr(priv, pdata->enetaddr);
648}
649
650static void sun8i_emac_board_setup(struct emac_eth_dev *priv)
651{
652 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
653
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200654 if (priv->variant == H3_EMAC) {
655 /* Only H3/H5 have clock controls for internal EPHY */
656 if (priv->use_internal_phy) {
657 /* Set clock gating for ephy */
658 setbits_le32(&ccm->bus_gate4,
659 BIT(AHB_GATE_OFFSET_EPHY));
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530660
Lothar Feltenc6a21d62018-07-13 10:45:27 +0200661 /* Deassert EPHY */
662 setbits_le32(&ccm->ahb_reset2_cfg,
663 BIT(AHB_RESET_OFFSET_EPHY));
664 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530665 }
666
Lothar Feltene46d73f2018-07-13 10:45:28 +0200667 if (priv->variant == R40_GMAC) {
668 /* Set clock gating for emac */
669 setbits_le32(&ccm->ahb_reset1_cfg, BIT(AHB_RESET_OFFSET_GMAC));
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530670
Lothar Feltene46d73f2018-07-13 10:45:28 +0200671 /* De-assert EMAC */
672 setbits_le32(&ccm->ahb_gate1, BIT(AHB_GATE_OFFSET_GMAC));
Lothar Feltene46d73f2018-07-13 10:45:28 +0200673 } else {
674 /* Set clock gating for emac */
675 setbits_le32(&ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_GMAC));
676
677 /* De-assert EMAC */
678 setbits_le32(&ccm->ahb_reset0_cfg, BIT(AHB_RESET_OFFSET_GMAC));
679 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530680}
681
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100682#if defined(CONFIG_DM_GPIO)
683static int sun8i_mdio_reset(struct mii_dev *bus)
684{
685 struct udevice *dev = bus->priv;
686 struct emac_eth_dev *priv = dev_get_priv(dev);
687 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
688 int ret;
689
690 if (!dm_gpio_is_valid(&priv->reset_gpio))
691 return 0;
692
693 /* reset the phy */
694 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
695 if (ret)
696 return ret;
697
698 udelay(pdata->reset_delays[0]);
699
700 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
701 if (ret)
702 return ret;
703
704 udelay(pdata->reset_delays[1]);
705
706 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
707 if (ret)
708 return ret;
709
710 udelay(pdata->reset_delays[2]);
711
712 return 0;
713}
714#endif
715
716static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530717{
718 struct mii_dev *bus = mdio_alloc();
719
720 if (!bus) {
721 debug("Failed to allocate MDIO bus\n");
722 return -ENOMEM;
723 }
724
725 bus->read = sun8i_mdio_read;
726 bus->write = sun8i_mdio_write;
727 snprintf(bus->name, sizeof(bus->name), name);
728 bus->priv = (void *)priv;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100729#if defined(CONFIG_DM_GPIO)
730 bus->reset = sun8i_mdio_reset;
731#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530732
733 return mdio_register(bus);
734}
735
736static int sun8i_emac_eth_start(struct udevice *dev)
737{
738 struct eth_pdata *pdata = dev_get_platdata(dev);
739
740 return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
741}
742
743static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
744{
745 struct emac_eth_dev *priv = dev_get_priv(dev);
746
747 return _sun8i_emac_eth_send(priv, packet, length);
748}
749
750static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
751{
752 struct emac_eth_dev *priv = dev_get_priv(dev);
753
754 return _sun8i_eth_recv(priv, packetp);
755}
756
757static int _sun8i_free_pkt(struct emac_eth_dev *priv)
758{
759 u32 desc_num = priv->rx_currdescnum;
760 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
761 uintptr_t desc_start = (uintptr_t)desc_p;
762 uintptr_t desc_end = desc_start +
763 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
764
765 /* Make the current descriptor valid again */
766 desc_p->status |= BIT(31);
767
768 /* Flush Status field of descriptor */
769 flush_dcache_range(desc_start, desc_end);
770
771 /* Move to next desc and wrap-around condition. */
772 if (++desc_num >= CONFIG_RX_DESCR_NUM)
773 desc_num = 0;
774 priv->rx_currdescnum = desc_num;
775
776 return 0;
777}
778
779static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
780 int length)
781{
782 struct emac_eth_dev *priv = dev_get_priv(dev);
783
784 return _sun8i_free_pkt(priv);
785}
786
787static void sun8i_emac_eth_stop(struct udevice *dev)
788{
789 struct emac_eth_dev *priv = dev_get_priv(dev);
790
791 /* Stop Rx/Tx transmitter */
792 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
793 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
794
795 /* Stop TX DMA */
796 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
797
798 phy_shutdown(priv->phydev);
799}
800
801static int sun8i_emac_eth_probe(struct udevice *dev)
802{
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100803 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
804 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530805 struct emac_eth_dev *priv = dev_get_priv(dev);
806
807 priv->mac_reg = (void *)pdata->iobase;
808
809 sun8i_emac_board_setup(priv);
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100810 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530811
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100812 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530813 priv->bus = miiphy_get_dev_by_name(dev->name);
814
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530815 return sun8i_phy_init(priv, dev);
816}
817
818static const struct eth_ops sun8i_emac_eth_ops = {
819 .start = sun8i_emac_eth_start,
820 .write_hwaddr = sun8i_eth_write_hwaddr,
821 .send = sun8i_emac_eth_send,
822 .recv = sun8i_emac_eth_recv,
823 .free_pkt = sun8i_eth_free_pkt,
824 .stop = sun8i_emac_eth_stop,
825};
826
827static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
828{
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100829 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
830 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530831 struct emac_eth_dev *priv = dev_get_priv(dev);
832 const char *phy_mode;
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100833 const fdt32_t *reg;
Simon Glasse160f7d2017-01-17 16:52:55 -0700834 int node = dev_of_offset(dev);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530835 int offset = 0;
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100836#ifdef CONFIG_DM_GPIO
837 int reset_flags = GPIOD_IS_OUT;
838 int ret = 0;
839#endif
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530840
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100841 pdata->iobase = devfdt_get_addr(dev);
Andre Przywara12afd952018-04-04 01:31:16 +0100842 if (pdata->iobase == FDT_ADDR_T_NONE) {
843 debug("%s: Cannot find MAC base address\n", __func__);
844 return -EINVAL;
845 }
846
Lothar Feltene46d73f2018-07-13 10:45:28 +0200847 priv->variant = dev_get_driver_data(dev);
848
849 if (!priv->variant) {
850 printf("%s: Missing variant\n", __func__);
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100851 return -EINVAL;
Andre Przywara12afd952018-04-04 01:31:16 +0100852 }
Lothar Feltene46d73f2018-07-13 10:45:28 +0200853
Jagan Teki695f6042019-02-28 00:26:51 +0530854 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
855 if (offset < 0) {
856 debug("%s: cannot find syscon node\n", __func__);
857 return -EINVAL;
858 }
859
860 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
861 if (!reg) {
862 debug("%s: cannot find reg property in syscon node\n",
863 __func__);
864 return -EINVAL;
865 }
866 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
867 offset, reg);
868 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
869 debug("%s: Cannot find syscon base address\n", __func__);
870 return -EINVAL;
Andre Przywara12afd952018-04-04 01:31:16 +0100871 }
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530872
873 pdata->phy_interface = -1;
874 priv->phyaddr = -1;
875 priv->use_internal_phy = false;
876
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100877 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywara12afd952018-04-04 01:31:16 +0100878 if (offset < 0) {
879 debug("%s: Cannot find PHY address\n", __func__);
880 return -EINVAL;
881 }
882 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530883
Simon Glasse160f7d2017-01-17 16:52:55 -0700884 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530885
886 if (phy_mode)
887 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
888 printf("phy interface%d\n", pdata->phy_interface);
889
890 if (pdata->phy_interface == -1) {
891 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
892 return -EINVAL;
893 }
894
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530895 if (priv->variant == H3_EMAC) {
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100896 int parent = fdt_parent_offset(gd->fdt_blob, offset);
Andre Przywara12afd952018-04-04 01:31:16 +0100897
Andre Przywaraecd0cec2018-04-04 01:31:20 +0100898 if (parent >= 0 &&
899 !fdt_node_check_compatible(gd->fdt_blob, parent,
900 "allwinner,sun8i-h3-mdio-internal"))
901 priv->use_internal_phy = true;
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530902 }
903
904 priv->interface = pdata->phy_interface;
905
906 if (!priv->use_internal_phy)
907 parse_phy_pins(dev);
908
Icenowy Zheng9b16ede2018-11-23 00:37:48 +0100909 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
910 "allwinner,tx-delay-ps", 0);
911 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
912 printf("%s: Invalid TX delay value %d\n", __func__,
913 sun8i_pdata->tx_delay_ps);
914
915 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
916 "allwinner,rx-delay-ps", 0);
917 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
918 printf("%s: Invalid RX delay value %d\n", __func__,
919 sun8i_pdata->rx_delay_ps);
920
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100921#ifdef CONFIG_DM_GPIO
Simon Glassda409cc2017-05-17 17:18:09 -0600922 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100923 "snps,reset-active-low"))
924 reset_flags |= GPIOD_ACTIVE_LOW;
925
926 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
927 &priv->reset_gpio, reset_flags);
928
929 if (ret == 0) {
Simon Glassda409cc2017-05-17 17:18:09 -0600930 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100931 "snps,reset-delays-us",
932 sun8i_pdata->reset_delays, 3);
933 } else if (ret == -ENOENT) {
934 ret = 0;
935 }
936#endif
937
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530938 return 0;
939}
940
941static const struct udevice_id sun8i_emac_eth_ids[] = {
942 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
943 {.compatible = "allwinner,sun50i-a64-emac",
944 .data = (uintptr_t)A64_EMAC },
945 {.compatible = "allwinner,sun8i-a83t-emac",
946 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene46d73f2018-07-13 10:45:28 +0200947 {.compatible = "allwinner,sun8i-r40-gmac",
948 .data = (uintptr_t)R40_GMAC },
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530949 { }
950};
951
952U_BOOT_DRIVER(eth_sun8i_emac) = {
953 .name = "eth_sun8i_emac",
954 .id = UCLASS_ETH,
955 .of_match = sun8i_emac_eth_ids,
956 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
957 .probe = sun8i_emac_eth_probe,
958 .ops = &sun8i_emac_eth_ops,
959 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich4d555ae2017-02-22 19:46:41 +0100960 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomara29710c2016-07-06 17:59:44 +0530961 .flags = DM_FLAG_ALLOC_PRIV_DMA,
962};