1 // SPDX-License-Identifier: GPL-2.0-only
3 * Simple memory allocator for on-board SRAM
5 * Maintainer : Sylvain Munaut <tnt@246tNt.com>
7 * Copyright (C) 2005 Sylvain Munaut <tnt@246tNt.com>
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 */
96 if (!regaddr_p
|| !psize
) {
97 /* Attach the whole zone */
98 rh_attach_region(bcom_sram
->rh
, 0, bcom_sram
->size
);
100 /* Attach each zone independently */
101 while (psize
>= 2 * sizeof(u32
)) {
102 phys_addr_t zbase
= of_translate_address(sram_node
, regaddr_p
);
103 rh_attach_region(bcom_sram
->rh
, zbase
- bcom_sram
->base_phys
, regaddr_p
[1]);
105 psize
-= 2 * sizeof(u32
);
109 /* Init our spinlock */
110 spin_lock_init(&bcom_sram
->lock
);
115 release_mem_region(res
.start
, resource_size(&res
));
122 EXPORT_SYMBOL_GPL(bcom_sram_init
);
124 void bcom_sram_cleanup(void)
128 rh_destroy(bcom_sram
->rh
);
129 iounmap((void __iomem
*)bcom_sram
->base_virt
);
130 release_mem_region(bcom_sram
->base_phys
, bcom_sram
->size
);
135 EXPORT_SYMBOL_GPL(bcom_sram_cleanup
);
137 void* bcom_sram_alloc(int size
, int align
, phys_addr_t
*phys
)
139 unsigned long offset
;
141 spin_lock(&bcom_sram
->lock
);
142 offset
= rh_alloc_align(bcom_sram
->rh
, size
, align
, NULL
);
143 spin_unlock(&bcom_sram
->lock
);
145 if (IS_ERR_VALUE(offset
))
148 *phys
= bcom_sram
->base_phys
+ offset
;
149 return bcom_sram
->base_virt
+ offset
;
151 EXPORT_SYMBOL_GPL(bcom_sram_alloc
);
153 void bcom_sram_free(void *ptr
)
155 unsigned long offset
;
160 offset
= ptr
- bcom_sram
->base_virt
;
162 spin_lock(&bcom_sram
->lock
);
163 rh_free(bcom_sram
->rh
, offset
);
164 spin_unlock(&bcom_sram
->lock
);
166 EXPORT_SYMBOL_GPL(bcom_sram_free
);