blob: fa56374912d42e4baf4649477bafef3034fa3619 [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>
David Brownell20e99692009-05-07 09:31:42 -070017#include <mach/sram.h>
18
David Brownell20e99692009-05-07 09:31:42 -070019static struct gen_pool *sram_pool;
20
21void *sram_alloc(size_t len, dma_addr_t *dma)
22{
23 unsigned long vaddr;
24 dma_addr_t dma_base = davinci_soc_info.sram_dma;
25
26 if (dma)
27 *dma = 0;
28 if (!sram_pool || (dma && !dma_base))
29 return NULL;
30
31 vaddr = gen_pool_alloc(sram_pool, len);
32 if (!vaddr)
33 return NULL;
34
35 if (dma)
Ben Gardiner626863a2012-10-05 13:04:41 -040036 *dma = gen_pool_virt_to_phys(sram_pool, vaddr);
David Brownell20e99692009-05-07 09:31:42 -070037 return (void *)vaddr;
38
39}
40EXPORT_SYMBOL(sram_alloc);
41
42void sram_free(void *addr, size_t len)
43{
44 gen_pool_free(sram_pool, (unsigned long) addr, len);
45}
46EXPORT_SYMBOL(sram_free);
47
48
49/*
50 * REVISIT This supports CPU and DMA access to/from SRAM, but it
51 * doesn't (yet?) support some other notable uses of SRAM: as TCM
52 * for data and/or instructions; and holding code needed to enter
53 * and exit suspend states (while DRAM can't be used).
54 */
55static int __init sram_init(void)
56{
Ben Gardiner626863a2012-10-05 13:04:41 -040057 phys_addr_t phys = davinci_soc_info.sram_dma;
David Brownell20e99692009-05-07 09:31:42 -070058 unsigned len = davinci_soc_info.sram_len;
59 int status = 0;
Ben Gardiner626863a2012-10-05 13:04:41 -040060 void *addr;
David Brownell20e99692009-05-07 09:31:42 -070061
62 if (len) {
David Brownell1d3bba62009-06-05 21:45:14 -070063 len = min_t(unsigned, len, SRAM_SIZE);
David Brownell20e99692009-05-07 09:31:42 -070064 sram_pool = gen_pool_create(ilog2(SRAM_GRANULARITY), -1);
65 if (!sram_pool)
66 status = -ENOMEM;
67 }
Ben Gardiner626863a2012-10-05 13:04:41 -040068
69 if (sram_pool) {
70 addr = ioremap(phys, len);
71 if (!addr)
72 return -ENOMEM;
73 status = gen_pool_add_virt(sram_pool, (unsigned)addr,
74 phys, len, -1);
75 if (status < 0)
76 iounmap(addr);
77 }
78
David Brownell20e99692009-05-07 09:31:42 -070079 WARN_ON(status < 0);
80 return status;
81}
82core_initcall(sram_init);
83