mpc5xxx_fec.c 25 KB

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