fec.c 22 KB

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