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