fec.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  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. #include <common.h>
  9. #include <mpc8220.h>
  10. #include <malloc.h>
  11. #include <net.h>
  12. #include <miiphy.h>
  13. #include "dma.h"
  14. #include "fec.h"
  15. #undef DEBUG
  16. #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
  17. defined(CONFIG_MPC8220_FEC)
  18. #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
  19. #error "CONFIG_MII has to be defined!"
  20. #endif
  21. #ifdef DEBUG
  22. static void tfifo_print (char *devname, mpc8220_fec_priv * fec);
  23. static void rfifo_print (char *devname, mpc8220_fec_priv * fec);
  24. #endif /* DEBUG */
  25. #ifdef DEBUG
  26. static u32 local_crc32 (char *string, unsigned int crc_value, int len);
  27. #endif
  28. typedef struct {
  29. u8 data[1500]; /* actual data */
  30. int length; /* actual length */
  31. int used; /* buffer in use or not */
  32. u8 head[16]; /* MAC header(6 + 6 + 2) + 2(aligned) */
  33. } NBUF;
  34. int fec8220_miiphy_read (char *devname, u8 phyAddr, u8 regAddr, u16 * retVal);
  35. int fec8220_miiphy_write (char *devname, u8 phyAddr, u8 regAddr, u16 data);
  36. /********************************************************************/
  37. #ifdef DEBUG
  38. static void mpc8220_fec_phydump (char *devname)
  39. {
  40. u16 phyStatus, i;
  41. u8 phyAddr = CONFIG_PHY_ADDR;
  42. u8 reg_mask[] = {
  43. #if CONFIG_PHY_TYPE == 0x79c874 /* AMD Am79C874 */
  44. /* regs to print: 0...7, 16...19, 21, 23, 24 */
  45. 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
  46. 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
  47. #else
  48. /* regs to print: 0...8, 16...20 */
  49. 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
  50. 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. #endif
  52. };
  53. for (i = 0; i < 32; i++) {
  54. if (reg_mask[i]) {
  55. miiphy_read (devname, phyAddr, i, &phyStatus);
  56. printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
  57. }
  58. }
  59. }
  60. #endif
  61. /********************************************************************/
  62. static int mpc8220_fec_rbd_init (mpc8220_fec_priv * fec)
  63. {
  64. int ix;
  65. char *data;
  66. static int once = 0;
  67. for (ix = 0; ix < FEC_RBD_NUM; ix++) {
  68. if (!once) {
  69. data = (char *) malloc (FEC_MAX_PKT_SIZE);
  70. if (data == NULL) {
  71. printf ("RBD INIT FAILED\n");
  72. return -1;
  73. }
  74. fec->rbdBase[ix].dataPointer = (u32) data;
  75. }
  76. fec->rbdBase[ix].status = FEC_RBD_EMPTY;
  77. fec->rbdBase[ix].dataLength = 0;
  78. }
  79. once++;
  80. /*
  81. * have the last RBD to close the ring
  82. */
  83. fec->rbdBase[ix - 1].status |= FEC_RBD_WRAP;
  84. fec->rbdIndex = 0;
  85. return 0;
  86. }
  87. /********************************************************************/
  88. static void mpc8220_fec_tbd_init (mpc8220_fec_priv * fec)
  89. {
  90. int ix;
  91. for (ix = 0; ix < FEC_TBD_NUM; ix++) {
  92. fec->tbdBase[ix].status = 0;
  93. }
  94. /*
  95. * Have the last TBD to close the ring
  96. */
  97. fec->tbdBase[ix - 1].status |= FEC_TBD_WRAP;
  98. /*
  99. * Initialize some indices
  100. */
  101. fec->tbdIndex = 0;
  102. fec->usedTbdIndex = 0;
  103. fec->cleanTbdNum = FEC_TBD_NUM;
  104. }
  105. /********************************************************************/
  106. static void mpc8220_fec_rbd_clean (mpc8220_fec_priv * fec, FEC_RBD * pRbd)
  107. {
  108. /*
  109. * Reset buffer descriptor as empty
  110. */
  111. if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
  112. pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
  113. else
  114. pRbd->status = FEC_RBD_EMPTY;
  115. pRbd->dataLength = 0;
  116. /*
  117. * Now, we have an empty RxBD, restart the SmartDMA receive task
  118. */
  119. DMA_TASK_ENABLE (FEC_RECV_TASK_NO);
  120. /*
  121. * Increment BD count
  122. */
  123. fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
  124. }
  125. /********************************************************************/
  126. static void mpc8220_fec_tbd_scrub (mpc8220_fec_priv * fec)
  127. {
  128. FEC_TBD *pUsedTbd;
  129. #ifdef DEBUG
  130. printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
  131. fec->cleanTbdNum, fec->usedTbdIndex);
  132. #endif
  133. /*
  134. * process all the consumed TBDs
  135. */
  136. while (fec->cleanTbdNum < FEC_TBD_NUM) {
  137. pUsedTbd = &fec->tbdBase[fec->usedTbdIndex];
  138. if (pUsedTbd->status & FEC_TBD_READY) {
  139. #ifdef DEBUG
  140. printf ("Cannot clean TBD %d, in use\n",
  141. fec->cleanTbdNum);
  142. #endif
  143. return;
  144. }
  145. /*
  146. * clean this buffer descriptor
  147. */
  148. if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
  149. pUsedTbd->status = FEC_TBD_WRAP;
  150. else
  151. pUsedTbd->status = 0;
  152. /*
  153. * update some indeces for a correct handling of the TBD ring
  154. */
  155. fec->cleanTbdNum++;
  156. fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
  157. }
  158. }
  159. /********************************************************************/
  160. static void mpc8220_fec_set_hwaddr (mpc8220_fec_priv * fec, char *mac)
  161. {
  162. u8 currByte; /* byte for which to compute the CRC */
  163. int byte; /* loop - counter */
  164. int bit; /* loop - counter */
  165. u32 crc = 0xffffffff; /* initial value */
  166. /*
  167. * The algorithm used is the following:
  168. * we loop on each of the six bytes of the provided address,
  169. * and we compute the CRC by left-shifting the previous
  170. * value by one position, so that each bit in the current
  171. * byte of the address may contribute the calculation. If
  172. * the latter and the MSB in the CRC are different, then
  173. * the CRC value so computed is also ex-ored with the
  174. * "polynomium generator". The current byte of the address
  175. * is also shifted right by one bit at each iteration.
  176. * This is because the CRC generatore in hardware is implemented
  177. * as a shift-register with as many ex-ores as the radixes
  178. * in the polynomium. This suggests that we represent the
  179. * polynomiumm itself as a 32-bit constant.
  180. */
  181. for (byte = 0; byte < 6; byte++) {
  182. currByte = mac[byte];
  183. for (bit = 0; bit < 8; bit++) {
  184. if ((currByte & 0x01) ^ (crc & 0x01)) {
  185. crc >>= 1;
  186. crc = crc ^ 0xedb88320;
  187. } else {
  188. crc >>= 1;
  189. }
  190. currByte >>= 1;
  191. }
  192. }
  193. crc = crc >> 26;
  194. /*
  195. * Set individual hash table register
  196. */
  197. if (crc >= 32) {
  198. fec->eth->iaddr1 = (1 << (crc - 32));
  199. fec->eth->iaddr2 = 0;
  200. } else {
  201. fec->eth->iaddr1 = 0;
  202. fec->eth->iaddr2 = (1 << crc);
  203. }
  204. /*
  205. * Set physical address
  206. */
  207. fec->eth->paddr1 =
  208. (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
  209. fec->eth->paddr2 = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
  210. }
  211. /********************************************************************/
  212. static int mpc8220_fec_init (struct eth_device *dev, bd_t * bis)
  213. {
  214. mpc8220_fec_priv *fec = (mpc8220_fec_priv *) dev->priv;
  215. struct mpc8220_dma *dma = (struct mpc8220_dma *) MMAP_DMA;
  216. const u8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
  217. #ifdef DEBUG
  218. printf ("mpc8220_fec_init... Begin\n");
  219. #endif
  220. /*
  221. * Initialize RxBD/TxBD rings
  222. */
  223. mpc8220_fec_rbd_init (fec);
  224. mpc8220_fec_tbd_init (fec);
  225. /*
  226. * Set up Pin Muxing for FEC 1
  227. */
  228. *(vu_long *) MMAP_PCFG = 0;
  229. *(vu_long *) (MMAP_PCFG + 4) = 0;
  230. /*
  231. * Clear FEC-Lite interrupt event register(IEVENT)
  232. */
  233. fec->eth->ievent = 0xffffffff;
  234. /*
  235. * Set interrupt mask register
  236. */
  237. fec->eth->imask = 0x00000000;
  238. /*
  239. * Set FEC-Lite receive control register(R_CNTRL):
  240. */
  241. if (fec->xcv_type == SEVENWIRE) {
  242. /*
  243. * Frame length=1518; 7-wire mode
  244. */
  245. fec->eth->r_cntrl = 0x05ee0020; /*0x05ee0000;FIXME */
  246. } else {
  247. /*
  248. * Frame length=1518; MII mode;
  249. */
  250. fec->eth->r_cntrl = 0x05ee0024; /*0x05ee0004;FIXME */
  251. }
  252. fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
  253. if (fec->xcv_type != SEVENWIRE) {
  254. /*
  255. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  256. * and do not drop the Preamble.
  257. */
  258. /* tbd - rtm */
  259. /*fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); */
  260. /* No MII for 7-wire mode */
  261. fec->eth->mii_speed = 0x00000030;
  262. }
  263. /*
  264. * Set Opcode/Pause Duration Register
  265. */
  266. fec->eth->op_pause = 0x00010020; /*FIXME0xffff0020; */
  267. /*
  268. * Set Rx FIFO alarm and granularity value
  269. */
  270. fec->eth->rfifo_cntrl = 0x0c000000;
  271. fec->eth->rfifo_alarm = 0x0000030c;
  272. #ifdef DEBUG
  273. if (fec->eth->rfifo_status & 0x00700000) {
  274. printf ("mpc8220_fec_init() RFIFO error\n");
  275. }
  276. #endif
  277. /*
  278. * Set Tx FIFO granularity value
  279. */
  280. /*fec->eth->tfifo_cntrl = 0x0c000000; */ /*tbd - rtm */
  281. fec->eth->tfifo_cntrl = 0x0e000000;
  282. #ifdef DEBUG
  283. printf ("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
  284. printf ("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
  285. #endif
  286. /*
  287. * Set transmit fifo watermark register(X_WMRK), default = 64
  288. */
  289. fec->eth->tfifo_alarm = 0x00000080;
  290. fec->eth->x_wmrk = 0x2;
  291. /*
  292. * Set individual address filter for unicast address
  293. * and set physical address registers.
  294. */
  295. mpc8220_fec_set_hwaddr (fec, (char *)(dev->enetaddr));
  296. /*
  297. * Set multicast address filter
  298. */
  299. fec->eth->gaddr1 = 0x00000000;
  300. fec->eth->gaddr2 = 0x00000000;
  301. /*
  302. * Turn ON cheater FSM: ????
  303. */
  304. fec->eth->xmit_fsm = 0x03000000;
  305. #if 1
  306. /*#if defined(CONFIG_MPC5200)*/
  307. /*
  308. * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
  309. * work w/ the current receive task.
  310. */
  311. dma->PtdCntrl |= 0x00000001;
  312. #endif
  313. /*
  314. * Set priority of different initiators
  315. */
  316. dma->IPR0 = 7; /* always */
  317. dma->IPR3 = 6; /* Eth RX */
  318. dma->IPR4 = 5; /* Eth Tx */
  319. /*
  320. * Clear SmartDMA task interrupt pending bits
  321. */
  322. DMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
  323. /*
  324. * Initialize SmartDMA parameters stored in SRAM
  325. */
  326. *(int *) FEC_TBD_BASE = (int) fec->tbdBase;
  327. *(int *) FEC_RBD_BASE = (int) fec->rbdBase;
  328. *(int *) FEC_TBD_NEXT = (int) fec->tbdBase;
  329. *(int *) FEC_RBD_NEXT = (int) fec->rbdBase;
  330. if (fec->xcv_type != SEVENWIRE) {
  331. /*
  332. * Initialize PHY(LXT971A):
  333. *
  334. * Generally, on power up, the LXT971A reads its configuration
  335. * pins to check for forced operation, If not cofigured for
  336. * forced operation, it uses auto-negotiation/parallel detection
  337. * to automatically determine line operating conditions.
  338. * If the PHY device on the other side of the link supports
  339. * auto-negotiation, the LXT971A auto-negotiates with it
  340. * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
  341. * support auto-negotiation, the LXT971A automatically detects
  342. * the presence of either link pulses(10Mbps PHY) or Idle
  343. * symbols(100Mbps) and sets its operating conditions accordingly.
  344. *
  345. * When auto-negotiation is controlled by software, the following
  346. * steps are recommended.
  347. *
  348. * Note:
  349. * The physical address is dependent on hardware configuration.
  350. *
  351. */
  352. int timeout = 1;
  353. u16 phyStatus;
  354. /*
  355. * Reset PHY, then delay 300ns
  356. */
  357. miiphy_write (dev->name, phyAddr, 0x0, 0x8000);
  358. udelay (1000);
  359. if (fec->xcv_type == MII10) {
  360. /*
  361. * Force 10Base-T, FDX operation
  362. */
  363. #ifdef DEBUG
  364. printf ("Forcing 10 Mbps ethernet link... ");
  365. #endif
  366. miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
  367. /*
  368. miiphy_write(fec, phyAddr, 0x0, 0x0100);
  369. */
  370. miiphy_write (dev->name, phyAddr, 0x0, 0x0180);
  371. timeout = 20;
  372. do { /* wait for link status to go down */
  373. udelay (10000);
  374. if ((timeout--) == 0) {
  375. #ifdef DEBUG
  376. printf ("hmmm, should not have waited...");
  377. #endif
  378. break;
  379. }
  380. miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
  381. #ifdef DEBUG
  382. printf ("=");
  383. #endif
  384. } while ((phyStatus & 0x0004)); /* !link up */
  385. timeout = 1000;
  386. do { /* wait for link status to come back up */
  387. udelay (10000);
  388. if ((timeout--) == 0) {
  389. printf ("failed. Link is down.\n");
  390. break;
  391. }
  392. miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
  393. #ifdef DEBUG
  394. printf ("+");
  395. #endif
  396. } while (!(phyStatus & 0x0004)); /* !link up */
  397. #ifdef DEBUG
  398. printf ("done.\n");
  399. #endif
  400. } else { /* MII100 */
  401. /*
  402. * Set the auto-negotiation advertisement register bits
  403. */
  404. miiphy_write (dev->name, phyAddr, 0x4, 0x01e1);
  405. /*
  406. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  407. */
  408. miiphy_write (dev->name, phyAddr, 0x0, 0x1200);
  409. /*
  410. * Wait for AN completion
  411. */
  412. timeout = 5000;
  413. do {
  414. udelay (1000);
  415. if ((timeout--) == 0) {
  416. #ifdef DEBUG
  417. printf ("PHY auto neg 0 failed...\n");
  418. #endif
  419. return -1;
  420. }
  421. if (miiphy_read (dev->name, phyAddr, 0x1, &phyStatus) !=
  422. 0) {
  423. #ifdef DEBUG
  424. printf ("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  425. #endif
  426. return -1;
  427. }
  428. } while (!(phyStatus & 0x0004));
  429. #ifdef DEBUG
  430. printf ("PHY auto neg complete! \n");
  431. #endif
  432. }
  433. }
  434. /*
  435. * Enable FEC-Lite controller
  436. */
  437. fec->eth->ecntrl |= 0x00000006;
  438. #ifdef DEBUG
  439. if (fec->xcv_type != SEVENWIRE)
  440. mpc8220_fec_phydump (dev->name);
  441. #endif
  442. /*
  443. * Enable SmartDMA receive task
  444. */
  445. DMA_TASK_ENABLE (FEC_RECV_TASK_NO);
  446. #ifdef DEBUG
  447. printf ("mpc8220_fec_init... Done \n");
  448. #endif
  449. return 1;
  450. }
  451. /********************************************************************/
  452. static void mpc8220_fec_halt (struct eth_device *dev)
  453. {
  454. mpc8220_fec_priv *fec = (mpc8220_fec_priv *) dev->priv;
  455. int counter = 0xffff;
  456. #ifdef DEBUG
  457. if (fec->xcv_type != SEVENWIRE)
  458. mpc8220_fec_phydump (dev->name);
  459. #endif
  460. /*
  461. * mask FEC chip interrupts
  462. */
  463. fec->eth->imask = 0;
  464. /*
  465. * issue graceful stop command to the FEC transmitter if necessary
  466. */
  467. fec->eth->x_cntrl |= 0x00000001;
  468. /*
  469. * wait for graceful stop to register
  470. */
  471. while ((counter--) && (!(fec->eth->ievent & 0x10000000)));
  472. /*
  473. * Disable SmartDMA tasks
  474. */
  475. DMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
  476. DMA_TASK_DISABLE (FEC_RECV_TASK_NO);
  477. /*
  478. * Disable the Ethernet Controller
  479. */
  480. fec->eth->ecntrl &= 0xfffffffd;
  481. /*
  482. * Clear FIFO status registers
  483. */
  484. fec->eth->rfifo_status &= 0x00700000;
  485. fec->eth->tfifo_status &= 0x00700000;
  486. fec->eth->reset_cntrl = 0x01000000;
  487. /*
  488. * Issue a reset command to the FEC chip
  489. */
  490. fec->eth->ecntrl |= 0x1;
  491. /*
  492. * wait at least 16 clock cycles
  493. */
  494. udelay (10);
  495. #ifdef DEBUG
  496. printf ("Ethernet task stopped\n");
  497. #endif
  498. }
  499. #ifdef DEBUG
  500. /********************************************************************/
  501. static void tfifo_print (char *devname, mpc8220_fec_priv * fec)
  502. {
  503. u16 phyAddr = CONFIG_PHY_ADDR;
  504. u16 phyStatus;
  505. if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
  506. || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
  507. miiphy_read (devname, phyAddr, 0x1, &phyStatus);
  508. printf ("\nphyStatus: 0x%04x\n", phyStatus);
  509. printf ("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  510. printf ("ievent: 0x%08x\n", fec->eth->ievent);
  511. printf ("x_status: 0x%08x\n", fec->eth->x_status);
  512. printf ("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
  513. printf (" control 0x%08x\n", fec->eth->tfifo_cntrl);
  514. printf (" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
  515. printf (" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
  516. printf (" alarm 0x%08x\n", fec->eth->tfifo_alarm);
  517. printf (" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
  518. printf (" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
  519. }
  520. }
  521. static void rfifo_print (char *devname, mpc8220_fec_priv * fec)
  522. {
  523. u16 phyAddr = CONFIG_PHY_ADDR;
  524. u16 phyStatus;
  525. if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
  526. || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
  527. miiphy_read (devname, phyAddr, 0x1, &phyStatus);
  528. printf ("\nphyStatus: 0x%04x\n", phyStatus);
  529. printf ("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  530. printf ("ievent: 0x%08x\n", fec->eth->ievent);
  531. printf ("x_status: 0x%08x\n", fec->eth->x_status);
  532. printf ("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
  533. printf (" control 0x%08x\n", fec->eth->rfifo_cntrl);
  534. printf (" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
  535. printf (" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
  536. printf (" alarm 0x%08x\n", fec->eth->rfifo_alarm);
  537. printf (" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
  538. printf (" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
  539. }
  540. }
  541. #endif /* DEBUG */
  542. /********************************************************************/
  543. static int mpc8220_fec_send (struct eth_device *dev, volatile void *eth_data,
  544. int data_length)
  545. {
  546. /*
  547. * This routine transmits one frame. This routine only accepts
  548. * 6-byte Ethernet addresses.
  549. */
  550. mpc8220_fec_priv *fec = (mpc8220_fec_priv *) dev->priv;
  551. FEC_TBD *pTbd;
  552. #ifdef DEBUG
  553. printf ("tbd status: 0x%04x\n", fec->tbdBase[0].status);
  554. tfifo_print (dev->name, fec);
  555. #endif
  556. /*
  557. * Clear Tx BD ring at first
  558. */
  559. mpc8220_fec_tbd_scrub (fec);
  560. /*
  561. * Check for valid length of data.
  562. */
  563. if ((data_length > 1500) || (data_length <= 0)) {
  564. return -1;
  565. }
  566. /*
  567. * Check the number of vacant TxBDs.
  568. */
  569. if (fec->cleanTbdNum < 1) {
  570. #ifdef DEBUG
  571. printf ("No available TxBDs ...\n");
  572. #endif
  573. return -1;
  574. }
  575. /*
  576. * Get the first TxBD to send the mac header
  577. */
  578. pTbd = &fec->tbdBase[fec->tbdIndex];
  579. pTbd->dataLength = data_length;
  580. pTbd->dataPointer = (u32) eth_data;
  581. pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  582. fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
  583. #ifdef DEBUG
  584. printf ("DMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
  585. #endif
  586. /*
  587. * Kick the MII i/f
  588. */
  589. if (fec->xcv_type != SEVENWIRE) {
  590. u16 phyStatus;
  591. miiphy_read (dev->name, 0, 0x1, &phyStatus);
  592. }
  593. /*
  594. * Enable SmartDMA transmit task
  595. */
  596. #ifdef DEBUG
  597. tfifo_print (dev->name, fec);
  598. #endif
  599. DMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
  600. #ifdef DEBUG
  601. tfifo_print (dev->name, fec);
  602. #endif
  603. #ifdef DEBUG
  604. printf ("+");
  605. #endif
  606. fec->cleanTbdNum -= 1;
  607. #ifdef DEBUG
  608. printf ("smartDMA ethernet Tx task enabled\n");
  609. #endif
  610. /*
  611. * wait until frame is sent .
  612. */
  613. while (pTbd->status & FEC_TBD_READY) {
  614. udelay (10);
  615. #ifdef DEBUG
  616. printf ("TDB status = %04x\n", pTbd->status);
  617. #endif
  618. }
  619. return 0;
  620. }
  621. /********************************************************************/
  622. static int mpc8220_fec_recv (struct eth_device *dev)
  623. {
  624. /*
  625. * This command pulls one frame from the card
  626. */
  627. mpc8220_fec_priv *fec = (mpc8220_fec_priv *) dev->priv;
  628. FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
  629. unsigned long ievent;
  630. int frame_length, len = 0;
  631. NBUF *frame;
  632. #ifdef DEBUG
  633. printf ("mpc8220_fec_recv %d Start...\n", fec->rbdIndex);
  634. printf ("-");
  635. #endif
  636. /*
  637. * Check if any critical events have happened
  638. */
  639. ievent = fec->eth->ievent;
  640. fec->eth->ievent = ievent;
  641. if (ievent & 0x20060000) {
  642. /* BABT, Rx/Tx FIFO errors */
  643. mpc8220_fec_halt (dev);
  644. mpc8220_fec_init (dev, NULL);
  645. return 0;
  646. }
  647. if (ievent & 0x80000000) {
  648. /* Heartbeat error */
  649. fec->eth->x_cntrl |= 0x00000001;
  650. }
  651. if (ievent & 0x10000000) {
  652. /* Graceful stop complete */
  653. if (fec->eth->x_cntrl & 0x00000001) {
  654. mpc8220_fec_halt (dev);
  655. fec->eth->x_cntrl &= ~0x00000001;
  656. mpc8220_fec_init (dev, NULL);
  657. }
  658. }
  659. if (!(pRbd->status & FEC_RBD_EMPTY)) {
  660. if ((pRbd->status & FEC_RBD_LAST)
  661. && !(pRbd->status & FEC_RBD_ERR)
  662. && ((pRbd->dataLength - 4) > 14)) {
  663. /*
  664. * Get buffer address and size
  665. */
  666. frame = (NBUF *) pRbd->dataPointer;
  667. frame_length = pRbd->dataLength - 4;
  668. #if (0)
  669. {
  670. int i;
  671. printf ("recv data hdr:");
  672. for (i = 0; i < 14; i++)
  673. printf ("%x ", *(frame->head + i));
  674. printf ("\n");
  675. }
  676. #endif
  677. /*
  678. * Fill the buffer and pass it to upper layers
  679. */
  680. /* memcpy(buff, frame->head, 14);
  681. memcpy(buff + 14, frame->data, frame_length);*/
  682. NetReceive ((volatile uchar *) pRbd->dataPointer,
  683. frame_length);
  684. len = frame_length;
  685. }
  686. /*
  687. * Reset buffer descriptor as empty
  688. */
  689. mpc8220_fec_rbd_clean (fec, pRbd);
  690. }
  691. DMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
  692. return len;
  693. }
  694. /********************************************************************/
  695. int mpc8220_fec_initialize (bd_t * bis)
  696. {
  697. mpc8220_fec_priv *fec;
  698. #ifdef CONFIG_HAS_ETH1
  699. mpc8220_fec_priv *fec2;
  700. #endif
  701. struct eth_device *dev;
  702. char *tmp, *end;
  703. char env_enetaddr[6];
  704. #ifdef CONFIG_HAS_ETH1
  705. char env_enet1addr[6];
  706. #endif
  707. int i;
  708. fec = (mpc8220_fec_priv *) malloc (sizeof (*fec));
  709. dev = (struct eth_device *) malloc (sizeof (*dev));
  710. memset (dev, 0, sizeof *dev);
  711. fec->eth = (ethernet_regs *) MMAP_FEC1;
  712. #ifdef CONFIG_HAS_ETH1
  713. fec2 = (mpc8220_fec_priv *) malloc (sizeof (*fec));
  714. fec2->eth = (ethernet_regs *) MMAP_FEC2;
  715. #endif
  716. fec->tbdBase = (FEC_TBD *) FEC_BD_BASE;
  717. fec->rbdBase =
  718. (FEC_RBD *) (FEC_BD_BASE + FEC_TBD_NUM * sizeof (FEC_TBD));
  719. fec->xcv_type = MII100;
  720. dev->priv = (void *) fec;
  721. dev->iobase = MMAP_FEC1;
  722. dev->init = mpc8220_fec_init;
  723. dev->halt = mpc8220_fec_halt;
  724. dev->send = mpc8220_fec_send;
  725. dev->recv = mpc8220_fec_recv;
  726. sprintf (dev->name, "FEC ETHERNET");
  727. eth_register (dev);
  728. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  729. miiphy_register (dev->name,
  730. fec8220_miiphy_read, fec8220_miiphy_write);
  731. #endif
  732. /*
  733. * Try to set the mac address now. The fec mac address is
  734. * a garbage after reset. When not using fec for booting
  735. * the Linux fec driver will try to work with this garbage.
  736. */
  737. tmp = getenv ("ethaddr");
  738. if (tmp) {
  739. for (i = 0; i < 6; i++) {
  740. env_enetaddr[i] =
  741. tmp ? simple_strtoul (tmp, &end, 16) : 0;
  742. if (tmp)
  743. tmp = (*end) ? end + 1 : end;
  744. }
  745. mpc8220_fec_set_hwaddr (fec, env_enetaddr);
  746. }
  747. #ifdef CONFIG_HAS_ETH1
  748. tmp = getenv ("eth1addr");
  749. if (tmp) {
  750. for (i = 0; i < 6; i++) {
  751. env_enet1addr[i] =
  752. tmp ? simple_strtoul (tmp, &end, 16) : 0;
  753. if (tmp)
  754. tmp = (*end) ? end + 1 : end;
  755. }
  756. mpc8220_fec_set_hwaddr (fec2, env_enet1addr);
  757. }
  758. #endif
  759. return 1;
  760. }
  761. /* MII-interface related functions */
  762. /********************************************************************/
  763. int fec8220_miiphy_read (char *devname, u8 phyAddr, u8 regAddr, u16 * retVal)
  764. {
  765. ethernet_regs *eth = (ethernet_regs *) MMAP_FEC1;
  766. u32 reg; /* convenient holder for the PHY register */
  767. u32 phy; /* convenient holder for the PHY */
  768. int timeout = 0xffff;
  769. /*
  770. * reading from any PHY's register is done by properly
  771. * programming the FEC's MII data register.
  772. */
  773. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  774. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  775. eth->mii_data =
  776. (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy
  777. | reg);
  778. /*
  779. * wait for the related interrupt
  780. */
  781. while ((timeout--) && (!(eth->ievent & 0x00800000)));
  782. if (timeout == 0) {
  783. #ifdef DEBUG
  784. printf ("Read MDIO failed...\n");
  785. #endif
  786. return -1;
  787. }
  788. /*
  789. * clear mii interrupt bit
  790. */
  791. eth->ievent = 0x00800000;
  792. /*
  793. * it's now safe to read the PHY's register
  794. */
  795. *retVal = (u16) eth->mii_data;
  796. return 0;
  797. }
  798. /********************************************************************/
  799. int fec8220_miiphy_write (char *devname, u8 phyAddr, u8 regAddr, u16 data)
  800. {
  801. ethernet_regs *eth = (ethernet_regs *) MMAP_FEC1;
  802. u32 reg; /* convenient holder for the PHY register */
  803. u32 phy; /* convenient holder for the PHY */
  804. int timeout = 0xffff;
  805. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  806. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  807. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
  808. FEC_MII_DATA_TA | phy | reg | data);
  809. /*
  810. * wait for the MII interrupt
  811. */
  812. while ((timeout--) && (!(eth->ievent & 0x00800000)));
  813. if (timeout == 0) {
  814. #ifdef DEBUG
  815. printf ("Write MDIO failed...\n");
  816. #endif
  817. return -1;
  818. }
  819. /*
  820. * clear MII interrupt bit
  821. */
  822. eth->ievent = 0x00800000;
  823. return 0;
  824. }
  825. #ifdef DEBUG
  826. static u32 local_crc32 (char *string, unsigned int crc_value, int len)
  827. {
  828. int i;
  829. char c;
  830. unsigned int crc, count;
  831. /*
  832. * crc32 algorithm
  833. */
  834. /*
  835. * crc = 0xffffffff; * The initialized value should be 0xffffffff
  836. */
  837. crc = crc_value;
  838. for (i = len; --i >= 0;) {
  839. c = *string++;
  840. for (count = 0; count < 8; count++) {
  841. if ((c & 0x01) ^ (crc & 0x01)) {
  842. crc >>= 1;
  843. crc = crc ^ 0xedb88320;
  844. } else {
  845. crc >>= 1;
  846. }
  847. c >>= 1;
  848. }
  849. }
  850. /*
  851. * In big endian system, do byte swaping for crc value
  852. */
  853. return crc;
  854. }
  855. #endif /* DEBUG */
  856. #endif /* CONFIG_MPC8220_FEC */