Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.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         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
80         if (res == NULL) {
81                 dev_err(&pdev->dev, "no memory resource defined\n");
82                 ret = -ENODEV;
83                 goto out;
84         }
85
86         if (!resource_size(res))
87                 return 0;
88
89         info->sram_phys   = (phys_addr_t)res->start;
90         info->sram_size   = resource_size(res);
91         info->sram_virt   = ioremap(info->sram_phys, info->sram_size);
92         info->pool_name   = kstrdup(pdata->pool_name, GFP_KERNEL);
93         info->granularity = pdata->granularity;
94
95         info->gpool = gen_pool_create(ilog2(info->granularity), -1);
96         if (!info->gpool) {
97                 dev_err(&pdev->dev, "create pool failed\n");
98                 ret = -ENOMEM;
99                 goto create_pool_err;
100         }
101
102         ret = gen_pool_add_virt(info->gpool, (unsigned long)info->sram_virt,
103                                 info->sram_phys, info->sram_size, -1);
104         if (ret < 0) {
105                 dev_err(&pdev->dev, "add new chunk failed\n");
106                 ret = -ENOMEM;
107                 goto add_chunk_err;
108         }
109
110         mutex_lock(&sram_lock);
111         list_add(&info->node, &sram_bank_list);
112         mutex_unlock(&sram_lock);
113
114         platform_set_drvdata(pdev, info);
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         if (info == NULL)
135                 return -ENODEV;
136
137         mutex_lock(&sram_lock);
138         list_del(&info->node);
139         mutex_unlock(&sram_lock);
140
141         gen_pool_destroy(info->gpool);
142         iounmap(info->sram_virt);
143         kfree(info->pool_name);
144         kfree(info);
145         return 0;
146 }
147
148 static const struct platform_device_id sram_id_table[] = {
149         { "asram", MMP_ASRAM },
150         { "isram", MMP_ISRAM },
151         { }
152 };
153
154 static struct platform_driver sram_driver = {
155         .probe          = sram_probe,
156         .remove         = sram_remove,
157         .driver         = {
158                 .name   = "mmp-sram",
159         },
160         .id_table       = sram_id_table,
161 };
162
163 static int __init sram_init(void)
164 {
165         return platform_driver_register(&sram_driver);
166 }
167 core_initcall(sram_init);
168
169 MODULE_LICENSE("GPL");