blob: d2b8b5c47f15b757548a8b574e76f322b22e8da0 [file] [log] [blame]
J. German Rivera8ff14b72014-06-23 15:15:55 -07001/*
2 * Copyright (C) 2014 Freescale Semiconductor
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
Stuart Yodera2bdb772015-07-02 11:29:03 +05306#include <common.h>
J. German Rivera8ff14b72014-06-23 15:15:55 -07007#include <errno.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +09008#include <linux/bug.h>
J. German Rivera8ff14b72014-06-23 15:15:55 -07009#include <asm/io.h>
Stuart Yodera2bdb772015-07-02 11:29:03 +053010#include <libfdt.h>
11#include <fdt_support.h>
J. German Rivera43e4ae32015-01-06 13:19:02 -080012#include <fsl-mc/fsl_mc.h>
13#include <fsl-mc/fsl_mc_sys.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070014#include <fsl-mc/fsl_mc_private.h>
J. German Rivera43e4ae32015-01-06 13:19:02 -080015#include <fsl-mc/fsl_dpmng.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070016#include <fsl-mc/fsl_dprc.h>
17#include <fsl-mc/fsl_dpio.h>
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053018#include <fsl-mc/fsl_dpni.h>
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070019#include <fsl-mc/fsl_qbman_portal.h>
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053020#include <fsl-mc/ldpaa_wriop.h>
J. German Rivera8ff14b72014-06-23 15:15:55 -070021
J. German Riveraf4fed4b2015-03-20 19:28:18 -070022#define MC_RAM_BASE_ADDR_ALIGNMENT (512UL * 1024 * 1024)
23#define MC_RAM_BASE_ADDR_ALIGNMENT_MASK (~(MC_RAM_BASE_ADDR_ALIGNMENT - 1))
24#define MC_RAM_SIZE_ALIGNMENT (256UL * 1024 * 1024)
25
26#define MC_MEM_SIZE_ENV_VAR "mcmemsize"
27#define MC_BOOT_TIMEOUT_ENV_VAR "mcboottimeout"
28
J. German Rivera8ff14b72014-06-23 15:15:55 -070029DECLARE_GLOBAL_DATA_PTR;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +053030static int mc_boot_status = -1;
31static int mc_dpl_applied = -1;
32#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
33static int mc_aiop_applied = -1;
34#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +053035struct fsl_mc_io *root_mc_io = NULL;
36struct fsl_mc_io *dflt_mc_io = NULL; /* child container */
37uint16_t root_dprc_handle = 0;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070038uint16_t dflt_dprc_handle = 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +053039int child_dprc_id;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -070040struct fsl_dpbp_obj *dflt_dpbp = NULL;
41struct fsl_dpio_obj *dflt_dpio = NULL;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +053042struct fsl_dpni_obj *dflt_dpni = NULL;
J. German Riveraf4fed4b2015-03-20 19:28:18 -070043
44#ifdef DEBUG
45void dump_ram_words(const char *title, void *addr)
46{
47 int i;
48 uint32_t *words = addr;
49
50 printf("Dumping beginning of %s (%p):\n", title, addr);
51 for (i = 0; i < 16; i++)
52 printf("%#x ", words[i]);
53
54 printf("\n");
55}
J. German Rivera8ff14b72014-06-23 15:15:55 -070056
J. German Riveraf4fed4b2015-03-20 19:28:18 -070057void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
58{
59 printf("MC CCSR registers:\n"
60 "reg_gcr1 %#x\n"
61 "reg_gsr %#x\n"
62 "reg_sicbalr %#x\n"
63 "reg_sicbahr %#x\n"
64 "reg_sicapr %#x\n"
65 "reg_mcfbalr %#x\n"
66 "reg_mcfbahr %#x\n"
67 "reg_mcfapr %#x\n"
68 "reg_psr %#x\n",
69 mc_ccsr_regs->reg_gcr1,
70 mc_ccsr_regs->reg_gsr,
71 mc_ccsr_regs->reg_sicbalr,
72 mc_ccsr_regs->reg_sicbahr,
73 mc_ccsr_regs->reg_sicapr,
74 mc_ccsr_regs->reg_mcfbalr,
75 mc_ccsr_regs->reg_mcfbahr,
76 mc_ccsr_regs->reg_mcfapr,
77 mc_ccsr_regs->reg_psr);
78}
79#else
80
81#define dump_ram_words(title, addr)
82#define dump_mc_ccsr_regs(mc_ccsr_regs)
83
84#endif /* DEBUG */
85
86#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Rivera8ff14b72014-06-23 15:15:55 -070087/**
88 * Copying MC firmware or DPL image to DDR
89 */
90static int mc_copy_image(const char *title,
J. German Rivera43e4ae32015-01-06 13:19:02 -080091 u64 image_addr, u32 image_size, u64 mc_ram_addr)
J. German Rivera8ff14b72014-06-23 15:15:55 -070092{
93 debug("%s copied to address %p\n", title, (void *)mc_ram_addr);
94 memcpy((void *)mc_ram_addr, (void *)image_addr, image_size);
J. German Riveraf4fed4b2015-03-20 19:28:18 -070095 flush_dcache_range(mc_ram_addr, mc_ram_addr + image_size);
J. German Rivera8ff14b72014-06-23 15:15:55 -070096 return 0;
97}
98
99/**
100 * MC firmware FIT image parser checks if the image is in FIT
101 * format, verifies integrity of the image and calculates
102 * raw image address and size values.
J. German Rivera43e4ae32015-01-06 13:19:02 -0800103 * Returns 0 on success and a negative errno on error.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700104 * task fail.
105 **/
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530106int parse_mc_firmware_fit_image(u64 mc_fw_addr,
107 const void **raw_image_addr,
J. German Rivera8ff14b72014-06-23 15:15:55 -0700108 size_t *raw_image_size)
109{
110 int format;
111 void *fit_hdr;
112 int node_offset;
113 const void *data;
114 size_t size;
115 const char *uname = "firmware";
116
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530117 fit_hdr = (void *)mc_fw_addr;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700118
119 /* Check if Image is in FIT format */
120 format = genimg_get_format(fit_hdr);
121
122 if (format != IMAGE_FORMAT_FIT) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530123 printf("fsl-mc: ERR: Bad firmware image (not a FIT image)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800124 return -EINVAL;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700125 }
126
127 if (!fit_check_format(fit_hdr)) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530128 printf("fsl-mc: ERR: Bad firmware image (bad FIT header)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800129 return -EINVAL;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700130 }
131
132 node_offset = fit_image_get_node(fit_hdr, uname);
133
134 if (node_offset < 0) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530135 printf("fsl-mc: ERR: Bad firmware image (missing subimage)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800136 return -ENOENT;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700137 }
138
139 /* Verify MC firmware image */
140 if (!(fit_image_verify(fit_hdr, node_offset))) {
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530141 printf("fsl-mc: ERR: Bad firmware image (bad CRC)\n");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800142 return -EINVAL;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700143 }
144
145 /* Get address and size of raw image */
146 fit_image_get_data(fit_hdr, node_offset, &data, &size);
147
148 *raw_image_addr = data;
149 *raw_image_size = size;
150
151 return 0;
152}
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700153#endif
154
155/*
156 * Calculates the values to be used to specify the address range
157 * for the MC private DRAM block, in the MCFBALR/MCFBAHR registers.
158 * It returns the highest 512MB-aligned address within the given
159 * address range, in '*aligned_base_addr', and the number of 256 MiB
160 * blocks in it, in 'num_256mb_blocks'.
161 */
162static int calculate_mc_private_ram_params(u64 mc_private_ram_start_addr,
163 size_t mc_ram_size,
164 u64 *aligned_base_addr,
165 u8 *num_256mb_blocks)
166{
167 u64 addr;
168 u16 num_blocks;
169
170 if (mc_ram_size % MC_RAM_SIZE_ALIGNMENT != 0) {
171 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
172 mc_ram_size);
173 return -EINVAL;
174 }
175
176 num_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
177 if (num_blocks < 1 || num_blocks > 0xff) {
178 printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
179 mc_ram_size);
180 return -EINVAL;
181 }
182
183 addr = (mc_private_ram_start_addr + mc_ram_size - 1) &
184 MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
185
186 if (addr < mc_private_ram_start_addr) {
187 printf("fsl-mc: ERROR: bad start address %#llx\n",
188 mc_private_ram_start_addr);
189 return -EFAULT;
190 }
191
192 *aligned_base_addr = addr;
193 *num_256mb_blocks = num_blocks;
194 return 0;
195}
196
Stuart Yodera2bdb772015-07-02 11:29:03 +0530197static int mc_fixup_dpc(u64 dpc_addr)
198{
199 void *blob = (void *)dpc_addr;
200 int nodeoffset;
201
202 /* delete any existing ICID pools */
203 nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
204 if (fdt_del_node(blob, nodeoffset) < 0)
205 printf("\nfsl-mc: WARNING: could not delete ICID pool\n");
206
207 /* add a new pool */
208 nodeoffset = fdt_path_offset(blob, "/resources");
209 if (nodeoffset < 0) {
210 printf("\nfsl-mc: ERROR: DPC is missing /resources\n");
211 return -EINVAL;
212 }
213 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pools");
214 nodeoffset = fdt_add_subnode(blob, nodeoffset, "icid_pool@0");
215 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
216 "base_icid", FSL_DPAA2_STREAM_ID_START, 1);
217 do_fixup_by_path_u32(blob, "/resources/icid_pools/icid_pool@0",
218 "num",
219 FSL_DPAA2_STREAM_ID_END -
220 FSL_DPAA2_STREAM_ID_START + 1, 1);
221
222 flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
223
224 return 0;
225}
226
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530227static int load_mc_dpc(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpc_addr)
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700228{
229 u64 mc_dpc_offset;
230#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
231 int error;
232 void *dpc_fdt_hdr;
233 int dpc_size;
234#endif
235
236#ifdef CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET
237 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET & 0x3) != 0 ||
238 CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET > 0xffffffff);
239
240 mc_dpc_offset = CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET;
241#else
242#error "CONFIG_SYS_LS_MC_DRAM_DPC_OFFSET not defined"
243#endif
244
245 /*
246 * Load the MC DPC blob in the MC private DRAM block:
247 */
248#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
249 printf("MC DPC is preloaded to %#llx\n", mc_ram_addr + mc_dpc_offset);
250#else
251 /*
252 * Get address and size of the DPC blob stored in flash:
253 */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530254 dpc_fdt_hdr = (void *)mc_dpc_addr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700255
256 error = fdt_check_header(dpc_fdt_hdr);
257 if (error != 0) {
258 /*
259 * Don't return with error here, since the MC firmware can
260 * still boot without a DPC
261 */
J. German Rivera75d7ace2015-07-02 11:28:56 +0530262 printf("\nfsl-mc: WARNING: No DPC image found");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700263 return 0;
264 }
265
266 dpc_size = fdt_totalsize(dpc_fdt_hdr);
267 if (dpc_size > CONFIG_SYS_LS_MC_DPC_MAX_LENGTH) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530268 printf("\nfsl-mc: ERROR: Bad DPC image (too large: %d)\n",
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700269 dpc_size);
270 return -EINVAL;
271 }
272
273 mc_copy_image("MC DPC blob",
274 (u64)dpc_fdt_hdr, dpc_size, mc_ram_addr + mc_dpc_offset);
275#endif /* not defined CONFIG_SYS_LS_MC_DPC_IN_DDR */
276
Stuart Yodera2bdb772015-07-02 11:29:03 +0530277 if (mc_fixup_dpc(mc_ram_addr + mc_dpc_offset))
278 return -EINVAL;
279
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700280 dump_ram_words("DPC", (void *)(mc_ram_addr + mc_dpc_offset));
281 return 0;
282}
283
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530284static int load_mc_dpl(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpl_addr)
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700285{
286 u64 mc_dpl_offset;
287#ifndef CONFIG_SYS_LS_MC_DPL_IN_DDR
288 int error;
289 void *dpl_fdt_hdr;
290 int dpl_size;
291#endif
292
293#ifdef CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET
294 BUILD_BUG_ON((CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET & 0x3) != 0 ||
295 CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET > 0xffffffff);
296
297 mc_dpl_offset = CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET;
298#else
299#error "CONFIG_SYS_LS_MC_DRAM_DPL_OFFSET not defined"
300#endif
301
302 /*
303 * Load the MC DPL blob in the MC private DRAM block:
304 */
305#ifdef CONFIG_SYS_LS_MC_DPL_IN_DDR
306 printf("MC DPL is preloaded to %#llx\n", mc_ram_addr + mc_dpl_offset);
307#else
308 /*
309 * Get address and size of the DPL blob stored in flash:
310 */
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530311 dpl_fdt_hdr = (void *)mc_dpl_addr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700312
313 error = fdt_check_header(dpl_fdt_hdr);
314 if (error != 0) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530315 printf("\nfsl-mc: ERROR: Bad DPL image (bad header)\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700316 return error;
317 }
318
319 dpl_size = fdt_totalsize(dpl_fdt_hdr);
320 if (dpl_size > CONFIG_SYS_LS_MC_DPL_MAX_LENGTH) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530321 printf("\nfsl-mc: ERROR: Bad DPL image (too large: %d)\n",
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700322 dpl_size);
323 return -EINVAL;
324 }
325
326 mc_copy_image("MC DPL blob",
327 (u64)dpl_fdt_hdr, dpl_size, mc_ram_addr + mc_dpl_offset);
328#endif /* not defined CONFIG_SYS_LS_MC_DPL_IN_DDR */
329
330 dump_ram_words("DPL", (void *)(mc_ram_addr + mc_dpl_offset));
331 return 0;
332}
333
334/**
335 * Return the MC boot timeout value in milliseconds
336 */
337static unsigned long get_mc_boot_timeout_ms(void)
338{
339 unsigned long timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
340
341 char *timeout_ms_env_var = getenv(MC_BOOT_TIMEOUT_ENV_VAR);
342
343 if (timeout_ms_env_var) {
344 timeout_ms = simple_strtoul(timeout_ms_env_var, NULL, 10);
345 if (timeout_ms == 0) {
346 printf("fsl-mc: WARNING: Invalid value for \'"
347 MC_BOOT_TIMEOUT_ENV_VAR
348 "\' environment variable: %lu\n",
349 timeout_ms);
350
351 timeout_ms = CONFIG_SYS_LS_MC_BOOT_TIMEOUT_MS;
352 }
353 }
354
355 return timeout_ms;
356}
357
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530358#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
359static int load_mc_aiop_img(u64 aiop_fw_addr)
J. German Riverac3b505f2015-07-02 11:28:58 +0530360{
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530361 u64 mc_ram_addr = mc_get_dram_addr();
362#ifndef CONFIG_SYS_LS_MC_DPC_IN_DDR
J. German Riverac3b505f2015-07-02 11:28:58 +0530363 void *aiop_img;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530364#endif
J. German Riverac3b505f2015-07-02 11:28:58 +0530365
366 /*
367 * Load the MC AIOP image in the MC private DRAM block:
368 */
369
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530370#ifdef CONFIG_SYS_LS_MC_DPC_IN_DDR
371 printf("MC AIOP is preloaded to %#llx\n", mc_ram_addr +
372 CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
373#else
374 aiop_img = (void *)aiop_fw_addr;
J. German Riverac3b505f2015-07-02 11:28:58 +0530375 mc_copy_image("MC AIOP image",
376 (u64)aiop_img, CONFIG_SYS_LS_MC_AIOP_IMG_MAX_LENGTH,
377 mc_ram_addr + CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530378#endif
379 mc_aiop_applied = 0;
J. German Riverac3b505f2015-07-02 11:28:58 +0530380
381 return 0;
382}
383#endif
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530384
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700385static int wait_for_mc(bool booting_mc, u32 *final_reg_gsr)
386{
387 u32 reg_gsr;
388 u32 mc_fw_boot_status;
389 unsigned long timeout_ms = get_mc_boot_timeout_ms();
390 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
391
392 dmb();
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700393 assert(timeout_ms > 0);
394 for (;;) {
395 udelay(1000); /* throttle polling */
396 reg_gsr = in_le32(&mc_ccsr_regs->reg_gsr);
397 mc_fw_boot_status = (reg_gsr & GSR_FS_MASK);
398 if (mc_fw_boot_status & 0x1)
399 break;
400
401 timeout_ms--;
402 if (timeout_ms == 0)
403 break;
404 }
405
406 if (timeout_ms == 0) {
J. German Rivera75d7ace2015-07-02 11:28:56 +0530407 printf("ERROR: timeout\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700408
409 /* TODO: Get an error status from an MC CCSR register */
410 return -ETIMEDOUT;
411 }
412
413 if (mc_fw_boot_status != 0x1) {
414 /*
415 * TODO: Identify critical errors from the GSR register's FS
416 * field and for those errors, set error to -ENODEV or other
417 * appropriate errno, so that the status property is set to
418 * failure in the fsl,dprc device tree node.
419 */
J. German Rivera75d7ace2015-07-02 11:28:56 +0530420 printf("WARNING: Firmware returned an error (GSR: %#x)\n",
421 reg_gsr);
422 } else {
423 printf("SUCCESS\n");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700424 }
425
J. German Rivera75d7ace2015-07-02 11:28:56 +0530426
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700427 *final_reg_gsr = reg_gsr;
428 return 0;
429}
J. German Rivera8ff14b72014-06-23 15:15:55 -0700430
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530431int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700432{
433 int error = 0;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700434 int portal_id = 0;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700435 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530436 u64 mc_ram_addr = mc_get_dram_addr();
J. German Rivera8ff14b72014-06-23 15:15:55 -0700437 u32 reg_gsr;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700438 u32 reg_mcfbalr;
439#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
J. German Rivera8ff14b72014-06-23 15:15:55 -0700440 const void *raw_image_addr;
441 size_t raw_image_size = 0;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700442#endif
J. German Rivera43e4ae32015-01-06 13:19:02 -0800443 struct mc_version mc_ver_info;
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700444 u64 mc_ram_aligned_base_addr;
445 u8 mc_ram_num_256mb_blocks;
446 size_t mc_ram_size = mc_get_dram_block_size();
J. German Rivera8ff14b72014-06-23 15:15:55 -0700447
J. German Rivera8ff14b72014-06-23 15:15:55 -0700448
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700449 error = calculate_mc_private_ram_params(mc_ram_addr,
450 mc_ram_size,
451 &mc_ram_aligned_base_addr,
452 &mc_ram_num_256mb_blocks);
453 if (error != 0)
454 goto out;
455
J. German Rivera8ff14b72014-06-23 15:15:55 -0700456 /*
457 * Management Complex cores should be held at reset out of POR.
Bin Meng75574052016-02-05 19:30:11 -0800458 * U-Boot should be the first software to touch MC. To be safe,
J. German Rivera8ff14b72014-06-23 15:15:55 -0700459 * we reset all cores again by setting GCR1 to 0. It doesn't do
460 * anything if they are held at reset. After we setup the firmware
461 * we kick off MC by deasserting the reset bit for core 0, and
462 * deasserting the reset bits for Command Portal Managers.
463 * The stop bits are not touched here. They are used to stop the
464 * cores when they are active. Setting stop bits doesn't stop the
465 * cores from fetching instructions when they are released from
466 * reset.
467 */
468 out_le32(&mc_ccsr_regs->reg_gcr1, 0);
469 dmb();
470
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700471#ifdef CONFIG_SYS_LS_MC_FW_IN_DDR
472 printf("MC firmware is preloaded to %#llx\n", mc_ram_addr);
473#else
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530474 error = parse_mc_firmware_fit_image(mc_fw_addr, &raw_image_addr,
475 &raw_image_size);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700476 if (error != 0)
477 goto out;
478 /*
479 * Load the MC FW at the beginning of the MC private DRAM block:
480 */
J. German Rivera43e4ae32015-01-06 13:19:02 -0800481 mc_copy_image("MC Firmware",
482 (u64)raw_image_addr, raw_image_size, mc_ram_addr);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800483#endif
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700484 dump_ram_words("firmware", (void *)mc_ram_addr);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800485
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530486 error = load_mc_dpc(mc_ram_addr, mc_ram_size, mc_dpc_addr);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700487 if (error != 0)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700488 goto out;
J. German Riverac3b505f2015-07-02 11:28:58 +0530489
J. German Rivera8ff14b72014-06-23 15:15:55 -0700490 debug("mc_ccsr_regs %p\n", mc_ccsr_regs);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700491 dump_mc_ccsr_regs(mc_ccsr_regs);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700492
493 /*
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700494 * Tell MC what is the address range of the DRAM block assigned to it:
J. German Rivera8ff14b72014-06-23 15:15:55 -0700495 */
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700496 reg_mcfbalr = (u32)mc_ram_aligned_base_addr |
497 (mc_ram_num_256mb_blocks - 1);
498 out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
499 out_le32(&mc_ccsr_regs->reg_mcfbahr,
500 (u32)(mc_ram_aligned_base_addr >> 32));
Stuart Yoderd1970352015-07-02 11:29:02 +0530501 out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700502
503 /*
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700504 * Tell the MC that we want delayed DPL deployment.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700505 */
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700506 out_le32(&mc_ccsr_regs->reg_gsr, 0xDD00);
J. German Rivera8ff14b72014-06-23 15:15:55 -0700507
J. German Rivera75d7ace2015-07-02 11:28:56 +0530508 printf("\nfsl-mc: Booting Management Complex ... ");
J. German Rivera43e4ae32015-01-06 13:19:02 -0800509
J. German Rivera8ff14b72014-06-23 15:15:55 -0700510 /*
511 * Deassert reset and release MC core 0 to run
512 */
513 out_le32(&mc_ccsr_regs->reg_gcr1, GCR1_P1_DE_RST | GCR1_M_ALL_DE_RST);
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700514 error = wait_for_mc(true, &reg_gsr);
515 if (error != 0)
J. German Rivera8ff14b72014-06-23 15:15:55 -0700516 goto out;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700517
J. German Rivera43e4ae32015-01-06 13:19:02 -0800518 /*
519 * TODO: need to obtain the portal_id for the root container from the
520 * DPL
521 */
522 portal_id = 0;
523
524 /*
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700525 * Initialize the global default MC portal
526 * And check that the MC firmware is responding portal commands:
J. German Rivera43e4ae32015-01-06 13:19:02 -0800527 */
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530528 root_mc_io = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
529 if (!root_mc_io) {
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700530 printf(" No memory: malloc() failed\n");
531 return -ENOMEM;
532 }
533
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530534 root_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(portal_id);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800535 debug("Checking access to MC portal of root DPRC container (portal_id %d, portal physical addr %p)\n",
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530536 portal_id, root_mc_io->mmio_regs);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800537
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530538 error = mc_get_version(root_mc_io, MC_CMD_NO_FLAGS, &mc_ver_info);
J. German Rivera43e4ae32015-01-06 13:19:02 -0800539 if (error != 0) {
540 printf("fsl-mc: ERROR: Firmware version check failed (error: %d)\n",
541 error);
542 goto out;
543 }
544
J. German Rivera43e4ae32015-01-06 13:19:02 -0800545 printf("fsl-mc: Management Complex booted (version: %d.%d.%d, boot status: %#x)\n",
546 mc_ver_info.major, mc_ver_info.minor, mc_ver_info.revision,
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700547 reg_gsr & GSR_FS_MASK);
548
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530549out:
550 if (error != 0)
551 mc_boot_status = error;
552 else
553 mc_boot_status = 0;
554
555 return error;
556}
557
558int mc_apply_dpl(u64 mc_dpl_addr)
559{
560 struct mc_ccsr_registers __iomem *mc_ccsr_regs = MC_CCSR_BASE_ADDR;
561 int error = 0;
562 u32 reg_gsr;
563 u64 mc_ram_addr = mc_get_dram_addr();
564 size_t mc_ram_size = mc_get_dram_block_size();
565
566 error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
567 if (error != 0)
568 return error;
569
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700570 /*
571 * Tell the MC to deploy the DPL:
572 */
573 out_le32(&mc_ccsr_regs->reg_gsr, 0x0);
J. German Rivera75d7ace2015-07-02 11:28:56 +0530574 printf("fsl-mc: Deploying data path layout ... ");
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700575 error = wait_for_mc(false, &reg_gsr);
J. German Rivera75d7ace2015-07-02 11:28:56 +0530576
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530577 if (!error)
578 mc_dpl_applied = 0;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700579
580 return error;
581}
582
583int get_mc_boot_status(void)
584{
585 return mc_boot_status;
586}
587
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530588#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
589int get_aiop_apply_status(void)
590{
591 return mc_aiop_applied;
592}
593#endif
594
595int get_dpl_apply_status(void)
596{
597 return mc_dpl_applied;
598}
599
J. German Rivera8ff14b72014-06-23 15:15:55 -0700600/**
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530601 * Return the MC address of private DRAM block.
602 */
603u64 mc_get_dram_addr(void)
604{
605 u64 mc_ram_addr;
606
607 /*
608 * The MC private DRAM block was already carved at the end of DRAM
609 * by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
610 */
611 if (gd->bd->bi_dram[1].start) {
612 mc_ram_addr =
613 gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
614 } else {
615 mc_ram_addr =
616 gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
617 }
618
619 return mc_ram_addr;
620}
621
622/**
J. German Rivera8ff14b72014-06-23 15:15:55 -0700623 * Return the actual size of the MC private DRAM block.
J. German Rivera8ff14b72014-06-23 15:15:55 -0700624 */
625unsigned long mc_get_dram_block_size(void)
626{
J. German Riveraf4fed4b2015-03-20 19:28:18 -0700627 unsigned long dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
628
629 char *dram_block_size_env_var = getenv(MC_MEM_SIZE_ENV_VAR);
630
631 if (dram_block_size_env_var) {
632 dram_block_size = simple_strtoul(dram_block_size_env_var, NULL,
633 10);
634
635 if (dram_block_size < CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE) {
636 printf("fsl-mc: WARNING: Invalid value for \'"
637 MC_MEM_SIZE_ENV_VAR
638 "\' environment variable: %lu\n",
639 dram_block_size);
640
641 dram_block_size = CONFIG_SYS_LS_MC_DRAM_BLOCK_MIN_SIZE;
642 }
643 }
644
645 return dram_block_size;
J. German Rivera8ff14b72014-06-23 15:15:55 -0700646}
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700647
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530648int fsl_mc_ldpaa_init(bd_t *bis)
649{
Prabhakar Kushwaha52d2e2c2015-11-04 12:26:00 +0530650 int i;
651
652 for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++)
653 if ((wriop_is_enabled_dpmac(i) == 1) &&
654 (wriop_get_phy_address(i) != -1))
655 ldpaa_eth_init(i, wriop_get_enet_if(i));
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530656 return 0;
657}
658
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530659static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
660{
661 struct dprc_attributes attr;
662 int error;
663
664 memset(&attr, 0, sizeof(struct dprc_attributes));
665 error = dprc_get_attributes(mc_io, MC_CMD_NO_FLAGS, handle, &attr);
666 if (error == 0) {
667 if ((attr.version.major != DPRC_VER_MAJOR) ||
668 (attr.version.minor != DPRC_VER_MINOR)) {
669 printf("DPRC version mismatch found %u.%u,",
670 attr.version.major,
671 attr.version.minor);
672 printf("supported version is %u.%u\n",
673 DPRC_VER_MAJOR, DPRC_VER_MINOR);
674 }
675 }
676 return error;
677}
678
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530679static int dpio_init(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700680{
681 struct qbman_swp_desc p_des;
682 struct dpio_attr attr;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530683 struct dpio_cfg dpio_cfg;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700684 int err = 0;
685
686 dflt_dpio = (struct fsl_dpio_obj *)malloc(sizeof(struct fsl_dpio_obj));
687 if (!dflt_dpio) {
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530688 printf("No memory: malloc() failed\n");
689 err = -ENOMEM;
690 goto err_malloc;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700691 }
692
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530693 dpio_cfg.channel_mode = DPIO_LOCAL_CHANNEL;
694 dpio_cfg.num_priorities = 8;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700695
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530696 err = dpio_create(dflt_mc_io, MC_CMD_NO_FLAGS, &dpio_cfg,
697 &dflt_dpio->dpio_handle);
698 if (err < 0) {
699 printf("dpio_create() failed: %d\n", err);
700 err = -ENODEV;
701 goto err_create;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700702 }
703
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530704 memset(&attr, 0, sizeof(struct dpio_attr));
Prabhakar Kushwaha9564df62015-07-07 15:40:06 +0530705 err = dpio_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530706 dflt_dpio->dpio_handle, &attr);
707 if (err < 0) {
708 printf("dpio_get_attributes() failed: %d\n", err);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700709 goto err_get_attr;
710 }
711
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530712 if ((attr.version.major != DPIO_VER_MAJOR) ||
713 (attr.version.minor != DPIO_VER_MINOR)) {
714 printf("DPIO version mismatch found %u.%u,",
715 attr.version.major, attr.version.minor);
716 printf("supported version is %u.%u\n",
717 DPIO_VER_MAJOR, DPIO_VER_MINOR);
718 }
719
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530720 dflt_dpio->dpio_id = attr.id;
721#ifdef DEBUG
722 printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
723#endif
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530724 err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
725 if (err < 0) {
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700726 printf("dpio_enable() failed %d\n", err);
727 goto err_get_enable;
728 }
Prabhakar Kushwahafd5d1272015-07-02 11:28:59 +0530729 debug("ce_offset=0x%llx, ci_offset=0x%llx, portalid=%d, prios=%d\n",
730 attr.qbman_portal_ce_offset,
731 attr.qbman_portal_ci_offset,
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700732 attr.qbman_portal_id,
733 attr.num_priorities);
734
Prabhakar Kushwahafd5d1272015-07-02 11:28:59 +0530735 p_des.cena_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
736 + attr.qbman_portal_ce_offset);
737 p_des.cinh_bar = (void *)(SOC_QBMAN_PORTALS_BASE_ADDR
738 + attr.qbman_portal_ci_offset);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700739
740 dflt_dpio->sw_portal = qbman_swp_init(&p_des);
741 if (dflt_dpio->sw_portal == NULL) {
742 printf("qbman_swp_init() failed\n");
743 goto err_get_swp_init;
744 }
745 return 0;
746
747err_get_swp_init:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530748 dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700749err_get_enable:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530750err_get_attr:
751 dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
752 dpio_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
753err_create:
Prabhakar Kushwahac6018bd2016-03-18 16:15:29 +0530754 free(dflt_dpio);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530755err_malloc:
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700756 return err;
757}
758
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530759static int dpio_exit(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700760{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530761 int err;
762
763 err = dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
764 if (err < 0) {
765 printf("dpio_disable() failed: %d\n", err);
766 goto err;
767 }
768
769 err = dpio_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
770 if (err < 0) {
771 printf("dpio_destroy() failed: %d\n", err);
772 goto err;
773 }
774
775#ifdef DEBUG
776 printf("Exit: DPIO id=0x%d\n", dflt_dpio->dpio_id);
777#endif
778
779 if (dflt_dpio)
780 free(dflt_dpio);
781
782 return 0;
783err:
784 return err;
785}
786
787static int dprc_init(void)
788{
789 int err, child_portal_id, container_id;
790 struct dprc_cfg cfg;
791 uint64_t mc_portal_offset;
792
793 /* Open root container */
794 err = dprc_get_container_id(root_mc_io, MC_CMD_NO_FLAGS, &container_id);
795 if (err < 0) {
796 printf("dprc_get_container_id(): Root failed: %d\n", err);
797 goto err_root_container_id;
798 }
799
800#ifdef DEBUG
801 printf("Root container id = %d\n", container_id);
802#endif
803 err = dprc_open(root_mc_io, MC_CMD_NO_FLAGS, container_id,
804 &root_dprc_handle);
805 if (err < 0) {
806 printf("dprc_open(): Root Container failed: %d\n", err);
807 goto err_root_open;
808 }
809
810 if (!root_dprc_handle) {
811 printf("dprc_open(): Root Container Handle is not valid\n");
812 goto err_root_open;
813 }
814
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530815 err = dprc_version_check(root_mc_io, root_dprc_handle);
816 if (err < 0) {
817 printf("dprc_version_check() failed: %d\n", err);
818 goto err_root_open;
819 }
820
Prabhakar Kushwaha901a88e2016-01-20 12:04:19 +0530821 memset(&cfg, 0, sizeof(struct dprc_cfg));
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530822 cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
823 DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
824 DPRC_CFG_OPT_ALLOC_ALLOWED;
825 cfg.icid = DPRC_GET_ICID_FROM_POOL;
Prabhakar Kushwaha7fd59d92015-12-24 15:33:49 +0530826 cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530827 err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
828 root_dprc_handle,
829 &cfg,
830 &child_dprc_id,
831 &mc_portal_offset);
832 if (err < 0) {
833 printf("dprc_create_container() failed: %d\n", err);
834 goto err_create;
835 }
836
837 dflt_mc_io = (struct fsl_mc_io *)malloc(sizeof(struct fsl_mc_io));
838 if (!dflt_mc_io) {
839 err = -ENOMEM;
840 printf(" No memory: malloc() failed\n");
841 goto err_malloc;
842 }
843
844 child_portal_id = MC_PORTAL_OFFSET_TO_PORTAL_ID(mc_portal_offset);
845 dflt_mc_io->mmio_regs = SOC_MC_PORTAL_ADDR(child_portal_id);
846#ifdef DEBUG
847 printf("MC portal of child DPRC container: %d, physical addr %p)\n",
848 child_dprc_id, dflt_mc_io->mmio_regs);
849#endif
850
851 err = dprc_open(dflt_mc_io, MC_CMD_NO_FLAGS, child_dprc_id,
852 &dflt_dprc_handle);
853 if (err < 0) {
854 printf("dprc_open(): Child container failed: %d\n", err);
855 goto err_child_open;
856 }
857
858 if (!dflt_dprc_handle) {
859 printf("dprc_open(): Child container Handle is not valid\n");
860 goto err_child_open;
861 }
862
863 return 0;
864err_child_open:
865 free(dflt_mc_io);
866err_malloc:
867 dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
868 root_dprc_handle, child_dprc_id);
869err_create:
870 dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
871err_root_open:
872err_root_container_id:
873 return err;
874}
875
876static int dprc_exit(void)
877{
878 int err;
879
880 err = dprc_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dprc_handle);
881 if (err < 0) {
882 printf("dprc_close(): Child failed: %d\n", err);
883 goto err;
884 }
885
886 err = dprc_destroy_container(root_mc_io, MC_CMD_NO_FLAGS,
887 root_dprc_handle, child_dprc_id);
888 if (err < 0) {
889 printf("dprc_destroy_container() failed: %d\n", err);
890 goto err;
891 }
892
893 err = dprc_close(root_mc_io, MC_CMD_NO_FLAGS, root_dprc_handle);
894 if (err < 0) {
895 printf("dprc_close(): Root failed: %d\n", err);
896 goto err;
897 }
898
899 if (dflt_mc_io)
900 free(dflt_mc_io);
901
902 if (root_mc_io)
903 free(root_mc_io);
904
905 return 0;
906
907err:
908 return err;
909}
910
911static int dpbp_init(void)
912{
913 int err;
914 struct dpbp_attr dpbp_attr;
915 struct dpbp_cfg dpbp_cfg;
916
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700917 dflt_dpbp = (struct fsl_dpbp_obj *)malloc(sizeof(struct fsl_dpbp_obj));
918 if (!dflt_dpbp) {
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530919 printf("No memory: malloc() failed\n");
920 err = -ENOMEM;
921 goto err_malloc;
922 }
923
924 dpbp_cfg.options = 512;
925
926 err = dpbp_create(dflt_mc_io, MC_CMD_NO_FLAGS, &dpbp_cfg,
927 &dflt_dpbp->dpbp_handle);
928
929 if (err < 0) {
930 err = -ENODEV;
931 printf("dpbp_create() failed: %d\n", err);
932 goto err_create;
933 }
934
935 memset(&dpbp_attr, 0, sizeof(struct dpbp_attr));
936 err = dpbp_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
937 dflt_dpbp->dpbp_handle,
938 &dpbp_attr);
939 if (err < 0) {
940 printf("dpbp_get_attributes() failed: %d\n", err);
941 goto err_get_attr;
942 }
943
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +0530944 if ((dpbp_attr.version.major != DPBP_VER_MAJOR) ||
945 (dpbp_attr.version.minor != DPBP_VER_MINOR)) {
946 printf("DPBP version mismatch found %u.%u,",
947 dpbp_attr.version.major, dpbp_attr.version.minor);
948 printf("supported version is %u.%u\n",
949 DPBP_VER_MAJOR, DPBP_VER_MINOR);
950 }
951
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530952 dflt_dpbp->dpbp_attr.id = dpbp_attr.id;
953#ifdef DEBUG
954 printf("Init: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
955#endif
956
957 err = dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
958 if (err < 0) {
959 printf("dpbp_close() failed: %d\n", err);
960 goto err_close;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700961 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700962
963 return 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530964
965err_close:
966 free(dflt_dpbp);
967err_get_attr:
968 dpbp_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
969 dpbp_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_handle);
970err_create:
971err_malloc:
972 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700973}
974
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530975static int dpbp_exit(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700976{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530977 int err;
978
979 err = dpbp_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpbp->dpbp_attr.id,
980 &dflt_dpbp->dpbp_handle);
981 if (err < 0) {
982 printf("dpbp_open() failed: %d\n", err);
983 goto err;
984 }
985
986 err = dpbp_destroy(dflt_mc_io, MC_CMD_NO_FLAGS,
987 dflt_dpbp->dpbp_handle);
988 if (err < 0) {
989 printf("dpbp_destroy() failed: %d\n", err);
990 goto err;
991 }
992
993#ifdef DEBUG
994 printf("Exit: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
995#endif
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -0700996
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +0530997 if (dflt_dpbp)
998 free(dflt_dpbp);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +0530999 return 0;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301000
1001err:
1002 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001003}
1004
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301005static int dpni_init(void)
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001006{
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301007 int err;
1008 struct dpni_attr dpni_attr;
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301009 uint8_t ext_cfg_buf[256] = {0};
1010 struct dpni_extended_cfg dpni_extended_cfg;
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301011 struct dpni_cfg dpni_cfg;
1012
1013 dflt_dpni = (struct fsl_dpni_obj *)malloc(sizeof(struct fsl_dpni_obj));
1014 if (!dflt_dpni) {
1015 printf("No memory: malloc() failed\n");
1016 err = -ENOMEM;
1017 goto err_malloc;
1018 }
1019
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301020 memset(&dpni_extended_cfg, 0, sizeof(dpni_extended_cfg));
1021 err = dpni_prepare_extended_cfg(&dpni_extended_cfg, &ext_cfg_buf[0]);
1022 if (err < 0) {
1023 err = -ENODEV;
1024 printf("dpni_prepare_extended_cfg() failed: %d\n", err);
1025 goto err_prepare_extended_cfg;
1026 }
1027
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301028 memset(&dpni_cfg, 0, sizeof(dpni_cfg));
1029 dpni_cfg.adv.options = DPNI_OPT_UNICAST_FILTER |
1030 DPNI_OPT_MULTICAST_FILTER;
1031
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301032 dpni_cfg.adv.ext_cfg_iova = (uint64_t)&ext_cfg_buf[0];
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301033 err = dpni_create(dflt_mc_io, MC_CMD_NO_FLAGS, &dpni_cfg,
1034 &dflt_dpni->dpni_handle);
1035
1036 if (err < 0) {
1037 err = -ENODEV;
1038 printf("dpni_create() failed: %d\n", err);
1039 goto err_create;
1040 }
1041
1042 memset(&dpni_attr, 0, sizeof(struct dpni_attr));
1043 err = dpni_get_attributes(dflt_mc_io, MC_CMD_NO_FLAGS,
1044 dflt_dpni->dpni_handle,
1045 &dpni_attr);
1046 if (err < 0) {
1047 printf("dpni_get_attributes() failed: %d\n", err);
1048 goto err_get_attr;
1049 }
1050
Prabhakar Kushwaha0a4156c2015-12-24 15:32:37 +05301051 if ((dpni_attr.version.major != DPNI_VER_MAJOR) ||
1052 (dpni_attr.version.minor != DPNI_VER_MINOR)) {
1053 printf("DPNI version mismatch found %u.%u,",
1054 dpni_attr.version.major, dpni_attr.version.minor);
1055 printf("supported version is %u.%u\n",
1056 DPNI_VER_MAJOR, DPNI_VER_MINOR);
1057 }
1058
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301059 dflt_dpni->dpni_id = dpni_attr.id;
1060#ifdef DEBUG
1061 printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1062#endif
1063
1064 err = dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1065 if (err < 0) {
1066 printf("dpni_close() failed: %d\n", err);
1067 goto err_close;
1068 }
1069
1070 return 0;
1071
1072err_close:
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301073err_get_attr:
1074 dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1075 dpni_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
1076err_create:
Prabhakar Kushwahae26e2852015-12-24 15:33:01 +05301077err_prepare_extended_cfg:
1078 free(dflt_dpni);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301079err_malloc:
1080 return err;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001081}
1082
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301083static int dpni_exit(void)
1084{
1085 int err;
1086
1087 err = dpni_open(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_id,
1088 &dflt_dpni->dpni_handle);
1089 if (err < 0) {
1090 printf("dpni_open() failed: %d\n", err);
1091 goto err;
1092 }
1093
1094 err = dpni_destroy(dflt_mc_io, MC_CMD_NO_FLAGS,
1095 dflt_dpni->dpni_handle);
1096 if (err < 0) {
1097 printf("dpni_destroy() failed: %d\n", err);
1098 goto err;
1099 }
1100
1101#ifdef DEBUG
1102 printf("Exit: DPNI id=0x%d\n", dflt_dpni->dpni_id);
1103#endif
1104
1105 if (dflt_dpni)
1106 free(dflt_dpni);
1107 return 0;
1108
1109err:
1110 return err;
1111}
1112
1113static int mc_init_object(void)
1114{
1115 int err = 0;
1116
1117 err = dprc_init();
1118 if (err < 0) {
1119 printf("dprc_init() failed: %d\n", err);
1120 goto err;
1121 }
1122
1123 err = dpbp_init();
1124 if (err < 0) {
1125 printf("dpbp_init() failed: %d\n", err);
1126 goto err;
1127 }
1128
1129 err = dpio_init();
1130 if (err < 0) {
1131 printf("dpio_init() failed: %d\n", err);
1132 goto err;
1133 }
1134
1135 err = dpni_init();
1136 if (err < 0) {
1137 printf("dpni_init() failed: %d\n", err);
1138 goto err;
1139 }
1140
1141 return 0;
1142err:
1143 return err;
1144}
1145
1146int fsl_mc_ldpaa_exit(bd_t *bd)
1147{
1148 int err = 0;
1149
Prabhakar Kushwaha878d3ec2016-03-21 14:19:39 +05301150 /* MC is not loaded intentionally, So return success. */
1151 if (bd && get_mc_boot_status() != 0)
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301152 return 0;
1153
1154 if (bd && !get_mc_boot_status() && get_dpl_apply_status() == -1) {
1155 printf("ERROR: fsl-mc: DPL is not applied\n");
1156 err = -ENODEV;
1157 return err;
1158 }
1159
1160 if (bd && !get_mc_boot_status() && !get_dpl_apply_status())
1161 return err;
1162
1163 err = dpbp_exit();
1164 if (err < 0) {
Prabhakar Kushwaha88ba4d62016-01-20 12:04:37 +05301165 printf("dpbp_exit() failed: %d\n", err);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301166 goto err;
1167 }
1168
1169 err = dpio_exit();
1170 if (err < 0) {
1171 printf("dpio_exit() failed: %d\n", err);
1172 goto err;
1173 }
1174
1175 err = dpni_exit();
1176 if (err < 0) {
1177 printf("dpni_exit() failed: %d\n", err);
1178 goto err;
1179 }
1180
1181 err = dprc_exit();
1182 if (err < 0) {
1183 printf("dprc_exit() failed: %d\n", err);
1184 goto err;
1185 }
1186
1187 return 0;
1188err:
1189 return err;
1190}
1191
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301192static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001193{
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301194 int err = 0;
1195 if (argc < 3)
1196 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001197
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301198 switch (argv[1][0]) {
1199 case 's': {
1200 char sub_cmd;
Prabhakar Kushwaha122bcfd2015-11-09 16:42:07 +05301201 u64 mc_fw_addr, mc_dpc_addr;
1202#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1203 u64 aiop_fw_addr;
1204#endif
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001205
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301206 sub_cmd = argv[2][0];
1207 switch (sub_cmd) {
1208 case 'm':
1209 if (argc < 5)
1210 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001211
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301212 if (get_mc_boot_status() == 0) {
1213 printf("fsl-mc: MC is already booted");
1214 printf("\n");
1215 return err;
1216 }
1217 mc_fw_addr = simple_strtoull(argv[3], NULL, 16);
1218 mc_dpc_addr = simple_strtoull(argv[4], NULL,
1219 16);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301220
1221 if (!mc_init(mc_fw_addr, mc_dpc_addr))
1222 err = mc_init_object();
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301223 break;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001224
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301225#ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
1226 case 'a':
1227 if (argc < 4)
1228 goto usage;
1229 if (get_aiop_apply_status() == 0) {
1230 printf("fsl-mc: AIOP FW is already");
1231 printf(" applied\n");
1232 return err;
1233 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001234
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301235 aiop_fw_addr = simple_strtoull(argv[3], NULL,
1236 16);
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001237
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301238 err = load_mc_aiop_img(aiop_fw_addr);
1239 if (!err)
1240 printf("fsl-mc: AIOP FW applied\n");
1241 break;
1242#endif
1243 default:
1244 printf("Invalid option: %s\n", argv[2]);
1245 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001246
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301247 break;
1248 }
1249 }
1250 break;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001251
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301252 case 'a': {
1253 u64 mc_dpl_addr;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001254
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301255 if (argc < 4)
1256 goto usage;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001257
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301258 if (get_dpl_apply_status() == 0) {
1259 printf("fsl-mc: DPL already applied\n");
1260 return err;
1261 }
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001262
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301263 mc_dpl_addr = simple_strtoull(argv[3], NULL,
1264 16);
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301265
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301266 if (get_mc_boot_status() != 0) {
1267 printf("fsl-mc: Deploying data path layout ..");
1268 printf("ERROR (MC is not booted)\n");
1269 return -ENODEV;
1270 }
Prabhakar Kushwaha316b71c2015-11-04 12:25:59 +05301271
1272 if (!fsl_mc_ldpaa_exit(NULL))
1273 err = mc_apply_dpl(mc_dpl_addr);
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301274 break;
J. German Riveraf4fed4b2015-03-20 19:28:18 -07001275 }
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301276 default:
1277 printf("Invalid option: %s\n", argv[1]);
1278 goto usage;
1279 break;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001280 }
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301281 return err;
1282 usage:
1283 return CMD_RET_USAGE;
Prabhakar Kushwahacfd9fbf2015-03-19 09:20:45 -07001284}
Prabhakar Kushwaha29a63e42015-11-04 12:25:58 +05301285
1286U_BOOT_CMD(
1287 fsl_mc, CONFIG_SYS_MAXARGS, 1, do_fsl_mc,
1288 "DPAA2 command to manage Management Complex (MC)",
1289 "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
1290 "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
1291 "fsl_mc start aiop [FW_addr] - Start AIOP\n"
1292);