fec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
  252. if (fec->xcv_type != SEVENWIRE) {
  253. /*
  254. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  255. * and do not drop the Preamble.
  256. */
  257. fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
  258. }
  259. /*
  260. * Set Opcode/Pause Duration Register
  261. */
  262. fec->eth->op_pause = 0x00010020; /*FIXME0xffff0020; */
  263. /*
  264. * Set Rx FIFO alarm and granularity value
  265. */
  266. fec->eth->rfifo_cntrl = 0x0c000000;
  267. fec->eth->rfifo_alarm = 0x0000030c;
  268. #if (DEBUG & 0x22)
  269. if (fec->eth->rfifo_status & 0x00700000 ) {
  270. printf("mpc5xxx_fec_init() RFIFO error\n");
  271. }
  272. #endif
  273. /*
  274. * Set Tx FIFO granularity value
  275. */
  276. fec->eth->tfifo_cntrl = 0x0c000000;
  277. #if (DEBUG & 0x2)
  278. printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
  279. printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
  280. #endif
  281. /*
  282. * Set transmit fifo watermark register(X_WMRK), default = 64
  283. */
  284. fec->eth->tfifo_alarm = 0x00000080;
  285. fec->eth->x_wmrk = 0x2;
  286. /*
  287. * Set individual address filter for unicast address
  288. * and set physical address registers.
  289. */
  290. mpc5xxx_fec_set_hwaddr(fec, dev->enetaddr);
  291. /*
  292. * Set multicast address filter
  293. */
  294. fec->eth->gaddr1 = 0x00000000;
  295. fec->eth->gaddr2 = 0x00000000;
  296. /*
  297. * Turn ON cheater FSM: ????
  298. */
  299. fec->eth->xmit_fsm = 0x03000000;
  300. #if defined(CONFIG_MPC5200)
  301. /*
  302. * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
  303. * work w/ the current receive task.
  304. */
  305. sdma->PtdCntrl |= 0x00000001;
  306. #endif
  307. /*
  308. * Set priority of different initiators
  309. */
  310. sdma->IPR0 = 7; /* always */
  311. sdma->IPR3 = 6; /* Eth RX */
  312. sdma->IPR4 = 5; /* Eth Tx */
  313. /*
  314. * Clear SmartDMA task interrupt pending bits
  315. */
  316. SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
  317. /*
  318. * Initialize SmartDMA parameters stored in SRAM
  319. */
  320. *(int *)FEC_TBD_BASE = (int)fec->tbdBase;
  321. *(int *)FEC_RBD_BASE = (int)fec->rbdBase;
  322. *(int *)FEC_TBD_NEXT = (int)fec->tbdBase;
  323. *(int *)FEC_RBD_NEXT = (int)fec->rbdBase;
  324. if (fec->xcv_type != SEVENWIRE) {
  325. /*
  326. * Initialize PHY(LXT971A):
  327. *
  328. * Generally, on power up, the LXT971A reads its configuration
  329. * pins to check for forced operation, If not cofigured for
  330. * forced operation, it uses auto-negotiation/parallel detection
  331. * to automatically determine line operating conditions.
  332. * If the PHY device on the other side of the link supports
  333. * auto-negotiation, the LXT971A auto-negotiates with it
  334. * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
  335. * support auto-negotiation, the LXT971A automatically detects
  336. * the presence of either link pulses(10Mbps PHY) or Idle
  337. * symbols(100Mbps) and sets its operating conditions accordingly.
  338. *
  339. * When auto-negotiation is controlled by software, the following
  340. * steps are recommended.
  341. *
  342. * Note:
  343. * The physical address is dependent on hardware configuration.
  344. *
  345. */
  346. int timeout = 1;
  347. uint16 phyStatus;
  348. /*
  349. * Reset PHY, then delay 300ns
  350. */
  351. miiphy_write(phyAddr, 0x0, 0x8000);
  352. udelay(1000);
  353. if (fec->xcv_type == MII10) {
  354. /*
  355. * Force 10Base-T, FDX operation
  356. */
  357. #if (DEBUG & 0x2)
  358. printf("Forcing 10 Mbps ethernet link... ");
  359. #endif
  360. miiphy_read(phyAddr, 0x1, &phyStatus);
  361. /*
  362. miiphy_write(fec, phyAddr, 0x0, 0x0100);
  363. */
  364. miiphy_write(phyAddr, 0x0, 0x0180);
  365. timeout = 20;
  366. do { /* wait for link status to go down */
  367. udelay(10000);
  368. if ((timeout--) == 0) {
  369. #if (DEBUG & 0x2)
  370. printf("hmmm, should not have waited...");
  371. #endif
  372. break;
  373. }
  374. miiphy_read(phyAddr, 0x1, &phyStatus);
  375. #if (DEBUG & 0x2)
  376. printf("=");
  377. #endif
  378. } while ((phyStatus & 0x0004)); /* !link up */
  379. timeout = 1000;
  380. do { /* wait for link status to come back up */
  381. udelay(10000);
  382. if ((timeout--) == 0) {
  383. printf("failed. Link is down.\n");
  384. break;
  385. }
  386. miiphy_read(phyAddr, 0x1, &phyStatus);
  387. #if (DEBUG & 0x2)
  388. printf("+");
  389. #endif
  390. } while (!(phyStatus & 0x0004)); /* !link up */
  391. #if (DEBUG & 0x2)
  392. printf ("done.\n");
  393. #endif
  394. } else { /* MII100 */
  395. /*
  396. * Set the auto-negotiation advertisement register bits
  397. */
  398. miiphy_write(phyAddr, 0x4, 0x01e1);
  399. /*
  400. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  401. */
  402. miiphy_write(phyAddr, 0x0, 0x1200);
  403. /*
  404. * Wait for AN completion
  405. */
  406. timeout = 5000;
  407. do {
  408. udelay(1000);
  409. if ((timeout--) == 0) {
  410. #if (DEBUG & 0x2)
  411. printf("PHY auto neg 0 failed...\n");
  412. #endif
  413. return -1;
  414. }
  415. if (miiphy_read(phyAddr, 0x1, &phyStatus) != 0) {
  416. #if (DEBUG & 0x2)
  417. printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  418. #endif
  419. return -1;
  420. }
  421. } while (!(phyStatus & 0x0004));
  422. #if (DEBUG & 0x2)
  423. printf("PHY auto neg complete! \n");
  424. #endif
  425. }
  426. }
  427. /*
  428. * Enable FEC-Lite controller
  429. */
  430. fec->eth->ecntrl |= 0x00000006;
  431. #if (DEBUG & 0x2)
  432. if (fec->xcv_type != SEVENWIRE)
  433. mpc5xxx_fec_phydump ();
  434. #endif
  435. /*
  436. * Enable SmartDMA receive task
  437. */
  438. SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
  439. #if (DEBUG & 0x1)
  440. printf("mpc5xxx_fec_init... Done \n");
  441. #endif
  442. return 1;
  443. }
  444. /********************************************************************/
  445. static void mpc5xxx_fec_halt(struct eth_device *dev)
  446. {
  447. #if defined(CONFIG_MPC5200)
  448. struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
  449. #endif
  450. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  451. int counter = 0xffff;
  452. #if (DEBUG & 0x2)
  453. if (fec->xcv_type != SEVENWIRE)
  454. mpc5xxx_fec_phydump ();
  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 = CONFIG_PHY_ADDR;
  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 = CONFIG_PHY_ADDR;
  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. #if defined(CONFIG_ICECUBE) || \
  713. defined(CONFIG_PM520) || \
  714. defined(CONFIG_TOP5200)
  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 */