blob: 8ca8e483803418ec995c931295230bad95a9fa29 [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>
8#include <asm/system.h>
R Sricharan96fdbec2013-03-04 20:04:44 +00009#include <asm/cache.h>
10#include <linux/compiler.h>
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +053011#include <asm/armv7_mpu.h>
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020012
Trevor Woerner10015022019-05-03 09:41:00 -040013#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Heiko Schocher880eff52010-09-17 13:10:29 +020014
Heiko Schocher880eff52010-09-17 13:10:29 +020015DECLARE_GLOBAL_DATA_PTR;
16
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +053017#ifdef CONFIG_SYS_ARM_MMU
Jeroen Hofsteefcfddfd2014-06-23 22:07:04 +020018__weak void arm_init_before_mmu(void)
Aneesh Vc2dd0d42011-06-16 23:30:49 +000019{
20}
Aneesh Vc2dd0d42011-06-16 23:30:49 +000021
R Sricharande63ac22013-03-04 20:04:45 +000022__weak void arm_init_domains(void)
23{
24}
25
Simon Glass0dde7f52012-10-17 13:24:53 +000026void set_section_dcache(int section, enum dcache_option option)
Heiko Schocherf1d2b312010-09-17 13:10:39 +020027{
Alexander Grafd990f5c2016-03-16 15:41:21 +010028#ifdef CONFIG_ARMV7_LPAE
29 u64 *page_table = (u64 *)gd->arch.tlb_addr;
30 /* Need to set the access flag to not fault */
31 u64 value = TTB_SECT_AP | TTB_SECT_AF;
32#else
Simon Glass34fd5d22012-12-13 20:48:39 +000033 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafd990f5c2016-03-16 15:41:21 +010034 u32 value = TTB_SECT_AP;
35#endif
Simon Glass0dde7f52012-10-17 13:24:53 +000036
Alexander Grafd990f5c2016-03-16 15:41:21 +010037 /* Add the page offset */
38 value |= ((u32)section << MMU_SECTION_SHIFT);
39
40 /* Add caching bits */
Simon Glass0dde7f52012-10-17 13:24:53 +000041 value |= option;
Alexander Grafd990f5c2016-03-16 15:41:21 +010042
43 /* Set PTE */
Simon Glass0dde7f52012-10-17 13:24:53 +000044 page_table[section] = value;
45}
46
Jeroen Hofsteefcfddfd2014-06-23 22:07:04 +020047__weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
Simon Glass0dde7f52012-10-17 13:24:53 +000048{
49 debug("%s: Warning: not implemented\n", __func__);
50}
51
Thierry Reding25026fa2014-08-26 17:34:21 +020052void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
Simon Glass0dde7f52012-10-17 13:24:53 +000053 enum dcache_option option)
54{
Stefan Agnerc5b3cab2016-08-14 21:33:00 -070055#ifdef CONFIG_ARMV7_LPAE
56 u64 *page_table = (u64 *)gd->arch.tlb_addr;
57#else
Simon Glass34fd5d22012-12-13 20:48:39 +000058 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Stefan Agnerc5b3cab2016-08-14 21:33:00 -070059#endif
Stefan Agner8f894a42016-08-14 21:33:01 -070060 unsigned long startpt, stoppt;
Thierry Reding25026fa2014-08-26 17:34:21 +020061 unsigned long upto, end;
Simon Glass0dde7f52012-10-17 13:24:53 +000062
63 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
64 start = start >> MMU_SECTION_SHIFT;
Keerthy06d43c82016-10-29 15:19:10 +053065#ifdef CONFIG_ARMV7_LPAE
66 debug("%s: start=%pa, size=%zu, option=%llx\n", __func__, &start, size,
67 option);
68#else
Keerthy2b373cb2016-10-29 15:19:09 +053069 debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size,
Simon Glass0dde7f52012-10-17 13:24:53 +000070 option);
Keerthy06d43c82016-10-29 15:19:10 +053071#endif
Simon Glass0dde7f52012-10-17 13:24:53 +000072 for (upto = start; upto < end; upto++)
73 set_section_dcache(upto, option);
Stefan Agner8f894a42016-08-14 21:33:01 -070074
75 /*
76 * Make sure range is cache line aligned
77 * Only CPU maintains page tables, hence it is safe to always
78 * flush complete cache lines...
79 */
80
81 startpt = (unsigned long)&page_table[start];
82 startpt &= ~(CONFIG_SYS_CACHELINE_SIZE - 1);
83 stoppt = (unsigned long)&page_table[end];
84 stoppt = ALIGN(stoppt, CONFIG_SYS_CACHELINE_SIZE);
85 mmu_page_table_flush(startpt, stoppt);
Simon Glass0dde7f52012-10-17 13:24:53 +000086}
87
R Sricharan96fdbec2013-03-04 20:04:44 +000088__weak void dram_bank_mmu_setup(int bank)
Simon Glass0dde7f52012-10-17 13:24:53 +000089{
Heiko Schocherf1d2b312010-09-17 13:10:39 +020090 bd_t *bd = gd->bd;
91 int i;
92
93 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafd990f5c2016-03-16 15:41:21 +010094 for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT;
95 i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) +
96 (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT);
Heiko Schocherf1d2b312010-09-17 13:10:39 +020097 i++) {
Simon Glass0dde7f52012-10-17 13:24:53 +000098#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
99 set_section_dcache(i, DCACHE_WRITETHROUGH);
Marek Vasutff7e9702014-09-15 02:44:36 +0200100#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
101 set_section_dcache(i, DCACHE_WRITEALLOC);
Simon Glass0dde7f52012-10-17 13:24:53 +0000102#else
103 set_section_dcache(i, DCACHE_WRITEBACK);
104#endif
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200105 }
106}
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200107
108/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher880eff52010-09-17 13:10:29 +0200109static inline void mmu_setup(void)
110{
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200111 int i;
Heiko Schocher880eff52010-09-17 13:10:29 +0200112 u32 reg;
113
Aneesh Vc2dd0d42011-06-16 23:30:49 +0000114 arm_init_before_mmu();
Heiko Schocher880eff52010-09-17 13:10:29 +0200115 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafd990f5c2016-03-16 15:41:21 +0100116 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glass0dde7f52012-10-17 13:24:53 +0000117 set_section_dcache(i, DCACHE_OFF);
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200118
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200119 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
120 dram_bank_mmu_setup(i);
121 }
Heiko Schocher880eff52010-09-17 13:10:29 +0200122
Simon Glass10d602a2017-05-31 17:57:13 -0600123#if defined(CONFIG_ARMV7_LPAE) && __LINUX_ARM_ARCH__ != 4
Alexander Grafd990f5c2016-03-16 15:41:21 +0100124 /* Set up 4 PTE entries pointing to our 4 1GB page tables */
125 for (i = 0; i < 4; i++) {
126 u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4));
127 u64 tpt = gd->arch.tlb_addr + (4096 * i);
128 page_table[i] = tpt | TTB_PAGETABLE;
129 }
130
131 reg = TTBCR_EAE;
132#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
133 reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT;
134#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
135 reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA;
136#else
137 reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA;
138#endif
139
140 if (is_hyp()) {
Simon Glass579dfca2017-05-31 17:57:12 -0600141 /* Set HTCR to enable LPAE */
Alexander Grafd990f5c2016-03-16 15:41:21 +0100142 asm volatile("mcr p15, 4, %0, c2, c0, 2"
143 : : "r" (reg) : "memory");
144 /* Set HTTBR0 */
145 asm volatile("mcrr p15, 4, %0, %1, c2"
146 :
147 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
148 : "memory");
149 /* Set HMAIR */
150 asm volatile("mcr p15, 4, %0, c10, c2, 0"
151 : : "r" (MEMORY_ATTRIBUTES) : "memory");
152 } else {
153 /* Set TTBCR to enable LPAE */
154 asm volatile("mcr p15, 0, %0, c2, c0, 2"
155 : : "r" (reg) : "memory");
156 /* Set 64-bit TTBR0 */
157 asm volatile("mcrr p15, 0, %0, %1, c2"
158 :
159 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
160 : "memory");
161 /* Set MAIR */
162 asm volatile("mcr p15, 0, %0, c10, c2, 0"
163 : : "r" (MEMORY_ATTRIBUTES) : "memory");
164 }
Lokesh Vutlaacf15002018-04-26 18:21:26 +0530165#elif defined(CONFIG_CPU_V7A)
Simon Glass50a48862017-05-31 17:57:14 -0600166 if (is_hyp()) {
167 /* Set HTCR to disable LPAE */
168 asm volatile("mcr p15, 4, %0, c2, c0, 2"
169 : : "r" (0) : "memory");
170 } else {
171 /* Set TTBCR to disable LPAE */
172 asm volatile("mcr p15, 0, %0, c2, c0, 2"
173 : : "r" (0) : "memory");
174 }
Bryan Brinsko97840b52015-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 Schocher880eff52010-09-17 13:10:29 +0200187 /* Copy the page table address to cp15 */
188 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass34fd5d22012-12-13 20:48:39 +0000189 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko97840b52015-03-24 11:25:12 -0500190#endif
Heiko Schocher880eff52010-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 Sricharande63ac22013-03-04 20:04:45 +0000194
195 arm_init_domains();
196
Heiko Schocher880eff52010-09-17 13:10:29 +0200197 /* and enable the mmu */
198 reg = get_cr(); /* get control reg. */
Heiko Schocher880eff52010-09-17 13:10:29 +0200199 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200200}
201
Aneesh Ve05f0072011-06-16 23:30:50 +0000202static int mmu_enabled(void)
203{
204 return get_cr() & CR_M;
205}
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +0530206#endif /* CONFIG_SYS_ARM_MMU */
Aneesh Ve05f0072011-06-16 23:30:50 +0000207
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-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
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +0530213 /* The data cache is not active unless the mmu/mpu is enabled too */
214#ifdef CONFIG_SYS_ARM_MMU
Aneesh Ve05f0072011-06-16 23:30:50 +0000215 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher880eff52010-09-17 13:10:29 +0200216 mmu_setup();
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +0530217#elif defined(CONFIG_SYS_ARM_MPU)
218 if ((cache_bit == CR_C) && !mpu_enabled()) {
219 printf("Consider enabling MPU before enabling caches\n");
220 return;
221 }
222#endif
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200223 reg = get_cr(); /* get control reg. */
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200224 set_cr(reg | cache_bit);
225}
226
227/* cache_bit must be either CR_I or CR_C */
228static void cache_disable(uint32_t cache_bit)
229{
230 uint32_t reg;
231
SRICHARAN Rd702b082012-05-16 23:52:54 +0000232 reg = get_cr();
SRICHARAN Rd702b082012-05-16 23:52:54 +0000233
Heiko Schocher880eff52010-09-17 13:10:29 +0200234 if (cache_bit == CR_C) {
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200235 /* if cache isn;t enabled no need to disable */
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200236 if ((reg & CR_C) != CR_C)
237 return;
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530238#ifdef CONFIG_SYS_ARM_MMU
Heiko Schocher880eff52010-09-17 13:10:29 +0200239 /* if disabling data cache, disable mmu too */
240 cache_bit |= CR_M;
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530241#endif
Heiko Schocher880eff52010-09-17 13:10:29 +0200242 }
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000243 reg = get_cr();
Lothar Waßmann53d4ed72017-06-08 09:48:41 +0200244
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530245#ifdef CONFIG_SYS_ARM_MMU
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000246 if (cache_bit == (CR_C | CR_M))
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530247#elif defined(CONFIG_SYS_ARM_MPU)
248 if (cache_bit == CR_C)
249#endif
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000250 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200251 set_cr(reg & ~cache_bit);
252}
253#endif
254
Trevor Woerner10015022019-05-03 09:41:00 -0400255#if CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Simon Glass6cc915b2019-11-14 12:57:36 -0700256void icache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200257{
258 return;
259}
260
Simon Glass6cc915b2019-11-14 12:57:36 -0700261void icache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200262{
263 return;
264}
265
Simon Glass6cc915b2019-11-14 12:57:36 -0700266int icache_status(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200267{
268 return 0; /* always off */
269}
270#else
271void icache_enable(void)
272{
273 cache_enable(CR_I);
274}
275
276void icache_disable(void)
277{
278 cache_disable(CR_I);
279}
280
281int icache_status(void)
282{
283 return (get_cr() & CR_I) != 0;
284}
285#endif
286
Trevor Woerner10015022019-05-03 09:41:00 -0400287#if CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass6cc915b2019-11-14 12:57:36 -0700288void dcache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200289{
290 return;
291}
292
Simon Glass6cc915b2019-11-14 12:57:36 -0700293void dcache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200294{
295 return;
296}
297
Simon Glass6cc915b2019-11-14 12:57:36 -0700298int dcache_status(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200299{
300 return 0; /* always off */
301}
302#else
303void dcache_enable(void)
304{
305 cache_enable(CR_C);
306}
307
308void dcache_disable(void)
309{
310 cache_disable(CR_C);
311}
312
313int dcache_status(void)
314{
315 return (get_cr() & CR_C) != 0;
316}
317#endif