blob: 5355a8131b9c3c65d345700d4821ada4968e7cf0 [file] [log] [blame]
wdenk945af8d2003-07-16 21:53:01 +00001/*
2 * (C) Copyright 2003
3 * 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
wdenk77846742003-07-26 08:08:08 +000017/* #define DEBUG 0x28 */
wdenk945af8d2003-07-16 21:53:01 +000018
19#if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
wdenkcbd8a352004-02-24 02:00:03 +000020 defined(CONFIG_MPC5xxx_FEC)
wdenk945af8d2003-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
wdenk77846742003-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
wdenk945af8d2003-07-16 21:53:01 +000038/********************************************************************/
wdenkd4ca31c2004-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/********************************************************************/
wdenk945af8d2003-07-16 21:53:01 +000066static int mpc5xxx_fec_rbd_init(mpc5xxx_fec_priv *fec)
67{
68 int ix;
69 char *data;
wdenk77846742003-07-26 08:08:08 +000070 static int once = 0;
wdenk945af8d2003-07-16 21:53:01 +000071
wdenk945af8d2003-07-16 21:53:01 +000072 for (ix = 0; ix < FEC_RBD_NUM; ix++) {
wdenk77846742003-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;
wdenk945af8d2003-07-16 21:53:01 +000080 }
81 fec->rbdBase[ix].status = FEC_RBD_EMPTY;
82 fec->rbdBase[ix].dataLength = 0;
wdenk945af8d2003-07-16 21:53:01 +000083 }
wdenk77846742003-07-26 08:08:08 +000084 once ++;
wdenk945af8d2003-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/********************************************************************/
118static void mpc5xxx_fec_rbd_clean(mpc5xxx_fec_priv *fec, FEC_RBD * pRbd)
119{
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{
144 FEC_TBD *pUsedTbd;
145
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{
wdenk7152b1d2003-09-05 23:19:14 +0000238 DECLARE_GLOBAL_DATA_PTR;
wdenk945af8d2003-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;
wdenk945af8d2003-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 /*
wdenk945af8d2003-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
wdenk7e780362004-04-08 22:31:29 +0000277 fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
278 if (fec->xcv_type != SEVENWIRE) {
wdenk945af8d2003-07-16 21:53:01 +0000279 /*
wdenk7152b1d2003-09-05 23:19:14 +0000280 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
wdenk945af8d2003-07-16 21:53:01 +0000281 * and do not drop the Preamble.
282 */
wdenk7152b1d2003-09-05 23:19:14 +0000283 fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
wdenk945af8d2003-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 */
294 fec->eth->rfifo_cntrl = 0x0c000000;
295 fec->eth->rfifo_alarm = 0x0000030c;
296#if (DEBUG & 0x22)
297 if (fec->eth->rfifo_status & 0x00700000 ) {
298 printf("mpc5xxx_fec_init() RFIFO error\n");
299 }
300#endif
301
302 /*
303 * Set Tx FIFO granularity value
304 */
305 fec->eth->tfifo_cntrl = 0x0c000000;
306#if (DEBUG & 0x2)
307 printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
308 printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
309#endif
310
311 /*
312 * Set transmit fifo watermark register(X_WMRK), default = 64
313 */
314 fec->eth->tfifo_alarm = 0x00000080;
315 fec->eth->x_wmrk = 0x2;
316
317 /*
318 * Set individual address filter for unicast address
319 * and set physical address registers.
320 */
321 mpc5xxx_fec_set_hwaddr(fec, dev->enetaddr);
322
323 /*
324 * Set multicast address filter
325 */
326 fec->eth->gaddr1 = 0x00000000;
327 fec->eth->gaddr2 = 0x00000000;
328
329 /*
330 * Turn ON cheater FSM: ????
331 */
332 fec->eth->xmit_fsm = 0x03000000;
333
334#if defined(CONFIG_MPC5200)
335 /*
336 * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
337 * work w/ the current receive task.
338 */
339 sdma->PtdCntrl |= 0x00000001;
340#endif
341
342 /*
343 * Set priority of different initiators
344 */
345 sdma->IPR0 = 7; /* always */
346 sdma->IPR3 = 6; /* Eth RX */
347 sdma->IPR4 = 5; /* Eth Tx */
348
349 /*
350 * Clear SmartDMA task interrupt pending bits
351 */
352 SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
353
354 /*
wdenk945af8d2003-07-16 21:53:01 +0000355 * Initialize SmartDMA parameters stored in SRAM
356 */
357 *(int *)FEC_TBD_BASE = (int)fec->tbdBase;
358 *(int *)FEC_RBD_BASE = (int)fec->rbdBase;
359 *(int *)FEC_TBD_NEXT = (int)fec->tbdBase;
360 *(int *)FEC_RBD_NEXT = (int)fec->rbdBase;
361
wdenk6c1362c2004-05-12 22:18:31 +0000362 /*
363 * Enable FEC-Lite controller
364 */
365 fec->eth->ecntrl |= 0x00000006;
366
367#if (DEBUG & 0x2)
368 if (fec->xcv_type != SEVENWIRE)
369 mpc5xxx_fec_phydump ();
370#endif
371
372 /*
373 * Enable SmartDMA receive task
374 */
375 SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
376
377#if (DEBUG & 0x1)
378 printf("mpc5xxx_fec_init... Done \n");
379#endif
380
381 return 1;
382}
383
384/********************************************************************/
385static int mpc5xxx_fec_init_phy(struct eth_device *dev, bd_t * bis)
386{
387 DECLARE_GLOBAL_DATA_PTR;
388 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
389 const uint8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
390
391#if (DEBUG & 0x1)
392 printf ("mpc5xxx_fec_init_phy... Begin\n");
393#endif
394
395 /*
396 * Initialize GPIO pins
397 */
398 if (fec->xcv_type == SEVENWIRE) {
399 /* 10MBit with 7-wire operation */
400 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00020000;
401 } else {
402 /* 100MBit with MD operation */
403 *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00050000;
404 }
405
406 /*
407 * Clear FEC-Lite interrupt event register(IEVENT)
408 */
409 fec->eth->ievent = 0xffffffff;
410
411 /*
412 * Set interrupt mask register
413 */
414 fec->eth->imask = 0x00000000;
415
416 if (fec->xcv_type != SEVENWIRE) {
417 /*
418 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
419 * and do not drop the Preamble.
420 */
421 fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
422 }
423
wdenk945af8d2003-07-16 21:53:01 +0000424 if (fec->xcv_type != SEVENWIRE) {
425 /*
426 * Initialize PHY(LXT971A):
427 *
428 * Generally, on power up, the LXT971A reads its configuration
429 * pins to check for forced operation, If not cofigured for
430 * forced operation, it uses auto-negotiation/parallel detection
431 * to automatically determine line operating conditions.
432 * If the PHY device on the other side of the link supports
433 * auto-negotiation, the LXT971A auto-negotiates with it
434 * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
435 * support auto-negotiation, the LXT971A automatically detects
436 * the presence of either link pulses(10Mbps PHY) or Idle
437 * symbols(100Mbps) and sets its operating conditions accordingly.
438 *
439 * When auto-negotiation is controlled by software, the following
440 * steps are recommended.
441 *
442 * Note:
443 * The physical address is dependent on hardware configuration.
444 *
445 */
446 int timeout = 1;
447 uint16 phyStatus;
448
449 /*
450 * Reset PHY, then delay 300ns
451 */
452 miiphy_write(phyAddr, 0x0, 0x8000);
453 udelay(1000);
454
455 if (fec->xcv_type == MII10) {
456 /*
457 * Force 10Base-T, FDX operation
458 */
wdenka57106f2003-09-16 17:29:31 +0000459#if (DEBUG & 0x2)
wdenk945af8d2003-07-16 21:53:01 +0000460 printf("Forcing 10 Mbps ethernet link... ");
wdenka57106f2003-09-16 17:29:31 +0000461#endif
wdenk945af8d2003-07-16 21:53:01 +0000462 miiphy_read(phyAddr, 0x1, &phyStatus);
463 /*
464 miiphy_write(fec, phyAddr, 0x0, 0x0100);
465 */
466 miiphy_write(phyAddr, 0x0, 0x0180);
467
468 timeout = 20;
469 do { /* wait for link status to go down */
470 udelay(10000);
471 if ((timeout--) == 0) {
472#if (DEBUG & 0x2)
473 printf("hmmm, should not have waited...");
474#endif
475 break;
476 }
477 miiphy_read(phyAddr, 0x1, &phyStatus);
478#if (DEBUG & 0x2)
479 printf("=");
480#endif
481 } while ((phyStatus & 0x0004)); /* !link up */
482
483 timeout = 1000;
484 do { /* wait for link status to come back up */
485 udelay(10000);
486 if ((timeout--) == 0) {
487 printf("failed. Link is down.\n");
488 break;
489 }
490 miiphy_read(phyAddr, 0x1, &phyStatus);
491#if (DEBUG & 0x2)
492 printf("+");
493#endif
494 } while (!(phyStatus & 0x0004)); /* !link up */
495
dzuab209d52003-09-30 14:08:43 +0000496#if (DEBUG & 0x2)
wdenk945af8d2003-07-16 21:53:01 +0000497 printf ("done.\n");
dzuab209d52003-09-30 14:08:43 +0000498#endif
wdenk945af8d2003-07-16 21:53:01 +0000499 } else { /* MII100 */
500 /*
501 * Set the auto-negotiation advertisement register bits
502 */
503 miiphy_write(phyAddr, 0x4, 0x01e1);
504
505 /*
506 * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
507 */
508 miiphy_write(phyAddr, 0x0, 0x1200);
509
510 /*
511 * Wait for AN completion
512 */
513 timeout = 5000;
514 do {
515 udelay(1000);
516
517 if ((timeout--) == 0) {
518#if (DEBUG & 0x2)
519 printf("PHY auto neg 0 failed...\n");
520#endif
521 return -1;
522 }
523
524 if (miiphy_read(phyAddr, 0x1, &phyStatus) != 0) {
525#if (DEBUG & 0x2)
526 printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
527#endif
528 return -1;
529 }
wdenk7e780362004-04-08 22:31:29 +0000530 } while (!(phyStatus & 0x0004));
wdenk945af8d2003-07-16 21:53:01 +0000531
532#if (DEBUG & 0x2)
533 printf("PHY auto neg complete! \n");
534#endif
535 }
536
537 }
538
wdenk945af8d2003-07-16 21:53:01 +0000539#if (DEBUG & 0x2)
wdenkd4ca31c2004-01-02 14:00:00 +0000540 if (fec->xcv_type != SEVENWIRE)
541 mpc5xxx_fec_phydump ();
wdenk945af8d2003-07-16 21:53:01 +0000542#endif
wdenkd4ca31c2004-01-02 14:00:00 +0000543
wdenk945af8d2003-07-16 21:53:01 +0000544
545#if (DEBUG & 0x1)
wdenk6c1362c2004-05-12 22:18:31 +0000546 printf("mpc5xxx_fec_init_phy... Done \n");
wdenk945af8d2003-07-16 21:53:01 +0000547#endif
548
wdenk013dc8d2003-08-07 14:52:18 +0000549 return 1;
wdenk945af8d2003-07-16 21:53:01 +0000550}
551
552/********************************************************************/
553static void mpc5xxx_fec_halt(struct eth_device *dev)
554{
wdenk77846742003-07-26 08:08:08 +0000555#if defined(CONFIG_MPC5200)
wdenk945af8d2003-07-16 21:53:01 +0000556 struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
wdenk77846742003-07-26 08:08:08 +0000557#endif
558 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
wdenk945af8d2003-07-16 21:53:01 +0000559 int counter = 0xffff;
560
561#if (DEBUG & 0x2)
wdenkd4ca31c2004-01-02 14:00:00 +0000562 if (fec->xcv_type != SEVENWIRE)
563 mpc5xxx_fec_phydump ();
wdenk945af8d2003-07-16 21:53:01 +0000564#endif
565
wdenk945af8d2003-07-16 21:53:01 +0000566 /*
567 * mask FEC chip interrupts
568 */
569 fec->eth->imask = 0;
570
571 /*
572 * issue graceful stop command to the FEC transmitter if necessary
573 */
574 fec->eth->x_cntrl |= 0x00000001;
575
576 /*
577 * wait for graceful stop to register
578 */
579 while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
580
wdenk945af8d2003-07-16 21:53:01 +0000581 /*
582 * Disable SmartDMA tasks
583 */
584 SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
585 SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
586
587#if defined(CONFIG_MPC5200)
588 /*
589 * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
590 * done. It doesn't work w/ the current receive task.
591 */
592 sdma->PtdCntrl &= ~0x00000001;
593#endif
594
595 /*
596 * Disable the Ethernet Controller
597 */
598 fec->eth->ecntrl &= 0xfffffffd;
599
600 /*
601 * Clear FIFO status registers
602 */
603 fec->eth->rfifo_status &= 0x00700000;
604 fec->eth->tfifo_status &= 0x00700000;
605
606 fec->eth->reset_cntrl = 0x01000000;
607
608 /*
609 * Issue a reset command to the FEC chip
610 */
611 fec->eth->ecntrl |= 0x1;
612
613 /*
614 * wait at least 16 clock cycles
615 */
616 udelay(10);
617
618#if (DEBUG & 0x3)
619 printf("Ethernet task stopped\n");
620#endif
621}
622
623#if (DEBUG & 0x60)
624/********************************************************************/
625
626static void tfifo_print(mpc5xxx_fec_priv *fec)
627{
wdenkd4ca31c2004-01-02 14:00:00 +0000628 uint16 phyAddr = CONFIG_PHY_ADDR;
wdenk945af8d2003-07-16 21:53:01 +0000629 uint16 phyStatus;
630
631 if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
632 || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
633
634 miiphy_read(phyAddr, 0x1, &phyStatus);
635 printf("\nphyStatus: 0x%04x\n", phyStatus);
636 printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
637 printf("ievent: 0x%08x\n", fec->eth->ievent);
638 printf("x_status: 0x%08x\n", fec->eth->x_status);
639 printf("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
640
641 printf(" control 0x%08x\n", fec->eth->tfifo_cntrl);
642 printf(" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
643 printf(" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
644 printf(" alarm 0x%08x\n", fec->eth->tfifo_alarm);
645 printf(" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
646 printf(" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
647 }
648}
649
650static void rfifo_print(mpc5xxx_fec_priv *fec)
651{
wdenkd4ca31c2004-01-02 14:00:00 +0000652 uint16 phyAddr = CONFIG_PHY_ADDR;
wdenk945af8d2003-07-16 21:53:01 +0000653 uint16 phyStatus;
654
655 if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
656 || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
657
658 miiphy_read(phyAddr, 0x1, &phyStatus);
659 printf("\nphyStatus: 0x%04x\n", phyStatus);
660 printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
661 printf("ievent: 0x%08x\n", fec->eth->ievent);
662 printf("x_status: 0x%08x\n", fec->eth->x_status);
663 printf("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
664
665 printf(" control 0x%08x\n", fec->eth->rfifo_cntrl);
666 printf(" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
667 printf(" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
668 printf(" alarm 0x%08x\n", fec->eth->rfifo_alarm);
669 printf(" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
670 printf(" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
671 }
672}
673#endif /* DEBUG */
674
675/********************************************************************/
676
677static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
678 int data_length)
679{
680 /*
681 * This routine transmits one frame. This routine only accepts
682 * 6-byte Ethernet addresses.
683 */
684 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
685 FEC_TBD *pTbd;
686
687#if (DEBUG & 0x20)
688 printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
689 tfifo_print(fec);
690#endif
691
692 /*
693 * Clear Tx BD ring at first
694 */
695 mpc5xxx_fec_tbd_scrub(fec);
696
697 /*
698 * Check for valid length of data.
699 */
700 if ((data_length > 1500) || (data_length <= 0)) {
701 return -1;
702 }
703
704 /*
705 * Check the number of vacant TxBDs.
706 */
707 if (fec->cleanTbdNum < 1) {
708#if (DEBUG & 0x20)
709 printf("No available TxBDs ...\n");
710#endif
711 return -1;
712 }
713
714 /*
715 * Get the first TxBD to send the mac header
716 */
717 pTbd = &fec->tbdBase[fec->tbdIndex];
718 pTbd->dataLength = data_length;
719 pTbd->dataPointer = (uint32)eth_data;
wdenk77846742003-07-26 08:08:08 +0000720 pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
wdenk945af8d2003-07-16 21:53:01 +0000721 fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
722
723#if (DEBUG & 0x100)
724 printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
725#endif
726
727 /*
728 * Kick the MII i/f
729 */
730 if (fec->xcv_type != SEVENWIRE) {
731 uint16 phyStatus;
732 miiphy_read(0, 0x1, &phyStatus);
733 }
734
735 /*
736 * Enable SmartDMA transmit task
737 */
738
739#if (DEBUG & 0x20)
740 tfifo_print(fec);
741#endif
742 SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
743#if (DEBUG & 0x20)
744 tfifo_print(fec);
745#endif
746#if (DEBUG & 0x8)
747 printf( "+" );
748#endif
749
750 fec->cleanTbdNum -= 1;
751
752#if (DEBUG & 0x129) && (DEBUG & 0x80000000)
753 printf ("smartDMA ethernet Tx task enabled\n");
754#endif
755 /*
756 * wait until frame is sent .
757 */
758 while (pTbd->status & FEC_TBD_READY) {
759 udelay(10);
760#if (DEBUG & 0x8)
761 printf ("TDB status = %04x\n", pTbd->status);
762#endif
763 }
764
765 return 0;
766}
767
768
769/********************************************************************/
770static int mpc5xxx_fec_recv(struct eth_device *dev)
771{
772 /*
773 * This command pulls one frame from the card
774 */
775 mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
776 FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
777 unsigned long ievent;
wdenk77846742003-07-26 08:08:08 +0000778 int frame_length, len = 0;
779 NBUF *frame;
780 char buff[FEC_MAX_PKT_SIZE];
wdenk945af8d2003-07-16 21:53:01 +0000781
782#if (DEBUG & 0x1)
783 printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
784#endif
785#if (DEBUG & 0x8)
786 printf( "-" );
787#endif
788
789 /*
790 * Check if any critical events have happened
791 */
792 ievent = fec->eth->ievent;
793 fec->eth->ievent = ievent;
794 if (ievent & 0x20060000) {
795 /* BABT, Rx/Tx FIFO errors */
796 mpc5xxx_fec_halt(dev);
797 mpc5xxx_fec_init(dev, NULL);
798 return 0;
799 }
800 if (ievent & 0x80000000) {
801 /* Heartbeat error */
802 fec->eth->x_cntrl |= 0x00000001;
803 }
804 if (ievent & 0x10000000) {
805 /* Graceful stop complete */
806 if (fec->eth->x_cntrl & 0x00000001) {
807 mpc5xxx_fec_halt(dev);
808 fec->eth->x_cntrl &= ~0x00000001;
809 mpc5xxx_fec_init(dev, NULL);
810 }
811 }
812
wdenk77846742003-07-26 08:08:08 +0000813 if (!(pRbd->status & FEC_RBD_EMPTY)) {
814 if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
815 ((pRbd->dataLength - 4) > 14)) {
wdenk945af8d2003-07-16 21:53:01 +0000816
wdenk77846742003-07-26 08:08:08 +0000817 /*
818 * Get buffer address and size
819 */
820 frame = (NBUF *)pRbd->dataPointer;
821 frame_length = pRbd->dataLength - 4;
822
823#if (DEBUG & 0x20)
824 {
825 int i;
826 printf("recv data hdr:");
827 for (i = 0; i < 14; i++)
828 printf("%x ", *(frame->head + i));
829 printf("\n");
830 }
wdenk945af8d2003-07-16 21:53:01 +0000831#endif
wdenk77846742003-07-26 08:08:08 +0000832 /*
833 * Fill the buffer and pass it to upper layers
834 */
835 memcpy(buff, frame->head, 14);
836 memcpy(buff + 14, frame->data, frame_length);
837 NetReceive(buff, frame_length);
838 len = frame_length;
839 }
840 /*
841 * Reset buffer descriptor as empty
842 */
843 mpc5xxx_fec_rbd_clean(fec, pRbd);
wdenk945af8d2003-07-16 21:53:01 +0000844 }
wdenk77846742003-07-26 08:08:08 +0000845 SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
846 return len;
wdenk945af8d2003-07-16 21:53:01 +0000847}
848
849
850/********************************************************************/
851int mpc5xxx_fec_initialize(bd_t * bis)
852{
853 mpc5xxx_fec_priv *fec;
854 struct eth_device *dev;
wdenk12f34242003-09-02 22:48:03 +0000855 char *tmp, *end;
856 char env_enetaddr[6];
857 int i;
wdenk945af8d2003-07-16 21:53:01 +0000858
859 fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
860 dev = (struct eth_device *)malloc(sizeof(*dev));
wdenk12f34242003-09-02 22:48:03 +0000861 memset(dev, 0, sizeof *dev);
wdenk945af8d2003-07-16 21:53:01 +0000862
863 fec->eth = (ethernet_regs *)MPC5XXX_FEC;
864 fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
865 fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
wdenk109c0e32004-03-23 21:43:07 +0000866#if defined(CONFIG_ICECUBE) || \
867 defined(CONFIG_PM520) || \
wdenkefa329c2004-03-23 20:18:25 +0000868 defined(CONFIG_TOP5200)
869# ifndef CONFIG_FEC_10MBIT
wdenk945af8d2003-07-16 21:53:01 +0000870 fec->xcv_type = MII100;
wdenkefa329c2004-03-23 20:18:25 +0000871# else
wdenka57106f2003-09-16 17:29:31 +0000872 fec->xcv_type = MII10;
wdenkefa329c2004-03-23 20:18:25 +0000873# endif
wdenka57106f2003-09-16 17:29:31 +0000874#else
875#error fec->xcv_type not initialized.
wdenk945af8d2003-07-16 21:53:01 +0000876#endif
877
878 dev->priv = (void *)fec;
879 dev->iobase = MPC5XXX_FEC;
880 dev->init = mpc5xxx_fec_init;
881 dev->halt = mpc5xxx_fec_halt;
882 dev->send = mpc5xxx_fec_send;
883 dev->recv = mpc5xxx_fec_recv;
884
wdenk77846742003-07-26 08:08:08 +0000885 sprintf(dev->name, "FEC ETHERNET");
wdenk945af8d2003-07-16 21:53:01 +0000886 eth_register(dev);
887
wdenk12f34242003-09-02 22:48:03 +0000888 /*
889 * Try to set the mac address now. The fec mac address is
wdenk42d1f032003-10-15 23:53:47 +0000890 * a garbage after reset. When not using fec for booting
wdenk12f34242003-09-02 22:48:03 +0000891 * the Linux fec driver will try to work with this garbage.
892 */
893 tmp = getenv("ethaddr");
894 if (tmp) {
895 for (i=0; i<6; i++) {
896 env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
897 if (tmp)
898 tmp = (*end) ? end+1 : end;
899 }
900 mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
901 }
902
wdenk6c1362c2004-05-12 22:18:31 +0000903 mpc5xxx_fec_init_phy(dev, bis);
wdenk945af8d2003-07-16 21:53:01 +0000904 return 1;
905}
906
907/* MII-interface related functions */
908/********************************************************************/
909int miiphy_read(uint8 phyAddr, uint8 regAddr, uint16 * retVal)
910{
911 ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
912 uint32 reg; /* convenient holder for the PHY register */
913 uint32 phy; /* convenient holder for the PHY */
914 int timeout = 0xffff;
915
916 /*
917 * reading from any PHY's register is done by properly
918 * programming the FEC's MII data register.
919 */
920 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
921 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
922
923 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
924
925 /*
926 * wait for the related interrupt
927 */
928 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
929
930 if (timeout == 0) {
931#if (DEBUG & 0x2)
932 printf ("Read MDIO failed...\n");
933#endif
934 return -1;
935 }
936
937 /*
938 * clear mii interrupt bit
939 */
940 eth->ievent = 0x00800000;
941
942 /*
943 * it's now safe to read the PHY's register
944 */
945 *retVal = (uint16) eth->mii_data;
946
947 return 0;
948}
949
950/********************************************************************/
951int miiphy_write(uint8 phyAddr, uint8 regAddr, uint16 data)
952{
953 ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
954 uint32 reg; /* convenient holder for the PHY register */
955 uint32 phy; /* convenient holder for the PHY */
956 int timeout = 0xffff;
957
958 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
959 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
960
961 eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
962 FEC_MII_DATA_TA | phy | reg | data);
963
964 /*
965 * wait for the MII interrupt
966 */
967 while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
968
969 if (timeout == 0) {
970#if (DEBUG & 0x2)
971 printf ("Write MDIO failed...\n");
972#endif
973 return -1;
974 }
975
976 /*
977 * clear MII interrupt bit
978 */
979 eth->ievent = 0x00800000;
980
981 return 0;
982}
983
984#if (DEBUG & 0x40)
985static uint32 local_crc32(char *string, unsigned int crc_value, int len)
986{
987 int i;
988 char c;
989 unsigned int crc, count;
990
991 /*
992 * crc32 algorithm
993 */
994 /*
995 * crc = 0xffffffff; * The initialized value should be 0xffffffff
996 */
997 crc = crc_value;
998
999 for (i = len; --i >= 0;) {
1000 c = *string++;
1001 for (count = 0; count < 8; count++) {
1002 if ((c & 0x01) ^ (crc & 0x01)) {
1003 crc >>= 1;
1004 crc = crc ^ 0xedb88320;
1005 } else {
1006 crc >>= 1;
1007 }
1008 c >>= 1;
1009 }
1010 }
1011
1012 /*
1013 * In big endian system, do byte swaping for crc value
1014 */
1015 /**/ return crc;
1016}
1017#endif /* DEBUG */
1018
wdenkcbd8a352004-02-24 02:00:03 +00001019#endif /* CONFIG_MPC5xxx_FEC */