xref: /openbmc/u-boot/arch/powerpc/cpu/mpc8xxx/fdt.c (revision 151d63cb)
1 /*
2  * Copyright 2009-2012 Freescale Semiconductor, Inc.
3  *
4  * This file is derived from arch/powerpc/cpu/mpc85xx/cpu.c and
5  * arch/powerpc/cpu/mpc86xx/cpu.c. Basically this file contains
6  * cpu specific common code for 85xx/86xx processors.
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25 
26 #include <common.h>
27 #include <libfdt.h>
28 #include <fdt_support.h>
29 #include <asm/mp.h>
30 #include <asm/fsl_serdes.h>
31 #include <phy.h>
32 #include <hwconfig.h>
33 
34 #define FSL_MAX_NUM_USB_CTRLS	2
35 
36 #if defined(CONFIG_MP) && (defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx))
37 static int ft_del_cpuhandle(void *blob, int cpuhandle)
38 {
39 	int off, ret = -FDT_ERR_NOTFOUND;
40 
41 	/* if we find a match, we'll delete at it which point the offsets are
42 	 * invalid so we start over from the beginning
43 	 */
44 	off = fdt_node_offset_by_prop_value(blob, -1, "cpu-handle",
45 						&cpuhandle, 4);
46 	while (off != -FDT_ERR_NOTFOUND) {
47 		fdt_delprop(blob, off, "cpu-handle");
48 		ret = 1;
49 		off = fdt_node_offset_by_prop_value(blob, -1, "cpu-handle",
50 				&cpuhandle, 4);
51 	}
52 
53 	return ret;
54 }
55 
56 void ft_fixup_num_cores(void *blob) {
57 	int off, num_cores, del_cores;
58 
59 	del_cores = 0;
60 	num_cores = cpu_numcores();
61 
62 	off = fdt_node_offset_by_prop_value(blob, -1, "device_type", "cpu", 4);
63 	while (off != -FDT_ERR_NOTFOUND) {
64 		u32 *reg = (u32 *)fdt_getprop(blob, off, "reg", 0);
65 		u32 phys_cpu_id = thread_to_core(*reg);
66 
67 		if (!is_core_valid(phys_cpu_id) || is_core_disabled(phys_cpu_id)) {
68 			int ph = fdt_get_phandle(blob, off);
69 
70 			/* Delete the cpu node once there are no cpu handles */
71 			if (-FDT_ERR_NOTFOUND == ft_del_cpuhandle(blob, ph)) {
72 				fdt_del_node(blob, off);
73 				del_cores++;
74 			}
75 			/* either we deleted some cpu handles or the cpu node
76 			 * so we reset the offset back to the start since we
77 			 * can't trust the offsets anymore
78 			 */
79 			off = -1;
80 		}
81 		off = fdt_node_offset_by_prop_value(blob, off,
82 				"device_type", "cpu", 4);
83 	}
84 	debug ("%x core system found\n", num_cores);
85 	debug ("deleted %d extra core entry entries from device tree\n",
86 								del_cores);
87 }
88 #endif /* defined(CONFIG_MPC85xx) || defined(CONFIG_MPC86xx) */
89 
90 #if defined(CONFIG_HAS_FSL_DR_USB) || defined(CONFIG_HAS_FSL_MPH_USB)
91 static int fdt_fixup_usb_mode_phy_type(void *blob, const char *mode,
92 				const char *phy_type, int start_offset)
93 {
94 	const char *compat_dr = "fsl-usb2-dr";
95 	const char *compat_mph = "fsl-usb2-mph";
96 	const char *prop_mode = "dr_mode";
97 	const char *prop_type = "phy_type";
98 	const char *node_type = NULL;
99 	int node_offset;
100 	int err;
101 
102 	node_offset = fdt_node_offset_by_compatible(blob,
103 			start_offset, compat_mph);
104 	if (node_offset < 0) {
105 		node_offset = fdt_node_offset_by_compatible(blob,
106 			start_offset, compat_dr);
107 		if (node_offset < 0) {
108 			printf("WARNING: could not find compatible"
109 				" node %s or %s: %s.\n", compat_mph,
110 				compat_dr, fdt_strerror(node_offset));
111 			return -1;
112 		} else
113 			node_type = compat_dr;
114 	} else
115 		node_type = compat_mph;
116 
117 	if (mode) {
118 		err = fdt_setprop(blob, node_offset, prop_mode, mode,
119 				  strlen(mode) + 1);
120 		if (err < 0)
121 			printf("WARNING: could not set %s for %s: %s.\n",
122 			       prop_mode, node_type, fdt_strerror(err));
123 	}
124 
125 	if (phy_type) {
126 		err = fdt_setprop(blob, node_offset, prop_type, phy_type,
127 				  strlen(phy_type) + 1);
128 		if (err < 0)
129 			printf("WARNING: could not set %s for %s: %s.\n",
130 			       prop_type, node_type, fdt_strerror(err));
131 	}
132 
133 	return node_offset;
134 }
135 
136 void fdt_fixup_dr_usb(void *blob, bd_t *bd)
137 {
138 	const char *modes[] = { "host", "peripheral", "otg" };
139 	const char *phys[] = { "ulpi", "utmi" };
140 	const char *mode = NULL;
141 	const char *phy_type = NULL;
142 	char usb1_defined = 0;
143 	int usb_mode_off = -1;
144 	int usb_phy_off = -1;
145 	char str[5];
146 	int i, j;
147 
148 	for (i = 1; i <= FSL_MAX_NUM_USB_CTRLS; i++) {
149 		int mode_idx = -1, phy_idx = -1;
150 		snprintf(str, 5, "%s%d", "usb", i);
151 		if (hwconfig(str)) {
152 			for (j = 0; j < ARRAY_SIZE(modes); j++) {
153 				if (hwconfig_subarg_cmp(str, "dr_mode",
154 						modes[j])) {
155 					mode_idx = j;
156 					break;
157 				}
158 			}
159 			for (j = 0; j < ARRAY_SIZE(phys); j++) {
160 				if (hwconfig_subarg_cmp(str, "phy_type",
161 						phys[j])) {
162 					phy_idx = j;
163 					break;
164 				}
165 			}
166 			if (mode_idx >= 0) {
167 				usb_mode_off = fdt_fixup_usb_mode_phy_type(blob,
168 					modes[mode_idx], NULL, usb_mode_off);
169 				if (usb_mode_off < 0)
170 					return;
171 			}
172 			if (phy_idx >= 0) {
173 				usb_phy_off = fdt_fixup_usb_mode_phy_type(blob,
174 					NULL, phys[phy_idx], usb_phy_off);
175 				if (usb_phy_off < 0)
176 					return;
177 			}
178 			if (!strcmp(str, "usb1"))
179 				usb1_defined = 1;
180 			if (mode_idx < 0 && phy_idx < 0)
181 				printf("WARNING: invalid phy or mode\n");
182 		}
183 	}
184 	if (!usb1_defined) {
185 		int usb_off = -1;
186 		mode = getenv("usb_dr_mode");
187 		phy_type = getenv("usb_phy_type");
188 		if (!mode && !phy_type)
189 			return;
190 		fdt_fixup_usb_mode_phy_type(blob, mode, phy_type, usb_off);
191 	}
192 }
193 #endif /* defined(CONFIG_HAS_FSL_DR_USB) || defined(CONFIG_HAS_FSL_MPH_USB) */
194 
195 /*
196  * update crypto node properties to a specified revision of the SEC
197  * called with sec_rev == 0 if not on an E processor
198  */
199 #if CONFIG_SYS_FSL_SEC_COMPAT == 2 /* SEC 2.x/3.x */
200 void fdt_fixup_crypto_node(void *blob, int sec_rev)
201 {
202 	const struct sec_rev_prop {
203 		u32 sec_rev;
204 		u32 num_channels;
205 		u32 channel_fifo_len;
206 		u32 exec_units_mask;
207 		u32 descriptor_types_mask;
208 	} sec_rev_prop_list [] = {
209 		{ 0x0200, 4, 24, 0x07e, 0x01010ebf }, /* SEC 2.0 */
210 		{ 0x0201, 4, 24, 0x0fe, 0x012b0ebf }, /* SEC 2.1 */
211 		{ 0x0202, 1, 24, 0x04c, 0x0122003f }, /* SEC 2.2 */
212 		{ 0x0204, 4, 24, 0x07e, 0x012b0ebf }, /* SEC 2.4 */
213 		{ 0x0300, 4, 24, 0x9fe, 0x03ab0ebf }, /* SEC 3.0 */
214 		{ 0x0301, 4, 24, 0xbfe, 0x03ab0ebf }, /* SEC 3.1 */
215 		{ 0x0303, 4, 24, 0x97c, 0x03a30abf }, /* SEC 3.3 */
216 	};
217 	char compat_strlist[ARRAY_SIZE(sec_rev_prop_list) *
218 			    sizeof("fsl,secX.Y")];
219 	int crypto_node, sec_idx, err;
220 	char *p;
221 	u32 val;
222 
223 	/* locate crypto node based on lowest common compatible */
224 	crypto_node = fdt_node_offset_by_compatible(blob, -1, "fsl,sec2.0");
225 	if (crypto_node == -FDT_ERR_NOTFOUND)
226 		return;
227 
228 	/* delete it if not on an E-processor */
229 	if (crypto_node > 0 && !sec_rev) {
230 		fdt_del_node(blob, crypto_node);
231 		return;
232 	}
233 
234 	/* else we got called for possible uprev */
235 	for (sec_idx = 0; sec_idx < ARRAY_SIZE(sec_rev_prop_list); sec_idx++)
236 		if (sec_rev_prop_list[sec_idx].sec_rev == sec_rev)
237 			break;
238 
239 	if (sec_idx == ARRAY_SIZE(sec_rev_prop_list)) {
240 		puts("warning: unknown SEC revision number\n");
241 		return;
242 	}
243 
244 	val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].num_channels);
245 	err = fdt_setprop(blob, crypto_node, "fsl,num-channels", &val, 4);
246 	if (err < 0)
247 		printf("WARNING: could not set crypto property: %s\n",
248 		       fdt_strerror(err));
249 
250 	val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].descriptor_types_mask);
251 	err = fdt_setprop(blob, crypto_node, "fsl,descriptor-types-mask", &val, 4);
252 	if (err < 0)
253 		printf("WARNING: could not set crypto property: %s\n",
254 		       fdt_strerror(err));
255 
256 	val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].exec_units_mask);
257 	err = fdt_setprop(blob, crypto_node, "fsl,exec-units-mask", &val, 4);
258 	if (err < 0)
259 		printf("WARNING: could not set crypto property: %s\n",
260 		       fdt_strerror(err));
261 
262 	val = cpu_to_fdt32(sec_rev_prop_list[sec_idx].channel_fifo_len);
263 	err = fdt_setprop(blob, crypto_node, "fsl,channel-fifo-len", &val, 4);
264 	if (err < 0)
265 		printf("WARNING: could not set crypto property: %s\n",
266 		       fdt_strerror(err));
267 
268 	val = 0;
269 	while (sec_idx >= 0) {
270 		p = compat_strlist + val;
271 		val += sprintf(p, "fsl,sec%d.%d",
272 			(sec_rev_prop_list[sec_idx].sec_rev & 0xff00) >> 8,
273 			sec_rev_prop_list[sec_idx].sec_rev & 0x00ff) + 1;
274 		sec_idx--;
275 	}
276 	err = fdt_setprop(blob, crypto_node, "compatible", &compat_strlist, val);
277 	if (err < 0)
278 		printf("WARNING: could not set crypto property: %s\n",
279 		       fdt_strerror(err));
280 }
281 #elif CONFIG_SYS_FSL_SEC_COMPAT >= 4  /* SEC4 */
282 void fdt_fixup_crypto_node(void *blob, int sec_rev)
283 {
284 	if (!sec_rev)
285 		fdt_del_node_and_alias(blob, "crypto");
286 }
287 #endif
288 
289 int fdt_fixup_phy_connection(void *blob, int offset, phy_interface_t phyc)
290 {
291 	return fdt_setprop_string(blob, offset, "phy-connection-type",
292 					 phy_string_for_interface(phyc));
293 }
294 
295 #ifdef CONFIG_SYS_SRIO
296 static inline void ft_disable_srio_port(void *blob, int srio_off, int port)
297 {
298 	int off = fdt_node_offset_by_prop_value(blob, srio_off,
299 			"cell-index", &port, 4);
300 	if (off >= 0) {
301 		off = fdt_setprop_string(blob, off, "status", "disabled");
302 		if (off > 0)
303 			printf("WARNING unable to set status for fsl,srio "
304 				"port %d: %s\n", port, fdt_strerror(off));
305 	}
306 }
307 
308 static inline void ft_disable_rman(void *blob)
309 {
310 	int off = fdt_node_offset_by_compatible(blob, -1, "fsl,rman");
311 	if (off >= 0) {
312 		off = fdt_setprop_string(blob, off, "status", "disabled");
313 		if (off > 0)
314 			printf("WARNING unable to set status for fsl,rman %s\n",
315 				fdt_strerror(off));
316 	}
317 }
318 
319 static inline void ft_disable_rmu(void *blob)
320 {
321 	int off = fdt_node_offset_by_compatible(blob, -1, "fsl,srio-rmu");
322 	if (off >= 0) {
323 		off = fdt_setprop_string(blob, off, "status", "disabled");
324 		if (off > 0)
325 			printf("WARNING unable to set status for "
326 				"fsl,srio-rmu %s\n", fdt_strerror(off));
327 	}
328 }
329 
330 void ft_srio_setup(void *blob)
331 {
332 	int srio1_used = 0, srio2_used = 0;
333 	int srio_off;
334 
335 	/* search for srio node, if doesn't exist just return - nothing todo */
336 	srio_off = fdt_node_offset_by_compatible(blob, -1, "fsl,srio");
337 	if (srio_off < 0)
338 		return ;
339 
340 #ifdef CONFIG_SRIO1
341 	if (is_serdes_configured(SRIO1))
342 		srio1_used = 1;
343 #endif
344 #ifdef CONFIG_SRIO2
345 	if (is_serdes_configured(SRIO2))
346 		srio2_used = 1;
347 #endif
348 
349 	/* mark port1 disabled */
350 	if (!srio1_used)
351 		ft_disable_srio_port(blob, srio_off, 1);
352 
353 	/* mark port2 disabled */
354 	if (!srio2_used)
355 		ft_disable_srio_port(blob, srio_off, 2);
356 
357 	/* if both ports not used, disable controller, rmu and rman */
358 	if (!srio1_used && !srio2_used) {
359 		fdt_setprop_string(blob, srio_off, "status", "disabled");
360 
361 		ft_disable_rman(blob);
362 		ft_disable_rmu(blob);
363 	}
364 }
365 #endif
366