blob: 9022679703b4079099c91de84880486964532042 [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>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Patrick Delaunay939d5362018-03-12 10:46:11 +010011#include <ram.h>
12#include <regmap.h>
13#include <syscon.h>
14#include <asm/io.h>
15#include "stm32mp1_ddr.h"
16
Patrick Delaunay939d5362018-03-12 10:46:11 +010017static const char *const clkname[] = {
18 "ddrc1",
19 "ddrc2",
20 "ddrcapb",
21 "ddrphycapb",
22 "ddrphyc" /* LAST clock => used for get_rate() */
23};
24
Patrick Delaunay29e1a942019-04-10 14:09:23 +020025int stm32mp1_ddr_clk_enable(struct ddr_info *priv, uint32_t mem_speed)
Patrick Delaunay939d5362018-03-12 10:46:11 +010026{
27 unsigned long ddrphy_clk;
28 unsigned long ddr_clk;
29 struct clk clk;
30 int ret;
Patrick Delaunay6abbd352019-06-21 15:26:51 +020031 unsigned int idx;
Patrick Delaunay939d5362018-03-12 10:46:11 +010032
33 for (idx = 0; idx < ARRAY_SIZE(clkname); idx++) {
34 ret = clk_get_by_name(priv->dev, clkname[idx], &clk);
35
36 if (!ret)
37 ret = clk_enable(&clk);
38
39 if (ret) {
40 printf("error for %s : %d\n", clkname[idx], ret);
41 return ret;
42 }
43 }
44
45 priv->clk = clk;
46 ddrphy_clk = clk_get_rate(&priv->clk);
47
Patrick Delaunay29e1a942019-04-10 14:09:23 +020048 debug("DDR: mem_speed (%d kHz), RCC %d kHz\n",
49 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunay939d5362018-03-12 10:46:11 +010050 /* max 10% frequency delta */
Patrick Delaunay29e1a942019-04-10 14:09:23 +020051 ddr_clk = abs(ddrphy_clk - mem_speed * 1000);
52 if (ddr_clk > (mem_speed * 100)) {
53 pr_err("DDR expected freq %d kHz, current is %d kHz\n",
54 mem_speed, (u32)(ddrphy_clk / 1000));
Patrick Delaunay939d5362018-03-12 10:46:11 +010055 return -EINVAL;
56 }
57
58 return 0;
59}
60
Marek Vasut697887a2020-04-22 13:18:12 +020061__weak int board_stm32mp1_ddr_config_name_match(struct udevice *dev,
62 const char *name)
63{
64 return 0; /* Always match */
65}
66
67static ofnode stm32mp1_ddr_get_ofnode(struct udevice *dev)
68{
69 const char *name;
70 ofnode node;
71
72 dev_for_each_subnode(node, dev) {
73 name = ofnode_get_property(node, "compatible", NULL);
74
75 if (!board_stm32mp1_ddr_config_name_match(dev, name))
76 return node;
77 }
78
79 return dev_ofnode(dev);
80}
81
Patrick Delaunay939d5362018-03-12 10:46:11 +010082static __maybe_unused int stm32mp1_ddr_setup(struct udevice *dev)
83{
84 struct ddr_info *priv = dev_get_priv(dev);
Patrick Delaunay6abbd352019-06-21 15:26:51 +020085 int ret;
86 unsigned int idx;
Patrick Delaunay939d5362018-03-12 10:46:11 +010087 struct clk axidcg;
88 struct stm32mp1_ddr_config config;
Marek Vasut697887a2020-04-22 13:18:12 +020089 ofnode node = stm32mp1_ddr_get_ofnode(dev);
Patrick Delaunay939d5362018-03-12 10:46:11 +010090
Patrick Delaunaya68e2d62020-03-06 11:14:11 +010091#define PARAM(x, y, z) \
92 { .name = x, \
93 .offset = offsetof(struct stm32mp1_ddr_config, y), \
94 .size = sizeof(config.y) / sizeof(u32), \
95 .present = z, \
96 }
Patrick Delaunay939d5362018-03-12 10:46:11 +010097
Patrick Delaunaya68e2d62020-03-06 11:14:11 +010098#define CTL_PARAM(x) PARAM("st,ctl-"#x, c_##x, NULL)
99#define PHY_PARAM(x) PARAM("st,phy-"#x, p_##x, NULL)
100#define PHY_PARAM_OPT(x) PARAM("st,phy-"#x, p_##x, &config.p_##x##_present)
Patrick Delaunay939d5362018-03-12 10:46:11 +0100101
102 const struct {
103 const char *name; /* name in DT */
104 const u32 offset; /* offset in config struct */
105 const u32 size; /* size of parameters */
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100106 bool * const present; /* presence indication for opt */
Patrick Delaunay939d5362018-03-12 10:46:11 +0100107 } param[] = {
108 CTL_PARAM(reg),
109 CTL_PARAM(timing),
110 CTL_PARAM(map),
111 CTL_PARAM(perf),
112 PHY_PARAM(reg),
113 PHY_PARAM(timing),
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100114 PHY_PARAM_OPT(cal)
Patrick Delaunay939d5362018-03-12 10:46:11 +0100115 };
116
Marek Vasut697887a2020-04-22 13:18:12 +0200117 config.info.speed = ofnode_read_u32_default(node, "st,mem-speed", 0);
118 config.info.size = ofnode_read_u32_default(node, "st,mem-size", 0);
119 config.info.name = ofnode_read_string(node, "st,mem-name");
Patrick Delaunay939d5362018-03-12 10:46:11 +0100120 if (!config.info.name) {
121 debug("%s: no st,mem-name\n", __func__);
122 return -EINVAL;
123 }
124 printf("RAM: %s\n", config.info.name);
125
126 for (idx = 0; idx < ARRAY_SIZE(param); idx++) {
Marek Vasut697887a2020-04-22 13:18:12 +0200127 ret = ofnode_read_u32_array(node, param[idx].name,
Patrick Delaunay939d5362018-03-12 10:46:11 +0100128 (void *)((u32)&config +
129 param[idx].offset),
130 param[idx].size);
131 debug("%s: %s[0x%x] = %d\n", __func__,
132 param[idx].name, param[idx].size, ret);
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100133 if (ret &&
134 (ret != -FDT_ERR_NOTFOUND || !param[idx].present)) {
Patrick Delaunayd892d272019-04-10 14:09:25 +0200135 pr_err("%s: Cannot read %s, error=%d\n",
136 __func__, param[idx].name, ret);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100137 return -EINVAL;
138 }
Patrick Delaunaya68e2d62020-03-06 11:14:11 +0100139 if (param[idx].present) {
140 /* save presence of optional parameters */
141 *param[idx].present = true;
142 if (ret == -FDT_ERR_NOTFOUND) {
143 *param[idx].present = false;
144#ifdef CONFIG_STM32MP1_DDR_INTERACTIVE
145 /* reset values if used later */
146 memset((void *)((u32)&config +
147 param[idx].offset),
148 0, param[idx].size * sizeof(u32));
149#endif
150 }
151 }
Patrick Delaunay939d5362018-03-12 10:46:11 +0100152 }
153
154 ret = clk_get_by_name(dev, "axidcg", &axidcg);
155 if (ret) {
156 debug("%s: Cannot found axidcg\n", __func__);
157 return -EINVAL;
158 }
159 clk_disable(&axidcg); /* disable clock gating during init */
160
161 stm32mp1_ddr_init(priv, &config);
162
163 clk_enable(&axidcg); /* enable clock gating */
164
165 /* check size */
166 debug("%s : get_ram_size(%x, %x)\n", __func__,
167 (u32)priv->info.base, (u32)STM32_DDR_SIZE);
168
169 priv->info.size = get_ram_size((long *)priv->info.base,
170 STM32_DDR_SIZE);
171
172 debug("%s : %x\n", __func__, (u32)priv->info.size);
173
174 /* check memory access for all memory */
175 if (config.info.size != priv->info.size) {
176 printf("DDR invalid size : 0x%x, expected 0x%x\n",
177 priv->info.size, config.info.size);
178 return -EINVAL;
179 }
180 return 0;
181}
182
183static int stm32mp1_ddr_probe(struct udevice *dev)
184{
185 struct ddr_info *priv = dev_get_priv(dev);
186 struct regmap *map;
187 int ret;
188
189 debug("STM32MP1 DDR probe\n");
190 priv->dev = dev;
191
Masahiro Yamadae4873e32018-04-19 12:14:03 +0900192 ret = regmap_init_mem(dev_ofnode(dev), &map);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100193 if (ret)
194 return ret;
195
196 priv->ctl = regmap_get_range(map, 0);
197 priv->phy = regmap_get_range(map, 1);
198
199 priv->rcc = STM32_RCC_BASE;
200
201 priv->info.base = STM32_DDR_BASE;
202
Patrick Delaunayf8fe21d2020-04-01 09:07:33 +0200203#if !defined(CONFIG_TFABOOT) && \
Patrick Delaunay5d061412019-02-12 11:44:39 +0100204 (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD))
Patrick Delaunay939d5362018-03-12 10:46:11 +0100205 priv->info.size = 0;
206 return stm32mp1_ddr_setup(dev);
207#else
Marek Vasut697887a2020-04-22 13:18:12 +0200208 ofnode node = stm32mp1_ddr_get_ofnode(dev);
209 priv->info.size = ofnode_read_u32_default(node, "st,mem-size", 0);
Patrick Delaunay939d5362018-03-12 10:46:11 +0100210 return 0;
211#endif
212}
213
214static int stm32mp1_ddr_get_info(struct udevice *dev, struct ram_info *info)
215{
216 struct ddr_info *priv = dev_get_priv(dev);
217
218 *info = priv->info;
219
220 return 0;
221}
222
223static struct ram_ops stm32mp1_ddr_ops = {
224 .get_info = stm32mp1_ddr_get_info,
225};
226
227static const struct udevice_id stm32mp1_ddr_ids[] = {
228 { .compatible = "st,stm32mp1-ddr" },
229 { }
230};
231
232U_BOOT_DRIVER(ddr_stm32mp1) = {
233 .name = "stm32mp1_ddr",
234 .id = UCLASS_RAM,
235 .of_match = stm32mp1_ddr_ids,
236 .ops = &stm32mp1_ddr_ops,
237 .probe = stm32mp1_ddr_probe,
238 .priv_auto_alloc_size = sizeof(struct ddr_info),
239};