1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simple memory allocator for on-board SRAM
10 #include <linux/err.h>
11 #include <linux/kernel.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/string.h>
16 #include <linux/ioport.h>
18 #include <linux/of_address.h>
23 #include <linux/fsl/bestcomm/sram.h>
26 /* Struct keeping our 'state' */
27 struct bcom_sram *bcom_sram = NULL;
28 EXPORT_SYMBOL_GPL(bcom_sram); /* needed for inline functions */
31 /* ======================================================================== */
33 /* ======================================================================== */
34 /* DO NOT USE in interrupts, if needed in irq handler, we should use the
35 _irqsave version of the spin_locks */
37 int bcom_sram_init(struct device_node *sram_node, char *owner)
44 /* Create our state struct */
46 printk(KERN_ERR "%s: bcom_sram_init: "
47 "Already initialized !\n", owner);
51 bcom_sram = kmalloc(sizeof(struct bcom_sram), GFP_KERNEL);
53 printk(KERN_ERR "%s: bcom_sram_init: "
54 "Couldn't allocate internal state !\n", owner);
58 /* Get address and size of the sram */
59 rv = of_address_to_resource(sram_node, 0, &res);
61 printk(KERN_ERR "%s: bcom_sram_init: "
62 "Invalid device node !\n", owner);
66 bcom_sram->base_phys = res.start;
67 bcom_sram->size = resource_size(&res);
70 if (!request_mem_region(res.start, resource_size(&res), owner)) {
71 printk(KERN_ERR "%s: bcom_sram_init: "
72 "Couldn't request region !\n", owner);
78 /* sram is not really __iomem */
79 bcom_sram->base_virt = (void *)ioremap(res.start, resource_size(&res));
81 if (!bcom_sram->base_virt) {
82 printk(KERN_ERR "%s: bcom_sram_init: "
83 "Map error SRAM zone 0x%08lx (0x%0x)!\n",
84 owner, (long)bcom_sram->base_phys, bcom_sram->size );
89 /* Create an rheap (defaults to 32 bits word alignment) */
90 bcom_sram->rh = rh_create(4);
92 /* Attach the free zones */
94 /* Currently disabled ... for future use only */
95 reg_addr_p = of_get_property(sram_node, "available", &psize);
101 if (!regaddr_p || !psize) {
102 /* Attach the whole zone */
103 rh_attach_region(bcom_sram->rh, 0, bcom_sram->size);
105 /* Attach each zone independently */
106 while (psize >= 2 * sizeof(u32)) {
107 phys_addr_t zbase = of_translate_address(sram_node, regaddr_p);
108 rh_attach_region(bcom_sram->rh, zbase - bcom_sram->base_phys, regaddr_p[1]);
110 psize -= 2 * sizeof(u32);
114 /* Init our spinlock */
115 spin_lock_init(&bcom_sram->lock);
120 release_mem_region(res.start, resource_size(&res));
127 EXPORT_SYMBOL_GPL(bcom_sram_init);
129 void bcom_sram_cleanup(void)
133 rh_destroy(bcom_sram->rh);
134 iounmap((void __iomem *)bcom_sram->base_virt);
135 release_mem_region(bcom_sram->base_phys, bcom_sram->size);
140 EXPORT_SYMBOL_GPL(bcom_sram_cleanup);
142 void* bcom_sram_alloc(int size, int align, phys_addr_t *phys)
144 unsigned long offset;
146 spin_lock(&bcom_sram->lock);
147 offset = rh_alloc_align(bcom_sram->rh, size, align, NULL);
148 spin_unlock(&bcom_sram->lock);
150 if (IS_ERR_VALUE(offset))
153 *phys = bcom_sram->base_phys + offset;
154 return bcom_sram->base_virt + offset;
156 EXPORT_SYMBOL_GPL(bcom_sram_alloc);
158 void bcom_sram_free(void *ptr)
160 unsigned long offset;
165 offset = ptr - bcom_sram->base_virt;
167 spin_lock(&bcom_sram->lock);
168 rh_free(bcom_sram->rh, offset);
169 spin_unlock(&bcom_sram->lock);
171 EXPORT_SYMBOL_GPL(bcom_sram_free);