fec.c 22 KB

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