blob: fb2c19a71b8466df7a877c6098e8f65865a56381 [file] [log] [blame]
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +02001/*
Wolfgang Denkef5f37d2009-05-16 10:47:44 +02002 * (C) Copyright 2003-2009
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +02003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * Derived from the MPC8xx FEC driver.
6 * Adapted for MPC512x by Grzegorz Bernacki <gjb@semihalf.com>
7 */
8
9#include <common.h>
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020010#include <malloc.h>
11#include <net.h>
Ben Warrenb664dea2008-08-31 10:36:38 -070012#include <netdev.h>
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020013#include <miiphy.h>
Wolfgang Denkb8539952009-05-16 10:47:43 +020014#include <asm/io.h>
Ben Warren604b6fb2008-08-28 23:58:30 -070015#include "mpc512x_fec.h"
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020016
17DECLARE_GLOBAL_DATA_PTR;
18
19#define DEBUG 0
20
Wolfgang Denk56cbd022007-08-12 14:27:39 +020021#if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020022 defined(CONFIG_MPC512x_FEC)
23
Wolfgang Denk56cbd022007-08-12 14:27:39 +020024#if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020025#error "CONFIG_MII has to be defined!"
26#endif
27
28#if (DEBUG & 0x40)
Wolfgang Denkf342f862009-05-16 10:47:45 +020029static u32 local_crc32(char *string, unsigned int crc_value, int len);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020030#endif
31
Wolfgang Denkf342f862009-05-16 10:47:45 +020032int fec512x_miiphy_read(char *devname, u8 phyAddr, u8 regAddr, u16 * retVal);
33int fec512x_miiphy_write(char *devname, u8 phyAddr, u8 regAddr, u16 data);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020034int mpc512x_fec_init_phy(struct eth_device *dev, bd_t * bis);
35
Grzegorz Bernacki9425b962007-09-10 17:39:08 +020036static uchar rx_buff[FEC_BUFFER_SIZE];
Grzegorz Bernacki7b654b92007-09-07 17:09:21 +020037static int rx_buff_idx = 0;
38
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020039/********************************************************************/
40#if (DEBUG & 0x2)
41static void mpc512x_fec_phydump (char *devname)
42{
Wolfgang Denkf342f862009-05-16 10:47:45 +020043 u16 phyStatus, i;
44 u8 phyAddr = CONFIG_PHY_ADDR;
45 u8 reg_mask[] = {
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020046 /* regs to print: 0...8, 21,27,31 */
47 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
49 };
50
51 for (i = 0; i < 32; i++) {
52 if (reg_mask[i]) {
53 miiphy_read (devname, phyAddr, i, &phyStatus);
54 printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
55 }
56 }
57}
58#endif
59
60/********************************************************************/
61static int mpc512x_fec_bd_init (mpc512x_fec_priv *fec)
62{
63 int ix;
64
65 /*
66 * Receive BDs init
67 */
68 for (ix = 0; ix < FEC_RBD_NUM; ix++) {
Wolfgang Denkb8539952009-05-16 10:47:43 +020069 fec->bdBase->rbd[ix].dataPointer =
Wolfgang Denkf342f862009-05-16 10:47:45 +020070 (u32)&fec->bdBase->recv_frames[ix];
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020071 fec->bdBase->rbd[ix].status = FEC_RBD_EMPTY;
72 fec->bdBase->rbd[ix].dataLength = 0;
73 }
74
75 /*
76 * have the last RBD to close the ring
77 */
78 fec->bdBase->rbd[ix - 1].status |= FEC_RBD_WRAP;
79 fec->rbdIndex = 0;
80
81 /*
82 * Trasmit BDs init
83 */
84 for (ix = 0; ix < FEC_TBD_NUM; ix++) {
Wolfgang Denk530181f2007-08-02 21:27:46 +020085 fec->bdBase->tbd[ix].status = 0;
86 }
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020087
Wolfgang Denk530181f2007-08-02 21:27:46 +020088 /*
89 * Have the last TBD to close the ring
90 */
91 fec->bdBase->tbd[ix - 1].status |= FEC_TBD_WRAP;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020092
Wolfgang Denk530181f2007-08-02 21:27:46 +020093 /*
94 * Initialize some indices
95 */
96 fec->tbdIndex = 0;
97 fec->usedTbdIndex = 0;
98 fec->cleanTbdNum = FEC_TBD_NUM;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +020099
100 return 0;
101}
102
103/********************************************************************/
104static void mpc512x_fec_rbd_clean (mpc512x_fec_priv *fec, volatile FEC_RBD * pRbd)
105{
106 /*
107 * Reset buffer descriptor as empty
108 */
109 if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
110 pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
111 else
112 pRbd->status = FEC_RBD_EMPTY;
113
114 pRbd->dataLength = 0;
115
116 /*
117 * Increment BD count
118 */
119 fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
120
121 /*
122 * Now, we have an empty RxBD, notify FEC
Wolfgang Denkb8539952009-05-16 10:47:43 +0200123 * Set Descriptor polling active
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200124 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200125 out_be32(&fec->eth->r_des_active, 0x01000000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200126}
127
128/********************************************************************/
129static void mpc512x_fec_tbd_scrub (mpc512x_fec_priv *fec)
130{
131 volatile FEC_TBD *pUsedTbd;
132
133#if (DEBUG & 0x1)
134 printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
135 fec->cleanTbdNum, fec->usedTbdIndex);
136#endif
137
138 /*
139 * process all the consumed TBDs
140 */
141 while (fec->cleanTbdNum < FEC_TBD_NUM) {
142 pUsedTbd = &fec->bdBase->tbd[fec->usedTbdIndex];
143 if (pUsedTbd->status & FEC_TBD_READY) {
144#if (DEBUG & 0x20)
145 printf ("Cannot clean TBD %d, in use\n", fec->usedTbdIndex);
146#endif
147 return;
148 }
149
150 /*
151 * clean this buffer descriptor
152 */
153 if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
154 pUsedTbd->status = FEC_TBD_WRAP;
155 else
156 pUsedTbd->status = 0;
157
158 /*
159 * update some indeces for a correct handling of the TBD ring
160 */
161 fec->cleanTbdNum++;
162 fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
163 }
164}
165
166/********************************************************************/
167static void mpc512x_fec_set_hwaddr (mpc512x_fec_priv *fec, char *mac)
168{
Wolfgang Denkf342f862009-05-16 10:47:45 +0200169 u8 currByte; /* byte for which to compute the CRC */
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200170 int byte; /* loop - counter */
171 int bit; /* loop - counter */
Wolfgang Denkf342f862009-05-16 10:47:45 +0200172 u32 crc = 0xffffffff; /* initial value */
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200173
174 /*
175 * The algorithm used is the following:
176 * we loop on each of the six bytes of the provided address,
177 * and we compute the CRC by left-shifting the previous
178 * value by one position, so that each bit in the current
179 * byte of the address may contribute the calculation. If
180 * the latter and the MSB in the CRC are different, then
181 * the CRC value so computed is also ex-ored with the
182 * "polynomium generator". The current byte of the address
183 * is also shifted right by one bit at each iteration.
184 * This is because the CRC generatore in hardware is implemented
185 * as a shift-register with as many ex-ores as the radixes
186 * in the polynomium. This suggests that we represent the
187 * polynomiumm itself as a 32-bit constant.
188 */
189 for (byte = 0; byte < 6; byte++) {
190 currByte = mac[byte];
191 for (bit = 0; bit < 8; bit++) {
192 if ((currByte & 0x01) ^ (crc & 0x01)) {
193 crc >>= 1;
194 crc = crc ^ 0xedb88320;
195 } else {
196 crc >>= 1;
197 }
198 currByte >>= 1;
199 }
200 }
201
202 crc = crc >> 26;
203
204 /*
205 * Set individual hash table register
206 */
207 if (crc >= 32) {
Wolfgang Denkb8539952009-05-16 10:47:43 +0200208 out_be32(&fec->eth->iaddr1, (1 << (crc - 32)));
209 out_be32(&fec->eth->iaddr2, 0);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200210 } else {
Wolfgang Denkb8539952009-05-16 10:47:43 +0200211 out_be32(&fec->eth->iaddr1, 0);
212 out_be32(&fec->eth->iaddr2, (1 << crc));
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200213 }
214
215 /*
216 * Set physical address
217 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200218 out_be32(&fec->eth->paddr1, (mac[0] << 24) + (mac[1] << 16) +
219 (mac[2] << 8) + mac[3]);
220 out_be32(&fec->eth->paddr2, (mac[4] << 24) + (mac[5] << 16) +
221 0x8808);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200222}
223
224/********************************************************************/
225static int mpc512x_fec_init (struct eth_device *dev, bd_t * bis)
226{
227 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
228
229#if (DEBUG & 0x1)
230 printf ("mpc512x_fec_init... Begin\n");
231#endif
232
233 /* Set interrupt mask register */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200234 out_be32(&fec->eth->imask, 0x00000000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200235
236 /* Clear FEC-Lite interrupt event register(IEVENT) */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200237 out_be32(&fec->eth->ievent, 0xffffffff);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200238
239 /* Set transmit fifo watermark register(X_WMRK), default = 64 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200240 out_be32(&fec->eth->x_wmrk, 0x0);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200241
242 /* Set Opcode/Pause Duration Register */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200243 out_be32(&fec->eth->op_pause, 0x00010020);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200244
Grzegorz Bernacki9425b962007-09-10 17:39:08 +0200245 /* Frame length=1522; MII mode */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200246 out_be32(&fec->eth->r_cntrl, (FEC_MAX_FRAME_LEN << 16) | 0x24);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200247
248 /* Half-duplex, heartbeat disabled */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200249 out_be32(&fec->eth->x_cntrl, 0x00000000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200250
251 /* Enable MIB counters */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200252 out_be32(&fec->eth->mib_control, 0x0);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200253
254 /* Setup recv fifo start and buff size */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200255 out_be32(&fec->eth->r_fstart, 0x500);
256 out_be32(&fec->eth->r_buff_size, FEC_BUFFER_SIZE);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200257
258 /* Setup BD base addresses */
Wolfgang Denkf342f862009-05-16 10:47:45 +0200259 out_be32(&fec->eth->r_des_start, (u32)fec->bdBase->rbd);
260 out_be32(&fec->eth->x_des_start, (u32)fec->bdBase->tbd);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200261
262 /* DMA Control */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200263 out_be32(&fec->eth->dma_control, 0xc0000000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200264
265 /* Enable FEC */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200266 setbits_be32(&fec->eth->ecntrl, 0x00000006);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200267
268 /* Initilize addresses and status words of BDs */
269 mpc512x_fec_bd_init (fec);
270
Wolfgang Denk530181f2007-08-02 21:27:46 +0200271 /* Descriptor polling active */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200272 out_be32(&fec->eth->r_des_active, 0x01000000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200273
274#if (DEBUG & 0x1)
275 printf("mpc512x_fec_init... Done \n");
276#endif
277 return 1;
278}
279
280/********************************************************************/
281int mpc512x_fec_init_phy (struct eth_device *dev, bd_t * bis)
282{
283 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
Wolfgang Denkf342f862009-05-16 10:47:45 +0200284 const u8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200285 int timeout = 1;
Wolfgang Denkf342f862009-05-16 10:47:45 +0200286 u16 phyStatus;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200287
288#if (DEBUG & 0x1)
289 printf ("mpc512x_fec_init_phy... Begin\n");
290#endif
291
292 /*
293 * Clear FEC-Lite interrupt event register(IEVENT)
294 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200295 out_be32(&fec->eth->ievent, 0xffffffff);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200296
297 /*
298 * Set interrupt mask register
299 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200300 out_be32(&fec->eth->imask, 0x00000000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200301
302 if (fec->xcv_type != SEVENWIRE) {
303 /*
304 * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
305 * and do not drop the Preamble.
306 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200307 out_be32(&fec->eth->mii_speed,
308 (((gd->ips_clk / 1000000) / 5) + 1) << 1);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200309
310 /*
311 * Reset PHY, then delay 300ns
312 */
313 miiphy_write (dev->name, phyAddr, 0x0, 0x8000);
314 udelay (1000);
315
316 if (fec->xcv_type == MII10) {
317 /*
318 * Force 10Base-T, FDX operation
319 */
320#if (DEBUG & 0x2)
321 printf ("Forcing 10 Mbps ethernet link... ");
322#endif
323 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
Wolfgang Denk530181f2007-08-02 21:27:46 +0200324
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200325 miiphy_write (dev->name, phyAddr, 0x0, 0x0180);
326
327 timeout = 20;
328 do { /* wait for link status to go down */
329 udelay (10000);
330 if ((timeout--) == 0) {
331#if (DEBUG & 0x2)
332 printf ("hmmm, should not have waited...");
333#endif
334 break;
335 }
336 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
337#if (DEBUG & 0x2)
338 printf ("=");
339#endif
340 } while ((phyStatus & 0x0004)); /* !link up */
341
342 timeout = 1000;
343 do { /* wait for link status to come back up */
344 udelay (10000);
345 if ((timeout--) == 0) {
346 printf ("failed. Link is down.\n");
347 break;
348 }
349 miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
350#if (DEBUG & 0x2)
351 printf ("+");
352#endif
353 } while (!(phyStatus & 0x0004)); /* !link up */
354
355#if (DEBUG & 0x2)
356 printf ("done.\n");
357#endif
Wolfgang Denk530181f2007-08-02 21:27:46 +0200358 } else { /* MII100 */
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200359 /*
360 * Set the auto-negotiation advertisement register bits
361 */
362 miiphy_write (dev->name, phyAddr, 0x4, 0x01e1);
363
364 /*
365 * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
366 */
367 miiphy_write (dev->name, phyAddr, 0x0, 0x1200);
368
369 /*
370 * Wait for AN completion
371 */
Wolfgang Denk818968c2008-09-12 13:52:21 +0200372 timeout = 2500;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200373 do {
374 udelay (1000);
375
376 if ((timeout--) == 0) {
377#if (DEBUG & 0x2)
378 printf ("PHY auto neg 0 failed...\n");
379#endif
380 return -1;
381 }
382
383 if (miiphy_read (dev->name, phyAddr, 0x1, &phyStatus) != 0) {
384#if (DEBUG & 0x2)
385 printf ("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
386#endif
387 return -1;
388 }
389 } while (!(phyStatus & 0x0004));
390
391#if (DEBUG & 0x2)
392 printf ("PHY auto neg complete! \n");
393#endif
394 }
395 }
396
397#if (DEBUG & 0x2)
398 if (fec->xcv_type != SEVENWIRE)
399 mpc512x_fec_phydump (dev->name);
400#endif
401
402#if (DEBUG & 0x1)
403 printf ("mpc512x_fec_init_phy... Done \n");
404#endif
405 return 1;
406}
407
408/********************************************************************/
409static void mpc512x_fec_halt (struct eth_device *dev)
410{
411 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
412 int counter = 0xffff;
413
414#if (DEBUG & 0x2)
415 if (fec->xcv_type != SEVENWIRE)
416 mpc512x_fec_phydump (dev->name);
417#endif
418
419 /*
420 * mask FEC chip interrupts
421 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200422 out_be32(&fec->eth->imask, 0);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200423
424 /*
425 * issue graceful stop command to the FEC transmitter if necessary
426 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200427 setbits_be32(&fec->eth->x_cntrl, 0x00000001);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200428
429 /*
430 * wait for graceful stop to register
431 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200432 while ((counter--) && (!(in_be32(&fec->eth->ievent) & 0x10000000)))
433 ;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200434
435 /*
436 * Disable the Ethernet Controller
437 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200438 clrbits_be32(&fec->eth->ecntrl, 0x00000002);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200439
440 /*
441 * Issue a reset command to the FEC chip
442 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200443 setbits_be32(&fec->eth->ecntrl, 0x1);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200444
445 /*
446 * wait at least 16 clock cycles
447 */
448 udelay (10);
449#if (DEBUG & 0x3)
450 printf ("Ethernet task stopped\n");
451#endif
452}
453
454/********************************************************************/
455
456static int mpc512x_fec_send (struct eth_device *dev, volatile void *eth_data,
457 int data_length)
458{
459 /*
460 * This routine transmits one frame. This routine only accepts
461 * 6-byte Ethernet addresses.
462 */
463 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
464 volatile FEC_TBD *pTbd;
465
466#if (DEBUG & 0x20)
467 printf("tbd status: 0x%04x\n", fec->tbdBase[fec->tbdIndex].status);
468#endif
469
470 /*
471 * Clear Tx BD ring at first
472 */
473 mpc512x_fec_tbd_scrub (fec);
474
475 /*
476 * Check for valid length of data.
477 */
478 if ((data_length > 1500) || (data_length <= 0)) {
479 return -1;
480 }
481
482 /*
483 * Check the number of vacant TxBDs.
484 */
485 if (fec->cleanTbdNum < 1) {
486#if (DEBUG & 0x20)
487 printf ("No available TxBDs ...\n");
488#endif
489 return -1;
490 }
491
492 /*
493 * Get the first TxBD to send the mac header
494 */
495 pTbd = &fec->bdBase->tbd[fec->tbdIndex];
496 pTbd->dataLength = data_length;
Wolfgang Denkf342f862009-05-16 10:47:45 +0200497 pTbd->dataPointer = (u32)eth_data;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200498 pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
499 fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
Wolfgang Denk530181f2007-08-02 21:27:46 +0200500
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200501 /* Activate transmit Buffer Descriptor polling */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200502 out_be32(&fec->eth->x_des_active, 0x01000000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200503
504#if (DEBUG & 0x8)
505 printf ( "+" );
506#endif
507
508 fec->cleanTbdNum -= 1;
509
510 /*
511 * wait until frame is sent .
512 */
513 while (pTbd->status & FEC_TBD_READY) {
514 udelay (10);
515#if (DEBUG & 0x8)
516 printf ("TDB status = %04x\n", pTbd->status);
517#endif
518 }
519
520 return 0;
521}
522
523
524/********************************************************************/
525static int mpc512x_fec_recv (struct eth_device *dev)
526{
527 /*
528 * This command pulls one frame from the card
529 */
530 mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
531 volatile FEC_RBD *pRbd = &fec->bdBase->rbd[fec->rbdIndex];
532 unsigned long ievent;
Grzegorz Bernacki7b654b92007-09-07 17:09:21 +0200533 int frame_length = 0;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200534
535#if (DEBUG & 0x1)
536 printf ("mpc512x_fec_recv %d Start...\n", fec->rbdIndex);
537#endif
538#if (DEBUG & 0x8)
539 printf( "-" );
540#endif
Wolfgang Denk530181f2007-08-02 21:27:46 +0200541
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200542 /*
543 * Check if any critical events have happened
544 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200545 ievent = in_be32(&fec->eth->ievent);
546 out_be32(&fec->eth->ievent, ievent);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200547 if (ievent & 0x20060000) {
548 /* BABT, Rx/Tx FIFO errors */
549 mpc512x_fec_halt (dev);
550 mpc512x_fec_init (dev, NULL);
551 return 0;
552 }
553 if (ievent & 0x80000000) {
554 /* Heartbeat error */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200555 setbits_be32(&fec->eth->x_cntrl, 0x00000001);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200556 }
557 if (ievent & 0x10000000) {
558 /* Graceful stop complete */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200559 if (in_be32(&fec->eth->x_cntrl) & 0x00000001) {
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200560 mpc512x_fec_halt (dev);
Wolfgang Denkb8539952009-05-16 10:47:43 +0200561 clrbits_be32(&fec->eth->x_cntrl, 0x00000001);;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200562 mpc512x_fec_init (dev, NULL);
563 }
564 }
565
566 if (!(pRbd->status & FEC_RBD_EMPTY)) {
Grzegorz Bernacki7b654b92007-09-07 17:09:21 +0200567 if (!(pRbd->status & FEC_RBD_ERR) &&
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200568 ((pRbd->dataLength - 4) > 14)) {
Wolfgang Denk530181f2007-08-02 21:27:46 +0200569
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200570 /*
571 * Get buffer size
572 */
Grzegorz Bernacki7b654b92007-09-07 17:09:21 +0200573 if (pRbd->status & FEC_RBD_LAST)
574 frame_length = pRbd->dataLength - 4;
575 else
576 frame_length = pRbd->dataLength;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200577#if (DEBUG & 0x20)
578 {
579 int i;
Grzegorz Bernacki7b654b92007-09-07 17:09:21 +0200580 printf ("recv data length 0x%08x data hdr: ",
581 pRbd->dataLength);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200582 for (i = 0; i < 14; i++)
Wolfgang Denkf342f862009-05-16 10:47:45 +0200583 printf ("%x ", *((u8*)pRbd->dataPointer + i));
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200584 printf("\n");
585 }
586#endif
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200587 /*
588 * Fill the buffer and pass it to upper layers
589 */
Grzegorz Bernacki7b654b92007-09-07 17:09:21 +0200590 memcpy (&rx_buff[rx_buff_idx], (void*)pRbd->dataPointer,
591 frame_length - rx_buff_idx);
592 rx_buff_idx = frame_length;
593
594 if (pRbd->status & FEC_RBD_LAST) {
595 NetReceive ((uchar*)rx_buff, frame_length);
596 rx_buff_idx = 0;
597 }
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200598 }
599
600 /*
601 * Reset buffer descriptor as empty
602 */
603 mpc512x_fec_rbd_clean (fec, pRbd);
604 }
605
606 /* Try to fill Buffer Descriptors */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200607 out_be32(&fec->eth->r_des_active, 0x01000000);
608
Grzegorz Bernacki7b654b92007-09-07 17:09:21 +0200609 return frame_length;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200610}
611
612/********************************************************************/
613int mpc512x_fec_initialize (bd_t * bis)
614{
Wolfgang Denkef5f37d2009-05-16 10:47:44 +0200615 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200616 mpc512x_fec_priv *fec;
617 struct eth_device *dev;
618 int i;
619 char *tmp, *end, env_enetaddr[6];
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200620 void * bd;
621
622 fec = (mpc512x_fec_priv *) malloc (sizeof(*fec));
623 dev = (struct eth_device *) malloc (sizeof(*dev));
624 memset (dev, 0, sizeof *dev);
625
Wolfgang Denkef5f37d2009-05-16 10:47:44 +0200626 fec->eth = &im->fec;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200627
628# ifndef CONFIG_FEC_10MBIT
629 fec->xcv_type = MII100;
630# else
631 fec->xcv_type = MII10;
632# endif
633 dev->priv = (void *)fec;
Wolfgang Denkef5f37d2009-05-16 10:47:44 +0200634 dev->iobase = (int)&im->fec;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200635 dev->init = mpc512x_fec_init;
636 dev->halt = mpc512x_fec_halt;
637 dev->send = mpc512x_fec_send;
638 dev->recv = mpc512x_fec_recv;
639
640 sprintf (dev->name, "FEC ETHERNET");
641 eth_register (dev);
642
Wolfgang Denk56cbd022007-08-12 14:27:39 +0200643#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200644 miiphy_register (dev->name,
645 fec512x_miiphy_read, fec512x_miiphy_write);
646#endif
647
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200648 /* Clean up space FEC's MIB and FIFO RAM ...*/
Wolfgang Denkf342f862009-05-16 10:47:45 +0200649 memset ((void *)&im->fec.mib, 0x00, sizeof(im->fec.mib));
650 memset ((void *)&im->fec.fifo, 0x00, sizeof(im->fec.fifo));
Wolfgang Denk530181f2007-08-02 21:27:46 +0200651
652 /*
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200653 * Malloc space for BDs (must be quad word-aligned)
Wolfgang Denk530181f2007-08-02 21:27:46 +0200654 * this pointer is lost, so cannot be freed
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200655 */
656 bd = malloc (sizeof(mpc512x_buff_descs) + 0x1f);
Wolfgang Denkf342f862009-05-16 10:47:45 +0200657 fec->bdBase = (mpc512x_buff_descs*)((u32)bd & 0xfffffff0);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200658 memset ((void *) bd, 0x00, sizeof(mpc512x_buff_descs) + 0x1f);
659
660 /*
661 * Set interrupt mask register
662 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200663 out_be32(&fec->eth->imask, 0x00000000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200664
665 /*
666 * Clear FEC-Lite interrupt event register(IEVENT)
667 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200668 out_be32(&fec->eth->ievent, 0xffffffff);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200669
670 /*
671 * Try to set the mac address now. The fec mac address is
672 * a garbage after reset. When not using fec for booting
673 * the Linux fec driver will try to work with this garbage.
674 */
675 tmp = getenv ("ethaddr");
676 if (tmp) {
677 for (i=0; i<6; i++) {
678 env_enetaddr[i] = tmp ? simple_strtoul (tmp, &end, 16) : 0;
679 if (tmp)
680 tmp = (*end) ? end+1 : end;
681 }
682 mpc512x_fec_set_hwaddr (fec, env_enetaddr);
Wolfgang Denkb8539952009-05-16 10:47:43 +0200683 out_be32(&fec->eth->gaddr1, 0x00000000);
684 out_be32(&fec->eth->gaddr2, 0x00000000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200685 }
686
687 mpc512x_fec_init_phy (dev, bis);
688
689 return 1;
690}
691
692/* MII-interface related functions */
693/********************************************************************/
Wolfgang Denkf342f862009-05-16 10:47:45 +0200694int fec512x_miiphy_read (char *devname, u8 phyAddr, u8 regAddr, u16 * retVal)
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200695{
Wolfgang Denkef5f37d2009-05-16 10:47:44 +0200696 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
697 volatile fec512x_t *eth = &im->fec;
Wolfgang Denkf342f862009-05-16 10:47:45 +0200698 u32 reg; /* convenient holder for the PHY register */
699 u32 phy; /* convenient holder for the PHY */
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200700 int timeout = 0xffff;
701
702 /*
703 * reading from any PHY's register is done by properly
704 * programming the FEC's MII data register.
705 */
706 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
707 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
708
Wolfgang Denkb8539952009-05-16 10:47:43 +0200709 out_be32(&eth->mii_data, FEC_MII_DATA_ST |
710 FEC_MII_DATA_OP_RD |
711 FEC_MII_DATA_TA |
712 phy | reg);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200713
714 /*
715 * wait for the related interrupt
716 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200717 while ((timeout--) && (!(in_be32(&eth->ievent) & 0x00800000)))
718 ;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200719
720 if (timeout == 0) {
721#if (DEBUG & 0x2)
722 printf ("Read MDIO failed...\n");
723#endif
724 return -1;
725 }
726
727 /*
728 * clear mii interrupt bit
729 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200730 out_be32(&eth->ievent, 0x00800000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200731
732 /*
733 * it's now safe to read the PHY's register
734 */
Wolfgang Denkf342f862009-05-16 10:47:45 +0200735 *retVal = (u16) in_be32(&eth->mii_data);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200736
737 return 0;
738}
739
740/********************************************************************/
Wolfgang Denkf342f862009-05-16 10:47:45 +0200741int fec512x_miiphy_write (char *devname, u8 phyAddr, u8 regAddr, u16 data)
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200742{
Wolfgang Denkef5f37d2009-05-16 10:47:44 +0200743 volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
744 volatile fec512x_t *eth = &im->fec;
Wolfgang Denkf342f862009-05-16 10:47:45 +0200745 u32 reg; /* convenient holder for the PHY register */
746 u32 phy; /* convenient holder for the PHY */
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200747 int timeout = 0xffff;
748
749 reg = regAddr << FEC_MII_DATA_RA_SHIFT;
750 phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
751
Wolfgang Denkb8539952009-05-16 10:47:43 +0200752 out_be32(&eth->mii_data, FEC_MII_DATA_ST |
753 FEC_MII_DATA_OP_WR |
754 FEC_MII_DATA_TA |
755 phy | reg | data);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200756
757 /*
758 * wait for the MII interrupt
759 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200760 while ((timeout--) && (!(in_be32(&eth->ievent) & 0x00800000)))
761 ;
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200762
763 if (timeout == 0) {
764#if (DEBUG & 0x2)
765 printf ("Write MDIO failed...\n");
766#endif
767 return -1;
768 }
769
770 /*
771 * clear MII interrupt bit
772 */
Wolfgang Denkb8539952009-05-16 10:47:43 +0200773 out_be32(&eth->ievent, 0x00800000);
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200774
775 return 0;
776}
777
778#if (DEBUG & 0x40)
Wolfgang Denkf342f862009-05-16 10:47:45 +0200779static u32 local_crc32 (char *string, unsigned int crc_value, int len)
Rafal Jaworowskid3a02c32007-07-27 14:43:59 +0200780{
781 int i;
782 char c;
783 unsigned int crc, count;
784
785 /*
786 * crc32 algorithm
787 */
788 /*
789 * crc = 0xffffffff; * The initialized value should be 0xffffffff
790 */
791 crc = crc_value;
792
793 for (i = len; --i >= 0;) {
794 c = *string++;
795 for (count = 0; count < 8; count++) {
796 if ((c & 0x01) ^ (crc & 0x01)) {
797 crc >>= 1;
798 crc = crc ^ 0xedb88320;
799 } else {
800 crc >>= 1;
801 }
802 c >>= 1;
803 }
804 }
805
806 /*
807 * In big endian system, do byte swaping for crc value
808 */
809 /**/ return crc;
810}
811#endif /* DEBUG */
812
813#endif /* CONFIG_MPC512x_FEC */