mpc512x_fec.c 18 KB

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