blob: cdfbfa67d89d32d0e53e804e66a79a28ba74aa57 [file] [log] [blame]
wdenk1adff3d2003-03-26 11:42:53 +00001/*
2 * INCA-IP internal switch ethernet driver.
3 *
wdenk0e2874cb2004-03-02 14:05:39 +00004 * (C) Copyright 2003-2004
wdenk1adff3d2003-03-26 11:42:53 +00005 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
wdenk1adff3d2003-03-26 11:42:53 +00008 */
9
10
11#include <common.h>
12
wdenk1adff3d2003-03-26 11:42:53 +000013#include <malloc.h>
14#include <net.h>
Ben Warrend2358c02008-08-31 10:16:59 -070015#include <netdev.h>
wdenk1adff3d2003-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)
wdenk7f1447f2004-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}
wdenk1adff3d2003-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) \
wdenk7f1447f2004-06-09 00:10:59 +000038 SWORD(reg, value);\
wdenk0a12b752004-03-11 22:46:36 +000039 DELAY;\
wdenk7f1447f2004-06-09 00:10:59 +000040 SWORD(reg, value);
wdenk1adff3d2003-03-26 11:42:53 +000041
wdenk0a12b752004-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);
wdenk1adff3d2003-03-26 11:42:53 +000046
wdenk0a12b752004-03-11 22:46:36 +000047#define INCA_DMA_TX_POLLING_TIME 0x07
48#define INCA_DMA_RX_POLLING_TIME 0x07
wdenk1adff3d2003-03-26 11:42:53 +000049
wdenk0a12b752004-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
wdenk1adff3d2003-03-26 11:42:53 +000055
wdenk0a12b752004-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
wdenk1adff3d2003-03-26 11:42:53 +000059
wdenk0e2874cb2004-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
wdenk4dd56e52004-02-20 22:02:48 +000065/************************ Auto MDIX settings ************************/
wdenk0a12b752004-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
wdenk4dd56e52004-02-20 22:02:48 +000070
wdenk0a12b752004-03-11 22:46:36 +000071#define WAIT_SIGNAL_RETRIES 100
72#define WAIT_LINK_RETRIES 100
73#define LINK_RETRY_DELAY 2000 /* ms */
wdenk4dd56e52004-02-20 22:02:48 +000074/********************************************************************/
wdenk1adff3d2003-03-26 11:42:53 +000075
76typedef struct
77{
wdenk5d841732003-08-17 18:55:18 +000078 union {
79 struct {
wdenk0a12b752004-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;
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +000095 union {
96 struct {
wdenk0a12b752004-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;
wdenk1adff3d2003-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{
wdenk5d841732003-08-17 18:55:18 +0000112 union {
113 struct {
wdenk0a12b752004-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;
wdenk1adff3d2003-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
wdenk0a12b752004-03-11 22:46:36 +0000130 volatile u32 C :1;
131 volatile u32 reserved3 :31;
wdenk1adff3d2003-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;
wdenk0a12b752004-03-11 22:46:36 +0000141static int initialized = 0;
wdenk1adff3d2003-03-26 11:42:53 +0000142
143
144static int inca_switch_init(struct eth_device *dev, bd_t * bis);
Joe Hershbergere4e04882012-05-22 18:36:19 +0000145static int inca_switch_send(struct eth_device *dev, void *packet, int length);
wdenk1adff3d2003-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);
wdenk4dd56e52004-02-20 22:02:48 +0000150static int inca_amdix(void);
wdenk1adff3d2003-03-26 11:42:53 +0000151
152
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000161 if (!(dev = (struct eth_device *) malloc (sizeof *dev))) {
wdenk1adff3d2003-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();
wdenk56ed43e2004-02-22 23:46:08 +0000170
wdenkdb82c8e2004-02-26 23:01:04 +0000171#if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
wdenk4dd56e52004-02-20 22:02:48 +0000172 inca_amdix();
wdenkdb82c8e2004-02-26 23:01:04 +0000173#endif
wdenk1adff3d2003-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 Warrend2358c02008-08-31 10:16:59 -0700187 return 0;
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000201 /* Set MAC address.
202 */
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000216 /* Initialize the descriptor rings.
217 */
wdenk0e2874cb2004-03-02 14:05:39 +0000218 for (i = 0; i < NUM_RX_DESC; i++) {
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900219 inca_rx_descriptor_t * rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[i]);
wdenk1adff3d2003-03-26 11:42:53 +0000220 memset(rx_desc, 0, sizeof(rx_ring[i]));
221
wdenk5d841732003-08-17 18:55:18 +0000222 /* Set maximum size of receive buffer.
223 */
wdenk1adff3d2003-03-26 11:42:53 +0000224 rx_desc->params.field.NFB = PKTSIZE_ALIGN;
225
wdenk5d841732003-08-17 18:55:18 +0000226 /* Set the offset of the receive buffer. Zero means
227 * that the offset mechanism is not used.
228 */
wdenk1adff3d2003-03-26 11:42:53 +0000229 rx_desc->params.field.offset = 0;
230
231 /* Check if it is the last descriptor.
232 */
wdenk5d841732003-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 Kuribayashi6c6b2612008-06-05 22:29:00 +0900237 rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(rx_ring);
wdenk5d841732003-08-17 18:55:18 +0000238 } else {
239 /* Set the address of the next descriptor.
240 */
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900241 rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(&rx_ring[i+1]);
wdenk1adff3d2003-03-26 11:42:53 +0000242 }
243
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900244 rx_desc->RxDataPtr = (u32)CKSEG1ADDR(NetRxPackets[i]);
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000252 for (i = 0; i < NUM_TX_DESC; i++) {
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900253 inca_tx_descriptor_t * tx_desc = (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[i]);
wdenk1adff3d2003-03-26 11:42:53 +0000254
255 memset(tx_desc, 0, sizeof(tx_ring[i]));
256
wdenk0a12b752004-03-11 22:46:36 +0000257 tx_desc->params.word = 0;
wdenk1adff3d2003-03-26 11:42:53 +0000258 tx_desc->params.field.HOLD = 1;
wdenk0a12b752004-03-11 22:46:36 +0000259 tx_desc->C = 1;
wdenk1adff3d2003-03-26 11:42:53 +0000260
261 /* Check if it is the last descriptor.
262 */
wdenk5d841732003-08-17 18:55:18 +0000263 if (i == (NUM_TX_DESC - 1)) {
wdenk1adff3d2003-03-26 11:42:53 +0000264 /* Let the last descriptor point to the
265 * first one.
266 */
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900267 tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(tx_ring);
wdenk5d841732003-08-17 18:55:18 +0000268 } else {
wdenk1adff3d2003-03-26 11:42:53 +0000269 /* Set the address of the next descriptor.
270 */
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900271 tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(&tx_ring[i+1]);
wdenk1adff3d2003-03-26 11:42:53 +0000272 }
273 }
274
wdenk5d841732003-08-17 18:55:18 +0000275 /* Initialize RxDMA.
276 */
wdenk1adff3d2003-03-26 11:42:53 +0000277 DMA_READ_REG(INCA_IP_DMA_DMA_RXISR, v);
Wolfgang Denkcc5a47a2011-12-08 02:01:51 +0000278 debug("RX status = 0x%08X\n", v);
wdenk1adff3d2003-03-26 11:42:53 +0000279
wdenk5d841732003-08-17 18:55:18 +0000280 /* Writing to the FRDA of CHANNEL.
281 */
wdenk1adff3d2003-03-26 11:42:53 +0000282 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXFRDA0, (u32)rx_ring);
283
wdenk5d841732003-08-17 18:55:18 +0000284 /* Writing to the COMMAND REG.
285 */
wdenk0a12b752004-03-11 22:46:36 +0000286 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_INIT);
wdenk1adff3d2003-03-26 11:42:53 +0000287
wdenk5d841732003-08-17 18:55:18 +0000288 /* Initialize TxDMA.
289 */
wdenk1adff3d2003-03-26 11:42:53 +0000290 DMA_READ_REG(INCA_IP_DMA_DMA_TXISR, v);
Wolfgang Denkcc5a47a2011-12-08 02:01:51 +0000291 debug("TX status = 0x%08X\n", v);
wdenk1adff3d2003-03-26 11:42:53 +0000292
wdenk5d841732003-08-17 18:55:18 +0000293 /* Writing to the FRDA of CHANNEL.
294 */
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000305 /* enable spanning tree forwarding, enable the CPU port */
306 /* ST_PT:
wdenk0a12b752004-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)
wdenk5d841732003-08-17 18:55:18 +0000310 */
wdenk1adff3d2003-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 Hershbergere4e04882012-05-22 18:36:19 +0000321static int inca_switch_send(struct eth_device *dev, void *packet, int length)
wdenk1adff3d2003-03-26 11:42:53 +0000322{
wdenk0a12b752004-03-11 22:46:36 +0000323 int i;
324 int res = -1;
325 u32 command;
326 u32 regValue;
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900327 inca_tx_descriptor_t * tx_desc = (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_new]);
wdenk1adff3d2003-03-26 11:42:53 +0000328
329#if 0
330 printf("Entered inca_switch_send()\n");
331#endif
332
wdenk5d841732003-08-17 18:55:18 +0000333 if (length <= 0) {
wdenk1adff3d2003-03-26 11:42:53 +0000334 printf ("%s: bad packet size: %d\n", dev->name, length);
335 goto Done;
336 }
wdenk57b2d802003-06-27 21:31:46 +0000337
wdenk5d841732003-08-17 18:55:18 +0000338 for(i = 0; tx_desc->C == 0; i++) {
339 if (i >= TOUT_LOOP) {
wdenk1adff3d2003-03-26 11:42:53 +0000340 printf("%s: tx error buffer not ready\n", dev->name);
341 goto Done;
342 }
343 }
344
wdenk5d841732003-08-17 18:55:18 +0000345 if (tx_old_hold >= 0) {
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900346 ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_old_hold]))->params.field.HOLD = 1;
wdenk1adff3d2003-03-26 11:42:53 +0000347 }
348 tx_old_hold = tx_hold;
349
350 tx_desc->params.word =
wdenk57b2d802003-06-27 21:31:46 +0000351 (INCA_DMA_TX_SOP | INCA_DMA_TX_EOP | INCA_DMA_TX_HOLD);
wdenk1adff3d2003-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 Kuribayashi6c6b2612008-06-05 22:29:00 +0900357 ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->params.field.HOLD = 0;
wdenk1adff3d2003-03-26 11:42:53 +0000358
359 tx_hold = tx_new;
wdenk0a12b752004-03-11 22:46:36 +0000360 tx_new = (tx_new + 1) % NUM_TX_DESC;
wdenk1adff3d2003-03-26 11:42:53 +0000361
362
wdenk5d841732003-08-17 18:55:18 +0000363 if (! initialized) {
wdenk1adff3d2003-03-26 11:42:53 +0000364 command = INCA_IP_DMA_DMA_TXCCR0_INIT;
365 initialized = 1;
wdenk5d841732003-08-17 18:55:18 +0000366 } else {
wdenk1adff3d2003-03-26 11:42:53 +0000367 command = INCA_IP_DMA_DMA_TXCCR0_HR;
368 }
wdenk57b2d802003-06-27 21:31:46 +0000369
wdenk1adff3d2003-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 Kuribayashi6c6b2612008-06-05 22:29:00 +0900378 for(i = 0; ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->C == 0; i++) {
wdenk5d841732003-08-17 18:55:18 +0000379 if (i >= TOUT_LOOP) {
wdenk1adff3d2003-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{
wdenk0a12b752004-03-11 22:46:36 +0000396 int length = 0;
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000403 for (;;) {
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900404 rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_new]);
wdenk1adff3d2003-03-26 11:42:53 +0000405
wdenk5d841732003-08-17 18:55:18 +0000406 if (rx_desc->status.field.C == 0) {
wdenk1adff3d2003-03-26 11:42:53 +0000407 break;
408 }
409
410#if 0
411 rx_ring[rx_new].params.field.HOLD = 1;
412#endif
413
wdenk5d841732003-08-17 18:55:18 +0000414 if (! rx_desc->status.field.Eop) {
wdenk1adff3d2003-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 &=
wdenk57b2d802003-06-27 21:31:46 +0000421 ~(INCA_DMA_RX_EOP | INCA_DMA_RX_SOP | INCA_DMA_RX_C);
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000433 if (length) {
wdenk1adff3d2003-03-26 11:42:53 +0000434#if 0
435 printf("Received %d bytes\n", length);
436#endif
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900437 NetReceive((void*)CKSEG1ADDR(NetRxPackets[rx_new]), length - 4);
wdenk5d841732003-08-17 18:55:18 +0000438 } else {
wdenk1adff3d2003-03-26 11:42:53 +0000439#if 1
440 printf("Zero length!!!\n");
441#endif
442 }
443
444
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900445 ((inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_hold]))->params.field.HOLD = 0;
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000470 /* Disable forwarding to the CPU port.
471 */
wdenk1adff3d2003-03-26 11:42:53 +0000472 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
473
wdenk5d841732003-08-17 18:55:18 +0000474 /* Close RxDMA channel.
475 */
wdenk1adff3d2003-03-26 11:42:53 +0000476 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
477
wdenk5d841732003-08-17 18:55:18 +0000478 /* Close TxDMA channel.
479 */
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000494 /* To workaround a problem with collision counter
495 * (see Errata sheet).
496 */
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000501 /* init MDIO configuration:
wdenk0a12b752004-03-11 22:46:36 +0000502 * MDS (Poll speed): 0x01 (4ms)
503 * PHY_LAN_ADDR: 0x06
504 * PHY_PC_ADDR: 0x05
wdenk5d841732003-08-17 18:55:18 +0000505 * UEP (Use External PHY): 0x00 (Internal PHY is used)
wdenk0a12b752004-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)
wdenk5d841732003-08-17 18:55:18 +0000509 */
wdenk1adff3d2003-03-26 11:42:53 +0000510 SW_WRITE_REG(INCA_IP_Switch_MDIO_CFG, 0x4c50);
511
wdenk5d841732003-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 */
wdenk1adff3d2003-03-26 11:42:53 +0000522 SW_WRITE_REG (INCA_IP_Switch_EPHY, 0xff);
523
wdenk5d841732003-08-17 18:55:18 +0000524 /* MDIO_ACC:
525 * RA (Request/Ack) 0x01 (Request)
wdenk0a12b752004-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
wdenk5d841732003-08-17 18:55:18 +0000533 * ANE (auto neg. enable) - enable
wdenk0a12b752004-03-11 22:46:36 +0000534 * PD (power down) - normal
535 * ISO (isolate) - normal
wdenk5d841732003-08-17 18:55:18 +0000536 * RAN (restart auto neg.) - normal
wdenk0a12b752004-03-11 22:46:36 +0000537 * DM (duplex mode) - half duplex
wdenk5d841732003-08-17 18:55:18 +0000538 * CT (collision test) - enable
539 */
540 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0a09000);
541
542 /* MDIO_ACC:
543 * RA (Request/Ack) 0x01 (Request)
wdenk0a12b752004-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
wdenk5d841732003-08-17 18:55:18 +0000551 * ANE (auto neg. enable) - enable
wdenk0a12b752004-03-11 22:46:36 +0000552 * PD (power down) - normal
553 * ISO (isolate) - normal
wdenk5d841732003-08-17 18:55:18 +0000554 * RAN (restart auto neg.) - normal
wdenk0a12b752004-03-11 22:46:36 +0000555 * DM (duplex mode) - half duplex
wdenk5d841732003-08-17 18:55:18 +0000556 * CT (collision test) - enable
557 */
wdenk0a12b752004-03-11 22:46:36 +0000558 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0c09000);
wdenk1adff3d2003-03-26 11:42:53 +0000559
wdenk1adff3d2003-03-26 11:42:53 +0000560#endif
561
wdenk5d841732003-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 */
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000570 /* CRC GEN is enabled.
571 */
wdenk1adff3d2003-03-26 11:42:53 +0000572 regValue |= 0x00000200;
573 SW_WRITE_REG(INCA_IP_Switch_ARL_CTL, regValue);
574
wdenk5d841732003-08-17 18:55:18 +0000575 /* ADD TAG is disabled.
576 */
wdenk1adff3d2003-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{
wdenk5d841732003-08-17 18:55:18 +0000585 /* Switch off all DMA channels.
586 */
wdenk1adff3d2003-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
wdenk5d841732003-08-17 18:55:18 +0000594 /* Setup TX channel polling time.
595 */
wdenk1adff3d2003-03-26 11:42:53 +0000596 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXPOLL, INCA_DMA_TX_POLLING_TIME);
597
wdenk5d841732003-08-17 18:55:18 +0000598 /* Setup RX channel polling time.
599 */
wdenk1adff3d2003-03-26 11:42:53 +0000600 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXPOLL, INCA_DMA_RX_POLLING_TIME);
601
wdenk5d841732003-08-17 18:55:18 +0000602 /* ERRATA: write reset value into the DMA RX IMR register.
603 */
wdenk1adff3d2003-03-26 11:42:53 +0000604 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXIMR, 0xFFFFFFFF);
605
wdenk5d841732003-08-17 18:55:18 +0000606 /* Just in case: disable all transmit interrupts also.
607 */
wdenk1adff3d2003-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
wdenkdb82c8e2004-02-26 23:01:04 +0000614#if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
wdenk4dd56e52004-02-20 22:02:48 +0000615static int inca_amdix(void)
616{
wdenk0e2874cb2004-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;
wdenk4dd56e52004-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
wdenk0e2874cb2004-03-02 14:05:39 +0000631#if 0
wdenk4dd56e52004-02-20 22:02:48 +0000632 /* Wait for signal.
633 */
634 retries = WAIT_SIGNAL_RETRIES;
wdenk0e2874cb2004-03-02 14:05:39 +0000635 while (--retries) {
wdenk4dd56e52004-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 */
wdenk0e2874cb2004-03-02 14:05:39 +0000641 do {
642 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
643 } while (phyReg1 & (1 << 31));
wdenk4dd56e52004-02-20 22:02:48 +0000644
wdenk0e2874cb2004-03-02 14:05:39 +0000645 if (phyReg1 & (1 << 1)) {
wdenk4dd56e52004-02-20 22:02:48 +0000646 /* Signal detected */
647 break;
648 }
649 }
650
651 if (!retries)
wdenk0e2874cb2004-03-02 14:05:39 +0000652 goto Fail;
653#endif
wdenk4dd56e52004-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;
wdenk0e2874cb2004-03-02 14:05:39 +0000663 while (--retries) {
wdenk4dd56e52004-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 */
wdenk0e2874cb2004-03-02 14:05:39 +0000670 do {
671 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
672 } while (phyReg1 & (1 << 31));
wdenk4dd56e52004-02-20 22:02:48 +0000673
wdenk0e2874cb2004-03-02 14:05:39 +0000674 if (phyReg1 & (1 << 2)) {
wdenk4dd56e52004-02-20 22:02:48 +0000675 /* Link is up */
676 break;
wdenk0e2874cb2004-03-02 14:05:39 +0000677 } else if (mdi_flag) {
wdenk4dd56e52004-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;
wdenk0e2874cb2004-03-02 14:05:39 +0000681 } else {
wdenk4dd56e52004-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
wdenk0e2874cb2004-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 {
wdenk0a12b752004-03-11 22:46:36 +0000709 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg31);
wdenk0e2874cb2004-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 Frysingerd63ee712010-12-23 15:40:12 -0500739 (6 << 16)); /* MII_EXPANSION */
wdenk0e2874cb2004-03-02 14:05:39 +0000740 do {
wdenk0a12b752004-03-11 22:46:36 +0000741 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg6);
wdenk0e2874cb2004-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 Frysingerd63ee712010-12-23 15:40:12 -0500752 (4 << 16)); /* MII_ADVERTISE */
wdenk0e2874cb2004-03-02 14:05:39 +0000753 do {
wdenk0a12b752004-03-11 22:46:36 +0000754 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg4);
wdenk0e2874cb2004-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 Frysingerd63ee712010-12-23 15:40:12 -0500765 (5 << 16)); /* MII_LPA */
wdenk0e2874cb2004-03-02 14:05:39 +0000766 do {
wdenk0a12b752004-03-11 22:46:36 +0000767 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg5);
wdenk0e2874cb2004-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 }
wdenk4dd56e52004-02-20 22:02:48 +0000786
787 return 0;
wdenk0e2874cb2004-03-02 14:05:39 +0000788
789Fail:
790 printf("No Link on LAN port\n");
791 return -1;
wdenk4dd56e52004-02-20 22:02:48 +0000792}
wdenkdb82c8e2004-02-26 23:01:04 +0000793#endif /* CONFIG_INCA_IP_SWITCH_AMDIX */