Thomas Gleixner | 1621633 | 2019-05-19 15:51:31 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Generic on-chip SRAM allocation driver |
| 4 | * |
| 5 | * Copyright (C) 2012 Philipp Zabel, Pengutronix |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 8 | #include <linux/clk.h> |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 9 | #include <linux/delay.h> |
Vladimir Zapolskiy | 98ce2d2 | 2015-06-01 15:30:00 +0300 | [diff] [blame] | 10 | #include <linux/genalloc.h> |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 11 | #include <linux/io.h> |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 12 | #include <linux/list_sort.h> |
Vladimir Zapolskiy | 98ce2d2 | 2015-06-01 15:30:00 +0300 | [diff] [blame] | 13 | #include <linux/of_address.h> |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 14 | #include <linux/of_device.h> |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 16 | #include <linux/regmap.h> |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 17 | #include <linux/slab.h> |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 18 | #include <linux/mfd/syscon.h> |
| 19 | #include <soc/at91/atmel-secumod.h> |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 20 | |
Dave Gerlach | cdd1737 | 2017-01-12 14:52:18 -0600 | [diff] [blame] | 21 | #include "sram.h" |
| 22 | |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 23 | #define SRAM_GRANULARITY 32 |
| 24 | |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 25 | static ssize_t sram_read(struct file *filp, struct kobject *kobj, |
| 26 | struct bin_attribute *attr, |
| 27 | char *buf, loff_t pos, size_t count) |
| 28 | { |
| 29 | struct sram_partition *part; |
| 30 | |
| 31 | part = container_of(attr, struct sram_partition, battr); |
| 32 | |
| 33 | mutex_lock(&part->lock); |
Vladimir Zapolskiy | 525d12f | 2015-10-18 20:57:09 +0300 | [diff] [blame] | 34 | memcpy_fromio(buf, part->base + pos, count); |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 35 | mutex_unlock(&part->lock); |
| 36 | |
| 37 | return count; |
| 38 | } |
| 39 | |
| 40 | static ssize_t sram_write(struct file *filp, struct kobject *kobj, |
| 41 | struct bin_attribute *attr, |
| 42 | char *buf, loff_t pos, size_t count) |
| 43 | { |
| 44 | struct sram_partition *part; |
| 45 | |
| 46 | part = container_of(attr, struct sram_partition, battr); |
| 47 | |
| 48 | mutex_lock(&part->lock); |
Vladimir Zapolskiy | 525d12f | 2015-10-18 20:57:09 +0300 | [diff] [blame] | 49 | memcpy_toio(part->base + pos, buf, count); |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 50 | mutex_unlock(&part->lock); |
| 51 | |
| 52 | return count; |
| 53 | } |
| 54 | |
| 55 | static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block, |
| 56 | phys_addr_t start, struct sram_partition *part) |
| 57 | { |
| 58 | int ret; |
| 59 | |
| 60 | part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY), |
| 61 | NUMA_NO_NODE, block->label); |
| 62 | if (IS_ERR(part->pool)) |
| 63 | return PTR_ERR(part->pool); |
| 64 | |
| 65 | ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start, |
| 66 | block->size, NUMA_NO_NODE); |
| 67 | if (ret < 0) { |
| 68 | dev_err(sram->dev, "failed to register subpool: %d\n", ret); |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block, |
| 76 | phys_addr_t start, struct sram_partition *part) |
| 77 | { |
| 78 | sysfs_bin_attr_init(&part->battr); |
| 79 | part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL, |
| 80 | "%llx.sram", |
| 81 | (unsigned long long)start); |
| 82 | if (!part->battr.attr.name) |
| 83 | return -ENOMEM; |
| 84 | |
| 85 | part->battr.attr.mode = S_IRUSR | S_IWUSR; |
| 86 | part->battr.read = sram_read; |
| 87 | part->battr.write = sram_write; |
| 88 | part->battr.size = block->size; |
| 89 | |
| 90 | return device_create_bin_file(sram->dev, &part->battr); |
| 91 | } |
| 92 | |
| 93 | static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block, |
| 94 | phys_addr_t start) |
| 95 | { |
| 96 | int ret; |
| 97 | struct sram_partition *part = &sram->partition[sram->partitions]; |
| 98 | |
| 99 | mutex_init(&part->lock); |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 100 | |
| 101 | if (sram->config && sram->config->map_only_reserved) { |
| 102 | void __iomem *virt_base; |
| 103 | |
| 104 | if (sram->no_memory_wc) |
| 105 | virt_base = devm_ioremap_resource(sram->dev, &block->res); |
| 106 | else |
| 107 | virt_base = devm_ioremap_resource_wc(sram->dev, &block->res); |
| 108 | |
| 109 | if (IS_ERR(virt_base)) { |
| 110 | dev_err(sram->dev, "could not map SRAM at %pr\n", &block->res); |
| 111 | return PTR_ERR(virt_base); |
| 112 | } |
| 113 | |
| 114 | part->base = virt_base; |
| 115 | } else { |
| 116 | part->base = sram->virt_base + block->start; |
| 117 | } |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 118 | |
| 119 | if (block->pool) { |
| 120 | ret = sram_add_pool(sram, block, start, part); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | } |
| 124 | if (block->export) { |
| 125 | ret = sram_add_export(sram, block, start, part); |
| 126 | if (ret) |
| 127 | return ret; |
| 128 | } |
Dave Gerlach | 37afff0 | 2017-01-12 14:52:20 -0600 | [diff] [blame] | 129 | if (block->protect_exec) { |
| 130 | ret = sram_check_protect_exec(sram, block, part); |
| 131 | if (ret) |
| 132 | return ret; |
| 133 | |
| 134 | ret = sram_add_pool(sram, block, start, part); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | |
| 138 | sram_add_protect_exec(part); |
| 139 | } |
| 140 | |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 141 | sram->partitions++; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | static void sram_free_partitions(struct sram_dev *sram) |
| 147 | { |
| 148 | struct sram_partition *part; |
| 149 | |
| 150 | if (!sram->partitions) |
| 151 | return; |
| 152 | |
| 153 | part = &sram->partition[sram->partitions - 1]; |
| 154 | for (; sram->partitions; sram->partitions--, part--) { |
| 155 | if (part->battr.size) |
| 156 | device_remove_bin_file(sram->dev, &part->battr); |
| 157 | |
| 158 | if (part->pool && |
| 159 | gen_pool_avail(part->pool) < gen_pool_size(part->pool)) |
| 160 | dev_err(sram->dev, "removed pool while SRAM allocated\n"); |
| 161 | } |
| 162 | } |
| 163 | |
Sami Tolvanen | 4f0f586 | 2021-04-08 11:28:34 -0700 | [diff] [blame] | 164 | static int sram_reserve_cmp(void *priv, const struct list_head *a, |
| 165 | const struct list_head *b) |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 166 | { |
| 167 | struct sram_reserve *ra = list_entry(a, struct sram_reserve, list); |
| 168 | struct sram_reserve *rb = list_entry(b, struct sram_reserve, list); |
| 169 | |
| 170 | return ra->start - rb->start; |
| 171 | } |
| 172 | |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 173 | static int sram_reserve_regions(struct sram_dev *sram, struct resource *res) |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 174 | { |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 175 | struct device_node *np = sram->dev->of_node, *child; |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 176 | unsigned long size, cur_start, cur_size; |
| 177 | struct sram_reserve *rblocks, *block; |
| 178 | struct list_head reserve_list; |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 179 | unsigned int nblocks, exports = 0; |
| 180 | const char *label; |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 181 | int ret = 0; |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 182 | |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 183 | INIT_LIST_HEAD(&reserve_list); |
| 184 | |
Alexander Shiyan | f3cbfa5 | 2013-06-10 18:43:53 +0400 | [diff] [blame] | 185 | size = resource_size(res); |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 186 | |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 187 | /* |
| 188 | * We need an additional block to mark the end of the memory region |
| 189 | * after the reserved blocks from the dt are processed. |
| 190 | */ |
| 191 | nblocks = (np) ? of_get_available_child_count(np) + 1 : 1; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 192 | rblocks = kcalloc(nblocks, sizeof(*rblocks), GFP_KERNEL); |
Vladimir Zapolskiy | ee895cc | 2015-06-01 15:29:54 +0300 | [diff] [blame] | 193 | if (!rblocks) |
| 194 | return -ENOMEM; |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 195 | |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 196 | block = &rblocks[0]; |
| 197 | for_each_available_child_of_node(np, child) { |
| 198 | struct resource child_res; |
| 199 | |
| 200 | ret = of_address_to_resource(child, 0, &child_res); |
| 201 | if (ret < 0) { |
Vladimir Zapolskiy | 665d82f | 2015-06-01 15:29:58 +0300 | [diff] [blame] | 202 | dev_err(sram->dev, |
Rob Herring | 34d0eb5 | 2017-07-18 16:43:15 -0500 | [diff] [blame] | 203 | "could not get address for node %pOF\n", |
| 204 | child); |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 205 | goto err_chunks; |
| 206 | } |
| 207 | |
| 208 | if (child_res.start < res->start || child_res.end > res->end) { |
Vladimir Zapolskiy | 665d82f | 2015-06-01 15:29:58 +0300 | [diff] [blame] | 209 | dev_err(sram->dev, |
Rob Herring | 34d0eb5 | 2017-07-18 16:43:15 -0500 | [diff] [blame] | 210 | "reserved block %pOF outside the sram area\n", |
| 211 | child); |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 212 | ret = -EINVAL; |
| 213 | goto err_chunks; |
| 214 | } |
| 215 | |
| 216 | block->start = child_res.start - res->start; |
| 217 | block->size = resource_size(&child_res); |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 218 | block->res = child_res; |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 219 | list_add_tail(&block->list, &reserve_list); |
| 220 | |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 221 | if (of_find_property(child, "export", NULL)) |
| 222 | block->export = true; |
| 223 | |
| 224 | if (of_find_property(child, "pool", NULL)) |
| 225 | block->pool = true; |
| 226 | |
Dave Gerlach | 37afff0 | 2017-01-12 14:52:20 -0600 | [diff] [blame] | 227 | if (of_find_property(child, "protect-exec", NULL)) |
| 228 | block->protect_exec = true; |
| 229 | |
| 230 | if ((block->export || block->pool || block->protect_exec) && |
| 231 | block->size) { |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 232 | exports++; |
| 233 | |
| 234 | label = NULL; |
| 235 | ret = of_property_read_string(child, "label", &label); |
| 236 | if (ret && ret != -EINVAL) { |
| 237 | dev_err(sram->dev, |
Rob Herring | 34d0eb5 | 2017-07-18 16:43:15 -0500 | [diff] [blame] | 238 | "%pOF has invalid label name\n", |
| 239 | child); |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 240 | goto err_chunks; |
| 241 | } |
| 242 | if (!label) |
| 243 | label = child->name; |
| 244 | |
| 245 | block->label = devm_kstrdup(sram->dev, |
| 246 | label, GFP_KERNEL); |
Pan Bian | ddc5c9a | 2016-12-03 17:29:28 +0800 | [diff] [blame] | 247 | if (!block->label) { |
| 248 | ret = -ENOMEM; |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 249 | goto err_chunks; |
Pan Bian | ddc5c9a | 2016-12-03 17:29:28 +0800 | [diff] [blame] | 250 | } |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 251 | |
| 252 | dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n", |
| 253 | block->export ? "exported " : "", block->label, |
| 254 | block->start, block->start + block->size); |
| 255 | } else { |
| 256 | dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n", |
| 257 | block->start, block->start + block->size); |
| 258 | } |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 259 | |
| 260 | block++; |
| 261 | } |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 262 | child = NULL; |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 263 | |
| 264 | /* the last chunk marks the end of the region */ |
| 265 | rblocks[nblocks - 1].start = size; |
| 266 | rblocks[nblocks - 1].size = 0; |
| 267 | list_add_tail(&rblocks[nblocks - 1].list, &reserve_list); |
| 268 | |
| 269 | list_sort(NULL, &reserve_list, sram_reserve_cmp); |
| 270 | |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 271 | if (exports) { |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 272 | sram->partition = devm_kcalloc(sram->dev, |
| 273 | exports, sizeof(*sram->partition), |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 274 | GFP_KERNEL); |
| 275 | if (!sram->partition) { |
| 276 | ret = -ENOMEM; |
| 277 | goto err_chunks; |
| 278 | } |
| 279 | } |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 280 | |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 281 | cur_start = 0; |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 282 | list_for_each_entry(block, &reserve_list, list) { |
| 283 | /* can only happen if sections overlap */ |
| 284 | if (block->start < cur_start) { |
Vladimir Zapolskiy | 665d82f | 2015-06-01 15:29:58 +0300 | [diff] [blame] | 285 | dev_err(sram->dev, |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 286 | "block at 0x%x starts after current offset 0x%lx\n", |
| 287 | block->start, cur_start); |
| 288 | ret = -EINVAL; |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 289 | sram_free_partitions(sram); |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 290 | goto err_chunks; |
| 291 | } |
| 292 | |
Dave Gerlach | 37afff0 | 2017-01-12 14:52:20 -0600 | [diff] [blame] | 293 | if ((block->export || block->pool || block->protect_exec) && |
| 294 | block->size) { |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 295 | ret = sram_add_partition(sram, block, |
| 296 | res->start + block->start); |
| 297 | if (ret) { |
| 298 | sram_free_partitions(sram); |
| 299 | goto err_chunks; |
| 300 | } |
| 301 | } |
| 302 | |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 303 | /* current start is in a reserved block, so continue after it */ |
| 304 | if (block->start == cur_start) { |
| 305 | cur_start = block->start + block->size; |
| 306 | continue; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * allocate the space between the current starting |
| 311 | * address and the following reserved block, or the |
| 312 | * end of the region. |
| 313 | */ |
| 314 | cur_size = block->start - cur_start; |
| 315 | |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 316 | if (sram->pool) { |
| 317 | dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n", |
| 318 | cur_start, cur_start + cur_size); |
Vladimir Zapolskiy | 665d82f | 2015-06-01 15:29:58 +0300 | [diff] [blame] | 319 | |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 320 | ret = gen_pool_add_virt(sram->pool, |
| 321 | (unsigned long)sram->virt_base + cur_start, |
| 322 | res->start + cur_start, cur_size, -1); |
| 323 | if (ret < 0) { |
| 324 | sram_free_partitions(sram); |
| 325 | goto err_chunks; |
| 326 | } |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 327 | } |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 328 | |
| 329 | /* next allocation after this reserved block */ |
| 330 | cur_start = block->start + block->size; |
| 331 | } |
| 332 | |
zhong jiang | 3104389 | 2018-09-16 21:08:45 +0800 | [diff] [blame] | 333 | err_chunks: |
| 334 | of_node_put(child); |
Heiko Stübner | 2da1968 | 2014-02-25 12:46:34 +0100 | [diff] [blame] | 335 | kfree(rblocks); |
| 336 | |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 337 | return ret; |
| 338 | } |
| 339 | |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 340 | static int atmel_securam_wait(void) |
| 341 | { |
| 342 | struct regmap *regmap; |
| 343 | u32 val; |
| 344 | |
| 345 | regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod"); |
| 346 | if (IS_ERR(regmap)) |
| 347 | return -ENODEV; |
| 348 | |
| 349 | return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val, |
| 350 | val & AT91_SECUMOD_RAMRDY_READY, |
| 351 | 10000, 500000); |
| 352 | } |
| 353 | |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 354 | static const struct sram_config atmel_securam_config = { |
| 355 | .init = atmel_securam_wait, |
| 356 | }; |
| 357 | |
| 358 | /* |
| 359 | * SYSRAM contains areas that are not accessible by the |
| 360 | * kernel, such as the first 256K that is reserved for TZ. |
| 361 | * Accesses to those areas (including speculative accesses) |
| 362 | * trigger SErrors. As such we must map only the areas of |
| 363 | * SYSRAM specified in the device tree. |
| 364 | */ |
| 365 | static const struct sram_config tegra_sysram_config = { |
| 366 | .map_only_reserved = true, |
| 367 | }; |
| 368 | |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 369 | static const struct of_device_id sram_dt_ids[] = { |
| 370 | { .compatible = "mmio-sram" }, |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 371 | { .compatible = "atmel,sama5d2-securam", .data = &atmel_securam_config }, |
| 372 | { .compatible = "nvidia,tegra186-sysram", .data = &tegra_sysram_config }, |
| 373 | { .compatible = "nvidia,tegra194-sysram", .data = &tegra_sysram_config }, |
Mikko Perttunen | 2925fc1 | 2021-12-08 15:05:41 +0100 | [diff] [blame] | 374 | { .compatible = "nvidia,tegra234-sysram", .data = &tegra_sysram_config }, |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 375 | {} |
| 376 | }; |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 377 | |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 378 | static int sram_probe(struct platform_device *pdev) |
| 379 | { |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 380 | const struct sram_config *config; |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 381 | struct sram_dev *sram; |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 382 | int ret; |
Uwe Kleine-König | 39b27e8 | 2021-05-25 12:37:11 +0200 | [diff] [blame] | 383 | struct resource *res; |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 384 | |
| 385 | config = of_device_get_match_data(&pdev->dev); |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 386 | |
| 387 | sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL); |
| 388 | if (!sram) |
| 389 | return -ENOMEM; |
| 390 | |
| 391 | sram->dev = &pdev->dev; |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 392 | sram->no_memory_wc = of_property_read_bool(pdev->dev.of_node, "no-memory-wc"); |
| 393 | sram->config = config; |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 394 | |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 395 | if (!config || !config->map_only_reserved) { |
| 396 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 397 | if (sram->no_memory_wc) |
| 398 | sram->virt_base = devm_ioremap_resource(&pdev->dev, res); |
| 399 | else |
| 400 | sram->virt_base = devm_ioremap_resource_wc(&pdev->dev, res); |
| 401 | if (IS_ERR(sram->virt_base)) { |
| 402 | dev_err(&pdev->dev, "could not map SRAM registers\n"); |
| 403 | return PTR_ERR(sram->virt_base); |
| 404 | } |
| 405 | |
| 406 | sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY), |
| 407 | NUMA_NO_NODE, NULL); |
| 408 | if (IS_ERR(sram->pool)) |
| 409 | return PTR_ERR(sram->pool); |
Bartosz Golaszewski | 444b011 | 2019-10-22 10:43:15 +0200 | [diff] [blame] | 410 | } |
Vladimir Zapolskiy | a0a5be0 | 2015-06-01 15:29:59 +0300 | [diff] [blame] | 411 | |
Vladimir Zapolskiy | 665d82f | 2015-06-01 15:29:58 +0300 | [diff] [blame] | 412 | sram->clk = devm_clk_get(sram->dev, NULL); |
Vladimir Zapolskiy | ee895cc | 2015-06-01 15:29:54 +0300 | [diff] [blame] | 413 | if (IS_ERR(sram->clk)) |
| 414 | sram->clk = NULL; |
| 415 | else |
| 416 | clk_prepare_enable(sram->clk); |
| 417 | |
Bartosz Golaszewski | 444b011 | 2019-10-22 10:43:15 +0200 | [diff] [blame] | 418 | ret = sram_reserve_regions(sram, |
| 419 | platform_get_resource(pdev, IORESOURCE_MEM, 0)); |
Johan Hovold | d5b9653 | 2018-07-03 12:05:48 +0200 | [diff] [blame] | 420 | if (ret) |
| 421 | goto err_disable_clk; |
| 422 | |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 423 | platform_set_drvdata(pdev, sram); |
| 424 | |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 425 | if (config && config->init) { |
| 426 | ret = config->init(); |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 427 | if (ret) |
Johan Hovold | d5b9653 | 2018-07-03 12:05:48 +0200 | [diff] [blame] | 428 | goto err_free_partitions; |
Alexandre Belloni | 2ae2e28 | 2016-09-22 00:09:38 +0200 | [diff] [blame] | 429 | } |
| 430 | |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 431 | if (sram->pool) |
| 432 | dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n", |
| 433 | gen_pool_size(sram->pool) / 1024, sram->virt_base); |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 434 | |
| 435 | return 0; |
Johan Hovold | f294d00 | 2018-07-03 12:05:47 +0200 | [diff] [blame] | 436 | |
Johan Hovold | d5b9653 | 2018-07-03 12:05:48 +0200 | [diff] [blame] | 437 | err_free_partitions: |
| 438 | sram_free_partitions(sram); |
Johan Hovold | f294d00 | 2018-07-03 12:05:47 +0200 | [diff] [blame] | 439 | err_disable_clk: |
| 440 | if (sram->clk) |
| 441 | clk_disable_unprepare(sram->clk); |
Johan Hovold | f294d00 | 2018-07-03 12:05:47 +0200 | [diff] [blame] | 442 | |
| 443 | return ret; |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static int sram_remove(struct platform_device *pdev) |
| 447 | { |
| 448 | struct sram_dev *sram = platform_get_drvdata(pdev); |
| 449 | |
Vladimir Zapolskiy | b4c3fcb | 2015-09-21 23:52:46 +0300 | [diff] [blame] | 450 | sram_free_partitions(sram); |
| 451 | |
Mikko Perttunen | fec29bf | 2021-07-15 13:34:23 +0300 | [diff] [blame] | 452 | if (sram->pool && gen_pool_avail(sram->pool) < gen_pool_size(sram->pool)) |
Vladimir Zapolskiy | 665d82f | 2015-06-01 15:29:58 +0300 | [diff] [blame] | 453 | dev_err(sram->dev, "removed while SRAM allocated\n"); |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 454 | |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 455 | if (sram->clk) |
| 456 | clk_disable_unprepare(sram->clk); |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 461 | static struct platform_driver sram_driver = { |
| 462 | .driver = { |
| 463 | .name = "sram", |
Arnd Bergmann | 2aa488a | 2016-11-22 15:30:54 +0100 | [diff] [blame] | 464 | .of_match_table = sram_dt_ids, |
Philipp Zabel | 4984c6f | 2013-04-29 16:17:12 -0700 | [diff] [blame] | 465 | }, |
| 466 | .probe = sram_probe, |
| 467 | .remove = sram_remove, |
| 468 | }; |
| 469 | |
| 470 | static int __init sram_init(void) |
| 471 | { |
| 472 | return platform_driver_register(&sram_driver); |
| 473 | } |
| 474 | |
| 475 | postcore_initcall(sram_init); |