sh_eth: fix EESIPR values for SH77{34|63}
[linux/fpc-iii.git] / drivers / dma / bestcomm / sram.c
blob2074e0e3fa21f8a23dd2179394d2b677c17a9934
1 /*
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>
21 #include <linux/of.h>
22 #include <linux/of_address.h>
24 #include <asm/io.h>
25 #include <asm/mmu.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 /* ======================================================================== */
36 /* Public API */
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)
43 int rv;
44 const u32 *regaddr_p;
45 u64 regaddr64, size64;
46 unsigned int psize;
48 /* Create our state struct */
49 if (bcom_sram) {
50 printk(KERN_ERR "%s: bcom_sram_init: "
51 "Already initialized !\n", owner);
52 return -EBUSY;
55 bcom_sram = kmalloc(sizeof(struct bcom_sram), GFP_KERNEL);
56 if (!bcom_sram) {
57 printk(KERN_ERR "%s: bcom_sram_init: "
58 "Couldn't allocate internal state !\n", owner);
59 return -ENOMEM;
62 /* Get address and size of the sram */
63 regaddr_p = of_get_address(sram_node, 0, &size64, NULL);
64 if (!regaddr_p) {
65 printk(KERN_ERR "%s: bcom_sram_init: "
66 "Invalid device node !\n", owner);
67 rv = -EINVAL;
68 goto error_free;
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;
76 /* Request region */
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);
80 rv = -EBUSY;
81 goto error_free;
84 /* Map SRAM */
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 );
92 rv = -ENOMEM;
93 goto error_release;
96 /* Create an rheap (defaults to 32 bits word alignment) */
97 bcom_sram->rh = rh_create(4);
99 /* Attach the free zones */
100 #if 0
101 /* Currently disabled ... for future use only */
102 reg_addr_p = of_get_property(sram_node, "available", &psize);
103 #else
104 regaddr_p = NULL;
105 psize = 0;
106 #endif
108 if (!regaddr_p || !psize) {
109 /* Attach the whole zone */
110 rh_attach_region(bcom_sram->rh, 0, bcom_sram->size);
111 } else {
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]);
116 regaddr_p += 2;
117 psize -= 2 * sizeof(u32);
121 /* Init our spinlock */
122 spin_lock_init(&bcom_sram->lock);
124 return 0;
126 error_release:
127 release_mem_region(bcom_sram->base_phys, bcom_sram->size);
128 error_free:
129 kfree(bcom_sram);
130 bcom_sram = NULL;
132 return rv;
134 EXPORT_SYMBOL_GPL(bcom_sram_init);
136 void bcom_sram_cleanup(void)
138 /* Free resources */
139 if (bcom_sram) {
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);
143 kfree(bcom_sram);
144 bcom_sram = NULL;
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))
158 return NULL;
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;
169 if (!ptr)
170 return;
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);