116216333SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
24984c6f5SPhilipp Zabel /*
34984c6f5SPhilipp Zabel * Generic on-chip SRAM allocation driver
44984c6f5SPhilipp Zabel *
54984c6f5SPhilipp Zabel * Copyright (C) 2012 Philipp Zabel, Pengutronix
64984c6f5SPhilipp Zabel */
74984c6f5SPhilipp Zabel
84984c6f5SPhilipp Zabel #include <linux/clk.h>
92ae2e288SAlexandre Belloni #include <linux/delay.h>
1098ce2d27SVladimir Zapolskiy #include <linux/genalloc.h>
114984c6f5SPhilipp Zabel #include <linux/io.h>
122da19688SHeiko Stübner #include <linux/list_sort.h>
13*d9c58aebSRob Herring #include <linux/of.h>
1498ce2d27SVladimir Zapolskiy #include <linux/of_address.h>
154984c6f5SPhilipp Zabel #include <linux/platform_device.h>
162ae2e288SAlexandre Belloni #include <linux/regmap.h>
174984c6f5SPhilipp Zabel #include <linux/slab.h>
182ae2e288SAlexandre Belloni #include <linux/mfd/syscon.h>
192ae2e288SAlexandre Belloni #include <soc/at91/atmel-secumod.h>
204984c6f5SPhilipp Zabel
21cdd1737cSDave Gerlach #include "sram.h"
22cdd1737cSDave Gerlach
234984c6f5SPhilipp Zabel #define SRAM_GRANULARITY 32
244984c6f5SPhilipp Zabel
sram_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t pos,size_t count)25b4c3fcb3SVladimir Zapolskiy static ssize_t sram_read(struct file *filp, struct kobject *kobj,
26b4c3fcb3SVladimir Zapolskiy struct bin_attribute *attr,
27b4c3fcb3SVladimir Zapolskiy char *buf, loff_t pos, size_t count)
28b4c3fcb3SVladimir Zapolskiy {
29b4c3fcb3SVladimir Zapolskiy struct sram_partition *part;
30b4c3fcb3SVladimir Zapolskiy
31b4c3fcb3SVladimir Zapolskiy part = container_of(attr, struct sram_partition, battr);
32b4c3fcb3SVladimir Zapolskiy
33b4c3fcb3SVladimir Zapolskiy mutex_lock(&part->lock);
34525d12f2SVladimir Zapolskiy memcpy_fromio(buf, part->base + pos, count);
35b4c3fcb3SVladimir Zapolskiy mutex_unlock(&part->lock);
36b4c3fcb3SVladimir Zapolskiy
37b4c3fcb3SVladimir Zapolskiy return count;
38b4c3fcb3SVladimir Zapolskiy }
39b4c3fcb3SVladimir Zapolskiy
sram_write(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t pos,size_t count)40b4c3fcb3SVladimir Zapolskiy static ssize_t sram_write(struct file *filp, struct kobject *kobj,
41b4c3fcb3SVladimir Zapolskiy struct bin_attribute *attr,
42b4c3fcb3SVladimir Zapolskiy char *buf, loff_t pos, size_t count)
43b4c3fcb3SVladimir Zapolskiy {
44b4c3fcb3SVladimir Zapolskiy struct sram_partition *part;
45b4c3fcb3SVladimir Zapolskiy
46b4c3fcb3SVladimir Zapolskiy part = container_of(attr, struct sram_partition, battr);
47b4c3fcb3SVladimir Zapolskiy
48b4c3fcb3SVladimir Zapolskiy mutex_lock(&part->lock);
49525d12f2SVladimir Zapolskiy memcpy_toio(part->base + pos, buf, count);
50b4c3fcb3SVladimir Zapolskiy mutex_unlock(&part->lock);
51b4c3fcb3SVladimir Zapolskiy
52b4c3fcb3SVladimir Zapolskiy return count;
53b4c3fcb3SVladimir Zapolskiy }
54b4c3fcb3SVladimir Zapolskiy
sram_add_pool(struct sram_dev * sram,struct sram_reserve * block,phys_addr_t start,struct sram_partition * part)55b4c3fcb3SVladimir Zapolskiy static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block,
56b4c3fcb3SVladimir Zapolskiy phys_addr_t start, struct sram_partition *part)
57b4c3fcb3SVladimir Zapolskiy {
58b4c3fcb3SVladimir Zapolskiy int ret;
59b4c3fcb3SVladimir Zapolskiy
60b4c3fcb3SVladimir Zapolskiy part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
61b4c3fcb3SVladimir Zapolskiy NUMA_NO_NODE, block->label);
62b4c3fcb3SVladimir Zapolskiy if (IS_ERR(part->pool))
63b4c3fcb3SVladimir Zapolskiy return PTR_ERR(part->pool);
64b4c3fcb3SVladimir Zapolskiy
65b4c3fcb3SVladimir Zapolskiy ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start,
66b4c3fcb3SVladimir Zapolskiy block->size, NUMA_NO_NODE);
67b4c3fcb3SVladimir Zapolskiy if (ret < 0) {
68b4c3fcb3SVladimir Zapolskiy dev_err(sram->dev, "failed to register subpool: %d\n", ret);
69b4c3fcb3SVladimir Zapolskiy return ret;
70b4c3fcb3SVladimir Zapolskiy }
71b4c3fcb3SVladimir Zapolskiy
72b4c3fcb3SVladimir Zapolskiy return 0;
73b4c3fcb3SVladimir Zapolskiy }
74b4c3fcb3SVladimir Zapolskiy
sram_add_export(struct sram_dev * sram,struct sram_reserve * block,phys_addr_t start,struct sram_partition * part)75b4c3fcb3SVladimir Zapolskiy static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block,
76b4c3fcb3SVladimir Zapolskiy phys_addr_t start, struct sram_partition *part)
77b4c3fcb3SVladimir Zapolskiy {
78b4c3fcb3SVladimir Zapolskiy sysfs_bin_attr_init(&part->battr);
79b4c3fcb3SVladimir Zapolskiy part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL,
80b4c3fcb3SVladimir Zapolskiy "%llx.sram",
81b4c3fcb3SVladimir Zapolskiy (unsigned long long)start);
82b4c3fcb3SVladimir Zapolskiy if (!part->battr.attr.name)
83b4c3fcb3SVladimir Zapolskiy return -ENOMEM;
84b4c3fcb3SVladimir Zapolskiy
85b4c3fcb3SVladimir Zapolskiy part->battr.attr.mode = S_IRUSR | S_IWUSR;
86b4c3fcb3SVladimir Zapolskiy part->battr.read = sram_read;
87b4c3fcb3SVladimir Zapolskiy part->battr.write = sram_write;
88b4c3fcb3SVladimir Zapolskiy part->battr.size = block->size;
89b4c3fcb3SVladimir Zapolskiy
90b4c3fcb3SVladimir Zapolskiy return device_create_bin_file(sram->dev, &part->battr);
91b4c3fcb3SVladimir Zapolskiy }
92b4c3fcb3SVladimir Zapolskiy
sram_add_partition(struct sram_dev * sram,struct sram_reserve * block,phys_addr_t start)93b4c3fcb3SVladimir Zapolskiy static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block,
94b4c3fcb3SVladimir Zapolskiy phys_addr_t start)
95b4c3fcb3SVladimir Zapolskiy {
96b4c3fcb3SVladimir Zapolskiy int ret;
97b4c3fcb3SVladimir Zapolskiy struct sram_partition *part = &sram->partition[sram->partitions];
98b4c3fcb3SVladimir Zapolskiy
99b4c3fcb3SVladimir Zapolskiy mutex_init(&part->lock);
100fec29bf0SMikko Perttunen
101fec29bf0SMikko Perttunen if (sram->config && sram->config->map_only_reserved) {
102fec29bf0SMikko Perttunen void __iomem *virt_base;
103fec29bf0SMikko Perttunen
104fec29bf0SMikko Perttunen if (sram->no_memory_wc)
105fec29bf0SMikko Perttunen virt_base = devm_ioremap_resource(sram->dev, &block->res);
106fec29bf0SMikko Perttunen else
107fec29bf0SMikko Perttunen virt_base = devm_ioremap_resource_wc(sram->dev, &block->res);
108fec29bf0SMikko Perttunen
109fec29bf0SMikko Perttunen if (IS_ERR(virt_base)) {
110fec29bf0SMikko Perttunen dev_err(sram->dev, "could not map SRAM at %pr\n", &block->res);
111fec29bf0SMikko Perttunen return PTR_ERR(virt_base);
112fec29bf0SMikko Perttunen }
113fec29bf0SMikko Perttunen
114fec29bf0SMikko Perttunen part->base = virt_base;
115fec29bf0SMikko Perttunen } else {
116b4c3fcb3SVladimir Zapolskiy part->base = sram->virt_base + block->start;
117fec29bf0SMikko Perttunen }
118b4c3fcb3SVladimir Zapolskiy
119b4c3fcb3SVladimir Zapolskiy if (block->pool) {
120b4c3fcb3SVladimir Zapolskiy ret = sram_add_pool(sram, block, start, part);
121b4c3fcb3SVladimir Zapolskiy if (ret)
122b4c3fcb3SVladimir Zapolskiy return ret;
123b4c3fcb3SVladimir Zapolskiy }
124b4c3fcb3SVladimir Zapolskiy if (block->export) {
125b4c3fcb3SVladimir Zapolskiy ret = sram_add_export(sram, block, start, part);
126b4c3fcb3SVladimir Zapolskiy if (ret)
127b4c3fcb3SVladimir Zapolskiy return ret;
128b4c3fcb3SVladimir Zapolskiy }
12937afff0dSDave Gerlach if (block->protect_exec) {
13037afff0dSDave Gerlach ret = sram_check_protect_exec(sram, block, part);
13137afff0dSDave Gerlach if (ret)
13237afff0dSDave Gerlach return ret;
13337afff0dSDave Gerlach
13437afff0dSDave Gerlach ret = sram_add_pool(sram, block, start, part);
13537afff0dSDave Gerlach if (ret)
13637afff0dSDave Gerlach return ret;
13737afff0dSDave Gerlach
13837afff0dSDave Gerlach sram_add_protect_exec(part);
13937afff0dSDave Gerlach }
14037afff0dSDave Gerlach
141b4c3fcb3SVladimir Zapolskiy sram->partitions++;
142b4c3fcb3SVladimir Zapolskiy
143b4c3fcb3SVladimir Zapolskiy return 0;
144b4c3fcb3SVladimir Zapolskiy }
145b4c3fcb3SVladimir Zapolskiy
sram_free_partitions(struct sram_dev * sram)146b4c3fcb3SVladimir Zapolskiy static void sram_free_partitions(struct sram_dev *sram)
147b4c3fcb3SVladimir Zapolskiy {
148b4c3fcb3SVladimir Zapolskiy struct sram_partition *part;
149b4c3fcb3SVladimir Zapolskiy
150b4c3fcb3SVladimir Zapolskiy if (!sram->partitions)
151b4c3fcb3SVladimir Zapolskiy return;
152b4c3fcb3SVladimir Zapolskiy
153b4c3fcb3SVladimir Zapolskiy part = &sram->partition[sram->partitions - 1];
154b4c3fcb3SVladimir Zapolskiy for (; sram->partitions; sram->partitions--, part--) {
155b4c3fcb3SVladimir Zapolskiy if (part->battr.size)
156b4c3fcb3SVladimir Zapolskiy device_remove_bin_file(sram->dev, &part->battr);
157b4c3fcb3SVladimir Zapolskiy
158b4c3fcb3SVladimir Zapolskiy if (part->pool &&
159b4c3fcb3SVladimir Zapolskiy gen_pool_avail(part->pool) < gen_pool_size(part->pool))
160b4c3fcb3SVladimir Zapolskiy dev_err(sram->dev, "removed pool while SRAM allocated\n");
161b4c3fcb3SVladimir Zapolskiy }
162b4c3fcb3SVladimir Zapolskiy }
163b4c3fcb3SVladimir Zapolskiy
sram_reserve_cmp(void * priv,const struct list_head * a,const struct list_head * b)1644f0f586bSSami Tolvanen static int sram_reserve_cmp(void *priv, const struct list_head *a,
1654f0f586bSSami Tolvanen const struct list_head *b)
1662da19688SHeiko Stübner {
1672da19688SHeiko Stübner struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
1682da19688SHeiko Stübner struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
1692da19688SHeiko Stübner
1702da19688SHeiko Stübner return ra->start - rb->start;
1712da19688SHeiko Stübner }
1722da19688SHeiko Stübner
sram_reserve_regions(struct sram_dev * sram,struct resource * res)173a0a5be0bSVladimir Zapolskiy static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
1744984c6f5SPhilipp Zabel {
175a0a5be0bSVladimir Zapolskiy struct device_node *np = sram->dev->of_node, *child;
1762da19688SHeiko Stübner unsigned long size, cur_start, cur_size;
1772da19688SHeiko Stübner struct sram_reserve *rblocks, *block;
1782da19688SHeiko Stübner struct list_head reserve_list;
179b4c3fcb3SVladimir Zapolskiy unsigned int nblocks, exports = 0;
180b4c3fcb3SVladimir Zapolskiy const char *label;
181a0a5be0bSVladimir Zapolskiy int ret = 0;
1824984c6f5SPhilipp Zabel
1832da19688SHeiko Stübner INIT_LIST_HEAD(&reserve_list);
1842da19688SHeiko Stübner
185f3cbfa5dSAlexander Shiyan size = resource_size(res);
1864984c6f5SPhilipp Zabel
1872da19688SHeiko Stübner /*
1882da19688SHeiko Stübner * We need an additional block to mark the end of the memory region
1892da19688SHeiko Stübner * after the reserved blocks from the dt are processed.
1902da19688SHeiko Stübner */
1912da19688SHeiko Stübner nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
1926396bb22SKees Cook rblocks = kcalloc(nblocks, sizeof(*rblocks), GFP_KERNEL);
193ee895ccdSVladimir Zapolskiy if (!rblocks)
194ee895ccdSVladimir Zapolskiy return -ENOMEM;
1954984c6f5SPhilipp Zabel
1962da19688SHeiko Stübner block = &rblocks[0];
1972da19688SHeiko Stübner for_each_available_child_of_node(np, child) {
1982da19688SHeiko Stübner struct resource child_res;
1992da19688SHeiko Stübner
2002da19688SHeiko Stübner ret = of_address_to_resource(child, 0, &child_res);
2012da19688SHeiko Stübner if (ret < 0) {
202665d82fbSVladimir Zapolskiy dev_err(sram->dev,
20334d0eb50SRob Herring "could not get address for node %pOF\n",
20434d0eb50SRob Herring child);
2052da19688SHeiko Stübner goto err_chunks;
2062da19688SHeiko Stübner }
2072da19688SHeiko Stübner
2082da19688SHeiko Stübner if (child_res.start < res->start || child_res.end > res->end) {
209665d82fbSVladimir Zapolskiy dev_err(sram->dev,
21034d0eb50SRob Herring "reserved block %pOF outside the sram area\n",
21134d0eb50SRob Herring child);
2122da19688SHeiko Stübner ret = -EINVAL;
2132da19688SHeiko Stübner goto err_chunks;
2142da19688SHeiko Stübner }
2152da19688SHeiko Stübner
2162da19688SHeiko Stübner block->start = child_res.start - res->start;
2172da19688SHeiko Stübner block->size = resource_size(&child_res);
218fec29bf0SMikko Perttunen block->res = child_res;
2192da19688SHeiko Stübner list_add_tail(&block->list, &reserve_list);
2202da19688SHeiko Stübner
221d7d744abSRob Herring block->export = of_property_read_bool(child, "export");
222d7d744abSRob Herring block->pool = of_property_read_bool(child, "pool");
223d7d744abSRob Herring block->protect_exec = of_property_read_bool(child, "protect-exec");
22437afff0dSDave Gerlach
22537afff0dSDave Gerlach if ((block->export || block->pool || block->protect_exec) &&
22637afff0dSDave Gerlach block->size) {
227b4c3fcb3SVladimir Zapolskiy exports++;
228b4c3fcb3SVladimir Zapolskiy
229b4c3fcb3SVladimir Zapolskiy label = NULL;
230b4c3fcb3SVladimir Zapolskiy ret = of_property_read_string(child, "label", &label);
231b4c3fcb3SVladimir Zapolskiy if (ret && ret != -EINVAL) {
232b4c3fcb3SVladimir Zapolskiy dev_err(sram->dev,
23334d0eb50SRob Herring "%pOF has invalid label name\n",
23434d0eb50SRob Herring child);
235b4c3fcb3SVladimir Zapolskiy goto err_chunks;
236b4c3fcb3SVladimir Zapolskiy }
237b4c3fcb3SVladimir Zapolskiy if (!label)
23821e5a2d1SLinus Walleij block->label = devm_kasprintf(sram->dev, GFP_KERNEL,
239f8ea9502SLinus Walleij "%s", of_node_full_name(child));
24021e5a2d1SLinus Walleij else
241b4c3fcb3SVladimir Zapolskiy block->label = devm_kstrdup(sram->dev,
242b4c3fcb3SVladimir Zapolskiy label, GFP_KERNEL);
243ddc5c9a3SPan Bian if (!block->label) {
244ddc5c9a3SPan Bian ret = -ENOMEM;
245b4c3fcb3SVladimir Zapolskiy goto err_chunks;
246ddc5c9a3SPan Bian }
247b4c3fcb3SVladimir Zapolskiy
248b4c3fcb3SVladimir Zapolskiy dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
249b4c3fcb3SVladimir Zapolskiy block->export ? "exported " : "", block->label,
250b4c3fcb3SVladimir Zapolskiy block->start, block->start + block->size);
251b4c3fcb3SVladimir Zapolskiy } else {
252665d82fbSVladimir Zapolskiy dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
253665d82fbSVladimir Zapolskiy block->start, block->start + block->size);
254b4c3fcb3SVladimir Zapolskiy }
2552da19688SHeiko Stübner
2562da19688SHeiko Stübner block++;
2572da19688SHeiko Stübner }
258b4c3fcb3SVladimir Zapolskiy child = NULL;
2592da19688SHeiko Stübner
2602da19688SHeiko Stübner /* the last chunk marks the end of the region */
2612da19688SHeiko Stübner rblocks[nblocks - 1].start = size;
2622da19688SHeiko Stübner rblocks[nblocks - 1].size = 0;
2632da19688SHeiko Stübner list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
2642da19688SHeiko Stübner
2652da19688SHeiko Stübner list_sort(NULL, &reserve_list, sram_reserve_cmp);
2662da19688SHeiko Stübner
267b4c3fcb3SVladimir Zapolskiy if (exports) {
268a86854d0SKees Cook sram->partition = devm_kcalloc(sram->dev,
269a86854d0SKees Cook exports, sizeof(*sram->partition),
270b4c3fcb3SVladimir Zapolskiy GFP_KERNEL);
271b4c3fcb3SVladimir Zapolskiy if (!sram->partition) {
272b4c3fcb3SVladimir Zapolskiy ret = -ENOMEM;
273b4c3fcb3SVladimir Zapolskiy goto err_chunks;
274b4c3fcb3SVladimir Zapolskiy }
275b4c3fcb3SVladimir Zapolskiy }
2762da19688SHeiko Stübner
277b4c3fcb3SVladimir Zapolskiy cur_start = 0;
2782da19688SHeiko Stübner list_for_each_entry(block, &reserve_list, list) {
2792da19688SHeiko Stübner /* can only happen if sections overlap */
2802da19688SHeiko Stübner if (block->start < cur_start) {
281665d82fbSVladimir Zapolskiy dev_err(sram->dev,
2822da19688SHeiko Stübner "block at 0x%x starts after current offset 0x%lx\n",
2832da19688SHeiko Stübner block->start, cur_start);
2842da19688SHeiko Stübner ret = -EINVAL;
285b4c3fcb3SVladimir Zapolskiy sram_free_partitions(sram);
2862da19688SHeiko Stübner goto err_chunks;
2872da19688SHeiko Stübner }
2882da19688SHeiko Stübner
28937afff0dSDave Gerlach if ((block->export || block->pool || block->protect_exec) &&
29037afff0dSDave Gerlach block->size) {
291b4c3fcb3SVladimir Zapolskiy ret = sram_add_partition(sram, block,
292b4c3fcb3SVladimir Zapolskiy res->start + block->start);
293b4c3fcb3SVladimir Zapolskiy if (ret) {
294b4c3fcb3SVladimir Zapolskiy sram_free_partitions(sram);
295b4c3fcb3SVladimir Zapolskiy goto err_chunks;
296b4c3fcb3SVladimir Zapolskiy }
297b4c3fcb3SVladimir Zapolskiy }
298b4c3fcb3SVladimir Zapolskiy
2992da19688SHeiko Stübner /* current start is in a reserved block, so continue after it */
3002da19688SHeiko Stübner if (block->start == cur_start) {
3012da19688SHeiko Stübner cur_start = block->start + block->size;
3022da19688SHeiko Stübner continue;
3032da19688SHeiko Stübner }
3042da19688SHeiko Stübner
3052da19688SHeiko Stübner /*
3062da19688SHeiko Stübner * allocate the space between the current starting
3072da19688SHeiko Stübner * address and the following reserved block, or the
3082da19688SHeiko Stübner * end of the region.
3092da19688SHeiko Stübner */
3102da19688SHeiko Stübner cur_size = block->start - cur_start;
3112da19688SHeiko Stübner
312fec29bf0SMikko Perttunen if (sram->pool) {
313665d82fbSVladimir Zapolskiy dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
3142da19688SHeiko Stübner cur_start, cur_start + cur_size);
315665d82fbSVladimir Zapolskiy
3162da19688SHeiko Stübner ret = gen_pool_add_virt(sram->pool,
317665d82fbSVladimir Zapolskiy (unsigned long)sram->virt_base + cur_start,
3182da19688SHeiko Stübner res->start + cur_start, cur_size, -1);
319b4c3fcb3SVladimir Zapolskiy if (ret < 0) {
320b4c3fcb3SVladimir Zapolskiy sram_free_partitions(sram);
3212da19688SHeiko Stübner goto err_chunks;
322b4c3fcb3SVladimir Zapolskiy }
323fec29bf0SMikko Perttunen }
3242da19688SHeiko Stübner
3252da19688SHeiko Stübner /* next allocation after this reserved block */
3262da19688SHeiko Stübner cur_start = block->start + block->size;
3272da19688SHeiko Stübner }
3282da19688SHeiko Stübner
329a0a5be0bSVladimir Zapolskiy err_chunks:
330b4c3fcb3SVladimir Zapolskiy of_node_put(child);
3312da19688SHeiko Stübner kfree(rblocks);
3322da19688SHeiko Stübner
333a0a5be0bSVladimir Zapolskiy return ret;
334a0a5be0bSVladimir Zapolskiy }
335a0a5be0bSVladimir Zapolskiy
atmel_securam_wait(void)3362ae2e288SAlexandre Belloni static int atmel_securam_wait(void)
3372ae2e288SAlexandre Belloni {
3382ae2e288SAlexandre Belloni struct regmap *regmap;
3392ae2e288SAlexandre Belloni u32 val;
3402ae2e288SAlexandre Belloni
3412ae2e288SAlexandre Belloni regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
3422ae2e288SAlexandre Belloni if (IS_ERR(regmap))
3432ae2e288SAlexandre Belloni return -ENODEV;
3442ae2e288SAlexandre Belloni
3452ae2e288SAlexandre Belloni return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val,
3462ae2e288SAlexandre Belloni val & AT91_SECUMOD_RAMRDY_READY,
3472ae2e288SAlexandre Belloni 10000, 500000);
3482ae2e288SAlexandre Belloni }
3492ae2e288SAlexandre Belloni
350fec29bf0SMikko Perttunen static const struct sram_config atmel_securam_config = {
351fec29bf0SMikko Perttunen .init = atmel_securam_wait,
352fec29bf0SMikko Perttunen };
353fec29bf0SMikko Perttunen
354fec29bf0SMikko Perttunen /*
355fec29bf0SMikko Perttunen * SYSRAM contains areas that are not accessible by the
356fec29bf0SMikko Perttunen * kernel, such as the first 256K that is reserved for TZ.
357fec29bf0SMikko Perttunen * Accesses to those areas (including speculative accesses)
358fec29bf0SMikko Perttunen * trigger SErrors. As such we must map only the areas of
359fec29bf0SMikko Perttunen * SYSRAM specified in the device tree.
360fec29bf0SMikko Perttunen */
361fec29bf0SMikko Perttunen static const struct sram_config tegra_sysram_config = {
362fec29bf0SMikko Perttunen .map_only_reserved = true,
363fec29bf0SMikko Perttunen };
364fec29bf0SMikko Perttunen
3652ae2e288SAlexandre Belloni static const struct of_device_id sram_dt_ids[] = {
3662ae2e288SAlexandre Belloni { .compatible = "mmio-sram" },
367fec29bf0SMikko Perttunen { .compatible = "atmel,sama5d2-securam", .data = &atmel_securam_config },
368fec29bf0SMikko Perttunen { .compatible = "nvidia,tegra186-sysram", .data = &tegra_sysram_config },
369fec29bf0SMikko Perttunen { .compatible = "nvidia,tegra194-sysram", .data = &tegra_sysram_config },
3702925fc1cSMikko Perttunen { .compatible = "nvidia,tegra234-sysram", .data = &tegra_sysram_config },
3712ae2e288SAlexandre Belloni {}
3722ae2e288SAlexandre Belloni };
3732ae2e288SAlexandre Belloni
sram_probe(struct platform_device * pdev)374a0a5be0bSVladimir Zapolskiy static int sram_probe(struct platform_device *pdev)
375a0a5be0bSVladimir Zapolskiy {
376fec29bf0SMikko Perttunen const struct sram_config *config;
377a0a5be0bSVladimir Zapolskiy struct sram_dev *sram;
378a0a5be0bSVladimir Zapolskiy int ret;
37939b27e89SUwe Kleine-König struct resource *res;
3809263271aSUwe Kleine-König struct clk *clk;
381fec29bf0SMikko Perttunen
382fec29bf0SMikko Perttunen config = of_device_get_match_data(&pdev->dev);
383a0a5be0bSVladimir Zapolskiy
384a0a5be0bSVladimir Zapolskiy sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
385a0a5be0bSVladimir Zapolskiy if (!sram)
386a0a5be0bSVladimir Zapolskiy return -ENOMEM;
387a0a5be0bSVladimir Zapolskiy
388a0a5be0bSVladimir Zapolskiy sram->dev = &pdev->dev;
389fec29bf0SMikko Perttunen sram->no_memory_wc = of_property_read_bool(pdev->dev.of_node, "no-memory-wc");
390fec29bf0SMikko Perttunen sram->config = config;
391a0a5be0bSVladimir Zapolskiy
392fec29bf0SMikko Perttunen if (!config || !config->map_only_reserved) {
39339b27e89SUwe Kleine-König res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
394fec29bf0SMikko Perttunen if (sram->no_memory_wc)
39539b27e89SUwe Kleine-König sram->virt_base = devm_ioremap_resource(&pdev->dev, res);
396eb43e023SMarcin Wojtas else
39739b27e89SUwe Kleine-König sram->virt_base = devm_ioremap_resource_wc(&pdev->dev, res);
398444b0111SBartosz Golaszewski if (IS_ERR(sram->virt_base)) {
399444b0111SBartosz Golaszewski dev_err(&pdev->dev, "could not map SRAM registers\n");
400444b0111SBartosz Golaszewski return PTR_ERR(sram->virt_base);
401444b0111SBartosz Golaszewski }
402a0a5be0bSVladimir Zapolskiy
40373858173SVladimir Zapolskiy sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
40473858173SVladimir Zapolskiy NUMA_NO_NODE, NULL);
40573858173SVladimir Zapolskiy if (IS_ERR(sram->pool))
40673858173SVladimir Zapolskiy return PTR_ERR(sram->pool);
407fec29bf0SMikko Perttunen }
408a0a5be0bSVladimir Zapolskiy
4099263271aSUwe Kleine-König clk = devm_clk_get_optional_enabled(sram->dev, NULL);
4109263271aSUwe Kleine-König if (IS_ERR(clk))
4119263271aSUwe Kleine-König return PTR_ERR(clk);
412ee895ccdSVladimir Zapolskiy
413444b0111SBartosz Golaszewski ret = sram_reserve_regions(sram,
414444b0111SBartosz Golaszewski platform_get_resource(pdev, IORESOURCE_MEM, 0));
415d5b9653dSJohan Hovold if (ret)
4169263271aSUwe Kleine-König return ret;
417d5b9653dSJohan Hovold
4184984c6f5SPhilipp Zabel platform_set_drvdata(pdev, sram);
4194984c6f5SPhilipp Zabel
420fec29bf0SMikko Perttunen if (config && config->init) {
421fec29bf0SMikko Perttunen ret = config->init();
4222ae2e288SAlexandre Belloni if (ret)
423d5b9653dSJohan Hovold goto err_free_partitions;
4242ae2e288SAlexandre Belloni }
4252ae2e288SAlexandre Belloni
426fec29bf0SMikko Perttunen if (sram->pool)
427665d82fbSVladimir Zapolskiy dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
428665d82fbSVladimir Zapolskiy gen_pool_size(sram->pool) / 1024, sram->virt_base);
4294984c6f5SPhilipp Zabel
4304984c6f5SPhilipp Zabel return 0;
431f294d009SJohan Hovold
432d5b9653dSJohan Hovold err_free_partitions:
433d5b9653dSJohan Hovold sram_free_partitions(sram);
434f294d009SJohan Hovold
435f294d009SJohan Hovold return ret;
4364984c6f5SPhilipp Zabel }
4374984c6f5SPhilipp Zabel
sram_remove(struct platform_device * pdev)4384984c6f5SPhilipp Zabel static int sram_remove(struct platform_device *pdev)
4394984c6f5SPhilipp Zabel {
4404984c6f5SPhilipp Zabel struct sram_dev *sram = platform_get_drvdata(pdev);
4414984c6f5SPhilipp Zabel
442b4c3fcb3SVladimir Zapolskiy sram_free_partitions(sram);
443b4c3fcb3SVladimir Zapolskiy
444fec29bf0SMikko Perttunen if (sram->pool && gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
445665d82fbSVladimir Zapolskiy dev_err(sram->dev, "removed while SRAM allocated\n");
4464984c6f5SPhilipp Zabel
4474984c6f5SPhilipp Zabel return 0;
4484984c6f5SPhilipp Zabel }
4494984c6f5SPhilipp Zabel
4504984c6f5SPhilipp Zabel static struct platform_driver sram_driver = {
4514984c6f5SPhilipp Zabel .driver = {
4524984c6f5SPhilipp Zabel .name = "sram",
4532aa488a6SArnd Bergmann .of_match_table = sram_dt_ids,
4544984c6f5SPhilipp Zabel },
4554984c6f5SPhilipp Zabel .probe = sram_probe,
4564984c6f5SPhilipp Zabel .remove = sram_remove,
4574984c6f5SPhilipp Zabel };
4584984c6f5SPhilipp Zabel
sram_init(void)4594984c6f5SPhilipp Zabel static int __init sram_init(void)
4604984c6f5SPhilipp Zabel {
4614984c6f5SPhilipp Zabel return platform_driver_register(&sram_driver);
4624984c6f5SPhilipp Zabel }
4634984c6f5SPhilipp Zabel
4644984c6f5SPhilipp Zabel postcore_initcall(sram_init);
465