mpc5xxx_fec.c 25 KB

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