Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016, NVIDIA CORPORATION. |
| 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 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
| 14 | extern unsigned long nvtboot_boot_x0; |
| 15 | |
| 16 | /* |
| 17 | * A parsed version of /memory/reg from the DTB that is passed to U-Boot in x0. |
| 18 | * |
Stephen Warren | 0603dd2 | 2018-01-03 14:32:33 -0700 | [diff] [blame] | 19 | * We assume bank 0 is RAM completely below 4G mostly ignore other banks; |
| 20 | * assuming they contain RAM above 4G. This is all a fairly safe assumption, |
| 21 | * since the L4T kernel makes the same assumption, so the bootloader is |
| 22 | * unlikely to change. |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 23 | * |
| 24 | * This is written to before relocation, and hence cannot be in .bss, since |
| 25 | * .bss overlaps the DTB that's appended to the U-Boot binary. The initializer |
| 26 | * forces this into .data and avoids this issue. This also has the nice side- |
| 27 | * effect of the content being valid after relocation. |
| 28 | */ |
| 29 | static struct { |
| 30 | u64 start; |
| 31 | u64 size; |
Stephen Warren | 0603dd2 | 2018-01-03 14:32:33 -0700 | [diff] [blame] | 32 | } ram_banks[CONFIG_NR_DRAM_BANKS] = {{1}}; |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 33 | |
| 34 | int dram_init(void) |
| 35 | { |
| 36 | unsigned int na, ns; |
| 37 | const void *nvtboot_blob = (void *)nvtboot_boot_x0; |
| 38 | int node, len, i; |
| 39 | const u32 *prop; |
| 40 | |
| 41 | memset(ram_banks, 0, sizeof(ram_banks)); |
| 42 | |
| 43 | na = fdtdec_get_uint(nvtboot_blob, 0, "#address-cells", 2); |
| 44 | ns = fdtdec_get_uint(nvtboot_blob, 0, "#size-cells", 2); |
| 45 | |
| 46 | node = fdt_path_offset(nvtboot_blob, "/memory"); |
| 47 | if (node < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 48 | pr_err("Can't find /memory node in nvtboot DTB"); |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 49 | hang(); |
| 50 | } |
| 51 | prop = fdt_getprop(nvtboot_blob, node, "reg", &len); |
| 52 | if (!prop) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 53 | pr_err("Can't find /memory/reg property in nvtboot DTB"); |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 54 | hang(); |
| 55 | } |
| 56 | |
Stephen Warren | 0603dd2 | 2018-01-03 14:32:33 -0700 | [diff] [blame] | 57 | /* Calculate the true # of base/size pairs to read */ |
| 58 | len /= 4; /* Convert bytes to number of cells */ |
| 59 | len /= (na + ns); /* Convert cells to number of banks */ |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 60 | if (len > ARRAY_SIZE(ram_banks)) |
| 61 | len = ARRAY_SIZE(ram_banks); |
| 62 | |
| 63 | gd->ram_size = 0; |
| 64 | for (i = 0; i < len; i++) { |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 65 | ram_banks[i].start = fdt_read_number(prop, na); |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 66 | prop += na; |
Simon Glass | bb7c01e | 2017-05-18 20:09:26 -0600 | [diff] [blame] | 67 | ram_banks[i].size = fdt_read_number(prop, ns); |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 68 | prop += ns; |
| 69 | gd->ram_size += ram_banks[i].size; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | extern unsigned long nvtboot_boot_x0; |
| 76 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 77 | int dram_init_banksize(void) |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 78 | { |
| 79 | int i; |
| 80 | |
Stephen Warren | 0603dd2 | 2018-01-03 14:32:33 -0700 | [diff] [blame] | 81 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 82 | gd->bd->bi_dram[i].start = ram_banks[i].start; |
| 83 | gd->bd->bi_dram[i].size = ram_banks[i].size; |
| 84 | } |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 85 | |
Stephen Warren | 0603dd2 | 2018-01-03 14:32:33 -0700 | [diff] [blame] | 86 | #ifdef CONFIG_PCI |
| 87 | gd->pci_ram_top = gd->bd->bi_dram[0].start + gd->bd->bi_dram[0].size; |
| 88 | #endif |
| 89 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 90 | return 0; |
Stephen Warren | c5f510f | 2016-07-18 17:01:51 -0600 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | ulong board_get_usable_ram_top(ulong total_size) |
| 94 | { |
| 95 | return ram_banks[0].start + ram_banks[0].size; |
| 96 | } |