2 * linux/arch/arm/mach-mmp/sram.c
4 * based on mach-davinci/sram.c - DaVinci simple SRAM allocator
6 * Copyright (c) 2011 Marvell Semiconductors Inc.
9 * Add for mmp sram support - Leo Yan <leoy@marvell.com>
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.
17 #include <linux/module.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/init.h>
20 #include <linux/platform_device.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/genalloc.h>
26 #include <linux/platform_data/dma-mmp_tdma.h>
28 struct sram_bank_info {
30 struct gen_pool *gpool;
33 phys_addr_t sram_phys;
34 void __iomem *sram_virt;
37 struct list_head node;
40 static DEFINE_MUTEX(sram_lock);
41 static LIST_HEAD(sram_bank_list);
43 struct gen_pool *sram_get_gpool(char *pool_name)
45 struct sram_bank_info *info = NULL;
50 mutex_lock(&sram_lock);
52 list_for_each_entry(info, &sram_bank_list, node)
53 if (!strcmp(pool_name, info->pool_name))
56 mutex_unlock(&sram_lock);
58 if (&info->node == &sram_bank_list)
63 EXPORT_SYMBOL(sram_get_gpool);
65 static int sram_probe(struct platform_device *pdev)
67 struct sram_platdata *pdata = pdev->dev.platform_data;
68 struct sram_bank_info *info;
72 if (!pdata || !pdata->pool_name)
75 info = kzalloc(sizeof(*info), GFP_KERNEL);
79 platform_set_drvdata(pdev, info);
81 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
83 dev_err(&pdev->dev, "no memory resource defined\n");
88 if (!resource_size(res))
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;
97 info->gpool = gen_pool_create(ilog2(info->granularity), -1);
99 dev_err(&pdev->dev, "create pool failed\n");
101 goto create_pool_err;
104 ret = gen_pool_add_virt(info->gpool, (unsigned long)info->sram_virt,
105 info->sram_phys, info->sram_size, -1);
107 dev_err(&pdev->dev, "add new chunk failed\n");
112 mutex_lock(&sram_lock);
113 list_add(&info->node, &sram_bank_list);
114 mutex_unlock(&sram_lock);
116 dev_info(&pdev->dev, "initialized\n");
120 gen_pool_destroy(info->gpool);
122 iounmap(info->sram_virt);
123 kfree(info->pool_name);
129 static int sram_remove(struct platform_device *pdev)
131 struct sram_bank_info *info;
133 info = platform_get_drvdata(pdev);
135 if (info->sram_size) {
136 mutex_lock(&sram_lock);
137 list_del(&info->node);
138 mutex_unlock(&sram_lock);
140 gen_pool_destroy(info->gpool);
141 iounmap(info->sram_virt);
142 kfree(info->pool_name);
150 static const struct platform_device_id sram_id_table[] = {
151 { "asram", MMP_ASRAM },
152 { "isram", MMP_ISRAM },
156 static struct platform_driver sram_driver = {
158 .remove = sram_remove,
162 .id_table = sram_id_table,
165 static int __init sram_init(void)
167 return platform_driver_register(&sram_driver);
169 core_initcall(sram_init);
171 MODULE_LICENSE("GPL");