2 * Copyright (C) 2014 Freescale Semiconductor
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <fsl-mc/fsl_mc.h>
9 #include <fsl-mc/fsl_mc_sys.h>
10 #include <fsl-mc/fsl_mc_private.h>
11 #include <fsl-mc/fsl_dpmng.h>
12 #include <fsl-mc/fsl_dprc.h>
13 #include <fsl-mc/fsl_dpio.h>
14 #include <fsl-mc/fsl_qbman_portal.h>
16 #define MC_RAM_BASE_ADDR_ALIGNMENT (512UL * 1024 * 1024)
17 #define MC_RAM_BASE_ADDR_ALIGNMENT_MASK (~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
18 #define MC_RAM_SIZE_ALIGNMENT (256UL * 1024 * 1024)
20 #define MC_MEM_SIZE_ENV_VAR "mcmemsize"
21 #define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
23 DECLARE_GLOBAL_DATA_PTR;
24 static int mc_boot_status;
25 struct fsl_mc_io *dflt_mc_io = NULL;
26 uint16_t dflt_dprc_handle = 0;
27 struct fsl_dpbp_obj *dflt_dpbp = NULL;
28 struct fsl_dpio_obj *dflt_dpio = NULL;
29 uint16_t dflt_dpio_handle = 0;
32 void dump_ram_words(const char *title, void *addr)
35 uint32_t *words = addr;
37 printf("Dumping beginning of %s (%p):\n", title, addr);
38 for (i = 0; i < 16; i++)
39 printf("%#x ", words[i]);
44 void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
46 printf("MC CCSR registers:\n"
56 mc_ccsr_regs->reg_gcr1,
57 mc_ccsr_regs->reg_gsr,
58 mc_ccsr_regs->reg_sicbalr,
59 mc_ccsr_regs->reg_sicbahr,
60 mc_ccsr_regs->reg_sicapr,
61 mc_ccsr_regs->reg_mcfbalr,
62 mc_ccsr_regs->reg_mcfbahr,
63 mc_ccsr_regs->reg_mcfapr,
64 mc_ccsr_regs->reg_psr);
68 #define dump_ram_words(title, addr)
69 #define dump_mc_ccsr_regs(mc_ccsr_regs)
73 #ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
75 * Copying MC firmware or DPL image to DDR
77 static int mc_copy_image(const char *title,
78 u64 image_addr, u32 image_size, u64 mc_ram_addr)
80 debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
81 memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
82 flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
87 * MC firmware FIT image parser checks if the image is in FIT
88 * format, verifies integrity of the image and calculates
89 * raw image address and size values.
90 * Returns 0 on success and a negative errno on error.
93 int parse_mc_firmware_fit_image(const void **raw_image_addr,
94 size_t *raw_image_size)
101 const char *uname = "firmware";
103 /* Check if the image is in NOR flash */
104 #ifdef CONFIG_SYS_LS_MC_FW_IN_NOR
105 fit_hdr = (void *)CONFIG_SYS_LS_MC_FW_ADDR;
107 #error "No CONFIG_SYS_LS_MC_FW_IN_xxx defined"
110 /* Check if Image is in FIT format */
111 format = genimg_get_format(fit_hdr);
113 if (format != IMAGE_FORMAT_FIT) {
114 printf("fsl-mc: ERROR: Bad firmware image (not a FIT image)\n");
118 if (!fit_check_format(fit_hdr)) {
119 printf("fsl-mc: ERROR: Bad firmware image (bad FIT header)\n");
123 node_offset = fit_image_get_node(fit_hdr, uname);
125 if (node_offset < 0) {
126 printf("fsl-mc: ERROR: Bad firmware image (missing subimage)\n");
130 /* Verify MC firmware image */
131 if (!(fit_image_verify(fit_hdr, node_offset))) {
132 printf("fsl-mc: ERROR: Bad firmware image (bad CRC)\n");
136 /* Get address and size of raw image */
137 fit_image_get_data(fit_hdr, node_offset, &data, &size);
139 *raw_image_addr = data;
140 *raw_image_size = size;
147 * Calculates the values to be used to specify the address range
148 * for the MC private DRAM block, in the MCFBALR/MCFBAHR registers.
149 * It returns the highest 512MB-aligned address within the given
150 * address range, in '*aligned_base_addr', and the number of 256 MiB
151 * blocks in it, in 'num_256mb_blocks'.
153 static int calculate_mc_private_ram_params(u64 mc_private_ram_start_addr,
155 u64 *aligned_base_addr,
156 u8 *num_256mb_blocks)
161 if (mc_ram_size % MC_RAM_SIZE_ALIGNMENT != 0) {
162 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
167 num_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
168 if (num_blocks < 1 || num_blocks > 0xff) {
169 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
174 addr = (mc_private_ram_start_addr + mc_ram_size - 1) &
175 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
177 if (addr < mc_private_ram_start_addr) {
178 printf("fsl-mc: ERROR: bad start address %#llx\n",
179 mc_private_ram_start_addr);
183 *aligned_base_addr = addr;
184 *num_256mb_blocks = num_blocks;
188 static int load_mc_dpc(u64 mc_ram_addr, size_t mc_ram_size)
191 #ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
197 #ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
198 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
199 CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
201 mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
203 #error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
207 * Load the MC DPC blob in the MC private DRAM block:
209 #ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
210 printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
213 * Get address and size of the DPC blob stored in flash:
215 #ifdef CONFIG_SYS_LS_MC_DPC_IN_NOR
216 dpc_fdt_hdr = (void *)CONFIG_SYS_LS_MC_DPC_ADDR;
218 #error "No CONFIG_SYS_LS_MC_DPC_IN_xxx defined"
221 error = fdt_check_header(dpc_fdt_hdr);
224 * Don't return with error here, since the MC firmware can
225 * still boot without a DPC
227 printf("fsl-mc: WARNING: No DPC image found\n");
231 dpc_size = fdt_totalsize(dpc_fdt_hdr);
232 if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
233 printf("fsl-mc: ERROR: Bad DPC image (too large: %d)\n",
238 mc_copy_image("MC DPC blob",
239 (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
240 #endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
242 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
246 static int load_mc_dpl(u64 mc_ram_addr, size_t mc_ram_size)
249 #ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
255 #ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
256 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
257 CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
259 mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
261 #error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
265 * Load the MC DPL blob in the MC private DRAM block:
267 #ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
268 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
271 * Get address and size of the DPL blob stored in flash:
273 #ifdef CONFIG_SYS_LS_MC_DPL_IN_NOR
274 dpl_fdt_hdr = (void *)CONFIG_SYS_LS_MC_DPL_ADDR;
276 #error "No CONFIG_SYS_LS_MC_DPL_IN_xxx defined"
279 error = fdt_check_header(dpl_fdt_hdr);
281 printf("fsl-mc: ERROR: Bad DPL image (bad header)\n");
285 dpl_size = fdt_totalsize(dpl_fdt_hdr);
286 if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
287 printf("fsl-mc: ERROR: Bad DPL image (too large: %d)\n",
292 mc_copy_image("MC DPL blob",
293 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
294 #endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
296 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
301 * Return the MC boot timeout value in milliseconds
303 static unsigned long get_mc_boot_timeout_ms(void)
305 unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
307 char *timeout_ms_env_var = getenv(MC_BOOT_TIMEOUT_ENV_VAR);
309 if (timeout_ms_env_var) {
310 timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
311 if (timeout_ms == 0) {
312 printf("fsl-mc: WARNING: Invalid value for \'"
313 MC_BOOT_TIMEOUT_ENV_VAR
314 "\' environment variable: %lu\n",
317 timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
324 static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
327 u32 mc_fw_boot_status;
328 unsigned long timeout_ms = get_mc_boot_timeout_ms();
329 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
332 debug("Polling mc_ccsr_regs->reg_gsr ...\n");
333 assert(timeout_ms > 0);
335 udelay(1000); /* throttle polling */
336 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
337 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
338 if (mc_fw_boot_status & 0x1)
346 if (timeout_ms == 0) {
348 printf("fsl-mc: timeout booting management complex firmware\n");
350 printf("fsl-mc: timeout deploying data path layout\n");
352 /* TODO: Get an error status from an MC CCSR register */
356 if (mc_fw_boot_status != 0x1) {
358 * TODO: Identify critical errors from the GSR register's FS
359 * field and for those errors, set error to -ENODEV or other
360 * appropriate errno, so that the status property is set to
361 * failure in the fsl,dprc device tree node.
364 printf("fsl-mc: WARNING: Firmware booted with error (GSR: %#x)\n",
367 printf("fsl-mc: WARNING: Data path layout deployed with error (GSR: %#x)\n",
372 *final_reg_gsr = reg_gsr;
380 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
384 #ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
385 const void *raw_image_addr;
386 size_t raw_image_size = 0;
388 struct mc_version mc_ver_info;
389 u64 mc_ram_aligned_base_addr;
390 u8 mc_ram_num_256mb_blocks;
391 size_t mc_ram_size = mc_get_dram_block_size();
394 * The MC private DRAM block was already carved at the end of DRAM
395 * by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
397 if (gd->bd->bi_dram[1].start) {
399 gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
402 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
405 error = calculate_mc_private_ram_params(mc_ram_addr,
407 &mc_ram_aligned_base_addr,
408 &mc_ram_num_256mb_blocks);
413 * Management Complex cores should be held at reset out of POR.
414 * U-boot should be the first software to touch MC. To be safe,
415 * we reset all cores again by setting GCR1 to 0. It doesn't do
416 * anything if they are held at reset. After we setup the firmware
417 * we kick off MC by deasserting the reset bit for core 0, and
418 * deasserting the reset bits for Command Portal Managers.
419 * The stop bits are not touched here. They are used to stop the
420 * cores when they are active. Setting stop bits doesn't stop the
421 * cores from fetching instructions when they are released from
424 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
427 #ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
428 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
430 error = parse_mc_firmware_fit_image(&raw_image_addr, &raw_image_size);
434 * Load the MC FW at the beginning of the MC private DRAM block:
436 mc_copy_image("MC Firmware",
437 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
439 dump_ram_words("firmware", (void *)mc_ram_addr);
441 error = load_mc_dpc(mc_ram_addr, mc_ram_size);
445 error = load_mc_dpl(mc_ram_addr, mc_ram_size);
449 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
450 dump_mc_ccsr_regs(mc_ccsr_regs);
453 * Tell MC what is the address range of the DRAM block assigned to it:
455 reg_mcfbalr = (u32)mc_ram_aligned_base_addr |
456 (mc_ram_num_256mb_blocks - 1);
457 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
458 out_le32(&mc_ccsr_regs->reg_mcfbahr,
459 (u32)(mc_ram_aligned_base_addr >> 32));
460 out_le32(&mc_ccsr_regs->reg_mcfapr, MCFAPR_BYPASS_ICID_MASK);
463 * Tell the MC that we want delayed DPL deployment.
465 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
467 printf("\nfsl-mc: Booting Management Complex ...\n");
470 * Deassert reset and release MC core 0 to run
472 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
473 error = wait_for_mc(true, ®_gsr);
478 * TODO: need to obtain the portal_id for the root container from the
484 * Initialize the global default MC portal
485 * And check that the MC firmware is responding portal commands:
487 dflt_mc_io = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
489 printf(" No memory: malloc() failed\n");
493 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
494 debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
495 portal_id, dflt_mc_io->mmio_regs);
497 error = mc_get_version(dflt_mc_io, &mc_ver_info);
499 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
504 if (MC_VER_MAJOR != mc_ver_info.major)
505 printf("fsl-mc: ERROR: Firmware major version mismatch (found: %d, expected: %d)\n",
506 mc_ver_info.major, MC_VER_MAJOR);
508 if (MC_VER_MINOR != mc_ver_info.minor)
509 printf("fsl-mc: WARNING: Firmware minor version mismatch (found: %d, expected: %d)\n",
510 mc_ver_info.minor, MC_VER_MINOR);
512 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
513 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
514 reg_gsr & GSR_FS_MASK);
517 * Tell the MC to deploy the DPL:
519 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
520 printf("\nfsl-mc: Deploying data path layout ...\n");
521 error = wait_for_mc(false, ®_gsr);
526 mc_boot_status = -error;
533 int get_mc_boot_status(void)
535 return mc_boot_status;
539 * Return the actual size of the MC private DRAM block.
541 unsigned long mc_get_dram_block_size(void)
543 unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
545 char *dram_block_size_env_var = getenv(MC_MEM_SIZE_ENV_VAR);
547 if (dram_block_size_env_var) {
548 dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
551 if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
552 printf("fsl-mc: WARNING: Invalid value for \'"
554 "\' environment variable: %lu\n",
557 dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
561 return dram_block_size;
564 int dpio_init(struct dprc_obj_desc obj_desc)
566 struct qbman_swp_desc p_des;
567 struct dpio_attr attr;
570 dflt_dpio = (struct fsl_dpio_obj *)malloc(sizeof(struct fsl_dpio_obj));
572 printf(" No memory: malloc() failed\n");
576 dflt_dpio->dpio_id = obj_desc.id;
578 err = dpio_open(dflt_mc_io, obj_desc.id, &dflt_dpio_handle);
580 printf("dpio_open() failed\n");
584 err = dpio_get_attributes(dflt_mc_io, dflt_dpio_handle, &attr);
586 printf("dpio_get_attributes() failed %d\n", err);
590 err = dpio_enable(dflt_mc_io, dflt_dpio_handle);
592 printf("dpio_enable() failed %d\n", err);
595 debug("ce_paddr=0x%llx, ci_paddr=0x%llx, portalid=%d, prios=%d\n",
596 attr.qbman_portal_ce_paddr,
597 attr.qbman_portal_ci_paddr,
598 attr.qbman_portal_id,
599 attr.num_priorities);
601 p_des.cena_bar = (void *)attr.qbman_portal_ce_paddr;
602 p_des.cinh_bar = (void *)attr.qbman_portal_ci_paddr;
604 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
605 if (dflt_dpio->sw_portal == NULL) {
606 printf("qbman_swp_init() failed\n");
607 goto err_get_swp_init;
613 dpio_disable(dflt_mc_io, dflt_dpio_handle);
615 dpio_close(dflt_mc_io, dflt_dpio_handle);
621 int dpbp_init(struct dprc_obj_desc obj_desc)
623 dflt_dpbp = (struct fsl_dpbp_obj *)malloc(sizeof(struct fsl_dpbp_obj));
625 printf(" No memory: malloc() failed\n");
628 dflt_dpbp->dpbp_attr.id = obj_desc.id;
633 int dprc_init_container_obj(struct dprc_obj_desc obj_desc, uint16_t dprc_handle)
635 int error = 0, state = 0;
636 struct dprc_endpoint dpni_endpoint, dpmac_endpoint;
637 if (!strcmp(obj_desc.type, "dpbp")) {
639 error = dpbp_init(obj_desc);
641 printf("dpbp_init failed\n");
643 } else if (!strcmp(obj_desc.type, "dpio")) {
645 error = dpio_init(obj_desc);
647 printf("dpio_init failed\n");
649 } else if (!strcmp(obj_desc.type, "dpni")) {
650 strcpy(dpni_endpoint.type, obj_desc.type);
651 dpni_endpoint.id = obj_desc.id;
652 error = dprc_get_connection(dflt_mc_io, dprc_handle,
653 &dpni_endpoint, &dpmac_endpoint, &state);
654 if (!strcmp(dpmac_endpoint.type, "dpmac"))
655 error = ldpaa_eth_init(obj_desc);
657 printf("ldpaa_eth_init failed\n");
663 int dprc_scan_container_obj(uint16_t dprc_handle, char *obj_type, int i)
666 struct dprc_obj_desc obj_desc;
668 memset((void *)&obj_desc, 0x00, sizeof(struct dprc_obj_desc));
670 error = dprc_get_obj(dflt_mc_io, dprc_handle,
673 printf("dprc_get_obj(i=%d) failed: %d\n",
678 if (!strcmp(obj_desc.type, obj_type)) {
679 debug("Discovered object: type %s, id %d, req %s\n",
680 obj_desc.type, obj_desc.id, obj_type);
682 error = dprc_init_container_obj(obj_desc, dprc_handle);
684 printf("dprc_init_container_obj(i=%d) failed: %d\n",
693 int fsl_mc_ldpaa_init(bd_t *bis)
696 int dprc_opened = 0, container_id;
697 int num_child_objects = 0;
703 error = dprc_get_container_id(dflt_mc_io, &container_id);
705 printf("dprc_get_container_id() failed: %d\n", error);
709 debug("fsl-mc: Container id=0x%x\n", container_id);
711 error = dprc_open(dflt_mc_io, container_id, &dflt_dprc_handle);
713 printf("dprc_open() failed: %d\n", error);
718 error = dprc_get_obj_count(dflt_mc_io,
722 printf("dprc_get_obj_count() failed: %d\n", error);
725 debug("Total child in container %d = %d\n", container_id,
728 if (num_child_objects != 0) {
730 * Discover objects currently in the DPRC container in the MC:
732 for (i = 0; i < num_child_objects; i++)
733 error = dprc_scan_container_obj(dflt_dprc_handle,
736 for (i = 0; i < num_child_objects; i++)
737 error = dprc_scan_container_obj(dflt_dprc_handle,
740 for (i = 0; i < num_child_objects; i++)
741 error = dprc_scan_container_obj(dflt_dprc_handle,
746 dprc_close(dflt_mc_io, dflt_dprc_handle);
751 void fsl_mc_ldpaa_exit(bd_t *bis)
755 if (get_mc_boot_status() == 0) {
756 err = dpio_disable(dflt_mc_io, dflt_dpio_handle);
758 printf("dpio_disable() failed: %d\n", err);
761 err = dpio_reset(dflt_mc_io, dflt_dpio_handle);
763 printf("dpio_reset() failed: %d\n", err);
766 err = dpio_close(dflt_mc_io, dflt_dpio_handle);
768 printf("dpio_close() failed: %d\n", err);