fec.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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 phy_addr;
  55. int actual_phy_addr;
  56. int initialized;
  57. }
  58. ether_fcc_info[] = {
  59. #if defined(CONFIG_ETHER_ON_FEC1)
  60. {
  61. 0,
  62. offsetof(immap_t, im_cpm.cp_fec1),
  63. #if defined(CONFIG_FEC1_PHY)
  64. CONFIG_FEC1_PHY,
  65. #else
  66. -1, /* discover */
  67. #endif
  68. -1,
  69. 0,
  70. },
  71. #endif
  72. #if defined(CONFIG_ETHER_ON_FEC2)
  73. {
  74. 1,
  75. offsetof(immap_t, im_cpm.cp_fec2),
  76. #if defined(CONFIG_FEC2_PHY)
  77. CONFIG_FEC2_PHY,
  78. #else
  79. -1,
  80. #endif
  81. -1,
  82. 0,
  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_NETTA) || defined(CONFIG_NETPHONE)
  318. /* our PHYs are the limit at 2.5 MHz */
  319. fecp->fec_mii_speed <<= 1;
  320. #endif
  321. #if defined(CONFIG_DUET) && defined(WANT_MII)
  322. /* use MDC for MII */
  323. immr->im_ioport.iop_pdpar |= 0x0080;
  324. immr->im_ioport.iop_pddir &= ~0x0080;
  325. #endif
  326. if (fecidx == 0) {
  327. #if defined(CONFIG_ETHER_ON_FEC1)
  328. #if defined(CONFIG_DUET) /* MPC87x/88x have got 2 FECs and different pinout */
  329. #if !defined(CONFIG_RMII)
  330. immr->im_ioport.iop_papar |= 0xf830;
  331. immr->im_ioport.iop_padir |= 0x0830;
  332. immr->im_ioport.iop_padir &= ~0xf000;
  333. immr->im_cpm.cp_pbpar |= 0x00001001;
  334. immr->im_cpm.cp_pbdir &= ~0x00001001;
  335. immr->im_ioport.iop_pcpar |= 0x000c;
  336. immr->im_ioport.iop_pcdir &= ~0x000c;
  337. immr->im_cpm.cp_pepar |= 0x00000003;
  338. immr->im_cpm.cp_pedir |= 0x00000003;
  339. immr->im_cpm.cp_peso &= ~0x00000003;
  340. immr->im_cpm.cp_cptr &= ~0x00000100;
  341. #else
  342. #if !defined(CONFIG_FEC1_PHY_NORXERR)
  343. immr->im_ioport.iop_papar |= 0x1000;
  344. immr->im_ioport.iop_padir &= ~0x1000;
  345. #endif
  346. immr->im_ioport.iop_papar |= 0xe810;
  347. immr->im_ioport.iop_padir |= 0x0810;
  348. immr->im_ioport.iop_padir &= ~0xe000;
  349. immr->im_cpm.cp_pbpar |= 0x00000001;
  350. immr->im_cpm.cp_pbdir &= ~0x00000001;
  351. immr->im_cpm.cp_cptr |= 0x00000100;
  352. immr->im_cpm.cp_cptr &= ~0x00000050;
  353. #endif /* !CONFIG_RMII */
  354. #elif !defined(CONFIG_ICU862) && !defined(CONFIG_IAD210)
  355. /*
  356. * Configure all of port D for MII.
  357. */
  358. immr->im_ioport.iop_pdpar = 0x1fff;
  359. /*
  360. * Bits moved from Rev. D onward
  361. */
  362. if ((get_immr(0) & 0xffff) < 0x0501)
  363. immr->im_ioport.iop_pddir = 0x1c58; /* Pre rev. D */
  364. else
  365. immr->im_ioport.iop_pddir = 0x1fff; /* Rev. D and later */
  366. #else
  367. /*
  368. * Configure port A for MII.
  369. */
  370. #if defined(CONFIG_ICU862) && defined(CFG_DISCOVER_PHY)
  371. /*
  372. * On the ICU862 board the MII-MDC pin is routed to PD8 pin
  373. * * of CPU, so for this board we need to configure Utopia and
  374. * * enable PD8 to MII-MDC function
  375. */
  376. immr->im_ioport.iop_pdpar |= 0x4080;
  377. #endif
  378. /*
  379. * Has Utopia been configured?
  380. */
  381. if (immr->im_ioport.iop_pdpar & (0x8000 >> 1)) {
  382. /*
  383. * YES - Use MUXED mode for UTOPIA bus.
  384. * This frees Port A for use by MII (see 862UM table 41-6).
  385. */
  386. immr->im_ioport.utmode &= ~0x80;
  387. } else {
  388. /*
  389. * NO - set SPLIT mode for UTOPIA bus.
  390. *
  391. * This doesn't really effect UTOPIA (which isn't
  392. * enabled anyway) but just tells the 862
  393. * to use port A for MII (see 862UM table 41-6).
  394. */
  395. immr->im_ioport.utmode |= 0x80;
  396. }
  397. #endif /* !defined(CONFIG_ICU862) */
  398. #endif /* CONFIG_ETHER_ON_FEC1 */
  399. } else if (fecidx == 1) {
  400. #if defined(CONFIG_ETHER_ON_FEC2)
  401. #if defined(CONFIG_DUET) /* MPC87x/88x have got 2 FECs and different pinout */
  402. #if !defined(CONFIG_RMII)
  403. #warning this configuration is not tested; please report if it works
  404. immr->im_cpm.cp_pepar |= 0x0003fffc;
  405. immr->im_cpm.cp_pedir |= 0x0003fffc;
  406. immr->im_cpm.cp_peso &= ~0x000087fc;
  407. immr->im_cpm.cp_peso |= 0x00037800;
  408. immr->im_cpm.cp_cptr &= ~0x00000080;
  409. #else
  410. #if !defined(CONFIG_FEC2_PHY_NORXERR)
  411. immr->im_cpm.cp_pepar |= 0x00000010;
  412. immr->im_cpm.cp_pedir |= 0x00000010;
  413. immr->im_cpm.cp_peso &= ~0x00000010;
  414. #endif
  415. immr->im_cpm.cp_pepar |= 0x00039620;
  416. immr->im_cpm.cp_pedir |= 0x00039620;
  417. immr->im_cpm.cp_peso |= 0x00031000;
  418. immr->im_cpm.cp_peso &= ~0x00008620;
  419. immr->im_cpm.cp_cptr |= 0x00000080;
  420. immr->im_cpm.cp_cptr &= ~0x00000028;
  421. #endif /* CONFIG_RMII */
  422. #endif /* CONFIG_DUET */
  423. #endif /* CONFIG_ETHER_ON_FEC2 */
  424. }
  425. }
  426. static int fec_init (struct eth_device *dev, bd_t * bd)
  427. {
  428. struct ether_fcc_info_s *efis = dev->priv;
  429. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  430. volatile fec_t *fecp =
  431. (volatile fec_t *) (CFG_IMMR + efis->fecp_offset);
  432. int i;
  433. if (efis->ether_index == 0) {
  434. #if defined(CONFIG_FADS) /* FADS family uses FPGA (BCSR) to control PHYs */
  435. #if defined(CONFIG_DUET_ADS)
  436. *(vu_char *) BCSR5 &= ~(BCSR5_MII1_EN | BCSR5_MII1_RST);
  437. #else
  438. /* configure FADS for fast (FEC) ethernet, half-duplex */
  439. /* The LXT970 needs about 50ms to recover from reset, so
  440. * wait for it by discovering the PHY before leaving eth_init().
  441. */
  442. {
  443. volatile uint *bcsr4 = (volatile uint *) BCSR4;
  444. *bcsr4 = (*bcsr4 & ~(BCSR4_FETH_EN | BCSR4_FETHCFG1))
  445. | (BCSR4_FETHCFG0 | BCSR4_FETHFDE |
  446. BCSR4_FETHRST);
  447. /* reset the LXT970 PHY */
  448. *bcsr4 &= ~BCSR4_FETHRST;
  449. udelay (10);
  450. *bcsr4 |= BCSR4_FETHRST;
  451. udelay (10);
  452. }
  453. #endif /* CONFIG_DUET_ADS */
  454. #endif /* CONFIG_FADS */
  455. }
  456. /* Whack a reset.
  457. * A delay is required between a reset of the FEC block and
  458. * initialization of other FEC registers because the reset takes
  459. * some time to complete. If you don't delay, subsequent writes
  460. * to FEC registers might get killed by the reset routine which is
  461. * still in progress.
  462. */
  463. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  464. for (i = 0;
  465. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  466. ++i) {
  467. udelay (1);
  468. }
  469. if (i == FEC_RESET_DELAY) {
  470. printf ("FEC_RESET_DELAY timeout\n");
  471. return 0;
  472. }
  473. /* We use strictly polling mode only
  474. */
  475. fecp->fec_imask = 0;
  476. /* Clear any pending interrupt
  477. */
  478. fecp->fec_ievent = 0xffc0;
  479. /* No need to set the IVEC register */
  480. /* Set station address
  481. */
  482. #define ea eth_get_dev()->enetaddr
  483. fecp->fec_addr_low = (ea[0] << 24) | (ea[1] << 16) | (ea[2] << 8) | (ea[3]);
  484. fecp->fec_addr_high = (ea[4] << 8) | (ea[5]);
  485. #undef ea
  486. #if (CONFIG_COMMANDS & CFG_CMD_CDP)
  487. /*
  488. * Turn on multicast address hash table
  489. */
  490. fecp->fec_hash_table_high = 0xffffffff;
  491. fecp->fec_hash_table_low = 0xffffffff;
  492. #else
  493. /* Clear multicast address hash table
  494. */
  495. fecp->fec_hash_table_high = 0;
  496. fecp->fec_hash_table_low = 0;
  497. #endif
  498. /* Set maximum receive buffer size.
  499. */
  500. fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;
  501. /* Set maximum frame length
  502. */
  503. fecp->fec_r_hash = PKT_MAXBUF_SIZE;
  504. /*
  505. * Setup Buffers and Buffer Desriptors
  506. */
  507. rxIdx = 0;
  508. txIdx = 0;
  509. if (!rtx) {
  510. #ifdef CFG_ALLOC_DPRAM
  511. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem +
  512. dpram_alloc_align (sizeof (RTXBD), 8));
  513. #else
  514. rtx = (RTXBD *) (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
  515. #endif
  516. }
  517. /*
  518. * Setup Receiver Buffer Descriptors (13.14.24.18)
  519. * Settings:
  520. * Empty, Wrap
  521. */
  522. for (i = 0; i < PKTBUFSRX; i++) {
  523. rtx->rxbd[i].cbd_sc = BD_ENET_RX_EMPTY;
  524. rtx->rxbd[i].cbd_datlen = 0; /* Reset */
  525. rtx->rxbd[i].cbd_bufaddr = (uint) NetRxPackets[i];
  526. }
  527. rtx->rxbd[PKTBUFSRX - 1].cbd_sc |= BD_ENET_RX_WRAP;
  528. /*
  529. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  530. * Settings:
  531. * Last, Tx CRC
  532. */
  533. for (i = 0; i < TX_BUF_CNT; i++) {
  534. rtx->txbd[i].cbd_sc = BD_ENET_TX_LAST | BD_ENET_TX_TC;
  535. rtx->txbd[i].cbd_datlen = 0; /* Reset */
  536. rtx->txbd[i].cbd_bufaddr = (uint) (&txbuf[0]);
  537. }
  538. rtx->txbd[TX_BUF_CNT - 1].cbd_sc |= BD_ENET_TX_WRAP;
  539. /* Set receive and transmit descriptor base
  540. */
  541. fecp->fec_r_des_start = (unsigned int) (&rtx->rxbd[0]);
  542. fecp->fec_x_des_start = (unsigned int) (&rtx->txbd[0]);
  543. /* Enable MII mode
  544. */
  545. #if 0 /* Full duplex mode */
  546. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE;
  547. fecp->fec_x_cntrl = FEC_TCNTRL_FDEN;
  548. #else /* Half duplex mode */
  549. fecp->fec_r_cntrl = FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT;
  550. fecp->fec_x_cntrl = 0;
  551. #endif
  552. /* Enable big endian and don't care about SDMA FC.
  553. */
  554. fecp->fec_fun_code = 0x78000000;
  555. /*
  556. * Setup the pin configuration of the FEC
  557. */
  558. fec_pin_init (efis->ether_index);
  559. rxIdx = 0;
  560. txIdx = 0;
  561. /*
  562. * Now enable the transmit and receive processing
  563. */
  564. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  565. if (efis->phy_addr == -1) {
  566. #ifdef CFG_DISCOVER_PHY
  567. /*
  568. * wait for the PHY to wake up after reset
  569. */
  570. efis->actual_phy_addr = mii_discover_phy (dev);
  571. #else
  572. efis->actual_phy_addr = -1;
  573. #endif
  574. if (efis->actual_phy_addr == -1) {
  575. printf ("Unable to discover phy!\n");
  576. return 0;
  577. }
  578. } else {
  579. efis->actual_phy_addr = efis->phy_addr;
  580. }
  581. #if defined(CONFIG_MII) && defined(CONFIG_RMII)
  582. /* the MII interface is connected to FEC1
  583. so for the miiphy_xxx function to work we must
  584. call mii_init since fec_halt messes the thing up */
  585. if (efis->ether_index != 0)
  586. mii_init();
  587. /*
  588. * adapt the RMII speed to the speed of the phy
  589. */
  590. if (miiphy_speed (efis->actual_phy_addr) == _100BASET) {
  591. fec_100Mbps (dev);
  592. } else {
  593. fec_10Mbps (dev);
  594. }
  595. #endif
  596. #if defined(CONFIG_MII)
  597. /*
  598. * adapt to the half/full speed settings
  599. */
  600. if (miiphy_duplex (efis->actual_phy_addr) == FULL) {
  601. fec_full_duplex (dev);
  602. } else {
  603. fec_half_duplex (dev);
  604. }
  605. #endif
  606. /* And last, try to fill Rx Buffer Descriptors */
  607. fecp->fec_r_des_active = 0x01000000; /* Descriptor polling active */
  608. efis->initialized = 1;
  609. return 1;
  610. }
  611. static void fec_halt(struct eth_device* dev)
  612. {
  613. struct ether_fcc_info_s *efis = dev->priv;
  614. volatile fec_t *fecp = (volatile fec_t *)(CFG_IMMR + efis->fecp_offset);
  615. int i;
  616. /* avoid halt if initialized; mii gets stuck otherwise */
  617. if (!efis->initialized)
  618. return;
  619. /* Whack a reset.
  620. * A delay is required between a reset of the FEC block and
  621. * initialization of other FEC registers because the reset takes
  622. * some time to complete. If you don't delay, subsequent writes
  623. * to FEC registers might get killed by the reset routine which is
  624. * still in progress.
  625. */
  626. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  627. for (i = 0;
  628. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  629. ++i) {
  630. udelay (1);
  631. }
  632. if (i == FEC_RESET_DELAY) {
  633. printf ("FEC_RESET_DELAY timeout\n");
  634. return;
  635. }
  636. efis->initialized = 0;
  637. }
  638. #if defined(CFG_DISCOVER_PHY) || defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)
  639. /* Make MII read/write commands for the FEC.
  640. */
  641. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  642. (REG & 0x1f) << 18))
  643. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  644. (REG & 0x1f) << 18) | \
  645. (VAL & 0xffff))
  646. /* Interrupt events/masks.
  647. */
  648. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  649. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  650. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  651. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  652. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  653. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  654. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  655. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  656. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  657. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  658. /* PHY identification
  659. */
  660. #define PHY_ID_LXT970 0x78100000 /* LXT970 */
  661. #define PHY_ID_LXT971 0x001378e0 /* LXT971 and 972 */
  662. #define PHY_ID_82555 0x02a80150 /* Intel 82555 */
  663. #define PHY_ID_QS6612 0x01814400 /* QS6612 */
  664. #define PHY_ID_AMD79C784 0x00225610 /* AMD 79C784 */
  665. #define PHY_ID_LSI80225 0x0016f870 /* LSI 80225 */
  666. #define PHY_ID_LSI80225B 0x0016f880 /* LSI 80225/B */
  667. #define PHY_ID_DM9161 0x0181B880 /* Davicom DM9161 */
  668. /* send command to phy using mii, wait for result */
  669. static uint
  670. mii_send(uint mii_cmd)
  671. {
  672. uint mii_reply;
  673. volatile fec_t *ep;
  674. int cnt;
  675. ep = &(((immap_t *)CFG_IMMR)->im_cpm.cp_fec);
  676. ep->fec_mii_data = mii_cmd; /* command to phy */
  677. /* wait for mii complete */
  678. cnt = 0;
  679. while (!(ep->fec_ievent & FEC_ENET_MII)) {
  680. if (++cnt > 1000) {
  681. printf("mii_send STUCK!\n");
  682. break;
  683. }
  684. }
  685. mii_reply = ep->fec_mii_data; /* result from phy */
  686. ep->fec_ievent = FEC_ENET_MII; /* clear MII complete */
  687. #if 0
  688. printf("%s[%d] %s: sent=0x%8.8x, reply=0x%8.8x\n",
  689. __FILE__,__LINE__,__FUNCTION__,mii_cmd,mii_reply);
  690. #endif
  691. return (mii_reply & 0xffff); /* data read from phy */
  692. }
  693. #endif /* CFG_DISCOVER_PHY || (CONFIG_COMMANDS & CFG_CMD_MII) */
  694. #if defined(CFG_DISCOVER_PHY)
  695. static int mii_discover_phy(struct eth_device *dev)
  696. {
  697. #define MAX_PHY_PASSES 11
  698. uint phyno;
  699. int pass;
  700. uint phytype;
  701. int phyaddr;
  702. phyaddr = -1; /* didn't find a PHY yet */
  703. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  704. if (pass > 1) {
  705. /* PHY may need more time to recover from reset.
  706. * The LXT970 needs 50ms typical, no maximum is
  707. * specified, so wait 10ms before try again.
  708. * With 11 passes this gives it 100ms to wake up.
  709. */
  710. udelay(10000); /* wait 10ms */
  711. }
  712. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  713. phytype = mii_send(mk_mii_read(phyno, PHY_PHYIDR1));
  714. #ifdef ET_DEBUG
  715. printf("PHY type 0x%x pass %d type ", phytype, pass);
  716. #endif
  717. if (phytype != 0xffff) {
  718. phyaddr = phyno;
  719. phytype <<= 16;
  720. phytype |= mii_send(mk_mii_read(phyno,
  721. PHY_PHYIDR2));
  722. #ifdef ET_DEBUG
  723. printf("PHY @ 0x%x pass %d type ",phyno,pass);
  724. switch (phytype & 0xfffffff0) {
  725. case PHY_ID_LXT970:
  726. printf("LXT970\n");
  727. break;
  728. case PHY_ID_LXT971:
  729. printf("LXT971\n");
  730. break;
  731. case PHY_ID_82555:
  732. printf("82555\n");
  733. break;
  734. case PHY_ID_QS6612:
  735. printf("QS6612\n");
  736. break;
  737. case PHY_ID_AMD79C784:
  738. printf("AMD79C784\n");
  739. break;
  740. case PHY_ID_LSI80225B:
  741. printf("LSI L80225/B\n");
  742. break;
  743. case PHY_ID_DM9161:
  744. printf("Davicom DM9161\n");
  745. break;
  746. default:
  747. printf("0x%08x\n", phytype);
  748. break;
  749. }
  750. #endif
  751. }
  752. }
  753. }
  754. if (phyaddr < 0) {
  755. printf("No PHY device found.\n");
  756. }
  757. return phyaddr;
  758. }
  759. #endif /* CFG_DISCOVER_PHY */
  760. #if (defined(CONFIG_MII) || (CONFIG_COMMANDS & CFG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
  761. /****************************************************************************
  762. * mii_init -- Initialize the MII for MII command without ethernet
  763. * This function is a subset of eth_init
  764. ****************************************************************************
  765. */
  766. void mii_init (void)
  767. {
  768. volatile immap_t *immr = (immap_t *) CFG_IMMR;
  769. volatile fec_t *fecp = &(immr->im_cpm.cp_fec);
  770. int i, j;
  771. for (j = 0; j < sizeof(ether_fcc_info) / sizeof(ether_fcc_info[0]); j++) {
  772. /* Whack a reset.
  773. * A delay is required between a reset of the FEC block and
  774. * initialization of other FEC registers because the reset takes
  775. * some time to complete. If you don't delay, subsequent writes
  776. * to FEC registers might get killed by the reset routine which is
  777. * still in progress.
  778. */
  779. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET;
  780. for (i = 0;
  781. (fecp->fec_ecntrl & FEC_ECNTRL_RESET) && (i < FEC_RESET_DELAY);
  782. ++i) {
  783. udelay (1);
  784. }
  785. if (i == FEC_RESET_DELAY) {
  786. printf ("FEC_RESET_DELAY timeout\n");
  787. return;
  788. }
  789. /* We use strictly polling mode only
  790. */
  791. fecp->fec_imask = 0;
  792. /* Clear any pending interrupt
  793. */
  794. fecp->fec_ievent = 0xffc0;
  795. /* Setup the pin configuration of the FEC(s)
  796. */
  797. fec_pin_init(ether_fcc_info[i].ether_index);
  798. /* Now enable the transmit and receive processing
  799. */
  800. fecp->fec_ecntrl = FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN;
  801. }
  802. }
  803. /*****************************************************************************
  804. * Read and write a MII PHY register, routines used by MII Utilities
  805. *
  806. * FIXME: These routines are expected to return 0 on success, but mii_send
  807. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  808. * no PHY connected...
  809. * For now always return 0.
  810. * FIXME: These routines only work after calling eth_init() at least once!
  811. * Otherwise they hang in mii_send() !!! Sorry!
  812. *****************************************************************************/
  813. int miiphy_read(unsigned char addr, unsigned char reg, unsigned short *value)
  814. {
  815. short rdreg; /* register working value */
  816. #ifdef MII_DEBUG
  817. printf ("miiphy_read(0x%x) @ 0x%x = ", reg, addr);
  818. #endif
  819. rdreg = mii_send(mk_mii_read(addr, reg));
  820. *value = rdreg;
  821. #ifdef MII_DEBUG
  822. printf ("0x%04x\n", *value);
  823. #endif
  824. return 0;
  825. }
  826. int miiphy_write(unsigned char addr, unsigned char reg, unsigned short value)
  827. {
  828. short rdreg; /* register working value */
  829. #ifdef MII_DEBUG
  830. printf ("miiphy_write(0x%x) @ 0x%x = ", reg, addr);
  831. #endif
  832. rdreg = mii_send(mk_mii_write(addr, reg, value));
  833. #ifdef MII_DEBUG
  834. printf ("0x%04x\n", value);
  835. #endif
  836. return 0;
  837. }
  838. #endif /* (CONFIG_COMMANDS & CFG_CMD_MII) && !defined(CONFIG_BITBANGMII)*/
  839. #endif /* CFG_CMD_NET, FEC_ENET */