Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 1 | /* |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2016-2018, NVIDIA CORPORATION. |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 3 | * |
| 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 Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 12 | #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 Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
| 30 | extern unsigned long nvtboot_boot_x0; |
| 31 | |
| 32 | /* |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 33 | * 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 Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 37 | */ |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 38 | |
| 39 | /* A parsed version of /memory/reg from the DTB passed to U-Boot in x0 */ |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 40 | static struct { |
| 41 | u64 start; |
| 42 | u64 size; |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 43 | } ram_banks[CONFIG_NR_DRAM_BANKS] __attribute__((section(".data"))); |
| 44 | |
| 45 | /* The number of valid entries in ram_banks[] */ |
| 46 | static 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 | */ |
| 54 | static u64 ram_top __attribute__((section(".data"))); |
| 55 | /* The base address of the region of RAM that ends at ram_top */ |
| 56 | static u64 region_base __attribute__((section(".data"))); |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 57 | |
| 58 | int 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 Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 72 | pr_err("Can't find /memory node in nvtboot DTB"); |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 73 | hang(); |
| 74 | } |
| 75 | prop = fdt_getprop(nvtboot_blob, node, "reg", &len); |
| 76 | if (!prop) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 77 | pr_err("Can't find /memory/reg property in nvtboot DTB"); |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 78 | hang(); |
| 79 | } |
| 80 | |
Stephen Warren | 0603dd2 | 2018-01-03 14:32:33 -0700 | [diff] [blame] | 81 | /* 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 Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 84 | if (len > ARRAY_SIZE(ram_banks)) |
| 85 | len = ARRAY_SIZE(ram_banks); |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 86 | ram_bank_count = len; |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 87 | |
| 88 | gd->ram_size = 0; |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 89 | for (i = 0; i < ram_bank_count; i++) { |
| 90 | u64 bank_end, usable_bank_size; |
| 91 | |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 92 | ram_banks[i].start = fdt_read_number(prop, na); |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 93 | prop += na; |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 94 | ram_banks[i].size = fdt_read_number(prop, ns); |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 95 | prop += ns; |
| 96 | gd->ram_size += ram_banks[i].size; |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 97 | 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 Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 122 | int dram_init_banksize(void) |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 123 | { |
| 124 | int i; |
| 125 | |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 126 | 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 Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 132 | gd->bd->bi_dram[i].start = ram_banks[i].start; |
| 133 | gd->bd->bi_dram[i].size = ram_banks[i].size; |
| 134 | } |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 135 | |
Stephen Warren | 0603dd2 | 2018-01-03 14:32:33 -0700 | [diff] [blame] | 136 | #ifdef CONFIG_PCI |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 137 | gd->pci_ram_top = ram_top; |
Stephen Warren | 0603dd2 | 2018-01-03 14:32:33 -0700 | [diff] [blame] | 138 | #endif |
| 139 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 140 | return 0; |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | ulong board_get_usable_ram_top(ulong total_size) |
| 144 | { |
Stephen Warren | 51aa324 | 2018-01-03 14:32:34 -0700 | [diff] [blame^] | 145 | return ram_top; |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 146 | } |