blob: 4854c39a354af79a690c4a55481c7309909c5a10 [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>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020011#include <asm/system.h>
R Sricharan96fdbec2013-03-04 20:04:44 +000012#include <asm/cache.h>
13#include <linux/compiler.h>
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +053014#include <asm/armv7_mpu.h>
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020015
Trevor Woerner10015022019-05-03 09:41:00 -040016#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Heiko Schocher880eff52010-09-17 13:10:29 +020017
Heiko Schocher880eff52010-09-17 13:10:29 +020018DECLARE_GLOBAL_DATA_PTR;
19
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +053020#ifdef CONFIG_SYS_ARM_MMU
Jeroen Hofsteefcfddfd2014-06-23 22:07:04 +020021__weak void arm_init_before_mmu(void)
Aneesh Vc2dd0d42011-06-16 23:30:49 +000022{
23}
Aneesh Vc2dd0d42011-06-16 23:30:49 +000024
R Sricharande63ac22013-03-04 20:04:45 +000025__weak void arm_init_domains(void)
26{
27}
28
Marek Szyprowskid877f8f2020-06-03 14:43:42 +020029static void set_section_phys(int section, phys_addr_t phys,
30 enum dcache_option option)
Heiko Schocherf1d2b312010-09-17 13:10:39 +020031{
Alexander Grafd990f5c2016-03-16 15:41:21 +010032#ifdef CONFIG_ARMV7_LPAE
33 u64 *page_table = (u64 *)gd->arch.tlb_addr;
34 /* Need to set the access flag to not fault */
35 u64 value = TTB_SECT_AP | TTB_SECT_AF;
36#else
Simon Glass34fd5d22012-12-13 20:48:39 +000037 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafd990f5c2016-03-16 15:41:21 +010038 u32 value = TTB_SECT_AP;
39#endif
Simon Glass0dde7f52012-10-17 13:24:53 +000040
Alexander Grafd990f5c2016-03-16 15:41:21 +010041 /* Add the page offset */
Marek Szyprowskid877f8f2020-06-03 14:43:42 +020042 value |= phys;
Alexander Grafd990f5c2016-03-16 15:41:21 +010043
44 /* Add caching bits */
Simon Glass0dde7f52012-10-17 13:24:53 +000045 value |= option;
Alexander Grafd990f5c2016-03-16 15:41:21 +010046
47 /* Set PTE */
Simon Glass0dde7f52012-10-17 13:24:53 +000048 page_table[section] = value;
49}
50
Marek Szyprowskid877f8f2020-06-03 14:43:42 +020051void set_section_dcache(int section, enum dcache_option option)
52{
53 set_section_phys(section, (u32)section << MMU_SECTION_SHIFT, option);
54}
55
Jeroen Hofsteefcfddfd2014-06-23 22:07:04 +020056__weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
Simon Glass0dde7f52012-10-17 13:24:53 +000057{
58 debug("%s: Warning: not implemented\n", __func__);
59}
60
Marek Szyprowskid877f8f2020-06-03 14:43:42 +020061void mmu_set_region_dcache_behaviour_phys(phys_addr_t start, phys_addr_t phys,
62 size_t size, enum dcache_option option)
Simon Glass0dde7f52012-10-17 13:24:53 +000063{
Stefan Agnerc5b3cab2016-08-14 21:33:00 -070064#ifdef CONFIG_ARMV7_LPAE
65 u64 *page_table = (u64 *)gd->arch.tlb_addr;
66#else
Simon Glass34fd5d22012-12-13 20:48:39 +000067 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Stefan Agnerc5b3cab2016-08-14 21:33:00 -070068#endif
Stefan Agner8f894a42016-08-14 21:33:01 -070069 unsigned long startpt, stoppt;
Thierry Reding25026fa2014-08-26 17:34:21 +020070 unsigned long upto, end;
Simon Glass0dde7f52012-10-17 13:24:53 +000071
Patrick Delaunay54be09c2020-04-24 20:20:17 +020072 /* div by 2 before start + size to avoid phys_addr_t overflow */
73 end = ALIGN((start / 2) + (size / 2), MMU_SECTION_SIZE / 2)
74 >> (MMU_SECTION_SHIFT - 1);
Simon Glass0dde7f52012-10-17 13:24:53 +000075 start = start >> MMU_SECTION_SHIFT;
Patrick Delaunay54be09c2020-04-24 20:20:17 +020076
Keerthy06d43c82016-10-29 15:19:10 +053077#ifdef CONFIG_ARMV7_LPAE
78 debug("%s: start=%pa, size=%zu, option=%llx\n", __func__, &start, size,
79 option);
80#else
Keerthy2b373cb2016-10-29 15:19:09 +053081 debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size,
Simon Glass0dde7f52012-10-17 13:24:53 +000082 option);
Keerthy06d43c82016-10-29 15:19:10 +053083#endif
Marek Szyprowskid877f8f2020-06-03 14:43:42 +020084 for (upto = start; upto < end; upto++, phys += MMU_SECTION_SIZE)
85 set_section_phys(upto, phys, option);
Stefan Agner8f894a42016-08-14 21:33:01 -070086
87 /*
88 * Make sure range is cache line aligned
89 * Only CPU maintains page tables, hence it is safe to always
90 * flush complete cache lines...
91 */
92
93 startpt = (unsigned long)&page_table[start];
94 startpt &= ~(CONFIG_SYS_CACHELINE_SIZE - 1);
95 stoppt = (unsigned long)&page_table[end];
96 stoppt = ALIGN(stoppt, CONFIG_SYS_CACHELINE_SIZE);
97 mmu_page_table_flush(startpt, stoppt);
Simon Glass0dde7f52012-10-17 13:24:53 +000098}
99
Marek Szyprowskid877f8f2020-06-03 14:43:42 +0200100void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
101 enum dcache_option option)
102{
103 mmu_set_region_dcache_behaviour_phys(start, start, size, option);
104}
105
R Sricharan96fdbec2013-03-04 20:04:44 +0000106__weak void dram_bank_mmu_setup(int bank)
Simon Glass0dde7f52012-10-17 13:24:53 +0000107{
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900108 struct bd_info *bd = gd->bd;
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200109 int i;
110
Patrick Delaunayc8ec1e32020-04-24 20:20:15 +0200111 /* bd->bi_dram is available only after relocation */
112 if ((gd->flags & GD_FLG_RELOC) == 0)
113 return;
114
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200115 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafd990f5c2016-03-16 15:41:21 +0100116 for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT;
117 i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) +
118 (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT);
Patrick Delaunay2e8d68e2020-04-24 20:20:16 +0200119 i++)
120 set_section_dcache(i, DCACHE_DEFAULT_OPTION);
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200121}
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200122
123/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher880eff52010-09-17 13:10:29 +0200124static inline void mmu_setup(void)
125{
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200126 int i;
Heiko Schocher880eff52010-09-17 13:10:29 +0200127 u32 reg;
128
Aneesh Vc2dd0d42011-06-16 23:30:49 +0000129 arm_init_before_mmu();
Heiko Schocher880eff52010-09-17 13:10:29 +0200130 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafd990f5c2016-03-16 15:41:21 +0100131 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glass0dde7f52012-10-17 13:24:53 +0000132 set_section_dcache(i, DCACHE_OFF);
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200133
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200134 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
135 dram_bank_mmu_setup(i);
136 }
Heiko Schocher880eff52010-09-17 13:10:29 +0200137
Simon Glass10d602a2017-05-31 17:57:13 -0600138#if defined(CONFIG_ARMV7_LPAE) && __LINUX_ARM_ARCH__ != 4
Alexander Grafd990f5c2016-03-16 15:41:21 +0100139 /* Set up 4 PTE entries pointing to our 4 1GB page tables */
140 for (i = 0; i < 4; i++) {
141 u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4));
142 u64 tpt = gd->arch.tlb_addr + (4096 * i);
143 page_table[i] = tpt | TTB_PAGETABLE;
144 }
145
146 reg = TTBCR_EAE;
147#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
148 reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT;
149#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
150 reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA;
151#else
152 reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA;
153#endif
154
155 if (is_hyp()) {
Simon Glass579dfca2017-05-31 17:57:12 -0600156 /* Set HTCR to enable LPAE */
Alexander Grafd990f5c2016-03-16 15:41:21 +0100157 asm volatile("mcr p15, 4, %0, c2, c0, 2"
158 : : "r" (reg) : "memory");
159 /* Set HTTBR0 */
160 asm volatile("mcrr p15, 4, %0, %1, c2"
161 :
162 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
163 : "memory");
164 /* Set HMAIR */
165 asm volatile("mcr p15, 4, %0, c10, c2, 0"
166 : : "r" (MEMORY_ATTRIBUTES) : "memory");
167 } else {
168 /* Set TTBCR to enable LPAE */
169 asm volatile("mcr p15, 0, %0, c2, c0, 2"
170 : : "r" (reg) : "memory");
171 /* Set 64-bit TTBR0 */
172 asm volatile("mcrr p15, 0, %0, %1, c2"
173 :
174 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
175 : "memory");
176 /* Set MAIR */
177 asm volatile("mcr p15, 0, %0, c10, c2, 0"
178 : : "r" (MEMORY_ATTRIBUTES) : "memory");
179 }
Lokesh Vutlaacf15002018-04-26 18:21:26 +0530180#elif defined(CONFIG_CPU_V7A)
Simon Glass50a48862017-05-31 17:57:14 -0600181 if (is_hyp()) {
182 /* Set HTCR to disable LPAE */
183 asm volatile("mcr p15, 4, %0, c2, c0, 2"
184 : : "r" (0) : "memory");
185 } else {
186 /* Set TTBCR to disable LPAE */
187 asm volatile("mcr p15, 0, %0, c2, c0, 2"
188 : : "r" (0) : "memory");
189 }
Bryan Brinsko97840b52015-03-24 11:25:12 -0500190 /* Set TTBR0 */
191 reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
192#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
193 reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
194#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
195 reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
196#else
197 reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
198#endif
199 asm volatile("mcr p15, 0, %0, c2, c0, 0"
200 : : "r" (reg) : "memory");
201#else
Heiko Schocher880eff52010-09-17 13:10:29 +0200202 /* Copy the page table address to cp15 */
203 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass34fd5d22012-12-13 20:48:39 +0000204 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko97840b52015-03-24 11:25:12 -0500205#endif
Patrick Delaunay503eea42021-02-05 13:53:36 +0100206 /*
207 * initial value of Domain Access Control Register (DACR)
208 * Set the access control to client (1U) for each of the 16 domains
209 */
Heiko Schocher880eff52010-09-17 13:10:29 +0200210 asm volatile("mcr p15, 0, %0, c3, c0, 0"
Patrick Delaunay503eea42021-02-05 13:53:36 +0100211 : : "r" (0x55555555));
R Sricharande63ac22013-03-04 20:04:45 +0000212
213 arm_init_domains();
214
Heiko Schocher880eff52010-09-17 13:10:29 +0200215 /* and enable the mmu */
216 reg = get_cr(); /* get control reg. */
Heiko Schocher880eff52010-09-17 13:10:29 +0200217 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200218}
219
Aneesh Ve05f0072011-06-16 23:30:50 +0000220static int mmu_enabled(void)
221{
222 return get_cr() & CR_M;
223}
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +0530224#endif /* CONFIG_SYS_ARM_MMU */
Aneesh Ve05f0072011-06-16 23:30:50 +0000225
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200226/* cache_bit must be either CR_I or CR_C */
227static void cache_enable(uint32_t cache_bit)
228{
229 uint32_t reg;
230
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +0530231 /* The data cache is not active unless the mmu/mpu is enabled too */
232#ifdef CONFIG_SYS_ARM_MMU
Aneesh Ve05f0072011-06-16 23:30:50 +0000233 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher880eff52010-09-17 13:10:29 +0200234 mmu_setup();
Lokesh Vutlaa43d46a2018-04-26 18:21:31 +0530235#elif defined(CONFIG_SYS_ARM_MPU)
236 if ((cache_bit == CR_C) && !mpu_enabled()) {
237 printf("Consider enabling MPU before enabling caches\n");
238 return;
239 }
240#endif
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200241 reg = get_cr(); /* get control reg. */
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200242 set_cr(reg | cache_bit);
243}
244
245/* cache_bit must be either CR_I or CR_C */
246static void cache_disable(uint32_t cache_bit)
247{
248 uint32_t reg;
249
SRICHARAN Rd702b082012-05-16 23:52:54 +0000250 reg = get_cr();
SRICHARAN Rd702b082012-05-16 23:52:54 +0000251
Heiko Schocher880eff52010-09-17 13:10:29 +0200252 if (cache_bit == CR_C) {
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200253 /* if cache isn;t enabled no need to disable */
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200254 if ((reg & CR_C) != CR_C)
255 return;
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530256#ifdef CONFIG_SYS_ARM_MMU
Heiko Schocher880eff52010-09-17 13:10:29 +0200257 /* if disabling data cache, disable mmu too */
258 cache_bit |= CR_M;
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530259#endif
Heiko Schocher880eff52010-09-17 13:10:29 +0200260 }
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000261 reg = get_cr();
Lothar Waßmann53d4ed72017-06-08 09:48:41 +0200262
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530263#ifdef CONFIG_SYS_ARM_MMU
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000264 if (cache_bit == (CR_C | CR_M))
Lokesh Vutla7a540ee2019-10-30 15:55:41 +0530265#elif defined(CONFIG_SYS_ARM_MPU)
266 if (cache_bit == CR_C)
267#endif
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000268 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200269 set_cr(reg & ~cache_bit);
270}
271#endif
272
Trevor Woerner10015022019-05-03 09:41:00 -0400273#if CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Simon Glass6cc915b2019-11-14 12:57:36 -0700274void icache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200275{
276 return;
277}
278
Simon Glass6cc915b2019-11-14 12:57:36 -0700279void icache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200280{
281 return;
282}
283
Simon Glass6cc915b2019-11-14 12:57:36 -0700284int icache_status(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200285{
286 return 0; /* always off */
287}
288#else
289void icache_enable(void)
290{
291 cache_enable(CR_I);
292}
293
294void icache_disable(void)
295{
296 cache_disable(CR_I);
297}
298
299int icache_status(void)
300{
301 return (get_cr() & CR_I) != 0;
302}
303#endif
304
Trevor Woerner10015022019-05-03 09:41:00 -0400305#if CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glass6cc915b2019-11-14 12:57:36 -0700306void dcache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200307{
308 return;
309}
310
Simon Glass6cc915b2019-11-14 12:57:36 -0700311void dcache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200312{
313 return;
314}
315
Simon Glass6cc915b2019-11-14 12:57:36 -0700316int dcache_status(void)
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200317{
318 return 0; /* always off */
319}
320#else
321void dcache_enable(void)
322{
323 cache_enable(CR_C);
324}
325
326void dcache_disable(void)
327{
328 cache_disable(CR_C);
329}
330
331int dcache_status(void)
332{
333 return (get_cr() & CR_C) != 0;
334}
335#endif