mpc8xx_fec.c 21 KB

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