blob: 76a23f9b5451836879dd36108812a83c8fb7b82d [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 {
38 struct gen_pool *pool;
39 struct clk *clk;
40};
41
Heiko Stübner2da19682014-02-25 12:46:34 +010042struct sram_reserve {
43 struct list_head list;
44 u32 start;
45 u32 size;
46};
47
48static int sram_reserve_cmp(void *priv, struct list_head *a,
49 struct list_head *b)
50{
51 struct sram_reserve *ra = list_entry(a, struct sram_reserve, list);
52 struct sram_reserve *rb = list_entry(b, struct sram_reserve, list);
53
54 return ra->start - rb->start;
55}
56
Philipp Zabel4984c6f2013-04-29 16:17:12 -070057static int sram_probe(struct platform_device *pdev)
58{
59 void __iomem *virt_base;
60 struct sram_dev *sram;
61 struct resource *res;
Heiko Stübner2da19682014-02-25 12:46:34 +010062 struct device_node *np = pdev->dev.of_node, *child;
63 unsigned long size, cur_start, cur_size;
64 struct sram_reserve *rblocks, *block;
65 struct list_head reserve_list;
66 unsigned int nblocks;
Philipp Zabel4984c6f2013-04-29 16:17:12 -070067 int ret;
68
Heiko Stübner2da19682014-02-25 12:46:34 +010069 INIT_LIST_HEAD(&reserve_list);
70
Philipp Zabel4984c6f2013-04-29 16:17:12 -070071 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Abhilash Kesavan0ab163ad2015-02-06 19:15:28 +053072 if (!res) {
73 dev_err(&pdev->dev, "found no memory resource\n");
74 return -EINVAL;
75 }
Laurent Navet21b066f2013-05-02 14:39:34 +020076
Alexander Shiyanf3cbfa52013-06-10 18:43:53 +040077 size = resource_size(res);
Philipp Zabel4984c6f2013-04-29 16:17:12 -070078
Abhilash Kesavan0ab163ad2015-02-06 19:15:28 +053079 if (!devm_request_mem_region(&pdev->dev,
80 res->start, size, pdev->name)) {
81 dev_err(&pdev->dev, "could not request region for resource\n");
82 return -EBUSY;
83 }
84
85 virt_base = devm_ioremap_wc(&pdev->dev, res->start, size);
86 if (IS_ERR(virt_base))
87 return PTR_ERR(virt_base);
88
Philipp Zabel4984c6f2013-04-29 16:17:12 -070089 sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
90 if (!sram)
91 return -ENOMEM;
92
Philipp Zabel4984c6f2013-04-29 16:17:12 -070093 sram->pool = devm_gen_pool_create(&pdev->dev, ilog2(SRAM_GRANULARITY), -1);
94 if (!sram->pool)
95 return -ENOMEM;
96
Heiko Stübner2da19682014-02-25 12:46:34 +010097 /*
98 * We need an additional block to mark the end of the memory region
99 * after the reserved blocks from the dt are processed.
100 */
101 nblocks = (np) ? of_get_available_child_count(np) + 1 : 1;
102 rblocks = kmalloc((nblocks) * sizeof(*rblocks), GFP_KERNEL);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300103 if (!rblocks)
104 return -ENOMEM;
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700105
Heiko Stübner2da19682014-02-25 12:46:34 +0100106 block = &rblocks[0];
107 for_each_available_child_of_node(np, child) {
108 struct resource child_res;
109
110 ret = of_address_to_resource(child, 0, &child_res);
111 if (ret < 0) {
112 dev_err(&pdev->dev,
113 "could not get address for node %s\n",
114 child->full_name);
115 goto err_chunks;
116 }
117
118 if (child_res.start < res->start || child_res.end > res->end) {
119 dev_err(&pdev->dev,
120 "reserved block %s outside the sram area\n",
121 child->full_name);
122 ret = -EINVAL;
123 goto err_chunks;
124 }
125
126 block->start = child_res.start - res->start;
127 block->size = resource_size(&child_res);
128 list_add_tail(&block->list, &reserve_list);
129
130 dev_dbg(&pdev->dev, "found reserved block 0x%x-0x%x\n",
131 block->start,
132 block->start + block->size);
133
134 block++;
135 }
136
137 /* the last chunk marks the end of the region */
138 rblocks[nblocks - 1].start = size;
139 rblocks[nblocks - 1].size = 0;
140 list_add_tail(&rblocks[nblocks - 1].list, &reserve_list);
141
142 list_sort(NULL, &reserve_list, sram_reserve_cmp);
143
144 cur_start = 0;
145
146 list_for_each_entry(block, &reserve_list, list) {
147 /* can only happen if sections overlap */
148 if (block->start < cur_start) {
149 dev_err(&pdev->dev,
150 "block at 0x%x starts after current offset 0x%lx\n",
151 block->start, cur_start);
152 ret = -EINVAL;
153 goto err_chunks;
154 }
155
156 /* current start is in a reserved block, so continue after it */
157 if (block->start == cur_start) {
158 cur_start = block->start + block->size;
159 continue;
160 }
161
162 /*
163 * allocate the space between the current starting
164 * address and the following reserved block, or the
165 * end of the region.
166 */
167 cur_size = block->start - cur_start;
168
169 dev_dbg(&pdev->dev, "adding chunk 0x%lx-0x%lx\n",
170 cur_start, cur_start + cur_size);
171 ret = gen_pool_add_virt(sram->pool,
172 (unsigned long)virt_base + cur_start,
173 res->start + cur_start, cur_size, -1);
174 if (ret < 0)
175 goto err_chunks;
176
177 /* next allocation after this reserved block */
178 cur_start = block->start + block->size;
179 }
180
181 kfree(rblocks);
182
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300183 sram->clk = devm_clk_get(&pdev->dev, NULL);
184 if (IS_ERR(sram->clk))
185 sram->clk = NULL;
186 else
187 clk_prepare_enable(sram->clk);
188
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700189 platform_set_drvdata(pdev, sram);
190
191 dev_dbg(&pdev->dev, "SRAM pool: %ld KiB @ 0x%p\n", size / 1024, virt_base);
192
193 return 0;
Heiko Stübner2da19682014-02-25 12:46:34 +0100194
195err_chunks:
196 kfree(rblocks);
Vladimir Zapolskiyee895cc2015-06-01 15:29:54 +0300197
Heiko Stübner2da19682014-02-25 12:46:34 +0100198 return ret;
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700199}
200
201static int sram_remove(struct platform_device *pdev)
202{
203 struct sram_dev *sram = platform_get_drvdata(pdev);
204
205 if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
206 dev_dbg(&pdev->dev, "removed while SRAM allocated\n");
207
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700208 if (sram->clk)
209 clk_disable_unprepare(sram->clk);
210
211 return 0;
212}
213
214#ifdef CONFIG_OF
Fabian Frederick6893d9b2015-03-16 20:20:26 +0100215static const struct of_device_id sram_dt_ids[] = {
Philipp Zabel4984c6f2013-04-29 16:17:12 -0700216 { .compatible = "mmio-sram" },
217 {}
218};
219#endif
220
221static struct platform_driver sram_driver = {
222 .driver = {
223 .name = "sram",
224 .of_match_table = of_match_ptr(sram_dt_ids),
225 },
226 .probe = sram_probe,
227 .remove = sram_remove,
228};
229
230static int __init sram_init(void)
231{
232 return platform_driver_register(&sram_driver);
233}
234
235postcore_initcall(sram_init);