blob: 7b1adc5b2433f26a21ce6e0f529464a0d2ca10cd [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Patrick Delaunay939d5362018-03-12 10:46:11 +01002/*
3 * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
Patrick Delaunay939d5362018-03-12 10:46:11 +01004 */
5
6#include <common.h>
7#include <clk.h>
8#include <dm.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -07009#include <init.h>
Patrick Delaunay939d5362018-03-12 10:46:11 +010010#include <ram.h>
11#include <regmap.h>
12#include <syscon.h>
13#include <asm/io.h>
14#include "stm32mp1_ddr.h"
15
Patrick Delaunay939d5362018-03-12 10:46:11 +010016static const char *const clkname[] = {
17 "ddrc1",
18 "ddrc2",
19 "ddrcapb",
20 "ddrphycapb",
21 "ddrphyc" /* LAST clock => used for get_rate() */
22};
23
Patrick Delaunay29e1a942019-04-10 14:09:23 +020024int stm32mp1_ddr_clk_enable(struct ddr_info *priv, uint32_t mem_speed)
Patrick Delaunay939d5362018-03-12 10:46:11 +010025{
26 unsigned long ddrphy_clk;
27 unsigned long ddr_clk;
28 struct clk clk;
29 int ret;
Patrick Delaunay6abbd352019-06-21 15:26:51 +020030 unsigned int idx;
Patrick Delaunay939d5362018-03-12 10:46:11 +010031
32 for (idx = 0; idx < ARRAY_SIZE(clkname); idx++) {
33 ret = clk_get_by_name(priv->dev, clkname[idx], &clk);
34
35 if (!ret)
36 ret = clk_enable(&clk);
37
38 if (ret) {
39 printf("error for %s : %d\n", clkname[idx], ret);
40 return ret;
41 }
42 }
43
44 priv->clk = clk;
45 ddrphy_clk = clk_get_rate(&priv->clk);
46
Patrick Delaunay29e1a942019-04-10 14:09:23 +020047 debug("DDR: mem_speed (%d kHz), RCC %d kHz\n",
48 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunay939d5362018-03-12 10:46:11 +010049 /* max 10% frequency delta */
Patrick Delaunay29e1a942019-04-10 14:09:23 +020050 ddr_clk = abs(ddrphy_clk - mem_speed * 1000);
51 if (ddr_clk > (mem_speed * 100)) {
52 pr_err("DDR expected freq %d kHz, current is %d kHz\n",
53 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunay939d5362018-03-12 10:46:11 +010054 return -EINVAL;
55 }
56
57 return 0;
58}
59
60static __maybe_unused int stm32mp1_ddr_setup(struct udevice *dev)
61{
62 struct ddr_info *priv = dev_get_priv(dev);
Patrick Delaunay6abbd352019-06-21 15:26:51 +020063 int ret;
64 unsigned int idx;
Patrick Delaunay939d5362018-03-12 10:46:11 +010065 struct clk axidcg;
66 struct stm32mp1_ddr_config config;
67
Patrick Delaunaya68e2d62020-03-06 11:14:11 +010068#define PARAM(x, y, z) \
69 { .name = x, \
70 .offset = offsetof(struct stm32mp1_ddr_config, y), \
71 .size = sizeof(config.y) / sizeof(u32), \
72 .present = z, \
73 }
Patrick Delaunay939d5362018-03-12 10:46:11 +010074
Patrick Delaunaya68e2d62020-03-06 11:14:11 +010075#define CTL_PARAM(x) PARAM("st,ctl-"#x, c_##x, NULL)
76#define PHY_PARAM(x) PARAM("st,phy-"#x, p_##x, NULL)
77#define PHY_PARAM_OPT(x) PARAM("st,phy-"#x, p_##x, &config.p_##x##_present)
Patrick Delaunay939d5362018-03-12 10:46:11 +010078
79 const struct {
80 const char *name; /* name in DT */
81 const u32 offset; /* offset in config struct */
82 const u32 size; /* size of parameters */
Patrick Delaunaya68e2d62020-03-06 11:14:11 +010083 bool * const present; /* presence indication for opt */
Patrick Delaunay939d5362018-03-12 10:46:11 +010084 } param[] = {
85 CTL_PARAM(reg),
86 CTL_PARAM(timing),
87 CTL_PARAM(map),
88 CTL_PARAM(perf),
89 PHY_PARAM(reg),
90 PHY_PARAM(timing),
Patrick Delaunaya68e2d62020-03-06 11:14:11 +010091 PHY_PARAM_OPT(cal)
Patrick Delaunay939d5362018-03-12 10:46:11 +010092 };
93
94 config.info.speed = dev_read_u32_default(dev, "st,mem-speed", 0);
95 config.info.size = dev_read_u32_default(dev, "st,mem-size", 0);
96 config.info.name = dev_read_string(dev, "st,mem-name");
97 if (!config.info.name) {
98 debug("%s: no st,mem-name\n", __func__);
99 return -EINVAL;
100 }
101 printf("RAM: %s\n", config.info.name);
102
103 for (idx = 0; idx < ARRAY_SIZE(param); idx++) {
104 ret = dev_read_u32_array(dev, param[idx].name,
105 (void *)((u32)&config +
106 param[idx].offset),
107 param[idx].size);
108 debug("%s: %s[0x%x] = %d\n", __func__,
109 param[idx].name, param[idx].size, ret);
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100110 if (ret &&
111 (ret != -FDT_ERR_NOTFOUND || !param[idx].present)) {
Patrick Delaunayd892d272019-04-10 14:09:25 +0200112 pr_err("%s: Cannot read %s, error=%d\n",
113 __func__, param[idx].name, ret);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100114 return -EINVAL;
115 }
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100116 if (param[idx].present) {
117 /* save presence of optional parameters */
118 *param[idx].present = true;
119 if (ret == -FDT_ERR_NOTFOUND) {
120 *param[idx].present = false;
121#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
122 /* reset values if used later */
123 memset((void *)((u32)&config +
124 param[idx].offset),
125 0, param[idx].size * sizeof(u32));
126#endif
127 }
128 }
Patrick Delaunay939d5362018-03-12 10:46:11 +0100129 }
130
131 ret = clk_get_by_name(dev, "axidcg", &axidcg);
132 if (ret) {
133 debug("%s: Cannot found axidcg\n", __func__);
134 return -EINVAL;
135 }
136 clk_disable(&axidcg); /* disable clock gating during init */
137
138 stm32mp1_ddr_init(priv, &config);
139
140 clk_enable(&axidcg); /* enable clock gating */
141
142 /* check size */
143 debug("%s : get_ram_size(%x, %x)\n", __func__,
144 (u32)priv->info.base, (u32)STM32_DDR_SIZE);
145
146 priv->info.size = get_ram_size((long *)priv->info.base,
147 STM32_DDR_SIZE);
148
149 debug("%s : %x\n", __func__, (u32)priv->info.size);
150
151 /* check memory access for all memory */
152 if (config.info.size != priv->info.size) {
153 printf("DDR invalid size : 0x%x, expected 0x%x\n",
154 priv->info.size, config.info.size);
155 return -EINVAL;
156 }
157 return 0;
158}
159
160static int stm32mp1_ddr_probe(struct udevice *dev)
161{
162 struct ddr_info *priv = dev_get_priv(dev);
163 struct regmap *map;
164 int ret;
165
166 debug("STM32MP1 DDR probe\n");
167 priv->dev = dev;
168
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900169 ret = regmap_init_mem(dev_ofnode(dev), &map);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100170 if (ret)
171 return ret;
172
173 priv->ctl = regmap_get_range(map, 0);
174 priv->phy = regmap_get_range(map, 1);
175
176 priv->rcc = STM32_RCC_BASE;
177
178 priv->info.base = STM32_DDR_BASE;
179
Patrick Delaunayf8fe21d2020-04-01 09:07:33 +0200180#if !defined(CONFIG_TFABOOT) && \
Patrick Delaunay5d061412019-02-12 11:44:39 +0100181 (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
Patrick Delaunay939d5362018-03-12 10:46:11 +0100182 priv->info.size = 0;
183 return stm32mp1_ddr_setup(dev);
184#else
185 priv->info.size = dev_read_u32_default(dev, "st,mem-size", 0);
186 return 0;
187#endif
188}
189
190static int stm32mp1_ddr_get_info(struct udevice *dev, struct ram_info *info)
191{
192 struct ddr_info *priv = dev_get_priv(dev);
193
194 *info = priv->info;
195
196 return 0;
197}
198
199static struct ram_ops stm32mp1_ddr_ops = {
200 .get_info = stm32mp1_ddr_get_info,
201};
202
203static const struct udevice_id stm32mp1_ddr_ids[] = {
204 { .compatible = "st,stm32mp1-ddr" },
205 { }
206};
207
208U_BOOT_DRIVER(ddr_stm32mp1) = {
209 .name = "stm32mp1_ddr",
210 .id = UCLASS_RAM,
211 .of_match = stm32mp1_ddr_ids,
212 .ops = &stm32mp1_ddr_ops,
213 .probe = stm32mp1_ddr_probe,
214 .priv_auto_alloc_size = sizeof(struct ddr_info),
215};