blob: 6c80424f678e3fad821aa8ea60685485db5ddb6e [file] [log] [blame]
Dinh Nguyen68a2f732010-10-12 11:29:01 -05001/*
2 * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301, USA.
18 */
19
20#include <linux/kernel.h>
21#include <linux/io.h>
22#include <linux/module.h>
23#include <linux/spinlock.h>
24#include <linux/genalloc.h>
Shawn Guoc045e3f2012-09-13 21:22:55 +080025
26#include "iram.h"
Dinh Nguyen68a2f732010-10-12 11:29:01 -050027
28static unsigned long iram_phys_base;
29static void __iomem *iram_virt_base;
30static struct gen_pool *iram_pool;
31
32static inline void __iomem *iram_phys_to_virt(unsigned long p)
33{
34 return iram_virt_base + (p - iram_phys_base);
35}
36
37void __iomem *iram_alloc(unsigned int size, unsigned long *dma_addr)
38{
39 if (!iram_pool)
40 return NULL;
41
42 *dma_addr = gen_pool_alloc(iram_pool, size);
43 pr_debug("iram alloc - %dB@0x%lX\n", size, *dma_addr);
44 if (!*dma_addr)
45 return NULL;
46 return iram_phys_to_virt(*dma_addr);
47}
48EXPORT_SYMBOL(iram_alloc);
49
50void iram_free(unsigned long addr, unsigned int size)
51{
52 if (!iram_pool)
53 return;
54
55 gen_pool_free(iram_pool, addr, size);
56}
57EXPORT_SYMBOL(iram_free);
58
59int __init iram_init(unsigned long base, unsigned long size)
60{
61 iram_phys_base = base;
62
63 iram_pool = gen_pool_create(PAGE_SHIFT, -1);
64 if (!iram_pool)
65 return -ENOMEM;
66
67 gen_pool_add(iram_pool, base, size, -1);
68 iram_virt_base = ioremap(iram_phys_base, size);
69 if (!iram_virt_base)
70 return -EIO;
71
72 pr_debug("i.MX IRAM pool: %ld KB@0x%p\n", size / 1024, iram_virt_base);
73 return 0;
74}