fec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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. #if (DEBUG & 0x2)
  374. printf ("done.\n");
  375. #endif
  376. } else { /* MII100 */
  377. /*
  378. * Set the auto-negotiation advertisement register bits
  379. */
  380. miiphy_write(phyAddr, 0x4, 0x01e1);
  381. /*
  382. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  383. */
  384. miiphy_write(phyAddr, 0x0, 0x1200);
  385. /*
  386. * Wait for AN completion
  387. */
  388. timeout = 5000;
  389. do {
  390. udelay(1000);
  391. if ((timeout--) == 0) {
  392. #if (DEBUG & 0x2)
  393. printf("PHY auto neg 0 failed...\n");
  394. #endif
  395. return -1;
  396. }
  397. if (miiphy_read(phyAddr, 0x1, &phyStatus) != 0) {
  398. #if (DEBUG & 0x2)
  399. printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  400. #endif
  401. return -1;
  402. }
  403. } while ((phyStatus & 0x0020) != 0x0020);
  404. #if (DEBUG & 0x2)
  405. printf("PHY auto neg complete! \n");
  406. #endif
  407. }
  408. }
  409. /*
  410. * Enable FEC-Lite controller
  411. */
  412. fec->eth->ecntrl |= 0x00000006;
  413. if (fec->xcv_type != SEVENWIRE) {
  414. #if (DEBUG & 0x2)
  415. uint16 phyStatus, i;
  416. uint8 phyAddr = 0;
  417. for (i = 0; i < 9; i++) {
  418. miiphy_read(phyAddr, i, &phyStatus);
  419. printf("Mii reg %d: 0x%04x\n", i, phyStatus);
  420. }
  421. for (i = 16; i < 21; i++) {
  422. miiphy_read(phyAddr, i, &phyStatus);
  423. printf("Mii reg %d: 0x%04x\n", i, phyStatus);
  424. }
  425. #endif
  426. }
  427. /*
  428. * Enable SmartDMA receive task
  429. */
  430. SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
  431. #if (DEBUG & 0x1)
  432. printf("mpc5xxx_fec_init... Done \n");
  433. #endif
  434. return 1;
  435. }
  436. /********************************************************************/
  437. static void mpc5xxx_fec_halt(struct eth_device *dev)
  438. {
  439. #if defined(CONFIG_MPC5200)
  440. struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
  441. #endif
  442. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  443. int counter = 0xffff;
  444. #if (DEBUG & 0x2)
  445. if (fec->xcv_type != SEVENWIRE) {
  446. uint16 phyStatus, i;
  447. uint8 phyAddr = 0;
  448. for (i = 0; i < 9; i++) {
  449. miiphy_read(phyAddr, i, &phyStatus);
  450. printf("Mii reg %d: 0x%04x\n", i, phyStatus);
  451. }
  452. for (i = 16; i < 21; i++) {
  453. miiphy_read(phyAddr, i, &phyStatus);
  454. printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
  455. }
  456. }
  457. #endif
  458. /*
  459. * mask FEC chip interrupts
  460. */
  461. fec->eth->imask = 0;
  462. /*
  463. * issue graceful stop command to the FEC transmitter if necessary
  464. */
  465. fec->eth->x_cntrl |= 0x00000001;
  466. /*
  467. * wait for graceful stop to register
  468. */
  469. while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
  470. /*
  471. * Disable SmartDMA tasks
  472. */
  473. SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
  474. SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
  475. #if defined(CONFIG_MPC5200)
  476. /*
  477. * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
  478. * done. It doesn't work w/ the current receive task.
  479. */
  480. sdma->PtdCntrl &= ~0x00000001;
  481. #endif
  482. /*
  483. * Disable the Ethernet Controller
  484. */
  485. fec->eth->ecntrl &= 0xfffffffd;
  486. /*
  487. * Clear FIFO status registers
  488. */
  489. fec->eth->rfifo_status &= 0x00700000;
  490. fec->eth->tfifo_status &= 0x00700000;
  491. fec->eth->reset_cntrl = 0x01000000;
  492. /*
  493. * Issue a reset command to the FEC chip
  494. */
  495. fec->eth->ecntrl |= 0x1;
  496. /*
  497. * wait at least 16 clock cycles
  498. */
  499. udelay(10);
  500. #if (DEBUG & 0x3)
  501. printf("Ethernet task stopped\n");
  502. #endif
  503. }
  504. #if (DEBUG & 0x60)
  505. /********************************************************************/
  506. static void tfifo_print(mpc5xxx_fec_priv *fec)
  507. {
  508. uint16 phyAddr = 0;
  509. uint16 phyStatus;
  510. if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
  511. || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
  512. miiphy_read(phyAddr, 0x1, &phyStatus);
  513. printf("\nphyStatus: 0x%04x\n", phyStatus);
  514. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  515. printf("ievent: 0x%08x\n", fec->eth->ievent);
  516. printf("x_status: 0x%08x\n", fec->eth->x_status);
  517. printf("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
  518. printf(" control 0x%08x\n", fec->eth->tfifo_cntrl);
  519. printf(" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
  520. printf(" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
  521. printf(" alarm 0x%08x\n", fec->eth->tfifo_alarm);
  522. printf(" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
  523. printf(" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
  524. }
  525. }
  526. static void rfifo_print(mpc5xxx_fec_priv *fec)
  527. {
  528. uint16 phyAddr = 0;
  529. uint16 phyStatus;
  530. if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
  531. || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
  532. miiphy_read(phyAddr, 0x1, &phyStatus);
  533. printf("\nphyStatus: 0x%04x\n", phyStatus);
  534. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  535. printf("ievent: 0x%08x\n", fec->eth->ievent);
  536. printf("x_status: 0x%08x\n", fec->eth->x_status);
  537. printf("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
  538. printf(" control 0x%08x\n", fec->eth->rfifo_cntrl);
  539. printf(" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
  540. printf(" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
  541. printf(" alarm 0x%08x\n", fec->eth->rfifo_alarm);
  542. printf(" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
  543. printf(" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
  544. }
  545. }
  546. #endif /* DEBUG */
  547. /********************************************************************/
  548. static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
  549. int data_length)
  550. {
  551. /*
  552. * This routine transmits one frame. This routine only accepts
  553. * 6-byte Ethernet addresses.
  554. */
  555. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  556. FEC_TBD *pTbd;
  557. #if (DEBUG & 0x20)
  558. printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
  559. tfifo_print(fec);
  560. #endif
  561. /*
  562. * Clear Tx BD ring at first
  563. */
  564. mpc5xxx_fec_tbd_scrub(fec);
  565. /*
  566. * Check for valid length of data.
  567. */
  568. if ((data_length > 1500) || (data_length <= 0)) {
  569. return -1;
  570. }
  571. /*
  572. * Check the number of vacant TxBDs.
  573. */
  574. if (fec->cleanTbdNum < 1) {
  575. #if (DEBUG & 0x20)
  576. printf("No available TxBDs ...\n");
  577. #endif
  578. return -1;
  579. }
  580. /*
  581. * Get the first TxBD to send the mac header
  582. */
  583. pTbd = &fec->tbdBase[fec->tbdIndex];
  584. pTbd->dataLength = data_length;
  585. pTbd->dataPointer = (uint32)eth_data;
  586. pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  587. fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
  588. #if (DEBUG & 0x100)
  589. printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
  590. #endif
  591. /*
  592. * Kick the MII i/f
  593. */
  594. if (fec->xcv_type != SEVENWIRE) {
  595. uint16 phyStatus;
  596. miiphy_read(0, 0x1, &phyStatus);
  597. }
  598. /*
  599. * Enable SmartDMA transmit task
  600. */
  601. #if (DEBUG & 0x20)
  602. tfifo_print(fec);
  603. #endif
  604. SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
  605. #if (DEBUG & 0x20)
  606. tfifo_print(fec);
  607. #endif
  608. #if (DEBUG & 0x8)
  609. printf( "+" );
  610. #endif
  611. fec->cleanTbdNum -= 1;
  612. #if (DEBUG & 0x129) && (DEBUG & 0x80000000)
  613. printf ("smartDMA ethernet Tx task enabled\n");
  614. #endif
  615. /*
  616. * wait until frame is sent .
  617. */
  618. while (pTbd->status & FEC_TBD_READY) {
  619. udelay(10);
  620. #if (DEBUG & 0x8)
  621. printf ("TDB status = %04x\n", pTbd->status);
  622. #endif
  623. }
  624. return 0;
  625. }
  626. /********************************************************************/
  627. static int mpc5xxx_fec_recv(struct eth_device *dev)
  628. {
  629. /*
  630. * This command pulls one frame from the card
  631. */
  632. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  633. FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
  634. unsigned long ievent;
  635. int frame_length, len = 0;
  636. NBUF *frame;
  637. char buff[FEC_MAX_PKT_SIZE];
  638. #if (DEBUG & 0x1)
  639. printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
  640. #endif
  641. #if (DEBUG & 0x8)
  642. printf( "-" );
  643. #endif
  644. /*
  645. * Check if any critical events have happened
  646. */
  647. ievent = fec->eth->ievent;
  648. fec->eth->ievent = ievent;
  649. if (ievent & 0x20060000) {
  650. /* BABT, Rx/Tx FIFO errors */
  651. mpc5xxx_fec_halt(dev);
  652. mpc5xxx_fec_init(dev, NULL);
  653. return 0;
  654. }
  655. if (ievent & 0x80000000) {
  656. /* Heartbeat error */
  657. fec->eth->x_cntrl |= 0x00000001;
  658. }
  659. if (ievent & 0x10000000) {
  660. /* Graceful stop complete */
  661. if (fec->eth->x_cntrl & 0x00000001) {
  662. mpc5xxx_fec_halt(dev);
  663. fec->eth->x_cntrl &= ~0x00000001;
  664. mpc5xxx_fec_init(dev, NULL);
  665. }
  666. }
  667. if (!(pRbd->status & FEC_RBD_EMPTY)) {
  668. if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
  669. ((pRbd->dataLength - 4) > 14)) {
  670. /*
  671. * Get buffer address and size
  672. */
  673. frame = (NBUF *)pRbd->dataPointer;
  674. frame_length = pRbd->dataLength - 4;
  675. #if (DEBUG & 0x20)
  676. {
  677. int i;
  678. printf("recv data hdr:");
  679. for (i = 0; i < 14; i++)
  680. printf("%x ", *(frame->head + i));
  681. printf("\n");
  682. }
  683. #endif
  684. /*
  685. * Fill the buffer and pass it to upper layers
  686. */
  687. memcpy(buff, frame->head, 14);
  688. memcpy(buff + 14, frame->data, frame_length);
  689. NetReceive(buff, frame_length);
  690. len = frame_length;
  691. }
  692. /*
  693. * Reset buffer descriptor as empty
  694. */
  695. mpc5xxx_fec_rbd_clean(fec, pRbd);
  696. }
  697. SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
  698. return len;
  699. }
  700. /********************************************************************/
  701. int mpc5xxx_fec_initialize(bd_t * bis)
  702. {
  703. mpc5xxx_fec_priv *fec;
  704. struct eth_device *dev;
  705. char *tmp, *end;
  706. char env_enetaddr[6];
  707. int i;
  708. fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
  709. dev = (struct eth_device *)malloc(sizeof(*dev));
  710. memset(dev, 0, sizeof *dev);
  711. fec->eth = (ethernet_regs *)MPC5XXX_FEC;
  712. fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
  713. fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
  714. #ifdef CONFIG_ICECUBE
  715. #ifndef CONFIG_FEC_10MBIT
  716. fec->xcv_type = MII100;
  717. #else
  718. fec->xcv_type = MII10;
  719. #endif
  720. #else
  721. #error fec->xcv_type not initialized.
  722. #endif
  723. dev->priv = (void *)fec;
  724. dev->iobase = MPC5XXX_FEC;
  725. dev->init = mpc5xxx_fec_init;
  726. dev->halt = mpc5xxx_fec_halt;
  727. dev->send = mpc5xxx_fec_send;
  728. dev->recv = mpc5xxx_fec_recv;
  729. sprintf(dev->name, "FEC ETHERNET");
  730. eth_register(dev);
  731. /*
  732. * Try to set the mac address now. The fec mac address is
  733. * a garbage after reset. When not using fec for booting
  734. * the Linux fec driver will try to work with this garbage.
  735. */
  736. tmp = getenv("ethaddr");
  737. if (tmp) {
  738. for (i=0; i<6; i++) {
  739. env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  740. if (tmp)
  741. tmp = (*end) ? end+1 : end;
  742. }
  743. mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
  744. }
  745. return 1;
  746. }
  747. /* MII-interface related functions */
  748. /********************************************************************/
  749. int miiphy_read(uint8 phyAddr, uint8 regAddr, uint16 * retVal)
  750. {
  751. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  752. uint32 reg; /* convenient holder for the PHY register */
  753. uint32 phy; /* convenient holder for the PHY */
  754. int timeout = 0xffff;
  755. /*
  756. * reading from any PHY's register is done by properly
  757. * programming the FEC's MII data register.
  758. */
  759. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  760. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  761. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
  762. /*
  763. * wait for the related interrupt
  764. */
  765. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  766. if (timeout == 0) {
  767. #if (DEBUG & 0x2)
  768. printf ("Read MDIO failed...\n");
  769. #endif
  770. return -1;
  771. }
  772. /*
  773. * clear mii interrupt bit
  774. */
  775. eth->ievent = 0x00800000;
  776. /*
  777. * it's now safe to read the PHY's register
  778. */
  779. *retVal = (uint16) eth->mii_data;
  780. return 0;
  781. }
  782. /********************************************************************/
  783. int miiphy_write(uint8 phyAddr, uint8 regAddr, uint16 data)
  784. {
  785. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  786. uint32 reg; /* convenient holder for the PHY register */
  787. uint32 phy; /* convenient holder for the PHY */
  788. int timeout = 0xffff;
  789. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  790. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  791. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
  792. FEC_MII_DATA_TA | phy | reg | data);
  793. /*
  794. * wait for the MII interrupt
  795. */
  796. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  797. if (timeout == 0) {
  798. #if (DEBUG & 0x2)
  799. printf ("Write MDIO failed...\n");
  800. #endif
  801. return -1;
  802. }
  803. /*
  804. * clear MII interrupt bit
  805. */
  806. eth->ievent = 0x00800000;
  807. return 0;
  808. }
  809. #if (DEBUG & 0x40)
  810. static uint32 local_crc32(char *string, unsigned int crc_value, int len)
  811. {
  812. int i;
  813. char c;
  814. unsigned int crc, count;
  815. /*
  816. * crc32 algorithm
  817. */
  818. /*
  819. * crc = 0xffffffff; * The initialized value should be 0xffffffff
  820. */
  821. crc = crc_value;
  822. for (i = len; --i >= 0;) {
  823. c = *string++;
  824. for (count = 0; count < 8; count++) {
  825. if ((c & 0x01) ^ (crc & 0x01)) {
  826. crc >>= 1;
  827. crc = crc ^ 0xedb88320;
  828. } else {
  829. crc >>= 1;
  830. }
  831. c >>= 1;
  832. }
  833. }
  834. /*
  835. * In big endian system, do byte swaping for crc value
  836. */
  837. /**/ return crc;
  838. }
  839. #endif /* DEBUG */
  840. #endif /* CONFIG_MPC5XXX_FEC */