fec.c 25 KB

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