blob: bd3360cad7c31b6de2e969969479aba5380af034 [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 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
wdenk79d696f2004-03-11 22:46:36 +000017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkac6dbb82003-03-26 11:42:53 +000018 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26
27#include <common.h>
28
wdenkac6dbb82003-03-26 11:42:53 +000029#include <malloc.h>
30#include <net.h>
Ben Warren8218bd22008-08-31 10:16:59 -070031#include <netdev.h>
wdenkac6dbb82003-03-26 11:42:53 +000032#include <asm/inca-ip.h>
33#include <asm/addrspace.h>
34
35
36#define NUM_RX_DESC PKTBUFSRX
37#define NUM_TX_DESC 3
38#define TOUT_LOOP 1000000
39
40
41#define DELAY udelay(10000)
wdenk356a0d92004-06-09 00:10:59 +000042 /* Sometimes the store word instruction hangs while writing to one
43 * of the Switch registers. Moving the instruction into a separate
44 * function somehow makes the problem go away.
45 */
46static void SWORD(volatile u32 * reg, u32 value)
47{
48 *reg = value;
49}
wdenkac6dbb82003-03-26 11:42:53 +000050
51#define DMA_WRITE_REG(reg, value) *((volatile u32 *)reg) = (u32)value;
52#define DMA_READ_REG(reg, value) value = (u32)*((volatile u32*)reg)
53#define SW_WRITE_REG(reg, value) \
wdenk356a0d92004-06-09 00:10:59 +000054 SWORD(reg, value);\
wdenk79d696f2004-03-11 22:46:36 +000055 DELAY;\
wdenk356a0d92004-06-09 00:10:59 +000056 SWORD(reg, value);
wdenkac6dbb82003-03-26 11:42:53 +000057
wdenk79d696f2004-03-11 22:46:36 +000058#define SW_READ_REG(reg, value) \
59 value = (u32)*((volatile u32*)reg);\
60 DELAY;\
61 value = (u32)*((volatile u32*)reg);
wdenkac6dbb82003-03-26 11:42:53 +000062
wdenk79d696f2004-03-11 22:46:36 +000063#define INCA_DMA_TX_POLLING_TIME 0x07
64#define INCA_DMA_RX_POLLING_TIME 0x07
wdenkac6dbb82003-03-26 11:42:53 +000065
wdenk79d696f2004-03-11 22:46:36 +000066#define INCA_DMA_TX_HOLD 0x80000000
67#define INCA_DMA_TX_EOP 0x40000000
68#define INCA_DMA_TX_SOP 0x20000000
69#define INCA_DMA_TX_ICPT 0x10000000
70#define INCA_DMA_TX_IEOP 0x08000000
wdenkac6dbb82003-03-26 11:42:53 +000071
wdenk79d696f2004-03-11 22:46:36 +000072#define INCA_DMA_RX_C 0x80000000
73#define INCA_DMA_RX_SOP 0x40000000
74#define INCA_DMA_RX_EOP 0x20000000
wdenkac6dbb82003-03-26 11:42:53 +000075
wdenkf8d813e2004-03-02 14:05:39 +000076#define INCA_SWITCH_PHY_SPEED_10H 0x1
77#define INCA_SWITCH_PHY_SPEED_10F 0x5
78#define INCA_SWITCH_PHY_SPEED_100H 0x2
79#define INCA_SWITCH_PHY_SPEED_100F 0x6
80
wdenkcf56e112004-02-20 22:02:48 +000081/************************ Auto MDIX settings ************************/
wdenk79d696f2004-03-11 22:46:36 +000082#define INCA_IP_AUTO_MDIX_LAN_PORTS_DIR INCA_IP_Ports_P1_DIR
83#define INCA_IP_AUTO_MDIX_LAN_PORTS_ALTSEL INCA_IP_Ports_P1_ALTSEL
84#define INCA_IP_AUTO_MDIX_LAN_PORTS_OUT INCA_IP_Ports_P1_OUT
85#define INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX 16
wdenkcf56e112004-02-20 22:02:48 +000086
wdenk79d696f2004-03-11 22:46:36 +000087#define WAIT_SIGNAL_RETRIES 100
88#define WAIT_LINK_RETRIES 100
89#define LINK_RETRY_DELAY 2000 /* ms */
wdenkcf56e112004-02-20 22:02:48 +000090/********************************************************************/
wdenkac6dbb82003-03-26 11:42:53 +000091
92typedef struct
93{
wdenke0ac62d2003-08-17 18:55:18 +000094 union {
95 struct {
wdenk79d696f2004-03-11 22:46:36 +000096 volatile u32 HOLD :1;
97 volatile u32 ICpt :1;
98 volatile u32 IEop :1;
99 volatile u32 offset :3;
100 volatile u32 reserved0 :4;
101 volatile u32 NFB :22;
wdenkac6dbb82003-03-26 11:42:53 +0000102 }field;
103
104 volatile u32 word;
105 }params;
106
107 volatile u32 nextRxDescPtr;
108
109 volatile u32 RxDataPtr;
110
wdenke0ac62d2003-08-17 18:55:18 +0000111 union {
112 struct {
wdenk79d696f2004-03-11 22:46:36 +0000113 volatile u32 C :1;
114 volatile u32 Sop :1;
115 volatile u32 Eop :1;
116 volatile u32 reserved3 :12;
117 volatile u32 NBT :17;
wdenkac6dbb82003-03-26 11:42:53 +0000118 }field;
119
120 volatile u32 word;
121 }status;
122
123} inca_rx_descriptor_t;
124
125
126typedef struct
127{
wdenke0ac62d2003-08-17 18:55:18 +0000128 union {
129 struct {
wdenk79d696f2004-03-11 22:46:36 +0000130 volatile u32 HOLD :1;
131 volatile u32 Eop :1;
132 volatile u32 Sop :1;
133 volatile u32 ICpt :1;
134 volatile u32 IEop :1;
135 volatile u32 reserved0 :5;
136 volatile u32 NBA :22;
wdenkac6dbb82003-03-26 11:42:53 +0000137 }field;
138
139 volatile u32 word;
140 }params;
141
142 volatile u32 nextTxDescPtr;
143
144 volatile u32 TxDataPtr;
145
wdenk79d696f2004-03-11 22:46:36 +0000146 volatile u32 C :1;
147 volatile u32 reserved3 :31;
wdenkac6dbb82003-03-26 11:42:53 +0000148
149} inca_tx_descriptor_t;
150
151
152static inca_rx_descriptor_t rx_ring[NUM_RX_DESC] __attribute__ ((aligned(16)));
153static inca_tx_descriptor_t tx_ring[NUM_TX_DESC] __attribute__ ((aligned(16)));
154
155static int tx_new, rx_new, tx_hold, rx_hold;
156static int tx_old_hold = -1;
wdenk79d696f2004-03-11 22:46:36 +0000157static int initialized = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000158
159
160static int inca_switch_init(struct eth_device *dev, bd_t * bis);
wdenk79d696f2004-03-11 22:46:36 +0000161static int inca_switch_send(struct eth_device *dev, volatile void *packet, int length);
wdenkac6dbb82003-03-26 11:42:53 +0000162static int inca_switch_recv(struct eth_device *dev);
163static void inca_switch_halt(struct eth_device *dev);
164static void inca_init_switch_chip(void);
165static void inca_dma_init(void);
wdenkcf56e112004-02-20 22:02:48 +0000166static int inca_amdix(void);
wdenkac6dbb82003-03-26 11:42:53 +0000167
168
wdenkac6dbb82003-03-26 11:42:53 +0000169int inca_switch_initialize(bd_t * bis)
170{
171 struct eth_device *dev;
172
173#if 0
174 printf("Entered inca_switch_initialize()\n");
175#endif
176
wdenke0ac62d2003-08-17 18:55:18 +0000177 if (!(dev = (struct eth_device *) malloc (sizeof *dev))) {
wdenkac6dbb82003-03-26 11:42:53 +0000178 printf("Failed to allocate memory\n");
179 return 0;
180 }
181 memset(dev, 0, sizeof(*dev));
182
183 inca_dma_init();
184
185 inca_init_switch_chip();
wdenk3c74e322004-02-22 23:46:08 +0000186
wdenk0c852a22004-02-26 23:01:04 +0000187#if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
wdenkcf56e112004-02-20 22:02:48 +0000188 inca_amdix();
wdenk0c852a22004-02-26 23:01:04 +0000189#endif
wdenkac6dbb82003-03-26 11:42:53 +0000190
191 sprintf(dev->name, "INCA-IP Switch");
192 dev->init = inca_switch_init;
193 dev->halt = inca_switch_halt;
194 dev->send = inca_switch_send;
195 dev->recv = inca_switch_recv;
196
197 eth_register(dev);
198
199#if 0
200 printf("Leaving inca_switch_initialize()\n");
201#endif
202
Ben Warren8218bd22008-08-31 10:16:59 -0700203 return 0;
wdenkac6dbb82003-03-26 11:42:53 +0000204}
205
206
207static int inca_switch_init(struct eth_device *dev, bd_t * bis)
208{
209 int i;
210 u32 v, regValue;
211 u16 wTmp;
212
213#if 0
214 printf("Entering inca_switch_init()\n");
215#endif
216
wdenke0ac62d2003-08-17 18:55:18 +0000217 /* Set MAC address.
218 */
wdenkac6dbb82003-03-26 11:42:53 +0000219 wTmp = (u16)dev->enetaddr[0];
220 regValue = (wTmp << 8) | dev->enetaddr[1];
221
222 SW_WRITE_REG(INCA_IP_Switch_PMAC_SA1, regValue);
223
224 wTmp = (u16)dev->enetaddr[2];
225 regValue = (wTmp << 8) | dev->enetaddr[3];
226 regValue = regValue << 16;
227 wTmp = (u16)dev->enetaddr[4];
228 regValue |= (wTmp<<8) | dev->enetaddr[5];
229
230 SW_WRITE_REG(INCA_IP_Switch_PMAC_SA2, regValue);
231
wdenke0ac62d2003-08-17 18:55:18 +0000232 /* Initialize the descriptor rings.
233 */
wdenkf8d813e2004-03-02 14:05:39 +0000234 for (i = 0; i < NUM_RX_DESC; i++) {
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900235 inca_rx_descriptor_t * rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[i]);
wdenkac6dbb82003-03-26 11:42:53 +0000236 memset(rx_desc, 0, sizeof(rx_ring[i]));
237
wdenke0ac62d2003-08-17 18:55:18 +0000238 /* Set maximum size of receive buffer.
239 */
wdenkac6dbb82003-03-26 11:42:53 +0000240 rx_desc->params.field.NFB = PKTSIZE_ALIGN;
241
wdenke0ac62d2003-08-17 18:55:18 +0000242 /* Set the offset of the receive buffer. Zero means
243 * that the offset mechanism is not used.
244 */
wdenkac6dbb82003-03-26 11:42:53 +0000245 rx_desc->params.field.offset = 0;
246
247 /* Check if it is the last descriptor.
248 */
wdenke0ac62d2003-08-17 18:55:18 +0000249 if (i == (NUM_RX_DESC - 1)) {
250 /* Let the last descriptor point to the first
251 * one.
252 */
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900253 rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(rx_ring);
wdenke0ac62d2003-08-17 18:55:18 +0000254 } else {
255 /* Set the address of the next descriptor.
256 */
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900257 rx_desc->nextRxDescPtr = (u32)CKSEG1ADDR(&rx_ring[i+1]);
wdenkac6dbb82003-03-26 11:42:53 +0000258 }
259
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900260 rx_desc->RxDataPtr = (u32)CKSEG1ADDR(NetRxPackets[i]);
wdenkac6dbb82003-03-26 11:42:53 +0000261 }
262
263#if 0
264 printf("rx_ring = 0x%08X 0x%08X\n", (u32)rx_ring, (u32)&rx_ring[0]);
265 printf("tx_ring = 0x%08X 0x%08X\n", (u32)tx_ring, (u32)&tx_ring[0]);
266#endif
267
wdenke0ac62d2003-08-17 18:55:18 +0000268 for (i = 0; i < NUM_TX_DESC; i++) {
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900269 inca_tx_descriptor_t * tx_desc = (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[i]);
wdenkac6dbb82003-03-26 11:42:53 +0000270
271 memset(tx_desc, 0, sizeof(tx_ring[i]));
272
wdenk79d696f2004-03-11 22:46:36 +0000273 tx_desc->params.word = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000274 tx_desc->params.field.HOLD = 1;
wdenk79d696f2004-03-11 22:46:36 +0000275 tx_desc->C = 1;
wdenkac6dbb82003-03-26 11:42:53 +0000276
277 /* Check if it is the last descriptor.
278 */
wdenke0ac62d2003-08-17 18:55:18 +0000279 if (i == (NUM_TX_DESC - 1)) {
wdenkac6dbb82003-03-26 11:42:53 +0000280 /* Let the last descriptor point to the
281 * first one.
282 */
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900283 tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(tx_ring);
wdenke0ac62d2003-08-17 18:55:18 +0000284 } else {
wdenkac6dbb82003-03-26 11:42:53 +0000285 /* Set the address of the next descriptor.
286 */
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900287 tx_desc->nextTxDescPtr = (u32)CKSEG1ADDR(&tx_ring[i+1]);
wdenkac6dbb82003-03-26 11:42:53 +0000288 }
289 }
290
wdenke0ac62d2003-08-17 18:55:18 +0000291 /* Initialize RxDMA.
292 */
wdenkac6dbb82003-03-26 11:42:53 +0000293 DMA_READ_REG(INCA_IP_DMA_DMA_RXISR, v);
294#if 0
295 printf("RX status = 0x%08X\n", v);
296#endif
297
wdenke0ac62d2003-08-17 18:55:18 +0000298 /* Writing to the FRDA of CHANNEL.
299 */
wdenkac6dbb82003-03-26 11:42:53 +0000300 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXFRDA0, (u32)rx_ring);
301
wdenke0ac62d2003-08-17 18:55:18 +0000302 /* Writing to the COMMAND REG.
303 */
wdenk79d696f2004-03-11 22:46:36 +0000304 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_INIT);
wdenkac6dbb82003-03-26 11:42:53 +0000305
wdenke0ac62d2003-08-17 18:55:18 +0000306 /* Initialize TxDMA.
307 */
wdenkac6dbb82003-03-26 11:42:53 +0000308 DMA_READ_REG(INCA_IP_DMA_DMA_TXISR, v);
309#if 0
310 printf("TX status = 0x%08X\n", v);
311#endif
312
wdenke0ac62d2003-08-17 18:55:18 +0000313 /* Writing to the FRDA of CHANNEL.
314 */
wdenkac6dbb82003-03-26 11:42:53 +0000315 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXFRDA0, (u32)tx_ring);
316
317 tx_new = rx_new = 0;
318
319 tx_hold = NUM_TX_DESC - 1;
320 rx_hold = NUM_RX_DESC - 1;
321
322#if 0
323 rx_ring[rx_hold].params.field.HOLD = 1;
324#endif
wdenke0ac62d2003-08-17 18:55:18 +0000325 /* enable spanning tree forwarding, enable the CPU port */
326 /* ST_PT:
wdenk79d696f2004-03-11 22:46:36 +0000327 * CPS (CPU port status) 0x3 (forwarding)
328 * LPS (LAN port status) 0x3 (forwarding)
329 * PPS (PC port status) 0x3 (forwarding)
wdenke0ac62d2003-08-17 18:55:18 +0000330 */
wdenkac6dbb82003-03-26 11:42:53 +0000331 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0x3f);
332
333#if 0
334 printf("Leaving inca_switch_init()\n");
335#endif
336
337 return 0;
338}
339
340
wdenkf8d813e2004-03-02 14:05:39 +0000341static int inca_switch_send(struct eth_device *dev, volatile void *packet, int length)
wdenkac6dbb82003-03-26 11:42:53 +0000342{
wdenk79d696f2004-03-11 22:46:36 +0000343 int i;
344 int res = -1;
345 u32 command;
346 u32 regValue;
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900347 inca_tx_descriptor_t * tx_desc = (inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_new]);
wdenkac6dbb82003-03-26 11:42:53 +0000348
349#if 0
350 printf("Entered inca_switch_send()\n");
351#endif
352
wdenke0ac62d2003-08-17 18:55:18 +0000353 if (length <= 0) {
wdenkac6dbb82003-03-26 11:42:53 +0000354 printf ("%s: bad packet size: %d\n", dev->name, length);
355 goto Done;
356 }
wdenk8bde7f72003-06-27 21:31:46 +0000357
wdenke0ac62d2003-08-17 18:55:18 +0000358 for(i = 0; tx_desc->C == 0; i++) {
359 if (i >= TOUT_LOOP) {
wdenkac6dbb82003-03-26 11:42:53 +0000360 printf("%s: tx error buffer not ready\n", dev->name);
361 goto Done;
362 }
363 }
364
wdenke0ac62d2003-08-17 18:55:18 +0000365 if (tx_old_hold >= 0) {
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900366 ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_old_hold]))->params.field.HOLD = 1;
wdenkac6dbb82003-03-26 11:42:53 +0000367 }
368 tx_old_hold = tx_hold;
369
370 tx_desc->params.word =
wdenk8bde7f72003-06-27 21:31:46 +0000371 (INCA_DMA_TX_SOP | INCA_DMA_TX_EOP | INCA_DMA_TX_HOLD);
wdenkac6dbb82003-03-26 11:42:53 +0000372
373 tx_desc->C = 0;
374 tx_desc->TxDataPtr = (u32)packet;
375 tx_desc->params.field.NBA = length;
376
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900377 ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->params.field.HOLD = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000378
379 tx_hold = tx_new;
wdenk79d696f2004-03-11 22:46:36 +0000380 tx_new = (tx_new + 1) % NUM_TX_DESC;
wdenkac6dbb82003-03-26 11:42:53 +0000381
382
wdenke0ac62d2003-08-17 18:55:18 +0000383 if (! initialized) {
wdenkac6dbb82003-03-26 11:42:53 +0000384 command = INCA_IP_DMA_DMA_TXCCR0_INIT;
385 initialized = 1;
wdenke0ac62d2003-08-17 18:55:18 +0000386 } else {
wdenkac6dbb82003-03-26 11:42:53 +0000387 command = INCA_IP_DMA_DMA_TXCCR0_HR;
388 }
wdenk8bde7f72003-06-27 21:31:46 +0000389
wdenkac6dbb82003-03-26 11:42:53 +0000390 DMA_READ_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
391 regValue |= command;
392#if 0
393 printf("regValue = 0x%x\n", regValue);
394#endif
395 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, regValue);
396
397#if 1
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900398 for(i = 0; ((inca_tx_descriptor_t *)CKSEG1ADDR(&tx_ring[tx_hold]))->C == 0; i++) {
wdenke0ac62d2003-08-17 18:55:18 +0000399 if (i >= TOUT_LOOP) {
wdenkac6dbb82003-03-26 11:42:53 +0000400 printf("%s: tx buffer not ready\n", dev->name);
401 goto Done;
402 }
403 }
404#endif
405 res = length;
406Done:
407#if 0
408 printf("Leaving inca_switch_send()\n");
409#endif
410 return res;
411}
412
413
414static int inca_switch_recv(struct eth_device *dev)
415{
wdenk79d696f2004-03-11 22:46:36 +0000416 int length = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000417 inca_rx_descriptor_t * rx_desc;
418
419#if 0
420 printf("Entered inca_switch_recv()\n");
421#endif
422
wdenke0ac62d2003-08-17 18:55:18 +0000423 for (;;) {
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900424 rx_desc = (inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_new]);
wdenkac6dbb82003-03-26 11:42:53 +0000425
wdenke0ac62d2003-08-17 18:55:18 +0000426 if (rx_desc->status.field.C == 0) {
wdenkac6dbb82003-03-26 11:42:53 +0000427 break;
428 }
429
430#if 0
431 rx_ring[rx_new].params.field.HOLD = 1;
432#endif
433
wdenke0ac62d2003-08-17 18:55:18 +0000434 if (! rx_desc->status.field.Eop) {
wdenkac6dbb82003-03-26 11:42:53 +0000435 printf("Partly received packet!!!\n");
436 break;
437 }
438
439 length = rx_desc->status.field.NBT;
440 rx_desc->status.word &=
wdenk8bde7f72003-06-27 21:31:46 +0000441 ~(INCA_DMA_RX_EOP | INCA_DMA_RX_SOP | INCA_DMA_RX_C);
wdenkac6dbb82003-03-26 11:42:53 +0000442#if 0
443{
444 int i;
445 for (i=0;i<length - 4;i++) {
446 if (i % 16 == 0) printf("\n%04x: ", i);
447 printf("%02X ", NetRxPackets[rx_new][i]);
448 }
449 printf("\n");
450}
451#endif
452
wdenke0ac62d2003-08-17 18:55:18 +0000453 if (length) {
wdenkac6dbb82003-03-26 11:42:53 +0000454#if 0
455 printf("Received %d bytes\n", length);
456#endif
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900457 NetReceive((void*)CKSEG1ADDR(NetRxPackets[rx_new]), length - 4);
wdenke0ac62d2003-08-17 18:55:18 +0000458 } else {
wdenkac6dbb82003-03-26 11:42:53 +0000459#if 1
460 printf("Zero length!!!\n");
461#endif
462 }
463
464
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900465 ((inca_rx_descriptor_t *)CKSEG1ADDR(&rx_ring[rx_hold]))->params.field.HOLD = 0;
wdenkac6dbb82003-03-26 11:42:53 +0000466
467 rx_hold = rx_new;
468
469 rx_new = (rx_new + 1) % NUM_RX_DESC;
470 }
471
472#if 0
473 printf("Leaving inca_switch_recv()\n");
474#endif
475
476 return length;
477}
478
479
480static void inca_switch_halt(struct eth_device *dev)
481{
482#if 0
483 printf("Entered inca_switch_halt()\n");
484#endif
485
486#if 1
487 initialized = 0;
488#endif
489#if 1
wdenke0ac62d2003-08-17 18:55:18 +0000490 /* Disable forwarding to the CPU port.
491 */
wdenkac6dbb82003-03-26 11:42:53 +0000492 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
493
wdenke0ac62d2003-08-17 18:55:18 +0000494 /* Close RxDMA channel.
495 */
wdenkac6dbb82003-03-26 11:42:53 +0000496 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
497
wdenke0ac62d2003-08-17 18:55:18 +0000498 /* Close TxDMA channel.
499 */
wdenkac6dbb82003-03-26 11:42:53 +0000500 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_TXCCR0_OFF);
501
502
503#endif
504#if 0
505 printf("Leaving inca_switch_halt()\n");
506#endif
507}
508
509
510static void inca_init_switch_chip(void)
511{
512 u32 regValue;
513
wdenke0ac62d2003-08-17 18:55:18 +0000514 /* To workaround a problem with collision counter
515 * (see Errata sheet).
516 */
wdenkac6dbb82003-03-26 11:42:53 +0000517 SW_WRITE_REG(INCA_IP_Switch_PC_TX_CTL, 0x00000001);
518 SW_WRITE_REG(INCA_IP_Switch_LAN_TX_CTL, 0x00000001);
519
520#if 1
wdenke0ac62d2003-08-17 18:55:18 +0000521 /* init MDIO configuration:
wdenk79d696f2004-03-11 22:46:36 +0000522 * MDS (Poll speed): 0x01 (4ms)
523 * PHY_LAN_ADDR: 0x06
524 * PHY_PC_ADDR: 0x05
wdenke0ac62d2003-08-17 18:55:18 +0000525 * UEP (Use External PHY): 0x00 (Internal PHY is used)
wdenk79d696f2004-03-11 22:46:36 +0000526 * PS (Port Select): 0x00 (PT/UMM for LAN)
527 * PT (PHY Test): 0x00 (no test mode)
528 * UMM (Use MDIO Mode): 0x00 (state machine is disabled)
wdenke0ac62d2003-08-17 18:55:18 +0000529 */
wdenkac6dbb82003-03-26 11:42:53 +0000530 SW_WRITE_REG(INCA_IP_Switch_MDIO_CFG, 0x4c50);
531
wdenke0ac62d2003-08-17 18:55:18 +0000532 /* init PHY:
533 * SL (Auto Neg. Speed for LAN)
534 * SP (Auto Neg. Speed for PC)
535 * LL (Link Status for LAN)
536 * LP (Link Status for PC)
537 * DL (Duplex Status for LAN)
538 * DP (Duplex Status for PC)
539 * PL (Auto Neg. Pause Status for LAN)
540 * PP (Auto Neg. Pause Status for PC)
541 */
wdenkac6dbb82003-03-26 11:42:53 +0000542 SW_WRITE_REG (INCA_IP_Switch_EPHY, 0xff);
543
wdenke0ac62d2003-08-17 18:55:18 +0000544 /* MDIO_ACC:
545 * RA (Request/Ack) 0x01 (Request)
wdenk79d696f2004-03-11 22:46:36 +0000546 * RW (Read/Write) 0x01 (Write)
547 * PHY_ADDR 0x05 (PC)
548 * REG_ADDR 0x00 (PHY_BCR: basic control register)
549 * PHY_DATA 0x8000
550 * Reset - software reset
551 * LB (loop back) - normal
552 * SS (speed select) - 10 Mbit/s
wdenke0ac62d2003-08-17 18:55:18 +0000553 * ANE (auto neg. enable) - enable
wdenk79d696f2004-03-11 22:46:36 +0000554 * PD (power down) - normal
555 * ISO (isolate) - normal
wdenke0ac62d2003-08-17 18:55:18 +0000556 * RAN (restart auto neg.) - normal
wdenk79d696f2004-03-11 22:46:36 +0000557 * DM (duplex mode) - half duplex
wdenke0ac62d2003-08-17 18:55:18 +0000558 * CT (collision test) - enable
559 */
560 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0a09000);
wdenkac6dbb82003-03-26 11:42:53 +0000561
wdenke0ac62d2003-08-17 18:55:18 +0000562 /* MDIO_ACC:
563 * RA (Request/Ack) 0x01 (Request)
wdenk79d696f2004-03-11 22:46:36 +0000564 * RW (Read/Write) 0x01 (Write)
565 * PHY_ADDR 0x06 (LAN)
566 * REG_ADDR 0x00 (PHY_BCR: basic control register)
567 * PHY_DATA 0x8000
568 * Reset - software reset
569 * LB (loop back) - normal
570 * SS (speed select) - 10 Mbit/s
wdenke0ac62d2003-08-17 18:55:18 +0000571 * ANE (auto neg. enable) - enable
wdenk79d696f2004-03-11 22:46:36 +0000572 * PD (power down) - normal
573 * ISO (isolate) - normal
wdenke0ac62d2003-08-17 18:55:18 +0000574 * RAN (restart auto neg.) - normal
wdenk79d696f2004-03-11 22:46:36 +0000575 * DM (duplex mode) - half duplex
wdenke0ac62d2003-08-17 18:55:18 +0000576 * CT (collision test) - enable
577 */
wdenk79d696f2004-03-11 22:46:36 +0000578 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC, 0xc0c09000);
wdenke0ac62d2003-08-17 18:55:18 +0000579
wdenkac6dbb82003-03-26 11:42:53 +0000580#endif
581
wdenke0ac62d2003-08-17 18:55:18 +0000582 /* Make sure the CPU port is disabled for now. We
583 * don't want packets to get stacked for us until
584 * we enable DMA and are prepared to receive them.
585 */
wdenkac6dbb82003-03-26 11:42:53 +0000586 SW_WRITE_REG(INCA_IP_Switch_ST_PT,0xf);
587
588 SW_READ_REG(INCA_IP_Switch_ARL_CTL, regValue);
589
wdenke0ac62d2003-08-17 18:55:18 +0000590 /* CRC GEN is enabled.
591 */
wdenkac6dbb82003-03-26 11:42:53 +0000592 regValue |= 0x00000200;
593 SW_WRITE_REG(INCA_IP_Switch_ARL_CTL, regValue);
594
wdenke0ac62d2003-08-17 18:55:18 +0000595 /* ADD TAG is disabled.
596 */
wdenkac6dbb82003-03-26 11:42:53 +0000597 SW_READ_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
598 regValue &= ~0x00000002;
599 SW_WRITE_REG(INCA_IP_Switch_PMAC_HD_CTL, regValue);
600}
601
602
603static void inca_dma_init(void)
604{
wdenke0ac62d2003-08-17 18:55:18 +0000605 /* Switch off all DMA channels.
606 */
wdenkac6dbb82003-03-26 11:42:53 +0000607 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
608 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXCCR1, INCA_IP_DMA_DMA_RXCCR1_OFF);
609
610 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR0, INCA_IP_DMA_DMA_RXCCR0_OFF);
611 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR1, INCA_IP_DMA_DMA_TXCCR1_OFF);
612 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXCCR2, INCA_IP_DMA_DMA_TXCCR2_OFF);
613
wdenke0ac62d2003-08-17 18:55:18 +0000614 /* Setup TX channel polling time.
615 */
wdenkac6dbb82003-03-26 11:42:53 +0000616 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXPOLL, INCA_DMA_TX_POLLING_TIME);
617
wdenke0ac62d2003-08-17 18:55:18 +0000618 /* Setup RX channel polling time.
619 */
wdenkac6dbb82003-03-26 11:42:53 +0000620 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXPOLL, INCA_DMA_RX_POLLING_TIME);
621
wdenke0ac62d2003-08-17 18:55:18 +0000622 /* ERRATA: write reset value into the DMA RX IMR register.
623 */
wdenkac6dbb82003-03-26 11:42:53 +0000624 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXIMR, 0xFFFFFFFF);
625
wdenke0ac62d2003-08-17 18:55:18 +0000626 /* Just in case: disable all transmit interrupts also.
627 */
wdenkac6dbb82003-03-26 11:42:53 +0000628 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXIMR, 0xFFFFFFFF);
629
630 DMA_WRITE_REG(INCA_IP_DMA_DMA_TXISR, 0xFFFFFFFF);
631 DMA_WRITE_REG(INCA_IP_DMA_DMA_RXISR, 0xFFFFFFFF);
632}
633
wdenk0c852a22004-02-26 23:01:04 +0000634#if defined(CONFIG_INCA_IP_SWITCH_AMDIX)
wdenkcf56e112004-02-20 22:02:48 +0000635static int inca_amdix(void)
636{
wdenkf8d813e2004-03-02 14:05:39 +0000637 u32 phyReg1 = 0;
638 u32 phyReg4 = 0;
639 u32 phyReg5 = 0;
640 u32 phyReg6 = 0;
641 u32 phyReg31 = 0;
642 u32 regEphy = 0;
wdenkcf56e112004-02-20 22:02:48 +0000643 int mdi_flag;
644 int retries;
645
646 /* Setup GPIO pins.
647 */
648 *INCA_IP_AUTO_MDIX_LAN_PORTS_DIR |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
649 *INCA_IP_AUTO_MDIX_LAN_PORTS_ALTSEL |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
650
wdenkf8d813e2004-03-02 14:05:39 +0000651#if 0
wdenkcf56e112004-02-20 22:02:48 +0000652 /* Wait for signal.
653 */
654 retries = WAIT_SIGNAL_RETRIES;
wdenkf8d813e2004-03-02 14:05:39 +0000655 while (--retries) {
wdenkcf56e112004-02-20 22:02:48 +0000656 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
657 (0x1 << 31) | /* RA */
658 (0x0 << 30) | /* Read */
659 (0x6 << 21) | /* LAN */
660 (17 << 16)); /* PHY_MCSR */
wdenkf8d813e2004-03-02 14:05:39 +0000661 do {
662 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
663 } while (phyReg1 & (1 << 31));
wdenkcf56e112004-02-20 22:02:48 +0000664
wdenkf8d813e2004-03-02 14:05:39 +0000665 if (phyReg1 & (1 << 1)) {
wdenkcf56e112004-02-20 22:02:48 +0000666 /* Signal detected */
667 break;
668 }
669 }
670
671 if (!retries)
wdenkf8d813e2004-03-02 14:05:39 +0000672 goto Fail;
673#endif
wdenkcf56e112004-02-20 22:02:48 +0000674
675 /* Set MDI mode.
676 */
677 *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
678 mdi_flag = 1;
679
680 /* Wait for link.
681 */
682 retries = WAIT_LINK_RETRIES;
wdenkf8d813e2004-03-02 14:05:39 +0000683 while (--retries) {
wdenkcf56e112004-02-20 22:02:48 +0000684 udelay(LINK_RETRY_DELAY * 1000);
685 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
686 (0x1 << 31) | /* RA */
687 (0x0 << 30) | /* Read */
688 (0x6 << 21) | /* LAN */
689 (1 << 16)); /* PHY_BSR */
wdenkf8d813e2004-03-02 14:05:39 +0000690 do {
691 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
692 } while (phyReg1 & (1 << 31));
wdenkcf56e112004-02-20 22:02:48 +0000693
wdenkf8d813e2004-03-02 14:05:39 +0000694 if (phyReg1 & (1 << 2)) {
wdenkcf56e112004-02-20 22:02:48 +0000695 /* Link is up */
696 break;
wdenkf8d813e2004-03-02 14:05:39 +0000697 } else if (mdi_flag) {
wdenkcf56e112004-02-20 22:02:48 +0000698 /* Set MDIX mode */
699 *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT |= (1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
700 mdi_flag = 0;
wdenkf8d813e2004-03-02 14:05:39 +0000701 } else {
wdenkcf56e112004-02-20 22:02:48 +0000702 /* Set MDI mode */
703 *INCA_IP_AUTO_MDIX_LAN_PORTS_OUT &= ~(1 << INCA_IP_AUTO_MDIX_LAN_GPIO_PIN_RXTX);
704 mdi_flag = 1;
705 }
706 }
707
wdenkf8d813e2004-03-02 14:05:39 +0000708 if (!retries) {
709 goto Fail;
710 } else {
711 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
712 (0x1 << 31) | /* RA */
713 (0x0 << 30) | /* Read */
714 (0x6 << 21) | /* LAN */
715 (1 << 16)); /* PHY_BSR */
716 do {
717 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg1);
718 } while (phyReg1 & (1 << 31));
719
720 /* Auto-negotiation / Parallel detection complete
721 */
722 if (phyReg1 & (1 << 5)) {
723 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
724 (0x1 << 31) | /* RA */
725 (0x0 << 30) | /* Read */
726 (0x6 << 21) | /* LAN */
727 (31 << 16)); /* PHY_SCSR */
728 do {
wdenk79d696f2004-03-11 22:46:36 +0000729 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg31);
wdenkf8d813e2004-03-02 14:05:39 +0000730 } while (phyReg31 & (1 << 31));
731
732 switch ((phyReg31 >> 2) & 0x7) {
733 case INCA_SWITCH_PHY_SPEED_10H:
734 /* 10Base-T Half-duplex */
735 regEphy = 0;
736 break;
737 case INCA_SWITCH_PHY_SPEED_10F:
738 /* 10Base-T Full-duplex */
739 regEphy = INCA_IP_Switch_EPHY_DL;
740 break;
741 case INCA_SWITCH_PHY_SPEED_100H:
742 /* 100Base-TX Half-duplex */
743 regEphy = INCA_IP_Switch_EPHY_SL;
744 break;
745 case INCA_SWITCH_PHY_SPEED_100F:
746 /* 100Base-TX Full-duplex */
747 regEphy = INCA_IP_Switch_EPHY_SL | INCA_IP_Switch_EPHY_DL;
748 break;
749 }
750
751 /* In case of Auto-negotiation,
752 * update the negotiated PAUSE support status
753 */
754 if (phyReg1 & (1 << 3)) {
755 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
756 (0x1 << 31) | /* RA */
757 (0x0 << 30) | /* Read */
758 (0x6 << 21) | /* LAN */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500759 (6 << 16)); /* MII_EXPANSION */
wdenkf8d813e2004-03-02 14:05:39 +0000760 do {
wdenk79d696f2004-03-11 22:46:36 +0000761 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg6);
wdenkf8d813e2004-03-02 14:05:39 +0000762 } while (phyReg6 & (1 << 31));
763
764 /* We are Autoneg-able.
765 * Is Link partner also able to autoneg?
766 */
767 if (phyReg6 & (1 << 0)) {
768 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
769 (0x1 << 31) | /* RA */
770 (0x0 << 30) | /* Read */
771 (0x6 << 21) | /* LAN */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500772 (4 << 16)); /* MII_ADVERTISE */
wdenkf8d813e2004-03-02 14:05:39 +0000773 do {
wdenk79d696f2004-03-11 22:46:36 +0000774 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg4);
wdenkf8d813e2004-03-02 14:05:39 +0000775 } while (phyReg4 & (1 << 31));
776
777 /* We advertise PAUSE capab.
778 * Does link partner also advertise it?
779 */
780 if (phyReg4 & (1 << 10)) {
781 SW_WRITE_REG(INCA_IP_Switch_MDIO_ACC,
782 (0x1 << 31) | /* RA */
783 (0x0 << 30) | /* Read */
784 (0x6 << 21) | /* LAN */
Mike Frysinger8ef583a2010-12-23 15:40:12 -0500785 (5 << 16)); /* MII_LPA */
wdenkf8d813e2004-03-02 14:05:39 +0000786 do {
wdenk79d696f2004-03-11 22:46:36 +0000787 SW_READ_REG(INCA_IP_Switch_MDIO_ACC, phyReg5);
wdenkf8d813e2004-03-02 14:05:39 +0000788 } while (phyReg5 & (1 << 31));
789
790 /* Link partner is PAUSE capab.
791 */
792 if (phyReg5 & (1 << 10)) {
793 regEphy |= INCA_IP_Switch_EPHY_PL;
794 }
795 }
796 }
797
798 }
799
800 /* Link is up */
801 regEphy |= INCA_IP_Switch_EPHY_LL;
802
803 SW_WRITE_REG(INCA_IP_Switch_EPHY, regEphy);
804 }
805 }
wdenkcf56e112004-02-20 22:02:48 +0000806
807 return 0;
wdenkf8d813e2004-03-02 14:05:39 +0000808
809Fail:
810 printf("No Link on LAN port\n");
811 return -1;
wdenkcf56e112004-02-20 22:02:48 +0000812}
wdenk0c852a22004-02-26 23:01:04 +0000813#endif /* CONFIG_INCA_IP_SWITCH_AMDIX */