Merge branch develop-3.10
[firefly-linux-kernel-4.4.55.git] / drivers / misc / sram.c
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 #include <linux/platform_data/sram.h>
32
33 #define SRAM_GRANULARITY        32
34
35 struct sram_dev {
36         struct gen_pool *pool;
37         struct clk *clk;
38 };
39
40 static int sram_probe(struct platform_device *pdev)
41 {
42         struct sram_platform_data *pdata = pdev->dev.platform_data;
43         void __iomem *virt_base;
44         struct sram_dev *sram;
45         struct resource *res;
46         unsigned long size;
47         bool map_exec = false;
48         int ret;
49
50         if (of_get_property(pdev->dev.of_node, "map-exec", NULL))
51                 map_exec = true;
52         if (pdata && pdata->map_exec)
53                 map_exec |= true;
54
55         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
56         if (res && of_get_property(pdev->dev.of_node, "map-cacheable", NULL))
57                 res->flags |= IORESOURCE_CACHEABLE;
58         if (map_exec)
59                 virt_base = devm_ioremap_exec_resource(&pdev->dev, res);
60         else
61                 virt_base = devm_ioremap_resource(&pdev->dev, res);
62         if (IS_ERR(virt_base))
63                 return PTR_ERR(virt_base);
64
65         size = resource_size(res);
66
67         sram = devm_kzalloc(&pdev->dev, sizeof(*sram), GFP_KERNEL);
68         if (!sram)
69                 return -ENOMEM;
70
71         sram->clk = devm_clk_get(&pdev->dev, NULL);
72         if (IS_ERR(sram->clk))
73                 sram->clk = NULL;
74         else
75                 clk_prepare_enable(sram->clk);
76
77         sram->pool = devm_gen_pool_create(&pdev->dev, ilog2(SRAM_GRANULARITY), -1);
78         if (!sram->pool)
79                 return -ENOMEM;
80
81         ret = gen_pool_add_virt(sram->pool, (unsigned long)virt_base,
82                                 res->start, size, -1);
83         if (ret < 0) {
84                 gen_pool_destroy(sram->pool);
85                 return ret;
86         }
87
88         platform_set_drvdata(pdev, sram);
89
90         dev_dbg(&pdev->dev, "SRAM pool: %ld KiB @ 0x%p\n", size / 1024, virt_base);
91
92         return 0;
93 }
94
95 static int sram_remove(struct platform_device *pdev)
96 {
97         struct sram_dev *sram = platform_get_drvdata(pdev);
98
99         if (gen_pool_avail(sram->pool) < gen_pool_size(sram->pool))
100                 dev_dbg(&pdev->dev, "removed while SRAM allocated\n");
101
102         gen_pool_destroy(sram->pool);
103
104         if (sram->clk)
105                 clk_disable_unprepare(sram->clk);
106
107         return 0;
108 }
109
110 #ifdef CONFIG_OF
111 static struct of_device_id sram_dt_ids[] = {
112         { .compatible = "mmio-sram" },
113         {}
114 };
115 #endif
116
117 static struct platform_driver sram_driver = {
118         .driver = {
119                 .name = "sram",
120                 .of_match_table = of_match_ptr(sram_dt_ids),
121         },
122         .probe = sram_probe,
123         .remove = sram_remove,
124 };
125
126 static int __init sram_init(void)
127 {
128         return platform_driver_register(&sram_driver);
129 }
130
131 postcore_initcall(sram_init);