Merge tag 'u-boot-rockchip-20200501' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / drivers / net / fsl-mc / mc.c
index c980ba4..fee3729 100644 (file)
@@ -1,12 +1,14 @@
 // SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright 2014 Freescale Semiconductor, Inc.
- * Copyright 2017 NXP
  * Copyright 2017-2018 NXP
  */
 #include <common.h>
+#include <command.h>
+#include <cpu_func.h>
 #include <env.h>
 #include <errno.h>
+#include <malloc.h>
 #include <linux/bug.h>
 #include <asm/io.h>
 #include <linux/libfdt.h>
@@ -19,6 +21,7 @@
 #include <fsl-mc/fsl_dprc.h>
 #include <fsl-mc/fsl_dpio.h>
 #include <fsl-mc/fsl_dpni.h>
+#include <fsl-mc/fsl_dpsparser.h>
 #include <fsl-mc/fsl_qbman_portal.h>
 #include <fsl-mc/ldpaa_wriop.h>
 
@@ -33,6 +36,7 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 static int mc_memset_resv_ram;
+static struct mc_version mc_ver_info;
 static int mc_boot_status = -1;
 static int mc_dpl_applied = -1;
 #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
@@ -47,6 +51,9 @@ struct fsl_dpbp_obj *dflt_dpbp = NULL;
 struct fsl_dpio_obj *dflt_dpio = NULL;
 struct fsl_dpni_obj *dflt_dpni = NULL;
 static u64 mc_lazy_dpl_addr;
+static u32 dpsparser_obj_id;
+static u16 dpsparser_handle;
+static char *mc_err_msg_apply_spb[] = MC_ERROR_MSG_APPLY_SPB;
 
 #ifdef DEBUG
 void dump_ram_words(const char *title, void *addr)
@@ -90,7 +97,6 @@ void dump_mc_ccsr_regs(struct mc_ccsr_registers __iomem *mc_ccsr_regs)
 
 #endif /* DEBUG */
 
-#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
 /**
  * Copying MC firmware or DPL image to DDR
  */
@@ -103,6 +109,7 @@ static int mc_copy_image(const char *title,
        return 0;
 }
 
+#ifndef CONFIG_SYS_LS_MC_FW_IN_DDR
 /**
  * MC firmware FIT image parser checks if the image is in FIT
  * format, verifies integrity of the image and calculates
@@ -167,9 +174,21 @@ enum mc_fixup_type {
 };
 
 static int mc_fixup_mac_addr(void *blob, int nodeoffset,
+#ifdef CONFIG_DM_ETH
+                            const char *propname, struct udevice *eth_dev,
+#else
                             const char *propname, struct eth_device *eth_dev,
+#endif
                             enum mc_fixup_type type)
 {
+#ifdef CONFIG_DM_ETH
+       struct eth_pdata *plat = dev_get_platdata(eth_dev);
+       unsigned char *enetaddr = plat->enetaddr;
+       int eth_index = eth_dev->seq;
+#else
+       unsigned char *enetaddr = eth_dev->enetaddr;
+       int eth_index = eth_dev->index;
+#endif
        int err = 0, len = 0, size, i;
        unsigned char env_enetaddr[ARP_HLEN];
        unsigned int enetaddr_32[ARP_HLEN];
@@ -177,23 +196,22 @@ static int mc_fixup_mac_addr(void *blob, int nodeoffset,
 
        switch (type) {
        case MC_FIXUP_DPL:
-       /* DPL likes its addresses on 32 * ARP_HLEN bits */
-       for (i = 0; i < ARP_HLEN; i++)
-               enetaddr_32[i] = cpu_to_fdt32(eth_dev->enetaddr[i]);
-       val = enetaddr_32;
-       len = sizeof(enetaddr_32);
-       break;
-
+               /* DPL likes its addresses on 32 * ARP_HLEN bits */
+               for (i = 0; i < ARP_HLEN; i++)
+                       enetaddr_32[i] = cpu_to_fdt32(enetaddr[i]);
+               val = enetaddr_32;
+               len = sizeof(enetaddr_32);
+               break;
        case MC_FIXUP_DPC:
-       val = eth_dev->enetaddr;
-       len = ARP_HLEN;
-       break;
+               val = enetaddr;
+               len = ARP_HLEN;
+               break;
        }
 
        /* MAC address property present */
        if (fdt_get_property(blob, nodeoffset, propname, NULL)) {
                /* u-boot MAC addr randomly assigned - leave the present one */
-               if (!eth_env_get_enetaddr_by_index("eth", eth_dev->index,
+               if (!eth_env_get_enetaddr_by_index("eth", eth_index,
                                                   env_enetaddr))
                        return err;
        } else {
@@ -243,7 +261,11 @@ const char *dpl_get_connection_endpoint(void *blob, char *endpoint)
 }
 
 static int mc_fixup_dpl_mac_addr(void *blob, int dpmac_id,
+#ifdef CONFIG_DM_ETH
+                                struct udevice *eth_dev)
+#else
                                 struct eth_device *eth_dev)
+#endif
 {
        int objoff = fdt_path_offset(blob, "/objects");
        int dpmacoff = -1, dpnioff = -1;
@@ -327,7 +349,11 @@ void fdt_fsl_mc_fixup_iommu_map_entry(void *blob)
 }
 
 static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
+#ifdef CONFIG_DM_ETH
+                                struct udevice *eth_dev)
+#else
                                 struct eth_device *eth_dev)
+#endif
 {
        int nodeoffset = fdt_path_offset(blob, "/board_info/ports"), noff;
        int err = 0;
@@ -370,8 +396,13 @@ static int mc_fixup_dpc_mac_addr(void *blob, int dpmac_id,
 static int mc_fixup_mac_addrs(void *blob, enum mc_fixup_type type)
 {
        int i, err = 0, ret = 0;
-       char ethname[ETH_NAME_LEN];
+#ifdef CONFIG_DM_ETH
+#define ETH_NAME_LEN 20
+       struct udevice *eth_dev;
+#else
        struct eth_device *eth_dev;
+#endif
+       char ethname[ETH_NAME_LEN];
 
        for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
                /* port not enabled */
@@ -689,7 +720,6 @@ int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
        const void *raw_image_addr;
        size_t raw_image_size = 0;
 #endif
-       struct mc_version mc_ver_info;
        u8 mc_ram_num_256mb_blocks;
        size_t mc_ram_size = mc_get_dram_block_size();
 
@@ -1445,6 +1475,170 @@ err:
        return err;
 }
 
+static bool is_dpsparser_supported(void)
+{
+       /* dpsparser support was first introduced in MC version: 10.12.0 */
+       if (mc_ver_info.major < 10)
+               return false;
+       if (mc_ver_info.major == 10)
+               return (mc_ver_info.minor >= 12);
+       return true;
+}
+
+static int dpsparser_version_check(struct fsl_mc_io *mc_io)
+{
+       int error;
+       u16 major_ver, minor_ver;
+
+       if (!is_dpsparser_supported())
+               return 0;
+
+       error = dpsparser_get_api_version(mc_io, 0,
+                                         &major_ver,
+                                         &minor_ver);
+       if (error < 0) {
+               printf("dpsparser_get_api_version() failed: %d\n", error);
+               return error;
+       }
+
+       if (major_ver < DPSPARSER_VER_MAJOR || (major_ver ==
+           DPSPARSER_VER_MAJOR && minor_ver < DPSPARSER_VER_MINOR)) {
+               printf("DPSPARSER version mismatch found %u.%u,",
+                      major_ver, minor_ver);
+               printf("supported version is %u.%u\n",
+                      DPSPARSER_VER_MAJOR, DPSPARSER_VER_MINOR);
+       }
+
+       return error;
+}
+
+static int dpsparser_init(void)
+{
+       int err = 0;
+
+       if (!is_dpsparser_supported())
+               return 0;
+
+       err = dpsparser_create(dflt_mc_io,
+                              dflt_dprc_handle,
+                              MC_CMD_NO_FLAGS,
+                              &dpsparser_obj_id);
+       if (err)
+               printf("dpsparser_create() failed\n");
+
+       err = dpsparser_version_check(dflt_mc_io);
+       if (err < 0) {
+               printf("dpsparser_version_check() failed: %d\n", err);
+               goto err_version_check;
+       }
+
+       err = dpsparser_open(dflt_mc_io,
+                            MC_CMD_NO_FLAGS,
+                            &dpsparser_handle);
+       if (err < 0) {
+               printf("dpsparser_open() failed: %d\n", err);
+               goto err_open;
+       }
+
+       return err;
+
+err_open:
+err_version_check:
+       dpsparser_destroy(dflt_mc_io,
+                         dflt_dprc_handle,
+                         MC_CMD_NO_FLAGS, dpsparser_obj_id);
+
+       return err;
+}
+
+#ifdef DPSPARSER_DESTROY
+/* TODO: refactoring needed in the future to allow DPSPARSER object destroy
+ * Workaround: DO NOT destroy DPSPARSER object because it needs to be available
+ * on Apply DPL
+ */
+static int dpsparser_exit(void)
+{
+       int err;
+
+       if (!is_dpsparser_supported())
+               return 0;
+
+       dpsparser_close(dflt_mc_io, MC_CMD_NO_FLAGS, dpsparser_handle);
+       if (err < 0) {
+               printf("dpsparser_close() failed: %d\n", err);
+               goto err;
+       }
+
+       err = dpsparser_destroy(dflt_mc_io, dflt_dprc_handle,
+                               MC_CMD_NO_FLAGS, dpsparser_obj_id);
+       if (err < 0) {
+               printf("dpsparser_destroy() failed: %d\n", err);
+               goto err;
+       }
+       return 0;
+
+err:
+       return err;
+}
+#endif
+
+int mc_apply_spb(u64 mc_spb_addr)
+{
+       int err = 0;
+       u16 error, err_arr_size;
+       u64 mc_spb_offset;
+       u32 spb_size;
+       struct sp_blob_header *sp_blob;
+       u64 mc_ram_addr = mc_get_dram_addr();
+
+       if (!is_dpsparser_supported())
+               return 0;
+
+       if (!mc_spb_addr) {
+               printf("fsl-mc: Invalid Blob address\n");
+               return -1;
+       }
+
+#ifdef CONFIG_MC_DRAM_SPB_OFFSET
+       mc_spb_offset = CONFIG_MC_DRAM_SPB_OFFSET;
+#else
+#error "CONFIG_MC_DRAM_SPB_OFFSET not defined"
+#endif
+
+       // Read blob header and get size of SPB blob
+       sp_blob = (struct sp_blob_header *)mc_spb_addr;
+       spb_size = le32_to_cpu(sp_blob->length);
+       if (spb_size > CONFIG_MC_SPB_MAX_SIZE) {
+               printf("\nfsl-mc: ERROR: Bad SPB image (too large: %d)\n",
+                      spb_size);
+               return -EINVAL;
+       }
+
+       mc_copy_image("MC SP Blob", mc_spb_addr, spb_size,
+                     mc_ram_addr + mc_spb_offset);
+
+       //Invoke MC command to apply SPB blob
+       printf("fsl-mc: Applying soft parser blob... ");
+       err = dpsparser_apply_spb(dflt_mc_io, MC_CMD_NO_FLAGS, dpsparser_handle,
+                                 mc_spb_offset, &error);
+       if (err)
+               return err;
+
+       if (error == 0) {
+               printf("SUCCESS\n");
+       } else {
+               printf("FAILED with error code = %d:\n", error);
+               err_arr_size = (u16)ARRAY_SIZE(mc_err_msg_apply_spb);
+
+               if (error > 0 && error < err_arr_size)
+                       printf(mc_err_msg_apply_spb[error]);
+               else
+                       printf(MC_ERROR_MSG_SPB_UNKNOWN);
+       }
+
+       return err;
+}
+
 static int mc_init_object(void)
 {
        int err = 0;
@@ -1473,6 +1667,12 @@ static int mc_init_object(void)
                goto err;
        }
 
+       err = dpsparser_init();
+       if (err < 0) {
+               printf("dpsparser_init() failed: %d\n", err);
+               goto err;
+       }
+
        return 0;
 err:
        return err;
@@ -1606,39 +1806,87 @@ static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                }
                break;
 
-       case 'l':
+       case 'l': {
+               /* lazyapply */
+               u64 mc_dpl_addr;
+
+               if (argc < 4)
+                       goto usage;
+
+               if (get_dpl_apply_status() == 0) {
+                       printf("fsl-mc: DPL already applied\n");
+                       return err;
+               }
+
+               mc_dpl_addr = simple_strtoull(argv[3], NULL, 16);
+
+               if (get_mc_boot_status() != 0) {
+                       printf("fsl-mc: Deploying data path layout ..");
+                       printf("ERROR (MC is not booted)\n");
+                       return -ENODEV;
+               }
+
+               /*
+                * We will do the actual dpaa exit and dpl apply
+                * later from announce_and_cleanup().
+                */
+               mc_lazy_dpl_addr = mc_dpl_addr;
+               break;
+               }
+
        case 'a': {
-                       u64 mc_dpl_addr;
+               /* apply */
+               char sub_cmd;
+               u64 mc_apply_addr;
 
-                       if (argc < 4)
-                               goto usage;
+               if (argc < 4)
+                       goto usage;
+
+               sub_cmd = argv[2][0];
 
+               switch (sub_cmd) {
+               case 'd':
+               case 'D':
                        if (get_dpl_apply_status() == 0) {
                                printf("fsl-mc: DPL already applied\n");
                                return err;
                        }
-
-                       mc_dpl_addr = simple_strtoull(argv[3], NULL,
-                                                             16);
-
                        if (get_mc_boot_status() != 0) {
                                printf("fsl-mc: Deploying data path layout ..");
                                printf("ERROR (MC is not booted)\n");
                                return -ENODEV;
                        }
 
-                       if (argv[1][0] == 'l') {
-                               /*
-                                * We will do the actual dpaa exit and dpl apply
-                                * later from announce_and_cleanup().
-                                */
-                               mc_lazy_dpl_addr = mc_dpl_addr;
-                       } else {
-                               /* The user wants it applied now */
-                               if (!fsl_mc_ldpaa_exit(NULL))
-                                       err = mc_apply_dpl(mc_dpl_addr);
+                       mc_apply_addr = simple_strtoull(argv[3], NULL, 16);
+
+                       /* The user wants DPL applied now */
+                       if (!fsl_mc_ldpaa_exit(NULL))
+                               err = mc_apply_dpl(mc_apply_addr);
+                       break;
+
+               case 's':
+                       if (!is_dpsparser_supported()) {
+                               printf("fsl-mc: apply spb command .. ");
+                               printf("ERROR: requires at least MC 10.12.0\n");
+                               return err;
                        }
+                       if (get_mc_boot_status() != 0) {
+                               printf("fsl-mc: Deploying Soft Parser Blob...");
+                               printf("ERROR (MC is not booted)\n");
+                               return err;
+                       }
+
+                       mc_apply_addr = simple_strtoull(argv[3], NULL, 16);
+
+                       /* Apply spb (Soft Parser Blob) */
+                       err = mc_apply_spb(mc_apply_addr);
                        break;
+
+               default:
+                       printf("Invalid option: %s\n", argv[2]);
+                       goto usage;
+               }
+               break;
                }
        default:
                printf("Invalid option: %s\n", argv[1]);
@@ -1656,6 +1904,7 @@ U_BOOT_CMD(
        "start mc [FW_addr] [DPC_addr] - Start Management Complex\n"
        "fsl_mc apply DPL [DPL_addr] - Apply DPL file\n"
        "fsl_mc lazyapply DPL [DPL_addr] - Apply DPL file on exit\n"
+       "fsl_mc apply spb [spb_addr] - Apply SPB Soft Parser Blob\n"
        "fsl_mc start aiop [FW_addr] - Start AIOP\n"
 );