blob: bbae190fd82c6537d5739c97c5dc24297830feba [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Brownell20e99692009-05-07 09:31:42 -07002/*
3 * mach-davinci/sram.c - DaVinci simple SRAM allocator
4 *
5 * Copyright (C) 2009 David Brownell
David Brownell20e99692009-05-07 09:31:42 -07006 */
7#include <linux/module.h>
David Brownell20e99692009-05-07 09:31:42 -07008#include <linux/init.h>
Ben Gardiner626863a2012-10-05 13:04:41 -04009#include <linux/io.h>
David Brownell20e99692009-05-07 09:31:42 -070010#include <linux/genalloc.h>
11
12#include <mach/common.h>
Arnd Bergmann3acf7312015-01-30 10:45:33 +010013#include "sram.h"
David Brownell20e99692009-05-07 09:31:42 -070014
David Brownell20e99692009-05-07 09:31:42 -070015static struct gen_pool *sram_pool;
16
Matt Porter983c42b2012-10-05 13:04:43 -040017struct gen_pool *sram_get_gen_pool(void)
18{
19 return sram_pool;
20}
21
David Brownell20e99692009-05-07 09:31:42 -070022void *sram_alloc(size_t len, dma_addr_t *dma)
23{
David Brownell20e99692009-05-07 09:31:42 -070024 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
Nicolin Chen66f11962013-11-12 15:09:54 -080031 return gen_pool_dma_alloc(sram_pool, len, dma);
David Brownell20e99692009-05-07 09:31:42 -070032
33}
34EXPORT_SYMBOL(sram_alloc);
35
36void sram_free(void *addr, size_t len)
37{
38 gen_pool_free(sram_pool, (unsigned long) addr, len);
39}
40EXPORT_SYMBOL(sram_free);
41
42
43/*
44 * REVISIT This supports CPU and DMA access to/from SRAM, but it
45 * doesn't (yet?) support some other notable uses of SRAM: as TCM
46 * for data and/or instructions; and holding code needed to enter
47 * and exit suspend states (while DRAM can't be used).
48 */
49static int __init sram_init(void)
50{
Ben Gardiner626863a2012-10-05 13:04:41 -040051 phys_addr_t phys = davinci_soc_info.sram_dma;
David Brownell20e99692009-05-07 09:31:42 -070052 unsigned len = davinci_soc_info.sram_len;
53 int status = 0;
Sekhar Nori182e7962013-04-10 14:57:14 +053054 void __iomem *addr;
David Brownell20e99692009-05-07 09:31:42 -070055
56 if (len) {
David Brownell1d3bba62009-06-05 21:45:14 -070057 len = min_t(unsigned, len, SRAM_SIZE);
David Brownell20e99692009-05-07 09:31:42 -070058 sram_pool = gen_pool_create(ilog2(SRAM_GRANULARITY), -1);
59 if (!sram_pool)
60 status = -ENOMEM;
61 }
Ben Gardiner626863a2012-10-05 13:04:41 -040062
63 if (sram_pool) {
64 addr = ioremap(phys, len);
65 if (!addr)
66 return -ENOMEM;
Sekhar Nori182e7962013-04-10 14:57:14 +053067 status = gen_pool_add_virt(sram_pool, (unsigned long) addr,
Ben Gardiner626863a2012-10-05 13:04:41 -040068 phys, len, -1);
69 if (status < 0)
70 iounmap(addr);
71 }
72
David Brownell20e99692009-05-07 09:31:42 -070073 WARN_ON(status < 0);
74 return status;
75}
76core_initcall(sram_init);
77