Linux 5.7.6
[linux/fpc-iii.git] / arch / arm / mach-mmp / sram.c
blob6794e2db1ad5f5ae1f0ea7026b73bf5fb7894b38
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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.
8 * All Rights Reserved
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>
17 #include <linux/io.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 {
25 char *pool_name;
26 struct gen_pool *gpool;
27 int granularity;
29 phys_addr_t sram_phys;
30 void __iomem *sram_virt;
31 u32 sram_size;
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;
43 if (!pool_name)
44 return NULL;
46 mutex_lock(&sram_lock);
48 list_for_each_entry(info, &sram_bank_list, node)
49 if (!strcmp(pool_name, info->pool_name))
50 break;
52 mutex_unlock(&sram_lock);
54 if (&info->node == &sram_bank_list)
55 return NULL;
57 return info->gpool;
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;
65 struct resource *res;
66 int ret = 0;
68 if (!pdata || !pdata->pool_name)
69 return -ENODEV;
71 info = kzalloc(sizeof(*info), GFP_KERNEL);
72 if (!info)
73 return -ENOMEM;
75 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
76 if (res == NULL) {
77 dev_err(&pdev->dev, "no memory resource defined\n");
78 ret = -ENODEV;
79 goto out;
82 if (!resource_size(res))
83 return 0;
85 info->sram_phys = (phys_addr_t)res->start;
86 info->sram_size = resource_size(res);
87 info->sram_virt = ioremap(info->sram_phys, info->sram_size);
88 info->pool_name = kstrdup(pdata->pool_name, GFP_KERNEL);
89 info->granularity = pdata->granularity;
91 info->gpool = gen_pool_create(ilog2(info->granularity), -1);
92 if (!info->gpool) {
93 dev_err(&pdev->dev, "create pool failed\n");
94 ret = -ENOMEM;
95 goto create_pool_err;
98 ret = gen_pool_add_virt(info->gpool, (unsigned long)info->sram_virt,
99 info->sram_phys, info->sram_size, -1);
100 if (ret < 0) {
101 dev_err(&pdev->dev, "add new chunk failed\n");
102 ret = -ENOMEM;
103 goto add_chunk_err;
106 mutex_lock(&sram_lock);
107 list_add(&info->node, &sram_bank_list);
108 mutex_unlock(&sram_lock);
110 platform_set_drvdata(pdev, info);
112 dev_info(&pdev->dev, "initialized\n");
113 return 0;
115 add_chunk_err:
116 gen_pool_destroy(info->gpool);
117 create_pool_err:
118 iounmap(info->sram_virt);
119 kfree(info->pool_name);
120 out:
121 kfree(info);
122 return ret;
125 static int sram_remove(struct platform_device *pdev)
127 struct sram_bank_info *info;
129 info = platform_get_drvdata(pdev);
130 if (info == NULL)
131 return -ENODEV;
133 mutex_lock(&sram_lock);
134 list_del(&info->node);
135 mutex_unlock(&sram_lock);
137 gen_pool_destroy(info->gpool);
138 iounmap(info->sram_virt);
139 kfree(info->pool_name);
140 kfree(info);
141 return 0;
144 static const struct platform_device_id sram_id_table[] = {
145 { "asram", MMP_ASRAM },
146 { "isram", MMP_ISRAM },
150 static struct platform_driver sram_driver = {
151 .probe = sram_probe,
152 .remove = sram_remove,
153 .driver = {
154 .name = "mmp-sram",
156 .id_table = sram_id_table,
159 static int __init sram_init(void)
161 return platform_driver_register(&sram_driver);
163 core_initcall(sram_init);
165 MODULE_LICENSE("GPL");