1bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij/*
2bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * Copyright (C) 2008-2009 ST-Ericsson AB
3bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * License terms: GNU General Public License (GPL) version 2
4bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * TCM memory handling for ARM systems
5bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij *
6bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * Author: Linus Walleij <linus.walleij@stericsson.com>
7bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * Author: Rickard Andersson <rickard.andersson@stericsson.com>
8bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij */
9bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include <linux/init.h>
10bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include <linux/kernel.h>
11bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include <linux/module.h>
12bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include <linux/stddef.h>
13bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include <linux/ioport.h>
14bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include <linux/genalloc.h>
15bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include <linux/string.h> /* memcpy */
16bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include <asm/cputype.h>
17bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include <asm/mach/map.h>
18f4117ac9e237b74afdf5e001d5ea26a4d15e9847Russell King#include <asm/memory.h>
199f97da78bf018206fb623cd351d454af2f105fe0David Howells#include <asm/system_info.h>
20bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij#include "tcm.h"
21bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
22bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijstatic struct gen_pool *tcm_pool;
23201043f227576d42529ddb340746a060a00f57f6Linus Walleijstatic bool dtcm_present;
24201043f227576d42529ddb340746a060a00f57f6Linus Walleijstatic bool itcm_present;
25bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
26bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij/* TCM section definitions from the linker */
27bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijextern char __itcm_start, __sitcm_text, __eitcm_text;
28bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijextern char __dtcm_start, __sdtcm_data, __edtcm_data;
29bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
301dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij/* These will be increased as we run */
311dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleiju32 dtcm_end = DTCM_OFFSET;
321dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleiju32 itcm_end = ITCM_OFFSET;
331dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij
34bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij/*
35bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * TCM memory resources
36bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij */
37bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijstatic struct resource dtcm_res = {
38bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	.name = "DTCM RAM",
39bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	.start = DTCM_OFFSET,
401dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	.end = DTCM_OFFSET,
41bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	.flags = IORESOURCE_MEM
42bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij};
43bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
44bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijstatic struct resource itcm_res = {
45bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	.name = "ITCM RAM",
46bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	.start = ITCM_OFFSET,
471dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	.end = ITCM_OFFSET,
48bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	.flags = IORESOURCE_MEM
49bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij};
50bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
51bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijstatic struct map_desc dtcm_iomap[] __initdata = {
52bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	{
53bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		.virtual	= DTCM_OFFSET,
54bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		.pfn		= __phys_to_pfn(DTCM_OFFSET),
551dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		.length		= 0,
56cb9d7707cd9be57830f31616233f6a872ca8416dLinus Walleij		.type		= MT_MEMORY_DTCM
57bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	}
58bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij};
59bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
60bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijstatic struct map_desc itcm_iomap[] __initdata = {
61bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	{
62bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		.virtual	= ITCM_OFFSET,
63bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		.pfn		= __phys_to_pfn(ITCM_OFFSET),
641dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		.length		= 0,
65cb9d7707cd9be57830f31616233f6a872ca8416dLinus Walleij		.type		= MT_MEMORY_ITCM
66bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	}
67bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij};
68bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
69bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij/*
70bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * Allocate a chunk of TCM memory
71bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij */
72bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijvoid *tcm_alloc(size_t len)
73bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij{
74bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	unsigned long vaddr;
75bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
76bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	if (!tcm_pool)
77bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		return NULL;
78bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
79bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	vaddr = gen_pool_alloc(tcm_pool, len);
80bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	if (!vaddr)
81bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		return NULL;
82bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
83bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	return (void *) vaddr;
84bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij}
85bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus WalleijEXPORT_SYMBOL(tcm_alloc);
86bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
87bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij/*
88bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * Free a chunk of TCM memory
89bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij */
90bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijvoid tcm_free(void *addr, size_t len)
91bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij{
92bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	gen_pool_free(tcm_pool, (unsigned long) addr, len);
93bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij}
94bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus WalleijEXPORT_SYMBOL(tcm_free);
95bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
96201043f227576d42529ddb340746a060a00f57f6Linus Walleijbool tcm_dtcm_present(void)
97201043f227576d42529ddb340746a060a00f57f6Linus Walleij{
98201043f227576d42529ddb340746a060a00f57f6Linus Walleij	return dtcm_present;
99201043f227576d42529ddb340746a060a00f57f6Linus Walleij}
100201043f227576d42529ddb340746a060a00f57f6Linus WalleijEXPORT_SYMBOL(tcm_dtcm_present);
101201043f227576d42529ddb340746a060a00f57f6Linus Walleij
102201043f227576d42529ddb340746a060a00f57f6Linus Walleijbool tcm_itcm_present(void)
103201043f227576d42529ddb340746a060a00f57f6Linus Walleij{
104201043f227576d42529ddb340746a060a00f57f6Linus Walleij	return itcm_present;
105201043f227576d42529ddb340746a060a00f57f6Linus Walleij}
106201043f227576d42529ddb340746a060a00f57f6Linus WalleijEXPORT_SYMBOL(tcm_itcm_present);
107201043f227576d42529ddb340746a060a00f57f6Linus Walleij
1081dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleijstatic int __init setup_tcm_bank(u8 type, u8 bank, u8 banks,
1091dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij				  u32 *offset)
110bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij{
111bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	const int tcm_sizes[16] = { 0, -1, -1, 4, 8, 16, 32, 64, 128,
112bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				    256, 512, 1024, -1, -1, -1, -1 };
113bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	u32 tcm_region;
114bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	int tcm_size;
115bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
116598509779e5b8037d371df764d7438744a24b61fLinus Walleij	/*
117598509779e5b8037d371df764d7438744a24b61fLinus Walleij	 * If there are more than one TCM bank of this type,
118598509779e5b8037d371df764d7438744a24b61fLinus Walleij	 * select the TCM bank to operate on in the TCM selection
119598509779e5b8037d371df764d7438744a24b61fLinus Walleij	 * register.
120598509779e5b8037d371df764d7438744a24b61fLinus Walleij	 */
121598509779e5b8037d371df764d7438744a24b61fLinus Walleij	if (banks > 1)
122598509779e5b8037d371df764d7438744a24b61fLinus Walleij		asm("mcr	p15, 0, %0, c9, c2, 0"
123598509779e5b8037d371df764d7438744a24b61fLinus Walleij		    : /* No output operands */
124598509779e5b8037d371df764d7438744a24b61fLinus Walleij		    : "r" (bank));
125598509779e5b8037d371df764d7438744a24b61fLinus Walleij
126bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	/* Read the special TCM region register c9, 0 */
127bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	if (!type)
128bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		asm("mrc	p15, 0, %0, c9, c1, 0"
129bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		    : "=r" (tcm_region));
130bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	else
131bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		asm("mrc	p15, 0, %0, c9, c1, 1"
132bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		    : "=r" (tcm_region));
133bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
134bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	tcm_size = tcm_sizes[(tcm_region >> 2) & 0x0f];
135bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	if (tcm_size < 0) {
1361dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		pr_err("CPU: %sTCM%d of unknown size\n",
137598509779e5b8037d371df764d7438744a24b61fLinus Walleij		       type ? "I" : "D", bank);
1381dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		return -EINVAL;
1391dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	} else if (tcm_size > 32) {
1401dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		pr_err("CPU: %sTCM%d larger than 32k found\n",
1411dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		       type ? "I" : "D", bank);
1421dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		return -EINVAL;
143bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	} else {
144598509779e5b8037d371df764d7438744a24b61fLinus Walleij		pr_info("CPU: found %sTCM%d %dk @ %08x, %senabled\n",
145bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			type ? "I" : "D",
146598509779e5b8037d371df764d7438744a24b61fLinus Walleij			bank,
147bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			tcm_size,
148bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			(tcm_region & 0xfffff000U),
149bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			(tcm_region & 1) ? "" : "not ");
150bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	}
151bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
1529715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij	/* Not much fun you can do with a size 0 bank */
1539715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij	if (tcm_size == 0)
1549715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		return 0;
1559715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij
156bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	/* Force move the TCM bank to where we want it, enable */
1571dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	tcm_region = *offset | (tcm_region & 0x00000ffeU) | 1;
158bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
159bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	if (!type)
160bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		asm("mcr	p15, 0, %0, c9, c1, 0"
161bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		    : /* No output operands */
162bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		    : "r" (tcm_region));
163bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	else
164bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		asm("mcr	p15, 0, %0, c9, c1, 1"
165bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		    : /* No output operands */
166bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		    : "r" (tcm_region));
167bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
1681dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	/* Increase offset */
1691dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	*offset += (tcm_size << 10);
1701dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij
171598509779e5b8037d371df764d7438744a24b61fLinus Walleij	pr_info("CPU: moved %sTCM%d %dk to %08x, enabled\n",
172598509779e5b8037d371df764d7438744a24b61fLinus Walleij		type ? "I" : "D",
173598509779e5b8037d371df764d7438744a24b61fLinus Walleij		bank,
174598509779e5b8037d371df764d7438744a24b61fLinus Walleij		tcm_size,
175598509779e5b8037d371df764d7438744a24b61fLinus Walleij		(tcm_region & 0xfffff000U));
1761dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	return 0;
177bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij}
178bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
179bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij/*
180bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * This initializes the TCM memory
181bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij */
182bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijvoid __init tcm_init(void)
183bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij{
18490b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	u32 tcm_status;
18590b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	u8 dtcm_banks;
18690b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	u8 itcm_banks;
1879715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij	size_t dtcm_code_sz = &__edtcm_data - &__sdtcm_data;
1889715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij	size_t itcm_code_sz = &__eitcm_text - &__sitcm_text;
189bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	char *start;
190bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	char *end;
191bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	char *ram;
1921dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	int ret;
193598509779e5b8037d371df764d7438744a24b61fLinus Walleij	int i;
194bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
19590b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	/*
19690b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	 * Prior to ARMv5 there is no TCM, and trying to read the status
19790b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	 * register will hang the processor.
19890b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	 */
19990b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	if (cpu_architecture() < CPU_ARCH_ARMv5) {
20090b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij		if (dtcm_code_sz || itcm_code_sz)
20190b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij			pr_info("CPU TCM: %u bytes of DTCM and %u bytes of "
20290b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij				"ITCM code compiled in, but no TCM present "
20390b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij				"in pre-v5 CPU\n", dtcm_code_sz, itcm_code_sz);
20490b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij		return;
20590b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	}
20690b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij
20790b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	tcm_status = read_cpuid_tcmstatus();
20890b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	dtcm_banks = (tcm_status >> 16) & 0x03;
20990b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij	itcm_banks = (tcm_status & 0x03);
21090b9222ec632bc8b262981768c7b16f7e67dfe58Linus Walleij
2119715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij	/* Values greater than 2 for D/ITCM banks are "reserved" */
2129715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij	if (dtcm_banks > 2)
2139715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		dtcm_banks = 0;
2149715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij	if (itcm_banks > 2)
2159715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		itcm_banks = 0;
2169715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij
217bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	/* Setup DTCM if present */
2181dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	if (dtcm_banks > 0) {
2191dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		for (i = 0; i < dtcm_banks; i++) {
2201dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij			ret = setup_tcm_bank(0, i, dtcm_banks, &dtcm_end);
2211dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij			if (ret)
2221dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij				return;
2231dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		}
2249715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		/* This means you compiled more code than fits into DTCM */
2259715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		if (dtcm_code_sz > (dtcm_end - DTCM_OFFSET)) {
2269715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij			pr_info("CPU DTCM: %u bytes of code compiled to "
2279715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij				"DTCM but only %lu bytes of DTCM present\n",
2289715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij				dtcm_code_sz, (dtcm_end - DTCM_OFFSET));
2299715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij			goto no_dtcm;
2309715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		}
2311dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		dtcm_res.end = dtcm_end - 1;
232bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		request_resource(&iomem_resource, &dtcm_res);
2331dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		dtcm_iomap[0].length = dtcm_end - DTCM_OFFSET;
234bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		iotable_init(dtcm_iomap, 1);
235bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		/* Copy data from RAM to DTCM */
236bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		start = &__sdtcm_data;
237bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		end   = &__edtcm_data;
238bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		ram   = &__dtcm_start;
2399715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		memcpy(start, ram, dtcm_code_sz);
2409715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		pr_debug("CPU DTCM: copied data from %p - %p\n",
2419715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij			 start, end);
242201043f227576d42529ddb340746a060a00f57f6Linus Walleij		dtcm_present = true;
2439715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij	} else if (dtcm_code_sz) {
2449715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		pr_info("CPU DTCM: %u bytes of code compiled to DTCM but no "
2459715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij			"DTCM banks present in CPU\n", dtcm_code_sz);
246bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	}
247bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
2489715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleijno_dtcm:
249bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	/* Setup ITCM if present */
2501dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij	if (itcm_banks > 0) {
2511dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		for (i = 0; i < itcm_banks; i++) {
2521dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij			ret = setup_tcm_bank(1, i, itcm_banks, &itcm_end);
2531dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij			if (ret)
2541dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij				return;
2551dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		}
2569715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		/* This means you compiled more code than fits into ITCM */
2579715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		if (itcm_code_sz > (itcm_end - ITCM_OFFSET)) {
2589715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij			pr_info("CPU ITCM: %u bytes of code compiled to "
2599715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij				"ITCM but only %lu bytes of ITCM present\n",
2609715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij				itcm_code_sz, (itcm_end - ITCM_OFFSET));
2619715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij			return;
2629715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		}
2631dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		itcm_res.end = itcm_end - 1;
264bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		request_resource(&iomem_resource, &itcm_res);
2651dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		itcm_iomap[0].length = itcm_end - ITCM_OFFSET;
266bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		iotable_init(itcm_iomap, 1);
267bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		/* Copy code from RAM to ITCM */
268bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		start = &__sitcm_text;
269bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		end   = &__eitcm_text;
270bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		ram   = &__itcm_start;
2719715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		memcpy(start, ram, itcm_code_sz);
2729715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		pr_debug("CPU ITCM: copied code from %p - %p\n",
2739715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij			 start, end);
274201043f227576d42529ddb340746a060a00f57f6Linus Walleij		itcm_present = true;
2759715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij	} else if (itcm_code_sz) {
2769715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij		pr_info("CPU ITCM: %u bytes of code compiled to ITCM but no "
2779715efb8dc9ffa629bf5a1215b11bf2f2f29908bLinus Walleij			"ITCM banks present in CPU\n", itcm_code_sz);
278bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	}
279bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij}
280bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
281bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij/*
282bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * This creates the TCM memory pool and has to be done later,
283bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * during the core_initicalls, since the allocator is not yet
284bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij * up and running when the first initialization runs.
285bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij */
286bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijstatic int __init setup_tcm_pool(void)
287bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij{
288bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	u32 dtcm_pool_start = (u32) &__edtcm_data;
289bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	u32 itcm_pool_start = (u32) &__eitcm_text;
290bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	int ret;
291bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
292bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	/*
293bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	 * Set up malloc pool, 2^2 = 4 bytes granularity since
294bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	 * the TCM is sometimes just 4 KiB. NB: pages and cache
295bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	 * line alignments does not matter in TCM!
296bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	 */
297bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	tcm_pool = gen_pool_create(2, -1);
298bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
299bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	pr_debug("Setting up TCM memory pool\n");
300bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
301bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	/* Add the rest of DTCM to the TCM pool */
302201043f227576d42529ddb340746a060a00f57f6Linus Walleij	if (dtcm_present) {
3031dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		if (dtcm_pool_start < dtcm_end) {
304bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			ret = gen_pool_add(tcm_pool, dtcm_pool_start,
3051dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij					   dtcm_end - dtcm_pool_start, -1);
306bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			if (ret) {
307bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				pr_err("CPU DTCM: could not add DTCM " \
308bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				       "remainder to pool!\n");
309bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				return ret;
310bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			}
311bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			pr_debug("CPU DTCM: Added %08x bytes @ %08x to " \
312bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				 "the TCM memory pool\n",
3131dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij				 dtcm_end - dtcm_pool_start,
314bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				 dtcm_pool_start);
315bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		}
316bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	}
317bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
318bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	/* Add the rest of ITCM to the TCM pool */
319201043f227576d42529ddb340746a060a00f57f6Linus Walleij	if (itcm_present) {
3201dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij		if (itcm_pool_start < itcm_end) {
321bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			ret = gen_pool_add(tcm_pool, itcm_pool_start,
3221dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij					   itcm_end - itcm_pool_start, -1);
323bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			if (ret) {
324bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				pr_err("CPU ITCM: could not add ITCM " \
325bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				       "remainder to pool!\n");
326bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				return ret;
327bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			}
328bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij			pr_debug("CPU ITCM: Added %08x bytes @ %08x to " \
329bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				 "the TCM memory pool\n",
3301dbd30e9890fd69e50b17edd70ca583546b0fe4eLinus Walleij				 itcm_end - itcm_pool_start,
331bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij				 itcm_pool_start);
332bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij		}
333bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	}
334bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij	return 0;
335bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij}
336bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleij
337bc581770cfdd8c17ea17d324dc05e2f9c599e7caLinus Walleijcore_initcall(setup_tcm_pool);
338