Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Generic on-chip SRAM allocation driver
  4 *
  5 * Copyright (C) 2012 Philipp Zabel, Pengutronix
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/clk.h>
  9#include <linux/delay.h>
 10#include <linux/genalloc.h>
 11#include <linux/io.h>
 12#include <linux/list_sort.h>
 13#include <linux/of.h>
 14#include <linux/of_address.h>
 
 15#include <linux/platform_device.h>
 16#include <linux/regmap.h>
 17#include <linux/slab.h>
 18#include <linux/mfd/syscon.h>
 19#include <soc/at91/atmel-secumod.h>
 20
 21#include "sram.h"
 22
 23#define SRAM_GRANULARITY	32
 24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 25static 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);
 34	memcpy_fromio(buf, part->base + pos, count);
 35	mutex_unlock(&part->lock);
 36
 37	return count;
 38}
 39
 40static 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);
 49	memcpy_toio(part->base + pos, buf, count);
 50	mutex_unlock(&part->lock);
 51
 52	return count;
 53}
 54
 55static 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
 75static 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
 93static 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);
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	}
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	}
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
141	sram->partitions++;
142
143	return 0;
144}
145
146static 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
164static int sram_reserve_cmp(void *priv, const struct list_head *a,
165					const struct list_head *b)
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
173static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
174{
175	struct device_node *np = sram->dev->of_node, *child;
176	unsigned long size, cur_start, cur_size;
177	struct sram_reserve *rblocks, *block;
178	struct list_head reserve_list;
179	unsigned int nblocks, exports = 0;
180	const char *label;
181	int ret = 0;
182
183	INIT_LIST_HEAD(&reserve_list);
184
185	size = resource_size(res);
186
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;
192	rblocks = kcalloc(nblocks, sizeof(*rblocks), GFP_KERNEL);
193	if (!rblocks)
194		return -ENOMEM;
195
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) {
202			dev_err(sram->dev,
203				"could not get address for node %pOF\n",
204				child);
205			goto err_chunks;
206		}
207
208		if (child_res.start < res->start || child_res.end > res->end) {
209			dev_err(sram->dev,
210				"reserved block %pOF outside the sram area\n",
211				child);
212			ret = -EINVAL;
213			goto err_chunks;
214		}
215
216		block->start = child_res.start - res->start;
217		block->size = resource_size(&child_res);
218		block->res = child_res;
219		list_add_tail(&block->list, &reserve_list);
220
221		block->export = of_property_read_bool(child, "export");
222		block->pool = of_property_read_bool(child, "pool");
223		block->protect_exec = of_property_read_bool(child, "protect-exec");
224
225		if ((block->export || block->pool || block->protect_exec) &&
226		    block->size) {
 
 
227			exports++;
228
229			label = NULL;
230			ret = of_property_read_string(child, "label", &label);
231			if (ret && ret != -EINVAL) {
232				dev_err(sram->dev,
233					"%pOF has invalid label name\n",
234					child);
235				goto err_chunks;
236			}
237			if (!label)
238				block->label = devm_kasprintf(sram->dev, GFP_KERNEL,
239							      "%s", of_node_full_name(child));
240			else
241				block->label = devm_kstrdup(sram->dev,
242							    label, GFP_KERNEL);
243			if (!block->label) {
244				ret = -ENOMEM;
245				goto err_chunks;
246			}
247
248			dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
249				block->export ? "exported " : "", block->label,
250				block->start, block->start + block->size);
251		} else {
252			dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
253				block->start, block->start + block->size);
254		}
255
256		block++;
257	}
258	child = NULL;
259
260	/* the last chunk marks the end of the region */
261	rblocks[nblocks - 1].start = size;
262	rblocks[nblocks - 1].size = 0;
263	list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
264
265	list_sort(NULL, &reserve_list, sram_reserve_cmp);
266
267	if (exports) {
268		sram->partition = devm_kcalloc(sram->dev,
269				       exports, sizeof(*sram->partition),
270				       GFP_KERNEL);
271		if (!sram->partition) {
272			ret = -ENOMEM;
273			goto err_chunks;
274		}
275	}
276
277	cur_start = 0;
278	list_for_each_entry(block, &reserve_list, list) {
279		/* can only happen if sections overlap */
280		if (block->start < cur_start) {
281			dev_err(sram->dev,
282				"block at 0x%x starts after current offset 0x%lx\n",
283				block->start, cur_start);
284			ret = -EINVAL;
285			sram_free_partitions(sram);
286			goto err_chunks;
287		}
288
289		if ((block->export || block->pool || block->protect_exec) &&
290		    block->size) {
291			ret = sram_add_partition(sram, block,
292						 res->start + block->start);
293			if (ret) {
294				sram_free_partitions(sram);
295				goto err_chunks;
296			}
297		}
298
299		/* current start is in a reserved block, so continue after it */
300		if (block->start == cur_start) {
301			cur_start = block->start + block->size;
302			continue;
303		}
304
305		/*
306		 * allocate the space between the current starting
307		 * address and the following reserved block, or the
308		 * end of the region.
309		 */
310		cur_size = block->start - cur_start;
311
312		if (sram->pool) {
313			dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
314				cur_start, cur_start + cur_size);
315
316			ret = gen_pool_add_virt(sram->pool,
317					(unsigned long)sram->virt_base + cur_start,
318					res->start + cur_start, cur_size, -1);
319			if (ret < 0) {
320				sram_free_partitions(sram);
321				goto err_chunks;
322			}
323		}
324
325		/* next allocation after this reserved block */
326		cur_start = block->start + block->size;
327	}
328
329err_chunks:
330	of_node_put(child);
 
 
331	kfree(rblocks);
332
333	return ret;
334}
335
336static int atmel_securam_wait(void)
337{
338	struct regmap *regmap;
339	u32 val;
340
341	regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
342	if (IS_ERR(regmap))
343		return -ENODEV;
344
345	return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val,
346					val & AT91_SECUMOD_RAMRDY_READY,
347					10000, 500000);
348}
349
350static const struct sram_config atmel_securam_config = {
351	.init = atmel_securam_wait,
352};
353
354/*
355 * SYSRAM contains areas that are not accessible by the
356 * kernel, such as the first 256K that is reserved for TZ.
357 * Accesses to those areas (including speculative accesses)
358 * trigger SErrors. As such we must map only the areas of
359 * SYSRAM specified in the device tree.
360 */
361static const struct sram_config tegra_sysram_config = {
362	.map_only_reserved = true,
363};
364
365static const struct of_device_id sram_dt_ids[] = {
366	{ .compatible = "mmio-sram" },
367	{ .compatible = "atmel,sama5d2-securam", .data = &atmel_securam_config },
368	{ .compatible = "nvidia,tegra186-sysram", .data = &tegra_sysram_config },
369	{ .compatible = "nvidia,tegra194-sysram", .data = &tegra_sysram_config },
370	{ .compatible = "nvidia,tegra234-sysram", .data = &tegra_sysram_config },
371	{}
372};
373
374static int sram_probe(struct platform_device *pdev)
375{
376	const struct sram_config *config;
377	struct sram_dev *sram;
378	int ret;
379	struct resource *res;
380	struct clk *clk;
381
382	config = of_device_get_match_data(&pdev->dev);
383
384	sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
385	if (!sram)
386		return -ENOMEM;
387
388	sram->dev = &pdev->dev;
389	sram->no_memory_wc = of_property_read_bool(pdev->dev.of_node, "no-memory-wc");
390	sram->config = config;
391
392	if (!config || !config->map_only_reserved) {
393		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
394		if (sram->no_memory_wc)
395			sram->virt_base = devm_ioremap_resource(&pdev->dev, res);
396		else
397			sram->virt_base = devm_ioremap_resource_wc(&pdev->dev, res);
398		if (IS_ERR(sram->virt_base)) {
399			dev_err(&pdev->dev, "could not map SRAM registers\n");
400			return PTR_ERR(sram->virt_base);
401		}
402
403		sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
404						  NUMA_NO_NODE, NULL);
405		if (IS_ERR(sram->pool))
406			return PTR_ERR(sram->pool);
 
407	}
408
409	clk = devm_clk_get_optional_enabled(sram->dev, NULL);
410	if (IS_ERR(clk))
411		return PTR_ERR(clk);
 
 
 
 
 
 
 
 
412
413	ret = sram_reserve_regions(sram,
414			platform_get_resource(pdev, IORESOURCE_MEM, 0));
415	if (ret)
416		return ret;
417
 
 
 
 
 
 
418	platform_set_drvdata(pdev, sram);
419
420	if (config && config->init) {
421		ret = config->init();
 
422		if (ret)
423			goto err_free_partitions;
424	}
425
426	if (sram->pool)
427		dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
428			gen_pool_size(sram->pool) / 1024, sram->virt_base);
429
430	return 0;
431
432err_free_partitions:
433	sram_free_partitions(sram);
434
435	return ret;
436}
437
438static int sram_remove(struct platform_device *pdev)
439{
440	struct sram_dev *sram = platform_get_drvdata(pdev);
441
442	sram_free_partitions(sram);
443
444	if (sram->pool && gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
445		dev_err(sram->dev, "removed while SRAM allocated\n");
 
 
 
446
447	return 0;
448}
449
450static struct platform_driver sram_driver = {
451	.driver = {
452		.name = "sram",
453		.of_match_table = sram_dt_ids,
454	},
455	.probe = sram_probe,
456	.remove = sram_remove,
457};
458
459static int __init sram_init(void)
460{
461	return platform_driver_register(&sram_driver);
462}
463
464postcore_initcall(sram_init);
v4.10.11
 
  1/*
  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
 21#include <linux/clk.h>
 22#include <linux/delay.h>
 23#include <linux/genalloc.h>
 24#include <linux/io.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>
 33
 
 
 34#define SRAM_GRANULARITY	32
 35
 36struct sram_partition {
 37	void __iomem *base;
 38
 39	struct gen_pool *pool;
 40	struct bin_attribute battr;
 41	struct mutex lock;
 42};
 43
 44struct sram_dev {
 45	struct device *dev;
 46	void __iomem *virt_base;
 47
 48	struct gen_pool *pool;
 49	struct clk *clk;
 50
 51	struct sram_partition *partition;
 52	u32 partitions;
 53};
 54
 55struct sram_reserve {
 56	struct list_head list;
 57	u32 start;
 58	u32 size;
 59	bool export;
 60	bool pool;
 61	const char *label;
 62};
 63
 64static 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);
 73	memcpy_fromio(buf, part->base + pos, count);
 74	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);
 88	memcpy_toio(part->base + pos, buf, count);
 89	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
174static 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
183static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
184{
185	struct device_node *np = sram->dev->of_node, *child;
186	unsigned long size, cur_start, cur_size;
187	struct sram_reserve *rblocks, *block;
188	struct list_head reserve_list;
189	unsigned int nblocks, exports = 0;
190	const char *label;
191	int ret = 0;
192
193	INIT_LIST_HEAD(&reserve_list);
194
195	size = resource_size(res);
196
197	/*
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;
202	rblocks = kzalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
203	if (!rblocks)
204		return -ENOMEM;
205
206	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) {
212			dev_err(sram->dev,
213				"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) {
219			dev_err(sram->dev,
220				"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
230		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);
252			if (!block->label)
 
 
253				goto err_chunks;
 
254
255			dev_dbg(sram->dev, "found %sblock '%s' 0x%x-0x%x\n",
256				block->export ? "exported " : "", block->label,
257				block->start, block->start + block->size);
258		} else {
259			dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
260				block->start, block->start + block->size);
261		}
262
263		block++;
264	}
265	child = NULL;
266
267	/* the last chunk marks the end of the region */
268	rblocks[nblocks - 1].start = size;
269	rblocks[nblocks - 1].size = 0;
270	list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
271
272	list_sort(NULL, &reserve_list, sram_reserve_cmp);
273
274	if (exports) {
275		sram->partition = devm_kzalloc(sram->dev,
276				       exports * sizeof(*sram->partition),
277				       GFP_KERNEL);
278		if (!sram->partition) {
279			ret = -ENOMEM;
280			goto err_chunks;
281		}
282	}
283
284	cur_start = 0;
285	list_for_each_entry(block, &reserve_list, list) {
286		/* can only happen if sections overlap */
287		if (block->start < cur_start) {
288			dev_err(sram->dev,
289				"block at 0x%x starts after current offset 0x%lx\n",
290				block->start, cur_start);
291			ret = -EINVAL;
292			sram_free_partitions(sram);
293			goto err_chunks;
294		}
295
296		if ((block->export || block->pool) && block->size) {
 
297			ret = sram_add_partition(sram, block,
298						 res->start + block->start);
299			if (ret) {
300				sram_free_partitions(sram);
301				goto err_chunks;
302			}
303		}
304
305		/* current start is in a reserved block, so continue after it */
306		if (block->start == cur_start) {
307			cur_start = block->start + block->size;
308			continue;
309		}
310
311		/*
312		 * allocate the space between the current starting
313		 * address and the following reserved block, or the
314		 * end of the region.
315		 */
316		cur_size = block->start - cur_start;
317
318		dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
319			cur_start, cur_start + cur_size);
320
321		ret = gen_pool_add_virt(sram->pool,
322				(unsigned long)sram->virt_base + cur_start,
323				res->start + cur_start, cur_size, -1);
324		if (ret < 0) {
325			sram_free_partitions(sram);
326			goto err_chunks;
 
 
327		}
328
329		/* next allocation after this reserved block */
330		cur_start = block->start + block->size;
331	}
332
333 err_chunks:
334	if (child)
335		of_node_put(child);
336
337	kfree(rblocks);
338
339	return ret;
340}
341
342static int atmel_securam_wait(void)
343{
344	struct regmap *regmap;
345	u32 val;
346
347	regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-secumod");
348	if (IS_ERR(regmap))
349		return -ENODEV;
350
351	return regmap_read_poll_timeout(regmap, AT91_SECUMOD_RAMRDY, val,
352					val & AT91_SECUMOD_RAMRDY_READY,
353					10000, 500000);
354}
355
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
356static const struct of_device_id sram_dt_ids[] = {
357	{ .compatible = "mmio-sram" },
358	{ .compatible = "atmel,sama5d2-securam", .data = atmel_securam_wait },
 
 
 
359	{}
360};
361
362static int sram_probe(struct platform_device *pdev)
363{
 
364	struct sram_dev *sram;
 
365	struct resource *res;
366	size_t size;
367	int ret;
368	int (*init_func)(void);
369
370	sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
371	if (!sram)
372		return -ENOMEM;
373
374	sram->dev = &pdev->dev;
 
 
375
376	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
377	if (!res) {
378		dev_err(sram->dev, "found no memory resource\n");
379		return -EINVAL;
380	}
 
 
 
 
 
381
382	size = resource_size(res);
383
384	if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
385		dev_err(sram->dev, "could not request region for resource\n");
386		return -EBUSY;
387	}
388
389	if (of_property_read_bool(pdev->dev.of_node, "no-memory-wc"))
390		sram->virt_base = devm_ioremap(sram->dev, res->start, size);
391	else
392		sram->virt_base = devm_ioremap_wc(sram->dev, res->start, size);
393	if (!sram->virt_base)
394		return -ENOMEM;
395
396	sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
397					  NUMA_NO_NODE, NULL);
398	if (IS_ERR(sram->pool))
399		return PTR_ERR(sram->pool);
400
401	ret = sram_reserve_regions(sram, res);
 
402	if (ret)
403		return ret;
404
405	sram->clk = devm_clk_get(sram->dev, NULL);
406	if (IS_ERR(sram->clk))
407		sram->clk = NULL;
408	else
409		clk_prepare_enable(sram->clk);
410
411	platform_set_drvdata(pdev, sram);
412
413	init_func = of_device_get_match_data(&pdev->dev);
414	if (init_func) {
415		ret = init_func();
416		if (ret)
417			return ret;
418	}
419
420	dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
421		gen_pool_size(sram->pool) / 1024, sram->virt_base);
 
422
423	return 0;
 
 
 
 
 
424}
425
426static int sram_remove(struct platform_device *pdev)
427{
428	struct sram_dev *sram = platform_get_drvdata(pdev);
429
430	sram_free_partitions(sram);
431
432	if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
433		dev_err(sram->dev, "removed while SRAM allocated\n");
434
435	if (sram->clk)
436		clk_disable_unprepare(sram->clk);
437
438	return 0;
439}
440
441static struct platform_driver sram_driver = {
442	.driver = {
443		.name = "sram",
444		.of_match_table = sram_dt_ids,
445	},
446	.probe = sram_probe,
447	.remove = sram_remove,
448};
449
450static int __init sram_init(void)
451{
452	return platform_driver_register(&sram_driver);
453}
454
455postcore_initcall(sram_init);