GNU Linux-libre 4.19.314-gnu1
[releases.git] / arch / arm / mach-mmp / sram.c
1 /*
2  *  linux/arch/arm/mach-mmp/sram.c
3  *
4  *  based on mach-davinci/sram.c - DaVinci simple SRAM allocator
5  *
6  *  Copyright (c) 2011 Marvell Semiconductors Inc.
7  *  All Rights Reserved
8  *
9  *  Add for mmp sram support - Leo Yan <leoy@marvell.com>
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License version 2 as
13  *  published by the Free Software Foundation.
14  *
15  */
16
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
21 #include <linux/io.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/genalloc.h>
25
26 #include <linux/platform_data/dma-mmp_tdma.h>
27
28 struct sram_bank_info {
29         char *pool_name;
30         struct gen_pool *gpool;
31         int granularity;
32
33         phys_addr_t sram_phys;
34         void __iomem *sram_virt;
35         u32 sram_size;
36
37         struct list_head node;
38 };
39
40 static DEFINE_MUTEX(sram_lock);
41 static LIST_HEAD(sram_bank_list);
42
43 struct gen_pool *sram_get_gpool(char *pool_name)
44 {
45         struct sram_bank_info *info = NULL;
46
47         if (!pool_name)
48                 return NULL;
49
50         mutex_lock(&sram_lock);
51
52         list_for_each_entry(info, &sram_bank_list, node)
53                 if (!strcmp(pool_name, info->pool_name))
54                         break;
55
56         mutex_unlock(&sram_lock);
57
58         if (&info->node == &sram_bank_list)
59                 return NULL;
60
61         return info->gpool;
62 }
63 EXPORT_SYMBOL(sram_get_gpool);
64
65 static int sram_probe(struct platform_device *pdev)
66 {
67         struct sram_platdata *pdata = pdev->dev.platform_data;
68         struct sram_bank_info *info;
69         struct resource *res;
70         int ret = 0;
71
72         if (!pdata || !pdata->pool_name)
73                 return -ENODEV;
74
75         info = kzalloc(sizeof(*info), GFP_KERNEL);
76         if (!info)
77                 return -ENOMEM;
78
79         platform_set_drvdata(pdev, info);
80
81         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
82         if (res == NULL) {
83                 dev_err(&pdev->dev, "no memory resource defined\n");
84                 ret = -ENODEV;
85                 goto out;
86         }
87
88         if (!resource_size(res))
89                 return 0;
90
91         info->sram_phys   = (phys_addr_t)res->start;
92         info->sram_size   = resource_size(res);
93         info->sram_virt   = ioremap(info->sram_phys, info->sram_size);
94         info->pool_name   = kstrdup(pdata->pool_name, GFP_KERNEL);
95         info->granularity = pdata->granularity;
96
97         info->gpool = gen_pool_create(ilog2(info->granularity), -1);
98         if (!info->gpool) {
99                 dev_err(&pdev->dev, "create pool failed\n");
100                 ret = -ENOMEM;
101                 goto create_pool_err;
102         }
103
104         ret = gen_pool_add_virt(info->gpool, (unsigned long)info->sram_virt,
105                                 info->sram_phys, info->sram_size, -1);
106         if (ret < 0) {
107                 dev_err(&pdev->dev, "add new chunk failed\n");
108                 ret = -ENOMEM;
109                 goto add_chunk_err;
110         }
111
112         mutex_lock(&sram_lock);
113         list_add(&info->node, &sram_bank_list);
114         mutex_unlock(&sram_lock);
115
116         dev_info(&pdev->dev, "initialized\n");
117         return 0;
118
119 add_chunk_err:
120         gen_pool_destroy(info->gpool);
121 create_pool_err:
122         iounmap(info->sram_virt);
123         kfree(info->pool_name);
124 out:
125         kfree(info);
126         return ret;
127 }
128
129 static int sram_remove(struct platform_device *pdev)
130 {
131         struct sram_bank_info *info;
132
133         info = platform_get_drvdata(pdev);
134
135         if (info->sram_size) {
136                 mutex_lock(&sram_lock);
137                 list_del(&info->node);
138                 mutex_unlock(&sram_lock);
139
140                 gen_pool_destroy(info->gpool);
141                 iounmap(info->sram_virt);
142                 kfree(info->pool_name);
143         }
144
145         kfree(info);
146
147         return 0;
148 }
149
150 static const struct platform_device_id sram_id_table[] = {
151         { "asram", MMP_ASRAM },
152         { "isram", MMP_ISRAM },
153         { }
154 };
155
156 static struct platform_driver sram_driver = {
157         .probe          = sram_probe,
158         .remove         = sram_remove,
159         .driver         = {
160                 .name   = "mmp-sram",
161         },
162         .id_table       = sram_id_table,
163 };
164
165 static int __init sram_init(void)
166 {
167         return platform_driver_register(&sram_driver);
168 }
169 core_initcall(sram_init);
170
171 MODULE_LICENSE("GPL");