ARM: imx: Do not hard-code MX8M MMU table DRAM entry offset
authorMarek Vasut <marex@denx.de>
Sat, 27 Feb 2021 13:59:00 +0000 (14:59 +0100)
committerStefano Babic <sbabic@denx.de>
Mon, 1 Mar 2021 09:21:36 +0000 (10:21 +0100)
Instead of hard-coding the offset of DRAM entries in MMU table all over
the code, auto-detect the offset. This removes error-prone code which
would break e.g. in case the MMU table is modified.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Fabio Estevam <festevam@gmail.com>
Cc: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Ye Li <ye.li@nxp.com>
Reviewed-by: Fabio Estevam <festevam@gmail.com>
arch/arm/mach-imx/imx8m/soc.c

index 5456c10..5f37282 100644 (file)
@@ -154,6 +154,17 @@ static struct mm_region imx8m_mem_map[] = {
 
 struct mm_region *mem_map = imx8m_mem_map;
 
+static unsigned int imx8m_find_dram_entry_in_mem_map(void)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(imx8m_mem_map); i++)
+               if (imx8m_mem_map[i].phys == CONFIG_SYS_SDRAM_BASE)
+                       return i;
+
+       hang(); /* Entry not found, this must never happen. */
+}
+
 void enable_caches(void)
 {
        /* If OPTEE runs, remove OPTEE memory from MMU table to avoid speculative prefetch */
@@ -167,10 +178,11 @@ void enable_caches(void)
                 * please make sure that entry initial value matches
                 * imx8m_mem_map for DRAM1
                 */
-               int entry = 5;
+               int entry = imx8m_find_dram_entry_in_mem_map();
                u64 attrs = imx8m_mem_map[entry].attrs;
 
-               while (i < CONFIG_NR_DRAM_BANKS && entry < 8) {
+               while (i < CONFIG_NR_DRAM_BANKS &&
+                      entry < ARRAY_SIZE(imx8m_mem_map)) {
                        if (gd->bd->bi_dram[i].start == 0)
                                break;
                        imx8m_mem_map[entry].phys = gd->bd->bi_dram[i].start;
@@ -198,6 +210,7 @@ __weak int board_phys_sdram_size(phys_size_t *size)
 
 int dram_init(void)
 {
+       unsigned int entry = imx8m_find_dram_entry_in_mem_map();
        phys_size_t sdram_size;
        int ret;
 
@@ -212,7 +225,7 @@ int dram_init(void)
                gd->ram_size = sdram_size;
 
        /* also update the SDRAM size in the mem_map used externally */
-       imx8m_mem_map[5].size = sdram_size;
+       imx8m_mem_map[entry].size = sdram_size;
 
 #ifdef PHYS_SDRAM_2_SIZE
        gd->ram_size += PHYS_SDRAM_2_SIZE;