fec.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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_CMD_NET)) && defined(CONFIG_NET_MULTI) && \
  18. defined(CONFIG_MPC5xxx_FEC)
  19. #if !(defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII) || defined(CONFIG_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. const uint8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
  342. #if (DEBUG & 0x1)
  343. printf ("mpc5xxx_fec_init_phy... Begin\n");
  344. #endif
  345. /*
  346. * Initialize GPIO pins
  347. */
  348. if (fec->xcv_type == SEVENWIRE) {
  349. /* 10MBit with 7-wire operation */
  350. #if defined(CONFIG_TOTAL5200)
  351. /* 7-wire and USB2 on Ethernet */
  352. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00030000;
  353. #else /* !CONFIG_TOTAL5200 */
  354. /* 7-wire only */
  355. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00020000;
  356. #endif /* CONFIG_TOTAL5200 */
  357. } else {
  358. /* 100MBit with MD operation */
  359. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00050000;
  360. }
  361. /*
  362. * Clear FEC-Lite interrupt event register(IEVENT)
  363. */
  364. fec->eth->ievent = 0xffffffff;
  365. /*
  366. * Set interrupt mask register
  367. */
  368. fec->eth->imask = 0x00000000;
  369. /*
  370. * In original Promess-provided code PHY initialization is disabled with the
  371. * following comment: "Phy initialization is DISABLED for now. There was a
  372. * problem with running 100 Mbps on PRO board". Thus we temporarily disable
  373. * PHY initialization for the Motion-PRO board, until a proper fix is found.
  374. */
  375. if (fec->xcv_type != SEVENWIRE) {
  376. /*
  377. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  378. * and do not drop the Preamble.
  379. */
  380. fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
  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 defined(CONFIG_UC101)
  412. /* Set the LED configuration Register for the UC101 Board */
  413. miiphy_write(dev->name, phyAddr, 0x14, 0x4122);
  414. #endif
  415. if (fec->xcv_type == MII10) {
  416. /*
  417. * Force 10Base-T, FDX operation
  418. */
  419. #if (DEBUG & 0x2)
  420. printf("Forcing 10 Mbps ethernet link... ");
  421. #endif
  422. miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
  423. /*
  424. miiphy_write(dev->name, fec, phyAddr, 0x0, 0x0100);
  425. */
  426. miiphy_write(dev->name, phyAddr, 0x0, 0x0180);
  427. timeout = 20;
  428. do { /* wait for link status to go down */
  429. udelay(10000);
  430. if ((timeout--) == 0) {
  431. #if (DEBUG & 0x2)
  432. printf("hmmm, should not have waited...");
  433. #endif
  434. break;
  435. }
  436. miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
  437. #if (DEBUG & 0x2)
  438. printf("=");
  439. #endif
  440. } while ((phyStatus & 0x0004)); /* !link up */
  441. timeout = 1000;
  442. do { /* wait for link status to come back up */
  443. udelay(10000);
  444. if ((timeout--) == 0) {
  445. printf("failed. Link is down.\n");
  446. break;
  447. }
  448. miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
  449. #if (DEBUG & 0x2)
  450. printf("+");
  451. #endif
  452. } while (!(phyStatus & 0x0004)); /* !link up */
  453. #if (DEBUG & 0x2)
  454. printf ("done.\n");
  455. #endif
  456. } else { /* MII100 */
  457. /*
  458. * Set the auto-negotiation advertisement register bits
  459. */
  460. miiphy_write(dev->name, phyAddr, 0x4, 0x01e1);
  461. /*
  462. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  463. */
  464. miiphy_write(dev->name, phyAddr, 0x0, 0x1200);
  465. /*
  466. * Wait for AN completion
  467. */
  468. timeout = 5000;
  469. do {
  470. udelay(1000);
  471. if ((timeout--) == 0) {
  472. #if (DEBUG & 0x2)
  473. printf("PHY auto neg 0 failed...\n");
  474. #endif
  475. return -1;
  476. }
  477. if (miiphy_read(dev->name, phyAddr, 0x1, &phyStatus) != 0) {
  478. #if (DEBUG & 0x2)
  479. printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  480. #endif
  481. return -1;
  482. }
  483. } while (!(phyStatus & 0x0004));
  484. #if (DEBUG & 0x2)
  485. printf("PHY auto neg complete! \n");
  486. #endif
  487. }
  488. }
  489. #if (DEBUG & 0x2)
  490. if (fec->xcv_type != SEVENWIRE)
  491. mpc5xxx_fec_phydump (dev->name);
  492. #endif
  493. #if (DEBUG & 0x1)
  494. printf("mpc5xxx_fec_init_phy... Done \n");
  495. #endif
  496. return 1;
  497. }
  498. /********************************************************************/
  499. static void mpc5xxx_fec_halt(struct eth_device *dev)
  500. {
  501. #if defined(CONFIG_MPC5200)
  502. struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
  503. #endif
  504. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  505. int counter = 0xffff;
  506. #if (DEBUG & 0x2)
  507. if (fec->xcv_type != SEVENWIRE)
  508. mpc5xxx_fec_phydump (dev->name);
  509. #endif
  510. /*
  511. * mask FEC chip interrupts
  512. */
  513. fec->eth->imask = 0;
  514. /*
  515. * issue graceful stop command to the FEC transmitter if necessary
  516. */
  517. fec->eth->x_cntrl |= 0x00000001;
  518. /*
  519. * wait for graceful stop to register
  520. */
  521. while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
  522. /*
  523. * Disable SmartDMA tasks
  524. */
  525. SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
  526. SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
  527. #if defined(CONFIG_MPC5200)
  528. /*
  529. * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
  530. * done. It doesn't work w/ the current receive task.
  531. */
  532. sdma->PtdCntrl &= ~0x00000001;
  533. #endif
  534. /*
  535. * Disable the Ethernet Controller
  536. */
  537. fec->eth->ecntrl &= 0xfffffffd;
  538. /*
  539. * Clear FIFO status registers
  540. */
  541. fec->eth->rfifo_status &= 0x00700000;
  542. fec->eth->tfifo_status &= 0x00700000;
  543. fec->eth->reset_cntrl = 0x01000000;
  544. /*
  545. * Issue a reset command to the FEC chip
  546. */
  547. fec->eth->ecntrl |= 0x1;
  548. /*
  549. * wait at least 16 clock cycles
  550. */
  551. udelay(10);
  552. #if (DEBUG & 0x3)
  553. printf("Ethernet task stopped\n");
  554. #endif
  555. }
  556. #if (DEBUG & 0x60)
  557. /********************************************************************/
  558. static void tfifo_print(char *devname, mpc5xxx_fec_priv *fec)
  559. {
  560. uint16 phyAddr = CONFIG_PHY_ADDR;
  561. uint16 phyStatus;
  562. if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
  563. || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
  564. miiphy_read(devname, phyAddr, 0x1, &phyStatus);
  565. printf("\nphyStatus: 0x%04x\n", phyStatus);
  566. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  567. printf("ievent: 0x%08x\n", fec->eth->ievent);
  568. printf("x_status: 0x%08x\n", fec->eth->x_status);
  569. printf("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
  570. printf(" control 0x%08x\n", fec->eth->tfifo_cntrl);
  571. printf(" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
  572. printf(" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
  573. printf(" alarm 0x%08x\n", fec->eth->tfifo_alarm);
  574. printf(" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
  575. printf(" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
  576. }
  577. }
  578. static void rfifo_print(char *devname, mpc5xxx_fec_priv *fec)
  579. {
  580. uint16 phyAddr = CONFIG_PHY_ADDR;
  581. uint16 phyStatus;
  582. if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
  583. || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
  584. miiphy_read(devname, phyAddr, 0x1, &phyStatus);
  585. printf("\nphyStatus: 0x%04x\n", phyStatus);
  586. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  587. printf("ievent: 0x%08x\n", fec->eth->ievent);
  588. printf("x_status: 0x%08x\n", fec->eth->x_status);
  589. printf("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
  590. printf(" control 0x%08x\n", fec->eth->rfifo_cntrl);
  591. printf(" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
  592. printf(" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
  593. printf(" alarm 0x%08x\n", fec->eth->rfifo_alarm);
  594. printf(" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
  595. printf(" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
  596. }
  597. }
  598. #endif /* DEBUG */
  599. /********************************************************************/
  600. static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
  601. int data_length)
  602. {
  603. /*
  604. * This routine transmits one frame. This routine only accepts
  605. * 6-byte Ethernet addresses.
  606. */
  607. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  608. volatile FEC_TBD *pTbd;
  609. #if (DEBUG & 0x20)
  610. printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
  611. tfifo_print(dev->name, fec);
  612. #endif
  613. /*
  614. * Clear Tx BD ring at first
  615. */
  616. mpc5xxx_fec_tbd_scrub(fec);
  617. /*
  618. * Check for valid length of data.
  619. */
  620. if ((data_length > 1500) || (data_length <= 0)) {
  621. return -1;
  622. }
  623. /*
  624. * Check the number of vacant TxBDs.
  625. */
  626. if (fec->cleanTbdNum < 1) {
  627. #if (DEBUG & 0x20)
  628. printf("No available TxBDs ...\n");
  629. #endif
  630. return -1;
  631. }
  632. /*
  633. * Get the first TxBD to send the mac header
  634. */
  635. pTbd = &fec->tbdBase[fec->tbdIndex];
  636. pTbd->dataLength = data_length;
  637. pTbd->dataPointer = (uint32)eth_data;
  638. pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  639. fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
  640. #if (DEBUG & 0x100)
  641. printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
  642. #endif
  643. /*
  644. * Kick the MII i/f
  645. */
  646. if (fec->xcv_type != SEVENWIRE) {
  647. uint16 phyStatus;
  648. miiphy_read(dev->name, 0, 0x1, &phyStatus);
  649. }
  650. /*
  651. * Enable SmartDMA transmit task
  652. */
  653. #if (DEBUG & 0x20)
  654. tfifo_print(dev->name, fec);
  655. #endif
  656. SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
  657. #if (DEBUG & 0x20)
  658. tfifo_print(dev->name, fec);
  659. #endif
  660. #if (DEBUG & 0x8)
  661. printf( "+" );
  662. #endif
  663. fec->cleanTbdNum -= 1;
  664. #if (DEBUG & 0x129) && (DEBUG & 0x80000000)
  665. printf ("smartDMA ethernet Tx task enabled\n");
  666. #endif
  667. /*
  668. * wait until frame is sent .
  669. */
  670. while (pTbd->status & FEC_TBD_READY) {
  671. udelay(10);
  672. #if (DEBUG & 0x8)
  673. printf ("TDB status = %04x\n", pTbd->status);
  674. #endif
  675. }
  676. return 0;
  677. }
  678. /********************************************************************/
  679. static int mpc5xxx_fec_recv(struct eth_device *dev)
  680. {
  681. /*
  682. * This command pulls one frame from the card
  683. */
  684. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  685. volatile FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
  686. unsigned long ievent;
  687. int frame_length, len = 0;
  688. NBUF *frame;
  689. uchar buff[FEC_MAX_PKT_SIZE];
  690. #if (DEBUG & 0x1)
  691. printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
  692. #endif
  693. #if (DEBUG & 0x8)
  694. printf( "-" );
  695. #endif
  696. /*
  697. * Check if any critical events have happened
  698. */
  699. ievent = fec->eth->ievent;
  700. fec->eth->ievent = ievent;
  701. if (ievent & 0x20060000) {
  702. /* BABT, Rx/Tx FIFO errors */
  703. mpc5xxx_fec_halt(dev);
  704. mpc5xxx_fec_init(dev, NULL);
  705. return 0;
  706. }
  707. if (ievent & 0x80000000) {
  708. /* Heartbeat error */
  709. fec->eth->x_cntrl |= 0x00000001;
  710. }
  711. if (ievent & 0x10000000) {
  712. /* Graceful stop complete */
  713. if (fec->eth->x_cntrl & 0x00000001) {
  714. mpc5xxx_fec_halt(dev);
  715. fec->eth->x_cntrl &= ~0x00000001;
  716. mpc5xxx_fec_init(dev, NULL);
  717. }
  718. }
  719. if (!(pRbd->status & FEC_RBD_EMPTY)) {
  720. if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
  721. ((pRbd->dataLength - 4) > 14)) {
  722. /*
  723. * Get buffer address and size
  724. */
  725. frame = (NBUF *)pRbd->dataPointer;
  726. frame_length = pRbd->dataLength - 4;
  727. #if (DEBUG & 0x20)
  728. {
  729. int i;
  730. printf("recv data hdr:");
  731. for (i = 0; i < 14; i++)
  732. printf("%x ", *(frame->head + i));
  733. printf("\n");
  734. }
  735. #endif
  736. /*
  737. * Fill the buffer and pass it to upper layers
  738. */
  739. memcpy(buff, frame->head, 14);
  740. memcpy(buff + 14, frame->data, frame_length);
  741. NetReceive(buff, frame_length);
  742. len = frame_length;
  743. }
  744. /*
  745. * Reset buffer descriptor as empty
  746. */
  747. mpc5xxx_fec_rbd_clean(fec, pRbd);
  748. }
  749. SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
  750. return len;
  751. }
  752. /********************************************************************/
  753. int mpc5xxx_fec_initialize(bd_t * bis)
  754. {
  755. mpc5xxx_fec_priv *fec;
  756. struct eth_device *dev;
  757. char *tmp, *end;
  758. char env_enetaddr[6];
  759. int i;
  760. fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
  761. dev = (struct eth_device *)malloc(sizeof(*dev));
  762. memset(dev, 0, sizeof *dev);
  763. fec->eth = (ethernet_regs *)MPC5XXX_FEC;
  764. fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
  765. fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
  766. #if defined(CONFIG_CANMB) || defined(CONFIG_HMI1001) || \
  767. defined(CONFIG_ICECUBE) || defined(CONFIG_INKA4X0) || \
  768. defined(CONFIG_JUPITER) || defined(CONFIG_MCC200) || \
  769. defined(CONFIG_MOTIONPRO)|| defined(CONFIG_O2DNT) || \
  770. defined(CONFIG_PM520) || defined(CONFIG_TOP5200) || \
  771. defined(CONFIG_TQM5200) || defined(CONFIG_UC101) || \
  772. defined(CONFIG_V38B)
  773. # ifndef CONFIG_FEC_10MBIT
  774. fec->xcv_type = MII100;
  775. # else
  776. fec->xcv_type = MII10;
  777. # endif
  778. #elif defined(CONFIG_TOTAL5200)
  779. fec->xcv_type = SEVENWIRE;
  780. #else
  781. #error fec->xcv_type not initialized.
  782. #endif
  783. dev->priv = (void *)fec;
  784. dev->iobase = MPC5XXX_FEC;
  785. dev->init = mpc5xxx_fec_init;
  786. dev->halt = mpc5xxx_fec_halt;
  787. dev->send = mpc5xxx_fec_send;
  788. dev->recv = mpc5xxx_fec_recv;
  789. sprintf(dev->name, "FEC ETHERNET");
  790. eth_register(dev);
  791. #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII) || defined(CONFIG_CMD_MII)
  792. miiphy_register (dev->name,
  793. fec5xxx_miiphy_read, fec5xxx_miiphy_write);
  794. #endif
  795. /*
  796. * Try to set the mac address now. The fec mac address is
  797. * a garbage after reset. When not using fec for booting
  798. * the Linux fec driver will try to work with this garbage.
  799. */
  800. tmp = getenv("ethaddr");
  801. if (tmp) {
  802. for (i=0; i<6; i++) {
  803. env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  804. if (tmp)
  805. tmp = (*end) ? end+1 : end;
  806. }
  807. mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
  808. }
  809. mpc5xxx_fec_init_phy(dev, bis);
  810. return 1;
  811. }
  812. /* MII-interface related functions */
  813. /********************************************************************/
  814. int fec5xxx_miiphy_read(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(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. }
  874. #if (DEBUG & 0x40)
  875. static uint32 local_crc32(char *string, unsigned int crc_value, int len)
  876. {
  877. int i;
  878. char c;
  879. unsigned int crc, count;
  880. /*
  881. * crc32 algorithm
  882. */
  883. /*
  884. * crc = 0xffffffff; * The initialized value should be 0xffffffff
  885. */
  886. crc = crc_value;
  887. for (i = len; --i >= 0;) {
  888. c = *string++;
  889. for (count = 0; count < 8; count++) {
  890. if ((c & 0x01) ^ (crc & 0x01)) {
  891. crc >>= 1;
  892. crc = crc ^ 0xedb88320;
  893. } else {
  894. crc >>= 1;
  895. }
  896. c >>= 1;
  897. }
  898. }
  899. /*
  900. * In big endian system, do byte swaping for crc value
  901. */
  902. /**/ return crc;
  903. }
  904. #endif /* DEBUG */
  905. #endif /* CONFIG_MPC5xxx_FEC */