fec.c 24 KB

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