1
2
3
4
5
6
7
8
9
10
11
12
13
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>
20#include <linux/of.h>
21
22#include <asm/io.h>
23#include <asm/mmu.h>
24
25#include "sram.h"
26
27
28
29struct bcom_sram *bcom_sram = NULL;
30EXPORT_SYMBOL_GPL(bcom_sram);
31
32
33
34
35
36
37
38
39int bcom_sram_init(struct device_node *sram_node, char *owner)
40{
41 int rv;
42 const u32 *regaddr_p;
43 u64 regaddr64, size64;
44 unsigned int psize;
45
46
47 if (bcom_sram) {
48 printk(KERN_ERR "%s: bcom_sram_init: "
49 "Already initialized !\n", owner);
50 return -EBUSY;
51 }
52
53 bcom_sram = kmalloc(sizeof(struct bcom_sram), GFP_KERNEL);
54 if (!bcom_sram) {
55 printk(KERN_ERR "%s: bcom_sram_init: "
56 "Couldn't allocate internal state !\n", owner);
57 return -ENOMEM;
58 }
59
60
61 regaddr_p = of_get_address(sram_node, 0, &size64, NULL);
62 if (!regaddr_p) {
63 printk(KERN_ERR "%s: bcom_sram_init: "
64 "Invalid device node !\n", owner);
65 rv = -EINVAL;
66 goto error_free;
67 }
68
69 regaddr64 = of_translate_address(sram_node, regaddr_p);
70
71 bcom_sram->base_phys = (phys_addr_t) regaddr64;
72 bcom_sram->size = (unsigned int) size64;
73
74
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);
78 rv = -EBUSY;
79 goto error_free;
80 }
81
82
83
84 bcom_sram->base_virt = (void*) ioremap(bcom_sram->base_phys, bcom_sram->size);
85
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, (long)bcom_sram->base_phys, bcom_sram->size );
90 rv = -ENOMEM;
91 goto error_release;
92 }
93
94
95 bcom_sram->rh = rh_create(4);
96
97
98#if 0
99
100 reg_addr_p = of_get_property(sram_node, "available", &psize);
101#else
102 regaddr_p = NULL;
103 psize = 0;
104#endif
105
106 if (!regaddr_p || !psize) {
107
108 rh_attach_region(bcom_sram->rh, 0, bcom_sram->size);
109 } else {
110
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]);
114 regaddr_p += 2;
115 psize -= 2 * sizeof(u32);
116 }
117 }
118
119
120 spin_lock_init(&bcom_sram->lock);
121
122 return 0;
123
124error_release:
125 release_mem_region(bcom_sram->base_phys, bcom_sram->size);
126error_free:
127 kfree(bcom_sram);
128 bcom_sram = NULL;
129
130 return rv;
131}
132EXPORT_SYMBOL_GPL(bcom_sram_init);
133
134void bcom_sram_cleanup(void)
135{
136
137 if (bcom_sram) {
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);
141 kfree(bcom_sram);
142 bcom_sram = NULL;
143 }
144}
145EXPORT_SYMBOL_GPL(bcom_sram_cleanup);
146
147void* bcom_sram_alloc(int size, int align, phys_addr_t *phys)
148{
149 unsigned long offset;
150
151 spin_lock(&bcom_sram->lock);
152 offset = rh_alloc_align(bcom_sram->rh, size, align, NULL);
153 spin_unlock(&bcom_sram->lock);
154
155 if (IS_ERR_VALUE(offset))
156 return NULL;
157
158 *phys = bcom_sram->base_phys + offset;
159 return bcom_sram->base_virt + offset;
160}
161EXPORT_SYMBOL_GPL(bcom_sram_alloc);
162
163void bcom_sram_free(void *ptr)
164{
165 unsigned long offset;
166
167 if (!ptr)
168 return;
169
170 offset = ptr - bcom_sram->base_virt;
171
172 spin_lock(&bcom_sram->lock);
173 rh_free(bcom_sram->rh, offset);
174 spin_unlock(&bcom_sram->lock);
175}
176EXPORT_SYMBOL_GPL(bcom_sram_free);
177
178