blob: d2ca5c657c9c206b4503fb255421e8045f9a8d63 [file] [log] [blame]
J. German Riverab940ca62014-06-23 15:15:55 -07001/*
2 * Copyright (C) 2014 Freescale Semiconductor
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
Stuart Yoder21c69872015-07-02 11:29:03 +05306#include <common.h>
J. German Riverab940ca62014-06-23 15:15:55 -07007#include <errno.h>
8#include <asm/io.h>
Stuart Yoder21c69872015-07-02 11:29:03 +05309#include <libfdt.h>
10#include <fdt_support.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080011#include <fsl-mc/fsl_mc.h>
12#include <fsl-mc/fsl_mc_sys.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070013#include <fsl-mc/fsl_mc_private.h>
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080014#include <fsl-mc/fsl_dpmng.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070015#include <fsl-mc/fsl_dprc.h>
16#include <fsl-mc/fsl_dpio.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053017#include <fsl-mc/fsl_dpni.h>
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070018#include <fsl-mc/fsl_qbman_portal.h>
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053019#include <fsl-mc/ldpaa_wriop.h>
J. German Riverab940ca62014-06-23 15:15:55 -070020
J. German Rivera125e2bc2015-03-20 19:28:18 -070021#define MC_RAM_BASE_ADDR_ALIGNMENT (512UL * 1024 * 1024)
22#define MC_RAM_BASE_ADDR_ALIGNMENT_MASK (~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
23#define MC_RAM_SIZE_ALIGNMENT (256UL * 1024 * 1024)
24
25#define MC_MEM_SIZE_ENV_VAR "mcmemsize"
26#define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
27
J. German Riverab940ca62014-06-23 15:15:55 -070028DECLARE_GLOBAL_DATA_PTR;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +053029static int mc_boot_status = -1;
30static int mc_dpl_applied = -1;
31#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
32static int mc_aiop_applied = -1;
33#endif
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -070034struct fsl_mc_io *dflt_mc_io = NULL;
35uint16_t dflt_dprc_handle = 0;
36struct fsl_dpbp_obj *dflt_dpbp = NULL;
37struct fsl_dpio_obj *dflt_dpio = NULL;
J. German Rivera125e2bc2015-03-20 19:28:18 -070038uint16_t dflt_dpio_handle = 0;
J. German Riverab940ca62014-06-23 15:15:55 -070039
J. German Rivera125e2bc2015-03-20 19:28:18 -070040#ifdef DEBUG
41void dump_ram_words(const char *title, void *addr)
42{
43 int i;
44 uint32_t *words = addr;
45
46 printf("Dumping beginning of %s (%p):\n", title, addr);
47 for (i = 0; i < 16; i++)
48 printf("%#x ", words[i]);
49
50 printf("\n");
51}
52
53void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
54{
55 printf("MC CCSR registers:\n"
56 "reg_gcr1 %#x\n"
57 "reg_gsr %#x\n"
58 "reg_sicbalr %#x\n"
59 "reg_sicbahr %#x\n"
60 "reg_sicapr %#x\n"
61 "reg_mcfbalr %#x\n"
62 "reg_mcfbahr %#x\n"
63 "reg_mcfapr %#x\n"
64 "reg_psr %#x\n",
65 mc_ccsr_regs->reg_gcr1,
66 mc_ccsr_regs->reg_gsr,
67 mc_ccsr_regs->reg_sicbalr,
68 mc_ccsr_regs->reg_sicbahr,
69 mc_ccsr_regs->reg_sicapr,
70 mc_ccsr_regs->reg_mcfbalr,
71 mc_ccsr_regs->reg_mcfbahr,
72 mc_ccsr_regs->reg_mcfapr,
73 mc_ccsr_regs->reg_psr);
74}
75#else
76
77#define dump_ram_words(title, addr)
78#define dump_mc_ccsr_regs(mc_ccsr_regs)
79
80#endif /* DEBUG */
81
82#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-06-23 15:15:55 -070083/**
84 * Copying MC firmware or DPL image to DDR
85 */
86static int mc_copy_image(const char *title,
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080087 u64 image_addr, u32 image_size, u64 mc_ram_addr)
J. German Riverab940ca62014-06-23 15:15:55 -070088{
89 debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
90 memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
J. German Rivera125e2bc2015-03-20 19:28:18 -070091 flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
J. German Riverab940ca62014-06-23 15:15:55 -070092 return 0;
93}
94
95/**
96 * MC firmware FIT image parser checks if the image is in FIT
97 * format, verifies integrity of the image and calculates
98 * raw image address and size values.
J. German Rivera7b3bd9a2015-01-06 13:19:02 -080099 * Returns 0 on success and a negative errno on error.
J. German Riverab940ca62014-06-23 15:15:55 -0700100 * task fail.
101 **/
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530102int parse_mc_firmware_fit_image(u64 mc_fw_addr,
103 const void **raw_image_addr,
J. German Riverab940ca62014-06-23 15:15:55 -0700104 size_t *raw_image_size)
105{
106 int format;
107 void *fit_hdr;
108 int node_offset;
109 const void *data;
110 size_t size;
111 const char *uname = "firmware";
112
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530113 fit_hdr = (void *)mc_fw_addr;
J. German Riverab940ca62014-06-23 15:15:55 -0700114
115 /* Check if Image is in FIT format */
116 format = genimg_get_format(fit_hdr);
117
118 if (format != IMAGE_FORMAT_FIT) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530119 printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800120 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700121 }
122
123 if (!fit_check_format(fit_hdr)) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530124 printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800125 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700126 }
127
128 node_offset = fit_image_get_node(fit_hdr, uname);
129
130 if (node_offset < 0) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530131 printf("fsl-mc: ERR: Bad firmware image (missing subimage)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800132 return -ENOENT;
J. German Riverab940ca62014-06-23 15:15:55 -0700133 }
134
135 /* Verify MC firmware image */
136 if (!(fit_image_verify(fit_hdr, node_offset))) {
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530137 printf("fsl-mc: ERR: Bad firmware image (bad CRC)\n");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800138 return -EINVAL;
J. German Riverab940ca62014-06-23 15:15:55 -0700139 }
140
141 /* Get address and size of raw image */
142 fit_image_get_data(fit_hdr, node_offset, &data, &size);
143
144 *raw_image_addr = data;
145 *raw_image_size = size;
146
147 return 0;
148}
J. German Rivera125e2bc2015-03-20 19:28:18 -0700149#endif
150
151/*
152 * Calculates the values to be used to specify the address range
153 * for the MC private DRAM block, in the MCFBALR/MCFBAHR registers.
154 * It returns the highest 512MB-aligned address within the given
155 * address range, in '*aligned_base_addr', and the number of 256 MiB
156 * blocks in it, in 'num_256mb_blocks'.
157 */
158static int calculate_mc_private_ram_params(u64 mc_private_ram_start_addr,
159 size_t mc_ram_size,
160 u64 *aligned_base_addr,
161 u8 *num_256mb_blocks)
162{
163 u64 addr;
164 u16 num_blocks;
165
166 if (mc_ram_size % MC_RAM_SIZE_ALIGNMENT != 0) {
167 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
168 mc_ram_size);
169 return -EINVAL;
170 }
171
172 num_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
173 if (num_blocks < 1 || num_blocks > 0xff) {
174 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
175 mc_ram_size);
176 return -EINVAL;
177 }
178
179 addr = (mc_private_ram_start_addr + mc_ram_size - 1) &
180 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
181
182 if (addr < mc_private_ram_start_addr) {
183 printf("fsl-mc: ERROR: bad start address %#llx\n",
184 mc_private_ram_start_addr);
185 return -EFAULT;
186 }
187
188 *aligned_base_addr = addr;
189 *num_256mb_blocks = num_blocks;
190 return 0;
191}
192
Stuart Yoder21c69872015-07-02 11:29:03 +0530193static int mc_fixup_dpc(u64 dpc_addr)
194{
195 void *blob = (void *)dpc_addr;
196 int nodeoffset;
197
198 /* delete any existing ICID pools */
199 nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
200 if (fdt_del_node(blob, nodeoffset) < 0)
201 printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
202
203 /* add a new pool */
204 nodeoffset = fdt_path_offset(blob, "/resources");
205 if (nodeoffset < 0) {
206 printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
207 return -EINVAL;
208 }
209 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
210 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
211 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
212 "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
213 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
214 "num",
215 FSL_DPAA2_STREAM_ID_END -
216 FSL_DPAA2_STREAM_ID_START + 1, 1);
217
218 flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
219
220 return 0;
221}
222
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530223static int load_mc_dpc(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpc_addr)
J. German Rivera125e2bc2015-03-20 19:28:18 -0700224{
225 u64 mc_dpc_offset;
226#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
227 int error;
228 void *dpc_fdt_hdr;
229 int dpc_size;
230#endif
231
232#ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
233 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
234 CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
235
236 mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
237#else
238#error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
239#endif
240
241 /*
242 * Load the MC DPC blob in the MC private DRAM block:
243 */
244#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
245 printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
246#else
247 /*
248 * Get address and size of the DPC blob stored in flash:
249 */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530250 dpc_fdt_hdr = (void *)mc_dpc_addr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700251
252 error = fdt_check_header(dpc_fdt_hdr);
253 if (error != 0) {
254 /*
255 * Don't return with error here, since the MC firmware can
256 * still boot without a DPC
257 */
J. German Riveracc088c32015-07-02 11:28:56 +0530258 printf("\nfsl-mc: WARNING: No DPC image found");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700259 return 0;
260 }
261
262 dpc_size = fdt_totalsize(dpc_fdt_hdr);
263 if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530264 printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
J. German Rivera125e2bc2015-03-20 19:28:18 -0700265 dpc_size);
266 return -EINVAL;
267 }
268
269 mc_copy_image("MC DPC blob",
270 (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
271#endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
272
Stuart Yoder21c69872015-07-02 11:29:03 +0530273 if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
274 return -EINVAL;
275
J. German Rivera125e2bc2015-03-20 19:28:18 -0700276 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
277 return 0;
278}
279
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530280static int load_mc_dpl(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpl_addr)
J. German Rivera125e2bc2015-03-20 19:28:18 -0700281{
282 u64 mc_dpl_offset;
283#ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
284 int error;
285 void *dpl_fdt_hdr;
286 int dpl_size;
287#endif
288
289#ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
290 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
291 CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
292
293 mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
294#else
295#error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
296#endif
297
298 /*
299 * Load the MC DPL blob in the MC private DRAM block:
300 */
301#ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
302 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
303#else
304 /*
305 * Get address and size of the DPL blob stored in flash:
306 */
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530307 dpl_fdt_hdr = (void *)mc_dpl_addr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700308
309 error = fdt_check_header(dpl_fdt_hdr);
310 if (error != 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530311 printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700312 return error;
313 }
314
315 dpl_size = fdt_totalsize(dpl_fdt_hdr);
316 if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
J. German Riveracc088c32015-07-02 11:28:56 +0530317 printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
J. German Rivera125e2bc2015-03-20 19:28:18 -0700318 dpl_size);
319 return -EINVAL;
320 }
321
322 mc_copy_image("MC DPL blob",
323 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
324#endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
325
326 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
327 return 0;
328}
329
330/**
331 * Return the MC boot timeout value in milliseconds
332 */
333static unsigned long get_mc_boot_timeout_ms(void)
334{
335 unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
336
337 char *timeout_ms_env_var = getenv(MC_BOOT_TIMEOUT_ENV_VAR);
338
339 if (timeout_ms_env_var) {
340 timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
341 if (timeout_ms == 0) {
342 printf("fsl-mc: WARNING: Invalid value for \'"
343 MC_BOOT_TIMEOUT_ENV_VAR
344 "\' environment variable: %lu\n",
345 timeout_ms);
346
347 timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
348 }
349 }
350
351 return timeout_ms;
352}
353
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530354#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
355static int load_mc_aiop_img(u64 aiop_fw_addr)
J. German Riverac1000c12015-07-02 11:28:58 +0530356{
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530357 u64 mc_ram_addr = mc_get_dram_addr();
358#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
J. German Riverac1000c12015-07-02 11:28:58 +0530359 void *aiop_img;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530360#endif
J. German Riverac1000c12015-07-02 11:28:58 +0530361
362 /*
363 * Load the MC AIOP image in the MC private DRAM block:
364 */
365
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530366#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
367 printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
368 CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
369#else
370 aiop_img = (void *)aiop_fw_addr;
J. German Riverac1000c12015-07-02 11:28:58 +0530371 mc_copy_image("MC AIOP image",
372 (u64)aiop_img, CONFIG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
373 mc_ram_addr + CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530374#endif
375 mc_aiop_applied = 0;
J. German Riverac1000c12015-07-02 11:28:58 +0530376
377 return 0;
378}
379#endif
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530380
J. German Rivera125e2bc2015-03-20 19:28:18 -0700381static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
382{
383 u32 reg_gsr;
384 u32 mc_fw_boot_status;
385 unsigned long timeout_ms = get_mc_boot_timeout_ms();
386 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
387
388 dmb();
J. German Rivera125e2bc2015-03-20 19:28:18 -0700389 assert(timeout_ms > 0);
390 for (;;) {
391 udelay(1000); /* throttle polling */
392 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
393 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
394 if (mc_fw_boot_status & 0x1)
395 break;
396
397 timeout_ms--;
398 if (timeout_ms == 0)
399 break;
400 }
401
402 if (timeout_ms == 0) {
J. German Riveracc088c32015-07-02 11:28:56 +0530403 printf("ERROR: timeout\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700404
405 /* TODO: Get an error status from an MC CCSR register */
406 return -ETIMEDOUT;
407 }
408
409 if (mc_fw_boot_status != 0x1) {
410 /*
411 * TODO: Identify critical errors from the GSR register's FS
412 * field and for those errors, set error to -ENODEV or other
413 * appropriate errno, so that the status property is set to
414 * failure in the fsl,dprc device tree node.
415 */
J. German Riveracc088c32015-07-02 11:28:56 +0530416 printf("WARNING: Firmware returned an error (GSR: %#x)\n",
417 reg_gsr);
418 } else {
419 printf("SUCCESS\n");
J. German Rivera125e2bc2015-03-20 19:28:18 -0700420 }
421
J. German Riveracc088c32015-07-02 11:28:56 +0530422
J. German Rivera125e2bc2015-03-20 19:28:18 -0700423 *final_reg_gsr = reg_gsr;
424 return 0;
425}
J. German Riverab940ca62014-06-23 15:15:55 -0700426
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530427int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
J. German Riverab940ca62014-06-23 15:15:55 -0700428{
429 int error = 0;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700430 int portal_id = 0;
J. German Riverab940ca62014-06-23 15:15:55 -0700431 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530432 u64 mc_ram_addr = mc_get_dram_addr();
J. German Riverab940ca62014-06-23 15:15:55 -0700433 u32 reg_gsr;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700434 u32 reg_mcfbalr;
435#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Riverab940ca62014-06-23 15:15:55 -0700436 const void *raw_image_addr;
437 size_t raw_image_size = 0;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700438#endif
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800439 struct mc_version mc_ver_info;
J. German Rivera125e2bc2015-03-20 19:28:18 -0700440 u64 mc_ram_aligned_base_addr;
441 u8 mc_ram_num_256mb_blocks;
442 size_t mc_ram_size = mc_get_dram_block_size();
J. German Riverab940ca62014-06-23 15:15:55 -0700443
J. German Riverab940ca62014-06-23 15:15:55 -0700444
J. German Rivera125e2bc2015-03-20 19:28:18 -0700445 error = calculate_mc_private_ram_params(mc_ram_addr,
446 mc_ram_size,
447 &mc_ram_aligned_base_addr,
448 &mc_ram_num_256mb_blocks);
449 if (error != 0)
450 goto out;
451
J. German Riverab940ca62014-06-23 15:15:55 -0700452 /*
453 * Management Complex cores should be held at reset out of POR.
454 * U-boot should be the first software to touch MC. To be safe,
455 * we reset all cores again by setting GCR1 to 0. It doesn't do
456 * anything if they are held at reset. After we setup the firmware
457 * we kick off MC by deasserting the reset bit for core 0, and
458 * deasserting the reset bits for Command Portal Managers.
459 * The stop bits are not touched here. They are used to stop the
460 * cores when they are active. Setting stop bits doesn't stop the
461 * cores from fetching instructions when they are released from
462 * reset.
463 */
464 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
465 dmb();
466
J. German Rivera125e2bc2015-03-20 19:28:18 -0700467#ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
468 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
469#else
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530470 error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
471 &raw_image_size);
J. German Riverab940ca62014-06-23 15:15:55 -0700472 if (error != 0)
473 goto out;
474 /*
475 * Load the MC FW at the beginning of the MC private DRAM block:
476 */
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800477 mc_copy_image("MC Firmware",
478 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800479#endif
J. German Rivera125e2bc2015-03-20 19:28:18 -0700480 dump_ram_words("firmware", (void *)mc_ram_addr);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800481
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530482 error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700483 if (error != 0)
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800484 goto out;
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800485
J. German Riverab940ca62014-06-23 15:15:55 -0700486 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700487 dump_mc_ccsr_regs(mc_ccsr_regs);
J. German Riverab940ca62014-06-23 15:15:55 -0700488
489 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700490 * Tell MC what is the address range of the DRAM block assigned to it:
J. German Riverab940ca62014-06-23 15:15:55 -0700491 */
J. German Rivera125e2bc2015-03-20 19:28:18 -0700492 reg_mcfbalr = (u32)mc_ram_aligned_base_addr |
493 (mc_ram_num_256mb_blocks - 1);
494 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
495 out_le32(&mc_ccsr_regs->reg_mcfbahr,
496 (u32)(mc_ram_aligned_base_addr >> 32));
Stuart Yoder39da6442015-07-02 11:29:02 +0530497 out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
J. German Riverab940ca62014-06-23 15:15:55 -0700498
499 /*
J. German Rivera125e2bc2015-03-20 19:28:18 -0700500 * Tell the MC that we want delayed DPL deployment.
J. German Riverab940ca62014-06-23 15:15:55 -0700501 */
J. German Rivera125e2bc2015-03-20 19:28:18 -0700502 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
J. German Riverab940ca62014-06-23 15:15:55 -0700503
J. German Riveracc088c32015-07-02 11:28:56 +0530504 printf("\nfsl-mc: Booting Management Complex ... ");
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800505
J. German Riverab940ca62014-06-23 15:15:55 -0700506 /*
507 * Deassert reset and release MC core 0 to run
508 */
509 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
J. German Rivera125e2bc2015-03-20 19:28:18 -0700510 error = wait_for_mc(true, &reg_gsr);
511 if (error != 0)
J. German Riverab940ca62014-06-23 15:15:55 -0700512 goto out;
J. German Riverab940ca62014-06-23 15:15:55 -0700513
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800514 /*
515 * TODO: need to obtain the portal_id for the root container from the
516 * DPL
517 */
518 portal_id = 0;
519
520 /*
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700521 * Initialize the global default MC portal
522 * And check that the MC firmware is responding portal commands:
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800523 */
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700524 dflt_mc_io = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
525 if (!dflt_mc_io) {
526 printf(" No memory: malloc() failed\n");
527 return -ENOMEM;
528 }
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800529
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700530 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
531 debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
532 portal_id, dflt_mc_io->mmio_regs);
533
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530534 error = mc_get_version(dflt_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800535 if (error != 0) {
536 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
537 error);
538 goto out;
539 }
540
Prabhakar Kushwaha2b7c4a12015-07-02 11:29:01 +0530541 if (MC_VER_MAJOR != mc_ver_info.major) {
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800542 printf("fsl-mc: ERROR: Firmware major version mismatch (found: %d, expected: %d)\n",
543 mc_ver_info.major, MC_VER_MAJOR);
Prabhakar Kushwaha2b7c4a12015-07-02 11:29:01 +0530544 printf("fsl-mc: Update the Management Complex firmware\n");
545
546 error = -ENODEV;
547 goto out;
548 }
J. German Rivera7b3bd9a2015-01-06 13:19:02 -0800549
550 if (MC_VER_MINOR != mc_ver_info.minor)
551 printf("fsl-mc: WARNING: Firmware minor version mismatch (found: %d, expected: %d)\n",
552 mc_ver_info.minor, MC_VER_MINOR);
553
554 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
555 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
J. German Rivera125e2bc2015-03-20 19:28:18 -0700556 reg_gsr & GSR_FS_MASK);
557
J. German Riverab940ca62014-06-23 15:15:55 -0700558out:
559 if (error != 0)
Prabhakar Kushwaha2b7c4a12015-07-02 11:29:01 +0530560 mc_boot_status = error;
J. German Riverab940ca62014-06-23 15:15:55 -0700561 else
562 mc_boot_status = 0;
563
564 return error;
565}
566
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530567int mc_apply_dpl(u64 mc_dpl_addr)
568{
569 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
570 int error = 0;
571 u32 reg_gsr;
572 u64 mc_ram_addr = mc_get_dram_addr();
573 size_t mc_ram_size = mc_get_dram_block_size();
574
575 error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
576 if (error != 0)
577 return error;
578
579 /*
580 * Tell the MC to deploy the DPL:
581 */
582 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
583 printf("fsl-mc: Deploying data path layout ... ");
584 error = wait_for_mc(false, &reg_gsr);
585
586 if (!error)
587 mc_dpl_applied = 0;
588
589 return error;
590}
591
J. German Riverab940ca62014-06-23 15:15:55 -0700592int get_mc_boot_status(void)
593{
594 return mc_boot_status;
595}
596
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530597#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
598int get_aiop_apply_status(void)
599{
600 return mc_aiop_applied;
601}
602#endif
603
604int get_dpl_apply_status(void)
605{
606 return mc_dpl_applied;
607}
608
609/**
610 * Return the MC address of private DRAM block.
611 */
612u64 mc_get_dram_addr(void)
613{
614 u64 mc_ram_addr;
615
616 /*
617 * The MC private DRAM block was already carved at the end of DRAM
618 * by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
619 */
620 if (gd->bd->bi_dram[1].start) {
621 mc_ram_addr =
622 gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
623 } else {
624 mc_ram_addr =
625 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
626 }
627
628 return mc_ram_addr;
629}
630
J. German Riverab940ca62014-06-23 15:15:55 -0700631/**
632 * Return the actual size of the MC private DRAM block.
J. German Riverab940ca62014-06-23 15:15:55 -0700633 */
634unsigned long mc_get_dram_block_size(void)
635{
J. German Rivera125e2bc2015-03-20 19:28:18 -0700636 unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
637
638 char *dram_block_size_env_var = getenv(MC_MEM_SIZE_ENV_VAR);
639
640 if (dram_block_size_env_var) {
641 dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
642 10);
643
644 if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
645 printf("fsl-mc: WARNING: Invalid value for \'"
646 MC_MEM_SIZE_ENV_VAR
647 "\' environment variable: %lu\n",
648 dram_block_size);
649
650 dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
651 }
652 }
653
654 return dram_block_size;
J. German Riverab940ca62014-06-23 15:15:55 -0700655}
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700656
657int dpio_init(struct dprc_obj_desc obj_desc)
658{
659 struct qbman_swp_desc p_des;
660 struct dpio_attr attr;
661 int err = 0;
662
663 dflt_dpio = (struct fsl_dpio_obj *)malloc(sizeof(struct fsl_dpio_obj));
664 if (!dflt_dpio) {
665 printf(" No memory: malloc() failed\n");
666 return -ENOMEM;
667 }
668
669 dflt_dpio->dpio_id = obj_desc.id;
670
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530671 err = dpio_open(dflt_mc_io, MC_CMD_NO_FLAGS, obj_desc.id,
672 &dflt_dpio_handle);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700673 if (err) {
674 printf("dpio_open() failed\n");
675 goto err_open;
676 }
677
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530678 err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
679 dflt_dpio_handle, &attr);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700680 if (err) {
681 printf("dpio_get_attributes() failed %d\n", err);
682 goto err_get_attr;
683 }
684
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530685 err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio_handle);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700686 if (err) {
687 printf("dpio_enable() failed %d\n", err);
688 goto err_get_enable;
689 }
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +0530690 debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
691 attr.qbman_portal_ce_offset,
692 attr.qbman_portal_ci_offset,
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700693 attr.qbman_portal_id,
694 attr.num_priorities);
695
Prabhakar Kushwaha1f1c25c2015-07-02 11:28:59 +0530696 p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
697 + attr.qbman_portal_ce_offset);
698 p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
699 + attr.qbman_portal_ci_offset);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700700
701 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
702 if (dflt_dpio->sw_portal == NULL) {
703 printf("qbman_swp_init() failed\n");
704 goto err_get_swp_init;
705 }
706 return 0;
707
708err_get_swp_init:
709err_get_enable:
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530710 dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio_handle);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700711err_get_attr:
Prabhakar Kushwaha87457d12015-07-07 15:40:06 +0530712 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio_handle);
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700713err_open:
714 free(dflt_dpio);
715 return err;
716}
717
718int dpbp_init(struct dprc_obj_desc obj_desc)
719{
720 dflt_dpbp = (struct fsl_dpbp_obj *)malloc(sizeof(struct fsl_dpbp_obj));
721 if (!dflt_dpbp) {
722 printf(" No memory: malloc() failed\n");
723 return -ENOMEM;
724 }
725 dflt_dpbp->dpbp_attr.id = obj_desc.id;
726
727 return 0;
728}
729
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700730int fsl_mc_ldpaa_init(bd_t *bis)
731{
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700732
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530733 return 0;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700734}
735
736void fsl_mc_ldpaa_exit(bd_t *bis)
737{
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530738 return;
Prabhakar Kushwahaa2a55e52015-03-19 09:20:45 -0700739}
Prabhakar Kushwahafb4a87a2015-11-04 12:25:58 +0530740
741static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
742{
743 int err = 0;
744 if (argc < 3)
745 goto usage;
746
747 switch (argv[1][0]) {
748 case 's': {
749 char sub_cmd;
750 u64 mc_fw_addr, mc_dpc_addr, aiop_fw_addr;
751
752 sub_cmd = argv[2][0];
753 switch (sub_cmd) {
754 case 'm':
755 if (argc < 5)
756 goto usage;
757
758 if (get_mc_boot_status() == 0) {
759 printf("fsl-mc: MC is already booted");
760 printf("\n");
761 return err;
762 }
763 mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
764 mc_dpc_addr = simple_strtoull(argv[4], NULL,
765 16);
766 err = mc_init(mc_fw_addr, mc_dpc_addr);
767 break;
768
769#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
770 case 'a':
771 if (argc < 4)
772 goto usage;
773 if (get_aiop_apply_status() == 0) {
774 printf("fsl-mc: AIOP FW is already");
775 printf(" applied\n");
776 return err;
777 }
778
779 aiop_fw_addr = simple_strtoull(argv[3], NULL,
780 16);
781
782 err = load_mc_aiop_img(aiop_fw_addr);
783 if (!err)
784 printf("fsl-mc: AIOP FW applied\n");
785 break;
786#endif
787 default:
788 printf("Invalid option: %s\n", argv[2]);
789 goto usage;
790
791 break;
792 }
793 }
794 break;
795
796 case 'a': {
797 u64 mc_dpl_addr;
798
799 if (argc < 4)
800 goto usage;
801
802 if (get_dpl_apply_status() == 0) {
803 printf("fsl-mc: DPL already applied\n");
804 return err;
805 }
806
807 mc_dpl_addr = simple_strtoull(argv[3], NULL,
808 16);
809 if (get_mc_boot_status() != 0) {
810 printf("fsl-mc: Deploying data path layout ..");
811 printf("ERROR (MC is not booted)\n");
812 return -ENODEV;
813 }
814 err = mc_apply_dpl(mc_dpl_addr);
815 break;
816 }
817 default:
818 printf("Invalid option: %s\n", argv[1]);
819 goto usage;
820 break;
821 }
822 return err;
823 usage:
824 return CMD_RET_USAGE;
825}
826
827U_BOOT_CMD(
828 fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
829 "DPAA2 command to manage Management Complex (MC)",
830 "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
831 "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
832 "fsl_mc start aiop [FW_addr] - Start AIOP\n"
833);