mpc8xx_fec.c 22 KB

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