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> |
Russell King | f4117ac | 2011-01-04 18:07:14 +0000 | [diff] [blame] | 18 | #include <asm/memory.h> |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 19 | #include "tcm.h" |
| 20 | |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 21 | static struct gen_pool *tcm_pool; |
| 22 | |
| 23 | /* TCM section definitions from the linker */ |
| 24 | extern char __itcm_start, __sitcm_text, __eitcm_text; |
| 25 | extern char __dtcm_start, __sdtcm_data, __edtcm_data; |
| 26 | |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 27 | /* These will be increased as we run */ |
| 28 | u32 dtcm_end = DTCM_OFFSET; |
| 29 | u32 itcm_end = ITCM_OFFSET; |
| 30 | |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 31 | /* |
| 32 | * TCM memory resources |
| 33 | */ |
| 34 | static struct resource dtcm_res = { |
| 35 | .name = "DTCM RAM", |
| 36 | .start = DTCM_OFFSET, |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 37 | .end = DTCM_OFFSET, |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 38 | .flags = IORESOURCE_MEM |
| 39 | }; |
| 40 | |
| 41 | static struct resource itcm_res = { |
| 42 | .name = "ITCM RAM", |
| 43 | .start = ITCM_OFFSET, |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 44 | .end = ITCM_OFFSET, |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 45 | .flags = IORESOURCE_MEM |
| 46 | }; |
| 47 | |
| 48 | static struct map_desc dtcm_iomap[] __initdata = { |
| 49 | { |
| 50 | .virtual = DTCM_OFFSET, |
| 51 | .pfn = __phys_to_pfn(DTCM_OFFSET), |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 52 | .length = 0, |
Linus Walleij | cb9d770 | 2010-07-12 21:50:59 +0100 | [diff] [blame] | 53 | .type = MT_MEMORY_DTCM |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
| 57 | static struct map_desc itcm_iomap[] __initdata = { |
| 58 | { |
| 59 | .virtual = ITCM_OFFSET, |
| 60 | .pfn = __phys_to_pfn(ITCM_OFFSET), |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 61 | .length = 0, |
Linus Walleij | cb9d770 | 2010-07-12 21:50:59 +0100 | [diff] [blame] | 62 | .type = MT_MEMORY_ITCM |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
| 66 | /* |
| 67 | * Allocate a chunk of TCM memory |
| 68 | */ |
| 69 | void *tcm_alloc(size_t len) |
| 70 | { |
| 71 | unsigned long vaddr; |
| 72 | |
| 73 | if (!tcm_pool) |
| 74 | return NULL; |
| 75 | |
| 76 | vaddr = gen_pool_alloc(tcm_pool, len); |
| 77 | if (!vaddr) |
| 78 | return NULL; |
| 79 | |
| 80 | return (void *) vaddr; |
| 81 | } |
| 82 | EXPORT_SYMBOL(tcm_alloc); |
| 83 | |
| 84 | /* |
| 85 | * Free a chunk of TCM memory |
| 86 | */ |
| 87 | void tcm_free(void *addr, size_t len) |
| 88 | { |
| 89 | gen_pool_free(tcm_pool, (unsigned long) addr, len); |
| 90 | } |
| 91 | EXPORT_SYMBOL(tcm_free); |
| 92 | |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 93 | static int __init setup_tcm_bank(u8 type, u8 bank, u8 banks, |
| 94 | u32 *offset) |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 95 | { |
| 96 | const int tcm_sizes[16] = { 0, -1, -1, 4, 8, 16, 32, 64, 128, |
| 97 | 256, 512, 1024, -1, -1, -1, -1 }; |
| 98 | u32 tcm_region; |
| 99 | int tcm_size; |
| 100 | |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 101 | /* |
| 102 | * If there are more than one TCM bank of this type, |
| 103 | * select the TCM bank to operate on in the TCM selection |
| 104 | * register. |
| 105 | */ |
| 106 | if (banks > 1) |
| 107 | asm("mcr p15, 0, %0, c9, c2, 0" |
| 108 | : /* No output operands */ |
| 109 | : "r" (bank)); |
| 110 | |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 111 | /* Read the special TCM region register c9, 0 */ |
| 112 | if (!type) |
| 113 | asm("mrc p15, 0, %0, c9, c1, 0" |
| 114 | : "=r" (tcm_region)); |
| 115 | else |
| 116 | asm("mrc p15, 0, %0, c9, c1, 1" |
| 117 | : "=r" (tcm_region)); |
| 118 | |
| 119 | tcm_size = tcm_sizes[(tcm_region >> 2) & 0x0f]; |
| 120 | if (tcm_size < 0) { |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 121 | pr_err("CPU: %sTCM%d of unknown size\n", |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 122 | type ? "I" : "D", bank); |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 123 | return -EINVAL; |
| 124 | } else if (tcm_size > 32) { |
| 125 | pr_err("CPU: %sTCM%d larger than 32k found\n", |
| 126 | type ? "I" : "D", bank); |
| 127 | return -EINVAL; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 128 | } else { |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 129 | pr_info("CPU: found %sTCM%d %dk @ %08x, %senabled\n", |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 130 | type ? "I" : "D", |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 131 | bank, |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 132 | tcm_size, |
| 133 | (tcm_region & 0xfffff000U), |
| 134 | (tcm_region & 1) ? "" : "not "); |
| 135 | } |
| 136 | |
Linus Walleij | 9715efb | 2011-07-01 08:23:06 +0100 | [diff] [blame^] | 137 | /* Not much fun you can do with a size 0 bank */ |
| 138 | if (tcm_size == 0) |
| 139 | return 0; |
| 140 | |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 141 | /* Force move the TCM bank to where we want it, enable */ |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 142 | tcm_region = *offset | (tcm_region & 0x00000ffeU) | 1; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 143 | |
| 144 | if (!type) |
| 145 | asm("mcr p15, 0, %0, c9, c1, 0" |
| 146 | : /* No output operands */ |
| 147 | : "r" (tcm_region)); |
| 148 | else |
| 149 | asm("mcr p15, 0, %0, c9, c1, 1" |
| 150 | : /* No output operands */ |
| 151 | : "r" (tcm_region)); |
| 152 | |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 153 | /* Increase offset */ |
| 154 | *offset += (tcm_size << 10); |
| 155 | |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 156 | pr_info("CPU: moved %sTCM%d %dk to %08x, enabled\n", |
| 157 | type ? "I" : "D", |
| 158 | bank, |
| 159 | tcm_size, |
| 160 | (tcm_region & 0xfffff000U)); |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 161 | return 0; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * This initializes the TCM memory |
| 166 | */ |
| 167 | void __init tcm_init(void) |
| 168 | { |
| 169 | u32 tcm_status = read_cpuid_tcmstatus(); |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 170 | u8 dtcm_banks = (tcm_status >> 16) & 0x03; |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 171 | u8 itcm_banks = (tcm_status & 0x03); |
Linus Walleij | 9715efb | 2011-07-01 08:23:06 +0100 | [diff] [blame^] | 172 | size_t dtcm_code_sz = &__edtcm_data - &__sdtcm_data; |
| 173 | size_t itcm_code_sz = &__eitcm_text - &__sitcm_text; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 174 | char *start; |
| 175 | char *end; |
| 176 | char *ram; |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 177 | int ret; |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 178 | int i; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 179 | |
Linus Walleij | 9715efb | 2011-07-01 08:23:06 +0100 | [diff] [blame^] | 180 | /* Values greater than 2 for D/ITCM banks are "reserved" */ |
| 181 | if (dtcm_banks > 2) |
| 182 | dtcm_banks = 0; |
| 183 | if (itcm_banks > 2) |
| 184 | itcm_banks = 0; |
| 185 | |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 186 | /* Setup DTCM if present */ |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 187 | if (dtcm_banks > 0) { |
| 188 | for (i = 0; i < dtcm_banks; i++) { |
| 189 | ret = setup_tcm_bank(0, i, dtcm_banks, &dtcm_end); |
| 190 | if (ret) |
| 191 | return; |
| 192 | } |
Linus Walleij | 9715efb | 2011-07-01 08:23:06 +0100 | [diff] [blame^] | 193 | /* This means you compiled more code than fits into DTCM */ |
| 194 | if (dtcm_code_sz > (dtcm_end - DTCM_OFFSET)) { |
| 195 | pr_info("CPU DTCM: %u bytes of code compiled to " |
| 196 | "DTCM but only %lu bytes of DTCM present\n", |
| 197 | dtcm_code_sz, (dtcm_end - DTCM_OFFSET)); |
| 198 | goto no_dtcm; |
| 199 | } |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 200 | dtcm_res.end = dtcm_end - 1; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 201 | request_resource(&iomem_resource, &dtcm_res); |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 202 | dtcm_iomap[0].length = dtcm_end - DTCM_OFFSET; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 203 | iotable_init(dtcm_iomap, 1); |
| 204 | /* Copy data from RAM to DTCM */ |
| 205 | start = &__sdtcm_data; |
| 206 | end = &__edtcm_data; |
| 207 | ram = &__dtcm_start; |
Linus Walleij | 9715efb | 2011-07-01 08:23:06 +0100 | [diff] [blame^] | 208 | memcpy(start, ram, dtcm_code_sz); |
| 209 | pr_debug("CPU DTCM: copied data from %p - %p\n", |
| 210 | start, end); |
| 211 | } else if (dtcm_code_sz) { |
| 212 | pr_info("CPU DTCM: %u bytes of code compiled to DTCM but no " |
| 213 | "DTCM banks present in CPU\n", dtcm_code_sz); |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 214 | } |
| 215 | |
Linus Walleij | 9715efb | 2011-07-01 08:23:06 +0100 | [diff] [blame^] | 216 | no_dtcm: |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 217 | /* Setup ITCM if present */ |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 218 | if (itcm_banks > 0) { |
| 219 | for (i = 0; i < itcm_banks; i++) { |
| 220 | ret = setup_tcm_bank(1, i, itcm_banks, &itcm_end); |
| 221 | if (ret) |
| 222 | return; |
| 223 | } |
Linus Walleij | 9715efb | 2011-07-01 08:23:06 +0100 | [diff] [blame^] | 224 | /* This means you compiled more code than fits into ITCM */ |
| 225 | if (itcm_code_sz > (itcm_end - ITCM_OFFSET)) { |
| 226 | pr_info("CPU ITCM: %u bytes of code compiled to " |
| 227 | "ITCM but only %lu bytes of ITCM present\n", |
| 228 | itcm_code_sz, (itcm_end - ITCM_OFFSET)); |
| 229 | return; |
| 230 | } |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 231 | itcm_res.end = itcm_end - 1; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 232 | request_resource(&iomem_resource, &itcm_res); |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 233 | itcm_iomap[0].length = itcm_end - ITCM_OFFSET; |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 234 | iotable_init(itcm_iomap, 1); |
| 235 | /* Copy code from RAM to ITCM */ |
| 236 | start = &__sitcm_text; |
| 237 | end = &__eitcm_text; |
| 238 | ram = &__itcm_start; |
Linus Walleij | 9715efb | 2011-07-01 08:23:06 +0100 | [diff] [blame^] | 239 | memcpy(start, ram, itcm_code_sz); |
| 240 | pr_debug("CPU ITCM: copied code from %p - %p\n", |
| 241 | start, end); |
| 242 | } else if (itcm_code_sz) { |
| 243 | pr_info("CPU ITCM: %u bytes of code compiled to ITCM but no " |
| 244 | "ITCM banks present in CPU\n", itcm_code_sz); |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * This creates the TCM memory pool and has to be done later, |
| 250 | * during the core_initicalls, since the allocator is not yet |
| 251 | * up and running when the first initialization runs. |
| 252 | */ |
| 253 | static int __init setup_tcm_pool(void) |
| 254 | { |
| 255 | u32 tcm_status = read_cpuid_tcmstatus(); |
| 256 | u32 dtcm_pool_start = (u32) &__edtcm_data; |
| 257 | u32 itcm_pool_start = (u32) &__eitcm_text; |
| 258 | int ret; |
| 259 | |
| 260 | /* |
| 261 | * Set up malloc pool, 2^2 = 4 bytes granularity since |
| 262 | * the TCM is sometimes just 4 KiB. NB: pages and cache |
| 263 | * line alignments does not matter in TCM! |
| 264 | */ |
| 265 | tcm_pool = gen_pool_create(2, -1); |
| 266 | |
| 267 | pr_debug("Setting up TCM memory pool\n"); |
| 268 | |
| 269 | /* Add the rest of DTCM to the TCM pool */ |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 270 | if (tcm_status & (0x03 << 16)) { |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 271 | if (dtcm_pool_start < dtcm_end) { |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 272 | ret = gen_pool_add(tcm_pool, dtcm_pool_start, |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 273 | dtcm_end - dtcm_pool_start, -1); |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 274 | if (ret) { |
| 275 | pr_err("CPU DTCM: could not add DTCM " \ |
| 276 | "remainder to pool!\n"); |
| 277 | return ret; |
| 278 | } |
| 279 | pr_debug("CPU DTCM: Added %08x bytes @ %08x to " \ |
| 280 | "the TCM memory pool\n", |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 281 | dtcm_end - dtcm_pool_start, |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 282 | dtcm_pool_start); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | /* Add the rest of ITCM to the TCM pool */ |
Linus Walleij | 5985097 | 2010-07-12 21:51:41 +0100 | [diff] [blame] | 287 | if (tcm_status & 0x03) { |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 288 | if (itcm_pool_start < itcm_end) { |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 289 | ret = gen_pool_add(tcm_pool, itcm_pool_start, |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 290 | itcm_end - itcm_pool_start, -1); |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 291 | if (ret) { |
| 292 | pr_err("CPU ITCM: could not add ITCM " \ |
| 293 | "remainder to pool!\n"); |
| 294 | return ret; |
| 295 | } |
| 296 | pr_debug("CPU ITCM: Added %08x bytes @ %08x to " \ |
| 297 | "the TCM memory pool\n", |
Linus Walleij | 1dbd30e | 2010-07-12 21:53:28 +0100 | [diff] [blame] | 298 | itcm_end - itcm_pool_start, |
Linus Walleij | bc58177 | 2009-09-15 17:30:37 +0100 | [diff] [blame] | 299 | itcm_pool_start); |
| 300 | } |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | core_initcall(setup_tcm_pool); |