inca-ip_sw.c 19 KB
Newer Older
1 2 3
/*
 * INCA-IP internal switch ethernet driver.
 *
4
 * (C) Copyright 2003-2004
5 6 7 8 9 10 11 12 13 14 15 16
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 19 20 21 22 23 24 25 26 27 28 29 30
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */


#include <common.h>

#include <malloc.h>
#include <net.h>
31
#include <netdev.h>
32 33 34 35 36 37 38 39 40 41
#include <asm/inca-ip.h>
#include <asm/addrspace.h>


#define NUM_RX_DESC	PKTBUFSRX
#define NUM_TX_DESC	3
#define TOUT_LOOP	1000000


#define DELAY	udelay(10000)
wdenk's avatar
wdenk committed
42 43 44 45 46 47 48 49
  /* Sometimes the store word instruction hangs while writing to one
   * of the Switch registers. Moving the instruction into a separate
   * function somehow makes the problem go away.
   */
static void SWORD(volatile u32 * reg, u32 value)
{
	*reg = value;
}
50 51 52 53

#define DMA_WRITE_REG(reg, value) *((volatile u32 *)reg) = (u32)value;
#define DMA_READ_REG(reg, value)    value = (u32)*((volatile u32*)reg)
#define SW_WRITE_REG(reg, value)   \
wdenk's avatar
wdenk committed
54
	SWORD(reg, value);\
55
	DELAY;\
wdenk's avatar
wdenk committed
56
	SWORD(reg, value);
57

58 59 60 61
#define SW_READ_REG(reg, value)	   \
	value = (u32)*((volatile u32*)reg);\
	DELAY;\
	value = (u32)*((volatile u32*)reg);
62

63 64
#define INCA_DMA_TX_POLLING_TIME	0x07
#define INCA_DMA_RX_POLLING_TIME	0x07
65

66 67 68 69 70
#define INCA_DMA_TX_HOLD		0x80000000
#define INCA_DMA_TX_EOP			0x40000000
#define INCA_DMA_TX_SOP			0x20000000
#define INCA_DMA_TX_ICPT		0x10000000
#define INCA_DMA_TX_IEOP		0x08000000
71

72 73 74
#define INCA_DMA_RX_C			0x80000000
#define INCA_DMA_RX_SOP			0x40000000
#define INCA_DMA_RX_EOP			0x20000000
75

76 77 78 79 80
#define INCA_SWITCH_PHY_SPEED_10H	0x1
#define INCA_SWITCH_PHY_SPEED_10F	0x5
#define INCA_SWITCH_PHY_SPEED_100H	0x2
#define INCA_SWITCH_PHY_SPEED_100F	0x6

wdenk's avatar
wdenk committed
81
/************************ Auto MDIX settings ************************/
82 83 84 85 86 87 88 89
#define INCA_IP_AUTO_MDIX_LAN_PORTS_DIR		INCA_IP_Ports_P1_DIR
#define INCA_IP_AUTO_MDIX_LAN_PORTS_ALTSEL	INCA_IP_Ports_P1_ALTSEL
#define INCA_IP_AUTO_MDIX_LAN_PORTS_OUT		INCA_IP_Ports_P1_OUT
#define INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX	16

#define WAIT_SIGNAL_RETRIES			100
#define WAIT_LINK_RETRIES			100
#define LINK_RETRY_DELAY			2000  /* ms */
wdenk's avatar
wdenk committed
90
/********************************************************************/
91 92 93

typedef struct
{
94 95
	union {
		struct {
96 97 98 99 100 101
			volatile u32 HOLD		:1;
			volatile u32 ICpt		:1;
			volatile u32 IEop		:1;
			volatile u32 offset		:3;
			volatile u32 reserved0		:4;
			volatile u32 NFB		:22;
102 103 104 105 106 107 108 109 110
		}field;

		volatile u32 word;
	}params;

	volatile u32 nextRxDescPtr;

	volatile u32 RxDataPtr;

111 112
	union {
		struct {
113 114 115 116 117
			volatile u32 C			:1;
			volatile u32 Sop		:1;
			volatile u32 Eop		:1;
			volatile u32 reserved3		:12;
			volatile u32 NBT		:17;
118 119 120 121 122 123 124 125 126 127
		}field;

		volatile u32 word;
	}status;

} inca_rx_descriptor_t;


typedef struct
{
128 129
	union {
		struct {
130 131 132 133 134 135 136
			volatile u32 HOLD		:1;
			volatile u32 Eop		:1;
			volatile u32 Sop		:1;
			volatile u32 ICpt		:1;
			volatile u32 IEop		:1;
			volatile u32 reserved0		:5;
			volatile u32 NBA		:22;
137 138 139 140 141 142 143 144 145
		}field;

		volatile u32 word;
	}params;

	volatile u32 nextTxDescPtr;

	volatile u32 TxDataPtr;

146 147
	volatile u32 C			:1;
	volatile u32 reserved3		:31;
148 149 150 151 152 153 154 155 156

} inca_tx_descriptor_t;


static inca_rx_descriptor_t rx_ring[NUM_RX_DESC] __attribute__ ((aligned(16)));
static inca_tx_descriptor_t tx_ring[NUM_TX_DESC] __attribute__ ((aligned(16)));

static int tx_new, rx_new, tx_hold, rx_hold;
static int tx_old_hold = -1;
157
static int initialized	= 0;
158 159 160


static int inca_switch_init(struct eth_device *dev, bd_t * bis);
161
static int inca_switch_send(struct eth_device *dev, volatile void *packet, int length);
162 163 164 165
static int inca_switch_recv(struct eth_device *dev);
static void inca_switch_halt(struct eth_device *dev);
static void inca_init_switch_chip(void);
static void inca_dma_init(void);
wdenk's avatar
wdenk committed
166
static int inca_amdix(void);
167 168 169 170 171 172 173 174 175 176


int inca_switch_initialize(bd_t * bis)
{
	struct eth_device *dev;

#if 0
	printf("Entered inca_switch_initialize()\n");
#endif

177
	if (!(dev = (struct eth_device *) malloc (sizeof *dev))) {
178 179 180 181 182 183 184 185
		printf("Failed to allocate memory\n");
		return 0;
	}
	memset(dev, 0, sizeof(*dev));

	inca_dma_init();

	inca_init_switch_chip();
wdenk's avatar
wdenk committed
186

187
#if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
wdenk's avatar
wdenk committed
188
	inca_amdix();
189
#endif
190 191 192 193 194 195 196 197 198 199 200 201 202

	sprintf(dev->name, "INCA-IP Switch");
	dev->init = inca_switch_init;
	dev->halt = inca_switch_halt;
	dev->send = inca_switch_send;
	dev->recv = inca_switch_recv;

	eth_register(dev);

#if 0
	printf("Leaving inca_switch_initialize()\n");
#endif

203
	return 0;
204 205 206 207 208 209 210 211 212 213 214 215 216
}


static int inca_switch_init(struct eth_device *dev, bd_t * bis)
{
	int i;
	u32 v, regValue;
	u16 wTmp;

#if 0
	printf("Entering inca_switch_init()\n");
#endif

217 218
	/* Set MAC address.
	 */
219 220 221 222 223 224 225 226 227 228 229 230 231
	wTmp = (u16)dev->enetaddr[0];
	regValue = (wTmp << 8) | dev->enetaddr[1];

	SW_WRITE_REG(INCA_IP_Switch_PMAC_SA1, regValue);

	wTmp = (u16)dev->enetaddr[2];
	regValue = (wTmp << 8) | dev->enetaddr[3];
	regValue = regValue << 16;
	wTmp = (u16)dev->enetaddr[4];
	regValue |= (wTmp<<8) | dev->enetaddr[5];

	SW_WRITE_REG(INCA_IP_Switch_PMAC_SA2, regValue);

232 233
	/* Initialize the descriptor rings.
	 */
234
	for (i = 0; i < NUM_RX_DESC; i++) {
235
		inca_rx_descriptor_t * rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[i]);
236 237
		memset(rx_desc, 0, sizeof(rx_ring[i]));

238 239
		/* Set maximum size of receive buffer.
		 */
240 241
		rx_desc->params.field.NFB = PKTSIZE_ALIGN;

242 243 244
		/* Set the offset of the receive buffer. Zero means
		 * that the offset mechanism is not used.
		 */
245 246 247 248
		rx_desc->params.field.offset = 0;

		/* Check if it is the last descriptor.
		 */
249 250 251 252
		if (i == (NUM_RX_DESC - 1)) {
			/* Let the last descriptor point to the first
			 * one.
			 */
253
			rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(rx_ring);
254 255 256
		} else {
			/* Set the address of the next descriptor.
			 */
257
			rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(&rx_ring[i+1]);
258 259
		}

260
		rx_desc->RxDataPtr = (u32)CKSEG1ADDR(NetRxPackets[i]);
261 262 263 264 265 266 267
	}

#if 0
	printf("rx_ring = 0x%08X 0x%08X\n", (u32)rx_ring, (u32)&rx_ring[0]);
	printf("tx_ring = 0x%08X 0x%08X\n", (u32)tx_ring, (u32)&tx_ring[0]);
#endif

268
	for (i = 0; i < NUM_TX_DESC; i++) {
269
		inca_tx_descriptor_t * tx_desc = (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[i]);
270 271 272

		memset(tx_desc, 0, sizeof(tx_ring[i]));

273
		tx_desc->params.word	   = 0;
274
		tx_desc->params.field.HOLD = 1;
275
		tx_desc->C		   = 1;
276 277 278

			/* Check if it is the last descriptor.
			 */
279
		if (i == (NUM_TX_DESC - 1)) {
280 281 282
				/* Let the last descriptor point to the
				 * first one.
				 */
283
			tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(tx_ring);
284
		} else {
285 286
				/* Set the address of the next descriptor.
				 */
287
			tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(&tx_ring[i+1]);
288 289 290
		}
	}

291 292
	/* Initialize RxDMA.
	 */
293 294 295 296 297
	DMA_READ_REG(INCA_IP_DMA_DMA_RXISR, v);
#if 0
	printf("RX status = 0x%08X\n", v);
#endif

298 299
	/* Writing to the FRDA of CHANNEL.
	 */
300 301
	DMA_WRITE_REG(INCA_IP_DMA_DMA_RXFRDA0, (u32)rx_ring);

302 303
	/* Writing to the COMMAND REG.
	 */
304
	DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_INIT);
305

306 307
	/* Initialize TxDMA.
	 */
308 309 310 311 312
	DMA_READ_REG(INCA_IP_DMA_DMA_TXISR, v);
#if 0
	printf("TX status = 0x%08X\n", v);
#endif

313 314
	/* Writing to the FRDA of CHANNEL.
	 */
315 316 317 318 319 320 321 322 323 324
	DMA_WRITE_REG(INCA_IP_DMA_DMA_TXFRDA0, (u32)tx_ring);

	tx_new = rx_new = 0;

	tx_hold = NUM_TX_DESC - 1;
	rx_hold = NUM_RX_DESC - 1;

#if 0
	rx_ring[rx_hold].params.field.HOLD = 1;
#endif
325 326
	/* enable spanning tree forwarding, enable the CPU port */
	/* ST_PT:
327 328 329
	 *	CPS (CPU port status)	0x3 (forwarding)
	 *	LPS (LAN port status)	0x3 (forwarding)
	 *	PPS (PC port status)	0x3 (forwarding)
330
	 */
331 332 333 334 335 336 337 338 339 340
	SW_WRITE_REG(INCA_IP_Switch_ST_PT,0x3f);

#if 0
	printf("Leaving inca_switch_init()\n");
#endif

	return 0;
}


341
static int inca_switch_send(struct eth_device *dev, volatile void *packet, int length)
342
{
343 344 345 346
	int		       i;
	int		       res	= -1;
	u32		       command;
	u32		       regValue;
347
	inca_tx_descriptor_t * tx_desc	= (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_new]);
348 349 350 351 352

#if 0
	printf("Entered inca_switch_send()\n");
#endif

353
	if (length <= 0) {
354 355 356
		printf ("%s: bad packet size: %d\n", dev->name, length);
		goto Done;
	}
wdenk's avatar
wdenk committed
357

358 359
	for(i = 0; tx_desc->C == 0; i++) {
		if (i >= TOUT_LOOP) {
360 361 362 363 364
			printf("%s: tx error buffer not ready\n", dev->name);
			goto Done;
		}
	}

365
	if (tx_old_hold >= 0) {
366
		((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_old_hold]))->params.field.HOLD = 1;
367 368 369 370
	}
	tx_old_hold = tx_hold;

	tx_desc->params.word =
wdenk's avatar
wdenk committed
371
			(INCA_DMA_TX_SOP | INCA_DMA_TX_EOP | INCA_DMA_TX_HOLD);
372 373 374 375 376

	tx_desc->C = 0;
	tx_desc->TxDataPtr = (u32)packet;
	tx_desc->params.field.NBA = length;

377
	((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->params.field.HOLD = 0;
378 379

	tx_hold = tx_new;
380
	tx_new	= (tx_new + 1) % NUM_TX_DESC;
381 382


383
	if (! initialized) {
384 385
		command = INCA_IP_DMA_DMA_TXCCR0_INIT;
		initialized = 1;
386
	} else {
387 388
		command = INCA_IP_DMA_DMA_TXCCR0_HR;
	}
wdenk's avatar
wdenk committed
389

390 391 392 393 394 395 396 397
	DMA_READ_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
	regValue |= command;
#if 0
	printf("regValue = 0x%x\n", regValue);
#endif
	DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);

#if 1
398
	for(i = 0; ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->C == 0; i++) {
399
		if (i >= TOUT_LOOP) {
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
			printf("%s: tx buffer not ready\n", dev->name);
			goto Done;
		}
	}
#endif
	res = length;
Done:
#if 0
	printf("Leaving inca_switch_send()\n");
#endif
	return res;
}


static int inca_switch_recv(struct eth_device *dev)
{
416
	int		       length  = 0;
417 418 419 420 421 422
	inca_rx_descriptor_t * rx_desc;

#if 0
	printf("Entered inca_switch_recv()\n");
#endif

423
	for (;;) {
424
		rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_new]);
425

426
		if (rx_desc->status.field.C == 0) {
427 428 429 430 431 432 433
			break;
		}

#if 0
		rx_ring[rx_new].params.field.HOLD = 1;
#endif

434
		if (! rx_desc->status.field.Eop) {
435 436 437 438 439 440
			printf("Partly received packet!!!\n");
			break;
		}

		length = rx_desc->status.field.NBT;
		rx_desc->status.word &=
wdenk's avatar
wdenk committed
441
			 ~(INCA_DMA_RX_EOP | INCA_DMA_RX_SOP | INCA_DMA_RX_C);
442 443 444 445 446 447 448 449 450 451 452
#if 0
{
  int i;
  for (i=0;i<length - 4;i++) {
    if (i % 16 == 0) printf("\n%04x: ", i);
    printf("%02X ", NetRxPackets[rx_new][i]);
  }
  printf("\n");
}
#endif

453
		if (length) {
454 455 456
#if 0
			printf("Received %d bytes\n", length);
#endif
457
			NetReceive((void*)CKSEG1ADDR(NetRxPackets[rx_new]), length - 4);
458
		} else {
459 460 461 462 463 464
#if 1
			printf("Zero length!!!\n");
#endif
		}


465
		((inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_hold]))->params.field.HOLD = 0;
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

		rx_hold = rx_new;

		rx_new = (rx_new + 1) % NUM_RX_DESC;
	}

#if 0
	printf("Leaving inca_switch_recv()\n");
#endif

	return length;
}


static void inca_switch_halt(struct eth_device *dev)
{
#if 0
	printf("Entered inca_switch_halt()\n");
#endif

#if 1
	initialized = 0;
#endif
#if 1
490 491
	/* Disable forwarding to the CPU port.
	 */
492 493
	SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);

494 495
	/* Close RxDMA channel.
	 */
496 497
	DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);

498 499
	/* Close TxDMA channel.
	 */
500 501 502 503 504 505 506 507 508 509 510 511 512 513
	DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_TXCCR0_OFF);


#endif
#if 0
	printf("Leaving inca_switch_halt()\n");
#endif
}


static void inca_init_switch_chip(void)
{
	u32 regValue;

514 515 516
	/* To workaround a problem with collision counter
	 * (see Errata sheet).
	 */
517 518 519 520
	SW_WRITE_REG(INCA_IP_Switch_PC_TX_CTL, 0x00000001);
	SW_WRITE_REG(INCA_IP_Switch_LAN_TX_CTL, 0x00000001);

#if 1
521
	/* init MDIO configuration:
522 523 524
	 *	MDS (Poll speed):	0x01 (4ms)
	 *	PHY_LAN_ADDR:		0x06
	 *	PHY_PC_ADDR:		0x05
525
	 *	UEP (Use External PHY): 0x00 (Internal PHY is used)
526 527 528
	 *	PS (Port Select):	0x00 (PT/UMM for LAN)
	 *	PT (PHY Test):		0x00 (no test mode)
	 *	UMM (Use MDIO Mode):	0x00 (state machine is disabled)
529
	 */
530 531
	SW_WRITE_REG(INCA_IP_Switch_MDIO_CFG, 0x4c50);

532 533 534 535 536 537 538 539 540 541
	/* init PHY:
	 *	SL (Auto Neg. Speed for LAN)
	 *	SP (Auto Neg. Speed for PC)
	 *	LL (Link Status for LAN)
	 *	LP (Link Status for PC)
	 *	DL (Duplex Status for LAN)
	 *	DP (Duplex Status for PC)
	 *	PL (Auto Neg. Pause Status for LAN)
	 *	PP (Auto Neg. Pause Status for PC)
	 */
542 543
	SW_WRITE_REG (INCA_IP_Switch_EPHY, 0xff);

544 545
	/* MDIO_ACC:
	 *	RA (Request/Ack)  0x01 (Request)
546 547 548 549 550 551 552
	 *	RW (Read/Write)	  0x01 (Write)
	 *	PHY_ADDR	  0x05 (PC)
	 *	REG_ADDR	  0x00 (PHY_BCR: basic control register)
	 *	PHY_DATA	  0x8000
	 *		      Reset		      - software reset
	 *		      LB (loop back)	      - normal
	 *		      SS (speed select)	      - 10 Mbit/s
553
	 *		      ANE (auto neg. enable)  - enable
554 555
	 *		      PD (power down)	      - normal
	 *		      ISO (isolate)	      - normal
556
	 *		      RAN (restart auto neg.) - normal
557
	 *		      DM (duplex mode)	      - half duplex
558 559 560 561 562 563
	 *		      CT (collision test)     - enable
	 */
	SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0a09000);

	/* MDIO_ACC:
	 *	RA (Request/Ack)  0x01 (Request)
564 565 566 567 568 569 570
	 *	RW (Read/Write)	  0x01 (Write)
	 *	PHY_ADDR	  0x06 (LAN)
	 *	REG_ADDR	  0x00 (PHY_BCR: basic control register)
	 *	PHY_DATA	  0x8000
	 *		      Reset		      - software reset
	 *		      LB (loop back)	      - normal
	 *		      SS (speed select)	      - 10 Mbit/s
571
	 *		      ANE (auto neg. enable)  - enable
572 573
	 *		      PD (power down)	      - normal
	 *		      ISO (isolate)	      - normal
574
	 *		      RAN (restart auto neg.) - normal
575
	 *		      DM (duplex mode)	      - half duplex
576 577
	 *		      CT (collision test)     - enable
	 */
578
	SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0c09000);
579

580 581
#endif

582 583 584 585
	/* Make sure the CPU port is disabled for now. We
	 * don't want packets to get stacked for us until
	 * we enable DMA and are prepared to receive them.
	 */
586 587 588 589
	SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);

	SW_READ_REG(INCA_IP_Switch_ARL_CTL, regValue);

590 591
	/* CRC GEN is enabled.
	 */
592 593 594
	regValue |= 0x00000200;
	SW_WRITE_REG(INCA_IP_Switch_ARL_CTL, regValue);

595 596
	/* ADD TAG is disabled.
	 */
597 598 599 600 601 602 603 604
	SW_READ_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
	regValue &= ~0x00000002;
	SW_WRITE_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
}


static void inca_dma_init(void)
{
605 606
	/* Switch off all DMA channels.
	 */
607 608 609 610 611 612 613
	DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
	DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR1, INCA_IP_DMA_DMA_RXCCR1_OFF);

	DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
	DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR1, INCA_IP_DMA_DMA_TXCCR1_OFF);
	DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR2, INCA_IP_DMA_DMA_TXCCR2_OFF);

614 615
	/* Setup TX channel polling time.
	 */
616 617
	DMA_WRITE_REG(INCA_IP_DMA_DMA_TXPOLL, INCA_DMA_TX_POLLING_TIME);

618 619
	/* Setup RX channel polling time.
	 */
620 621
	DMA_WRITE_REG(INCA_IP_DMA_DMA_RXPOLL, INCA_DMA_RX_POLLING_TIME);

622 623
	/* ERRATA: write reset value into the DMA RX IMR register.
	 */
624 625
	DMA_WRITE_REG(INCA_IP_DMA_DMA_RXIMR, 0xFFFFFFFF);

626 627
	/* Just in case: disable all transmit interrupts also.
	 */
628 629 630 631 632 633
	DMA_WRITE_REG(INCA_IP_DMA_DMA_TXIMR, 0xFFFFFFFF);

	DMA_WRITE_REG(INCA_IP_DMA_DMA_TXISR, 0xFFFFFFFF);
	DMA_WRITE_REG(INCA_IP_DMA_DMA_RXISR, 0xFFFFFFFF);
}

634
#if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
wdenk's avatar
wdenk committed
635 636
static int inca_amdix(void)
{
637 638 639 640 641 642
	u32 phyReg1 = 0;
	u32 phyReg4 = 0;
	u32 phyReg5 = 0;
	u32 phyReg6 = 0;
	u32 phyReg31 = 0;
	u32 regEphy = 0;
wdenk's avatar
wdenk committed
643 644 645 646 647 648 649 650
	int mdi_flag;
	int retries;

	/* Setup GPIO pins.
	 */
	*INCA_IP_AUTO_MDIX_LAN_PORTS_DIR    |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
	*INCA_IP_AUTO_MDIX_LAN_PORTS_ALTSEL |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);

651
#if 0
wdenk's avatar
wdenk committed
652 653 654
	/* Wait for signal.
	 */
	retries = WAIT_SIGNAL_RETRIES;
655
	while (--retries) {
wdenk's avatar
wdenk committed
656 657 658 659 660
		SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
				(0x1 << 31) |	/* RA		*/
				(0x0 << 30) |	/* Read		*/
				(0x6 << 21) |	/* LAN		*/
				(17  << 16));	/* PHY_MCSR	*/
661 662 663
		do {
			SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
		} while (phyReg1 & (1 << 31));
wdenk's avatar
wdenk committed
664

665
		if (phyReg1 & (1 << 1)) {
wdenk's avatar
wdenk committed
666 667 668 669 670 671
			/* Signal detected */
			break;
		}
	}

	if (!retries)
672 673
		goto Fail;
#endif
wdenk's avatar
wdenk committed
674 675 676 677 678 679 680 681 682

	/* Set MDI mode.
	 */
	*INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
	mdi_flag = 1;

	/* Wait for link.
	 */
	retries = WAIT_LINK_RETRIES;
683
	while (--retries) {
wdenk's avatar
wdenk committed
684 685 686 687 688 689
		udelay(LINK_RETRY_DELAY * 1000);
		SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
				(0x1 << 31) |	/* RA		*/
				(0x0 << 30) |	/* Read		*/
				(0x6 << 21) |	/* LAN		*/
				(1   << 16));	/* PHY_BSR	*/
690 691 692
		do {
			SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
		} while (phyReg1 & (1 << 31));
wdenk's avatar
wdenk committed
693

694
		if (phyReg1 & (1 << 2)) {
wdenk's avatar
wdenk committed
695 696
			/* Link is up */
			break;
697
		} else if (mdi_flag) {
wdenk's avatar
wdenk committed
698 699 700
			/* Set MDIX mode */
			*INCA_IP_AUTO_MDIX_LAN_PORTS_OUT |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
			mdi_flag = 0;
701
		} else {
wdenk's avatar
wdenk committed
702 703 704 705 706 707
			/* Set MDI mode */
			*INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
			mdi_flag = 1;
		}
	}

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
	if (!retries) {
		goto Fail;
	} else {
		SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
				(0x1 << 31) |	/* RA		*/
				(0x0 << 30) |	/* Read		*/
				(0x6 << 21) |	/* LAN		*/
				(1   << 16));	/* PHY_BSR	*/
		do {
			SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
		} while (phyReg1 & (1 << 31));

		/* Auto-negotiation / Parallel detection complete
		 */
		if (phyReg1 & (1 << 5)) {
			SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
				(0x1 << 31) |	/* RA		*/
				(0x0 << 30) |	/* Read		*/
				(0x6 << 21) |	/* LAN		*/
				(31  << 16));	/* PHY_SCSR	*/
			do {
729
				SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg31);
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
			} while (phyReg31 & (1 << 31));

			switch ((phyReg31 >> 2) & 0x7) {
			case INCA_SWITCH_PHY_SPEED_10H:
				/* 10Base-T Half-duplex */
				regEphy = 0;
				break;
			case INCA_SWITCH_PHY_SPEED_10F:
				/* 10Base-T Full-duplex */
				regEphy = INCA_IP_Switch_EPHY_DL;
				break;
			case INCA_SWITCH_PHY_SPEED_100H:
				/* 100Base-TX Half-duplex */
				regEphy = INCA_IP_Switch_EPHY_SL;
				break;
			case INCA_SWITCH_PHY_SPEED_100F:
				/* 100Base-TX Full-duplex */
				regEphy = INCA_IP_Switch_EPHY_SL | INCA_IP_Switch_EPHY_DL;
				break;
			}

			/* In case of Auto-negotiation,
			 * update the negotiated PAUSE support status
			 */
			if (phyReg1 & (1 << 3)) {
				SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
					(0x1 << 31) |	/* RA		*/
					(0x0 << 30) |	/* Read		*/
					(0x6 << 21) |	/* LAN		*/
					(6   << 16));	/* PHY_ANER	*/
				do {
761
					SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg6);
762 763 764 765 766 767 768 769 770 771 772 773
				} while (phyReg6 & (1 << 31));

				/* We are Autoneg-able.
				 * Is Link partner also able to autoneg?
				 */
				if (phyReg6 & (1 << 0)) {
					SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
						(0x1 << 31) |	/* RA		*/
						(0x0 << 30) |	/* Read		*/
						(0x6 << 21) |	/* LAN		*/
						(4   << 16));	/* PHY_ANAR	*/
					do {
774
						SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg4);
775 776 777 778 779 780 781 782 783 784 785 786
					} while (phyReg4 & (1 << 31));

					/* We advertise PAUSE capab.
					 * Does link partner also advertise it?
					 */
					if (phyReg4 & (1 << 10)) {
						SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
							(0x1 << 31) |	/* RA		*/
							(0x0 << 30) |	/* Read		*/
							(0x6 << 21) |	/* LAN		*/
							(5   << 16));	/* PHY_ANLPAR	*/
						do {
787
							SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg5);
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
						} while (phyReg5 & (1 << 31));

						/* Link partner is PAUSE capab.
						 */
						if (phyReg5 & (1 << 10)) {
							regEphy |= INCA_IP_Switch_EPHY_PL;
						}
					}
				}

			}

			/* Link is up */
			regEphy |= INCA_IP_Switch_EPHY_LL;

			SW_WRITE_REG(INCA_IP_Switch_EPHY, regEphy);
		}
	}
wdenk's avatar
wdenk committed
806 807

	return 0;
808 809 810 811

Fail:
	printf("No Link on LAN port\n");
	return -1;
wdenk's avatar
wdenk committed
812
}
813
#endif /* CONFIG_INCA_IP_SWITCH_AMDIX */