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