flash: Tidy up coding style for flash functions
[oweals/u-boot.git] / common / board_f.c
index 149a7229e8fa982d53ce003c67ddb2db7d45e2e1..a5ead31ca7dc87e9265fd22fff29529d5c7bfbde 100644 (file)
 
 #include <common.h>
 #include <bloblist.h>
+#include <clock_legacy.h>
 #include <console.h>
 #include <cpu.h>
+#include <cpu_func.h>
 #include <dm.h>
-#include <environment.h>
+#include <env.h>
+#include <env_internal.h>
 #include <fdtdec.h>
 #include <fs.h>
+#include <hang.h>
 #include <i2c.h>
+#include <init.h>
 #include <initcall.h>
+#include <lcd.h>
 #include <malloc.h>
 #include <mapmem.h>
 #include <os.h>
 #include <post.h>
 #include <relocate.h>
+#include <serial.h>
 #ifdef CONFIG_SPL
 #include <spl.h>
 #endif
@@ -176,11 +183,11 @@ static int print_cpuinfo(void)
        char desc[512];
        int ret;
 
-       ret = uclass_first_device_err(UCLASS_CPU, &dev);
-       if (ret) {
-               debug("%s: Could not get CPU device (err = %d)\n",
-                     __func__, ret);
-               return ret;
+       dev = cpu_get_current_dev();
+       if (!dev) {
+               debug("%s: Could not get CPU device\n",
+                     __func__);
+               return -ENODEV;
        }
 
        ret = cpu_get_desc(dev, desc, sizeof(desc));
@@ -302,7 +309,7 @@ __weak int mach_cpu_init(void)
 /* Get the top of usable RAM */
 __weak ulong board_get_usable_ram_top(ulong total_size)
 {
-#ifdef CONFIG_SYS_SDRAM_BASE
+#if defined(CONFIG_SYS_SDRAM_BASE) && CONFIG_SYS_SDRAM_BASE > 0
        /*
         * Detect whether we have so much RAM that it goes past the end of our
         * 32-bit address space. If so, clip the usable RAM so it doesn't.
@@ -378,33 +385,10 @@ static int reserve_round_4k(void)
        return 0;
 }
 
-#ifdef CONFIG_ARM
-__weak int reserve_mmu(void)
+__weak int arch_reserve_mmu(void)
 {
-#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
-       /* reserve TLB table */
-       gd->arch.tlb_size = PGTABLE_SIZE;
-       gd->relocaddr -= gd->arch.tlb_size;
-
-       /* round down to next 64 kB limit */
-       gd->relocaddr &= ~(0x10000 - 1);
-
-       gd->arch.tlb_addr = gd->relocaddr;
-       debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
-             gd->arch.tlb_addr + gd->arch.tlb_size);
-
-#ifdef CONFIG_SYS_MEM_RESERVE_SECURE
-       /*
-        * Record allocated tlb_addr in case gd->tlb_addr to be overwritten
-        * with location within secure ram.
-        */
-       gd->arch.tlb_allocated = gd->arch.tlb_addr;
-#endif
-#endif
-
        return 0;
 }
-#endif
 
 static int reserve_video(void)
 {
@@ -425,13 +409,6 @@ static int reserve_video(void)
        gd->relocaddr = lcd_setmem(gd->relocaddr);
        gd->fb_base = gd->relocaddr;
 #  endif /* CONFIG_FB_ADDR */
-#elif defined(CONFIG_VIDEO) && \
-               (!defined(CONFIG_PPC)) && \
-               !defined(CONFIG_ARM) && !defined(CONFIG_X86) && \
-               !defined(CONFIG_M68K)
-       /* reserve memory for video display (always full pages) */
-       gd->relocaddr = video_setmem(gd->relocaddr);
-       gd->fb_base = gd->relocaddr;
 #endif
 
        return 0;
@@ -442,8 +419,8 @@ static int reserve_trace(void)
 #ifdef CONFIG_TRACE
        gd->relocaddr -= CONFIG_TRACE_BUFFER_SIZE;
        gd->trace_buff = map_sysmem(gd->relocaddr, CONFIG_TRACE_BUFFER_SIZE);
-       debug("Reserving %dk for trace data at: %08lx\n",
-             CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
+       debug("Reserving %luk for trace data at: %08lx\n",
+             (unsigned long)CONFIG_TRACE_BUFFER_SIZE >> 10, gd->relocaddr);
 #endif
 
        return 0;
@@ -472,12 +449,49 @@ static int reserve_uboot(void)
        return 0;
 }
 
+/*
+ * reserve after start_addr_sp the requested size and make the stack pointer
+ * 16-byte aligned, this alignment is needed for cast on the reserved memory
+ * ref = x86_64 ABI: https://reviews.llvm.org/D30049: 16 bytes
+ *     = ARMv8 Instruction Set Overview: quad word, 16 bytes
+ */
+static unsigned long reserve_stack_aligned(size_t size)
+{
+       return ALIGN_DOWN(gd->start_addr_sp - size, 16);
+}
+
+#ifdef CONFIG_SYS_NONCACHED_MEMORY
+static int reserve_noncached(void)
+{
+       /*
+        * The value of gd->start_addr_sp must match the value of malloc_start
+        * calculated in boatrd_f.c:initr_malloc(), which is passed to
+        * board_r.c:mem_malloc_init() and then used by
+        * cache.c:noncached_init()
+        *
+        * These calculations must match the code in cache.c:noncached_init()
+        */
+       gd->start_addr_sp = ALIGN(gd->start_addr_sp, MMU_SECTION_SIZE) -
+               MMU_SECTION_SIZE;
+       gd->start_addr_sp -= ALIGN(CONFIG_SYS_NONCACHED_MEMORY,
+                                  MMU_SECTION_SIZE);
+       debug("Reserving %dM for noncached_alloc() at: %08lx\n",
+             CONFIG_SYS_NONCACHED_MEMORY >> 20, gd->start_addr_sp);
+
+       return 0;
+}
+#endif
+
 /* reserve memory for malloc() area */
 static int reserve_malloc(void)
 {
-       gd->start_addr_sp = gd->start_addr_sp - TOTAL_MALLOC_LEN;
+       gd->start_addr_sp = reserve_stack_aligned(TOTAL_MALLOC_LEN);
        debug("Reserving %dk for malloc() at: %08lx\n",
              TOTAL_MALLOC_LEN >> 10, gd->start_addr_sp);
+#ifdef CONFIG_SYS_NONCACHED_MEMORY
+       reserve_noncached();
+#endif
+
        return 0;
 }
 
@@ -485,7 +499,7 @@ static int reserve_malloc(void)
 static int reserve_board(void)
 {
        if (!gd->bd) {
-               gd->start_addr_sp -= sizeof(bd_t);
+               gd->start_addr_sp = reserve_stack_aligned(sizeof(bd_t));
                gd->bd = (bd_t *)map_sysmem(gd->start_addr_sp, sizeof(bd_t));
                memset(gd->bd, '\0', sizeof(bd_t));
                debug("Reserving %zu Bytes for Board Info at: %08lx\n",
@@ -504,7 +518,7 @@ static int setup_machine(void)
 
 static int reserve_global_data(void)
 {
-       gd->start_addr_sp -= sizeof(gd_t);
+       gd->start_addr_sp = reserve_stack_aligned(sizeof(gd_t));
        gd->new_gd = (gd_t *)map_sysmem(gd->start_addr_sp, sizeof(gd_t));
        debug("Reserving %zu Bytes for Global Data at: %08lx\n",
              sizeof(gd_t), gd->start_addr_sp);
@@ -522,7 +536,7 @@ static int reserve_fdt(void)
        if (gd->fdt_blob) {
                gd->fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
 
-               gd->start_addr_sp -= gd->fdt_size;
+               gd->start_addr_sp = reserve_stack_aligned(gd->fdt_size);
                gd->new_fdt = map_sysmem(gd->start_addr_sp, gd->fdt_size);
                debug("Reserving %lu Bytes for FDT at: %08lx\n",
                      gd->fdt_size, gd->start_addr_sp);
@@ -537,7 +551,7 @@ static int reserve_bootstage(void)
 #ifdef CONFIG_BOOTSTAGE
        int size = bootstage_get_size();
 
-       gd->start_addr_sp -= size;
+       gd->start_addr_sp = reserve_stack_aligned(size);
        gd->new_bootstage = map_sysmem(gd->start_addr_sp, size);
        debug("Reserving %#x Bytes for bootstage at: %08lx\n", size,
              gd->start_addr_sp);
@@ -554,8 +568,7 @@ __weak int arch_reserve_stacks(void)
 static int reserve_stacks(void)
 {
        /* make stack pointer 16-byte aligned */
-       gd->start_addr_sp -= 16;
-       gd->start_addr_sp &= ~0xf;
+       gd->start_addr_sp = reserve_stack_aligned(16);
 
        /*
         * let the architecture-specific code tailor gd->start_addr_sp and
@@ -567,7 +580,7 @@ static int reserve_stacks(void)
 static int reserve_bloblist(void)
 {
 #ifdef CONFIG_BLOBLIST
-       gd->start_addr_sp -= CONFIG_BLOBLIST_SIZE;
+       gd->start_addr_sp = reserve_stack_aligned(CONFIG_BLOBLIST_SIZE);
        gd->new_bloblist = map_sysmem(gd->start_addr_sp, CONFIG_BLOBLIST_SIZE);
 #endif
 
@@ -674,6 +687,7 @@ static int reloc_bootstage(void)
                      gd->bootstage, gd->new_bootstage, size);
                memcpy(gd->new_bootstage, gd->bootstage, size);
                gd->bootstage = gd->new_bootstage;
+               bootstage_relocate();
        }
 #endif
 
@@ -714,7 +728,7 @@ static int setup_reloc(void)
         * just after the default vector table location, so at 0x400
         */
        gd->reloc_off = gd->relocaddr - (CONFIG_SYS_TEXT_BASE + 0x400);
-#else
+#elif !defined(CONFIG_SANDBOX)
        gd->reloc_off = gd->relocaddr - CONFIG_SYS_TEXT_BASE;
 #endif
 #endif
@@ -834,12 +848,22 @@ __weak int arch_cpu_init_dm(void)
        return 0;
 }
 
+__weak int checkcpu(void)
+{
+       return 0;
+}
+
+__weak int clear_bss(void)
+{
+       return 0;
+}
+
 static const init_fnc_t init_sequence_f[] = {
        setup_mon_len,
 #ifdef CONFIG_OF_CONTROL
        fdtdec_setup,
 #endif
-#ifdef CONFIG_TRACE
+#ifdef CONFIG_TRACE_EARLY
        trace_early_init,
 #endif
        initf_malloc,
@@ -876,9 +900,7 @@ static const init_fnc_t init_sequence_f[] = {
        console_init_f,         /* stage 1 init of console */
        display_options,        /* say that we are here */
        display_text_info,      /* show debugging info if required */
-#if defined(CONFIG_PPC) || defined(CONFIG_SH) || defined(CONFIG_X86)
        checkcpu,
-#endif
 #if defined(CONFIG_SYSRESET)
        print_resetinfo,
 #endif
@@ -934,9 +956,7 @@ static const init_fnc_t init_sequence_f[] = {
        reserve_pram,
 #endif
        reserve_round_4k,
-#ifdef CONFIG_ARM
-       reserve_mmu,
-#endif
+       arch_reserve_mmu,
        reserve_video,
        reserve_trace,
        reserve_uboot,
@@ -971,11 +991,8 @@ static const init_fnc_t init_sequence_f[] = {
 #if defined(CONFIG_X86) || defined(CONFIG_ARC)
        copy_uboot_to_ram,
        do_elf_reloc_fixups,
-       clear_bss,
 #endif
-#if defined(CONFIG_XTENSA)
        clear_bss,
-#endif
 #if !defined(CONFIG_ARM) && !defined(CONFIG_SANDBOX) && \
                !CONFIG_IS_ENABLED(X86_64)
        jump_to_copy,