blob: 98bd7a58232fd0e9416529e67bd835f8949202a0 [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;
141 struct reset_ctl tx_rst;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100142#ifdef CONFIG_DM_GPIO
143 struct gpio_desc reset_gpio;
144#endif
145};
146
147
148struct sun8i_eth_pdata {
149 struct eth_pdata eth_pdata;
150 u32 reset_delays[3];
Icenowy Zheng525dc442018-11-23 00:37:48 +0100151 int tx_delay_ps;
152 int rx_delay_ps;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530153};
154
Philipp Tomsich3297b552017-02-22 19:46:41 +0100155
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530156static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
157{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100158 struct udevice *dev = bus->priv;
159 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530160 ulong start;
161 u32 miiaddr = 0;
162 int timeout = CONFIG_MDIO_TIMEOUT;
163
164 miiaddr &= ~MDIO_CMD_MII_WRITE;
165 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
166 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
167 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
168
169 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
170
171 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
172 MDIO_CMD_MII_PHY_ADDR_MASK;
173
174 miiaddr |= MDIO_CMD_MII_BUSY;
175
176 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
177
178 start = get_timer(0);
179 while (get_timer(start) < timeout) {
180 if (!(readl(priv->mac_reg + EMAC_MII_CMD) & MDIO_CMD_MII_BUSY))
181 return readl(priv->mac_reg + EMAC_MII_DATA);
182 udelay(10);
183 };
184
185 return -1;
186}
187
188static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
189 u16 val)
190{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100191 struct udevice *dev = bus->priv;
192 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530193 ulong start;
194 u32 miiaddr = 0;
195 int ret = -1, timeout = CONFIG_MDIO_TIMEOUT;
196
197 miiaddr &= ~MDIO_CMD_MII_PHY_REG_ADDR_MASK;
198 miiaddr |= (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
199 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
200
201 miiaddr &= ~MDIO_CMD_MII_PHY_ADDR_MASK;
202 miiaddr |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
203 MDIO_CMD_MII_PHY_ADDR_MASK;
204
205 miiaddr |= MDIO_CMD_MII_WRITE;
206 miiaddr |= MDIO_CMD_MII_BUSY;
207
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530208 writel(val, priv->mac_reg + EMAC_MII_DATA);
Philipp Tomsich2b6dee12016-11-16 01:40:27 +0000209 writel(miiaddr, priv->mac_reg + EMAC_MII_CMD);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530210
211 start = get_timer(0);
212 while (get_timer(start) < timeout) {
213 if (!(readl(priv->mac_reg + EMAC_MII_CMD) &
214 MDIO_CMD_MII_BUSY)) {
215 ret = 0;
216 break;
217 }
218 udelay(10);
219 };
220
221 return ret;
222}
223
224static int _sun8i_write_hwaddr(struct emac_eth_dev *priv, u8 *mac_id)
225{
226 u32 macid_lo, macid_hi;
227
228 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
229 (mac_id[3] << 24);
230 macid_hi = mac_id[4] + (mac_id[5] << 8);
231
232 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
233 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
234
235 return 0;
236}
237
238static void sun8i_adjust_link(struct emac_eth_dev *priv,
239 struct phy_device *phydev)
240{
241 u32 v;
242
243 v = readl(priv->mac_reg + EMAC_CTL0);
244
245 if (phydev->duplex)
246 v |= BIT(0);
247 else
248 v &= ~BIT(0);
249
250 v &= ~0x0C;
251
252 switch (phydev->speed) {
253 case 1000:
254 break;
255 case 100:
256 v |= BIT(2);
257 v |= BIT(3);
258 break;
259 case 10:
260 v |= BIT(3);
261 break;
262 }
263 writel(v, priv->mac_reg + EMAC_CTL0);
264}
265
266static int sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 *reg)
267{
268 if (priv->use_internal_phy) {
269 /* H3 based SoC's that has an Internal 100MBit PHY
270 * needs to be configured and powered up before use
271 */
272 *reg &= ~H3_EPHY_DEFAULT_MASK;
273 *reg |= H3_EPHY_DEFAULT_VALUE;
274 *reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
275 *reg &= ~H3_EPHY_SHUTDOWN;
276 *reg |= H3_EPHY_SELECT;
277 } else
278 /* This is to select External Gigabit PHY on
279 * the boards with H3 SoC.
280 */
281 *reg &= ~H3_EPHY_SELECT;
282
283 return 0;
284}
285
Icenowy Zheng525dc442018-11-23 00:37:48 +0100286static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
287 struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530288{
289 int ret;
290 u32 reg;
291
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530292 if (priv->variant == R40_GMAC) {
293 /* Select RGMII for R40 */
294 reg = readl(priv->sysctl_reg + 0x164);
295 reg |= CCM_GMAC_CTRL_TX_CLK_SRC_INT_RGMII |
296 CCM_GMAC_CTRL_GPIT_RGMII |
297 CCM_GMAC_CTRL_TX_CLK_DELAY(CONFIG_GMAC_TX_DELAY);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530298
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530299 writel(reg, priv->sysctl_reg + 0x164);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200300 return 0;
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530301 }
302
303 reg = readl(priv->sysctl_reg + 0x30);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200304
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530305 if (priv->variant == H3_EMAC) {
306 ret = sun8i_emac_set_syscon_ephy(priv, &reg);
307 if (ret)
308 return ret;
309 }
310
311 reg &= ~(SC_ETCS_MASK | SC_EPIT);
312 if (priv->variant == H3_EMAC || priv->variant == A64_EMAC)
313 reg &= ~SC_RMII_EN;
314
315 switch (priv->interface) {
316 case PHY_INTERFACE_MODE_MII:
317 /* default */
318 break;
319 case PHY_INTERFACE_MODE_RGMII:
320 reg |= SC_EPIT | SC_ETCS_INT_GMII;
321 break;
322 case PHY_INTERFACE_MODE_RMII:
323 if (priv->variant == H3_EMAC ||
324 priv->variant == A64_EMAC) {
325 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
326 break;
327 }
328 /* RMII not supported on A83T */
329 default:
330 debug("%s: Invalid PHY interface\n", __func__);
331 return -EINVAL;
332 }
333
Icenowy Zheng525dc442018-11-23 00:37:48 +0100334 if (pdata->tx_delay_ps)
335 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
336 & SC_ETXDC_MASK;
337
338 if (pdata->rx_delay_ps)
339 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
340 & SC_ERXDC_MASK;
341
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100342 writel(reg, priv->sysctl_reg + 0x30);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530343
344 return 0;
345}
346
347static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
348{
349 struct phy_device *phydev;
350
351 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
352 if (!phydev)
353 return -ENODEV;
354
355 phy_connect_dev(phydev, dev);
356
357 priv->phydev = phydev;
358 phy_config(priv->phydev);
359
360 return 0;
361}
362
363static void rx_descs_init(struct emac_eth_dev *priv)
364{
365 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
366 char *rxbuffs = &priv->rxbuffer[0];
367 struct emac_dma_desc *desc_p;
368 u32 idx;
369
370 /* flush Rx buffers */
371 flush_dcache_range((uintptr_t)rxbuffs, (ulong)rxbuffs +
372 RX_TOTAL_BUFSIZE);
373
374 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
375 desc_p = &desc_table_p[idx];
376 desc_p->buf_addr = (uintptr_t)&rxbuffs[idx * CONFIG_ETH_BUFSIZE]
377 ;
378 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
Hans de Goedefcdb3b32016-07-27 17:31:17 +0200379 desc_p->st |= CONFIG_ETH_RXSIZE;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530380 desc_p->status = BIT(31);
381 }
382
383 /* Correcting the last pointer of the chain */
384 desc_p->next = (uintptr_t)&desc_table_p[0];
385
386 flush_dcache_range((uintptr_t)priv->rx_chain,
387 (uintptr_t)priv->rx_chain +
388 sizeof(priv->rx_chain));
389
390 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
391 priv->rx_currdescnum = 0;
392}
393
394static void tx_descs_init(struct emac_eth_dev *priv)
395{
396 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
397 char *txbuffs = &priv->txbuffer[0];
398 struct emac_dma_desc *desc_p;
399 u32 idx;
400
401 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
402 desc_p = &desc_table_p[idx];
403 desc_p->buf_addr = (uintptr_t)&txbuffs[idx * CONFIG_ETH_BUFSIZE]
404 ;
405 desc_p->next = (uintptr_t)&desc_table_p[idx + 1];
406 desc_p->status = (1 << 31);
407 desc_p->st = 0;
408 }
409
410 /* Correcting the last pointer of the chain */
411 desc_p->next = (uintptr_t)&desc_table_p[0];
412
413 /* Flush all Tx buffer descriptors */
414 flush_dcache_range((uintptr_t)priv->tx_chain,
415 (uintptr_t)priv->tx_chain +
416 sizeof(priv->tx_chain));
417
418 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
419 priv->tx_currdescnum = 0;
420}
421
422static int _sun8i_emac_eth_init(struct emac_eth_dev *priv, u8 *enetaddr)
423{
424 u32 reg, v;
425 int timeout = 100;
426
427 reg = readl((priv->mac_reg + EMAC_CTL1));
428
429 if (!(reg & 0x1)) {
430 /* Soft reset MAC */
431 setbits_le32((priv->mac_reg + EMAC_CTL1), 0x1);
432 do {
433 reg = readl(priv->mac_reg + EMAC_CTL1);
434 } while ((reg & 0x01) != 0 && (--timeout));
435 if (!timeout) {
436 printf("%s: Timeout\n", __func__);
437 return -1;
438 }
439 }
440
441 /* Rewrite mac address after reset */
442 _sun8i_write_hwaddr(priv, enetaddr);
443
444 v = readl(priv->mac_reg + EMAC_TX_CTL1);
445 /* TX_MD Transmission starts after a full frame located in TX DMA FIFO*/
446 v |= BIT(1);
447 writel(v, priv->mac_reg + EMAC_TX_CTL1);
448
449 v = readl(priv->mac_reg + EMAC_RX_CTL1);
450 /* RX_MD RX DMA reads data from RX DMA FIFO to host memory after a
451 * complete frame has been written to RX DMA FIFO
452 */
453 v |= BIT(1);
454 writel(v, priv->mac_reg + EMAC_RX_CTL1);
455
456 /* DMA */
457 writel(8 << 24, priv->mac_reg + EMAC_CTL1);
458
459 /* Initialize rx/tx descriptors */
460 rx_descs_init(priv);
461 tx_descs_init(priv);
462
463 /* PHY Start Up */
Samuel Holland6a4d7992018-01-27 23:53:20 -0600464 phy_startup(priv->phydev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530465
466 sun8i_adjust_link(priv, priv->phydev);
467
468 /* Start RX DMA */
469 v = readl(priv->mac_reg + EMAC_RX_CTL1);
470 v |= BIT(30);
471 writel(v, priv->mac_reg + EMAC_RX_CTL1);
472 /* Start TX DMA */
473 v = readl(priv->mac_reg + EMAC_TX_CTL1);
474 v |= BIT(30);
475 writel(v, priv->mac_reg + EMAC_TX_CTL1);
476
477 /* Enable RX/TX */
478 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
479 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
480
481 return 0;
482}
483
484static int parse_phy_pins(struct udevice *dev)
485{
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200486 struct emac_eth_dev *priv = dev_get_priv(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530487 int offset;
488 const char *pin_name;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100489 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530490
Simon Glassdd79d6e2017-01-17 16:52:55 -0700491 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530492 "pinctrl-0");
493 if (offset < 0) {
494 printf("WARNING: emac: cannot find pinctrl-0 node\n");
495 return offset;
496 }
497
498 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
Andre Przywara26e549b2018-04-04 01:31:15 +0100499 "drive-strength", ~0);
500 if (drive != ~0) {
501 if (drive <= 10)
502 drive = SUN4I_PINCTRL_10_MA;
503 else if (drive <= 20)
504 drive = SUN4I_PINCTRL_20_MA;
505 else if (drive <= 30)
506 drive = SUN4I_PINCTRL_30_MA;
507 else
508 drive = SUN4I_PINCTRL_40_MA;
Andre Przywara26e549b2018-04-04 01:31:15 +0100509 }
510
511 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
512 pull = SUN4I_PINCTRL_PULL_UP;
Andre Przywara26e549b2018-04-04 01:31:15 +0100513 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
514 pull = SUN4I_PINCTRL_PULL_DOWN;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100515
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530516 for (i = 0; ; i++) {
517 int pin;
518
Simon Glassb0ea7402016-10-02 17:59:28 -0600519 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100520 "pins", i, NULL);
521 if (!pin_name)
522 break;
Andre Przywara26e549b2018-04-04 01:31:15 +0100523
524 pin = sunxi_name_to_gpio(pin_name);
525 if (pin < 0)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530526 continue;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530527
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200528 if (priv->variant == H3_EMAC)
529 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_H3);
Lothar Feltene8cbced2018-07-13 10:45:28 +0200530 else if (priv->variant == R40_GMAC)
531 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX_R40);
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200532 else
533 sunxi_gpio_set_cfgpin(pin, SUN8I_IOMUX);
534
Andre Przywara26e549b2018-04-04 01:31:15 +0100535 if (drive != ~0)
536 sunxi_gpio_set_drv(pin, drive);
537 if (pull != ~0)
538 sunxi_gpio_set_pull(pin, pull);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530539 }
540
541 if (!i) {
Andre Przywara26e549b2018-04-04 01:31:15 +0100542 printf("WARNING: emac: cannot find pins property\n");
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530543 return -2;
544 }
545
546 return 0;
547}
548
549static int _sun8i_eth_recv(struct emac_eth_dev *priv, uchar **packetp)
550{
551 u32 status, desc_num = priv->rx_currdescnum;
552 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
553 int length = -EAGAIN;
554 int good_packet = 1;
555 uintptr_t desc_start = (uintptr_t)desc_p;
556 uintptr_t desc_end = desc_start +
557 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
558
559 ulong data_start = (uintptr_t)desc_p->buf_addr;
560 ulong data_end;
561
562 /* Invalidate entire buffer descriptor */
563 invalidate_dcache_range(desc_start, desc_end);
564
565 status = desc_p->status;
566
567 /* Check for DMA own bit */
568 if (!(status & BIT(31))) {
569 length = (desc_p->status >> 16) & 0x3FFF;
570
571 if (length < 0x40) {
572 good_packet = 0;
573 debug("RX: Bad Packet (runt)\n");
574 }
575
576 data_end = data_start + length;
577 /* Invalidate received data */
578 invalidate_dcache_range(rounddown(data_start,
579 ARCH_DMA_MINALIGN),
580 roundup(data_end,
581 ARCH_DMA_MINALIGN));
582 if (good_packet) {
Hans de Goedefcdb3b32016-07-27 17:31:17 +0200583 if (length > CONFIG_ETH_RXSIZE) {
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530584 printf("Received packet is too big (len=%d)\n",
585 length);
586 return -EMSGSIZE;
587 }
588 *packetp = (uchar *)(ulong)desc_p->buf_addr;
589 return length;
590 }
591 }
592
593 return length;
594}
595
596static int _sun8i_emac_eth_send(struct emac_eth_dev *priv, void *packet,
597 int len)
598{
599 u32 v, desc_num = priv->tx_currdescnum;
600 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
601 uintptr_t desc_start = (uintptr_t)desc_p;
602 uintptr_t desc_end = desc_start +
603 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
604
605 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
606 uintptr_t data_end = data_start +
607 roundup(len, ARCH_DMA_MINALIGN);
608
609 /* Invalidate entire buffer descriptor */
610 invalidate_dcache_range(desc_start, desc_end);
611
612 desc_p->st = len;
613 /* Mandatory undocumented bit */
614 desc_p->st |= BIT(24);
615
616 memcpy((void *)data_start, packet, len);
617
618 /* Flush data to be sent */
619 flush_dcache_range(data_start, data_end);
620
621 /* frame end */
622 desc_p->st |= BIT(30);
623 desc_p->st |= BIT(31);
624
625 /*frame begin */
626 desc_p->st |= BIT(29);
627 desc_p->status = BIT(31);
628
629 /*Descriptors st and status field has changed, so FLUSH it */
630 flush_dcache_range(desc_start, desc_end);
631
632 /* Move to next Descriptor and wrap around */
633 if (++desc_num >= CONFIG_TX_DESCR_NUM)
634 desc_num = 0;
635 priv->tx_currdescnum = desc_num;
636
637 /* Start the DMA */
638 v = readl(priv->mac_reg + EMAC_TX_CTL1);
639 v |= BIT(31);/* mandatory */
640 v |= BIT(30);/* mandatory */
641 writel(v, priv->mac_reg + EMAC_TX_CTL1);
642
643 return 0;
644}
645
646static int sun8i_eth_write_hwaddr(struct udevice *dev)
647{
648 struct eth_pdata *pdata = dev_get_platdata(dev);
649 struct emac_eth_dev *priv = dev_get_priv(dev);
650
651 return _sun8i_write_hwaddr(priv, pdata->enetaddr);
652}
653
Jagan Tekicb63d282019-02-28 00:26:58 +0530654static int sun8i_emac_board_setup(struct emac_eth_dev *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530655{
656 struct sunxi_ccm_reg *ccm = (struct sunxi_ccm_reg *)SUNXI_CCM_BASE;
Jagan Tekicb63d282019-02-28 00:26:58 +0530657 int ret;
658
659 ret = clk_enable(&priv->tx_clk);
660 if (ret) {
661 dev_err(dev, "failed to enable TX clock\n");
662 return ret;
663 }
664
665 if (reset_valid(&priv->tx_rst)) {
666 ret = reset_deassert(&priv->tx_rst);
667 if (ret) {
668 dev_err(dev, "failed to deassert TX reset\n");
669 goto err_tx_clk;
670 }
671 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530672
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200673 if (priv->variant == H3_EMAC) {
674 /* Only H3/H5 have clock controls for internal EPHY */
675 if (priv->use_internal_phy) {
676 /* Set clock gating for ephy */
677 setbits_le32(&ccm->bus_gate4,
678 BIT(AHB_GATE_OFFSET_EPHY));
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530679
Lothar Feltenacb9a5b2018-07-13 10:45:27 +0200680 /* Deassert EPHY */
681 setbits_le32(&ccm->ahb_reset2_cfg,
682 BIT(AHB_RESET_OFFSET_EPHY));
683 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530684 }
685
Jagan Tekicb63d282019-02-28 00:26:58 +0530686 return 0;
Lothar Feltene8cbced2018-07-13 10:45:28 +0200687
Jagan Tekicb63d282019-02-28 00:26:58 +0530688err_tx_clk:
689 clk_disable(&priv->tx_clk);
690 return ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530691}
692
Philipp Tomsich3297b552017-02-22 19:46:41 +0100693#if defined(CONFIG_DM_GPIO)
694static int sun8i_mdio_reset(struct mii_dev *bus)
695{
696 struct udevice *dev = bus->priv;
697 struct emac_eth_dev *priv = dev_get_priv(dev);
698 struct sun8i_eth_pdata *pdata = dev_get_platdata(dev);
699 int ret;
700
701 if (!dm_gpio_is_valid(&priv->reset_gpio))
702 return 0;
703
704 /* reset the phy */
705 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
706 if (ret)
707 return ret;
708
709 udelay(pdata->reset_delays[0]);
710
711 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
712 if (ret)
713 return ret;
714
715 udelay(pdata->reset_delays[1]);
716
717 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
718 if (ret)
719 return ret;
720
721 udelay(pdata->reset_delays[2]);
722
723 return 0;
724}
725#endif
726
727static int sun8i_mdio_init(const char *name, struct udevice *priv)
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530728{
729 struct mii_dev *bus = mdio_alloc();
730
731 if (!bus) {
732 debug("Failed to allocate MDIO bus\n");
733 return -ENOMEM;
734 }
735
736 bus->read = sun8i_mdio_read;
737 bus->write = sun8i_mdio_write;
738 snprintf(bus->name, sizeof(bus->name), name);
739 bus->priv = (void *)priv;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100740#if defined(CONFIG_DM_GPIO)
741 bus->reset = sun8i_mdio_reset;
742#endif
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530743
744 return mdio_register(bus);
745}
746
747static int sun8i_emac_eth_start(struct udevice *dev)
748{
749 struct eth_pdata *pdata = dev_get_platdata(dev);
750
751 return _sun8i_emac_eth_init(dev->priv, pdata->enetaddr);
752}
753
754static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
755{
756 struct emac_eth_dev *priv = dev_get_priv(dev);
757
758 return _sun8i_emac_eth_send(priv, packet, length);
759}
760
761static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
762{
763 struct emac_eth_dev *priv = dev_get_priv(dev);
764
765 return _sun8i_eth_recv(priv, packetp);
766}
767
768static int _sun8i_free_pkt(struct emac_eth_dev *priv)
769{
770 u32 desc_num = priv->rx_currdescnum;
771 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
772 uintptr_t desc_start = (uintptr_t)desc_p;
773 uintptr_t desc_end = desc_start +
774 roundup(sizeof(u32), ARCH_DMA_MINALIGN);
775
776 /* Make the current descriptor valid again */
777 desc_p->status |= BIT(31);
778
779 /* Flush Status field of descriptor */
780 flush_dcache_range(desc_start, desc_end);
781
782 /* Move to next desc and wrap-around condition. */
783 if (++desc_num >= CONFIG_RX_DESCR_NUM)
784 desc_num = 0;
785 priv->rx_currdescnum = desc_num;
786
787 return 0;
788}
789
790static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
791 int length)
792{
793 struct emac_eth_dev *priv = dev_get_priv(dev);
794
795 return _sun8i_free_pkt(priv);
796}
797
798static void sun8i_emac_eth_stop(struct udevice *dev)
799{
800 struct emac_eth_dev *priv = dev_get_priv(dev);
801
802 /* Stop Rx/Tx transmitter */
803 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, BIT(31));
804 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, BIT(31));
805
806 /* Stop TX DMA */
807 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, BIT(30));
808
809 phy_shutdown(priv->phydev);
810}
811
812static int sun8i_emac_eth_probe(struct udevice *dev)
813{
Icenowy Zheng525dc442018-11-23 00:37:48 +0100814 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
815 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530816 struct emac_eth_dev *priv = dev_get_priv(dev);
Jagan Tekicb63d282019-02-28 00:26:58 +0530817 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530818
819 priv->mac_reg = (void *)pdata->iobase;
820
Jagan Tekicb63d282019-02-28 00:26:58 +0530821 ret = sun8i_emac_board_setup(priv);
822 if (ret)
823 return ret;
824
Icenowy Zheng525dc442018-11-23 00:37:48 +0100825 sun8i_emac_set_syscon(sun8i_pdata, priv);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530826
Philipp Tomsich3297b552017-02-22 19:46:41 +0100827 sun8i_mdio_init(dev->name, dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530828 priv->bus = miiphy_get_dev_by_name(dev->name);
829
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530830 return sun8i_phy_init(priv, dev);
831}
832
833static const struct eth_ops sun8i_emac_eth_ops = {
834 .start = sun8i_emac_eth_start,
835 .write_hwaddr = sun8i_eth_write_hwaddr,
836 .send = sun8i_emac_eth_send,
837 .recv = sun8i_emac_eth_recv,
838 .free_pkt = sun8i_eth_free_pkt,
839 .stop = sun8i_emac_eth_stop,
840};
841
842static int sun8i_emac_eth_ofdata_to_platdata(struct udevice *dev)
843{
Philipp Tomsich3297b552017-02-22 19:46:41 +0100844 struct sun8i_eth_pdata *sun8i_pdata = dev_get_platdata(dev);
845 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530846 struct emac_eth_dev *priv = dev_get_priv(dev);
847 const char *phy_mode;
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100848 const fdt32_t *reg;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700849 int node = dev_of_offset(dev);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530850 int offset = 0;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100851#ifdef CONFIG_DM_GPIO
852 int reset_flags = GPIOD_IS_OUT;
Philipp Tomsich3297b552017-02-22 19:46:41 +0100853#endif
Jagan Tekicb63d282019-02-28 00:26:58 +0530854 int ret;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530855
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100856 pdata->iobase = devfdt_get_addr(dev);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100857 if (pdata->iobase == FDT_ADDR_T_NONE) {
858 debug("%s: Cannot find MAC base address\n", __func__);
859 return -EINVAL;
860 }
861
Lothar Feltene8cbced2018-07-13 10:45:28 +0200862 priv->variant = dev_get_driver_data(dev);
863
864 if (!priv->variant) {
865 printf("%s: Missing variant\n", __func__);
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100866 return -EINVAL;
867 }
Lothar Feltene8cbced2018-07-13 10:45:28 +0200868
Jagan Tekicb63d282019-02-28 00:26:58 +0530869 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
870 if (ret) {
871 dev_err(dev, "failed to get TX clock\n");
872 return ret;
873 }
874
875 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
876 if (ret && ret != -ENOENT) {
877 dev_err(dev, "failed to get TX reset\n");
878 return ret;
879 }
880
Jagan Teki1cfc64c2019-02-28 00:26:51 +0530881 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
882 if (offset < 0) {
883 debug("%s: cannot find syscon node\n", __func__);
884 return -EINVAL;
885 }
886
887 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
888 if (!reg) {
889 debug("%s: cannot find reg property in syscon node\n",
890 __func__);
891 return -EINVAL;
892 }
893 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
894 offset, reg);
895 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
896 debug("%s: Cannot find syscon base address\n", __func__);
897 return -EINVAL;
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100898 }
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530899
900 pdata->phy_interface = -1;
901 priv->phyaddr = -1;
902 priv->use_internal_phy = false;
903
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100904 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100905 if (offset < 0) {
906 debug("%s: Cannot find PHY address\n", __func__);
907 return -EINVAL;
908 }
909 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530910
Simon Glassdd79d6e2017-01-17 16:52:55 -0700911 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530912
913 if (phy_mode)
914 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
915 printf("phy interface%d\n", pdata->phy_interface);
916
917 if (pdata->phy_interface == -1) {
918 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
919 return -EINVAL;
920 }
921
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530922 if (priv->variant == H3_EMAC) {
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100923 int parent = fdt_parent_offset(gd->fdt_blob, offset);
Andre Przywaraba3a96d2018-04-04 01:31:16 +0100924
Andre Przywara94f3bbd2018-04-04 01:31:20 +0100925 if (parent >= 0 &&
926 !fdt_node_check_compatible(gd->fdt_blob, parent,
927 "allwinner,sun8i-h3-mdio-internal"))
928 priv->use_internal_phy = true;
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530929 }
930
931 priv->interface = pdata->phy_interface;
932
933 if (!priv->use_internal_phy)
934 parse_phy_pins(dev);
935
Icenowy Zheng525dc442018-11-23 00:37:48 +0100936 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
937 "allwinner,tx-delay-ps", 0);
938 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
939 printf("%s: Invalid TX delay value %d\n", __func__,
940 sun8i_pdata->tx_delay_ps);
941
942 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
943 "allwinner,rx-delay-ps", 0);
944 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
945 printf("%s: Invalid RX delay value %d\n", __func__,
946 sun8i_pdata->rx_delay_ps);
947
Philipp Tomsich3297b552017-02-22 19:46:41 +0100948#ifdef CONFIG_DM_GPIO
Simon Glass7a494432017-05-17 17:18:09 -0600949 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100950 "snps,reset-active-low"))
951 reset_flags |= GPIOD_ACTIVE_LOW;
952
953 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
954 &priv->reset_gpio, reset_flags);
955
956 if (ret == 0) {
Simon Glass7a494432017-05-17 17:18:09 -0600957 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100958 "snps,reset-delays-us",
959 sun8i_pdata->reset_delays, 3);
960 } else if (ret == -ENOENT) {
961 ret = 0;
962 }
963#endif
964
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530965 return 0;
966}
967
968static const struct udevice_id sun8i_emac_eth_ids[] = {
969 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
970 {.compatible = "allwinner,sun50i-a64-emac",
971 .data = (uintptr_t)A64_EMAC },
972 {.compatible = "allwinner,sun8i-a83t-emac",
973 .data = (uintptr_t)A83T_EMAC },
Lothar Feltene8cbced2018-07-13 10:45:28 +0200974 {.compatible = "allwinner,sun8i-r40-gmac",
975 .data = (uintptr_t)R40_GMAC },
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530976 { }
977};
978
979U_BOOT_DRIVER(eth_sun8i_emac) = {
980 .name = "eth_sun8i_emac",
981 .id = UCLASS_ETH,
982 .of_match = sun8i_emac_eth_ids,
983 .ofdata_to_platdata = sun8i_emac_eth_ofdata_to_platdata,
984 .probe = sun8i_emac_eth_probe,
985 .ops = &sun8i_emac_eth_ops,
986 .priv_auto_alloc_size = sizeof(struct emac_eth_dev),
Philipp Tomsich3297b552017-02-22 19:46:41 +0100987 .platdata_auto_alloc_size = sizeof(struct sun8i_eth_pdata),
Amit Singh Tomard194c0e2016-07-06 17:59:44 +0530988 .flags = DM_FLAG_ALLOC_PRIV_DMA,
989};