]> Git Repo - J-u-boot.git/blame - drivers/net/sun8i_emac.c
Merge https://source.denx.de/u-boot/custodians/u-boot-sunxi
[J-u-boot.git] / drivers / net / sun8i_emac.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
a29710c5
AST
2/*
3 * (C) Copyright 2016
4 * Author: Amit Singh Tomar, [email protected]
5 *
a29710c5
AST
6 * 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
1eb69ae4 13#include <cpu_func.h>
f7ae49fc 14#include <log.h>
90526e9f 15#include <asm/cache.h>
401d1c4f 16#include <asm/global_data.h>
42508461 17#include <asm/gpio.h>
a29710c5
AST
18#include <asm/io.h>
19#include <asm/arch/clock.h>
a29710c5 20#include <common.h>
d3a2c058 21#include <clk.h>
a29710c5
AST
22#include <dm.h>
23#include <fdt_support.h>
336d4615 24#include <dm/device_compat.h>
cd93d625 25#include <linux/bitops.h>
c05ed00a 26#include <linux/delay.h>
a29710c5
AST
27#include <linux/err.h>
28#include <malloc.h>
29#include <miiphy.h>
30#include <net.h>
d3a2c058 31#include <reset.h>
c0341173 32#include <dt-bindings/pinctrl/sun4i-a10.h>
f20f9465 33#include <wait_bit.h>
a29710c5 34
a29710c5
AST
35#define MDIO_CMD_MII_BUSY BIT(0)
36#define MDIO_CMD_MII_WRITE BIT(1)
37
38#define MDIO_CMD_MII_PHY_REG_ADDR_MASK 0x000001f0
39#define MDIO_CMD_MII_PHY_REG_ADDR_SHIFT 4
40#define MDIO_CMD_MII_PHY_ADDR_MASK 0x0001f000
41#define MDIO_CMD_MII_PHY_ADDR_SHIFT 12
4f0278da
AP
42#define MDIO_CMD_MII_CLK_CSR_DIV_16 0x0
43#define MDIO_CMD_MII_CLK_CSR_DIV_32 0x1
44#define MDIO_CMD_MII_CLK_CSR_DIV_64 0x2
45#define MDIO_CMD_MII_CLK_CSR_DIV_128 0x3
46#define MDIO_CMD_MII_CLK_CSR_SHIFT 20
a29710c5
AST
47
48#define CONFIG_TX_DESCR_NUM 32
49#define CONFIG_RX_DESCR_NUM 32
4069437d
HG
50#define CONFIG_ETH_BUFSIZE 2048 /* Note must be dma aligned */
51
52/*
53 * The datasheet says that each descriptor can transfers up to 4096 bytes
54 * But later, the register documentation reduces that value to 2048,
55 * using 2048 cause strange behaviours and even BSP driver use 2047
56 */
57#define CONFIG_ETH_RXSIZE 2044 /* Note must fit in ETH_BUFSIZE */
a29710c5
AST
58
59#define TX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_TX_DESCR_NUM)
60#define RX_TOTAL_BUFSIZE (CONFIG_ETH_BUFSIZE * CONFIG_RX_DESCR_NUM)
61
62#define H3_EPHY_DEFAULT_VALUE 0x58000
63#define H3_EPHY_DEFAULT_MASK GENMASK(31, 15)
64#define H3_EPHY_ADDR_SHIFT 20
65#define REG_PHY_ADDR_MASK GENMASK(4, 0)
66#define H3_EPHY_LED_POL BIT(17) /* 1: active low, 0: active high */
67#define H3_EPHY_SHUTDOWN BIT(16) /* 1: shutdown, 0: power up */
68#define H3_EPHY_SELECT BIT(15) /* 1: internal PHY, 0: external PHY */
69
70#define SC_RMII_EN BIT(13)
71#define SC_EPIT BIT(2) /* 1: RGMII, 0: MII */
72#define SC_ETCS_MASK GENMASK(1, 0)
73#define SC_ETCS_EXT_GMII 0x1
74#define SC_ETCS_INT_GMII 0x2
9b16ede4
IZ
75#define SC_ETXDC_MASK GENMASK(12, 10)
76#define SC_ETXDC_OFFSET 10
77#define SC_ERXDC_MASK GENMASK(9, 5)
78#define SC_ERXDC_OFFSET 5
a29710c5
AST
79
80#define CONFIG_MDIO_TIMEOUT (3 * CONFIG_SYS_HZ)
81
82#define AHB_GATE_OFFSET_EPHY 0
83
c6a21d66
LF
84/* IO mux settings */
85#define SUN8I_IOMUX_H3 2
eb5a2b67
AP
86#define SUN8I_IOMUX_R40 5
87#define SUN8I_IOMUX_H6 5
88#define SUN8I_IOMUX_H616 2
c6a21d66 89#define SUN8I_IOMUX 4
a29710c5
AST
90
91/* H3/A64 EMAC Register's offset */
92#define EMAC_CTL0 0x00
4fe86412
AP
93#define EMAC_CTL0_FULL_DUPLEX BIT(0)
94#define EMAC_CTL0_SPEED_MASK GENMASK(3, 2)
95#define EMAC_CTL0_SPEED_10 (0x2 << 2)
96#define EMAC_CTL0_SPEED_100 (0x3 << 2)
97#define EMAC_CTL0_SPEED_1000 (0x0 << 2)
a29710c5 98#define EMAC_CTL1 0x04
4fe86412
AP
99#define EMAC_CTL1_SOFT_RST BIT(0)
100#define EMAC_CTL1_BURST_LEN_SHIFT 24
a29710c5
AST
101#define EMAC_INT_STA 0x08
102#define EMAC_INT_EN 0x0c
103#define EMAC_TX_CTL0 0x10
4fe86412 104#define EMAC_TX_CTL0_TX_EN BIT(31)
a29710c5 105#define EMAC_TX_CTL1 0x14
4fe86412
AP
106#define EMAC_TX_CTL1_TX_MD BIT(1)
107#define EMAC_TX_CTL1_TX_DMA_EN BIT(30)
108#define EMAC_TX_CTL1_TX_DMA_START BIT(31)
a29710c5
AST
109#define EMAC_TX_FLOW_CTL 0x1c
110#define EMAC_TX_DMA_DESC 0x20
111#define EMAC_RX_CTL0 0x24
4fe86412 112#define EMAC_RX_CTL0_RX_EN BIT(31)
a29710c5 113#define EMAC_RX_CTL1 0x28
4fe86412 114#define EMAC_RX_CTL1_RX_MD BIT(1)
7edcb4e2
AP
115#define EMAC_RX_CTL1_RX_RUNT_FRM BIT(2)
116#define EMAC_RX_CTL1_RX_ERR_FRM BIT(3)
4fe86412
AP
117#define EMAC_RX_CTL1_RX_DMA_EN BIT(30)
118#define EMAC_RX_CTL1_RX_DMA_START BIT(31)
a29710c5
AST
119#define EMAC_RX_DMA_DESC 0x34
120#define EMAC_MII_CMD 0x48
121#define EMAC_MII_DATA 0x4c
122#define EMAC_ADDR0_HIGH 0x50
123#define EMAC_ADDR0_LOW 0x54
124#define EMAC_TX_DMA_STA 0xb0
125#define EMAC_TX_CUR_DESC 0xb4
126#define EMAC_TX_CUR_BUF 0xb8
127#define EMAC_RX_DMA_STA 0xc0
128#define EMAC_RX_CUR_DESC 0xc4
129
4fe86412
AP
130#define EMAC_DESC_OWN_DMA BIT(31)
131#define EMAC_DESC_LAST_DESC BIT(30)
132#define EMAC_DESC_FIRST_DESC BIT(29)
133#define EMAC_DESC_CHAIN_SECOND BIT(24)
134
7edcb4e2
AP
135#define EMAC_DESC_RX_ERROR_MASK 0x400068db
136
a29710c5
AST
137DECLARE_GLOBAL_DATA_PTR;
138
139enum emac_variant {
140 A83T_EMAC = 1,
141 H3_EMAC,
142 A64_EMAC,
e46d73fa 143 R40_GMAC,
99ac8618 144 H6_EMAC,
a29710c5
AST
145};
146
147struct emac_dma_desc {
148 u32 status;
4fe86412 149 u32 ctl_size;
a29710c5
AST
150 u32 buf_addr;
151 u32 next;
152} __aligned(ARCH_DMA_MINALIGN);
153
154struct emac_eth_dev {
155 struct emac_dma_desc rx_chain[CONFIG_TX_DESCR_NUM];
156 struct emac_dma_desc tx_chain[CONFIG_RX_DESCR_NUM];
157 char rxbuffer[RX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
158 char txbuffer[TX_TOTAL_BUFSIZE] __aligned(ARCH_DMA_MINALIGN);
159
160 u32 interface;
161 u32 phyaddr;
162 u32 link;
163 u32 speed;
164 u32 duplex;
165 u32 phy_configured;
166 u32 tx_currdescnum;
167 u32 rx_currdescnum;
168 u32 addr;
169 u32 tx_slot;
170 bool use_internal_phy;
171
172 enum emac_variant variant;
173 void *mac_reg;
174 phys_addr_t sysctl_reg;
175 struct phy_device *phydev;
176 struct mii_dev *bus;
d3a2c058 177 struct clk tx_clk;
2348453c 178 struct clk ephy_clk;
d3a2c058 179 struct reset_ctl tx_rst;
2348453c 180 struct reset_ctl ephy_rst;
bcee8d67 181#if CONFIG_IS_ENABLED(DM_GPIO)
4d555ae3
PT
182 struct gpio_desc reset_gpio;
183#endif
184};
185
186
187struct sun8i_eth_pdata {
188 struct eth_pdata eth_pdata;
189 u32 reset_delays[3];
9b16ede4
IZ
190 int tx_delay_ps;
191 int rx_delay_ps;
a29710c5
AST
192};
193
4d555ae3 194
a29710c5
AST
195static int sun8i_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
196{
4d555ae3
PT
197 struct udevice *dev = bus->priv;
198 struct emac_eth_dev *priv = dev_get_priv(dev);
f20f9465
AP
199 u32 mii_cmd;
200 int ret;
a29710c5 201
f20f9465 202 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
a29710c5 203 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
f20f9465 204 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
a29710c5
AST
205 MDIO_CMD_MII_PHY_ADDR_MASK;
206
4f0278da
AP
207 /*
208 * The EMAC clock is either 200 or 300 MHz, so we need a divider
209 * of 128 to get the MDIO frequency below the required 2.5 MHz.
210 */
02036d90
HS
211 if (!priv->use_internal_phy)
212 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
213 MDIO_CMD_MII_CLK_CSR_SHIFT;
4f0278da 214
f20f9465 215 mii_cmd |= MDIO_CMD_MII_BUSY;
a29710c5 216
f20f9465 217 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
a29710c5 218
f20f9465
AP
219 ret = wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
220 MDIO_CMD_MII_BUSY, false,
221 CONFIG_MDIO_TIMEOUT, true);
222 if (ret < 0)
223 return ret;
a29710c5 224
f20f9465 225 return readl(priv->mac_reg + EMAC_MII_DATA);
a29710c5
AST
226}
227
228static int sun8i_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
229 u16 val)
230{
4d555ae3
PT
231 struct udevice *dev = bus->priv;
232 struct emac_eth_dev *priv = dev_get_priv(dev);
f20f9465 233 u32 mii_cmd;
a29710c5 234
f20f9465 235 mii_cmd = (reg << MDIO_CMD_MII_PHY_REG_ADDR_SHIFT) &
a29710c5 236 MDIO_CMD_MII_PHY_REG_ADDR_MASK;
f20f9465 237 mii_cmd |= (addr << MDIO_CMD_MII_PHY_ADDR_SHIFT) &
a29710c5
AST
238 MDIO_CMD_MII_PHY_ADDR_MASK;
239
4f0278da
AP
240 /*
241 * The EMAC clock is either 200 or 300 MHz, so we need a divider
242 * of 128 to get the MDIO frequency below the required 2.5 MHz.
243 */
02036d90
HS
244 if (!priv->use_internal_phy)
245 mii_cmd |= MDIO_CMD_MII_CLK_CSR_DIV_128 <<
246 MDIO_CMD_MII_CLK_CSR_SHIFT;
4f0278da 247
f20f9465
AP
248 mii_cmd |= MDIO_CMD_MII_WRITE;
249 mii_cmd |= MDIO_CMD_MII_BUSY;
a29710c5 250
a29710c5 251 writel(val, priv->mac_reg + EMAC_MII_DATA);
f20f9465
AP
252 writel(mii_cmd, priv->mac_reg + EMAC_MII_CMD);
253
254 return wait_for_bit_le32(priv->mac_reg + EMAC_MII_CMD,
255 MDIO_CMD_MII_BUSY, false,
256 CONFIG_MDIO_TIMEOUT, true);
a29710c5
AST
257}
258
a5b2a991 259static int sun8i_eth_write_hwaddr(struct udevice *dev)
a29710c5 260{
a5b2a991 261 struct emac_eth_dev *priv = dev_get_priv(dev);
c69cda25 262 struct eth_pdata *pdata = dev_get_plat(dev);
a5b2a991 263 uchar *mac_id = pdata->enetaddr;
a29710c5
AST
264 u32 macid_lo, macid_hi;
265
266 macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
267 (mac_id[3] << 24);
268 macid_hi = mac_id[4] + (mac_id[5] << 8);
269
270 writel(macid_hi, priv->mac_reg + EMAC_ADDR0_HIGH);
271 writel(macid_lo, priv->mac_reg + EMAC_ADDR0_LOW);
272
273 return 0;
274}
275
276static void sun8i_adjust_link(struct emac_eth_dev *priv,
277 struct phy_device *phydev)
278{
279 u32 v;
280
281 v = readl(priv->mac_reg + EMAC_CTL0);
282
283 if (phydev->duplex)
4fe86412 284 v |= EMAC_CTL0_FULL_DUPLEX;
a29710c5 285 else
4fe86412 286 v &= ~EMAC_CTL0_FULL_DUPLEX;
a29710c5 287
4fe86412 288 v &= ~EMAC_CTL0_SPEED_MASK;
a29710c5
AST
289
290 switch (phydev->speed) {
291 case 1000:
4fe86412 292 v |= EMAC_CTL0_SPEED_1000;
a29710c5
AST
293 break;
294 case 100:
4fe86412 295 v |= EMAC_CTL0_SPEED_100;
a29710c5
AST
296 break;
297 case 10:
4fe86412 298 v |= EMAC_CTL0_SPEED_10;
a29710c5
AST
299 break;
300 }
301 writel(v, priv->mac_reg + EMAC_CTL0);
302}
303
b14e5205 304static u32 sun8i_emac_set_syscon_ephy(struct emac_eth_dev *priv, u32 reg)
a29710c5
AST
305{
306 if (priv->use_internal_phy) {
307 /* H3 based SoC's that has an Internal 100MBit PHY
308 * needs to be configured and powered up before use
309 */
b14e5205
AP
310 reg &= ~H3_EPHY_DEFAULT_MASK;
311 reg |= H3_EPHY_DEFAULT_VALUE;
312 reg |= priv->phyaddr << H3_EPHY_ADDR_SHIFT;
313 reg &= ~H3_EPHY_SHUTDOWN;
314 return reg | H3_EPHY_SELECT;
315 }
a29710c5 316
b14e5205
AP
317 /* This is to select External Gigabit PHY on those boards with
318 * an internal PHY. Does not hurt on other SoCs. Linux does
319 * it as well.
320 */
321 return reg & ~H3_EPHY_SELECT;
a29710c5
AST
322}
323
9b16ede4
IZ
324static int sun8i_emac_set_syscon(struct sun8i_eth_pdata *pdata,
325 struct emac_eth_dev *priv)
a29710c5 326{
a29710c5
AST
327 u32 reg;
328
695f6043
JT
329 if (priv->variant == R40_GMAC) {
330 /* Select RGMII for R40 */
331 reg = readl(priv->sysctl_reg + 0x164);
abdbefba
SH
332 reg |= SC_ETCS_INT_GMII |
333 SC_EPIT |
334 (CONFIG_GMAC_TX_DELAY << SC_ETXDC_OFFSET);
a29710c5 335
695f6043 336 writel(reg, priv->sysctl_reg + 0x164);
e46d73fa 337 return 0;
695f6043
JT
338 }
339
340 reg = readl(priv->sysctl_reg + 0x30);
e46d73fa 341
b14e5205 342 reg = sun8i_emac_set_syscon_ephy(priv, reg);
a29710c5
AST
343
344 reg &= ~(SC_ETCS_MASK | SC_EPIT);
99ac8618
SH
345 if (priv->variant == H3_EMAC ||
346 priv->variant == A64_EMAC ||
347 priv->variant == H6_EMAC)
a29710c5
AST
348 reg &= ~SC_RMII_EN;
349
350 switch (priv->interface) {
351 case PHY_INTERFACE_MODE_MII:
352 /* default */
353 break;
354 case PHY_INTERFACE_MODE_RGMII:
219a5d5a
AP
355 case PHY_INTERFACE_MODE_RGMII_ID:
356 case PHY_INTERFACE_MODE_RGMII_RXID:
357 case PHY_INTERFACE_MODE_RGMII_TXID:
a29710c5
AST
358 reg |= SC_EPIT | SC_ETCS_INT_GMII;
359 break;
360 case PHY_INTERFACE_MODE_RMII:
361 if (priv->variant == H3_EMAC ||
99ac8618
SH
362 priv->variant == A64_EMAC ||
363 priv->variant == H6_EMAC) {
a29710c5
AST
364 reg |= SC_RMII_EN | SC_ETCS_EXT_GMII;
365 break;
366 }
367 /* RMII not supported on A83T */
368 default:
369 debug("%s: Invalid PHY interface\n", __func__);
370 return -EINVAL;
371 }
372
9b16ede4
IZ
373 if (pdata->tx_delay_ps)
374 reg |= ((pdata->tx_delay_ps / 100) << SC_ETXDC_OFFSET)
375 & SC_ETXDC_MASK;
376
377 if (pdata->rx_delay_ps)
378 reg |= ((pdata->rx_delay_ps / 100) << SC_ERXDC_OFFSET)
379 & SC_ERXDC_MASK;
380
12afd957 381 writel(reg, priv->sysctl_reg + 0x30);
a29710c5
AST
382
383 return 0;
384}
385
386static int sun8i_phy_init(struct emac_eth_dev *priv, void *dev)
387{
388 struct phy_device *phydev;
389
390 phydev = phy_connect(priv->bus, priv->phyaddr, dev, priv->interface);
391 if (!phydev)
392 return -ENODEV;
393
394 phy_connect_dev(phydev, dev);
395
396 priv->phydev = phydev;
397 phy_config(priv->phydev);
398
399 return 0;
400}
401
8c274ec0 402#define cache_clean_descriptor(desc) \
0cf207ec 403 flush_dcache_range((uintptr_t)(desc), \
8c274ec0
AP
404 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
405
406#define cache_inv_descriptor(desc) \
407 invalidate_dcache_range((uintptr_t)(desc), \
408 (uintptr_t)(desc) + sizeof(struct emac_dma_desc))
409
a29710c5
AST
410static void rx_descs_init(struct emac_eth_dev *priv)
411{
412 struct emac_dma_desc *desc_table_p = &priv->rx_chain[0];
413 char *rxbuffs = &priv->rxbuffer[0];
414 struct emac_dma_desc *desc_p;
09501ff3 415 int i;
a29710c5 416
69853123
AP
417 /*
418 * Make sure we don't have dirty cache lines around, which could
419 * be cleaned to DRAM *after* the MAC has already written data to it.
420 */
421 invalidate_dcache_range((uintptr_t)desc_table_p,
422 (uintptr_t)desc_table_p + sizeof(priv->rx_chain));
423 invalidate_dcache_range((uintptr_t)rxbuffs,
424 (uintptr_t)rxbuffs + sizeof(priv->rxbuffer));
a29710c5 425
09501ff3
AP
426 for (i = 0; i < CONFIG_RX_DESCR_NUM; i++) {
427 desc_p = &desc_table_p[i];
428 desc_p->buf_addr = (uintptr_t)&rxbuffs[i * CONFIG_ETH_BUFSIZE];
429 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
69853123 430 desc_p->ctl_size = CONFIG_ETH_RXSIZE;
4fe86412 431 desc_p->status = EMAC_DESC_OWN_DMA;
a29710c5
AST
432 }
433
434 /* Correcting the last pointer of the chain */
435 desc_p->next = (uintptr_t)&desc_table_p[0];
436
437 flush_dcache_range((uintptr_t)priv->rx_chain,
438 (uintptr_t)priv->rx_chain +
439 sizeof(priv->rx_chain));
440
441 writel((uintptr_t)&desc_table_p[0], (priv->mac_reg + EMAC_RX_DMA_DESC));
442 priv->rx_currdescnum = 0;
443}
444
445static void tx_descs_init(struct emac_eth_dev *priv)
446{
447 struct emac_dma_desc *desc_table_p = &priv->tx_chain[0];
448 char *txbuffs = &priv->txbuffer[0];
449 struct emac_dma_desc *desc_p;
09501ff3 450 int i;
a29710c5 451
09501ff3
AP
452 for (i = 0; i < CONFIG_TX_DESCR_NUM; i++) {
453 desc_p = &desc_table_p[i];
454 desc_p->buf_addr = (uintptr_t)&txbuffs[i * CONFIG_ETH_BUFSIZE];
455 desc_p->next = (uintptr_t)&desc_table_p[i + 1];
4fe86412 456 desc_p->ctl_size = 0;
c35380c7 457 desc_p->status = 0;
a29710c5
AST
458 }
459
460 /* Correcting the last pointer of the chain */
461 desc_p->next = (uintptr_t)&desc_table_p[0];
462
ed909de5 463 /* Flush the first TX buffer descriptor we will tell the MAC about. */
8c274ec0 464 cache_clean_descriptor(desc_table_p);
a29710c5
AST
465
466 writel((uintptr_t)&desc_table_p[0], priv->mac_reg + EMAC_TX_DMA_DESC);
467 priv->tx_currdescnum = 0;
468}
469
a5b2a991 470static int sun8i_emac_eth_start(struct udevice *dev)
a29710c5 471{
a5b2a991 472 struct emac_eth_dev *priv = dev_get_priv(dev);
2808cf6c 473 int ret;
a29710c5 474
2c5600c3
AP
475 /* Soft reset MAC */
476 writel(EMAC_CTL1_SOFT_RST, priv->mac_reg + EMAC_CTL1);
477 ret = wait_for_bit_le32(priv->mac_reg + EMAC_CTL1,
478 EMAC_CTL1_SOFT_RST, false, 10, true);
479 if (ret) {
480 printf("%s: Timeout\n", __func__);
481 return ret;
a29710c5
AST
482 }
483
484 /* Rewrite mac address after reset */
a5b2a991 485 sun8i_eth_write_hwaddr(dev);
a29710c5 486
4fe86412
AP
487 /* transmission starts after the full frame arrived in TX DMA FIFO */
488 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_MD);
a29710c5 489
4fe86412
AP
490 /*
491 * RX DMA reads data from RX DMA FIFO to host memory after a
a29710c5
AST
492 * complete frame has been written to RX DMA FIFO
493 */
4fe86412 494 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_MD);
a29710c5 495
4fe86412
AP
496 /* DMA burst length */
497 writel(8 << EMAC_CTL1_BURST_LEN_SHIFT, priv->mac_reg + EMAC_CTL1);
a29710c5
AST
498
499 /* Initialize rx/tx descriptors */
500 rx_descs_init(priv);
501 tx_descs_init(priv);
502
503 /* PHY Start Up */
2808cf6c
AP
504 ret = phy_startup(priv->phydev);
505 if (ret)
506 return ret;
a29710c5
AST
507
508 sun8i_adjust_link(priv, priv->phydev);
509
4fe86412 510 /* Start RX/TX DMA */
7edcb4e2
AP
511 setbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN |
512 EMAC_RX_CTL1_RX_ERR_FRM | EMAC_RX_CTL1_RX_RUNT_FRM);
4fe86412 513 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
a29710c5
AST
514
515 /* Enable RX/TX */
4fe86412
AP
516 setbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
517 setbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
a29710c5
AST
518
519 return 0;
520}
521
522static int parse_phy_pins(struct udevice *dev)
523{
524 int offset;
525 const char *pin_name;
ecd0cec0 526 int drive, pull = SUN4I_PINCTRL_NO_PULL, i;
eb5a2b67 527 u32 iomux;
a29710c5 528
e160f7d4 529 offset = fdtdec_lookup_phandle(gd->fdt_blob, dev_of_offset(dev),
a29710c5
AST
530 "pinctrl-0");
531 if (offset < 0) {
532 printf("WARNING: emac: cannot find pinctrl-0 node\n");
533 return offset;
534 }
535
536 drive = fdt_getprop_u32_default_node(gd->fdt_blob, offset, 0,
c0341173
AP
537 "drive-strength", ~0);
538 if (drive != ~0) {
539 if (drive <= 10)
540 drive = SUN4I_PINCTRL_10_MA;
541 else if (drive <= 20)
542 drive = SUN4I_PINCTRL_20_MA;
543 else if (drive <= 30)
544 drive = SUN4I_PINCTRL_30_MA;
545 else
546 drive = SUN4I_PINCTRL_40_MA;
c0341173
AP
547 }
548
549 if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-up", NULL))
550 pull = SUN4I_PINCTRL_PULL_UP;
c0341173
AP
551 else if (fdt_get_property(gd->fdt_blob, offset, "bias-pull-down", NULL))
552 pull = SUN4I_PINCTRL_PULL_DOWN;
ecd0cec0 553
eb5a2b67
AP
554 /*
555 * The GPIO pinmux value is an integration choice, so depends on the
556 * SoC, not the EMAC variant.
557 */
4e26bc63 558 if (IS_ENABLED(CONFIG_MACH_SUNXI_H3_H5))
eb5a2b67
AP
559 iomux = SUN8I_IOMUX_H3;
560 else if (IS_ENABLED(CONFIG_MACH_SUN8I_R40))
561 iomux = SUN8I_IOMUX_R40;
562 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H6))
563 iomux = SUN8I_IOMUX_H6;
564 else if (IS_ENABLED(CONFIG_MACH_SUN50I_H616))
565 iomux = SUN8I_IOMUX_H616;
4e26bc63
AP
566 else if (IS_ENABLED(CONFIG_MACH_SUN8I_A83T))
567 iomux = SUN8I_IOMUX;
568 else if (IS_ENABLED(CONFIG_MACH_SUN50I))
eb5a2b67 569 iomux = SUN8I_IOMUX;
4e26bc63
AP
570 else
571 BUILD_BUG_ON_MSG(1, "missing pinmux value for Ethernet pins");
eb5a2b67 572
a29710c5
AST
573 for (i = 0; ; i++) {
574 int pin;
575
b02e4044 576 pin_name = fdt_stringlist_get(gd->fdt_blob, offset,
ecd0cec0
AP
577 "pins", i, NULL);
578 if (!pin_name)
579 break;
c0341173
AP
580
581 pin = sunxi_name_to_gpio(pin_name);
582 if (pin < 0)
a29710c5 583 continue;
a29710c5 584
eb5a2b67 585 sunxi_gpio_set_cfgpin(pin, iomux);
c6a21d66 586
c0341173
AP
587 if (drive != ~0)
588 sunxi_gpio_set_drv(pin, drive);
589 if (pull != ~0)
590 sunxi_gpio_set_pull(pin, pull);
a29710c5
AST
591 }
592
593 if (!i) {
c0341173 594 printf("WARNING: emac: cannot find pins property\n");
a29710c5
AST
595 return -2;
596 }
597
598 return 0;
599}
600
a5b2a991 601static int sun8i_emac_eth_recv(struct udevice *dev, int flags, uchar **packetp)
a29710c5 602{
a5b2a991 603 struct emac_eth_dev *priv = dev_get_priv(dev);
a29710c5
AST
604 u32 status, desc_num = priv->rx_currdescnum;
605 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
7edcb4e2
AP
606 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
607 int length;
a29710c5
AST
608
609 /* Invalidate entire buffer descriptor */
8c274ec0 610 cache_inv_descriptor(desc_p);
a29710c5
AST
611
612 status = desc_p->status;
613
614 /* Check for DMA own bit */
7edcb4e2
AP
615 if (status & EMAC_DESC_OWN_DMA)
616 return -EAGAIN;
a29710c5 617
7edcb4e2 618 length = (status >> 16) & 0x3fff;
a29710c5 619
7edcb4e2
AP
620 /* make sure we read from DRAM, not our cache */
621 invalidate_dcache_range(data_start,
622 data_start + roundup(length, ARCH_DMA_MINALIGN));
623
624 if (status & EMAC_DESC_RX_ERROR_MASK) {
625 debug("RX: packet error: 0x%x\n",
626 status & EMAC_DESC_RX_ERROR_MASK);
627 return 0;
a29710c5 628 }
7edcb4e2
AP
629 if (length < 0x40) {
630 debug("RX: Bad Packet (runt)\n");
631 return 0;
632 }
633
634 if (length > CONFIG_ETH_RXSIZE) {
635 debug("RX: Too large packet (%d bytes)\n", length);
636 return 0;
637 }
638
639 *packetp = (uchar *)(ulong)desc_p->buf_addr;
a29710c5
AST
640
641 return length;
642}
643
a5b2a991 644static int sun8i_emac_eth_send(struct udevice *dev, void *packet, int length)
a29710c5 645{
a5b2a991 646 struct emac_eth_dev *priv = dev_get_priv(dev);
4fe86412 647 u32 desc_num = priv->tx_currdescnum;
a29710c5 648 struct emac_dma_desc *desc_p = &priv->tx_chain[desc_num];
a29710c5
AST
649 uintptr_t data_start = (uintptr_t)desc_p->buf_addr;
650 uintptr_t data_end = data_start +
a5b2a991 651 roundup(length, ARCH_DMA_MINALIGN);
a29710c5 652
4fe86412 653 desc_p->ctl_size = length | EMAC_DESC_CHAIN_SECOND;
a29710c5 654
a5b2a991 655 memcpy((void *)data_start, packet, length);
a29710c5
AST
656
657 /* Flush data to be sent */
658 flush_dcache_range(data_start, data_end);
659
4fe86412
AP
660 /* frame begin and end */
661 desc_p->ctl_size |= EMAC_DESC_LAST_DESC | EMAC_DESC_FIRST_DESC;
662 desc_p->status = EMAC_DESC_OWN_DMA;
a29710c5 663
8c274ec0
AP
664 /* make sure the MAC reads the actual data from DRAM */
665 cache_clean_descriptor(desc_p);
a29710c5
AST
666
667 /* Move to next Descriptor and wrap around */
668 if (++desc_num >= CONFIG_TX_DESCR_NUM)
669 desc_num = 0;
670 priv->tx_currdescnum = desc_num;
671
672 /* Start the DMA */
4fe86412
AP
673 setbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_START);
674
675 /*
676 * Since we copied the data above, we return here without waiting
677 * for the packet to be actually send out.
678 */
a29710c5
AST
679
680 return 0;
681}
682
ef043693
SA
683static int sun8i_emac_board_setup(struct udevice *dev,
684 struct emac_eth_dev *priv)
a29710c5 685{
d3a2c058
JT
686 int ret;
687
688 ret = clk_enable(&priv->tx_clk);
689 if (ret) {
690 dev_err(dev, "failed to enable TX clock\n");
691 return ret;
692 }
693
694 if (reset_valid(&priv->tx_rst)) {
695 ret = reset_deassert(&priv->tx_rst);
696 if (ret) {
697 dev_err(dev, "failed to deassert TX reset\n");
698 goto err_tx_clk;
699 }
700 }
a29710c5 701
2348453c
JT
702 /* Only H3/H5 have clock controls for internal EPHY */
703 if (clk_valid(&priv->ephy_clk)) {
704 ret = clk_enable(&priv->ephy_clk);
705 if (ret) {
706 dev_err(dev, "failed to enable EPHY TX clock\n");
707 return ret;
708 }
709 }
710
711 if (reset_valid(&priv->ephy_rst)) {
712 ret = reset_deassert(&priv->ephy_rst);
713 if (ret) {
714 dev_err(dev, "failed to deassert EPHY TX clock\n");
715 return ret;
c6a21d66 716 }
a29710c5
AST
717 }
718
d3a2c058 719 return 0;
e46d73fa 720
d3a2c058
JT
721err_tx_clk:
722 clk_disable(&priv->tx_clk);
723 return ret;
a29710c5
AST
724}
725
bcee8d67 726#if CONFIG_IS_ENABLED(DM_GPIO)
4d555ae3
PT
727static int sun8i_mdio_reset(struct mii_dev *bus)
728{
729 struct udevice *dev = bus->priv;
730 struct emac_eth_dev *priv = dev_get_priv(dev);
c69cda25 731 struct sun8i_eth_pdata *pdata = dev_get_plat(dev);
4d555ae3
PT
732 int ret;
733
734 if (!dm_gpio_is_valid(&priv->reset_gpio))
735 return 0;
736
737 /* reset the phy */
738 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
739 if (ret)
740 return ret;
741
742 udelay(pdata->reset_delays[0]);
743
744 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
745 if (ret)
746 return ret;
747
748 udelay(pdata->reset_delays[1]);
749
750 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
751 if (ret)
752 return ret;
753
754 udelay(pdata->reset_delays[2]);
755
756 return 0;
757}
758#endif
759
760static int sun8i_mdio_init(const char *name, struct udevice *priv)
a29710c5
AST
761{
762 struct mii_dev *bus = mdio_alloc();
763
764 if (!bus) {
765 debug("Failed to allocate MDIO bus\n");
766 return -ENOMEM;
767 }
768
769 bus->read = sun8i_mdio_read;
770 bus->write = sun8i_mdio_write;
771 snprintf(bus->name, sizeof(bus->name), name);
772 bus->priv = (void *)priv;
bcee8d67 773#if CONFIG_IS_ENABLED(DM_GPIO)
4d555ae3
PT
774 bus->reset = sun8i_mdio_reset;
775#endif
a29710c5
AST
776
777 return mdio_register(bus);
778}
779
a5b2a991
AP
780static int sun8i_eth_free_pkt(struct udevice *dev, uchar *packet,
781 int length)
a29710c5
AST
782{
783 struct emac_eth_dev *priv = dev_get_priv(dev);
a29710c5
AST
784 u32 desc_num = priv->rx_currdescnum;
785 struct emac_dma_desc *desc_p = &priv->rx_chain[desc_num];
a29710c5 786
8c274ec0 787 /* give the current descriptor back to the MAC */
4fe86412 788 desc_p->status |= EMAC_DESC_OWN_DMA;
a29710c5
AST
789
790 /* Flush Status field of descriptor */
8c274ec0 791 cache_clean_descriptor(desc_p);
a29710c5
AST
792
793 /* Move to next desc and wrap-around condition. */
794 if (++desc_num >= CONFIG_RX_DESCR_NUM)
795 desc_num = 0;
796 priv->rx_currdescnum = desc_num;
797
798 return 0;
799}
800
a29710c5
AST
801static void sun8i_emac_eth_stop(struct udevice *dev)
802{
803 struct emac_eth_dev *priv = dev_get_priv(dev);
804
805 /* Stop Rx/Tx transmitter */
4fe86412
AP
806 clrbits_le32(priv->mac_reg + EMAC_RX_CTL0, EMAC_RX_CTL0_RX_EN);
807 clrbits_le32(priv->mac_reg + EMAC_TX_CTL0, EMAC_TX_CTL0_TX_EN);
a29710c5 808
4fe86412
AP
809 /* Stop RX/TX DMA */
810 clrbits_le32(priv->mac_reg + EMAC_TX_CTL1, EMAC_TX_CTL1_TX_DMA_EN);
811 clrbits_le32(priv->mac_reg + EMAC_RX_CTL1, EMAC_RX_CTL1_RX_DMA_EN);
a29710c5
AST
812
813 phy_shutdown(priv->phydev);
814}
815
816static int sun8i_emac_eth_probe(struct udevice *dev)
817{
c69cda25 818 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
9b16ede4 819 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
a29710c5 820 struct emac_eth_dev *priv = dev_get_priv(dev);
d3a2c058 821 int ret;
a29710c5
AST
822
823 priv->mac_reg = (void *)pdata->iobase;
824
ef043693 825 ret = sun8i_emac_board_setup(dev, priv);
d3a2c058
JT
826 if (ret)
827 return ret;
828
9b16ede4 829 sun8i_emac_set_syscon(sun8i_pdata, priv);
a29710c5 830
4d555ae3 831 sun8i_mdio_init(dev->name, dev);
a29710c5
AST
832 priv->bus = miiphy_get_dev_by_name(dev->name);
833
a29710c5
AST
834 return sun8i_phy_init(priv, dev);
835}
836
837static const struct eth_ops sun8i_emac_eth_ops = {
838 .start = sun8i_emac_eth_start,
839 .write_hwaddr = sun8i_eth_write_hwaddr,
840 .send = sun8i_emac_eth_send,
841 .recv = sun8i_emac_eth_recv,
842 .free_pkt = sun8i_eth_free_pkt,
843 .stop = sun8i_emac_eth_stop,
844};
845
88ae8fba 846static int sun8i_handle_internal_phy(struct udevice *dev, struct emac_eth_dev *priv)
2348453c 847{
88ae8fba
AP
848 struct ofnode_phandle_args phandle;
849 int ret;
d53e5222 850
88ae8fba
AP
851 ret = ofnode_parse_phandle_with_args(dev_ofnode(dev), "phy-handle",
852 NULL, 0, 0, &phandle);
853 if (ret)
854 return ret;
2348453c 855
88ae8fba
AP
856 /* If the PHY node is not a child of the internal MDIO bus, we are
857 * using some external PHY.
858 */
859 if (!ofnode_device_is_compatible(ofnode_get_parent(phandle.node),
860 "allwinner,sun8i-h3-mdio-internal"))
d53e5222
EV
861 return 0;
862
88ae8fba 863 ret = clk_get_by_index_nodev(phandle.node, 0, &priv->ephy_clk);
2348453c
JT
864 if (ret) {
865 dev_err(dev, "failed to get EPHY TX clock\n");
866 return ret;
867 }
868
88ae8fba 869 ret = reset_get_by_index_nodev(phandle.node, 0, &priv->ephy_rst);
2348453c
JT
870 if (ret) {
871 dev_err(dev, "failed to get EPHY TX reset\n");
872 return ret;
873 }
874
875 priv->use_internal_phy = true;
876
877 return 0;
878}
879
d1998a9f 880static int sun8i_emac_eth_of_to_plat(struct udevice *dev)
a29710c5 881{
c69cda25 882 struct sun8i_eth_pdata *sun8i_pdata = dev_get_plat(dev);
4d555ae3 883 struct eth_pdata *pdata = &sun8i_pdata->eth_pdata;
a29710c5
AST
884 struct emac_eth_dev *priv = dev_get_priv(dev);
885 const char *phy_mode;
ecd0cec0 886 const fdt32_t *reg;
e160f7d4 887 int node = dev_of_offset(dev);
a29710c5 888 int offset = 0;
bcee8d67 889#if CONFIG_IS_ENABLED(DM_GPIO)
4d555ae3 890 int reset_flags = GPIOD_IS_OUT;
4d555ae3 891#endif
d3a2c058 892 int ret;
a29710c5 893
2548493a 894 pdata->iobase = dev_read_addr(dev);
12afd957
AP
895 if (pdata->iobase == FDT_ADDR_T_NONE) {
896 debug("%s: Cannot find MAC base address\n", __func__);
897 return -EINVAL;
898 }
899
e46d73fa
LF
900 priv->variant = dev_get_driver_data(dev);
901
902 if (!priv->variant) {
903 printf("%s: Missing variant\n", __func__);
ecd0cec0
AP
904 return -EINVAL;
905 }
e46d73fa 906
d3a2c058
JT
907 ret = clk_get_by_name(dev, "stmmaceth", &priv->tx_clk);
908 if (ret) {
909 dev_err(dev, "failed to get TX clock\n");
910 return ret;
911 }
912
913 ret = reset_get_by_name(dev, "stmmaceth", &priv->tx_rst);
914 if (ret && ret != -ENOENT) {
915 dev_err(dev, "failed to get TX reset\n");
916 return ret;
917 }
918
695f6043
JT
919 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "syscon");
920 if (offset < 0) {
921 debug("%s: cannot find syscon node\n", __func__);
922 return -EINVAL;
923 }
924
925 reg = fdt_getprop(gd->fdt_blob, offset, "reg", NULL);
926 if (!reg) {
927 debug("%s: cannot find reg property in syscon node\n",
928 __func__);
929 return -EINVAL;
930 }
931 priv->sysctl_reg = fdt_translate_address((void *)gd->fdt_blob,
932 offset, reg);
933 if (priv->sysctl_reg == FDT_ADDR_T_NONE) {
934 debug("%s: Cannot find syscon base address\n", __func__);
935 return -EINVAL;
12afd957 936 }
a29710c5
AST
937
938 pdata->phy_interface = -1;
939 priv->phyaddr = -1;
940 priv->use_internal_phy = false;
941
ecd0cec0 942 offset = fdtdec_lookup_phandle(gd->fdt_blob, node, "phy-handle");
12afd957
AP
943 if (offset < 0) {
944 debug("%s: Cannot find PHY address\n", __func__);
945 return -EINVAL;
946 }
947 priv->phyaddr = fdtdec_get_int(gd->fdt_blob, offset, "reg", -1);
a29710c5 948
e160f7d4 949 phy_mode = fdt_getprop(gd->fdt_blob, node, "phy-mode", NULL);
a29710c5
AST
950
951 if (phy_mode)
952 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
953 printf("phy interface%d\n", pdata->phy_interface);
954
955 if (pdata->phy_interface == -1) {
956 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
957 return -EINVAL;
958 }
959
a29710c5 960 if (priv->variant == H3_EMAC) {
88ae8fba 961 ret = sun8i_handle_internal_phy(dev, priv);
2348453c
JT
962 if (ret)
963 return ret;
a29710c5
AST
964 }
965
966 priv->interface = pdata->phy_interface;
967
968 if (!priv->use_internal_phy)
969 parse_phy_pins(dev);
970
9b16ede4
IZ
971 sun8i_pdata->tx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
972 "allwinner,tx-delay-ps", 0);
973 if (sun8i_pdata->tx_delay_ps < 0 || sun8i_pdata->tx_delay_ps > 700)
974 printf("%s: Invalid TX delay value %d\n", __func__,
975 sun8i_pdata->tx_delay_ps);
976
977 sun8i_pdata->rx_delay_ps = fdtdec_get_int(gd->fdt_blob, node,
978 "allwinner,rx-delay-ps", 0);
979 if (sun8i_pdata->rx_delay_ps < 0 || sun8i_pdata->rx_delay_ps > 3100)
980 printf("%s: Invalid RX delay value %d\n", __func__,
981 sun8i_pdata->rx_delay_ps);
982
bcee8d67 983#if CONFIG_IS_ENABLED(DM_GPIO)
da409ccc 984 if (fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
4d555ae3
PT
985 "snps,reset-active-low"))
986 reset_flags |= GPIOD_ACTIVE_LOW;
987
988 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
989 &priv->reset_gpio, reset_flags);
990
991 if (ret == 0) {
da409ccc 992 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
4d555ae3
PT
993 "snps,reset-delays-us",
994 sun8i_pdata->reset_delays, 3);
995 } else if (ret == -ENOENT) {
996 ret = 0;
997 }
998#endif
999
a29710c5
AST
1000 return 0;
1001}
1002
1003static const struct udevice_id sun8i_emac_eth_ids[] = {
1004 {.compatible = "allwinner,sun8i-h3-emac", .data = (uintptr_t)H3_EMAC },
1005 {.compatible = "allwinner,sun50i-a64-emac",
1006 .data = (uintptr_t)A64_EMAC },
1007 {.compatible = "allwinner,sun8i-a83t-emac",
1008 .data = (uintptr_t)A83T_EMAC },
e46d73fa
LF
1009 {.compatible = "allwinner,sun8i-r40-gmac",
1010 .data = (uintptr_t)R40_GMAC },
99ac8618
SH
1011 {.compatible = "allwinner,sun50i-h6-emac",
1012 .data = (uintptr_t)H6_EMAC },
a29710c5
AST
1013 { }
1014};
1015
1016U_BOOT_DRIVER(eth_sun8i_emac) = {
1017 .name = "eth_sun8i_emac",
1018 .id = UCLASS_ETH,
1019 .of_match = sun8i_emac_eth_ids,
d1998a9f 1020 .of_to_plat = sun8i_emac_eth_of_to_plat,
a29710c5
AST
1021 .probe = sun8i_emac_eth_probe,
1022 .ops = &sun8i_emac_eth_ops,
41575d8e 1023 .priv_auto = sizeof(struct emac_eth_dev),
caa4daa2 1024 .plat_auto = sizeof(struct sun8i_eth_pdata),
a29710c5
AST
1025 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1026};
This page took 0.415744 seconds and 4 git commands to generate.