blob: 26f0b4f1eae1e2762b1244f17a7922b52a551ef9 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunaye70f70a2018-03-12 10:46:11 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunaye70f70a2018-03-12 10:46:11 +01004 */
5
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +01006#define LOG_CATEGORY UCLASS_RAM
7
Patrick Delaunaye70f70a2018-03-12 10:46:11 +01008#include <common.h>
9#include <clk.h>
10#include <dm.h>
Simon Glass9b4a2052019-12-28 10:45:05 -070011#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010013#include <ram.h>
14#include <regmap.h>
15#include <syscon.h>
16#include <asm/io.h>
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +010017#include <dm/device_compat.h>
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010018#include "stm32mp1_ddr.h"
19
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010020static const char *const clkname[] = {
21 "ddrc1",
22 "ddrc2",
23 "ddrcapb",
24 "ddrphycapb",
25 "ddrphyc" /* LAST clock => used for get_rate() */
26};
27
Patrick Delaunayc60fed12019-04-10 14:09:23 +020028int stm32mp1_ddr_clk_enable(struct ddr_info *priv, uint32_t mem_speed)
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010029{
30 unsigned long ddrphy_clk;
31 unsigned long ddr_clk;
32 struct clk clk;
33 int ret;
Patrick Delaunay58844852019-06-21 15:26:51 +020034 unsigned int idx;
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010035
36 for (idx = 0; idx < ARRAY_SIZE(clkname); idx++) {
37 ret = clk_get_by_name(priv->dev, clkname[idx], &clk);
38
39 if (!ret)
40 ret = clk_enable(&clk);
41
42 if (ret) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +010043 log_err("error for %s : %d\n", clkname[idx], ret);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010044 return ret;
45 }
46 }
47
48 priv->clk = clk;
49 ddrphy_clk = clk_get_rate(&priv->clk);
50
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +010051 log_debug("DDR: mem_speed (%d kHz), RCC %d kHz\n",
52 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010053 /* max 10% frequency delta */
Patrick Delaunayc60fed12019-04-10 14:09:23 +020054 ddr_clk = abs(ddrphy_clk - mem_speed * 1000);
55 if (ddr_clk > (mem_speed * 100)) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +010056 log_err("DDR expected freq %d kHz, current is %d kHz\n",
57 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010058 return -EINVAL;
59 }
60
61 return 0;
62}
63
Marek Vasut0c27c162020-04-22 13:18:12 +020064__weak int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
65 const char *name)
66{
67 return 0; /* Always match */
68}
69
70static ofnode stm32mp1_ddr_get_ofnode(struct udevice *dev)
71{
72 const char *name;
73 ofnode node;
74
75 dev_for_each_subnode(node, dev) {
76 name = ofnode_get_property(node, "compatible", NULL);
77
78 if (!board_stm32mp1_ddr_config_name_match(dev, name))
79 return node;
80 }
81
82 return dev_ofnode(dev);
83}
84
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010085static __maybe_unused int stm32mp1_ddr_setup(struct udevice *dev)
86{
87 struct ddr_info *priv = dev_get_priv(dev);
Patrick Delaunay58844852019-06-21 15:26:51 +020088 int ret;
89 unsigned int idx;
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010090 struct clk axidcg;
91 struct stm32mp1_ddr_config config;
Marek Vasut0c27c162020-04-22 13:18:12 +020092 ofnode node = stm32mp1_ddr_get_ofnode(dev);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +010093
Patrick Delaunay9368bdf2020-03-06 11:14:11 +010094#define PARAM(x, y, z) \
95 { .name = x, \
96 .offset = offsetof(struct stm32mp1_ddr_config, y), \
97 .size = sizeof(config.y) / sizeof(u32), \
98 .present = z, \
99 }
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100100
Patrick Delaunay9368bdf2020-03-06 11:14:11 +0100101#define CTL_PARAM(x) PARAM("st,ctl-"#x, c_##x, NULL)
102#define PHY_PARAM(x) PARAM("st,phy-"#x, p_##x, NULL)
103#define PHY_PARAM_OPT(x) PARAM("st,phy-"#x, p_##x, &config.p_##x##_present)
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100104
105 const struct {
106 const char *name; /* name in DT */
107 const u32 offset; /* offset in config struct */
108 const u32 size; /* size of parameters */
Patrick Delaunay9368bdf2020-03-06 11:14:11 +0100109 bool * const present; /* presence indication for opt */
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100110 } param[] = {
111 CTL_PARAM(reg),
112 CTL_PARAM(timing),
113 CTL_PARAM(map),
114 CTL_PARAM(perf),
115 PHY_PARAM(reg),
116 PHY_PARAM(timing),
Patrick Delaunay9368bdf2020-03-06 11:14:11 +0100117 PHY_PARAM_OPT(cal)
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100118 };
119
Marek Vasut0c27c162020-04-22 13:18:12 +0200120 config.info.speed = ofnode_read_u32_default(node, "st,mem-speed", 0);
121 config.info.size = ofnode_read_u32_default(node, "st,mem-size", 0);
122 config.info.name = ofnode_read_string(node, "st,mem-name");
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100123 if (!config.info.name) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100124 dev_dbg(dev, "no st,mem-name\n");
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100125 return -EINVAL;
126 }
127 printf("RAM: %s\n", config.info.name);
128
129 for (idx = 0; idx < ARRAY_SIZE(param); idx++) {
Marek Vasut0c27c162020-04-22 13:18:12 +0200130 ret = ofnode_read_u32_array(node, param[idx].name,
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100131 (void *)((u32)&config +
132 param[idx].offset),
133 param[idx].size);
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100134 dev_dbg(dev, "%s: %s[0x%x] = %d\n", __func__,
135 param[idx].name, param[idx].size, ret);
Patrick Delaunay9368bdf2020-03-06 11:14:11 +0100136 if (ret &&
137 (ret != -FDT_ERR_NOTFOUND || !param[idx].present)) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100138 dev_err(dev, "Cannot read %s, error=%d\n",
139 param[idx].name, ret);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100140 return -EINVAL;
141 }
Patrick Delaunay9368bdf2020-03-06 11:14:11 +0100142 if (param[idx].present) {
143 /* save presence of optional parameters */
144 *param[idx].present = true;
145 if (ret == -FDT_ERR_NOTFOUND) {
146 *param[idx].present = false;
147#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
148 /* reset values if used later */
149 memset((void *)((u32)&config +
150 param[idx].offset),
151 0, param[idx].size * sizeof(u32));
152#endif
153 }
154 }
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100155 }
156
157 ret = clk_get_by_name(dev, "axidcg", &axidcg);
158 if (ret) {
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100159 dev_dbg(dev, "%s: Cannot found axidcg\n", __func__);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100160 return -EINVAL;
161 }
162 clk_disable(&axidcg); /* disable clock gating during init */
163
164 stm32mp1_ddr_init(priv, &config);
165
166 clk_enable(&axidcg); /* enable clock gating */
167
168 /* check size */
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100169 dev_dbg(dev, "get_ram_size(%x, %x)\n",
170 (u32)priv->info.base, (u32)STM32_DDR_SIZE);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100171
172 priv->info.size = get_ram_size((long *)priv->info.base,
173 STM32_DDR_SIZE);
174
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100175 dev_dbg(dev, "info.size: %x\n", (u32)priv->info.size);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100176
177 /* check memory access for all memory */
178 if (config.info.size != priv->info.size) {
179 printf("DDR invalid size : 0x%x, expected 0x%x\n",
180 priv->info.size, config.info.size);
181 return -EINVAL;
182 }
183 return 0;
184}
185
186static int stm32mp1_ddr_probe(struct udevice *dev)
187{
188 struct ddr_info *priv = dev_get_priv(dev);
189 struct regmap *map;
190 int ret;
191
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100192 priv->dev = dev;
193
Masahiro Yamadad3581232018-04-19 12:14:03 +0900194 ret = regmap_init_mem(dev_ofnode(dev), &map);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100195 if (ret)
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100196 return log_ret(ret);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100197
198 priv->ctl = regmap_get_range(map, 0);
199 priv->phy = regmap_get_range(map, 1);
200
201 priv->rcc = STM32_RCC_BASE;
202
203 priv->info.base = STM32_DDR_BASE;
204
Patrick Delaunay654706b2020-04-01 09:07:33 +0200205#if !defined(CONFIG_TFABOOT) && \
Patrick Delaunayabf26782019-02-12 11:44:39 +0100206 (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100207 priv->info.size = 0;
Patrick Delaunay66b3b9d2020-11-06 19:01:36 +0100208 ret = stm32mp1_ddr_setup(dev);
209
210 return log_ret(ret);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100211#else
Marek Vasut0c27c162020-04-22 13:18:12 +0200212 ofnode node = stm32mp1_ddr_get_ofnode(dev);
213 priv->info.size = ofnode_read_u32_default(node, "st,mem-size", 0);
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100214 return 0;
215#endif
216}
217
218static int stm32mp1_ddr_get_info(struct udevice *dev, struct ram_info *info)
219{
220 struct ddr_info *priv = dev_get_priv(dev);
221
222 *info = priv->info;
223
224 return 0;
225}
226
227static struct ram_ops stm32mp1_ddr_ops = {
228 .get_info = stm32mp1_ddr_get_info,
229};
230
231static const struct udevice_id stm32mp1_ddr_ids[] = {
232 { .compatible = "st,stm32mp1-ddr" },
233 { }
234};
235
236U_BOOT_DRIVER(ddr_stm32mp1) = {
237 .name = "stm32mp1_ddr",
238 .id = UCLASS_RAM,
239 .of_match = stm32mp1_ddr_ids,
240 .ops = &stm32mp1_ddr_ops,
241 .probe = stm32mp1_ddr_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700242 .priv_auto = sizeof(struct ddr_info),
Patrick Delaunaye70f70a2018-03-12 10:46:11 +0100243};