]> git.sur5r.net Git - u-boot/blobdiff - drivers/net/fsl-mc/mc.c
rockchip: mkimage: remove (left-over) assignment w/o effect [coverity]
[u-boot] / drivers / net / fsl-mc / mc.c
index 4b9b3720f7440092b2c8de8e0ecb359f023ff2ad..9f69d75a89d0dbb40b5fa16deb92793909d0c73f 100644 (file)
@@ -8,6 +8,7 @@
 #include <linux/bug.h>
 #include <asm/io.h>
 #include <libfdt.h>
+#include <net.h>
 #include <fdt_support.h>
 #include <fsl-mc/fsl_mc.h>
 #include <fsl-mc/fsl_mc_sys.h>
@@ -40,6 +41,7 @@ int child_dprc_id;
 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;
 
 #ifdef DEBUG
 void dump_ram_words(const char *title, void *addr)
@@ -152,52 +154,81 @@ int parse_mc_firmware_fit_image(u64 mc_fw_addr,
 }
 #endif
 
-/*
- * Calculates the values to be used to specify the address range
- * for the MC private DRAM block, in the MCFBALR/MCFBAHR registers.
- * It returns the highest 512MB-aligned address within the given
- * address range, in '*aligned_base_addr', and the number of 256 MiB
- * blocks in it, in 'num_256mb_blocks'.
- */
-static int calculate_mc_private_ram_params(u64 mc_private_ram_start_addr,
-                                          size_t mc_ram_size,
-                                          u64 *aligned_base_addr,
-                                          u8 *num_256mb_blocks)
+static int mc_fixup_dpc_mac_addr(void *blob, int noff, int dpmac_id,
+               struct eth_device *eth_dev)
 {
-       u64 addr;
-       u16 num_blocks;
+       int nodeoffset, err = 0;
+       char mac_name[10];
+       const char link_type_mode[] = "FIXED_LINK";
+       unsigned char env_enetaddr[6];
 
-       if (mc_ram_size % MC_RAM_SIZE_ALIGNMENT != 0) {
-               printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
-                      mc_ram_size);
-               return -EINVAL;
+       sprintf(mac_name, "mac@%d", dpmac_id);
+
+       /* node not found - create it */
+       nodeoffset = fdt_subnode_offset(blob, noff, (const char *) mac_name);
+       if (nodeoffset < 0) {
+               err = fdt_increase_size(blob, 200);
+               if (err) {
+                       printf("fdt_increase_size: err=%s\n",
+                               fdt_strerror(err));
+                       return err;
+               }
+
+               nodeoffset = fdt_add_subnode(blob, noff, mac_name);
+
+               /* add default property of fixed link */
+               err = fdt_appendprop_string(blob, nodeoffset,
+                                           "link_type", link_type_mode);
+               if (err) {
+                       printf("fdt_appendprop_string: err=%s\n",
+                               fdt_strerror(err));
+                       return err;
+               }
        }
 
-       num_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
-       if (num_blocks < 1 || num_blocks > 0xff) {
-               printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
-                      mc_ram_size);
-               return -EINVAL;
+       /* port_mac_address property present in DPC */
+       if (fdt_get_property(blob, nodeoffset, "port_mac_address", NULL)) {
+               /* MAC addr randomly assigned - leave the one in DPC */
+               eth_getenv_enetaddr_by_index("eth", eth_dev->index,
+                                               env_enetaddr);
+               if (is_zero_ethaddr(env_enetaddr))
+                       return err;
+
+               /* replace DPC MAC address with u-boot env one */
+               err = fdt_setprop(blob, nodeoffset, "port_mac_address",
+                                 eth_dev->enetaddr, 6);
+               if (err) {
+                       printf("fdt_setprop mac: err=%s\n", fdt_strerror(err));
+                       return err;
+               }
+
+               return 0;
        }
 
-       addr = (mc_private_ram_start_addr + mc_ram_size - 1) &
-               MC_RAM_BASE_ADDR_ALIGNMENT_MASK;
+       /* append port_mac_address property to mac node in DPC */
+       err = fdt_increase_size(blob, 80);
+       if (err) {
+               printf("fdt_increase_size: err=%s\n", fdt_strerror(err));
+               return err;
+       }
 
-       if (addr < mc_private_ram_start_addr) {
-               printf("fsl-mc: ERROR: bad start address %#llx\n",
-                      mc_private_ram_start_addr);
-               return -EFAULT;
+       err = fdt_appendprop(blob, nodeoffset,
+                            "port_mac_address", eth_dev->enetaddr, 6);
+       if (err) {
+               printf("fdt_appendprop: err=%s\n", fdt_strerror(err));
+               return err;
        }
 
-       *aligned_base_addr = addr;
-       *num_256mb_blocks = num_blocks;
-       return 0;
+       return err;
 }
 
 static int mc_fixup_dpc(u64 dpc_addr)
 {
        void *blob = (void *)dpc_addr;
-       int nodeoffset;
+       int nodeoffset, err = 0;
+       char ethname[10];
+       struct eth_device *eth_dev;
+       int i;
 
        /* delete any existing ICID pools */
        nodeoffset = fdt_path_offset(blob, "/resources/icid_pools");
@@ -219,9 +250,36 @@ static int mc_fixup_dpc(u64 dpc_addr)
                             FSL_DPAA2_STREAM_ID_END -
                             FSL_DPAA2_STREAM_ID_START + 1, 1);
 
+       /* fixup MAC addresses for dpmac ports */
+       nodeoffset = fdt_path_offset(blob, "/board_info/ports");
+       if (nodeoffset < 0)
+               goto out;
+
+       for (i = WRIOP1_DPMAC1; i < NUM_WRIOP_PORTS; i++) {
+               /* port not enabled */
+               if ((wriop_is_enabled_dpmac(i) != 1) ||
+                   (wriop_get_phy_address(i) == -1))
+                       continue;
+
+               sprintf(ethname, "DPMAC%d@%s", i,
+                       phy_interface_strings[wriop_get_enet_if(i)]);
+
+               eth_dev = eth_get_dev_by_name(ethname);
+               if (eth_dev == NULL)
+                       continue;
+
+               err = mc_fixup_dpc_mac_addr(blob, nodeoffset, i, eth_dev);
+               if (err) {
+                       printf("mc_fixup_dpc_mac_addr failed: err=%s\n",
+                       fdt_strerror(err));
+                       goto out;
+               }
+       }
+
+out:
        flush_dcache_range(dpc_addr, dpc_addr + fdt_totalsize(blob));
 
-       return 0;
+       return err;
 }
 
 static int load_mc_dpc(u64 mc_ram_addr, size_t mc_ram_size, u64 mc_dpc_addr)
@@ -356,6 +414,12 @@ static unsigned long get_mc_boot_timeout_ms(void)
 }
 
 #ifdef CONFIG_SYS_LS_MC_DRAM_AIOP_IMG_OFFSET
+
+__weak bool soc_has_aiop(void)
+{
+       return false;
+}
+
 static int load_mc_aiop_img(u64 aiop_fw_addr)
 {
        u64 mc_ram_addr = mc_get_dram_addr();
@@ -363,6 +427,9 @@ static int load_mc_aiop_img(u64 aiop_fw_addr)
        void *aiop_img;
 #endif
 
+       /* Check if AIOP is available */
+       if (!soc_has_aiop())
+               return -ENODEV;
        /*
         * Load the MC AIOP image in the MC private DRAM block:
         */
@@ -441,21 +508,20 @@ int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
        size_t raw_image_size = 0;
 #endif
        struct mc_version mc_ver_info;
-       u64 mc_ram_aligned_base_addr;
        u8 mc_ram_num_256mb_blocks;
        size_t mc_ram_size = mc_get_dram_block_size();
 
-
-       error = calculate_mc_private_ram_params(mc_ram_addr,
-                                               mc_ram_size,
-                                               &mc_ram_aligned_base_addr,
-                                               &mc_ram_num_256mb_blocks);
-       if (error != 0)
+       mc_ram_num_256mb_blocks = mc_ram_size / MC_RAM_SIZE_ALIGNMENT;
+       if (mc_ram_num_256mb_blocks < 1 || mc_ram_num_256mb_blocks > 0xff) {
+               error = -EINVAL;
+               printf("fsl-mc: ERROR: invalid MC private RAM size (%lu)\n",
+                      mc_ram_size);
                goto out;
+       }
 
        /*
         * Management Complex cores should be held at reset out of POR.
-        * U-boot should be the first software to touch MC. To be safe,
+        * U-Boot should be the first software to touch MC. To be safe,
         * we reset all cores again by setting GCR1 to 0. It doesn't do
         * anything if they are held at reset. After we setup the firmware
         * we kick off MC by deasserting the reset bit for core 0, and
@@ -493,11 +559,11 @@ int mc_init(u64 mc_fw_addr, u64 mc_dpc_addr)
        /*
         * Tell MC what is the address range of the DRAM block assigned to it:
         */
-       reg_mcfbalr = (u32)mc_ram_aligned_base_addr |
+       reg_mcfbalr = (u32)mc_ram_addr |
                      (mc_ram_num_256mb_blocks - 1);
        out_le32(&mc_ccsr_regs->reg_mcfbalr, reg_mcfbalr);
        out_le32(&mc_ccsr_regs->reg_mcfbahr,
-                (u32)(mc_ram_aligned_base_addr >> 32));
+                (u32)(mc_ram_addr >> 32));
        out_le32(&mc_ccsr_regs->reg_mcfapr, FSL_BYPASS_AMQ);
 
        /*
@@ -563,6 +629,9 @@ int mc_apply_dpl(u64 mc_dpl_addr)
        u64 mc_ram_addr = mc_get_dram_addr();
        size_t mc_ram_size = mc_get_dram_block_size();
 
+       if (!mc_dpl_addr)
+               return -1;
+
        error = load_mc_dpl(mc_ram_addr, mc_ram_size, mc_dpl_addr);
        if (error != 0)
                return error;
@@ -602,21 +671,7 @@ int get_dpl_apply_status(void)
  */
 u64 mc_get_dram_addr(void)
 {
-       u64 mc_ram_addr;
-
-       /*
-        * The MC private DRAM block was already carved at the end of DRAM
-        * by board_init_f() using CONFIG_SYS_MEM_TOP_HIDE:
-        */
-       if (gd->bd->bi_dram[1].start) {
-               mc_ram_addr =
-                       gd->bd->bi_dram[1].start + gd->bd->bi_dram[1].size;
-       } else {
-               mc_ram_addr =
-                       gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size;
-       }
-
-       return mc_ram_addr;
+       return gd->arch.resv_ram;
 }
 
 /**
@@ -656,6 +711,26 @@ int fsl_mc_ldpaa_init(bd_t *bis)
        return 0;
 }
 
+static int dprc_version_check(struct fsl_mc_io *mc_io, uint16_t handle)
+{
+       struct dprc_attributes attr;
+       int error;
+
+       memset(&attr, 0, sizeof(struct dprc_attributes));
+       error = dprc_get_attributes(mc_io, MC_CMD_NO_FLAGS, handle, &attr);
+       if (error == 0) {
+               if ((attr.version.major != DPRC_VER_MAJOR) ||
+                   (attr.version.minor != DPRC_VER_MINOR)) {
+                       printf("DPRC version mismatch found %u.%u,",
+                              attr.version.major,
+                              attr.version.minor);
+                       printf("supported version is %u.%u\n",
+                              DPRC_VER_MAJOR, DPRC_VER_MINOR);
+               }
+       }
+       return error;
+}
+
 static int dpio_init(void)
 {
        struct qbman_swp_desc p_des;
@@ -689,11 +764,18 @@ static int dpio_init(void)
                goto err_get_attr;
        }
 
+       if ((attr.version.major != DPIO_VER_MAJOR) ||
+           (attr.version.minor != DPIO_VER_MINOR)) {
+               printf("DPIO version mismatch found %u.%u,",
+                      attr.version.major, attr.version.minor);
+               printf("supported version is %u.%u\n",
+                      DPIO_VER_MAJOR, DPIO_VER_MINOR);
+       }
+
        dflt_dpio->dpio_id = attr.id;
 #ifdef DEBUG
        printf("Init: DPIO id=0x%d\n", dflt_dpio->dpio_id);
 #endif
-
        err = dpio_enable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
        if (err < 0) {
                printf("dpio_enable() failed %d\n", err);
@@ -720,11 +802,11 @@ static int dpio_init(void)
 err_get_swp_init:
        dpio_disable(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
 err_get_enable:
-       free(dflt_dpio);
 err_get_attr:
        dpio_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
        dpio_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpio->dpio_handle);
 err_create:
+       free(dflt_dpio);
 err_malloc:
        return err;
 }
@@ -785,11 +867,18 @@ static int dprc_init(void)
                goto err_root_open;
        }
 
+       err = dprc_version_check(root_mc_io, root_dprc_handle);
+       if (err < 0) {
+               printf("dprc_version_check() failed: %d\n", err);
+               goto err_root_open;
+       }
+
+       memset(&cfg, 0, sizeof(struct dprc_cfg));
        cfg.options = DPRC_CFG_OPT_TOPOLOGY_CHANGES_ALLOWED |
                      DPRC_CFG_OPT_OBJ_CREATE_ALLOWED |
                      DPRC_CFG_OPT_ALLOC_ALLOWED;
        cfg.icid = DPRC_GET_ICID_FROM_POOL;
-       cfg.portal_id = 250;
+       cfg.portal_id = DPRC_GET_PORTAL_ID_FROM_POOL;
        err = dprc_create_container(root_mc_io, MC_CMD_NO_FLAGS,
                        root_dprc_handle,
                        &cfg,
@@ -907,6 +996,14 @@ static int dpbp_init(void)
                goto err_get_attr;
        }
 
+       if ((dpbp_attr.version.major != DPBP_VER_MAJOR) ||
+           (dpbp_attr.version.minor != DPBP_VER_MINOR)) {
+               printf("DPBP version mismatch found %u.%u,",
+                      dpbp_attr.version.major, dpbp_attr.version.minor);
+               printf("supported version is %u.%u\n",
+                      DPBP_VER_MAJOR, DPBP_VER_MINOR);
+       }
+
        dflt_dpbp->dpbp_attr.id = dpbp_attr.id;
 #ifdef DEBUG
        printf("Init: DPBP id=0x%d\n", dflt_dpbp->dpbp_attr.id);
@@ -964,6 +1061,8 @@ static int dpni_init(void)
 {
        int err;
        struct dpni_attr dpni_attr;
+       uint8_t ext_cfg_buf[256] = {0};
+       struct dpni_extended_cfg dpni_extended_cfg;
        struct dpni_cfg dpni_cfg;
 
        dflt_dpni = (struct fsl_dpni_obj *)malloc(sizeof(struct fsl_dpni_obj));
@@ -973,10 +1072,19 @@ static int dpni_init(void)
                goto err_malloc;
        }
 
+       memset(&dpni_extended_cfg, 0, sizeof(dpni_extended_cfg));
+       err = dpni_prepare_extended_cfg(&dpni_extended_cfg, &ext_cfg_buf[0]);
+       if (err < 0) {
+               err = -ENODEV;
+               printf("dpni_prepare_extended_cfg() failed: %d\n", err);
+               goto err_prepare_extended_cfg;
+       }
+
        memset(&dpni_cfg, 0, sizeof(dpni_cfg));
        dpni_cfg.adv.options = DPNI_OPT_UNICAST_FILTER |
                               DPNI_OPT_MULTICAST_FILTER;
 
+       dpni_cfg.adv.ext_cfg_iova = (uint64_t)&ext_cfg_buf[0];
        err = dpni_create(dflt_mc_io, MC_CMD_NO_FLAGS, &dpni_cfg,
                          &dflt_dpni->dpni_handle);
 
@@ -995,6 +1103,14 @@ static int dpni_init(void)
                goto err_get_attr;
        }
 
+       if ((dpni_attr.version.major != DPNI_VER_MAJOR) ||
+           (dpni_attr.version.minor != DPNI_VER_MINOR)) {
+               printf("DPNI version mismatch found %u.%u,",
+                      dpni_attr.version.major, dpni_attr.version.minor);
+               printf("supported version is %u.%u\n",
+                      DPNI_VER_MAJOR, DPNI_VER_MINOR);
+       }
+
        dflt_dpni->dpni_id = dpni_attr.id;
 #ifdef DEBUG
        printf("Init: DPNI id=0x%d\n", dflt_dpni->dpni_id);
@@ -1009,11 +1125,12 @@ static int dpni_init(void)
        return 0;
 
 err_close:
-       free(dflt_dpni);
 err_get_attr:
        dpni_close(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
        dpni_destroy(dflt_mc_io, MC_CMD_NO_FLAGS, dflt_dpni->dpni_handle);
 err_create:
+err_prepare_extended_cfg:
+       free(dflt_dpni);
 err_malloc:
        return err;
 }
@@ -1085,7 +1202,13 @@ int fsl_mc_ldpaa_exit(bd_t *bd)
 {
        int err = 0;
 
-       if (bd && get_mc_boot_status() == -1)
+       if (bd && mc_lazy_dpl_addr && !fsl_mc_ldpaa_exit(NULL)) {
+               mc_apply_dpl(mc_lazy_dpl_addr);
+               mc_lazy_dpl_addr = 0;
+       }
+
+       /* MC is not loaded intentionally, So return success. */
+       if (bd && get_mc_boot_status() != 0)
                return 0;
 
        if (bd && !get_mc_boot_status() && get_dpl_apply_status() == -1) {
@@ -1099,7 +1222,7 @@ int fsl_mc_ldpaa_exit(bd_t *bd)
 
        err = dpbp_exit();
        if (err < 0) {
-               printf("dpni_exit() failed: %d\n", err);
+               printf("dpbp_exit() failed: %d\n", err);
                goto err;
        }
 
@@ -1172,6 +1295,7 @@ static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                                aiop_fw_addr = simple_strtoull(argv[3], NULL,
                                                               16);
 
+                               /* if SoC doesn't have AIOP, err = -ENODEV */
                                err = load_mc_aiop_img(aiop_fw_addr);
                                if (!err)
                                        printf("fsl-mc: AIOP FW applied\n");
@@ -1186,6 +1310,7 @@ static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                }
                break;
 
+       case 'l':
        case 'a': {
                        u64 mc_dpl_addr;
 
@@ -1206,8 +1331,17 @@ static int do_fsl_mc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                                return -ENODEV;
                        }
 
-                       if (!fsl_mc_ldpaa_exit(NULL))
-                               err = mc_apply_dpl(mc_dpl_addr);
+                       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);
+                       }
                        break;
                }
        default:
@@ -1225,5 +1359,6 @@ U_BOOT_CMD(
        "DPAA2 command to manage Management Complex (MC)",
        "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 start aiop [FW_addr] - Start AIOP\n"
 );