xref: /openbmc/linux/arch/arm/mach-davinci/sram.c (revision 626863a3f32f0baaf55a1d18b8d4fbb937700dda)
120e9969bSDavid Brownell /*
220e9969bSDavid Brownell  * mach-davinci/sram.c - DaVinci simple SRAM allocator
320e9969bSDavid Brownell  *
420e9969bSDavid Brownell  * Copyright (C) 2009 David Brownell
520e9969bSDavid Brownell  *
620e9969bSDavid Brownell  * This program is free software; you can redistribute it and/or modify
720e9969bSDavid Brownell  * it under the terms of the GNU General Public License as published by
820e9969bSDavid Brownell  * the Free Software Foundation; either version 2 of the License, or
920e9969bSDavid Brownell  * (at your option) any later version.
1020e9969bSDavid Brownell  */
1120e9969bSDavid Brownell #include <linux/module.h>
1220e9969bSDavid Brownell #include <linux/init.h>
13*626863a3SBen Gardiner #include <linux/io.h>
1420e9969bSDavid Brownell #include <linux/genalloc.h>
1520e9969bSDavid Brownell 
1620e9969bSDavid Brownell #include <mach/common.h>
1720e9969bSDavid Brownell #include <mach/sram.h>
1820e9969bSDavid Brownell 
1920e9969bSDavid Brownell static struct gen_pool *sram_pool;
2020e9969bSDavid Brownell 
2120e9969bSDavid Brownell void *sram_alloc(size_t len, dma_addr_t *dma)
2220e9969bSDavid Brownell {
2320e9969bSDavid Brownell 	unsigned long vaddr;
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 
3120e9969bSDavid Brownell 	vaddr = gen_pool_alloc(sram_pool, len);
3220e9969bSDavid Brownell 	if (!vaddr)
3320e9969bSDavid Brownell 		return NULL;
3420e9969bSDavid Brownell 
3520e9969bSDavid Brownell 	if (dma)
36*626863a3SBen Gardiner 		*dma = gen_pool_virt_to_phys(sram_pool, vaddr);
3720e9969bSDavid Brownell 	return (void *)vaddr;
3820e9969bSDavid Brownell 
3920e9969bSDavid Brownell }
4020e9969bSDavid Brownell EXPORT_SYMBOL(sram_alloc);
4120e9969bSDavid Brownell 
4220e9969bSDavid Brownell void sram_free(void *addr, size_t len)
4320e9969bSDavid Brownell {
4420e9969bSDavid Brownell 	gen_pool_free(sram_pool, (unsigned long) addr, len);
4520e9969bSDavid Brownell }
4620e9969bSDavid Brownell EXPORT_SYMBOL(sram_free);
4720e9969bSDavid Brownell 
4820e9969bSDavid Brownell 
4920e9969bSDavid Brownell /*
5020e9969bSDavid Brownell  * REVISIT This supports CPU and DMA access to/from SRAM, but it
5120e9969bSDavid Brownell  * doesn't (yet?) support some other notable uses of SRAM:  as TCM
5220e9969bSDavid Brownell  * for data and/or instructions; and holding code needed to enter
5320e9969bSDavid Brownell  * and exit suspend states (while DRAM can't be used).
5420e9969bSDavid Brownell  */
5520e9969bSDavid Brownell static int __init sram_init(void)
5620e9969bSDavid Brownell {
57*626863a3SBen Gardiner 	phys_addr_t phys = davinci_soc_info.sram_dma;
5820e9969bSDavid Brownell 	unsigned len = davinci_soc_info.sram_len;
5920e9969bSDavid Brownell 	int status = 0;
60*626863a3SBen Gardiner 	void *addr;
6120e9969bSDavid Brownell 
6220e9969bSDavid Brownell 	if (len) {
631d3bba61SDavid Brownell 		len = min_t(unsigned, len, SRAM_SIZE);
6420e9969bSDavid Brownell 		sram_pool = gen_pool_create(ilog2(SRAM_GRANULARITY), -1);
6520e9969bSDavid Brownell 		if (!sram_pool)
6620e9969bSDavid Brownell 			status = -ENOMEM;
6720e9969bSDavid Brownell 	}
68*626863a3SBen Gardiner 
69*626863a3SBen Gardiner 	if (sram_pool) {
70*626863a3SBen Gardiner 		addr = ioremap(phys, len);
71*626863a3SBen Gardiner 		if (!addr)
72*626863a3SBen Gardiner 			return -ENOMEM;
73*626863a3SBen Gardiner 		status = gen_pool_add_virt(sram_pool, (unsigned)addr,
74*626863a3SBen Gardiner 					   phys, len, -1);
75*626863a3SBen Gardiner 		if (status < 0)
76*626863a3SBen Gardiner 			iounmap(addr);
77*626863a3SBen Gardiner 	}
78*626863a3SBen Gardiner 
7920e9969bSDavid Brownell 	WARN_ON(status < 0);
8020e9969bSDavid Brownell 	return status;
8120e9969bSDavid Brownell }
8220e9969bSDavid Brownell core_initcall(sram_init);
8320e9969bSDavid Brownell 
84