blob: 54509f11c304389963eb50caed77193881b7ba79 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +02002/*
3 * (C) Copyright 2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +02005 */
6
7#include <common.h>
Simon Glass9edefc22019-11-14 12:57:37 -07008#include <cpu_func.h>
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +02009#include <asm/system.h>
R Sricharan96fdbec2013-03-04 20:04:44 +000010#include <asm/cache.h>
11#include <linux/compiler.h>
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +053012#include <asm/armv7_mpu.h>
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020013
Trevor Woerner10015022019-05-03 09:41:00 -040014#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Heiko Schocher880eff52010-09-17 13:10:29 +020015
Heiko Schocher880eff52010-09-17 13:10:29 +020016DECLARE_GLOBAL_DATA_PTR;
17
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +053018#ifdef CONFIG_SYS_ARM_MMU
Jeroen Hofsteefcfddfd2014-06-23 22:07:04 +020019__weak void arm_init_before_mmu(void)
Aneesh Vc2dd0d42011-06-16 23:30:49 +000020{
21}
Aneesh Vc2dd0d42011-06-16 23:30:49 +000022
R Sricharande63ac22013-03-04 20:04:45 +000023__weak void arm_init_domains(void)
24{
25}
26
Simon Glass0dde7f52012-10-17 13:24:53 +000027void set_section_dcache(int section, enum dcache_option option)
Heiko Schocherf1d2b312010-09-17 13:10:39 +020028{
Alexander Grafd990f5c2016-03-16 15:41:21 +010029#ifdef CONFIG_ARMV7_LPAE
30 u64 *page_table = (u64 *)gd->arch.tlb_addr;
31 /* Need to set the access flag to not fault */
32 u64 value = TTB_SECT_AP | TTB_SECT_AF;
33#else
Simon Glass34fd5d22012-12-13 20:48:39 +000034 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafd990f5c2016-03-16 15:41:21 +010035 u32 value = TTB_SECT_AP;
36#endif
Simon Glass0dde7f52012-10-17 13:24:53 +000037
Alexander Grafd990f5c2016-03-16 15:41:21 +010038 /* Add the page offset */
39 value |= ((u32)section << MMU_SECTION_SHIFT);
40
41 /* Add caching bits */
Simon Glass0dde7f52012-10-17 13:24:53 +000042 value |= option;
Alexander Grafd990f5c2016-03-16 15:41:21 +010043
44 /* Set PTE */
Simon Glass0dde7f52012-10-17 13:24:53 +000045 page_table[section] = value;
46}
47
Jeroen Hofsteefcfddfd2014-06-23 22:07:04 +020048__weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
Simon Glass0dde7f52012-10-17 13:24:53 +000049{
50 debug("%s: Warning: not implemented\n", __func__);
51}
52
Thierry Reding25026fa2014-08-26 17:34:21 +020053void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
Simon Glass0dde7f52012-10-17 13:24:53 +000054 enum dcache_option option)
55{
Stefan Agnerc5b3cab2016-08-14 21:33:00 -070056#ifdef CONFIG_ARMV7_LPAE
57 u64 *page_table = (u64 *)gd->arch.tlb_addr;
58#else
Simon Glass34fd5d22012-12-13 20:48:39 +000059 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Stefan Agnerc5b3cab2016-08-14 21:33:00 -070060#endif
Stefan Agner8f894a42016-08-14 21:33:01 -070061 unsigned long startpt, stoppt;
Thierry Reding25026fa2014-08-26 17:34:21 +020062 unsigned long upto, end;
Simon Glass0dde7f52012-10-17 13:24:53 +000063
64 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
65 start = start >> MMU_SECTION_SHIFT;
Keerthy06d43c82016-10-29 15:19:10 +053066#ifdef CONFIG_ARMV7_LPAE
67 debug("%s: start=%pa, size=%zu, option=%llx\n", __func__, &start, size,
68 option);
69#else
Keerthy2b373cb2016-10-29 15:19:09 +053070 debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size,
Simon Glass0dde7f52012-10-17 13:24:53 +000071 option);
Keerthy06d43c82016-10-29 15:19:10 +053072#endif
Simon Glass0dde7f52012-10-17 13:24:53 +000073 for (upto = start; upto < end; upto++)
74 set_section_dcache(upto, option);
Stefan Agner8f894a42016-08-14 21:33:01 -070075
76 /*
77 * Make sure range is cache line aligned
78 * Only CPU maintains page tables, hence it is safe to always
79 * flush complete cache lines...
80 */
81
82 startpt = (unsigned long)&page_table[start];
83 startpt &= ~(CONFIG_SYS_CACHELINE_SIZE - 1);
84 stoppt = (unsigned long)&page_table[end];
85 stoppt = ALIGN(stoppt, CONFIG_SYS_CACHELINE_SIZE);
86 mmu_page_table_flush(startpt, stoppt);
Simon Glass0dde7f52012-10-17 13:24:53 +000087}
88
R Sricharan96fdbec2013-03-04 20:04:44 +000089__weak void dram_bank_mmu_setup(int bank)
Simon Glass0dde7f52012-10-17 13:24:53 +000090{
Heiko Schocherf1d2b312010-09-17 13:10:39 +020091 bd_t *bd = gd->bd;
92 int i;
93
Patrick Delaunayc8ec1e32020-04-24 20:20:15 +020094 /* bd->bi_dram is available only after relocation */
95 if ((gd->flags & GD_FLG_RELOC) == 0)
96 return;
97
Heiko Schocherf1d2b312010-09-17 13:10:39 +020098 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafd990f5c2016-03-16 15:41:21 +010099 for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT;
100 i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) +
101 (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT);
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200102 i++) {
Simon Glass0dde7f52012-10-17 13:24:53 +0000103#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
104 set_section_dcache(i, DCACHE_WRITETHROUGH);
Marek Vasutff7e9702014-09-15 02:44:36 +0200105#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
106 set_section_dcache(i, DCACHE_WRITEALLOC);
Simon Glass0dde7f52012-10-17 13:24:53 +0000107#else
108 set_section_dcache(i, DCACHE_WRITEBACK);
109#endif
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200110 }
111}
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200112
113/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher880eff52010-09-17 13:10:29 +0200114static inline void mmu_setup(void)
115{
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200116 int i;
Heiko Schocher880eff52010-09-17 13:10:29 +0200117 u32 reg;
118
Aneesh Vc2dd0d42011-06-16 23:30:49 +0000119 arm_init_before_mmu();
Heiko Schocher880eff52010-09-17 13:10:29 +0200120 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafd990f5c2016-03-16 15:41:21 +0100121 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glass0dde7f52012-10-17 13:24:53 +0000122 set_section_dcache(i, DCACHE_OFF);
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200123
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200124 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
125 dram_bank_mmu_setup(i);
126 }
Heiko Schocher880eff52010-09-17 13:10:29 +0200127
Simon Glass10d602a2017-05-31 17:57:13 -0600128#if defined(CONFIG_ARMV7_LPAE) && __LINUX_ARM_ARCH__ != 4
Alexander Grafd990f5c2016-03-16 15:41:21 +0100129 /* Set up 4 PTE entries pointing to our 4 1GB page tables */
130 for (i = 0; i < 4; i++) {
131 u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4));
132 u64 tpt = gd->arch.tlb_addr + (4096 * i);
133 page_table[i] = tpt | TTB_PAGETABLE;
134 }
135
136 reg = TTBCR_EAE;
137#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
138 reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT;
139#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
140 reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA;
141#else
142 reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA;
143#endif
144
145 if (is_hyp()) {
Simon Glass579dfca2017-05-31 17:57:12 -0600146 /* Set HTCR to enable LPAE */
Alexander Grafd990f5c2016-03-16 15:41:21 +0100147 asm volatile("mcr p15, 4, %0, c2, c0, 2"
148 : : "r" (reg) : "memory");
149 /* Set HTTBR0 */
150 asm volatile("mcrr p15, 4, %0, %1, c2"
151 :
152 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
153 : "memory");
154 /* Set HMAIR */
155 asm volatile("mcr p15, 4, %0, c10, c2, 0"
156 : : "r" (MEMORY_ATTRIBUTES) : "memory");
157 } else {
158 /* Set TTBCR to enable LPAE */
159 asm volatile("mcr p15, 0, %0, c2, c0, 2"
160 : : "r" (reg) : "memory");
161 /* Set 64-bit TTBR0 */
162 asm volatile("mcrr p15, 0, %0, %1, c2"
163 :
164 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
165 : "memory");
166 /* Set MAIR */
167 asm volatile("mcr p15, 0, %0, c10, c2, 0"
168 : : "r" (MEMORY_ATTRIBUTES) : "memory");
169 }
Lokesh Vutlaacf15002018-04-26 18:21:26 +0530170#elif defined(CONFIG_CPU_V7A)
Simon Glass50a48862017-05-31 17:57:14 -0600171 if (is_hyp()) {
172 /* Set HTCR to disable LPAE */
173 asm volatile("mcr p15, 4, %0, c2, c0, 2"
174 : : "r" (0) : "memory");
175 } else {
176 /* Set TTBCR to disable LPAE */
177 asm volatile("mcr p15, 0, %0, c2, c0, 2"
178 : : "r" (0) : "memory");
179 }
Bryan Brinsko97840b52015-03-24 11:25:12 -0500180 /* Set TTBR0 */
181 reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
182#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
183 reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
184#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
185 reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
186#else
187 reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
188#endif
189 asm volatile("mcr p15, 0, %0, c2, c0, 0"
190 : : "r" (reg) : "memory");
191#else
Heiko Schocher880eff52010-09-17 13:10:29 +0200192 /* Copy the page table address to cp15 */
193 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass34fd5d22012-12-13 20:48:39 +0000194 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko97840b52015-03-24 11:25:12 -0500195#endif
Heiko Schocher880eff52010-09-17 13:10:29 +0200196 /* Set the access control to all-supervisor */
197 asm volatile("mcr p15, 0, %0, c3, c0, 0"
198 : : "r" (~0));
R Sricharande63ac22013-03-04 20:04:45 +0000199
200 arm_init_domains();
201
Heiko Schocher880eff52010-09-17 13:10:29 +0200202 /* and enable the mmu */
203 reg = get_cr(); /* get control reg. */
Heiko Schocher880eff52010-09-17 13:10:29 +0200204 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200205}
206
Aneesh Ve05f0072011-06-16 23:30:50 +0000207static int mmu_enabled(void)
208{
209 return get_cr() & CR_M;
210}
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +0530211#endif /* CONFIG_SYS_ARM_MMU */
Aneesh Ve05f0072011-06-16 23:30:50 +0000212
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200213/* cache_bit must be either CR_I or CR_C */
214static void cache_enable(uint32_t cache_bit)
215{
216 uint32_t reg;
217
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +0530218 /* The data cache is not active unless the mmu/mpu is enabled too */
219#ifdef CONFIG_SYS_ARM_MMU
Aneesh Ve05f0072011-06-16 23:30:50 +0000220 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher880eff52010-09-17 13:10:29 +0200221 mmu_setup();
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +0530222#elif defined(CONFIG_SYS_ARM_MPU)
223 if ((cache_bit == CR_C) && !mpu_enabled()) {
224 printf("Consider enabling MPU before enabling caches\n");
225 return;
226 }
227#endif
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200228 reg = get_cr(); /* get control reg. */
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200229 set_cr(reg | cache_bit);
230}
231
232/* cache_bit must be either CR_I or CR_C */
233static void cache_disable(uint32_t cache_bit)
234{
235 uint32_t reg;
236
SRICHARAN Rd702b082012-05-16 23:52:54 +0000237 reg = get_cr();
SRICHARAN Rd702b082012-05-16 23:52:54 +0000238
Heiko Schocher880eff52010-09-17 13:10:29 +0200239 if (cache_bit == CR_C) {
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200240 /* if cache isn;t enabled no need to disable */
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200241 if ((reg & CR_C) != CR_C)
242 return;
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530243#ifdef CONFIG_SYS_ARM_MMU
Heiko Schocher880eff52010-09-17 13:10:29 +0200244 /* if disabling data cache, disable mmu too */
245 cache_bit |= CR_M;
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530246#endif
Heiko Schocher880eff52010-09-17 13:10:29 +0200247 }
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000248 reg = get_cr();
Lothar Waßmann53d4ed72017-06-08 09:48:41 +0200249
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530250#ifdef CONFIG_SYS_ARM_MMU
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000251 if (cache_bit == (CR_C | CR_M))
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530252#elif defined(CONFIG_SYS_ARM_MPU)
253 if (cache_bit == CR_C)
254#endif
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000255 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200256 set_cr(reg & ~cache_bit);
257}
258#endif
259
Trevor Woerner10015022019-05-03 09:41:00 -0400260#if CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Simon Glass6cc915b2019-11-14 12:57:36 -0700261void icache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200262{
263 return;
264}
265
Simon Glass6cc915b2019-11-14 12:57:36 -0700266void icache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200267{
268 return;
269}
270
Simon Glass6cc915b2019-11-14 12:57:36 -0700271int icache_status(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200272{
273 return 0; /* always off */
274}
275#else
276void icache_enable(void)
277{
278 cache_enable(CR_I);
279}
280
281void icache_disable(void)
282{
283 cache_disable(CR_I);
284}
285
286int icache_status(void)
287{
288 return (get_cr() & CR_I) != 0;
289}
290#endif
291
Trevor Woerner10015022019-05-03 09:41:00 -0400292#if CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass6cc915b2019-11-14 12:57:36 -0700293void dcache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200294{
295 return;
296}
297
Simon Glass6cc915b2019-11-14 12:57:36 -0700298void dcache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200299{
300 return;
301}
302
Simon Glass6cc915b2019-11-14 12:57:36 -0700303int dcache_status(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200304{
305 return 0; /* always off */
306}
307#else
308void dcache_enable(void)
309{
310 cache_enable(CR_C);
311}
312
313void dcache_disable(void)
314{
315 cache_disable(CR_C);
316}
317
318int dcache_status(void)
319{
320 return (get_cr() & CR_C) != 0;
321}
322#endif