2 * INCA-IP internal switch ethernet driver.
4 * (C) Copyright 2003-2004
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0+
16 #include <asm/inca-ip.h>
17 #include <asm/addrspace.h>
20 #define NUM_RX_DESC PKTBUFSRX
22 #define TOUT_LOOP 1000000
25 #define DELAY udelay(10000)
26 /* Sometimes the store word instruction hangs while writing to one
27 * of the Switch registers. Moving the instruction into a separate
28 * function somehow makes the problem go away.
30 static void SWORD(volatile u32 * reg, u32 value)
35 #define DMA_WRITE_REG(reg, value) *((volatile u32 *)reg) = (u32)value;
36 #define DMA_READ_REG(reg, value) value = (u32)*((volatile u32*)reg)
37 #define SW_WRITE_REG(reg, value) \
42 #define SW_READ_REG(reg, value) \
43 value = (u32)*((volatile u32*)reg);\
45 value = (u32)*((volatile u32*)reg);
47 #define INCA_DMA_TX_POLLING_TIME 0x07
48 #define INCA_DMA_RX_POLLING_TIME 0x07
50 #define INCA_DMA_TX_HOLD 0x80000000
51 #define INCA_DMA_TX_EOP 0x40000000
52 #define INCA_DMA_TX_SOP 0x20000000
53 #define INCA_DMA_TX_ICPT 0x10000000
54 #define INCA_DMA_TX_IEOP 0x08000000
56 #define INCA_DMA_RX_C 0x80000000
57 #define INCA_DMA_RX_SOP 0x40000000
58 #define INCA_DMA_RX_EOP 0x20000000
60 #define INCA_SWITCH_PHY_SPEED_10H 0x1
61 #define INCA_SWITCH_PHY_SPEED_10F 0x5
62 #define INCA_SWITCH_PHY_SPEED_100H 0x2
63 #define INCA_SWITCH_PHY_SPEED_100F 0x6
65 /************************ Auto MDIX settings ************************/
66 #define INCA_IP_AUTO_MDIX_LAN_PORTS_DIR INCA_IP_Ports_P1_DIR
67 #define INCA_IP_AUTO_MDIX_LAN_PORTS_ALTSEL INCA_IP_Ports_P1_ALTSEL
68 #define INCA_IP_AUTO_MDIX_LAN_PORTS_OUT INCA_IP_Ports_P1_OUT
69 #define INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX 16
71 #define WAIT_SIGNAL_RETRIES 100
72 #define WAIT_LINK_RETRIES 100
73 #define LINK_RETRY_DELAY 2000 /* ms */
74 /********************************************************************/
83 volatile u32 offset :3;
84 volatile u32 reserved0 :4;
91 volatile u32 nextRxDescPtr;
93 volatile u32 RxDataPtr;
100 volatile u32 reserved3 :12;
101 volatile u32 NBT :17;
107 } inca_rx_descriptor_t;
114 volatile u32 HOLD :1;
117 volatile u32 ICpt :1;
118 volatile u32 IEop :1;
119 volatile u32 reserved0 :5;
120 volatile u32 NBA :22;
126 volatile u32 nextTxDescPtr;
128 volatile u32 TxDataPtr;
131 volatile u32 reserved3 :31;
133 } inca_tx_descriptor_t;
136 static inca_rx_descriptor_t rx_ring[NUM_RX_DESC] __attribute__ ((aligned(16)));
137 static inca_tx_descriptor_t tx_ring[NUM_TX_DESC] __attribute__ ((aligned(16)));
139 static int tx_new, rx_new, tx_hold, rx_hold;
140 static int tx_old_hold = -1;
141 static int initialized = 0;
144 static int inca_switch_init(struct eth_device *dev, bd_t * bis);
145 static int inca_switch_send(struct eth_device *dev, void *packet, int length);
146 static int inca_switch_recv(struct eth_device *dev);
147 static void inca_switch_halt(struct eth_device *dev);
148 static void inca_init_switch_chip(void);
149 static void inca_dma_init(void);
150 static int inca_amdix(void);
153 int inca_switch_initialize(bd_t * bis)
155 struct eth_device *dev;
158 printf("Entered inca_switch_initialize()\n");
161 if (!(dev = (struct eth_device *) malloc (sizeof *dev))) {
162 printf("Failed to allocate memory\n");
165 memset(dev, 0, sizeof(*dev));
169 inca_init_switch_chip();
171 #if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
175 sprintf(dev->name, "INCA-IP Switch");
176 dev->init = inca_switch_init;
177 dev->halt = inca_switch_halt;
178 dev->send = inca_switch_send;
179 dev->recv = inca_switch_recv;
184 printf("Leaving inca_switch_initialize()\n");
191 static int inca_switch_init(struct eth_device *dev, bd_t * bis)
198 printf("Entering inca_switch_init()\n");
203 wTmp = (u16)dev->enetaddr[0];
204 regValue = (wTmp << 8) | dev->enetaddr[1];
206 SW_WRITE_REG(INCA_IP_Switch_PMAC_SA1, regValue);
208 wTmp = (u16)dev->enetaddr[2];
209 regValue = (wTmp << 8) | dev->enetaddr[3];
210 regValue = regValue << 16;
211 wTmp = (u16)dev->enetaddr[4];
212 regValue |= (wTmp<<8) | dev->enetaddr[5];
214 SW_WRITE_REG(INCA_IP_Switch_PMAC_SA2, regValue);
216 /* Initialize the descriptor rings.
218 for (i = 0; i < NUM_RX_DESC; i++) {
219 inca_rx_descriptor_t * rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[i]);
220 memset(rx_desc, 0, sizeof(rx_ring[i]));
222 /* Set maximum size of receive buffer.
224 rx_desc->params.field.NFB = PKTSIZE_ALIGN;
226 /* Set the offset of the receive buffer. Zero means
227 * that the offset mechanism is not used.
229 rx_desc->params.field.offset = 0;
231 /* Check if it is the last descriptor.
233 if (i == (NUM_RX_DESC - 1)) {
234 /* Let the last descriptor point to the first
237 rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(rx_ring);
239 /* Set the address of the next descriptor.
241 rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(&rx_ring[i+1]);
244 rx_desc->RxDataPtr = (u32)CKSEG1ADDR(NetRxPackets[i]);
248 printf("rx_ring = 0x%08X 0x%08X\n", (u32)rx_ring, (u32)&rx_ring[0]);
249 printf("tx_ring = 0x%08X 0x%08X\n", (u32)tx_ring, (u32)&tx_ring[0]);
252 for (i = 0; i < NUM_TX_DESC; i++) {
253 inca_tx_descriptor_t * tx_desc = (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[i]);
255 memset(tx_desc, 0, sizeof(tx_ring[i]));
257 tx_desc->params.word = 0;
258 tx_desc->params.field.HOLD = 1;
261 /* Check if it is the last descriptor.
263 if (i == (NUM_TX_DESC - 1)) {
264 /* Let the last descriptor point to the
267 tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(tx_ring);
269 /* Set the address of the next descriptor.
271 tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(&tx_ring[i+1]);
277 DMA_READ_REG(INCA_IP_DMA_DMA_RXISR, v);
278 debug("RX status = 0x%08X\n", v);
280 /* Writing to the FRDA of CHANNEL.
282 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXFRDA0, (u32)rx_ring);
284 /* Writing to the COMMAND REG.
286 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_INIT);
290 DMA_READ_REG(INCA_IP_DMA_DMA_TXISR, v);
291 debug("TX status = 0x%08X\n", v);
293 /* Writing to the FRDA of CHANNEL.
295 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXFRDA0, (u32)tx_ring);
299 tx_hold = NUM_TX_DESC - 1;
300 rx_hold = NUM_RX_DESC - 1;
303 rx_ring[rx_hold].params.field.HOLD = 1;
305 /* enable spanning tree forwarding, enable the CPU port */
307 * CPS (CPU port status) 0x3 (forwarding)
308 * LPS (LAN port status) 0x3 (forwarding)
309 * PPS (PC port status) 0x3 (forwarding)
311 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0x3f);
314 printf("Leaving inca_switch_init()\n");
321 static int inca_switch_send(struct eth_device *dev, void *packet, int length)
327 inca_tx_descriptor_t * tx_desc = (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_new]);
330 printf("Entered inca_switch_send()\n");
334 printf ("%s: bad packet size: %d\n", dev->name, length);
338 for(i = 0; tx_desc->C == 0; i++) {
339 if (i >= TOUT_LOOP) {
340 printf("%s: tx error buffer not ready\n", dev->name);
345 if (tx_old_hold >= 0) {
346 ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_old_hold]))->params.field.HOLD = 1;
348 tx_old_hold = tx_hold;
350 tx_desc->params.word =
351 (INCA_DMA_TX_SOP | INCA_DMA_TX_EOP | INCA_DMA_TX_HOLD);
354 tx_desc->TxDataPtr = (u32)packet;
355 tx_desc->params.field.NBA = length;
357 ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->params.field.HOLD = 0;
360 tx_new = (tx_new + 1) % NUM_TX_DESC;
364 command = INCA_IP_DMA_DMA_TXCCR0_INIT;
367 command = INCA_IP_DMA_DMA_TXCCR0_HR;
370 DMA_READ_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
373 printf("regValue = 0x%x\n", regValue);
375 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
378 for(i = 0; ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->C == 0; i++) {
379 if (i >= TOUT_LOOP) {
380 printf("%s: tx buffer not ready\n", dev->name);
388 printf("Leaving inca_switch_send()\n");
394 static int inca_switch_recv(struct eth_device *dev)
397 inca_rx_descriptor_t * rx_desc;
400 printf("Entered inca_switch_recv()\n");
404 rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_new]);
406 if (rx_desc->status.field.C == 0) {
411 rx_ring[rx_new].params.field.HOLD = 1;
414 if (! rx_desc->status.field.Eop) {
415 printf("Partly received packet!!!\n");
419 length = rx_desc->status.field.NBT;
420 rx_desc->status.word &=
421 ~(INCA_DMA_RX_EOP | INCA_DMA_RX_SOP | INCA_DMA_RX_C);
425 for (i=0;i<length - 4;i++) {
426 if (i % 16 == 0) printf("\n%04x: ", i);
427 printf("%02X ", NetRxPackets[rx_new][i]);
435 printf("Received %d bytes\n", length);
437 NetReceive((void*)CKSEG1ADDR(NetRxPackets[rx_new]), length - 4);
440 printf("Zero length!!!\n");
445 ((inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_hold]))->params.field.HOLD = 0;
449 rx_new = (rx_new + 1) % NUM_RX_DESC;
453 printf("Leaving inca_switch_recv()\n");
460 static void inca_switch_halt(struct eth_device *dev)
463 printf("Entered inca_switch_halt()\n");
470 /* Disable forwarding to the CPU port.
472 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
474 /* Close RxDMA channel.
476 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
478 /* Close TxDMA channel.
480 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_TXCCR0_OFF);
485 printf("Leaving inca_switch_halt()\n");
490 static void inca_init_switch_chip(void)
494 /* To workaround a problem with collision counter
495 * (see Errata sheet).
497 SW_WRITE_REG(INCA_IP_Switch_PC_TX_CTL, 0x00000001);
498 SW_WRITE_REG(INCA_IP_Switch_LAN_TX_CTL, 0x00000001);
501 /* init MDIO configuration:
502 * MDS (Poll speed): 0x01 (4ms)
505 * UEP (Use External PHY): 0x00 (Internal PHY is used)
506 * PS (Port Select): 0x00 (PT/UMM for LAN)
507 * PT (PHY Test): 0x00 (no test mode)
508 * UMM (Use MDIO Mode): 0x00 (state machine is disabled)
510 SW_WRITE_REG(INCA_IP_Switch_MDIO_CFG, 0x4c50);
513 * SL (Auto Neg. Speed for LAN)
514 * SP (Auto Neg. Speed for PC)
515 * LL (Link Status for LAN)
516 * LP (Link Status for PC)
517 * DL (Duplex Status for LAN)
518 * DP (Duplex Status for PC)
519 * PL (Auto Neg. Pause Status for LAN)
520 * PP (Auto Neg. Pause Status for PC)
522 SW_WRITE_REG (INCA_IP_Switch_EPHY, 0xff);
525 * RA (Request/Ack) 0x01 (Request)
526 * RW (Read/Write) 0x01 (Write)
528 * REG_ADDR 0x00 (PHY_BCR: basic control register)
530 * Reset - software reset
531 * LB (loop back) - normal
532 * SS (speed select) - 10 Mbit/s
533 * ANE (auto neg. enable) - enable
534 * PD (power down) - normal
535 * ISO (isolate) - normal
536 * RAN (restart auto neg.) - normal
537 * DM (duplex mode) - half duplex
538 * CT (collision test) - enable
540 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0a09000);
543 * RA (Request/Ack) 0x01 (Request)
544 * RW (Read/Write) 0x01 (Write)
545 * PHY_ADDR 0x06 (LAN)
546 * REG_ADDR 0x00 (PHY_BCR: basic control register)
548 * Reset - software reset
549 * LB (loop back) - normal
550 * SS (speed select) - 10 Mbit/s
551 * ANE (auto neg. enable) - enable
552 * PD (power down) - normal
553 * ISO (isolate) - normal
554 * RAN (restart auto neg.) - normal
555 * DM (duplex mode) - half duplex
556 * CT (collision test) - enable
558 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0c09000);
562 /* Make sure the CPU port is disabled for now. We
563 * don't want packets to get stacked for us until
564 * we enable DMA and are prepared to receive them.
566 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
568 SW_READ_REG(INCA_IP_Switch_ARL_CTL, regValue);
570 /* CRC GEN is enabled.
572 regValue |= 0x00000200;
573 SW_WRITE_REG(INCA_IP_Switch_ARL_CTL, regValue);
575 /* ADD TAG is disabled.
577 SW_READ_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
578 regValue &= ~0x00000002;
579 SW_WRITE_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
583 static void inca_dma_init(void)
585 /* Switch off all DMA channels.
587 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
588 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR1, INCA_IP_DMA_DMA_RXCCR1_OFF);
590 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
591 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR1, INCA_IP_DMA_DMA_TXCCR1_OFF);
592 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR2, INCA_IP_DMA_DMA_TXCCR2_OFF);
594 /* Setup TX channel polling time.
596 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXPOLL, INCA_DMA_TX_POLLING_TIME);
598 /* Setup RX channel polling time.
600 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXPOLL, INCA_DMA_RX_POLLING_TIME);
602 /* ERRATA: write reset value into the DMA RX IMR register.
604 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXIMR, 0xFFFFFFFF);
606 /* Just in case: disable all transmit interrupts also.
608 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXIMR, 0xFFFFFFFF);
610 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXISR, 0xFFFFFFFF);
611 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXISR, 0xFFFFFFFF);
614 #if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
615 static int inca_amdix(void)
628 *INCA_IP_AUTO_MDIX_LAN_PORTS_DIR |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
629 *INCA_IP_AUTO_MDIX_LAN_PORTS_ALTSEL |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
634 retries = WAIT_SIGNAL_RETRIES;
636 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
637 (0x1 << 31) | /* RA */
638 (0x0 << 30) | /* Read */
639 (0x6 << 21) | /* LAN */
640 (17 << 16)); /* PHY_MCSR */
642 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
643 } while (phyReg1 & (1 << 31));
645 if (phyReg1 & (1 << 1)) {
646 /* Signal detected */
657 *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
662 retries = WAIT_LINK_RETRIES;
664 udelay(LINK_RETRY_DELAY * 1000);
665 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
666 (0x1 << 31) | /* RA */
667 (0x0 << 30) | /* Read */
668 (0x6 << 21) | /* LAN */
669 (1 << 16)); /* PHY_BSR */
671 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
672 } while (phyReg1 & (1 << 31));
674 if (phyReg1 & (1 << 2)) {
677 } else if (mdi_flag) {
679 *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
683 *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
691 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
692 (0x1 << 31) | /* RA */
693 (0x0 << 30) | /* Read */
694 (0x6 << 21) | /* LAN */
695 (1 << 16)); /* PHY_BSR */
697 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
698 } while (phyReg1 & (1 << 31));
700 /* Auto-negotiation / Parallel detection complete
702 if (phyReg1 & (1 << 5)) {
703 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
704 (0x1 << 31) | /* RA */
705 (0x0 << 30) | /* Read */
706 (0x6 << 21) | /* LAN */
707 (31 << 16)); /* PHY_SCSR */
709 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg31);
710 } while (phyReg31 & (1 << 31));
712 switch ((phyReg31 >> 2) & 0x7) {
713 case INCA_SWITCH_PHY_SPEED_10H:
714 /* 10Base-T Half-duplex */
717 case INCA_SWITCH_PHY_SPEED_10F:
718 /* 10Base-T Full-duplex */
719 regEphy = INCA_IP_Switch_EPHY_DL;
721 case INCA_SWITCH_PHY_SPEED_100H:
722 /* 100Base-TX Half-duplex */
723 regEphy = INCA_IP_Switch_EPHY_SL;
725 case INCA_SWITCH_PHY_SPEED_100F:
726 /* 100Base-TX Full-duplex */
727 regEphy = INCA_IP_Switch_EPHY_SL | INCA_IP_Switch_EPHY_DL;
731 /* In case of Auto-negotiation,
732 * update the negotiated PAUSE support status
734 if (phyReg1 & (1 << 3)) {
735 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
736 (0x1 << 31) | /* RA */
737 (0x0 << 30) | /* Read */
738 (0x6 << 21) | /* LAN */
739 (6 << 16)); /* MII_EXPANSION */
741 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg6);
742 } while (phyReg6 & (1 << 31));
744 /* We are Autoneg-able.
745 * Is Link partner also able to autoneg?
747 if (phyReg6 & (1 << 0)) {
748 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
749 (0x1 << 31) | /* RA */
750 (0x0 << 30) | /* Read */
751 (0x6 << 21) | /* LAN */
752 (4 << 16)); /* MII_ADVERTISE */
754 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg4);
755 } while (phyReg4 & (1 << 31));
757 /* We advertise PAUSE capab.
758 * Does link partner also advertise it?
760 if (phyReg4 & (1 << 10)) {
761 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
762 (0x1 << 31) | /* RA */
763 (0x0 << 30) | /* Read */
764 (0x6 << 21) | /* LAN */
765 (5 << 16)); /* MII_LPA */
767 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg5);
768 } while (phyReg5 & (1 << 31));
770 /* Link partner is PAUSE capab.
772 if (phyReg5 & (1 << 10)) {
773 regEphy |= INCA_IP_Switch_EPHY_PL;
781 regEphy |= INCA_IP_Switch_EPHY_LL;
783 SW_WRITE_REG(INCA_IP_Switch_EPHY, regEphy);
790 printf("No Link on LAN port\n");
793 #endif /* CONFIG_INCA_IP_SWITCH_AMDIX */