fec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <malloc.h>
  25. #include <commproc.h>
  26. #include <net.h>
  27. #include <command.h>
  28. #undef ET_DEBUG
  29. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(FEC_ENET)
  30. #ifdef CFG_DISCOVER_PHY
  31. #include <miiphy.h>
  32. static void mii_discover_phy(void);
  33. #endif
  34. /* Ethernet Transmit and Receive Buffers */
  35. #define DBUF_LENGTH 1520
  36. #define TX_BUF_CNT 2
  37. #define TOUT_LOOP 100
  38. #define PKT_MAXBUF_SIZE 1518
  39. #define PKT_MINBUF_SIZE 64
  40. #define PKT_MAXBLR_SIZE 1520
  41. static char txbuf[DBUF_LENGTH];
  42. static uint rxIdx; /* index of the current RX buffer */
  43. static uint txIdx; /* index of the current TX buffer */
  44. /*
  45. * FEC Ethernet Tx and Rx buffer descriptors allocated at the
  46. * immr->udata_bd address on Dual-Port RAM
  47. * Provide for Double Buffering
  48. */
  49. typedef volatile struct CommonBufferDescriptor {
  50. cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
  51. cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
  52. } RTXBD;
  53. static RTXBD *rtx = NULL;
  54. static int fec_send(struct eth_device* dev, volatile void *packet, int length);
  55. static int fec_recv(struct eth_device* dev);
  56. static int fec_init(struct eth_device* dev, bd_t * bd);
  57. static void fec_halt(struct eth_device* dev);
  58. int fec_initialize(bd_t *bis)
  59. {
  60. struct eth_device* dev;
  61. dev = (struct eth_device*) malloc(sizeof *dev);
  62. memset(dev, 0, sizeof *dev);
  63. sprintf(dev->name, "FEC ETHERNET");
  64. dev->iobase = 0;
  65. dev->priv = 0;
  66. dev->init = fec_init;
  67. dev->halt = fec_halt;
  68. dev->send = fec_send;
  69. dev->recv = fec_recv;
  70. eth_register(dev);
  71. return 1;
  72. }
  73. static int fec_send(struct eth_device* dev, volatile void *packet, int length)
  74. {
  75. int j, rc;
  76. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  77. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  78. /* section 16.9.23.3
  79. * Wait for ready
  80. */
  81. j = 0;
  82. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
  83. udelay(1);
  84. j++;
  85. }
  86. if (j>=TOUT_LOOP) {
  87. printf("TX not ready\n");
  88. }
  89. rtx->txbd[txIdx].cbd_bufaddr = (uint)packet;
  90. rtx->txbd[txIdx].cbd_datlen = length;
  91. rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST;
  92. __asm__ ("eieio");
  93. /* Activate transmit Buffer Descriptor polling */
  94. fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */
  95. j = 0;
  96. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
  97. #if defined(CONFIG_ICU862)
  98. udelay(10);
  99. #else
  100. udelay(1);
  101. #endif
  102. j++;
  103. }
  104. if (j>=TOUT_LOOP) {
  105. printf("TX timeout\n");
  106. }
  107. #ifdef ET_DEBUG
  108. printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
  109. __FILE__,__LINE__,__FUNCTION__,j,rtx->txbd[txIdx].cbd_sc,
  110. (rtx->txbd[txIdx].cbd_sc & 0x003C)>>2);
  111. #endif
  112. /* return only status bits */;
  113. rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
  114. txIdx = (txIdx + 1) % TX_BUF_CNT;
  115. return rc;
  116. }
  117. static int fec_recv(struct eth_device* dev)
  118. {
  119. int length;
  120. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  121. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  122. for (;;) {
  123. /* section 16.9.23.2 */
  124. if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  125. length = -1;
  126. break; /* nothing received - leave for() loop */
  127. }
  128. length = rtx->rxbd[rxIdx].cbd_datlen;
  129. if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
  130. #ifdef ET_DEBUG
  131. printf("%s[%d] err: %x\n",
  132. __FUNCTION__,__LINE__,rtx->rxbd[rxIdx].cbd_sc);
  133. #endif
  134. } else {
  135. /* Pass the packet up to the protocol layers. */
  136. NetReceive(NetRxPackets[rxIdx], length - 4);
  137. }
  138. /* Give the buffer back to the FEC. */
  139. rtx->rxbd[rxIdx].cbd_datlen = 0;
  140. /* wrap around buffer index when necessary */
  141. if ((rxIdx + 1) >= PKTBUFSRX) {
  142. rtx->rxbd[PKTBUFSRX - 1].cbd_sc = (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  143. rxIdx = 0;
  144. } else {
  145. rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  146. rxIdx++;
  147. }
  148. __asm__ ("eieio");
  149. /* Try to fill Buffer Descriptors */
  150. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  151. }
  152. return length;
  153. }
  154. /**************************************************************
  155. *
  156. * FEC Ethernet Initialization Routine
  157. *
  158. *************************************************************/
  159. #define FEC_ECNTRL_PINMUX 0x00000004
  160. #define FEC_ECNTRL_ETHER_EN 0x00000002
  161. #define FEC_ECNTRL_RESET 0x00000001
  162. #define FEC_RCNTRL_BC_REJ 0x00000010
  163. #define FEC_RCNTRL_PROM 0x00000008
  164. #define FEC_RCNTRL_MII_MODE 0x00000004
  165. #define FEC_RCNTRL_DRT 0x00000002
  166. #define FEC_RCNTRL_LOOP 0x00000001
  167. #define FEC_TCNTRL_FDEN 0x00000004
  168. #define FEC_TCNTRL_HBC 0x00000002
  169. #define FEC_TCNTRL_GTS 0x00000001
  170. #define FEC_RESET_DELAY 50
  171. static int fec_init(struct eth_device* dev, bd_t * bd)
  172. {
  173. int i;
  174. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  175. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  176. #if defined(CONFIG_FADS) && defined(CONFIG_MPC860T)
  177. /* configure FADS for fast (FEC) ethernet, half-duplex */
  178. /* The LXT970 needs about 50ms to recover from reset, so
  179. * wait for it by discovering the PHY before leaving eth_init().
  180. */
  181. {
  182. volatile uint *bcsr4 = (volatile uint *) BCSR4;
  183. *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1))
  184. | (BCSR4_FETHCFG0 | BCSR4_FETHFDE | BCSR4_FETHRST);
  185. /* reset the LXT970 PHY */
  186. *bcsr4 &= ~BCSR4_FETHRST;
  187. udelay (10);
  188. *bcsr4 |= BCSR4_FETHRST;
  189. udelay (10);
  190. }
  191. #endif
  192. /* Whack a reset.
  193. * A delay is required between a reset of the FEC block and
  194. * initialization of other FEC registers because the reset takes
  195. * some time to complete. If you don't delay, subsequent writes
  196. * to FEC registers might get killed by the reset routine which is
  197. * still in progress.
  198. */
  199. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  200. for (i = 0;
  201. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  202. ++i) {
  203. udelay (1);
  204. }
  205. if (i == FEC_RESET_DELAY) {
  206. printf ("FEC_RESET_DELAY timeout\n");
  207. return 0;
  208. }
  209. /* We use strictly polling mode only
  210. */
  211. fecp->fec_imask = 0;
  212. /* Clear any pending interrupt
  213. */
  214. fecp->fec_ievent = 0xffc0;
  215. /* No need to set the IVEC register */
  216. /* Set station address
  217. */
  218. #define ea eth_get_dev()->enetaddr
  219. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) |
  220. (ea[2] << 8) | (ea[3] ) ;
  221. fecp->fec_addr_high = (ea[4] << 8) | (ea[5] ) ;
  222. #undef ea
  223. /* Clear multicast address hash table
  224. */
  225. fecp->fec_hash_table_high = 0;
  226. fecp->fec_hash_table_low = 0;
  227. /* Set maximum receive buffer size.
  228. */
  229. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  230. /* Set maximum frame length
  231. */
  232. fecp->fec_r_hash = PKT_MAXBUF_SIZE;
  233. /*
  234. * Setup Buffers and Buffer Desriptors
  235. */
  236. rxIdx = 0;
  237. txIdx = 0;
  238. if (!rtx) {
  239. #ifdef CFG_ALLOC_DPRAM
  240. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + dpram_alloc_align(sizeof(RTXBD),8));
  241. #else
  242. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
  243. #endif
  244. }
  245. /*
  246. * Setup Receiver Buffer Descriptors (13.14.24.18)
  247. * Settings:
  248. * Empty, Wrap
  249. */
  250. for (i = 0; i < PKTBUFSRX; i++) {
  251. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  252. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  253. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  254. }
  255. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  256. /*
  257. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  258. * Settings:
  259. * Last, Tx CRC
  260. */
  261. for (i = 0; i < TX_BUF_CNT; i++) {
  262. rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  263. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  264. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  265. }
  266. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  267. /* Set receive and transmit descriptor base
  268. */
  269. fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
  270. fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
  271. /* Enable MII mode
  272. */
  273. #if 0 /* Full duplex mode */
  274. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
  275. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
  276. #else /* Half duplex mode */
  277. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  278. fecp->fec_x_cntrl = 0;
  279. #endif
  280. /* Enable big endian and don't care about SDMA FC.
  281. */
  282. fecp->fec_fun_code = 0x78000000;
  283. /* Set MII speed to 2.5 MHz or slightly below.
  284. * According to the MPC860T (Rev. D) Fast ethernet controller user
  285. * manual (6.2.14),
  286. * the MII management interface clock must be less than or equal
  287. * to 2.5 MHz.
  288. * This MDC frequency is equal to system clock / (2 * MII_SPEED).
  289. * Then MII_SPEED = system_clock / 2 * 2,5 Mhz.
  290. */
  291. fecp->fec_mii_speed = ((bd->bi_busfreq + 4999999) / 5000000) << 1;
  292. #if !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210)
  293. /* Configure all of port D for MII.
  294. */
  295. immr->im_ioport.iop_pdpar = 0x1fff;
  296. /* Bits moved from Rev. D onward */
  297. if ((get_immr (0) & 0xffff) < 0x0501) {
  298. immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  299. } else {
  300. immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  301. }
  302. #else
  303. /* Configure port A for MII.
  304. */
  305. #if defined(CONFIG_ICU862) && defined(CFG_DISCOVER_PHY)
  306. /* On the ICU862 board the MII-MDC pin is routed to PD8 pin
  307. * of CPU, so for this board we need to configure Utopia and
  308. * enable PD8 to MII-MDC function */
  309. immr->im_ioport.iop_pdpar |= 0x4080;
  310. #endif
  311. /* Has Utopia been configured? */
  312. if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
  313. /*
  314. * YES - Use MUXED mode for UTOPIA bus.
  315. * This frees Port A for use by MII (see 862UM table 41-6).
  316. */
  317. immr->im_ioport.utmode &= ~0x80;
  318. } else {
  319. /*
  320. * NO - set SPLIT mode for UTOPIA bus.
  321. *
  322. * This doesn't really effect UTOPIA (which isn't
  323. * enabled anyway) but just tells the 862
  324. * to use port A for MII (see 862UM table 41-6).
  325. */
  326. immr->im_ioport.utmode |= 0x80;
  327. }
  328. #endif /* !defined(CONFIG_ICU862) */
  329. rxIdx = 0;
  330. txIdx = 0;
  331. /* Now enable the transmit and receive processing
  332. */
  333. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  334. #ifdef CFG_DISCOVER_PHY
  335. /* wait for the PHY to wake up after reset
  336. */
  337. mii_discover_phy();
  338. #endif
  339. /* And last, try to fill Rx Buffer Descriptors */
  340. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  341. return 1;
  342. }
  343. static void fec_halt(struct eth_device* dev)
  344. {
  345. #if 0
  346. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  347. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  348. #endif
  349. }
  350. #if 0
  351. void restart(void)
  352. {
  353. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  354. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  355. }
  356. #endif
  357. #if defined(CFG_DISCOVER_PHY) || (CONFIG_COMMANDS & CFG_CMD_MII)
  358. static int phyaddr = -1; /* didn't find a PHY yet */
  359. static uint phytype;
  360. /* Make MII read/write commands for the FEC.
  361. */
  362. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  363. (REG & 0x1f) << 18))
  364. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  365. (REG & 0x1f) << 18) | \
  366. (VAL & 0xffff))
  367. /* Interrupt events/masks.
  368. */
  369. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  370. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  371. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  372. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  373. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  374. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  375. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  376. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  377. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  378. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  379. /* PHY identification
  380. */
  381. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  382. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  383. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  384. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  385. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  386. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  387. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  388. /* send command to phy using mii, wait for result */
  389. static uint
  390. mii_send(uint mii_cmd)
  391. {
  392. uint mii_reply;
  393. volatile fec_t *ep;
  394. ep = &(((immap_t *)CFG_IMMR)->im_cpm.cp_fec);
  395. ep->fec_mii_data = mii_cmd; /* command to phy */
  396. /* wait for mii complete */
  397. while (!(ep->fec_ievent & FEC_ENET_MII))
  398. ; /* spin until done */
  399. mii_reply = ep->fec_mii_data; /* result from phy */
  400. ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
  401. #if 0
  402. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  403. __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
  404. #endif
  405. return (mii_reply & 0xffff); /* data read from phy */
  406. }
  407. #endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CFG_CMD_MII) */
  408. #if defined(CFG_DISCOVER_PHY)
  409. static void
  410. mii_discover_phy(void)
  411. {
  412. #define MAX_PHY_PASSES 11
  413. uint phyno;
  414. int pass;
  415. phyaddr = -1; /* didn't find a PHY yet */
  416. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  417. if (pass > 1) {
  418. /* PHY may need more time to recover from reset.
  419. * The LXT970 needs 50ms typical, no maximum is
  420. * specified, so wait 10ms before try again.
  421. * With 11 passes this gives it 100ms to wake up.
  422. */
  423. udelay(10000); /* wait 10ms */
  424. }
  425. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  426. phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR1));
  427. #ifdef ET_DEBUG
  428. printf("PHY type 0x%x pass %d type ", phytype, pass);
  429. #endif
  430. if (phytype != 0xffff) {
  431. phyaddr = phyno;
  432. phytype <<= 16;
  433. phytype |= mii_send(mk_mii_read(phyno,
  434. PHY_PHYIDR2));
  435. #ifdef ET_DEBUG
  436. printf("PHY @ 0x%x pass %d type ",phyno,pass);
  437. switch (phytype & 0xfffffff0) {
  438. case PHY_ID_LXT970:
  439. printf("LXT970\n");
  440. break;
  441. case PHY_ID_LXT971:
  442. printf("LXT971\n");
  443. break;
  444. case PHY_ID_82555:
  445. printf("82555\n");
  446. break;
  447. case PHY_ID_QS6612:
  448. printf("QS6612\n");
  449. break;
  450. case PHY_ID_AMD79C784:
  451. printf("AMD79C784\n");
  452. break;
  453. case PHY_ID_LSI80225B:
  454. printf("LSI L80225/B\n");
  455. break;
  456. default:
  457. printf("0x%08x\n", phytype);
  458. break;
  459. }
  460. #endif
  461. }
  462. }
  463. }
  464. if (phyaddr < 0) {
  465. printf("No PHY device found.\n");
  466. }
  467. }
  468. #endif /* CFG_DISCOVER_PHY */
  469. #if (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)
  470. static int mii_init_done = 0;
  471. /****************************************************************************
  472. * mii_init -- Initialize the MII for MII command without ethernet
  473. * This function is a subset of eth_init
  474. ****************************************************************************
  475. */
  476. void mii_init (void)
  477. {
  478. DECLARE_GLOBAL_DATA_PTR;
  479. bd_t *bd = gd->bd;
  480. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  481. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  482. int i;
  483. if (mii_init_done != 0) {
  484. return;
  485. }
  486. /* Whack a reset.
  487. * A delay is required between a reset of the FEC block and
  488. * initialization of other FEC registers because the reset takes
  489. * some time to complete. If you don't delay, subsequent writes
  490. * to FEC registers might get killed by the reset routine which is
  491. * still in progress.
  492. */
  493. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  494. for (i = 0;
  495. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  496. ++i) {
  497. udelay (1);
  498. }
  499. if (i == FEC_RESET_DELAY) {
  500. printf ("FEC_RESET_DELAY timeout\n");
  501. return;
  502. }
  503. /* We use strictly polling mode only
  504. */
  505. fecp->fec_imask = 0;
  506. /* Clear any pending interrupt
  507. */
  508. fecp->fec_ievent = 0xffc0;
  509. /* Set MII speed to 2.5 MHz or slightly below.
  510. * According to the MPC860T (Rev. D) Fast ethernet controller user
  511. * manual (6.2.14),
  512. * the MII management interface clock must be less than or equal
  513. * to 2.5 MHz.
  514. * This MDC frequency is equal to system clock / (2 * MII_SPEED).
  515. * Then MII_SPEED = system_clock / 2 * 2,5 Mhz.
  516. */
  517. fecp->fec_mii_speed = ((bd->bi_busfreq + 4999999) / 5000000) << 1;
  518. #if !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210)
  519. /* Configure all of port D for MII.
  520. */
  521. immr->im_ioport.iop_pdpar = 0x1fff;
  522. /* Bits moved from Rev. D onward */
  523. if ((get_immr (0) & 0xffff) < 0x0501) {
  524. immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  525. } else {
  526. immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  527. }
  528. #else
  529. /* Configure port A for MII.
  530. */
  531. #if defined(CONFIG_ICU862)
  532. /* On the ICU862 board the MII-MDC pin is routed to PD8 pin
  533. * of CPU, so for this board we need to configure Utopia and
  534. * enable PD8 to MII-MDC function */
  535. immr->im_ioport.iop_pdpar |= 0x4080;
  536. #endif
  537. /* Has Utopia been configured? */
  538. if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
  539. /*
  540. * YES - Use MUXED mode for UTOPIA bus.
  541. * This frees Port A for use by MII (see 862UM table 41-6).
  542. */
  543. immr->im_ioport.utmode &= ~0x80;
  544. } else {
  545. /*
  546. * NO - set SPLIT mode for UTOPIA bus.
  547. *
  548. * This doesn't really effect UTOPIA (which isn't
  549. * enabled anyway) but just tells the 862
  550. * to use port A for MII (see 862UM table 41-6).
  551. */
  552. immr->im_ioport.utmode |= 0x80;
  553. }
  554. #endif /* !defined(CONFIG_ICU862) */
  555. /* Now enable the transmit and receive processing
  556. */
  557. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  558. mii_init_done = 1;
  559. }
  560. /*****************************************************************************
  561. * Read and write a MII PHY register, routines used by MII Utilities
  562. *
  563. * FIXME: These routines are expected to return 0 on success, but mii_send
  564. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  565. * no PHY connected...
  566. * For now always return 0.
  567. * FIXME: These routines only work after calling eth_init() at least once!
  568. * Otherwise they hang in mii_send() !!! Sorry!
  569. *****************************************************************************/
  570. int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
  571. {
  572. short rdreg; /* register working value */
  573. #ifdef MII_DEBUG
  574. printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  575. #endif
  576. rdreg = mii_send(mk_mii_read(addr, reg));
  577. *value = rdreg;
  578. #ifdef MII_DEBUG
  579. printf ("0x%04x\n", *value);
  580. #endif
  581. return 0;
  582. }
  583. int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
  584. {
  585. short rdreg; /* register working value */
  586. #ifdef MII_DEBUG
  587. printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  588. #endif
  589. rdreg = mii_send(mk_mii_write(addr, reg, value));
  590. #ifdef MII_DEBUG
  591. printf ("0x%04x\n", value);
  592. #endif
  593. return 0;
  594. }
  595. #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)*/
  596. #endif /* CFG_CMD_NET, FEC_ENET */