blob: f8d20960da9d6173e2985c5fbcb486eb62438554 [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>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02009#include <asm/system.h>
R Sricharan08716072013-03-04 20:04:44 +000010#include <asm/cache.h>
11#include <linux/compiler.h>
Lokesh Vutla19858f92018-04-26 18:21:31 +053012#include <asm/armv7_mpu.h>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020013
Trevor Woerner43ec7e02019-05-03 09:41:00 -040014#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020015
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020016DECLARE_GLOBAL_DATA_PTR;
17
Lokesh Vutla19858f92018-04-26 18:21:31 +053018#ifdef CONFIG_SYS_ARM_MMU
Jeroen Hofsteed7460772014-06-23 22:07:04 +020019__weak void arm_init_before_mmu(void)
Aneesh V3e3bc1e2011-06-16 23:30:49 +000020{
21}
Aneesh V3e3bc1e2011-06-16 23:30:49 +000022
R Sricharan06396c12013-03-04 20:04:45 +000023__weak void arm_init_domains(void)
24{
25}
26
Simon Glassa4f20792012-10-17 13:24:53 +000027void set_section_dcache(int section, enum dcache_option option)
Heiko Schocheraeb29912010-09-17 13:10:39 +020028{
Alexander Grafae6c2bc2016-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 Glass6b4ee152012-12-13 20:48:39 +000034 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010035 u32 value = TTB_SECT_AP;
36#endif
37
38 /* Add the page offset */
39 value |= ((u32)section << MMU_SECTION_SHIFT);
Simon Glassa4f20792012-10-17 13:24:53 +000040
Alexander Grafae6c2bc2016-03-16 15:41:21 +010041 /* Add caching bits */
Simon Glassa4f20792012-10-17 13:24:53 +000042 value |= option;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010043
44 /* Set PTE */
Simon Glassa4f20792012-10-17 13:24:53 +000045 page_table[section] = value;
46}
47
Jeroen Hofsteed7460772014-06-23 22:07:04 +020048__weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
Simon Glassa4f20792012-10-17 13:24:53 +000049{
50 debug("%s: Warning: not implemented\n", __func__);
51}
52
Thierry Redingfe2007152014-08-26 17:34:21 +020053void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
Simon Glassa4f20792012-10-17 13:24:53 +000054 enum dcache_option option)
55{
Stefan Agnerc4a73222016-08-14 21:33:00 -070056#ifdef CONFIG_ARMV7_LPAE
57 u64 *page_table = (u64 *)gd->arch.tlb_addr;
58#else
Simon Glass6b4ee152012-12-13 20:48:39 +000059 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Stefan Agnerc4a73222016-08-14 21:33:00 -070060#endif
Stefan Agnerbae14802016-08-14 21:33:01 -070061 unsigned long startpt, stoppt;
Thierry Redingfe2007152014-08-26 17:34:21 +020062 unsigned long upto, end;
Simon Glassa4f20792012-10-17 13:24:53 +000063
64 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
65 start = start >> MMU_SECTION_SHIFT;
Keerthy266c8c12016-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
Keerthy485110a2016-10-29 15:19:09 +053070 debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size,
Simon Glassa4f20792012-10-17 13:24:53 +000071 option);
Keerthy266c8c12016-10-29 15:19:10 +053072#endif
Simon Glassa4f20792012-10-17 13:24:53 +000073 for (upto = start; upto < end; upto++)
74 set_section_dcache(upto, option);
Stefan Agnerbae14802016-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 Glassa4f20792012-10-17 13:24:53 +000087}
88
R Sricharan08716072013-03-04 20:04:44 +000089__weak void dram_bank_mmu_setup(int bank)
Simon Glassa4f20792012-10-17 13:24:53 +000090{
Heiko Schocheraeb29912010-09-17 13:10:39 +020091 bd_t *bd = gd->bd;
92 int i;
93
94 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafae6c2bc2016-03-16 15:41:21 +010095 for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT;
96 i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) +
97 (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT);
Heiko Schocheraeb29912010-09-17 13:10:39 +020098 i++) {
Simon Glassa4f20792012-10-17 13:24:53 +000099#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
100 set_section_dcache(i, DCACHE_WRITETHROUGH);
Marek Vasut79b90722014-09-15 02:44:36 +0200101#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
102 set_section_dcache(i, DCACHE_WRITEALLOC);
Simon Glassa4f20792012-10-17 13:24:53 +0000103#else
104 set_section_dcache(i, DCACHE_WRITEBACK);
105#endif
Heiko Schocheraeb29912010-09-17 13:10:39 +0200106 }
107}
Heiko Schocheraeb29912010-09-17 13:10:39 +0200108
109/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200110static inline void mmu_setup(void)
111{
Heiko Schocheraeb29912010-09-17 13:10:39 +0200112 int i;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200113 u32 reg;
114
Aneesh V3e3bc1e2011-06-16 23:30:49 +0000115 arm_init_before_mmu();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200116 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100117 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glassa4f20792012-10-17 13:24:53 +0000118 set_section_dcache(i, DCACHE_OFF);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200119
Heiko Schocheraeb29912010-09-17 13:10:39 +0200120 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
121 dram_bank_mmu_setup(i);
122 }
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200123
Simon Glass5bfd41d2017-05-31 17:57:13 -0600124#if defined(CONFIG_ARMV7_LPAE) && __LINUX_ARM_ARCH__ != 4
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100125 /* Set up 4 PTE entries pointing to our 4 1GB page tables */
126 for (i = 0; i < 4; i++) {
127 u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4));
128 u64 tpt = gd->arch.tlb_addr + (4096 * i);
129 page_table[i] = tpt | TTB_PAGETABLE;
130 }
131
132 reg = TTBCR_EAE;
133#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
134 reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT;
135#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
136 reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA;
137#else
138 reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA;
139#endif
140
141 if (is_hyp()) {
Simon Glass3b372472017-05-31 17:57:12 -0600142 /* Set HTCR to enable LPAE */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100143 asm volatile("mcr p15, 4, %0, c2, c0, 2"
144 : : "r" (reg) : "memory");
145 /* Set HTTBR0 */
146 asm volatile("mcrr p15, 4, %0, %1, c2"
147 :
148 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
149 : "memory");
150 /* Set HMAIR */
151 asm volatile("mcr p15, 4, %0, c10, c2, 0"
152 : : "r" (MEMORY_ATTRIBUTES) : "memory");
153 } else {
154 /* Set TTBCR to enable LPAE */
155 asm volatile("mcr p15, 0, %0, c2, c0, 2"
156 : : "r" (reg) : "memory");
157 /* Set 64-bit TTBR0 */
158 asm volatile("mcrr p15, 0, %0, %1, c2"
159 :
160 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
161 : "memory");
162 /* Set MAIR */
163 asm volatile("mcr p15, 0, %0, c10, c2, 0"
164 : : "r" (MEMORY_ATTRIBUTES) : "memory");
165 }
Lokesh Vutla81b1a672018-04-26 18:21:26 +0530166#elif defined(CONFIG_CPU_V7A)
Simon Glass1375e582017-05-31 17:57:14 -0600167 if (is_hyp()) {
168 /* Set HTCR to disable LPAE */
169 asm volatile("mcr p15, 4, %0, c2, c0, 2"
170 : : "r" (0) : "memory");
171 } else {
172 /* Set TTBCR to disable LPAE */
173 asm volatile("mcr p15, 0, %0, c2, c0, 2"
174 : : "r" (0) : "memory");
175 }
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500176 /* Set TTBR0 */
177 reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
178#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
179 reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
180#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
181 reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
182#else
183 reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
184#endif
185 asm volatile("mcr p15, 0, %0, c2, c0, 0"
186 : : "r" (reg) : "memory");
187#else
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200188 /* Copy the page table address to cp15 */
189 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass6b4ee152012-12-13 20:48:39 +0000190 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500191#endif
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200192 /* Set the access control to all-supervisor */
193 asm volatile("mcr p15, 0, %0, c3, c0, 0"
194 : : "r" (~0));
R Sricharan06396c12013-03-04 20:04:45 +0000195
196 arm_init_domains();
197
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200198 /* and enable the mmu */
199 reg = get_cr(); /* get control reg. */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200200 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}
Lokesh Vutla19858f92018-04-26 18:21:31 +0530207#endif /* CONFIG_SYS_ARM_MMU */
Aneesh V3bda3772011-06-16 23:30:50 +0000208
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200209/* cache_bit must be either CR_I or CR_C */
210static void cache_enable(uint32_t cache_bit)
211{
212 uint32_t reg;
213
Lokesh Vutla19858f92018-04-26 18:21:31 +0530214 /* The data cache is not active unless the mmu/mpu is enabled too */
215#ifdef CONFIG_SYS_ARM_MMU
Aneesh V3bda3772011-06-16 23:30:50 +0000216 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200217 mmu_setup();
Lokesh Vutla19858f92018-04-26 18:21:31 +0530218#elif defined(CONFIG_SYS_ARM_MPU)
219 if ((cache_bit == CR_C) && !mpu_enabled()) {
220 printf("Consider enabling MPU before enabling caches\n");
221 return;
222 }
223#endif
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200224 reg = get_cr(); /* get control reg. */
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200225 set_cr(reg | cache_bit);
226}
227
228/* cache_bit must be either CR_I or CR_C */
229static void cache_disable(uint32_t cache_bit)
230{
231 uint32_t reg;
232
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000233 reg = get_cr();
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000234
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200235 if (cache_bit == CR_C) {
Heiko Schocheraeb29912010-09-17 13:10:39 +0200236 /* if cache isn;t enabled no need to disable */
Heiko Schocheraeb29912010-09-17 13:10:39 +0200237 if ((reg & CR_C) != CR_C)
238 return;
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530239#ifdef CONFIG_SYS_ARM_MMU
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200240 /* if disabling data cache, disable mmu too */
241 cache_bit |= CR_M;
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530242#endif
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200243 }
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000244 reg = get_cr();
Lothar Waßmannbded0c82017-06-08 09:48:41 +0200245
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530246#ifdef CONFIG_SYS_ARM_MMU
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000247 if (cache_bit == (CR_C | CR_M))
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530248#elif defined(CONFIG_SYS_ARM_MPU)
249 if (cache_bit == CR_C)
250#endif
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000251 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200252 set_cr(reg & ~cache_bit);
253}
254#endif
255
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400256#if CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Simon Glassfbf091b2019-11-14 12:57:36 -0700257void icache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200258{
259 return;
260}
261
Simon Glassfbf091b2019-11-14 12:57:36 -0700262void icache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200263{
264 return;
265}
266
Simon Glassfbf091b2019-11-14 12:57:36 -0700267int icache_status(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200268{
269 return 0; /* always off */
270}
271#else
272void icache_enable(void)
273{
274 cache_enable(CR_I);
275}
276
277void icache_disable(void)
278{
279 cache_disable(CR_I);
280}
281
282int icache_status(void)
283{
284 return (get_cr() & CR_I) != 0;
285}
286#endif
287
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400288#if CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glassfbf091b2019-11-14 12:57:36 -0700289void dcache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200290{
291 return;
292}
293
Simon Glassfbf091b2019-11-14 12:57:36 -0700294void dcache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200295{
296 return;
297}
298
Simon Glassfbf091b2019-11-14 12:57:36 -0700299int dcache_status(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200300{
301 return 0; /* always off */
302}
303#else
304void dcache_enable(void)
305{
306 cache_enable(CR_C);
307}
308
309void dcache_disable(void)
310{
311 cache_disable(CR_C);
312}
313
314int dcache_status(void)
315{
316 return (get_cr() & CR_C) != 0;
317}
318#endif