Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 2 | * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 8 | #include <libfdt.h> |
Masahiro Yamada | 1f75280 | 2016-04-21 14:43:12 +0900 | [diff] [blame] | 9 | #include <fdtdec.h> |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 10 | #include <linux/err.h> |
| 11 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
| 14 | static const void *get_memory_reg_prop(const void *fdt, int *lenp) |
| 15 | { |
| 16 | int offset; |
| 17 | |
| 18 | offset = fdt_path_offset(fdt, "/memory"); |
| 19 | if (offset < 0) |
| 20 | return NULL; |
| 21 | |
| 22 | return fdt_getprop(fdt, offset, "reg", lenp); |
| 23 | } |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 24 | |
| 25 | int dram_init(void) |
| 26 | { |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 27 | const void *fdt = gd->fdt_blob; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 28 | const fdt32_t *val; |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 29 | int ac, sc, len; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 30 | |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 31 | ac = fdt_address_cells(fdt, 0); |
| 32 | sc = fdt_size_cells(fdt, 0); |
| 33 | if (ac < 0 || sc < 1 || sc > 2) { |
| 34 | printf("invalid address/size cells\n"); |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 35 | return -EINVAL; |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 36 | } |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 37 | |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 38 | val = get_memory_reg_prop(fdt, &len); |
| 39 | if (len / sizeof(*val) < ac + sc) |
| 40 | return -EINVAL; |
| 41 | |
| 42 | val += ac; |
| 43 | |
Masahiro Yamada | 1f75280 | 2016-04-21 14:43:12 +0900 | [diff] [blame] | 44 | gd->ram_size = fdtdec_get_number(val, sc); |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 45 | |
Masahiro Yamada | 0935010 | 2016-02-26 18:59:45 +0900 | [diff] [blame] | 46 | debug("DRAM size = %08lx\n", (unsigned long)gd->ram_size); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 47 | |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 48 | return 0; |
| 49 | } |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 50 | |
| 51 | void dram_init_banksize(void) |
| 52 | { |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 53 | const void *fdt = gd->fdt_blob; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 54 | const fdt32_t *val; |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 55 | int ac, sc, cells, len, i; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 56 | |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 57 | val = get_memory_reg_prop(fdt, &len); |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 58 | if (len < 0) |
| 59 | return; |
| 60 | |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 61 | ac = fdt_address_cells(fdt, 0); |
| 62 | sc = fdt_size_cells(fdt, 0); |
| 63 | if (ac < 1 || sc > 2 || sc < 1 || sc > 2) { |
| 64 | printf("invalid address/size cells\n"); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | cells = ac + sc; |
| 69 | |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 70 | len /= sizeof(*val); |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 71 | |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 72 | for (i = 0; i < CONFIG_NR_DRAM_BANKS && len >= cells; |
| 73 | i++, len -= cells) { |
Masahiro Yamada | 1f75280 | 2016-04-21 14:43:12 +0900 | [diff] [blame] | 74 | gd->bd->bi_dram[i].start = fdtdec_get_number(val, ac); |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 75 | val += ac; |
Masahiro Yamada | 1f75280 | 2016-04-21 14:43:12 +0900 | [diff] [blame] | 76 | gd->bd->bi_dram[i].size = fdtdec_get_number(val, sc); |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 77 | val += sc; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 78 | |
| 79 | debug("DRAM bank %d: start = %08lx, size = %08lx\n", |
Masahiro Yamada | 0935010 | 2016-02-26 18:59:45 +0900 | [diff] [blame] | 80 | i, (unsigned long)gd->bd->bi_dram[i].start, |
| 81 | (unsigned long)gd->bd->bi_dram[i].size); |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 82 | } |
| 83 | } |