designware.c 19.6 KB
Newer Older
1 2 3 4
/*
 * (C) Copyright 2010
 * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
 *
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 16
#include <miiphy.h>
#include <malloc.h>
17
#include <pci.h>
18
#include <linux/compiler.h>
19 20
#include <linux/err.h>
#include <asm/io.h>
21
#include <power/regulator.h>
22 23
#include "designware.h"

24 25
DECLARE_GLOBAL_DATA_PTR;

26 27
static int dw_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
{
28 29 30 31
#ifdef CONFIG_DM_ETH
	struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
	struct eth_mac_regs *mac_p = priv->mac_regs_p;
#else
32
	struct eth_mac_regs *mac_p = bus->priv;
33
#endif
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
	ulong start;
	u16 miiaddr;
	int timeout = CONFIG_MDIO_TIMEOUT;

	miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
		  ((reg << MIIREGSHIFT) & MII_REGMSK);

	writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);

	start = get_timer(0);
	while (get_timer(start) < timeout) {
		if (!(readl(&mac_p->miiaddr) & MII_BUSY))
			return readl(&mac_p->miidata);
		udelay(10);
	};

50
	return -ETIMEDOUT;
51 52 53 54 55
}

static int dw_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
			u16 val)
{
56 57 58 59
#ifdef CONFIG_DM_ETH
	struct dw_eth_dev *priv = dev_get_priv((struct udevice *)bus->priv);
	struct eth_mac_regs *mac_p = priv->mac_regs_p;
#else
60
	struct eth_mac_regs *mac_p = bus->priv;
61
#endif
62 63
	ulong start;
	u16 miiaddr;
64
	int ret = -ETIMEDOUT, timeout = CONFIG_MDIO_TIMEOUT;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

	writel(val, &mac_p->miidata);
	miiaddr = ((addr << MIIADDRSHIFT) & MII_ADDRMSK) |
		  ((reg << MIIREGSHIFT) & MII_REGMSK) | MII_WRITE;

	writel(miiaddr | MII_CLKRANGE_150_250M | MII_BUSY, &mac_p->miiaddr);

	start = get_timer(0);
	while (get_timer(start) < timeout) {
		if (!(readl(&mac_p->miiaddr) & MII_BUSY)) {
			ret = 0;
			break;
		}
		udelay(10);
	};

	return ret;
}

84
#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
static int dw_mdio_reset(struct mii_dev *bus)
{
	struct udevice *dev = bus->priv;
	struct dw_eth_dev *priv = dev_get_priv(dev);
	struct dw_eth_pdata *pdata = dev_get_platdata(dev);
	int ret;

	if (!dm_gpio_is_valid(&priv->reset_gpio))
		return 0;

	/* reset the phy */
	ret = dm_gpio_set_value(&priv->reset_gpio, 0);
	if (ret)
		return ret;

	udelay(pdata->reset_delays[0]);

	ret = dm_gpio_set_value(&priv->reset_gpio, 1);
	if (ret)
		return ret;

	udelay(pdata->reset_delays[1]);

	ret = dm_gpio_set_value(&priv->reset_gpio, 0);
	if (ret)
		return ret;

	udelay(pdata->reset_delays[2]);

	return 0;
}
#endif

static int dw_mdio_init(const char *name, void *priv)
119 120 121 122 123
{
	struct mii_dev *bus = mdio_alloc();

	if (!bus) {
		printf("Failed to allocate MDIO bus\n");
124
		return -ENOMEM;
125 126 127 128
	}

	bus->read = dw_mdio_read;
	bus->write = dw_mdio_write;
129
	snprintf(bus->name, sizeof(bus->name), "%s", name);
130
#if defined(CONFIG_DM_ETH) && defined(CONFIG_DM_GPIO)
131 132
	bus->reset = dw_mdio_reset;
#endif
133

134
	bus->priv = priv;
135 136 137

	return mdio_register(bus);
}
138

139
static void tx_descs_init(struct dw_eth_dev *priv)
140 141 142 143 144 145 146 147 148
{
	struct eth_dma_regs *dma_p = priv->dma_regs_p;
	struct dmamacdescr *desc_table_p = &priv->tx_mac_descrtable[0];
	char *txbuffs = &priv->txbuffs[0];
	struct dmamacdescr *desc_p;
	u32 idx;

	for (idx = 0; idx < CONFIG_TX_DESCR_NUM; idx++) {
		desc_p = &desc_table_p[idx];
149 150
		desc_p->dmamac_addr = (ulong)&txbuffs[idx * CONFIG_ETH_BUFSIZE];
		desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
151 152 153

#if defined(CONFIG_DW_ALTDESCRIPTOR)
		desc_p->txrx_status &= ~(DESC_TXSTS_TXINT | DESC_TXSTS_TXLAST |
154 155
				DESC_TXSTS_TXFIRST | DESC_TXSTS_TXCRCDIS |
				DESC_TXSTS_TXCHECKINSCTRL |
156 157 158 159 160 161 162 163 164 165 166 167
				DESC_TXSTS_TXRINGEND | DESC_TXSTS_TXPADDIS);

		desc_p->txrx_status |= DESC_TXSTS_TXCHAIN;
		desc_p->dmamac_cntl = 0;
		desc_p->txrx_status &= ~(DESC_TXSTS_MSK | DESC_TXSTS_OWNBYDMA);
#else
		desc_p->dmamac_cntl = DESC_TXCTRL_TXCHAIN;
		desc_p->txrx_status = 0;
#endif
	}

	/* Correcting the last pointer of the chain */
168
	desc_p->dmamac_next = (ulong)&desc_table_p[0];
169

170
	/* Flush all Tx buffer descriptors at once */
171 172
	flush_dcache_range((ulong)priv->tx_mac_descrtable,
			   (ulong)priv->tx_mac_descrtable +
173 174
			   sizeof(priv->tx_mac_descrtable));

175
	writel((ulong)&desc_table_p[0], &dma_p->txdesclistaddr);
176
	priv->tx_currdescnum = 0;
177 178
}

179
static void rx_descs_init(struct dw_eth_dev *priv)
180 181 182 183 184 185 186
{
	struct eth_dma_regs *dma_p = priv->dma_regs_p;
	struct dmamacdescr *desc_table_p = &priv->rx_mac_descrtable[0];
	char *rxbuffs = &priv->rxbuffs[0];
	struct dmamacdescr *desc_p;
	u32 idx;

187 188 189 190 191 192
	/* Before passing buffers to GMAC we need to make sure zeros
	 * written there right after "priv" structure allocation were
	 * flushed into RAM.
	 * Otherwise there's a chance to get some of them flushed in RAM when
	 * GMAC is already pushing data to RAM via DMA. This way incoming from
	 * GMAC data will be corrupted. */
193
	flush_dcache_range((ulong)rxbuffs, (ulong)rxbuffs + RX_TOTAL_BUFSIZE);
194

195 196
	for (idx = 0; idx < CONFIG_RX_DESCR_NUM; idx++) {
		desc_p = &desc_table_p[idx];
197 198
		desc_p->dmamac_addr = (ulong)&rxbuffs[idx * CONFIG_ETH_BUFSIZE];
		desc_p->dmamac_next = (ulong)&desc_table_p[idx + 1];
199 200

		desc_p->dmamac_cntl =
201
			(MAC_MAX_FRAME_SZ & DESC_RXCTRL_SIZE1MASK) |
202 203 204 205 206 207
				      DESC_RXCTRL_RXCHAIN;

		desc_p->txrx_status = DESC_RXSTS_OWNBYDMA;
	}

	/* Correcting the last pointer of the chain */
208
	desc_p->dmamac_next = (ulong)&desc_table_p[0];
209

210
	/* Flush all Rx buffer descriptors at once */
211 212
	flush_dcache_range((ulong)priv->rx_mac_descrtable,
			   (ulong)priv->rx_mac_descrtable +
213 214
			   sizeof(priv->rx_mac_descrtable));

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 222 223 224 225 226 227 228 229 230 231
	struct eth_mac_regs *mac_p = priv->mac_regs_p;
	u32 macid_lo, macid_hi;

	macid_lo = mac_id[0] + (mac_id[1] << 8) + (mac_id[2] << 16) +
		   (mac_id[3] << 24);
	macid_hi = mac_id[4] + (mac_id[5] << 8);

	writel(macid_hi, &mac_p->macaddr0hi);
	writel(macid_lo, &mac_p->macaddr0lo);

	return 0;
232 233
}

234 235
static int dw_adjust_link(struct dw_eth_dev *priv, struct eth_mac_regs *mac_p,
			  struct phy_device *phydev)
236
{
237
	u32 conf = readl(&mac_p->conf) | FRAMEBURSTENABLE | DISABLERXOWN;
238

239 240
	if (!phydev->link) {
		printf("%s: No link.\n", phydev->dev->name);
241
		return 0;
242
	}
243

244 245
	if (phydev->speed != 1000)
		conf |= MII_PORTSELECT;
246 247
	else
		conf &= ~MII_PORTSELECT;
248

249 250
	if (phydev->speed == 100)
		conf |= FES_100;
251

252 253
	if (phydev->duplex)
		conf |= FULLDPLXMODE;
254

255
	writel(conf, &mac_p->conf);
256

257 258 259
	printf("Speed: %d, %s duplex%s\n", phydev->speed,
	       (phydev->duplex) ? "full" : "half",
	       (phydev->port == PORT_FIBRE) ? ", fiber mode" : "");
260 261

	return 0;
262 263
}

264
static void _dw_eth_halt(struct dw_eth_dev *priv)
265 266
{
	struct eth_mac_regs *mac_p = priv->mac_regs_p;
267
	struct eth_dma_regs *dma_p = priv->dma_regs_p;
268

269 270
	writel(readl(&mac_p->conf) & ~(RXENABLE | TXENABLE), &mac_p->conf);
	writel(readl(&dma_p->opmode) & ~(RXSTART | TXSTART), &dma_p->opmode);
271

272
	phy_shutdown(priv->phydev);
273 274
}

275
int designware_eth_init(struct dw_eth_dev *priv, u8 *enetaddr)
276 277 278
{
	struct eth_mac_regs *mac_p = priv->mac_regs_p;
	struct eth_dma_regs *dma_p = priv->dma_regs_p;
279
	unsigned int start;
280
	int ret;
281

282
	writel(readl(&dma_p->busmode) | DMAMAC_SRST, &dma_p->busmode);
283

284 285
	start = get_timer(0);
	while (readl(&dma_p->busmode) & DMAMAC_SRST) {
286 287
		if (get_timer(start) >= CONFIG_MACRESET_TIMEOUT) {
			printf("DMA reset timeout\n");
288
			return -ETIMEDOUT;
289
		}
290

291 292
		mdelay(100);
	};
293

294 295 296 297 298 299
	/*
	 * Soft reset above clears HW address registers.
	 * So we have to set it here once again.
	 */
	_dw_write_hwaddr(priv, enetaddr);

300 301
	rx_descs_init(priv);
	tx_descs_init(priv);
302

303
	writel(FIXEDBURST | PRIORXTX_41 | DMA_PBL, &dma_p->busmode);
304

305
#ifndef CONFIG_DW_MAC_FORCE_THRESHOLD_MODE
306 307
	writel(readl(&dma_p->opmode) | FLUSHTXFIFO | STOREFORWARD,
	       &dma_p->opmode);
308 309 310 311
#else
	writel(readl(&dma_p->opmode) | FLUSHTXFIFO,
	       &dma_p->opmode);
#endif
312

313
	writel(readl(&dma_p->opmode) | RXSTART | TXSTART, &dma_p->opmode);
314

315 316 317 318
#ifdef CONFIG_DW_AXI_BURST_LEN
	writel((CONFIG_DW_AXI_BURST_LEN & 0x1FF >> 1), &dma_p->axibus);
#endif

319
	/* Start up the PHY */
320 321
	ret = phy_startup(priv->phydev);
	if (ret) {
322 323
		printf("Could not initialize PHY %s\n",
		       priv->phydev->dev->name);
324
		return ret;
325 326
	}

327 328 329
	ret = dw_adjust_link(priv, mac_p, priv->phydev);
	if (ret)
		return ret;
330

331 332 333
	return 0;
}

334
int designware_eth_enable(struct dw_eth_dev *priv)
335 336 337
{
	struct eth_mac_regs *mac_p = priv->mac_regs_p;

338
	if (!priv->phydev->link)
339
		return -EIO;
340

341
	writel(readl(&mac_p->conf) | RXENABLE | TXENABLE, &mac_p->conf);
342 343 344 345

	return 0;
}

346
static int _dw_eth_send(struct dw_eth_dev *priv, void *packet, int length)
347 348 349 350
{
	struct eth_dma_regs *dma_p = priv->dma_regs_p;
	u32 desc_num = priv->tx_currdescnum;
	struct dmamacdescr *desc_p = &priv->tx_mac_descrtable[desc_num];
351 352
	ulong desc_start = (ulong)desc_p;
	ulong desc_end = desc_start +
353
		roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
354 355
	ulong data_start = desc_p->dmamac_addr;
	ulong data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
356 357 358
	/*
	 * Strictly we only need to invalidate the "txrx_status" field
	 * for the following check, but on some platforms we cannot
359 360 361 362
	 * invalidate only 4 bytes, so we flush the entire descriptor,
	 * which is 16 bytes in total. This is safe because the
	 * individual descriptors in the array are each aligned to
	 * ARCH_DMA_MINALIGN and padded appropriately.
363
	 */
364
	invalidate_dcache_range(desc_start, desc_end);
365

366 367 368
	/* Check if the descriptor is owned by CPU */
	if (desc_p->txrx_status & DESC_TXSTS_OWNBYDMA) {
		printf("CPU not owner of tx frame\n");
369
		return -EPERM;
370 371
	}

372
	memcpy((void *)data_start, packet, length);
373

374
	/* Flush data to be sent */
375
	flush_dcache_range(data_start, data_end);
376

377 378
#if defined(CONFIG_DW_ALTDESCRIPTOR)
	desc_p->txrx_status |= DESC_TXSTS_TXFIRST | DESC_TXSTS_TXLAST;
379
	desc_p->dmamac_cntl |= (length << DESC_TXCTRL_SIZE1SHFT) &
380 381 382 383 384
			       DESC_TXCTRL_SIZE1MASK;

	desc_p->txrx_status &= ~(DESC_TXSTS_MSK);
	desc_p->txrx_status |= DESC_TXSTS_OWNBYDMA;
#else
385 386
	desc_p->dmamac_cntl |= ((length << DESC_TXCTRL_SIZE1SHFT) &
			       DESC_TXCTRL_SIZE1MASK) | DESC_TXCTRL_TXLAST |
387 388 389 390 391
			       DESC_TXCTRL_TXFIRST;

	desc_p->txrx_status = DESC_TXSTS_OWNBYDMA;
#endif

392
	/* Flush modified buffer descriptor */
393
	flush_dcache_range(desc_start, desc_end);
394

395 396 397 398 399 400 401 402 403 404 405 406
	/* Test the wrap-around condition. */
	if (++desc_num >= CONFIG_TX_DESCR_NUM)
		desc_num = 0;

	priv->tx_currdescnum = desc_num;

	/* Start the transmission */
	writel(POLL_DATA, &dma_p->txpolldemand);

	return 0;
}

407
static int _dw_eth_recv(struct dw_eth_dev *priv, uchar **packetp)
408
{
409
	u32 status, desc_num = priv->rx_currdescnum;
410
	struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
411
	int length = -EAGAIN;
412 413
	ulong desc_start = (ulong)desc_p;
	ulong desc_end = desc_start +
414
		roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
415 416
	ulong data_start = desc_p->dmamac_addr;
	ulong data_end;
417

418
	/* Invalidate entire buffer descriptor */
419
	invalidate_dcache_range(desc_start, desc_end);
420 421 422

	status = desc_p->txrx_status;

423 424 425
	/* Check  if the owner is the CPU */
	if (!(status & DESC_RXSTS_OWNBYDMA)) {

426
		length = (status & DESC_RXSTS_FRMLENMSK) >>
427 428
			 DESC_RXSTS_FRMLENSHFT;

429
		/* Invalidate received data */
430 431
		data_end = data_start + roundup(length, ARCH_DMA_MINALIGN);
		invalidate_dcache_range(data_start, data_end);
432
		*packetp = (uchar *)(ulong)desc_p->dmamac_addr;
433
	}
434

435 436
	return length;
}
437

438 439 440 441
static int _dw_free_pkt(struct dw_eth_dev *priv)
{
	u32 desc_num = priv->rx_currdescnum;
	struct dmamacdescr *desc_p = &priv->rx_mac_descrtable[desc_num];
442 443
	ulong desc_start = (ulong)desc_p;
	ulong desc_end = desc_start +
444
		roundup(sizeof(*desc_p), ARCH_DMA_MINALIGN);
445

446 447 448 449 450
	/*
	 * Make the current descriptor valid again and go to
	 * the next one
	 */
	desc_p->txrx_status |= DESC_RXSTS_OWNBYDMA;
451

452 453
	/* Flush only status field - others weren't changed */
	flush_dcache_range(desc_start, desc_end);
454

455 456 457
	/* Test the wrap-around condition. */
	if (++desc_num >= CONFIG_RX_DESCR_NUM)
		desc_num = 0;
458 459
	priv->rx_currdescnum = desc_num;

460
	return 0;
461 462
}

463
static int dw_phy_init(struct dw_eth_dev *priv, void *dev)
464
{
465
	struct phy_device *phydev;
466
	int mask = 0xffffffff, ret;
467

468 469
#ifdef CONFIG_PHY_ADDR
	mask = 1 << CONFIG_PHY_ADDR;
470 471
#endif

472 473
	phydev = phy_find_by_mask(priv->bus, mask, priv->interface);
	if (!phydev)
474
		return -ENODEV;
475

476 477
	phy_connect_dev(phydev, dev);

478
	phydev->supported &= PHY_GBIT_FEATURES;
479 480 481 482 483
	if (priv->max_speed) {
		ret = phy_set_supported(phydev, priv->max_speed);
		if (ret)
			return ret;
	}
484
	phydev->advertising = phydev->supported;
485

486 487
	priv->phydev = phydev;
	phy_config(phydev);
488

489 490 491
	return 0;
}

492
#ifndef CONFIG_DM_ETH
493 494
static int dw_eth_init(struct eth_device *dev, bd_t *bis)
{
495 496
	int ret;

497
	ret = designware_eth_init(dev->priv, dev->enetaddr);
498 499 500 501
	if (!ret)
		ret = designware_eth_enable(dev->priv);

	return ret;
502 503 504 505 506 507 508 509 510
}

static int dw_eth_send(struct eth_device *dev, void *packet, int length)
{
	return _dw_eth_send(dev->priv, packet, length);
}

static int dw_eth_recv(struct eth_device *dev)
{
511 512 513 514 515 516 517 518 519 520 521
	uchar *packet;
	int length;

	length = _dw_eth_recv(dev->priv, &packet);
	if (length == -EAGAIN)
		return 0;
	net_process_received_packet(packet, length);

	_dw_free_pkt(dev->priv);

	return 0;
522 523 524 525 526 527 528 529 530 531
}

static void dw_eth_halt(struct eth_device *dev)
{
	return _dw_eth_halt(dev->priv);
}

static int dw_write_hwaddr(struct eth_device *dev)
{
	return _dw_write_hwaddr(dev->priv, dev->enetaddr);
532 533
}

534
int designware_initialize(ulong base_addr, u32 interface)
535 536 537 538 539 540 541 542 543 544 545 546
{
	struct eth_device *dev;
	struct dw_eth_dev *priv;

	dev = (struct eth_device *) malloc(sizeof(struct eth_device));
	if (!dev)
		return -ENOMEM;

	/*
	 * Since the priv structure contains the descriptors which need a strict
	 * buswidth alignment, memalign is used to allocate memory
	 */
547 548
	priv = (struct dw_eth_dev *) memalign(ARCH_DMA_MINALIGN,
					      sizeof(struct dw_eth_dev));
549 550 551 552 553
	if (!priv) {
		free(dev);
		return -ENOMEM;
	}

554 555 556 557 558
	if ((phys_addr_t)priv + sizeof(*priv) > (1ULL << 32)) {
		printf("designware: buffers are outside DMA memory\n");
		return -EINVAL;
	}

559 560 561
	memset(dev, 0, sizeof(struct eth_device));
	memset(priv, 0, sizeof(struct dw_eth_dev));

562
	sprintf(dev->name, "dwmac.%lx", base_addr);
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
	dev->iobase = (int)base_addr;
	dev->priv = priv;

	priv->dev = dev;
	priv->mac_regs_p = (struct eth_mac_regs *)base_addr;
	priv->dma_regs_p = (struct eth_dma_regs *)(base_addr +
			DW_DMA_BASE_OFFSET);

	dev->init = dw_eth_init;
	dev->send = dw_eth_send;
	dev->recv = dw_eth_recv;
	dev->halt = dw_eth_halt;
	dev->write_hwaddr = dw_write_hwaddr;

	eth_register(dev);

579 580 581 582 583
	priv->interface = interface;

	dw_mdio_init(dev->name, priv->mac_regs_p);
	priv->bus = miiphy_get_dev_by_name(dev->name);

584
	return dw_phy_init(priv, dev);
585
}
586 587 588 589 590 591
#endif

#ifdef CONFIG_DM_ETH
static int designware_eth_start(struct udevice *dev)
{
	struct eth_pdata *pdata = dev_get_platdata(dev);
592 593
	struct dw_eth_dev *priv = dev_get_priv(dev);
	int ret;
594

595
	ret = designware_eth_init(priv, pdata->enetaddr);
596 597 598 599 600 601 602
	if (ret)
		return ret;
	ret = designware_eth_enable(priv);
	if (ret)
		return ret;

	return 0;
603 604
}

605
int designware_eth_send(struct udevice *dev, void *packet, int length)
606 607 608 609 610 611
{
	struct dw_eth_dev *priv = dev_get_priv(dev);

	return _dw_eth_send(priv, packet, length);
}

612
int designware_eth_recv(struct udevice *dev, int flags, uchar **packetp)
613 614 615 616 617 618
{
	struct dw_eth_dev *priv = dev_get_priv(dev);

	return _dw_eth_recv(priv, packetp);
}

619
int designware_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
620 621 622 623 624 625
{
	struct dw_eth_dev *priv = dev_get_priv(dev);

	return _dw_free_pkt(priv);
}

626
void designware_eth_stop(struct udevice *dev)
627 628 629 630 631 632
{
	struct dw_eth_dev *priv = dev_get_priv(dev);

	return _dw_eth_halt(priv);
}

633
int designware_eth_write_hwaddr(struct udevice *dev)
634 635 636 637 638 639 640
{
	struct eth_pdata *pdata = dev_get_platdata(dev);
	struct dw_eth_dev *priv = dev_get_priv(dev);

	return _dw_write_hwaddr(priv, pdata->enetaddr);
}

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
static int designware_eth_bind(struct udevice *dev)
{
#ifdef CONFIG_DM_PCI
	static int num_cards;
	char name[20];

	/* Create a unique device name for PCI type devices */
	if (device_is_on_pci_bus(dev)) {
		sprintf(name, "eth_designware#%u", num_cards++);
		device_set_name(dev, name);
	}
#endif

	return 0;
}

657
int designware_eth_probe(struct udevice *dev)
658 659 660
{
	struct eth_pdata *pdata = dev_get_platdata(dev);
	struct dw_eth_dev *priv = dev_get_priv(dev);
B
Bin Meng 已提交
661
	u32 iobase = pdata->iobase;
662
	ulong ioaddr;
663 664
	int ret;

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
#if defined(CONFIG_DM_REGULATOR)
	struct udevice *phy_supply;

	ret = device_get_supply_regulator(dev, "phy-supply",
					  &phy_supply);
	if (ret) {
		debug("%s: No phy supply\n", dev->name);
	} else {
		ret = regulator_set_enable(phy_supply, true);
		if (ret) {
			puts("Error enabling phy supply\n");
			return ret;
		}
	}
#endif

681 682 683 684 685 686 687 688
#ifdef CONFIG_DM_PCI
	/*
	 * If we are on PCI bus, either directly attached to a PCI root port,
	 * or via a PCI bridge, fill in platdata before we probe the hardware.
	 */
	if (device_is_on_pci_bus(dev)) {
		dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
		iobase &= PCI_BASE_ADDRESS_MEM_MASK;
689
		iobase = dm_pci_mem_to_phys(dev, iobase);
690 691 692 693 694 695

		pdata->iobase = iobase;
		pdata->phy_interface = PHY_INTERFACE_MODE_RMII;
	}
#endif

B
Bin Meng 已提交
696
	debug("%s, iobase=%x, priv=%p\n", __func__, iobase, priv);
697 698 699
	ioaddr = iobase;
	priv->mac_regs_p = (struct eth_mac_regs *)ioaddr;
	priv->dma_regs_p = (struct eth_dma_regs *)(ioaddr + DW_DMA_BASE_OFFSET);
700
	priv->interface = pdata->phy_interface;
701
	priv->max_speed = pdata->max_speed;
702

703
	dw_mdio_init(dev->name, dev);
704 705 706 707 708 709 710 711
	priv->bus = miiphy_get_dev_by_name(dev->name);

	ret = dw_phy_init(priv, dev);
	debug("%s, ret=%d\n", __func__, ret);

	return ret;
}

712 713 714 715 716 717 718 719 720 721 722
static int designware_eth_remove(struct udevice *dev)
{
	struct dw_eth_dev *priv = dev_get_priv(dev);

	free(priv->phydev);
	mdio_unregister(priv->bus);
	mdio_free(priv->bus);

	return 0;
}

723
const struct eth_ops designware_eth_ops = {
724 725 726 727 728 729 730 731
	.start			= designware_eth_start,
	.send			= designware_eth_send,
	.recv			= designware_eth_recv,
	.free_pkt		= designware_eth_free_pkt,
	.stop			= designware_eth_stop,
	.write_hwaddr		= designware_eth_write_hwaddr,
};

732
int designware_eth_ofdata_to_platdata(struct udevice *dev)
733
{
734
	struct dw_eth_pdata *dw_pdata = dev_get_platdata(dev);
735
#ifdef CONFIG_DM_GPIO
736
	struct dw_eth_dev *priv = dev_get_priv(dev);
737
#endif
738
	struct eth_pdata *pdata = &dw_pdata->eth_pdata;
739
	const char *phy_mode;
740
	const fdt32_t *cell;
741
#ifdef CONFIG_DM_GPIO
742
	int reset_flags = GPIOD_IS_OUT;
743
#endif
744
	int ret = 0;
745

S
Simon Glass 已提交
746
	pdata->iobase = devfdt_get_addr(dev);
747
	pdata->phy_interface = -1;
748 749
	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
			       NULL);
750 751 752 753 754 755 756
	if (phy_mode)
		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
	if (pdata->phy_interface == -1) {
		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
		return -EINVAL;
	}

757
	pdata->max_speed = 0;
758
	cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
759 760 761
	if (cell)
		pdata->max_speed = fdt32_to_cpu(*cell);

762
#ifdef CONFIG_DM_GPIO
763
	if (dev_read_bool(dev, "snps,reset-active-low"))
764 765 766 767 768
		reset_flags |= GPIOD_ACTIVE_LOW;

	ret = gpio_request_by_name(dev, "snps,reset-gpio", 0,
		&priv->reset_gpio, reset_flags);
	if (ret == 0) {
769 770
		ret = dev_read_u32_array(dev, "snps,reset-delays-us",
					 dw_pdata->reset_delays, 3);
771 772 773
	} else if (ret == -ENOENT) {
		ret = 0;
	}
774
#endif
775 776

	return ret;
777 778 779 780
}

static const struct udevice_id designware_eth_ids[] = {
	{ .compatible = "allwinner,sun7i-a20-gmac" },
781
	{ .compatible = "altr,socfpga-stmmac" },
782
	{ .compatible = "amlogic,meson6-dwmac" },
783
	{ .compatible = "amlogic,meson-gx-dwmac" },
784
	{ .compatible = "st,stm32-dwmac" },
785 786 787
	{ }
};

788
U_BOOT_DRIVER(eth_designware) = {
789 790 791 792
	.name	= "eth_designware",
	.id	= UCLASS_ETH,
	.of_match = designware_eth_ids,
	.ofdata_to_platdata = designware_eth_ofdata_to_platdata,
793
	.bind	= designware_eth_bind,
794
	.probe	= designware_eth_probe,
795
	.remove	= designware_eth_remove,
796 797
	.ops	= &designware_eth_ops,
	.priv_auto_alloc_size = sizeof(struct dw_eth_dev),
798
	.platdata_auto_alloc_size = sizeof(struct dw_eth_pdata),
799 800
	.flags = DM_FLAG_ALLOC_PRIV_DMA,
};
801 802 803 804 805 806 807

static struct pci_device_id supported[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_QRK_EMAC) },
	{ }
};

U_BOOT_PCI_DEVICE(eth_designware, supported);
808
#endif