fec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. DECLARE_GLOBAL_DATA_PTR;
  183. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  184. struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
  185. const uint8 phyAddr = 0; /* Only one PHY */
  186. #if (DEBUG & 0x1)
  187. printf ("mpc5xxx_fec_init... Begin\n");
  188. #endif
  189. /*
  190. * Initialize RxBD/TxBD rings
  191. */
  192. mpc5xxx_fec_rbd_init(fec);
  193. mpc5xxx_fec_tbd_init(fec);
  194. /*
  195. * Initialize GPIO pins
  196. */
  197. if (fec->xcv_type == SEVENWIRE) {
  198. /* 10MBit with 7-wire operation */
  199. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00020000;
  200. } else {
  201. /* 100MBit with MD operation */
  202. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00050000;
  203. }
  204. /*
  205. * Clear FEC-Lite interrupt event register(IEVENT)
  206. */
  207. fec->eth->ievent = 0xffffffff;
  208. /*
  209. * Set interrupt mask register
  210. */
  211. fec->eth->imask = 0x00000000;
  212. /*
  213. * Set FEC-Lite receive control register(R_CNTRL):
  214. */
  215. if (fec->xcv_type == SEVENWIRE) {
  216. /*
  217. * Frame length=1518; 7-wire mode
  218. */
  219. fec->eth->r_cntrl = 0x05ee0020; /*0x05ee0000;FIXME */
  220. } else {
  221. /*
  222. * Frame length=1518; MII mode;
  223. */
  224. fec->eth->r_cntrl = 0x05ee0024; /*0x05ee0004;FIXME */
  225. }
  226. if (fec->xcv_type == SEVENWIRE) {
  227. /*
  228. * Set FEC-Lite transmit control register(X_CNTRL):
  229. */
  230. /*fec->eth->x_cntrl = 0x00000002; */ /* half-duplex, heartbeat */
  231. fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
  232. } else {
  233. /*fec->eth->x_cntrl = 0x00000006; */ /* full-duplex, heartbeat */
  234. fec->eth->x_cntrl = 0x00000004; /* full-duplex, heartbeat disabled */
  235. /*
  236. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  237. * and do not drop the Preamble.
  238. */
  239. fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
  240. }
  241. /*
  242. * Set Opcode/Pause Duration Register
  243. */
  244. fec->eth->op_pause = 0x00010020; /*FIXME0xffff0020; */
  245. /*
  246. * Set Rx FIFO alarm and granularity value
  247. */
  248. fec->eth->rfifo_cntrl = 0x0c000000;
  249. fec->eth->rfifo_alarm = 0x0000030c;
  250. #if (DEBUG & 0x22)
  251. if (fec->eth->rfifo_status & 0x00700000 ) {
  252. printf("mpc5xxx_fec_init() RFIFO error\n");
  253. }
  254. #endif
  255. /*
  256. * Set Tx FIFO granularity value
  257. */
  258. fec->eth->tfifo_cntrl = 0x0c000000;
  259. #if (DEBUG & 0x2)
  260. printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
  261. printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
  262. #endif
  263. /*
  264. * Set transmit fifo watermark register(X_WMRK), default = 64
  265. */
  266. fec->eth->tfifo_alarm = 0x00000080;
  267. fec->eth->x_wmrk = 0x2;
  268. /*
  269. * Set individual address filter for unicast address
  270. * and set physical address registers.
  271. */
  272. mpc5xxx_fec_set_hwaddr(fec, dev->enetaddr);
  273. /*
  274. * Set multicast address filter
  275. */
  276. fec->eth->gaddr1 = 0x00000000;
  277. fec->eth->gaddr2 = 0x00000000;
  278. /*
  279. * Turn ON cheater FSM: ????
  280. */
  281. fec->eth->xmit_fsm = 0x03000000;
  282. #if defined(CONFIG_MPC5200)
  283. /*
  284. * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
  285. * work w/ the current receive task.
  286. */
  287. sdma->PtdCntrl |= 0x00000001;
  288. #endif
  289. /*
  290. * Set priority of different initiators
  291. */
  292. sdma->IPR0 = 7; /* always */
  293. sdma->IPR3 = 6; /* Eth RX */
  294. sdma->IPR4 = 5; /* Eth Tx */
  295. /*
  296. * Clear SmartDMA task interrupt pending bits
  297. */
  298. SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
  299. /*
  300. * Initialize SmartDMA parameters stored in SRAM
  301. */
  302. *(int *)FEC_TBD_BASE = (int)fec->tbdBase;
  303. *(int *)FEC_RBD_BASE = (int)fec->rbdBase;
  304. *(int *)FEC_TBD_NEXT = (int)fec->tbdBase;
  305. *(int *)FEC_RBD_NEXT = (int)fec->rbdBase;
  306. if (fec->xcv_type != SEVENWIRE) {
  307. /*
  308. * Initialize PHY(LXT971A):
  309. *
  310. * Generally, on power up, the LXT971A reads its configuration
  311. * pins to check for forced operation, If not cofigured for
  312. * forced operation, it uses auto-negotiation/parallel detection
  313. * to automatically determine line operating conditions.
  314. * If the PHY device on the other side of the link supports
  315. * auto-negotiation, the LXT971A auto-negotiates with it
  316. * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
  317. * support auto-negotiation, the LXT971A automatically detects
  318. * the presence of either link pulses(10Mbps PHY) or Idle
  319. * symbols(100Mbps) and sets its operating conditions accordingly.
  320. *
  321. * When auto-negotiation is controlled by software, the following
  322. * steps are recommended.
  323. *
  324. * Note:
  325. * The physical address is dependent on hardware configuration.
  326. *
  327. */
  328. int timeout = 1;
  329. uint16 phyStatus;
  330. /*
  331. * Reset PHY, then delay 300ns
  332. */
  333. miiphy_write(phyAddr, 0x0, 0x8000);
  334. udelay(1000);
  335. if (fec->xcv_type == MII10) {
  336. /*
  337. * Force 10Base-T, FDX operation
  338. */
  339. #if (DEBUG & 0x2)
  340. printf("Forcing 10 Mbps ethernet link... ");
  341. #endif
  342. miiphy_read(phyAddr, 0x1, &phyStatus);
  343. /*
  344. miiphy_write(fec, phyAddr, 0x0, 0x0100);
  345. */
  346. miiphy_write(phyAddr, 0x0, 0x0180);
  347. timeout = 20;
  348. do { /* wait for link status to go down */
  349. udelay(10000);
  350. if ((timeout--) == 0) {
  351. #if (DEBUG & 0x2)
  352. printf("hmmm, should not have waited...");
  353. #endif
  354. break;
  355. }
  356. miiphy_read(phyAddr, 0x1, &phyStatus);
  357. #if (DEBUG & 0x2)
  358. printf("=");
  359. #endif
  360. } while ((phyStatus & 0x0004)); /* !link up */
  361. timeout = 1000;
  362. do { /* wait for link status to come back up */
  363. udelay(10000);
  364. if ((timeout--) == 0) {
  365. printf("failed. Link is down.\n");
  366. break;
  367. }
  368. miiphy_read(phyAddr, 0x1, &phyStatus);
  369. #if (DEBUG & 0x2)
  370. printf("+");
  371. #endif
  372. } while (!(phyStatus & 0x0004)); /* !link up */
  373. printf ("done.\n");
  374. } else { /* MII100 */
  375. /*
  376. * Set the auto-negotiation advertisement register bits
  377. */
  378. miiphy_write(phyAddr, 0x4, 0x01e1);
  379. /*
  380. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  381. */
  382. miiphy_write(phyAddr, 0x0, 0x1200);
  383. /*
  384. * Wait for AN completion
  385. */
  386. timeout = 5000;
  387. do {
  388. udelay(1000);
  389. if ((timeout--) == 0) {
  390. #if (DEBUG & 0x2)
  391. printf("PHY auto neg 0 failed...\n");
  392. #endif
  393. return -1;
  394. }
  395. if (miiphy_read(phyAddr, 0x1, &phyStatus) != 0) {
  396. #if (DEBUG & 0x2)
  397. printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  398. #endif
  399. return -1;
  400. }
  401. } while ((phyStatus & 0x0020) != 0x0020);
  402. #if (DEBUG & 0x2)
  403. printf("PHY auto neg complete! \n");
  404. #endif
  405. }
  406. }
  407. /*
  408. * Enable FEC-Lite controller
  409. */
  410. fec->eth->ecntrl |= 0x00000006;
  411. if (fec->xcv_type != SEVENWIRE) {
  412. #if (DEBUG & 0x2)
  413. uint16 phyStatus, i;
  414. uint8 phyAddr = 0;
  415. for (i = 0; i < 9; i++) {
  416. miiphy_read(phyAddr, i, &phyStatus);
  417. printf("Mii reg %d: 0x%04x\n", i, phyStatus);
  418. }
  419. for (i = 16; i < 21; i++) {
  420. miiphy_read(phyAddr, i, &phyStatus);
  421. printf("Mii reg %d: 0x%04x\n", i, phyStatus);
  422. }
  423. #endif
  424. }
  425. /*
  426. * Enable SmartDMA receive task
  427. */
  428. SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
  429. #if (DEBUG & 0x1)
  430. printf("mpc5xxx_fec_init... Done \n");
  431. #endif
  432. return 1;
  433. }
  434. /********************************************************************/
  435. static void mpc5xxx_fec_halt(struct eth_device *dev)
  436. {
  437. #if defined(CONFIG_MPC5200)
  438. struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
  439. #endif
  440. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  441. int counter = 0xffff;
  442. #if (DEBUG & 0x2)
  443. if (fec->xcv_type != SEVENWIRE) {
  444. uint16 phyStatus, i;
  445. uint8 phyAddr = 0;
  446. for (i = 0; i < 9; i++) {
  447. miiphy_read(phyAddr, i, &phyStatus);
  448. printf("Mii reg %d: 0x%04x\n", i, phyStatus);
  449. }
  450. for (i = 16; i < 21; i++) {
  451. miiphy_read(phyAddr, i, &phyStatus);
  452. printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
  453. }
  454. }
  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. SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
  472. SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
  473. #if defined(CONFIG_MPC5200)
  474. /*
  475. * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
  476. * done. It doesn't work w/ the current receive task.
  477. */
  478. sdma->PtdCntrl &= ~0x00000001;
  479. #endif
  480. /*
  481. * Disable the Ethernet Controller
  482. */
  483. fec->eth->ecntrl &= 0xfffffffd;
  484. /*
  485. * Clear FIFO status registers
  486. */
  487. fec->eth->rfifo_status &= 0x00700000;
  488. fec->eth->tfifo_status &= 0x00700000;
  489. fec->eth->reset_cntrl = 0x01000000;
  490. /*
  491. * Issue a reset command to the FEC chip
  492. */
  493. fec->eth->ecntrl |= 0x1;
  494. /*
  495. * wait at least 16 clock cycles
  496. */
  497. udelay(10);
  498. #if (DEBUG & 0x3)
  499. printf("Ethernet task stopped\n");
  500. #endif
  501. }
  502. #if (DEBUG & 0x60)
  503. /********************************************************************/
  504. static void tfifo_print(mpc5xxx_fec_priv *fec)
  505. {
  506. uint16 phyAddr = 0;
  507. uint16 phyStatus;
  508. if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
  509. || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
  510. miiphy_read(phyAddr, 0x1, &phyStatus);
  511. printf("\nphyStatus: 0x%04x\n", phyStatus);
  512. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  513. printf("ievent: 0x%08x\n", fec->eth->ievent);
  514. printf("x_status: 0x%08x\n", fec->eth->x_status);
  515. printf("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
  516. printf(" control 0x%08x\n", fec->eth->tfifo_cntrl);
  517. printf(" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
  518. printf(" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
  519. printf(" alarm 0x%08x\n", fec->eth->tfifo_alarm);
  520. printf(" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
  521. printf(" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
  522. }
  523. }
  524. static void rfifo_print(mpc5xxx_fec_priv *fec)
  525. {
  526. uint16 phyAddr = 0;
  527. uint16 phyStatus;
  528. if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
  529. || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
  530. miiphy_read(phyAddr, 0x1, &phyStatus);
  531. printf("\nphyStatus: 0x%04x\n", phyStatus);
  532. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  533. printf("ievent: 0x%08x\n", fec->eth->ievent);
  534. printf("x_status: 0x%08x\n", fec->eth->x_status);
  535. printf("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
  536. printf(" control 0x%08x\n", fec->eth->rfifo_cntrl);
  537. printf(" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
  538. printf(" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
  539. printf(" alarm 0x%08x\n", fec->eth->rfifo_alarm);
  540. printf(" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
  541. printf(" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
  542. }
  543. }
  544. #endif /* DEBUG */
  545. /********************************************************************/
  546. static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
  547. int data_length)
  548. {
  549. /*
  550. * This routine transmits one frame. This routine only accepts
  551. * 6-byte Ethernet addresses.
  552. */
  553. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  554. FEC_TBD *pTbd;
  555. #if (DEBUG & 0x20)
  556. printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
  557. tfifo_print(fec);
  558. #endif
  559. /*
  560. * Clear Tx BD ring at first
  561. */
  562. mpc5xxx_fec_tbd_scrub(fec);
  563. /*
  564. * Check for valid length of data.
  565. */
  566. if ((data_length > 1500) || (data_length <= 0)) {
  567. return -1;
  568. }
  569. /*
  570. * Check the number of vacant TxBDs.
  571. */
  572. if (fec->cleanTbdNum < 1) {
  573. #if (DEBUG & 0x20)
  574. printf("No available TxBDs ...\n");
  575. #endif
  576. return -1;
  577. }
  578. /*
  579. * Get the first TxBD to send the mac header
  580. */
  581. pTbd = &fec->tbdBase[fec->tbdIndex];
  582. pTbd->dataLength = data_length;
  583. pTbd->dataPointer = (uint32)eth_data;
  584. pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  585. fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
  586. #if (DEBUG & 0x100)
  587. printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
  588. #endif
  589. /*
  590. * Kick the MII i/f
  591. */
  592. if (fec->xcv_type != SEVENWIRE) {
  593. uint16 phyStatus;
  594. miiphy_read(0, 0x1, &phyStatus);
  595. }
  596. /*
  597. * Enable SmartDMA transmit task
  598. */
  599. #if (DEBUG & 0x20)
  600. tfifo_print(fec);
  601. #endif
  602. SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
  603. #if (DEBUG & 0x20)
  604. tfifo_print(fec);
  605. #endif
  606. #if (DEBUG & 0x8)
  607. printf( "+" );
  608. #endif
  609. fec->cleanTbdNum -= 1;
  610. #if (DEBUG & 0x129) && (DEBUG & 0x80000000)
  611. printf ("smartDMA ethernet Tx task enabled\n");
  612. #endif
  613. /*
  614. * wait until frame is sent .
  615. */
  616. while (pTbd->status & FEC_TBD_READY) {
  617. udelay(10);
  618. #if (DEBUG & 0x8)
  619. printf ("TDB status = %04x\n", pTbd->status);
  620. #endif
  621. }
  622. return 0;
  623. }
  624. /********************************************************************/
  625. static int mpc5xxx_fec_recv(struct eth_device *dev)
  626. {
  627. /*
  628. * This command pulls one frame from the card
  629. */
  630. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  631. FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
  632. unsigned long ievent;
  633. int frame_length, len = 0;
  634. NBUF *frame;
  635. char buff[FEC_MAX_PKT_SIZE];
  636. #if (DEBUG & 0x1)
  637. printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
  638. #endif
  639. #if (DEBUG & 0x8)
  640. printf( "-" );
  641. #endif
  642. /*
  643. * Check if any critical events have happened
  644. */
  645. ievent = fec->eth->ievent;
  646. fec->eth->ievent = ievent;
  647. if (ievent & 0x20060000) {
  648. /* BABT, Rx/Tx FIFO errors */
  649. mpc5xxx_fec_halt(dev);
  650. mpc5xxx_fec_init(dev, NULL);
  651. return 0;
  652. }
  653. if (ievent & 0x80000000) {
  654. /* Heartbeat error */
  655. fec->eth->x_cntrl |= 0x00000001;
  656. }
  657. if (ievent & 0x10000000) {
  658. /* Graceful stop complete */
  659. if (fec->eth->x_cntrl & 0x00000001) {
  660. mpc5xxx_fec_halt(dev);
  661. fec->eth->x_cntrl &= ~0x00000001;
  662. mpc5xxx_fec_init(dev, NULL);
  663. }
  664. }
  665. if (!(pRbd->status & FEC_RBD_EMPTY)) {
  666. if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
  667. ((pRbd->dataLength - 4) > 14)) {
  668. /*
  669. * Get buffer address and size
  670. */
  671. frame = (NBUF *)pRbd->dataPointer;
  672. frame_length = pRbd->dataLength - 4;
  673. #if (DEBUG & 0x20)
  674. {
  675. int i;
  676. printf("recv data hdr:");
  677. for (i = 0; i < 14; i++)
  678. printf("%x ", *(frame->head + i));
  679. printf("\n");
  680. }
  681. #endif
  682. /*
  683. * Fill the buffer and pass it to upper layers
  684. */
  685. memcpy(buff, frame->head, 14);
  686. memcpy(buff + 14, frame->data, frame_length);
  687. NetReceive(buff, frame_length);
  688. len = frame_length;
  689. }
  690. /*
  691. * Reset buffer descriptor as empty
  692. */
  693. mpc5xxx_fec_rbd_clean(fec, pRbd);
  694. }
  695. SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
  696. return len;
  697. }
  698. /********************************************************************/
  699. int mpc5xxx_fec_initialize(bd_t * bis)
  700. {
  701. mpc5xxx_fec_priv *fec;
  702. struct eth_device *dev;
  703. char *tmp, *end;
  704. char env_enetaddr[6];
  705. int i;
  706. fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
  707. dev = (struct eth_device *)malloc(sizeof(*dev));
  708. memset(dev, 0, sizeof *dev);
  709. fec->eth = (ethernet_regs *)MPC5XXX_FEC;
  710. fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
  711. fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
  712. #ifdef CONFIG_ICECUBE
  713. #ifndef CONFIG_FEC_10MBIT
  714. fec->xcv_type = MII100;
  715. #else
  716. fec->xcv_type = MII10;
  717. #endif
  718. #else
  719. #error fec->xcv_type not initialized.
  720. #endif
  721. dev->priv = (void *)fec;
  722. dev->iobase = MPC5XXX_FEC;
  723. dev->init = mpc5xxx_fec_init;
  724. dev->halt = mpc5xxx_fec_halt;
  725. dev->send = mpc5xxx_fec_send;
  726. dev->recv = mpc5xxx_fec_recv;
  727. sprintf(dev->name, "FEC ETHERNET");
  728. eth_register(dev);
  729. /*
  730. * Try to set the mac address now. The fec mac address is
  731. * a garbage after reset. When not using fec for booting
  732. * the Linux fec driver will try to work with this garbage.
  733. */
  734. tmp = getenv("ethaddr");
  735. if (tmp) {
  736. for (i=0; i<6; i++) {
  737. env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  738. if (tmp)
  739. tmp = (*end) ? end+1 : end;
  740. }
  741. mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
  742. }
  743. return 1;
  744. }
  745. /* MII-interface related functions */
  746. /********************************************************************/
  747. int miiphy_read(uint8 phyAddr, uint8 regAddr, uint16 * retVal)
  748. {
  749. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  750. uint32 reg; /* convenient holder for the PHY register */
  751. uint32 phy; /* convenient holder for the PHY */
  752. int timeout = 0xffff;
  753. /*
  754. * reading from any PHY's register is done by properly
  755. * programming the FEC's MII data register.
  756. */
  757. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  758. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  759. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
  760. /*
  761. * wait for the related interrupt
  762. */
  763. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  764. if (timeout == 0) {
  765. #if (DEBUG & 0x2)
  766. printf ("Read MDIO failed...\n");
  767. #endif
  768. return -1;
  769. }
  770. /*
  771. * clear mii interrupt bit
  772. */
  773. eth->ievent = 0x00800000;
  774. /*
  775. * it's now safe to read the PHY's register
  776. */
  777. *retVal = (uint16) eth->mii_data;
  778. return 0;
  779. }
  780. /********************************************************************/
  781. int miiphy_write(uint8 phyAddr, uint8 regAddr, uint16 data)
  782. {
  783. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  784. uint32 reg; /* convenient holder for the PHY register */
  785. uint32 phy; /* convenient holder for the PHY */
  786. int timeout = 0xffff;
  787. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  788. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  789. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
  790. FEC_MII_DATA_TA | phy | reg | data);
  791. /*
  792. * wait for the MII interrupt
  793. */
  794. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  795. if (timeout == 0) {
  796. #if (DEBUG & 0x2)
  797. printf ("Write MDIO failed...\n");
  798. #endif
  799. return -1;
  800. }
  801. /*
  802. * clear MII interrupt bit
  803. */
  804. eth->ievent = 0x00800000;
  805. return 0;
  806. }
  807. #if (DEBUG & 0x40)
  808. static uint32 local_crc32(char *string, unsigned int crc_value, int len)
  809. {
  810. int i;
  811. char c;
  812. unsigned int crc, count;
  813. /*
  814. * crc32 algorithm
  815. */
  816. /*
  817. * crc = 0xffffffff; * The initialized value should be 0xffffffff
  818. */
  819. crc = crc_value;
  820. for (i = len; --i >= 0;) {
  821. c = *string++;
  822. for (count = 0; count < 8; count++) {
  823. if ((c & 0x01) ^ (crc & 0x01)) {
  824. crc >>= 1;
  825. crc = crc ^ 0xedb88320;
  826. } else {
  827. crc >>= 1;
  828. }
  829. c >>= 1;
  830. }
  831. }
  832. /*
  833. * In big endian system, do byte swaping for crc value
  834. */
  835. /**/ return crc;
  836. }
  837. #endif /* DEBUG */
  838. #endif /* CONFIG_MPC5XXX_FEC */