fec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. /*
  255. * tbd - rtm
  256. * fec->eth->mii_speed = (((gd->arch.ipb_clk >> 20) / 5) << 1);
  257. * No MII for 7-wire mode
  258. */
  259. fec->eth->mii_speed = 0x00000030;
  260. }
  261. /*
  262. * Set Opcode/Pause Duration Register
  263. */
  264. fec->eth->op_pause = 0x00010020; /*FIXME0xffff0020; */
  265. /*
  266. * Set Rx FIFO alarm and granularity value
  267. */
  268. fec->eth->rfifo_cntrl = 0x0c000000;
  269. fec->eth->rfifo_alarm = 0x0000030c;
  270. #ifdef DEBUG
  271. if (fec->eth->rfifo_status & 0x00700000) {
  272. printf ("mpc8220_fec_init() RFIFO error\n");
  273. }
  274. #endif
  275. /*
  276. * Set Tx FIFO granularity value
  277. */
  278. /*fec->eth->tfifo_cntrl = 0x0c000000; */ /*tbd - rtm */
  279. fec->eth->tfifo_cntrl = 0x0e000000;
  280. #ifdef DEBUG
  281. printf ("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
  282. printf ("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
  283. #endif
  284. /*
  285. * Set transmit fifo watermark register(X_WMRK), default = 64
  286. */
  287. fec->eth->tfifo_alarm = 0x00000080;
  288. fec->eth->x_wmrk = 0x2;
  289. /*
  290. * Set individual address filter for unicast address
  291. * and set physical address registers.
  292. */
  293. mpc8220_fec_set_hwaddr (fec, (char *)(dev->enetaddr));
  294. /*
  295. * Set multicast address filter
  296. */
  297. fec->eth->gaddr1 = 0x00000000;
  298. fec->eth->gaddr2 = 0x00000000;
  299. /*
  300. * Turn ON cheater FSM: ????
  301. */
  302. fec->eth->xmit_fsm = 0x03000000;
  303. #if 1
  304. /*#if defined(CONFIG_MPC5200)*/
  305. /*
  306. * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
  307. * work w/ the current receive task.
  308. */
  309. dma->PtdCntrl |= 0x00000001;
  310. #endif
  311. /*
  312. * Set priority of different initiators
  313. */
  314. dma->IPR0 = 7; /* always */
  315. dma->IPR3 = 6; /* Eth RX */
  316. dma->IPR4 = 5; /* Eth Tx */
  317. /*
  318. * Clear SmartDMA task interrupt pending bits
  319. */
  320. DMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
  321. /*
  322. * Initialize SmartDMA parameters stored in SRAM
  323. */
  324. *(int *) FEC_TBD_BASE = (int) fec->tbdBase;
  325. *(int *) FEC_RBD_BASE = (int) fec->rbdBase;
  326. *(int *) FEC_TBD_NEXT = (int) fec->tbdBase;
  327. *(int *) FEC_RBD_NEXT = (int) fec->rbdBase;
  328. if (fec->xcv_type != SEVENWIRE) {
  329. /*
  330. * Initialize PHY(LXT971A):
  331. *
  332. * Generally, on power up, the LXT971A reads its configuration
  333. * pins to check for forced operation, If not cofigured for
  334. * forced operation, it uses auto-negotiation/parallel detection
  335. * to automatically determine line operating conditions.
  336. * If the PHY device on the other side of the link supports
  337. * auto-negotiation, the LXT971A auto-negotiates with it
  338. * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
  339. * support auto-negotiation, the LXT971A automatically detects
  340. * the presence of either link pulses(10Mbps PHY) or Idle
  341. * symbols(100Mbps) and sets its operating conditions accordingly.
  342. *
  343. * When auto-negotiation is controlled by software, the following
  344. * steps are recommended.
  345. *
  346. * Note:
  347. * The physical address is dependent on hardware configuration.
  348. *
  349. */
  350. int timeout = 1;
  351. u16 phyStatus;
  352. /*
  353. * Reset PHY, then delay 300ns
  354. */
  355. miiphy_write (dev->name, phyAddr, 0x0, 0x8000);
  356. udelay (1000);
  357. if (fec->xcv_type == MII10) {
  358. /*
  359. * Force 10Base-T, FDX operation
  360. */
  361. #ifdef DEBUG
  362. printf ("Forcing 10 Mbps ethernet link... ");
  363. #endif
  364. miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
  365. /*
  366. miiphy_write(fec, phyAddr, 0x0, 0x0100);
  367. */
  368. miiphy_write (dev->name, phyAddr, 0x0, 0x0180);
  369. timeout = 20;
  370. do { /* wait for link status to go down */
  371. udelay (10000);
  372. if ((timeout--) == 0) {
  373. #ifdef DEBUG
  374. printf ("hmmm, should not have waited...");
  375. #endif
  376. break;
  377. }
  378. miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
  379. #ifdef DEBUG
  380. printf ("=");
  381. #endif
  382. } while ((phyStatus & 0x0004)); /* !link up */
  383. timeout = 1000;
  384. do { /* wait for link status to come back up */
  385. udelay (10000);
  386. if ((timeout--) == 0) {
  387. printf ("failed. Link is down.\n");
  388. break;
  389. }
  390. miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
  391. #ifdef DEBUG
  392. printf ("+");
  393. #endif
  394. } while (!(phyStatus & 0x0004)); /* !link up */
  395. #ifdef DEBUG
  396. printf ("done.\n");
  397. #endif
  398. } else { /* MII100 */
  399. /*
  400. * Set the auto-negotiation advertisement register bits
  401. */
  402. miiphy_write (dev->name, phyAddr, 0x4, 0x01e1);
  403. /*
  404. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  405. */
  406. miiphy_write (dev->name, phyAddr, 0x0, 0x1200);
  407. /*
  408. * Wait for AN completion
  409. */
  410. timeout = 5000;
  411. do {
  412. udelay (1000);
  413. if ((timeout--) == 0) {
  414. #ifdef DEBUG
  415. printf ("PHY auto neg 0 failed...\n");
  416. #endif
  417. return -1;
  418. }
  419. if (miiphy_read (dev->name, phyAddr, 0x1, &phyStatus) !=
  420. 0) {
  421. #ifdef DEBUG
  422. printf ("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  423. #endif
  424. return -1;
  425. }
  426. } while (!(phyStatus & 0x0004));
  427. #ifdef DEBUG
  428. printf ("PHY auto neg complete! \n");
  429. #endif
  430. }
  431. }
  432. /*
  433. * Enable FEC-Lite controller
  434. */
  435. fec->eth->ecntrl |= 0x00000006;
  436. #ifdef DEBUG
  437. if (fec->xcv_type != SEVENWIRE)
  438. mpc8220_fec_phydump (dev->name);
  439. #endif
  440. /*
  441. * Enable SmartDMA receive task
  442. */
  443. DMA_TASK_ENABLE (FEC_RECV_TASK_NO);
  444. #ifdef DEBUG
  445. printf ("mpc8220_fec_init... Done \n");
  446. #endif
  447. return 1;
  448. }
  449. /********************************************************************/
  450. static void mpc8220_fec_halt (struct eth_device *dev)
  451. {
  452. mpc8220_fec_priv *fec = (mpc8220_fec_priv *) dev->priv;
  453. int counter = 0xffff;
  454. #ifdef DEBUG
  455. if (fec->xcv_type != SEVENWIRE)
  456. mpc8220_fec_phydump (dev->name);
  457. #endif
  458. /*
  459. * mask FEC chip interrupts
  460. */
  461. fec->eth->imask = 0;
  462. /*
  463. * issue graceful stop command to the FEC transmitter if necessary
  464. */
  465. fec->eth->x_cntrl |= 0x00000001;
  466. /*
  467. * wait for graceful stop to register
  468. */
  469. while ((counter--) && (!(fec->eth->ievent & 0x10000000)));
  470. /*
  471. * Disable SmartDMA tasks
  472. */
  473. DMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
  474. DMA_TASK_DISABLE (FEC_RECV_TASK_NO);
  475. /*
  476. * Disable the Ethernet Controller
  477. */
  478. fec->eth->ecntrl &= 0xfffffffd;
  479. /*
  480. * Clear FIFO status registers
  481. */
  482. fec->eth->rfifo_status &= 0x00700000;
  483. fec->eth->tfifo_status &= 0x00700000;
  484. fec->eth->reset_cntrl = 0x01000000;
  485. /*
  486. * Issue a reset command to the FEC chip
  487. */
  488. fec->eth->ecntrl |= 0x1;
  489. /*
  490. * wait at least 16 clock cycles
  491. */
  492. udelay (10);
  493. #ifdef DEBUG
  494. printf ("Ethernet task stopped\n");
  495. #endif
  496. }
  497. #ifdef DEBUG
  498. /********************************************************************/
  499. static void tfifo_print (char *devname, mpc8220_fec_priv * fec)
  500. {
  501. u16 phyAddr = CONFIG_PHY_ADDR;
  502. u16 phyStatus;
  503. if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
  504. || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
  505. miiphy_read (devname, phyAddr, 0x1, &phyStatus);
  506. printf ("\nphyStatus: 0x%04x\n", phyStatus);
  507. printf ("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  508. printf ("ievent: 0x%08x\n", fec->eth->ievent);
  509. printf ("x_status: 0x%08x\n", fec->eth->x_status);
  510. printf ("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
  511. printf (" control 0x%08x\n", fec->eth->tfifo_cntrl);
  512. printf (" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
  513. printf (" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
  514. printf (" alarm 0x%08x\n", fec->eth->tfifo_alarm);
  515. printf (" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
  516. printf (" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
  517. }
  518. }
  519. static void rfifo_print (char *devname, mpc8220_fec_priv * fec)
  520. {
  521. u16 phyAddr = CONFIG_PHY_ADDR;
  522. u16 phyStatus;
  523. if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
  524. || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
  525. miiphy_read (devname, phyAddr, 0x1, &phyStatus);
  526. printf ("\nphyStatus: 0x%04x\n", phyStatus);
  527. printf ("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  528. printf ("ievent: 0x%08x\n", fec->eth->ievent);
  529. printf ("x_status: 0x%08x\n", fec->eth->x_status);
  530. printf ("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
  531. printf (" control 0x%08x\n", fec->eth->rfifo_cntrl);
  532. printf (" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
  533. printf (" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
  534. printf (" alarm 0x%08x\n", fec->eth->rfifo_alarm);
  535. printf (" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
  536. printf (" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
  537. }
  538. }
  539. #endif /* DEBUG */
  540. /********************************************************************/
  541. static int mpc8220_fec_send(struct eth_device *dev, void *eth_data,
  542. int data_length)
  543. {
  544. /*
  545. * This routine transmits one frame. This routine only accepts
  546. * 6-byte Ethernet addresses.
  547. */
  548. mpc8220_fec_priv *fec = (mpc8220_fec_priv *) dev->priv;
  549. FEC_TBD *pTbd;
  550. #ifdef DEBUG
  551. printf ("tbd status: 0x%04x\n", fec->tbdBase[0].status);
  552. tfifo_print (dev->name, fec);
  553. #endif
  554. /*
  555. * Clear Tx BD ring at first
  556. */
  557. mpc8220_fec_tbd_scrub (fec);
  558. /*
  559. * Check for valid length of data.
  560. */
  561. if ((data_length > 1500) || (data_length <= 0)) {
  562. return -1;
  563. }
  564. /*
  565. * Check the number of vacant TxBDs.
  566. */
  567. if (fec->cleanTbdNum < 1) {
  568. #ifdef DEBUG
  569. printf ("No available TxBDs ...\n");
  570. #endif
  571. return -1;
  572. }
  573. /*
  574. * Get the first TxBD to send the mac header
  575. */
  576. pTbd = &fec->tbdBase[fec->tbdIndex];
  577. pTbd->dataLength = data_length;
  578. pTbd->dataPointer = (u32) eth_data;
  579. pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  580. fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
  581. #ifdef DEBUG
  582. printf ("DMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
  583. #endif
  584. /*
  585. * Kick the MII i/f
  586. */
  587. if (fec->xcv_type != SEVENWIRE) {
  588. u16 phyStatus;
  589. miiphy_read (dev->name, 0, 0x1, &phyStatus);
  590. }
  591. /*
  592. * Enable SmartDMA transmit task
  593. */
  594. #ifdef DEBUG
  595. tfifo_print (dev->name, fec);
  596. #endif
  597. DMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
  598. #ifdef DEBUG
  599. tfifo_print (dev->name, fec);
  600. #endif
  601. #ifdef DEBUG
  602. printf ("+");
  603. #endif
  604. fec->cleanTbdNum -= 1;
  605. #ifdef DEBUG
  606. printf ("smartDMA ethernet Tx task enabled\n");
  607. #endif
  608. /*
  609. * wait until frame is sent .
  610. */
  611. while (pTbd->status & FEC_TBD_READY) {
  612. udelay (10);
  613. #ifdef DEBUG
  614. printf ("TDB status = %04x\n", pTbd->status);
  615. #endif
  616. }
  617. return 0;
  618. }
  619. /********************************************************************/
  620. static int mpc8220_fec_recv (struct eth_device *dev)
  621. {
  622. /*
  623. * This command pulls one frame from the card
  624. */
  625. mpc8220_fec_priv *fec = (mpc8220_fec_priv *) dev->priv;
  626. FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
  627. unsigned long ievent;
  628. int frame_length, len = 0;
  629. NBUF *frame;
  630. #ifdef DEBUG
  631. printf ("mpc8220_fec_recv %d Start...\n", fec->rbdIndex);
  632. printf ("-");
  633. #endif
  634. /*
  635. * Check if any critical events have happened
  636. */
  637. ievent = fec->eth->ievent;
  638. fec->eth->ievent = ievent;
  639. if (ievent & 0x20060000) {
  640. /* BABT, Rx/Tx FIFO errors */
  641. mpc8220_fec_halt (dev);
  642. mpc8220_fec_init (dev, NULL);
  643. return 0;
  644. }
  645. if (ievent & 0x80000000) {
  646. /* Heartbeat error */
  647. fec->eth->x_cntrl |= 0x00000001;
  648. }
  649. if (ievent & 0x10000000) {
  650. /* Graceful stop complete */
  651. if (fec->eth->x_cntrl & 0x00000001) {
  652. mpc8220_fec_halt (dev);
  653. fec->eth->x_cntrl &= ~0x00000001;
  654. mpc8220_fec_init (dev, NULL);
  655. }
  656. }
  657. if (!(pRbd->status & FEC_RBD_EMPTY)) {
  658. if ((pRbd->status & FEC_RBD_LAST)
  659. && !(pRbd->status & FEC_RBD_ERR)
  660. && ((pRbd->dataLength - 4) > 14)) {
  661. /*
  662. * Get buffer address and size
  663. */
  664. frame = (NBUF *) pRbd->dataPointer;
  665. frame_length = pRbd->dataLength - 4;
  666. /* DEBUG code */
  667. if (_DEBUG) {
  668. int i;
  669. printf ("recv data hdr:");
  670. for (i = 0; i < 14; i++)
  671. printf ("%x ", *(frame->head + i));
  672. printf ("\n");
  673. }
  674. /*
  675. * Fill the buffer and pass it to upper layers
  676. */
  677. /* memcpy(buff, frame->head, 14);
  678. memcpy(buff + 14, frame->data, frame_length);*/
  679. NetReceive((uchar *)pRbd->dataPointer, 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 */