fec.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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 defined(CONFIG_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. #ifndef CFG_ENET_BD_BASE
  169. DECLARE_GLOBAL_DATA_PTR;
  170. #endif
  171. int i;
  172. volatile fec_t *fecp = (fec_t *) (FEC_ADDR);
  173. /* Whack a reset.
  174. * A delay is required between a reset of the FEC block and
  175. * initialization of other FEC registers because the reset takes
  176. * some time to complete. If you don't delay, subsequent writes
  177. * to FEC registers might get killed by the reset routine which is
  178. * still in progress.
  179. */
  180. fecp->fec_ecntrl = FEC_ECNTRL_RESET;
  181. for (i = 0;
  182. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  183. ++i) {
  184. udelay (1);
  185. }
  186. if (i == FEC_RESET_DELAY) {
  187. printf ("FEC_RESET_DELAY timeout\n");
  188. return 0;
  189. }
  190. /* We use strictly polling mode only
  191. */
  192. fecp->fec_imask = 0;
  193. /* Clear any pending interrupt */
  194. fecp->fec_ievent = 0xffffffff;
  195. /* Set station address */
  196. #define ea bd->bi_enetaddr
  197. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) |
  198. (ea[2] << 8) | (ea[3]);
  199. fecp->fec_addr_high = (ea[4] << 24) | (ea[5] << 16);
  200. #ifdef ET_DEBUG
  201. printf ("Eth Addrs: %02x:%02x:%02x:%02x:%02x:%02x\n",
  202. ea[0], ea[1], ea[2], ea[3], ea[4], ea[5]);
  203. #endif
  204. #undef ea
  205. #ifdef CONFIG_M5271
  206. /* Clear multicast address hash table
  207. */
  208. fecp->fec_ghash_table_high = 0;
  209. fecp->fec_ghash_table_low = 0;
  210. /* Clear individual address hash table
  211. */
  212. fecp->fec_ihash_table_high = 0;
  213. fecp->fec_ihash_table_low = 0;
  214. #else
  215. /* Clear multicast address hash table
  216. */
  217. #ifdef CONFIG_M5282
  218. fecp->fec_ihash_table_high = 0;
  219. fecp->fec_ihash_table_low = 0;
  220. #else
  221. fecp->fec_hash_table_high = 0;
  222. fecp->fec_hash_table_low = 0;
  223. #endif
  224. #endif
  225. /* Set maximum receive buffer size.
  226. */
  227. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  228. /*
  229. * Setup Buffers and Buffer Desriptors
  230. */
  231. rxIdx = 0;
  232. txIdx = 0;
  233. if (!rtx) {
  234. #ifdef CFG_ENET_BD_BASE
  235. rtx = (RTXBD *) CFG_ENET_BD_BASE;
  236. #else
  237. rtx = (RTXBD *) (CFG_MONITOR_BASE+gd->reloc_off -
  238. (((PKTBUFSRX+TX_BUF_CNT)*+sizeof(cbd_t)
  239. +0xFF)
  240. & ~0xFF)
  241. );
  242. debug("set ENET_DB_BASE to %lX\n",(long) rtx);
  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 = (PKT_MAXBUF_SIZE << 16); /* set max frame length */
  278. fecp->fec_r_cntrl |= FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  279. fecp->fec_x_cntrl = 0;
  280. #endif
  281. /* Set MII speed */
  282. fecp->fec_mii_speed = (((CFG_CLK / 2) / (2500000 / 10)) + 5) / 10;
  283. fecp->fec_mii_speed *= 2;
  284. /* Configure port B for MII.
  285. */
  286. /* port initialization was already made in cpu_init_f() */
  287. /* Now enable the transmit and receive processing
  288. */
  289. fecp->fec_ecntrl = FEC_ECNTRL_ETHER_EN;
  290. #ifdef CFG_DISCOVER_PHY
  291. /* wait for the PHY to wake up after reset */
  292. mii_discover_phy ();
  293. #endif
  294. /* And last, try to fill Rx Buffer Descriptors */
  295. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  296. return 1;
  297. }
  298. void eth_halt (void)
  299. {
  300. volatile fec_t *fecp = (fec_t *) FEC_ADDR;
  301. fecp->fec_ecntrl = 0;
  302. }
  303. #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_CMD_MII)
  304. static int phyaddr = -1; /* didn't find a PHY yet */
  305. static uint phytype;
  306. /* Make MII read/write commands for the FEC.
  307. */
  308. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  309. (REG & 0x1f) << 18))
  310. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  311. (REG & 0x1f) << 18) | \
  312. (VAL & 0xffff))
  313. /* Interrupt events/masks.
  314. */
  315. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  316. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  317. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  318. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  319. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  320. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  321. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  322. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  323. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  324. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  325. /* PHY identification
  326. */
  327. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  328. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  329. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  330. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  331. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  332. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  333. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  334. /* send command to phy using mii, wait for result */
  335. static uint mii_send (uint mii_cmd)
  336. {
  337. uint mii_reply;
  338. volatile fec_t *ep = (fec_t *) (FEC_ADDR);
  339. ep->fec_mii_data = mii_cmd; /* command to phy */
  340. /* wait for mii complete */
  341. while (!(ep->fec_ievent & FEC_ENET_MII)); /* spin until done */
  342. mii_reply = ep->fec_mii_data; /* result from phy */
  343. ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
  344. #ifdef ET_DEBUG
  345. printf ("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  346. __FILE__, __LINE__, __FUNCTION__, mii_cmd, mii_reply);
  347. #endif
  348. return (mii_reply & 0xffff); /* data read from phy */
  349. }
  350. #endif
  351. #if defined(CFG_DISCOVER_PHY)
  352. static void mii_discover_phy (void)
  353. {
  354. #define MAX_PHY_PASSES 11
  355. uint phyno;
  356. int pass;
  357. phyaddr = -1; /* didn't find a PHY yet */
  358. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  359. if (pass > 1) {
  360. /* PHY may need more time to recover from reset.
  361. * The LXT970 needs 50ms typical, no maximum is
  362. * specified, so wait 10ms before try again.
  363. * With 11 passes this gives it 100ms to wake up.
  364. */
  365. udelay (10000); /* wait 10ms */
  366. }
  367. for (phyno = 1; phyno < 32 && phyaddr < 0; ++phyno) {
  368. phytype = mii_send (mk_mii_read (phyno, PHY_PHYIDR1));
  369. #ifdef ET_DEBUG
  370. printf ("PHY type 0x%x pass %d type ", phytype, pass);
  371. #endif
  372. if (phytype != 0xffff) {
  373. phyaddr = phyno;
  374. phytype <<= 16;
  375. phytype |= mii_send (mk_mii_read (phyno,
  376. PHY_PHYIDR2));
  377. #ifdef ET_DEBUG
  378. printf ("PHY @ 0x%x pass %d type ", phyno,
  379. pass);
  380. switch (phytype & 0xfffffff0) {
  381. case PHY_ID_LXT970:
  382. printf ("LXT970\n");
  383. break;
  384. case PHY_ID_LXT971:
  385. printf ("LXT971\n");
  386. break;
  387. case PHY_ID_82555:
  388. printf ("82555\n");
  389. break;
  390. case PHY_ID_QS6612:
  391. printf ("QS6612\n");
  392. break;
  393. case PHY_ID_AMD79C784:
  394. printf ("AMD79C784\n");
  395. break;
  396. case PHY_ID_LSI80225B:
  397. printf ("LSI L80225/B\n");
  398. break;
  399. default:
  400. printf ("0x%08x\n", phytype);
  401. break;
  402. }
  403. #endif
  404. }
  405. }
  406. }
  407. if (phyaddr < 0) {
  408. printf ("No PHY device found.\n");
  409. }
  410. }
  411. #endif /* CFG_DISCOVER_PHY */
  412. #if defined(CONFIG_CMD_MII) && !defined(CONFIG_BITBANGMII)
  413. static int mii_init_done = 0;
  414. /****************************************************************************
  415. * mii_init -- Initialize the MII for MII command without ethernet
  416. * This function is a subset of eth_init
  417. ****************************************************************************
  418. */
  419. void mii_init (void)
  420. {
  421. volatile fec_t *fecp = (fec_t *) (FEC_ADDR);
  422. int i;
  423. if (mii_init_done != 0) {
  424. return;
  425. }
  426. /* Whack a reset.
  427. * A delay is required between a reset of the FEC block and
  428. * initialization of other FEC registers because the reset takes
  429. * some time to complete. If you don't delay, subsequent writes
  430. * to FEC registers might get killed by the reset routine which is
  431. * still in progress.
  432. */
  433. fecp->fec_ecntrl = FEC_ECNTRL_RESET;
  434. for (i = 0;
  435. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  436. ++i) {
  437. udelay (1);
  438. }
  439. if (i == FEC_RESET_DELAY) {
  440. printf ("FEC_RESET_DELAY timeout\n");
  441. return;
  442. }
  443. /* We use strictly polling mode only
  444. */
  445. fecp->fec_imask = 0;
  446. /* Clear any pending interrupt
  447. */
  448. fecp->fec_ievent = 0xffffffff;
  449. /* Set MII speed */
  450. fecp->fec_mii_speed = 0x0e;
  451. /* Configure port B for MII.
  452. */
  453. /* port initialization was already made in cpu_init_f() */
  454. /* Now enable the transmit and receive processing */
  455. fecp->fec_ecntrl = FEC_ECNTRL_ETHER_EN;
  456. mii_init_done = 1;
  457. }
  458. /*****************************************************************************
  459. * Read and write a MII PHY register, routines used by MII Utilities
  460. *
  461. * FIXME: These routines are expected to return 0 on success, but mii_send
  462. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  463. * no PHY connected...
  464. * For now always return 0.
  465. * FIXME: These routines only work after calling eth_init() at least once!
  466. * Otherwise they hang in mii_send() !!! Sorry!
  467. *****************************************************************************/
  468. int mcf52x2_miiphy_read (char *devname, unsigned char addr,
  469. unsigned char reg, unsigned short *value)
  470. {
  471. short rdreg; /* register working value */
  472. #ifdef MII_DEBUG
  473. printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  474. #endif
  475. rdreg = mii_send (mk_mii_read (addr, reg));
  476. *value = rdreg;
  477. #ifdef MII_DEBUG
  478. printf ("0x%04x\n", *value);
  479. #endif
  480. return 0;
  481. }
  482. int mcf52x2_miiphy_write (char *devname, unsigned char addr,
  483. unsigned char reg, unsigned short value)
  484. {
  485. short rdreg; /* register working value */
  486. #ifdef MII_DEBUG
  487. printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  488. #endif
  489. rdreg = mii_send (mk_mii_write (addr, reg, value));
  490. #ifdef MII_DEBUG
  491. printf ("0x%04x\n", value);
  492. #endif
  493. return 0;
  494. }
  495. #endif
  496. #endif
  497. int mcf52x2_miiphy_initialize(bd_t *bis)
  498. {
  499. #if defined(CONFIG_CMD_NET) && defined(FEC_ENET)
  500. #if defined(CONFIG_CMD_MII) && !defined(CONFIG_BITBANGMII)
  501. miiphy_register("mcf52x2phy", mcf52x2_miiphy_read, mcf52x2_miiphy_write);
  502. #endif
  503. #endif
  504. return 0;
  505. }