fec.c 19 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) && \
  177. ( defined(CONFIG_MPC860T) || defined(CONFIG_MPC866_et_al) )
  178. /* configure FADS for fast (FEC) ethernet, half-duplex */
  179. /* The LXT970 needs about 50ms to recover from reset, so
  180. * wait for it by discovering the PHY before leaving eth_init().
  181. */
  182. {
  183. volatile uint *bcsr4 = (volatile uint *) BCSR4;
  184. *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1))
  185. | (BCSR4_FETHCFG0 | BCSR4_FETHFDE | BCSR4_FETHRST);
  186. /* reset the LXT970 PHY */
  187. *bcsr4 &= ~BCSR4_FETHRST;
  188. udelay (10);
  189. *bcsr4 |= BCSR4_FETHRST;
  190. udelay (10);
  191. }
  192. #endif
  193. /* Whack a reset.
  194. * A delay is required between a reset of the FEC block and
  195. * initialization of other FEC registers because the reset takes
  196. * some time to complete. If you don't delay, subsequent writes
  197. * to FEC registers might get killed by the reset routine which is
  198. * still in progress.
  199. */
  200. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  201. for (i = 0;
  202. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  203. ++i) {
  204. udelay (1);
  205. }
  206. if (i == FEC_RESET_DELAY) {
  207. printf ("FEC_RESET_DELAY timeout\n");
  208. return 0;
  209. }
  210. /* We use strictly polling mode only
  211. */
  212. fecp->fec_imask = 0;
  213. /* Clear any pending interrupt
  214. */
  215. fecp->fec_ievent = 0xffc0;
  216. /* No need to set the IVEC register */
  217. /* Set station address
  218. */
  219. #define ea eth_get_dev()->enetaddr
  220. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) |
  221. (ea[2] << 8) | (ea[3] ) ;
  222. fecp->fec_addr_high = (ea[4] << 8) | (ea[5] ) ;
  223. #undef ea
  224. /* Clear multicast address hash table
  225. */
  226. fecp->fec_hash_table_high = 0;
  227. fecp->fec_hash_table_low = 0;
  228. /* Set maximum receive buffer size.
  229. */
  230. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  231. /* Set maximum frame length
  232. */
  233. fecp->fec_r_hash = PKT_MAXBUF_SIZE;
  234. /*
  235. * Setup Buffers and Buffer Desriptors
  236. */
  237. rxIdx = 0;
  238. txIdx = 0;
  239. if (!rtx) {
  240. #ifdef CFG_ALLOC_DPRAM
  241. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + dpram_alloc_align(sizeof(RTXBD),8));
  242. #else
  243. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
  244. #endif
  245. }
  246. /*
  247. * Setup Receiver Buffer Descriptors (13.14.24.18)
  248. * Settings:
  249. * Empty, Wrap
  250. */
  251. for (i = 0; i < PKTBUFSRX; i++) {
  252. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  253. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  254. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  255. }
  256. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  257. /*
  258. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  259. * Settings:
  260. * Last, Tx CRC
  261. */
  262. for (i = 0; i < TX_BUF_CNT; i++) {
  263. rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  264. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  265. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  266. }
  267. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  268. /* Set receive and transmit descriptor base
  269. */
  270. fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
  271. fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
  272. /* Enable MII mode
  273. */
  274. #if 0 /* Full duplex mode */
  275. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
  276. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
  277. #else /* Half duplex mode */
  278. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  279. fecp->fec_x_cntrl = 0;
  280. #endif
  281. /* Enable big endian and don't care about SDMA FC.
  282. */
  283. fecp->fec_fun_code = 0x78000000;
  284. /* Set MII speed to 2.5 MHz or slightly below.
  285. * According to the MPC860T (Rev. D) Fast ethernet controller user
  286. * manual (6.2.14),
  287. * the MII management interface clock must be less than or equal
  288. * to 2.5 MHz.
  289. * This MDC frequency is equal to system clock / (2 * MII_SPEED).
  290. * Then MII_SPEED = system_clock / 2 * 2,5 Mhz.
  291. */
  292. fecp->fec_mii_speed = ((bd->bi_busfreq + 4999999) / 5000000) << 1;
  293. #if !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210)
  294. /* Configure all of port D for MII.
  295. */
  296. immr->im_ioport.iop_pdpar = 0x1fff;
  297. /* Bits moved from Rev. D onward */
  298. if ((get_immr (0) & 0xffff) < 0x0501) {
  299. immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  300. } else {
  301. immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  302. }
  303. #else
  304. /* Configure port A for MII.
  305. */
  306. #if defined(CONFIG_ICU862) && defined(CFG_DISCOVER_PHY)
  307. /* On the ICU862 board the MII-MDC pin is routed to PD8 pin
  308. * of CPU, so for this board we need to configure Utopia and
  309. * enable PD8 to MII-MDC function */
  310. immr->im_ioport.iop_pdpar |= 0x4080;
  311. #endif
  312. /* Has Utopia been configured? */
  313. if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
  314. /*
  315. * YES - Use MUXED mode for UTOPIA bus.
  316. * This frees Port A for use by MII (see 862UM table 41-6).
  317. */
  318. immr->im_ioport.utmode &= ~0x80;
  319. } else {
  320. /*
  321. * NO - set SPLIT mode for UTOPIA bus.
  322. *
  323. * This doesn't really effect UTOPIA (which isn't
  324. * enabled anyway) but just tells the 862
  325. * to use port A for MII (see 862UM table 41-6).
  326. */
  327. immr->im_ioport.utmode |= 0x80;
  328. }
  329. #endif /* !defined(CONFIG_ICU862) */
  330. rxIdx = 0;
  331. txIdx = 0;
  332. /* Now enable the transmit and receive processing
  333. */
  334. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  335. #ifdef CFG_DISCOVER_PHY
  336. /* wait for the PHY to wake up after reset
  337. */
  338. mii_discover_phy();
  339. #endif
  340. /* And last, try to fill Rx Buffer Descriptors */
  341. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  342. return 1;
  343. }
  344. static void fec_halt(struct eth_device* dev)
  345. {
  346. #if 0
  347. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  348. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  349. #endif
  350. }
  351. #if 0
  352. void restart(void)
  353. {
  354. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  355. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  356. }
  357. #endif
  358. #if defined(CFG_DISCOVER_PHY) || (CONFIG_COMMANDS & CFG_CMD_MII)
  359. static int phyaddr = -1; /* didn't find a PHY yet */
  360. static uint phytype;
  361. /* Make MII read/write commands for the FEC.
  362. */
  363. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  364. (REG & 0x1f) << 18))
  365. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  366. (REG & 0x1f) << 18) | \
  367. (VAL & 0xffff))
  368. /* Interrupt events/masks.
  369. */
  370. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  371. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  372. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  373. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  374. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  375. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  376. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  377. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  378. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  379. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  380. /* PHY identification
  381. */
  382. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  383. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  384. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  385. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  386. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  387. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  388. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  389. /* send command to phy using mii, wait for result */
  390. static uint
  391. mii_send(uint mii_cmd)
  392. {
  393. uint mii_reply;
  394. volatile fec_t *ep;
  395. ep = &(((immap_t *)CFG_IMMR)->im_cpm.cp_fec);
  396. ep->fec_mii_data = mii_cmd; /* command to phy */
  397. /* wait for mii complete */
  398. while (!(ep->fec_ievent & FEC_ENET_MII))
  399. ; /* spin until done */
  400. mii_reply = ep->fec_mii_data; /* result from phy */
  401. ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
  402. #if 0
  403. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  404. __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
  405. #endif
  406. return (mii_reply & 0xffff); /* data read from phy */
  407. }
  408. #endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CFG_CMD_MII) */
  409. #if defined(CFG_DISCOVER_PHY)
  410. static void
  411. mii_discover_phy(void)
  412. {
  413. #define MAX_PHY_PASSES 11
  414. uint phyno;
  415. int pass;
  416. phyaddr = -1; /* didn't find a PHY yet */
  417. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  418. if (pass > 1) {
  419. /* PHY may need more time to recover from reset.
  420. * The LXT970 needs 50ms typical, no maximum is
  421. * specified, so wait 10ms before try again.
  422. * With 11 passes this gives it 100ms to wake up.
  423. */
  424. udelay(10000); /* wait 10ms */
  425. }
  426. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  427. phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR1));
  428. #ifdef ET_DEBUG
  429. printf("PHY type 0x%x pass %d type ", phytype, pass);
  430. #endif
  431. if (phytype != 0xffff) {
  432. phyaddr = phyno;
  433. phytype <<= 16;
  434. phytype |= mii_send(mk_mii_read(phyno,
  435. PHY_PHYIDR2));
  436. #ifdef ET_DEBUG
  437. printf("PHY @ 0x%x pass %d type ",phyno,pass);
  438. switch (phytype & 0xfffffff0) {
  439. case PHY_ID_LXT970:
  440. printf("LXT970\n");
  441. break;
  442. case PHY_ID_LXT971:
  443. printf("LXT971\n");
  444. break;
  445. case PHY_ID_82555:
  446. printf("82555\n");
  447. break;
  448. case PHY_ID_QS6612:
  449. printf("QS6612\n");
  450. break;
  451. case PHY_ID_AMD79C784:
  452. printf("AMD79C784\n");
  453. break;
  454. case PHY_ID_LSI80225B:
  455. printf("LSI L80225/B\n");
  456. break;
  457. default:
  458. printf("0x%08x\n", phytype);
  459. break;
  460. }
  461. #endif
  462. }
  463. }
  464. }
  465. if (phyaddr < 0) {
  466. printf("No PHY device found.\n");
  467. }
  468. }
  469. #endif /* CFG_DISCOVER_PHY */
  470. #if (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)
  471. static int mii_init_done = 0;
  472. /****************************************************************************
  473. * mii_init -- Initialize the MII for MII command without ethernet
  474. * This function is a subset of eth_init
  475. ****************************************************************************
  476. */
  477. void mii_init (void)
  478. {
  479. DECLARE_GLOBAL_DATA_PTR;
  480. bd_t *bd = gd->bd;
  481. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  482. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  483. int i;
  484. if (mii_init_done != 0) {
  485. return;
  486. }
  487. /* Whack a reset.
  488. * A delay is required between a reset of the FEC block and
  489. * initialization of other FEC registers because the reset takes
  490. * some time to complete. If you don't delay, subsequent writes
  491. * to FEC registers might get killed by the reset routine which is
  492. * still in progress.
  493. */
  494. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  495. for (i = 0;
  496. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  497. ++i) {
  498. udelay (1);
  499. }
  500. if (i == FEC_RESET_DELAY) {
  501. printf ("FEC_RESET_DELAY timeout\n");
  502. return;
  503. }
  504. /* We use strictly polling mode only
  505. */
  506. fecp->fec_imask = 0;
  507. /* Clear any pending interrupt
  508. */
  509. fecp->fec_ievent = 0xffc0;
  510. /* Set MII speed to 2.5 MHz or slightly below.
  511. * According to the MPC860T (Rev. D) Fast ethernet controller user
  512. * manual (6.2.14),
  513. * the MII management interface clock must be less than or equal
  514. * to 2.5 MHz.
  515. * This MDC frequency is equal to system clock / (2 * MII_SPEED).
  516. * Then MII_SPEED = system_clock / 2 * 2,5 Mhz.
  517. */
  518. fecp->fec_mii_speed = ((bd->bi_busfreq + 4999999) / 5000000) << 1;
  519. #if !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210)
  520. /* Configure all of port D for MII.
  521. */
  522. immr->im_ioport.iop_pdpar = 0x1fff;
  523. /* Bits moved from Rev. D onward */
  524. if ((get_immr (0) & 0xffff) < 0x0501) {
  525. immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  526. } else {
  527. immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  528. }
  529. #else
  530. /* Configure port A for MII.
  531. */
  532. #if defined(CONFIG_ICU862)
  533. /* On the ICU862 board the MII-MDC pin is routed to PD8 pin
  534. * of CPU, so for this board we need to configure Utopia and
  535. * enable PD8 to MII-MDC function */
  536. immr->im_ioport.iop_pdpar |= 0x4080;
  537. #endif
  538. /* Has Utopia been configured? */
  539. if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
  540. /*
  541. * YES - Use MUXED mode for UTOPIA bus.
  542. * This frees Port A for use by MII (see 862UM table 41-6).
  543. */
  544. immr->im_ioport.utmode &= ~0x80;
  545. } else {
  546. /*
  547. * NO - set SPLIT mode for UTOPIA bus.
  548. *
  549. * This doesn't really effect UTOPIA (which isn't
  550. * enabled anyway) but just tells the 862
  551. * to use port A for MII (see 862UM table 41-6).
  552. */
  553. immr->im_ioport.utmode |= 0x80;
  554. }
  555. #endif /* !defined(CONFIG_ICU862) */
  556. /* Now enable the transmit and receive processing
  557. */
  558. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  559. mii_init_done = 1;
  560. }
  561. /*****************************************************************************
  562. * Read and write a MII PHY register, routines used by MII Utilities
  563. *
  564. * FIXME: These routines are expected to return 0 on success, but mii_send
  565. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  566. * no PHY connected...
  567. * For now always return 0.
  568. * FIXME: These routines only work after calling eth_init() at least once!
  569. * Otherwise they hang in mii_send() !!! Sorry!
  570. *****************************************************************************/
  571. int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
  572. {
  573. short rdreg; /* register working value */
  574. #ifdef MII_DEBUG
  575. printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  576. #endif
  577. rdreg = mii_send(mk_mii_read(addr, reg));
  578. *value = rdreg;
  579. #ifdef MII_DEBUG
  580. printf ("0x%04x\n", *value);
  581. #endif
  582. return 0;
  583. }
  584. int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
  585. {
  586. short rdreg; /* register working value */
  587. #ifdef MII_DEBUG
  588. printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  589. #endif
  590. rdreg = mii_send(mk_mii_write(addr, reg, value));
  591. #ifdef MII_DEBUG
  592. printf ("0x%04x\n", value);
  593. #endif
  594. return 0;
  595. }
  596. #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)*/
  597. #endif /* CFG_CMD_NET, FEC_ENET */