blob: 668b6e74976801862e4d100e2da2434603b8ae04 [file] [log] [blame]
David Brownell20e99692009-05-07 09:31:42 -07001/*
2 * mach-davinci/sram.c - DaVinci simple SRAM allocator
3 *
4 * Copyright (C) 2009 David Brownell
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11#include <linux/module.h>
David Brownell20e99692009-05-07 09:31:42 -070012#include <linux/init.h>
Ben Gardiner626863a2012-10-05 13:04:41 -040013#include <linux/io.h>
David Brownell20e99692009-05-07 09:31:42 -070014#include <linux/genalloc.h>
15
16#include <mach/common.h>
Arnd Bergmann3acf7312015-01-30 10:45:33 +010017#include "sram.h"
David Brownell20e99692009-05-07 09:31:42 -070018
David Brownell20e99692009-05-07 09:31:42 -070019static struct gen_pool *sram_pool;
20
Matt Porter983c42b2012-10-05 13:04:43 -040021struct gen_pool *sram_get_gen_pool(void)
22{
23 return sram_pool;
24}
25
David Brownell20e99692009-05-07 09:31:42 -070026void *sram_alloc(size_t len, dma_addr_t *dma)
27{
David Brownell20e99692009-05-07 09:31:42 -070028 dma_addr_t dma_base = davinci_soc_info.sram_dma;
29
30 if (dma)
31 *dma = 0;
32 if (!sram_pool || (dma && !dma_base))
33 return NULL;
34
Nicolin Chen66f11962013-11-12 15:09:54 -080035 return gen_pool_dma_alloc(sram_pool, len, dma);
David Brownell20e99692009-05-07 09:31:42 -070036
37}
38EXPORT_SYMBOL(sram_alloc);
39
40void sram_free(void *addr, size_t len)
41{
42 gen_pool_free(sram_pool, (unsigned long) addr, len);
43}
44EXPORT_SYMBOL(sram_free);
45
46
47/*
48 * REVISIT This supports CPU and DMA access to/from SRAM, but it
49 * doesn't (yet?) support some other notable uses of SRAM: as TCM
50 * for data and/or instructions; and holding code needed to enter
51 * and exit suspend states (while DRAM can't be used).
52 */
53static int __init sram_init(void)
54{
Ben Gardiner626863a2012-10-05 13:04:41 -040055 phys_addr_t phys = davinci_soc_info.sram_dma;
David Brownell20e99692009-05-07 09:31:42 -070056 unsigned len = davinci_soc_info.sram_len;
57 int status = 0;
Sekhar Nori182e7962013-04-10 14:57:14 +053058 void __iomem *addr;
David Brownell20e99692009-05-07 09:31:42 -070059
60 if (len) {
David Brownell1d3bba62009-06-05 21:45:14 -070061 len = min_t(unsigned, len, SRAM_SIZE);
David Brownell20e99692009-05-07 09:31:42 -070062 sram_pool = gen_pool_create(ilog2(SRAM_GRANULARITY), -1);
63 if (!sram_pool)
64 status = -ENOMEM;
65 }
Ben Gardiner626863a2012-10-05 13:04:41 -040066
67 if (sram_pool) {
68 addr = ioremap(phys, len);
69 if (!addr)
70 return -ENOMEM;
Sekhar Nori182e7962013-04-10 14:57:14 +053071 status = gen_pool_add_virt(sram_pool, (unsigned long) addr,
Ben Gardiner626863a2012-10-05 13:04:41 -040072 phys, len, -1);
73 if (status < 0)
74 iounmap(addr);
75 }
76
David Brownell20e99692009-05-07 09:31:42 -070077 WARN_ON(status < 0);
78 return status;
79}
80core_initcall(sram_init);
81