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/genalloc.h>
24 #include <linux/list_sort.h>
25 #include <linux/of_address.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
29 #define SRAM_GRANULARITY 32
33 void __iomem *virt_base;
35 struct gen_pool *pool;
40 struct list_head list;
45 static int sram_reserve_cmp(void *priv, struct list_head *a,
48 struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
49 struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
51 return ra->start - rb->start;
54 static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
56 struct device_node *np = sram->dev->of_node, *child;
57 unsigned long size, cur_start, cur_size;
58 struct sram_reserve *rblocks, *block;
59 struct list_head reserve_list;
63 INIT_LIST_HEAD(&reserve_list);
65 size = resource_size(res);
68 * We need an additional block to mark the end of the memory region
69 * after the reserved blocks from the dt are processed.
71 nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
72 rblocks = kmalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
77 for_each_available_child_of_node(np, child) {
78 struct resource child_res;
80 ret = of_address_to_resource(child, 0, &child_res);
83 "could not get address for node %s\n",
89 if (child_res.start < res->start || child_res.end > res->end) {
91 "reserved block %s outside the sram area\n",
98 block->start = child_res.start - res->start;
99 block->size = resource_size(&child_res);
100 list_add_tail(&block->list, &reserve_list);
102 dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
103 block->start, block->start + block->size);
108 /* the last chunk marks the end of the region */
109 rblocks[nblocks - 1].start = size;
110 rblocks[nblocks - 1].size = 0;
111 list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
113 list_sort(NULL, &reserve_list, sram_reserve_cmp);
117 list_for_each_entry(block, &reserve_list, list) {
118 /* can only happen if sections overlap */
119 if (block->start < cur_start) {
121 "block at 0x%x starts after current offset 0x%lx\n",
122 block->start, cur_start);
127 /* current start is in a reserved block, so continue after it */
128 if (block->start == cur_start) {
129 cur_start = block->start + block->size;
134 * allocate the space between the current starting
135 * address and the following reserved block, or the
138 cur_size = block->start - cur_start;
140 dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
141 cur_start, cur_start + cur_size);
143 ret = gen_pool_add_virt(sram->pool,
144 (unsigned long)sram->virt_base + cur_start,
145 res->start + cur_start, cur_size, -1);
149 /* next allocation after this reserved block */
150 cur_start = block->start + block->size;
159 static int sram_probe(struct platform_device *pdev)
161 struct sram_dev *sram;
162 struct resource *res;
166 sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
170 sram->dev = &pdev->dev;
172 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
174 dev_err(sram->dev, "found no memory resource\n");
178 size = resource_size(res);
180 if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
181 dev_err(sram->dev, "could not request region for resource\n");
185 sram->virt_base = devm_ioremap_wc(sram->dev, res->start, size);
186 if (IS_ERR(sram->virt_base))
187 return PTR_ERR(sram->virt_base);
189 sram->pool = devm_gen_pool_create(sram->dev, ilog2(SRAM_GRANULARITY),
191 if (IS_ERR(sram->pool))
192 return PTR_ERR(sram->pool);
194 ret = sram_reserve_regions(sram, res);
198 sram->clk = devm_clk_get(sram->dev, NULL);
199 if (IS_ERR(sram->clk))
202 clk_prepare_enable(sram->clk);
204 platform_set_drvdata(pdev, sram);
206 dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
207 gen_pool_size(sram->pool) / 1024, sram->virt_base);
212 static int sram_remove(struct platform_device *pdev)
214 struct sram_dev *sram = platform_get_drvdata(pdev);
216 if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
217 dev_err(sram->dev, "removed while SRAM allocated\n");
220 clk_disable_unprepare(sram->clk);
226 static const struct of_device_id sram_dt_ids[] = {
227 { .compatible = "mmio-sram" },
232 static struct platform_driver sram_driver = {
235 .of_match_table = of_match_ptr(sram_dt_ids),
238 .remove = sram_remove,
241 static int __init sram_init(void)
243 return platform_driver_register(&sram_driver);
246 postcore_initcall(sram_init);