blob: 9ecc7d5ed4f8e7530e6fcf4a2179a1b7839bbc8e [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
21#include <linux/kernel.h>
22#include <linux/init.h>
23#include <linux/clk.h>
24#include <linux/err.h>
25#include <linux/io.h>
26#include <linux/of.h>
Heiko Stübner2da19682014-02-25 12:46:34 +010027#include <linux/of_address.h>
28#include <linux/list.h>
29#include <linux/list_sort.h>
Philipp Zabel4984c6f2013-04-29 16:17:12 -070030#include <linux/platform_device.h>
31#include <linux/slab.h>
32#include <linux/spinlock.h>
33#include <linux/genalloc.h>
34
35#define SRAM_GRANULARITY 32
36
37struct sram_dev {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +030038 struct device *dev;
39 void __iomem *virt_base;
40
Philipp Zabel4984c6f2013-04-29 16:17:12 -070041 struct gen_pool *pool;
42 struct clk *clk;
43};
44
Heiko Stübner2da19682014-02-25 12:46:34 +010045struct sram_reserve {
46 struct list_head list;
47 u32 start;
48 u32 size;
49};
50
51static int sram_reserve_cmp(void *priv, struct list_head *a,
52 struct list_head *b)
53{
54 struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
55 struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
56
57 return ra->start - rb->start;
58}
59
Philipp Zabel4984c6f2013-04-29 16:17:12 -070060static int sram_probe(struct platform_device *pdev)
61{
Philipp Zabel4984c6f2013-04-29 16:17:12 -070062 struct sram_dev *sram;
63 struct resource *res;
Heiko Stübner2da19682014-02-25 12:46:34 +010064 struct device_node *np = pdev->dev.of_node, *child;
65 unsigned long size, cur_start, cur_size;
66 struct sram_reserve *rblocks, *block;
67 struct list_head reserve_list;
68 unsigned int nblocks;
Philipp Zabel4984c6f2013-04-29 16:17:12 -070069 int ret;
70
Heiko Stübner2da19682014-02-25 12:46:34 +010071 INIT_LIST_HEAD(&reserve_list);
72
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +030073 sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
74 if (!sram)
75 return -ENOMEM;
76
77 sram->dev = &pdev->dev;
78
Philipp Zabel4984c6f2013-04-29 16:17:12 -070079 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Abhilash Kesavan0ab163ad2015-02-06 19:15:28 +053080 if (!res) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +030081 dev_err(sram->dev, "found no memory resource\n");
Abhilash Kesavan0ab163ad2015-02-06 19:15:28 +053082 return -EINVAL;
83 }
Laurent Navet21b066f2013-05-02 14:39:34 +020084
Alexander Shiyanf3cbfa52013-06-10 18:43:53 +040085 size = resource_size(res);
Philipp Zabel4984c6f2013-04-29 16:17:12 -070086
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +030087 if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
88 dev_err(sram->dev, "could not request region for resource\n");
Abhilash Kesavan0ab163ad2015-02-06 19:15:28 +053089 return -EBUSY;
90 }
91
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +030092 sram->virt_base = devm_ioremap_wc(sram->dev, res->start, size);
93 if (IS_ERR(sram->virt_base))
94 return PTR_ERR(sram->virt_base);
Abhilash Kesavan0ab163ad2015-02-06 19:15:28 +053095
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +030096 sram->pool = devm_gen_pool_create(sram->dev,
97 ilog2(SRAM_GRANULARITY), -1);
Philipp Zabel4984c6f2013-04-29 16:17:12 -070098 if (!sram->pool)
99 return -ENOMEM;
100
Heiko Stübner2da19682014-02-25 12:46:34 +0100101 /*
102 * We need an additional block to mark the end of the memory region
103 * after the reserved blocks from the dt are processed.
104 */
105 nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
106 rblocks = kmalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300107 if (!rblocks)
108 return -ENOMEM;
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700109
Heiko Stübner2da19682014-02-25 12:46:34 +0100110 block = &rblocks[0];
111 for_each_available_child_of_node(np, child) {
112 struct resource child_res;
113
114 ret = of_address_to_resource(child, 0, &child_res);
115 if (ret < 0) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300116 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100117 "could not get address for node %s\n",
118 child->full_name);
Vladimir Zapolskiyb13365b2015-06-01 15:29:55 +0300119 of_node_put(child);
Heiko Stübner2da19682014-02-25 12:46:34 +0100120 goto err_chunks;
121 }
122
123 if (child_res.start < res->start || child_res.end > res->end) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300124 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100125 "reserved block %s outside the sram area\n",
126 child->full_name);
127 ret = -EINVAL;
Vladimir Zapolskiyb13365b2015-06-01 15:29:55 +0300128 of_node_put(child);
Heiko Stübner2da19682014-02-25 12:46:34 +0100129 goto err_chunks;
130 }
131
132 block->start = child_res.start - res->start;
133 block->size = resource_size(&child_res);
134 list_add_tail(&block->list, &reserve_list);
135
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300136 dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
137 block->start, block->start + block->size);
Heiko Stübner2da19682014-02-25 12:46:34 +0100138
139 block++;
140 }
141
142 /* the last chunk marks the end of the region */
143 rblocks[nblocks - 1].start = size;
144 rblocks[nblocks - 1].size = 0;
145 list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
146
147 list_sort(NULL, &reserve_list, sram_reserve_cmp);
148
149 cur_start = 0;
150
151 list_for_each_entry(block, &reserve_list, list) {
152 /* can only happen if sections overlap */
153 if (block->start < cur_start) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300154 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100155 "block at 0x%x starts after current offset 0x%lx\n",
156 block->start, cur_start);
157 ret = -EINVAL;
158 goto err_chunks;
159 }
160
161 /* current start is in a reserved block, so continue after it */
162 if (block->start == cur_start) {
163 cur_start = block->start + block->size;
164 continue;
165 }
166
167 /*
168 * allocate the space between the current starting
169 * address and the following reserved block, or the
170 * end of the region.
171 */
172 cur_size = block->start - cur_start;
173
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300174 dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
Heiko Stübner2da19682014-02-25 12:46:34 +0100175 cur_start, cur_start + cur_size);
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300176
Heiko Stübner2da19682014-02-25 12:46:34 +0100177 ret = gen_pool_add_virt(sram->pool,
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300178 (unsigned long)sram->virt_base + cur_start,
Heiko Stübner2da19682014-02-25 12:46:34 +0100179 res->start + cur_start, cur_size, -1);
180 if (ret < 0)
181 goto err_chunks;
182
183 /* next allocation after this reserved block */
184 cur_start = block->start + block->size;
185 }
186
187 kfree(rblocks);
188
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300189 sram->clk = devm_clk_get(sram->dev, NULL);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300190 if (IS_ERR(sram->clk))
191 sram->clk = NULL;
192 else
193 clk_prepare_enable(sram->clk);
194
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700195 platform_set_drvdata(pdev, sram);
196
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300197 dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
198 gen_pool_size(sram->pool) / 1024, sram->virt_base);
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700199
200 return 0;
Heiko Stübner2da19682014-02-25 12:46:34 +0100201
202err_chunks:
203 kfree(rblocks);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300204
Heiko Stübner2da19682014-02-25 12:46:34 +0100205 return ret;
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700206}
207
208static int sram_remove(struct platform_device *pdev)
209{
210 struct sram_dev *sram = platform_get_drvdata(pdev);
211
212 if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300213 dev_err(sram->dev, "removed while SRAM allocated\n");
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700214
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700215 if (sram->clk)
216 clk_disable_unprepare(sram->clk);
217
218 return 0;
219}
220
221#ifdef CONFIG_OF
Fabian Frederick6893d9b2015-03-16 20:20:26 +0100222static const struct of_device_id sram_dt_ids[] = {
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700223 { .compatible = "mmio-sram" },
224 {}
225};
226#endif
227
228static struct platform_driver sram_driver = {
229 .driver = {
230 .name = "sram",
231 .of_match_table = of_match_ptr(sram_dt_ids),
232 },
233 .probe = sram_probe,
234 .remove = sram_remove,
235};
236
237static int __init sram_init(void)
238{
239 return platform_driver_register(&sram_driver);
240}
241
242postcore_initcall(sram_init);