mpc8xx_fec.c 21 KB

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