blob: 87418f496ebd92afda9043ab4658cfa4e99281d2 [file] [log] [blame]
Masahiro Yamada5894ca02014-10-03 19:21:06 +09001/*
Masahiro Yamadacf88aff2015-09-11 20:17:49 +09002 * Copyright (C) 2012-2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada5894ca02014-10-03 19:21:06 +09003 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
Masahiro Yamadacf88aff2015-09-11 20:17:49 +09008#include <libfdt.h>
Masahiro Yamada7b3a0322016-04-21 14:43:12 +09009#include <fdtdec.h>
Masahiro Yamada0f4ec052017-01-21 18:05:24 +090010#include <linux/errno.h>
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090011
Masahiro Yamada51ea5a02016-06-17 19:24:29 +090012#include "init.h"
13#include "soc-info.h"
14
Masahiro Yamadacf88aff2015-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 Yamada5894ca02014-10-03 19:21:06 +090027
28int dram_init(void)
29{
Masahiro Yamadaac2a1032016-03-29 20:18:45 +090030 const void *fdt = gd->fdt_blob;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090031 const fdt32_t *val;
Masahiro Yamadaac2a1032016-03-29 20:18:45 +090032 int ac, sc, len;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090033
Masahiro Yamadaac2a1032016-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");
38 return -EINVAL;
39 }
40
41 val = get_memory_reg_prop(fdt, &len);
42 if (len / sizeof(*val) < ac + sc)
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090043 return -EINVAL;
44
Masahiro Yamadaac2a1032016-03-29 20:18:45 +090045 val += ac;
46
Masahiro Yamada7b3a0322016-04-21 14:43:12 +090047 gd->ram_size = fdtdec_get_number(val, sc);
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090048
Masahiro Yamada11d3ede2016-02-26 18:59:45 +090049 debug("DRAM size = %08lx\n", (unsigned long)gd->ram_size);
Masahiro Yamada5894ca02014-10-03 19:21:06 +090050
Masahiro Yamada5894ca02014-10-03 19:21:06 +090051 return 0;
52}
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090053
54void dram_init_banksize(void)
55{
Masahiro Yamadaac2a1032016-03-29 20:18:45 +090056 const void *fdt = gd->fdt_blob;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090057 const fdt32_t *val;
Masahiro Yamadaac2a1032016-03-29 20:18:45 +090058 int ac, sc, cells, len, i;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090059
Masahiro Yamadaac2a1032016-03-29 20:18:45 +090060 val = get_memory_reg_prop(fdt, &len);
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090061 if (len < 0)
62 return;
63
Masahiro Yamadaac2a1032016-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 }
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090070
Masahiro Yamadaac2a1032016-03-29 20:18:45 +090071 cells = ac + sc;
72
73 len /= sizeof(*val);
74
75 for (i = 0; i < CONFIG_NR_DRAM_BANKS && len >= cells;
76 i++, len -= cells) {
Masahiro Yamada7b3a0322016-04-21 14:43:12 +090077 gd->bd->bi_dram[i].start = fdtdec_get_number(val, ac);
Masahiro Yamadaac2a1032016-03-29 20:18:45 +090078 val += ac;
Masahiro Yamada7b3a0322016-04-21 14:43:12 +090079 gd->bd->bi_dram[i].size = fdtdec_get_number(val, sc);
Masahiro Yamadaac2a1032016-03-29 20:18:45 +090080 val += sc;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090081
82 debug("DRAM bank %d: start = %08lx, size = %08lx\n",
Masahiro Yamada11d3ede2016-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 Yamadacf88aff2015-09-11 20:17:49 +090085 }
86}
Masahiro Yamada51ea5a02016-06-17 19:24:29 +090087
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;
122}
123#endif