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 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
77 dev_err(&pdev
->dev
, "no memory resource defined\n");
82 if (!resource_size(res
))
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);
93 dev_err(&pdev
->dev
, "create pool failed\n");
98 ret
= gen_pool_add_virt(info
->gpool
, (unsigned long)info
->sram_virt
,
99 info
->sram_phys
, info
->sram_size
, -1);
101 dev_err(&pdev
->dev
, "add new chunk failed\n");
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");
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
);
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
);
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
= {
152 .remove
= sram_remove
,
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");