mpc5xxx_fec.c 25 KB

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