blob: c0a440886e29c42976174e0443d38046171555e8 [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>
Jagan Tekicb63d282019-02-28 00:26:58 +053017#include <clk.h>
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053018#include <dm.h>
19#include <fdt_support.h>
20#include <linux/err.h>
21#include <malloc.h>
22#include <miiphy.h>
23#include <net.h>
Jagan Tekicb63d282019-02-28 00:26:58 +053024#include <reset.h>
Andre Przywara26e549b2018-04-04 01:31:15 +010025#include <dt-bindings/pinctrl/sun4i-a10.h>
Philipp Tomsich3297b552017-02-22 19:46:41 +010026#ifdef CONFIG_DM_GPIO
27#include <asm-generic/gpio.h>
28#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053029
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053030#define MDIO_CMD_MII_BUSY BIT(0)
31#define MDIO_CMD_MII_WRITE BIT(1)
32
33#define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
34#define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
35#define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
36#define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
37
38#define CONFIG_TX_DESCR_NUM 32
39#define CONFIG_RX_DESCR_NUM 32
Hans de Goedefcdb3b32016-07-27 17:31:17 +020040#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
41
42/*
43 * The datasheet says that each descriptor can transfers up to 4096 bytes
44 * But later, the register documentation reduces that value to 2048,
45 * using 2048 cause strange behaviours and even BSP driver use 2047
46 */
47#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053048
49#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
50#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
51
52#define H3_EPHY_DEFAULT_VALUE 0x58000
53#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
54#define H3_EPHY_ADDR_SHIFT 20
55#define REG_PHY_ADDR_MASK GENMASK(4, 0)
56#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
57#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
58#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
59
60#define SC_RMII_EN BIT(13)
61#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
62#define SC_ETCS_MASK GENMASK(1, 0)
63#define SC_ETCS_EXT_GMII 0x1
64#define SC_ETCS_INT_GMII 0x2
Icenowy Zheng525dc442018-11-23 00:37:48 +010065#define SC_ETXDC_MASK GENMASK(12, 10)
66#define SC_ETXDC_OFFSET 10
67#define SC_ERXDC_MASK GENMASK(9, 5)
68#define SC_ERXDC_OFFSET 5
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053069
70#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
71
72#define AHB_GATE_OFFSET_EPHY 0
73
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020074/* IO mux settings */
75#define SUN8I_IOMUX_H3 2
Lothar Feltene8cbced2018-07-13 10:45:28 +020076#define SUN8I_IOMUX_R40 5
Lothar Feltenacb9a5b2018-07-13 10:45:27 +020077#define SUN8I_IOMUX 4
Amit Singh Tomard194c0e2016-07-06 17:59:44 +053078
79/* H3/A64 EMAC Register's offset */
80#define EMAC_CTL0 0x00
81#define EMAC_CTL1 0x04
82#define EMAC_INT_STA 0x08
83#define EMAC_INT_EN 0x0c
84#define EMAC_TX_CTL0 0x10
85#define EMAC_TX_CTL1 0x14
86#define EMAC_TX_FLOW_CTL 0x1c
87#define EMAC_TX_DMA_DESC 0x20
88#define EMAC_RX_CTL0 0x24
89#define EMAC_RX_CTL1 0x28
90#define EMAC_RX_DMA_DESC 0x34
91#define EMAC_MII_CMD 0x48
92#define EMAC_MII_DATA 0x4c
93#define EMAC_ADDR0_HIGH 0x50
94#define EMAC_ADDR0_LOW 0x54
95#define EMAC_TX_DMA_STA 0xb0
96#define EMAC_TX_CUR_DESC 0xb4
97#define EMAC_TX_CUR_BUF 0xb8
98#define EMAC_RX_DMA_STA 0xc0
99#define EMAC_RX_CUR_DESC 0xc4
100
101DECLARE_GLOBAL_DATA_PTR;
102
103enum emac_variant {
104 A83T_EMAC = 1,
105 H3_EMAC,
106 A64_EMAC,
Lothar Feltene8cbced2018-07-13 10:45:28 +0200107 R40_GMAC,
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530108};
109
110struct emac_dma_desc {
111 u32 status;
112 u32 st;
113 u32 buf_addr;
114 u32 next;
115} __aligned(ARCH_DMA_MINALIGN);
116
117struct emac_eth_dev {
118 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
119 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
120 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
121 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
122
123 u32 interface;
124 u32 phyaddr;
125 u32 link;
126 u32 speed;
127 u32 duplex;
128 u32 phy_configured;
129 u32 tx_currdescnum;
130 u32 rx_currdescnum;
131 u32 addr;
132 u32 tx_slot;
133 bool use_internal_phy;
134
135 enum emac_variant variant;
136 void *mac_reg;
137 phys_addr_t sysctl_reg;
138 struct phy_device *phydev;
139 struct mii_dev *bus;
Jagan Tekicb63d282019-02-28 00:26:58 +0530140 struct clk tx_clk;
Jagan Teki727ed792019-02-28 00:27:00 +0530141 struct clk ephy_clk;
Jagan Tekicb63d282019-02-28 00:26:58 +0530142 struct reset_ctl tx_rst;
Jagan Teki727ed792019-02-28 00:27:00 +0530143 struct reset_ctl ephy_rst;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100144#ifdef CONFIG_DM_GPIO
145 struct gpio_desc reset_gpio;
146#endif
147};
148
149
150struct sun8i_eth_pdata {
151 struct eth_pdata eth_pdata;
152 u32 reset_delays[3];
Icenowy Zheng525dc442018-11-23 00:37:48 +0100153 int tx_delay_ps;
154 int rx_delay_ps;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530155};
156
Philipp Tomsich3297b552017-02-22 19:46:41 +0100157
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530158static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
159{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100160 struct udevice *dev = bus->priv;
161 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530162 ulong start;
163 u32 miiaddr = 0;
164 int timeout = CONFIG_MDIO_TIMEOUT;
165
166 miiaddr &= ~MDIO_CMD_MII_WRITE;
167 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
168 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
169 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
170
171 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
172
173 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
174 MDIO_CMD_MII_PHY_ADDR_MASK;
175
176 miiaddr |= MDIO_CMD_MII_BUSY;
177
178 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
179
180 start = get_timer(0);
181 while (get_timer(start) < timeout) {
182 if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY))
183 return readl(priv->mac_reg + EMAC_MII_DATA);
184 udelay(10);
185 };
186
187 return -1;
188}
189
190static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
191 u16 val)
192{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100193 struct udevice *dev = bus->priv;
194 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530195 ulong start;
196 u32 miiaddr = 0;
197 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
198
199 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
200 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
201 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
202
203 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
204 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
205 MDIO_CMD_MII_PHY_ADDR_MASK;
206
207 miiaddr |= MDIO_CMD_MII_WRITE;
208 miiaddr |= MDIO_CMD_MII_BUSY;
209
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530210 writel(val, priv->mac_reg + EMAC_MII_DATA);
Philipp Tomsich2b6dee12016-11-16 01:40:27 +0000211 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530212
213 start = get_timer(0);
214 while (get_timer(start) < timeout) {
215 if (!(readl(priv->mac_reg + EMAC_MII_CMD) &
216 MDIO_CMD_MII_BUSY)) {
217 ret = 0;
218 break;
219 }
220 udelay(10);
221 };
222
223 return ret;
224}
225
226static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
227{
228 u32 macid_lo, macid_hi;
229
230 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
231 (mac_id[3] << 24);
232 macid_hi = mac_id[4] + (mac_id[5] << 8);
233
234 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
235 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
236
237 return 0;
238}
239
240static void sun8i_adjust_link(struct emac_eth_dev *priv,
241 struct phy_device *phydev)
242{
243 u32 v;
244
245 v = readl(priv->mac_reg + EMAC_CTL0);
246
247 if (phydev->duplex)
248 v |= BIT(0);
249 else
250 v &= ~BIT(0);
251
252 v &= ~0x0C;
253
254 switch (phydev->speed) {
255 case 1000:
256 break;
257 case 100:
258 v |= BIT(2);
259 v |= BIT(3);
260 break;
261 case 10:
262 v |= BIT(3);
263 break;
264 }
265 writel(v, priv->mac_reg + EMAC_CTL0);
266}
267
268static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
269{
270 if (priv->use_internal_phy) {
271 /* H3 based SoC's that has an Internal 100MBit PHY
272 * needs to be configured and powered up before use
273 */
274 *reg &= ~H3_EPHY_DEFAULT_MASK;
275 *reg |= H3_EPHY_DEFAULT_VALUE;
276 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
277 *reg &= ~H3_EPHY_SHUTDOWN;
278 *reg |= H3_EPHY_SELECT;
279 } else
280 /* This is to select External Gigabit PHY on
281 * the boards with H3 SoC.
282 */
283 *reg &= ~H3_EPHY_SELECT;
284
285 return 0;
286}
287
Icenowy Zheng525dc442018-11-23 00:37:48 +0100288static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
289 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530290{
291 int ret;
292 u32 reg;
293
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530294 if (priv->variant == R40_GMAC) {
295 /* Select RGMII for R40 */
296 reg = readl(priv->sysctl_reg + 0x164);
297 reg |= CCM_GMAC_CTRL_TX_CLK_SRC_INT_RGMII |
298 CCM_GMAC_CTRL_GPIT_RGMII |
299 CCM_GMAC_CTRL_TX_CLK_DELAY(CONFIG_GMAC_TX_DELAY);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530300
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530301 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200302 return 0;
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530303 }
304
305 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200306
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530307 if (priv->variant == H3_EMAC) {
308 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
309 if (ret)
310 return ret;
311 }
312
313 reg &= ~(SC_ETCS_MASK | SC_EPIT);
314 if (priv->variant == H3_EMAC || priv->variant == A64_EMAC)
315 reg &= ~SC_RMII_EN;
316
317 switch (priv->interface) {
318 case PHY_INTERFACE_MODE_MII:
319 /* default */
320 break;
321 case PHY_INTERFACE_MODE_RGMII:
322 reg |= SC_EPIT | SC_ETCS_INT_GMII;
323 break;
324 case PHY_INTERFACE_MODE_RMII:
325 if (priv->variant == H3_EMAC ||
326 priv->variant == A64_EMAC) {
327 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
328 break;
329 }
330 /* RMII not supported on A83T */
331 default:
332 debug("%s: Invalid PHY interface\n", __func__);
333 return -EINVAL;
334 }
335
Icenowy Zheng525dc442018-11-23 00:37:48 +0100336 if (pdata->tx_delay_ps)
337 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
338 & SC_ETXDC_MASK;
339
340 if (pdata->rx_delay_ps)
341 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
342 & SC_ERXDC_MASK;
343
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100344 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530345
346 return 0;
347}
348
349static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
350{
351 struct phy_device *phydev;
352
353 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
354 if (!phydev)
355 return -ENODEV;
356
357 phy_connect_dev(phydev, dev);
358
359 priv->phydev = phydev;
360 phy_config(priv->phydev);
361
362 return 0;
363}
364
365static void rx_descs_init(struct emac_eth_dev *priv)
366{
367 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
368 char *rxbuffs = &priv->rxbuffer[0];
369 struct emac_dma_desc *desc_p;
370 u32 idx;
371
372 /* flush Rx buffers */
373 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
374 RX_TOTAL_BUFSIZE);
375
376 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
377 desc_p = &desc_table_p[idx];
378 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
379 ;
380 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Hans de Goedefcdb3b32016-07-27 17:31:17 +0200381 desc_p->st |= CONFIG_ETH_RXSIZE;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530382 desc_p->status = BIT(31);
383 }
384
385 /* Correcting the last pointer of the chain */
386 desc_p->next = (uintptr_t)&desc_table_p[0];
387
388 flush_dcache_range((uintptr_t)priv->rx_chain,
389 (uintptr_t)priv->rx_chain +
390 sizeof(priv->rx_chain));
391
392 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
393 priv->rx_currdescnum = 0;
394}
395
396static void tx_descs_init(struct emac_eth_dev *priv)
397{
398 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
399 char *txbuffs = &priv->txbuffer[0];
400 struct emac_dma_desc *desc_p;
401 u32 idx;
402
403 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
404 desc_p = &desc_table_p[idx];
405 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
406 ;
407 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
408 desc_p->status = (1 << 31);
409 desc_p->st = 0;
410 }
411
412 /* Correcting the last pointer of the chain */
413 desc_p->next = (uintptr_t)&desc_table_p[0];
414
415 /* Flush all Tx buffer descriptors */
416 flush_dcache_range((uintptr_t)priv->tx_chain,
417 (uintptr_t)priv->tx_chain +
418 sizeof(priv->tx_chain));
419
420 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
421 priv->tx_currdescnum = 0;
422}
423
424static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
425{
426 u32 reg, v;
427 int timeout = 100;
428
429 reg = readl((priv->mac_reg + EMAC_CTL1));
430
431 if (!(reg & 0x1)) {
432 /* Soft reset MAC */
433 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
434 do {
435 reg = readl(priv->mac_reg + EMAC_CTL1);
436 } while ((reg & 0x01) != 0 && (--timeout));
437 if (!timeout) {
438 printf("%s: Timeout\n", __func__);
439 return -1;
440 }
441 }
442
443 /* Rewrite mac address after reset */
444 _sun8i_write_hwaddr(priv, enetaddr);
445
446 v = readl(priv->mac_reg + EMAC_TX_CTL1);
447 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
448 v |= BIT(1);
449 writel(v, priv->mac_reg + EMAC_TX_CTL1);
450
451 v = readl(priv->mac_reg + EMAC_RX_CTL1);
452 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
453 * complete frame has been written to RX DMA FIFO
454 */
455 v |= BIT(1);
456 writel(v, priv->mac_reg + EMAC_RX_CTL1);
457
458 /* DMA */
459 writel(8 << 24, priv->mac_reg + EMAC_CTL1);
460
461 /* Initialize rx/tx descriptors */
462 rx_descs_init(priv);
463 tx_descs_init(priv);
464
465 /* PHY Start Up */
Samuel Holland6a4d7992018-01-27 23:53:20 -0600466 phy_startup(priv->phydev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530467
468 sun8i_adjust_link(priv, priv->phydev);
469
470 /* Start RX DMA */
471 v = readl(priv->mac_reg + EMAC_RX_CTL1);
472 v |= BIT(30);
473 writel(v, priv->mac_reg + EMAC_RX_CTL1);
474 /* Start TX DMA */
475 v = readl(priv->mac_reg + EMAC_TX_CTL1);
476 v |= BIT(30);
477 writel(v, priv->mac_reg + EMAC_TX_CTL1);
478
479 /* Enable RX/TX */
480 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
481 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
482
483 return 0;
484}
485
486static int parse_phy_pins(struct udevice *dev)
487{
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200488 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530489 int offset;
490 const char *pin_name;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100491 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530492
Simon Glassdd79d6e2017-01-17 16:52:55 -0700493 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530494 "pinctrl-0");
495 if (offset < 0) {
496 printf("WARNING: emac: cannot find pinctrl-0 node\n");
497 return offset;
498 }
499
500 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywara26e549b2018-04-04 01:31:15 +0100501 "drive-strength", ~0);
502 if (drive != ~0) {
503 if (drive <= 10)
504 drive = SUN4I_PINCTRL_10_MA;
505 else if (drive <= 20)
506 drive = SUN4I_PINCTRL_20_MA;
507 else if (drive <= 30)
508 drive = SUN4I_PINCTRL_30_MA;
509 else
510 drive = SUN4I_PINCTRL_40_MA;
Andre Przywara26e549b2018-04-04 01:31:15 +0100511 }
512
513 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
514 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywara26e549b2018-04-04 01:31:15 +0100515 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
516 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100517
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530518 for (i = 0; ; i++) {
519 int pin;
520
Simon Glassb0ea7402016-10-02 17:59:28 -0600521 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100522 "pins", i, NULL);
523 if (!pin_name)
524 break;
Andre Przywara26e549b2018-04-04 01:31:15 +0100525
526 pin = sunxi_name_to_gpio(pin_name);
527 if (pin < 0)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530528 continue;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530529
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200530 if (priv->variant == H3_EMAC)
531 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200532 else if (priv->variant == R40_GMAC)
533 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200534 else
535 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
536
Andre Przywara26e549b2018-04-04 01:31:15 +0100537 if (drive != ~0)
538 sunxi_gpio_set_drv(pin, drive);
539 if (pull != ~0)
540 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530541 }
542
543 if (!i) {
Andre Przywara26e549b2018-04-04 01:31:15 +0100544 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530545 return -2;
546 }
547
548 return 0;
549}
550
551static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
552{
553 u32 status, desc_num = priv->rx_currdescnum;
554 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
555 int length = -EAGAIN;
556 int good_packet = 1;
557 uintptr_t desc_start = (uintptr_t)desc_p;
558 uintptr_t desc_end = desc_start +
559 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
560
561 ulong data_start = (uintptr_t)desc_p->buf_addr;
562 ulong data_end;
563
564 /* Invalidate entire buffer descriptor */
565 invalidate_dcache_range(desc_start, desc_end);
566
567 status = desc_p->status;
568
569 /* Check for DMA own bit */
570 if (!(status & BIT(31))) {
571 length = (desc_p->status >> 16) & 0x3FFF;
572
573 if (length < 0x40) {
574 good_packet = 0;
575 debug("RX: Bad Packet (runt)\n");
576 }
577
578 data_end = data_start + length;
579 /* Invalidate received data */
580 invalidate_dcache_range(rounddown(data_start,
581 ARCH_DMA_MINALIGN),
582 roundup(data_end,
583 ARCH_DMA_MINALIGN));
584 if (good_packet) {
Hans de Goedefcdb3b32016-07-27 17:31:17 +0200585 if (length > CONFIG_ETH_RXSIZE) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530586 printf("Received packet is too big (len=%d)\n",
587 length);
588 return -EMSGSIZE;
589 }
590 *packetp = (uchar *)(ulong)desc_p->buf_addr;
591 return length;
592 }
593 }
594
595 return length;
596}
597
598static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
599 int len)
600{
601 u32 v, desc_num = priv->tx_currdescnum;
602 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
603 uintptr_t desc_start = (uintptr_t)desc_p;
604 uintptr_t desc_end = desc_start +
605 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
606
607 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
608 uintptr_t data_end = data_start +
609 roundup(len, ARCH_DMA_MINALIGN);
610
611 /* Invalidate entire buffer descriptor */
612 invalidate_dcache_range(desc_start, desc_end);
613
614 desc_p->st = len;
615 /* Mandatory undocumented bit */
616 desc_p->st |= BIT(24);
617
618 memcpy((void *)data_start, packet, len);
619
620 /* Flush data to be sent */
621 flush_dcache_range(data_start, data_end);
622
623 /* frame end */
624 desc_p->st |= BIT(30);
625 desc_p->st |= BIT(31);
626
627 /*frame begin */
628 desc_p->st |= BIT(29);
629 desc_p->status = BIT(31);
630
631 /*Descriptors st and status field has changed, so FLUSH it */
632 flush_dcache_range(desc_start, desc_end);
633
634 /* Move to next Descriptor and wrap around */
635 if (++desc_num >= CONFIG_TX_DESCR_NUM)
636 desc_num = 0;
637 priv->tx_currdescnum = desc_num;
638
639 /* Start the DMA */
640 v = readl(priv->mac_reg + EMAC_TX_CTL1);
641 v |= BIT(31);/* mandatory */
642 v |= BIT(30);/* mandatory */
643 writel(v, priv->mac_reg + EMAC_TX_CTL1);
644
645 return 0;
646}
647
648static int sun8i_eth_write_hwaddr(struct udevice *dev)
649{
650 struct eth_pdata *pdata = dev_get_platdata(dev);
651 struct emac_eth_dev *priv = dev_get_priv(dev);
652
653 return _sun8i_write_hwaddr(priv, pdata->enetaddr);
654}
655
Jagan Tekicb63d282019-02-28 00:26:58 +0530656static int sun8i_emac_board_setup(struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530657{
Jagan Tekicb63d282019-02-28 00:26:58 +0530658 int ret;
659
660 ret = clk_enable(&priv->tx_clk);
661 if (ret) {
662 dev_err(dev, "failed to enable TX clock\n");
663 return ret;
664 }
665
666 if (reset_valid(&priv->tx_rst)) {
667 ret = reset_deassert(&priv->tx_rst);
668 if (ret) {
669 dev_err(dev, "failed to deassert TX reset\n");
670 goto err_tx_clk;
671 }
672 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530673
Jagan Teki727ed792019-02-28 00:27:00 +0530674 /* Only H3/H5 have clock controls for internal EPHY */
675 if (clk_valid(&priv->ephy_clk)) {
676 ret = clk_enable(&priv->ephy_clk);
677 if (ret) {
678 dev_err(dev, "failed to enable EPHY TX clock\n");
679 return ret;
680 }
681 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530682
Jagan Teki727ed792019-02-28 00:27:00 +0530683 if (reset_valid(&priv->ephy_rst)) {
684 ret = reset_deassert(&priv->ephy_rst);
685 if (ret) {
686 dev_err(dev, "failed to deassert EPHY TX clock\n");
687 return ret;
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200688 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530689 }
690
Jagan Tekicb63d282019-02-28 00:26:58 +0530691 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200692
Jagan Tekicb63d282019-02-28 00:26:58 +0530693err_tx_clk:
694 clk_disable(&priv->tx_clk);
695 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530696}
697
Philipp Tomsich3297b552017-02-22 19:46:41 +0100698#if defined(CONFIG_DM_GPIO)
699static int sun8i_mdio_reset(struct mii_dev *bus)
700{
701 struct udevice *dev = bus->priv;
702 struct emac_eth_dev *priv = dev_get_priv(dev);
703 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
704 int ret;
705
706 if (!dm_gpio_is_valid(&priv->reset_gpio))
707 return 0;
708
709 /* reset the phy */
710 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
711 if (ret)
712 return ret;
713
714 udelay(pdata->reset_delays[0]);
715
716 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
717 if (ret)
718 return ret;
719
720 udelay(pdata->reset_delays[1]);
721
722 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
723 if (ret)
724 return ret;
725
726 udelay(pdata->reset_delays[2]);
727
728 return 0;
729}
730#endif
731
732static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530733{
734 struct mii_dev *bus = mdio_alloc();
735
736 if (!bus) {
737 debug("Failed to allocate MDIO bus\n");
738 return -ENOMEM;
739 }
740
741 bus->read = sun8i_mdio_read;
742 bus->write = sun8i_mdio_write;
743 snprintf(bus->name, sizeof(bus->name), name);
744 bus->priv = (void *)priv;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100745#if defined(CONFIG_DM_GPIO)
746 bus->reset = sun8i_mdio_reset;
747#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530748
749 return mdio_register(bus);
750}
751
752static int sun8i_emac_eth_start(struct udevice *dev)
753{
754 struct eth_pdata *pdata = dev_get_platdata(dev);
755
756 return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
757}
758
759static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
760{
761 struct emac_eth_dev *priv = dev_get_priv(dev);
762
763 return _sun8i_emac_eth_send(priv, packet, length);
764}
765
766static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
767{
768 struct emac_eth_dev *priv = dev_get_priv(dev);
769
770 return _sun8i_eth_recv(priv, packetp);
771}
772
773static int _sun8i_free_pkt(struct emac_eth_dev *priv)
774{
775 u32 desc_num = priv->rx_currdescnum;
776 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
777 uintptr_t desc_start = (uintptr_t)desc_p;
778 uintptr_t desc_end = desc_start +
779 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
780
781 /* Make the current descriptor valid again */
782 desc_p->status |= BIT(31);
783
784 /* Flush Status field of descriptor */
785 flush_dcache_range(desc_start, desc_end);
786
787 /* Move to next desc and wrap-around condition. */
788 if (++desc_num >= CONFIG_RX_DESCR_NUM)
789 desc_num = 0;
790 priv->rx_currdescnum = desc_num;
791
792 return 0;
793}
794
795static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
796 int length)
797{
798 struct emac_eth_dev *priv = dev_get_priv(dev);
799
800 return _sun8i_free_pkt(priv);
801}
802
803static void sun8i_emac_eth_stop(struct udevice *dev)
804{
805 struct emac_eth_dev *priv = dev_get_priv(dev);
806
807 /* Stop Rx/Tx transmitter */
808 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
809 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
810
811 /* Stop TX DMA */
812 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
813
814 phy_shutdown(priv->phydev);
815}
816
817static int sun8i_emac_eth_probe(struct udevice *dev)
818{
Icenowy Zheng525dc442018-11-23 00:37:48 +0100819 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
820 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530821 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530822 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530823
824 priv->mac_reg = (void *)pdata->iobase;
825
Jagan Tekicb63d282019-02-28 00:26:58 +0530826 ret = sun8i_emac_board_setup(priv);
827 if (ret)
828 return ret;
829
Icenowy Zheng525dc442018-11-23 00:37:48 +0100830 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530831
Philipp Tomsich3297b552017-02-22 19:46:41 +0100832 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530833 priv->bus = miiphy_get_dev_by_name(dev->name);
834
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530835 return sun8i_phy_init(priv, dev);
836}
837
838static const struct eth_ops sun8i_emac_eth_ops = {
839 .start = sun8i_emac_eth_start,
840 .write_hwaddr = sun8i_eth_write_hwaddr,
841 .send = sun8i_emac_eth_send,
842 .recv = sun8i_emac_eth_recv,
843 .free_pkt = sun8i_eth_free_pkt,
844 .stop = sun8i_emac_eth_stop,
845};
846
Jagan Teki727ed792019-02-28 00:27:00 +0530847static int sun8i_get_ephy_nodes(struct emac_eth_dev *priv)
848{
849 int node, ret;
850
851 /* look for mdio-mux node for internal PHY node */
852 node = fdt_path_offset(gd->fdt_blob,
853 "/soc/ethernet@1c30000/mdio-mux/mdio@1/ethernet-phy@1");
854 if (node < 0) {
855 debug("failed to get mdio-mux with internal PHY\n");
856 return node;
857 }
858
859 ret = fdt_node_check_compatible(gd->fdt_blob, node,
860 "allwinner,sun8i-h3-mdio-internal");
861 if (ret < 0) {
862 debug("failed to find mdio-internal node\n");
863 return ret;
864 }
865
866 ret = clk_get_by_index_nodev(offset_to_ofnode(node), 0,
867 &priv->ephy_clk);
868 if (ret) {
869 dev_err(dev, "failed to get EPHY TX clock\n");
870 return ret;
871 }
872
873 ret = reset_get_by_index_nodev(offset_to_ofnode(node), 0,
874 &priv->ephy_rst);
875 if (ret) {
876 dev_err(dev, "failed to get EPHY TX reset\n");
877 return ret;
878 }
879
880 priv->use_internal_phy = true;
881
882 return 0;
883}
884
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530885static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
886{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100887 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
888 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530889 struct emac_eth_dev *priv = dev_get_priv(dev);
890 const char *phy_mode;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100891 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700892 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530893 int offset = 0;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100894#ifdef CONFIG_DM_GPIO
895 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100896#endif
Jagan Tekicb63d282019-02-28 00:26:58 +0530897 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530898
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100899 pdata->iobase = devfdt_get_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100900 if (pdata->iobase == FDT_ADDR_T_NONE) {
901 debug("%s: Cannot find MAC base address\n", __func__);
902 return -EINVAL;
903 }
904
Lothar Feltene8cbced2018-07-13 10:45:28 +0200905 priv->variant = dev_get_driver_data(dev);
906
907 if (!priv->variant) {
908 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100909 return -EINVAL;
910 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200911
Jagan Tekicb63d282019-02-28 00:26:58 +0530912 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
913 if (ret) {
914 dev_err(dev, "failed to get TX clock\n");
915 return ret;
916 }
917
918 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
919 if (ret && ret != -ENOENT) {
920 dev_err(dev, "failed to get TX reset\n");
921 return ret;
922 }
923
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530924 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
925 if (offset < 0) {
926 debug("%s: cannot find syscon node\n", __func__);
927 return -EINVAL;
928 }
929
930 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
931 if (!reg) {
932 debug("%s: cannot find reg property in syscon node\n",
933 __func__);
934 return -EINVAL;
935 }
936 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
937 offset, reg);
938 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
939 debug("%s: Cannot find syscon base address\n", __func__);
940 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100941 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530942
943 pdata->phy_interface = -1;
944 priv->phyaddr = -1;
945 priv->use_internal_phy = false;
946
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100947 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100948 if (offset < 0) {
949 debug("%s: Cannot find PHY address\n", __func__);
950 return -EINVAL;
951 }
952 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530953
Simon Glassdd79d6e2017-01-17 16:52:55 -0700954 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530955
956 if (phy_mode)
957 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
958 printf("phy interface%d\n", pdata->phy_interface);
959
960 if (pdata->phy_interface == -1) {
961 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
962 return -EINVAL;
963 }
964
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530965 if (priv->variant == H3_EMAC) {
Jagan Teki727ed792019-02-28 00:27:00 +0530966 ret = sun8i_get_ephy_nodes(priv);
967 if (ret)
968 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530969 }
970
971 priv->interface = pdata->phy_interface;
972
973 if (!priv->use_internal_phy)
974 parse_phy_pins(dev);
975
Icenowy Zheng525dc442018-11-23 00:37:48 +0100976 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
977 "allwinner,tx-delay-ps", 0);
978 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
979 printf("%s: Invalid TX delay value %d\n", __func__,
980 sun8i_pdata->tx_delay_ps);
981
982 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
983 "allwinner,rx-delay-ps", 0);
984 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
985 printf("%s: Invalid RX delay value %d\n", __func__,
986 sun8i_pdata->rx_delay_ps);
987
Philipp Tomsich3297b552017-02-22 19:46:41 +0100988#ifdef CONFIG_DM_GPIO
Simon Glass7a494432017-05-17 17:18:09 -0600989 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100990 "snps,reset-active-low"))
991 reset_flags |= GPIOD_ACTIVE_LOW;
992
993 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
994 &priv->reset_gpio, reset_flags);
995
996 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600997 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100998 "snps,reset-delays-us",
999 sun8i_pdata->reset_delays, 3);
1000 } else if (ret == -ENOENT) {
1001 ret = 0;
1002 }
1003#endif
1004
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301005 return 0;
1006}
1007
1008static const struct udevice_id sun8i_emac_eth_ids[] = {
1009 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
1010 {.compatible = "allwinner,sun50i-a64-emac",
1011 .data = (uintptr_t)A64_EMAC },
1012 {.compatible = "allwinner,sun8i-a83t-emac",
1013 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene8cbced2018-07-13 10:45:28 +02001014 {.compatible = "allwinner,sun8i-r40-gmac",
1015 .data = (uintptr_t)R40_GMAC },
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301016 { }
1017};
1018
1019U_BOOT_DRIVER(eth_sun8i_emac) = {
1020 .name = "eth_sun8i_emac",
1021 .id = UCLASS_ETH,
1022 .of_match = sun8i_emac_eth_ids,
1023 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
1024 .probe = sun8i_emac_eth_probe,
1025 .ops = &sun8i_emac_eth_ops,
1026 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +01001027 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +05301028 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1029};