blob: c3d30a049d7fc6ebbf7a2b32af98ab5f86731baa [file] [log] [blame]
wdenk21136db2003-07-16 21:53:01 +00001/*
wdenkf1d0ff42005-04-13 23:15:10 +00002 * (C) Copyright 2003-2005
wdenk21136db2003-07-16 21:53:01 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * This file is based on mpc4200fec.c,
6 * (C) Copyright Motorola, Inc., 2000
7 */
8
9#include <common.h>
10#include <mpc5xxx.h>
11#include <malloc.h>
12#include <net.h>
13#include <miiphy.h>
14#include "sdma.h"
15#include "fec.h"
16
wdenkb8463562003-07-26 08:08:08 +000017/* #define DEBUG 0x28 */
wdenk21136db2003-07-16 21:53:01 +000018
19#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
wdenkbe9c1cb2004-02-24 02:00:03 +000020 defined(CONFIG_MPC5xxx_FEC)
wdenk21136db2003-07-16 21:53:01 +000021
22#if (DEBUG & 0x60)
23static void tfifo_print(mpc5xxx_fec_priv *fec);
24static void rfifo_print(mpc5xxx_fec_priv *fec);
25#endif /* DEBUG */
26
27#if (DEBUG & 0x40)
28static uint32 local_crc32(char *string, unsigned int crc_value, int len);
29#endif
30
wdenkb8463562003-07-26 08:08:08 +000031typedef struct {
32 uint8 data[1500]; /* actual data */
33 int length; /* actual length */
34 int used; /* buffer in use or not */
35 uint8 head[16]; /* MAC header(6 + 6 + 2) + 2(aligned) */
36} NBUF;
37
wdenk21136db2003-07-16 21:53:01 +000038/********************************************************************/
wdenk1ebf41e2004-01-02 14:00:00 +000039#if (DEBUG & 0x2)
40static void mpc5xxx_fec_phydump (void)
41{
42 uint16 phyStatus, i;
43 uint8 phyAddr = CONFIG_PHY_ADDR;
44 uint8 reg_mask[] = {
45#if CONFIG_PHY_TYPE == 0x79c874 /* AMD Am79C874 */
46 /* regs to print: 0...7, 16...19, 21, 23, 24 */
47 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
48 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
49#else
50 /* regs to print: 0...8, 16...20 */
51 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
52 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53#endif
54 };
55
56 for (i = 0; i < 32; i++) {
57 if (reg_mask[i]) {
58 miiphy_read(phyAddr, i, &phyStatus);
59 printf("Mii reg %d: 0x%04x\n", i, phyStatus);
60 }
61 }
62}
63#endif
64
65/********************************************************************/
wdenk21136db2003-07-16 21:53:01 +000066static int mpc5xxx_fec_rbd_init(mpc5xxx_fec_priv *fec)
67{
68 int ix;
69 char *data;
wdenkb8463562003-07-26 08:08:08 +000070 static int once = 0;
wdenk21136db2003-07-16 21:53:01 +000071
wdenk21136db2003-07-16 21:53:01 +000072 for (ix = 0; ix < FEC_RBD_NUM; ix++) {
wdenkb8463562003-07-26 08:08:08 +000073 if (!once) {
74 data = (char *)malloc(FEC_MAX_PKT_SIZE);
75 if (data == NULL) {
76 printf ("RBD INIT FAILED\n");
77 return -1;
78 }
79 fec->rbdBase[ix].dataPointer = (uint32)data;
wdenk21136db2003-07-16 21:53:01 +000080 }
81 fec->rbdBase[ix].status = FEC_RBD_EMPTY;
82 fec->rbdBase[ix].dataLength = 0;
wdenk21136db2003-07-16 21:53:01 +000083 }
wdenkb8463562003-07-26 08:08:08 +000084 once ++;
wdenk21136db2003-07-16 21:53:01 +000085
86 /*
87 * have the last RBD to close the ring
88 */
89 fec->rbdBase[ix - 1].status |= FEC_RBD_WRAP;
90 fec->rbdIndex = 0;
91
92 return 0;
93}
94
95/********************************************************************/
96static void mpc5xxx_fec_tbd_init(mpc5xxx_fec_priv *fec)
97{
98 int ix;
99
100 for (ix = 0; ix < FEC_TBD_NUM; ix++) {
101 fec->tbdBase[ix].status = 0;
102 }
103
104 /*
105 * Have the last TBD to close the ring
106 */
107 fec->tbdBase[ix - 1].status |= FEC_TBD_WRAP;
108
109 /*
110 * Initialize some indices
111 */
112 fec->tbdIndex = 0;
113 fec->usedTbdIndex = 0;
114 fec->cleanTbdNum = FEC_TBD_NUM;
115}
116
117/********************************************************************/
wdenk99408ba2005-02-24 22:44:16 +0000118static void mpc5xxx_fec_rbd_clean(mpc5xxx_fec_priv *fec, volatile FEC_RBD * pRbd)
wdenk21136db2003-07-16 21:53:01 +0000119{
120 /*
121 * Reset buffer descriptor as empty
122 */
123 if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
124 pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
125 else
126 pRbd->status = FEC_RBD_EMPTY;
127
128 pRbd->dataLength = 0;
129
130 /*
131 * Now, we have an empty RxBD, restart the SmartDMA receive task
132 */
133 SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
134
135 /*
136 * Increment BD count
137 */
138 fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
139}
140
141/********************************************************************/
142static void mpc5xxx_fec_tbd_scrub(mpc5xxx_fec_priv *fec)
143{
wdenk99408ba2005-02-24 22:44:16 +0000144 volatile FEC_TBD *pUsedTbd;
wdenk21136db2003-07-16 21:53:01 +0000145
146#if (DEBUG & 0x1)
147 printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
148 fec->cleanTbdNum, fec->usedTbdIndex);
149#endif
150
151 /*
152 * process all the consumed TBDs
153 */
154 while (fec->cleanTbdNum < FEC_TBD_NUM) {
155 pUsedTbd = &fec->tbdBase[fec->usedTbdIndex];
156 if (pUsedTbd->status & FEC_TBD_READY) {
157#if (DEBUG & 0x20)
158 printf("Cannot clean TBD %d, in use\n", fec->cleanTbdNum);
159#endif
160 return;
161 }
162
163 /*
164 * clean this buffer descriptor
165 */
166 if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
167 pUsedTbd->status = FEC_TBD_WRAP;
168 else
169 pUsedTbd->status = 0;
170
171 /*
172 * update some indeces for a correct handling of the TBD ring
173 */
174 fec->cleanTbdNum++;
175 fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
176 }
177}
178
179/********************************************************************/
180static void mpc5xxx_fec_set_hwaddr(mpc5xxx_fec_priv *fec, char *mac)
181{
182 uint8 currByte; /* byte for which to compute the CRC */
183 int byte; /* loop - counter */
184 int bit; /* loop - counter */
185 uint32 crc = 0xffffffff; /* initial value */
186
187 /*
188 * The algorithm used is the following:
189 * we loop on each of the six bytes of the provided address,
190 * and we compute the CRC by left-shifting the previous
191 * value by one position, so that each bit in the current
192 * byte of the address may contribute the calculation. If
193 * the latter and the MSB in the CRC are different, then
194 * the CRC value so computed is also ex-ored with the
195 * "polynomium generator". The current byte of the address
196 * is also shifted right by one bit at each iteration.
197 * This is because the CRC generatore in hardware is implemented
198 * as a shift-register with as many ex-ores as the radixes
199 * in the polynomium. This suggests that we represent the
200 * polynomiumm itself as a 32-bit constant.
201 */
202 for (byte = 0; byte < 6; byte++) {
203 currByte = mac[byte];
204 for (bit = 0; bit < 8; bit++) {
205 if ((currByte & 0x01) ^ (crc & 0x01)) {
206 crc >>= 1;
207 crc = crc ^ 0xedb88320;
208 } else {
209 crc >>= 1;
210 }
211 currByte >>= 1;
212 }
213 }
214
215 crc = crc >> 26;
216
217 /*
218 * Set individual hash table register
219 */
220 if (crc >= 32) {
221 fec->eth->iaddr1 = (1 << (crc - 32));
222 fec->eth->iaddr2 = 0;
223 } else {
224 fec->eth->iaddr1 = 0;
225 fec->eth->iaddr2 = (1 << crc);
226 }
227
228 /*
229 * Set physical address
230 */
231 fec->eth->paddr1 = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
232 fec->eth->paddr2 = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
233}
234
235/********************************************************************/
236static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
237{
wdenkeb20ad32003-09-05 23:19:14 +0000238 DECLARE_GLOBAL_DATA_PTR;
wdenk21136db2003-07-16 21:53:01 +0000239 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
240 struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
wdenk21136db2003-07-16 21:53:01 +0000241
242#if (DEBUG & 0x1)
243 printf ("mpc5xxx_fec_init... Begin\n");
244#endif
245
246 /*
247 * Initialize RxBD/TxBD rings
248 */
249 mpc5xxx_fec_rbd_init(fec);
250 mpc5xxx_fec_tbd_init(fec);
251
252 /*
wdenk21136db2003-07-16 21:53:01 +0000253 * Clear FEC-Lite interrupt event register(IEVENT)
254 */
255 fec->eth->ievent = 0xffffffff;
256
257 /*
258 * Set interrupt mask register
259 */
260 fec->eth->imask = 0x00000000;
261
262 /*
263 * Set FEC-Lite receive control register(R_CNTRL):
264 */
265 if (fec->xcv_type == SEVENWIRE) {
266 /*
267 * Frame length=1518; 7-wire mode
268 */
269 fec->eth->r_cntrl = 0x05ee0020; /*0x05ee0000;FIXME */
270 } else {
271 /*
272 * Frame length=1518; MII mode;
273 */
274 fec->eth->r_cntrl = 0x05ee0024; /*0x05ee0004;FIXME */
275 }
276
wdenka09491a2004-04-08 22:31:29 +0000277 fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
278 if (fec->xcv_type != SEVENWIRE) {
wdenk21136db2003-07-16 21:53:01 +0000279 /*
wdenkeb20ad32003-09-05 23:19:14 +0000280 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
wdenk21136db2003-07-16 21:53:01 +0000281 * and do not drop the Preamble.
282 */
wdenkeb20ad32003-09-05 23:19:14 +0000283 fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
wdenk21136db2003-07-16 21:53:01 +0000284 }
285
286 /*
287 * Set Opcode/Pause Duration Register
288 */
289 fec->eth->op_pause = 0x00010020; /*FIXME0xffff0020; */
290
291 /*
292 * Set Rx FIFO alarm and granularity value
293 */
Wolfgang Denk08cb2d62005-09-04 23:19:41 +0200294 fec->eth->rfifo_cntrl = 0x0c000000
295 | (fec->eth->rfifo_cntrl & ~0x0f000000);
wdenk21136db2003-07-16 21:53:01 +0000296 fec->eth->rfifo_alarm = 0x0000030c;
297#if (DEBUG & 0x22)
298 if (fec->eth->rfifo_status & 0x00700000 ) {
299 printf("mpc5xxx_fec_init() RFIFO error\n");
300 }
301#endif
302
303 /*
304 * Set Tx FIFO granularity value
305 */
Wolfgang Denk08cb2d62005-09-04 23:19:41 +0200306 fec->eth->tfifo_cntrl = 0x0c000000
307 | (fec->eth->tfifo_cntrl & ~0x0f000000);
wdenk21136db2003-07-16 21:53:01 +0000308#if (DEBUG & 0x2)
309 printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
310 printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
311#endif
312
313 /*
314 * Set transmit fifo watermark register(X_WMRK), default = 64
315 */
316 fec->eth->tfifo_alarm = 0x00000080;
317 fec->eth->x_wmrk = 0x2;
318
319 /*
320 * Set individual address filter for unicast address
321 * and set physical address registers.
322 */
323 mpc5xxx_fec_set_hwaddr(fec, dev->enetaddr);
324
325 /*
326 * Set multicast address filter
327 */
328 fec->eth->gaddr1 = 0x00000000;
329 fec->eth->gaddr2 = 0x00000000;
330
331 /*
332 * Turn ON cheater FSM: ????
333 */
334 fec->eth->xmit_fsm = 0x03000000;
335
336#if defined(CONFIG_MPC5200)
337 /*
338 * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
339 * work w/ the current receive task.
340 */
341 sdma->PtdCntrl |= 0x00000001;
342#endif
343
344 /*
345 * Set priority of different initiators
346 */
347 sdma->IPR0 = 7; /* always */
348 sdma->IPR3 = 6; /* Eth RX */
349 sdma->IPR4 = 5; /* Eth Tx */
350
351 /*
352 * Clear SmartDMA task interrupt pending bits
353 */
354 SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
355
356 /*
wdenk21136db2003-07-16 21:53:01 +0000357 * Initialize SmartDMA parameters stored in SRAM
358 */
wdenk99408ba2005-02-24 22:44:16 +0000359 *(volatile int *)FEC_TBD_BASE = (int)fec->tbdBase;
360 *(volatile int *)FEC_RBD_BASE = (int)fec->rbdBase;
361 *(volatile int *)FEC_TBD_NEXT = (int)fec->tbdBase;
362 *(volatile int *)FEC_RBD_NEXT = (int)fec->rbdBase;
wdenk21136db2003-07-16 21:53:01 +0000363
wdenkaf143832004-05-12 22:18:31 +0000364 /*
365 * Enable FEC-Lite controller
366 */
367 fec->eth->ecntrl |= 0x00000006;
368
369#if (DEBUG & 0x2)
370 if (fec->xcv_type != SEVENWIRE)
371 mpc5xxx_fec_phydump ();
372#endif
373
374 /*
375 * Enable SmartDMA receive task
376 */
377 SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
378
379#if (DEBUG & 0x1)
380 printf("mpc5xxx_fec_init... Done \n");
381#endif
382
383 return 1;
384}
385
386/********************************************************************/
387static int mpc5xxx_fec_init_phy(struct eth_device *dev, bd_t * bis)
388{
389 DECLARE_GLOBAL_DATA_PTR;
390 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
391 const uint8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
392
393#if (DEBUG & 0x1)
394 printf ("mpc5xxx_fec_init_phy... Begin\n");
395#endif
396
397 /*
398 * Initialize GPIO pins
399 */
400 if (fec->xcv_type == SEVENWIRE) {
401 /* 10MBit with 7-wire operation */
wdenkcc3f8a92004-07-11 19:17:20 +0000402#if defined(CONFIG_TOTAL5200)
403 /* 7-wire and USB2 on Ethernet */
404 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00030000;
405#else /* !CONFIG_TOTAL5200 */
406 /* 7-wire only */
wdenkaf143832004-05-12 22:18:31 +0000407 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00020000;
wdenkcc3f8a92004-07-11 19:17:20 +0000408#endif /* CONFIG_TOTAL5200 */
wdenkaf143832004-05-12 22:18:31 +0000409 } else {
410 /* 100MBit with MD operation */
411 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00050000;
412 }
413
414 /*
415 * Clear FEC-Lite interrupt event register(IEVENT)
416 */
417 fec->eth->ievent = 0xffffffff;
418
419 /*
420 * Set interrupt mask register
421 */
422 fec->eth->imask = 0x00000000;
423
424 if (fec->xcv_type != SEVENWIRE) {
425 /*
426 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
427 * and do not drop the Preamble.
428 */
429 fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
430 }
431
wdenk21136db2003-07-16 21:53:01 +0000432 if (fec->xcv_type != SEVENWIRE) {
433 /*
434 * Initialize PHY(LXT971A):
435 *
436 * Generally, on power up, the LXT971A reads its configuration
437 * pins to check for forced operation, If not cofigured for
438 * forced operation, it uses auto-negotiation/parallel detection
439 * to automatically determine line operating conditions.
440 * If the PHY device on the other side of the link supports
441 * auto-negotiation, the LXT971A auto-negotiates with it
442 * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
443 * support auto-negotiation, the LXT971A automatically detects
444 * the presence of either link pulses(10Mbps PHY) or Idle
445 * symbols(100Mbps) and sets its operating conditions accordingly.
446 *
447 * When auto-negotiation is controlled by software, the following
448 * steps are recommended.
449 *
450 * Note:
451 * The physical address is dependent on hardware configuration.
452 *
453 */
454 int timeout = 1;
455 uint16 phyStatus;
456
457 /*
458 * Reset PHY, then delay 300ns
459 */
460 miiphy_write(phyAddr, 0x0, 0x8000);
461 udelay(1000);
462
463 if (fec->xcv_type == MII10) {
464 /*
465 * Force 10Base-T, FDX operation
466 */
wdenk56bb5352003-09-16 17:29:31 +0000467#if (DEBUG & 0x2)
wdenk21136db2003-07-16 21:53:01 +0000468 printf("Forcing 10 Mbps ethernet link... ");
wdenk56bb5352003-09-16 17:29:31 +0000469#endif
wdenk21136db2003-07-16 21:53:01 +0000470 miiphy_read(phyAddr, 0x1, &phyStatus);
471 /*
472 miiphy_write(fec, phyAddr, 0x0, 0x0100);
473 */
474 miiphy_write(phyAddr, 0x0, 0x0180);
475
476 timeout = 20;
477 do { /* wait for link status to go down */
478 udelay(10000);
479 if ((timeout--) == 0) {
480#if (DEBUG & 0x2)
481 printf("hmmm, should not have waited...");
482#endif
483 break;
484 }
485 miiphy_read(phyAddr, 0x1, &phyStatus);
486#if (DEBUG & 0x2)
487 printf("=");
488#endif
489 } while ((phyStatus & 0x0004)); /* !link up */
490
491 timeout = 1000;
492 do { /* wait for link status to come back up */
493 udelay(10000);
494 if ((timeout--) == 0) {
495 printf("failed. Link is down.\n");
496 break;
497 }
498 miiphy_read(phyAddr, 0x1, &phyStatus);
499#if (DEBUG & 0x2)
500 printf("+");
501#endif
502 } while (!(phyStatus & 0x0004)); /* !link up */
503
dzu62177922003-09-30 14:08:43 +0000504#if (DEBUG & 0x2)
wdenk21136db2003-07-16 21:53:01 +0000505 printf ("done.\n");
dzu62177922003-09-30 14:08:43 +0000506#endif
wdenk21136db2003-07-16 21:53:01 +0000507 } else { /* MII100 */
508 /*
509 * Set the auto-negotiation advertisement register bits
510 */
511 miiphy_write(phyAddr, 0x4, 0x01e1);
512
513 /*
514 * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
515 */
516 miiphy_write(phyAddr, 0x0, 0x1200);
517
518 /*
519 * Wait for AN completion
520 */
521 timeout = 5000;
522 do {
523 udelay(1000);
524
525 if ((timeout--) == 0) {
526#if (DEBUG & 0x2)
527 printf("PHY auto neg 0 failed...\n");
528#endif
529 return -1;
530 }
531
532 if (miiphy_read(phyAddr, 0x1, &phyStatus) != 0) {
533#if (DEBUG & 0x2)
534 printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
535#endif
536 return -1;
537 }
wdenka09491a2004-04-08 22:31:29 +0000538 } while (!(phyStatus & 0x0004));
wdenk21136db2003-07-16 21:53:01 +0000539
540#if (DEBUG & 0x2)
541 printf("PHY auto neg complete! \n");
542#endif
543 }
544
545 }
546
wdenk21136db2003-07-16 21:53:01 +0000547#if (DEBUG & 0x2)
wdenk1ebf41e2004-01-02 14:00:00 +0000548 if (fec->xcv_type != SEVENWIRE)
549 mpc5xxx_fec_phydump ();
wdenk21136db2003-07-16 21:53:01 +0000550#endif
wdenk1ebf41e2004-01-02 14:00:00 +0000551
wdenk21136db2003-07-16 21:53:01 +0000552
553#if (DEBUG & 0x1)
wdenkaf143832004-05-12 22:18:31 +0000554 printf("mpc5xxx_fec_init_phy... Done \n");
wdenk21136db2003-07-16 21:53:01 +0000555#endif
556
wdenk3f08e202003-08-07 14:52:18 +0000557 return 1;
wdenk21136db2003-07-16 21:53:01 +0000558}
559
560/********************************************************************/
561static void mpc5xxx_fec_halt(struct eth_device *dev)
562{
wdenkb8463562003-07-26 08:08:08 +0000563#if defined(CONFIG_MPC5200)
wdenk21136db2003-07-16 21:53:01 +0000564 struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
wdenkb8463562003-07-26 08:08:08 +0000565#endif
566 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
wdenk21136db2003-07-16 21:53:01 +0000567 int counter = 0xffff;
568
569#if (DEBUG & 0x2)
wdenk1ebf41e2004-01-02 14:00:00 +0000570 if (fec->xcv_type != SEVENWIRE)
571 mpc5xxx_fec_phydump ();
wdenk21136db2003-07-16 21:53:01 +0000572#endif
573
wdenk21136db2003-07-16 21:53:01 +0000574 /*
575 * mask FEC chip interrupts
576 */
577 fec->eth->imask = 0;
578
579 /*
580 * issue graceful stop command to the FEC transmitter if necessary
581 */
582 fec->eth->x_cntrl |= 0x00000001;
583
584 /*
585 * wait for graceful stop to register
586 */
587 while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
588
wdenk21136db2003-07-16 21:53:01 +0000589 /*
590 * Disable SmartDMA tasks
591 */
592 SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
593 SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
594
595#if defined(CONFIG_MPC5200)
596 /*
597 * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
598 * done. It doesn't work w/ the current receive task.
599 */
600 sdma->PtdCntrl &= ~0x00000001;
601#endif
602
603 /*
604 * Disable the Ethernet Controller
605 */
606 fec->eth->ecntrl &= 0xfffffffd;
607
608 /*
609 * Clear FIFO status registers
610 */
611 fec->eth->rfifo_status &= 0x00700000;
612 fec->eth->tfifo_status &= 0x00700000;
613
614 fec->eth->reset_cntrl = 0x01000000;
615
616 /*
617 * Issue a reset command to the FEC chip
618 */
619 fec->eth->ecntrl |= 0x1;
620
621 /*
622 * wait at least 16 clock cycles
623 */
624 udelay(10);
625
626#if (DEBUG & 0x3)
627 printf("Ethernet task stopped\n");
628#endif
629}
630
631#if (DEBUG & 0x60)
632/********************************************************************/
633
634static void tfifo_print(mpc5xxx_fec_priv *fec)
635{
wdenk1ebf41e2004-01-02 14:00:00 +0000636 uint16 phyAddr = CONFIG_PHY_ADDR;
wdenk21136db2003-07-16 21:53:01 +0000637 uint16 phyStatus;
638
639 if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
640 || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
641
642 miiphy_read(phyAddr, 0x1, &phyStatus);
643 printf("\nphyStatus: 0x%04x\n", phyStatus);
644 printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
645 printf("ievent: 0x%08x\n", fec->eth->ievent);
646 printf("x_status: 0x%08x\n", fec->eth->x_status);
647 printf("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
648
649 printf(" control 0x%08x\n", fec->eth->tfifo_cntrl);
650 printf(" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
651 printf(" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
652 printf(" alarm 0x%08x\n", fec->eth->tfifo_alarm);
653 printf(" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
654 printf(" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
655 }
656}
657
658static void rfifo_print(mpc5xxx_fec_priv *fec)
659{
wdenk1ebf41e2004-01-02 14:00:00 +0000660 uint16 phyAddr = CONFIG_PHY_ADDR;
wdenk21136db2003-07-16 21:53:01 +0000661 uint16 phyStatus;
662
663 if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
664 || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
665
666 miiphy_read(phyAddr, 0x1, &phyStatus);
667 printf("\nphyStatus: 0x%04x\n", phyStatus);
668 printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
669 printf("ievent: 0x%08x\n", fec->eth->ievent);
670 printf("x_status: 0x%08x\n", fec->eth->x_status);
671 printf("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
672
673 printf(" control 0x%08x\n", fec->eth->rfifo_cntrl);
674 printf(" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
675 printf(" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
676 printf(" alarm 0x%08x\n", fec->eth->rfifo_alarm);
677 printf(" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
678 printf(" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
679 }
680}
681#endif /* DEBUG */
682
683/********************************************************************/
684
685static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
686 int data_length)
687{
688 /*
689 * This routine transmits one frame. This routine only accepts
690 * 6-byte Ethernet addresses.
691 */
692 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
wdenk99408ba2005-02-24 22:44:16 +0000693 volatile FEC_TBD *pTbd;
wdenk21136db2003-07-16 21:53:01 +0000694
695#if (DEBUG & 0x20)
696 printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
697 tfifo_print(fec);
698#endif
699
700 /*
701 * Clear Tx BD ring at first
702 */
703 mpc5xxx_fec_tbd_scrub(fec);
704
705 /*
706 * Check for valid length of data.
707 */
708 if ((data_length > 1500) || (data_length <= 0)) {
709 return -1;
710 }
711
712 /*
713 * Check the number of vacant TxBDs.
714 */
715 if (fec->cleanTbdNum < 1) {
716#if (DEBUG & 0x20)
717 printf("No available TxBDs ...\n");
718#endif
719 return -1;
720 }
721
722 /*
723 * Get the first TxBD to send the mac header
724 */
725 pTbd = &fec->tbdBase[fec->tbdIndex];
726 pTbd->dataLength = data_length;
727 pTbd->dataPointer = (uint32)eth_data;
wdenkb8463562003-07-26 08:08:08 +0000728 pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
wdenk21136db2003-07-16 21:53:01 +0000729 fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
730
731#if (DEBUG & 0x100)
732 printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
733#endif
734
735 /*
736 * Kick the MII i/f
737 */
738 if (fec->xcv_type != SEVENWIRE) {
739 uint16 phyStatus;
740 miiphy_read(0, 0x1, &phyStatus);
741 }
742
743 /*
744 * Enable SmartDMA transmit task
745 */
746
747#if (DEBUG & 0x20)
748 tfifo_print(fec);
749#endif
750 SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
751#if (DEBUG & 0x20)
752 tfifo_print(fec);
753#endif
754#if (DEBUG & 0x8)
755 printf( "+" );
756#endif
757
758 fec->cleanTbdNum -= 1;
759
760#if (DEBUG & 0x129) && (DEBUG & 0x80000000)
761 printf ("smartDMA ethernet Tx task enabled\n");
762#endif
763 /*
764 * wait until frame is sent .
765 */
766 while (pTbd->status & FEC_TBD_READY) {
767 udelay(10);
768#if (DEBUG & 0x8)
769 printf ("TDB status = %04x\n", pTbd->status);
770#endif
771 }
772
773 return 0;
774}
775
776
777/********************************************************************/
778static int mpc5xxx_fec_recv(struct eth_device *dev)
779{
780 /*
781 * This command pulls one frame from the card
782 */
783 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
wdenk99408ba2005-02-24 22:44:16 +0000784 volatile FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
wdenk21136db2003-07-16 21:53:01 +0000785 unsigned long ievent;
wdenkb8463562003-07-26 08:08:08 +0000786 int frame_length, len = 0;
787 NBUF *frame;
788 char buff[FEC_MAX_PKT_SIZE];
wdenk21136db2003-07-16 21:53:01 +0000789
790#if (DEBUG & 0x1)
791 printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
792#endif
793#if (DEBUG & 0x8)
794 printf( "-" );
795#endif
796
797 /*
798 * Check if any critical events have happened
799 */
800 ievent = fec->eth->ievent;
801 fec->eth->ievent = ievent;
802 if (ievent & 0x20060000) {
803 /* BABT, Rx/Tx FIFO errors */
804 mpc5xxx_fec_halt(dev);
805 mpc5xxx_fec_init(dev, NULL);
806 return 0;
807 }
808 if (ievent & 0x80000000) {
809 /* Heartbeat error */
810 fec->eth->x_cntrl |= 0x00000001;
811 }
812 if (ievent & 0x10000000) {
813 /* Graceful stop complete */
814 if (fec->eth->x_cntrl & 0x00000001) {
815 mpc5xxx_fec_halt(dev);
816 fec->eth->x_cntrl &= ~0x00000001;
817 mpc5xxx_fec_init(dev, NULL);
818 }
819 }
820
wdenkb8463562003-07-26 08:08:08 +0000821 if (!(pRbd->status & FEC_RBD_EMPTY)) {
822 if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
823 ((pRbd->dataLength - 4) > 14)) {
wdenk21136db2003-07-16 21:53:01 +0000824
wdenkb8463562003-07-26 08:08:08 +0000825 /*
826 * Get buffer address and size
827 */
828 frame = (NBUF *)pRbd->dataPointer;
829 frame_length = pRbd->dataLength - 4;
830
831#if (DEBUG & 0x20)
832 {
833 int i;
834 printf("recv data hdr:");
835 for (i = 0; i < 14; i++)
836 printf("%x ", *(frame->head + i));
837 printf("\n");
838 }
wdenk21136db2003-07-16 21:53:01 +0000839#endif
wdenkb8463562003-07-26 08:08:08 +0000840 /*
841 * Fill the buffer and pass it to upper layers
842 */
843 memcpy(buff, frame->head, 14);
844 memcpy(buff + 14, frame->data, frame_length);
845 NetReceive(buff, frame_length);
846 len = frame_length;
847 }
848 /*
849 * Reset buffer descriptor as empty
850 */
851 mpc5xxx_fec_rbd_clean(fec, pRbd);
wdenk21136db2003-07-16 21:53:01 +0000852 }
wdenkb8463562003-07-26 08:08:08 +0000853 SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
854 return len;
wdenk21136db2003-07-16 21:53:01 +0000855}
856
857
858/********************************************************************/
859int mpc5xxx_fec_initialize(bd_t * bis)
860{
861 mpc5xxx_fec_priv *fec;
862 struct eth_device *dev;
wdenk232fe0b2003-09-02 22:48:03 +0000863 char *tmp, *end;
864 char env_enetaddr[6];
865 int i;
wdenk21136db2003-07-16 21:53:01 +0000866
867 fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
868 dev = (struct eth_device *)malloc(sizeof(*dev));
wdenk232fe0b2003-09-02 22:48:03 +0000869 memset(dev, 0, sizeof *dev);
wdenk21136db2003-07-16 21:53:01 +0000870
871 fec->eth = (ethernet_regs *)MPC5XXX_FEC;
872 fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
873 fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
wdenk8d414a72005-06-10 10:00:19 +0000874#if defined(CONFIG_CANMB) || defined(CONFIG_HMI1001) || \
875 defined(CONFIG_ICECUBE) || defined(CONFIG_INKA4X0) || \
876 defined(CONFIG_PM520) || defined(CONFIG_TOP5200) || \
m8dfae75b2005-08-09 14:48:07 +0200877 defined(CONFIG_TQM5200) || defined(CONFIG_O2DNT)
wdenkc12081a2004-03-23 20:18:25 +0000878# ifndef CONFIG_FEC_10MBIT
wdenk21136db2003-07-16 21:53:01 +0000879 fec->xcv_type = MII100;
wdenkc12081a2004-03-23 20:18:25 +0000880# else
wdenk56bb5352003-09-16 17:29:31 +0000881 fec->xcv_type = MII10;
wdenkc12081a2004-03-23 20:18:25 +0000882# endif
wdenkcc3f8a92004-07-11 19:17:20 +0000883#elif defined(CONFIG_TOTAL5200)
884 fec->xcv_type = SEVENWIRE;
wdenk56bb5352003-09-16 17:29:31 +0000885#else
886#error fec->xcv_type not initialized.
wdenk21136db2003-07-16 21:53:01 +0000887#endif
888
889 dev->priv = (void *)fec;
890 dev->iobase = MPC5XXX_FEC;
891 dev->init = mpc5xxx_fec_init;
892 dev->halt = mpc5xxx_fec_halt;
893 dev->send = mpc5xxx_fec_send;
894 dev->recv = mpc5xxx_fec_recv;
895
wdenkb8463562003-07-26 08:08:08 +0000896 sprintf(dev->name, "FEC ETHERNET");
wdenk21136db2003-07-16 21:53:01 +0000897 eth_register(dev);
898
wdenk232fe0b2003-09-02 22:48:03 +0000899 /*
900 * Try to set the mac address now. The fec mac address is
wdenk9c53f402003-10-15 23:53:47 +0000901 * a garbage after reset. When not using fec for booting
wdenk232fe0b2003-09-02 22:48:03 +0000902 * the Linux fec driver will try to work with this garbage.
903 */
904 tmp = getenv("ethaddr");
905 if (tmp) {
906 for (i=0; i<6; i++) {
907 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
908 if (tmp)
909 tmp = (*end) ? end+1 : end;
910 }
911 mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
912 }
913
wdenkaf143832004-05-12 22:18:31 +0000914 mpc5xxx_fec_init_phy(dev, bis);
wdenk21136db2003-07-16 21:53:01 +0000915 return 1;
916}
917
918/* MII-interface related functions */
919/********************************************************************/
920int miiphy_read(uint8 phyAddr, uint8 regAddr, uint16 * retVal)
921{
922 ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
923 uint32 reg; /* convenient holder for the PHY register */
924 uint32 phy; /* convenient holder for the PHY */
925 int timeout = 0xffff;
926
927 /*
928 * reading from any PHY's register is done by properly
929 * programming the FEC's MII data register.
930 */
931 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
932 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
933
934 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
935
936 /*
937 * wait for the related interrupt
938 */
939 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
940
941 if (timeout == 0) {
942#if (DEBUG & 0x2)
943 printf ("Read MDIO failed...\n");
944#endif
945 return -1;
946 }
947
948 /*
949 * clear mii interrupt bit
950 */
951 eth->ievent = 0x00800000;
952
953 /*
954 * it's now safe to read the PHY's register
955 */
956 *retVal = (uint16) eth->mii_data;
957
958 return 0;
959}
960
961/********************************************************************/
962int miiphy_write(uint8 phyAddr, uint8 regAddr, uint16 data)
963{
964 ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
965 uint32 reg; /* convenient holder for the PHY register */
966 uint32 phy; /* convenient holder for the PHY */
967 int timeout = 0xffff;
968
969 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
970 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
971
972 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
973 FEC_MII_DATA_TA | phy | reg | data);
974
975 /*
976 * wait for the MII interrupt
977 */
978 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
979
980 if (timeout == 0) {
981#if (DEBUG & 0x2)
982 printf ("Write MDIO failed...\n");
983#endif
984 return -1;
985 }
986
987 /*
988 * clear MII interrupt bit
989 */
990 eth->ievent = 0x00800000;
991
992 return 0;
993}
994
995#if (DEBUG & 0x40)
996static uint32 local_crc32(char *string, unsigned int crc_value, int len)
997{
998 int i;
999 char c;
1000 unsigned int crc, count;
1001
1002 /*
1003 * crc32 algorithm
1004 */
1005 /*
1006 * crc = 0xffffffff; * The initialized value should be 0xffffffff
1007 */
1008 crc = crc_value;
1009
1010 for (i = len; --i >= 0;) {
1011 c = *string++;
1012 for (count = 0; count < 8; count++) {
1013 if ((c & 0x01) ^ (crc & 0x01)) {
1014 crc >>= 1;
1015 crc = crc ^ 0xedb88320;
1016 } else {
1017 crc >>= 1;
1018 }
1019 c >>= 1;
1020 }
1021 }
1022
1023 /*
1024 * In big endian system, do byte swaping for crc value
1025 */
1026 /**/ return crc;
1027}
1028#endif /* DEBUG */
1029
wdenkbe9c1cb2004-02-24 02:00:03 +00001030#endif /* CONFIG_MPC5xxx_FEC */