eth_hydra.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2009-2011 Freescale Semiconductor, Inc.
  4. * Author: Timur Tabi <timur@freescale.com>
  5. */
  6. /*
  7. * This file handles the board muxing between the Fman Ethernet MACs and
  8. * the RGMII/SGMII/XGMII PHYs on a Freescale P3041/P5020 "Hydra" reference
  9. * board. The RGMII PHYs are the two on-board 1Gb ports. The SGMII PHYs are
  10. * provided by the standard Freescale four-port SGMII riser card. The 10Gb
  11. * XGMII PHY is provided via the XAUI riser card. Since there is only one
  12. * Fman device on a P3041 and P5020, we only support one SGMII card and one
  13. * RGMII card.
  14. *
  15. * Muxing is handled via the PIXIS BRDCFG1 register. The EMI1 bits control
  16. * muxing among the RGMII PHYs and the SGMII PHYs. The value for RGMII is
  17. * always the same (0). The value for SGMII depends on which slot the riser is
  18. * inserted in. The EMI2 bits control muxing for the the XGMII. Like SGMII,
  19. * the value is based on which slot the XAUI is inserted in.
  20. *
  21. * The SERDES configuration is used to determine where the SGMII and XAUI cards
  22. * exist, and also which Fman MACs are routed to which PHYs. So for a given
  23. * Fman MAC, there is one and only PHY it connects to. MACs cannot be routed
  24. * to PHYs dynamically.
  25. *
  26. *
  27. * This file also updates the device tree in three ways:
  28. *
  29. * 1) The status of each virtual MDIO node that is referenced by an Ethernet
  30. * node is set to "okay".
  31. *
  32. * 2) The phy-handle property of each active Ethernet MAC node is set to the
  33. * appropriate PHY node.
  34. *
  35. * 3) The "mux value" for each virtual MDIO node is set to the correct value,
  36. * if necessary. Some virtual MDIO nodes do not have configurable mux
  37. * values, so those values are hard-coded in the DTS. On the HYDRA board,
  38. * the virtual MDIO node for the SGMII card needs to be updated.
  39. *
  40. * For all this to work, the device tree needs to have the following:
  41. *
  42. * 1) An alias for each PHY node that an Ethernet node could be routed to.
  43. *
  44. * 2) An alias for each real and virtual MDIO node that is disabled by default
  45. * and might need to be enabled, and also might need to have its mux-value
  46. * updated.
  47. */
  48. #include <common.h>
  49. #include <net.h>
  50. #include <netdev.h>
  51. #include <asm/fsl_serdes.h>
  52. #include <fm_eth.h>
  53. #include <fsl_mdio.h>
  54. #include <malloc.h>
  55. #include <fdt_support.h>
  56. #include <fsl_dtsec.h>
  57. #include "../common/ngpixis.h"
  58. #include "../common/fman.h"
  59. #ifdef CONFIG_FMAN_ENET
  60. #define BRDCFG1_EMI1_SEL_MASK 0x78
  61. #define BRDCFG1_EMI1_SEL_SLOT1 0x10
  62. #define BRDCFG1_EMI1_SEL_SLOT2 0x20
  63. #define BRDCFG1_EMI1_SEL_SLOT5 0x30
  64. #define BRDCFG1_EMI1_SEL_SLOT6 0x40
  65. #define BRDCFG1_EMI1_SEL_SLOT7 0x50
  66. #define BRDCFG1_EMI1_SEL_RGMII 0x00
  67. #define BRDCFG1_EMI1_EN 0x08
  68. #define BRDCFG1_EMI2_SEL_MASK 0x06
  69. #define BRDCFG1_EMI2_SEL_SLOT1 0x00
  70. #define BRDCFG1_EMI2_SEL_SLOT2 0x02
  71. #define BRDCFG2_REG_GPIO_SEL 0x20
  72. #define PHY_BASE_ADDR 0x00
  73. /*
  74. * BRDCFG1 mask and value for each MAC
  75. *
  76. * This array contains the BRDCFG1 values (in mask/val format) that route the
  77. * MDIO bus to a particular RGMII or SGMII PHY.
  78. */
  79. struct {
  80. u8 mask;
  81. u8 val;
  82. } mdio_mux[NUM_FM_PORTS];
  83. /*
  84. * Mapping of all 18 SERDES lanes to board slots. A value of '0' here means
  85. * that the mapping must be determined dynamically, or that the lane maps to
  86. * something other than a board slot
  87. */
  88. static u8 lane_to_slot[] = {
  89. 7, 7, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 1, 1, 0, 0
  90. };
  91. /*
  92. * Set the board muxing for a given MAC
  93. *
  94. * The MDIO layer calls this function every time it wants to talk to a PHY.
  95. */
  96. void hydra_mux_mdio(u8 mask, u8 val)
  97. {
  98. clrsetbits_8(&pixis->brdcfg1, mask, val);
  99. }
  100. struct hydra_mdio {
  101. u8 mask;
  102. u8 val;
  103. struct mii_dev *realbus;
  104. };
  105. static int hydra_mdio_read(struct mii_dev *bus, int addr, int devad,
  106. int regnum)
  107. {
  108. struct hydra_mdio *priv = bus->priv;
  109. hydra_mux_mdio(priv->mask, priv->val);
  110. return priv->realbus->read(priv->realbus, addr, devad, regnum);
  111. }
  112. static int hydra_mdio_write(struct mii_dev *bus, int addr, int devad,
  113. int regnum, u16 value)
  114. {
  115. struct hydra_mdio *priv = bus->priv;
  116. hydra_mux_mdio(priv->mask, priv->val);
  117. return priv->realbus->write(priv->realbus, addr, devad, regnum, value);
  118. }
  119. static int hydra_mdio_reset(struct mii_dev *bus)
  120. {
  121. struct hydra_mdio *priv = bus->priv;
  122. return priv->realbus->reset(priv->realbus);
  123. }
  124. static void hydra_mdio_set_mux(char *name, u8 mask, u8 val)
  125. {
  126. struct mii_dev *bus = miiphy_get_dev_by_name(name);
  127. struct hydra_mdio *priv = bus->priv;
  128. priv->mask = mask;
  129. priv->val = val;
  130. }
  131. static int hydra_mdio_init(char *realbusname, char *fakebusname)
  132. {
  133. struct hydra_mdio *hmdio;
  134. struct mii_dev *bus = mdio_alloc();
  135. if (!bus) {
  136. printf("Failed to allocate Hydra MDIO bus\n");
  137. return -1;
  138. }
  139. hmdio = malloc(sizeof(*hmdio));
  140. if (!hmdio) {
  141. printf("Failed to allocate Hydra private data\n");
  142. free(bus);
  143. return -1;
  144. }
  145. bus->read = hydra_mdio_read;
  146. bus->write = hydra_mdio_write;
  147. bus->reset = hydra_mdio_reset;
  148. strcpy(bus->name, fakebusname);
  149. hmdio->realbus = miiphy_get_dev_by_name(realbusname);
  150. if (!hmdio->realbus) {
  151. printf("No bus with name %s\n", realbusname);
  152. free(bus);
  153. free(hmdio);
  154. return -1;
  155. }
  156. bus->priv = hmdio;
  157. return mdio_register(bus);
  158. }
  159. /*
  160. * Given an alias or a path for a node, set the mux value of that node.
  161. *
  162. * If 'alias' is not a valid alias, then it is treated as a full path to the
  163. * node. No error checking is performed.
  164. *
  165. * This function is normally called to set the fsl,hydra-mdio-muxval property
  166. * of a virtual MDIO node.
  167. */
  168. static void fdt_set_mdio_mux(void *fdt, const char *alias, u32 mux)
  169. {
  170. const char *path = fdt_get_alias(fdt, alias);
  171. if (!path)
  172. path = alias;
  173. do_fixup_by_path(fdt, path, "reg",
  174. &mux, sizeof(mux), 1);
  175. do_fixup_by_path(fdt, path, "fsl,hydra-mdio-muxval",
  176. &mux, sizeof(mux), 1);
  177. }
  178. /*
  179. * Given the following ...
  180. *
  181. * 1) A pointer to an Fman Ethernet node (as identified by the 'compat'
  182. * compatible string and 'addr' physical address)
  183. *
  184. * 2) An Fman port
  185. *
  186. * ... update the phy-handle property of the Ethernet node to point to the
  187. * right PHY. This assumes that we already know the PHY for each port. That
  188. * information is stored in mdio_mux[].
  189. *
  190. * The offset of the Fman Ethernet node is also passed in for convenience, but
  191. * it is not used, and we recalculate the offset anyway.
  192. *
  193. * Note that what we call "Fman ports" (enum fm_port) is really an Fman MAC.
  194. * Inside the Fman, "ports" are things that connect to MACs. We only call them
  195. * ports in U-Boot because on previous Ethernet devices (e.g. Gianfar), MACs
  196. * and ports are the same thing.
  197. *
  198. * Note that this code would be cleaner if had a function called
  199. * fm_info_get_phy_address(), which returns a value from the fm1_dtsec_info[]
  200. * array. That's because all we're doing is figuring out the PHY address for
  201. * a given Fman MAC and writing it to the device tree. Well, we already did
  202. * the hard work to figure that out in board_eth_init(), so it's silly to
  203. * repeat that here.
  204. */
  205. void board_ft_fman_fixup_port(void *fdt, char *compat, phys_addr_t addr,
  206. enum fm_port port, int offset)
  207. {
  208. unsigned int mux = mdio_mux[port].val & mdio_mux[port].mask;
  209. char phy[16];
  210. if (port == FM1_10GEC1) {
  211. /* XAUI */
  212. int lane = serdes_get_first_lane(XAUI_FM1);
  213. if (lane >= 0) {
  214. /* The XAUI PHY is identified by the slot */
  215. sprintf(phy, "phy_xgmii_%u", lane_to_slot[lane]);
  216. fdt_set_phy_handle(fdt, compat, addr, phy);
  217. }
  218. return;
  219. }
  220. if (mux == (BRDCFG1_EMI1_SEL_RGMII | BRDCFG1_EMI1_EN)) {
  221. /* RGMII */
  222. /* The RGMII PHY is identified by the MAC connected to it */
  223. sprintf(phy, "phy_rgmii_%u", port == FM1_DTSEC4 ? 0 : 1);
  224. fdt_set_phy_handle(fdt, compat, addr, phy);
  225. return;
  226. }
  227. /* If it's not RGMII or XGMII, it must be SGMII */
  228. if (mux) {
  229. /* The SGMII PHY is identified by the MAC connected to it */
  230. sprintf(phy, "phy_sgmii_%x",
  231. CONFIG_SYS_FM1_DTSEC1_PHY_ADDR + (port - FM1_DTSEC1));
  232. fdt_set_phy_handle(fdt, compat, addr, phy);
  233. }
  234. }
  235. #define PIXIS_SW2_LANE_23_SEL 0x80
  236. #define PIXIS_SW2_LANE_45_SEL 0x40
  237. #define PIXIS_SW2_LANE_67_SEL_MASK 0x30
  238. #define PIXIS_SW2_LANE_67_SEL_5 0x00
  239. #define PIXIS_SW2_LANE_67_SEL_6 0x20
  240. #define PIXIS_SW2_LANE_67_SEL_7 0x10
  241. #define PIXIS_SW2_LANE_8_SEL 0x08
  242. #define PIXIS_SW2_LANE_1617_SEL 0x04
  243. /*
  244. * Initialize the lane_to_slot[] array.
  245. *
  246. * On the P4080DS "Expedition" board, the mapping of SERDES lanes to board
  247. * slots is hard-coded. On the Hydra board, however, the mapping is controlled
  248. * by board switch SW2, so the lane_to_slot[] array needs to be dynamically
  249. * initialized.
  250. */
  251. static void initialize_lane_to_slot(void)
  252. {
  253. u8 sw2 = in_8(&PIXIS_SW(2));
  254. lane_to_slot[2] = (sw2 & PIXIS_SW2_LANE_23_SEL) ? 7 : 4;
  255. lane_to_slot[3] = lane_to_slot[2];
  256. lane_to_slot[4] = (sw2 & PIXIS_SW2_LANE_45_SEL) ? 7 : 6;
  257. lane_to_slot[5] = lane_to_slot[4];
  258. switch (sw2 & PIXIS_SW2_LANE_67_SEL_MASK) {
  259. case PIXIS_SW2_LANE_67_SEL_5:
  260. lane_to_slot[6] = 5;
  261. break;
  262. case PIXIS_SW2_LANE_67_SEL_6:
  263. lane_to_slot[6] = 6;
  264. break;
  265. case PIXIS_SW2_LANE_67_SEL_7:
  266. lane_to_slot[6] = 7;
  267. break;
  268. }
  269. lane_to_slot[7] = lane_to_slot[6];
  270. lane_to_slot[8] = (sw2 & PIXIS_SW2_LANE_8_SEL) ? 3 : 0;
  271. lane_to_slot[16] = (sw2 & PIXIS_SW2_LANE_1617_SEL) ? 1 : 0;
  272. lane_to_slot[17] = lane_to_slot[16];
  273. }
  274. #endif /* #ifdef CONFIG_FMAN_ENET */
  275. /*
  276. * Configure the status for the virtual MDIO nodes
  277. *
  278. * Rather than create the virtual MDIO nodes from scratch for each active
  279. * virtual MDIO, we expect the DTS to have the nodes defined already, and we
  280. * only enable the ones that are actually active.
  281. *
  282. * We assume that the DTS already hard-codes the status for all the
  283. * virtual MDIO nodes to "disabled", so all we need to do is enable the
  284. * active ones.
  285. *
  286. * For SGMII, we also need to set the mux value in the node.
  287. */
  288. void fdt_fixup_board_enet(void *fdt)
  289. {
  290. #ifdef CONFIG_FMAN_ENET
  291. unsigned int i;
  292. int lane;
  293. for (i = FM1_DTSEC1; i < FM1_DTSEC1 + CONFIG_SYS_NUM_FM1_DTSEC; i++) {
  294. int idx = i - FM1_DTSEC1;
  295. switch (fm_info_get_enet_if(i)) {
  296. case PHY_INTERFACE_MODE_SGMII:
  297. lane = serdes_get_first_lane(SGMII_FM1_DTSEC1 + idx);
  298. if (lane >= 0) {
  299. fdt_status_okay_by_alias(fdt, "emi1_sgmii");
  300. /* Also set the MUX value */
  301. fdt_set_mdio_mux(fdt, "emi1_sgmii",
  302. mdio_mux[i].val);
  303. }
  304. break;
  305. case PHY_INTERFACE_MODE_RGMII:
  306. case PHY_INTERFACE_MODE_RGMII_TXID:
  307. case PHY_INTERFACE_MODE_RGMII_RXID:
  308. case PHY_INTERFACE_MODE_RGMII_ID:
  309. fdt_status_okay_by_alias(fdt, "emi1_rgmii");
  310. break;
  311. default:
  312. break;
  313. }
  314. }
  315. lane = serdes_get_first_lane(XAUI_FM1);
  316. if (lane >= 0)
  317. fdt_status_okay_by_alias(fdt, "emi2_xgmii");
  318. #endif
  319. }
  320. int board_eth_init(struct bd_info *bis)
  321. {
  322. #ifdef CONFIG_FMAN_ENET
  323. struct fsl_pq_mdio_info dtsec_mdio_info;
  324. struct tgec_mdio_info tgec_mdio_info;
  325. unsigned int i, slot;
  326. int lane;
  327. struct mii_dev *bus;
  328. printf("Initializing Fman\n");
  329. initialize_lane_to_slot();
  330. /* We want to use the PIXIS to configure MUX routing, not GPIOs. */
  331. setbits_8(&pixis->brdcfg2, BRDCFG2_REG_GPIO_SEL);
  332. memset(mdio_mux, 0, sizeof(mdio_mux));
  333. dtsec_mdio_info.regs =
  334. (struct tsec_mii_mng *)CONFIG_SYS_FM1_DTSEC1_MDIO_ADDR;
  335. dtsec_mdio_info.name = DEFAULT_FM_MDIO_NAME;
  336. /* Register the real 1G MDIO bus */
  337. fsl_pq_mdio_init(bis, &dtsec_mdio_info);
  338. tgec_mdio_info.regs =
  339. (struct tgec_mdio_controller *)CONFIG_SYS_FM1_TGEC_MDIO_ADDR;
  340. tgec_mdio_info.name = DEFAULT_FM_TGEC_MDIO_NAME;
  341. /* Register the real 10G MDIO bus */
  342. fm_tgec_mdio_init(bis, &tgec_mdio_info);
  343. /* Register the three virtual MDIO front-ends */
  344. hydra_mdio_init(DEFAULT_FM_MDIO_NAME, "HYDRA_RGMII_MDIO");
  345. hydra_mdio_init(DEFAULT_FM_MDIO_NAME, "HYDRA_SGMII_MDIO");
  346. /*
  347. * Program the DTSEC PHY addresses assuming that they are all SGMII.
  348. * For any DTSEC that's RGMII, we'll override its PHY address later.
  349. * We assume that DTSEC5 is only used for RGMII.
  350. */
  351. fm_info_set_phy_address(FM1_DTSEC1, CONFIG_SYS_FM1_DTSEC1_PHY_ADDR);
  352. fm_info_set_phy_address(FM1_DTSEC2, CONFIG_SYS_FM1_DTSEC2_PHY_ADDR);
  353. fm_info_set_phy_address(FM1_DTSEC3, CONFIG_SYS_FM1_DTSEC3_PHY_ADDR);
  354. fm_info_set_phy_address(FM1_DTSEC4, CONFIG_SYS_FM1_DTSEC4_PHY_ADDR);
  355. for (i = FM1_DTSEC1; i < FM1_DTSEC1 + CONFIG_SYS_NUM_FM1_DTSEC; i++) {
  356. int idx = i - FM1_DTSEC1;
  357. switch (fm_info_get_enet_if(i)) {
  358. case PHY_INTERFACE_MODE_SGMII:
  359. lane = serdes_get_first_lane(SGMII_FM1_DTSEC1 + idx);
  360. if (lane < 0)
  361. break;
  362. slot = lane_to_slot[lane];
  363. mdio_mux[i].mask = BRDCFG1_EMI1_SEL_MASK;
  364. switch (slot) {
  365. case 1:
  366. /* Always DTSEC5 on Bank 3 */
  367. mdio_mux[i].val = BRDCFG1_EMI1_SEL_SLOT1 |
  368. BRDCFG1_EMI1_EN;
  369. break;
  370. case 2:
  371. mdio_mux[i].val = BRDCFG1_EMI1_SEL_SLOT2 |
  372. BRDCFG1_EMI1_EN;
  373. break;
  374. case 5:
  375. mdio_mux[i].val = BRDCFG1_EMI1_SEL_SLOT5 |
  376. BRDCFG1_EMI1_EN;
  377. break;
  378. case 6:
  379. mdio_mux[i].val = BRDCFG1_EMI1_SEL_SLOT6 |
  380. BRDCFG1_EMI1_EN;
  381. break;
  382. case 7:
  383. mdio_mux[i].val = BRDCFG1_EMI1_SEL_SLOT7 |
  384. BRDCFG1_EMI1_EN;
  385. break;
  386. };
  387. hydra_mdio_set_mux("HYDRA_SGMII_MDIO",
  388. mdio_mux[i].mask, mdio_mux[i].val);
  389. fm_info_set_mdio(i,
  390. miiphy_get_dev_by_name("HYDRA_SGMII_MDIO"));
  391. break;
  392. case PHY_INTERFACE_MODE_RGMII:
  393. case PHY_INTERFACE_MODE_RGMII_TXID:
  394. case PHY_INTERFACE_MODE_RGMII_RXID:
  395. case PHY_INTERFACE_MODE_RGMII_ID:
  396. /*
  397. * If DTSEC4 is RGMII, then it's routed via via EC1 to
  398. * the first on-board RGMII port. If DTSEC5 is RGMII,
  399. * then it's routed via via EC2 to the second on-board
  400. * RGMII port. The other DTSECs cannot be routed to
  401. * RGMII.
  402. */
  403. fm_info_set_phy_address(i, i == FM1_DTSEC4 ? 0 : 1);
  404. mdio_mux[i].mask = BRDCFG1_EMI1_SEL_MASK;
  405. mdio_mux[i].val = BRDCFG1_EMI1_SEL_RGMII |
  406. BRDCFG1_EMI1_EN;
  407. hydra_mdio_set_mux("HYDRA_RGMII_MDIO",
  408. mdio_mux[i].mask, mdio_mux[i].val);
  409. fm_info_set_mdio(i,
  410. miiphy_get_dev_by_name("HYDRA_RGMII_MDIO"));
  411. break;
  412. case PHY_INTERFACE_MODE_NONE:
  413. fm_info_set_phy_address(i, 0);
  414. break;
  415. default:
  416. printf("Fman1: DTSEC%u set to unknown interface %i\n",
  417. idx + 1, fm_info_get_enet_if(i));
  418. fm_info_set_phy_address(i, 0);
  419. break;
  420. }
  421. }
  422. bus = miiphy_get_dev_by_name("HYDRA_SGMII_MDIO");
  423. set_sgmii_phy(bus, FM1_DTSEC1, CONFIG_SYS_NUM_FM1_DTSEC, PHY_BASE_ADDR);
  424. /*
  425. * For 10G, we only support one XAUI card per Fman. If present, then we
  426. * force its routing and never touch those bits again, which removes the
  427. * need for Linux to do any muxing. This works because of the way
  428. * BRDCFG1 is defined, but it's a bit hackish.
  429. *
  430. * The PHY address for the XAUI card depends on which slot it's in. The
  431. * macros we use imply that the PHY address is based on which FM, but
  432. * that's not true. On the P4080DS, FM1 could only use XAUI in slot 5,
  433. * and FM2 could only use a XAUI in slot 4. On the Hydra board, we
  434. * check the actual slot and just use the macros as-is, even though
  435. * the P3041 and P5020 only have one Fman.
  436. */
  437. lane = serdes_get_first_lane(XAUI_FM1);
  438. if (lane >= 0) {
  439. slot = lane_to_slot[lane];
  440. if (slot == 1) {
  441. /* XAUI card is in slot 1 */
  442. clrsetbits_8(&pixis->brdcfg1, BRDCFG1_EMI2_SEL_MASK,
  443. BRDCFG1_EMI2_SEL_SLOT1);
  444. fm_info_set_phy_address(FM1_10GEC1,
  445. CONFIG_SYS_FM1_10GEC1_PHY_ADDR);
  446. } else {
  447. /* XAUI card is in slot 2 */
  448. clrsetbits_8(&pixis->brdcfg1, BRDCFG1_EMI2_SEL_MASK,
  449. BRDCFG1_EMI2_SEL_SLOT2);
  450. fm_info_set_phy_address(FM1_10GEC1,
  451. CONFIG_SYS_FM2_10GEC1_PHY_ADDR);
  452. }
  453. }
  454. fm_info_set_mdio(FM1_10GEC1,
  455. miiphy_get_dev_by_name(DEFAULT_FM_TGEC_MDIO_NAME));
  456. cpu_eth_init(bis);
  457. #endif
  458. return pci_eth_init(bis);
  459. }