| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 12 | #include <linux/init.h> | 
| Ben Gardiner | 626863a | 2012-10-05 13:04:41 -0400 | [diff] [blame] | 13 | #include <linux/io.h> | 
| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 14 | #include <linux/genalloc.h> | 
|  | 15 |  | 
|  | 16 | #include <mach/common.h> | 
| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 17 | #include <mach/sram.h> | 
|  | 18 |  | 
| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 19 | static struct gen_pool *sram_pool; | 
|  | 20 |  | 
| Matt Porter | 983c42b | 2012-10-05 13:04:43 -0400 | [diff] [blame] | 21 | struct gen_pool *sram_get_gen_pool(void) | 
|  | 22 | { | 
|  | 23 | return sram_pool; | 
|  | 24 | } | 
|  | 25 |  | 
| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 26 | void *sram_alloc(size_t len, dma_addr_t *dma) | 
|  | 27 | { | 
|  | 28 | unsigned long vaddr; | 
|  | 29 | dma_addr_t dma_base = davinci_soc_info.sram_dma; | 
|  | 30 |  | 
|  | 31 | if (dma) | 
|  | 32 | *dma = 0; | 
|  | 33 | if (!sram_pool || (dma && !dma_base)) | 
|  | 34 | return NULL; | 
|  | 35 |  | 
|  | 36 | vaddr = gen_pool_alloc(sram_pool, len); | 
|  | 37 | if (!vaddr) | 
|  | 38 | return NULL; | 
|  | 39 |  | 
|  | 40 | if (dma) | 
| Ben Gardiner | 626863a | 2012-10-05 13:04:41 -0400 | [diff] [blame] | 41 | *dma = gen_pool_virt_to_phys(sram_pool, vaddr); | 
| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 42 | return (void *)vaddr; | 
|  | 43 |  | 
|  | 44 | } | 
|  | 45 | EXPORT_SYMBOL(sram_alloc); | 
|  | 46 |  | 
|  | 47 | void sram_free(void *addr, size_t len) | 
|  | 48 | { | 
|  | 49 | gen_pool_free(sram_pool, (unsigned long) addr, len); | 
|  | 50 | } | 
|  | 51 | EXPORT_SYMBOL(sram_free); | 
|  | 52 |  | 
|  | 53 |  | 
|  | 54 | /* | 
|  | 55 | * REVISIT This supports CPU and DMA access to/from SRAM, but it | 
|  | 56 | * doesn't (yet?) support some other notable uses of SRAM:  as TCM | 
|  | 57 | * for data and/or instructions; and holding code needed to enter | 
|  | 58 | * and exit suspend states (while DRAM can't be used). | 
|  | 59 | */ | 
|  | 60 | static int __init sram_init(void) | 
|  | 61 | { | 
| Ben Gardiner | 626863a | 2012-10-05 13:04:41 -0400 | [diff] [blame] | 62 | phys_addr_t phys = davinci_soc_info.sram_dma; | 
| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 63 | unsigned len = davinci_soc_info.sram_len; | 
|  | 64 | int status = 0; | 
| Ben Gardiner | 626863a | 2012-10-05 13:04:41 -0400 | [diff] [blame] | 65 | void *addr; | 
| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 66 |  | 
|  | 67 | if (len) { | 
| David Brownell | 1d3bba6 | 2009-06-05 21:45:14 -0700 | [diff] [blame] | 68 | len = min_t(unsigned, len, SRAM_SIZE); | 
| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 69 | sram_pool = gen_pool_create(ilog2(SRAM_GRANULARITY), -1); | 
|  | 70 | if (!sram_pool) | 
|  | 71 | status = -ENOMEM; | 
|  | 72 | } | 
| Ben Gardiner | 626863a | 2012-10-05 13:04:41 -0400 | [diff] [blame] | 73 |  | 
|  | 74 | if (sram_pool) { | 
|  | 75 | addr = ioremap(phys, len); | 
|  | 76 | if (!addr) | 
|  | 77 | return -ENOMEM; | 
|  | 78 | status = gen_pool_add_virt(sram_pool, (unsigned)addr, | 
|  | 79 | phys, len, -1); | 
|  | 80 | if (status < 0) | 
|  | 81 | iounmap(addr); | 
|  | 82 | } | 
|  | 83 |  | 
| David Brownell | 20e9969 | 2009-05-07 09:31:42 -0700 | [diff] [blame] | 84 | WARN_ON(status < 0); | 
|  | 85 | return status; | 
|  | 86 | } | 
|  | 87 | core_initcall(sram_init); | 
|  | 88 |  |