blob: 489366c63f948ad36d7f2531c774cb927f82fb7a [file] [log] [blame]
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09001/*
Masahiro Yamadab4782cd2015-09-11 20:17:49 +09002 * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09003 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Masahiro Yamadab4782cd2015-09-11 20:17:49 +09008#include <libfdt.h>
Masahiro Yamada1f752802016-04-21 14:43:12 +09009#include <fdtdec.h>
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090010#include <linux/err.h>
11
Masahiro Yamada460483c2016-06-17 19:24:29 +090012#include "init.h"
13#include "soc-info.h"
14
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090015DECLARE_GLOBAL_DATA_PTR;
16
17static const void *get_memory_reg_prop(const void *fdt, int *lenp)
18{
19 int offset;
20
21 offset = fdt_path_offset(fdt, "/memory");
22 if (offset < 0)
23 return NULL;
24
25 return fdt_getprop(fdt, offset, "reg", lenp);
26}
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090027
28int dram_init(void)
29{
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090030 const void *fdt = gd->fdt_blob;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090031 const fdt32_t *val;
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090032 int ac, sc, len;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090033
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090034 ac = fdt_address_cells(fdt, 0);
35 sc = fdt_size_cells(fdt, 0);
36 if (ac < 0 || sc < 1 || sc > 2) {
37 printf("invalid address/size cells\n");
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090038 return -EINVAL;
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090039 }
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090040
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090041 val = get_memory_reg_prop(fdt, &len);
42 if (len / sizeof(*val) < ac + sc)
43 return -EINVAL;
44
45 val += ac;
46
Masahiro Yamada1f752802016-04-21 14:43:12 +090047 gd->ram_size = fdtdec_get_number(val, sc);
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090048
Masahiro Yamada09350102016-02-26 18:59:45 +090049 debug("DRAM size = %08lx\n", (unsigned long)gd->ram_size);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090050
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090051 return 0;
52}
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090053
54void dram_init_banksize(void)
55{
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090056 const void *fdt = gd->fdt_blob;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090057 const fdt32_t *val;
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090058 int ac, sc, cells, len, i;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090059
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090060 val = get_memory_reg_prop(fdt, &len);
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090061 if (len < 0)
62 return;
63
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090064 ac = fdt_address_cells(fdt, 0);
65 sc = fdt_size_cells(fdt, 0);
66 if (ac < 1 || sc > 2 || sc < 1 || sc > 2) {
67 printf("invalid address/size cells\n");
68 return;
69 }
70
71 cells = ac + sc;
72
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090073 len /= sizeof(*val);
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090074
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090075 for (i = 0; i < CONFIG_NR_DRAM_BANKS && len >= cells;
76 i++, len -= cells) {
Masahiro Yamada1f752802016-04-21 14:43:12 +090077 gd->bd->bi_dram[i].start = fdtdec_get_number(val, ac);
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090078 val += ac;
Masahiro Yamada1f752802016-04-21 14:43:12 +090079 gd->bd->bi_dram[i].size = fdtdec_get_number(val, sc);
Masahiro Yamadaa90b1102016-03-29 20:18:45 +090080 val += sc;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090081
82 debug("DRAM bank %d: start = %08lx, size = %08lx\n",
Masahiro Yamada09350102016-02-26 18:59:45 +090083 i, (unsigned long)gd->bd->bi_dram[i].start,
84 (unsigned long)gd->bd->bi_dram[i].size);
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090085 }
Masahiro Yamada460483c2016-06-17 19:24:29 +090086}
87
88#ifdef CONFIG_OF_BOARD_SETUP
89/*
90 * The DRAM PHY requires 64 byte scratch area in each DRAM channel
91 * for its dynamic PHY training feature.
92 */
93int ft_board_setup(void *fdt, bd_t *bd)
94{
95 const struct uniphier_board_data *param;
96 unsigned long rsv_addr;
97 const unsigned long rsv_size = 64;
98 int ch, ret;
99
100 if (uniphier_get_soc_type() != SOC_UNIPHIER_LD20)
101 return 0;
102
103 param = uniphier_get_board_param();
104 if (!param) {
105 printf("failed to get board parameter\n");
106 return -ENODEV;
107 }
108
109 for (ch = 0; ch < param->dram_nr_ch; ch++) {
110 rsv_addr = param->dram_ch[ch].base + param->dram_ch[ch].size;
111 rsv_addr -= rsv_size;
112
113 ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size);
114 if (ret)
115 return -ENOSPC;
116
117 printf(" Reserved memory region for DRAM PHY training: addr=%lx size=%lx\n",
118 rsv_addr, rsv_size);
119 }
120
121 return 0;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900122}
Masahiro Yamada460483c2016-06-17 19:24:29 +0900123#endif