blob: 0c62aa2c86ea35981a4b32e201754b5a5489dad3 [file] [log] [blame]
Linus Walleijbc581772009-09-15 17:30:37 +01001/*
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 Walleijbc581772009-09-15 17:30:37 +010016#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
27static struct gen_pool *tcm_pool;
28
29/* TCM section definitions from the linker */
30extern char __itcm_start, __sitcm_text, __eitcm_text;
31extern char __dtcm_start, __sdtcm_data, __edtcm_data;
32
33/*
34 * TCM memory resources
35 */
36static struct resource dtcm_res = {
37 .name = "DTCM RAM",
38 .start = DTCM_OFFSET,
39 .end = DTCM_END,
40 .flags = IORESOURCE_MEM
41};
42
43static struct resource itcm_res = {
44 .name = "ITCM RAM",
45 .start = ITCM_OFFSET,
46 .end = ITCM_END,
47 .flags = IORESOURCE_MEM
48};
49
50static 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 Walleijcb9d7702010-07-12 21:50:59 +010055 .type = MT_MEMORY_DTCM
Linus Walleijbc581772009-09-15 17:30:37 +010056 }
57};
58
59static 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 Walleijcb9d7702010-07-12 21:50:59 +010064 .type = MT_MEMORY_ITCM
Linus Walleijbc581772009-09-15 17:30:37 +010065 }
66};
67
68/*
69 * Allocate a chunk of TCM memory
70 */
71void *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}
84EXPORT_SYMBOL(tcm_alloc);
85
86/*
87 * Free a chunk of TCM memory
88 */
89void tcm_free(void *addr, size_t len)
90{
91 gen_pool_free(tcm_pool, (unsigned long) addr, len);
92}
93EXPORT_SYMBOL(tcm_free);
94
95
96static void __init setup_tcm_bank(u8 type, u32 offset, u32 expected_size)
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
103 /* Read the special TCM region register c9, 0 */
104 if (!type)
105 asm("mrc p15, 0, %0, c9, c1, 0"
106 : "=r" (tcm_region));
107 else
108 asm("mrc p15, 0, %0, c9, c1, 1"
109 : "=r" (tcm_region));
110
111 tcm_size = tcm_sizes[(tcm_region >> 2) & 0x0f];
112 if (tcm_size < 0) {
113 pr_err("CPU: %sTCM of unknown size!\n",
114 type ? "I" : "D");
115 } else {
116 pr_info("CPU: found %sTCM %dk @ %08x, %senabled\n",
117 type ? "I" : "D",
118 tcm_size,
119 (tcm_region & 0xfffff000U),
120 (tcm_region & 1) ? "" : "not ");
121 }
122
123 if (tcm_size != expected_size) {
124 pr_crit("CPU: %sTCM was detected %dk but expected %dk!\n",
125 type ? "I" : "D",
126 tcm_size,
127 expected_size);
128 /* Adjust to the expected size? what can we do... */
129 }
130
131 /* Force move the TCM bank to where we want it, enable */
132 tcm_region = offset | (tcm_region & 0x00000ffeU) | 1;
133
134 if (!type)
135 asm("mcr p15, 0, %0, c9, c1, 0"
136 : /* No output operands */
137 : "r" (tcm_region));
138 else
139 asm("mcr p15, 0, %0, c9, c1, 1"
140 : /* No output operands */
141 : "r" (tcm_region));
142
143 pr_debug("CPU: moved %sTCM %dk to %08x, enabled\n",
144 type ? "I" : "D",
145 tcm_size,
146 (tcm_region & 0xfffff000U));
147}
148
149/*
150 * This initializes the TCM memory
151 */
152void __init tcm_init(void)
153{
154 u32 tcm_status = read_cpuid_tcmstatus();
155 char *start;
156 char *end;
157 char *ram;
158
159 /* Setup DTCM if present */
160 if (tcm_status & (1 << 16)) {
161 setup_tcm_bank(0, DTCM_OFFSET,
162 (DTCM_END - DTCM_OFFSET + 1) >> 10);
163 request_resource(&iomem_resource, &dtcm_res);
164 iotable_init(dtcm_iomap, 1);
165 /* Copy data from RAM to DTCM */
166 start = &__sdtcm_data;
167 end = &__edtcm_data;
168 ram = &__dtcm_start;
169 memcpy(start, ram, (end-start));
170 pr_debug("CPU DTCM: copied data from %p - %p\n", start, end);
171 }
172
173 /* Setup ITCM if present */
174 if (tcm_status & 1) {
175 setup_tcm_bank(1, ITCM_OFFSET,
176 (ITCM_END - ITCM_OFFSET + 1) >> 10);
177 request_resource(&iomem_resource, &itcm_res);
178 iotable_init(itcm_iomap, 1);
179 /* Copy code from RAM to ITCM */
180 start = &__sitcm_text;
181 end = &__eitcm_text;
182 ram = &__itcm_start;
183 memcpy(start, ram, (end-start));
184 pr_debug("CPU ITCM: copied code from %p - %p\n", start, end);
185 }
186}
187
188/*
189 * This creates the TCM memory pool and has to be done later,
190 * during the core_initicalls, since the allocator is not yet
191 * up and running when the first initialization runs.
192 */
193static int __init setup_tcm_pool(void)
194{
195 u32 tcm_status = read_cpuid_tcmstatus();
196 u32 dtcm_pool_start = (u32) &__edtcm_data;
197 u32 itcm_pool_start = (u32) &__eitcm_text;
198 int ret;
199
200 /*
201 * Set up malloc pool, 2^2 = 4 bytes granularity since
202 * the TCM is sometimes just 4 KiB. NB: pages and cache
203 * line alignments does not matter in TCM!
204 */
205 tcm_pool = gen_pool_create(2, -1);
206
207 pr_debug("Setting up TCM memory pool\n");
208
209 /* Add the rest of DTCM to the TCM pool */
210 if (tcm_status & (1 << 16)) {
211 if (dtcm_pool_start < DTCM_END) {
212 ret = gen_pool_add(tcm_pool, dtcm_pool_start,
213 DTCM_END - dtcm_pool_start + 1, -1);
214 if (ret) {
215 pr_err("CPU DTCM: could not add DTCM " \
216 "remainder to pool!\n");
217 return ret;
218 }
219 pr_debug("CPU DTCM: Added %08x bytes @ %08x to " \
220 "the TCM memory pool\n",
221 DTCM_END - dtcm_pool_start + 1,
222 dtcm_pool_start);
223 }
224 }
225
226 /* Add the rest of ITCM to the TCM pool */
227 if (tcm_status & 1) {
228 if (itcm_pool_start < ITCM_END) {
229 ret = gen_pool_add(tcm_pool, itcm_pool_start,
230 ITCM_END - itcm_pool_start + 1, -1);
231 if (ret) {
232 pr_err("CPU ITCM: could not add ITCM " \
233 "remainder to pool!\n");
234 return ret;
235 }
236 pr_debug("CPU ITCM: Added %08x bytes @ %08x to " \
237 "the TCM memory pool\n",
238 ITCM_END - itcm_pool_start + 1,
239 itcm_pool_start);
240 }
241 }
242 return 0;
243}
244
245core_initcall(setup_tcm_pool);