davinci_emac.c 22.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Ethernet driver for TI TMS320DM644x (DaVinci) chips.
 *
 * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
 *
 * Parts shamelessly stolen from TI's dm644x_emac.c. Original copyright
 * follows:
 *
 * ----------------------------------------------------------------------------
 *
 * dm644x_emac.c
 *
 * TI DaVinci (DM644X) EMAC peripheral driver source for DV-EVM
 *
 * Copyright (C) 2005 Texas Instruments.
 *
 * ----------------------------------------------------------------------------
 *
19
 * SPDX-License-Identifier:	GPL-2.0+
20 21 22 23 24 25 26 27 28
 *
 * Modifications:
 * ver. 1.0: Sep 2005, Anant Gole - Created EMAC version for uBoot.
 * ver  1.1: Nov 2005, Anant Gole - Extended the RX logic for multiple descriptors
 */
#include <common.h>
#include <command.h>
#include <net.h>
#include <miiphy.h>
29
#include <malloc.h>
J
Jeroen Hofstee 已提交
30
#include <netdev.h>
31
#include <linux/compiler.h>
32
#include <asm/arch/emac_defs.h>
33
#include <asm/io.h>
34
#include "davinci_emac.h"
35 36 37 38

unsigned int	emac_dbg = 0;
#define debug_emac(fmt,args...)	if (emac_dbg) printf(fmt,##args)

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#ifdef EMAC_HW_RAM_ADDR
static inline unsigned long BD_TO_HW(unsigned long x)
{
	if (x == 0)
		return 0;

	return x - EMAC_WRAPPER_RAM_ADDR + EMAC_HW_RAM_ADDR;
}

static inline unsigned long HW_TO_BD(unsigned long x)
{
	if (x == 0)
		return 0;

	return x - EMAC_HW_RAM_ADDR + EMAC_WRAPPER_RAM_ADDR;
}
#else
#define BD_TO_HW(x)	(x)
#define HW_TO_BD(x)	(x)
#endif

60
#ifdef DAVINCI_EMAC_GIG_ENABLE
61
#define emac_gigabit_enable(phy_addr)	davinci_eth_gigabit_enable(phy_addr)
62
#else
63
#define emac_gigabit_enable(phy_addr)	/* no gigabit to enable */
64 65
#endif

66 67 68 69 70
#if !defined(CONFIG_SYS_EMAC_TI_CLKDIV)
#define CONFIG_SYS_EMAC_TI_CLKDIV	((EMAC_MDIO_BUS_FREQ / \
		EMAC_MDIO_CLOCK_FREQ) - 1)
#endif

71
static void davinci_eth_mdio_enable(void);
72 73 74 75 76 77 78 79

static int gen_init_phy(int phy_addr);
static int gen_is_phy_connected(int phy_addr);
static int gen_get_link_speed(int phy_addr);
static int gen_auto_negotiate(int phy_addr);

void eth_mdio_enable(void)
{
80
	davinci_eth_mdio_enable();
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
}

/* EMAC Addresses */
static volatile emac_regs	*adap_emac = (emac_regs *)EMAC_BASE_ADDR;
static volatile ewrap_regs	*adap_ewrap = (ewrap_regs *)EMAC_WRAPPER_BASE_ADDR;
static volatile mdio_regs	*adap_mdio = (mdio_regs *)EMAC_MDIO_BASE_ADDR;

/* EMAC descriptors */
static volatile emac_desc	*emac_rx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE);
static volatile emac_desc	*emac_tx_desc = (emac_desc *)(EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);
static volatile emac_desc	*emac_rx_active_head = 0;
static volatile emac_desc	*emac_rx_active_tail = 0;
static int			emac_rx_queue_active = 0;

/* Receive packet buffers */
96 97
static unsigned char emac_rx_buffers[EMAC_MAX_RX_BUFFERS * EMAC_RXBUF_SIZE]
				__aligned(ARCH_DMA_MINALIGN);
98

99 100 101
#ifndef CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT
#define CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT	3
#endif
102

103
/* PHY address for a discovered PHY (0xff - not found) */
104
static u_int8_t	active_phy_addr[CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT];
105 106 107

/* number of PHY found active */
static u_int8_t	num_phy;
108

109
phy_t				phy[CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT];
110

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static inline void davinci_flush_rx_descs(void)
{
	/* flush the whole RX descs area */
	flush_dcache_range(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE,
			EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);
}

static inline void davinci_invalidate_rx_descs(void)
{
	/* invalidate the whole RX descs area */
	invalidate_dcache_range(EMAC_WRAPPER_RAM_ADDR + EMAC_RX_DESC_BASE,
			EMAC_WRAPPER_RAM_ADDR + EMAC_TX_DESC_BASE);
}

static inline void davinci_flush_desc(emac_desc *desc)
{
	flush_dcache_range((unsigned long)desc,
			(unsigned long)desc + sizeof(*desc));
}

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static int davinci_eth_set_mac_addr(struct eth_device *dev)
{
	unsigned long		mac_hi;
	unsigned long		mac_lo;

	/*
	 * Set MAC Addresses & Init multicast Hash to 0 (disable any multicast
	 * receive)
	 *  Using channel 0 only - other channels are disabled
	 *  */
	writel(0, &adap_emac->MACINDEX);
	mac_hi = (dev->enetaddr[3] << 24) |
		 (dev->enetaddr[2] << 16) |
		 (dev->enetaddr[1] << 8)  |
		 (dev->enetaddr[0]);
	mac_lo = (dev->enetaddr[5] << 8) |
		 (dev->enetaddr[4]);

	writel(mac_hi, &adap_emac->MACADDRHI);
#if defined(DAVINCI_EMAC_VERSION2)
	writel(mac_lo | EMAC_MAC_ADDR_IS_VALID | EMAC_MAC_ADDR_MATCH,
	       &adap_emac->MACADDRLO);
#else
	writel(mac_lo, &adap_emac->MACADDRLO);
#endif

	writel(0, &adap_emac->MACHASH1);
	writel(0, &adap_emac->MACHASH2);

	/* Set source MAC address - REQUIRED */
	writel(mac_hi, &adap_emac->MACSRCADDRHI);
	writel(mac_lo, &adap_emac->MACSRCADDRLO);


	return 0;
}

168
static void davinci_eth_mdio_enable(void)
169 170 171
{
	u_int32_t	clkdiv;

172
	clkdiv = CONFIG_SYS_EMAC_TI_CLKDIV;
173

174 175 176 177 178
	writel((clkdiv & 0xff) |
	       MDIO_CONTROL_ENABLE |
	       MDIO_CONTROL_FAULT |
	       MDIO_CONTROL_FAULT_ENABLE,
	       &adap_mdio->CONTROL);
179

180 181
	while (readl(&adap_mdio->CONTROL) & MDIO_CONTROL_IDLE)
		;
182 183 184 185 186 187 188
}

/*
 * Tries to find an active connected PHY. Returns 1 if address if found.
 * If no active PHY (or more than one PHY) found returns 0.
 * Sets active_phy_addr variable.
 */
189
static int davinci_eth_phy_detect(void)
190 191 192
{
	u_int32_t	phy_act_state;
	int		i;
193 194 195
	int		j;
	unsigned int	count = 0;

196 197
	for (i = 0; i < CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT; i++)
		active_phy_addr[i] = 0xff;
198

199 200
	udelay(1000);
	phy_act_state = readl(&adap_mdio->ALIVE);
201

202
	if (phy_act_state == 0)
203
		return 0;		/* No active PHYs */
204

205
	debug_emac("davinci_eth_phy_detect(), ALIVE = 0x%08x\n", phy_act_state);
206

207
	for (i = 0, j = 0; i < 32; i++)
208
		if (phy_act_state & (1 << i)) {
209
			count++;
210
			if (count <= CONFIG_SYS_DAVINCI_EMAC_PHY_COUNT) {
211 212 213 214 215 216 217
				active_phy_addr[j++] = i;
			} else {
				printf("%s: to many PHYs detected.\n",
					__func__);
				count = 0;
				break;
			}
218 219
		}

220 221 222
	num_phy = count;

	return count;
223 224 225 226
}


/* Read a PHY register via MDIO inteface. Returns 1 on success, 0 otherwise */
227
int davinci_eth_phy_read(u_int8_t phy_addr, u_int8_t reg_num, u_int16_t *data)
228 229 230
{
	int	tmp;

231 232
	while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
		;
233

234 235 236 237 238
	writel(MDIO_USERACCESS0_GO |
	       MDIO_USERACCESS0_WRITE_READ |
	       ((reg_num & 0x1f) << 21) |
	       ((phy_addr & 0x1f) << 16),
	       &adap_mdio->USERACCESS0);
239 240

	/* Wait for command to complete */
241 242
	while ((tmp = readl(&adap_mdio->USERACCESS0)) & MDIO_USERACCESS0_GO)
		;
243 244 245 246 247 248 249 250 251 252 253

	if (tmp & MDIO_USERACCESS0_ACK) {
		*data = tmp & 0xffff;
		return(1);
	}

	*data = -1;
	return(0);
}

/* Write to a PHY register via MDIO inteface. Blocks until operation is complete. */
254
int davinci_eth_phy_write(u_int8_t phy_addr, u_int8_t reg_num, u_int16_t data)
255 256
{

257 258
	while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
		;
259

260 261 262 263 264 265
	writel(MDIO_USERACCESS0_GO |
	       MDIO_USERACCESS0_WRITE_WRITE |
	       ((reg_num & 0x1f) << 21) |
	       ((phy_addr & 0x1f) << 16) |
	       (data & 0xffff),
	       &adap_mdio->USERACCESS0);
266 267

	/* Wait for command to complete */
268 269
	while (readl(&adap_mdio->USERACCESS0) & MDIO_USERACCESS0_GO)
		;
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290

	return(1);
}

/* PHY functions for a generic PHY */
static int gen_init_phy(int phy_addr)
{
	int	ret = 1;

	if (gen_get_link_speed(phy_addr)) {
		/* Try another time */
		ret = gen_get_link_speed(phy_addr);
	}

	return(ret);
}

static int gen_is_phy_connected(int phy_addr)
{
	u_int16_t	dummy;

291 292 293 294 295 296 297 298 299 300 301 302
	return davinci_eth_phy_read(phy_addr, MII_PHYSID1, &dummy);
}

static int get_active_phy(void)
{
	int i;

	for (i = 0; i < num_phy; i++)
		if (phy[i].get_link_speed(active_phy_addr[i]))
			return i;

	return -1;	/* Return error if no link */
303 304 305 306 307 308
}

static int gen_get_link_speed(int phy_addr)
{
	u_int16_t	tmp;

309 310 311 312
	if (davinci_eth_phy_read(phy_addr, MII_STATUS_REG, &tmp) &&
			(tmp & 0x04)) {
#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
		defined(CONFIG_MACH_DAVINCI_DA850_EVM)
B
Ben Gardiner 已提交
313
		davinci_eth_phy_read(phy_addr, MII_LPA, &tmp);
314 315

		/* Speed doesn't matter, there is no setting for it in EMAC. */
B
Ben Gardiner 已提交
316
		if (tmp & (LPA_100FULL | LPA_10FULL)) {
317 318 319 320 321 322 323 324 325 326
			/* set EMAC for Full Duplex  */
			writel(EMAC_MACCONTROL_MIIEN_ENABLE |
					EMAC_MACCONTROL_FULLDUPLEX_ENABLE,
					&adap_emac->MACCONTROL);
		} else {
			/*set EMAC for Half Duplex  */
			writel(EMAC_MACCONTROL_MIIEN_ENABLE,
					&adap_emac->MACCONTROL);
		}

B
Ben Gardiner 已提交
327
		if (tmp & (LPA_100FULL | LPA_100HALF))
328 329 330 331 332 333 334 335
			writel(readl(&adap_emac->MACCONTROL) |
					EMAC_MACCONTROL_RMIISPEED_100,
					 &adap_emac->MACCONTROL);
		else
			writel(readl(&adap_emac->MACCONTROL) &
					~EMAC_MACCONTROL_RMIISPEED_100,
					 &adap_emac->MACCONTROL);
#endif
336
		return(1);
337
	}
338 339 340 341 342 343 344

	return(0);
}

static int gen_auto_negotiate(int phy_addr)
{
	u_int16_t	tmp;
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
	u_int16_t	val;
	unsigned long	cntr = 0;

	if (!davinci_eth_phy_read(phy_addr, MII_BMCR, &tmp))
		return 0;

	val = tmp | BMCR_FULLDPLX | BMCR_ANENABLE |
						BMCR_SPEED100;
	davinci_eth_phy_write(phy_addr, MII_BMCR, val);

	if (!davinci_eth_phy_read(phy_addr, MII_ADVERTISE, &val))
		return 0;

	val |= (ADVERTISE_100FULL | ADVERTISE_100HALF | ADVERTISE_10FULL |
							ADVERTISE_10HALF);
	davinci_eth_phy_write(phy_addr, MII_ADVERTISE, val);
361

M
Mike Frysinger 已提交
362
	if (!davinci_eth_phy_read(phy_addr, MII_BMCR, &tmp))
363 364 365
		return(0);

	/* Restart Auto_negotiation  */
366
	tmp |= BMCR_ANRESTART;
M
Mike Frysinger 已提交
367
	davinci_eth_phy_write(phy_addr, MII_BMCR, tmp);
368 369

	/*check AutoNegotiate complete */
370 371 372 373 374 375 376 377 378 379 380
	do {
		udelay(40000);
		if (!davinci_eth_phy_read(phy_addr, MII_BMSR, &tmp))
			return 0;

		if (tmp & BMSR_ANEGCOMPLETE)
			break;

		cntr++;
	} while (cntr < 200);

M
Mike Frysinger 已提交
381
	if (!davinci_eth_phy_read(phy_addr, MII_BMSR, &tmp))
382 383
		return(0);

M
Mike Frysinger 已提交
384
	if (!(tmp & BMSR_ANEGCOMPLETE))
385 386 387 388 389 390 391
		return(0);

	return(gen_get_link_speed(phy_addr));
}
/* End of generic PHY functions */


392
#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
M
Mike Frysinger 已提交
393
static int davinci_mii_phy_read(const char *devname, unsigned char addr, unsigned char reg, unsigned short *value)
394
{
395
	return(davinci_eth_phy_read(addr, reg, value) ? 0 : 1);
396 397
}

M
Mike Frysinger 已提交
398
static int davinci_mii_phy_write(const char *devname, unsigned char addr, unsigned char reg, unsigned short value)
399
{
400
	return(davinci_eth_phy_write(addr, reg, value) ? 0 : 1);
401 402 403
}
#endif

404
static void  __attribute__((unused)) davinci_eth_gigabit_enable(int phy_addr)
405 406 407
{
	u_int16_t data;

408
	if (davinci_eth_phy_read(phy_addr, 0, &data)) {
409 410 411 412 413
		if (data & (1 << 6)) { /* speed selection MSB */
			/*
			 * Check if link detected is giga-bit
			 * If Gigabit mode detected, enable gigbit in MAC
			 */
414 415 416 417
			writel(readl(&adap_emac->MACCONTROL) |
				EMAC_MACCONTROL_GIGFORCE |
				EMAC_MACCONTROL_GIGABIT_ENABLE,
				&adap_emac->MACCONTROL);
418 419 420
		}
	}
}
421 422

/* Eth device open */
423
static int davinci_eth_open(struct eth_device *dev, bd_t *bis)
424 425 426 427
{
	dv_reg_p		addr;
	u_int32_t		clkdiv, cnt;
	volatile emac_desc	*rx_desc;
428
	int			index;
429 430 431 432

	debug_emac("+ emac_open\n");

	/* Reset EMAC module and disable interrupts in wrapper */
433 434 435 436 437 438 439 440 441
	writel(1, &adap_emac->SOFTRESET);
	while (readl(&adap_emac->SOFTRESET) != 0)
		;
#if defined(DAVINCI_EMAC_VERSION2)
	writel(1, &adap_ewrap->softrst);
	while (readl(&adap_ewrap->softrst) != 0)
		;
#else
	writel(0, &adap_ewrap->EWCTL);
442
	for (cnt = 0; cnt < 5; cnt++) {
443
		clkdiv = readl(&adap_ewrap->EWCTL);
444
	}
445
#endif
446

447 448 449 450 451 452
#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
	defined(CONFIG_MACH_DAVINCI_DA850_EVM)
	adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
	adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
	adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
#endif
453 454
	rx_desc = emac_rx_desc;

455 456
	writel(1, &adap_emac->TXCONTROL);
	writel(1, &adap_emac->RXCONTROL);
457

458
	davinci_eth_set_mac_addr(dev);
459 460 461 462

	/* Set DMA 8 TX / 8 RX Head pointers to 0 */
	addr = &adap_emac->TX0HDP;
	for(cnt = 0; cnt < 16; cnt++)
463
		writel(0, addr++);
464 465 466

	addr = &adap_emac->RX0HDP;
	for(cnt = 0; cnt < 16; cnt++)
467
		writel(0, addr++);
468 469 470 471

	/* Clear Statistics (do this before setting MacControl register) */
	addr = &adap_emac->RXGOODFRAMES;
	for(cnt = 0; cnt < EMAC_NUM_STATS; cnt++)
472
		writel(0, addr++);
473 474

	/* No multicast addressing */
475 476
	writel(0, &adap_emac->MACHASH1);
	writel(0, &adap_emac->MACHASH2);
477 478 479 480

	/* Create RX queue and set receive process in place */
	emac_rx_active_head = emac_rx_desc;
	for (cnt = 0; cnt < EMAC_MAX_RX_BUFFERS; cnt++) {
481
		rx_desc->next = BD_TO_HW((u_int32_t)(rx_desc + 1));
482
		rx_desc->buffer = &emac_rx_buffers[cnt * EMAC_RXBUF_SIZE];
483 484 485 486 487
		rx_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
		rx_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
		rx_desc++;
	}

488
	/* Finalize the rx desc list */
489 490 491 492 493
	rx_desc--;
	rx_desc->next = 0;
	emac_rx_active_tail = rx_desc;
	emac_rx_queue_active = 1;

494 495
	davinci_flush_rx_descs();

496
	/* Enable TX/RX */
497 498
	writel(EMAC_MAX_ETHERNET_PKT_SIZE, &adap_emac->RXMAXLEN);
	writel(0, &adap_emac->RXBUFFEROFFSET);
499

500 501 502 503 504
	/*
	 * No fancy configs - Use this for promiscous debug
	 *   - EMAC_RXMBPENABLE_RXCAFEN_ENABLE
	 */
	writel(EMAC_RXMBPENABLE_RXBROADEN, &adap_emac->RXMBPENABLE);
505 506

	/* Enable ch 0 only */
507
	writel(1, &adap_emac->RXUNICASTSET);
508 509

	/* Enable MII interface and Full duplex mode */
510 511
#if defined(CONFIG_SOC_DA8XX) || \
	(defined(CONFIG_OMAP34XX) && defined(CONFIG_DRIVER_TI_EMAC_USE_RMII))
512 513 514 515 516 517 518 519 520
	writel((EMAC_MACCONTROL_MIIEN_ENABLE |
		EMAC_MACCONTROL_FULLDUPLEX_ENABLE |
		EMAC_MACCONTROL_RMIISPEED_100),
	       &adap_emac->MACCONTROL);
#else
	writel((EMAC_MACCONTROL_MIIEN_ENABLE |
		EMAC_MACCONTROL_FULLDUPLEX_ENABLE),
	       &adap_emac->MACCONTROL);
#endif
521 522

	/* Init MDIO & get link state */
523
	clkdiv = CONFIG_SYS_EMAC_TI_CLKDIV;
524 525 526 527 528
	writel((clkdiv & 0xff) | MDIO_CONTROL_ENABLE | MDIO_CONTROL_FAULT,
	       &adap_mdio->CONTROL);

	/* We need to wait for MDIO to start */
	udelay(1000);
529

530 531
	index = get_active_phy();
	if (index == -1)
532 533
		return(0);

534
	emac_gigabit_enable(active_phy_addr[index]);
535

536
	/* Start receive process */
537
	writel(BD_TO_HW((u_int32_t)emac_rx_desc), &adap_emac->RX0HDP);
538 539 540 541 542 543 544

	debug_emac("- emac_open\n");

	return(1);
}

/* EMAC Channel Teardown */
545
static void davinci_eth_ch_teardown(int ch)
546 547 548 549 550 551 552 553
{
	dv_reg		dly = 0xff;
	dv_reg		cnt;

	debug_emac("+ emac_ch_teardown\n");

	if (ch == EMAC_CH_TX) {
		/* Init TX channel teardown */
554
		writel(0, &adap_emac->TXTEARDOWN);
555 556 557 558 559 560 561 562 563 564 565
		do {
			/*
			 * Wait here for Tx teardown completion interrupt to
			 * occur. Note: A task delay can be called here to pend
			 * rather than occupying CPU cycles - anyway it has
			 * been found that teardown takes very few cpu cycles
			 * and does not affect functionality
			 */
			dly--;
			udelay(1);
			if (dly == 0)
W
Wolfgang Denk 已提交
566
				break;
567 568 569 570
			cnt = readl(&adap_emac->TX0CP);
		} while (cnt != 0xfffffffc);
		writel(cnt, &adap_emac->TX0CP);
		writel(0, &adap_emac->TX0HDP);
571 572
	} else {
		/* Init RX channel teardown */
573
		writel(0, &adap_emac->RXTEARDOWN);
574 575 576 577 578 579 580 581 582 583 584
		do {
			/*
			 * Wait here for Rx teardown completion interrupt to
			 * occur. Note: A task delay can be called here to pend
			 * rather than occupying CPU cycles - anyway it has
			 * been found that teardown takes very few cpu cycles
			 * and does not affect functionality
			 */
			dly--;
			udelay(1);
			if (dly == 0)
W
Wolfgang Denk 已提交
585
				break;
586 587 588 589
			cnt = readl(&adap_emac->RX0CP);
		} while (cnt != 0xfffffffc);
		writel(cnt, &adap_emac->RX0CP);
		writel(0, &adap_emac->RX0HDP);
590 591 592 593 594 595
	}

	debug_emac("- emac_ch_teardown\n");
}

/* Eth device close */
596
static void davinci_eth_close(struct eth_device *dev)
597 598 599
{
	debug_emac("+ emac_close\n");

600
	davinci_eth_ch_teardown(EMAC_CH_TX);	/* TX Channel teardown */
601 602
	if (readl(&adap_emac->RXCONTROL) & 1)
		davinci_eth_ch_teardown(EMAC_CH_RX); /* RX Channel teardown */
603 604

	/* Reset EMAC module and disable interrupts in wrapper */
605 606 607 608 609 610
	writel(1, &adap_emac->SOFTRESET);
#if defined(DAVINCI_EMAC_VERSION2)
	writel(1, &adap_ewrap->softrst);
#else
	writel(0, &adap_ewrap->EWCTL);
#endif
611

612 613 614 615 616 617
#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
	defined(CONFIG_MACH_DAVINCI_DA850_EVM)
	adap_ewrap->c0rxen = adap_ewrap->c1rxen = adap_ewrap->c2rxen = 0;
	adap_ewrap->c0txen = adap_ewrap->c1txen = adap_ewrap->c2txen = 0;
	adap_ewrap->c0miscen = adap_ewrap->c1miscen = adap_ewrap->c2miscen = 0;
#endif
618 619 620 621 622 623 624 625 626
	debug_emac("- emac_close\n");
}

static int tx_send_loop = 0;

/*
 * This function sends a single packet on the network and returns
 * positive number (number of bytes transmitted) or negative for error
 */
627
static int davinci_eth_send_packet (struct eth_device *dev,
628
					void *packet, int length)
629 630
{
	int ret_status = -1;
631
	int index;
632 633
	tx_send_loop = 0;

634 635 636
	index = get_active_phy();
	if (index == -1) {
		printf(" WARN: emac_send_packet: No link\n");
637 638 639
		return (ret_status);
	}

640
	emac_gigabit_enable(active_phy_addr[index]);
641

642
	/* Check packet size and if < EMAC_MIN_ETHERNET_PKT_SIZE, pad it up */
W
Wolfgang Denk 已提交
643
	if (length < EMAC_MIN_ETHERNET_PKT_SIZE) {
644 645 646 647
		length = EMAC_MIN_ETHERNET_PKT_SIZE;
	}

	/* Populate the TX descriptor */
W
Wolfgang Denk 已提交
648 649
	emac_tx_desc->next = 0;
	emac_tx_desc->buffer = (u_int8_t *) packet;
650 651
	emac_tx_desc->buff_off_len = (length & 0xffff);
	emac_tx_desc->pkt_flag_len = ((length & 0xffff) |
W
Wolfgang Denk 已提交
652 653 654
				      EMAC_CPPI_SOP_BIT |
				      EMAC_CPPI_OWNERSHIP_BIT |
				      EMAC_CPPI_EOP_BIT);
655 656 657 658 659

	flush_dcache_range((unsigned long)packet,
			(unsigned long)packet + length);
	davinci_flush_desc(emac_tx_desc);

660
	/* Send the packet */
661
	writel(BD_TO_HW((unsigned long)emac_tx_desc), &adap_emac->TX0HDP);
662 663 664

	/* Wait for packet to complete or link down */
	while (1) {
665
		if (!phy[index].get_link_speed(active_phy_addr[index])) {
666
			davinci_eth_ch_teardown (EMAC_CH_TX);
W
Wolfgang Denk 已提交
667 668
			return (ret_status);
		}
669

670
		emac_gigabit_enable(active_phy_addr[index]);
671 672

		if (readl(&adap_emac->TXINTSTATRAW) & 0x01) {
W
Wolfgang Denk 已提交
673 674
			ret_status = length;
			break;
675
		}
W
Wolfgang Denk 已提交
676
		tx_send_loop++;
677 678
	}

W
Wolfgang Denk 已提交
679
	return (ret_status);
680 681 682 683 684
}

/*
 * This function handles receipt of a packet from the network
 */
685
static int davinci_eth_rcv_packet (struct eth_device *dev)
686
{
W
Wolfgang Denk 已提交
687 688 689 690
	volatile emac_desc *rx_curr_desc;
	volatile emac_desc *curr_desc;
	volatile emac_desc *tail_desc;
	int status, ret = -1;
691

692 693
	davinci_invalidate_rx_descs();

694 695 696
	rx_curr_desc = emac_rx_active_head;
	status = rx_curr_desc->pkt_flag_len;
	if ((rx_curr_desc) && ((status & EMAC_CPPI_OWNERSHIP_BIT) == 0)) {
W
Wolfgang Denk 已提交
697 698 699
		if (status & EMAC_CPPI_RX_ERROR_FRAME) {
			/* Error in packet - discard it and requeue desc */
			printf ("WARN: emac_rcv_pkt: Error in packet\n");
700
		} else {
701 702 703
			unsigned long tmp = (unsigned long)rx_curr_desc->buffer;

			invalidate_dcache_range(tmp, tmp + EMAC_RXBUF_SIZE);
704 705 706
			net_process_received_packet(
				rx_curr_desc->buffer,
				rx_curr_desc->buff_off_len & 0xffff);
707
			ret = rx_curr_desc->buff_off_len & 0xffff;
W
Wolfgang Denk 已提交
708
		}
709

W
Wolfgang Denk 已提交
710
		/* Ack received packet descriptor */
711
		writel(BD_TO_HW((ulong)rx_curr_desc), &adap_emac->RX0CP);
W
Wolfgang Denk 已提交
712 713
		curr_desc = rx_curr_desc;
		emac_rx_active_head =
714
			(volatile emac_desc *) (HW_TO_BD(rx_curr_desc->next));
715

W
Wolfgang Denk 已提交
716 717
		if (status & EMAC_CPPI_EOQ_BIT) {
			if (emac_rx_active_head) {
718
				writel(BD_TO_HW((ulong)emac_rx_active_head),
719
				       &adap_emac->RX0HDP);
720 721
			} else {
				emac_rx_queue_active = 0;
W
Wolfgang Denk 已提交
722
				printf ("INFO:emac_rcv_packet: RX Queue not active\n");
723 724 725 726 727 728 729
			}
		}

		/* Recycle RX descriptor */
		rx_curr_desc->buff_off_len = EMAC_MAX_ETHERNET_PKT_SIZE;
		rx_curr_desc->pkt_flag_len = EMAC_CPPI_OWNERSHIP_BIT;
		rx_curr_desc->next = 0;
730
		davinci_flush_desc(rx_curr_desc);
731 732

		if (emac_rx_active_head == 0) {
W
Wolfgang Denk 已提交
733
			printf ("INFO: emac_rcv_pkt: active queue head = 0\n");
734 735 736
			emac_rx_active_head = curr_desc;
			emac_rx_active_tail = curr_desc;
			if (emac_rx_queue_active != 0) {
737
				writel(BD_TO_HW((ulong)emac_rx_active_head),
738
				       &adap_emac->RX0HDP);
W
Wolfgang Denk 已提交
739
				printf ("INFO: emac_rcv_pkt: active queue head = 0, HDP fired\n");
740 741 742 743 744
				emac_rx_queue_active = 1;
			}
		} else {
			tail_desc = emac_rx_active_tail;
			emac_rx_active_tail = curr_desc;
745
			tail_desc->next = BD_TO_HW((ulong) curr_desc);
746 747
			status = tail_desc->pkt_flag_len;
			if (status & EMAC_CPPI_EOQ_BIT) {
748
				davinci_flush_desc(tail_desc);
749
				writel(BD_TO_HW((ulong)curr_desc),
750
				       &adap_emac->RX0HDP);
751 752 753
				status &= ~EMAC_CPPI_EOQ_BIT;
				tail_desc->pkt_flag_len = status;
			}
754
			davinci_flush_desc(tail_desc);
755
		}
W
Wolfgang Denk 已提交
756
		return (ret);
757
	}
W
Wolfgang Denk 已提交
758
	return (0);
759 760
}

761 762 763 764 765
/*
 * This function initializes the emac hardware. It does NOT initialize
 * EMAC modules power or pin multiplexors, that is done by board_init()
 * much earlier in bootup process. Returns 1 on success, 0 otherwise.
 */
766
int davinci_emac_initialize(void)
767 768 769 770
{
	u_int32_t	phy_id;
	u_int16_t	tmp;
	int		i;
771
	int		ret;
772 773 774 775 776 777 778 779
	struct eth_device *dev;

	dev = malloc(sizeof *dev);

	if (dev == NULL)
		return -1;

	memset(dev, 0, sizeof *dev);
780
	sprintf(dev->name, "DaVinci-EMAC");
781 782 783 784 785 786

	dev->iobase = 0;
	dev->init = davinci_eth_open;
	dev->halt = davinci_eth_close;
	dev->send = davinci_eth_send_packet;
	dev->recv = davinci_eth_rcv_packet;
787
	dev->write_hwaddr = davinci_eth_set_mac_addr;
788 789

	eth_register(dev);
790 791 792

	davinci_eth_mdio_enable();

793 794 795
	/* let the EMAC detect the PHYs */
	udelay(5000);

796
	for (i = 0; i < 256; i++) {
797
		if (readl(&adap_mdio->ALIVE))
798
			break;
799
		udelay(1000);
800 801 802 803 804 805 806
	}

	if (i >= 256) {
		printf("No ETH PHY detected!!!\n");
		return(0);
	}

807 808 809
	/* Find if PHY(s) is/are connected */
	ret = davinci_eth_phy_detect();
	if (!ret)
810
		return(0);
811
	else
812
		debug_emac(" %d ETH PHY detected\n", ret);
813 814

	/* Get PHY ID and initialize phy_ops for a detected PHY */
815 816 817 818 819 820
	for (i = 0; i < num_phy; i++) {
		if (!davinci_eth_phy_read(active_phy_addr[i], MII_PHYSID1,
							&tmp)) {
			active_phy_addr[i] = 0xff;
			continue;
		}
821

822
		phy_id = (tmp << 16) & 0xffff0000;
823

824 825 826 827 828
		if (!davinci_eth_phy_read(active_phy_addr[i], MII_PHYSID2,
							&tmp)) {
			active_phy_addr[i] = 0xff;
			continue;
		}
829

830
		phy_id |= tmp & 0x0000ffff;
831

832
		switch (phy_id) {
833
#ifdef PHY_KSZ8873
834 835 836 837 838 839 840 841
		case PHY_KSZ8873:
			sprintf(phy[i].name, "KSZ8873 @ 0x%02x",
						active_phy_addr[i]);
			phy[i].init = ksz8873_init_phy;
			phy[i].is_phy_connected = ksz8873_is_phy_connected;
			phy[i].get_link_speed = ksz8873_get_link_speed;
			phy[i].auto_negotiate = ksz8873_auto_negotiate;
			break;
842 843
#endif
#ifdef PHY_LXT972
844
		case PHY_LXT972:
845 846 847 848 849 850
			sprintf(phy[i].name, "LXT972 @ 0x%02x",
						active_phy_addr[i]);
			phy[i].init = lxt972_init_phy;
			phy[i].is_phy_connected = lxt972_is_phy_connected;
			phy[i].get_link_speed = lxt972_get_link_speed;
			phy[i].auto_negotiate = lxt972_auto_negotiate;
851
			break;
852 853
#endif
#ifdef PHY_DP83848
854
		case PHY_DP83848:
855 856 857 858 859 860
			sprintf(phy[i].name, "DP83848 @ 0x%02x",
						active_phy_addr[i]);
			phy[i].init = dp83848_init_phy;
			phy[i].is_phy_connected = dp83848_is_phy_connected;
			phy[i].get_link_speed = dp83848_get_link_speed;
			phy[i].auto_negotiate = dp83848_auto_negotiate;
861
			break;
862 863
#endif
#ifdef PHY_ET1011C
864
		case PHY_ET1011C:
865 866 867 868 869 870
			sprintf(phy[i].name, "ET1011C @ 0x%02x",
						active_phy_addr[i]);
			phy[i].init = gen_init_phy;
			phy[i].is_phy_connected = gen_is_phy_connected;
			phy[i].get_link_speed = et1011c_get_link_speed;
			phy[i].auto_negotiate = gen_auto_negotiate;
871
			break;
872
#endif
873
		default:
874 875 876 877 878 879 880
			sprintf(phy[i].name, "GENERIC @ 0x%02x",
						active_phy_addr[i]);
			phy[i].init = gen_init_phy;
			phy[i].is_phy_connected = gen_is_phy_connected;
			phy[i].get_link_speed = gen_get_link_speed;
			phy[i].auto_negotiate = gen_auto_negotiate;
		}
881

882
		debug("Ethernet PHY: %s\n", phy[i].name);
883

884 885 886
		miiphy_register(phy[i].name, davinci_mii_phy_read,
						davinci_mii_phy_write);
	}
887 888

#if defined(CONFIG_DRIVER_TI_EMAC_USE_RMII) && \
889 890
		defined(CONFIG_MACH_DAVINCI_DA850_EVM) && \
			!defined(CONFIG_DRIVER_TI_EMAC_RMII_NO_NEGOTIATE)
891 892 893 894 895
	for (i = 0; i < num_phy; i++) {
		if (phy[i].is_phy_connected(i))
			phy[i].auto_negotiate(i);
	}
#endif
896 897
	return(1);
}