1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/mach-mmp/sram.c
5 * based on mach-davinci/sram.c - DaVinci simple SRAM allocator
7 * Copyright (c) 2011 Marvell Semiconductors Inc.
10 * Add for mmp sram support - Leo Yan <leoy@marvell.com>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/init.h>
16 #include <linux/platform_device.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/genalloc.h>
22 #include <linux/platform_data/dma-mmp_tdma.h>
24 struct sram_bank_info {
26 struct gen_pool *gpool;
29 phys_addr_t sram_phys;
30 void __iomem *sram_virt;
33 struct list_head node;
36 static DEFINE_MUTEX(sram_lock);
37 static LIST_HEAD(sram_bank_list);
39 struct gen_pool *sram_get_gpool(char *pool_name)
41 struct sram_bank_info *info = NULL;
46 mutex_lock(&sram_lock);
48 list_for_each_entry(info, &sram_bank_list, node)
49 if (!strcmp(pool_name, info->pool_name))
52 mutex_unlock(&sram_lock);
54 if (&info->node == &sram_bank_list)
59 EXPORT_SYMBOL(sram_get_gpool);
61 static int sram_probe(struct platform_device *pdev)
63 struct sram_platdata *pdata = pdev->dev.platform_data;
64 struct sram_bank_info *info;
68 if (!pdata || !pdata->pool_name)
71 info = kzalloc(sizeof(*info), GFP_KERNEL);
75 platform_set_drvdata(pdev, info);
77 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
79 dev_err(&pdev->dev, "no memory resource defined\n");
84 if (!resource_size(res))
87 info->sram_phys = (phys_addr_t)res->start;
88 info->sram_size = resource_size(res);
89 info->sram_virt = ioremap(info->sram_phys, info->sram_size);
90 info->pool_name = kstrdup(pdata->pool_name, GFP_KERNEL);
91 info->granularity = pdata->granularity;
93 info->gpool = gen_pool_create(ilog2(info->granularity), -1);
95 dev_err(&pdev->dev, "create pool failed\n");
100 ret = gen_pool_add_virt(info->gpool, (unsigned long)info->sram_virt,
101 info->sram_phys, info->sram_size, -1);
103 dev_err(&pdev->dev, "add new chunk failed\n");
108 mutex_lock(&sram_lock);
109 list_add(&info->node, &sram_bank_list);
110 mutex_unlock(&sram_lock);
112 dev_info(&pdev->dev, "initialized\n");
116 gen_pool_destroy(info->gpool);
118 iounmap(info->sram_virt);
119 kfree(info->pool_name);
125 static int sram_remove(struct platform_device *pdev)
127 struct sram_bank_info *info;
129 info = platform_get_drvdata(pdev);
131 if (info->sram_size) {
132 mutex_lock(&sram_lock);
133 list_del(&info->node);
134 mutex_unlock(&sram_lock);
136 gen_pool_destroy(info->gpool);
137 iounmap(info->sram_virt);
138 kfree(info->pool_name);
146 static const struct platform_device_id sram_id_table[] = {
147 { "asram", MMP_ASRAM },
148 { "isram", MMP_ISRAM },
152 static struct platform_driver sram_driver = {
154 .remove = sram_remove,
158 .id_table = sram_id_table,
161 static int __init sram_init(void)
163 return platform_driver_register(&sram_driver);
165 core_initcall(sram_init);
167 MODULE_LICENSE("GPL");