blob: 07ec2a8a93439e72071cc3bdceceefa0843cdf63 [file] [log] [blame]
Philipp Zabel4984c6f2013-04-29 16:17:12 -07001/*
2 * Generic on-chip SRAM allocation driver
3 *
4 * Copyright (C) 2012 Philipp Zabel, Pengutronix
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * MA 02110-1301, USA.
19 */
20
Philipp Zabel4984c6f2013-04-29 16:17:12 -070021#include <linux/clk.h>
Alexandre Belloni2ae2e282016-09-22 00:09:38 +020022#include <linux/delay.h>
Vladimir Zapolskiy98ce2d22015-06-01 15:30:00 +030023#include <linux/genalloc.h>
Philipp Zabel4984c6f2013-04-29 16:17:12 -070024#include <linux/io.h>
Heiko Stübner2da19682014-02-25 12:46:34 +010025#include <linux/list_sort.h>
Vladimir Zapolskiy98ce2d22015-06-01 15:30:00 +030026#include <linux/of_address.h>
Alexandre Belloni2ae2e282016-09-22 00:09:38 +020027#include <linux/of_device.h>
Philipp Zabel4984c6f2013-04-29 16:17:12 -070028#include <linux/platform_device.h>
Alexandre Belloni2ae2e282016-09-22 00:09:38 +020029#include <linux/regmap.h>
Philipp Zabel4984c6f2013-04-29 16:17:12 -070030#include <linux/slab.h>
Alexandre Belloni2ae2e282016-09-22 00:09:38 +020031#include <linux/mfd/syscon.h>
32#include <soc/at91/atmel-secumod.h>
Philipp Zabel4984c6f2013-04-29 16:17:12 -070033
34#define SRAM_GRANULARITY 32
35
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030036struct sram_partition {
Vladimir Zapolskiy525d12f2015-10-18 20:57:09 +030037 void __iomem *base;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030038
39 struct gen_pool *pool;
40 struct bin_attribute battr;
41 struct mutex lock;
42};
43
Philipp Zabel4984c6f2013-04-29 16:17:12 -070044struct sram_dev {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +030045 struct device *dev;
46 void __iomem *virt_base;
47
Philipp Zabel4984c6f2013-04-29 16:17:12 -070048 struct gen_pool *pool;
49 struct clk *clk;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030050
51 struct sram_partition *partition;
52 u32 partitions;
Philipp Zabel4984c6f2013-04-29 16:17:12 -070053};
54
Heiko Stübner2da19682014-02-25 12:46:34 +010055struct sram_reserve {
56 struct list_head list;
57 u32 start;
58 u32 size;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030059 bool export;
60 bool pool;
61 const char *label;
Heiko Stübner2da19682014-02-25 12:46:34 +010062};
63
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030064static ssize_t sram_read(struct file *filp, struct kobject *kobj,
65 struct bin_attribute *attr,
66 char *buf, loff_t pos, size_t count)
67{
68 struct sram_partition *part;
69
70 part = container_of(attr, struct sram_partition, battr);
71
72 mutex_lock(&part->lock);
Vladimir Zapolskiy525d12f2015-10-18 20:57:09 +030073 memcpy_fromio(buf, part->base + pos, count);
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030074 mutex_unlock(&part->lock);
75
76 return count;
77}
78
79static ssize_t sram_write(struct file *filp, struct kobject *kobj,
80 struct bin_attribute *attr,
81 char *buf, loff_t pos, size_t count)
82{
83 struct sram_partition *part;
84
85 part = container_of(attr, struct sram_partition, battr);
86
87 mutex_lock(&part->lock);
Vladimir Zapolskiy525d12f2015-10-18 20:57:09 +030088 memcpy_toio(part->base + pos, buf, count);
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030089 mutex_unlock(&part->lock);
90
91 return count;
92}
93
94static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block,
95 phys_addr_t start, struct sram_partition *part)
96{
97 int ret;
98
99 part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
100 NUMA_NO_NODE, block->label);
101 if (IS_ERR(part->pool))
102 return PTR_ERR(part->pool);
103
104 ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start,
105 block->size, NUMA_NO_NODE);
106 if (ret < 0) {
107 dev_err(sram->dev, "failed to register subpool: %d\n", ret);
108 return ret;
109 }
110
111 return 0;
112}
113
114static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block,
115 phys_addr_t start, struct sram_partition *part)
116{
117 sysfs_bin_attr_init(&part->battr);
118 part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL,
119 "%llx.sram",
120 (unsigned long long)start);
121 if (!part->battr.attr.name)
122 return -ENOMEM;
123
124 part->battr.attr.mode = S_IRUSR | S_IWUSR;
125 part->battr.read = sram_read;
126 part->battr.write = sram_write;
127 part->battr.size = block->size;
128
129 return device_create_bin_file(sram->dev, &part->battr);
130}
131
132static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block,
133 phys_addr_t start)
134{
135 int ret;
136 struct sram_partition *part = &sram->partition[sram->partitions];
137
138 mutex_init(&part->lock);
139 part->base = sram->virt_base + block->start;
140
141 if (block->pool) {
142 ret = sram_add_pool(sram, block, start, part);
143 if (ret)
144 return ret;
145 }
146 if (block->export) {
147 ret = sram_add_export(sram, block, start, part);
148 if (ret)
149 return ret;
150 }
151 sram->partitions++;
152
153 return 0;
154}
155
156static void sram_free_partitions(struct sram_dev *sram)
157{
158 struct sram_partition *part;
159
160 if (!sram->partitions)
161 return;
162
163 part = &sram->partition[sram->partitions - 1];
164 for (; sram->partitions; sram->partitions--, part--) {
165 if (part->battr.size)
166 device_remove_bin_file(sram->dev, &part->battr);
167
168 if (part->pool &&
169 gen_pool_avail(part->pool) < gen_pool_size(part->pool))
170 dev_err(sram->dev, "removed pool while SRAM allocated\n");
171 }
172}
173
Heiko Stübner2da19682014-02-25 12:46:34 +0100174static int sram_reserve_cmp(void *priv, struct list_head *a,
175 struct list_head *b)
176{
177 struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
178 struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
179
180 return ra->start - rb->start;
181}
182
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300183static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700184{
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300185 struct device_node *np = sram->dev->of_node, *child;
Heiko Stübner2da19682014-02-25 12:46:34 +0100186 unsigned long size, cur_start, cur_size;
187 struct sram_reserve *rblocks, *block;
188 struct list_head reserve_list;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300189 unsigned int nblocks, exports = 0;
190 const char *label;
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300191 int ret = 0;
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700192
Heiko Stübner2da19682014-02-25 12:46:34 +0100193 INIT_LIST_HEAD(&reserve_list);
194
Alexander Shiyanf3cbfa52013-06-10 18:43:53 +0400195 size = resource_size(res);
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700196
Heiko Stübner2da19682014-02-25 12:46:34 +0100197 /*
198 * We need an additional block to mark the end of the memory region
199 * after the reserved blocks from the dt are processed.
200 */
201 nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300202 rblocks = kzalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300203 if (!rblocks)
204 return -ENOMEM;
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700205
Heiko Stübner2da19682014-02-25 12:46:34 +0100206 block = &rblocks[0];
207 for_each_available_child_of_node(np, child) {
208 struct resource child_res;
209
210 ret = of_address_to_resource(child, 0, &child_res);
211 if (ret < 0) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300212 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100213 "could not get address for node %s\n",
214 child->full_name);
215 goto err_chunks;
216 }
217
218 if (child_res.start < res->start || child_res.end > res->end) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300219 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100220 "reserved block %s outside the sram area\n",
221 child->full_name);
222 ret = -EINVAL;
223 goto err_chunks;
224 }
225
226 block->start = child_res.start - res->start;
227 block->size = resource_size(&child_res);
228 list_add_tail(&block->list, &reserve_list);
229
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300230 if (of_find_property(child, "export", NULL))
231 block->export = true;
232
233 if (of_find_property(child, "pool", NULL))
234 block->pool = true;
235
236 if ((block->export || block->pool) && block->size) {
237 exports++;
238
239 label = NULL;
240 ret = of_property_read_string(child, "label", &label);
241 if (ret && ret != -EINVAL) {
242 dev_err(sram->dev,
243 "%s has invalid label name\n",
244 child->full_name);
245 goto err_chunks;
246 }
247 if (!label)
248 label = child->name;
249
250 block->label = devm_kstrdup(sram->dev,
251 label, GFP_KERNEL);
Pan Bianddc5c9a2016-12-03 17:29:28 +0800252 if (!block->label) {
253 ret = -ENOMEM;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300254 goto err_chunks;
Pan Bianddc5c9a2016-12-03 17:29:28 +0800255 }
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300256
257 dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
258 block->export ? "exported " : "", block->label,
259 block->start, block->start + block->size);
260 } else {
261 dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
262 block->start, block->start + block->size);
263 }
Heiko Stübner2da19682014-02-25 12:46:34 +0100264
265 block++;
266 }
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300267 child = NULL;
Heiko Stübner2da19682014-02-25 12:46:34 +0100268
269 /* the last chunk marks the end of the region */
270 rblocks[nblocks - 1].start = size;
271 rblocks[nblocks - 1].size = 0;
272 list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
273
274 list_sort(NULL, &reserve_list, sram_reserve_cmp);
275
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300276 if (exports) {
277 sram->partition = devm_kzalloc(sram->dev,
278 exports * sizeof(*sram->partition),
279 GFP_KERNEL);
280 if (!sram->partition) {
281 ret = -ENOMEM;
282 goto err_chunks;
283 }
284 }
Heiko Stübner2da19682014-02-25 12:46:34 +0100285
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300286 cur_start = 0;
Heiko Stübner2da19682014-02-25 12:46:34 +0100287 list_for_each_entry(block, &reserve_list, list) {
288 /* can only happen if sections overlap */
289 if (block->start < cur_start) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300290 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100291 "block at 0x%x starts after current offset 0x%lx\n",
292 block->start, cur_start);
293 ret = -EINVAL;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300294 sram_free_partitions(sram);
Heiko Stübner2da19682014-02-25 12:46:34 +0100295 goto err_chunks;
296 }
297
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300298 if ((block->export || block->pool) && block->size) {
299 ret = sram_add_partition(sram, block,
300 res->start + block->start);
301 if (ret) {
302 sram_free_partitions(sram);
303 goto err_chunks;
304 }
305 }
306
Heiko Stübner2da19682014-02-25 12:46:34 +0100307 /* current start is in a reserved block, so continue after it */
308 if (block->start == cur_start) {
309 cur_start = block->start + block->size;
310 continue;
311 }
312
313 /*
314 * allocate the space between the current starting
315 * address and the following reserved block, or the
316 * end of the region.
317 */
318 cur_size = block->start - cur_start;
319
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300320 dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
Heiko Stübner2da19682014-02-25 12:46:34 +0100321 cur_start, cur_start + cur_size);
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300322
Heiko Stübner2da19682014-02-25 12:46:34 +0100323 ret = gen_pool_add_virt(sram->pool,
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300324 (unsigned long)sram->virt_base + cur_start,
Heiko Stübner2da19682014-02-25 12:46:34 +0100325 res->start + cur_start, cur_size, -1);
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300326 if (ret < 0) {
327 sram_free_partitions(sram);
Heiko Stübner2da19682014-02-25 12:46:34 +0100328 goto err_chunks;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300329 }
Heiko Stübner2da19682014-02-25 12:46:34 +0100330
331 /* next allocation after this reserved block */
332 cur_start = block->start + block->size;
333 }
334
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300335 err_chunks:
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300336 if (child)
337 of_node_put(child);
338
Heiko Stübner2da19682014-02-25 12:46:34 +0100339 kfree(rblocks);
340
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300341 return ret;
342}
343
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200344static int atmel_securam_wait(void)
345{
346 struct regmap *regmap;
347 u32 val;
348
349 regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
350 if (IS_ERR(regmap))
351 return -ENODEV;
352
353 return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val,
354 val & AT91_SECUMOD_RAMRDY_READY,
355 10000, 500000);
356}
357
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200358static const struct of_device_id sram_dt_ids[] = {
359 { .compatible = "mmio-sram" },
360 { .compatible = "atmel,sama5d2-securam", .data = atmel_securam_wait },
361 {}
362};
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200363
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300364static int sram_probe(struct platform_device *pdev)
365{
366 struct sram_dev *sram;
367 struct resource *res;
368 size_t size;
369 int ret;
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200370 int (*init_func)(void);
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300371
372 sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
373 if (!sram)
374 return -ENOMEM;
375
376 sram->dev = &pdev->dev;
377
378 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
379 if (!res) {
380 dev_err(sram->dev, "found no memory resource\n");
381 return -EINVAL;
382 }
383
384 size = resource_size(res);
385
386 if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
387 dev_err(sram->dev, "could not request region for resource\n");
388 return -EBUSY;
389 }
390
Marcin Wojtaseb43e022016-03-14 09:38:56 +0100391 if (of_property_read_bool(pdev->dev.of_node, "no-memory-wc"))
392 sram->virt_base = devm_ioremap(sram->dev, res->start, size);
393 else
394 sram->virt_base = devm_ioremap_wc(sram->dev, res->start, size);
Vladimir Zapolskiyd449d692016-03-23 04:52:45 +0200395 if (!sram->virt_base)
396 return -ENOMEM;
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300397
Vladimir Zapolskiy73858172015-09-04 15:47:43 -0700398 sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
399 NUMA_NO_NODE, NULL);
400 if (IS_ERR(sram->pool))
401 return PTR_ERR(sram->pool);
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300402
403 ret = sram_reserve_regions(sram, res);
404 if (ret)
405 return ret;
406
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300407 sram->clk = devm_clk_get(sram->dev, NULL);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300408 if (IS_ERR(sram->clk))
409 sram->clk = NULL;
410 else
411 clk_prepare_enable(sram->clk);
412
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700413 platform_set_drvdata(pdev, sram);
414
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200415 init_func = of_device_get_match_data(&pdev->dev);
416 if (init_func) {
417 ret = init_func();
418 if (ret)
419 return ret;
420 }
421
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300422 dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
423 gen_pool_size(sram->pool) / 1024, sram->virt_base);
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700424
425 return 0;
426}
427
428static int sram_remove(struct platform_device *pdev)
429{
430 struct sram_dev *sram = platform_get_drvdata(pdev);
431
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300432 sram_free_partitions(sram);
433
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700434 if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300435 dev_err(sram->dev, "removed while SRAM allocated\n");
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700436
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700437 if (sram->clk)
438 clk_disable_unprepare(sram->clk);
439
440 return 0;
441}
442
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700443static struct platform_driver sram_driver = {
444 .driver = {
445 .name = "sram",
Arnd Bergmann2aa488a2016-11-22 15:30:54 +0100446 .of_match_table = sram_dt_ids,
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700447 },
448 .probe = sram_probe,
449 .remove = sram_remove,
450};
451
452static int __init sram_init(void)
453{
454 return platform_driver_register(&sram_driver);
455}
456
457postcore_initcall(sram_init);