blob: a57a0eb4682545b3b86dde61e297cc40c1ba82da [file] [log] [blame]
Graeme Russa875dda2011-12-23 16:51:29 +11001/*
2 * (C) Copyright 2011
3 * Graeme Russ, <graeme.russ@gmail.com>
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23#include <common.h>
Simon Glass347c05b2013-02-28 19:26:15 +000024#include <fdtdec.h>
Gabe Blacka97cbda2012-11-03 11:41:23 +000025#include <spi.h>
Simon Glass3e93e332013-03-05 14:39:54 +000026#include <asm/sections.h>
Graeme Russa875dda2011-12-23 16:51:29 +110027
28DECLARE_GLOBAL_DATA_PTR;
29
Simon Glass3297d4d2013-02-28 19:26:10 +000030/* Get the top of usable RAM */
31__weak ulong board_get_usable_ram_top(ulong total_size)
Graeme Russ3fb4f9e2011-12-23 21:14:22 +110032{
Simon Glass3297d4d2013-02-28 19:26:10 +000033 return gd->ram_size;
34}
35
36int calculate_relocation_address(void)
37{
38 const ulong uboot_size = (uintptr_t)&__bss_end -
39 (uintptr_t)&__text_start;
40 ulong total_size;
Graeme Russ3fb4f9e2011-12-23 21:14:22 +110041 ulong dest_addr;
Simon Glass347c05b2013-02-28 19:26:15 +000042 ulong fdt_size = 0;
Graeme Russ3fb4f9e2011-12-23 21:14:22 +110043
Simon Glass347c05b2013-02-28 19:26:15 +000044#if defined(CONFIG_OF_SEPARATE) && defined(CONFIG_OF_CONTROL)
45 if (gd->fdt_blob)
46 fdt_size = ALIGN(fdt_totalsize(gd->fdt_blob) + 0x1000, 32);
47#endif
Simon Glass3297d4d2013-02-28 19:26:10 +000048 total_size = ALIGN(uboot_size, 1 << 12) + CONFIG_SYS_MALLOC_LEN +
Simon Glass347c05b2013-02-28 19:26:15 +000049 CONFIG_SYS_STACK_SIZE + fdt_size;
Simon Glass3297d4d2013-02-28 19:26:10 +000050
Simon Glass347c05b2013-02-28 19:26:15 +000051 dest_addr = board_get_usable_ram_top(total_size);
Graeme Russ3fb4f9e2011-12-23 21:14:22 +110052 /*
53 * NOTE: All destination address are rounded down to 16-byte
54 * boundary to satisfy various worst-case alignment
55 * requirements
56 */
Simon Glass347c05b2013-02-28 19:26:15 +000057 dest_addr &= ~15;
Graeme Russ3fb4f9e2011-12-23 21:14:22 +110058
Simon Glass347c05b2013-02-28 19:26:15 +000059#if defined(CONFIG_OF_SEPARATE) && defined(CONFIG_OF_CONTROL)
60 /*
61 * If the device tree is sitting immediate above our image then we
62 * must relocate it. If it is embedded in the data section, then it
63 * will be relocated with other data.
64 */
65 if (gd->fdt_blob) {
66 dest_addr -= fdt_size;
Simon Glassc45e3592013-03-11 06:49:53 +000067 gd->new_fdt = (void *)dest_addr;
Simon Glass347c05b2013-02-28 19:26:15 +000068 dest_addr &= ~15;
69 }
70#endif
Simon Glass3297d4d2013-02-28 19:26:10 +000071 /* U-Boot is below the FDT */
72 dest_addr -= uboot_size;
73 dest_addr &= ~((1 << 12) - 1);
Graeme Russ3fb4f9e2011-12-23 21:14:22 +110074 gd->relocaddr = dest_addr;
Simon Glass3297d4d2013-02-28 19:26:10 +000075 gd->reloc_off = dest_addr - (uintptr_t)&__text_start;
Graeme Russ3fb4f9e2011-12-23 21:14:22 +110076
Gabe Black13113612012-11-03 11:41:24 +000077 /* Stack is at the bottom, so it can grow down */
78 gd->start_addr_sp = dest_addr - CONFIG_SYS_MALLOC_LEN;
79
Graeme Russ3fb4f9e2011-12-23 21:14:22 +110080 return 0;
81}
82
Graeme Russ3fb4f9e2011-12-23 21:14:22 +110083int init_cache_f_r(void)
84{
85 /* Initialise the CPU cache(s) */
86 return init_cache();
87}
88
Graeme Russa875dda2011-12-23 16:51:29 +110089bd_t bd_data;
90
91int init_bd_struct_r(void)
92{
93 gd->bd = &bd_data;
94 memset(gd->bd, 0, sizeof(bd_t));
95
96 return 0;
97}
98
Gabe Blacka97cbda2012-11-03 11:41:23 +000099int init_func_spi(void)
100{
101 puts("SPI: ");
102 spi_init();
103 puts("ready\n");
104 return 0;
105}
Gabe Black050a4c62012-11-03 11:41:30 +0000106
Gabe Black050a4c62012-11-03 11:41:30 +0000107int find_fdt(void)
108{
109#ifdef CONFIG_OF_EMBED
110 /* Get a pointer to the FDT */
111 gd->fdt_blob = _binary_dt_dtb_start;
112#elif defined CONFIG_OF_SEPARATE
113 /* FDT is at end of image */
Simon Glass781db8c2013-02-28 19:26:13 +0000114 gd->fdt_blob = (ulong *)&_end;
Gabe Black050a4c62012-11-03 11:41:30 +0000115#endif
116 /* Allow the early environment to override the fdt address */
117 gd->fdt_blob = (void *)getenv_ulong("fdtcontroladdr", 16,
118 (uintptr_t)gd->fdt_blob);
119
120 return 0;
121}
122
123int prepare_fdt(void)
124{
125 /* For now, put this check after the console is ready */
126 if (fdtdec_prepare_fdt()) {
127 panic("** CONFIG_OF_CONTROL defined but no FDT - please see "
128 "doc/README.fdt-control");
129 }
130
131 return 0;
132}