fec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * (C) Copyright 2000-2004
  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 <asm/fec.h>
  26. #ifdef CONFIG_M5271
  27. #include <asm/m5271.h>
  28. #include <asm/immap_5271.h>
  29. #endif
  30. #ifdef CONFIG_M5272
  31. #include <asm/m5272.h>
  32. #include <asm/immap_5272.h>
  33. #endif
  34. #ifdef CONFIG_M5282
  35. #include <asm/m5282.h>
  36. #include <asm/immap_5282.h>
  37. #endif
  38. #include <net.h>
  39. #include <command.h>
  40. #ifdef CONFIG_M5272
  41. #define FEC_ADDR (CFG_MBAR + 0x840)
  42. #endif
  43. #if defined(CONFIG_M5282) || defined(CONFIG_M5271)
  44. #define FEC_ADDR (CFG_MBAR + 0x1000)
  45. #endif
  46. #undef ET_DEBUG
  47. #undef MII_DEBUG
  48. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(FEC_ENET)
  49. #ifdef CFG_DISCOVER_PHY
  50. #include <miiphy.h>
  51. static void mii_discover_phy (void);
  52. #endif
  53. /* Ethernet Transmit and Receive Buffers */
  54. #define DBUF_LENGTH 1520
  55. #define TX_BUF_CNT 2
  56. #define TOUT_LOOP 100
  57. #define PKT_MAXBUF_SIZE 1518
  58. #define PKT_MINBUF_SIZE 64
  59. #define PKT_MAXBLR_SIZE 1520
  60. static char txbuf[DBUF_LENGTH];
  61. static uint rxIdx; /* index of the current RX buffer */
  62. static uint txIdx; /* index of the current TX buffer */
  63. /*
  64. * FEC Ethernet Tx and Rx buffer descriptors allocated at the
  65. * immr->udata_bd address on Dual-Port RAM
  66. * Provide for Double Buffering
  67. */
  68. typedef volatile struct CommonBufferDescriptor {
  69. cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
  70. cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
  71. } RTXBD;
  72. static RTXBD *rtx = NULL;
  73. int eth_send (volatile void *packet, int length)
  74. {
  75. int j, rc;
  76. volatile fec_t *fecp = (fec_t *) (FEC_ADDR);
  77. /* section 16.9.23.3
  78. * Wait for ready
  79. */
  80. j = 0;
  81. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY)
  82. && (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. /* Activate transmit Buffer Descriptor polling */
  93. fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */
  94. j = 0;
  95. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY)
  96. && (j < TOUT_LOOP)) {
  97. udelay (1);
  98. j++;
  99. }
  100. if (j >= TOUT_LOOP) {
  101. printf ("TX timeout\n");
  102. }
  103. #ifdef ET_DEBUG
  104. printf ("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
  105. __FILE__, __LINE__, __FUNCTION__, j, rtx->txbd[txIdx].cbd_sc,
  106. (rtx->txbd[txIdx].cbd_sc & 0x003C) >> 2);
  107. #endif
  108. /* return only status bits */ ;
  109. rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
  110. txIdx = (txIdx + 1) % TX_BUF_CNT;
  111. return rc;
  112. }
  113. int eth_rx (void)
  114. {
  115. int length;
  116. volatile fec_t *fecp = (fec_t *) FEC_ADDR;
  117. for (;;) {
  118. /* section 16.9.23.2 */
  119. if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  120. length = -1;
  121. break; /* nothing received - leave for() loop */
  122. }
  123. length = rtx->rxbd[rxIdx].cbd_datlen;
  124. if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
  125. #ifdef ET_DEBUG
  126. printf ("%s[%d] err: %x\n",
  127. __FUNCTION__, __LINE__,
  128. rtx->rxbd[rxIdx].cbd_sc);
  129. #endif
  130. } else {
  131. /* Pass the packet up to the protocol layers. */
  132. NetReceive (NetRxPackets[rxIdx], length - 4);
  133. }
  134. /* Give the buffer back to the FEC. */
  135. rtx->rxbd[rxIdx].cbd_datlen = 0;
  136. /* wrap around buffer index when necessary */
  137. if ((rxIdx + 1) >= PKTBUFSRX) {
  138. rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
  139. (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  140. rxIdx = 0;
  141. } else {
  142. rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  143. rxIdx++;
  144. }
  145. /* Try to fill Buffer Descriptors */
  146. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  147. }
  148. return length;
  149. }
  150. /**************************************************************
  151. *
  152. * FEC Ethernet Initialization Routine
  153. *
  154. *************************************************************/
  155. #define FEC_ECNTRL_ETHER_EN 0x00000002
  156. #define FEC_ECNTRL_RESET 0x00000001
  157. #define FEC_RCNTRL_BC_REJ 0x00000010
  158. #define FEC_RCNTRL_PROM 0x00000008
  159. #define FEC_RCNTRL_MII_MODE 0x00000004
  160. #define FEC_RCNTRL_DRT 0x00000002
  161. #define FEC_RCNTRL_LOOP 0x00000001
  162. #define FEC_TCNTRL_FDEN 0x00000004
  163. #define FEC_TCNTRL_HBC 0x00000002
  164. #define FEC_TCNTRL_GTS 0x00000001
  165. #define FEC_RESET_DELAY 50000
  166. int eth_init (bd_t * bd)
  167. {
  168. int i;
  169. volatile fec_t *fecp = (fec_t *) (FEC_ADDR);
  170. /* Whack a reset.
  171. * A delay is required between a reset of the FEC block and
  172. * initialization of other FEC registers because the reset takes
  173. * some time to complete. If you don't delay, subsequent writes
  174. * to FEC registers might get killed by the reset routine which is
  175. * still in progress.
  176. */
  177. fecp->fec_ecntrl = FEC_ECNTRL_RESET;
  178. for (i = 0;
  179. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  180. ++i) {
  181. udelay (1);
  182. }
  183. if (i == FEC_RESET_DELAY) {
  184. printf ("FEC_RESET_DELAY timeout\n");
  185. return 0;
  186. }
  187. /* We use strictly polling mode only
  188. */
  189. fecp->fec_imask = 0;
  190. /* Clear any pending interrupt */
  191. fecp->fec_ievent = 0xffffffff;
  192. /* Set station address */
  193. #define ea bd->bi_enetaddr
  194. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) |
  195. (ea[2] << 8) | (ea[3]);
  196. fecp->fec_addr_high = (ea[4] << 24) | (ea[5] << 16);
  197. #ifdef ET_DEBUG
  198. printf ("Eth Addrs: %02x:%02x:%02x:%02x:%02x:%02x\n",
  199. ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
  200. #endif
  201. #undef ea
  202. #ifdef CONFIG_M5271
  203. /* Clear multicast address hash table
  204. */
  205. fecp->fec_ghash_table_high = 0;
  206. fecp->fec_ghash_table_low = 0;
  207. /* Clear individual address hash table
  208. */
  209. fecp->fec_ihash_table_high = 0;
  210. fecp->fec_ihash_table_low = 0;
  211. #else
  212. /* Clear multicast address hash table
  213. */
  214. fecp->fec_hash_table_high = 0;
  215. fecp->fec_hash_table_low = 0;
  216. #endif
  217. /* Set maximum receive buffer size.
  218. */
  219. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  220. /*
  221. * Setup Buffers and Buffer Desriptors
  222. */
  223. rxIdx = 0;
  224. txIdx = 0;
  225. if (!rtx) {
  226. rtx = (RTXBD *) CFG_ENET_BD_BASE;
  227. }
  228. /*
  229. * Setup Receiver Buffer Descriptors (13.14.24.18)
  230. * Settings:
  231. * Empty, Wrap
  232. */
  233. for (i = 0; i < PKTBUFSRX; i++) {
  234. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  235. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  236. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  237. }
  238. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  239. /*
  240. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  241. * Settings:
  242. * Last, Tx CRC
  243. */
  244. for (i = 0; i < TX_BUF_CNT; i++) {
  245. rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  246. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  247. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  248. }
  249. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  250. /* Set receive and transmit descriptor base
  251. */
  252. fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
  253. fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
  254. /* Enable MII mode
  255. */
  256. #if 0 /* Full duplex mode */
  257. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
  258. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
  259. #else /* Half duplex mode */
  260. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  261. #ifdef CONFIG_M5271
  262. fecp->fec_r_cntrl |= (PKT_MAXBUF_SIZE << 16); /* set max frame length */
  263. #endif
  264. fecp->fec_x_cntrl = 0;
  265. #endif
  266. /* Set MII speed */
  267. fecp->fec_mii_speed = 0x0e;
  268. /* Configure port B for MII.
  269. */
  270. /* port initialization was already made in cpu_init_f() */
  271. /* Now enable the transmit and receive processing
  272. */
  273. fecp->fec_ecntrl = FEC_ECNTRL_ETHER_EN;
  274. #ifdef CFG_DISCOVER_PHY
  275. /* wait for the PHY to wake up after reset */
  276. mii_discover_phy ();
  277. #endif
  278. /* And last, try to fill Rx Buffer Descriptors */
  279. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  280. return 1;
  281. }
  282. void eth_halt (void)
  283. {
  284. volatile fec_t *fecp = (fec_t *) FEC_ADDR;
  285. fecp->fec_ecntrl = 0;
  286. }
  287. #if defined(CFG_DISCOVER_PHY) || (CONFIG_COMMANDS & CFG_CMD_MII)
  288. static int phyaddr = -1; /* didn't find a PHY yet */
  289. static uint phytype;
  290. /* Make MII read/write commands for the FEC.
  291. */
  292. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  293. (REG & 0x1f) << 18))
  294. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  295. (REG & 0x1f) << 18) | \
  296. (VAL & 0xffff))
  297. /* Interrupt events/masks.
  298. */
  299. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  300. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  301. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  302. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  303. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  304. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  305. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  306. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  307. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  308. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  309. /* PHY identification
  310. */
  311. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  312. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  313. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  314. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  315. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  316. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  317. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  318. /* send command to phy using mii, wait for result */
  319. static uint mii_send (uint mii_cmd)
  320. {
  321. uint mii_reply;
  322. volatile fec_t *ep = (fec_t *) (FEC_ADDR);
  323. ep->fec_mii_data = mii_cmd; /* command to phy */
  324. /* wait for mii complete */
  325. while (!(ep->fec_ievent & FEC_ENET_MII)); /* spin until done */
  326. mii_reply = ep->fec_mii_data; /* result from phy */
  327. ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
  328. #ifdef ET_DEBUG
  329. printf ("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  330. __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply);
  331. #endif
  332. return (mii_reply & 0xffff); /* data read from phy */
  333. }
  334. #endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CFG_CMD_MII) */
  335. #if defined(CFG_DISCOVER_PHY)
  336. static void mii_discover_phy (void)
  337. {
  338. #define MAX_PHY_PASSES 11
  339. uint phyno;
  340. int pass;
  341. phyaddr = -1; /* didn't find a PHY yet */
  342. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  343. if (pass > 1) {
  344. /* PHY may need more time to recover from reset.
  345. * The LXT970 needs 50ms typical, no maximum is
  346. * specified, so wait 10ms before try again.
  347. * With 11 passes this gives it 100ms to wake up.
  348. */
  349. udelay (10000); /* wait 10ms */
  350. }
  351. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  352. phytype = mii_send (mk_mii_read (phyno, PHY_PHYIDR1));
  353. #ifdef ET_DEBUG
  354. printf ("PHY type 0x%x pass %d type ", phytype, pass);
  355. #endif
  356. if (phytype != 0xffff) {
  357. phyaddr = phyno;
  358. phytype <<= 16;
  359. phytype |= mii_send (mk_mii_read (phyno,
  360. PHY_PHYIDR2));
  361. #ifdef ET_DEBUG
  362. printf ("PHY @ 0x%x pass %d type ", phyno,
  363. pass);
  364. switch (phytype & 0xfffffff0) {
  365. case PHY_ID_LXT970:
  366. printf ("LXT970\n");
  367. break;
  368. case PHY_ID_LXT971:
  369. printf ("LXT971\n");
  370. break;
  371. case PHY_ID_82555:
  372. printf ("82555\n");
  373. break;
  374. case PHY_ID_QS6612:
  375. printf ("QS6612\n");
  376. break;
  377. case PHY_ID_AMD79C784:
  378. printf ("AMD79C784\n");
  379. break;
  380. case PHY_ID_LSI80225B:
  381. printf ("LSI L80225/B\n");
  382. break;
  383. default:
  384. printf ("0x%08x\n", phytype);
  385. break;
  386. }
  387. #endif
  388. }
  389. }
  390. }
  391. if (phyaddr < 0) {
  392. printf ("No PHY device found.\n");
  393. }
  394. }
  395. #endif /* CFG_DISCOVER_PHY */
  396. #if (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)
  397. static int mii_init_done = 0;
  398. /****************************************************************************
  399. * mii_init -- Initialize the MII for MII command without ethernet
  400. * This function is a subset of eth_init
  401. ****************************************************************************
  402. */
  403. void mii_init (void)
  404. {
  405. volatile fec_t *fecp = (fec_t *) (FEC_ADDR);
  406. int i;
  407. if (mii_init_done != 0) {
  408. return;
  409. }
  410. /* Whack a reset.
  411. * A delay is required between a reset of the FEC block and
  412. * initialization of other FEC registers because the reset takes
  413. * some time to complete. If you don't delay, subsequent writes
  414. * to FEC registers might get killed by the reset routine which is
  415. * still in progress.
  416. */
  417. fecp->fec_ecntrl = FEC_ECNTRL_RESET;
  418. for (i = 0;
  419. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  420. ++i) {
  421. udelay (1);
  422. }
  423. if (i == FEC_RESET_DELAY) {
  424. printf ("FEC_RESET_DELAY timeout\n");
  425. return;
  426. }
  427. /* We use strictly polling mode only
  428. */
  429. fecp->fec_imask = 0;
  430. /* Clear any pending interrupt
  431. */
  432. fecp->fec_ievent = 0xffffffff;
  433. /* Set MII speed */
  434. fecp->fec_mii_speed = 0x0e;
  435. /* Configure port B for MII.
  436. */
  437. /* port initialization was already made in cpu_init_f() */
  438. /* Now enable the transmit and receive processing */
  439. fecp->fec_ecntrl = FEC_ECNTRL_ETHER_EN;
  440. mii_init_done = 1;
  441. }
  442. /*****************************************************************************
  443. * Read and write a MII PHY register, routines used by MII Utilities
  444. *
  445. * FIXME: These routines are expected to return 0 on success, but mii_send
  446. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  447. * no PHY connected...
  448. * For now always return 0.
  449. * FIXME: These routines only work after calling eth_init() at least once!
  450. * Otherwise they hang in mii_send() !!! Sorry!
  451. *****************************************************************************/
  452. int mcf52x2_miiphy_read (char *devname, unsigned char addr,
  453. unsigned char reg, unsigned short *value)
  454. {
  455. short rdreg; /* register working value */
  456. #ifdef MII_DEBUG
  457. printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  458. #endif
  459. rdreg = mii_send (mk_mii_read (addr, reg));
  460. *value = rdreg;
  461. #ifdef MII_DEBUG
  462. printf ("0x%04x\n", *value);
  463. #endif
  464. return 0;
  465. }
  466. int mcf52x2_miiphy_write (char *devname, unsigned char addr,
  467. unsigned char reg, unsigned short value)
  468. {
  469. short rdreg; /* register working value */
  470. #ifdef MII_DEBUG
  471. printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  472. #endif
  473. rdreg = mii_send (mk_mii_write (addr, reg, value));
  474. #ifdef MII_DEBUG
  475. printf ("0x%04x\n", value);
  476. #endif
  477. return 0;
  478. }
  479. #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII) */
  480. #endif /* CFG_CMD_NET, FEC_ENET */
  481. int mcf52x2_miiphy_initialize(bd_t *bis)
  482. {
  483. #if (CONFIG_COMMANDS & CFG_CMD_NET) && defined(FEC_ENET)
  484. #if (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)
  485. miiphy_register("mcf52x2phy", mcf52x2_miiphy_read, mcf52x2_miiphy_write);
  486. #endif
  487. #endif
  488. return 0;
  489. }