fec.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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) && \
  30. (defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FEC1) || defined(CONFIG_ETHER_ON_FEC2))
  31. /* compatibility test, if only FEC_ENET defined assume ETHER on FEC1 */
  32. #if defined(FEC_ENET) && !defined(CONFIG_ETHER_ON_FEC1) && !defined(CONFIG_ETHER_ON_FEC2)
  33. #define CONFIG_ETHER_ON_FEC1 1
  34. #endif
  35. /* define WANT_MII when MII support is required */
  36. #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY)
  37. #define WANT_MII
  38. #else
  39. #undef WANT_MII
  40. #endif
  41. #if defined(WANT_MII)
  42. #include <miiphy.h>
  43. #endif
  44. #if defined(CONFIG_RMII) && !defined(WANT_MII)
  45. #error RMII support is unusable without a working PHY.
  46. #endif
  47. #ifdef CFG_DISCOVER_PHY
  48. static int mii_discover_phy(struct eth_device *dev);
  49. #endif
  50. static struct ether_fcc_info_s
  51. {
  52. int ether_index;
  53. int fecp_offset;
  54. int bd_offset;
  55. int phy_addr;
  56. int actual_phy_addr;
  57. }
  58. ether_fcc_info[] = {
  59. #if defined(CONFIG_ETHER_ON_FEC1)
  60. {
  61. 0,
  62. offsetof(immap_t, im_cpm.cp_fec1),
  63. CPM_FEC_BASE,
  64. #if defined(CONFIG_FEC1_PHY)
  65. CONFIG_FEC1_PHY,
  66. #else
  67. -1, /* discover */
  68. #endif
  69. -1,
  70. },
  71. #endif
  72. #if defined(CONFIG_ETHER_ON_FEC2)
  73. {
  74. 1,
  75. offsetof(immap_t, im_cpm.cp_fec2),
  76. CPM_FEC_BASE + 0x50,
  77. #if defined(CONFIG_FEC2_PHY)
  78. CONFIG_FEC2_PHY,
  79. #else
  80. -1,
  81. #endif
  82. -1,
  83. },
  84. #endif
  85. };
  86. /* Ethernet Transmit and Receive Buffers */
  87. #define DBUF_LENGTH 1520
  88. #define TX_BUF_CNT 2
  89. #define TOUT_LOOP 100
  90. #define PKT_MAXBUF_SIZE 1518
  91. #define PKT_MINBUF_SIZE 64
  92. #define PKT_MAXBLR_SIZE 1520
  93. #ifdef __GNUC__
  94. static char txbuf[DBUF_LENGTH] __attribute__ ((aligned(8)));
  95. #else
  96. #error txbuf must be aligned.
  97. #endif
  98. static uint rxIdx; /* index of the current RX buffer */
  99. static uint txIdx; /* index of the current TX buffer */
  100. /*
  101. * FEC Ethernet Tx and Rx buffer descriptors allocated at the
  102. * immr->udata_bd address on Dual-Port RAM
  103. * Provide for Double Buffering
  104. */
  105. typedef volatile struct CommonBufferDescriptor {
  106. cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
  107. cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
  108. } RTXBD;
  109. static RTXBD *rtx = NULL;
  110. static int fec_send(struct eth_device* dev, volatile void *packet, int length);
  111. static int fec_recv(struct eth_device* dev);
  112. static int fec_init(struct eth_device* dev, bd_t * bd);
  113. static void fec_halt(struct eth_device* dev);
  114. int fec_initialize(bd_t *bis)
  115. {
  116. struct eth_device* dev;
  117. struct ether_fcc_info_s *efis;
  118. int i;
  119. for (i = 0; i < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); i++) {
  120. dev = malloc(sizeof(*dev));
  121. if (dev == NULL)
  122. hang();
  123. memset(dev, 0, sizeof(*dev));
  124. /* for FEC1 make sure that the name of the interface is the same
  125. as the old one for compatibility reasons */
  126. if (i == 0) {
  127. sprintf (dev->name, "FEC ETHERNET");
  128. } else {
  129. sprintf (dev->name, "FEC%d ETHERNET",
  130. ether_fcc_info[i].ether_index + 1);
  131. }
  132. efis = &ether_fcc_info[i];
  133. /*
  134. * reset actual phy addr
  135. */
  136. efis->actual_phy_addr = -1;
  137. dev->priv = efis;
  138. dev->init = fec_init;
  139. dev->halt = fec_halt;
  140. dev->send = fec_send;
  141. dev->recv = fec_recv;
  142. eth_register(dev);
  143. }
  144. return 1;
  145. }
  146. static int fec_send(struct eth_device* dev, volatile void *packet, int length)
  147. {
  148. int j, rc;
  149. struct ether_fcc_info_s *efis = dev->priv;
  150. volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
  151. /* section 16.9.23.3
  152. * Wait for ready
  153. */
  154. j = 0;
  155. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
  156. udelay(1);
  157. j++;
  158. }
  159. if (j>=TOUT_LOOP) {
  160. printf("TX not ready\n");
  161. }
  162. rtx->txbd[txIdx].cbd_bufaddr = (uint)packet;
  163. rtx->txbd[txIdx].cbd_datlen = length;
  164. rtx->txbd[txIdx].cbd_sc |= BD_ENET_TX_READY | BD_ENET_TX_LAST;
  165. __asm__ ("eieio");
  166. /* Activate transmit Buffer Descriptor polling */
  167. fecp->fec_x_des_active = 0x01000000; /* Descriptor polling active */
  168. j = 0;
  169. while ((rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_READY) && (j<TOUT_LOOP)) {
  170. #if defined(CONFIG_ICU862)
  171. udelay(10);
  172. #else
  173. udelay(1);
  174. #endif
  175. j++;
  176. }
  177. if (j>=TOUT_LOOP) {
  178. printf("TX timeout\n");
  179. }
  180. #ifdef ET_DEBUG
  181. printf("%s[%d] %s: cycles: %d status: %x retry cnt: %d\n",
  182. __FILE__,__LINE__,__FUNCTION__,j,rtx->txbd[txIdx].cbd_sc,
  183. (rtx->txbd[txIdx].cbd_sc & 0x003C)>>2);
  184. #endif
  185. /* return only status bits */;
  186. rc = (rtx->txbd[txIdx].cbd_sc & BD_ENET_TX_STATS);
  187. txIdx = (txIdx + 1) % TX_BUF_CNT;
  188. return rc;
  189. }
  190. static int fec_recv (struct eth_device *dev)
  191. {
  192. struct ether_fcc_info_s *efis = dev->priv;
  193. volatile fec_t *fecp =
  194. (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
  195. int length;
  196. for (;;) {
  197. /* section 16.9.23.2 */
  198. if (rtx->rxbd[rxIdx].cbd_sc & BD_ENET_RX_EMPTY) {
  199. length = -1;
  200. break; /* nothing received - leave for() loop */
  201. }
  202. length = rtx->rxbd[rxIdx].cbd_datlen;
  203. if (rtx->rxbd[rxIdx].cbd_sc & 0x003f) {
  204. #ifdef ET_DEBUG
  205. printf ("%s[%d] err: %x\n",
  206. __FUNCTION__, __LINE__,
  207. rtx->rxbd[rxIdx].cbd_sc);
  208. #endif
  209. } else {
  210. volatile uchar *rx = NetRxPackets[rxIdx];
  211. length -= 4;
  212. #if (CONFIG_COMMANDS & CFG_CMD_CDP)
  213. if ((rx[0] & 1) != 0
  214. && memcmp ((uchar *) rx, NetBcastAddr, 6) != 0
  215. && memcmp ((uchar *) rx, NetCDPAddr, 6) != 0)
  216. rx = NULL;
  217. #endif
  218. /*
  219. * Pass the packet up to the protocol layers.
  220. */
  221. if (rx != NULL)
  222. NetReceive (rx, length);
  223. }
  224. /* Give the buffer back to the FEC. */
  225. rtx->rxbd[rxIdx].cbd_datlen = 0;
  226. /* wrap around buffer index when necessary */
  227. if ((rxIdx + 1) >= PKTBUFSRX) {
  228. rtx->rxbd[PKTBUFSRX - 1].cbd_sc =
  229. (BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  230. rxIdx = 0;
  231. } else {
  232. rtx->rxbd[rxIdx].cbd_sc = BD_ENET_RX_EMPTY;
  233. rxIdx++;
  234. }
  235. __asm__ ("eieio");
  236. /* Try to fill Buffer Descriptors */
  237. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  238. }
  239. return length;
  240. }
  241. /**************************************************************
  242. *
  243. * FEC Ethernet Initialization Routine
  244. *
  245. *************************************************************/
  246. #define FEC_ECNTRL_PINMUX 0x00000004
  247. #define FEC_ECNTRL_ETHER_EN 0x00000002
  248. #define FEC_ECNTRL_RESET 0x00000001
  249. #define FEC_RCNTRL_BC_REJ 0x00000010
  250. #define FEC_RCNTRL_PROM 0x00000008
  251. #define FEC_RCNTRL_MII_MODE 0x00000004
  252. #define FEC_RCNTRL_DRT 0x00000002
  253. #define FEC_RCNTRL_LOOP 0x00000001
  254. #define FEC_TCNTRL_FDEN 0x00000004
  255. #define FEC_TCNTRL_HBC 0x00000002
  256. #define FEC_TCNTRL_GTS 0x00000001
  257. #define FEC_RESET_DELAY 50
  258. #if defined(CONFIG_RMII)
  259. static inline void fec_10Mbps(struct eth_device *dev)
  260. {
  261. struct ether_fcc_info_s *efis = dev->priv;
  262. int fecidx = efis->ether_index;
  263. uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
  264. if ((unsigned int)fecidx >= 2)
  265. hang();
  266. ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr |= mask;
  267. }
  268. static inline void fec_100Mbps(struct eth_device *dev)
  269. {
  270. struct ether_fcc_info_s *efis = dev->priv;
  271. int fecidx = efis->ether_index;
  272. uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
  273. if ((unsigned int)fecidx >= 2)
  274. hang();
  275. ((volatile immap_t *)CFG_IMMR)->im_cpm.cp_cptr &= ~mask;
  276. }
  277. #endif
  278. static inline void fec_full_duplex(struct eth_device *dev)
  279. {
  280. struct ether_fcc_info_s *efis = dev->priv;
  281. volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
  282. fecp->fec_r_cntrl &= ~FEC_RCNTRL_DRT;
  283. fecp->fec_x_cntrl |= FEC_TCNTRL_FDEN; /* FD enable */
  284. }
  285. static inline void fec_half_duplex(struct eth_device *dev)
  286. {
  287. struct ether_fcc_info_s *efis = dev->priv;
  288. volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
  289. fecp->fec_r_cntrl |= FEC_RCNTRL_DRT;
  290. fecp->fec_x_cntrl &= ~FEC_TCNTRL_FDEN; /* FD disable */
  291. }
  292. static void fec_pin_init(int fecidx)
  293. {
  294. DECLARE_GLOBAL_DATA_PTR;
  295. bd_t *bd = gd->bd;
  296. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  297. volatile fec_t *fecp;
  298. /*
  299. * only two FECs please
  300. */
  301. if ((unsigned int)fecidx >= 2)
  302. hang();
  303. if (fecidx == 0)
  304. fecp = &immr->im_cpm.cp_fec1;
  305. else
  306. fecp = &immr->im_cpm.cp_fec2;
  307. /*
  308. * Set MII speed to 2.5 MHz or slightly below.
  309. * * According to the MPC860T (Rev. D) Fast ethernet controller user
  310. * * manual (6.2.14),
  311. * * the MII management interface clock must be less than or equal
  312. * * to 2.5 MHz.
  313. * * This MDC frequency is equal to system clock / (2 * MII_SPEED).
  314. * * Then MII_SPEED = system_clock / 2 * 2,5 Mhz.
  315. */
  316. fecp->fec_mii_speed = ((bd->bi_intfreq + 4999999) / 5000000) << 1;
  317. #if defined(CONFIG_DUET) && defined(WANT_MII)
  318. /* use MDC for MII */
  319. immr->im_ioport.iop_pdpar |= 0x0080;
  320. immr->im_ioport.iop_pddir &= ~0x0080;
  321. #endif
  322. if (fecidx == 0) {
  323. #if defined(CONFIG_ETHER_ON_FEC1)
  324. #if defined(CONFIG_DUET) /* MPC87x/88x have got 2 FECs and different pinout */
  325. #if !defined(CONFIG_RMII)
  326. immr->im_ioport.iop_papar |= 0xf830;
  327. immr->im_ioport.iop_padir |= 0x0830;
  328. immr->im_ioport.iop_padir &= ~0xf000;
  329. immr->im_cpm.cp_pbpar |= 0x00001001;
  330. immr->im_cpm.cp_pbdir &= ~0x00001001;
  331. immr->im_ioport.iop_pcpar |= 0x000c;
  332. immr->im_ioport.iop_pcdir &= ~0x000c;
  333. immr->im_cpm.cp_pepar |= 0x00000003;
  334. immr->im_cpm.cp_pedir |= 0x00000003;
  335. immr->im_cpm.cp_peso &= ~0x00000003;
  336. immr->im_cpm.cp_cptr &= ~0x00000100;
  337. #else
  338. #if !defined(CONFIG_FEC1_PHY_NORXERR)
  339. immr->im_ioport.iop_papar |= 0x1000;
  340. immr->im_ioport.iop_padir &= ~0x1000;
  341. #endif
  342. immr->im_ioport.iop_papar |= 0xe810;
  343. immr->im_ioport.iop_padir |= 0x0810;
  344. immr->im_ioport.iop_padir &= ~0xe000;
  345. immr->im_cpm.cp_pbpar |= 0x00000001;
  346. immr->im_cpm.cp_pbdir &= ~0x00000001;
  347. immr->im_cpm.cp_cptr |= 0x00000100;
  348. immr->im_cpm.cp_cptr &= ~0x00000050;
  349. #endif /* !CONFIG_RMII */
  350. #elif !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210)
  351. /*
  352. * Configure all of port D for MII.
  353. */
  354. immr->im_ioport.iop_pdpar = 0x1fff;
  355. /*
  356. * Bits moved from Rev. D onward
  357. */
  358. if ((get_immr(0) & 0xffff) < 0x0501)
  359. immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  360. else
  361. immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  362. #else
  363. /*
  364. * Configure port A for MII.
  365. */
  366. #if defined(CONFIG_ICU862) && defined(CFG_DISCOVER_PHY)
  367. /*
  368. * On the ICU862 board the MII-MDC pin is routed to PD8 pin
  369. * * of CPU, so for this board we need to configure Utopia and
  370. * * enable PD8 to MII-MDC function
  371. */
  372. immr->im_ioport.iop_pdpar |= 0x4080;
  373. #endif
  374. /*
  375. * Has Utopia been configured?
  376. */
  377. if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
  378. /*
  379. * YES - Use MUXED mode for UTOPIA bus.
  380. * This frees Port A for use by MII (see 862UM table 41-6).
  381. */
  382. immr->im_ioport.utmode &= ~0x80;
  383. } else {
  384. /*
  385. * NO - set SPLIT mode for UTOPIA bus.
  386. *
  387. * This doesn't really effect UTOPIA (which isn't
  388. * enabled anyway) but just tells the 862
  389. * to use port A for MII (see 862UM table 41-6).
  390. */
  391. immr->im_ioport.utmode |= 0x80;
  392. }
  393. #endif /* !defined(CONFIG_ICU862) */
  394. #endif /* CONFIG_ETHER_ON_FEC1 */
  395. } else if (fecidx == 1) {
  396. #if defined(CONFIG_ETHER_ON_FEC2)
  397. #if defined(CONFIG_DUET) /* MPC87x/88x have got 2 FECs and different pinout */
  398. #if !defined(CONFIG_RMII)
  399. #warning this configuration is not tested; please report if it works
  400. immr->im_cpm.cp_pepar |= 0x0003fffc;
  401. immr->im_cpm.cp_pedir |= 0x0003fffc;
  402. immr->im_cpm.cp_peso &= ~0x000087fc;
  403. immr->im_cpm.cp_peso |= 0x00037800;
  404. immr->im_cpm.cp_cptr &= ~0x00000080;
  405. #else
  406. #if !defined(CONFIG_FEC2_PHY_NORXERR)
  407. immr->im_cpm.cp_pepar |= 0x00000010;
  408. immr->im_cpm.cp_pedir |= 0x00000010;
  409. immr->im_cpm.cp_peso &= ~0x00000010;
  410. #endif
  411. immr->im_cpm.cp_pepar |= 0x00039620;
  412. immr->im_cpm.cp_pedir |= 0x00039620;
  413. immr->im_cpm.cp_peso |= 0x00031000;
  414. immr->im_cpm.cp_peso &= ~0x00008620;
  415. immr->im_cpm.cp_cptr |= 0x00000080;
  416. immr->im_cpm.cp_cptr &= ~0x00000028;
  417. #endif /* CONFIG_RMII */
  418. #endif /* CONFIG_DUET */
  419. #endif /* CONFIG_ETHER_ON_FEC2 */
  420. }
  421. }
  422. static int fec_init (struct eth_device *dev, bd_t * bd)
  423. {
  424. struct ether_fcc_info_s *efis = dev->priv;
  425. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  426. volatile fec_t *fecp =
  427. (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
  428. int i;
  429. if (efis->ether_index == 0) {
  430. #if defined(CONFIG_FADS) /* FADS family uses FPGA (BCSR) to control PHYs */
  431. #if defined(CONFIG_DUET_ADS)
  432. *(vu_char *) BCSR5 &= ~(BCSR5_MII1_EN | BCSR5_MII1_RST);
  433. #else
  434. /* configure FADS for fast (FEC) ethernet, half-duplex */
  435. /* The LXT970 needs about 50ms to recover from reset, so
  436. * wait for it by discovering the PHY before leaving eth_init().
  437. */
  438. {
  439. volatile uint *bcsr4 = (volatile uint *) BCSR4;
  440. *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1))
  441. | (BCSR4_FETHCFG0 | BCSR4_FETHFDE |
  442. BCSR4_FETHRST);
  443. /* reset the LXT970 PHY */
  444. *bcsr4 &= ~BCSR4_FETHRST;
  445. udelay (10);
  446. *bcsr4 |= BCSR4_FETHRST;
  447. udelay (10);
  448. }
  449. #endif /* CONFIG_DUET_ADS */
  450. #endif /* CONFIG_FADS */
  451. }
  452. /* Whack a reset.
  453. * A delay is required between a reset of the FEC block and
  454. * initialization of other FEC registers because the reset takes
  455. * some time to complete. If you don't delay, subsequent writes
  456. * to FEC registers might get killed by the reset routine which is
  457. * still in progress.
  458. */
  459. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  460. for (i = 0;
  461. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  462. ++i) {
  463. udelay (1);
  464. }
  465. if (i == FEC_RESET_DELAY) {
  466. printf ("FEC_RESET_DELAY timeout\n");
  467. return 0;
  468. }
  469. /* We use strictly polling mode only
  470. */
  471. fecp->fec_imask = 0;
  472. /* Clear any pending interrupt
  473. */
  474. fecp->fec_ievent = 0xffc0;
  475. /* No need to set the IVEC register */
  476. /* Set station address
  477. */
  478. #define ea eth_get_dev()->enetaddr
  479. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
  480. fecp->fec_addr_high = (ea[4] << 8) | (ea[5]);
  481. #undef ea
  482. #if (CONFIG_COMMANDS & CFG_CMD_CDP)
  483. /*
  484. * Turn on multicast address hash table
  485. */
  486. fecp->fec_hash_table_high = 0xffffffff;
  487. fecp->fec_hash_table_low = 0xffffffff;
  488. #else
  489. /* Clear multicast address hash table
  490. */
  491. fecp->fec_hash_table_high = 0;
  492. fecp->fec_hash_table_low = 0;
  493. #endif
  494. /* Set maximum receive buffer size.
  495. */
  496. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  497. /* Set maximum frame length
  498. */
  499. fecp->fec_r_hash = PKT_MAXBUF_SIZE;
  500. /*
  501. * Setup Buffers and Buffer Desriptors
  502. */
  503. rxIdx = 0;
  504. txIdx = 0;
  505. if (!rtx) {
  506. #ifdef CFG_ALLOC_DPRAM
  507. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
  508. dpram_alloc_align (sizeof (RTXBD), 8));
  509. #else
  510. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
  511. #endif
  512. }
  513. /*
  514. * Setup Receiver Buffer Descriptors (13.14.24.18)
  515. * Settings:
  516. * Empty, Wrap
  517. */
  518. for (i = 0; i < PKTBUFSRX; i++) {
  519. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  520. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  521. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  522. }
  523. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  524. /*
  525. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  526. * Settings:
  527. * Last, Tx CRC
  528. */
  529. for (i = 0; i < TX_BUF_CNT; i++) {
  530. rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  531. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  532. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  533. }
  534. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  535. /* Set receive and transmit descriptor base
  536. */
  537. fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
  538. fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
  539. /* Enable MII mode
  540. */
  541. #if 0 /* Full duplex mode */
  542. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
  543. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
  544. #else /* Half duplex mode */
  545. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  546. fecp->fec_x_cntrl = 0;
  547. #endif
  548. /* Enable big endian and don't care about SDMA FC.
  549. */
  550. fecp->fec_fun_code = 0x78000000;
  551. /*
  552. * Setup the pin configuration of the FEC
  553. */
  554. fec_pin_init (efis->ether_index);
  555. rxIdx = 0;
  556. txIdx = 0;
  557. /*
  558. * Now enable the transmit and receive processing
  559. */
  560. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  561. if (efis->phy_addr == -1) {
  562. #ifdef CFG_DISCOVER_PHY
  563. /*
  564. * wait for the PHY to wake up after reset
  565. */
  566. efis->actual_phy_addr = mii_discover_phy (dev);
  567. #else
  568. efis->actual_phy_addr = -1;
  569. #endif
  570. if (efis->actual_phy_addr == -1) {
  571. printf ("Unable to discover phy!\n");
  572. return 0;
  573. }
  574. } else {
  575. efis->actual_phy_addr = efis->phy_addr;
  576. }
  577. #if defined(CONFIG_MII) && defined(CONFIG_RMII)
  578. /*
  579. * adapt the RMII speed to the speed of the phy
  580. */
  581. if (miiphy_speed (efis->actual_phy_addr) == _100BASET) {
  582. fec_100Mbps (dev);
  583. } else {
  584. fec_10Mbps (dev);
  585. }
  586. #endif
  587. #if defined(CONFIG_MII)
  588. /*
  589. * adapt to the half/full speed settings
  590. */
  591. if (miiphy_duplex (efis->actual_phy_addr) == FULL) {
  592. fec_full_duplex (dev);
  593. } else {
  594. fec_half_duplex (dev);
  595. }
  596. #endif
  597. /* And last, try to fill Rx Buffer Descriptors */
  598. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  599. return 1;
  600. }
  601. static void fec_halt(struct eth_device* dev)
  602. {
  603. #if 0
  604. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  605. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  606. #endif
  607. }
  608. #if 0
  609. void restart(void)
  610. {
  611. volatile immap_t *immr = (immap_t *)CFG_IMMR;
  612. immr->im_cpm.cp_scc[SCC_ENET].scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
  613. }
  614. #endif
  615. #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  616. /* Make MII read/write commands for the FEC.
  617. */
  618. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  619. (REG & 0x1f) << 18))
  620. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  621. (REG & 0x1f) << 18) | \
  622. (VAL & 0xffff))
  623. /* Interrupt events/masks.
  624. */
  625. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  626. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  627. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  628. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  629. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  630. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  631. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  632. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  633. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  634. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  635. /* PHY identification
  636. */
  637. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  638. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  639. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  640. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  641. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  642. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  643. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  644. #define PHY_ID_DM9161 0x0181B880 /* Davicom DM9161 */
  645. /* send command to phy using mii, wait for result */
  646. static uint
  647. mii_send(uint mii_cmd)
  648. {
  649. uint mii_reply;
  650. volatile fec_t *ep;
  651. ep = &(((immap_t *)CFG_IMMR)->im_cpm.cp_fec);
  652. ep->fec_mii_data = mii_cmd; /* command to phy */
  653. /* wait for mii complete */
  654. while (!(ep->fec_ievent & FEC_ENET_MII))
  655. ; /* spin until done */
  656. mii_reply = ep->fec_mii_data; /* result from phy */
  657. ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
  658. #if 0
  659. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  660. __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
  661. #endif
  662. return (mii_reply & 0xffff); /* data read from phy */
  663. }
  664. #endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CFG_CMD_MII) */
  665. #if defined(CFG_DISCOVER_PHY)
  666. static int mii_discover_phy(struct eth_device *dev)
  667. {
  668. #define MAX_PHY_PASSES 11
  669. uint phyno;
  670. int pass;
  671. uint phytype;
  672. int phyaddr;
  673. phyaddr = -1; /* didn't find a PHY yet */
  674. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  675. if (pass > 1) {
  676. /* PHY may need more time to recover from reset.
  677. * The LXT970 needs 50ms typical, no maximum is
  678. * specified, so wait 10ms before try again.
  679. * With 11 passes this gives it 100ms to wake up.
  680. */
  681. udelay(10000); /* wait 10ms */
  682. }
  683. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  684. phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR1));
  685. #ifdef ET_DEBUG
  686. printf("PHY type 0x%x pass %d type ", phytype, pass);
  687. #endif
  688. if (phytype != 0xffff) {
  689. phyaddr = phyno;
  690. phytype <<= 16;
  691. phytype |= mii_send(mk_mii_read(phyno,
  692. PHY_PHYIDR2));
  693. #ifdef ET_DEBUG
  694. printf("PHY @ 0x%x pass %d type ",phyno,pass);
  695. switch (phytype & 0xfffffff0) {
  696. case PHY_ID_LXT970:
  697. printf("LXT970\n");
  698. break;
  699. case PHY_ID_LXT971:
  700. printf("LXT971\n");
  701. break;
  702. case PHY_ID_82555:
  703. printf("82555\n");
  704. break;
  705. case PHY_ID_QS6612:
  706. printf("QS6612\n");
  707. break;
  708. case PHY_ID_AMD79C784:
  709. printf("AMD79C784\n");
  710. break;
  711. case PHY_ID_LSI80225B:
  712. printf("LSI L80225/B\n");
  713. break;
  714. case PHY_ID_DM9161:
  715. printf("Davicom DM9161\n");
  716. break;
  717. default:
  718. printf("0x%08x\n", phytype);
  719. break;
  720. }
  721. #endif
  722. }
  723. }
  724. }
  725. if (phyaddr < 0) {
  726. printf("No PHY device found.\n");
  727. }
  728. return phyaddr;
  729. }
  730. #endif /* CFG_DISCOVER_PHY */
  731. #if (defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
  732. static int mii_init_done = 0;
  733. /****************************************************************************
  734. * mii_init -- Initialize the MII for MII command without ethernet
  735. * This function is a subset of eth_init
  736. ****************************************************************************
  737. */
  738. void mii_init (void)
  739. {
  740. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  741. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  742. int i, j;
  743. if (mii_init_done != 0) {
  744. return;
  745. }
  746. for (j = 0; j < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); j++) {
  747. /* Whack a reset.
  748. * A delay is required between a reset of the FEC block and
  749. * initialization of other FEC registers because the reset takes
  750. * some time to complete. If you don't delay, subsequent writes
  751. * to FEC registers might get killed by the reset routine which is
  752. * still in progress.
  753. */
  754. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  755. for (i = 0;
  756. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  757. ++i) {
  758. udelay (1);
  759. }
  760. if (i == FEC_RESET_DELAY) {
  761. printf ("FEC_RESET_DELAY timeout\n");
  762. return;
  763. }
  764. /* We use strictly polling mode only
  765. */
  766. fecp->fec_imask = 0;
  767. /* Clear any pending interrupt
  768. */
  769. fecp->fec_ievent = 0xffc0;
  770. /* Setup the pin configuration of the FEC(s)
  771. */
  772. fec_pin_init(ether_fcc_info[i].ether_index);
  773. /* Now enable the transmit and receive processing
  774. */
  775. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  776. }
  777. mii_init_done = 1;
  778. }
  779. /*****************************************************************************
  780. * Read and write a MII PHY register, routines used by MII Utilities
  781. *
  782. * FIXME: These routines are expected to return 0 on success, but mii_send
  783. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  784. * no PHY connected...
  785. * For now always return 0.
  786. * FIXME: These routines only work after calling eth_init() at least once!
  787. * Otherwise they hang in mii_send() !!! Sorry!
  788. *****************************************************************************/
  789. int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
  790. {
  791. short rdreg; /* register working value */
  792. #ifdef MII_DEBUG
  793. printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  794. #endif
  795. rdreg = mii_send(mk_mii_read(addr, reg));
  796. *value = rdreg;
  797. #ifdef MII_DEBUG
  798. printf ("0x%04x\n", *value);
  799. #endif
  800. return 0;
  801. }
  802. int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
  803. {
  804. short rdreg; /* register working value */
  805. #ifdef MII_DEBUG
  806. printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  807. #endif
  808. rdreg = mii_send(mk_mii_write(addr, reg, value));
  809. #ifdef MII_DEBUG
  810. printf ("0x%04x\n", value);
  811. #endif
  812. return 0;
  813. }
  814. #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)*/
  815. #endif /* CFG_CMD_NET, FEC_ENET */