blob: cdfbfa67d89d32d0e53e804e66a79a28ba74aa57 [file] [log] [blame]
wdenkac6dbb82003-03-26 11:42:53 +00001/*
2 * INCA-IP internal switch ethernet driver.
3 *
wdenkf8d813e2004-03-02 14:05:39 +00004 * (C) Copyright 2003-2004
wdenkac6dbb82003-03-26 11:42:53 +00005 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
wdenkac6dbb82003-03-26 11:42:53 +00008 */
9
10
11#include <common.h>
12
wdenkac6dbb82003-03-26 11:42:53 +000013#include <malloc.h>
14#include <net.h>
Ben Warren8218bd22008-08-31 10:16:59 -070015#include <netdev.h>
wdenkac6dbb82003-03-26 11:42:53 +000016#include <asm/inca-ip.h>
17#include <asm/addrspace.h>
18
19
20#define NUM_RX_DESC PKTBUFSRX
21#define NUM_TX_DESC 3
22#define TOUT_LOOP 1000000
23
24
25#define DELAY udelay(10000)
wdenk356a0d92004-06-09 00:10:59 +000026 /* 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.
29 */
30static void SWORD(volatile u32 * reg, u32 value)
31{
32 *reg = value;
33}
wdenkac6dbb82003-03-26 11:42:53 +000034
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) \
wdenk356a0d92004-06-09 00:10:59 +000038 SWORD(reg, value);\
wdenk79d696f2004-03-11 22:46:36 +000039 DELAY;\
wdenk356a0d92004-06-09 00:10:59 +000040 SWORD(reg, value);
wdenkac6dbb82003-03-26 11:42:53 +000041
wdenk79d696f2004-03-11 22:46:36 +000042#define SW_READ_REG(reg, value) \
43 value = (u32)*((volatile u32*)reg);\
44 DELAY;\
45 value = (u32)*((volatile u32*)reg);
wdenkac6dbb82003-03-26 11:42:53 +000046
wdenk79d696f2004-03-11 22:46:36 +000047#define INCA_DMA_TX_POLLING_TIME 0x07
48#define INCA_DMA_RX_POLLING_TIME 0x07
wdenkac6dbb82003-03-26 11:42:53 +000049
wdenk79d696f2004-03-11 22:46:36 +000050#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
wdenkac6dbb82003-03-26 11:42:53 +000055
wdenk79d696f2004-03-11 22:46:36 +000056#define INCA_DMA_RX_C 0x80000000
57#define INCA_DMA_RX_SOP 0x40000000
58#define INCA_DMA_RX_EOP 0x20000000
wdenkac6dbb82003-03-26 11:42:53 +000059
wdenkf8d813e2004-03-02 14:05:39 +000060#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
64
wdenkcf56e112004-02-20 22:02:48 +000065/************************ Auto MDIX settings ************************/
wdenk79d696f2004-03-11 22:46:36 +000066#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
wdenkcf56e112004-02-20 22:02:48 +000070
wdenk79d696f2004-03-11 22:46:36 +000071#define WAIT_SIGNAL_RETRIES 100
72#define WAIT_LINK_RETRIES 100
73#define LINK_RETRY_DELAY 2000 /* ms */
wdenkcf56e112004-02-20 22:02:48 +000074/********************************************************************/
wdenkac6dbb82003-03-26 11:42:53 +000075
76typedef struct
77{
wdenke0ac62d2003-08-17 18:55:18 +000078 union {
79 struct {
wdenk79d696f2004-03-11 22:46:36 +000080 volatile u32 HOLD :1;
81 volatile u32 ICpt :1;
82 volatile u32 IEop :1;
83 volatile u32 offset :3;
84 volatile u32 reserved0 :4;
85 volatile u32 NFB :22;
wdenkac6dbb82003-03-26 11:42:53 +000086 }field;
87
88 volatile u32 word;
89 }params;
90
91 volatile u32 nextRxDescPtr;
92
93 volatile u32 RxDataPtr;
94
wdenke0ac62d2003-08-17 18:55:18 +000095 union {
96 struct {
wdenk79d696f2004-03-11 22:46:36 +000097 volatile u32 C :1;
98 volatile u32 Sop :1;
99 volatile u32 Eop :1;
100 volatile u32 reserved3 :12;
101 volatile u32 NBT :17;
wdenkac6dbb82003-03-26 11:42:53 +0000102 }field;
103
104 volatile u32 word;
105 }status;
106
107} inca_rx_descriptor_t;
108
109
110typedef struct
111{
wdenke0ac62d2003-08-17 18:55:18 +0000112 union {
113 struct {
wdenk79d696f2004-03-11 22:46:36 +0000114 volatile u32 HOLD :1;
115 volatile u32 Eop :1;
116 volatile u32 Sop :1;
117 volatile u32 ICpt :1;
118 volatile u32 IEop :1;
119 volatile u32 reserved0 :5;
120 volatile u32 NBA :22;
wdenkac6dbb82003-03-26 11:42:53 +0000121 }field;
122
123 volatile u32 word;
124 }params;
125
126 volatile u32 nextTxDescPtr;
127
128 volatile u32 TxDataPtr;
129
wdenk79d696f2004-03-11 22:46:36 +0000130 volatile u32 C :1;
131 volatile u32 reserved3 :31;
wdenkac6dbb82003-03-26 11:42:53 +0000132
133} inca_tx_descriptor_t;
134
135
136static inca_rx_descriptor_t rx_ring[NUM_RX_DESC] __attribute__ ((aligned(16)));
137static inca_tx_descriptor_t tx_ring[NUM_TX_DESC] __attribute__ ((aligned(16)));
138
139static int tx_new, rx_new, tx_hold, rx_hold;
140static int tx_old_hold = -1;
wdenk79d696f2004-03-11 22:46:36 +0000141static int initialized = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000142
143
144static int inca_switch_init(struct eth_device *dev, bd_t * bis);
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000145static int inca_switch_send(struct eth_device *dev, void *packet, int length);
wdenkac6dbb82003-03-26 11:42:53 +0000146static int inca_switch_recv(struct eth_device *dev);
147static void inca_switch_halt(struct eth_device *dev);
148static void inca_init_switch_chip(void);
149static void inca_dma_init(void);
wdenkcf56e112004-02-20 22:02:48 +0000150static int inca_amdix(void);
wdenkac6dbb82003-03-26 11:42:53 +0000151
152
wdenkac6dbb82003-03-26 11:42:53 +0000153int inca_switch_initialize(bd_t * bis)
154{
155 struct eth_device *dev;
156
157#if 0
158 printf("Entered inca_switch_initialize()\n");
159#endif
160
wdenke0ac62d2003-08-17 18:55:18 +0000161 if (!(dev = (struct eth_device *) malloc (sizeof *dev))) {
wdenkac6dbb82003-03-26 11:42:53 +0000162 printf("Failed to allocate memory\n");
163 return 0;
164 }
165 memset(dev, 0, sizeof(*dev));
166
167 inca_dma_init();
168
169 inca_init_switch_chip();
wdenk3c74e322004-02-22 23:46:08 +0000170
wdenk0c852a22004-02-26 23:01:04 +0000171#if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
wdenkcf56e112004-02-20 22:02:48 +0000172 inca_amdix();
wdenk0c852a22004-02-26 23:01:04 +0000173#endif
wdenkac6dbb82003-03-26 11:42:53 +0000174
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;
180
181 eth_register(dev);
182
183#if 0
184 printf("Leaving inca_switch_initialize()\n");
185#endif
186
Ben Warren8218bd22008-08-31 10:16:59 -0700187 return 0;
wdenkac6dbb82003-03-26 11:42:53 +0000188}
189
190
191static int inca_switch_init(struct eth_device *dev, bd_t * bis)
192{
193 int i;
194 u32 v, regValue;
195 u16 wTmp;
196
197#if 0
198 printf("Entering inca_switch_init()\n");
199#endif
200
wdenke0ac62d2003-08-17 18:55:18 +0000201 /* Set MAC address.
202 */
wdenkac6dbb82003-03-26 11:42:53 +0000203 wTmp = (u16)dev->enetaddr[0];
204 regValue = (wTmp << 8) | dev->enetaddr[1];
205
206 SW_WRITE_REG(INCA_IP_Switch_PMAC_SA1, regValue);
207
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];
213
214 SW_WRITE_REG(INCA_IP_Switch_PMAC_SA2, regValue);
215
wdenke0ac62d2003-08-17 18:55:18 +0000216 /* Initialize the descriptor rings.
217 */
wdenkf8d813e2004-03-02 14:05:39 +0000218 for (i = 0; i < NUM_RX_DESC; i++) {
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900219 inca_rx_descriptor_t * rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[i]);
wdenkac6dbb82003-03-26 11:42:53 +0000220 memset(rx_desc, 0, sizeof(rx_ring[i]));
221
wdenke0ac62d2003-08-17 18:55:18 +0000222 /* Set maximum size of receive buffer.
223 */
wdenkac6dbb82003-03-26 11:42:53 +0000224 rx_desc->params.field.NFB = PKTSIZE_ALIGN;
225
wdenke0ac62d2003-08-17 18:55:18 +0000226 /* Set the offset of the receive buffer. Zero means
227 * that the offset mechanism is not used.
228 */
wdenkac6dbb82003-03-26 11:42:53 +0000229 rx_desc->params.field.offset = 0;
230
231 /* Check if it is the last descriptor.
232 */
wdenke0ac62d2003-08-17 18:55:18 +0000233 if (i == (NUM_RX_DESC - 1)) {
234 /* Let the last descriptor point to the first
235 * one.
236 */
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900237 rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(rx_ring);
wdenke0ac62d2003-08-17 18:55:18 +0000238 } else {
239 /* Set the address of the next descriptor.
240 */
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900241 rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(&rx_ring[i+1]);
wdenkac6dbb82003-03-26 11:42:53 +0000242 }
243
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900244 rx_desc->RxDataPtr = (u32)CKSEG1ADDR(NetRxPackets[i]);
wdenkac6dbb82003-03-26 11:42:53 +0000245 }
246
247#if 0
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]);
250#endif
251
wdenke0ac62d2003-08-17 18:55:18 +0000252 for (i = 0; i < NUM_TX_DESC; i++) {
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900253 inca_tx_descriptor_t * tx_desc = (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[i]);
wdenkac6dbb82003-03-26 11:42:53 +0000254
255 memset(tx_desc, 0, sizeof(tx_ring[i]));
256
wdenk79d696f2004-03-11 22:46:36 +0000257 tx_desc->params.word = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000258 tx_desc->params.field.HOLD = 1;
wdenk79d696f2004-03-11 22:46:36 +0000259 tx_desc->C = 1;
wdenkac6dbb82003-03-26 11:42:53 +0000260
261 /* Check if it is the last descriptor.
262 */
wdenke0ac62d2003-08-17 18:55:18 +0000263 if (i == (NUM_TX_DESC - 1)) {
wdenkac6dbb82003-03-26 11:42:53 +0000264 /* Let the last descriptor point to the
265 * first one.
266 */
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900267 tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(tx_ring);
wdenke0ac62d2003-08-17 18:55:18 +0000268 } else {
wdenkac6dbb82003-03-26 11:42:53 +0000269 /* Set the address of the next descriptor.
270 */
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900271 tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(&tx_ring[i+1]);
wdenkac6dbb82003-03-26 11:42:53 +0000272 }
273 }
274
wdenke0ac62d2003-08-17 18:55:18 +0000275 /* Initialize RxDMA.
276 */
wdenkac6dbb82003-03-26 11:42:53 +0000277 DMA_READ_REG(INCA_IP_DMA_DMA_RXISR, v);
Wolfgang Denkd82a27b2011-12-08 02:01:51 +0000278 debug("RX status = 0x%08X\n", v);
wdenkac6dbb82003-03-26 11:42:53 +0000279
wdenke0ac62d2003-08-17 18:55:18 +0000280 /* Writing to the FRDA of CHANNEL.
281 */
wdenkac6dbb82003-03-26 11:42:53 +0000282 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXFRDA0, (u32)rx_ring);
283
wdenke0ac62d2003-08-17 18:55:18 +0000284 /* Writing to the COMMAND REG.
285 */
wdenk79d696f2004-03-11 22:46:36 +0000286 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_INIT);
wdenkac6dbb82003-03-26 11:42:53 +0000287
wdenke0ac62d2003-08-17 18:55:18 +0000288 /* Initialize TxDMA.
289 */
wdenkac6dbb82003-03-26 11:42:53 +0000290 DMA_READ_REG(INCA_IP_DMA_DMA_TXISR, v);
Wolfgang Denkd82a27b2011-12-08 02:01:51 +0000291 debug("TX status = 0x%08X\n", v);
wdenkac6dbb82003-03-26 11:42:53 +0000292
wdenke0ac62d2003-08-17 18:55:18 +0000293 /* Writing to the FRDA of CHANNEL.
294 */
wdenkac6dbb82003-03-26 11:42:53 +0000295 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXFRDA0, (u32)tx_ring);
296
297 tx_new = rx_new = 0;
298
299 tx_hold = NUM_TX_DESC - 1;
300 rx_hold = NUM_RX_DESC - 1;
301
302#if 0
303 rx_ring[rx_hold].params.field.HOLD = 1;
304#endif
wdenke0ac62d2003-08-17 18:55:18 +0000305 /* enable spanning tree forwarding, enable the CPU port */
306 /* ST_PT:
wdenk79d696f2004-03-11 22:46:36 +0000307 * CPS (CPU port status) 0x3 (forwarding)
308 * LPS (LAN port status) 0x3 (forwarding)
309 * PPS (PC port status) 0x3 (forwarding)
wdenke0ac62d2003-08-17 18:55:18 +0000310 */
wdenkac6dbb82003-03-26 11:42:53 +0000311 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0x3f);
312
313#if 0
314 printf("Leaving inca_switch_init()\n");
315#endif
316
317 return 0;
318}
319
320
Joe Hershberger10cbe3b2012-05-22 18:36:19 +0000321static int inca_switch_send(struct eth_device *dev, void *packet, int length)
wdenkac6dbb82003-03-26 11:42:53 +0000322{
wdenk79d696f2004-03-11 22:46:36 +0000323 int i;
324 int res = -1;
325 u32 command;
326 u32 regValue;
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900327 inca_tx_descriptor_t * tx_desc = (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_new]);
wdenkac6dbb82003-03-26 11:42:53 +0000328
329#if 0
330 printf("Entered inca_switch_send()\n");
331#endif
332
wdenke0ac62d2003-08-17 18:55:18 +0000333 if (length <= 0) {
wdenkac6dbb82003-03-26 11:42:53 +0000334 printf ("%s: bad packet size: %d\n", dev->name, length);
335 goto Done;
336 }
wdenk8bde7f72003-06-27 21:31:46 +0000337
wdenke0ac62d2003-08-17 18:55:18 +0000338 for(i = 0; tx_desc->C == 0; i++) {
339 if (i >= TOUT_LOOP) {
wdenkac6dbb82003-03-26 11:42:53 +0000340 printf("%s: tx error buffer not ready\n", dev->name);
341 goto Done;
342 }
343 }
344
wdenke0ac62d2003-08-17 18:55:18 +0000345 if (tx_old_hold >= 0) {
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900346 ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_old_hold]))->params.field.HOLD = 1;
wdenkac6dbb82003-03-26 11:42:53 +0000347 }
348 tx_old_hold = tx_hold;
349
350 tx_desc->params.word =
wdenk8bde7f72003-06-27 21:31:46 +0000351 (INCA_DMA_TX_SOP | INCA_DMA_TX_EOP | INCA_DMA_TX_HOLD);
wdenkac6dbb82003-03-26 11:42:53 +0000352
353 tx_desc->C = 0;
354 tx_desc->TxDataPtr = (u32)packet;
355 tx_desc->params.field.NBA = length;
356
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900357 ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->params.field.HOLD = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000358
359 tx_hold = tx_new;
wdenk79d696f2004-03-11 22:46:36 +0000360 tx_new = (tx_new + 1) % NUM_TX_DESC;
wdenkac6dbb82003-03-26 11:42:53 +0000361
362
wdenke0ac62d2003-08-17 18:55:18 +0000363 if (! initialized) {
wdenkac6dbb82003-03-26 11:42:53 +0000364 command = INCA_IP_DMA_DMA_TXCCR0_INIT;
365 initialized = 1;
wdenke0ac62d2003-08-17 18:55:18 +0000366 } else {
wdenkac6dbb82003-03-26 11:42:53 +0000367 command = INCA_IP_DMA_DMA_TXCCR0_HR;
368 }
wdenk8bde7f72003-06-27 21:31:46 +0000369
wdenkac6dbb82003-03-26 11:42:53 +0000370 DMA_READ_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
371 regValue |= command;
372#if 0
373 printf("regValue = 0x%x\n", regValue);
374#endif
375 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
376
377#if 1
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900378 for(i = 0; ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->C == 0; i++) {
wdenke0ac62d2003-08-17 18:55:18 +0000379 if (i >= TOUT_LOOP) {
wdenkac6dbb82003-03-26 11:42:53 +0000380 printf("%s: tx buffer not ready\n", dev->name);
381 goto Done;
382 }
383 }
384#endif
385 res = length;
386Done:
387#if 0
388 printf("Leaving inca_switch_send()\n");
389#endif
390 return res;
391}
392
393
394static int inca_switch_recv(struct eth_device *dev)
395{
wdenk79d696f2004-03-11 22:46:36 +0000396 int length = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000397 inca_rx_descriptor_t * rx_desc;
398
399#if 0
400 printf("Entered inca_switch_recv()\n");
401#endif
402
wdenke0ac62d2003-08-17 18:55:18 +0000403 for (;;) {
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900404 rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_new]);
wdenkac6dbb82003-03-26 11:42:53 +0000405
wdenke0ac62d2003-08-17 18:55:18 +0000406 if (rx_desc->status.field.C == 0) {
wdenkac6dbb82003-03-26 11:42:53 +0000407 break;
408 }
409
410#if 0
411 rx_ring[rx_new].params.field.HOLD = 1;
412#endif
413
wdenke0ac62d2003-08-17 18:55:18 +0000414 if (! rx_desc->status.field.Eop) {
wdenkac6dbb82003-03-26 11:42:53 +0000415 printf("Partly received packet!!!\n");
416 break;
417 }
418
419 length = rx_desc->status.field.NBT;
420 rx_desc->status.word &=
wdenk8bde7f72003-06-27 21:31:46 +0000421 ~(INCA_DMA_RX_EOP | INCA_DMA_RX_SOP | INCA_DMA_RX_C);
wdenkac6dbb82003-03-26 11:42:53 +0000422#if 0
423{
424 int i;
425 for (i=0;i<length - 4;i++) {
426 if (i % 16 == 0) printf("\n%04x: ", i);
427 printf("%02X ", NetRxPackets[rx_new][i]);
428 }
429 printf("\n");
430}
431#endif
432
wdenke0ac62d2003-08-17 18:55:18 +0000433 if (length) {
wdenkac6dbb82003-03-26 11:42:53 +0000434#if 0
435 printf("Received %d bytes\n", length);
436#endif
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900437 NetReceive((void*)CKSEG1ADDR(NetRxPackets[rx_new]), length - 4);
wdenke0ac62d2003-08-17 18:55:18 +0000438 } else {
wdenkac6dbb82003-03-26 11:42:53 +0000439#if 1
440 printf("Zero length!!!\n");
441#endif
442 }
443
444
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900445 ((inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_hold]))->params.field.HOLD = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000446
447 rx_hold = rx_new;
448
449 rx_new = (rx_new + 1) % NUM_RX_DESC;
450 }
451
452#if 0
453 printf("Leaving inca_switch_recv()\n");
454#endif
455
456 return length;
457}
458
459
460static void inca_switch_halt(struct eth_device *dev)
461{
462#if 0
463 printf("Entered inca_switch_halt()\n");
464#endif
465
466#if 1
467 initialized = 0;
468#endif
469#if 1
wdenke0ac62d2003-08-17 18:55:18 +0000470 /* Disable forwarding to the CPU port.
471 */
wdenkac6dbb82003-03-26 11:42:53 +0000472 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
473
wdenke0ac62d2003-08-17 18:55:18 +0000474 /* Close RxDMA channel.
475 */
wdenkac6dbb82003-03-26 11:42:53 +0000476 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
477
wdenke0ac62d2003-08-17 18:55:18 +0000478 /* Close TxDMA channel.
479 */
wdenkac6dbb82003-03-26 11:42:53 +0000480 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_TXCCR0_OFF);
481
482
483#endif
484#if 0
485 printf("Leaving inca_switch_halt()\n");
486#endif
487}
488
489
490static void inca_init_switch_chip(void)
491{
492 u32 regValue;
493
wdenke0ac62d2003-08-17 18:55:18 +0000494 /* To workaround a problem with collision counter
495 * (see Errata sheet).
496 */
wdenkac6dbb82003-03-26 11:42:53 +0000497 SW_WRITE_REG(INCA_IP_Switch_PC_TX_CTL, 0x00000001);
498 SW_WRITE_REG(INCA_IP_Switch_LAN_TX_CTL, 0x00000001);
499
500#if 1
wdenke0ac62d2003-08-17 18:55:18 +0000501 /* init MDIO configuration:
wdenk79d696f2004-03-11 22:46:36 +0000502 * MDS (Poll speed): 0x01 (4ms)
503 * PHY_LAN_ADDR: 0x06
504 * PHY_PC_ADDR: 0x05
wdenke0ac62d2003-08-17 18:55:18 +0000505 * UEP (Use External PHY): 0x00 (Internal PHY is used)
wdenk79d696f2004-03-11 22:46:36 +0000506 * 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)
wdenke0ac62d2003-08-17 18:55:18 +0000509 */
wdenkac6dbb82003-03-26 11:42:53 +0000510 SW_WRITE_REG(INCA_IP_Switch_MDIO_CFG, 0x4c50);
511
wdenke0ac62d2003-08-17 18:55:18 +0000512 /* init PHY:
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)
521 */
wdenkac6dbb82003-03-26 11:42:53 +0000522 SW_WRITE_REG (INCA_IP_Switch_EPHY, 0xff);
523
wdenke0ac62d2003-08-17 18:55:18 +0000524 /* MDIO_ACC:
525 * RA (Request/Ack) 0x01 (Request)
wdenk79d696f2004-03-11 22:46:36 +0000526 * RW (Read/Write) 0x01 (Write)
527 * PHY_ADDR 0x05 (PC)
528 * REG_ADDR 0x00 (PHY_BCR: basic control register)
529 * PHY_DATA 0x8000
530 * Reset - software reset
531 * LB (loop back) - normal
532 * SS (speed select) - 10 Mbit/s
wdenke0ac62d2003-08-17 18:55:18 +0000533 * ANE (auto neg. enable) - enable
wdenk79d696f2004-03-11 22:46:36 +0000534 * PD (power down) - normal
535 * ISO (isolate) - normal
wdenke0ac62d2003-08-17 18:55:18 +0000536 * RAN (restart auto neg.) - normal
wdenk79d696f2004-03-11 22:46:36 +0000537 * DM (duplex mode) - half duplex
wdenke0ac62d2003-08-17 18:55:18 +0000538 * CT (collision test) - enable
539 */
540 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0a09000);
wdenkac6dbb82003-03-26 11:42:53 +0000541
wdenke0ac62d2003-08-17 18:55:18 +0000542 /* MDIO_ACC:
543 * RA (Request/Ack) 0x01 (Request)
wdenk79d696f2004-03-11 22:46:36 +0000544 * RW (Read/Write) 0x01 (Write)
545 * PHY_ADDR 0x06 (LAN)
546 * REG_ADDR 0x00 (PHY_BCR: basic control register)
547 * PHY_DATA 0x8000
548 * Reset - software reset
549 * LB (loop back) - normal
550 * SS (speed select) - 10 Mbit/s
wdenke0ac62d2003-08-17 18:55:18 +0000551 * ANE (auto neg. enable) - enable
wdenk79d696f2004-03-11 22:46:36 +0000552 * PD (power down) - normal
553 * ISO (isolate) - normal
wdenke0ac62d2003-08-17 18:55:18 +0000554 * RAN (restart auto neg.) - normal
wdenk79d696f2004-03-11 22:46:36 +0000555 * DM (duplex mode) - half duplex
wdenke0ac62d2003-08-17 18:55:18 +0000556 * CT (collision test) - enable
557 */
wdenk79d696f2004-03-11 22:46:36 +0000558 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0c09000);
wdenke0ac62d2003-08-17 18:55:18 +0000559
wdenkac6dbb82003-03-26 11:42:53 +0000560#endif
561
wdenke0ac62d2003-08-17 18:55:18 +0000562 /* 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.
565 */
wdenkac6dbb82003-03-26 11:42:53 +0000566 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
567
568 SW_READ_REG(INCA_IP_Switch_ARL_CTL, regValue);
569
wdenke0ac62d2003-08-17 18:55:18 +0000570 /* CRC GEN is enabled.
571 */
wdenkac6dbb82003-03-26 11:42:53 +0000572 regValue |= 0x00000200;
573 SW_WRITE_REG(INCA_IP_Switch_ARL_CTL, regValue);
574
wdenke0ac62d2003-08-17 18:55:18 +0000575 /* ADD TAG is disabled.
576 */
wdenkac6dbb82003-03-26 11:42:53 +0000577 SW_READ_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
578 regValue &= ~0x00000002;
579 SW_WRITE_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
580}
581
582
583static void inca_dma_init(void)
584{
wdenke0ac62d2003-08-17 18:55:18 +0000585 /* Switch off all DMA channels.
586 */
wdenkac6dbb82003-03-26 11:42:53 +0000587 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);
589
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);
593
wdenke0ac62d2003-08-17 18:55:18 +0000594 /* Setup TX channel polling time.
595 */
wdenkac6dbb82003-03-26 11:42:53 +0000596 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXPOLL, INCA_DMA_TX_POLLING_TIME);
597
wdenke0ac62d2003-08-17 18:55:18 +0000598 /* Setup RX channel polling time.
599 */
wdenkac6dbb82003-03-26 11:42:53 +0000600 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXPOLL, INCA_DMA_RX_POLLING_TIME);
601
wdenke0ac62d2003-08-17 18:55:18 +0000602 /* ERRATA: write reset value into the DMA RX IMR register.
603 */
wdenkac6dbb82003-03-26 11:42:53 +0000604 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXIMR, 0xFFFFFFFF);
605
wdenke0ac62d2003-08-17 18:55:18 +0000606 /* Just in case: disable all transmit interrupts also.
607 */
wdenkac6dbb82003-03-26 11:42:53 +0000608 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXIMR, 0xFFFFFFFF);
609
610 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXISR, 0xFFFFFFFF);
611 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXISR, 0xFFFFFFFF);
612}
613
wdenk0c852a22004-02-26 23:01:04 +0000614#if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
wdenkcf56e112004-02-20 22:02:48 +0000615static int inca_amdix(void)
616{
wdenkf8d813e2004-03-02 14:05:39 +0000617 u32 phyReg1 = 0;
618 u32 phyReg4 = 0;
619 u32 phyReg5 = 0;
620 u32 phyReg6 = 0;
621 u32 phyReg31 = 0;
622 u32 regEphy = 0;
wdenkcf56e112004-02-20 22:02:48 +0000623 int mdi_flag;
624 int retries;
625
626 /* Setup GPIO pins.
627 */
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);
630
wdenkf8d813e2004-03-02 14:05:39 +0000631#if 0
wdenkcf56e112004-02-20 22:02:48 +0000632 /* Wait for signal.
633 */
634 retries = WAIT_SIGNAL_RETRIES;
wdenkf8d813e2004-03-02 14:05:39 +0000635 while (--retries) {
wdenkcf56e112004-02-20 22:02:48 +0000636 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 */
wdenkf8d813e2004-03-02 14:05:39 +0000641 do {
642 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
643 } while (phyReg1 & (1 << 31));
wdenkcf56e112004-02-20 22:02:48 +0000644
wdenkf8d813e2004-03-02 14:05:39 +0000645 if (phyReg1 & (1 << 1)) {
wdenkcf56e112004-02-20 22:02:48 +0000646 /* Signal detected */
647 break;
648 }
649 }
650
651 if (!retries)
wdenkf8d813e2004-03-02 14:05:39 +0000652 goto Fail;
653#endif
wdenkcf56e112004-02-20 22:02:48 +0000654
655 /* Set MDI mode.
656 */
657 *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
658 mdi_flag = 1;
659
660 /* Wait for link.
661 */
662 retries = WAIT_LINK_RETRIES;
wdenkf8d813e2004-03-02 14:05:39 +0000663 while (--retries) {
wdenkcf56e112004-02-20 22:02:48 +0000664 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 */
wdenkf8d813e2004-03-02 14:05:39 +0000670 do {
671 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
672 } while (phyReg1 & (1 << 31));
wdenkcf56e112004-02-20 22:02:48 +0000673
wdenkf8d813e2004-03-02 14:05:39 +0000674 if (phyReg1 & (1 << 2)) {
wdenkcf56e112004-02-20 22:02:48 +0000675 /* Link is up */
676 break;
wdenkf8d813e2004-03-02 14:05:39 +0000677 } else if (mdi_flag) {
wdenkcf56e112004-02-20 22:02:48 +0000678 /* Set MDIX mode */
679 *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
680 mdi_flag = 0;
wdenkf8d813e2004-03-02 14:05:39 +0000681 } else {
wdenkcf56e112004-02-20 22:02:48 +0000682 /* Set MDI mode */
683 *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
684 mdi_flag = 1;
685 }
686 }
687
wdenkf8d813e2004-03-02 14:05:39 +0000688 if (!retries) {
689 goto Fail;
690 } else {
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 */
696 do {
697 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
698 } while (phyReg1 & (1 << 31));
699
700 /* Auto-negotiation / Parallel detection complete
701 */
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 */
708 do {
wdenk79d696f2004-03-11 22:46:36 +0000709 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg31);
wdenkf8d813e2004-03-02 14:05:39 +0000710 } while (phyReg31 & (1 << 31));
711
712 switch ((phyReg31 >> 2) & 0x7) {
713 case INCA_SWITCH_PHY_SPEED_10H:
714 /* 10Base-T Half-duplex */
715 regEphy = 0;
716 break;
717 case INCA_SWITCH_PHY_SPEED_10F:
718 /* 10Base-T Full-duplex */
719 regEphy = INCA_IP_Switch_EPHY_DL;
720 break;
721 case INCA_SWITCH_PHY_SPEED_100H:
722 /* 100Base-TX Half-duplex */
723 regEphy = INCA_IP_Switch_EPHY_SL;
724 break;
725 case INCA_SWITCH_PHY_SPEED_100F:
726 /* 100Base-TX Full-duplex */
727 regEphy = INCA_IP_Switch_EPHY_SL | INCA_IP_Switch_EPHY_DL;
728 break;
729 }
730
731 /* In case of Auto-negotiation,
732 * update the negotiated PAUSE support status
733 */
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 */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500739 (6 << 16)); /* MII_EXPANSION */
wdenkf8d813e2004-03-02 14:05:39 +0000740 do {
wdenk79d696f2004-03-11 22:46:36 +0000741 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg6);
wdenkf8d813e2004-03-02 14:05:39 +0000742 } while (phyReg6 & (1 << 31));
743
744 /* We are Autoneg-able.
745 * Is Link partner also able to autoneg?
746 */
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 */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500752 (4 << 16)); /* MII_ADVERTISE */
wdenkf8d813e2004-03-02 14:05:39 +0000753 do {
wdenk79d696f2004-03-11 22:46:36 +0000754 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg4);
wdenkf8d813e2004-03-02 14:05:39 +0000755 } while (phyReg4 & (1 << 31));
756
757 /* We advertise PAUSE capab.
758 * Does link partner also advertise it?
759 */
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 */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500765 (5 << 16)); /* MII_LPA */
wdenkf8d813e2004-03-02 14:05:39 +0000766 do {
wdenk79d696f2004-03-11 22:46:36 +0000767 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg5);
wdenkf8d813e2004-03-02 14:05:39 +0000768 } while (phyReg5 & (1 << 31));
769
770 /* Link partner is PAUSE capab.
771 */
772 if (phyReg5 & (1 << 10)) {
773 regEphy |= INCA_IP_Switch_EPHY_PL;
774 }
775 }
776 }
777
778 }
779
780 /* Link is up */
781 regEphy |= INCA_IP_Switch_EPHY_LL;
782
783 SW_WRITE_REG(INCA_IP_Switch_EPHY, regEphy);
784 }
785 }
wdenkcf56e112004-02-20 22:02:48 +0000786
787 return 0;
wdenkf8d813e2004-03-02 14:05:39 +0000788
789Fail:
790 printf("No Link on LAN port\n");
791 return -1;
wdenkcf56e112004-02-20 22:02:48 +0000792}
wdenk0c852a22004-02-26 23:01:04 +0000793#endif /* CONFIG_INCA_IP_SWITCH_AMDIX */