fec.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  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 <mpc5xxx.h>
  10. #include <malloc.h>
  11. #include <net.h>
  12. #include <miiphy.h>
  13. #include "sdma.h"
  14. #include "fec.h"
  15. #define DEBUG 0x8
  16. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
  17. defined(CONFIG_MPC5XXX_FEC)
  18. #if (DEBUG & 0x60)
  19. static void tfifo_print(mpc5xxx_fec_priv *fec);
  20. static void rfifo_print(mpc5xxx_fec_priv *fec);
  21. #endif /* DEBUG */
  22. #if (DEBUG & 0x40)
  23. static uint32 local_crc32(char *string, unsigned int crc_value, int len);
  24. #endif
  25. /********************************************************************/
  26. static int mpc5xxx_fec_rbd_init(mpc5xxx_fec_priv *fec)
  27. {
  28. int ix;
  29. char *data;
  30. /*
  31. * the receive ring is located right after the transmit one
  32. */
  33. for (ix = 0; ix < FEC_RBD_NUM; ix++) {
  34. data = (char *)malloc(FEC_MAX_PKT_SIZE);
  35. if (data == NULL) {
  36. printf ("RBD INIT FAILED\n");
  37. return -1;
  38. }
  39. fec->rbdBase[ix].status = FEC_RBD_EMPTY;
  40. fec->rbdBase[ix].dataLength = 0;
  41. fec->rbdBase[ix].dataPointer = (uint32)data;
  42. }
  43. /*
  44. * have the last RBD to close the ring
  45. */
  46. fec->rbdBase[ix - 1].status |= FEC_RBD_WRAP;
  47. fec->rbdIndex = 0;
  48. return 0;
  49. }
  50. /********************************************************************/
  51. static void mpc5xxx_fec_tbd_init(mpc5xxx_fec_priv *fec)
  52. {
  53. int ix;
  54. for (ix = 0; ix < FEC_TBD_NUM; ix++) {
  55. fec->tbdBase[ix].status = 0;
  56. }
  57. /*
  58. * Have the last TBD to close the ring
  59. */
  60. fec->tbdBase[ix - 1].status |= FEC_TBD_WRAP;
  61. /*
  62. * Initialize some indices
  63. */
  64. fec->tbdIndex = 0;
  65. fec->usedTbdIndex = 0;
  66. fec->cleanTbdNum = FEC_TBD_NUM;
  67. }
  68. /********************************************************************/
  69. static void mpc5xxx_fec_rbd_clean(mpc5xxx_fec_priv *fec, FEC_RBD * pRbd)
  70. {
  71. /*
  72. * Reset buffer descriptor as empty
  73. */
  74. if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
  75. pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
  76. else
  77. pRbd->status = FEC_RBD_EMPTY;
  78. pRbd->dataLength = 0;
  79. /*
  80. * Now, we have an empty RxBD, restart the SmartDMA receive task
  81. */
  82. SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
  83. /*
  84. * Increment BD count
  85. */
  86. fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
  87. }
  88. /********************************************************************/
  89. static void mpc5xxx_fec_tbd_scrub(mpc5xxx_fec_priv *fec)
  90. {
  91. FEC_TBD *pUsedTbd;
  92. #if (DEBUG & 0x1)
  93. printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
  94. fec->cleanTbdNum, fec->usedTbdIndex);
  95. #endif
  96. /*
  97. * process all the consumed TBDs
  98. */
  99. while (fec->cleanTbdNum < FEC_TBD_NUM) {
  100. pUsedTbd = &fec->tbdBase[fec->usedTbdIndex];
  101. if (pUsedTbd->status & FEC_TBD_READY) {
  102. #if (DEBUG & 0x20)
  103. printf("Cannot clean TBD %d, in use\n", fec->cleanTbdNum);
  104. #endif
  105. return;
  106. }
  107. /*
  108. * clean this buffer descriptor
  109. */
  110. if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
  111. pUsedTbd->status = FEC_TBD_WRAP;
  112. else
  113. pUsedTbd->status = 0;
  114. /*
  115. * update some indeces for a correct handling of the TBD ring
  116. */
  117. fec->cleanTbdNum++;
  118. fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
  119. }
  120. }
  121. /********************************************************************/
  122. static void mpc5xxx_fec_set_hwaddr(mpc5xxx_fec_priv *fec, char *mac)
  123. {
  124. uint8 currByte; /* byte for which to compute the CRC */
  125. int byte; /* loop - counter */
  126. int bit; /* loop - counter */
  127. uint32 crc = 0xffffffff; /* initial value */
  128. /*
  129. * The algorithm used is the following:
  130. * we loop on each of the six bytes of the provided address,
  131. * and we compute the CRC by left-shifting the previous
  132. * value by one position, so that each bit in the current
  133. * byte of the address may contribute the calculation. If
  134. * the latter and the MSB in the CRC are different, then
  135. * the CRC value so computed is also ex-ored with the
  136. * "polynomium generator". The current byte of the address
  137. * is also shifted right by one bit at each iteration.
  138. * This is because the CRC generatore in hardware is implemented
  139. * as a shift-register with as many ex-ores as the radixes
  140. * in the polynomium. This suggests that we represent the
  141. * polynomiumm itself as a 32-bit constant.
  142. */
  143. for (byte = 0; byte < 6; byte++) {
  144. currByte = mac[byte];
  145. for (bit = 0; bit < 8; bit++) {
  146. if ((currByte & 0x01) ^ (crc & 0x01)) {
  147. crc >>= 1;
  148. crc = crc ^ 0xedb88320;
  149. } else {
  150. crc >>= 1;
  151. }
  152. currByte >>= 1;
  153. }
  154. }
  155. crc = crc >> 26;
  156. /*
  157. * Set individual hash table register
  158. */
  159. if (crc >= 32) {
  160. fec->eth->iaddr1 = (1 << (crc - 32));
  161. fec->eth->iaddr2 = 0;
  162. } else {
  163. fec->eth->iaddr1 = 0;
  164. fec->eth->iaddr2 = (1 << crc);
  165. }
  166. /*
  167. * Set physical address
  168. */
  169. fec->eth->paddr1 = (mac[0] << 24) + (mac[1] << 16) + (mac[2] << 8) + mac[3];
  170. fec->eth->paddr2 = (mac[4] << 24) + (mac[5] << 16) + 0x8808;
  171. }
  172. /********************************************************************/
  173. static int mpc5xxx_fec_init(struct eth_device *dev, bd_t * bis)
  174. {
  175. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  176. struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
  177. const uint8 phyAddr = 0; /* Only one PHY */
  178. #if (DEBUG & 0x1)
  179. printf ("mpc5xxx_fec_init... Begin\n");
  180. #endif
  181. /*
  182. * Initialize RxBD/TxBD rings
  183. */
  184. mpc5xxx_fec_rbd_init(fec);
  185. mpc5xxx_fec_tbd_init(fec);
  186. /*
  187. * Initialize GPIO pins
  188. */
  189. if (fec->xcv_type == SEVENWIRE) {
  190. /* 10MBit with 7-wire operation */
  191. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00020000;
  192. } else {
  193. /* 100MBit with MD operation */
  194. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00050000;
  195. }
  196. /*
  197. * Clear FEC-Lite interrupt event register(IEVENT)
  198. */
  199. fec->eth->ievent = 0xffffffff;
  200. /*
  201. * Set interrupt mask register
  202. */
  203. fec->eth->imask = 0x00000000;
  204. /*
  205. * Set FEC-Lite receive control register(R_CNTRL):
  206. */
  207. if (fec->xcv_type == SEVENWIRE) {
  208. /*
  209. * Frame length=1518; 7-wire mode
  210. */
  211. fec->eth->r_cntrl = 0x05ee0020; /*0x05ee0000;FIXME */
  212. } else {
  213. /*
  214. * Frame length=1518; MII mode;
  215. */
  216. fec->eth->r_cntrl = 0x05ee0024; /*0x05ee0004;FIXME */
  217. }
  218. if (fec->xcv_type == SEVENWIRE) {
  219. /*
  220. * Set FEC-Lite transmit control register(X_CNTRL):
  221. */
  222. /*fec->eth->x_cntrl = 0x00000002; */ /* half-duplex, heartbeat */
  223. fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
  224. } else {
  225. /*fec->eth->x_cntrl = 0x00000006; */ /* full-duplex, heartbeat */
  226. fec->eth->x_cntrl = 0x00000004; /* full-duplex, heartbeat disabled */
  227. /*
  228. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock(25Mhz)
  229. * and do not drop the Preamble.
  230. */
  231. fec->eth->mii_speed = (0x5 << 1); /* No MII for 7-wire mode */
  232. }
  233. /*
  234. * Set Opcode/Pause Duration Register
  235. */
  236. fec->eth->op_pause = 0x00010020; /*FIXME0xffff0020; */
  237. /*
  238. * Set Rx FIFO alarm and granularity value
  239. */
  240. fec->eth->rfifo_cntrl = 0x0c000000;
  241. fec->eth->rfifo_alarm = 0x0000030c;
  242. #if (DEBUG & 0x22)
  243. if (fec->eth->rfifo_status & 0x00700000 ) {
  244. printf("mpc5xxx_fec_init() RFIFO error\n");
  245. }
  246. #endif
  247. /*
  248. * Set Tx FIFO granularity value
  249. */
  250. fec->eth->tfifo_cntrl = 0x0c000000;
  251. #if (DEBUG & 0x2)
  252. printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
  253. printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
  254. #endif
  255. /*
  256. * Set transmit fifo watermark register(X_WMRK), default = 64
  257. */
  258. fec->eth->tfifo_alarm = 0x00000080;
  259. fec->eth->x_wmrk = 0x2;
  260. /*
  261. * Set individual address filter for unicast address
  262. * and set physical address registers.
  263. */
  264. mpc5xxx_fec_set_hwaddr(fec, dev->enetaddr);
  265. /*
  266. * Set multicast address filter
  267. */
  268. fec->eth->gaddr1 = 0x00000000;
  269. fec->eth->gaddr2 = 0x00000000;
  270. /*
  271. * Turn ON cheater FSM: ????
  272. */
  273. fec->eth->xmit_fsm = 0x03000000;
  274. #if defined(CONFIG_MPC5200)
  275. /*
  276. * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
  277. * work w/ the current receive task.
  278. */
  279. sdma->PtdCntrl |= 0x00000001;
  280. #endif
  281. /*
  282. * Set priority of different initiators
  283. */
  284. sdma->IPR0 = 7; /* always */
  285. sdma->IPR3 = 6; /* Eth RX */
  286. sdma->IPR4 = 5; /* Eth Tx */
  287. /*
  288. * Clear SmartDMA task interrupt pending bits
  289. */
  290. SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
  291. /*
  292. * Set SmartDMA intMask register to enable SmartDMA task interrupts
  293. */
  294. SDMA_INT_ENABLE(FEC_RECV_TASK_NO);
  295. /*
  296. * Initialize SmartDMA parameters stored in SRAM
  297. */
  298. *(int *)FEC_TBD_BASE = (int)fec->tbdBase;
  299. *(int *)FEC_RBD_BASE = (int)fec->rbdBase;
  300. *(int *)FEC_TBD_NEXT = (int)fec->tbdBase;
  301. *(int *)FEC_RBD_NEXT = (int)fec->rbdBase;
  302. if (fec->xcv_type != SEVENWIRE) {
  303. /*
  304. * Initialize PHY(LXT971A):
  305. *
  306. * Generally, on power up, the LXT971A reads its configuration
  307. * pins to check for forced operation, If not cofigured for
  308. * forced operation, it uses auto-negotiation/parallel detection
  309. * to automatically determine line operating conditions.
  310. * If the PHY device on the other side of the link supports
  311. * auto-negotiation, the LXT971A auto-negotiates with it
  312. * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
  313. * support auto-negotiation, the LXT971A automatically detects
  314. * the presence of either link pulses(10Mbps PHY) or Idle
  315. * symbols(100Mbps) and sets its operating conditions accordingly.
  316. *
  317. * When auto-negotiation is controlled by software, the following
  318. * steps are recommended.
  319. *
  320. * Note:
  321. * The physical address is dependent on hardware configuration.
  322. *
  323. */
  324. int timeout = 1;
  325. uint16 phyStatus;
  326. /*
  327. * Reset PHY, then delay 300ns
  328. */
  329. miiphy_write(phyAddr, 0x0, 0x8000);
  330. udelay(1000);
  331. if (fec->xcv_type == MII10) {
  332. /*
  333. * Force 10Base-T, FDX operation
  334. */
  335. printf("Forcing 10 Mbps ethernet link... ");
  336. miiphy_read(phyAddr, 0x1, &phyStatus);
  337. /*
  338. miiphy_write(fec, phyAddr, 0x0, 0x0100);
  339. */
  340. miiphy_write(phyAddr, 0x0, 0x0180);
  341. timeout = 20;
  342. do { /* wait for link status to go down */
  343. udelay(10000);
  344. if ((timeout--) == 0) {
  345. #if (DEBUG & 0x2)
  346. printf("hmmm, should not have waited...");
  347. #endif
  348. break;
  349. }
  350. miiphy_read(phyAddr, 0x1, &phyStatus);
  351. #if (DEBUG & 0x2)
  352. printf("=");
  353. #endif
  354. } while ((phyStatus & 0x0004)); /* !link up */
  355. timeout = 1000;
  356. do { /* wait for link status to come back up */
  357. udelay(10000);
  358. if ((timeout--) == 0) {
  359. printf("failed. Link is down.\n");
  360. break;
  361. }
  362. miiphy_read(phyAddr, 0x1, &phyStatus);
  363. #if (DEBUG & 0x2)
  364. printf("+");
  365. #endif
  366. } while (!(phyStatus & 0x0004)); /* !link up */
  367. printf ("done.\n");
  368. } else { /* MII100 */
  369. /*
  370. * Set the auto-negotiation advertisement register bits
  371. */
  372. miiphy_write(phyAddr, 0x4, 0x01e1);
  373. /*
  374. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  375. */
  376. miiphy_write(phyAddr, 0x0, 0x1200);
  377. /*
  378. * Wait for AN completion
  379. */
  380. timeout = 5000;
  381. do {
  382. udelay(1000);
  383. if ((timeout--) == 0) {
  384. #if (DEBUG & 0x2)
  385. printf("PHY auto neg 0 failed...\n");
  386. #endif
  387. return -1;
  388. }
  389. if (miiphy_read(phyAddr, 0x1, &phyStatus) != 0) {
  390. #if (DEBUG & 0x2)
  391. printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  392. #endif
  393. return -1;
  394. }
  395. } while ((phyStatus & 0x0020) != 0x0020);
  396. #if (DEBUG & 0x2)
  397. printf("PHY auto neg complete! \n");
  398. #endif
  399. }
  400. }
  401. /*
  402. * Enable FEC-Lite controller
  403. */
  404. fec->eth->ecntrl |= 0x00000006;
  405. if (fec->xcv_type != SEVENWIRE) {
  406. #if (DEBUG & 0x2)
  407. uint16 phyStatus, i;
  408. uint8 phyAddr = 0;
  409. for (i = 0; i < 9; i++) {
  410. miiphy_read(phyAddr, i, &phyStatus);
  411. printf("Mii reg %d: 0x%04x\n", i, phyStatus);
  412. }
  413. for (i = 16; i < 21; i++) {
  414. miiphy_read(phyAddr, i, &phyStatus);
  415. printf("Mii reg %d: 0x%04x\n", i, phyStatus);
  416. }
  417. #endif
  418. }
  419. /*
  420. * Enable SmartDMA receive task
  421. */
  422. SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
  423. #if (DEBUG & 0x1)
  424. printf("mpc5xxx_fec_init... Done \n");
  425. #endif
  426. return 0;
  427. }
  428. /********************************************************************/
  429. static void mpc5xxx_fec_halt(struct eth_device *dev)
  430. {
  431. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  432. struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
  433. int counter = 0xffff;
  434. #if (DEBUG & 0x2)
  435. if (fec->xcv_type != SEVENWIRE) {
  436. uint16 phyStatus, i;
  437. uint8 phyAddr = 0;
  438. for (i = 0; i < 9; i++) {
  439. miiphy_read(phyAddr, i, &phyStatus);
  440. printf("Mii reg %d: 0x%04x\n", i, phyStatus);
  441. }
  442. for (i = 16; i < 21; i++) {
  443. miiphy_read(phyAddr, i, &phyStatus);
  444. printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
  445. }
  446. }
  447. #endif
  448. /*
  449. * mask FEC chip interrupts
  450. */
  451. fec->eth->imask = 0;
  452. /*
  453. * issue graceful stop command to the FEC transmitter if necessary
  454. */
  455. fec->eth->x_cntrl |= 0x00000001;
  456. /*
  457. * wait for graceful stop to register
  458. */
  459. while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
  460. SDMA_INT_DISABLE (FEC_RECV_TASK_NO);
  461. /*
  462. * Disable SmartDMA tasks
  463. */
  464. SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
  465. SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
  466. #if defined(CONFIG_MPC5200)
  467. /*
  468. * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
  469. * done. It doesn't work w/ the current receive task.
  470. */
  471. sdma->PtdCntrl &= ~0x00000001;
  472. #endif
  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(mpc5xxx_fec_priv *fec)
  498. {
  499. uint16 phyAddr = 0;
  500. uint16 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(mpc5xxx_fec_priv *fec)
  518. {
  519. uint16 phyAddr = 0;
  520. uint16 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 mpc5xxx_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. mpc5xxx_fec_priv *fec = (mpc5xxx_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. mpc5xxx_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 = (uint32)eth_data;
  577. pTbd->status |= FEC_TBD_READY;
  578. fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
  579. #if (DEBUG & 0x100)
  580. printf("SDMA_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. uint16 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. SDMA_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 mpc5xxx_fec_recv(struct eth_device *dev)
  619. {
  620. /*
  621. * This command pulls one frame from the card
  622. */
  623. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  624. FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
  625. unsigned long ievent;
  626. int frame_length;
  627. char *frame;
  628. #if (DEBUG & 0x1)
  629. printf ("mpc5xxx_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. mpc5xxx_fec_halt(dev);
  642. mpc5xxx_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. mpc5xxx_fec_halt(dev);
  653. fec->eth->x_cntrl &= ~0x00000001;
  654. mpc5xxx_fec_init(dev, NULL);
  655. }
  656. }
  657. /*
  658. * Do we have data in Rx FIFO?
  659. */
  660. if ((pRbd->status & FEC_RBD_EMPTY) || !(pRbd->status & FEC_RBD_LAST)){
  661. return 0;
  662. }
  663. /*
  664. * Pass the packet up only if reception was Ok
  665. */
  666. if ((pRbd->dataLength <= 14) || (pRbd->status & FEC_RBD_ERR)) {
  667. mpc5xxx_fec_rbd_clean(fec, pRbd);
  668. #if (DEBUG & 0x8)
  669. printf( "X0" );
  670. #endif
  671. return 0;
  672. }
  673. /*
  674. * Get buffer address and size
  675. */
  676. frame = (char *)pRbd->dataPointer;
  677. frame_length = pRbd->dataLength;
  678. /*
  679. * Pass the buffer to upper layers
  680. */
  681. NetReceive(frame, frame_length);
  682. /*
  683. * Reset buffer descriptor as empty
  684. */
  685. mpc5xxx_fec_rbd_clean(fec, pRbd);
  686. return frame_length;
  687. }
  688. /********************************************************************/
  689. int mpc5xxx_fec_initialize(bd_t * bis)
  690. {
  691. mpc5xxx_fec_priv *fec;
  692. struct eth_device *dev;
  693. fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
  694. dev = (struct eth_device *)malloc(sizeof(*dev));
  695. fec->eth = (ethernet_regs *)MPC5XXX_FEC;
  696. fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
  697. fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
  698. #ifdef CONFIG_ICECUBE
  699. fec->xcv_type = MII100;
  700. #endif
  701. dev->priv = (void *)fec;
  702. dev->iobase = MPC5XXX_FEC;
  703. dev->init = mpc5xxx_fec_init;
  704. dev->halt = mpc5xxx_fec_halt;
  705. dev->send = mpc5xxx_fec_send;
  706. dev->recv = mpc5xxx_fec_recv;
  707. eth_register(dev);
  708. return 1;
  709. }
  710. /* MII-interface related functions */
  711. /********************************************************************/
  712. int miiphy_read(uint8 phyAddr, uint8 regAddr, uint16 * retVal)
  713. {
  714. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  715. uint32 reg; /* convenient holder for the PHY register */
  716. uint32 phy; /* convenient holder for the PHY */
  717. int timeout = 0xffff;
  718. /*
  719. * reading from any PHY's register is done by properly
  720. * programming the FEC's MII data register.
  721. */
  722. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  723. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  724. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
  725. /*
  726. * wait for the related interrupt
  727. */
  728. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  729. if (timeout == 0) {
  730. #if (DEBUG & 0x2)
  731. printf ("Read MDIO failed...\n");
  732. #endif
  733. return -1;
  734. }
  735. /*
  736. * clear mii interrupt bit
  737. */
  738. eth->ievent = 0x00800000;
  739. /*
  740. * it's now safe to read the PHY's register
  741. */
  742. *retVal = (uint16) eth->mii_data;
  743. return 0;
  744. }
  745. /********************************************************************/
  746. int miiphy_write(uint8 phyAddr, uint8 regAddr, uint16 data)
  747. {
  748. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  749. uint32 reg; /* convenient holder for the PHY register */
  750. uint32 phy; /* convenient holder for the PHY */
  751. int timeout = 0xffff;
  752. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  753. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  754. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
  755. FEC_MII_DATA_TA | phy | reg | data);
  756. /*
  757. * wait for the MII interrupt
  758. */
  759. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  760. if (timeout == 0) {
  761. #if (DEBUG & 0x2)
  762. printf ("Write MDIO failed...\n");
  763. #endif
  764. return -1;
  765. }
  766. /*
  767. * clear MII interrupt bit
  768. */
  769. eth->ievent = 0x00800000;
  770. return 0;
  771. }
  772. #if (DEBUG & 0x40)
  773. static uint32 local_crc32(char *string, unsigned int crc_value, int len)
  774. {
  775. int i;
  776. char c;
  777. unsigned int crc, count;
  778. /*
  779. * crc32 algorithm
  780. */
  781. /*
  782. * crc = 0xffffffff; * The initialized value should be 0xffffffff
  783. */
  784. crc = crc_value;
  785. for (i = len; --i >= 0;) {
  786. c = *string++;
  787. for (count = 0; count < 8; count++) {
  788. if ((c & 0x01) ^ (crc & 0x01)) {
  789. crc >>= 1;
  790. crc = crc ^ 0xedb88320;
  791. } else {
  792. crc >>= 1;
  793. }
  794. c >>= 1;
  795. }
  796. }
  797. /*
  798. * In big endian system, do byte swaping for crc value
  799. */
  800. /**/ return crc;
  801. }
  802. #endif /* DEBUG */
  803. #endif /* CONFIG_MPC5XXX_FEC */