mpc5xxx_fec.c 24 KB

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