• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2014 Freescale Semiconductor, Inc.
4  * Copyright 2017 NXP
5  * Copyright 2017-2018 NXP
6  */
7 #include <common.h>
8 #include <command.h>
9 #include <cpu_func.h>
10 #include <env.h>
11 #include <errno.h>
12 #include <linux/bug.h>
13 #include <asm/io.h>
14 #include <linux/libfdt.h>
15 #include <net.h>
16 #include <fdt_support.h>
17 #include <fsl-mc/fsl_mc.h>
18 #include <fsl-mc/fsl_mc_sys.h>
19 #include <fsl-mc/fsl_mc_private.h>
20 #include <fsl-mc/fsl_dpmng.h>
21 #include <fsl-mc/fsl_dprc.h>
22 #include <fsl-mc/fsl_dpio.h>
23 #include <fsl-mc/fsl_dpni.h>
24 #include <fsl-mc/fsl_qbman_portal.h>
25 #include <fsl-mc/ldpaa_wriop.h>
26 
27 #define MC_RAM_BASE_ADDR_ALIGNMENT  (512UL * 1024 * 1024)
28 #define MC_RAM_BASE_ADDR_ALIGNMENT_MASK	(~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
29 #define MC_RAM_SIZE_ALIGNMENT	    (256UL * 1024 * 1024)
30 
31 #define MC_MEM_SIZE_ENV_VAR	"mcmemsize"
32 #define MC_BOOT_TIMEOUT_ENV_VAR	"mcboottimeout"
33 #define MC_BOOT_ENV_VAR		"mcinitcmd"
34 #define MC_DRAM_BLOCK_DEFAULT_SIZE (512UL * 1024 * 1024)
35 
36 DECLARE_GLOBAL_DATA_PTR;
37 static int mc_memset_resv_ram;
38 static int mc_boot_status = -1;
39 static int mc_dpl_applied = -1;
40 #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
41 static int mc_aiop_applied = -1;
42 #endif
43 struct fsl_mc_io *root_mc_io = NULL;
44 struct fsl_mc_io *dflt_mc_io = NULL; /* child container */
45 uint16_t root_dprc_handle = 0;
46 uint16_t dflt_dprc_handle = 0;
47 int child_dprc_id;
48 struct fsl_dpbp_obj *dflt_dpbp = NULL;
49 struct fsl_dpio_obj *dflt_dpio = NULL;
50 struct fsl_dpni_obj *dflt_dpni = NULL;
51 static u64 mc_lazy_dpl_addr;
52 
53 #ifdef DEBUG
dump_ram_words(const char * title,void * addr)54 void dump_ram_words(const char *title, void *addr)
55 {
56 	int i;
57 	uint32_t *words = addr;
58 
59 	printf("Dumping beginning of %s (%p):\n", title, addr);
60 	for (i = 0; i < 16; i++)
61 		printf("%#x ", words[i]);
62 
63 	printf("\n");
64 }
65 
dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem * mc_ccsr_regs)66 void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
67 {
68 	printf("MC CCSR registers:\n"
69 		"reg_gcr1 %#x\n"
70 		"reg_gsr %#x\n"
71 		"reg_sicbalr %#x\n"
72 		"reg_sicbahr %#x\n"
73 		"reg_sicapr %#x\n"
74 		"reg_mcfbalr %#x\n"
75 		"reg_mcfbahr %#x\n"
76 		"reg_mcfapr %#x\n"
77 		"reg_psr %#x\n",
78 		mc_ccsr_regs->reg_gcr1,
79 		mc_ccsr_regs->reg_gsr,
80 		mc_ccsr_regs->reg_sicbalr,
81 		mc_ccsr_regs->reg_sicbahr,
82 		mc_ccsr_regs->reg_sicapr,
83 		mc_ccsr_regs->reg_mcfbalr,
84 		mc_ccsr_regs->reg_mcfbahr,
85 		mc_ccsr_regs->reg_mcfapr,
86 		mc_ccsr_regs->reg_psr);
87 }
88 #else
89 
90 #define dump_ram_words(title, addr)
91 #define dump_mc_ccsr_regs(mc_ccsr_regs)
92 
93 #endif /* DEBUG */
94 
95 #ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
96 /**
97  * Copying MC firmware or DPL image to DDR
98  */
mc_copy_image(const char * title,u64 image_addr,u32 image_size,u64 mc_ram_addr)99 static int mc_copy_image(const char *title,
100 			 u64 image_addr, u32 image_size, u64 mc_ram_addr)
101 {
102 	debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
103 	memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
104 	flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
105 	return 0;
106 }
107 
108 /**
109  * MC firmware FIT image parser checks if the image is in FIT
110  * format, verifies integrity of the image and calculates
111  * raw image address and size values.
112  * Returns 0 on success and a negative errno on error.
113  * task fail.
114  **/
parse_mc_firmware_fit_image(u64 mc_fw_addr,const void ** raw_image_addr,size_t * raw_image_size)115 int parse_mc_firmware_fit_image(u64 mc_fw_addr,
116 				const void **raw_image_addr,
117 				size_t *raw_image_size)
118 {
119 	int format;
120 	void *fit_hdr;
121 	int node_offset;
122 	const void *data;
123 	size_t size;
124 	const char *uname = "firmware";
125 
126 	fit_hdr = (void *)mc_fw_addr;
127 
128 	/* Check if Image is in FIT format */
129 	format = genimg_get_format(fit_hdr);
130 
131 	if (format != IMAGE_FORMAT_FIT) {
132 		printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
133 		return -EINVAL;
134 	}
135 
136 	if (!fit_check_format(fit_hdr)) {
137 		printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
138 		return -EINVAL;
139 	}
140 
141 	node_offset = fit_image_get_node(fit_hdr, uname);
142 
143 	if (node_offset < 0) {
144 		printf("fsl-mc: ERR: Bad firmware image (missing subimage)\n");
145 		return -ENOENT;
146 	}
147 
148 	/* Verify MC firmware image */
149 	if (!(fit_image_verify(fit_hdr, node_offset))) {
150 		printf("fsl-mc: ERR: Bad firmware image (bad CRC)\n");
151 		return -EINVAL;
152 	}
153 
154 	/* Get address and size of raw image */
155 	fit_image_get_data(fit_hdr, node_offset, &data, &size);
156 
157 	*raw_image_addr = data;
158 	*raw_image_size = size;
159 
160 	return 0;
161 }
162 #endif
163 
164 #define MC_DT_INCREASE_SIZE	64
165 
166 enum mc_fixup_type {
167 	MC_FIXUP_DPL,
168 	MC_FIXUP_DPC
169 };
170 
mc_fixup_mac_addr(void * blob,int nodeoffset,const char * propname,struct eth_device * eth_dev,enum mc_fixup_type type)171 static int mc_fixup_mac_addr(void *blob, int nodeoffset,
172 			     const char *propname, struct eth_device *eth_dev,
173 			     enum mc_fixup_type type)
174 {
175 	int err = 0, len = 0, size, i;
176 	unsigned char env_enetaddr[ARP_HLEN];
177 	unsigned int enetaddr_32[ARP_HLEN];
178 	void *val = NULL;
179 
180 	switch (type) {
181 	case MC_FIXUP_DPL:
182 	/* DPL likes its addresses on 32 * ARP_HLEN bits */
183 	for (i = 0; i < ARP_HLEN; i++)
184 		enetaddr_32[i] = cpu_to_fdt32(eth_dev->enetaddr[i]);
185 	val = enetaddr_32;
186 	len = sizeof(enetaddr_32);
187 	break;
188 
189 	case MC_FIXUP_DPC:
190 	val = eth_dev->enetaddr;
191 	len = ARP_HLEN;
192 	break;
193 	}
194 
195 	/* MAC address property present */
196 	if (fdt_get_property(blob, nodeoffset, propname, NULL)) {
197 		/* u-boot MAC addr randomly assigned - leave the present one */
198 		if (!eth_env_get_enetaddr_by_index("eth", eth_dev->index,
199 						   env_enetaddr))
200 			return err;
201 	} else {
202 		size = MC_DT_INCREASE_SIZE + strlen(propname) + len;
203 		/* make room for mac address property */
204 		err = fdt_increase_size(blob, size);
205 		if (err) {
206 			printf("fdt_increase_size: err=%s\n",
207 			       fdt_strerror(err));
208 			return err;
209 		}
210 	}
211 
212 	err = fdt_setprop(blob, nodeoffset, propname, val, len);
213 	if (err) {
214 		printf("fdt_setprop: err=%s\n", fdt_strerror(err));
215 		return err;
216 	}
217 
218 	return err;
219 }
220 
221 #define is_dpni(s) (s != NULL ? !strncmp(s, "dpni@", 5) : 0)
222 
dpl_get_connection_endpoint(void * blob,char * endpoint)223 const char *dpl_get_connection_endpoint(void *blob, char *endpoint)
224 {
225 	int connoffset = fdt_path_offset(blob, "/connections"), off;
226 	const char *s1, *s2;
227 
228 	for (off = fdt_first_subnode(blob, connoffset);
229 	     off >= 0;
230 	     off = fdt_next_subnode(blob, off)) {
231 		s1 = fdt_stringlist_get(blob, off, "endpoint1", 0, NULL);
232 		s2 = fdt_stringlist_get(blob, off, "endpoint2", 0, NULL);
233 
234 		if (!s1 || !s2)
235 			continue;
236 
237 		if (strcmp(endpoint, s1) == 0)
238 			return s2;
239 
240 		if (strcmp(endpoint, s2) == 0)
241 			return s1;
242 	}
243 
244 	return NULL;
245 }
246 
mc_fixup_dpl_mac_addr(void * blob,int dpmac_id,struct eth_device * eth_dev)247 static int mc_fixup_dpl_mac_addr(void *blob, int dpmac_id,
248 				 struct eth_device *eth_dev)
249 {
250 	int objoff = fdt_path_offset(blob, "/objects");
251 	int dpmacoff = -1, dpnioff = -1;
252 	const char *endpoint;
253 	char mac_name[10];
254 	int err;
255 
256 	sprintf(mac_name, "dpmac@%d", dpmac_id);
257 	dpmacoff = fdt_subnode_offset(blob, objoff, mac_name);
258 	if (dpmacoff < 0)
259 		/* dpmac not defined in DPL, so skip it. */
260 		return 0;
261 
262 	err = mc_fixup_mac_addr(blob, dpmacoff, "mac_addr", eth_dev,
263 				MC_FIXUP_DPL);
264 	if (err) {
265 		printf("Error fixing up dpmac mac_addr in DPL\n");
266 		return err;
267 	}
268 
269 	/* now we need to figure out if there is any
270 	 * DPNI connected to this MAC, so we walk the
271 	 * connection list
272 	 */
273 	endpoint = dpl_get_connection_endpoint(blob, mac_name);
274 	if (!is_dpni(endpoint))
275 		return 0;
276 
277 	/* let's see if we can fixup the DPNI as well */
278 	dpnioff = fdt_subnode_offset(blob, objoff, endpoint);
279 	if (dpnioff < 0)
280 		/* DPNI not defined in DPL in the objects area */
281 		return 0;
282 
283 	return mc_fixup_mac_addr(blob, dpnioff, "mac_addr", eth_dev,
284 				 MC_FIXUP_DPL);
285 }
286 
fdt_fixup_mc_ddr(u64 * base,u64 * size)287 void fdt_fixup_mc_ddr(u64 *base, u64 *size)
288 {
289 	u64 mc_size = mc_get_dram_block_size();
290 
291 	if (mc_size < MC_DRAM_BLOCK_DEFAULT_SIZE) {
292 		*base = mc_get_dram_addr() + mc_size;
293 		*size = MC_DRAM_BLOCK_DEFAULT_SIZE - mc_size;
294 	}
295 }
296 
fdt_fsl_mc_fixup_iommu_map_entry(void * blob)297 void fdt_fsl_mc_fixup_iommu_map_entry(void *blob)
298 {
299 	u32 *prop;
300 	u32 iommu_map[4];
301 	int offset;
302 	int lenp;
303 
304 	/* find fsl-mc node */
305 	offset = fdt_path_offset(blob, "/soc/fsl-mc");
306 	if (offset < 0)
307 		offset = fdt_path_offset(blob, "/fsl-mc");
308 	if (offset < 0) {
309 		printf("%s: fsl-mc: ERR: fsl-mc node not found in DT, err %d\n",
310 		       __func__, offset);
311 		return;
312 	}
313 
314 	prop = fdt_getprop_w(blob, offset, "iommu-map", &lenp);
315 	if (!prop) {
316 		debug("%s: fsl-mc: ERR: missing iommu-map in fsl-mc bus node\n",
317 		      __func__);
318 		return;
319 	}
320 
321 	iommu_map[0] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
322 	iommu_map[1] = *++prop;
323 	iommu_map[2] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_START);
324 	iommu_map[3] = cpu_to_fdt32(FSL_DPAA2_STREAM_ID_END -
325 		FSL_DPAA2_STREAM_ID_START + 1);
326 
327 	fdt_setprop_inplace(blob, offset, "iommu-map",
328 			    iommu_map, sizeof(iommu_map));
329 }
330 
mc_fixup_dpc_mac_addr(void * blob,int dpmac_id,struct eth_device * eth_dev)331 static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
332 				 struct eth_device *eth_dev)
333 {
334 	int nodeoffset = fdt_path_offset(blob, "/board_info/ports"), noff;
335 	int err = 0;
336 	char mac_name[10];
337 	const char link_type_mode[] = "MAC_LINK_TYPE_FIXED";
338 
339 	sprintf(mac_name, "mac@%d", dpmac_id);
340 
341 	/* node not found - create it */
342 	noff = fdt_subnode_offset(blob, nodeoffset, (const char *)mac_name);
343 	if (noff < 0) {
344 		err = fdt_increase_size(blob, 200);
345 		if (err) {
346 			printf("fdt_increase_size: err=%s\n",
347 				fdt_strerror(err));
348 			return err;
349 		}
350 
351 		noff = fdt_add_subnode(blob, nodeoffset, mac_name);
352 		if (noff < 0) {
353 			printf("fdt_add_subnode: err=%s\n",
354 			       fdt_strerror(err));
355 			return err;
356 		}
357 
358 		/* add default property of fixed link */
359 		err = fdt_appendprop_string(blob, noff,
360 					    "link_type", link_type_mode);
361 		if (err) {
362 			printf("fdt_appendprop_string: err=%s\n",
363 				fdt_strerror(err));
364 			return err;
365 		}
366 	}
367 
368 	return mc_fixup_mac_addr(blob, noff, "port_mac_address", eth_dev,
369 				 MC_FIXUP_DPC);
370 }
371 
mc_fixup_mac_addrs(void * blob,enum mc_fixup_type type)372 static int mc_fixup_mac_addrs(void *blob, enum mc_fixup_type type)
373 {
374 	int i, err = 0, ret = 0;
375 	char ethname[ETH_NAME_LEN];
376 	struct eth_device *eth_dev;
377 
378 	for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
379 		/* port not enabled */
380 		if (wriop_is_enabled_dpmac(i) != 1)
381 			continue;
382 
383 		snprintf(ethname, ETH_NAME_LEN, "DPMAC%d@%s", i,
384 			 phy_interface_strings[wriop_get_enet_if(i)]);
385 
386 		eth_dev = eth_get_dev_by_name(ethname);
387 		if (eth_dev == NULL)
388 			continue;
389 
390 		switch (type) {
391 		case MC_FIXUP_DPL:
392 			err = mc_fixup_dpl_mac_addr(blob, i, eth_dev);
393 			break;
394 		case MC_FIXUP_DPC:
395 			err = mc_fixup_dpc_mac_addr(blob, i, eth_dev);
396 			break;
397 		default:
398 			break;
399 		}
400 
401 		if (err)
402 			printf("fsl-mc: ERROR fixing mac address for %s\n",
403 			       ethname);
404 		ret |= err;
405 	}
406 
407 	return ret;
408 }
409 
mc_fixup_dpc(u64 dpc_addr)410 static int mc_fixup_dpc(u64 dpc_addr)
411 {
412 	void *blob = (void *)dpc_addr;
413 	int nodeoffset, err = 0;
414 
415 	/* delete any existing ICID pools */
416 	nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
417 	if (fdt_del_node(blob, nodeoffset) < 0)
418 		printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
419 
420 	/* add a new pool */
421 	nodeoffset = fdt_path_offset(blob, "/resources");
422 	if (nodeoffset < 0) {
423 		printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
424 		return -EINVAL;
425 	}
426 	nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
427 	nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
428 	do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
429 			     "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
430 	do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
431 			     "num",
432 			     FSL_DPAA2_STREAM_ID_END -
433 			     FSL_DPAA2_STREAM_ID_START + 1, 1);
434 
435 	/* fixup MAC addresses for dpmac ports */
436 	nodeoffset = fdt_path_offset(blob, "/board_info/ports");
437 	if (nodeoffset < 0)
438 		goto out;
439 
440 	err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPC);
441 
442 out:
443 	flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
444 
445 	return err;
446 }
447 
load_mc_dpc(u64 mc_ram_addr,size_t mc_ram_size,u64 mc_dpc_addr)448 static int load_mc_dpc(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpc_addr)
449 {
450 	u64 mc_dpc_offset;
451 #ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
452 	int error;
453 	void *dpc_fdt_hdr;
454 	int dpc_size;
455 #endif
456 
457 #ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
458 	BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
459 		     CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
460 
461 	mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
462 #else
463 #error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
464 #endif
465 
466 	/*
467 	 * Load the MC DPC blob in the MC private DRAM block:
468 	 */
469 #ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
470 	printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
471 #else
472 	/*
473 	 * Get address and size of the DPC blob stored in flash:
474 	 */
475 	dpc_fdt_hdr = (void *)mc_dpc_addr;
476 
477 	error = fdt_check_header(dpc_fdt_hdr);
478 	if (error != 0) {
479 		/*
480 		 * Don't return with error here, since the MC firmware can
481 		 * still boot without a DPC
482 		 */
483 		printf("\nfsl-mc: WARNING: No DPC image found");
484 		return 0;
485 	}
486 
487 	dpc_size = fdt_totalsize(dpc_fdt_hdr);
488 	if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
489 		printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
490 		       dpc_size);
491 		return -EINVAL;
492 	}
493 
494 	mc_copy_image("MC DPC blob",
495 		      (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
496 #endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
497 
498 	if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
499 		return -EINVAL;
500 
501 	dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
502 	return 0;
503 }
504 
505 
mc_fixup_dpl(u64 dpl_addr)506 static int mc_fixup_dpl(u64 dpl_addr)
507 {
508 	void *blob = (void *)dpl_addr;
509 	u32 ver = fdt_getprop_u32_default(blob, "/", "dpl-version", 0);
510 	int err = 0;
511 
512 	/* The DPL fixup for mac addresses is only relevant
513 	 * for old-style DPLs
514 	 */
515 	if (ver >= 10)
516 		return 0;
517 
518 	err = mc_fixup_mac_addrs(blob, MC_FIXUP_DPL);
519 	flush_dcache_range(dpl_addr, dpl_addr + fdt_totalsize(blob));
520 
521 	return err;
522 }
523 
load_mc_dpl(u64 mc_ram_addr,size_t mc_ram_size,u64 mc_dpl_addr)524 static int load_mc_dpl(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpl_addr)
525 {
526 	u64 mc_dpl_offset;
527 #ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
528 	int error;
529 	void *dpl_fdt_hdr;
530 	int dpl_size;
531 #endif
532 
533 #ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
534 	BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
535 		     CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
536 
537 	mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
538 #else
539 #error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
540 #endif
541 
542 	/*
543 	 * Load the MC DPL blob in the MC private DRAM block:
544 	 */
545 #ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
546 	printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
547 #else
548 	/*
549 	 * Get address and size of the DPL blob stored in flash:
550 	 */
551 	dpl_fdt_hdr = (void *)mc_dpl_addr;
552 
553 	error = fdt_check_header(dpl_fdt_hdr);
554 	if (error != 0) {
555 		printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
556 		return error;
557 	}
558 
559 	dpl_size = fdt_totalsize(dpl_fdt_hdr);
560 	if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
561 		printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
562 		       dpl_size);
563 		return -EINVAL;
564 	}
565 
566 	mc_copy_image("MC DPL blob",
567 		      (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
568 #endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
569 
570 	if (mc_fixup_dpl(mc_ram_addr + mc_dpl_offset))
571 		return -EINVAL;
572 	dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
573 	return 0;
574 }
575 
576 /**
577  * Return the MC boot timeout value in milliseconds
578  */
get_mc_boot_timeout_ms(void)579 static unsigned long get_mc_boot_timeout_ms(void)
580 {
581 	unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
582 
583 	char *timeout_ms_env_var = env_get(MC_BOOT_TIMEOUT_ENV_VAR);
584 
585 	if (timeout_ms_env_var) {
586 		timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
587 		if (timeout_ms == 0) {
588 			printf("fsl-mc: WARNING: Invalid value for \'"
589 			       MC_BOOT_TIMEOUT_ENV_VAR
590 			       "\' environment variable: %lu\n",
591 			       timeout_ms);
592 
593 			timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
594 		}
595 	}
596 
597 	return timeout_ms;
598 }
599 
600 #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
601 
soc_has_aiop(void)602 __weak bool soc_has_aiop(void)
603 {
604 	return false;
605 }
606 
load_mc_aiop_img(u64 aiop_fw_addr)607 static int load_mc_aiop_img(u64 aiop_fw_addr)
608 {
609 	u64 mc_ram_addr = mc_get_dram_addr();
610 #ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
611 	void *aiop_img;
612 #endif
613 
614 	/* Check if AIOP is available */
615 	if (!soc_has_aiop())
616 		return -ENODEV;
617 	/*
618 	 * Load the MC AIOP image in the MC private DRAM block:
619 	 */
620 
621 #ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
622 	printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
623 	       CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
624 #else
625 	aiop_img = (void *)aiop_fw_addr;
626 	mc_copy_image("MC AIOP image",
627 		      (u64)aiop_img, CONFIG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
628 		      mc_ram_addr + CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
629 #endif
630 	mc_aiop_applied = 0;
631 
632 	return 0;
633 }
634 #endif
635 
wait_for_mc(bool booting_mc,u32 * final_reg_gsr)636 static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
637 {
638 	u32 reg_gsr;
639 	u32 mc_fw_boot_status;
640 	unsigned long timeout_ms = get_mc_boot_timeout_ms();
641 	struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
642 
643 	dmb();
644 	assert(timeout_ms > 0);
645 	for (;;) {
646 		udelay(1000);	/* throttle polling */
647 		reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
648 		mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
649 		if (mc_fw_boot_status & 0x1)
650 			break;
651 
652 		timeout_ms--;
653 		if (timeout_ms == 0)
654 			break;
655 	}
656 
657 	if (timeout_ms == 0) {
658 		printf("ERROR: timeout\n");
659 
660 		/* TODO: Get an error status from an MC CCSR register */
661 		return -ETIMEDOUT;
662 	}
663 
664 	if (mc_fw_boot_status != 0x1) {
665 		/*
666 		 * TODO: Identify critical errors from the GSR register's FS
667 		 * field and for those errors, set error to -ENODEV or other
668 		 * appropriate errno, so that the status property is set to
669 		 * failure in the fsl,dprc device tree node.
670 		 */
671 		printf("WARNING: Firmware returned an error (GSR: %#x)\n",
672 		       reg_gsr);
673 	} else {
674 		printf("SUCCESS\n");
675 	}
676 
677 
678 	*final_reg_gsr = reg_gsr;
679 	return 0;
680 }
681 
mc_init(u64 mc_fw_addr,u64 mc_dpc_addr)682 int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
683 {
684 	int error = 0;
685 	int portal_id = 0;
686 	struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
687 	u64 mc_ram_addr = mc_get_dram_addr();
688 	u32 reg_gsr;
689 	u32 reg_mcfbalr;
690 #ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
691 	const void *raw_image_addr;
692 	size_t raw_image_size = 0;
693 #endif
694 	struct mc_version mc_ver_info;
695 	u8 mc_ram_num_256mb_blocks;
696 	size_t mc_ram_size = mc_get_dram_block_size();
697 
698 	mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
699 
700 	if (mc_ram_num_256mb_blocks >= 0xff) {
701 		error = -EINVAL;
702 		printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
703 		       mc_ram_size);
704 		goto out;
705 	}
706 
707 	/*
708 	 * To support 128 MB DDR Size for MC
709 	 */
710 	if (mc_ram_num_256mb_blocks == 0)
711 		mc_ram_num_256mb_blocks = 0xFF;
712 
713 	/*
714 	 * Management Complex cores should be held at reset out of POR.
715 	 * U-Boot should be the first software to touch MC. To be safe,
716 	 * we reset all cores again by setting GCR1 to 0. It doesn't do
717 	 * anything if they are held at reset. After we setup the firmware
718 	 * we kick off MC by deasserting the reset bit for core 0, and
719 	 * deasserting the reset bits for Command Portal Managers.
720 	 * The stop bits are not touched here. They are used to stop the
721 	 * cores when they are active. Setting stop bits doesn't stop the
722 	 * cores from fetching instructions when they are released from
723 	 * reset.
724 	 */
725 	out_le32(&mc_ccsr_regs->reg_gcr1, 0);
726 	dmb();
727 
728 #ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
729 	printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
730 #else
731 	error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
732 					    &raw_image_size);
733 	if (error != 0)
734 		goto out;
735 	/*
736 	 * Load the MC FW at the beginning of the MC private DRAM block:
737 	 */
738 	mc_copy_image("MC Firmware",
739 		      (u64)raw_image_addr, raw_image_size, mc_ram_addr);
740 #endif
741 	dump_ram_words("firmware", (void *)mc_ram_addr);
742 
743 	error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
744 	if (error != 0)
745 		goto out;
746 
747 	debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
748 	dump_mc_ccsr_regs(mc_ccsr_regs);
749 
750 	/*
751 	 * Tell MC what is the address range of the DRAM block assigned to it:
752 	 */
753 	if (mc_ram_num_256mb_blocks < 0xFF) {
754 		reg_mcfbalr = (u32)mc_ram_addr |
755 				(mc_ram_num_256mb_blocks - 1);
756 	} else {
757 		reg_mcfbalr = (u32)mc_ram_addr |
758 				(mc_ram_num_256mb_blocks);
759 	}
760 
761 	out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
762 	out_le32(&mc_ccsr_regs->reg_mcfbahr,
763 		 (u32)(mc_ram_addr >> 32));
764 	out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
765 
766 	/*
767 	 * Tell the MC that we want delayed DPL deployment.
768 	 */
769 	out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
770 
771 	printf("\nfsl-mc: Booting Management Complex ... ");
772 
773 	/*
774 	 * Deassert reset and release MC core 0 to run
775 	 */
776 	out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
777 	error = wait_for_mc(true, &reg_gsr);
778 	if (error != 0)
779 		goto out;
780 
781 	/*
782 	 * TODO: need to obtain the portal_id for the root container from the
783 	 * DPL
784 	 */
785 	portal_id = 0;
786 
787 	/*
788 	 * Initialize the global default MC portal
789 	 * And check that the MC firmware is responding portal commands:
790 	 */
791 	root_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
792 	if (!root_mc_io) {
793 		printf(" No memory: calloc() failed\n");
794 		return -ENOMEM;
795 	}
796 
797 	root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
798 	debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
799 	      portal_id, root_mc_io->mmio_regs);
800 
801 	error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
802 	if (error != 0) {
803 		printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
804 		       error);
805 		goto out;
806 	}
807 
808 	printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
809 	       mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
810 	       reg_gsr & GSR_FS_MASK);
811 
812 out:
813 	if (error != 0)
814 		mc_boot_status = error;
815 	else
816 		mc_boot_status = 0;
817 
818 	return error;
819 }
820 
mc_apply_dpl(u64 mc_dpl_addr)821 int mc_apply_dpl(u64 mc_dpl_addr)
822 {
823 	struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
824 	int error = 0;
825 	u32 reg_gsr;
826 	u64 mc_ram_addr = mc_get_dram_addr();
827 	size_t mc_ram_size = mc_get_dram_block_size();
828 
829 	if (!mc_dpl_addr)
830 		return -1;
831 
832 	error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
833 	if (error != 0)
834 		return error;
835 
836 	/*
837 	 * Tell the MC to deploy the DPL:
838 	 */
839 	out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
840 	printf("fsl-mc: Deploying data path layout ... ");
841 	error = wait_for_mc(false, &reg_gsr);
842 
843 	if (!error)
844 		mc_dpl_applied = 0;
845 
846 	return error;
847 }
848 
get_mc_boot_status(void)849 int get_mc_boot_status(void)
850 {
851 	return mc_boot_status;
852 }
853 
854 #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
get_aiop_apply_status(void)855 int get_aiop_apply_status(void)
856 {
857 	return mc_aiop_applied;
858 }
859 #endif
860 
get_dpl_apply_status(void)861 int get_dpl_apply_status(void)
862 {
863 	return mc_dpl_applied;
864 }
865 
is_lazy_dpl_addr_valid(void)866 int is_lazy_dpl_addr_valid(void)
867 {
868 	return !!mc_lazy_dpl_addr;
869 }
870 
871 /*
872  * Return the MC address of private DRAM block.
873  * As per MC design document, MC initial base address
874  * should be least significant 512MB address of MC private
875  * memory, i.e. address should point to end address masked
876  * with 512MB offset in private DRAM block.
877  */
mc_get_dram_addr(void)878 u64 mc_get_dram_addr(void)
879 {
880 	size_t mc_ram_size = mc_get_dram_block_size();
881 
882 	if (!mc_memset_resv_ram || (get_mc_boot_status() < 0)) {
883 		mc_memset_resv_ram = 1;
884 		memset((void *)gd->arch.resv_ram, 0, mc_ram_size);
885 	}
886 
887 	return (gd->arch.resv_ram + mc_ram_size - 1) &
888 		MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
889 }
890 
891 /**
892  * Return the actual size of the MC private DRAM block.
893  */
mc_get_dram_block_size(void)894 unsigned long mc_get_dram_block_size(void)
895 {
896 	unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
897 
898 	char *dram_block_size_env_var = env_get(MC_MEM_SIZE_ENV_VAR);
899 
900 	if (dram_block_size_env_var) {
901 		dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
902 						 16);
903 
904 		if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
905 			printf("fsl-mc: WARNING: Invalid value for \'"
906 			       MC_MEM_SIZE_ENV_VAR
907 			       "\' environment variable: %lu\n",
908 			       dram_block_size);
909 
910 			dram_block_size = MC_DRAM_BLOCK_DEFAULT_SIZE;
911 		}
912 	}
913 
914 	return dram_block_size;
915 }
916 
fsl_mc_ldpaa_init(bd_t * bis)917 int fsl_mc_ldpaa_init(bd_t *bis)
918 {
919 	int i;
920 
921 	for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
922 		if (wriop_is_enabled_dpmac(i) == 1)
923 			ldpaa_eth_init(i, wriop_get_enet_if(i));
924 	return 0;
925 }
926 
dprc_version_check(struct fsl_mc_io * mc_io,uint16_t handle)927 static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
928 {
929 	int error;
930 	uint16_t major_ver, minor_ver;
931 
932 	error = dprc_get_api_version(mc_io, 0,
933 				     &major_ver,
934 				     &minor_ver);
935 	if (error < 0) {
936 		printf("dprc_get_api_version() failed: %d\n", error);
937 		return error;
938 	}
939 
940 	if (major_ver < DPRC_VER_MAJOR || (major_ver == DPRC_VER_MAJOR &&
941 					   minor_ver < DPRC_VER_MINOR)) {
942 		printf("DPRC version mismatch found %u.%u,",
943 		       major_ver, minor_ver);
944 		printf("supported version is %u.%u\n",
945 		       DPRC_VER_MAJOR, DPRC_VER_MINOR);
946 	}
947 
948 	return error;
949 }
950 
dpio_init(void)951 static int dpio_init(void)
952 {
953 	struct qbman_swp_desc p_des;
954 	struct dpio_attr attr;
955 	struct dpio_cfg dpio_cfg;
956 	int err = 0;
957 	uint16_t major_ver, minor_ver;
958 
959 	dflt_dpio = (struct fsl_dpio_obj *)calloc(
960 					sizeof(struct fsl_dpio_obj), 1);
961 	if (!dflt_dpio) {
962 		printf("No memory: calloc() failed\n");
963 		err = -ENOMEM;
964 		goto err_calloc;
965 	}
966 	dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
967 	dpio_cfg.num_priorities = 8;
968 
969 	err = dpio_create(dflt_mc_io,
970 			  dflt_dprc_handle,
971 			  MC_CMD_NO_FLAGS,
972 			  &dpio_cfg,
973 			  &dflt_dpio->dpio_id);
974 	if (err < 0) {
975 		printf("dpio_create() failed: %d\n", err);
976 		err = -ENODEV;
977 		goto err_create;
978 	}
979 
980 	err = dpio_get_api_version(dflt_mc_io, 0,
981 				   &major_ver,
982 				   &minor_ver);
983 	if (err < 0) {
984 		printf("dpio_get_api_version() failed: %d\n", err);
985 		goto err_get_api_ver;
986 	}
987 
988 	if (major_ver < DPIO_VER_MAJOR || (major_ver == DPIO_VER_MAJOR &&
989 					   minor_ver < DPIO_VER_MINOR)) {
990 		printf("DPRC version mismatch found %u.%u,",
991 		       major_ver,
992 		       minor_ver);
993 	}
994 
995 	err = dpio_open(dflt_mc_io,
996 			MC_CMD_NO_FLAGS,
997 			dflt_dpio->dpio_id,
998 			&dflt_dpio->dpio_handle);
999 	if (err) {
1000 		printf("dpio_open() failed\n");
1001 		goto err_open;
1002 	}
1003 
1004 	memset(&attr, 0, sizeof(struct dpio_attr));
1005 	err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1006 				  dflt_dpio->dpio_handle, &attr);
1007 	if (err < 0) {
1008 		printf("dpio_get_attributes() failed: %d\n", err);
1009 		goto err_get_attr;
1010 	}
1011 
1012 	if (dflt_dpio->dpio_id != attr.id) {
1013 		printf("dnpi object id and attribute id are not same\n");
1014 		goto err_attr_not_same;
1015 	}
1016 
1017 #ifdef DEBUG
1018 	printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
1019 #endif
1020 	err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1021 	if (err < 0) {
1022 		printf("dpio_enable() failed %d\n", err);
1023 		goto err_get_enable;
1024 	}
1025 	debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
1026 	      attr.qbman_portal_ce_offset,
1027 	      attr.qbman_portal_ci_offset,
1028 	      attr.qbman_portal_id,
1029 	      attr.num_priorities);
1030 
1031 	p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1032 					+ attr.qbman_portal_ce_offset);
1033 	p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
1034 					+ attr.qbman_portal_ci_offset);
1035 
1036 	dflt_dpio->sw_portal = qbman_swp_init(&p_des);
1037 	if (dflt_dpio->sw_portal == NULL) {
1038 		printf("qbman_swp_init() failed\n");
1039 		goto err_get_swp_init;
1040 	}
1041 	return 0;
1042 
1043 err_get_swp_init:
1044 	dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1045 err_get_enable:
1046 err_get_attr:
1047 err_attr_not_same:
1048 	dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1049 err_open:
1050 err_get_api_ver:
1051 	dpio_destroy(dflt_mc_io,
1052 		     dflt_dprc_handle,
1053 		     MC_CMD_NO_FLAGS,
1054 		     dflt_dpio->dpio_id);
1055 err_create:
1056 	free(dflt_dpio);
1057 err_calloc:
1058 	return err;
1059 }
1060 
dpio_exit(void)1061 static int dpio_exit(void)
1062 {
1063 	int err;
1064 
1065 	err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1066 	if (err < 0) {
1067 		printf("dpio_disable() failed: %d\n", err);
1068 		goto err;
1069 	}
1070 
1071 	dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
1072 	if (err < 0) {
1073 		printf("dpio_close() failed: %d\n", err);
1074 		goto err;
1075 	}
1076 
1077 	err = dpio_destroy(dflt_mc_io,
1078 			   dflt_dprc_handle,
1079 			   MC_CMD_NO_FLAGS,
1080 			   dflt_dpio->dpio_id);
1081 	if (err < 0) {
1082 		printf("dpio_destroy() failed: %d\n", err);
1083 		goto err;
1084 	}
1085 
1086 #ifdef DEBUG
1087 	printf("Exit: DPIO id=0x%d\n", dflt_dpio->dpio_id);
1088 #endif
1089 
1090 	if (dflt_dpio)
1091 		free(dflt_dpio);
1092 
1093 	return 0;
1094 err:
1095 	return err;
1096 }
1097 
dprc_init(void)1098 static int dprc_init(void)
1099 {
1100 	int err, child_portal_id, container_id;
1101 	struct dprc_cfg cfg;
1102 	uint64_t mc_portal_offset;
1103 
1104 	/* Open root container */
1105 	err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
1106 	if (err < 0) {
1107 		printf("dprc_get_container_id(): Root failed: %d\n", err);
1108 		goto err_root_container_id;
1109 	}
1110 
1111 #ifdef DEBUG
1112 	printf("Root container id = %d\n", container_id);
1113 #endif
1114 	err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
1115 			&root_dprc_handle);
1116 	if (err < 0) {
1117 		printf("dprc_open(): Root Container failed: %d\n", err);
1118 		goto err_root_open;
1119 	}
1120 
1121 	if (!root_dprc_handle) {
1122 		printf("dprc_open(): Root Container Handle is not valid\n");
1123 		goto err_root_open;
1124 	}
1125 
1126 	err = dprc_version_check(root_mc_io, root_dprc_handle);
1127 	if (err < 0) {
1128 		printf("dprc_version_check() failed: %d\n", err);
1129 		goto err_root_open;
1130 	}
1131 
1132 	memset(&cfg, 0, sizeof(struct dprc_cfg));
1133 	cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
1134 		      DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
1135 		      DPRC_CFG_OPT_ALLOC_ALLOWED;
1136 	cfg.icid = DPRC_GET_ICID_FROM_POOL;
1137 	cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
1138 	err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
1139 			root_dprc_handle,
1140 			&cfg,
1141 			&child_dprc_id,
1142 			&mc_portal_offset);
1143 	if (err < 0) {
1144 		printf("dprc_create_container() failed: %d\n", err);
1145 		goto err_create;
1146 	}
1147 
1148 	dflt_mc_io = (struct fsl_mc_io *)calloc(sizeof(struct fsl_mc_io), 1);
1149 	if (!dflt_mc_io) {
1150 		err  = -ENOMEM;
1151 		printf(" No memory: calloc() failed\n");
1152 		goto err_calloc;
1153 	}
1154 
1155 	child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
1156 	dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
1157 
1158 #ifdef DEBUG
1159 	printf("MC portal of child DPRC container: %d, physical addr %p)\n",
1160 	       child_dprc_id, dflt_mc_io->mmio_regs);
1161 #endif
1162 
1163 	err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
1164 			&dflt_dprc_handle);
1165 	if (err < 0) {
1166 		printf("dprc_open(): Child container failed: %d\n", err);
1167 		goto err_child_open;
1168 	}
1169 
1170 	if (!dflt_dprc_handle) {
1171 		printf("dprc_open(): Child container Handle is not valid\n");
1172 		goto err_child_open;
1173 	}
1174 
1175 	return 0;
1176 err_child_open:
1177 	free(dflt_mc_io);
1178 err_calloc:
1179 	dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1180 			       root_dprc_handle, child_dprc_id);
1181 err_create:
1182 	dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1183 err_root_open:
1184 err_root_container_id:
1185 	return err;
1186 }
1187 
dprc_exit(void)1188 static int dprc_exit(void)
1189 {
1190 	int err;
1191 
1192 	err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
1193 	if (err < 0) {
1194 		printf("dprc_close(): Child failed: %d\n", err);
1195 		goto err;
1196 	}
1197 
1198 	err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
1199 				     root_dprc_handle, child_dprc_id);
1200 	if (err < 0) {
1201 		printf("dprc_destroy_container() failed: %d\n", err);
1202 		goto err;
1203 	}
1204 
1205 	err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
1206 	if (err < 0) {
1207 		printf("dprc_close(): Root failed: %d\n", err);
1208 		goto err;
1209 	}
1210 
1211 	if (dflt_mc_io)
1212 		free(dflt_mc_io);
1213 
1214 	if (root_mc_io)
1215 		free(root_mc_io);
1216 
1217 	return 0;
1218 
1219 err:
1220 	return err;
1221 }
1222 
dpbp_init(void)1223 static int dpbp_init(void)
1224 {
1225 	int err;
1226 	struct dpbp_attr dpbp_attr;
1227 	struct dpbp_cfg dpbp_cfg;
1228 	uint16_t major_ver, minor_ver;
1229 
1230 	dflt_dpbp = (struct fsl_dpbp_obj *)calloc(
1231 					sizeof(struct fsl_dpbp_obj), 1);
1232 	if (!dflt_dpbp) {
1233 		printf("No memory: calloc() failed\n");
1234 		err = -ENOMEM;
1235 		goto err_calloc;
1236 	}
1237 
1238 	dpbp_cfg.options = 512;
1239 
1240 	err = dpbp_create(dflt_mc_io,
1241 			  dflt_dprc_handle,
1242 			  MC_CMD_NO_FLAGS,
1243 			  &dpbp_cfg,
1244 			  &dflt_dpbp->dpbp_id);
1245 
1246 	if (err < 0) {
1247 		err = -ENODEV;
1248 		printf("dpbp_create() failed: %d\n", err);
1249 		goto err_create;
1250 	}
1251 
1252 	err = dpbp_get_api_version(dflt_mc_io, 0,
1253 				   &major_ver,
1254 				   &minor_ver);
1255 	if (err < 0) {
1256 		printf("dpbp_get_api_version() failed: %d\n", err);
1257 		goto err_get_api_ver;
1258 	}
1259 
1260 	if (major_ver < DPBP_VER_MAJOR || (major_ver == DPBP_VER_MAJOR &&
1261 					   minor_ver < DPBP_VER_MINOR)) {
1262 		printf("DPBP version mismatch found %u.%u,",
1263 		       major_ver, minor_ver);
1264 		printf("supported version is %u.%u\n",
1265 		       DPBP_VER_MAJOR, DPBP_VER_MINOR);
1266 	}
1267 
1268 	err = dpbp_open(dflt_mc_io,
1269 			MC_CMD_NO_FLAGS,
1270 			dflt_dpbp->dpbp_id,
1271 			&dflt_dpbp->dpbp_handle);
1272 	if (err) {
1273 		printf("dpbp_open() failed\n");
1274 		goto err_open;
1275 	}
1276 
1277 	memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
1278 	err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1279 				  dflt_dpbp->dpbp_handle,
1280 				  &dpbp_attr);
1281 	if (err < 0) {
1282 		printf("dpbp_get_attributes() failed: %d\n", err);
1283 		goto err_get_attr;
1284 	}
1285 
1286 	if (dflt_dpbp->dpbp_id != dpbp_attr.id) {
1287 		printf("dpbp object id and attribute id are not same\n");
1288 		goto err_attr_not_same;
1289 	}
1290 
1291 #ifdef DEBUG
1292 	printf("Init: DPBP id=0x%x\n", dflt_dpbp->dpbp_attr.id);
1293 #endif
1294 
1295 	err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
1296 	if (err < 0) {
1297 		printf("dpbp_close() failed: %d\n", err);
1298 		goto err_close;
1299 	}
1300 
1301 	return 0;
1302 
1303 err_get_attr:
1304 err_attr_not_same:
1305 	dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
1306 	dpbp_destroy(dflt_mc_io,
1307 		     dflt_dprc_handle,
1308 		     MC_CMD_NO_FLAGS,
1309 		     dflt_dpbp->dpbp_id);
1310 err_get_api_ver:
1311 err_close:
1312 err_open:
1313 err_create:
1314 	free(dflt_dpbp);
1315 err_calloc:
1316 	return err;
1317 }
1318 
dpbp_exit(void)1319 static int dpbp_exit(void)
1320 {
1321 	int err;
1322 
1323 	err = dpbp_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1324 			   dflt_dpbp->dpbp_id);
1325 	if (err < 0) {
1326 		printf("dpbp_destroy() failed: %d\n", err);
1327 		goto err;
1328 	}
1329 
1330 #ifdef DEBUG
1331 	printf("Exit: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
1332 #endif
1333 
1334 	if (dflt_dpbp)
1335 		free(dflt_dpbp);
1336 	return 0;
1337 
1338 err:
1339 	return err;
1340 }
1341 
dpni_init(void)1342 static int dpni_init(void)
1343 {
1344 	int err;
1345 	uint8_t	cfg_buf[256] = {0};
1346 	struct dpni_cfg dpni_cfg;
1347 	uint16_t major_ver, minor_ver;
1348 
1349 	dflt_dpni = (struct fsl_dpni_obj *)calloc(
1350 					sizeof(struct fsl_dpni_obj), 1);
1351 	if (!dflt_dpni) {
1352 		printf("No memory: calloc() failed\n");
1353 		err = -ENOMEM;
1354 		goto err_calloc;
1355 	}
1356 
1357 	memset(&dpni_cfg, 0, sizeof(dpni_cfg));
1358 	err = dpni_prepare_cfg(&dpni_cfg, &cfg_buf[0]);
1359 	if (err < 0) {
1360 		err = -ENODEV;
1361 		printf("dpni_prepare_cfg() failed: %d\n", err);
1362 		goto err_prepare_cfg;
1363 	}
1364 
1365 	err = dpni_create(dflt_mc_io,
1366 			  dflt_dprc_handle,
1367 			  MC_CMD_NO_FLAGS,
1368 			  &dpni_cfg,
1369 			  &dflt_dpni->dpni_id);
1370 	if (err < 0) {
1371 		err = -ENODEV;
1372 		printf("dpni create() failed: %d\n", err);
1373 		goto err_create;
1374 	}
1375 
1376 	err = dpni_get_api_version(dflt_mc_io, 0,
1377 				   &major_ver,
1378 				   &minor_ver);
1379 	if (err < 0) {
1380 		printf("dpni_get_api_version() failed: %d\n", err);
1381 		goto err_get_version;
1382 	}
1383 
1384 	if (major_ver < DPNI_VER_MAJOR || (major_ver == DPNI_VER_MAJOR &&
1385 					   minor_ver < DPNI_VER_MINOR)) {
1386 		printf("DPNI version mismatch found %u.%u,",
1387 		       major_ver, minor_ver);
1388 		printf("supported version is %u.%u\n",
1389 		       DPNI_VER_MAJOR, DPNI_VER_MINOR);
1390 	}
1391 
1392 	err = dpni_open(dflt_mc_io,
1393 			MC_CMD_NO_FLAGS,
1394 			dflt_dpni->dpni_id,
1395 			&dflt_dpni->dpni_handle);
1396 	if (err) {
1397 		printf("dpni_open() failed\n");
1398 		goto err_open;
1399 	}
1400 
1401 #ifdef DEBUG
1402 	printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1403 #endif
1404 	err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1405 	if (err < 0) {
1406 		printf("dpni_close() failed: %d\n", err);
1407 		goto err_close;
1408 	}
1409 
1410 	return 0;
1411 
1412 err_close:
1413 	dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1414 err_open:
1415 err_get_version:
1416 	dpni_destroy(dflt_mc_io,
1417 		     dflt_dprc_handle,
1418 		     MC_CMD_NO_FLAGS,
1419 		     dflt_dpni->dpni_id);
1420 err_create:
1421 err_prepare_cfg:
1422 	free(dflt_dpni);
1423 err_calloc:
1424 	return err;
1425 }
1426 
dpni_exit(void)1427 static int dpni_exit(void)
1428 {
1429 	int err;
1430 
1431 	err = dpni_destroy(dflt_mc_io, dflt_dprc_handle, MC_CMD_NO_FLAGS,
1432 			   dflt_dpni->dpni_id);
1433 	if (err < 0) {
1434 		printf("dpni_destroy() failed: %d\n", err);
1435 		goto err;
1436 	}
1437 
1438 #ifdef DEBUG
1439 	printf("Exit: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1440 #endif
1441 
1442 	if (dflt_dpni)
1443 		free(dflt_dpni);
1444 	return 0;
1445 
1446 err:
1447 	return err;
1448 }
1449 
mc_init_object(void)1450 static int mc_init_object(void)
1451 {
1452 	int err = 0;
1453 
1454 	err = dprc_init();
1455 	if (err < 0) {
1456 		printf("dprc_init() failed: %d\n", err);
1457 		goto err;
1458 	}
1459 
1460 	err = dpbp_init();
1461 	if (err < 0) {
1462 		printf("dpbp_init() failed: %d\n", err);
1463 		goto err;
1464 	}
1465 
1466 	err = dpio_init();
1467 	if (err < 0) {
1468 		printf("dpio_init() failed: %d\n", err);
1469 		goto err;
1470 	}
1471 
1472 	err = dpni_init();
1473 	if (err < 0) {
1474 		printf("dpni_init() failed: %d\n", err);
1475 		goto err;
1476 	}
1477 
1478 	return 0;
1479 err:
1480 	return err;
1481 }
1482 
fsl_mc_ldpaa_exit(bd_t * bd)1483 int fsl_mc_ldpaa_exit(bd_t *bd)
1484 {
1485 	int err = 0;
1486 	bool is_dpl_apply_status = false;
1487 	bool mc_boot_status = false;
1488 
1489 	if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
1490 		err = mc_apply_dpl(mc_lazy_dpl_addr);
1491 		if (!err)
1492 			fdt_fixup_board_enet(working_fdt);
1493 		mc_lazy_dpl_addr = 0;
1494 	}
1495 
1496 	if (!get_mc_boot_status())
1497 		mc_boot_status = true;
1498 
1499 	/* MC is not loaded intentionally, So return success. */
1500 	if (bd && !mc_boot_status)
1501 		return 0;
1502 
1503 	/* If DPL is deployed, set is_dpl_apply_status as TRUE. */
1504 	if (!get_dpl_apply_status())
1505 		is_dpl_apply_status = true;
1506 
1507 	/*
1508 	 * For case MC is loaded but DPL is not deployed, return success and
1509 	 * print message on console. Else FDT fix-up code execution hanged.
1510 	 */
1511 	if (bd && mc_boot_status && !is_dpl_apply_status) {
1512 		printf("fsl-mc: DPL not deployed, DPAA2 ethernet not work\n");
1513 		goto mc_obj_cleanup;
1514 	}
1515 
1516 	if (bd && mc_boot_status && is_dpl_apply_status)
1517 		return 0;
1518 
1519 mc_obj_cleanup:
1520 	err = dpbp_exit();
1521 	if (err < 0) {
1522 		printf("dpbp_exit() failed: %d\n", err);
1523 		goto err;
1524 	}
1525 
1526 	err = dpio_exit();
1527 	if (err < 0) {
1528 		printf("dpio_exit() failed: %d\n", err);
1529 		goto err;
1530 	}
1531 
1532 	err = dpni_exit();
1533 	if (err < 0) {
1534 		printf("dpni_exit() failed: %d\n", err);
1535 		goto err;
1536 	}
1537 
1538 	err = dprc_exit();
1539 	if (err < 0) {
1540 		printf("dprc_exit() failed: %d\n", err);
1541 		goto err;
1542 	}
1543 
1544 	return 0;
1545 err:
1546 	return err;
1547 }
1548 
do_fsl_mc(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])1549 static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1550 {
1551 	int err = 0;
1552 	if (argc < 3)
1553 		goto usage;
1554 
1555 	switch (argv[1][0]) {
1556 	case 's': {
1557 			char sub_cmd;
1558 			u64 mc_fw_addr, mc_dpc_addr;
1559 #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1560 			u64 aiop_fw_addr;
1561 #endif
1562 
1563 			sub_cmd = argv[2][0];
1564 
1565 			switch (sub_cmd) {
1566 			case 'm':
1567 				if (argc < 5)
1568 					goto usage;
1569 
1570 				if (get_mc_boot_status() == 0) {
1571 					printf("fsl-mc: MC is already booted");
1572 					printf("\n");
1573 					return err;
1574 				}
1575 				mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
1576 				mc_dpc_addr = simple_strtoull(argv[4], NULL,
1577 							      16);
1578 
1579 				if (!mc_init(mc_fw_addr, mc_dpc_addr))
1580 					err = mc_init_object();
1581 				break;
1582 
1583 #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1584 			case 'a':
1585 				if (argc < 4)
1586 					goto usage;
1587 				if (get_aiop_apply_status() == 0) {
1588 					printf("fsl-mc: AIOP FW is already");
1589 					printf(" applied\n");
1590 					return err;
1591 				}
1592 
1593 				aiop_fw_addr = simple_strtoull(argv[3], NULL,
1594 							       16);
1595 
1596 				/* if SoC doesn't have AIOP, err = -ENODEV */
1597 				err = load_mc_aiop_img(aiop_fw_addr);
1598 				if (!err)
1599 					printf("fsl-mc: AIOP FW applied\n");
1600 				break;
1601 #endif
1602 			default:
1603 				printf("Invalid option: %s\n", argv[2]);
1604 				goto usage;
1605 
1606 				break;
1607 			}
1608 		}
1609 		break;
1610 
1611 	case 'l':
1612 	case 'a': {
1613 			u64 mc_dpl_addr;
1614 
1615 			if (argc < 4)
1616 				goto usage;
1617 
1618 			if (get_dpl_apply_status() == 0) {
1619 				printf("fsl-mc: DPL already applied\n");
1620 				return err;
1621 			}
1622 
1623 			mc_dpl_addr = simple_strtoull(argv[3], NULL,
1624 							      16);
1625 
1626 			if (get_mc_boot_status() != 0) {
1627 				printf("fsl-mc: Deploying data path layout ..");
1628 				printf("ERROR (MC is not booted)\n");
1629 				return -ENODEV;
1630 			}
1631 
1632 			if (argv[1][0] == 'l') {
1633 				/*
1634 				 * We will do the actual dpaa exit and dpl apply
1635 				 * later from announce_and_cleanup().
1636 				 */
1637 				mc_lazy_dpl_addr = mc_dpl_addr;
1638 			} else {
1639 				/* The user wants it applied now */
1640 				if (!fsl_mc_ldpaa_exit(NULL))
1641 					err = mc_apply_dpl(mc_dpl_addr);
1642 			}
1643 			break;
1644 		}
1645 	default:
1646 		printf("Invalid option: %s\n", argv[1]);
1647 		goto usage;
1648 		break;
1649 	}
1650 	return err;
1651  usage:
1652 	return CMD_RET_USAGE;
1653 }
1654 
1655 U_BOOT_CMD(
1656 	fsl_mc,  CONFIG_SYS_MAXARGS,  1,   do_fsl_mc,
1657 	"DPAA2 command to manage Management Complex (MC)",
1658 	"start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
1659 	"fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
1660 	"fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
1661 	"fsl_mc start aiop [FW_addr] - Start AIOP\n"
1662 );
1663 
mc_env_boot(void)1664 void mc_env_boot(void)
1665 {
1666 #if defined(CONFIG_FSL_MC_ENET)
1667 	char *mc_boot_env_var;
1668 	/* The MC may only be initialized in the reset PHY function
1669 	 * because otherwise U-Boot has not yet set up all the MAC
1670 	 * address info properly. Without MAC addresses, the MC code
1671 	 * can not properly initialize the DPC.
1672 	 */
1673 	mc_boot_env_var = env_get(MC_BOOT_ENV_VAR);
1674 	if (mc_boot_env_var)
1675 		run_command_list(mc_boot_env_var, -1, 0);
1676 #endif /* CONFIG_FSL_MC_ENET */
1677 }
1678