blob: 5a6e001845c23e33862f93ca621ef4f9e7ba0ade [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
Dave Gerlachcdd17372017-01-12 14:52:18 -060034#include "sram.h"
35
Philipp Zabel4984c6f2013-04-29 16:17:12 -070036#define SRAM_GRANULARITY 32
37
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030038static ssize_t sram_read(struct file *filp, struct kobject *kobj,
39 struct bin_attribute *attr,
40 char *buf, loff_t pos, size_t count)
41{
42 struct sram_partition *part;
43
44 part = container_of(attr, struct sram_partition, battr);
45
46 mutex_lock(&part->lock);
Vladimir Zapolskiy525d12f2015-10-18 20:57:09 +030047 memcpy_fromio(buf, part->base + pos, count);
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030048 mutex_unlock(&part->lock);
49
50 return count;
51}
52
53static ssize_t sram_write(struct file *filp, struct kobject *kobj,
54 struct bin_attribute *attr,
55 char *buf, loff_t pos, size_t count)
56{
57 struct sram_partition *part;
58
59 part = container_of(attr, struct sram_partition, battr);
60
61 mutex_lock(&part->lock);
Vladimir Zapolskiy525d12f2015-10-18 20:57:09 +030062 memcpy_toio(part->base + pos, buf, count);
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +030063 mutex_unlock(&part->lock);
64
65 return count;
66}
67
68static int sram_add_pool(struct sram_dev *sram, struct sram_reserve *block,
69 phys_addr_t start, struct sram_partition *part)
70{
71 int ret;
72
73 part->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
74 NUMA_NO_NODE, block->label);
75 if (IS_ERR(part->pool))
76 return PTR_ERR(part->pool);
77
78 ret = gen_pool_add_virt(part->pool, (unsigned long)part->base, start,
79 block->size, NUMA_NO_NODE);
80 if (ret < 0) {
81 dev_err(sram->dev, "failed to register subpool: %d\n", ret);
82 return ret;
83 }
84
85 return 0;
86}
87
88static int sram_add_export(struct sram_dev *sram, struct sram_reserve *block,
89 phys_addr_t start, struct sram_partition *part)
90{
91 sysfs_bin_attr_init(&part->battr);
92 part->battr.attr.name = devm_kasprintf(sram->dev, GFP_KERNEL,
93 "%llx.sram",
94 (unsigned long long)start);
95 if (!part->battr.attr.name)
96 return -ENOMEM;
97
98 part->battr.attr.mode = S_IRUSR | S_IWUSR;
99 part->battr.read = sram_read;
100 part->battr.write = sram_write;
101 part->battr.size = block->size;
102
103 return device_create_bin_file(sram->dev, &part->battr);
104}
105
106static int sram_add_partition(struct sram_dev *sram, struct sram_reserve *block,
107 phys_addr_t start)
108{
109 int ret;
110 struct sram_partition *part = &sram->partition[sram->partitions];
111
112 mutex_init(&part->lock);
113 part->base = sram->virt_base + block->start;
114
115 if (block->pool) {
116 ret = sram_add_pool(sram, block, start, part);
117 if (ret)
118 return ret;
119 }
120 if (block->export) {
121 ret = sram_add_export(sram, block, start, part);
122 if (ret)
123 return ret;
124 }
125 sram->partitions++;
126
127 return 0;
128}
129
130static void sram_free_partitions(struct sram_dev *sram)
131{
132 struct sram_partition *part;
133
134 if (!sram->partitions)
135 return;
136
137 part = &sram->partition[sram->partitions - 1];
138 for (; sram->partitions; sram->partitions--, part--) {
139 if (part->battr.size)
140 device_remove_bin_file(sram->dev, &part->battr);
141
142 if (part->pool &&
143 gen_pool_avail(part->pool) < gen_pool_size(part->pool))
144 dev_err(sram->dev, "removed pool while SRAM allocated\n");
145 }
146}
147
Heiko Stübner2da19682014-02-25 12:46:34 +0100148static int sram_reserve_cmp(void *priv, struct list_head *a,
149 struct list_head *b)
150{
151 struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
152 struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
153
154 return ra->start - rb->start;
155}
156
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300157static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700158{
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300159 struct device_node *np = sram->dev->of_node, *child;
Heiko Stübner2da19682014-02-25 12:46:34 +0100160 unsigned long size, cur_start, cur_size;
161 struct sram_reserve *rblocks, *block;
162 struct list_head reserve_list;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300163 unsigned int nblocks, exports = 0;
164 const char *label;
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300165 int ret = 0;
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700166
Heiko Stübner2da19682014-02-25 12:46:34 +0100167 INIT_LIST_HEAD(&reserve_list);
168
Alexander Shiyanf3cbfa52013-06-10 18:43:53 +0400169 size = resource_size(res);
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700170
Heiko Stübner2da19682014-02-25 12:46:34 +0100171 /*
172 * We need an additional block to mark the end of the memory region
173 * after the reserved blocks from the dt are processed.
174 */
175 nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300176 rblocks = kzalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300177 if (!rblocks)
178 return -ENOMEM;
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700179
Heiko Stübner2da19682014-02-25 12:46:34 +0100180 block = &rblocks[0];
181 for_each_available_child_of_node(np, child) {
182 struct resource child_res;
183
184 ret = of_address_to_resource(child, 0, &child_res);
185 if (ret < 0) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300186 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100187 "could not get address for node %s\n",
188 child->full_name);
189 goto err_chunks;
190 }
191
192 if (child_res.start < res->start || child_res.end > res->end) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300193 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100194 "reserved block %s outside the sram area\n",
195 child->full_name);
196 ret = -EINVAL;
197 goto err_chunks;
198 }
199
200 block->start = child_res.start - res->start;
201 block->size = resource_size(&child_res);
202 list_add_tail(&block->list, &reserve_list);
203
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300204 if (of_find_property(child, "export", NULL))
205 block->export = true;
206
207 if (of_find_property(child, "pool", NULL))
208 block->pool = true;
209
210 if ((block->export || block->pool) && block->size) {
211 exports++;
212
213 label = NULL;
214 ret = of_property_read_string(child, "label", &label);
215 if (ret && ret != -EINVAL) {
216 dev_err(sram->dev,
217 "%s has invalid label name\n",
218 child->full_name);
219 goto err_chunks;
220 }
221 if (!label)
222 label = child->name;
223
224 block->label = devm_kstrdup(sram->dev,
225 label, GFP_KERNEL);
Pan Bianddc5c9a2016-12-03 17:29:28 +0800226 if (!block->label) {
227 ret = -ENOMEM;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300228 goto err_chunks;
Pan Bianddc5c9a2016-12-03 17:29:28 +0800229 }
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300230
231 dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
232 block->export ? "exported " : "", block->label,
233 block->start, block->start + block->size);
234 } else {
235 dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
236 block->start, block->start + block->size);
237 }
Heiko Stübner2da19682014-02-25 12:46:34 +0100238
239 block++;
240 }
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300241 child = NULL;
Heiko Stübner2da19682014-02-25 12:46:34 +0100242
243 /* the last chunk marks the end of the region */
244 rblocks[nblocks - 1].start = size;
245 rblocks[nblocks - 1].size = 0;
246 list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
247
248 list_sort(NULL, &reserve_list, sram_reserve_cmp);
249
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300250 if (exports) {
251 sram->partition = devm_kzalloc(sram->dev,
252 exports * sizeof(*sram->partition),
253 GFP_KERNEL);
254 if (!sram->partition) {
255 ret = -ENOMEM;
256 goto err_chunks;
257 }
258 }
Heiko Stübner2da19682014-02-25 12:46:34 +0100259
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300260 cur_start = 0;
Heiko Stübner2da19682014-02-25 12:46:34 +0100261 list_for_each_entry(block, &reserve_list, list) {
262 /* can only happen if sections overlap */
263 if (block->start < cur_start) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300264 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100265 "block at 0x%x starts after current offset 0x%lx\n",
266 block->start, cur_start);
267 ret = -EINVAL;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300268 sram_free_partitions(sram);
Heiko Stübner2da19682014-02-25 12:46:34 +0100269 goto err_chunks;
270 }
271
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300272 if ((block->export || block->pool) && block->size) {
273 ret = sram_add_partition(sram, block,
274 res->start + block->start);
275 if (ret) {
276 sram_free_partitions(sram);
277 goto err_chunks;
278 }
279 }
280
Heiko Stübner2da19682014-02-25 12:46:34 +0100281 /* current start is in a reserved block, so continue after it */
282 if (block->start == cur_start) {
283 cur_start = block->start + block->size;
284 continue;
285 }
286
287 /*
288 * allocate the space between the current starting
289 * address and the following reserved block, or the
290 * end of the region.
291 */
292 cur_size = block->start - cur_start;
293
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300294 dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
Heiko Stübner2da19682014-02-25 12:46:34 +0100295 cur_start, cur_start + cur_size);
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300296
Heiko Stübner2da19682014-02-25 12:46:34 +0100297 ret = gen_pool_add_virt(sram->pool,
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300298 (unsigned long)sram->virt_base + cur_start,
Heiko Stübner2da19682014-02-25 12:46:34 +0100299 res->start + cur_start, cur_size, -1);
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300300 if (ret < 0) {
301 sram_free_partitions(sram);
Heiko Stübner2da19682014-02-25 12:46:34 +0100302 goto err_chunks;
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300303 }
Heiko Stübner2da19682014-02-25 12:46:34 +0100304
305 /* next allocation after this reserved block */
306 cur_start = block->start + block->size;
307 }
308
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300309 err_chunks:
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300310 if (child)
311 of_node_put(child);
312
Heiko Stübner2da19682014-02-25 12:46:34 +0100313 kfree(rblocks);
314
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300315 return ret;
316}
317
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200318static int atmel_securam_wait(void)
319{
320 struct regmap *regmap;
321 u32 val;
322
323 regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
324 if (IS_ERR(regmap))
325 return -ENODEV;
326
327 return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val,
328 val & AT91_SECUMOD_RAMRDY_READY,
329 10000, 500000);
330}
331
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200332static const struct of_device_id sram_dt_ids[] = {
333 { .compatible = "mmio-sram" },
334 { .compatible = "atmel,sama5d2-securam", .data = atmel_securam_wait },
335 {}
336};
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200337
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300338static int sram_probe(struct platform_device *pdev)
339{
340 struct sram_dev *sram;
341 struct resource *res;
342 size_t size;
343 int ret;
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200344 int (*init_func)(void);
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300345
346 sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
347 if (!sram)
348 return -ENOMEM;
349
350 sram->dev = &pdev->dev;
351
352 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
353 if (!res) {
354 dev_err(sram->dev, "found no memory resource\n");
355 return -EINVAL;
356 }
357
358 size = resource_size(res);
359
360 if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
361 dev_err(sram->dev, "could not request region for resource\n");
362 return -EBUSY;
363 }
364
Marcin Wojtaseb43e022016-03-14 09:38:56 +0100365 if (of_property_read_bool(pdev->dev.of_node, "no-memory-wc"))
366 sram->virt_base = devm_ioremap(sram->dev, res->start, size);
367 else
368 sram->virt_base = devm_ioremap_wc(sram->dev, res->start, size);
Vladimir Zapolskiyd449d692016-03-23 04:52:45 +0200369 if (!sram->virt_base)
370 return -ENOMEM;
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300371
Vladimir Zapolskiy73858172015-09-04 15:47:43 -0700372 sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
373 NUMA_NO_NODE, NULL);
374 if (IS_ERR(sram->pool))
375 return PTR_ERR(sram->pool);
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300376
377 ret = sram_reserve_regions(sram, res);
378 if (ret)
379 return ret;
380
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300381 sram->clk = devm_clk_get(sram->dev, NULL);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300382 if (IS_ERR(sram->clk))
383 sram->clk = NULL;
384 else
385 clk_prepare_enable(sram->clk);
386
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700387 platform_set_drvdata(pdev, sram);
388
Alexandre Belloni2ae2e282016-09-22 00:09:38 +0200389 init_func = of_device_get_match_data(&pdev->dev);
390 if (init_func) {
391 ret = init_func();
392 if (ret)
393 return ret;
394 }
395
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300396 dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
397 gen_pool_size(sram->pool) / 1024, sram->virt_base);
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700398
399 return 0;
400}
401
402static int sram_remove(struct platform_device *pdev)
403{
404 struct sram_dev *sram = platform_get_drvdata(pdev);
405
Vladimir Zapolskiyb4c3fcb2015-09-21 23:52:46 +0300406 sram_free_partitions(sram);
407
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700408 if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300409 dev_err(sram->dev, "removed while SRAM allocated\n");
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700410
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700411 if (sram->clk)
412 clk_disable_unprepare(sram->clk);
413
414 return 0;
415}
416
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700417static struct platform_driver sram_driver = {
418 .driver = {
419 .name = "sram",
Arnd Bergmann2aa488a2016-11-22 15:30:54 +0100420 .of_match_table = sram_dt_ids,
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700421 },
422 .probe = sram_probe,
423 .remove = sram_remove,
424};
425
426static int __init sram_init(void)
427{
428 return platform_driver_register(&sram_driver);
429}
430
431postcore_initcall(sram_init);