blob: aab1bf43607981159e29b55550898db0160c75f2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02002/*
3 * (C) Copyright 2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02005 */
6
7#include <common.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07008#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020011#include <asm/system.h>
R Sricharan08716072013-03-04 20:04:44 +000012#include <asm/cache.h>
13#include <linux/compiler.h>
Lokesh Vutla19858f92018-04-26 18:21:31 +053014#include <asm/armv7_mpu.h>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020015
Trevor Woerner43ec7e02019-05-03 09:41:00 -040016#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020017
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020018DECLARE_GLOBAL_DATA_PTR;
19
Lokesh Vutla19858f92018-04-26 18:21:31 +053020#ifdef CONFIG_SYS_ARM_MMU
Jeroen Hofsteed7460772014-06-23 22:07:04 +020021__weak void arm_init_before_mmu(void)
Aneesh V3e3bc1e2011-06-16 23:30:49 +000022{
23}
Aneesh V3e3bc1e2011-06-16 23:30:49 +000024
Marek Szyprowskif76fb512020-06-03 14:43:42 +020025static void set_section_phys(int section, phys_addr_t phys,
26 enum dcache_option option)
Heiko Schocheraeb29912010-09-17 13:10:39 +020027{
Alexander Grafae6c2bc2016-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 Glass6b4ee152012-12-13 20:48:39 +000033 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010034 u32 value = TTB_SECT_AP;
35#endif
36
37 /* Add the page offset */
Marek Szyprowskif76fb512020-06-03 14:43:42 +020038 value |= phys;
Simon Glassa4f20792012-10-17 13:24:53 +000039
Alexander Grafae6c2bc2016-03-16 15:41:21 +010040 /* Add caching bits */
Simon Glassa4f20792012-10-17 13:24:53 +000041 value |= option;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010042
43 /* Set PTE */
Simon Glassa4f20792012-10-17 13:24:53 +000044 page_table[section] = value;
45}
46
Marek Szyprowskif76fb512020-06-03 14:43:42 +020047void set_section_dcache(int section, enum dcache_option option)
48{
49 set_section_phys(section, (u32)section << MMU_SECTION_SHIFT, option);
50}
51
Jeroen Hofsteed7460772014-06-23 22:07:04 +020052__weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
Simon Glassa4f20792012-10-17 13:24:53 +000053{
54 debug("%s: Warning: not implemented\n", __func__);
55}
56
Marek Szyprowskif76fb512020-06-03 14:43:42 +020057void mmu_set_region_dcache_behaviour_phys(phys_addr_t start, phys_addr_t phys,
58 size_t size, enum dcache_option option)
Simon Glassa4f20792012-10-17 13:24:53 +000059{
Stefan Agnerc4a73222016-08-14 21:33:00 -070060#ifdef CONFIG_ARMV7_LPAE
61 u64 *page_table = (u64 *)gd->arch.tlb_addr;
62#else
Simon Glass6b4ee152012-12-13 20:48:39 +000063 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Stefan Agnerc4a73222016-08-14 21:33:00 -070064#endif
Stefan Agnerbae14802016-08-14 21:33:01 -070065 unsigned long startpt, stoppt;
Thierry Redingfe2007152014-08-26 17:34:21 +020066 unsigned long upto, end;
Simon Glassa4f20792012-10-17 13:24:53 +000067
Patrick Delaunay594b7cf2020-04-24 20:20:17 +020068 /* div by 2 before start + size to avoid phys_addr_t overflow */
69 end = ALIGN((start / 2) + (size / 2), MMU_SECTION_SIZE / 2)
70 >> (MMU_SECTION_SHIFT - 1);
Simon Glassa4f20792012-10-17 13:24:53 +000071 start = start >> MMU_SECTION_SHIFT;
Patrick Delaunay594b7cf2020-04-24 20:20:17 +020072
Keerthy266c8c12016-10-29 15:19:10 +053073#ifdef CONFIG_ARMV7_LPAE
74 debug("%s: start=%pa, size=%zu, option=%llx\n", __func__, &start, size,
75 option);
76#else
Keerthy485110a2016-10-29 15:19:09 +053077 debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size,
Simon Glassa4f20792012-10-17 13:24:53 +000078 option);
Keerthy266c8c12016-10-29 15:19:10 +053079#endif
Marek Szyprowskif76fb512020-06-03 14:43:42 +020080 for (upto = start; upto < end; upto++, phys += MMU_SECTION_SIZE)
81 set_section_phys(upto, phys, option);
Stefan Agnerbae14802016-08-14 21:33:01 -070082
83 /*
84 * Make sure range is cache line aligned
85 * Only CPU maintains page tables, hence it is safe to always
86 * flush complete cache lines...
87 */
88
89 startpt = (unsigned long)&page_table[start];
90 startpt &= ~(CONFIG_SYS_CACHELINE_SIZE - 1);
91 stoppt = (unsigned long)&page_table[end];
92 stoppt = ALIGN(stoppt, CONFIG_SYS_CACHELINE_SIZE);
93 mmu_page_table_flush(startpt, stoppt);
Simon Glassa4f20792012-10-17 13:24:53 +000094}
95
Marek Szyprowskif76fb512020-06-03 14:43:42 +020096void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
97 enum dcache_option option)
98{
99 mmu_set_region_dcache_behaviour_phys(start, start, size, option);
100}
101
R Sricharan08716072013-03-04 20:04:44 +0000102__weak void dram_bank_mmu_setup(int bank)
Simon Glassa4f20792012-10-17 13:24:53 +0000103{
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900104 struct bd_info *bd = gd->bd;
Heiko Schocheraeb29912010-09-17 13:10:39 +0200105 int i;
106
Patrick Delaunay77cc8b22020-04-24 20:20:15 +0200107 /* bd->bi_dram is available only after relocation */
108 if ((gd->flags & GD_FLG_RELOC) == 0)
109 return;
110
Heiko Schocheraeb29912010-09-17 13:10:39 +0200111 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100112 for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT;
113 i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) +
114 (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT);
Patrick Delaunayd7e6a1d2020-04-24 20:20:16 +0200115 i++)
116 set_section_dcache(i, DCACHE_DEFAULT_OPTION);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200117}
Heiko Schocheraeb29912010-09-17 13:10:39 +0200118
119/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200120static inline void mmu_setup(void)
121{
Heiko Schocheraeb29912010-09-17 13:10:39 +0200122 int i;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200123 u32 reg;
124
Aneesh V3e3bc1e2011-06-16 23:30:49 +0000125 arm_init_before_mmu();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200126 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100127 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glassa4f20792012-10-17 13:24:53 +0000128 set_section_dcache(i, DCACHE_OFF);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200129
Heiko Schocheraeb29912010-09-17 13:10:39 +0200130 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
131 dram_bank_mmu_setup(i);
132 }
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200133
Simon Glass5bfd41d2017-05-31 17:57:13 -0600134#if defined(CONFIG_ARMV7_LPAE) && __LINUX_ARM_ARCH__ != 4
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100135 /* Set up 4 PTE entries pointing to our 4 1GB page tables */
136 for (i = 0; i < 4; i++) {
137 u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4));
138 u64 tpt = gd->arch.tlb_addr + (4096 * i);
139 page_table[i] = tpt | TTB_PAGETABLE;
140 }
141
142 reg = TTBCR_EAE;
143#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
144 reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT;
145#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
146 reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA;
147#else
148 reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA;
149#endif
150
151 if (is_hyp()) {
Simon Glass3b372472017-05-31 17:57:12 -0600152 /* Set HTCR to enable LPAE */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100153 asm volatile("mcr p15, 4, %0, c2, c0, 2"
154 : : "r" (reg) : "memory");
155 /* Set HTTBR0 */
156 asm volatile("mcrr p15, 4, %0, %1, c2"
157 :
158 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
159 : "memory");
160 /* Set HMAIR */
161 asm volatile("mcr p15, 4, %0, c10, c2, 0"
162 : : "r" (MEMORY_ATTRIBUTES) : "memory");
163 } else {
164 /* Set TTBCR to enable LPAE */
165 asm volatile("mcr p15, 0, %0, c2, c0, 2"
166 : : "r" (reg) : "memory");
167 /* Set 64-bit TTBR0 */
168 asm volatile("mcrr p15, 0, %0, %1, c2"
169 :
170 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
171 : "memory");
172 /* Set MAIR */
173 asm volatile("mcr p15, 0, %0, c10, c2, 0"
174 : : "r" (MEMORY_ATTRIBUTES) : "memory");
175 }
Lokesh Vutla81b1a672018-04-26 18:21:26 +0530176#elif defined(CONFIG_CPU_V7A)
Simon Glass1375e582017-05-31 17:57:14 -0600177 if (is_hyp()) {
178 /* Set HTCR to disable LPAE */
179 asm volatile("mcr p15, 4, %0, c2, c0, 2"
180 : : "r" (0) : "memory");
181 } else {
182 /* Set TTBCR to disable LPAE */
183 asm volatile("mcr p15, 0, %0, c2, c0, 2"
184 : : "r" (0) : "memory");
185 }
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500186 /* Set TTBR0 */
187 reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
188#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
189 reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
190#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
191 reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
192#else
193 reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
194#endif
195 asm volatile("mcr p15, 0, %0, c2, c0, 0"
196 : : "r" (reg) : "memory");
197#else
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200198 /* Copy the page table address to cp15 */
199 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass6b4ee152012-12-13 20:48:39 +0000200 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500201#endif
Patrick Delaunay4aae24d2021-02-05 13:53:36 +0100202 /*
203 * initial value of Domain Access Control Register (DACR)
204 * Set the access control to client (1U) for each of the 16 domains
205 */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200206 asm volatile("mcr p15, 0, %0, c3, c0, 0"
Patrick Delaunay4aae24d2021-02-05 13:53:36 +0100207 : : "r" (0x55555555));
R Sricharan06396c12013-03-04 20:04:45 +0000208
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200209 /* and enable the mmu */
210 reg = get_cr(); /* get control reg. */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200211 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200212}
213
Aneesh V3bda3772011-06-16 23:30:50 +0000214static int mmu_enabled(void)
215{
216 return get_cr() & CR_M;
217}
Lokesh Vutla19858f92018-04-26 18:21:31 +0530218#endif /* CONFIG_SYS_ARM_MMU */
Aneesh V3bda3772011-06-16 23:30:50 +0000219
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200220/* cache_bit must be either CR_I or CR_C */
221static void cache_enable(uint32_t cache_bit)
222{
223 uint32_t reg;
224
Lokesh Vutla19858f92018-04-26 18:21:31 +0530225 /* The data cache is not active unless the mmu/mpu is enabled too */
226#ifdef CONFIG_SYS_ARM_MMU
Aneesh V3bda3772011-06-16 23:30:50 +0000227 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200228 mmu_setup();
Lokesh Vutla19858f92018-04-26 18:21:31 +0530229#elif defined(CONFIG_SYS_ARM_MPU)
230 if ((cache_bit == CR_C) && !mpu_enabled()) {
231 printf("Consider enabling MPU before enabling caches\n");
232 return;
233 }
234#endif
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200235 reg = get_cr(); /* get control reg. */
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200236 set_cr(reg | cache_bit);
237}
238
239/* cache_bit must be either CR_I or CR_C */
240static void cache_disable(uint32_t cache_bit)
241{
242 uint32_t reg;
243
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000244 reg = get_cr();
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000245
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200246 if (cache_bit == CR_C) {
Heiko Schocheraeb29912010-09-17 13:10:39 +0200247 /* if cache isn;t enabled no need to disable */
Heiko Schocheraeb29912010-09-17 13:10:39 +0200248 if ((reg & CR_C) != CR_C)
249 return;
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530250#ifdef CONFIG_SYS_ARM_MMU
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200251 /* if disabling data cache, disable mmu too */
252 cache_bit |= CR_M;
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530253#endif
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200254 }
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000255 reg = get_cr();
Lothar Waßmannbded0c82017-06-08 09:48:41 +0200256
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530257#ifdef CONFIG_SYS_ARM_MMU
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000258 if (cache_bit == (CR_C | CR_M))
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530259#elif defined(CONFIG_SYS_ARM_MPU)
260 if (cache_bit == CR_C)
261#endif
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000262 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200263 set_cr(reg & ~cache_bit);
264}
265#endif
266
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400267#if CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Simon Glassfbf091b2019-11-14 12:57:36 -0700268void icache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200269{
270 return;
271}
272
Simon Glassfbf091b2019-11-14 12:57:36 -0700273void icache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200274{
275 return;
276}
277
Simon Glassfbf091b2019-11-14 12:57:36 -0700278int icache_status(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200279{
280 return 0; /* always off */
281}
282#else
283void icache_enable(void)
284{
285 cache_enable(CR_I);
286}
287
288void icache_disable(void)
289{
290 cache_disable(CR_I);
291}
292
293int icache_status(void)
294{
295 return (get_cr() & CR_I) != 0;
296}
297#endif
298
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400299#if CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glassfbf091b2019-11-14 12:57:36 -0700300void dcache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200301{
302 return;
303}
304
Simon Glassfbf091b2019-11-14 12:57:36 -0700305void dcache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200306{
307 return;
308}
309
Simon Glassfbf091b2019-11-14 12:57:36 -0700310int dcache_status(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200311{
312 return 0; /* always off */
313}
314#else
315void dcache_enable(void)
316{
317 cache_enable(CR_C);
318}
319
320void dcache_disable(void)
321{
322 cache_disable(CR_C);
323}
324
325int dcache_status(void)
326{
327 return (get_cr() & CR_C) != 0;
328}
329#endif