mpc5xxx_fec.c 25 KB

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