fec.c 22 KB

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