Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008-2009 ST-Ericsson AB |
| 3 | * License terms: GNU General Public License (GPL) version 2 |
| 4 | * TCM memory handling for ARM systems |
| 5 | * |
| 6 | * Author: Linus Walleij <linus.walleij@stericsson.com> |
| 7 | * Author: Rickard Andersson <rickard.andersson@stericsson.com> |
| 8 | */ |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/stddef.h> |
| 13 | #include <linux/ioport.h> |
| 14 | #include <linux/genalloc.h> |
| 15 | #include <linux/string.h> /* memcpy */ |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 16 | #include <asm/cputype.h> |
| 17 | #include <asm/mach/map.h> |
| 18 | #include <mach/memory.h> |
| 19 | #include "tcm.h" |
| 20 | |
| 21 | /* Scream and warn about misuse */ |
| 22 | #if !defined(ITCM_OFFSET) || !defined(ITCM_END) || \ |
| 23 | !defined(DTCM_OFFSET) || !defined(DTCM_END) |
| 24 | #error "TCM support selected but offsets not defined!" |
| 25 | #endif |
| 26 | |
| 27 | static struct gen_pool *tcm_pool; |
| 28 | |
| 29 | /* TCM section definitions from the linker */ |
| 30 | extern char __itcm_start, __sitcm_text, __eitcm_text; |
| 31 | extern char __dtcm_start, __sdtcm_data, __edtcm_data; |
| 32 | |
| 33 | /* |
| 34 | * TCM memory resources |
| 35 | */ |
| 36 | static struct resource dtcm_res = { |
| 37 | .name = "DTCM RAM", |
| 38 | .start = DTCM_OFFSET, |
| 39 | .end = DTCM_END, |
| 40 | .flags = IORESOURCE_MEM |
| 41 | }; |
| 42 | |
| 43 | static struct resource itcm_res = { |
| 44 | .name = "ITCM RAM", |
| 45 | .start = ITCM_OFFSET, |
| 46 | .end = ITCM_END, |
| 47 | .flags = IORESOURCE_MEM |
| 48 | }; |
| 49 | |
| 50 | static struct map_desc dtcm_iomap[] __initdata = { |
| 51 | { |
| 52 | .virtual = DTCM_OFFSET, |
| 53 | .pfn = __phys_to_pfn(DTCM_OFFSET), |
| 54 | .length = (DTCM_END - DTCM_OFFSET + 1), |
Linus Walleij | cb9d770 | 2010-07-12 21:50:59 +0100 | [diff] [blame] | 55 | .type = MT_MEMORY_DTCM |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 56 | } |
| 57 | }; |
| 58 | |
| 59 | static struct map_desc itcm_iomap[] __initdata = { |
| 60 | { |
| 61 | .virtual = ITCM_OFFSET, |
| 62 | .pfn = __phys_to_pfn(ITCM_OFFSET), |
| 63 | .length = (ITCM_END - ITCM_OFFSET + 1), |
Linus Walleij | cb9d770 | 2010-07-12 21:50:59 +0100 | [diff] [blame] | 64 | .type = MT_MEMORY_ITCM |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 65 | } |
| 66 | }; |
| 67 | |
| 68 | /* |
| 69 | * Allocate a chunk of TCM memory |
| 70 | */ |
| 71 | void *tcm_alloc(size_t len) |
| 72 | { |
| 73 | unsigned long vaddr; |
| 74 | |
| 75 | if (!tcm_pool) |
| 76 | return NULL; |
| 77 | |
| 78 | vaddr = gen_pool_alloc(tcm_pool, len); |
| 79 | if (!vaddr) |
| 80 | return NULL; |
| 81 | |
| 82 | return (void *) vaddr; |
| 83 | } |
| 84 | EXPORT_SYMBOL(tcm_alloc); |
| 85 | |
| 86 | /* |
| 87 | * Free a chunk of TCM memory |
| 88 | */ |
| 89 | void tcm_free(void *addr, size_t len) |
| 90 | { |
| 91 | gen_pool_free(tcm_pool, (unsigned long) addr, len); |
| 92 | } |
| 93 | EXPORT_SYMBOL(tcm_free); |
| 94 | |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 95 | static void __init setup_tcm_bank(u8 type, u8 bank, u8 banks, |
| 96 | u32 offset, u32 expected_size) |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 97 | { |
| 98 | const int tcm_sizes[16] = { 0, -1, -1, 4, 8, 16, 32, 64, 128, |
| 99 | 256, 512, 1024, -1, -1, -1, -1 }; |
| 100 | u32 tcm_region; |
| 101 | int tcm_size; |
| 102 | |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 103 | /* |
| 104 | * If there are more than one TCM bank of this type, |
| 105 | * select the TCM bank to operate on in the TCM selection |
| 106 | * register. |
| 107 | */ |
| 108 | if (banks > 1) |
| 109 | asm("mcr p15, 0, %0, c9, c2, 0" |
| 110 | : /* No output operands */ |
| 111 | : "r" (bank)); |
| 112 | |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 113 | /* Read the special TCM region register c9, 0 */ |
| 114 | if (!type) |
| 115 | asm("mrc p15, 0, %0, c9, c1, 0" |
| 116 | : "=r" (tcm_region)); |
| 117 | else |
| 118 | asm("mrc p15, 0, %0, c9, c1, 1" |
| 119 | : "=r" (tcm_region)); |
| 120 | |
| 121 | tcm_size = tcm_sizes[(tcm_region >> 2) & 0x0f]; |
| 122 | if (tcm_size < 0) { |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 123 | pr_err("CPU: %sTCM%d of unknown size!\n", |
| 124 | type ? "I" : "D", bank); |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 125 | } else { |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 126 | pr_info("CPU: found %sTCM%d %dk @ %08x, %senabled\n", |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 127 | type ? "I" : "D", |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 128 | bank, |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 129 | tcm_size, |
| 130 | (tcm_region & 0xfffff000U), |
| 131 | (tcm_region & 1) ? "" : "not "); |
| 132 | } |
| 133 | |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 134 | if (tcm_size != (expected_size >> 10)) { |
| 135 | pr_crit("CPU: %sTCM%d was detected %dk but expected %dk!\n", |
| 136 | type ? "I" : "D", |
| 137 | bank, |
| 138 | tcm_size, |
| 139 | (expected_size >> 10)); |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 140 | /* Adjust to the expected size? what can we do... */ |
| 141 | } |
| 142 | |
| 143 | /* Force move the TCM bank to where we want it, enable */ |
| 144 | tcm_region = offset | (tcm_region & 0x00000ffeU) | 1; |
| 145 | |
| 146 | if (!type) |
| 147 | asm("mcr p15, 0, %0, c9, c1, 0" |
| 148 | : /* No output operands */ |
| 149 | : "r" (tcm_region)); |
| 150 | else |
| 151 | asm("mcr p15, 0, %0, c9, c1, 1" |
| 152 | : /* No output operands */ |
| 153 | : "r" (tcm_region)); |
| 154 | |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 155 | pr_info("CPU: moved %sTCM%d %dk to %08x, enabled\n", |
| 156 | type ? "I" : "D", |
| 157 | bank, |
| 158 | tcm_size, |
| 159 | (tcm_region & 0xfffff000U)); |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 162 | /* We expect to find what is configured for the platform */ |
| 163 | #define DTCM_EXPECTED (DTCM_END - DTCM_OFFSET + 1) |
| 164 | #define ITCM_EXPECTED (ITCM_END - ITCM_OFFSET + 1) |
| 165 | |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 166 | /* |
| 167 | * This initializes the TCM memory |
| 168 | */ |
| 169 | void __init tcm_init(void) |
| 170 | { |
| 171 | u32 tcm_status = read_cpuid_tcmstatus(); |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 172 | u8 dtcm_banks = (tcm_status >> 16) & 0x03; |
| 173 | u32 dtcm_banksize = DTCM_EXPECTED / dtcm_banks; |
| 174 | u8 itcm_banks = (tcm_status & 0x03); |
| 175 | u32 itcm_banksize = ITCM_EXPECTED / itcm_banks; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 176 | char *start; |
| 177 | char *end; |
| 178 | char *ram; |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 179 | int i; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 180 | |
| 181 | /* Setup DTCM if present */ |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 182 | for (i = 0; i < dtcm_banks; i++) { |
| 183 | setup_tcm_bank(0, i, dtcm_banks, |
| 184 | DTCM_OFFSET + (i * dtcm_banksize), |
| 185 | dtcm_banksize); |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 186 | request_resource(&iomem_resource, &dtcm_res); |
| 187 | iotable_init(dtcm_iomap, 1); |
| 188 | /* Copy data from RAM to DTCM */ |
| 189 | start = &__sdtcm_data; |
| 190 | end = &__edtcm_data; |
| 191 | ram = &__dtcm_start; |
| 192 | memcpy(start, ram, (end-start)); |
| 193 | pr_debug("CPU DTCM: copied data from %p - %p\n", start, end); |
| 194 | } |
| 195 | |
| 196 | /* Setup ITCM if present */ |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 197 | for (i = 0; i < itcm_banks; i++) { |
| 198 | setup_tcm_bank(1, i, itcm_banks, |
| 199 | ITCM_OFFSET + (i * itcm_banksize), |
| 200 | itcm_banksize); |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 201 | request_resource(&iomem_resource, &itcm_res); |
| 202 | iotable_init(itcm_iomap, 1); |
| 203 | /* Copy code from RAM to ITCM */ |
| 204 | start = &__sitcm_text; |
| 205 | end = &__eitcm_text; |
| 206 | ram = &__itcm_start; |
| 207 | memcpy(start, ram, (end-start)); |
| 208 | pr_debug("CPU ITCM: copied code from %p - %p\n", start, end); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * This creates the TCM memory pool and has to be done later, |
| 214 | * during the core_initicalls, since the allocator is not yet |
| 215 | * up and running when the first initialization runs. |
| 216 | */ |
| 217 | static int __init setup_tcm_pool(void) |
| 218 | { |
| 219 | u32 tcm_status = read_cpuid_tcmstatus(); |
| 220 | u32 dtcm_pool_start = (u32) &__edtcm_data; |
| 221 | u32 itcm_pool_start = (u32) &__eitcm_text; |
| 222 | int ret; |
| 223 | |
| 224 | /* |
| 225 | * Set up malloc pool, 2^2 = 4 bytes granularity since |
| 226 | * the TCM is sometimes just 4 KiB. NB: pages and cache |
| 227 | * line alignments does not matter in TCM! |
| 228 | */ |
| 229 | tcm_pool = gen_pool_create(2, -1); |
| 230 | |
| 231 | pr_debug("Setting up TCM memory pool\n"); |
| 232 | |
| 233 | /* Add the rest of DTCM to the TCM pool */ |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 234 | if (tcm_status & (0x03 << 16)) { |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 235 | if (dtcm_pool_start < DTCM_END) { |
| 236 | ret = gen_pool_add(tcm_pool, dtcm_pool_start, |
| 237 | DTCM_END - dtcm_pool_start + 1, -1); |
| 238 | if (ret) { |
| 239 | pr_err("CPU DTCM: could not add DTCM " \ |
| 240 | "remainder to pool!\n"); |
| 241 | return ret; |
| 242 | } |
| 243 | pr_debug("CPU DTCM: Added %08x bytes @ %08x to " \ |
| 244 | "the TCM memory pool\n", |
| 245 | DTCM_END - dtcm_pool_start + 1, |
| 246 | dtcm_pool_start); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* Add the rest of ITCM to the TCM pool */ |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame^] | 251 | if (tcm_status & 0x03) { |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 252 | if (itcm_pool_start < ITCM_END) { |
| 253 | ret = gen_pool_add(tcm_pool, itcm_pool_start, |
| 254 | ITCM_END - itcm_pool_start + 1, -1); |
| 255 | if (ret) { |
| 256 | pr_err("CPU ITCM: could not add ITCM " \ |
| 257 | "remainder to pool!\n"); |
| 258 | return ret; |
| 259 | } |
| 260 | pr_debug("CPU ITCM: Added %08x bytes @ %08x to " \ |
| 261 | "the TCM memory pool\n", |
| 262 | ITCM_END - itcm_pool_start + 1, |
| 263 | itcm_pool_start); |
| 264 | } |
| 265 | } |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | core_initcall(setup_tcm_pool); |