xref: /rk3399_rockchip-uboot/drivers/net/designware.c (revision 90b7fc924adfe7f1745dcf6a1dabb9e77aa762a7)
1 /*
2  * (C) Copyright 2010
3  * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 /*
9  * Designware ethernet IP driver for U-Boot
10  */
11 
12 #include <common.h>
13 #include <dm.h>
14 #include <errno.h>
15 #include <miiphy.h>
16 #include <malloc.h>
17 #include <pci.h>
18 #include <linux/compiler.h>
19 #include <linux/err.h>
20 #include <asm/io.h>
21 #include "designware.h"
22 
23 DECLARE_GLOBAL_DATA_PTR;
24 
25 static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
26 {
27 #ifdef CONFIG_DM_ETH
28 	struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
29 	struct eth_mac_regs *mac_p = priv->mac_regs_p;
30 #else
31 	struct eth_mac_regs *mac_p = bus->priv;
32 #endif
33 	ulong start;
34 	u16 miiaddr;
35 	int timeout = CONFIG_MDIO_TIMEOUT;
36 
37 	miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
38 		  ((reg << MIIREGSHIFT) & MII_REGMSK);
39 
40 	writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
41 
42 	start = get_timer(0);
43 	while (get_timer(start) < timeout) {
44 		if (!(readl(&mac_p->miiaddr) & MII_BUSY))
45 			return readl(&mac_p->miidata);
46 		udelay(10);
47 	};
48 
49 	return -ETIMEDOUT;
50 }
51 
52 static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
53 			u16 val)
54 {
55 #ifdef CONFIG_DM_ETH
56 	struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
57 	struct eth_mac_regs *mac_p = priv->mac_regs_p;
58 #else
59 	struct eth_mac_regs *mac_p = bus->priv;
60 #endif
61 	ulong start;
62 	u16 miiaddr;
63 	int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
64 
65 	writel(val, &mac_p->miidata);
66 	miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
67 		  ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;
68 
69 	writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);
70 
71 	start = get_timer(0);
72 	while (get_timer(start) < timeout) {
73 		if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
74 			ret = 0;
75 			break;
76 		}
77 		udelay(10);
78 	};
79 
80 	return ret;
81 }
82 
83 #if CONFIG_DM_ETH
84 static int dw_mdio_reset(struct mii_dev *bus)
85 {
86 	struct udevice *dev = bus->priv;
87 	struct dw_eth_dev *priv = dev_get_priv(dev);
88 	struct dw_eth_pdata *pdata = dev_get_platdata(dev);
89 	int ret;
90 
91 	if (!dm_gpio_is_valid(&priv->reset_gpio))
92 		return 0;
93 
94 	/* reset the phy */
95 	ret = dm_gpio_set_value(&priv->reset_gpio, 0);
96 	if (ret)
97 		return ret;
98 
99 	udelay(pdata->reset_delays[0]);
100 
101 	ret = dm_gpio_set_value(&priv->reset_gpio, 1);
102 	if (ret)
103 		return ret;
104 
105 	udelay(pdata->reset_delays[1]);
106 
107 	ret = dm_gpio_set_value(&priv->reset_gpio, 0);
108 	if (ret)
109 		return ret;
110 
111 	udelay(pdata->reset_delays[2]);
112 
113 	return 0;
114 }
115 #endif
116 
117 static int dw_mdio_init(const char *name, void *priv)
118 {
119 	struct mii_dev *bus = mdio_alloc();
120 
121 	if (!bus) {
122 		printf("Failed to allocate MDIO bus\n");
123 		return -ENOMEM;
124 	}
125 
126 	bus->read = dw_mdio_read;
127 	bus->write = dw_mdio_write;
128 	snprintf(bus->name, sizeof(bus->name), "%s", name);
129 #ifdef CONFIG_DM_ETH
130 	bus->reset = dw_mdio_reset;
131 #endif
132 
133 	bus->priv = priv;
134 
135 	return mdio_register(bus);
136 }
137 
138 static void tx_descs_init(struct dw_eth_dev *priv)
139 {
140 	struct eth_dma_regs *dma_p = priv->dma_regs_p;
141 	struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
142 	char *txbuffs = &priv->txbuffs[0];
143 	struct dmamacdescr *desc_p;
144 	u32 idx;
145 
146 	for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
147 		desc_p = &desc_table_p[idx];
148 		desc_p->dmamac_addr = &txbuffs[idx * CONFIG_ETH_BUFSIZE];
149 		desc_p->dmamac_next = &desc_table_p[idx + 1];
150 
151 #if defined(CONFIG_DW_ALTDESCRIPTOR)
152 		desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
153 				DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
154 				DESC_TXSTS_TXCHECKINSCTRL |
155 				DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);
156 
157 		desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
158 		desc_p->dmamac_cntl = 0;
159 		desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
160 #else
161 		desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
162 		desc_p->txrx_status = 0;
163 #endif
164 	}
165 
166 	/* Correcting the last pointer of the chain */
167 	desc_p->dmamac_next = &desc_table_p[0];
168 
169 	/* Flush all Tx buffer descriptors at once */
170 	flush_dcache_range((unsigned int)priv->tx_mac_descrtable,
171 			   (unsigned int)priv->tx_mac_descrtable +
172 			   sizeof(priv->tx_mac_descrtable));
173 
174 	writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
175 	priv->tx_currdescnum = 0;
176 }
177 
178 static void rx_descs_init(struct dw_eth_dev *priv)
179 {
180 	struct eth_dma_regs *dma_p = priv->dma_regs_p;
181 	struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
182 	char *rxbuffs = &priv->rxbuffs[0];
183 	struct dmamacdescr *desc_p;
184 	u32 idx;
185 
186 	/* Before passing buffers to GMAC we need to make sure zeros
187 	 * written there right after "priv" structure allocation were
188 	 * flushed into RAM.
189 	 * Otherwise there's a chance to get some of them flushed in RAM when
190 	 * GMAC is already pushing data to RAM via DMA. This way incoming from
191 	 * GMAC data will be corrupted. */
192 	flush_dcache_range((unsigned int)rxbuffs, (unsigned int)rxbuffs +
193 			   RX_TOTAL_BUFSIZE);
194 
195 	for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
196 		desc_p = &desc_table_p[idx];
197 		desc_p->dmamac_addr = &rxbuffs[idx * CONFIG_ETH_BUFSIZE];
198 		desc_p->dmamac_next = &desc_table_p[idx + 1];
199 
200 		desc_p->dmamac_cntl =
201 			(MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
202 				      DESC_RXCTRL_RXCHAIN;
203 
204 		desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
205 	}
206 
207 	/* Correcting the last pointer of the chain */
208 	desc_p->dmamac_next = &desc_table_p[0];
209 
210 	/* Flush all Rx buffer descriptors at once */
211 	flush_dcache_range((unsigned int)priv->rx_mac_descrtable,
212 			   (unsigned int)priv->rx_mac_descrtable +
213 			   sizeof(priv->rx_mac_descrtable));
214 
215 	writel((ulong)&desc_table_p[0], &dma_p->rxdesclistaddr);
216 	priv->rx_currdescnum = 0;
217 }
218 
219 static int _dw_write_hwaddr(struct dw_eth_dev *priv, u8 *mac_id)
220 {
221 	struct eth_mac_regs *mac_p = priv->mac_regs_p;
222 	u32 macid_lo, macid_hi;
223 
224 	macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
225 		   (mac_id[3] << 24);
226 	macid_hi = mac_id[4] + (mac_id[5] << 8);
227 
228 	writel(macid_hi, &mac_p->macaddr0hi);
229 	writel(macid_lo, &mac_p->macaddr0lo);
230 
231 	return 0;
232 }
233 
234 static void dw_adjust_link(struct eth_mac_regs *mac_p,
235 			   struct phy_device *phydev)
236 {
237 	u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
238 
239 	if (!phydev->link) {
240 		printf("%s: No link.\n", phydev->dev->name);
241 		return;
242 	}
243 
244 	if (phydev->speed != 1000)
245 		conf |= MII_PORTSELECT;
246 	else
247 		conf &= ~MII_PORTSELECT;
248 
249 	if (phydev->speed == 100)
250 		conf |= FES_100;
251 
252 	if (phydev->duplex)
253 		conf |= FULLDPLXMODE;
254 
255 	writel(conf, &mac_p->conf);
256 
257 	printf("Speed: %d, %s duplex%s\n", phydev->speed,
258 	       (phydev->duplex) ? "full" : "half",
259 	       (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
260 }
261 
262 static void _dw_eth_halt(struct dw_eth_dev *priv)
263 {
264 	struct eth_mac_regs *mac_p = priv->mac_regs_p;
265 	struct eth_dma_regs *dma_p = priv->dma_regs_p;
266 
267 	writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
268 	writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
269 
270 	phy_shutdown(priv->phydev);
271 }
272 
273 static int _dw_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
274 {
275 	struct eth_mac_regs *mac_p = priv->mac_regs_p;
276 	struct eth_dma_regs *dma_p = priv->dma_regs_p;
277 	unsigned int start;
278 	int ret;
279 
280 	writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
281 
282 	start = get_timer(0);
283 	while (readl(&dma_p->busmode) & DMAMAC_SRST) {
284 		if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
285 			printf("DMA reset timeout\n");
286 			return -ETIMEDOUT;
287 		}
288 
289 		mdelay(100);
290 	};
291 
292 	/*
293 	 * Soft reset above clears HW address registers.
294 	 * So we have to set it here once again.
295 	 */
296 	_dw_write_hwaddr(priv, enetaddr);
297 
298 	rx_descs_init(priv);
299 	tx_descs_init(priv);
300 
301 	writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
302 
303 #ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
304 	writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
305 	       &dma_p->opmode);
306 #else
307 	writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
308 	       &dma_p->opmode);
309 #endif
310 
311 	writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
312 
313 #ifdef CONFIG_DW_AXI_BURST_LEN
314 	writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
315 #endif
316 
317 	/* Start up the PHY */
318 	ret = phy_startup(priv->phydev);
319 	if (ret) {
320 		printf("Could not initialize PHY %s\n",
321 		       priv->phydev->dev->name);
322 		return ret;
323 	}
324 
325 	dw_adjust_link(mac_p, priv->phydev);
326 
327 	if (!priv->phydev->link)
328 		return -EIO;
329 
330 	writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
331 
332 	return 0;
333 }
334 
335 static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
336 {
337 	struct eth_dma_regs *dma_p = priv->dma_regs_p;
338 	u32 desc_num = priv->tx_currdescnum;
339 	struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
340 	uint32_t desc_start = (uint32_t)desc_p;
341 	uint32_t desc_end = desc_start +
342 		roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
343 	uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
344 	uint32_t data_end = data_start +
345 		roundup(length, ARCH_DMA_MINALIGN);
346 	/*
347 	 * Strictly we only need to invalidate the "txrx_status" field
348 	 * for the following check, but on some platforms we cannot
349 	 * invalidate only 4 bytes, so we flush the entire descriptor,
350 	 * which is 16 bytes in total. This is safe because the
351 	 * individual descriptors in the array are each aligned to
352 	 * ARCH_DMA_MINALIGN and padded appropriately.
353 	 */
354 	invalidate_dcache_range(desc_start, desc_end);
355 
356 	/* Check if the descriptor is owned by CPU */
357 	if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
358 		printf("CPU not owner of tx frame\n");
359 		return -EPERM;
360 	}
361 
362 	memcpy(desc_p->dmamac_addr, packet, length);
363 
364 	/* Flush data to be sent */
365 	flush_dcache_range(data_start, data_end);
366 
367 #if defined(CONFIG_DW_ALTDESCRIPTOR)
368 	desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
369 	desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
370 			       DESC_TXCTRL_SIZE1MASK;
371 
372 	desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
373 	desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
374 #else
375 	desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
376 			       DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
377 			       DESC_TXCTRL_TXFIRST;
378 
379 	desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
380 #endif
381 
382 	/* Flush modified buffer descriptor */
383 	flush_dcache_range(desc_start, desc_end);
384 
385 	/* Test the wrap-around condition. */
386 	if (++desc_num >= CONFIG_TX_DESCR_NUM)
387 		desc_num = 0;
388 
389 	priv->tx_currdescnum = desc_num;
390 
391 	/* Start the transmission */
392 	writel(POLL_DATA, &dma_p->txpolldemand);
393 
394 	return 0;
395 }
396 
397 static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
398 {
399 	u32 status, desc_num = priv->rx_currdescnum;
400 	struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
401 	int length = -EAGAIN;
402 	uint32_t desc_start = (uint32_t)desc_p;
403 	uint32_t desc_end = desc_start +
404 		roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
405 	uint32_t data_start = (uint32_t)desc_p->dmamac_addr;
406 	uint32_t data_end;
407 
408 	/* Invalidate entire buffer descriptor */
409 	invalidate_dcache_range(desc_start, desc_end);
410 
411 	status = desc_p->txrx_status;
412 
413 	/* Check  if the owner is the CPU */
414 	if (!(status & DESC_RXSTS_OWNBYDMA)) {
415 
416 		length = (status & DESC_RXSTS_FRMLENMSK) >>
417 			 DESC_RXSTS_FRMLENSHFT;
418 
419 		/* Invalidate received data */
420 		data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
421 		invalidate_dcache_range(data_start, data_end);
422 		*packetp = desc_p->dmamac_addr;
423 	}
424 
425 	return length;
426 }
427 
428 static int _dw_free_pkt(struct dw_eth_dev *priv)
429 {
430 	u32 desc_num = priv->rx_currdescnum;
431 	struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
432 	uint32_t desc_start = (uint32_t)desc_p;
433 	uint32_t desc_end = desc_start +
434 		roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
435 
436 	/*
437 	 * Make the current descriptor valid again and go to
438 	 * the next one
439 	 */
440 	desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
441 
442 	/* Flush only status field - others weren't changed */
443 	flush_dcache_range(desc_start, desc_end);
444 
445 	/* Test the wrap-around condition. */
446 	if (++desc_num >= CONFIG_RX_DESCR_NUM)
447 		desc_num = 0;
448 	priv->rx_currdescnum = desc_num;
449 
450 	return 0;
451 }
452 
453 static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
454 {
455 	struct phy_device *phydev;
456 	int mask = 0xffffffff, ret;
457 
458 #ifdef CONFIG_PHY_ADDR
459 	mask = 1 << CONFIG_PHY_ADDR;
460 #endif
461 
462 	phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
463 	if (!phydev)
464 		return -ENODEV;
465 
466 	phy_connect_dev(phydev, dev);
467 
468 	phydev->supported &= PHY_GBIT_FEATURES;
469 	if (priv->max_speed) {
470 		ret = phy_set_supported(phydev, priv->max_speed);
471 		if (ret)
472 			return ret;
473 	}
474 	phydev->advertising = phydev->supported;
475 
476 	priv->phydev = phydev;
477 	phy_config(phydev);
478 
479 	return 0;
480 }
481 
482 #ifndef CONFIG_DM_ETH
483 static int dw_eth_init(struct eth_device *dev, bd_t *bis)
484 {
485 	return _dw_eth_init(dev->priv, dev->enetaddr);
486 }
487 
488 static int dw_eth_send(struct eth_device *dev, void *packet, int length)
489 {
490 	return _dw_eth_send(dev->priv, packet, length);
491 }
492 
493 static int dw_eth_recv(struct eth_device *dev)
494 {
495 	uchar *packet;
496 	int length;
497 
498 	length = _dw_eth_recv(dev->priv, &packet);
499 	if (length == -EAGAIN)
500 		return 0;
501 	net_process_received_packet(packet, length);
502 
503 	_dw_free_pkt(dev->priv);
504 
505 	return 0;
506 }
507 
508 static void dw_eth_halt(struct eth_device *dev)
509 {
510 	return _dw_eth_halt(dev->priv);
511 }
512 
513 static int dw_write_hwaddr(struct eth_device *dev)
514 {
515 	return _dw_write_hwaddr(dev->priv, dev->enetaddr);
516 }
517 
518 int designware_initialize(ulong base_addr, u32 interface)
519 {
520 	struct eth_device *dev;
521 	struct dw_eth_dev *priv;
522 
523 	dev = (struct eth_device *) malloc(sizeof(struct eth_device));
524 	if (!dev)
525 		return -ENOMEM;
526 
527 	/*
528 	 * Since the priv structure contains the descriptors which need a strict
529 	 * buswidth alignment, memalign is used to allocate memory
530 	 */
531 	priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
532 					      sizeof(struct dw_eth_dev));
533 	if (!priv) {
534 		free(dev);
535 		return -ENOMEM;
536 	}
537 
538 	memset(dev, 0, sizeof(struct eth_device));
539 	memset(priv, 0, sizeof(struct dw_eth_dev));
540 
541 	sprintf(dev->name, "dwmac.%lx", base_addr);
542 	dev->iobase = (int)base_addr;
543 	dev->priv = priv;
544 
545 	priv->dev = dev;
546 	priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
547 	priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
548 			DW_DMA_BASE_OFFSET);
549 
550 	dev->init = dw_eth_init;
551 	dev->send = dw_eth_send;
552 	dev->recv = dw_eth_recv;
553 	dev->halt = dw_eth_halt;
554 	dev->write_hwaddr = dw_write_hwaddr;
555 
556 	eth_register(dev);
557 
558 	priv->interface = interface;
559 
560 	dw_mdio_init(dev->name, priv->mac_regs_p);
561 	priv->bus = miiphy_get_dev_by_name(dev->name);
562 
563 	return dw_phy_init(priv, dev);
564 }
565 #endif
566 
567 #ifdef CONFIG_DM_ETH
568 static int designware_eth_start(struct udevice *dev)
569 {
570 	struct eth_pdata *pdata = dev_get_platdata(dev);
571 
572 	return _dw_eth_init(dev->priv, pdata->enetaddr);
573 }
574 
575 static int designware_eth_send(struct udevice *dev, void *packet, int length)
576 {
577 	struct dw_eth_dev *priv = dev_get_priv(dev);
578 
579 	return _dw_eth_send(priv, packet, length);
580 }
581 
582 static int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
583 {
584 	struct dw_eth_dev *priv = dev_get_priv(dev);
585 
586 	return _dw_eth_recv(priv, packetp);
587 }
588 
589 static int designware_eth_free_pkt(struct udevice *dev, uchar *packet,
590 				   int length)
591 {
592 	struct dw_eth_dev *priv = dev_get_priv(dev);
593 
594 	return _dw_free_pkt(priv);
595 }
596 
597 static void designware_eth_stop(struct udevice *dev)
598 {
599 	struct dw_eth_dev *priv = dev_get_priv(dev);
600 
601 	return _dw_eth_halt(priv);
602 }
603 
604 static int designware_eth_write_hwaddr(struct udevice *dev)
605 {
606 	struct eth_pdata *pdata = dev_get_platdata(dev);
607 	struct dw_eth_dev *priv = dev_get_priv(dev);
608 
609 	return _dw_write_hwaddr(priv, pdata->enetaddr);
610 }
611 
612 static int designware_eth_bind(struct udevice *dev)
613 {
614 #ifdef CONFIG_DM_PCI
615 	static int num_cards;
616 	char name[20];
617 
618 	/* Create a unique device name for PCI type devices */
619 	if (device_is_on_pci_bus(dev)) {
620 		sprintf(name, "eth_designware#%u", num_cards++);
621 		device_set_name(dev, name);
622 	}
623 #endif
624 
625 	return 0;
626 }
627 
628 static int designware_eth_probe(struct udevice *dev)
629 {
630 	struct eth_pdata *pdata = dev_get_platdata(dev);
631 	struct dw_eth_dev *priv = dev_get_priv(dev);
632 	u32 iobase = pdata->iobase;
633 	int ret;
634 
635 #ifdef CONFIG_DM_PCI
636 	/*
637 	 * If we are on PCI bus, either directly attached to a PCI root port,
638 	 * or via a PCI bridge, fill in platdata before we probe the hardware.
639 	 */
640 	if (device_is_on_pci_bus(dev)) {
641 		dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
642 		iobase &= PCI_BASE_ADDRESS_MEM_MASK;
643 		iobase = dm_pci_mem_to_phys(dev, iobase);
644 
645 		pdata->iobase = iobase;
646 		pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
647 	}
648 #endif
649 
650 	debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
651 	priv->mac_regs_p = (struct eth_mac_regs *)iobase;
652 	priv->dma_regs_p = (struct eth_dma_regs *)(iobase + DW_DMA_BASE_OFFSET);
653 	priv->interface = pdata->phy_interface;
654 	priv->max_speed = pdata->max_speed;
655 
656 	dw_mdio_init(dev->name, dev);
657 	priv->bus = miiphy_get_dev_by_name(dev->name);
658 
659 	ret = dw_phy_init(priv, dev);
660 	debug("%s, ret=%d\n", __func__, ret);
661 
662 	return ret;
663 }
664 
665 static int designware_eth_remove(struct udevice *dev)
666 {
667 	struct dw_eth_dev *priv = dev_get_priv(dev);
668 
669 	free(priv->phydev);
670 	mdio_unregister(priv->bus);
671 	mdio_free(priv->bus);
672 
673 	return 0;
674 }
675 
676 static const struct eth_ops designware_eth_ops = {
677 	.start			= designware_eth_start,
678 	.send			= designware_eth_send,
679 	.recv			= designware_eth_recv,
680 	.free_pkt		= designware_eth_free_pkt,
681 	.stop			= designware_eth_stop,
682 	.write_hwaddr		= designware_eth_write_hwaddr,
683 };
684 
685 static int designware_eth_ofdata_to_platdata(struct udevice *dev)
686 {
687 	struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
688 	struct dw_eth_dev *priv = dev_get_priv(dev);
689 	struct eth_pdata *pdata = &dw_pdata->eth_pdata;
690 	const char *phy_mode;
691 	const fdt32_t *cell;
692 	int reset_flags = GPIOD_IS_OUT;
693 	int ret = 0;
694 
695 	pdata->iobase = dev_get_addr(dev);
696 	pdata->phy_interface = -1;
697 	phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
698 	if (phy_mode)
699 		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
700 	if (pdata->phy_interface == -1) {
701 		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
702 		return -EINVAL;
703 	}
704 
705 	pdata->max_speed = 0;
706 	cell = fdt_getprop(gd->fdt_blob, dev->of_offset, "max-speed", NULL);
707 	if (cell)
708 		pdata->max_speed = fdt32_to_cpu(*cell);
709 
710 	if (fdtdec_get_bool(gd->fdt_blob, dev->of_offset,
711 			    "snps,reset-active-low"))
712 		reset_flags |= GPIOD_ACTIVE_LOW;
713 
714 	ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
715 		&priv->reset_gpio, reset_flags);
716 	if (ret == 0) {
717 		ret = fdtdec_get_int_array(gd->fdt_blob, dev->of_offset,
718 			"snps,reset-delays-us", dw_pdata->reset_delays, 3);
719 	} else if (ret == -ENOENT) {
720 		ret = 0;
721 	}
722 
723 	return ret;
724 }
725 
726 static const struct udevice_id designware_eth_ids[] = {
727 	{ .compatible = "allwinner,sun7i-a20-gmac" },
728 	{ .compatible = "altr,socfpga-stmmac" },
729 	{ }
730 };
731 
732 U_BOOT_DRIVER(eth_designware) = {
733 	.name	= "eth_designware",
734 	.id	= UCLASS_ETH,
735 	.of_match = designware_eth_ids,
736 	.ofdata_to_platdata = designware_eth_ofdata_to_platdata,
737 	.bind	= designware_eth_bind,
738 	.probe	= designware_eth_probe,
739 	.remove	= designware_eth_remove,
740 	.ops	= &designware_eth_ops,
741 	.priv_auto_alloc_size = sizeof(struct dw_eth_dev),
742 	.platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
743 	.flags = DM_FLAG_ALLOC_PRIV_DMA,
744 };
745 
746 static struct pci_device_id supported[] = {
747 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
748 	{ }
749 };
750 
751 U_BOOT_PCI_DEVICE(eth_designware, supported);
752 #endif
753