fec.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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 <malloc.h>
  11. #include <net.h>
  12. #include <miiphy.h>
  13. #include "sdma.h"
  14. #include "fec.h"
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /* #define DEBUG 0x28 */
  17. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
  18. defined(CONFIG_MPC5xxx_FEC)
  19. #if !(defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII))
  20. #error "CONFIG_MII has to be defined!"
  21. #endif
  22. #if (DEBUG & 0x60)
  23. static void tfifo_print(char *devname, mpc5xxx_fec_priv *fec);
  24. static void rfifo_print(char *devname, mpc5xxx_fec_priv *fec);
  25. #endif /* DEBUG */
  26. #if (DEBUG & 0x40)
  27. static uint32 local_crc32(char *string, unsigned int crc_value, int len);
  28. #endif
  29. typedef struct {
  30. uint8 data[1500]; /* actual data */
  31. int length; /* actual length */
  32. int used; /* buffer in use or not */
  33. uint8 head[16]; /* MAC header(6 + 6 + 2) + 2(aligned) */
  34. } NBUF;
  35. int fec5xxx_miiphy_read(char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal);
  36. int fec5xxx_miiphy_write(char *devname, uint8 phyAddr, uint8 regAddr, uint16 data);
  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. /*
  219. * Initialize RxBD/TxBD rings
  220. */
  221. mpc5xxx_fec_rbd_init(fec);
  222. mpc5xxx_fec_tbd_init(fec);
  223. /*
  224. * Clear FEC-Lite interrupt event register(IEVENT)
  225. */
  226. fec->eth->ievent = 0xffffffff;
  227. /*
  228. * Set interrupt mask register
  229. */
  230. fec->eth->imask = 0x00000000;
  231. /*
  232. * Set FEC-Lite receive control register(R_CNTRL):
  233. */
  234. if (fec->xcv_type == SEVENWIRE) {
  235. /*
  236. * Frame length=1518; 7-wire mode
  237. */
  238. fec->eth->r_cntrl = 0x05ee0020; /*0x05ee0000;FIXME */
  239. } else {
  240. /*
  241. * Frame length=1518; MII mode;
  242. */
  243. fec->eth->r_cntrl = 0x05ee0024; /*0x05ee0004;FIXME */
  244. }
  245. fec->eth->x_cntrl = 0x00000000; /* half-duplex, heartbeat disabled */
  246. if (fec->xcv_type != SEVENWIRE) {
  247. /*
  248. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  249. * and do not drop the Preamble.
  250. */
  251. fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
  252. }
  253. /*
  254. * Set Opcode/Pause Duration Register
  255. */
  256. fec->eth->op_pause = 0x00010020; /*FIXME0xffff0020; */
  257. /*
  258. * Set Rx FIFO alarm and granularity value
  259. */
  260. fec->eth->rfifo_cntrl = 0x0c000000
  261. | (fec->eth->rfifo_cntrl & ~0x0f000000);
  262. fec->eth->rfifo_alarm = 0x0000030c;
  263. #if (DEBUG & 0x22)
  264. if (fec->eth->rfifo_status & 0x00700000 ) {
  265. printf("mpc5xxx_fec_init() RFIFO error\n");
  266. }
  267. #endif
  268. /*
  269. * Set Tx FIFO granularity value
  270. */
  271. fec->eth->tfifo_cntrl = 0x0c000000
  272. | (fec->eth->tfifo_cntrl & ~0x0f000000);
  273. #if (DEBUG & 0x2)
  274. printf("tfifo_status: 0x%08x\n", fec->eth->tfifo_status);
  275. printf("tfifo_alarm: 0x%08x\n", fec->eth->tfifo_alarm);
  276. #endif
  277. /*
  278. * Set transmit fifo watermark register(X_WMRK), default = 64
  279. */
  280. fec->eth->tfifo_alarm = 0x00000080;
  281. fec->eth->x_wmrk = 0x2;
  282. /*
  283. * Set individual address filter for unicast address
  284. * and set physical address registers.
  285. */
  286. mpc5xxx_fec_set_hwaddr(fec, (char *)dev->enetaddr);
  287. /*
  288. * Set multicast address filter
  289. */
  290. fec->eth->gaddr1 = 0x00000000;
  291. fec->eth->gaddr2 = 0x00000000;
  292. /*
  293. * Turn ON cheater FSM: ????
  294. */
  295. fec->eth->xmit_fsm = 0x03000000;
  296. #if defined(CONFIG_MPC5200)
  297. /*
  298. * Turn off COMM bus prefetch in the MGT5200 BestComm. It doesn't
  299. * work w/ the current receive task.
  300. */
  301. sdma->PtdCntrl |= 0x00000001;
  302. #endif
  303. /*
  304. * Set priority of different initiators
  305. */
  306. sdma->IPR0 = 7; /* always */
  307. sdma->IPR3 = 6; /* Eth RX */
  308. sdma->IPR4 = 5; /* Eth Tx */
  309. /*
  310. * Clear SmartDMA task interrupt pending bits
  311. */
  312. SDMA_CLEAR_IEVENT(FEC_RECV_TASK_NO);
  313. /*
  314. * Initialize SmartDMA parameters stored in SRAM
  315. */
  316. *(volatile int *)FEC_TBD_BASE = (int)fec->tbdBase;
  317. *(volatile int *)FEC_RBD_BASE = (int)fec->rbdBase;
  318. *(volatile int *)FEC_TBD_NEXT = (int)fec->tbdBase;
  319. *(volatile int *)FEC_RBD_NEXT = (int)fec->rbdBase;
  320. /*
  321. * Enable FEC-Lite controller
  322. */
  323. fec->eth->ecntrl |= 0x00000006;
  324. #if (DEBUG & 0x2)
  325. if (fec->xcv_type != SEVENWIRE)
  326. mpc5xxx_fec_phydump (dev->name);
  327. #endif
  328. /*
  329. * Enable SmartDMA receive task
  330. */
  331. SDMA_TASK_ENABLE(FEC_RECV_TASK_NO);
  332. #if (DEBUG & 0x1)
  333. printf("mpc5xxx_fec_init... Done \n");
  334. #endif
  335. return 1;
  336. }
  337. /********************************************************************/
  338. static int mpc5xxx_fec_init_phy(struct eth_device *dev, bd_t * bis)
  339. {
  340. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  341. const uint8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
  342. #if (DEBUG & 0x1)
  343. printf ("mpc5xxx_fec_init_phy... Begin\n");
  344. #endif
  345. /*
  346. * Initialize GPIO pins
  347. */
  348. if (fec->xcv_type == SEVENWIRE) {
  349. /* 10MBit with 7-wire operation */
  350. #if defined(CONFIG_TOTAL5200)
  351. /* 7-wire and USB2 on Ethernet */
  352. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00030000;
  353. #else /* !CONFIG_TOTAL5200 */
  354. /* 7-wire only */
  355. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00020000;
  356. #endif /* CONFIG_TOTAL5200 */
  357. } else {
  358. /* 100MBit with MD operation */
  359. *(vu_long *)MPC5XXX_GPS_PORT_CONFIG |= 0x00050000;
  360. }
  361. /*
  362. * Clear FEC-Lite interrupt event register(IEVENT)
  363. */
  364. fec->eth->ievent = 0xffffffff;
  365. /*
  366. * Set interrupt mask register
  367. */
  368. fec->eth->imask = 0x00000000;
  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. */
  374. fec->eth->mii_speed = (((gd->ipb_clk >> 20) / 5) << 1); /* No MII for 7-wire mode */
  375. }
  376. if (fec->xcv_type != SEVENWIRE) {
  377. /*
  378. * Initialize PHY(LXT971A):
  379. *
  380. * Generally, on power up, the LXT971A reads its configuration
  381. * pins to check for forced operation, If not cofigured for
  382. * forced operation, it uses auto-negotiation/parallel detection
  383. * to automatically determine line operating conditions.
  384. * If the PHY device on the other side of the link supports
  385. * auto-negotiation, the LXT971A auto-negotiates with it
  386. * using Fast Link Pulse(FLP) Bursts. If the PHY partner does not
  387. * support auto-negotiation, the LXT971A automatically detects
  388. * the presence of either link pulses(10Mbps PHY) or Idle
  389. * symbols(100Mbps) and sets its operating conditions accordingly.
  390. *
  391. * When auto-negotiation is controlled by software, the following
  392. * steps are recommended.
  393. *
  394. * Note:
  395. * The physical address is dependent on hardware configuration.
  396. *
  397. */
  398. int timeout = 1;
  399. uint16 phyStatus;
  400. /*
  401. * Reset PHY, then delay 300ns
  402. */
  403. miiphy_write(dev->name, phyAddr, 0x0, 0x8000);
  404. udelay(1000);
  405. #if defined(CONFIG_UC101)
  406. /* Set the LED configuration Register for the UC101 Board */
  407. miiphy_write(dev->name, phyAddr, 0x14, 0x4122);
  408. #endif
  409. if (fec->xcv_type == MII10) {
  410. /*
  411. * Force 10Base-T, FDX operation
  412. */
  413. #if (DEBUG & 0x2)
  414. printf("Forcing 10 Mbps ethernet link... ");
  415. #endif
  416. miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
  417. /*
  418. miiphy_write(dev->name, fec, phyAddr, 0x0, 0x0100);
  419. */
  420. miiphy_write(dev->name, phyAddr, 0x0, 0x0180);
  421. timeout = 20;
  422. do { /* wait for link status to go down */
  423. udelay(10000);
  424. if ((timeout--) == 0) {
  425. #if (DEBUG & 0x2)
  426. printf("hmmm, should not have waited...");
  427. #endif
  428. break;
  429. }
  430. miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
  431. #if (DEBUG & 0x2)
  432. printf("=");
  433. #endif
  434. } while ((phyStatus & 0x0004)); /* !link up */
  435. timeout = 1000;
  436. do { /* wait for link status to come back up */
  437. udelay(10000);
  438. if ((timeout--) == 0) {
  439. printf("failed. Link is down.\n");
  440. break;
  441. }
  442. miiphy_read(dev->name, phyAddr, 0x1, &phyStatus);
  443. #if (DEBUG & 0x2)
  444. printf("+");
  445. #endif
  446. } while (!(phyStatus & 0x0004)); /* !link up */
  447. #if (DEBUG & 0x2)
  448. printf ("done.\n");
  449. #endif
  450. } else { /* MII100 */
  451. /*
  452. * Set the auto-negotiation advertisement register bits
  453. */
  454. miiphy_write(dev->name, phyAddr, 0x4, 0x01e1);
  455. /*
  456. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  457. */
  458. miiphy_write(dev->name, phyAddr, 0x0, 0x1200);
  459. /*
  460. * Wait for AN completion
  461. */
  462. timeout = 5000;
  463. do {
  464. udelay(1000);
  465. if ((timeout--) == 0) {
  466. #if (DEBUG & 0x2)
  467. printf("PHY auto neg 0 failed...\n");
  468. #endif
  469. return -1;
  470. }
  471. if (miiphy_read(dev->name, phyAddr, 0x1, &phyStatus) != 0) {
  472. #if (DEBUG & 0x2)
  473. printf("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  474. #endif
  475. return -1;
  476. }
  477. } while (!(phyStatus & 0x0004));
  478. #if (DEBUG & 0x2)
  479. printf("PHY auto neg complete! \n");
  480. #endif
  481. }
  482. }
  483. #if (DEBUG & 0x2)
  484. if (fec->xcv_type != SEVENWIRE)
  485. mpc5xxx_fec_phydump (dev->name);
  486. #endif
  487. #if (DEBUG & 0x1)
  488. printf("mpc5xxx_fec_init_phy... Done \n");
  489. #endif
  490. return 1;
  491. }
  492. /********************************************************************/
  493. static void mpc5xxx_fec_halt(struct eth_device *dev)
  494. {
  495. #if defined(CONFIG_MPC5200)
  496. struct mpc5xxx_sdma *sdma = (struct mpc5xxx_sdma *)MPC5XXX_SDMA;
  497. #endif
  498. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  499. int counter = 0xffff;
  500. #if (DEBUG & 0x2)
  501. if (fec->xcv_type != SEVENWIRE)
  502. mpc5xxx_fec_phydump (dev->name);
  503. #endif
  504. /*
  505. * mask FEC chip interrupts
  506. */
  507. fec->eth->imask = 0;
  508. /*
  509. * issue graceful stop command to the FEC transmitter if necessary
  510. */
  511. fec->eth->x_cntrl |= 0x00000001;
  512. /*
  513. * wait for graceful stop to register
  514. */
  515. while ((counter--) && (!(fec->eth->ievent & 0x10000000))) ;
  516. /*
  517. * Disable SmartDMA tasks
  518. */
  519. SDMA_TASK_DISABLE (FEC_XMIT_TASK_NO);
  520. SDMA_TASK_DISABLE (FEC_RECV_TASK_NO);
  521. #if defined(CONFIG_MPC5200)
  522. /*
  523. * Turn on COMM bus prefetch in the MGT5200 BestComm after we're
  524. * done. It doesn't work w/ the current receive task.
  525. */
  526. sdma->PtdCntrl &= ~0x00000001;
  527. #endif
  528. /*
  529. * Disable the Ethernet Controller
  530. */
  531. fec->eth->ecntrl &= 0xfffffffd;
  532. /*
  533. * Clear FIFO status registers
  534. */
  535. fec->eth->rfifo_status &= 0x00700000;
  536. fec->eth->tfifo_status &= 0x00700000;
  537. fec->eth->reset_cntrl = 0x01000000;
  538. /*
  539. * Issue a reset command to the FEC chip
  540. */
  541. fec->eth->ecntrl |= 0x1;
  542. /*
  543. * wait at least 16 clock cycles
  544. */
  545. udelay(10);
  546. #if (DEBUG & 0x3)
  547. printf("Ethernet task stopped\n");
  548. #endif
  549. }
  550. #if (DEBUG & 0x60)
  551. /********************************************************************/
  552. static void tfifo_print(char *devname, mpc5xxx_fec_priv *fec)
  553. {
  554. uint16 phyAddr = CONFIG_PHY_ADDR;
  555. uint16 phyStatus;
  556. if ((fec->eth->tfifo_lrf_ptr != fec->eth->tfifo_lwf_ptr)
  557. || (fec->eth->tfifo_rdptr != fec->eth->tfifo_wrptr)) {
  558. miiphy_read(devname, phyAddr, 0x1, &phyStatus);
  559. printf("\nphyStatus: 0x%04x\n", phyStatus);
  560. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  561. printf("ievent: 0x%08x\n", fec->eth->ievent);
  562. printf("x_status: 0x%08x\n", fec->eth->x_status);
  563. printf("tfifo: status 0x%08x\n", fec->eth->tfifo_status);
  564. printf(" control 0x%08x\n", fec->eth->tfifo_cntrl);
  565. printf(" lrfp 0x%08x\n", fec->eth->tfifo_lrf_ptr);
  566. printf(" lwfp 0x%08x\n", fec->eth->tfifo_lwf_ptr);
  567. printf(" alarm 0x%08x\n", fec->eth->tfifo_alarm);
  568. printf(" readptr 0x%08x\n", fec->eth->tfifo_rdptr);
  569. printf(" writptr 0x%08x\n", fec->eth->tfifo_wrptr);
  570. }
  571. }
  572. static void rfifo_print(char *devname, mpc5xxx_fec_priv *fec)
  573. {
  574. uint16 phyAddr = CONFIG_PHY_ADDR;
  575. uint16 phyStatus;
  576. if ((fec->eth->rfifo_lrf_ptr != fec->eth->rfifo_lwf_ptr)
  577. || (fec->eth->rfifo_rdptr != fec->eth->rfifo_wrptr)) {
  578. miiphy_read(devname, phyAddr, 0x1, &phyStatus);
  579. printf("\nphyStatus: 0x%04x\n", phyStatus);
  580. printf("ecntrl: 0x%08x\n", fec->eth->ecntrl);
  581. printf("ievent: 0x%08x\n", fec->eth->ievent);
  582. printf("x_status: 0x%08x\n", fec->eth->x_status);
  583. printf("rfifo: status 0x%08x\n", fec->eth->rfifo_status);
  584. printf(" control 0x%08x\n", fec->eth->rfifo_cntrl);
  585. printf(" lrfp 0x%08x\n", fec->eth->rfifo_lrf_ptr);
  586. printf(" lwfp 0x%08x\n", fec->eth->rfifo_lwf_ptr);
  587. printf(" alarm 0x%08x\n", fec->eth->rfifo_alarm);
  588. printf(" readptr 0x%08x\n", fec->eth->rfifo_rdptr);
  589. printf(" writptr 0x%08x\n", fec->eth->rfifo_wrptr);
  590. }
  591. }
  592. #endif /* DEBUG */
  593. /********************************************************************/
  594. static int mpc5xxx_fec_send(struct eth_device *dev, volatile void *eth_data,
  595. int data_length)
  596. {
  597. /*
  598. * This routine transmits one frame. This routine only accepts
  599. * 6-byte Ethernet addresses.
  600. */
  601. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  602. volatile FEC_TBD *pTbd;
  603. #if (DEBUG & 0x20)
  604. printf("tbd status: 0x%04x\n", fec->tbdBase[0].status);
  605. tfifo_print(dev->name, fec);
  606. #endif
  607. /*
  608. * Clear Tx BD ring at first
  609. */
  610. mpc5xxx_fec_tbd_scrub(fec);
  611. /*
  612. * Check for valid length of data.
  613. */
  614. if ((data_length > 1500) || (data_length <= 0)) {
  615. return -1;
  616. }
  617. /*
  618. * Check the number of vacant TxBDs.
  619. */
  620. if (fec->cleanTbdNum < 1) {
  621. #if (DEBUG & 0x20)
  622. printf("No available TxBDs ...\n");
  623. #endif
  624. return -1;
  625. }
  626. /*
  627. * Get the first TxBD to send the mac header
  628. */
  629. pTbd = &fec->tbdBase[fec->tbdIndex];
  630. pTbd->dataLength = data_length;
  631. pTbd->dataPointer = (uint32)eth_data;
  632. pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  633. fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
  634. #if (DEBUG & 0x100)
  635. printf("SDMA_TASK_ENABLE, fec->tbdIndex = %d \n", fec->tbdIndex);
  636. #endif
  637. /*
  638. * Kick the MII i/f
  639. */
  640. if (fec->xcv_type != SEVENWIRE) {
  641. uint16 phyStatus;
  642. miiphy_read(dev->name, 0, 0x1, &phyStatus);
  643. }
  644. /*
  645. * Enable SmartDMA transmit task
  646. */
  647. #if (DEBUG & 0x20)
  648. tfifo_print(dev->name, fec);
  649. #endif
  650. SDMA_TASK_ENABLE (FEC_XMIT_TASK_NO);
  651. #if (DEBUG & 0x20)
  652. tfifo_print(dev->name, fec);
  653. #endif
  654. #if (DEBUG & 0x8)
  655. printf( "+" );
  656. #endif
  657. fec->cleanTbdNum -= 1;
  658. #if (DEBUG & 0x129) && (DEBUG & 0x80000000)
  659. printf ("smartDMA ethernet Tx task enabled\n");
  660. #endif
  661. /*
  662. * wait until frame is sent .
  663. */
  664. while (pTbd->status & FEC_TBD_READY) {
  665. udelay(10);
  666. #if (DEBUG & 0x8)
  667. printf ("TDB status = %04x\n", pTbd->status);
  668. #endif
  669. }
  670. return 0;
  671. }
  672. /********************************************************************/
  673. static int mpc5xxx_fec_recv(struct eth_device *dev)
  674. {
  675. /*
  676. * This command pulls one frame from the card
  677. */
  678. mpc5xxx_fec_priv *fec = (mpc5xxx_fec_priv *)dev->priv;
  679. volatile FEC_RBD *pRbd = &fec->rbdBase[fec->rbdIndex];
  680. unsigned long ievent;
  681. int frame_length, len = 0;
  682. NBUF *frame;
  683. uchar buff[FEC_MAX_PKT_SIZE];
  684. #if (DEBUG & 0x1)
  685. printf ("mpc5xxx_fec_recv %d Start...\n", fec->rbdIndex);
  686. #endif
  687. #if (DEBUG & 0x8)
  688. printf( "-" );
  689. #endif
  690. /*
  691. * Check if any critical events have happened
  692. */
  693. ievent = fec->eth->ievent;
  694. fec->eth->ievent = ievent;
  695. if (ievent & 0x20060000) {
  696. /* BABT, Rx/Tx FIFO errors */
  697. mpc5xxx_fec_halt(dev);
  698. mpc5xxx_fec_init(dev, NULL);
  699. return 0;
  700. }
  701. if (ievent & 0x80000000) {
  702. /* Heartbeat error */
  703. fec->eth->x_cntrl |= 0x00000001;
  704. }
  705. if (ievent & 0x10000000) {
  706. /* Graceful stop complete */
  707. if (fec->eth->x_cntrl & 0x00000001) {
  708. mpc5xxx_fec_halt(dev);
  709. fec->eth->x_cntrl &= ~0x00000001;
  710. mpc5xxx_fec_init(dev, NULL);
  711. }
  712. }
  713. if (!(pRbd->status & FEC_RBD_EMPTY)) {
  714. if ((pRbd->status & FEC_RBD_LAST) && !(pRbd->status & FEC_RBD_ERR) &&
  715. ((pRbd->dataLength - 4) > 14)) {
  716. /*
  717. * Get buffer address and size
  718. */
  719. frame = (NBUF *)pRbd->dataPointer;
  720. frame_length = pRbd->dataLength - 4;
  721. #if (DEBUG & 0x20)
  722. {
  723. int i;
  724. printf("recv data hdr:");
  725. for (i = 0; i < 14; i++)
  726. printf("%x ", *(frame->head + i));
  727. printf("\n");
  728. }
  729. #endif
  730. /*
  731. * Fill the buffer and pass it to upper layers
  732. */
  733. memcpy(buff, frame->head, 14);
  734. memcpy(buff + 14, frame->data, frame_length);
  735. NetReceive(buff, frame_length);
  736. len = frame_length;
  737. }
  738. /*
  739. * Reset buffer descriptor as empty
  740. */
  741. mpc5xxx_fec_rbd_clean(fec, pRbd);
  742. }
  743. SDMA_CLEAR_IEVENT (FEC_RECV_TASK_NO);
  744. return len;
  745. }
  746. /********************************************************************/
  747. int mpc5xxx_fec_initialize(bd_t * bis)
  748. {
  749. mpc5xxx_fec_priv *fec;
  750. struct eth_device *dev;
  751. char *tmp, *end;
  752. char env_enetaddr[6];
  753. int i;
  754. fec = (mpc5xxx_fec_priv *)malloc(sizeof(*fec));
  755. dev = (struct eth_device *)malloc(sizeof(*dev));
  756. memset(dev, 0, sizeof *dev);
  757. fec->eth = (ethernet_regs *)MPC5XXX_FEC;
  758. fec->tbdBase = (FEC_TBD *)FEC_BD_BASE;
  759. fec->rbdBase = (FEC_RBD *)(FEC_BD_BASE + FEC_TBD_NUM * sizeof(FEC_TBD));
  760. #if defined(CONFIG_CANMB) || defined(CONFIG_HMI1001) || \
  761. defined(CONFIG_ICECUBE) || defined(CONFIG_INKA4X0) || \
  762. defined(CONFIG_JUPITER) || defined(CONFIG_MCC200) || \
  763. defined(CONFIG_MOTIONPRO)|| defined(CONFIG_O2DNT) || \
  764. defined(CONFIG_PM520) || defined(CONFIG_TOP5200) || \
  765. defined(CONFIG_TQM5200) || defined(CONFIG_UC101) || \
  766. defined(CONFIG_V38B)
  767. # ifndef CONFIG_FEC_10MBIT
  768. fec->xcv_type = MII100;
  769. # else
  770. fec->xcv_type = MII10;
  771. # endif
  772. #elif defined(CONFIG_TOTAL5200)
  773. fec->xcv_type = SEVENWIRE;
  774. #else
  775. #error fec->xcv_type not initialized.
  776. #endif
  777. dev->priv = (void *)fec;
  778. dev->iobase = MPC5XXX_FEC;
  779. dev->init = mpc5xxx_fec_init;
  780. dev->halt = mpc5xxx_fec_halt;
  781. dev->send = mpc5xxx_fec_send;
  782. dev->recv = mpc5xxx_fec_recv;
  783. sprintf(dev->name, "FEC ETHERNET");
  784. eth_register(dev);
  785. #if defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  786. miiphy_register (dev->name,
  787. fec5xxx_miiphy_read, fec5xxx_miiphy_write);
  788. #endif
  789. /*
  790. * Try to set the mac address now. The fec mac address is
  791. * a garbage after reset. When not using fec for booting
  792. * the Linux fec driver will try to work with this garbage.
  793. */
  794. tmp = getenv("ethaddr");
  795. if (tmp) {
  796. for (i=0; i<6; i++) {
  797. env_enetaddr[i] = tmp ? simple_strtoul(tmp, &end, 16) : 0;
  798. if (tmp)
  799. tmp = (*end) ? end+1 : end;
  800. }
  801. mpc5xxx_fec_set_hwaddr(fec, env_enetaddr);
  802. }
  803. mpc5xxx_fec_init_phy(dev, bis);
  804. return 1;
  805. }
  806. /* MII-interface related functions */
  807. /********************************************************************/
  808. int fec5xxx_miiphy_read(char *devname, uint8 phyAddr, uint8 regAddr, uint16 * retVal)
  809. {
  810. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  811. uint32 reg; /* convenient holder for the PHY register */
  812. uint32 phy; /* convenient holder for the PHY */
  813. int timeout = 0xffff;
  814. /*
  815. * reading from any PHY's register is done by properly
  816. * programming the FEC's MII data register.
  817. */
  818. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  819. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  820. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_RD | FEC_MII_DATA_TA | phy | reg);
  821. /*
  822. * wait for the related interrupt
  823. */
  824. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  825. if (timeout == 0) {
  826. #if (DEBUG & 0x2)
  827. printf ("Read MDIO failed...\n");
  828. #endif
  829. return -1;
  830. }
  831. /*
  832. * clear mii interrupt bit
  833. */
  834. eth->ievent = 0x00800000;
  835. /*
  836. * it's now safe to read the PHY's register
  837. */
  838. *retVal = (uint16) eth->mii_data;
  839. return 0;
  840. }
  841. /********************************************************************/
  842. int fec5xxx_miiphy_write(char *devname, uint8 phyAddr, uint8 regAddr, uint16 data)
  843. {
  844. ethernet_regs *eth = (ethernet_regs *)MPC5XXX_FEC;
  845. uint32 reg; /* convenient holder for the PHY register */
  846. uint32 phy; /* convenient holder for the PHY */
  847. int timeout = 0xffff;
  848. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  849. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  850. eth->mii_data = (FEC_MII_DATA_ST | FEC_MII_DATA_OP_WR |
  851. FEC_MII_DATA_TA | phy | reg | data);
  852. /*
  853. * wait for the MII interrupt
  854. */
  855. while ((timeout--) && (!(eth->ievent & 0x00800000))) ;
  856. if (timeout == 0) {
  857. #if (DEBUG & 0x2)
  858. printf ("Write MDIO failed...\n");
  859. #endif
  860. return -1;
  861. }
  862. /*
  863. * clear MII interrupt bit
  864. */
  865. eth->ievent = 0x00800000;
  866. return 0;
  867. }
  868. #if (DEBUG & 0x40)
  869. static uint32 local_crc32(char *string, unsigned int crc_value, int len)
  870. {
  871. int i;
  872. char c;
  873. unsigned int crc, count;
  874. /*
  875. * crc32 algorithm
  876. */
  877. /*
  878. * crc = 0xffffffff; * The initialized value should be 0xffffffff
  879. */
  880. crc = crc_value;
  881. for (i = len; --i >= 0;) {
  882. c = *string++;
  883. for (count = 0; count < 8; count++) {
  884. if ((c & 0x01) ^ (crc & 0x01)) {
  885. crc >>= 1;
  886. crc = crc ^ 0xedb88320;
  887. } else {
  888. crc >>= 1;
  889. }
  890. c >>= 1;
  891. }
  892. }
  893. /*
  894. * In big endian system, do byte swaping for crc value
  895. */
  896. /**/ return crc;
  897. }
  898. #endif /* DEBUG */
  899. #endif /* CONFIG_MPC5xxx_FEC */