Tom Rini | 4549e78 | 2018-05-06 18:27:01 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2018, STMicroelectronics - All Rights Reserved |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <clk.h> |
| 8 | #include <dm.h> |
| 9 | #include <ram.h> |
| 10 | #include <regmap.h> |
| 11 | #include <syscon.h> |
| 12 | #include <asm/io.h> |
| 13 | #include "stm32mp1_ddr.h" |
| 14 | |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 15 | static const char *const clkname[] = { |
| 16 | "ddrc1", |
| 17 | "ddrc2", |
| 18 | "ddrcapb", |
| 19 | "ddrphycapb", |
| 20 | "ddrphyc" /* LAST clock => used for get_rate() */ |
| 21 | }; |
| 22 | |
Patrick Delaunay | c60fed1 | 2019-04-10 14:09:23 +0200 | [diff] [blame] | 23 | int stm32mp1_ddr_clk_enable(struct ddr_info *priv, uint32_t mem_speed) |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 24 | { |
| 25 | unsigned long ddrphy_clk; |
| 26 | unsigned long ddr_clk; |
| 27 | struct clk clk; |
| 28 | int ret; |
Patrick Delaunay | 5884485 | 2019-06-21 15:26:51 +0200 | [diff] [blame] | 29 | unsigned int idx; |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 30 | |
| 31 | for (idx = 0; idx < ARRAY_SIZE(clkname); idx++) { |
| 32 | ret = clk_get_by_name(priv->dev, clkname[idx], &clk); |
| 33 | |
| 34 | if (!ret) |
| 35 | ret = clk_enable(&clk); |
| 36 | |
| 37 | if (ret) { |
| 38 | printf("error for %s : %d\n", clkname[idx], ret); |
| 39 | return ret; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | priv->clk = clk; |
| 44 | ddrphy_clk = clk_get_rate(&priv->clk); |
| 45 | |
Patrick Delaunay | c60fed1 | 2019-04-10 14:09:23 +0200 | [diff] [blame] | 46 | debug("DDR: mem_speed (%d kHz), RCC %d kHz\n", |
| 47 | mem_speed, (u32)(ddrphy_clk / 1000)); |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 48 | /* max 10% frequency delta */ |
Patrick Delaunay | c60fed1 | 2019-04-10 14:09:23 +0200 | [diff] [blame] | 49 | ddr_clk = abs(ddrphy_clk - mem_speed * 1000); |
| 50 | if (ddr_clk > (mem_speed * 100)) { |
| 51 | pr_err("DDR expected freq %d kHz, current is %d kHz\n", |
| 52 | mem_speed, (u32)(ddrphy_clk / 1000)); |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 53 | return -EINVAL; |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static __maybe_unused int stm32mp1_ddr_setup(struct udevice *dev) |
| 60 | { |
| 61 | struct ddr_info *priv = dev_get_priv(dev); |
Patrick Delaunay | 5884485 | 2019-06-21 15:26:51 +0200 | [diff] [blame] | 62 | int ret; |
| 63 | unsigned int idx; |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 64 | struct clk axidcg; |
| 65 | struct stm32mp1_ddr_config config; |
| 66 | |
| 67 | #define PARAM(x, y) \ |
| 68 | { x,\ |
| 69 | offsetof(struct stm32mp1_ddr_config, y),\ |
| 70 | sizeof(config.y) / sizeof(u32)} |
| 71 | |
| 72 | #define CTL_PARAM(x) PARAM("st,ctl-"#x, c_##x) |
| 73 | #define PHY_PARAM(x) PARAM("st,phy-"#x, p_##x) |
| 74 | |
| 75 | const struct { |
| 76 | const char *name; /* name in DT */ |
| 77 | const u32 offset; /* offset in config struct */ |
| 78 | const u32 size; /* size of parameters */ |
| 79 | } param[] = { |
| 80 | CTL_PARAM(reg), |
| 81 | CTL_PARAM(timing), |
| 82 | CTL_PARAM(map), |
| 83 | CTL_PARAM(perf), |
| 84 | PHY_PARAM(reg), |
| 85 | PHY_PARAM(timing), |
| 86 | PHY_PARAM(cal) |
| 87 | }; |
| 88 | |
| 89 | config.info.speed = dev_read_u32_default(dev, "st,mem-speed", 0); |
| 90 | config.info.size = dev_read_u32_default(dev, "st,mem-size", 0); |
| 91 | config.info.name = dev_read_string(dev, "st,mem-name"); |
| 92 | if (!config.info.name) { |
| 93 | debug("%s: no st,mem-name\n", __func__); |
| 94 | return -EINVAL; |
| 95 | } |
| 96 | printf("RAM: %s\n", config.info.name); |
| 97 | |
| 98 | for (idx = 0; idx < ARRAY_SIZE(param); idx++) { |
| 99 | ret = dev_read_u32_array(dev, param[idx].name, |
| 100 | (void *)((u32)&config + |
| 101 | param[idx].offset), |
| 102 | param[idx].size); |
| 103 | debug("%s: %s[0x%x] = %d\n", __func__, |
| 104 | param[idx].name, param[idx].size, ret); |
| 105 | if (ret) { |
Patrick Delaunay | 53bb831 | 2019-04-10 14:09:25 +0200 | [diff] [blame] | 106 | pr_err("%s: Cannot read %s, error=%d\n", |
| 107 | __func__, param[idx].name, ret); |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 108 | return -EINVAL; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | ret = clk_get_by_name(dev, "axidcg", &axidcg); |
| 113 | if (ret) { |
| 114 | debug("%s: Cannot found axidcg\n", __func__); |
| 115 | return -EINVAL; |
| 116 | } |
| 117 | clk_disable(&axidcg); /* disable clock gating during init */ |
| 118 | |
| 119 | stm32mp1_ddr_init(priv, &config); |
| 120 | |
| 121 | clk_enable(&axidcg); /* enable clock gating */ |
| 122 | |
| 123 | /* check size */ |
| 124 | debug("%s : get_ram_size(%x, %x)\n", __func__, |
| 125 | (u32)priv->info.base, (u32)STM32_DDR_SIZE); |
| 126 | |
| 127 | priv->info.size = get_ram_size((long *)priv->info.base, |
| 128 | STM32_DDR_SIZE); |
| 129 | |
| 130 | debug("%s : %x\n", __func__, (u32)priv->info.size); |
| 131 | |
| 132 | /* check memory access for all memory */ |
| 133 | if (config.info.size != priv->info.size) { |
| 134 | printf("DDR invalid size : 0x%x, expected 0x%x\n", |
| 135 | priv->info.size, config.info.size); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static int stm32mp1_ddr_probe(struct udevice *dev) |
| 142 | { |
| 143 | struct ddr_info *priv = dev_get_priv(dev); |
| 144 | struct regmap *map; |
| 145 | int ret; |
| 146 | |
| 147 | debug("STM32MP1 DDR probe\n"); |
| 148 | priv->dev = dev; |
| 149 | |
Masahiro Yamada | d358123 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 150 | ret = regmap_init_mem(dev_ofnode(dev), &map); |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | priv->ctl = regmap_get_range(map, 0); |
| 155 | priv->phy = regmap_get_range(map, 1); |
| 156 | |
| 157 | priv->rcc = STM32_RCC_BASE; |
| 158 | |
| 159 | priv->info.base = STM32_DDR_BASE; |
| 160 | |
Patrick Delaunay | abf2678 | 2019-02-12 11:44:39 +0100 | [diff] [blame] | 161 | #if !defined(CONFIG_STM32MP1_TRUSTED) && \ |
| 162 | (!defined(CONFIG_SPL) || defined(CONFIG_SPL_BUILD)) |
Patrick Delaunay | e70f70a | 2018-03-12 10:46:11 +0100 | [diff] [blame] | 163 | priv->info.size = 0; |
| 164 | return stm32mp1_ddr_setup(dev); |
| 165 | #else |
| 166 | priv->info.size = dev_read_u32_default(dev, "st,mem-size", 0); |
| 167 | return 0; |
| 168 | #endif |
| 169 | } |
| 170 | |
| 171 | static int stm32mp1_ddr_get_info(struct udevice *dev, struct ram_info *info) |
| 172 | { |
| 173 | struct ddr_info *priv = dev_get_priv(dev); |
| 174 | |
| 175 | *info = priv->info; |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static struct ram_ops stm32mp1_ddr_ops = { |
| 181 | .get_info = stm32mp1_ddr_get_info, |
| 182 | }; |
| 183 | |
| 184 | static const struct udevice_id stm32mp1_ddr_ids[] = { |
| 185 | { .compatible = "st,stm32mp1-ddr" }, |
| 186 | { } |
| 187 | }; |
| 188 | |
| 189 | U_BOOT_DRIVER(ddr_stm32mp1) = { |
| 190 | .name = "stm32mp1_ddr", |
| 191 | .id = UCLASS_RAM, |
| 192 | .of_match = stm32mp1_ddr_ids, |
| 193 | .ops = &stm32mp1_ddr_ops, |
| 194 | .probe = stm32mp1_ddr_probe, |
| 195 | .priv_auto_alloc_size = sizeof(struct ddr_info), |
| 196 | }; |