]> Git Repo - J-u-boot.git/blame - drivers/net/designware.c
common: Drop log.h from common header
[J-u-boot.git] / drivers / net / designware.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
5b1b1883
VK
2/*
3 * (C) Copyright 2010
4 * Vipin Kumar, ST Micoelectronics, [email protected].
5b1b1883
VK
5 */
6
7/*
64dcd25f 8 * Designware ethernet IP driver for U-Boot
5b1b1883
VK
9 */
10
11#include <common.h>
ba1f9667 12#include <clk.h>
1eb69ae4 13#include <cpu_func.h>
75577ba4 14#include <dm.h>
64dcd25f 15#include <errno.h>
f7ae49fc 16#include <log.h>
5b1b1883
VK
17#include <miiphy.h>
18#include <malloc.h>
90526e9f 19#include <net.h>
8b7ee66c 20#include <pci.h>
495c70f9 21#include <reset.h>
90526e9f 22#include <asm/cache.h>
336d4615 23#include <dm/device_compat.h>
61b29b82 24#include <dm/devres.h>
ef76025a 25#include <linux/compiler.h>
5b1b1883 26#include <linux/err.h>
7a9ca9db 27#include <linux/kernel.h>
5b1b1883 28#include <asm/io.h>
6ec922fa 29#include <power/regulator.h>
5b1b1883
VK
30#include "designware.h"
31
92a190aa
AB
32static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
33{
90b7fc92
SS
34#ifdef CONFIG_DM_ETH
35 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
36 struct eth_mac_regs *mac_p = priv->mac_regs_p;
37#else
92a190aa 38 struct eth_mac_regs *mac_p = bus->priv;
90b7fc92 39#endif
92a190aa
AB
40 ulong start;
41 u16 miiaddr;
42 int timeout = CONFIG_MDIO_TIMEOUT;
43
44 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
45 ((reg << MIIREGSHIFT) & MII_REGMSK);
46
47 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
48
49 start = get_timer(0);
50 while (get_timer(start) < timeout) {
51 if (!(readl(&mac_p->miiaddr) & MII_BUSY))
52 return readl(&mac_p->miidata);
53 udelay(10);
54 };
55
64dcd25f 56 return -ETIMEDOUT;
92a190aa
AB
57}
58
59static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
60 u16 val)
61{
90b7fc92
SS
62#ifdef CONFIG_DM_ETH
63 struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
64 struct eth_mac_regs *mac_p = priv->mac_regs_p;
65#else
92a190aa 66 struct eth_mac_regs *mac_p = bus->priv;
90b7fc92 67#endif
92a190aa
AB
68 ulong start;
69 u16 miiaddr;
64dcd25f 70 int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
92a190aa
AB
71
72 writel(val, &mac_p->miidata);
73 miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
74 ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
75
76 writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
77
78 start = get_timer(0);
79 while (get_timer(start) < timeout) {
80 if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
81 ret = 0;
82 break;
83 }
84 udelay(10);
85 };
86
87 return ret;
88}
89
bcee8d67 90#if defined(CONFIG_DM_ETH) && CONFIG_IS_ENABLED(DM_GPIO)
90b7fc92
SS
91static int dw_mdio_reset(struct mii_dev *bus)
92{
93 struct udevice *dev = bus->priv;
94 struct dw_eth_dev *priv = dev_get_priv(dev);
95 struct dw_eth_pdata *pdata = dev_get_platdata(dev);
96 int ret;
97
98 if (!dm_gpio_is_valid(&priv->reset_gpio))
99 return 0;
100
101 /* reset the phy */
102 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
103 if (ret)
104 return ret;
105
106 udelay(pdata->reset_delays[0]);
107
108 ret = dm_gpio_set_value(&priv->reset_gpio, 1);
109 if (ret)
110 return ret;
111
112 udelay(pdata->reset_delays[1]);
113
114 ret = dm_gpio_set_value(&priv->reset_gpio, 0);
115 if (ret)
116 return ret;
117
118 udelay(pdata->reset_delays[2]);
119
120 return 0;
121}
122#endif
123
124static int dw_mdio_init(const char *name, void *priv)
92a190aa
AB
125{
126 struct mii_dev *bus = mdio_alloc();
127
128 if (!bus) {
129 printf("Failed to allocate MDIO bus\n");
64dcd25f 130 return -ENOMEM;
92a190aa
AB
131 }
132
133 bus->read = dw_mdio_read;
134 bus->write = dw_mdio_write;
192bc694 135 snprintf(bus->name, sizeof(bus->name), "%s", name);
bcee8d67 136#if defined(CONFIG_DM_ETH) && CONFIG_IS_ENABLED(DM_GPIO)
90b7fc92
SS
137 bus->reset = dw_mdio_reset;
138#endif
92a190aa 139
90b7fc92 140 bus->priv = priv;
92a190aa
AB
141
142 return mdio_register(bus);
143}
13edd170 144
64dcd25f 145static void tx_descs_init(struct dw_eth_dev *priv)
5b1b1883 146{
5b1b1883
VK
147 struct eth_dma_regs *dma_p = priv->dma_regs_p;
148 struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
149 char *txbuffs = &priv->txbuffs[0];
150 struct dmamacdescr *desc_p;
151 u32 idx;
152
153 for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
154 desc_p = &desc_table_p[idx];
0e1a3e30
BG
155 desc_p->dmamac_addr = (ulong)&txbuffs[idx * CONFIG_ETH_BUFSIZE];
156 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
5b1b1883
VK
157
158#if defined(CONFIG_DW_ALTDESCRIPTOR)
159 desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
2b261092
MV
160 DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
161 DESC_TXSTS_TXCHECKINSCTRL |
5b1b1883
VK
162 DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
163
164 desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
165 desc_p->dmamac_cntl = 0;
166 desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
167#else
168 desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
169 desc_p->txrx_status = 0;
170#endif
171 }
172
173 /* Correcting the last pointer of the chain */
0e1a3e30 174 desc_p->dmamac_next = (ulong)&desc_table_p[0];
5b1b1883 175
50b0df81 176 /* Flush all Tx buffer descriptors at once */
0e1a3e30
BG
177 flush_dcache_range((ulong)priv->tx_mac_descrtable,
178 (ulong)priv->tx_mac_descrtable +
50b0df81
AB
179 sizeof(priv->tx_mac_descrtable));
180
5b1b1883 181 writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
74cb708d 182 priv->tx_currdescnum = 0;
5b1b1883
VK
183}
184
64dcd25f 185static void rx_descs_init(struct dw_eth_dev *priv)
5b1b1883 186{
5b1b1883
VK
187 struct eth_dma_regs *dma_p = priv->dma_regs_p;
188 struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
189 char *rxbuffs = &priv->rxbuffs[0];
190 struct dmamacdescr *desc_p;
191 u32 idx;
192
50b0df81
AB
193 /* Before passing buffers to GMAC we need to make sure zeros
194 * written there right after "priv" structure allocation were
195 * flushed into RAM.
196 * Otherwise there's a chance to get some of them flushed in RAM when
197 * GMAC is already pushing data to RAM via DMA. This way incoming from
198 * GMAC data will be corrupted. */
0e1a3e30 199 flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
50b0df81 200
5b1b1883
VK
201 for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
202 desc_p = &desc_table_p[idx];
0e1a3e30
BG
203 desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CONFIG_ETH_BUFSIZE];
204 desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
5b1b1883
VK
205
206 desc_p->dmamac_cntl =
2b261092 207 (MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
5b1b1883
VK
208 DESC_RXCTRL_RXCHAIN;
209
210 desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
211 }
212
213 /* Correcting the last pointer of the chain */
0e1a3e30 214 desc_p->dmamac_next = (ulong)&desc_table_p[0];
5b1b1883 215
50b0df81 216 /* Flush all Rx buffer descriptors at once */
0e1a3e30
BG
217 flush_dcache_range((ulong)priv->rx_mac_descrtable,
218 (ulong)priv->rx_mac_descrtable +
50b0df81
AB
219 sizeof(priv->rx_mac_descrtable));
220
5b1b1883 221 writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
74cb708d 222 priv->rx_currdescnum = 0;
5b1b1883
VK
223}
224
64dcd25f 225static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
5b1b1883 226{
92a190aa
AB
227 struct eth_mac_regs *mac_p = priv->mac_regs_p;
228 u32 macid_lo, macid_hi;
92a190aa
AB
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, &mac_p->macaddr0hi);
235 writel(macid_lo, &mac_p->macaddr0lo);
236
237 return 0;
5b1b1883
VK
238}
239
0ea38db9
SG
240static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
241 struct phy_device *phydev)
5b1b1883 242{
92a190aa 243 u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
5b1b1883 244
92a190aa
AB
245 if (!phydev->link) {
246 printf("%s: No link.\n", phydev->dev->name);
0ea38db9 247 return 0;
92a190aa 248 }
5b1b1883 249
92a190aa
AB
250 if (phydev->speed != 1000)
251 conf |= MII_PORTSELECT;
b884c3fe
AB
252 else
253 conf &= ~MII_PORTSELECT;
7091915a 254
92a190aa
AB
255 if (phydev->speed == 100)
256 conf |= FES_100;
5b1b1883 257
92a190aa
AB
258 if (phydev->duplex)
259 conf |= FULLDPLXMODE;
cafabe19 260
92a190aa 261 writel(conf, &mac_p->conf);
5b1b1883 262
92a190aa
AB
263 printf("Speed: %d, %s duplex%s\n", phydev->speed,
264 (phydev->duplex) ? "full" : "half",
265 (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
0ea38db9
SG
266
267 return 0;
5b1b1883
VK
268}
269
64dcd25f 270static void _dw_eth_halt(struct dw_eth_dev *priv)
5b1b1883 271{
5b1b1883 272 struct eth_mac_regs *mac_p = priv->mac_regs_p;
92a190aa 273 struct eth_dma_regs *dma_p = priv->dma_regs_p;
5b1b1883 274
92a190aa
AB
275 writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
276 writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
5b1b1883 277
92a190aa 278 phy_shutdown(priv->phydev);
5b1b1883
VK
279}
280
e72ced23 281int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
5b1b1883 282{
5b1b1883
VK
283 struct eth_mac_regs *mac_p = priv->mac_regs_p;
284 struct eth_dma_regs *dma_p = priv->dma_regs_p;
92a190aa 285 unsigned int start;
64dcd25f 286 int ret;
5b1b1883 287
92a190aa 288 writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
13edd170 289
c6122194
QS
290 /*
291 * When a MII PHY is used, we must set the PS bit for the DMA
292 * reset to succeed.
293 */
294 if (priv->phydev->interface == PHY_INTERFACE_MODE_MII)
295 writel(readl(&mac_p->conf) | MII_PORTSELECT, &mac_p->conf);
296 else
297 writel(readl(&mac_p->conf) & ~MII_PORTSELECT, &mac_p->conf);
298
92a190aa
AB
299 start = get_timer(0);
300 while (readl(&dma_p->busmode) & DMAMAC_SRST) {
875143f3
AB
301 if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
302 printf("DMA reset timeout\n");
64dcd25f 303 return -ETIMEDOUT;
875143f3 304 }
ef76025a 305
92a190aa
AB
306 mdelay(100);
307 };
5b1b1883 308
f3edfd30
BM
309 /*
310 * Soft reset above clears HW address registers.
311 * So we have to set it here once again.
312 */
313 _dw_write_hwaddr(priv, enetaddr);
314
64dcd25f
SG
315 rx_descs_init(priv);
316 tx_descs_init(priv);
5b1b1883 317
49692c5f 318 writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
5b1b1883 319
d2279221 320#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
92a190aa
AB
321 writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
322 &dma_p->opmode);
d2279221
SZ
323#else
324 writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
325 &dma_p->opmode);
326#endif
5b1b1883 327
92a190aa 328 writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
9afc1af0 329
2ddaf13b
SZ
330#ifdef CONFIG_DW_AXI_BURST_LEN
331 writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
332#endif
333
92a190aa 334 /* Start up the PHY */
64dcd25f
SG
335 ret = phy_startup(priv->phydev);
336 if (ret) {
92a190aa
AB
337 printf("Could not initialize PHY %s\n",
338 priv->phydev->dev->name);
64dcd25f 339 return ret;
9afc1af0
VK
340 }
341
0ea38db9
SG
342 ret = dw_adjust_link(priv, mac_p, priv->phydev);
343 if (ret)
344 return ret;
5b1b1883 345
f63f28ee
SG
346 return 0;
347}
348
e72ced23 349int designware_eth_enable(struct dw_eth_dev *priv)
f63f28ee
SG
350{
351 struct eth_mac_regs *mac_p = priv->mac_regs_p;
352
92a190aa 353 if (!priv->phydev->link)
64dcd25f 354 return -EIO;
5b1b1883 355
aa51005c 356 writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
5b1b1883
VK
357
358 return 0;
359}
360
7a9ca9db
FF
361#define ETH_ZLEN 60
362
64dcd25f 363static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
5b1b1883 364{
5b1b1883
VK
365 struct eth_dma_regs *dma_p = priv->dma_regs_p;
366 u32 desc_num = priv->tx_currdescnum;
367 struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
0e1a3e30
BG
368 ulong desc_start = (ulong)desc_p;
369 ulong desc_end = desc_start +
96cec17d 370 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
0e1a3e30
BG
371 ulong data_start = desc_p->dmamac_addr;
372 ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
964ea7c1
IC
373 /*
374 * Strictly we only need to invalidate the "txrx_status" field
375 * for the following check, but on some platforms we cannot
96cec17d
MV
376 * invalidate only 4 bytes, so we flush the entire descriptor,
377 * which is 16 bytes in total. This is safe because the
378 * individual descriptors in the array are each aligned to
379 * ARCH_DMA_MINALIGN and padded appropriately.
964ea7c1 380 */
96cec17d 381 invalidate_dcache_range(desc_start, desc_end);
50b0df81 382
5b1b1883
VK
383 /* Check if the descriptor is owned by CPU */
384 if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
385 printf("CPU not owner of tx frame\n");
64dcd25f 386 return -EPERM;
5b1b1883
VK
387 }
388
0e1a3e30 389 memcpy((void *)data_start, packet, length);
7efb75b1
SG
390 if (length < ETH_ZLEN) {
391 memset(&((char *)data_start)[length], 0, ETH_ZLEN - length);
392 length = ETH_ZLEN;
393 }
5b1b1883 394
50b0df81 395 /* Flush data to be sent */
96cec17d 396 flush_dcache_range(data_start, data_end);
50b0df81 397
5b1b1883
VK
398#if defined(CONFIG_DW_ALTDESCRIPTOR)
399 desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
ae8ac8d4
SG
400 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
401 ((length << DESC_TXCTRL_SIZE1SHFT) &
402 DESC_TXCTRL_SIZE1MASK);
5b1b1883
VK
403
404 desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
405 desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
406#else
ae8ac8d4
SG
407 desc_p->dmamac_cntl = (desc_p->dmamac_cntl & ~DESC_TXCTRL_SIZE1MASK) |
408 ((length << DESC_TXCTRL_SIZE1SHFT) &
409 DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
410 DESC_TXCTRL_TXFIRST;
5b1b1883
VK
411
412 desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
413#endif
414
50b0df81 415 /* Flush modified buffer descriptor */
96cec17d 416 flush_dcache_range(desc_start, desc_end);
50b0df81 417
5b1b1883
VK
418 /* Test the wrap-around condition. */
419 if (++desc_num >= CONFIG_TX_DESCR_NUM)
420 desc_num = 0;
421
422 priv->tx_currdescnum = desc_num;
423
424 /* Start the transmission */
425 writel(POLL_DATA, &dma_p->txpolldemand);
426
427 return 0;
428}
429
75577ba4 430static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
5b1b1883 431{
50b0df81 432 u32 status, desc_num = priv->rx_currdescnum;
5b1b1883 433 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
75577ba4 434 int length = -EAGAIN;
0e1a3e30
BG
435 ulong desc_start = (ulong)desc_p;
436 ulong desc_end = desc_start +
96cec17d 437 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
0e1a3e30
BG
438 ulong data_start = desc_p->dmamac_addr;
439 ulong data_end;
5b1b1883 440
50b0df81 441 /* Invalidate entire buffer descriptor */
96cec17d 442 invalidate_dcache_range(desc_start, desc_end);
50b0df81
AB
443
444 status = desc_p->txrx_status;
445
5b1b1883
VK
446 /* Check if the owner is the CPU */
447 if (!(status & DESC_RXSTS_OWNBYDMA)) {
448
2b261092 449 length = (status & DESC_RXSTS_FRMLENMSK) >>
5b1b1883
VK
450 DESC_RXSTS_FRMLENSHFT;
451
50b0df81 452 /* Invalidate received data */
96cec17d
MV
453 data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
454 invalidate_dcache_range(data_start, data_end);
0e1a3e30 455 *packetp = (uchar *)(ulong)desc_p->dmamac_addr;
75577ba4 456 }
50b0df81 457
75577ba4
SG
458 return length;
459}
5b1b1883 460
75577ba4
SG
461static int _dw_free_pkt(struct dw_eth_dev *priv)
462{
463 u32 desc_num = priv->rx_currdescnum;
464 struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
0e1a3e30
BG
465 ulong desc_start = (ulong)desc_p;
466 ulong desc_end = desc_start +
75577ba4 467 roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
5b1b1883 468
75577ba4
SG
469 /*
470 * Make the current descriptor valid again and go to
471 * the next one
472 */
473 desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
50b0df81 474
75577ba4
SG
475 /* Flush only status field - others weren't changed */
476 flush_dcache_range(desc_start, desc_end);
5b1b1883 477
75577ba4
SG
478 /* Test the wrap-around condition. */
479 if (++desc_num >= CONFIG_RX_DESCR_NUM)
480 desc_num = 0;
5b1b1883
VK
481 priv->rx_currdescnum = desc_num;
482
75577ba4 483 return 0;
5b1b1883
VK
484}
485
64dcd25f 486static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
5b1b1883 487{
92a190aa 488 struct phy_device *phydev;
5dce9df0 489 int phy_addr = -1, ret;
cafabe19 490
92a190aa 491#ifdef CONFIG_PHY_ADDR
5dce9df0 492 phy_addr = CONFIG_PHY_ADDR;
5b1b1883
VK
493#endif
494
5dce9df0 495 phydev = phy_connect(priv->bus, phy_addr, dev, priv->interface);
92a190aa 496 if (!phydev)
64dcd25f 497 return -ENODEV;
5b1b1883 498
92a190aa 499 phydev->supported &= PHY_GBIT_FEATURES;
6968ec92
AB
500 if (priv->max_speed) {
501 ret = phy_set_supported(phydev, priv->max_speed);
502 if (ret)
503 return ret;
504 }
92a190aa 505 phydev->advertising = phydev->supported;
5b1b1883 506
92a190aa
AB
507 priv->phydev = phydev;
508 phy_config(phydev);
ef76025a 509
64dcd25f
SG
510 return 0;
511}
512
75577ba4 513#ifndef CONFIG_DM_ETH
64dcd25f
SG
514static int dw_eth_init(struct eth_device *dev, bd_t *bis)
515{
f63f28ee
SG
516 int ret;
517
e72ced23 518 ret = designware_eth_init(dev->priv, dev->enetaddr);
f63f28ee
SG
519 if (!ret)
520 ret = designware_eth_enable(dev->priv);
521
522 return ret;
64dcd25f
SG
523}
524
525static int dw_eth_send(struct eth_device *dev, void *packet, int length)
526{
527 return _dw_eth_send(dev->priv, packet, length);
528}
529
530static int dw_eth_recv(struct eth_device *dev)
531{
75577ba4
SG
532 uchar *packet;
533 int length;
534
535 length = _dw_eth_recv(dev->priv, &packet);
536 if (length == -EAGAIN)
537 return 0;
538 net_process_received_packet(packet, length);
539
540 _dw_free_pkt(dev->priv);
541
542 return 0;
64dcd25f
SG
543}
544
545static void dw_eth_halt(struct eth_device *dev)
546{
547 return _dw_eth_halt(dev->priv);
548}
549
550static int dw_write_hwaddr(struct eth_device *dev)
551{
552 return _dw_write_hwaddr(dev->priv, dev->enetaddr);
5b1b1883 553}
5b1b1883 554
92a190aa 555int designware_initialize(ulong base_addr, u32 interface)
5b1b1883
VK
556{
557 struct eth_device *dev;
558 struct dw_eth_dev *priv;
559
560 dev = (struct eth_device *) malloc(sizeof(struct eth_device));
561 if (!dev)
562 return -ENOMEM;
563
564 /*
565 * Since the priv structure contains the descriptors which need a strict
566 * buswidth alignment, memalign is used to allocate memory
567 */
1c848a25
IC
568 priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
569 sizeof(struct dw_eth_dev));
5b1b1883
VK
570 if (!priv) {
571 free(dev);
572 return -ENOMEM;
573 }
574
0e1a3e30
BG
575 if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
576 printf("designware: buffers are outside DMA memory\n");
577 return -EINVAL;
578 }
579
5b1b1883
VK
580 memset(dev, 0, sizeof(struct eth_device));
581 memset(priv, 0, sizeof(struct dw_eth_dev));
582
92a190aa 583 sprintf(dev->name, "dwmac.%lx", base_addr);
5b1b1883
VK
584 dev->iobase = (int)base_addr;
585 dev->priv = priv;
586
5b1b1883
VK
587 priv->dev = dev;
588 priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
589 priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
590 DW_DMA_BASE_OFFSET);
5b1b1883 591
5b1b1883
VK
592 dev->init = dw_eth_init;
593 dev->send = dw_eth_send;
594 dev->recv = dw_eth_recv;
595 dev->halt = dw_eth_halt;
596 dev->write_hwaddr = dw_write_hwaddr;
597
598 eth_register(dev);
599
92a190aa
AB
600 priv->interface = interface;
601
602 dw_mdio_init(dev->name, priv->mac_regs_p);
603 priv->bus = miiphy_get_dev_by_name(dev->name);
604
64dcd25f 605 return dw_phy_init(priv, dev);
5b1b1883 606}
75577ba4
SG
607#endif
608
609#ifdef CONFIG_DM_ETH
610static int designware_eth_start(struct udevice *dev)
611{
612 struct eth_pdata *pdata = dev_get_platdata(dev);
f63f28ee
SG
613 struct dw_eth_dev *priv = dev_get_priv(dev);
614 int ret;
75577ba4 615
e72ced23 616 ret = designware_eth_init(priv, pdata->enetaddr);
f63f28ee
SG
617 if (ret)
618 return ret;
619 ret = designware_eth_enable(priv);
620 if (ret)
621 return ret;
622
623 return 0;
75577ba4
SG
624}
625
e72ced23 626int designware_eth_send(struct udevice *dev, void *packet, int length)
75577ba4
SG
627{
628 struct dw_eth_dev *priv = dev_get_priv(dev);
629
630 return _dw_eth_send(priv, packet, length);
631}
632
e72ced23 633int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
75577ba4
SG
634{
635 struct dw_eth_dev *priv = dev_get_priv(dev);
636
637 return _dw_eth_recv(priv, packetp);
638}
639
e72ced23 640int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
75577ba4
SG
641{
642 struct dw_eth_dev *priv = dev_get_priv(dev);
643
644 return _dw_free_pkt(priv);
645}
646
e72ced23 647void designware_eth_stop(struct udevice *dev)
75577ba4
SG
648{
649 struct dw_eth_dev *priv = dev_get_priv(dev);
650
651 return _dw_eth_halt(priv);
652}
653
e72ced23 654int designware_eth_write_hwaddr(struct udevice *dev)
75577ba4
SG
655{
656 struct eth_pdata *pdata = dev_get_platdata(dev);
657 struct dw_eth_dev *priv = dev_get_priv(dev);
658
659 return _dw_write_hwaddr(priv, pdata->enetaddr);
660}
661
8b7ee66c
BM
662static int designware_eth_bind(struct udevice *dev)
663{
664#ifdef CONFIG_DM_PCI
665 static int num_cards;
666 char name[20];
667
668 /* Create a unique device name for PCI type devices */
669 if (device_is_on_pci_bus(dev)) {
670 sprintf(name, "eth_designware#%u", num_cards++);
671 device_set_name(dev, name);
672 }
673#endif
674
675 return 0;
676}
677
b9e08d0e 678int designware_eth_probe(struct udevice *dev)
75577ba4
SG
679{
680 struct eth_pdata *pdata = dev_get_platdata(dev);
681 struct dw_eth_dev *priv = dev_get_priv(dev);
f0dc73c0 682 u32 iobase = pdata->iobase;
0e1a3e30 683 ulong ioaddr;
4ee587e2 684 int ret, err;
495c70f9 685 struct reset_ctl_bulk reset_bulk;
ba1f9667 686#ifdef CONFIG_CLK
4ee587e2 687 int i, clock_nb;
ba1f9667
PC
688
689 priv->clock_count = 0;
690 clock_nb = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
691 if (clock_nb > 0) {
692 priv->clocks = devm_kcalloc(dev, clock_nb, sizeof(struct clk),
693 GFP_KERNEL);
694 if (!priv->clocks)
695 return -ENOMEM;
696
697 for (i = 0; i < clock_nb; i++) {
698 err = clk_get_by_index(dev, i, &priv->clocks[i]);
699 if (err < 0)
700 break;
701
702 err = clk_enable(&priv->clocks[i]);
1693a577 703 if (err && err != -ENOSYS && err != -ENOTSUPP) {
ba1f9667
PC
704 pr_err("failed to enable clock %d\n", i);
705 clk_free(&priv->clocks[i]);
706 goto clk_err;
707 }
708 priv->clock_count++;
709 }
710 } else if (clock_nb != -ENOENT) {
711 pr_err("failed to get clock phandle(%d)\n", clock_nb);
712 return clock_nb;
713 }
714#endif
75577ba4 715
6ec922fa
JC
716#if defined(CONFIG_DM_REGULATOR)
717 struct udevice *phy_supply;
718
719 ret = device_get_supply_regulator(dev, "phy-supply",
720 &phy_supply);
721 if (ret) {
722 debug("%s: No phy supply\n", dev->name);
723 } else {
724 ret = regulator_set_enable(phy_supply, true);
725 if (ret) {
726 puts("Error enabling phy supply\n");
727 return ret;
728 }
729 }
730#endif
731
495c70f9
LFT
732 ret = reset_get_bulk(dev, &reset_bulk);
733 if (ret)
734 dev_warn(dev, "Can't get reset: %d\n", ret);
735 else
736 reset_deassert_bulk(&reset_bulk);
737
8b7ee66c
BM
738#ifdef CONFIG_DM_PCI
739 /*
740 * If we are on PCI bus, either directly attached to a PCI root port,
741 * or via a PCI bridge, fill in platdata before we probe the hardware.
742 */
743 if (device_is_on_pci_bus(dev)) {
8b7ee66c
BM
744 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
745 iobase &= PCI_BASE_ADDRESS_MEM_MASK;
6758a6cc 746 iobase = dm_pci_mem_to_phys(dev, iobase);
8b7ee66c
BM
747
748 pdata->iobase = iobase;
749 pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
750 }
751#endif
752
f0dc73c0 753 debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
0e1a3e30
BG
754 ioaddr = iobase;
755 priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
756 priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
75577ba4 757 priv->interface = pdata->phy_interface;
6968ec92 758 priv->max_speed = pdata->max_speed;
75577ba4 759
4ee587e2
SG
760 ret = dw_mdio_init(dev->name, dev);
761 if (ret) {
762 err = ret;
763 goto mdio_err;
764 }
75577ba4
SG
765 priv->bus = miiphy_get_dev_by_name(dev->name);
766
767 ret = dw_phy_init(priv, dev);
768 debug("%s, ret=%d\n", __func__, ret);
4ee587e2
SG
769 if (!ret)
770 return 0;
75577ba4 771
4ee587e2
SG
772 /* continue here for cleanup if no PHY found */
773 err = ret;
774 mdio_unregister(priv->bus);
775 mdio_free(priv->bus);
776mdio_err:
ba1f9667
PC
777
778#ifdef CONFIG_CLK
779clk_err:
780 ret = clk_release_all(priv->clocks, priv->clock_count);
781 if (ret)
782 pr_err("failed to disable all clocks\n");
783
ba1f9667 784#endif
4ee587e2 785 return err;
75577ba4
SG
786}
787
5d2459fd
BM
788static int designware_eth_remove(struct udevice *dev)
789{
790 struct dw_eth_dev *priv = dev_get_priv(dev);
791
792 free(priv->phydev);
793 mdio_unregister(priv->bus);
794 mdio_free(priv->bus);
795
ba1f9667
PC
796#ifdef CONFIG_CLK
797 return clk_release_all(priv->clocks, priv->clock_count);
798#else
5d2459fd 799 return 0;
ba1f9667 800#endif
5d2459fd
BM
801}
802
b9e08d0e 803const struct eth_ops designware_eth_ops = {
75577ba4
SG
804 .start = designware_eth_start,
805 .send = designware_eth_send,
806 .recv = designware_eth_recv,
807 .free_pkt = designware_eth_free_pkt,
808 .stop = designware_eth_stop,
809 .write_hwaddr = designware_eth_write_hwaddr,
810};
811
b9e08d0e 812int designware_eth_ofdata_to_platdata(struct udevice *dev)
75577ba4 813{
90b7fc92 814 struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
bcee8d67 815#if CONFIG_IS_ENABLED(DM_GPIO)
90b7fc92 816 struct dw_eth_dev *priv = dev_get_priv(dev);
66d027e2 817#endif
90b7fc92 818 struct eth_pdata *pdata = &dw_pdata->eth_pdata;
75577ba4 819 const char *phy_mode;
bcee8d67 820#if CONFIG_IS_ENABLED(DM_GPIO)
90b7fc92 821 int reset_flags = GPIOD_IS_OUT;
66d027e2 822#endif
90b7fc92 823 int ret = 0;
75577ba4 824
15050f1c 825 pdata->iobase = dev_read_addr(dev);
75577ba4 826 pdata->phy_interface = -1;
15050f1c 827 phy_mode = dev_read_string(dev, "phy-mode");
75577ba4
SG
828 if (phy_mode)
829 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
830 if (pdata->phy_interface == -1) {
831 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
832 return -EINVAL;
833 }
834
15050f1c 835 pdata->max_speed = dev_read_u32_default(dev, "max-speed", 0);
6968ec92 836
bcee8d67 837#if CONFIG_IS_ENABLED(DM_GPIO)
7ad326a9 838 if (dev_read_bool(dev, "snps,reset-active-low"))
90b7fc92
SS
839 reset_flags |= GPIOD_ACTIVE_LOW;
840
841 ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
842 &priv->reset_gpio, reset_flags);
843 if (ret == 0) {
7ad326a9
PT
844 ret = dev_read_u32_array(dev, "snps,reset-delays-us",
845 dw_pdata->reset_delays, 3);
90b7fc92
SS
846 } else if (ret == -ENOENT) {
847 ret = 0;
848 }
66d027e2 849#endif
90b7fc92
SS
850
851 return ret;
75577ba4
SG
852}
853
854static const struct udevice_id designware_eth_ids[] = {
855 { .compatible = "allwinner,sun7i-a20-gmac" },
cfe25561 856 { .compatible = "amlogic,meson6-dwmac" },
655217d9 857 { .compatible = "amlogic,meson-gx-dwmac" },
ec353ad1 858 { .compatible = "amlogic,meson-gxbb-dwmac" },
71a38a8e 859 { .compatible = "amlogic,meson-axg-dwmac" },
b20b70fc 860 { .compatible = "st,stm32-dwmac" },
2a723237 861 { .compatible = "snps,arc-dwmac-3.70a" },
75577ba4
SG
862 { }
863};
864
9f76f105 865U_BOOT_DRIVER(eth_designware) = {
75577ba4
SG
866 .name = "eth_designware",
867 .id = UCLASS_ETH,
868 .of_match = designware_eth_ids,
869 .ofdata_to_platdata = designware_eth_ofdata_to_platdata,
8b7ee66c 870 .bind = designware_eth_bind,
75577ba4 871 .probe = designware_eth_probe,
5d2459fd 872 .remove = designware_eth_remove,
75577ba4
SG
873 .ops = &designware_eth_ops,
874 .priv_auto_alloc_size = sizeof(struct dw_eth_dev),
90b7fc92 875 .platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
75577ba4
SG
876 .flags = DM_FLAG_ALLOC_PRIV_DMA,
877};
8b7ee66c
BM
878
879static struct pci_device_id supported[] = {
880 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
881 { }
882};
883
884U_BOOT_PCI_DEVICE(eth_designware, supported);
75577ba4 885#endif
This page took 0.595254 seconds and 4 git commands to generate.