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