fec.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <commproc.h>
  10. #include <malloc.h>
  11. #include <net.h>
  12. #include <asm/io.h>
  13. #include <phy.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. #if defined(CONFIG_CMD_NET) && \
  16. (defined(FEC_ENET) || defined(CONFIG_ETHER_ON_FEC1) || defined(CONFIG_ETHER_ON_FEC2))
  17. /* compatibility test, if only FEC_ENET defined assume ETHER on FEC1 */
  18. #if defined(FEC_ENET) && !defined(CONFIG_ETHER_ON_FEC1) && !defined(CONFIG_ETHER_ON_FEC2)
  19. #define CONFIG_ETHER_ON_FEC1 1
  20. #endif
  21. /* define WANT_MII when MII support is required */
  22. #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_FEC1_PHY) || defined(CONFIG_FEC2_PHY)
  23. #define WANT_MII
  24. #else
  25. #undef WANT_MII
  26. #endif
  27. #if defined(WANT_MII)
  28. #include <miiphy.h>
  29. #if !(defined(CONFIG_MII) || defined(CONFIG_CMD_MII))
  30. #error "CONFIG_MII has to be defined!"
  31. #endif
  32. #endif
  33. #if defined(CONFIG_RMII) && !defined(WANT_MII)
  34. #error RMII support is unusable without a working PHY.
  35. #endif
  36. #ifdef CONFIG_SYS_DISCOVER_PHY
  37. static int mii_discover_phy(struct eth_device *dev);
  38. #endif
  39. int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg);
  40. int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
  41. u16 value);
  42. static struct ether_fcc_info_s
  43. {
  44. int ether_index;
  45. int fecp_offset;
  46. int phy_addr;
  47. int actual_phy_addr;
  48. int initialized;
  49. }
  50. ether_fcc_info[] = {
  51. #if defined(CONFIG_ETHER_ON_FEC1)
  52. {
  53. 0,
  54. offsetof(immap_t, im_cpm.cp_fec1),
  55. #if defined(CONFIG_FEC1_PHY)
  56. CONFIG_FEC1_PHY,
  57. #else
  58. -1, /* discover */
  59. #endif
  60. -1,
  61. 0,
  62. },
  63. #endif
  64. #if defined(CONFIG_ETHER_ON_FEC2)
  65. {
  66. 1,
  67. offsetof(immap_t, im_cpm.cp_fec2),
  68. #if defined(CONFIG_FEC2_PHY)
  69. CONFIG_FEC2_PHY,
  70. #else
  71. -1,
  72. #endif
  73. -1,
  74. 0,
  75. },
  76. #endif
  77. };
  78. /* Ethernet Transmit and Receive Buffers */
  79. #define DBUF_LENGTH 1520
  80. #define TX_BUF_CNT 2
  81. #define TOUT_LOOP 100
  82. #define PKT_MAXBUF_SIZE 1518
  83. #define PKT_MINBUF_SIZE 64
  84. #define PKT_MAXBLR_SIZE 1520
  85. #ifdef __GNUC__
  86. static char txbuf[DBUF_LENGTH] __aligned(8);
  87. #else
  88. #error txbuf must be aligned.
  89. #endif
  90. static uint rxIdx; /* index of the current RX buffer */
  91. static uint txIdx; /* index of the current TX buffer */
  92. /*
  93. * FEC Ethernet Tx and Rx buffer descriptors allocated at the
  94. * immr->udata_bd address on Dual-Port RAM
  95. * Provide for Double Buffering
  96. */
  97. struct common_buf_desc {
  98. cbd_t rxbd[PKTBUFSRX]; /* Rx BD */
  99. cbd_t txbd[TX_BUF_CNT]; /* Tx BD */
  100. };
  101. static struct common_buf_desc __iomem *rtx;
  102. static int fec_send(struct eth_device *dev, void *packet, int length);
  103. static int fec_recv(struct eth_device *dev);
  104. static int fec_init(struct eth_device *dev, bd_t *bd);
  105. static void fec_halt(struct eth_device *dev);
  106. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  107. static void __mii_init(void);
  108. #endif
  109. int fec_initialize(bd_t *bis)
  110. {
  111. struct eth_device *dev;
  112. struct ether_fcc_info_s *efis;
  113. int i;
  114. for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++) {
  115. dev = malloc(sizeof(*dev));
  116. if (dev == NULL)
  117. hang();
  118. memset(dev, 0, sizeof(*dev));
  119. /* for FEC1 make sure that the name of the interface is the same
  120. as the old one for compatibility reasons */
  121. if (i == 0)
  122. strcpy(dev->name, "FEC");
  123. else
  124. sprintf(dev->name, "FEC%d",
  125. ether_fcc_info[i].ether_index + 1);
  126. efis = &ether_fcc_info[i];
  127. /*
  128. * reset actual phy addr
  129. */
  130. efis->actual_phy_addr = -1;
  131. dev->priv = efis;
  132. dev->init = fec_init;
  133. dev->halt = fec_halt;
  134. dev->send = fec_send;
  135. dev->recv = fec_recv;
  136. eth_register(dev);
  137. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  138. int retval;
  139. struct mii_dev *mdiodev = mdio_alloc();
  140. if (!mdiodev)
  141. return -ENOMEM;
  142. strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
  143. mdiodev->read = fec8xx_miiphy_read;
  144. mdiodev->write = fec8xx_miiphy_write;
  145. retval = mdio_register(mdiodev);
  146. if (retval < 0)
  147. return retval;
  148. #endif
  149. }
  150. return 1;
  151. }
  152. static int fec_send(struct eth_device *dev, void *packet, int length)
  153. {
  154. int j, rc;
  155. struct ether_fcc_info_s *efis = dev->priv;
  156. fec_t __iomem *fecp =
  157. (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  158. /* section 16.9.23.3
  159. * Wait for ready
  160. */
  161. j = 0;
  162. while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) &&
  163. (j < TOUT_LOOP)) {
  164. udelay(1);
  165. j++;
  166. }
  167. if (j >= TOUT_LOOP)
  168. printf("TX not ready\n");
  169. out_be32(&rtx->txbd[txIdx].cbd_bufaddr, (uint)packet);
  170. out_be16(&rtx->txbd[txIdx].cbd_datlen, length);
  171. setbits_be16(&rtx->txbd[txIdx].cbd_sc,
  172. BD_ENET_TX_READY | BD_ENET_TX_LAST);
  173. /* Activate transmit Buffer Descriptor polling */
  174. /* Descriptor polling active */
  175. out_be32(&fecp->fec_x_des_active, 0x01000000);
  176. j = 0;
  177. while ((in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_READY) &&
  178. (j < TOUT_LOOP)) {
  179. udelay(1);
  180. j++;
  181. }
  182. if (j >= TOUT_LOOP)
  183. printf("TX timeout\n");
  184. /* return only status bits */;
  185. rc = in_be16(&rtx->txbd[txIdx].cbd_sc) & BD_ENET_TX_STATS;
  186. txIdx = (txIdx + 1) % TX_BUF_CNT;
  187. return rc;
  188. }
  189. static int fec_recv(struct eth_device *dev)
  190. {
  191. struct ether_fcc_info_s *efis = dev->priv;
  192. fec_t __iomem *fecp =
  193. (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  194. int length;
  195. for (;;) {
  196. /* section 16.9.23.2 */
  197. if (in_be16(&rtx->rxbd[rxIdx].cbd_sc) & BD_ENET_RX_EMPTY) {
  198. length = -1;
  199. break; /* nothing received - leave for() loop */
  200. }
  201. length = in_be16(&rtx->rxbd[rxIdx].cbd_datlen);
  202. if (!(in_be16(&rtx->rxbd[rxIdx].cbd_sc) & 0x003f)) {
  203. uchar *rx = net_rx_packets[rxIdx];
  204. length -= 4;
  205. #if defined(CONFIG_CMD_CDP)
  206. if ((rx[0] & 1) != 0 &&
  207. memcmp((uchar *)rx, net_bcast_ethaddr, 6) != 0 &&
  208. !is_cdp_packet((uchar *)rx))
  209. rx = NULL;
  210. #endif
  211. /*
  212. * Pass the packet up to the protocol layers.
  213. */
  214. if (rx != NULL)
  215. net_process_received_packet(rx, length);
  216. }
  217. /* Give the buffer back to the FEC. */
  218. out_be16(&rtx->rxbd[rxIdx].cbd_datlen, 0);
  219. /* wrap around buffer index when necessary */
  220. if ((rxIdx + 1) >= PKTBUFSRX) {
  221. out_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc,
  222. BD_ENET_RX_WRAP | BD_ENET_RX_EMPTY);
  223. rxIdx = 0;
  224. } else {
  225. out_be16(&rtx->rxbd[rxIdx].cbd_sc, BD_ENET_RX_EMPTY);
  226. rxIdx++;
  227. }
  228. /* Try to fill Buffer Descriptors */
  229. /* Descriptor polling active */
  230. out_be32(&fecp->fec_r_des_active, 0x01000000);
  231. }
  232. return length;
  233. }
  234. /**************************************************************
  235. *
  236. * FEC Ethernet Initialization Routine
  237. *
  238. *************************************************************/
  239. #define FEC_ECNTRL_PINMUX 0x00000004
  240. #define FEC_ECNTRL_ETHER_EN 0x00000002
  241. #define FEC_ECNTRL_RESET 0x00000001
  242. #define FEC_RCNTRL_BC_REJ 0x00000010
  243. #define FEC_RCNTRL_PROM 0x00000008
  244. #define FEC_RCNTRL_MII_MODE 0x00000004
  245. #define FEC_RCNTRL_DRT 0x00000002
  246. #define FEC_RCNTRL_LOOP 0x00000001
  247. #define FEC_TCNTRL_FDEN 0x00000004
  248. #define FEC_TCNTRL_HBC 0x00000002
  249. #define FEC_TCNTRL_GTS 0x00000001
  250. #define FEC_RESET_DELAY 50
  251. #if defined(CONFIG_RMII)
  252. static inline void fec_10Mbps(struct eth_device *dev)
  253. {
  254. struct ether_fcc_info_s *efis = dev->priv;
  255. int fecidx = efis->ether_index;
  256. uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
  257. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  258. if ((unsigned int)fecidx >= 2)
  259. hang();
  260. setbits_be32(&immr->im_cpm.cp_cptr, mask);
  261. }
  262. static inline void fec_100Mbps(struct eth_device *dev)
  263. {
  264. struct ether_fcc_info_s *efis = dev->priv;
  265. int fecidx = efis->ether_index;
  266. uint mask = (fecidx == 0) ? 0x0000010 : 0x0000008;
  267. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  268. if ((unsigned int)fecidx >= 2)
  269. hang();
  270. clrbits_be32(&immr->im_cpm.cp_cptr, mask);
  271. }
  272. #endif
  273. static inline void fec_full_duplex(struct eth_device *dev)
  274. {
  275. struct ether_fcc_info_s *efis = dev->priv;
  276. fec_t __iomem *fecp =
  277. (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  278. clrbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT);
  279. setbits_be32(&fecp->fec_x_cntrl, FEC_TCNTRL_FDEN); /* FD enable */
  280. }
  281. static inline void fec_half_duplex(struct eth_device *dev)
  282. {
  283. struct ether_fcc_info_s *efis = dev->priv;
  284. fec_t __iomem *fecp =
  285. (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  286. setbits_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_DRT);
  287. clrbits_be32(&fecp->fec_x_cntrl, FEC_TCNTRL_FDEN); /* FD disable */
  288. }
  289. static void fec_pin_init(int fecidx)
  290. {
  291. bd_t *bd = gd->bd;
  292. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  293. /*
  294. * Set MII speed to 2.5 MHz or slightly below.
  295. *
  296. * According to the MPC860T (Rev. D) Fast ethernet controller user
  297. * manual (6.2.14),
  298. * the MII management interface clock must be less than or equal
  299. * to 2.5 MHz.
  300. * This MDC frequency is equal to system clock / (2 * MII_SPEED).
  301. * Then MII_SPEED = system_clock / 2 * 2,5 MHz.
  302. *
  303. * All MII configuration is done via FEC1 registers:
  304. */
  305. out_be32(&immr->im_cpm.cp_fec1.fec_mii_speed,
  306. ((bd->bi_intfreq + 4999999) / 5000000) << 1);
  307. #if defined(CONFIG_MPC885) && defined(WANT_MII)
  308. /* use MDC for MII */
  309. setbits_be16(&immr->im_ioport.iop_pdpar, 0x0080);
  310. clrbits_be16(&immr->im_ioport.iop_pddir, 0x0080);
  311. #endif
  312. if (fecidx == 0) {
  313. #if defined(CONFIG_ETHER_ON_FEC1)
  314. #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */
  315. #if !defined(CONFIG_RMII)
  316. setbits_be16(&immr->im_ioport.iop_papar, 0xf830);
  317. setbits_be16(&immr->im_ioport.iop_padir, 0x0830);
  318. clrbits_be16(&immr->im_ioport.iop_padir, 0xf000);
  319. setbits_be32(&immr->im_cpm.cp_pbpar, 0x00001001);
  320. clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00001001);
  321. setbits_be16(&immr->im_ioport.iop_pcpar, 0x000c);
  322. clrbits_be16(&immr->im_ioport.iop_pcdir, 0x000c);
  323. setbits_be32(&immr->im_cpm.cp_pepar, 0x00000003);
  324. setbits_be32(&immr->im_cpm.cp_pedir, 0x00000003);
  325. clrbits_be32(&immr->im_cpm.cp_peso, 0x00000003);
  326. clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000100);
  327. #else
  328. #if !defined(CONFIG_FEC1_PHY_NORXERR)
  329. setbits_be16(&immr->im_ioport.iop_papar, 0x1000);
  330. clrbits_be16(&immr->im_ioport.iop_padir, 0x1000);
  331. #endif
  332. setbits_be16(&immr->im_ioport.iop_papar, 0xe810);
  333. setbits_be16(&immr->im_ioport.iop_padir, 0x0810);
  334. clrbits_be16(&immr->im_ioport.iop_padir, 0xe000);
  335. setbits_be32(&immr->im_cpm.cp_pbpar, 0x00000001);
  336. clrbits_be32(&immr->im_cpm.cp_pbdir, 0x00000001);
  337. setbits_be32(&immr->im_cpm.cp_cptr, 0x00000100);
  338. clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000050);
  339. #endif /* !CONFIG_RMII */
  340. #else
  341. /*
  342. * Configure all of port D for MII.
  343. */
  344. out_be16(&immr->im_ioport.iop_pdpar, 0x1fff);
  345. out_be16(&immr->im_ioport.iop_pddir, 0x1fff);
  346. #endif
  347. #endif /* CONFIG_ETHER_ON_FEC1 */
  348. } else if (fecidx == 1) {
  349. #if defined(CONFIG_ETHER_ON_FEC2)
  350. #if defined(CONFIG_MPC885) /* MPC87x/88x have got 2 FECs and different pinout */
  351. #if !defined(CONFIG_RMII)
  352. setbits_be32(&immr->im_cpm.cp_pepar, 0x0003fffc);
  353. setbits_be32(&immr->im_cpm.cp_pedir, 0x0003fffc);
  354. clrbits_be32(&immr->im_cpm.cp_peso, 0x000087fc);
  355. setbits_be32(&immr->im_cpm.cp_peso, 0x00037800);
  356. clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000080);
  357. #else
  358. #if !defined(CONFIG_FEC2_PHY_NORXERR)
  359. setbits_be32(&immr->im_cpm.cp_pepar, 0x00000010);
  360. setbits_be32(&immr->im_cpm.cp_pedir, 0x00000010);
  361. clrbits_be32(&immr->im_cpm.cp_peso, 0x00000010);
  362. #endif
  363. setbits_be32(&immr->im_cpm.cp_pepar, 0x00039620);
  364. setbits_be32(&immr->im_cpm.cp_pedir, 0x00039620);
  365. setbits_be32(&immr->im_cpm.cp_peso, 0x00031000);
  366. clrbits_be32(&immr->im_cpm.cp_peso, 0x00008620);
  367. setbits_be32(&immr->im_cpm.cp_cptr, 0x00000080);
  368. clrbits_be32(&immr->im_cpm.cp_cptr, 0x00000028);
  369. #endif /* CONFIG_RMII */
  370. #endif /* CONFIG_MPC885 */
  371. #endif /* CONFIG_ETHER_ON_FEC2 */
  372. }
  373. }
  374. static int fec_reset(fec_t __iomem *fecp)
  375. {
  376. int i;
  377. /* Whack a reset.
  378. * A delay is required between a reset of the FEC block and
  379. * initialization of other FEC registers because the reset takes
  380. * some time to complete. If you don't delay, subsequent writes
  381. * to FEC registers might get killed by the reset routine which is
  382. * still in progress.
  383. */
  384. out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
  385. for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) &&
  386. (i < FEC_RESET_DELAY); ++i)
  387. udelay(1);
  388. if (i == FEC_RESET_DELAY)
  389. return -1;
  390. return 0;
  391. }
  392. static int fec_init(struct eth_device *dev, bd_t *bd)
  393. {
  394. struct ether_fcc_info_s *efis = dev->priv;
  395. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  396. fec_t __iomem *fecp =
  397. (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  398. int i;
  399. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  400. /* the MII interface is connected to FEC1
  401. * so for the miiphy_xxx function to work we must
  402. * call mii_init since fec_halt messes the thing up
  403. */
  404. if (efis->ether_index != 0)
  405. __mii_init();
  406. #endif
  407. if (fec_reset(fecp) < 0)
  408. printf("FEC_RESET_DELAY timeout\n");
  409. /* We use strictly polling mode only
  410. */
  411. out_be32(&fecp->fec_imask, 0);
  412. /* Clear any pending interrupt
  413. */
  414. out_be32(&fecp->fec_ievent, 0xffc0);
  415. /* No need to set the IVEC register */
  416. /* Set station address
  417. */
  418. #define ea dev->enetaddr
  419. out_be32(&fecp->fec_addr_low, (ea[0] << 24) | (ea[1] << 16) |
  420. (ea[2] << 8) | ea[3]);
  421. out_be16(&fecp->fec_addr_high, (ea[4] << 8) | ea[5]);
  422. #undef ea
  423. #if defined(CONFIG_CMD_CDP)
  424. /*
  425. * Turn on multicast address hash table
  426. */
  427. out_be32(&fecp->fec_hash_table_high, 0xffffffff);
  428. out_be32(&fecp->fec_hash_table_low, 0xffffffff);
  429. #else
  430. /* Clear multicast address hash table
  431. */
  432. out_be32(&fecp->fec_hash_table_high, 0);
  433. out_be32(&fecp->fec_hash_table_low, 0);
  434. #endif
  435. /* Set maximum receive buffer size.
  436. */
  437. out_be32(&fecp->fec_r_buff_size, PKT_MAXBLR_SIZE);
  438. /* Set maximum frame length
  439. */
  440. out_be32(&fecp->fec_r_hash, PKT_MAXBUF_SIZE);
  441. /*
  442. * Setup Buffers and Buffer Descriptors
  443. */
  444. rxIdx = 0;
  445. txIdx = 0;
  446. if (!rtx)
  447. rtx = (struct common_buf_desc __iomem *)
  448. (immr->im_cpm.cp_dpmem + CPM_FEC_BASE);
  449. /*
  450. * Setup Receiver Buffer Descriptors (13.14.24.18)
  451. * Settings:
  452. * Empty, Wrap
  453. */
  454. for (i = 0; i < PKTBUFSRX; i++) {
  455. out_be16(&rtx->rxbd[i].cbd_sc, BD_ENET_RX_EMPTY);
  456. out_be16(&rtx->rxbd[i].cbd_datlen, 0); /* Reset */
  457. out_be32(&rtx->rxbd[i].cbd_bufaddr, (uint)net_rx_packets[i]);
  458. }
  459. setbits_be16(&rtx->rxbd[PKTBUFSRX - 1].cbd_sc, BD_ENET_RX_WRAP);
  460. /*
  461. * Setup Ethernet Transmitter Buffer Descriptors (13.14.24.19)
  462. * Settings:
  463. * Last, Tx CRC
  464. */
  465. for (i = 0; i < TX_BUF_CNT; i++) {
  466. out_be16(&rtx->txbd[i].cbd_sc, BD_ENET_TX_LAST | BD_ENET_TX_TC);
  467. out_be16(&rtx->txbd[i].cbd_datlen, 0); /* Reset */
  468. out_be32(&rtx->txbd[i].cbd_bufaddr, (uint)txbuf);
  469. }
  470. setbits_be16(&rtx->txbd[TX_BUF_CNT - 1].cbd_sc, BD_ENET_TX_WRAP);
  471. /* Set receive and transmit descriptor base
  472. */
  473. out_be32(&fecp->fec_r_des_start, (__force unsigned int)rtx->rxbd);
  474. out_be32(&fecp->fec_x_des_start, (__force unsigned int)rtx->txbd);
  475. /* Enable MII mode
  476. */
  477. /* Half duplex mode */
  478. out_be32(&fecp->fec_r_cntrl, FEC_RCNTRL_MII_MODE | FEC_RCNTRL_DRT);
  479. out_be32(&fecp->fec_x_cntrl, 0);
  480. /* Enable big endian and don't care about SDMA FC.
  481. */
  482. out_be32(&fecp->fec_fun_code, 0x78000000);
  483. /*
  484. * Setup the pin configuration of the FEC
  485. */
  486. fec_pin_init(efis->ether_index);
  487. rxIdx = 0;
  488. txIdx = 0;
  489. /*
  490. * Now enable the transmit and receive processing
  491. */
  492. out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  493. if (efis->phy_addr == -1) {
  494. #ifdef CONFIG_SYS_DISCOVER_PHY
  495. /*
  496. * wait for the PHY to wake up after reset
  497. */
  498. efis->actual_phy_addr = mii_discover_phy(dev);
  499. if (efis->actual_phy_addr == -1) {
  500. printf("Unable to discover phy!\n");
  501. return -1;
  502. }
  503. #else
  504. efis->actual_phy_addr = -1;
  505. #endif
  506. } else {
  507. efis->actual_phy_addr = efis->phy_addr;
  508. }
  509. #if defined(CONFIG_MII) && defined(CONFIG_RMII)
  510. /*
  511. * adapt the RMII speed to the speed of the phy
  512. */
  513. if (miiphy_speed(dev->name, efis->actual_phy_addr) == _100BASET)
  514. fec_100Mbps(dev);
  515. else
  516. fec_10Mbps(dev);
  517. #endif
  518. #if defined(CONFIG_MII)
  519. /*
  520. * adapt to the half/full speed settings
  521. */
  522. if (miiphy_duplex(dev->name, efis->actual_phy_addr) == FULL)
  523. fec_full_duplex(dev);
  524. else
  525. fec_half_duplex(dev);
  526. #endif
  527. /* And last, try to fill Rx Buffer Descriptors */
  528. /* Descriptor polling active */
  529. out_be32(&fecp->fec_r_des_active, 0x01000000);
  530. efis->initialized = 1;
  531. return 0;
  532. }
  533. static void fec_halt(struct eth_device *dev)
  534. {
  535. struct ether_fcc_info_s *efis = dev->priv;
  536. fec_t __iomem *fecp =
  537. (fec_t __iomem *)(CONFIG_SYS_IMMR + efis->fecp_offset);
  538. int i;
  539. /* avoid halt if initialized; mii gets stuck otherwise */
  540. if (!efis->initialized)
  541. return;
  542. /* Whack a reset.
  543. * A delay is required between a reset of the FEC block and
  544. * initialization of other FEC registers because the reset takes
  545. * some time to complete. If you don't delay, subsequent writes
  546. * to FEC registers might get killed by the reset routine which is
  547. * still in progress.
  548. */
  549. out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_RESET);
  550. for (i = 0; (in_be32(&fecp->fec_ecntrl) & FEC_ECNTRL_RESET) &&
  551. (i < FEC_RESET_DELAY); ++i)
  552. udelay(1);
  553. if (i == FEC_RESET_DELAY) {
  554. printf("FEC_RESET_DELAY timeout\n");
  555. return;
  556. }
  557. efis->initialized = 0;
  558. }
  559. #if defined(CONFIG_SYS_DISCOVER_PHY) || defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  560. /* Make MII read/write commands for the FEC.
  561. */
  562. #define mk_mii_read(ADDR, REG) (0x60020000 | ((ADDR << 23) | \
  563. (REG & 0x1f) << 18))
  564. #define mk_mii_write(ADDR, REG, VAL) (0x50020000 | ((ADDR << 23) | \
  565. (REG & 0x1f) << 18) | \
  566. (VAL & 0xffff))
  567. /* Interrupt events/masks.
  568. */
  569. #define FEC_ENET_HBERR ((uint)0x80000000) /* Heartbeat error */
  570. #define FEC_ENET_BABR ((uint)0x40000000) /* Babbling receiver */
  571. #define FEC_ENET_BABT ((uint)0x20000000) /* Babbling transmitter */
  572. #define FEC_ENET_GRA ((uint)0x10000000) /* Graceful stop complete */
  573. #define FEC_ENET_TXF ((uint)0x08000000) /* Full frame transmitted */
  574. #define FEC_ENET_TXB ((uint)0x04000000) /* A buffer was transmitted */
  575. #define FEC_ENET_RXF ((uint)0x02000000) /* Full frame received */
  576. #define FEC_ENET_RXB ((uint)0x01000000) /* A buffer was received */
  577. #define FEC_ENET_MII ((uint)0x00800000) /* MII interrupt */
  578. #define FEC_ENET_EBERR ((uint)0x00400000) /* SDMA bus error */
  579. /* send command to phy using mii, wait for result */
  580. static uint
  581. mii_send(uint mii_cmd)
  582. {
  583. uint mii_reply;
  584. fec_t __iomem *ep;
  585. int cnt;
  586. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  587. ep = &immr->im_cpm.cp_fec;
  588. out_be32(&ep->fec_mii_data, mii_cmd); /* command to phy */
  589. /* wait for mii complete */
  590. cnt = 0;
  591. while (!(in_be32(&ep->fec_ievent) & FEC_ENET_MII)) {
  592. if (++cnt > 1000) {
  593. printf("mii_send STUCK!\n");
  594. break;
  595. }
  596. }
  597. mii_reply = in_be32(&ep->fec_mii_data); /* result from phy */
  598. out_be32(&ep->fec_ievent, FEC_ENET_MII); /* clear MII complete */
  599. return mii_reply & 0xffff; /* data read from phy */
  600. }
  601. #endif
  602. #if defined(CONFIG_SYS_DISCOVER_PHY)
  603. static int mii_discover_phy(struct eth_device *dev)
  604. {
  605. #define MAX_PHY_PASSES 11
  606. uint phyno;
  607. int pass;
  608. uint phytype;
  609. int phyaddr;
  610. phyaddr = -1; /* didn't find a PHY yet */
  611. for (pass = 1; pass <= MAX_PHY_PASSES && phyaddr < 0; ++pass) {
  612. if (pass > 1) {
  613. /* PHY may need more time to recover from reset.
  614. * The LXT970 needs 50ms typical, no maximum is
  615. * specified, so wait 10ms before try again.
  616. * With 11 passes this gives it 100ms to wake up.
  617. */
  618. udelay(10000); /* wait 10ms */
  619. }
  620. for (phyno = 0; phyno < 32 && phyaddr < 0; ++phyno) {
  621. phytype = mii_send(mk_mii_read(phyno, MII_PHYSID2));
  622. if (phytype != 0xffff) {
  623. phyaddr = phyno;
  624. phytype |= mii_send(mk_mii_read(phyno,
  625. MII_PHYSID1)) << 16;
  626. }
  627. }
  628. }
  629. if (phyaddr < 0)
  630. printf("No PHY device found.\n");
  631. return phyaddr;
  632. }
  633. #endif /* CONFIG_SYS_DISCOVER_PHY */
  634. #if (defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && !defined(CONFIG_BITBANGMII)
  635. /****************************************************************************
  636. * mii_init -- Initialize the MII via FEC 1 for MII command without ethernet
  637. * This function is a subset of eth_init
  638. ****************************************************************************
  639. */
  640. static void __mii_init(void)
  641. {
  642. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  643. fec_t __iomem *fecp = &immr->im_cpm.cp_fec;
  644. if (fec_reset(fecp) < 0)
  645. printf("FEC_RESET_DELAY timeout\n");
  646. /* We use strictly polling mode only
  647. */
  648. out_be32(&fecp->fec_imask, 0);
  649. /* Clear any pending interrupt
  650. */
  651. out_be32(&fecp->fec_ievent, 0xffc0);
  652. /* Now enable the transmit and receive processing
  653. */
  654. out_be32(&fecp->fec_ecntrl, FEC_ECNTRL_PINMUX | FEC_ECNTRL_ETHER_EN);
  655. }
  656. void mii_init(void)
  657. {
  658. int i;
  659. __mii_init();
  660. /* Setup the pin configuration of the FEC(s)
  661. */
  662. for (i = 0; i < ARRAY_SIZE(ether_fcc_info); i++)
  663. fec_pin_init(ether_fcc_info[i].ether_index);
  664. }
  665. /*****************************************************************************
  666. * Read and write a MII PHY register, routines used by MII Utilities
  667. *
  668. * FIXME: These routines are expected to return 0 on success, but mii_send
  669. * does _not_ return an error code. Maybe 0xFFFF means error, i.e.
  670. * no PHY connected...
  671. * For now always return 0.
  672. * FIXME: These routines only work after calling eth_init() at least once!
  673. * Otherwise they hang in mii_send() !!! Sorry!
  674. *****************************************************************************/
  675. int fec8xx_miiphy_read(struct mii_dev *bus, int addr, int devad, int reg)
  676. {
  677. unsigned short value = 0;
  678. short rdreg; /* register working value */
  679. rdreg = mii_send(mk_mii_read(addr, reg));
  680. value = rdreg;
  681. return value;
  682. }
  683. int fec8xx_miiphy_write(struct mii_dev *bus, int addr, int devad, int reg,
  684. u16 value)
  685. {
  686. (void)mii_send(mk_mii_write(addr, reg, value));
  687. return 0;
  688. }
  689. #endif
  690. #endif