mpc512x_fec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /*
  2. * (C) Copyright 2003-2009
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Derived from the MPC8xx FEC driver.
  6. * Adapted for MPC512x by Grzegorz Bernacki <gjb@semihalf.com>
  7. */
  8. #include <common.h>
  9. #include <malloc.h>
  10. #include <net.h>
  11. #include <netdev.h>
  12. #include <miiphy.h>
  13. #include <asm/io.h>
  14. #include "mpc512x_fec.h"
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #define DEBUG 0
  17. #if defined(CONFIG_CMD_NET) && defined(CONFIG_NET_MULTI) && \
  18. defined(CONFIG_MPC512x_FEC)
  19. #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
  20. #error "CONFIG_MII has to be defined!"
  21. #endif
  22. #if (DEBUG & 0x40)
  23. static u32 local_crc32(char *string, unsigned int crc_value, int len);
  24. #endif
  25. int fec512x_miiphy_read(char *devname, u8 phyAddr, u8 regAddr, u16 * retVal);
  26. int fec512x_miiphy_write(char *devname, u8 phyAddr, u8 regAddr, u16 data);
  27. int mpc512x_fec_init_phy(struct eth_device *dev, bd_t * bis);
  28. static uchar rx_buff[FEC_BUFFER_SIZE];
  29. static int rx_buff_idx = 0;
  30. /********************************************************************/
  31. #if (DEBUG & 0x2)
  32. static void mpc512x_fec_phydump (char *devname)
  33. {
  34. u16 phyStatus, i;
  35. u8 phyAddr = CONFIG_PHY_ADDR;
  36. u8 reg_mask[] = {
  37. /* regs to print: 0...8, 21,27,31 */
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
  39. 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
  40. };
  41. for (i = 0; i < 32; i++) {
  42. if (reg_mask[i]) {
  43. miiphy_read (devname, phyAddr, i, &phyStatus);
  44. printf ("Mii reg %d: 0x%04x\n", i, phyStatus);
  45. }
  46. }
  47. }
  48. #endif
  49. /********************************************************************/
  50. static int mpc512x_fec_bd_init (mpc512x_fec_priv *fec)
  51. {
  52. int ix;
  53. /*
  54. * Receive BDs init
  55. */
  56. for (ix = 0; ix < FEC_RBD_NUM; ix++) {
  57. fec->bdBase->rbd[ix].dataPointer =
  58. (u32)&fec->bdBase->recv_frames[ix];
  59. fec->bdBase->rbd[ix].status = FEC_RBD_EMPTY;
  60. fec->bdBase->rbd[ix].dataLength = 0;
  61. }
  62. /*
  63. * have the last RBD to close the ring
  64. */
  65. fec->bdBase->rbd[ix - 1].status |= FEC_RBD_WRAP;
  66. fec->rbdIndex = 0;
  67. /*
  68. * Trasmit BDs init
  69. */
  70. for (ix = 0; ix < FEC_TBD_NUM; ix++) {
  71. fec->bdBase->tbd[ix].status = 0;
  72. }
  73. /*
  74. * Have the last TBD to close the ring
  75. */
  76. fec->bdBase->tbd[ix - 1].status |= FEC_TBD_WRAP;
  77. /*
  78. * Initialize some indices
  79. */
  80. fec->tbdIndex = 0;
  81. fec->usedTbdIndex = 0;
  82. fec->cleanTbdNum = FEC_TBD_NUM;
  83. return 0;
  84. }
  85. /********************************************************************/
  86. static void mpc512x_fec_rbd_clean (mpc512x_fec_priv *fec, volatile FEC_RBD * pRbd)
  87. {
  88. /*
  89. * Reset buffer descriptor as empty
  90. */
  91. if ((fec->rbdIndex) == (FEC_RBD_NUM - 1))
  92. pRbd->status = (FEC_RBD_WRAP | FEC_RBD_EMPTY);
  93. else
  94. pRbd->status = FEC_RBD_EMPTY;
  95. pRbd->dataLength = 0;
  96. /*
  97. * Increment BD count
  98. */
  99. fec->rbdIndex = (fec->rbdIndex + 1) % FEC_RBD_NUM;
  100. /*
  101. * Now, we have an empty RxBD, notify FEC
  102. * Set Descriptor polling active
  103. */
  104. out_be32(&fec->eth->r_des_active, 0x01000000);
  105. }
  106. /********************************************************************/
  107. static void mpc512x_fec_tbd_scrub (mpc512x_fec_priv *fec)
  108. {
  109. volatile FEC_TBD *pUsedTbd;
  110. #if (DEBUG & 0x1)
  111. printf ("tbd_scrub: fec->cleanTbdNum = %d, fec->usedTbdIndex = %d\n",
  112. fec->cleanTbdNum, fec->usedTbdIndex);
  113. #endif
  114. /*
  115. * process all the consumed TBDs
  116. */
  117. while (fec->cleanTbdNum < FEC_TBD_NUM) {
  118. pUsedTbd = &fec->bdBase->tbd[fec->usedTbdIndex];
  119. if (pUsedTbd->status & FEC_TBD_READY) {
  120. #if (DEBUG & 0x20)
  121. printf ("Cannot clean TBD %d, in use\n", fec->usedTbdIndex);
  122. #endif
  123. return;
  124. }
  125. /*
  126. * clean this buffer descriptor
  127. */
  128. if (fec->usedTbdIndex == (FEC_TBD_NUM - 1))
  129. pUsedTbd->status = FEC_TBD_WRAP;
  130. else
  131. pUsedTbd->status = 0;
  132. /*
  133. * update some indeces for a correct handling of the TBD ring
  134. */
  135. fec->cleanTbdNum++;
  136. fec->usedTbdIndex = (fec->usedTbdIndex + 1) % FEC_TBD_NUM;
  137. }
  138. }
  139. /********************************************************************/
  140. static void mpc512x_fec_set_hwaddr (mpc512x_fec_priv *fec, char *mac)
  141. {
  142. u8 currByte; /* byte for which to compute the CRC */
  143. int byte; /* loop - counter */
  144. int bit; /* loop - counter */
  145. u32 crc = 0xffffffff; /* initial value */
  146. /*
  147. * The algorithm used is the following:
  148. * we loop on each of the six bytes of the provided address,
  149. * and we compute the CRC by left-shifting the previous
  150. * value by one position, so that each bit in the current
  151. * byte of the address may contribute the calculation. If
  152. * the latter and the MSB in the CRC are different, then
  153. * the CRC value so computed is also ex-ored with the
  154. * "polynomium generator". The current byte of the address
  155. * is also shifted right by one bit at each iteration.
  156. * This is because the CRC generatore in hardware is implemented
  157. * as a shift-register with as many ex-ores as the radixes
  158. * in the polynomium. This suggests that we represent the
  159. * polynomiumm itself as a 32-bit constant.
  160. */
  161. for (byte = 0; byte < 6; byte++) {
  162. currByte = mac[byte];
  163. for (bit = 0; bit < 8; bit++) {
  164. if ((currByte & 0x01) ^ (crc & 0x01)) {
  165. crc >>= 1;
  166. crc = crc ^ 0xedb88320;
  167. } else {
  168. crc >>= 1;
  169. }
  170. currByte >>= 1;
  171. }
  172. }
  173. crc = crc >> 26;
  174. /*
  175. * Set individual hash table register
  176. */
  177. if (crc >= 32) {
  178. out_be32(&fec->eth->iaddr1, (1 << (crc - 32)));
  179. out_be32(&fec->eth->iaddr2, 0);
  180. } else {
  181. out_be32(&fec->eth->iaddr1, 0);
  182. out_be32(&fec->eth->iaddr2, (1 << crc));
  183. }
  184. /*
  185. * Set physical address
  186. */
  187. out_be32(&fec->eth->paddr1, (mac[0] << 24) + (mac[1] << 16) +
  188. (mac[2] << 8) + mac[3]);
  189. out_be32(&fec->eth->paddr2, (mac[4] << 24) + (mac[5] << 16) +
  190. 0x8808);
  191. }
  192. /********************************************************************/
  193. static int mpc512x_fec_init (struct eth_device *dev, bd_t * bis)
  194. {
  195. mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
  196. #if (DEBUG & 0x1)
  197. printf ("mpc512x_fec_init... Begin\n");
  198. #endif
  199. /* Set interrupt mask register */
  200. out_be32(&fec->eth->imask, 0x00000000);
  201. /* Clear FEC-Lite interrupt event register(IEVENT) */
  202. out_be32(&fec->eth->ievent, 0xffffffff);
  203. /* Set transmit fifo watermark register(X_WMRK), default = 64 */
  204. out_be32(&fec->eth->x_wmrk, 0x0);
  205. /* Set Opcode/Pause Duration Register */
  206. out_be32(&fec->eth->op_pause, 0x00010020);
  207. /* Frame length=1522; MII mode */
  208. out_be32(&fec->eth->r_cntrl, (FEC_MAX_FRAME_LEN << 16) | 0x24);
  209. /* Half-duplex, heartbeat disabled */
  210. out_be32(&fec->eth->x_cntrl, 0x00000000);
  211. /* Enable MIB counters */
  212. out_be32(&fec->eth->mib_control, 0x0);
  213. /* Setup recv fifo start and buff size */
  214. out_be32(&fec->eth->r_fstart, 0x500);
  215. out_be32(&fec->eth->r_buff_size, FEC_BUFFER_SIZE);
  216. /* Setup BD base addresses */
  217. out_be32(&fec->eth->r_des_start, (u32)fec->bdBase->rbd);
  218. out_be32(&fec->eth->x_des_start, (u32)fec->bdBase->tbd);
  219. /* DMA Control */
  220. out_be32(&fec->eth->dma_control, 0xc0000000);
  221. /* Enable FEC */
  222. setbits_be32(&fec->eth->ecntrl, 0x00000006);
  223. /* Initilize addresses and status words of BDs */
  224. mpc512x_fec_bd_init (fec);
  225. /* Descriptor polling active */
  226. out_be32(&fec->eth->r_des_active, 0x01000000);
  227. #if (DEBUG & 0x1)
  228. printf("mpc512x_fec_init... Done \n");
  229. #endif
  230. return 1;
  231. }
  232. /********************************************************************/
  233. int mpc512x_fec_init_phy (struct eth_device *dev, bd_t * bis)
  234. {
  235. mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
  236. const u8 phyAddr = CONFIG_PHY_ADDR; /* Only one PHY */
  237. int timeout = 1;
  238. u16 phyStatus;
  239. #if (DEBUG & 0x1)
  240. printf ("mpc512x_fec_init_phy... Begin\n");
  241. #endif
  242. /*
  243. * Clear FEC-Lite interrupt event register(IEVENT)
  244. */
  245. out_be32(&fec->eth->ievent, 0xffffffff);
  246. /*
  247. * Set interrupt mask register
  248. */
  249. out_be32(&fec->eth->imask, 0x00000000);
  250. if (fec->xcv_type != SEVENWIRE) {
  251. /*
  252. * Set MII_SPEED = (1/(mii_speed * 2)) * System Clock
  253. * and do not drop the Preamble.
  254. */
  255. out_be32(&fec->eth->mii_speed,
  256. (((gd->ips_clk / 1000000) / 5) + 1) << 1);
  257. /*
  258. * Reset PHY, then delay 300ns
  259. */
  260. miiphy_write (dev->name, phyAddr, 0x0, 0x8000);
  261. udelay (1000);
  262. if (fec->xcv_type == MII10) {
  263. /*
  264. * Force 10Base-T, FDX operation
  265. */
  266. #if (DEBUG & 0x2)
  267. printf ("Forcing 10 Mbps ethernet link... ");
  268. #endif
  269. miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
  270. miiphy_write (dev->name, phyAddr, 0x0, 0x0180);
  271. timeout = 20;
  272. do { /* wait for link status to go down */
  273. udelay (10000);
  274. if ((timeout--) == 0) {
  275. #if (DEBUG & 0x2)
  276. printf ("hmmm, should not have waited...");
  277. #endif
  278. break;
  279. }
  280. miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
  281. #if (DEBUG & 0x2)
  282. printf ("=");
  283. #endif
  284. } while ((phyStatus & 0x0004)); /* !link up */
  285. timeout = 1000;
  286. do { /* wait for link status to come back up */
  287. udelay (10000);
  288. if ((timeout--) == 0) {
  289. printf ("failed. Link is down.\n");
  290. break;
  291. }
  292. miiphy_read (dev->name, phyAddr, 0x1, &phyStatus);
  293. #if (DEBUG & 0x2)
  294. printf ("+");
  295. #endif
  296. } while (!(phyStatus & 0x0004)); /* !link up */
  297. #if (DEBUG & 0x2)
  298. printf ("done.\n");
  299. #endif
  300. } else { /* MII100 */
  301. /*
  302. * Set the auto-negotiation advertisement register bits
  303. */
  304. miiphy_write (dev->name, phyAddr, 0x4, 0x01e1);
  305. /*
  306. * Set MDIO bit 0.12 = 1(&& bit 0.9=1?) to enable auto-negotiation
  307. */
  308. miiphy_write (dev->name, phyAddr, 0x0, 0x1200);
  309. /*
  310. * Wait for AN completion
  311. */
  312. timeout = 2500;
  313. do {
  314. udelay (1000);
  315. if ((timeout--) == 0) {
  316. #if (DEBUG & 0x2)
  317. printf ("PHY auto neg 0 failed...\n");
  318. #endif
  319. return -1;
  320. }
  321. if (miiphy_read (dev->name, phyAddr, 0x1, &phyStatus) != 0) {
  322. #if (DEBUG & 0x2)
  323. printf ("PHY auto neg 1 failed 0x%04x...\n", phyStatus);
  324. #endif
  325. return -1;
  326. }
  327. } while (!(phyStatus & 0x0004));
  328. #if (DEBUG & 0x2)
  329. printf ("PHY auto neg complete! \n");
  330. #endif
  331. }
  332. }
  333. #if (DEBUG & 0x2)
  334. if (fec->xcv_type != SEVENWIRE)
  335. mpc512x_fec_phydump (dev->name);
  336. #endif
  337. #if (DEBUG & 0x1)
  338. printf ("mpc512x_fec_init_phy... Done \n");
  339. #endif
  340. return 1;
  341. }
  342. /********************************************************************/
  343. static void mpc512x_fec_halt (struct eth_device *dev)
  344. {
  345. mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
  346. int counter = 0xffff;
  347. #if (DEBUG & 0x2)
  348. if (fec->xcv_type != SEVENWIRE)
  349. mpc512x_fec_phydump (dev->name);
  350. #endif
  351. /*
  352. * mask FEC chip interrupts
  353. */
  354. out_be32(&fec->eth->imask, 0);
  355. /*
  356. * issue graceful stop command to the FEC transmitter if necessary
  357. */
  358. setbits_be32(&fec->eth->x_cntrl, 0x00000001);
  359. /*
  360. * wait for graceful stop to register
  361. */
  362. while ((counter--) && (!(in_be32(&fec->eth->ievent) & 0x10000000)))
  363. ;
  364. /*
  365. * Disable the Ethernet Controller
  366. */
  367. clrbits_be32(&fec->eth->ecntrl, 0x00000002);
  368. /*
  369. * Issue a reset command to the FEC chip
  370. */
  371. setbits_be32(&fec->eth->ecntrl, 0x1);
  372. /*
  373. * wait at least 16 clock cycles
  374. */
  375. udelay (10);
  376. #if (DEBUG & 0x3)
  377. printf ("Ethernet task stopped\n");
  378. #endif
  379. }
  380. /********************************************************************/
  381. static int mpc512x_fec_send (struct eth_device *dev, volatile void *eth_data,
  382. int data_length)
  383. {
  384. /*
  385. * This routine transmits one frame. This routine only accepts
  386. * 6-byte Ethernet addresses.
  387. */
  388. mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
  389. volatile FEC_TBD *pTbd;
  390. #if (DEBUG & 0x20)
  391. printf("tbd status: 0x%04x\n", fec->tbdBase[fec->tbdIndex].status);
  392. #endif
  393. /*
  394. * Clear Tx BD ring at first
  395. */
  396. mpc512x_fec_tbd_scrub (fec);
  397. /*
  398. * Check for valid length of data.
  399. */
  400. if ((data_length > 1500) || (data_length <= 0)) {
  401. return -1;
  402. }
  403. /*
  404. * Check the number of vacant TxBDs.
  405. */
  406. if (fec->cleanTbdNum < 1) {
  407. #if (DEBUG & 0x20)
  408. printf ("No available TxBDs ...\n");
  409. #endif
  410. return -1;
  411. }
  412. /*
  413. * Get the first TxBD to send the mac header
  414. */
  415. pTbd = &fec->bdBase->tbd[fec->tbdIndex];
  416. pTbd->dataLength = data_length;
  417. pTbd->dataPointer = (u32)eth_data;
  418. pTbd->status |= FEC_TBD_LAST | FEC_TBD_TC | FEC_TBD_READY;
  419. fec->tbdIndex = (fec->tbdIndex + 1) % FEC_TBD_NUM;
  420. /* Activate transmit Buffer Descriptor polling */
  421. out_be32(&fec->eth->x_des_active, 0x01000000);
  422. #if (DEBUG & 0x8)
  423. printf ( "+" );
  424. #endif
  425. fec->cleanTbdNum -= 1;
  426. /*
  427. * wait until frame is sent .
  428. */
  429. while (pTbd->status & FEC_TBD_READY) {
  430. udelay (10);
  431. #if (DEBUG & 0x8)
  432. printf ("TDB status = %04x\n", pTbd->status);
  433. #endif
  434. }
  435. return 0;
  436. }
  437. /********************************************************************/
  438. static int mpc512x_fec_recv (struct eth_device *dev)
  439. {
  440. /*
  441. * This command pulls one frame from the card
  442. */
  443. mpc512x_fec_priv *fec = (mpc512x_fec_priv *)dev->priv;
  444. volatile FEC_RBD *pRbd = &fec->bdBase->rbd[fec->rbdIndex];
  445. unsigned long ievent;
  446. int frame_length = 0;
  447. #if (DEBUG & 0x1)
  448. printf ("mpc512x_fec_recv %d Start...\n", fec->rbdIndex);
  449. #endif
  450. #if (DEBUG & 0x8)
  451. printf( "-" );
  452. #endif
  453. /*
  454. * Check if any critical events have happened
  455. */
  456. ievent = in_be32(&fec->eth->ievent);
  457. out_be32(&fec->eth->ievent, ievent);
  458. if (ievent & 0x20060000) {
  459. /* BABT, Rx/Tx FIFO errors */
  460. mpc512x_fec_halt (dev);
  461. mpc512x_fec_init (dev, NULL);
  462. return 0;
  463. }
  464. if (ievent & 0x80000000) {
  465. /* Heartbeat error */
  466. setbits_be32(&fec->eth->x_cntrl, 0x00000001);
  467. }
  468. if (ievent & 0x10000000) {
  469. /* Graceful stop complete */
  470. if (in_be32(&fec->eth->x_cntrl) & 0x00000001) {
  471. mpc512x_fec_halt (dev);
  472. clrbits_be32(&fec->eth->x_cntrl, 0x00000001);;
  473. mpc512x_fec_init (dev, NULL);
  474. }
  475. }
  476. if (!(pRbd->status & FEC_RBD_EMPTY)) {
  477. if (!(pRbd->status & FEC_RBD_ERR) &&
  478. ((pRbd->dataLength - 4) > 14)) {
  479. /*
  480. * Get buffer size
  481. */
  482. if (pRbd->status & FEC_RBD_LAST)
  483. frame_length = pRbd->dataLength - 4;
  484. else
  485. frame_length = pRbd->dataLength;
  486. #if (DEBUG & 0x20)
  487. {
  488. int i;
  489. printf ("recv data length 0x%08x data hdr: ",
  490. pRbd->dataLength);
  491. for (i = 0; i < 14; i++)
  492. printf ("%x ", *((u8*)pRbd->dataPointer + i));
  493. printf("\n");
  494. }
  495. #endif
  496. /*
  497. * Fill the buffer and pass it to upper layers
  498. */
  499. memcpy (&rx_buff[rx_buff_idx], (void*)pRbd->dataPointer,
  500. frame_length - rx_buff_idx);
  501. rx_buff_idx = frame_length;
  502. if (pRbd->status & FEC_RBD_LAST) {
  503. NetReceive ((uchar*)rx_buff, frame_length);
  504. rx_buff_idx = 0;
  505. }
  506. }
  507. /*
  508. * Reset buffer descriptor as empty
  509. */
  510. mpc512x_fec_rbd_clean (fec, pRbd);
  511. }
  512. /* Try to fill Buffer Descriptors */
  513. out_be32(&fec->eth->r_des_active, 0x01000000);
  514. return frame_length;
  515. }
  516. /********************************************************************/
  517. int mpc512x_fec_initialize (bd_t * bis)
  518. {
  519. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  520. mpc512x_fec_priv *fec;
  521. struct eth_device *dev;
  522. int i;
  523. char *tmp, *end, env_enetaddr[6];
  524. void * bd;
  525. fec = (mpc512x_fec_priv *) malloc (sizeof(*fec));
  526. dev = (struct eth_device *) malloc (sizeof(*dev));
  527. memset (dev, 0, sizeof *dev);
  528. fec->eth = &im->fec;
  529. # ifndef CONFIG_FEC_10MBIT
  530. fec->xcv_type = MII100;
  531. # else
  532. fec->xcv_type = MII10;
  533. # endif
  534. dev->priv = (void *)fec;
  535. dev->iobase = (int)&im->fec;
  536. dev->init = mpc512x_fec_init;
  537. dev->halt = mpc512x_fec_halt;
  538. dev->send = mpc512x_fec_send;
  539. dev->recv = mpc512x_fec_recv;
  540. sprintf (dev->name, "FEC ETHERNET");
  541. eth_register (dev);
  542. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  543. miiphy_register (dev->name,
  544. fec512x_miiphy_read, fec512x_miiphy_write);
  545. #endif
  546. /* Clean up space FEC's MIB and FIFO RAM ...*/
  547. memset ((void *)&im->fec.mib, 0x00, sizeof(im->fec.mib));
  548. memset ((void *)&im->fec.fifo, 0x00, sizeof(im->fec.fifo));
  549. /*
  550. * Malloc space for BDs (must be quad word-aligned)
  551. * this pointer is lost, so cannot be freed
  552. */
  553. bd = malloc (sizeof(mpc512x_buff_descs) + 0x1f);
  554. fec->bdBase = (mpc512x_buff_descs*)((u32)bd & 0xfffffff0);
  555. memset ((void *) bd, 0x00, sizeof(mpc512x_buff_descs) + 0x1f);
  556. /*
  557. * Set interrupt mask register
  558. */
  559. out_be32(&fec->eth->imask, 0x00000000);
  560. /*
  561. * Clear FEC-Lite interrupt event register(IEVENT)
  562. */
  563. out_be32(&fec->eth->ievent, 0xffffffff);
  564. /*
  565. * Try to set the mac address now. The fec mac address is
  566. * a garbage after reset. When not using fec for booting
  567. * the Linux fec driver will try to work with this garbage.
  568. */
  569. tmp = getenv ("ethaddr");
  570. if (tmp) {
  571. for (i=0; i<6; i++) {
  572. env_enetaddr[i] = tmp ? simple_strtoul (tmp, &end, 16) : 0;
  573. if (tmp)
  574. tmp = (*end) ? end+1 : end;
  575. }
  576. mpc512x_fec_set_hwaddr (fec, env_enetaddr);
  577. out_be32(&fec->eth->gaddr1, 0x00000000);
  578. out_be32(&fec->eth->gaddr2, 0x00000000);
  579. }
  580. mpc512x_fec_init_phy (dev, bis);
  581. return 1;
  582. }
  583. /* MII-interface related functions */
  584. /********************************************************************/
  585. int fec512x_miiphy_read (char *devname, u8 phyAddr, u8 regAddr, u16 * retVal)
  586. {
  587. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  588. volatile fec512x_t *eth = &im->fec;
  589. u32 reg; /* convenient holder for the PHY register */
  590. u32 phy; /* convenient holder for the PHY */
  591. int timeout = 0xffff;
  592. /*
  593. * reading from any PHY's register is done by properly
  594. * programming the FEC's MII data register.
  595. */
  596. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  597. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  598. out_be32(&eth->mii_data, FEC_MII_DATA_ST |
  599. FEC_MII_DATA_OP_RD |
  600. FEC_MII_DATA_TA |
  601. phy | reg);
  602. /*
  603. * wait for the related interrupt
  604. */
  605. while ((timeout--) && (!(in_be32(&eth->ievent) & 0x00800000)))
  606. ;
  607. if (timeout == 0) {
  608. #if (DEBUG & 0x2)
  609. printf ("Read MDIO failed...\n");
  610. #endif
  611. return -1;
  612. }
  613. /*
  614. * clear mii interrupt bit
  615. */
  616. out_be32(&eth->ievent, 0x00800000);
  617. /*
  618. * it's now safe to read the PHY's register
  619. */
  620. *retVal = (u16) in_be32(&eth->mii_data);
  621. return 0;
  622. }
  623. /********************************************************************/
  624. int fec512x_miiphy_write (char *devname, u8 phyAddr, u8 regAddr, u16 data)
  625. {
  626. volatile immap_t *im = (immap_t *) CONFIG_SYS_IMMR;
  627. volatile fec512x_t *eth = &im->fec;
  628. u32 reg; /* convenient holder for the PHY register */
  629. u32 phy; /* convenient holder for the PHY */
  630. int timeout = 0xffff;
  631. reg = regAddr << FEC_MII_DATA_RA_SHIFT;
  632. phy = phyAddr << FEC_MII_DATA_PA_SHIFT;
  633. out_be32(&eth->mii_data, FEC_MII_DATA_ST |
  634. FEC_MII_DATA_OP_WR |
  635. FEC_MII_DATA_TA |
  636. phy | reg | data);
  637. /*
  638. * wait for the MII interrupt
  639. */
  640. while ((timeout--) && (!(in_be32(&eth->ievent) & 0x00800000)))
  641. ;
  642. if (timeout == 0) {
  643. #if (DEBUG & 0x2)
  644. printf ("Write MDIO failed...\n");
  645. #endif
  646. return -1;
  647. }
  648. /*
  649. * clear MII interrupt bit
  650. */
  651. out_be32(&eth->ievent, 0x00800000);
  652. return 0;
  653. }
  654. #if (DEBUG & 0x40)
  655. static u32 local_crc32 (char *string, unsigned int crc_value, int len)
  656. {
  657. int i;
  658. char c;
  659. unsigned int crc, count;
  660. /*
  661. * crc32 algorithm
  662. */
  663. /*
  664. * crc = 0xffffffff; * The initialized value should be 0xffffffff
  665. */
  666. crc = crc_value;
  667. for (i = len; --i >= 0;) {
  668. c = *string++;
  669. for (count = 0; count < 8; count++) {
  670. if ((c & 0x01) ^ (crc & 0x01)) {
  671. crc >>= 1;
  672. crc = crc ^ 0xedb88320;
  673. } else {
  674. crc >>= 1;
  675. }
  676. c >>= 1;
  677. }
  678. }
  679. /*
  680. * In big endian system, do byte swaping for crc value
  681. */
  682. /**/ return crc;
  683. }
  684. #endif /* DEBUG */
  685. #endif /* CONFIG_MPC512x_FEC */