fec.c 18 KB

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