2 * Simple memory allocator for on-board SRAM
9 * This file is licensed under the terms of the GNU General Public License
10 * version 2. This program is licensed "as is" without any warranty of any
11 * kind, whether express or implied.
14 #include <linux/err.h>
15 #include <linux/kernel.h>
16 #include <linux/export.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/string.h>
20 #include <linux/ioport.h>
22 #include <linux/of_address.h>
27 #include <linux/fsl/bestcomm/sram.h>
30 /* Struct keeping our 'state' */
31 struct bcom_sram *bcom_sram = NULL;
32 EXPORT_SYMBOL_GPL(bcom_sram); /* needed for inline functions */
35 /* ======================================================================== */
37 /* ======================================================================== */
38 /* DO NOT USE in interrupts, if needed in irq handler, we should use the
39 _irqsave version of the spin_locks */
41 int bcom_sram_init(struct device_node *sram_node, char *owner)
45 u64 regaddr64, size64;
48 /* Create our state struct */
50 printk(KERN_ERR "%s: bcom_sram_init: "
51 "Already initialized !\n", owner);
55 bcom_sram = kmalloc(sizeof(struct bcom_sram), GFP_KERNEL);
57 printk(KERN_ERR "%s: bcom_sram_init: "
58 "Couldn't allocate internal state !\n", owner);
62 /* Get address and size of the sram */
63 regaddr_p = of_get_address(sram_node, 0, &size64, NULL);
65 printk(KERN_ERR "%s: bcom_sram_init: "
66 "Invalid device node !\n", owner);
71 regaddr64 = of_translate_address(sram_node, regaddr_p);
73 bcom_sram->base_phys = (phys_addr_t) regaddr64;
74 bcom_sram->size = (unsigned int) size64;
77 if (!request_mem_region(bcom_sram->base_phys, bcom_sram->size, owner)) {
78 printk(KERN_ERR "%s: bcom_sram_init: "
79 "Couldn't request region !\n", owner);
85 /* sram is not really __iomem */
86 bcom_sram->base_virt = (void*) ioremap(bcom_sram->base_phys, bcom_sram->size);
88 if (!bcom_sram->base_virt) {
89 printk(KERN_ERR "%s: bcom_sram_init: "
90 "Map error SRAM zone 0x%08lx (0x%0x)!\n",
91 owner, (long)bcom_sram->base_phys, bcom_sram->size );
96 /* Create an rheap (defaults to 32 bits word alignment) */
97 bcom_sram->rh = rh_create(4);
99 /* Attach the free zones */
101 /* Currently disabled ... for future use only */
102 reg_addr_p = of_get_property(sram_node, "available", &psize);
108 if (!regaddr_p || !psize) {
109 /* Attach the whole zone */
110 rh_attach_region(bcom_sram->rh, 0, bcom_sram->size);
112 /* Attach each zone independently */
113 while (psize >= 2 * sizeof(u32)) {
114 phys_addr_t zbase = of_translate_address(sram_node, regaddr_p);
115 rh_attach_region(bcom_sram->rh, zbase - bcom_sram->base_phys, regaddr_p[1]);
117 psize -= 2 * sizeof(u32);
121 /* Init our spinlock */
122 spin_lock_init(&bcom_sram->lock);
127 release_mem_region(bcom_sram->base_phys, bcom_sram->size);
134 EXPORT_SYMBOL_GPL(bcom_sram_init);
136 void bcom_sram_cleanup(void)
140 rh_destroy(bcom_sram->rh);
141 iounmap((void __iomem *)bcom_sram->base_virt);
142 release_mem_region(bcom_sram->base_phys, bcom_sram->size);
147 EXPORT_SYMBOL_GPL(bcom_sram_cleanup);
149 void* bcom_sram_alloc(int size, int align, phys_addr_t *phys)
151 unsigned long offset;
153 spin_lock(&bcom_sram->lock);
154 offset = rh_alloc_align(bcom_sram->rh, size, align, NULL);
155 spin_unlock(&bcom_sram->lock);
157 if (IS_ERR_VALUE(offset))
160 *phys = bcom_sram->base_phys + offset;
161 return bcom_sram->base_virt + offset;
163 EXPORT_SYMBOL_GPL(bcom_sram_alloc);
165 void bcom_sram_free(void *ptr)
167 unsigned long offset;
172 offset = ptr - bcom_sram->base_virt;
174 spin_lock(&bcom_sram->lock);
175 rh_free(bcom_sram->rh, offset);
176 spin_unlock(&bcom_sram->lock);
178 EXPORT_SYMBOL_GPL(bcom_sram_free);