xref: /openbmc/linux/arch/arm/mach-davinci/sram.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
220e9969bSDavid Brownell /*
320e9969bSDavid Brownell  * mach-davinci/sram.c - DaVinci simple SRAM allocator
420e9969bSDavid Brownell  *
520e9969bSDavid Brownell  * Copyright (C) 2009 David Brownell
620e9969bSDavid Brownell  */
720e9969bSDavid Brownell #include <linux/module.h>
820e9969bSDavid Brownell #include <linux/init.h>
9626863a3SBen Gardiner #include <linux/io.h>
1020e9969bSDavid Brownell #include <linux/genalloc.h>
1120e9969bSDavid Brownell 
12*ca31807bSArnd Bergmann #include "common.h"
133acf731cSArnd Bergmann #include "sram.h"
1420e9969bSDavid Brownell 
1520e9969bSDavid Brownell static struct gen_pool *sram_pool;
1620e9969bSDavid Brownell 
sram_get_gen_pool(void)17983c42baSMatt Porter struct gen_pool *sram_get_gen_pool(void)
18983c42baSMatt Porter {
19983c42baSMatt Porter 	return sram_pool;
20983c42baSMatt Porter }
21983c42baSMatt Porter 
sram_alloc(size_t len,dma_addr_t * dma)2220e9969bSDavid Brownell void *sram_alloc(size_t len, dma_addr_t *dma)
2320e9969bSDavid Brownell {
2420e9969bSDavid Brownell 	dma_addr_t dma_base = davinci_soc_info.sram_dma;
2520e9969bSDavid Brownell 
2620e9969bSDavid Brownell 	if (dma)
2720e9969bSDavid Brownell 		*dma = 0;
2820e9969bSDavid Brownell 	if (!sram_pool || (dma && !dma_base))
2920e9969bSDavid Brownell 		return NULL;
3020e9969bSDavid Brownell 
3166f11969SNicolin Chen 	return gen_pool_dma_alloc(sram_pool, len, dma);
3220e9969bSDavid Brownell 
3320e9969bSDavid Brownell }
3420e9969bSDavid Brownell EXPORT_SYMBOL(sram_alloc);
3520e9969bSDavid Brownell 
sram_free(void * addr,size_t len)3620e9969bSDavid Brownell void sram_free(void *addr, size_t len)
3720e9969bSDavid Brownell {
3820e9969bSDavid Brownell 	gen_pool_free(sram_pool, (unsigned long) addr, len);
3920e9969bSDavid Brownell }
4020e9969bSDavid Brownell EXPORT_SYMBOL(sram_free);
4120e9969bSDavid Brownell 
4220e9969bSDavid Brownell 
4320e9969bSDavid Brownell /*
4420e9969bSDavid Brownell  * REVISIT This supports CPU and DMA access to/from SRAM, but it
4520e9969bSDavid Brownell  * doesn't (yet?) support some other notable uses of SRAM:  as TCM
4620e9969bSDavid Brownell  * for data and/or instructions; and holding code needed to enter
4720e9969bSDavid Brownell  * and exit suspend states (while DRAM can't be used).
4820e9969bSDavid Brownell  */
sram_init(void)4920e9969bSDavid Brownell static int __init sram_init(void)
5020e9969bSDavid Brownell {
51626863a3SBen Gardiner 	phys_addr_t phys = davinci_soc_info.sram_dma;
5220e9969bSDavid Brownell 	unsigned len = davinci_soc_info.sram_len;
5320e9969bSDavid Brownell 	int status = 0;
54182e7961SSekhar Nori 	void __iomem *addr;
5520e9969bSDavid Brownell 
5620e9969bSDavid Brownell 	if (len) {
571d3bba61SDavid Brownell 		len = min_t(unsigned, len, SRAM_SIZE);
5820e9969bSDavid Brownell 		sram_pool = gen_pool_create(ilog2(SRAM_GRANULARITY), -1);
5920e9969bSDavid Brownell 		if (!sram_pool)
6020e9969bSDavid Brownell 			status = -ENOMEM;
6120e9969bSDavid Brownell 	}
62626863a3SBen Gardiner 
63626863a3SBen Gardiner 	if (sram_pool) {
64626863a3SBen Gardiner 		addr = ioremap(phys, len);
65626863a3SBen Gardiner 		if (!addr)
66626863a3SBen Gardiner 			return -ENOMEM;
67182e7961SSekhar Nori 		status = gen_pool_add_virt(sram_pool, (unsigned long) addr,
68626863a3SBen Gardiner 					   phys, len, -1);
69626863a3SBen Gardiner 		if (status < 0)
70626863a3SBen Gardiner 			iounmap(addr);
71626863a3SBen Gardiner 	}
72626863a3SBen Gardiner 
7320e9969bSDavid Brownell 	WARN_ON(status < 0);
7420e9969bSDavid Brownell 	return status;
7520e9969bSDavid Brownell }
7620e9969bSDavid Brownell core_initcall(sram_init);
7720e9969bSDavid Brownell 
78