mpc5xxx_fec.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. */
  379. fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
  380. }
  381. if (fec->xcv_type != SEVENWIRE) {
  382. /*
  383. * Initialize PHY(LXT971A):
  384. *
  385. * Generally, on power up, the LXT971A reads its configuration
  386. * pins to check for forced operation, If not cofigured for
  387. * forced operation, it uses auto-negotiation/parallel detection
  388. * to automatically determine line operating conditions.
  389. * If the PHY device on the other side of the link supports
  390. * auto-negotiation, the LXT971A auto-negotiates with it
  391. * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
  392. * support auto-negotiation, the LXT971A automatically detects
  393. * the presence of either link pulses(10Mbps PHY) or Idle
  394. * symbols(100Mbps) and sets its operating conditions accordingly.
  395. *
  396. * When auto-negotiation is controlled by software, the following
  397. * steps are recommended.
  398. *
  399. * Note:
  400. * The physical address is dependent on hardware configuration.
  401. *
  402. */
  403. int timeout = 1;
  404. uint16 phyStatus;
  405. /*
  406. * Reset PHY, then delay 300ns
  407. */
  408. miiphy_write(dev->name, phyAddr, 0x0, 0x8000);
  409. udelay(1000);
  410. #if defined(CONFIG_UC101) || defined(CONFIG_MUCMC52)
  411. /* Set the LED configuration Register for the UC101
  412. and MUCMC52 Board */
  413. miiphy_write(dev->name, phyAddr, 0x14, 0x4122);
  414. #endif
  415. if (fec->xcv_type == MII10) {
  416. /*
  417. * Force 10Base-T, FDX operation
  418. */
  419. #if (DEBUG & 0x2)
  420. printf("Forcing 10 Mbps ethernet link... ");
  421. #endif
  422. miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
  423. /*
  424. miiphy_write(dev->name, fec, phyAddr, 0x0, 0x0100);
  425. */
  426. miiphy_write(dev->name, phyAddr, 0x0, 0x0180);
  427. timeout = 20;
  428. do { /* wait for link status to go down */
  429. udelay(10000);
  430. if ((timeout--) == 0) {
  431. #if (DEBUG & 0x2)
  432. printf("hmmm, should not have waited...");
  433. #endif
  434. break;
  435. }
  436. miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
  437. #if (DEBUG & 0x2)
  438. printf("=");
  439. #endif
  440. } while ((phyStatus & 0x0004)); /* !link up */
  441. timeout = 1000;
  442. do { /* wait for link status to come back up */
  443. udelay(10000);
  444. if ((timeout--) == 0) {
  445. printf("failed. Link is down.\n");
  446. break;
  447. }
  448. miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
  449. #if (DEBUG & 0x2)
  450. printf("+");
  451. #endif
  452. } while (!(phyStatus & 0x0004)); /* !link up */
  453. #if (DEBUG & 0x2)
  454. printf ("done.\n");
  455. #endif
  456. } else { /* MII100 */
  457. /*
  458. * Set the auto-negotiation advertisement register bits
  459. */
  460. miiphy_write(dev->name, phyAddr, 0x4, 0x01e1);
  461. /*
  462. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  463. */
  464. miiphy_write(dev->name, phyAddr, 0x0, 0x1200);
  465. /*
  466. * Wait for AN completion
  467. */
  468. timeout = 5000;
  469. do {
  470. udelay(1000);
  471. if ((timeout--) == 0) {
  472. #if (DEBUG & 0x2)
  473. printf("PHY auto neg 0 failed...\n");
  474. #endif
  475. return -1;
  476. }
  477. if (miiphy_read(dev->name, phyAddr, 0x1, &phyStatus) != 0) {
  478. #if (DEBUG & 0x2)
  479. printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  480. #endif
  481. return -1;
  482. }
  483. } while (!(phyStatus & 0x0004));
  484. #if (DEBUG & 0x2)
  485. printf("PHY auto neg complete! \n");
  486. #endif
  487. }
  488. }
  489. #if (DEBUG & 0x2)
  490. if (fec->xcv_type != SEVENWIRE)
  491. mpc5xxx_fec_phydump (dev->name);
  492. #endif
  493. #if (DEBUG & 0x1)
  494. printf("mpc5xxx_fec_init_phy... Done \n");
  495. #endif
  496. return 1;
  497. }
  498. /********************************************************************/
  499. static void mpc5xxx_fec_halt(struct eth_device *dev)
  500. {
  501. struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
  502. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  503. int counter = 0xffff;
  504. #if (DEBUG & 0x2)
  505. if (fec->xcv_type != SEVENWIRE)
  506. mpc5xxx_fec_phydump (dev->name);
  507. #endif
  508. /*
  509. * mask FEC chip interrupts
  510. */
  511. fec->eth->imask = 0;
  512. /*
  513. * issue graceful stop command to the FEC transmitter if necessary
  514. */
  515. fec->eth->x_cntrl |= 0x00000001;
  516. /*
  517. * wait for graceful stop to register
  518. */
  519. while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
  520. /*
  521. * Disable SmartDMA tasks
  522. */
  523. SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
  524. SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
  525. /*
  526. * Turn on COMM bus prefetch in the MPC5200 BestComm after we're
  527. * done. It doesn't work w/ the current receive task.
  528. */
  529. sdma->PtdCntrl &= ~0x00000001;
  530. /*
  531. * Disable the Ethernet Controller
  532. */
  533. fec->eth->ecntrl &= 0xfffffffd;
  534. /*
  535. * Clear FIFO status registers
  536. */
  537. fec->eth->rfifo_status &= 0x00700000;
  538. fec->eth->tfifo_status &= 0x00700000;
  539. fec->eth->reset_cntrl = 0x01000000;
  540. /*
  541. * Issue a reset command to the FEC chip
  542. */
  543. fec->eth->ecntrl |= 0x1;
  544. /*
  545. * wait at least 16 clock cycles
  546. */
  547. udelay(10);
  548. /* don't leave the MII speed set to zero */
  549. if (fec->xcv_type != SEVENWIRE) {
  550. /*
  551. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  552. * and do not drop the Preamble.
  553. */
  554. fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
  555. }
  556. #if (DEBUG & 0x3)
  557. printf("Ethernet task stopped\n");
  558. #endif
  559. }
  560. #if (DEBUG & 0x60)
  561. /********************************************************************/
  562. static void tfifo_print(char *devname, mpc5xxx_fec_priv *fec)
  563. {
  564. uint16 phyAddr = CONFIG_PHY_ADDR;
  565. uint16 phyStatus;
  566. if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
  567. || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
  568. miiphy_read(devname, phyAddr, 0x1, &phyStatus);
  569. printf("\nphyStatus: 0x%04x\n", phyStatus);
  570. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  571. printf("ievent: 0x%08x\n", fec->eth->ievent);
  572. printf("x_status: 0x%08x\n", fec->eth->x_status);
  573. printf("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
  574. printf(" control 0x%08x\n", fec->eth->tfifo_cntrl);
  575. printf(" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
  576. printf(" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
  577. printf(" alarm 0x%08x\n", fec->eth->tfifo_alarm);
  578. printf(" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
  579. printf(" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
  580. }
  581. }
  582. static void rfifo_print(char *devname, mpc5xxx_fec_priv *fec)
  583. {
  584. uint16 phyAddr = CONFIG_PHY_ADDR;
  585. uint16 phyStatus;
  586. if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
  587. || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
  588. miiphy_read(devname, phyAddr, 0x1, &phyStatus);
  589. printf("\nphyStatus: 0x%04x\n", phyStatus);
  590. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  591. printf("ievent: 0x%08x\n", fec->eth->ievent);
  592. printf("x_status: 0x%08x\n", fec->eth->x_status);
  593. printf("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
  594. printf(" control 0x%08x\n", fec->eth->rfifo_cntrl);
  595. printf(" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
  596. printf(" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
  597. printf(" alarm 0x%08x\n", fec->eth->rfifo_alarm);
  598. printf(" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
  599. printf(" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
  600. }
  601. }
  602. #endif /* DEBUG */
  603. /********************************************************************/
  604. static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
  605. int data_length)
  606. {
  607. /*
  608. * This routine transmits one frame. This routine only accepts
  609. * 6-byte Ethernet addresses.
  610. */
  611. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  612. volatile FEC_TBD *pTbd;
  613. #if (DEBUG & 0x20)
  614. printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
  615. tfifo_print(dev->name, fec);
  616. #endif
  617. /*
  618. * Clear Tx BD ring at first
  619. */
  620. mpc5xxx_fec_tbd_scrub(fec);
  621. /*
  622. * Check for valid length of data.
  623. */
  624. if ((data_length > 1500) || (data_length <= 0)) {
  625. return -1;
  626. }
  627. /*
  628. * Check the number of vacant TxBDs.
  629. */
  630. if (fec->cleanTbdNum < 1) {
  631. #if (DEBUG & 0x20)
  632. printf("No available TxBDs ...\n");
  633. #endif
  634. return -1;
  635. }
  636. /*
  637. * Get the first TxBD to send the mac header
  638. */
  639. pTbd = &fec->tbdBase[fec->tbdIndex];
  640. pTbd->dataLength = data_length;
  641. pTbd->dataPointer = (uint32)eth_data;
  642. pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  643. fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
  644. #if (DEBUG & 0x100)
  645. printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
  646. #endif
  647. /*
  648. * Kick the MII i/f
  649. */
  650. if (fec->xcv_type != SEVENWIRE) {
  651. uint16 phyStatus;
  652. miiphy_read(dev->name, 0, 0x1, &phyStatus);
  653. }
  654. /*
  655. * Enable SmartDMA transmit task
  656. */
  657. #if (DEBUG & 0x20)
  658. tfifo_print(dev->name, fec);
  659. #endif
  660. SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
  661. #if (DEBUG & 0x20)
  662. tfifo_print(dev->name, fec);
  663. #endif
  664. #if (DEBUG & 0x8)
  665. printf( "+" );
  666. #endif
  667. fec->cleanTbdNum -= 1;
  668. #if (DEBUG & 0x129) && (DEBUG & 0x80000000)
  669. printf ("smartDMA ethernet Tx task enabled\n");
  670. #endif
  671. /*
  672. * wait until frame is sent .
  673. */
  674. while (pTbd->status & FEC_TBD_READY) {
  675. udelay(10);
  676. #if (DEBUG & 0x8)
  677. printf ("TDB status = %04x\n", pTbd->status);
  678. #endif
  679. }
  680. return 0;
  681. }
  682. /********************************************************************/
  683. static int mpc5xxx_fec_recv(struct eth_device *dev)
  684. {
  685. /*
  686. * This command pulls one frame from the card
  687. */
  688. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  689. volatile FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
  690. unsigned long ievent;
  691. int frame_length, len = 0;
  692. NBUF *frame;
  693. uchar buff[FEC_MAX_PKT_SIZE];
  694. #if (DEBUG & 0x1)
  695. printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
  696. #endif
  697. #if (DEBUG & 0x8)
  698. printf( "-" );
  699. #endif
  700. /*
  701. * Check if any critical events have happened
  702. */
  703. ievent = fec->eth->ievent;
  704. fec->eth->ievent = ievent;
  705. if (ievent & 0x20060000) {
  706. /* BABT, Rx/Tx FIFO errors */
  707. mpc5xxx_fec_halt(dev);
  708. mpc5xxx_fec_init(dev, NULL);
  709. return 0;
  710. }
  711. if (ievent & 0x80000000) {
  712. /* Heartbeat error */
  713. fec->eth->x_cntrl |= 0x00000001;
  714. }
  715. if (ievent & 0x10000000) {
  716. /* Graceful stop complete */
  717. if (fec->eth->x_cntrl & 0x00000001) {
  718. mpc5xxx_fec_halt(dev);
  719. fec->eth->x_cntrl &= ~0x00000001;
  720. mpc5xxx_fec_init(dev, NULL);
  721. }
  722. }
  723. if (!(pRbd->status & FEC_RBD_EMPTY)) {
  724. if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
  725. ((pRbd->dataLength - 4) > 14)) {
  726. /*
  727. * Get buffer address and size
  728. */
  729. frame = (NBUF *)pRbd->dataPointer;
  730. frame_length = pRbd->dataLength - 4;
  731. #if (DEBUG & 0x20)
  732. {
  733. int i;
  734. printf("recv data hdr:");
  735. for (i = 0; i < 14; i++)
  736. printf("%x ", *(frame->head + i));
  737. printf("\n");
  738. }
  739. #endif
  740. /*
  741. * Fill the buffer and pass it to upper layers
  742. */
  743. memcpy(buff, frame->head, 14);
  744. memcpy(buff + 14, frame->data, frame_length);
  745. NetReceive(buff, frame_length);
  746. len = frame_length;
  747. }
  748. /*
  749. * Reset buffer descriptor as empty
  750. */
  751. mpc5xxx_fec_rbd_clean(fec, pRbd);
  752. }
  753. SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
  754. return len;
  755. }
  756. /********************************************************************/
  757. int mpc5xxx_fec_initialize(bd_t * bis)
  758. {
  759. mpc5xxx_fec_priv *fec;
  760. struct eth_device *dev;
  761. char *tmp, *end;
  762. char env_enetaddr[6];
  763. int i;
  764. fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
  765. dev = (struct eth_device *)malloc(sizeof(*dev));
  766. memset(dev, 0, sizeof *dev);
  767. fec->eth = (ethernet_regs *)MPC5XXX_FEC;
  768. fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
  769. fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
  770. #if defined(CONFIG_MPC5xxx_FEC_MII100)
  771. fec->xcv_type = MII100;
  772. #elif defined(CONFIG_MPC5xxx_FEC_MII10)
  773. fec->xcv_type = MII10;
  774. #elif defined(CONFIG_MPC5xxx_FEC_SEVENWIRE)
  775. fec->xcv_type = SEVENWIRE;
  776. #else
  777. #error fec->xcv_type not initialized.
  778. #endif
  779. if (fec->xcv_type != SEVENWIRE) {
  780. /*
  781. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  782. * and do not drop the Preamble.
  783. */
  784. fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
  785. }
  786. dev->priv = (void *)fec;
  787. dev->iobase = MPC5XXX_FEC;
  788. dev->init = mpc5xxx_fec_init;
  789. dev->halt = mpc5xxx_fec_halt;
  790. dev->send = mpc5xxx_fec_send;
  791. dev->recv = mpc5xxx_fec_recv;
  792. sprintf(dev->name, "FEC");
  793. eth_register(dev);
  794. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  795. miiphy_register (dev->name,
  796. fec5xxx_miiphy_read, fec5xxx_miiphy_write);
  797. #endif
  798. /*
  799. * Try to set the mac address now. The fec mac address is
  800. * a garbage after reset. When not using fec for booting
  801. * the Linux fec driver will try to work with this garbage.
  802. */
  803. tmp = getenv("ethaddr");
  804. if (tmp) {
  805. for (i=0; i<6; i++) {
  806. env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  807. if (tmp)
  808. tmp = (*end) ? end+1 : end;
  809. }
  810. mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
  811. }
  812. return 1;
  813. }
  814. /* MII-interface related functions */
  815. /********************************************************************/
  816. int fec5xxx_miiphy_read(const char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal)
  817. {
  818. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  819. uint32 reg; /* convenient holder for the PHY register */
  820. uint32 phy; /* convenient holder for the PHY */
  821. int timeout = 0xffff;
  822. /*
  823. * reading from any PHY's register is done by properly
  824. * programming the FEC's MII data register.
  825. */
  826. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  827. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  828. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
  829. /*
  830. * wait for the related interrupt
  831. */
  832. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  833. if (timeout == 0) {
  834. #if (DEBUG & 0x2)
  835. printf ("Read MDIO failed...\n");
  836. #endif
  837. return -1;
  838. }
  839. /*
  840. * clear mii interrupt bit
  841. */
  842. eth->ievent = 0x00800000;
  843. /*
  844. * it's now safe to read the PHY's register
  845. */
  846. *retVal = (uint16) eth->mii_data;
  847. return 0;
  848. }
  849. /********************************************************************/
  850. int fec5xxx_miiphy_write(const char *devname, uint8 phyAddr, uint8 regAddr, uint16 data)
  851. {
  852. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  853. uint32 reg; /* convenient holder for the PHY register */
  854. uint32 phy; /* convenient holder for the PHY */
  855. int timeout = 0xffff;
  856. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  857. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  858. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
  859. FEC_MII_DATA_TA | phy | reg | data);
  860. /*
  861. * wait for the MII interrupt
  862. */
  863. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  864. if (timeout == 0) {
  865. #if (DEBUG & 0x2)
  866. printf ("Write MDIO failed...\n");
  867. #endif
  868. return -1;
  869. }
  870. /*
  871. * clear MII interrupt bit
  872. */
  873. eth->ievent = 0x00800000;
  874. return 0;
  875. }