fec.c 25 KB

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