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/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>
29 /* Struct keeping our 'state' */
30 struct bcom_sram
*bcom_sram
= NULL
;
31 EXPORT_SYMBOL_GPL(bcom_sram
); /* needed for inline functions */
34 /* ======================================================================== */
36 /* ======================================================================== */
37 /* DO NOT USE in interrupts, if needed in irq handler, we should use the
38 _irqsave version of the spin_locks */
40 int bcom_sram_init(struct device_node
*sram_node
, char *owner
)
44 u64 regaddr64
, size64
;
47 /* Create our state struct */
49 printk(KERN_ERR
"%s: bcom_sram_init: "
50 "Already initialized !\n", owner
);
54 bcom_sram
= kmalloc(sizeof(struct bcom_sram
), GFP_KERNEL
);
56 printk(KERN_ERR
"%s: bcom_sram_init: "
57 "Couldn't allocate internal state !\n", owner
);
61 /* Get address and size of the sram */
62 regaddr_p
= of_get_address(sram_node
, 0, &size64
, NULL
);
64 printk(KERN_ERR
"%s: bcom_sram_init: "
65 "Invalid device node !\n", owner
);
70 regaddr64
= of_translate_address(sram_node
, regaddr_p
);
72 bcom_sram
->base_phys
= (phys_addr_t
) regaddr64
;
73 bcom_sram
->size
= (unsigned int) size64
;
76 if (!request_mem_region(bcom_sram
->base_phys
, bcom_sram
->size
, owner
)) {
77 printk(KERN_ERR
"%s: bcom_sram_init: "
78 "Couldn't request region !\n", owner
);
84 /* sram is not really __iomem */
85 bcom_sram
->base_virt
= (void*) ioremap(bcom_sram
->base_phys
, bcom_sram
->size
);
87 if (!bcom_sram
->base_virt
) {
88 printk(KERN_ERR
"%s: bcom_sram_init: "
89 "Map error SRAM zone 0x%08lx (0x%0x)!\n",
90 owner
, (long)bcom_sram
->base_phys
, bcom_sram
->size
);
95 /* Create an rheap (defaults to 32 bits word alignment) */
96 bcom_sram
->rh
= rh_create(4);
98 /* Attach the free zones */
100 /* Currently disabled ... for future use only */
101 reg_addr_p
= of_get_property(sram_node
, "available", &psize
);
107 if (!regaddr_p
|| !psize
) {
108 /* Attach the whole zone */
109 rh_attach_region(bcom_sram
->rh
, 0, bcom_sram
->size
);
111 /* Attach each zone independently */
112 while (psize
>= 2 * sizeof(u32
)) {
113 phys_addr_t zbase
= of_translate_address(sram_node
, regaddr_p
);
114 rh_attach_region(bcom_sram
->rh
, zbase
- bcom_sram
->base_phys
, regaddr_p
[1]);
116 psize
-= 2 * sizeof(u32
);
120 /* Init our spinlock */
121 spin_lock_init(&bcom_sram
->lock
);
126 release_mem_region(bcom_sram
->base_phys
, bcom_sram
->size
);
133 EXPORT_SYMBOL_GPL(bcom_sram_init
);
135 void bcom_sram_cleanup(void)
139 rh_destroy(bcom_sram
->rh
);
140 iounmap((void __iomem
*)bcom_sram
->base_virt
);
141 release_mem_region(bcom_sram
->base_phys
, bcom_sram
->size
);
146 EXPORT_SYMBOL_GPL(bcom_sram_cleanup
);
148 void* bcom_sram_alloc(int size
, int align
, phys_addr_t
*phys
)
150 unsigned long offset
;
152 spin_lock(&bcom_sram
->lock
);
153 offset
= rh_alloc_align(bcom_sram
->rh
, size
, align
, NULL
);
154 spin_unlock(&bcom_sram
->lock
);
156 if (IS_ERR_VALUE(offset
))
159 *phys
= bcom_sram
->base_phys
+ offset
;
160 return bcom_sram
->base_virt
+ offset
;
162 EXPORT_SYMBOL_GPL(bcom_sram_alloc
);
164 void bcom_sram_free(void *ptr
)
166 unsigned long offset
;
171 offset
= ptr
- bcom_sram
->base_virt
;
173 spin_lock(&bcom_sram
->lock
);
174 rh_free(bcom_sram
->rh
, offset
);
175 spin_unlock(&bcom_sram
->lock
);
177 EXPORT_SYMBOL_GPL(bcom_sram_free
);