Commit | Line | Data |
---|---|---|
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> | |
27 | #include <linux/platform_device.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/genalloc.h> | |
31 | ||
32 | #define SRAM_GRANULARITY 32 | |
33 | ||
34 | struct sram_dev { | |
35 | struct gen_pool *pool; | |
36 | struct clk *clk; | |
37 | }; | |
38 | ||
39 | static int sram_probe(struct platform_device *pdev) | |
40 | { | |
41 | void __iomem *virt_base; | |
42 | struct sram_dev *sram; | |
43 | struct resource *res; | |
44 | unsigned long size; | |
45 | int ret; | |
46 | ||
47 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
21b066f1 LN |
48 | virt_base = devm_ioremap_resource(&pdev->dev, res); |
49 | if (IS_ERR(virt_base)) | |
50 | return PTR_ERR(virt_base); | |
51 | ||
f3cbfa5d | 52 | size = resource_size(res); |
4984c6f5 PZ |
53 | |
54 | sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL); | |
55 | if (!sram) | |
56 | return -ENOMEM; | |
57 | ||
58 | sram->clk = devm_clk_get(&pdev->dev, NULL); | |
59 | if (IS_ERR(sram->clk)) | |
60 | sram->clk = NULL; | |
61 | else | |
62 | clk_prepare_enable(sram->clk); | |
63 | ||
64 | sram->pool = devm_gen_pool_create(&pdev->dev, ilog2(SRAM_GRANULARITY), -1); | |
65 | if (!sram->pool) | |
66 | return -ENOMEM; | |
67 | ||
68 | ret = gen_pool_add_virt(sram->pool, (unsigned long)virt_base, | |
69 | res->start, size, -1); | |
70 | if (ret < 0) { | |
5f90b9b4 HS |
71 | if (sram->clk) |
72 | clk_disable_unprepare(sram->clk); | |
4984c6f5 PZ |
73 | return ret; |
74 | } | |
75 | ||
76 | platform_set_drvdata(pdev, sram); | |
77 | ||
78 | dev_dbg(&pdev->dev, "SRAM pool: %ld KiB @ 0x%p\n", size / 1024, virt_base); | |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
83 | static int sram_remove(struct platform_device *pdev) | |
84 | { | |
85 | struct sram_dev *sram = platform_get_drvdata(pdev); | |
86 | ||
87 | if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool)) | |
88 | dev_dbg(&pdev->dev, "removed while SRAM allocated\n"); | |
89 | ||
90 | gen_pool_destroy(sram->pool); | |
91 | ||
92 | if (sram->clk) | |
93 | clk_disable_unprepare(sram->clk); | |
94 | ||
95 | return 0; | |
96 | } | |
97 | ||
98 | #ifdef CONFIG_OF | |
99 | static struct of_device_id sram_dt_ids[] = { | |
100 | { .compatible = "mmio-sram" }, | |
101 | {} | |
102 | }; | |
103 | #endif | |
104 | ||
105 | static struct platform_driver sram_driver = { | |
106 | .driver = { | |
107 | .name = "sram", | |
108 | .of_match_table = of_match_ptr(sram_dt_ids), | |
109 | }, | |
110 | .probe = sram_probe, | |
111 | .remove = sram_remove, | |
112 | }; | |
113 | ||
114 | static int __init sram_init(void) | |
115 | { | |
116 | return platform_driver_register(&sram_driver); | |
117 | } | |
118 | ||
119 | postcore_initcall(sram_init); |