fec.c 23 KB

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