mpc5xxx_fec.c 24 KB

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