2 * Generic on-chip SRAM allocation driver
4 * Copyright (C) 2012 Philipp Zabel, Pengutronix
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.
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,
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/genalloc.h>
25 #include <linux/list_sort.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/platform_device.h>
29 #include <linux/regmap.h>
30 #include <linux/slab.h>
31 #include <linux/mfd/syscon.h>
32 #include <soc/at91/atmel-secumod.h>
36 #define SRAM_GRANULARITY 32
38 static ssize_t
sram_read(struct file
*filp
, struct kobject
*kobj
,
39 struct bin_attribute
*attr
,
40 char *buf
, loff_t pos
, size_t count
)
42 struct sram_partition
*part
;
44 part
= container_of(attr
, struct sram_partition
, battr
);
46 mutex_lock(&part
->lock
);
47 memcpy_fromio(buf
, part
->base
+ pos
, count
);
48 mutex_unlock(&part
->lock
);
53 static ssize_t
sram_write(struct file
*filp
, struct kobject
*kobj
,
54 struct bin_attribute
*attr
,
55 char *buf
, loff_t pos
, size_t count
)
57 struct sram_partition
*part
;
59 part
= container_of(attr
, struct sram_partition
, battr
);
61 mutex_lock(&part
->lock
);
62 memcpy_toio(part
->base
+ pos
, buf
, count
);
63 mutex_unlock(&part
->lock
);
68 static int sram_add_pool(struct sram_dev
*sram
, struct sram_reserve
*block
,
69 phys_addr_t start
, struct sram_partition
*part
)
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
);
78 ret
= gen_pool_add_virt(part
->pool
, (unsigned long)part
->base
, start
,
79 block
->size
, NUMA_NO_NODE
);
81 dev_err(sram
->dev
, "failed to register subpool: %d\n", ret
);
88 static int sram_add_export(struct sram_dev
*sram
, struct sram_reserve
*block
,
89 phys_addr_t start
, struct sram_partition
*part
)
91 sysfs_bin_attr_init(&part
->battr
);
92 part
->battr
.attr
.name
= devm_kasprintf(sram
->dev
, GFP_KERNEL
,
94 (unsigned long long)start
);
95 if (!part
->battr
.attr
.name
)
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
;
103 return device_create_bin_file(sram
->dev
, &part
->battr
);
106 static int sram_add_partition(struct sram_dev
*sram
, struct sram_reserve
*block
,
110 struct sram_partition
*part
= &sram
->partition
[sram
->partitions
];
112 mutex_init(&part
->lock
);
113 part
->base
= sram
->virt_base
+ block
->start
;
116 ret
= sram_add_pool(sram
, block
, start
, part
);
121 ret
= sram_add_export(sram
, block
, start
, part
);
125 if (block
->protect_exec
) {
126 ret
= sram_check_protect_exec(sram
, block
, part
);
130 ret
= sram_add_pool(sram
, block
, start
, part
);
134 sram_add_protect_exec(part
);
142 static void sram_free_partitions(struct sram_dev
*sram
)
144 struct sram_partition
*part
;
146 if (!sram
->partitions
)
149 part
= &sram
->partition
[sram
->partitions
- 1];
150 for (; sram
->partitions
; sram
->partitions
--, part
--) {
151 if (part
->battr
.size
)
152 device_remove_bin_file(sram
->dev
, &part
->battr
);
155 gen_pool_avail(part
->pool
) < gen_pool_size(part
->pool
))
156 dev_err(sram
->dev
, "removed pool while SRAM allocated\n");
160 static int sram_reserve_cmp(void *priv
, struct list_head
*a
,
163 struct sram_reserve
*ra
= list_entry(a
, struct sram_reserve
, list
);
164 struct sram_reserve
*rb
= list_entry(b
, struct sram_reserve
, list
);
166 return ra
->start
- rb
->start
;
169 static int sram_reserve_regions(struct sram_dev
*sram
, struct resource
*res
)
171 struct device_node
*np
= sram
->dev
->of_node
, *child
;
172 unsigned long size
, cur_start
, cur_size
;
173 struct sram_reserve
*rblocks
, *block
;
174 struct list_head reserve_list
;
175 unsigned int nblocks
, exports
= 0;
179 INIT_LIST_HEAD(&reserve_list
);
181 size
= resource_size(res
);
184 * We need an additional block to mark the end of the memory region
185 * after the reserved blocks from the dt are processed.
187 nblocks
= (np
) ? of_get_available_child_count(np
) + 1 : 1;
188 rblocks
= kcalloc(nblocks
, sizeof(*rblocks
), GFP_KERNEL
);
193 for_each_available_child_of_node(np
, child
) {
194 struct resource child_res
;
196 ret
= of_address_to_resource(child
, 0, &child_res
);
199 "could not get address for node %pOF\n",
204 if (child_res
.start
< res
->start
|| child_res
.end
> res
->end
) {
206 "reserved block %pOF outside the sram area\n",
212 block
->start
= child_res
.start
- res
->start
;
213 block
->size
= resource_size(&child_res
);
214 list_add_tail(&block
->list
, &reserve_list
);
216 if (of_find_property(child
, "export", NULL
))
217 block
->export
= true;
219 if (of_find_property(child
, "pool", NULL
))
222 if (of_find_property(child
, "protect-exec", NULL
))
223 block
->protect_exec
= true;
225 if ((block
->export
|| block
->pool
|| block
->protect_exec
) &&
230 ret
= of_property_read_string(child
, "label", &label
);
231 if (ret
&& ret
!= -EINVAL
) {
233 "%pOF has invalid label name\n",
240 block
->label
= devm_kstrdup(sram
->dev
,
247 dev_dbg(sram
->dev
, "found %sblock '%s' 0x%x-0x%x\n",
248 block
->export
? "exported " : "", block
->label
,
249 block
->start
, block
->start
+ block
->size
);
251 dev_dbg(sram
->dev
, "found reserved block 0x%x-0x%x\n",
252 block
->start
, block
->start
+ block
->size
);
259 /* the last chunk marks the end of the region */
260 rblocks
[nblocks
- 1].start
= size
;
261 rblocks
[nblocks
- 1].size
= 0;
262 list_add_tail(&rblocks
[nblocks
- 1].list
, &reserve_list
);
264 list_sort(NULL
, &reserve_list
, sram_reserve_cmp
);
267 sram
->partition
= devm_kcalloc(sram
->dev
,
268 exports
, sizeof(*sram
->partition
),
270 if (!sram
->partition
) {
277 list_for_each_entry(block
, &reserve_list
, list
) {
278 /* can only happen if sections overlap */
279 if (block
->start
< cur_start
) {
281 "block at 0x%x starts after current offset 0x%lx\n",
282 block
->start
, cur_start
);
284 sram_free_partitions(sram
);
288 if ((block
->export
|| block
->pool
|| block
->protect_exec
) &&
290 ret
= sram_add_partition(sram
, block
,
291 res
->start
+ block
->start
);
293 sram_free_partitions(sram
);
298 /* current start is in a reserved block, so continue after it */
299 if (block
->start
== cur_start
) {
300 cur_start
= block
->start
+ block
->size
;
305 * allocate the space between the current starting
306 * address and the following reserved block, or the
309 cur_size
= block
->start
- cur_start
;
311 dev_dbg(sram
->dev
, "adding chunk 0x%lx-0x%lx\n",
312 cur_start
, cur_start
+ cur_size
);
314 ret
= gen_pool_add_virt(sram
->pool
,
315 (unsigned long)sram
->virt_base
+ cur_start
,
316 res
->start
+ cur_start
, cur_size
, -1);
318 sram_free_partitions(sram
);
322 /* next allocation after this reserved block */
323 cur_start
= block
->start
+ block
->size
;
333 static int atmel_securam_wait(void)
335 struct regmap
*regmap
;
338 regmap
= syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
342 return regmap_read_poll_timeout(regmap
, AT91_SECUMOD_RAMRDY
, val
,
343 val
& AT91_SECUMOD_RAMRDY_READY
,
347 static const struct of_device_id sram_dt_ids
[] = {
348 { .compatible
= "mmio-sram" },
349 { .compatible
= "atmel,sama5d2-securam", .data
= atmel_securam_wait
},
353 static int sram_probe(struct platform_device
*pdev
)
355 struct sram_dev
*sram
;
356 struct resource
*res
;
359 int (*init_func
)(void);
361 sram
= devm_kzalloc(&pdev
->dev
, sizeof(*sram
), GFP_KERNEL
);
365 sram
->dev
= &pdev
->dev
;
367 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
369 dev_err(sram
->dev
, "found no memory resource\n");
373 size
= resource_size(res
);
375 if (!devm_request_mem_region(sram
->dev
, res
->start
, size
, pdev
->name
)) {
376 dev_err(sram
->dev
, "could not request region for resource\n");
380 if (of_property_read_bool(pdev
->dev
.of_node
, "no-memory-wc"))
381 sram
->virt_base
= devm_ioremap(sram
->dev
, res
->start
, size
);
383 sram
->virt_base
= devm_ioremap_wc(sram
->dev
, res
->start
, size
);
384 if (!sram
->virt_base
)
387 sram
->pool
= devm_gen_pool_create(sram
->dev
, ilog2(SRAM_GRANULARITY
),
389 if (IS_ERR(sram
->pool
))
390 return PTR_ERR(sram
->pool
);
392 sram
->clk
= devm_clk_get(sram
->dev
, NULL
);
393 if (IS_ERR(sram
->clk
))
396 clk_prepare_enable(sram
->clk
);
398 ret
= sram_reserve_regions(sram
, res
);
400 goto err_disable_clk
;
402 platform_set_drvdata(pdev
, sram
);
404 init_func
= of_device_get_match_data(&pdev
->dev
);
408 goto err_free_partitions
;
411 dev_dbg(sram
->dev
, "SRAM pool: %zu KiB @ 0x%p\n",
412 gen_pool_size(sram
->pool
) / 1024, sram
->virt_base
);
417 sram_free_partitions(sram
);
420 clk_disable_unprepare(sram
->clk
);
425 static int sram_remove(struct platform_device
*pdev
)
427 struct sram_dev
*sram
= platform_get_drvdata(pdev
);
429 sram_free_partitions(sram
);
431 if (gen_pool_avail(sram
->pool
) < gen_pool_size(sram
->pool
))
432 dev_err(sram
->dev
, "removed while SRAM allocated\n");
435 clk_disable_unprepare(sram
->clk
);
440 static struct platform_driver sram_driver
= {
443 .of_match_table
= sram_dt_ids
,
446 .remove
= sram_remove
,
449 static int __init
sram_init(void)
451 return platform_driver_register(&sram_driver
);
454 postcore_initcall(sram_init
);