4 * Author: Scott Wood <scottwood@freescale.com>
6 * Copyright 2007-2008,2010 Freescale Semiconductor, Inc.
8 * Some parts derived from commproc.c/cpm2_common.c, which is:
9 * Copyright (c) 1997 Dan error_act (dmalek@jlc.net)
10 * Copyright (c) 1999-2001 Dan Malek <dan@embeddedalley.com>
11 * Copyright (c) 2000 MontaVista Software, Inc (source@mvista.com)
12 * 2006 (c) MontaVista Software, Inc.
13 * Vitaly Bordug <vbordug@ru.mvista.com>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 #include <linux/genalloc.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/of_device.h>
23 #include <linux/spinlock.h>
24 #include <linux/export.h>
26 #include <linux/of_address.h>
27 #include <linux/slab.h>
29 #include <soc/fsl/qe/qe.h>
31 static struct gen_pool
*muram_pool
;
32 static spinlock_t cpm_muram_lock
;
33 static u8 __iomem
*muram_vbase
;
34 static phys_addr_t muram_pbase
;
37 struct list_head head
;
42 static LIST_HEAD(muram_block_list
);
44 /* max address size we deal with */
45 #define OF_MAX_ADDR_CELLS 4
46 #define GENPOOL_OFFSET (4096 * 8)
48 int cpm_muram_init(void)
50 struct device_node
*np
;
52 u32 zero
[OF_MAX_ADDR_CELLS
] = {};
53 resource_size_t max
= 0;
60 spin_lock_init(&cpm_muram_lock
);
61 np
= of_find_compatible_node(NULL
, NULL
, "fsl,cpm-muram-data");
63 /* try legacy bindings */
64 np
= of_find_node_by_name(NULL
, "data-only");
66 pr_err("Cannot find CPM muram data node");
72 muram_pool
= gen_pool_create(0, -1);
74 pr_err("Cannot allocate memory pool for CPM/QE muram");
78 muram_pbase
= of_translate_address(np
, zero
);
79 if (muram_pbase
== (phys_addr_t
)OF_BAD_ADDR
) {
80 pr_err("Cannot translate zero through CPM muram node");
85 while (of_address_to_resource(np
, i
++, &r
) == 0) {
88 ret
= gen_pool_add(muram_pool
, r
.start
- muram_pbase
+
89 GENPOOL_OFFSET
, resource_size(&r
), -1);
91 pr_err("QE: couldn't add muram to pool!\n");
96 muram_vbase
= ioremap(muram_pbase
, max
- muram_pbase
+ 1);
98 pr_err("Cannot map QE muram");
104 gen_pool_destroy(muram_pool
);
111 * cpm_muram_alloc_common - cpm_muram_alloc common code
112 * @size: number of bytes to allocate
113 * @algo: algorithm for alloc.
114 * @data: data for genalloc's algorithm.
116 * This function returns an offset into the muram area.
118 static unsigned long cpm_muram_alloc_common(unsigned long size
,
119 genpool_algo_t algo
, void *data
)
121 struct muram_block
*entry
;
124 if (!muram_pool
&& cpm_muram_init())
127 start
= gen_pool_alloc_algo(muram_pool
, size
, algo
, data
);
130 start
= start
- GENPOOL_OFFSET
;
131 memset_io(cpm_muram_addr(start
), 0, size
);
132 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
135 entry
->start
= start
;
137 list_add(&entry
->head
, &muram_block_list
);
141 gen_pool_free(muram_pool
, start
, size
);
143 return (unsigned long)-ENOMEM
;
147 * cpm_muram_alloc - allocate the requested size worth of multi-user ram
148 * @size: number of bytes to allocate
149 * @align: requested alignment, in bytes
151 * This function returns an offset into the muram area.
152 * Use cpm_dpram_addr() to get the virtual address of the area.
153 * Use cpm_muram_free() to free the allocation.
155 unsigned long cpm_muram_alloc(unsigned long size
, unsigned long align
)
159 struct genpool_data_align muram_pool_data
;
161 spin_lock_irqsave(&cpm_muram_lock
, flags
);
162 muram_pool_data
.align
= align
;
163 start
= cpm_muram_alloc_common(size
, gen_pool_first_fit_align
,
165 spin_unlock_irqrestore(&cpm_muram_lock
, flags
);
168 EXPORT_SYMBOL(cpm_muram_alloc
);
171 * cpm_muram_free - free a chunk of multi-user ram
172 * @offset: The beginning of the chunk as returned by cpm_muram_alloc().
174 int cpm_muram_free(unsigned long offset
)
178 struct muram_block
*tmp
;
181 spin_lock_irqsave(&cpm_muram_lock
, flags
);
182 list_for_each_entry(tmp
, &muram_block_list
, head
) {
183 if (tmp
->start
== offset
) {
185 list_del(&tmp
->head
);
190 gen_pool_free(muram_pool
, offset
+ GENPOOL_OFFSET
, size
);
191 spin_unlock_irqrestore(&cpm_muram_lock
, flags
);
194 EXPORT_SYMBOL(cpm_muram_free
);
197 * cpm_muram_alloc_fixed - reserve a specific region of multi-user ram
198 * @offset: offset of allocation start address
199 * @size: number of bytes to allocate
200 * This function returns an offset into the muram area
201 * Use cpm_dpram_addr() to get the virtual address of the area.
202 * Use cpm_muram_free() to free the allocation.
204 unsigned long cpm_muram_alloc_fixed(unsigned long offset
, unsigned long size
)
208 struct genpool_data_fixed muram_pool_data_fixed
;
210 spin_lock_irqsave(&cpm_muram_lock
, flags
);
211 muram_pool_data_fixed
.offset
= offset
+ GENPOOL_OFFSET
;
212 start
= cpm_muram_alloc_common(size
, gen_pool_fixed_alloc
,
213 &muram_pool_data_fixed
);
214 spin_unlock_irqrestore(&cpm_muram_lock
, flags
);
217 EXPORT_SYMBOL(cpm_muram_alloc_fixed
);
220 * cpm_muram_addr - turn a muram offset into a virtual address
221 * @offset: muram offset to convert
223 void __iomem
*cpm_muram_addr(unsigned long offset
)
225 return muram_vbase
+ offset
;
227 EXPORT_SYMBOL(cpm_muram_addr
);
229 unsigned long cpm_muram_offset(void __iomem
*addr
)
231 return addr
- (void __iomem
*)muram_vbase
;
233 EXPORT_SYMBOL(cpm_muram_offset
);
236 * cpm_muram_dma - turn a muram virtual address into a DMA address
237 * @offset: virtual address from cpm_muram_addr() to convert
239 dma_addr_t
cpm_muram_dma(void __iomem
*addr
)
241 return muram_pbase
+ ((u8 __iomem
*)addr
- muram_vbase
);
243 EXPORT_SYMBOL(cpm_muram_dma
);