blob: f2935736014f9863f86096d8fd85259eca76250e [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02006 */
7
8#include <common.h>
9#include <asm/system.h>
R Sricharan08716072013-03-04 20:04:44 +000010#include <asm/cache.h>
11#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020012
Aneesh Vecee9c82011-06-16 23:30:48 +000013#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020014
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020015DECLARE_GLOBAL_DATA_PTR;
16
Jeroen Hofsteed7460772014-06-23 22:07:04 +020017__weak void arm_init_before_mmu(void)
Aneesh V3e3bc1e2011-06-16 23:30:49 +000018{
19}
Aneesh V3e3bc1e2011-06-16 23:30:49 +000020
R Sricharan06396c12013-03-04 20:04:45 +000021__weak void arm_init_domains(void)
22{
23}
24
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020025static void cp_delay (void)
26{
27 volatile int i;
28
29 /* copro seems to need some delay between reading and writing */
30 for (i = 0; i < 100; i++)
31 nop();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020032 asm volatile("" : : : "memory");
33}
34
Simon Glassa4f20792012-10-17 13:24:53 +000035void set_section_dcache(int section, enum dcache_option option)
Heiko Schocheraeb29912010-09-17 13:10:39 +020036{
Alexander Grafae6c2bc2016-03-16 15:41:21 +010037#ifdef CONFIG_ARMV7_LPAE
38 u64 *page_table = (u64 *)gd->arch.tlb_addr;
39 /* Need to set the access flag to not fault */
40 u64 value = TTB_SECT_AP | TTB_SECT_AF;
41#else
Simon Glass6b4ee152012-12-13 20:48:39 +000042 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010043 u32 value = TTB_SECT_AP;
44#endif
45
46 /* Add the page offset */
47 value |= ((u32)section << MMU_SECTION_SHIFT);
Simon Glassa4f20792012-10-17 13:24:53 +000048
Alexander Grafae6c2bc2016-03-16 15:41:21 +010049 /* Add caching bits */
Simon Glassa4f20792012-10-17 13:24:53 +000050 value |= option;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010051
52 /* Set PTE */
Simon Glassa4f20792012-10-17 13:24:53 +000053 page_table[section] = value;
54}
55
Jeroen Hofsteed7460772014-06-23 22:07:04 +020056__weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
Simon Glassa4f20792012-10-17 13:24:53 +000057{
58 debug("%s: Warning: not implemented\n", __func__);
59}
60
Thierry Redingfe2007152014-08-26 17:34:21 +020061void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
Simon Glassa4f20792012-10-17 13:24:53 +000062 enum dcache_option option)
63{
Stefan Agnerc4a73222016-08-14 21:33:00 -070064#ifdef CONFIG_ARMV7_LPAE
65 u64 *page_table = (u64 *)gd->arch.tlb_addr;
66#else
Simon Glass6b4ee152012-12-13 20:48:39 +000067 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Stefan Agnerc4a73222016-08-14 21:33:00 -070068#endif
Stefan Agnerbae14802016-08-14 21:33:01 -070069 unsigned long startpt, stoppt;
Thierry Redingfe2007152014-08-26 17:34:21 +020070 unsigned long upto, end;
Simon Glassa4f20792012-10-17 13:24:53 +000071
72 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
73 start = start >> MMU_SECTION_SHIFT;
Keerthy266c8c12016-10-29 15:19:10 +053074#ifdef CONFIG_ARMV7_LPAE
75 debug("%s: start=%pa, size=%zu, option=%llx\n", __func__, &start, size,
76 option);
77#else
Keerthy485110a2016-10-29 15:19:09 +053078 debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size,
Simon Glassa4f20792012-10-17 13:24:53 +000079 option);
Keerthy266c8c12016-10-29 15:19:10 +053080#endif
Simon Glassa4f20792012-10-17 13:24:53 +000081 for (upto = start; upto < end; upto++)
82 set_section_dcache(upto, option);
Stefan Agnerbae14802016-08-14 21:33:01 -070083
84 /*
85 * Make sure range is cache line aligned
86 * Only CPU maintains page tables, hence it is safe to always
87 * flush complete cache lines...
88 */
89
90 startpt = (unsigned long)&page_table[start];
91 startpt &= ~(CONFIG_SYS_CACHELINE_SIZE - 1);
92 stoppt = (unsigned long)&page_table[end];
93 stoppt = ALIGN(stoppt, CONFIG_SYS_CACHELINE_SIZE);
94 mmu_page_table_flush(startpt, stoppt);
Simon Glassa4f20792012-10-17 13:24:53 +000095}
96
R Sricharan08716072013-03-04 20:04:44 +000097__weak void dram_bank_mmu_setup(int bank)
Simon Glassa4f20792012-10-17 13:24:53 +000098{
Heiko Schocheraeb29912010-09-17 13:10:39 +020099 bd_t *bd = gd->bd;
100 int i;
101
102 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100103 for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT;
104 i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) +
105 (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200106 i++) {
Simon Glassa4f20792012-10-17 13:24:53 +0000107#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
108 set_section_dcache(i, DCACHE_WRITETHROUGH);
Marek Vasut79b90722014-09-15 02:44:36 +0200109#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
110 set_section_dcache(i, DCACHE_WRITEALLOC);
Simon Glassa4f20792012-10-17 13:24:53 +0000111#else
112 set_section_dcache(i, DCACHE_WRITEBACK);
113#endif
Heiko Schocheraeb29912010-09-17 13:10:39 +0200114 }
115}
Heiko Schocheraeb29912010-09-17 13:10:39 +0200116
117/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200118static inline void mmu_setup(void)
119{
Heiko Schocheraeb29912010-09-17 13:10:39 +0200120 int i;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200121 u32 reg;
122
Aneesh V3e3bc1e2011-06-16 23:30:49 +0000123 arm_init_before_mmu();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200124 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100125 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glassa4f20792012-10-17 13:24:53 +0000126 set_section_dcache(i, DCACHE_OFF);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200127
Heiko Schocheraeb29912010-09-17 13:10:39 +0200128 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
129 dram_bank_mmu_setup(i);
130 }
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200131
Simon Glass5bfd41d2017-05-31 17:57:13 -0600132#if defined(CONFIG_ARMV7_LPAE) && __LINUX_ARM_ARCH__ != 4
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100133 /* Set up 4 PTE entries pointing to our 4 1GB page tables */
134 for (i = 0; i < 4; i++) {
135 u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4));
136 u64 tpt = gd->arch.tlb_addr + (4096 * i);
137 page_table[i] = tpt | TTB_PAGETABLE;
138 }
139
140 reg = TTBCR_EAE;
141#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
142 reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT;
143#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
144 reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA;
145#else
146 reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA;
147#endif
148
149 if (is_hyp()) {
Simon Glass3b372472017-05-31 17:57:12 -0600150 /* Set HTCR to enable LPAE */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100151 asm volatile("mcr p15, 4, %0, c2, c0, 2"
152 : : "r" (reg) : "memory");
153 /* Set HTTBR0 */
154 asm volatile("mcrr p15, 4, %0, %1, c2"
155 :
156 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
157 : "memory");
158 /* Set HMAIR */
159 asm volatile("mcr p15, 4, %0, c10, c2, 0"
160 : : "r" (MEMORY_ATTRIBUTES) : "memory");
161 } else {
162 /* Set TTBCR to enable LPAE */
163 asm volatile("mcr p15, 0, %0, c2, c0, 2"
164 : : "r" (reg) : "memory");
165 /* Set 64-bit TTBR0 */
166 asm volatile("mcrr p15, 0, %0, %1, c2"
167 :
168 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
169 : "memory");
170 /* Set MAIR */
171 asm volatile("mcr p15, 0, %0, c10, c2, 0"
172 : : "r" (MEMORY_ATTRIBUTES) : "memory");
173 }
174#elif defined(CONFIG_CPU_V7)
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500175 /* Set TTBR0 */
176 reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
177#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
178 reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
179#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
180 reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
181#else
182 reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
183#endif
184 asm volatile("mcr p15, 0, %0, c2, c0, 0"
185 : : "r" (reg) : "memory");
186#else
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200187 /* Copy the page table address to cp15 */
188 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass6b4ee152012-12-13 20:48:39 +0000189 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500190#endif
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200191 /* Set the access control to all-supervisor */
192 asm volatile("mcr p15, 0, %0, c3, c0, 0"
193 : : "r" (~0));
R Sricharan06396c12013-03-04 20:04:45 +0000194
195 arm_init_domains();
196
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200197 /* and enable the mmu */
198 reg = get_cr(); /* get control reg. */
199 cp_delay();
200 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200201}
202
Aneesh V3bda3772011-06-16 23:30:50 +0000203static int mmu_enabled(void)
204{
205 return get_cr() & CR_M;
206}
207
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200208/* cache_bit must be either CR_I or CR_C */
209static void cache_enable(uint32_t cache_bit)
210{
211 uint32_t reg;
212
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200213 /* The data cache is not active unless the mmu is enabled too */
Aneesh V3bda3772011-06-16 23:30:50 +0000214 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200215 mmu_setup();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200216 reg = get_cr(); /* get control reg. */
217 cp_delay();
218 set_cr(reg | cache_bit);
219}
220
221/* cache_bit must be either CR_I or CR_C */
222static void cache_disable(uint32_t cache_bit)
223{
224 uint32_t reg;
225
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000226 reg = get_cr();
227 cp_delay();
228
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200229 if (cache_bit == CR_C) {
Heiko Schocheraeb29912010-09-17 13:10:39 +0200230 /* if cache isn;t enabled no need to disable */
Heiko Schocheraeb29912010-09-17 13:10:39 +0200231 if ((reg & CR_C) != CR_C)
232 return;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200233 /* if disabling data cache, disable mmu too */
234 cache_bit |= CR_M;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200235 }
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000236 reg = get_cr();
237 cp_delay();
238 if (cache_bit == (CR_C | CR_M))
239 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200240 set_cr(reg & ~cache_bit);
241}
242#endif
243
Aneesh Vecee9c82011-06-16 23:30:48 +0000244#ifdef CONFIG_SYS_ICACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200245void icache_enable (void)
246{
247 return;
248}
249
250void icache_disable (void)
251{
252 return;
253}
254
255int icache_status (void)
256{
257 return 0; /* always off */
258}
259#else
260void icache_enable(void)
261{
262 cache_enable(CR_I);
263}
264
265void icache_disable(void)
266{
267 cache_disable(CR_I);
268}
269
270int icache_status(void)
271{
272 return (get_cr() & CR_I) != 0;
273}
274#endif
275
Aneesh Vecee9c82011-06-16 23:30:48 +0000276#ifdef CONFIG_SYS_DCACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200277void dcache_enable (void)
278{
279 return;
280}
281
282void dcache_disable (void)
283{
284 return;
285}
286
287int dcache_status (void)
288{
289 return 0; /* always off */
290}
291#else
292void dcache_enable(void)
293{
294 cache_enable(CR_C);
295}
296
297void dcache_disable(void)
298{
299 cache_disable(CR_C);
300}
301
302int dcache_status(void)
303{
304 return (get_cr() & CR_C) != 0;
305}
306#endif