fec.c 22 KB

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