2 * Simple memory allocator for on-board SRAM
5 * Maintainer : Sylvain Munaut <tnt@246tNt.com>
7 * Copyright (C) 2005 Sylvain Munaut <tnt@246tNt.com>
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/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
18 #include <linux/string.h>
19 #include <linux/ioport.h>
28 /* Struct keeping our 'state' */
29 struct bcom_sram *bcom_sram = NULL;
30 EXPORT_SYMBOL_GPL(bcom_sram); /* needed for inline functions */
33 /* ======================================================================== */
35 /* ======================================================================== */
36 /* DO NOT USE in interrupts, if needed in irq handler, we should use the
37 _irqsave version of the spin_locks */
39 int bcom_sram_init(struct device_node *sram_node, char *owner)
43 u64 regaddr64, size64;
46 /* Create our state struct */
48 printk(KERN_ERR "%s: bcom_sram_init: "
49 "Already initialized !\n", owner);
53 bcom_sram = kmalloc(sizeof(struct bcom_sram), GFP_KERNEL);
55 printk(KERN_ERR "%s: bcom_sram_init: "
56 "Couldn't allocate internal state !\n", owner);
60 /* Get address and size of the sram */
61 regaddr_p = of_get_address(sram_node, 0, &size64, NULL);
63 printk(KERN_ERR "%s: bcom_sram_init: "
64 "Invalid device node !\n", owner);
69 regaddr64 = of_translate_address(sram_node, regaddr_p);
71 bcom_sram->base_phys = (phys_addr_t) regaddr64;
72 bcom_sram->size = (unsigned int) size64;
75 if (!request_mem_region(bcom_sram->base_phys, bcom_sram->size, owner)) {
76 printk(KERN_ERR "%s: bcom_sram_init: "
77 "Couldn't request region !\n", owner);
83 /* sram is not really __iomem */
84 bcom_sram->base_virt = (void*) ioremap(bcom_sram->base_phys, bcom_sram->size);
86 if (!bcom_sram->base_virt) {
87 printk(KERN_ERR "%s: bcom_sram_init: "
88 "Map error SRAM zone 0x%08lx (0x%0x)!\n",
89 owner, bcom_sram->base_phys, bcom_sram->size );
94 /* Create an rheap (defaults to 32 bits word alignment) */
95 bcom_sram->rh = rh_create(4);
97 /* Attach the free zones */
99 /* Currently disabled ... for future use only */
100 reg_addr_p = of_get_property(sram_node, "available", &psize);
106 if (!regaddr_p || !psize) {
107 /* Attach the whole zone */
108 rh_attach_region(bcom_sram->rh, 0, bcom_sram->size);
110 /* Attach each zone independently */
111 while (psize >= 2 * sizeof(u32)) {
112 phys_addr_t zbase = of_translate_address(sram_node, regaddr_p);
113 rh_attach_region(bcom_sram->rh, zbase - bcom_sram->base_phys, regaddr_p[1]);
115 psize -= 2 * sizeof(u32);
119 /* Init our spinlock */
120 spin_lock_init(&bcom_sram->lock);
125 release_mem_region(bcom_sram->base_phys, bcom_sram->size);
132 EXPORT_SYMBOL_GPL(bcom_sram_init);
134 void bcom_sram_cleanup(void)
138 rh_destroy(bcom_sram->rh);
139 iounmap((void __iomem *)bcom_sram->base_virt);
140 release_mem_region(bcom_sram->base_phys, bcom_sram->size);
145 EXPORT_SYMBOL_GPL(bcom_sram_cleanup);
147 void* bcom_sram_alloc(int size, int align, phys_addr_t *phys)
149 unsigned long offset;
151 spin_lock(&bcom_sram->lock);
152 offset = rh_alloc_align(bcom_sram->rh, size, align, NULL);
153 spin_unlock(&bcom_sram->lock);
155 if (IS_ERR_VALUE(offset))
158 *phys = bcom_sram->base_phys + offset;
159 return bcom_sram->base_virt + offset;
161 EXPORT_SYMBOL_GPL(bcom_sram_alloc);
163 void bcom_sram_free(void *ptr)
165 unsigned long offset;
170 offset = ptr - bcom_sram->base_virt;
172 spin_lock(&bcom_sram->lock);
173 rh_free(bcom_sram->rh, offset);
174 spin_unlock(&bcom_sram->lock);
176 EXPORT_SYMBOL_GPL(bcom_sram_free);