fec.c 25 KB

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