fec.c 24 KB

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