1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* DANUBE internal switch ethernet driver.
*
* (C) Copyright 2003
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <common.h>
#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) \
&& defined(CONFIG_DANUBE_SWITCH)
#include <malloc.h>
#include <net.h>
#include <asm/danube.h>
#include <asm/addrspace.h>
#include <asm/pinstrap.h>
#define MII_MODE 1
#define REV_MII_MODE 2
#define TX_CHAN_NO 7
#define RX_CHAN_NO 6
#define NUM_RX_DESC PKTBUFSRX
#define NUM_TX_DESC 8
#define MAX_PACKET_SIZE 1536
#define TOUT_LOOP 100
#define PHY0_ADDR 1 /*fixme: set the correct value here*/
#define DMA_WRITE_REG(reg, value) *((volatile u32 *)reg) = (u32)value
#define DMA_READ_REG(reg, value) value = (u32)*((volatile u32*)reg)
#define SW_WRITE_REG(reg, value) *((volatile u32*)reg) = (u32)value
#define SW_READ_REG(reg, value) value = (u32)*((volatile u32*)reg)
#define TANTOS_CHIP_ID 0x2599
typedef struct
{
union
{
struct
{
volatile u32 OWN :1;
volatile u32 C :1;
volatile u32 Sop :1;
volatile u32 Eop :1;
volatile u32 reserved :3;
volatile u32 Byteoffset :2;
volatile u32 reserve :7;
volatile u32 DataLen :16;
}field;
volatile u32 word;
}status;
volatile u32 DataPtr;
} danube_rx_descriptor_t;
typedef struct
{
union
{
struct
{
volatile u32 OWN :1;
volatile u32 C :1;
volatile u32 Sop :1;
volatile u32 Eop :1;
volatile u32 Byteoffset :5;
volatile u32 reserved :7;
volatile u32 DataLen :16;
}field;
volatile u32 word;
}status;
volatile u32 DataPtr;
} danube_tx_descriptor_t;
static danube_rx_descriptor_t rx_des_ring[NUM_RX_DESC] __attribute__ ((aligned(8)));
static danube_tx_descriptor_t tx_des_ring[NUM_TX_DESC] __attribute__ ((aligned(8)));
static int tx_num, rx_num;
int danube_switch_init(struct eth_device *dev, bd_t * bis);
int danube_switch_send(struct eth_device *dev, volatile void *packet,int length);
int danube_switch_recv(struct eth_device *dev);
void danube_switch_halt(struct eth_device *dev);
static void danube_init_switch_chip(int mode);
static void danube_dma_init(void);
int danube_switch_initialize(bd_t * bis)
{
struct eth_device *dev;
unsigned short chipid;
#if 0
printf("Entered danube_switch_initialize()\n");
#endif
if (!(dev = (struct eth_device *) malloc (sizeof *dev)))
{
printf("Failed to allocate memory\n");
return 0;
}
memset(dev, 0, sizeof(*dev));
danube_dma_init();
danube_init_switch_chip(REV_MII_MODE);
#ifdef CLK_OUT2_25MHZ
*DANUBE_GPIO_P0_DIR=0x0000ae78;
*DANUBE_GPIO_P0_ALTSEL0=0x00008078;
//joelin for Mii-1 *DANUBE_GPIO_P0_ALTSEL1=0x80000080;
*DANUBE_GPIO_P0_ALTSEL1=0x80000000; //joelin for Mii-1
*DANUBE_CGU_IFCCR=0x00400010;
*DANUBE_GPIO_P0_OD=0x0000ae78;
#endif
/*patch for 6996*/
*DANUBE_RCU_RST_REQ |=1;
mdelay(200);
*DANUBE_RCU_RST_REQ &=(unsigned long)~1;
mdelay(1);
/*while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x80123602;
*/
/*while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x80123602;
*/
/***************/
sprintf(dev->name, "danube Switch");
dev->init = danube_switch_init;
dev->halt = danube_switch_halt;
dev->send = danube_switch_send;
dev->recv = danube_switch_recv;
eth_register(dev);
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0xc1010000;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
chipid = (unsigned short)(*DANUBE_PPE_ETOP_MDIO_ACC & 0xffff);
if (chipid != TANTOS_CHIP_ID) // not tantos switch.
{
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x8001840F;
while((*DANUBE_PPE_ETOP_MDIO_ACC)&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x8003840F;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x8005840F;
//while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
//*DANUBE_PPE_ETOP_MDIO_ACC =0x8006840F;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x8007840F;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x8008840F;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x8001840F;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x80123602;
#ifdef CLK_OUT2_25MHZ
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x80334000;
#endif
}
else // Tantos switch chip
{
//printf("Tantos Switch detected!!\n\r");
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x80a10004;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x80c10004;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
*DANUBE_PPE_ETOP_MDIO_ACC =0x80f50773;
/* Software workaround. */
/* PHY reset from P0 to P4. */
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
mdelay(1);
*DANUBE_PPE_ETOP_MDIO_ACC =0x81218000;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
mdelay(1);
/* P0 */
*DANUBE_PPE_ETOP_MDIO_ACC =0x81200400;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
mdelay(1);
/* P1 */
*DANUBE_PPE_ETOP_MDIO_ACC =0x81200420;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
mdelay(1);
/* P2 */
*DANUBE_PPE_ETOP_MDIO_ACC =0x81200440;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
mdelay(1);
/* P3 */
*DANUBE_PPE_ETOP_MDIO_ACC =0x81200460;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
mdelay(1);
/* p4 */
*DANUBE_PPE_ETOP_MDIO_ACC =0x81200480;
while(*DANUBE_PPE_ETOP_MDIO_ACC&0x80000000);
mdelay(1);
}
return 1;
}
int danube_switch_init(struct eth_device *dev, bd_t * bis)
{
int i;
tx_num=0;
rx_num=0;
/* Reset DMA */
// serial_puts("i \n\0");
*DANUBE_DMA_CS=RX_CHAN_NO;
*DANUBE_DMA_CCTRL=0x2;/*fix me, need to reset this channel first?*/
*DANUBE_DMA_CPOLL= 0x80000040;
/*set descriptor base*/
*DANUBE_DMA_CDBA=(u32)rx_des_ring;
*DANUBE_DMA_CDLEN=NUM_RX_DESC;
*DANUBE_DMA_CIE = 0;
*DANUBE_DMA_CCTRL=0x30000;
*DANUBE_DMA_CS=TX_CHAN_NO;
*DANUBE_DMA_CCTRL=0x2;/*fix me, need to reset this channel first?*/
*DANUBE_DMA_CPOLL= 0x80000040;
*DANUBE_DMA_CDBA=(u32)tx_des_ring;
*DANUBE_DMA_CDLEN=NUM_TX_DESC;
*DANUBE_DMA_CIE = 0;
*DANUBE_DMA_CCTRL=0x30100;
for(i=0;i < NUM_RX_DESC; i++)
{
danube_rx_descriptor_t * rx_desc = KSEG1ADDR(&rx_des_ring[i]);
rx_desc->status.word=0;
rx_desc->status.field.OWN=1;
rx_desc->status.field.DataLen=PKTSIZE_ALIGN; /* 1536 */
rx_desc->DataPtr=(u32)KSEG1ADDR(NetRxPackets[i]);
}
for(i=0;i < NUM_TX_DESC; i++)
{
danube_tx_descriptor_t * tx_desc = KSEG1ADDR(&tx_des_ring[i]);
memset(tx_desc, 0, sizeof(tx_des_ring[0]));
}
/* turn on DMA rx & tx channel
*/
*DANUBE_DMA_CS=RX_CHAN_NO;
*DANUBE_DMA_CCTRL|=1;/*reset and turn on the channel*/
return 0;
}
void danube_switch_halt(struct eth_device *dev)
{
int i;
for(i=0;i<8;i++)
{
*DANUBE_DMA_CS=i;
*DANUBE_DMA_CCTRL&=~1;/*stop the dma channel*/
}
// udelay(1000000);
}
int danube_switch_send(struct eth_device *dev, volatile void *packet,int length)
{
int i;
int res = -1;
danube_tx_descriptor_t * tx_desc= KSEG1ADDR(&tx_des_ring[tx_num]);
if (length <= 0)
{
printf ("%s: bad packet size: %d\n", dev->name, length);
goto Done;
}
for(i=0; tx_desc->status.field.OWN==1; i++)
{
if(i>=TOUT_LOOP)
{
printf("NO Tx Descriptor...");
goto Done;
}
}
//serial_putc('s');
tx_desc->status.field.Sop=1;
tx_desc->status.field.Eop=1;
tx_desc->status.field.C=0;
tx_desc->DataPtr = (u32)KSEG1ADDR(packet);
if(length<60)
tx_desc->status.field.DataLen = 60;
else
tx_desc->status.field.DataLen = (u32)length;
asm("SYNC");
tx_desc->status.field.OWN=1;
res=length;
tx_num++;
if(tx_num==NUM_TX_DESC) tx_num=0;
*DANUBE_DMA_CS=TX_CHAN_NO;
if(!(*DANUBE_DMA_CCTRL & 1))
*DANUBE_DMA_CCTRL|=1;
Done:
return res;
}
int danube_switch_recv(struct eth_device *dev)
{
int length = 0;
danube_rx_descriptor_t * rx_desc;
for (;;)
{
rx_desc = KSEG1ADDR(&rx_des_ring[rx_num]);
if ((rx_desc->status.field.C == 0) || (rx_desc->status.field.OWN == 1))
{
break;
}
length = rx_desc->status.field.DataLen;
if (length)
{
NetReceive((void*)KSEG1ADDR(NetRxPackets[rx_num]), length - 4);
// serial_putc('*');
}
else
{
printf("Zero length!!!\n");
}
rx_desc->status.field.Sop=0;
rx_desc->status.field.Eop=0;
rx_desc->status.field.C=0;
rx_desc->status.field.DataLen=PKTSIZE_ALIGN;
rx_desc->status.field.OWN=1;
rx_num++;
if(rx_num==NUM_RX_DESC) rx_num=0;
}
return length;
}
static void danube_init_switch_chip(int mode)
{
/*get and set mac address for MAC*/
char *tmp;
tmp = getenv ("ethaddr");
if (NULL == tmp) {
printf("Can't get environment ethaddr!!!\n");
// return NULL;
} else {
printf("ethaddr=%s\n", tmp);
}
*DANUBE_PMU_PWDCR = *DANUBE_PMU_PWDCR & 0xFFFFEFDF;
*DANUBE_PPE32_ETOP_MDIO_CFG &= ~0x6;
*DANUBE_PPE32_ENET_MAC_CFG = 0x187;
// turn on port0, set to rmii and turn off port1.
if (mode==REV_MII_MODE)
{
*DANUBE_PPE32_ETOP_CFG = (*DANUBE_PPE32_ETOP_CFG & 0xfffffffc) | 0x0000000a;
}
else if (mode == MII_MODE)
{
*DANUBE_PPE32_ETOP_CFG = (*DANUBE_PPE32_ETOP_CFG & 0xfffffffc) | 0x00000008;
}
*DANUBE_PPE32_ETOP_IG_PLEN_CTRL = 0x4005ee; // set packetlen.
*ENET_MAC_CFG |= 1<<11; /*enable the crc*/
return;
}
static void danube_dma_init(void)
{
// serial_puts("d \n\0");
*DANUBE_PMU_PWDCR &=~(1<<DANUBE_PMU_DMA_SHIFT);/*enable DMA from PMU*/
/* Reset DMA */
*DANUBE_DMA_CTRL|=1;
*DANUBE_DMA_IRNEN=0;/*disable all the interrupts first*/
/* Clear Interrupt Status Register */
*DANUBE_DMA_IRNCR=0xfffff;
/*disable all the dma interrupts*/
*DANUBE_DMA_IRNEN=0;
/*disable channel 0 and channel 1 interrupts*/
*DANUBE_DMA_CS=RX_CHAN_NO;
*DANUBE_DMA_CCTRL=0x2;/*fix me, need to reset this channel first?*/
*DANUBE_DMA_CPOLL= 0x80000040;
/*set descriptor base*/
*DANUBE_DMA_CDBA=(u32)rx_des_ring;
*DANUBE_DMA_CDLEN=NUM_RX_DESC;
*DANUBE_DMA_CIE = 0;
*DANUBE_DMA_CCTRL=0x30000;
*DANUBE_DMA_CS=TX_CHAN_NO;
*DANUBE_DMA_CCTRL=0x2;/*fix me, need to reset this channel first?*/
*DANUBE_DMA_CPOLL= 0x80000040;
*DANUBE_DMA_CDBA=(u32)tx_des_ring;
*DANUBE_DMA_CDLEN=NUM_TX_DESC;
*DANUBE_DMA_CIE = 0;
*DANUBE_DMA_CCTRL=0x30100;
/*enable the poll function and set the poll counter*/
//*DANUBE_DMA_CPOLL=DANUBE_DMA_POLL_EN | (DANUBE_DMA_POLL_COUNT<<4);
/*set port properties, enable endian conversion for switch*/
*DANUBE_DMA_PS=0;
*DANUBE_DMA_PCTRL|=0xf<<8;/*enable 32 bit endian conversion*/
return;
}
#endif
|