blob: 33e331f4ca87c4bf348bc3cb1c1ca2d9afcd03b8 [file] [log] [blame]
Stephen Warrenc5f510f2016-07-18 17:01:51 -06001/*
Stephen Warren51aa3242018-01-03 14:32:34 -07002 * Copyright (c) 2016-2018, NVIDIA CORPORATION.
Stephen Warrenc5f510f2016-07-18 17:01:51 -06003 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <fdt_support.h>
9#include <fdtdec.h>
10#include <asm/arch/tegra.h>
11
Stephen Warren51aa3242018-01-03 14:32:34 -070012#define SZ_4G 0x100000000ULL
13
14/*
15 * Size of a region that's large enough to hold the relocated U-Boot and all
16 * other allocations made around it (stack, heap, page tables, etc.)
17 * In practice, running "bdinfo" at the shell prompt, the stack reaches about
18 * 5MB from the address selected for ram_top as of the time of writing,
19 * so a 16MB region should be plenty.
20 */
21#define MIN_USABLE_RAM_SIZE SZ_16M
22/*
23 * The amount of space we expect to require for stack usage. Used to validate
24 * that all reservations fit into the region selected for the relocation target
25 */
26#define MIN_USABLE_STACK_SIZE SZ_1M
27
Stephen Warrenc5f510f2016-07-18 17:01:51 -060028DECLARE_GLOBAL_DATA_PTR;
29
30extern unsigned long nvtboot_boot_x0;
31
32/*
Stephen Warren51aa3242018-01-03 14:32:34 -070033 * These variables are written to before relocation, and hence cannot be
34 * in.bss, since .bss overlaps the DTB that's appended to the U-Boot binary.
35 * The section attribute forces this into .data and avoids this issue. This
36 * also has the nice side-effect of the content being valid after relocation.
Stephen Warrenc5f510f2016-07-18 17:01:51 -060037 */
Stephen Warren51aa3242018-01-03 14:32:34 -070038
39/* A parsed version of /memory/reg from the DTB passed to U-Boot in x0 */
Stephen Warrenc5f510f2016-07-18 17:01:51 -060040static struct {
41 u64 start;
42 u64 size;
Stephen Warren51aa3242018-01-03 14:32:34 -070043} ram_banks[CONFIG_NR_DRAM_BANKS] __attribute__((section(".data")));
44
45/* The number of valid entries in ram_banks[] */
46static int ram_bank_count __attribute__((section(".data")));
47
48/*
49 * The usable top-of-RAM for U-Boot. This is both:
50 * a) Below 4GB to avoid issues with peripherals that use 32-bit addressing.
51 * b) At the end of a region that has enough space to hold the relocated U-Boot
52 * and all other allocations made around it (stack, heap, page tables, etc.)
53 */
54static u64 ram_top __attribute__((section(".data")));
55/* The base address of the region of RAM that ends at ram_top */
56static u64 region_base __attribute__((section(".data")));
Stephen Warrenc5f510f2016-07-18 17:01:51 -060057
58int dram_init(void)
59{
60 unsigned int na, ns;
61 const void *nvtboot_blob = (void *)nvtboot_boot_x0;
62 int node, len, i;
63 const u32 *prop;
64
65 memset(ram_banks, 0, sizeof(ram_banks));
66
67 na = fdtdec_get_uint(nvtboot_blob, 0, "#address-cells", 2);
68 ns = fdtdec_get_uint(nvtboot_blob, 0, "#size-cells", 2);
69
70 node = fdt_path_offset(nvtboot_blob, "/memory");
71 if (node < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090072 pr_err("Can't find /memory node in nvtboot DTB");
Stephen Warrenc5f510f2016-07-18 17:01:51 -060073 hang();
74 }
75 prop = fdt_getprop(nvtboot_blob, node, "reg", &len);
76 if (!prop) {
Masahiro Yamada81e10422017-09-16 14:10:41 +090077 pr_err("Can't find /memory/reg property in nvtboot DTB");
Stephen Warrenc5f510f2016-07-18 17:01:51 -060078 hang();
79 }
80
Stephen Warren0603dd22018-01-03 14:32:33 -070081 /* Calculate the true # of base/size pairs to read */
82 len /= 4; /* Convert bytes to number of cells */
83 len /= (na + ns); /* Convert cells to number of banks */
Stephen Warrenc5f510f2016-07-18 17:01:51 -060084 if (len > ARRAY_SIZE(ram_banks))
85 len = ARRAY_SIZE(ram_banks);
Stephen Warren51aa3242018-01-03 14:32:34 -070086 ram_bank_count = len;
Stephen Warrenc5f510f2016-07-18 17:01:51 -060087
88 gd->ram_size = 0;
Stephen Warren51aa3242018-01-03 14:32:34 -070089 for (i = 0; i < ram_bank_count; i++) {
90 u64 bank_end, usable_bank_size;
91
Simon Glassbb7c01e2017-05-18 20:09:26 -060092 ram_banks[i].start = fdt_read_number(prop, na);
Stephen Warrenc5f510f2016-07-18 17:01:51 -060093 prop += na;
Simon Glassbb7c01e2017-05-18 20:09:26 -060094 ram_banks[i].size = fdt_read_number(prop, ns);
Stephen Warrenc5f510f2016-07-18 17:01:51 -060095 prop += ns;
96 gd->ram_size += ram_banks[i].size;
Stephen Warren51aa3242018-01-03 14:32:34 -070097 debug("Bank %d: start: %llx size: %llx\n", i,
98 ram_banks[i].start, ram_banks[i].size);
99
100 bank_end = ram_banks[i].start + ram_banks[i].size;
101 debug(" end %llx\n", bank_end);
102 if (bank_end > SZ_4G)
103 bank_end = SZ_4G;
104 debug(" end %llx (usable)\n", bank_end);
105 usable_bank_size = bank_end - ram_banks[i].start;
106 debug(" size %llx (usable)\n", usable_bank_size);
107 if ((usable_bank_size >= MIN_USABLE_RAM_SIZE) &&
108 (bank_end > ram_top)) {
109 ram_top = bank_end;
110 region_base = ram_banks[i].start;
111 debug("ram top now %llx\n", ram_top);
112 }
113 }
114 if (!ram_top) {
115 pr_err("Can't find a usable RAM top");
116 hang();
Stephen Warrenc5f510f2016-07-18 17:01:51 -0600117 }
118
119 return 0;
120}
121
Simon Glass2f949c32017-03-31 08:40:32 -0600122int dram_init_banksize(void)
Stephen Warrenc5f510f2016-07-18 17:01:51 -0600123{
124 int i;
125
Stephen Warren51aa3242018-01-03 14:32:34 -0700126 if ((gd->start_addr_sp - region_base) < MIN_USABLE_STACK_SIZE) {
127 pr_err("Reservations exceed chosen region size");
128 hang();
129 }
130
131 for (i = 0; i < ram_bank_count; i++) {
Stephen Warrenc5f510f2016-07-18 17:01:51 -0600132 gd->bd->bi_dram[i].start = ram_banks[i].start;
133 gd->bd->bi_dram[i].size = ram_banks[i].size;
134 }
Simon Glass2f949c32017-03-31 08:40:32 -0600135
Stephen Warren0603dd22018-01-03 14:32:33 -0700136#ifdef CONFIG_PCI
Stephen Warren51aa3242018-01-03 14:32:34 -0700137 gd->pci_ram_top = ram_top;
Stephen Warren0603dd22018-01-03 14:32:33 -0700138#endif
139
Simon Glass2f949c32017-03-31 08:40:32 -0600140 return 0;
Stephen Warrenc5f510f2016-07-18 17:01:51 -0600141}
142
143ulong board_get_usable_ram_top(ulong total_size)
144{
Stephen Warren51aa3242018-01-03 14:32:34 -0700145 return ram_top;
Stephen Warrenc5f510f2016-07-18 17:01:51 -0600146}