blob: 582c0ffe245ca6739d40347408a89bc80c728744 [file] [log] [blame]
Graeme Russd47ab0e2011-12-23 16:51:29 +11001/*
2 * (C) Copyright 2011
3 * Graeme Russ, <graeme.russ@gmail.com>
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Graeme Russd47ab0e2011-12-23 16:51:29 +11006 */
7#include <common.h>
Simon Glassf697d522013-02-28 19:26:15 +00008#include <fdtdec.h>
Gabe Black83133152012-11-03 11:41:23 +00009#include <spi.h>
Simon Glass86cfb6b2013-03-05 14:39:54 +000010#include <asm/sections.h>
Graeme Russd47ab0e2011-12-23 16:51:29 +110011
12DECLARE_GLOBAL_DATA_PTR;
13
Simon Glass5e989472013-02-28 19:26:10 +000014/* Get the top of usable RAM */
15__weak ulong board_get_usable_ram_top(ulong total_size)
Graeme Russa1d57b72011-12-23 21:14:22 +110016{
Simon Glass5e989472013-02-28 19:26:10 +000017 return gd->ram_size;
18}
19
20int calculate_relocation_address(void)
21{
22 const ulong uboot_size = (uintptr_t)&__bss_end -
23 (uintptr_t)&__text_start;
24 ulong total_size;
Graeme Russa1d57b72011-12-23 21:14:22 +110025 ulong dest_addr;
Simon Glassf697d522013-02-28 19:26:15 +000026 ulong fdt_size = 0;
Graeme Russa1d57b72011-12-23 21:14:22 +110027
Simon Glassf697d522013-02-28 19:26:15 +000028#if defined(CONFIG_OF_SEPARATE) && defined(CONFIG_OF_CONTROL)
29 if (gd->fdt_blob)
30 fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
31#endif
Simon Glass5e989472013-02-28 19:26:10 +000032 total_size = ALIGN(uboot_size, 1 << 12) + CONFIG_SYS_MALLOC_LEN +
Simon Glassf697d522013-02-28 19:26:15 +000033 CONFIG_SYS_STACK_SIZE + fdt_size;
Simon Glass5e989472013-02-28 19:26:10 +000034
Simon Glassf697d522013-02-28 19:26:15 +000035 dest_addr = board_get_usable_ram_top(total_size);
Graeme Russa1d57b72011-12-23 21:14:22 +110036 /*
37 * NOTE: All destination address are rounded down to 16-byte
38 * boundary to satisfy various worst-case alignment
39 * requirements
40 */
Simon Glassf697d522013-02-28 19:26:15 +000041 dest_addr &= ~15;
Graeme Russa1d57b72011-12-23 21:14:22 +110042
Simon Glassf697d522013-02-28 19:26:15 +000043#if defined(CONFIG_OF_SEPARATE) && defined(CONFIG_OF_CONTROL)
44 /*
45 * If the device tree is sitting immediate above our image then we
46 * must relocate it. If it is embedded in the data section, then it
47 * will be relocated with other data.
48 */
49 if (gd->fdt_blob) {
50 dest_addr -= fdt_size;
Simon Glass1938f4a2013-03-11 06:49:53 +000051 gd->new_fdt = (void *)dest_addr;
Simon Glassf697d522013-02-28 19:26:15 +000052 dest_addr &= ~15;
53 }
54#endif
Simon Glass5e989472013-02-28 19:26:10 +000055 /* U-Boot is below the FDT */
56 dest_addr -= uboot_size;
57 dest_addr &= ~((1 << 12) - 1);
Graeme Russa1d57b72011-12-23 21:14:22 +110058 gd->relocaddr = dest_addr;
Simon Glass5e989472013-02-28 19:26:10 +000059 gd->reloc_off = dest_addr - (uintptr_t)&__text_start;
Graeme Russa1d57b72011-12-23 21:14:22 +110060
Gabe Black32f98732012-11-03 11:41:24 +000061 /* Stack is at the bottom, so it can grow down */
62 gd->start_addr_sp = dest_addr - CONFIG_SYS_MALLOC_LEN;
63
Graeme Russa1d57b72011-12-23 21:14:22 +110064 return 0;
65}
66
Graeme Russa1d57b72011-12-23 21:14:22 +110067int init_cache_f_r(void)
68{
69 /* Initialise the CPU cache(s) */
70 return init_cache();
71}
72
Graeme Russd47ab0e2011-12-23 16:51:29 +110073bd_t bd_data;
74
75int init_bd_struct_r(void)
76{
77 gd->bd = &bd_data;
78 memset(gd->bd, 0, sizeof(bd_t));
79
80 return 0;
81}
82
Gabe Black83133152012-11-03 11:41:23 +000083int init_func_spi(void)
84{
85 puts("SPI: ");
86 spi_init();
87 puts("ready\n");
88 return 0;
89}
Gabe Blackb208c7f2012-11-03 11:41:30 +000090
Gabe Blackb208c7f2012-11-03 11:41:30 +000091int find_fdt(void)
92{
93#ifdef CONFIG_OF_EMBED
94 /* Get a pointer to the FDT */
95 gd->fdt_blob = _binary_dt_dtb_start;
96#elif defined CONFIG_OF_SEPARATE
97 /* FDT is at end of image */
Simon Glass4b491b82013-02-28 19:26:13 +000098 gd->fdt_blob = (ulong *)&_end;
Gabe Blackb208c7f2012-11-03 11:41:30 +000099#endif
100 /* Allow the early environment to override the fdt address */
101 gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
102 (uintptr_t)gd->fdt_blob);
103
104 return 0;
105}
106
107int prepare_fdt(void)
108{
109 /* For now, put this check after the console is ready */
110 if (fdtdec_prepare_fdt()) {
111 panic("** CONFIG_OF_CONTROL defined but no FDT - please see "
112 "doc/README.fdt-control");
113 }
114
115 return 0;
116}