xref: /openbmc/u-boot/drivers/net/fm/init.c (revision 3765b3e7)
1 /*
2  * Copyright 2011 Freescale Semiconductor, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 #include <common.h>
7 #include <asm/io.h>
8 #include <asm/fsl_serdes.h>
9 
10 #include "fm.h"
11 
12 struct fm_eth_info fm_info[] = {
13 #if (CONFIG_SYS_NUM_FM1_DTSEC >= 1)
14 	FM_DTSEC_INFO_INITIALIZER(1, 1),
15 #endif
16 #if (CONFIG_SYS_NUM_FM1_DTSEC >= 2)
17 	FM_DTSEC_INFO_INITIALIZER(1, 2),
18 #endif
19 #if (CONFIG_SYS_NUM_FM1_DTSEC >= 3)
20 	FM_DTSEC_INFO_INITIALIZER(1, 3),
21 #endif
22 #if (CONFIG_SYS_NUM_FM1_DTSEC >= 4)
23 	FM_DTSEC_INFO_INITIALIZER(1, 4),
24 #endif
25 #if (CONFIG_SYS_NUM_FM1_DTSEC >= 5)
26 	FM_DTSEC_INFO_INITIALIZER(1, 5),
27 #endif
28 #if (CONFIG_SYS_NUM_FM1_DTSEC >= 6)
29 	FM_DTSEC_INFO_INITIALIZER(1, 6),
30 #endif
31 #if (CONFIG_SYS_NUM_FM1_DTSEC >= 7)
32 	FM_DTSEC_INFO_INITIALIZER(1, 9),
33 #endif
34 #if (CONFIG_SYS_NUM_FM1_DTSEC >= 8)
35 	FM_DTSEC_INFO_INITIALIZER(1, 10),
36 #endif
37 #if (CONFIG_SYS_NUM_FM2_DTSEC >= 1)
38 	FM_DTSEC_INFO_INITIALIZER(2, 1),
39 #endif
40 #if (CONFIG_SYS_NUM_FM2_DTSEC >= 2)
41 	FM_DTSEC_INFO_INITIALIZER(2, 2),
42 #endif
43 #if (CONFIG_SYS_NUM_FM2_DTSEC >= 3)
44 	FM_DTSEC_INFO_INITIALIZER(2, 3),
45 #endif
46 #if (CONFIG_SYS_NUM_FM2_DTSEC >= 4)
47 	FM_DTSEC_INFO_INITIALIZER(2, 4),
48 #endif
49 #if (CONFIG_SYS_NUM_FM2_DTSEC >= 5)
50 	FM_DTSEC_INFO_INITIALIZER(2, 5),
51 #endif
52 #if (CONFIG_SYS_NUM_FM2_DTSEC >= 6)
53 	FM_DTSEC_INFO_INITIALIZER(2, 6),
54 #endif
55 #if (CONFIG_SYS_NUM_FM2_DTSEC >= 7)
56 	FM_DTSEC_INFO_INITIALIZER(2, 9),
57 #endif
58 #if (CONFIG_SYS_NUM_FM2_DTSEC >= 8)
59 	FM_DTSEC_INFO_INITIALIZER(2, 10),
60 #endif
61 #if (CONFIG_SYS_NUM_FM1_10GEC >= 1)
62 	FM_TGEC_INFO_INITIALIZER(1, 1),
63 #endif
64 #if (CONFIG_SYS_NUM_FM1_10GEC >= 2)
65 	FM_TGEC_INFO_INITIALIZER(1, 2),
66 #endif
67 #if (CONFIG_SYS_NUM_FM2_10GEC >= 1)
68 	FM_TGEC_INFO_INITIALIZER(2, 1),
69 #endif
70 #if (CONFIG_SYS_NUM_FM2_10GEC >= 2)
71 	FM_TGEC_INFO_INITIALIZER(2, 2),
72 #endif
73 };
74 
75 int fm_standard_init(bd_t *bis)
76 {
77 	int i;
78 	struct ccsr_fman *reg;
79 
80 	reg = (void *)CONFIG_SYS_FSL_FM1_ADDR;
81 	if (fm_init_common(0, reg))
82 		return 0;
83 
84 	for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
85 		if ((fm_info[i].enabled) && (fm_info[i].index == 1))
86 			fm_eth_initialize(reg, &fm_info[i]);
87 	}
88 
89 #if (CONFIG_SYS_NUM_FMAN == 2)
90 	reg = (void *)CONFIG_SYS_FSL_FM2_ADDR;
91 	if (fm_init_common(1, reg))
92 		return 0;
93 
94 	for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
95 		if ((fm_info[i].enabled) && (fm_info[i].index == 2))
96 			fm_eth_initialize(reg, &fm_info[i]);
97 	}
98 #endif
99 
100 	return 1;
101 }
102 
103 /* simple linear search to map from port to array index */
104 static int fm_port_to_index(enum fm_port port)
105 {
106 	int i;
107 
108 	for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
109 		if (fm_info[i].port == port)
110 			return i;
111 	}
112 
113 	return -1;
114 }
115 
116 /*
117  * Determine if an interface is actually active based on HW config
118  * we expect fman_port_enet_if() to report PHY_INTERFACE_MODE_NONE if
119  * the interface is not active based on HW cfg of the SoC
120  */
121 void fman_enet_init(void)
122 {
123 	int i;
124 
125 	for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
126 		phy_interface_t enet_if;
127 
128 		enet_if = fman_port_enet_if(fm_info[i].port);
129 		if (enet_if != PHY_INTERFACE_MODE_NONE) {
130 			fm_info[i].enabled = 1;
131 			fm_info[i].enet_if = enet_if;
132 		} else {
133 			fm_info[i].enabled = 0;
134 		}
135 	}
136 
137 	return ;
138 }
139 
140 void fm_disable_port(enum fm_port port)
141 {
142 	int i = fm_port_to_index(port);
143 
144 	fm_info[i].enabled = 0;
145 	fman_disable_port(port);
146 }
147 
148 void fm_info_set_mdio(enum fm_port port, struct mii_dev *bus)
149 {
150 	int i = fm_port_to_index(port);
151 
152 	if (i == -1)
153 		return;
154 
155 	fm_info[i].bus = bus;
156 }
157 
158 void fm_info_set_phy_address(enum fm_port port, int address)
159 {
160 	int i = fm_port_to_index(port);
161 
162 	if (i == -1)
163 		return;
164 
165 	fm_info[i].phy_addr = address;
166 }
167 
168 /*
169  * Returns the PHY address for a given Fman port
170  *
171  * The port must be set via a prior call to fm_info_set_phy_address().
172  * A negative error code is returned if the port is invalid.
173  */
174 int fm_info_get_phy_address(enum fm_port port)
175 {
176 	int i = fm_port_to_index(port);
177 
178 	if (i == -1)
179 		return -1;
180 
181 	return fm_info[i].phy_addr;
182 }
183 
184 /*
185  * Returns the type of the data interface between the given MAC and its PHY.
186  * This is typically determined by the RCW.
187  */
188 phy_interface_t fm_info_get_enet_if(enum fm_port port)
189 {
190 	int i = fm_port_to_index(port);
191 
192 	if (i == -1)
193 		return PHY_INTERFACE_MODE_NONE;
194 
195 	if (fm_info[i].enabled)
196 		return fm_info[i].enet_if;
197 
198 	return PHY_INTERFACE_MODE_NONE;
199 }
200 
201 static void
202 __def_board_ft_fman_fixup_port(void *blob, char * prop, phys_addr_t pa,
203 				enum fm_port port, int offset)
204 {
205 	return ;
206 }
207 
208 void board_ft_fman_fixup_port(void *blob, char * prop, phys_addr_t pa,
209 				enum fm_port port, int offset)
210 	 __attribute__((weak, alias("__def_board_ft_fman_fixup_port")));
211 
212 static void ft_fixup_port(void *blob, struct fm_eth_info *info, char *prop)
213 {
214 	int off;
215 	uint32_t ph;
216 	phys_addr_t paddr = CONFIG_SYS_CCSRBAR_PHYS + info->compat_offset;
217 	u64 dtsec1_addr = (u64)CONFIG_SYS_CCSRBAR_PHYS +
218 				CONFIG_SYS_FSL_FM1_DTSEC1_OFFSET;
219 
220 	off = fdt_node_offset_by_compat_reg(blob, prop, paddr);
221 
222 	if (info->enabled) {
223 		fdt_fixup_phy_connection(blob, off, info->enet_if);
224 		board_ft_fman_fixup_port(blob, prop, paddr, info->port, off);
225 		return ;
226 	}
227 
228 #ifdef CONFIG_SYS_FMAN_V3
229 	/*
230 	 * Physically FM1_DTSEC9 and FM1_10GEC1 use the same dual-role MAC, when
231 	 * FM1_10GEC1 is enabled and  FM1_DTSEC9 is disabled, ensure that the
232 	 * dual-role MAC is not disabled, ditto for other dual-role MACs.
233 	 */
234 	if (((info->port == FM1_DTSEC9) && (PORT_IS_ENABLED(FM1_10GEC1)))	||
235 	    ((info->port == FM1_DTSEC10) && (PORT_IS_ENABLED(FM1_10GEC2)))	||
236 	    ((info->port == FM1_10GEC1) && (PORT_IS_ENABLED(FM1_DTSEC9)))	||
237 	    ((info->port == FM1_10GEC2) && (PORT_IS_ENABLED(FM1_DTSEC10)))
238 #if (CONFIG_SYS_NUM_FMAN == 2)
239 										||
240 	    ((info->port == FM2_DTSEC9) && (PORT_IS_ENABLED(FM2_10GEC1)))	||
241 	    ((info->port == FM2_DTSEC10) && (PORT_IS_ENABLED(FM2_10GEC2)))	||
242 	    ((info->port == FM2_10GEC1) && (PORT_IS_ENABLED(FM2_DTSEC9)))	||
243 	    ((info->port == FM2_10GEC2) && (PORT_IS_ENABLED(FM2_DTSEC10)))
244 #endif
245 	)
246 		return;
247 #endif
248 	/* board code might have caused offset to change */
249 	off = fdt_node_offset_by_compat_reg(blob, prop, paddr);
250 
251 	/* Don't disable FM1-DTSEC1 MAC as its used for MDIO */
252 	if (paddr != dtsec1_addr)
253 		fdt_status_disabled(blob, off); /* disable the MAC node */
254 
255 	/* disable the fsl,dpa-ethernet node that points to the MAC */
256 	ph = fdt_get_phandle(blob, off);
257 	do_fixup_by_prop(blob, "fsl,fman-mac", &ph, sizeof(ph),
258 		"status", "disabled", strlen("disabled") + 1, 1);
259 }
260 
261 void fdt_fixup_fman_ethernet(void *blob)
262 {
263 	int i;
264 
265 #ifdef CONFIG_SYS_FMAN_V3
266 	for (i = 0; i < ARRAY_SIZE(fm_info); i++)
267 		ft_fixup_port(blob, &fm_info[i], "fsl,fman-memac");
268 #else
269 	for (i = 0; i < ARRAY_SIZE(fm_info); i++) {
270 		if (fm_info[i].type == FM_ETH_1G_E)
271 			ft_fixup_port(blob, &fm_info[i], "fsl,fman-1g-mac");
272 		else
273 			ft_fixup_port(blob, &fm_info[i], "fsl,fman-10g-mac");
274 	}
275 #endif
276 }
277