blob: 1f5c630e0232f9faaca3d481087338888bd7708a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05302/*
3 * (C) Copyright 2016
4 * Author: Amit Singh Tomar, amittomer25@gmail.com
5 *
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05306 * Ethernet driver for H3/A64/A83T based SoC's
7 *
8 * It is derived from the work done by
9 * LABBE Corentin & Chen-Yu Tsai for Linux, THANKS!
10 *
11*/
12
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 Przywara26e549b2018-04-04 01:31:15 +010023#include <dt-bindings/pinctrl/sun4i-a10.h>
Philipp Tomsich3297b552017-02-22 19:46:41 +010024#ifdef CONFIG_DM_GPIO
25#include <asm-generic/gpio.h>
26#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053027
Amit Singh Tomard194c0e2016-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 Goedefcdb3b32016-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 Tomard194c0e2016-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
63
64#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
65
66#define AHB_GATE_OFFSET_EPHY 0
67
Andre Przywara5fb97432017-02-16 01:20:27 +000068#if defined(CONFIG_MACH_SUNXI_H3_H5)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053069#define SUN8I_GPD8_GMAC 2
70#else
71#define SUN8I_GPD8_GMAC 4
72#endif
73
74/* H3/A64 EMAC Register's offset */
75#define EMAC_CTL0 0x00
76#define EMAC_CTL1 0x04
77#define EMAC_INT_STA 0x08
78#define EMAC_INT_EN 0x0c
79#define EMAC_TX_CTL0 0x10
80#define EMAC_TX_CTL1 0x14
81#define EMAC_TX_FLOW_CTL 0x1c
82#define EMAC_TX_DMA_DESC 0x20
83#define EMAC_RX_CTL0 0x24
84#define EMAC_RX_CTL1 0x28
85#define EMAC_RX_DMA_DESC 0x34
86#define EMAC_MII_CMD 0x48
87#define EMAC_MII_DATA 0x4c
88#define EMAC_ADDR0_HIGH 0x50
89#define EMAC_ADDR0_LOW 0x54
90#define EMAC_TX_DMA_STA 0xb0
91#define EMAC_TX_CUR_DESC 0xb4
92#define EMAC_TX_CUR_BUF 0xb8
93#define EMAC_RX_DMA_STA 0xc0
94#define EMAC_RX_CUR_DESC 0xc4
95
96DECLARE_GLOBAL_DATA_PTR;
97
98enum emac_variant {
99 A83T_EMAC = 1,
100 H3_EMAC,
101 A64_EMAC,
102};
103
104struct emac_dma_desc {
105 u32 status;
106 u32 st;
107 u32 buf_addr;
108 u32 next;
109} __aligned(ARCH_DMA_MINALIGN);
110
111struct emac_eth_dev {
112 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
113 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
114 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
115 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
116
117 u32 interface;
118 u32 phyaddr;
119 u32 link;
120 u32 speed;
121 u32 duplex;
122 u32 phy_configured;
123 u32 tx_currdescnum;
124 u32 rx_currdescnum;
125 u32 addr;
126 u32 tx_slot;
127 bool use_internal_phy;
128
129 enum emac_variant variant;
130 void *mac_reg;
131 phys_addr_t sysctl_reg;
132 struct phy_device *phydev;
133 struct mii_dev *bus;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100134#ifdef CONFIG_DM_GPIO
135 struct gpio_desc reset_gpio;
136#endif
137};
138
139
140struct sun8i_eth_pdata {
141 struct eth_pdata eth_pdata;
142 u32 reset_delays[3];
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530143};
144
Philipp Tomsich3297b552017-02-22 19:46:41 +0100145
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530146static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
147{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100148 struct udevice *dev = bus->priv;
149 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530150 ulong start;
151 u32 miiaddr = 0;
152 int timeout = CONFIG_MDIO_TIMEOUT;
153
154 miiaddr &= ~MDIO_CMD_MII_WRITE;
155 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
156 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
157 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
158
159 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
160
161 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
162 MDIO_CMD_MII_PHY_ADDR_MASK;
163
164 miiaddr |= MDIO_CMD_MII_BUSY;
165
166 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
167
168 start = get_timer(0);
169 while (get_timer(start) < timeout) {
170 if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY))
171 return readl(priv->mac_reg + EMAC_MII_DATA);
172 udelay(10);
173 };
174
175 return -1;
176}
177
178static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
179 u16 val)
180{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100181 struct udevice *dev = bus->priv;
182 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530183 ulong start;
184 u32 miiaddr = 0;
185 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
186
187 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
188 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
189 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
190
191 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
192 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
193 MDIO_CMD_MII_PHY_ADDR_MASK;
194
195 miiaddr |= MDIO_CMD_MII_WRITE;
196 miiaddr |= MDIO_CMD_MII_BUSY;
197
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530198 writel(val, priv->mac_reg + EMAC_MII_DATA);
Philipp Tomsich2b6dee12016-11-16 01:40:27 +0000199 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530200
201 start = get_timer(0);
202 while (get_timer(start) < timeout) {
203 if (!(readl(priv->mac_reg + EMAC_MII_CMD) &
204 MDIO_CMD_MII_BUSY)) {
205 ret = 0;
206 break;
207 }
208 udelay(10);
209 };
210
211 return ret;
212}
213
214static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
215{
216 u32 macid_lo, macid_hi;
217
218 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
219 (mac_id[3] << 24);
220 macid_hi = mac_id[4] + (mac_id[5] << 8);
221
222 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
223 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
224
225 return 0;
226}
227
228static void sun8i_adjust_link(struct emac_eth_dev *priv,
229 struct phy_device *phydev)
230{
231 u32 v;
232
233 v = readl(priv->mac_reg + EMAC_CTL0);
234
235 if (phydev->duplex)
236 v |= BIT(0);
237 else
238 v &= ~BIT(0);
239
240 v &= ~0x0C;
241
242 switch (phydev->speed) {
243 case 1000:
244 break;
245 case 100:
246 v |= BIT(2);
247 v |= BIT(3);
248 break;
249 case 10:
250 v |= BIT(3);
251 break;
252 }
253 writel(v, priv->mac_reg + EMAC_CTL0);
254}
255
256static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
257{
258 if (priv->use_internal_phy) {
259 /* H3 based SoC's that has an Internal 100MBit PHY
260 * needs to be configured and powered up before use
261 */
262 *reg &= ~H3_EPHY_DEFAULT_MASK;
263 *reg |= H3_EPHY_DEFAULT_VALUE;
264 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
265 *reg &= ~H3_EPHY_SHUTDOWN;
266 *reg |= H3_EPHY_SELECT;
267 } else
268 /* This is to select External Gigabit PHY on
269 * the boards with H3 SoC.
270 */
271 *reg &= ~H3_EPHY_SELECT;
272
273 return 0;
274}
275
276static int sun8i_emac_set_syscon(struct emac_eth_dev *priv)
277{
278 int ret;
279 u32 reg;
280
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100281 reg = readl(priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530282
283 if (priv->variant == H3_EMAC) {
284 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
285 if (ret)
286 return ret;
287 }
288
289 reg &= ~(SC_ETCS_MASK | SC_EPIT);
290 if (priv->variant == H3_EMAC || priv->variant == A64_EMAC)
291 reg &= ~SC_RMII_EN;
292
293 switch (priv->interface) {
294 case PHY_INTERFACE_MODE_MII:
295 /* default */
296 break;
297 case PHY_INTERFACE_MODE_RGMII:
298 reg |= SC_EPIT | SC_ETCS_INT_GMII;
299 break;
300 case PHY_INTERFACE_MODE_RMII:
301 if (priv->variant == H3_EMAC ||
302 priv->variant == A64_EMAC) {
303 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
304 break;
305 }
306 /* RMII not supported on A83T */
307 default:
308 debug("%s: Invalid PHY interface\n", __func__);
309 return -EINVAL;
310 }
311
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100312 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530313
314 return 0;
315}
316
317static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
318{
319 struct phy_device *phydev;
320
321 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
322 if (!phydev)
323 return -ENODEV;
324
325 phy_connect_dev(phydev, dev);
326
327 priv->phydev = phydev;
328 phy_config(priv->phydev);
329
330 return 0;
331}
332
333static void rx_descs_init(struct emac_eth_dev *priv)
334{
335 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
336 char *rxbuffs = &priv->rxbuffer[0];
337 struct emac_dma_desc *desc_p;
338 u32 idx;
339
340 /* flush Rx buffers */
341 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
342 RX_TOTAL_BUFSIZE);
343
344 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
345 desc_p = &desc_table_p[idx];
346 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
347 ;
348 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Hans de Goedefcdb3b32016-07-27 17:31:17 +0200349 desc_p->st |= CONFIG_ETH_RXSIZE;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530350 desc_p->status = BIT(31);
351 }
352
353 /* Correcting the last pointer of the chain */
354 desc_p->next = (uintptr_t)&desc_table_p[0];
355
356 flush_dcache_range((uintptr_t)priv->rx_chain,
357 (uintptr_t)priv->rx_chain +
358 sizeof(priv->rx_chain));
359
360 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
361 priv->rx_currdescnum = 0;
362}
363
364static void tx_descs_init(struct emac_eth_dev *priv)
365{
366 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
367 char *txbuffs = &priv->txbuffer[0];
368 struct emac_dma_desc *desc_p;
369 u32 idx;
370
371 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
372 desc_p = &desc_table_p[idx];
373 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
374 ;
375 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
376 desc_p->status = (1 << 31);
377 desc_p->st = 0;
378 }
379
380 /* Correcting the last pointer of the chain */
381 desc_p->next = (uintptr_t)&desc_table_p[0];
382
383 /* Flush all Tx buffer descriptors */
384 flush_dcache_range((uintptr_t)priv->tx_chain,
385 (uintptr_t)priv->tx_chain +
386 sizeof(priv->tx_chain));
387
388 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
389 priv->tx_currdescnum = 0;
390}
391
392static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
393{
394 u32 reg, v;
395 int timeout = 100;
396
397 reg = readl((priv->mac_reg + EMAC_CTL1));
398
399 if (!(reg & 0x1)) {
400 /* Soft reset MAC */
401 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
402 do {
403 reg = readl(priv->mac_reg + EMAC_CTL1);
404 } while ((reg & 0x01) != 0 && (--timeout));
405 if (!timeout) {
406 printf("%s: Timeout\n", __func__);
407 return -1;
408 }
409 }
410
411 /* Rewrite mac address after reset */
412 _sun8i_write_hwaddr(priv, enetaddr);
413
414 v = readl(priv->mac_reg + EMAC_TX_CTL1);
415 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
416 v |= BIT(1);
417 writel(v, priv->mac_reg + EMAC_TX_CTL1);
418
419 v = readl(priv->mac_reg + EMAC_RX_CTL1);
420 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
421 * complete frame has been written to RX DMA FIFO
422 */
423 v |= BIT(1);
424 writel(v, priv->mac_reg + EMAC_RX_CTL1);
425
426 /* DMA */
427 writel(8 << 24, priv->mac_reg + EMAC_CTL1);
428
429 /* Initialize rx/tx descriptors */
430 rx_descs_init(priv);
431 tx_descs_init(priv);
432
433 /* PHY Start Up */
Samuel Holland6a4d7992018-01-27 23:53:20 -0600434 phy_startup(priv->phydev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530435
436 sun8i_adjust_link(priv, priv->phydev);
437
438 /* Start RX DMA */
439 v = readl(priv->mac_reg + EMAC_RX_CTL1);
440 v |= BIT(30);
441 writel(v, priv->mac_reg + EMAC_RX_CTL1);
442 /* Start TX DMA */
443 v = readl(priv->mac_reg + EMAC_TX_CTL1);
444 v |= BIT(30);
445 writel(v, priv->mac_reg + EMAC_TX_CTL1);
446
447 /* Enable RX/TX */
448 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
449 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
450
451 return 0;
452}
453
454static int parse_phy_pins(struct udevice *dev)
455{
456 int offset;
457 const char *pin_name;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100458 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530459
Simon Glassdd79d6e2017-01-17 16:52:55 -0700460 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530461 "pinctrl-0");
462 if (offset < 0) {
463 printf("WARNING: emac: cannot find pinctrl-0 node\n");
464 return offset;
465 }
466
467 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywara26e549b2018-04-04 01:31:15 +0100468 "drive-strength", ~0);
469 if (drive != ~0) {
470 if (drive <= 10)
471 drive = SUN4I_PINCTRL_10_MA;
472 else if (drive <= 20)
473 drive = SUN4I_PINCTRL_20_MA;
474 else if (drive <= 30)
475 drive = SUN4I_PINCTRL_30_MA;
476 else
477 drive = SUN4I_PINCTRL_40_MA;
Andre Przywara26e549b2018-04-04 01:31:15 +0100478 }
479
480 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
481 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywara26e549b2018-04-04 01:31:15 +0100482 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
483 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100484
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530485 for (i = 0; ; i++) {
486 int pin;
487
Simon Glassb0ea7402016-10-02 17:59:28 -0600488 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100489 "pins", i, NULL);
490 if (!pin_name)
491 break;
Andre Przywara26e549b2018-04-04 01:31:15 +0100492
493 pin = sunxi_name_to_gpio(pin_name);
494 if (pin < 0)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530495 continue;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530496
497 sunxi_gpio_set_cfgpin(pin, SUN8I_GPD8_GMAC);
Andre Przywara26e549b2018-04-04 01:31:15 +0100498 if (drive != ~0)
499 sunxi_gpio_set_drv(pin, drive);
500 if (pull != ~0)
501 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530502 }
503
504 if (!i) {
Andre Przywara26e549b2018-04-04 01:31:15 +0100505 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530506 return -2;
507 }
508
509 return 0;
510}
511
512static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
513{
514 u32 status, desc_num = priv->rx_currdescnum;
515 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
516 int length = -EAGAIN;
517 int good_packet = 1;
518 uintptr_t desc_start = (uintptr_t)desc_p;
519 uintptr_t desc_end = desc_start +
520 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
521
522 ulong data_start = (uintptr_t)desc_p->buf_addr;
523 ulong data_end;
524
525 /* Invalidate entire buffer descriptor */
526 invalidate_dcache_range(desc_start, desc_end);
527
528 status = desc_p->status;
529
530 /* Check for DMA own bit */
531 if (!(status & BIT(31))) {
532 length = (desc_p->status >> 16) & 0x3FFF;
533
534 if (length < 0x40) {
535 good_packet = 0;
536 debug("RX: Bad Packet (runt)\n");
537 }
538
539 data_end = data_start + length;
540 /* Invalidate received data */
541 invalidate_dcache_range(rounddown(data_start,
542 ARCH_DMA_MINALIGN),
543 roundup(data_end,
544 ARCH_DMA_MINALIGN));
545 if (good_packet) {
Hans de Goedefcdb3b32016-07-27 17:31:17 +0200546 if (length > CONFIG_ETH_RXSIZE) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530547 printf("Received packet is too big (len=%d)\n",
548 length);
549 return -EMSGSIZE;
550 }
551 *packetp = (uchar *)(ulong)desc_p->buf_addr;
552 return length;
553 }
554 }
555
556 return length;
557}
558
559static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
560 int len)
561{
562 u32 v, desc_num = priv->tx_currdescnum;
563 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
564 uintptr_t desc_start = (uintptr_t)desc_p;
565 uintptr_t desc_end = desc_start +
566 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
567
568 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
569 uintptr_t data_end = data_start +
570 roundup(len, ARCH_DMA_MINALIGN);
571
572 /* Invalidate entire buffer descriptor */
573 invalidate_dcache_range(desc_start, desc_end);
574
575 desc_p->st = len;
576 /* Mandatory undocumented bit */
577 desc_p->st |= BIT(24);
578
579 memcpy((void *)data_start, packet, len);
580
581 /* Flush data to be sent */
582 flush_dcache_range(data_start, data_end);
583
584 /* frame end */
585 desc_p->st |= BIT(30);
586 desc_p->st |= BIT(31);
587
588 /*frame begin */
589 desc_p->st |= BIT(29);
590 desc_p->status = BIT(31);
591
592 /*Descriptors st and status field has changed, so FLUSH it */
593 flush_dcache_range(desc_start, desc_end);
594
595 /* Move to next Descriptor and wrap around */
596 if (++desc_num >= CONFIG_TX_DESCR_NUM)
597 desc_num = 0;
598 priv->tx_currdescnum = desc_num;
599
600 /* Start the DMA */
601 v = readl(priv->mac_reg + EMAC_TX_CTL1);
602 v |= BIT(31);/* mandatory */
603 v |= BIT(30);/* mandatory */
604 writel(v, priv->mac_reg + EMAC_TX_CTL1);
605
606 return 0;
607}
608
609static int sun8i_eth_write_hwaddr(struct udevice *dev)
610{
611 struct eth_pdata *pdata = dev_get_platdata(dev);
612 struct emac_eth_dev *priv = dev_get_priv(dev);
613
614 return _sun8i_write_hwaddr(priv, pdata->enetaddr);
615}
616
617static void sun8i_emac_board_setup(struct emac_eth_dev *priv)
618{
619 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
620
Chen-Yu Tsaib6b655b2017-09-22 15:26:33 +0800621#ifdef CONFIG_MACH_SUNXI_H3_H5
622 /* Only H3/H5 have clock controls for internal EPHY */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530623 if (priv->use_internal_phy) {
624 /* Set clock gating for ephy */
625 setbits_le32(&ccm->bus_gate4, BIT(AHB_GATE_OFFSET_EPHY));
626
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530627 /* Deassert EPHY */
628 setbits_le32(&ccm->ahb_reset2_cfg, BIT(AHB_RESET_OFFSET_EPHY));
629 }
Chen-Yu Tsaib6b655b2017-09-22 15:26:33 +0800630#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530631
632 /* Set clock gating for emac */
633 setbits_le32(&ccm->ahb_gate0, BIT(AHB_GATE_OFFSET_GMAC));
634
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530635 /* De-assert EMAC */
636 setbits_le32(&ccm->ahb_reset0_cfg, BIT(AHB_RESET_OFFSET_GMAC));
637}
638
Philipp Tomsich3297b552017-02-22 19:46:41 +0100639#if defined(CONFIG_DM_GPIO)
640static int sun8i_mdio_reset(struct mii_dev *bus)
641{
642 struct udevice *dev = bus->priv;
643 struct emac_eth_dev *priv = dev_get_priv(dev);
644 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
645 int ret;
646
647 if (!dm_gpio_is_valid(&priv->reset_gpio))
648 return 0;
649
650 /* reset the phy */
651 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
652 if (ret)
653 return ret;
654
655 udelay(pdata->reset_delays[0]);
656
657 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
658 if (ret)
659 return ret;
660
661 udelay(pdata->reset_delays[1]);
662
663 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
664 if (ret)
665 return ret;
666
667 udelay(pdata->reset_delays[2]);
668
669 return 0;
670}
671#endif
672
673static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530674{
675 struct mii_dev *bus = mdio_alloc();
676
677 if (!bus) {
678 debug("Failed to allocate MDIO bus\n");
679 return -ENOMEM;
680 }
681
682 bus->read = sun8i_mdio_read;
683 bus->write = sun8i_mdio_write;
684 snprintf(bus->name, sizeof(bus->name), name);
685 bus->priv = (void *)priv;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100686#if defined(CONFIG_DM_GPIO)
687 bus->reset = sun8i_mdio_reset;
688#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530689
690 return mdio_register(bus);
691}
692
693static int sun8i_emac_eth_start(struct udevice *dev)
694{
695 struct eth_pdata *pdata = dev_get_platdata(dev);
696
697 return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
698}
699
700static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
701{
702 struct emac_eth_dev *priv = dev_get_priv(dev);
703
704 return _sun8i_emac_eth_send(priv, packet, length);
705}
706
707static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
708{
709 struct emac_eth_dev *priv = dev_get_priv(dev);
710
711 return _sun8i_eth_recv(priv, packetp);
712}
713
714static int _sun8i_free_pkt(struct emac_eth_dev *priv)
715{
716 u32 desc_num = priv->rx_currdescnum;
717 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
718 uintptr_t desc_start = (uintptr_t)desc_p;
719 uintptr_t desc_end = desc_start +
720 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
721
722 /* Make the current descriptor valid again */
723 desc_p->status |= BIT(31);
724
725 /* Flush Status field of descriptor */
726 flush_dcache_range(desc_start, desc_end);
727
728 /* Move to next desc and wrap-around condition. */
729 if (++desc_num >= CONFIG_RX_DESCR_NUM)
730 desc_num = 0;
731 priv->rx_currdescnum = desc_num;
732
733 return 0;
734}
735
736static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
737 int length)
738{
739 struct emac_eth_dev *priv = dev_get_priv(dev);
740
741 return _sun8i_free_pkt(priv);
742}
743
744static void sun8i_emac_eth_stop(struct udevice *dev)
745{
746 struct emac_eth_dev *priv = dev_get_priv(dev);
747
748 /* Stop Rx/Tx transmitter */
749 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
750 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
751
752 /* Stop TX DMA */
753 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
754
755 phy_shutdown(priv->phydev);
756}
757
758static int sun8i_emac_eth_probe(struct udevice *dev)
759{
760 struct eth_pdata *pdata = dev_get_platdata(dev);
761 struct emac_eth_dev *priv = dev_get_priv(dev);
762
763 priv->mac_reg = (void *)pdata->iobase;
764
765 sun8i_emac_board_setup(priv);
Chen-Yu Tsai54f47a12016-07-22 18:16:10 +0800766 sun8i_emac_set_syscon(priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530767
Philipp Tomsich3297b552017-02-22 19:46:41 +0100768 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530769 priv->bus = miiphy_get_dev_by_name(dev->name);
770
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530771 return sun8i_phy_init(priv, dev);
772}
773
774static const struct eth_ops sun8i_emac_eth_ops = {
775 .start = sun8i_emac_eth_start,
776 .write_hwaddr = sun8i_eth_write_hwaddr,
777 .send = sun8i_emac_eth_send,
778 .recv = sun8i_emac_eth_recv,
779 .free_pkt = sun8i_eth_free_pkt,
780 .stop = sun8i_emac_eth_stop,
781};
782
783static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
784{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100785 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
786 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530787 struct emac_eth_dev *priv = dev_get_priv(dev);
788 const char *phy_mode;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100789 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700790 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530791 int offset = 0;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100792#ifdef CONFIG_DM_GPIO
793 int reset_flags = GPIOD_IS_OUT;
794 int ret = 0;
795#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530796
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100797 pdata->iobase = devfdt_get_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100798 if (pdata->iobase == FDT_ADDR_T_NONE) {
799 debug("%s: Cannot find MAC base address\n", __func__);
800 return -EINVAL;
801 }
802
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100803 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
804 if (offset < 0) {
805 debug("%s: cannot find syscon node\n", __func__);
806 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100807 }
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100808 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
809 if (!reg) {
810 debug("%s: cannot find reg property in syscon node\n",
811 __func__);
812 return -EINVAL;
813 }
814 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
815 offset, reg);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100816 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
817 debug("%s: Cannot find syscon base address\n", __func__);
818 return -EINVAL;
819 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530820
821 pdata->phy_interface = -1;
822 priv->phyaddr = -1;
823 priv->use_internal_phy = false;
824
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100825 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100826 if (offset < 0) {
827 debug("%s: Cannot find PHY address\n", __func__);
828 return -EINVAL;
829 }
830 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530831
Simon Glassdd79d6e2017-01-17 16:52:55 -0700832 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530833
834 if (phy_mode)
835 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
836 printf("phy interface%d\n", pdata->phy_interface);
837
838 if (pdata->phy_interface == -1) {
839 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
840 return -EINVAL;
841 }
842
843 priv->variant = dev_get_driver_data(dev);
844
845 if (!priv->variant) {
846 printf("%s: Missing variant '%s'\n", __func__,
847 (char *)priv->variant);
848 return -EINVAL;
849 }
850
851 if (priv->variant == H3_EMAC) {
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100852 int parent = fdt_parent_offset(gd->fdt_blob, offset);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100853
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100854 if (parent >= 0 &&
855 !fdt_node_check_compatible(gd->fdt_blob, parent,
856 "allwinner,sun8i-h3-mdio-internal"))
857 priv->use_internal_phy = true;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530858 }
859
860 priv->interface = pdata->phy_interface;
861
862 if (!priv->use_internal_phy)
863 parse_phy_pins(dev);
864
Philipp Tomsich3297b552017-02-22 19:46:41 +0100865#ifdef CONFIG_DM_GPIO
Simon Glass7a494432017-05-17 17:18:09 -0600866 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100867 "snps,reset-active-low"))
868 reset_flags |= GPIOD_ACTIVE_LOW;
869
870 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
871 &priv->reset_gpio, reset_flags);
872
873 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600874 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100875 "snps,reset-delays-us",
876 sun8i_pdata->reset_delays, 3);
877 } else if (ret == -ENOENT) {
878 ret = 0;
879 }
880#endif
881
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530882 return 0;
883}
884
885static const struct udevice_id sun8i_emac_eth_ids[] = {
886 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
887 {.compatible = "allwinner,sun50i-a64-emac",
888 .data = (uintptr_t)A64_EMAC },
889 {.compatible = "allwinner,sun8i-a83t-emac",
890 .data = (uintptr_t)A83T_EMAC },
891 { }
892};
893
894U_BOOT_DRIVER(eth_sun8i_emac) = {
895 .name = "eth_sun8i_emac",
896 .id = UCLASS_ETH,
897 .of_match = sun8i_emac_eth_ids,
898 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
899 .probe = sun8i_emac_eth_probe,
900 .ops = &sun8i_emac_eth_ops,
901 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100902 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530903 .flags = DM_FLAG_ALLOC_PRIV_DMA,
904};