2 * Copyright (C) 2014 Freescale Semiconductor
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <fsl-mc/fsl_mc.h>
10 #include <fsl-mc/fsl_mc_sys.h>
11 #include <fsl-mc/fsl_mc_private.h>
12 #include <fsl-mc/fsl_dpmng.h>
13 #include <fsl_debug_server.h>
14 #include <fsl-mc/fsl_dprc.h>
15 #include <fsl-mc/fsl_dpio.h>
16 #include <fsl-mc/fsl_qbman_portal.h>
18 DECLARE_GLOBAL_DATA_PTR;
19 static int mc_boot_status;
20 struct fsl_mc_io *dflt_mc_io = NULL;
21 uint16_t dflt_dprc_handle = 0;
22 struct fsl_dpbp_obj *dflt_dpbp = NULL;
23 struct fsl_dpio_obj *dflt_dpio = NULL;
24 uint16_t dflt_dpio_handle = NULL;
27 * Copying MC firmware or DPL image to DDR
29 static int mc_copy_image(const char *title,
30 u64 image_addr, u32 image_size, u64 mc_ram_addr)
32 debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
33 memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
38 * MC firmware FIT image parser checks if the image is in FIT
39 * format, verifies integrity of the image and calculates
40 * raw image address and size values.
41 * Returns 0 on success and a negative errno on error.
44 int parse_mc_firmware_fit_image(const void **raw_image_addr,
45 size_t *raw_image_size)
52 const char *uname = "firmware";
54 /* Check if the image is in NOR flash */
55 #ifdef CONFIG_SYS_LS_MC_FW_IN_NOR
56 fit_hdr = (void *)CONFIG_SYS_LS_MC_FW_ADDR;
58 #error "No CONFIG_SYS_LS_MC_FW_IN_xxx defined"
61 /* Check if Image is in FIT format */
62 format = genimg_get_format(fit_hdr);
64 if (format != IMAGE_FORMAT_FIT) {
65 printf("fsl-mc: ERROR: Bad firmware image (not a FIT image)\n");
69 if (!fit_check_format(fit_hdr)) {
70 printf("fsl-mc: ERROR: Bad firmware image (bad FIT header)\n");
74 node_offset = fit_image_get_node(fit_hdr, uname);
76 if (node_offset < 0) {
77 printf("fsl-mc: ERROR: Bad firmware image (missing subimage)\n");
81 /* Verify MC firmware image */
82 if (!(fit_image_verify(fit_hdr, node_offset))) {
83 printf("fsl-mc: ERROR: Bad firmware image (bad CRC)\n");
87 /* Get address and size of raw image */
88 fit_image_get_data(fit_hdr, node_offset, &data, &size);
90 *raw_image_addr = data;
91 *raw_image_size = size;
101 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
105 u32 mc_fw_boot_status;
108 const void *raw_image_addr;
109 size_t raw_image_size = 0;
110 struct mc_version mc_ver_info;
113 * The MC private DRAM block was already carved at the end of DRAM
114 * by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
116 if (gd->bd->bi_dram[1].start) {
118 gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
121 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
124 #ifdef CONFIG_FSL_DEBUG_SERVER
125 mc_ram_addr -= debug_server_get_dram_block_size();
128 * Management Complex cores should be held at reset out of POR.
129 * U-boot should be the first software to touch MC. To be safe,
130 * we reset all cores again by setting GCR1 to 0. It doesn't do
131 * anything if they are held at reset. After we setup the firmware
132 * we kick off MC by deasserting the reset bit for core 0, and
133 * deasserting the reset bits for Command Portal Managers.
134 * The stop bits are not touched here. They are used to stop the
135 * cores when they are active. Setting stop bits doesn't stop the
136 * cores from fetching instructions when they are released from
139 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
142 error = parse_mc_firmware_fit_image(&raw_image_addr, &raw_image_size);
146 * Load the MC FW at the beginning of the MC private DRAM block:
148 mc_copy_image("MC Firmware",
149 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
152 * Get address and size of the DPL blob stored in flash:
154 #ifdef CONFIG_SYS_LS_MC_DPL_IN_NOR
155 dpl_fdt_hdr = (void *)CONFIG_SYS_LS_MC_DPL_ADDR;
157 #error "No CONFIG_SYS_LS_MC_DPL_IN_xxx defined"
160 error = fdt_check_header(dpl_fdt_hdr);
162 printf("fsl-mc: ERROR: Bad DPL image (bad header)\n");
166 dpl_size = fdt_totalsize(dpl_fdt_hdr);
167 if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
168 printf("fsl-mc: ERROR: Bad DPL image (too large: %d)\n",
175 * Calculate offset in the MC private DRAM block at which the MC DPL
176 * blob is to be placed:
178 #ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
179 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
180 CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
182 mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
184 mc_dpl_offset = mc_get_dram_block_size() -
185 roundup(CONFIG_SYS_LS_MC_DPL_MAX_LENGTH, 4096);
187 if ((mc_dpl_offset & 0x3) != 0 || mc_dpl_offset > 0xffffffff) {
188 printf("%s: Invalid MC DPL offset: %llu\n",
189 __func__, mc_dpl_offset);
196 * Load the MC DPL blob at the far end of the MC private DRAM block:
198 * TODO: Should we place the DPL at a different location to match
199 * assumptions of MC firmware about its memory layout?
201 mc_copy_image("MC DPL blob",
202 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
204 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
207 * Tell MC where the MC Firmware image was loaded in DDR:
209 out_le32(&mc_ccsr_regs->reg_mcfbalr, (u32)mc_ram_addr);
210 out_le32(&mc_ccsr_regs->reg_mcfbahr, (u32)((u64)mc_ram_addr >> 32));
211 out_le32(&mc_ccsr_regs->reg_mcfapr, MCFAPR_BYPASS_ICID_MASK);
214 * Tell MC where the DPL blob was loaded in DDR, by indicating
215 * its offset relative to the beginning of the DDR block
216 * allocated to the MC firmware. The MC firmware is responsible
217 * for checking that there is no overlap between the DPL blob
218 * and the runtime heap and stack of the MC firmware itself.
220 * NOTE: bits [31:2] of this offset need to be stored in bits [29:0] of
221 * the GSR MC CCSR register. So, this offset is assumed to be 4-byte
223 * Care must be taken not to write 1s into bits 31 and 30 of the GSR in
224 * this case as the SoC COP or PIC will be signaled.
226 out_le32(&mc_ccsr_regs->reg_gsr, (u32)(mc_dpl_offset >> 2));
228 printf("\nfsl-mc: Booting Management Complex ...\n");
231 * Deassert reset and release MC core 0 to run
233 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
235 debug("Polling mc_ccsr_regs->reg_gsr ...\n");
238 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
239 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
240 if (mc_fw_boot_status & 0x1)
243 udelay(1000); /* throttle polling */
249 printf("fsl-mc: timeout booting management complex firmware\n");
251 /* TODO: Get an error status from an MC CCSR register */
256 if (mc_fw_boot_status != 0x1) {
258 * TODO: Identify critical errors from the GSR register's FS
259 * field and for those errors, set error to -ENODEV or other
260 * appropriate errno, so that the status property is set to
261 * failure in the fsl,dprc device tree node.
263 printf("fsl-mc: WARNING: Firmware booted with error (GSR: %#x)\n",
268 * TODO: need to obtain the portal_id for the root container from the
274 * Initialize the global default MC portal
275 * And check that the MC firmware is responding portal commands:
277 dflt_mc_io = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
279 printf(" No memory: malloc() failed\n");
283 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
284 debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
285 portal_id, dflt_mc_io->mmio_regs);
287 error = mc_get_version(dflt_mc_io, &mc_ver_info);
289 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
294 if (MC_VER_MAJOR != mc_ver_info.major)
295 printf("fsl-mc: ERROR: Firmware major version mismatch (found: %d, expected: %d)\n",
296 mc_ver_info.major, MC_VER_MAJOR);
298 if (MC_VER_MINOR != mc_ver_info.minor)
299 printf("fsl-mc: WARNING: Firmware minor version mismatch (found: %d, expected: %d)\n",
300 mc_ver_info.minor, MC_VER_MINOR);
302 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
303 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
307 mc_boot_status = -error;
314 int get_mc_boot_status(void)
316 return mc_boot_status;
320 * Return the actual size of the MC private DRAM block.
322 * NOTE: For now this function always returns the minimum required size,
323 * However, in the future, the actual size may be obtained from an environment
326 unsigned long mc_get_dram_block_size(void)
328 return CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
331 int dpio_init(struct dprc_obj_desc obj_desc)
333 struct qbman_swp_desc p_des;
334 struct dpio_attr attr;
337 dflt_dpio = (struct fsl_dpio_obj *)malloc(sizeof(struct fsl_dpio_obj));
339 printf(" No memory: malloc() failed\n");
343 dflt_dpio->dpio_id = obj_desc.id;
345 err = dpio_open(dflt_mc_io, obj_desc.id, &dflt_dpio_handle);
347 printf("dpio_open() failed\n");
351 err = dpio_get_attributes(dflt_mc_io, dflt_dpio_handle, &attr);
353 printf("dpio_get_attributes() failed %d\n", err);
357 err = dpio_enable(dflt_mc_io, dflt_dpio_handle);
359 printf("dpio_enable() failed %d\n", err);
362 debug("ce_paddr=0x%llx, ci_paddr=0x%llx, portalid=%d, prios=%d\n",
363 attr.qbman_portal_ce_paddr,
364 attr.qbman_portal_ci_paddr,
365 attr.qbman_portal_id,
366 attr.num_priorities);
368 p_des.cena_bar = (void *)attr.qbman_portal_ce_paddr;
369 p_des.cinh_bar = (void *)attr.qbman_portal_ci_paddr;
371 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
372 if (dflt_dpio->sw_portal == NULL) {
373 printf("qbman_swp_init() failed\n");
374 goto err_get_swp_init;
380 dpio_disable(dflt_mc_io, dflt_dpio_handle);
382 dpio_close(dflt_mc_io, dflt_dpio_handle);
388 int dpbp_init(struct dprc_obj_desc obj_desc)
390 dflt_dpbp = (struct fsl_dpbp_obj *)malloc(sizeof(struct fsl_dpbp_obj));
392 printf(" No memory: malloc() failed\n");
395 dflt_dpbp->dpbp_attr.id = obj_desc.id;
400 int dprc_init_container_obj(struct dprc_obj_desc obj_desc)
403 if (!strcmp(obj_desc.type, "dpbp")) {
405 error = dpbp_init(obj_desc);
407 printf("dpbp_init failed\n");
409 } else if (!strcmp(obj_desc.type, "dpio")) {
411 error = dpio_init(obj_desc);
413 printf("dpio_init failed\n");
420 int dprc_scan_container_obj(uint16_t dprc_handle, char *obj_type, int i)
423 struct dprc_obj_desc obj_desc;
425 memset((void *)&obj_desc, 0x00, sizeof(struct dprc_obj_desc));
427 error = dprc_get_obj(dflt_mc_io, dprc_handle,
430 printf("dprc_get_obj(i=%d) failed: %d\n",
435 if (!strcmp(obj_desc.type, obj_type)) {
436 debug("Discovered object: type %s, id %d, req %s\n",
437 obj_desc.type, obj_desc.id, obj_type);
439 error = dprc_init_container_obj(obj_desc);
441 printf("dprc_init_container_obj(i=%d) failed: %d\n",
450 int fsl_mc_ldpaa_init(bd_t *bis)
453 int dprc_opened = 0, container_id;
454 int num_child_objects = 0;
458 error = dprc_get_container_id(dflt_mc_io, &container_id);
460 printf("dprc_get_container_id() failed: %d\n", error);
464 debug("fsl-mc: Container id=0x%x\n", container_id);
466 error = dprc_open(dflt_mc_io, container_id, &dflt_dprc_handle);
468 printf("dprc_open() failed: %d\n", error);
473 error = dprc_get_obj_count(dflt_mc_io,
477 printf("dprc_get_obj_count() failed: %d\n", error);
480 debug("Total child in container %d = %d\n", container_id,
483 if (num_child_objects != 0) {
485 * Discover objects currently in the DPRC container in the MC:
487 for (i = 0; i < num_child_objects; i++)
488 error = dprc_scan_container_obj(dflt_dprc_handle,
491 for (i = 0; i < num_child_objects; i++)
492 error = dprc_scan_container_obj(dflt_dprc_handle,
495 for (i = 0; i < num_child_objects; i++)
496 error = dprc_scan_container_obj(dflt_dprc_handle,
501 dprc_close(dflt_mc_io, dflt_dprc_handle);
506 void fsl_mc_ldpaa_exit(bd_t *bis)
511 err = dpio_disable(dflt_mc_io, dflt_dpio_handle);
513 printf("dpio_disable() failed: %d\n", err);
516 err = dpio_reset(dflt_mc_io, dflt_dpio_handle);
518 printf("dpio_reset() failed: %d\n", err);
521 err = dpio_close(dflt_mc_io, dflt_dpio_handle);
523 printf("dpio_close() failed: %d\n", err);