misc: sram: add private struct device and virt_base members
[deliverable/linux.git] / drivers / misc / sram.c
CommitLineData
4984c6f5
PZ
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/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>
2da19688
HS
27#include <linux/of_address.h>
28#include <linux/list.h>
29#include <linux/list_sort.h>
4984c6f5
PZ
30#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 {
665d82fb
VZ
38 struct device *dev;
39 void __iomem *virt_base;
40
4984c6f5
PZ
41 struct gen_pool *pool;
42 struct clk *clk;
43};
44
2da19688
HS
45struct 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
4984c6f5
PZ
60static int sram_probe(struct platform_device *pdev)
61{
4984c6f5
PZ
62 struct sram_dev *sram;
63 struct resource *res;
2da19688
HS
64 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;
4984c6f5
PZ
69 int ret;
70
2da19688
HS
71 INIT_LIST_HEAD(&reserve_list);
72
665d82fb
VZ
73 sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
74 if (!sram)
75 return -ENOMEM;
76
77 sram->dev = &pdev->dev;
78
4984c6f5 79 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0ab163ad 80 if (!res) {
665d82fb 81 dev_err(sram->dev, "found no memory resource\n");
0ab163ad
AK
82 return -EINVAL;
83 }
21b066f1 84
f3cbfa5d 85 size = resource_size(res);
4984c6f5 86
665d82fb
VZ
87 if (!devm_request_mem_region(sram->dev, res->start, size, pdev->name)) {
88 dev_err(sram->dev, "could not request region for resource\n");
0ab163ad
AK
89 return -EBUSY;
90 }
91
665d82fb
VZ
92 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);
4984c6f5 95
665d82fb
VZ
96 sram->pool = devm_gen_pool_create(sram->dev,
97 ilog2(SRAM_GRANULARITY), -1);
4984c6f5
PZ
98 if (!sram->pool)
99 return -ENOMEM;
100
2da19688
HS
101 /*
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);
ee895ccd
VZ
107 if (!rblocks)
108 return -ENOMEM;
4984c6f5 109
2da19688
HS
110 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) {
665d82fb 116 dev_err(sram->dev,
2da19688
HS
117 "could not get address for node %s\n",
118 child->full_name);
b13365bb 119 of_node_put(child);
2da19688
HS
120 goto err_chunks;
121 }
122
123 if (child_res.start < res->start || child_res.end > res->end) {
665d82fb 124 dev_err(sram->dev,
2da19688
HS
125 "reserved block %s outside the sram area\n",
126 child->full_name);
127 ret = -EINVAL;
b13365bb 128 of_node_put(child);
2da19688
HS
129 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
665d82fb
VZ
136 dev_dbg(sram->dev, "found reserved block 0x%x-0x%x\n",
137 block->start, block->start + block->size);
2da19688
HS
138
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) {
665d82fb 154 dev_err(sram->dev,
2da19688
HS
155 "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
665d82fb 174 dev_dbg(sram->dev, "adding chunk 0x%lx-0x%lx\n",
2da19688 175 cur_start, cur_start + cur_size);
665d82fb 176
2da19688 177 ret = gen_pool_add_virt(sram->pool,
665d82fb 178 (unsigned long)sram->virt_base + cur_start,
2da19688
HS
179 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
665d82fb 189 sram->clk = devm_clk_get(sram->dev, NULL);
ee895ccd
VZ
190 if (IS_ERR(sram->clk))
191 sram->clk = NULL;
192 else
193 clk_prepare_enable(sram->clk);
194
4984c6f5
PZ
195 platform_set_drvdata(pdev, sram);
196
665d82fb
VZ
197 dev_dbg(sram->dev, "SRAM pool: %zu KiB @ 0x%p\n",
198 gen_pool_size(sram->pool) / 1024, sram->virt_base);
4984c6f5
PZ
199
200 return 0;
2da19688
HS
201
202err_chunks:
203 kfree(rblocks);
ee895ccd 204
2da19688 205 return ret;
4984c6f5
PZ
206}
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))
665d82fb 213 dev_err(sram->dev, "removed while SRAM allocated\n");
4984c6f5 214
4984c6f5
PZ
215 if (sram->clk)
216 clk_disable_unprepare(sram->clk);
217
218 return 0;
219}
220
221#ifdef CONFIG_OF
6893d9b5 222static const struct of_device_id sram_dt_ids[] = {
4984c6f5
PZ
223 { .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);
This page took 0.138705 seconds and 5 git commands to generate.