blob: 1da2e92fe24110a00c0c1038ffea17d6a8e9ec0f [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>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020010#include <asm/system.h>
R Sricharan08716072013-03-04 20:04:44 +000011#include <asm/cache.h>
12#include <linux/compiler.h>
Lokesh Vutla19858f92018-04-26 18:21:31 +053013#include <asm/armv7_mpu.h>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020014
Trevor Woerner43ec7e02019-05-03 09:41:00 -040015#if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020016
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020017DECLARE_GLOBAL_DATA_PTR;
18
Lokesh Vutla19858f92018-04-26 18:21:31 +053019#ifdef CONFIG_SYS_ARM_MMU
Jeroen Hofsteed7460772014-06-23 22:07:04 +020020__weak void arm_init_before_mmu(void)
Aneesh V3e3bc1e2011-06-16 23:30:49 +000021{
22}
Aneesh V3e3bc1e2011-06-16 23:30:49 +000023
R Sricharan06396c12013-03-04 20:04:45 +000024__weak void arm_init_domains(void)
25{
26}
27
Simon Glassa4f20792012-10-17 13:24:53 +000028void set_section_dcache(int section, enum dcache_option option)
Heiko Schocheraeb29912010-09-17 13:10:39 +020029{
Alexander Grafae6c2bc2016-03-16 15:41:21 +010030#ifdef CONFIG_ARMV7_LPAE
31 u64 *page_table = (u64 *)gd->arch.tlb_addr;
32 /* Need to set the access flag to not fault */
33 u64 value = TTB_SECT_AP | TTB_SECT_AF;
34#else
Simon Glass6b4ee152012-12-13 20:48:39 +000035 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010036 u32 value = TTB_SECT_AP;
37#endif
38
39 /* Add the page offset */
40 value |= ((u32)section << MMU_SECTION_SHIFT);
Simon Glassa4f20792012-10-17 13:24:53 +000041
Alexander Grafae6c2bc2016-03-16 15:41:21 +010042 /* Add caching bits */
Simon Glassa4f20792012-10-17 13:24:53 +000043 value |= option;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010044
45 /* Set PTE */
Simon Glassa4f20792012-10-17 13:24:53 +000046 page_table[section] = value;
47}
48
Jeroen Hofsteed7460772014-06-23 22:07:04 +020049__weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
Simon Glassa4f20792012-10-17 13:24:53 +000050{
51 debug("%s: Warning: not implemented\n", __func__);
52}
53
Thierry Redingfe2007152014-08-26 17:34:21 +020054void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
Simon Glassa4f20792012-10-17 13:24:53 +000055 enum dcache_option option)
56{
Stefan Agnerc4a73222016-08-14 21:33:00 -070057#ifdef CONFIG_ARMV7_LPAE
58 u64 *page_table = (u64 *)gd->arch.tlb_addr;
59#else
Simon Glass6b4ee152012-12-13 20:48:39 +000060 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Stefan Agnerc4a73222016-08-14 21:33:00 -070061#endif
Stefan Agnerbae14802016-08-14 21:33:01 -070062 unsigned long startpt, stoppt;
Thierry Redingfe2007152014-08-26 17:34:21 +020063 unsigned long upto, end;
Simon Glassa4f20792012-10-17 13:24:53 +000064
Patrick Delaunay594b7cf2020-04-24 20:20:17 +020065 /* div by 2 before start + size to avoid phys_addr_t overflow */
66 end = ALIGN((start / 2) + (size / 2), MMU_SECTION_SIZE / 2)
67 >> (MMU_SECTION_SHIFT - 1);
Simon Glassa4f20792012-10-17 13:24:53 +000068 start = start >> MMU_SECTION_SHIFT;
Patrick Delaunay594b7cf2020-04-24 20:20:17 +020069
Keerthy266c8c12016-10-29 15:19:10 +053070#ifdef CONFIG_ARMV7_LPAE
71 debug("%s: start=%pa, size=%zu, option=%llx\n", __func__, &start, size,
72 option);
73#else
Keerthy485110a2016-10-29 15:19:09 +053074 debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size,
Simon Glassa4f20792012-10-17 13:24:53 +000075 option);
Keerthy266c8c12016-10-29 15:19:10 +053076#endif
Simon Glassa4f20792012-10-17 13:24:53 +000077 for (upto = start; upto < end; upto++)
78 set_section_dcache(upto, option);
Stefan Agnerbae14802016-08-14 21:33:01 -070079
80 /*
81 * Make sure range is cache line aligned
82 * Only CPU maintains page tables, hence it is safe to always
83 * flush complete cache lines...
84 */
85
86 startpt = (unsigned long)&page_table[start];
87 startpt &= ~(CONFIG_SYS_CACHELINE_SIZE - 1);
88 stoppt = (unsigned long)&page_table[end];
89 stoppt = ALIGN(stoppt, CONFIG_SYS_CACHELINE_SIZE);
90 mmu_page_table_flush(startpt, stoppt);
Simon Glassa4f20792012-10-17 13:24:53 +000091}
92
R Sricharan08716072013-03-04 20:04:44 +000093__weak void dram_bank_mmu_setup(int bank)
Simon Glassa4f20792012-10-17 13:24:53 +000094{
Heiko Schocheraeb29912010-09-17 13:10:39 +020095 bd_t *bd = gd->bd;
96 int i;
97
Patrick Delaunay77cc8b22020-04-24 20:20:15 +020098 /* bd->bi_dram is available only after relocation */
99 if ((gd->flags & GD_FLG_RELOC) == 0)
100 return;
101
Heiko Schocheraeb29912010-09-17 13:10:39 +0200102 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100103 for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT;
104 i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) +
105 (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT);
Patrick Delaunayd7e6a1d2020-04-24 20:20:16 +0200106 i++)
107 set_section_dcache(i, DCACHE_DEFAULT_OPTION);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200108}
Heiko Schocheraeb29912010-09-17 13:10:39 +0200109
110/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200111static inline void mmu_setup(void)
112{
Heiko Schocheraeb29912010-09-17 13:10:39 +0200113 int i;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200114 u32 reg;
115
Aneesh V3e3bc1e2011-06-16 23:30:49 +0000116 arm_init_before_mmu();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200117 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100118 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glassa4f20792012-10-17 13:24:53 +0000119 set_section_dcache(i, DCACHE_OFF);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200120
Heiko Schocheraeb29912010-09-17 13:10:39 +0200121 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
122 dram_bank_mmu_setup(i);
123 }
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200124
Simon Glass5bfd41d2017-05-31 17:57:13 -0600125#if defined(CONFIG_ARMV7_LPAE) && __LINUX_ARM_ARCH__ != 4
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100126 /* Set up 4 PTE entries pointing to our 4 1GB page tables */
127 for (i = 0; i < 4; i++) {
128 u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4));
129 u64 tpt = gd->arch.tlb_addr + (4096 * i);
130 page_table[i] = tpt | TTB_PAGETABLE;
131 }
132
133 reg = TTBCR_EAE;
134#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
135 reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT;
136#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
137 reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA;
138#else
139 reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA;
140#endif
141
142 if (is_hyp()) {
Simon Glass3b372472017-05-31 17:57:12 -0600143 /* Set HTCR to enable LPAE */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100144 asm volatile("mcr p15, 4, %0, c2, c0, 2"
145 : : "r" (reg) : "memory");
146 /* Set HTTBR0 */
147 asm volatile("mcrr p15, 4, %0, %1, c2"
148 :
149 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
150 : "memory");
151 /* Set HMAIR */
152 asm volatile("mcr p15, 4, %0, c10, c2, 0"
153 : : "r" (MEMORY_ATTRIBUTES) : "memory");
154 } else {
155 /* Set TTBCR to enable LPAE */
156 asm volatile("mcr p15, 0, %0, c2, c0, 2"
157 : : "r" (reg) : "memory");
158 /* Set 64-bit TTBR0 */
159 asm volatile("mcrr p15, 0, %0, %1, c2"
160 :
161 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
162 : "memory");
163 /* Set MAIR */
164 asm volatile("mcr p15, 0, %0, c10, c2, 0"
165 : : "r" (MEMORY_ATTRIBUTES) : "memory");
166 }
Lokesh Vutla81b1a672018-04-26 18:21:26 +0530167#elif defined(CONFIG_CPU_V7A)
Simon Glass1375e582017-05-31 17:57:14 -0600168 if (is_hyp()) {
169 /* Set HTCR to disable LPAE */
170 asm volatile("mcr p15, 4, %0, c2, c0, 2"
171 : : "r" (0) : "memory");
172 } else {
173 /* Set TTBCR to disable LPAE */
174 asm volatile("mcr p15, 0, %0, c2, c0, 2"
175 : : "r" (0) : "memory");
176 }
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500177 /* Set TTBR0 */
178 reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
179#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
180 reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
181#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
182 reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
183#else
184 reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
185#endif
186 asm volatile("mcr p15, 0, %0, c2, c0, 0"
187 : : "r" (reg) : "memory");
188#else
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200189 /* Copy the page table address to cp15 */
190 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass6b4ee152012-12-13 20:48:39 +0000191 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500192#endif
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200193 /* Set the access control to all-supervisor */
194 asm volatile("mcr p15, 0, %0, c3, c0, 0"
195 : : "r" (~0));
R Sricharan06396c12013-03-04 20:04:45 +0000196
197 arm_init_domains();
198
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200199 /* and enable the mmu */
200 reg = get_cr(); /* get control reg. */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200201 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200202}
203
Aneesh V3bda3772011-06-16 23:30:50 +0000204static int mmu_enabled(void)
205{
206 return get_cr() & CR_M;
207}
Lokesh Vutla19858f92018-04-26 18:21:31 +0530208#endif /* CONFIG_SYS_ARM_MMU */
Aneesh V3bda3772011-06-16 23:30:50 +0000209
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200210/* cache_bit must be either CR_I or CR_C */
211static void cache_enable(uint32_t cache_bit)
212{
213 uint32_t reg;
214
Lokesh Vutla19858f92018-04-26 18:21:31 +0530215 /* The data cache is not active unless the mmu/mpu is enabled too */
216#ifdef CONFIG_SYS_ARM_MMU
Aneesh V3bda3772011-06-16 23:30:50 +0000217 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200218 mmu_setup();
Lokesh Vutla19858f92018-04-26 18:21:31 +0530219#elif defined(CONFIG_SYS_ARM_MPU)
220 if ((cache_bit == CR_C) && !mpu_enabled()) {
221 printf("Consider enabling MPU before enabling caches\n");
222 return;
223 }
224#endif
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200225 reg = get_cr(); /* get control reg. */
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200226 set_cr(reg | cache_bit);
227}
228
229/* cache_bit must be either CR_I or CR_C */
230static void cache_disable(uint32_t cache_bit)
231{
232 uint32_t reg;
233
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000234 reg = get_cr();
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000235
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200236 if (cache_bit == CR_C) {
Heiko Schocheraeb29912010-09-17 13:10:39 +0200237 /* if cache isn;t enabled no need to disable */
Heiko Schocheraeb29912010-09-17 13:10:39 +0200238 if ((reg & CR_C) != CR_C)
239 return;
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530240#ifdef CONFIG_SYS_ARM_MMU
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200241 /* if disabling data cache, disable mmu too */
242 cache_bit |= CR_M;
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530243#endif
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200244 }
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000245 reg = get_cr();
Lothar Waßmannbded0c82017-06-08 09:48:41 +0200246
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530247#ifdef CONFIG_SYS_ARM_MMU
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000248 if (cache_bit == (CR_C | CR_M))
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530249#elif defined(CONFIG_SYS_ARM_MPU)
250 if (cache_bit == CR_C)
251#endif
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000252 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200253 set_cr(reg & ~cache_bit);
254}
255#endif
256
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400257#if CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Simon Glassfbf091b2019-11-14 12:57:36 -0700258void icache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200259{
260 return;
261}
262
Simon Glassfbf091b2019-11-14 12:57:36 -0700263void icache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200264{
265 return;
266}
267
Simon Glassfbf091b2019-11-14 12:57:36 -0700268int icache_status(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200269{
270 return 0; /* always off */
271}
272#else
273void icache_enable(void)
274{
275 cache_enable(CR_I);
276}
277
278void icache_disable(void)
279{
280 cache_disable(CR_I);
281}
282
283int icache_status(void)
284{
285 return (get_cr() & CR_I) != 0;
286}
287#endif
288
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400289#if CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glassfbf091b2019-11-14 12:57:36 -0700290void dcache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200291{
292 return;
293}
294
Simon Glassfbf091b2019-11-14 12:57:36 -0700295void dcache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200296{
297 return;
298}
299
Simon Glassfbf091b2019-11-14 12:57:36 -0700300int dcache_status(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200301{
302 return 0; /* always off */
303}
304#else
305void dcache_enable(void)
306{
307 cache_enable(CR_C);
308}
309
310void dcache_disable(void)
311{
312 cache_disable(CR_C);
313}
314
315int dcache_status(void)
316{
317 return (get_cr() & CR_C) != 0;
318}
319#endif