blob: 7502b6ceb3388440324e9dc858066dfec0605b38 [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
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +030060static int sram_reserve_regions(struct sram_dev *sram, struct resource *res)
Philipp Zabel4984c6f2013-04-29 16:17:12 -070061{
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +030062 struct device_node *np = sram->dev->of_node, *child;
Heiko Stübner2da19682014-02-25 12:46:34 +010063 unsigned long size, cur_start, cur_size;
64 struct sram_reserve *rblocks, *block;
65 struct list_head reserve_list;
66 unsigned int nblocks;
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +030067 int ret = 0;
Philipp Zabel4984c6f2013-04-29 16:17:12 -070068
Heiko Stübner2da19682014-02-25 12:46:34 +010069 INIT_LIST_HEAD(&reserve_list);
70
Alexander Shiyanf3cbfa52013-06-10 18:43:53 +040071 size = resource_size(res);
Philipp Zabel4984c6f2013-04-29 16:17:12 -070072
Heiko Stübner2da19682014-02-25 12:46:34 +010073 /*
74 * We need an additional block to mark the end of the memory region
75 * after the reserved blocks from the dt are processed.
76 */
77 nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
78 rblocks = kmalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +030079 if (!rblocks)
80 return -ENOMEM;
Philipp Zabel4984c6f2013-04-29 16:17:12 -070081
Heiko Stübner2da19682014-02-25 12:46:34 +010082 block = &rblocks[0];
83 for_each_available_child_of_node(np, child) {
84 struct resource child_res;
85
86 ret = of_address_to_resource(child, 0, &child_res);
87 if (ret < 0) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +030088 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +010089 "could not get address for node %s\n",
90 child->full_name);
Vladimir Zapolskiyb13365b2015-06-01 15:29:55 +030091 of_node_put(child);
Heiko Stübner2da19682014-02-25 12:46:34 +010092 goto err_chunks;
93 }
94
95 if (child_res.start < res->start || child_res.end > res->end) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +030096 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +010097 "reserved block %s outside the sram area\n",
98 child->full_name);
99 ret = -EINVAL;
Vladimir Zapolskiyb13365b2015-06-01 15:29:55 +0300100 of_node_put(child);
Heiko Stübner2da19682014-02-25 12:46:34 +0100101 goto err_chunks;
102 }
103
104 block->start = child_res.start - res->start;
105 block->size = resource_size(&child_res);
106 list_add_tail(&block->list, &reserve_list);
107
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300108 dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
109 block->start, block->start + block->size);
Heiko Stübner2da19682014-02-25 12:46:34 +0100110
111 block++;
112 }
113
114 /* the last chunk marks the end of the region */
115 rblocks[nblocks - 1].start = size;
116 rblocks[nblocks - 1].size = 0;
117 list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
118
119 list_sort(NULL, &reserve_list, sram_reserve_cmp);
120
121 cur_start = 0;
122
123 list_for_each_entry(block, &reserve_list, list) {
124 /* can only happen if sections overlap */
125 if (block->start < cur_start) {
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300126 dev_err(sram->dev,
Heiko Stübner2da19682014-02-25 12:46:34 +0100127 "block at 0x%x starts after current offset 0x%lx\n",
128 block->start, cur_start);
129 ret = -EINVAL;
130 goto err_chunks;
131 }
132
133 /* current start is in a reserved block, so continue after it */
134 if (block->start == cur_start) {
135 cur_start = block->start + block->size;
136 continue;
137 }
138
139 /*
140 * allocate the space between the current starting
141 * address and the following reserved block, or the
142 * end of the region.
143 */
144 cur_size = block->start - cur_start;
145
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300146 dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
Heiko Stübner2da19682014-02-25 12:46:34 +0100147 cur_start, cur_start + cur_size);
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300148
Heiko Stübner2da19682014-02-25 12:46:34 +0100149 ret = gen_pool_add_virt(sram->pool,
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300150 (unsigned long)sram->virt_base + cur_start,
Heiko Stübner2da19682014-02-25 12:46:34 +0100151 res->start + cur_start, cur_size, -1);
152 if (ret < 0)
153 goto err_chunks;
154
155 /* next allocation after this reserved block */
156 cur_start = block->start + block->size;
157 }
158
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300159 err_chunks:
Heiko Stübner2da19682014-02-25 12:46:34 +0100160 kfree(rblocks);
161
Vladimir Zapolskiya0a5be02015-06-01 15:29:59 +0300162 return ret;
163}
164
165static int sram_probe(struct platform_device *pdev)
166{
167 struct sram_dev *sram;
168 struct resource *res;
169 size_t size;
170 int ret;
171
172 sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
173 if (!sram)
174 return -ENOMEM;
175
176 sram->dev = &pdev->dev;
177
178 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
179 if (!res) {
180 dev_err(sram->dev, "found no memory resource\n");
181 return -EINVAL;
182 }
183
184 size = resource_size(res);
185
186 if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
187 dev_err(sram->dev, "could not request region for resource\n");
188 return -EBUSY;
189 }
190
191 sram->virt_base = devm_ioremap_wc(sram->dev, res->start, size);
192 if (IS_ERR(sram->virt_base))
193 return PTR_ERR(sram->virt_base);
194
195 sram->pool = devm_gen_pool_create(sram->dev,
196 ilog2(SRAM_GRANULARITY), -1);
197 if (!sram->pool)
198 return -ENOMEM;
199
200 ret = sram_reserve_regions(sram, res);
201 if (ret)
202 return ret;
203
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300204 sram->clk = devm_clk_get(sram->dev, NULL);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300205 if (IS_ERR(sram->clk))
206 sram->clk = NULL;
207 else
208 clk_prepare_enable(sram->clk);
209
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700210 platform_set_drvdata(pdev, sram);
211
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300212 dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
213 gen_pool_size(sram->pool) / 1024, sram->virt_base);
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700214
215 return 0;
216}
217
218static int sram_remove(struct platform_device *pdev)
219{
220 struct sram_dev *sram = platform_get_drvdata(pdev);
221
222 if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
Vladimir Zapolskiy665d82f2015-06-01 15:29:58 +0300223 dev_err(sram->dev, "removed while SRAM allocated\n");
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700224
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700225 if (sram->clk)
226 clk_disable_unprepare(sram->clk);
227
228 return 0;
229}
230
231#ifdef CONFIG_OF
Fabian Frederick6893d9b2015-03-16 20:20:26 +0100232static const struct of_device_id sram_dt_ids[] = {
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700233 { .compatible = "mmio-sram" },
234 {}
235};
236#endif
237
238static struct platform_driver sram_driver = {
239 .driver = {
240 .name = "sram",
241 .of_match_table = of_match_ptr(sram_dt_ids),
242 },
243 .probe = sram_probe,
244 .remove = sram_remove,
245};
246
247static int __init sram_init(void)
248{
249 return platform_driver_register(&sram_driver);
250}
251
252postcore_initcall(sram_init);