fec.c 15 KB

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