blob: 24050e5bdd30ed92cb810f70118b0ee001672978 [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
R Sricharan06396c12013-03-04 20:04:45 +000025__weak void arm_init_domains(void)
26{
27}
28
Marek Szyprowskif76fb512020-06-03 14:43:42 +020029static void set_section_phys(int section, phys_addr_t phys,
30 enum dcache_option option)
Heiko Schocheraeb29912010-09-17 13:10:39 +020031{
Alexander Grafae6c2bc2016-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 Glass6b4ee152012-12-13 20:48:39 +000037 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010038 u32 value = TTB_SECT_AP;
39#endif
40
41 /* Add the page offset */
Marek Szyprowskif76fb512020-06-03 14:43:42 +020042 value |= phys;
Simon Glassa4f20792012-10-17 13:24:53 +000043
Alexander Grafae6c2bc2016-03-16 15:41:21 +010044 /* Add caching bits */
Simon Glassa4f20792012-10-17 13:24:53 +000045 value |= option;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010046
47 /* Set PTE */
Simon Glassa4f20792012-10-17 13:24:53 +000048 page_table[section] = value;
49}
50
Marek Szyprowskif76fb512020-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 Hofsteed7460772014-06-23 22:07:04 +020056__weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
Simon Glassa4f20792012-10-17 13:24:53 +000057{
58 debug("%s: Warning: not implemented\n", __func__);
59}
60
Marek Szyprowskif76fb512020-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 Glassa4f20792012-10-17 13:24:53 +000063{
Stefan Agnerc4a73222016-08-14 21:33:00 -070064#ifdef CONFIG_ARMV7_LPAE
65 u64 *page_table = (u64 *)gd->arch.tlb_addr;
66#else
Simon Glass6b4ee152012-12-13 20:48:39 +000067 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Stefan Agnerc4a73222016-08-14 21:33:00 -070068#endif
Stefan Agnerbae14802016-08-14 21:33:01 -070069 unsigned long startpt, stoppt;
Thierry Redingfe2007152014-08-26 17:34:21 +020070 unsigned long upto, end;
Simon Glassa4f20792012-10-17 13:24:53 +000071
Patrick Delaunay594b7cf2020-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 Glassa4f20792012-10-17 13:24:53 +000075 start = start >> MMU_SECTION_SHIFT;
Patrick Delaunay594b7cf2020-04-24 20:20:17 +020076
Keerthy266c8c12016-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
Keerthy485110a2016-10-29 15:19:09 +053081 debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size,
Simon Glassa4f20792012-10-17 13:24:53 +000082 option);
Keerthy266c8c12016-10-29 15:19:10 +053083#endif
Marek Szyprowskif76fb512020-06-03 14:43:42 +020084 for (upto = start; upto < end; upto++, phys += MMU_SECTION_SIZE)
85 set_section_phys(upto, phys, option);
Stefan Agnerbae14802016-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 Glassa4f20792012-10-17 13:24:53 +000098}
99
Marek Szyprowskif76fb512020-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 Sricharan08716072013-03-04 20:04:44 +0000106__weak void dram_bank_mmu_setup(int bank)
Simon Glassa4f20792012-10-17 13:24:53 +0000107{
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900108 struct bd_info *bd = gd->bd;
Heiko Schocheraeb29912010-09-17 13:10:39 +0200109 int i;
110
Patrick Delaunay77cc8b22020-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 Schocheraeb29912010-09-17 13:10:39 +0200115 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafae6c2bc2016-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 Delaunayd7e6a1d2020-04-24 20:20:16 +0200119 i++)
120 set_section_dcache(i, DCACHE_DEFAULT_OPTION);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200121}
Heiko Schocheraeb29912010-09-17 13:10:39 +0200122
123/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200124static inline void mmu_setup(void)
125{
Heiko Schocheraeb29912010-09-17 13:10:39 +0200126 int i;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200127 u32 reg;
128
Aneesh V3e3bc1e2011-06-16 23:30:49 +0000129 arm_init_before_mmu();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200130 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100131 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glassa4f20792012-10-17 13:24:53 +0000132 set_section_dcache(i, DCACHE_OFF);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200133
Heiko Schocheraeb29912010-09-17 13:10:39 +0200134 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
135 dram_bank_mmu_setup(i);
136 }
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200137
Simon Glass5bfd41d2017-05-31 17:57:13 -0600138#if defined(CONFIG_ARMV7_LPAE) && __LINUX_ARM_ARCH__ != 4
Alexander Grafae6c2bc2016-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 Glass3b372472017-05-31 17:57:12 -0600156 /* Set HTCR to enable LPAE */
Alexander Grafae6c2bc2016-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 Vutla81b1a672018-04-26 18:21:26 +0530180#elif defined(CONFIG_CPU_V7A)
Simon Glass1375e582017-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 Brinsko29d23ec2015-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 Schocher69f1d0c2010-09-17 13:10:29 +0200202 /* Copy the page table address to cp15 */
203 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass6b4ee152012-12-13 20:48:39 +0000204 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500205#endif
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200206 /* Set the access control to all-supervisor */
207 asm volatile("mcr p15, 0, %0, c3, c0, 0"
208 : : "r" (~0));
R Sricharan06396c12013-03-04 20:04:45 +0000209
210 arm_init_domains();
211
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200212 /* and enable the mmu */
213 reg = get_cr(); /* get control reg. */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200214 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200215}
216
Aneesh V3bda3772011-06-16 23:30:50 +0000217static int mmu_enabled(void)
218{
219 return get_cr() & CR_M;
220}
Lokesh Vutla19858f92018-04-26 18:21:31 +0530221#endif /* CONFIG_SYS_ARM_MMU */
Aneesh V3bda3772011-06-16 23:30:50 +0000222
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200223/* cache_bit must be either CR_I or CR_C */
224static void cache_enable(uint32_t cache_bit)
225{
226 uint32_t reg;
227
Lokesh Vutla19858f92018-04-26 18:21:31 +0530228 /* The data cache is not active unless the mmu/mpu is enabled too */
229#ifdef CONFIG_SYS_ARM_MMU
Aneesh V3bda3772011-06-16 23:30:50 +0000230 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200231 mmu_setup();
Lokesh Vutla19858f92018-04-26 18:21:31 +0530232#elif defined(CONFIG_SYS_ARM_MPU)
233 if ((cache_bit == CR_C) && !mpu_enabled()) {
234 printf("Consider enabling MPU before enabling caches\n");
235 return;
236 }
237#endif
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200238 reg = get_cr(); /* get control reg. */
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200239 set_cr(reg | cache_bit);
240}
241
242/* cache_bit must be either CR_I or CR_C */
243static void cache_disable(uint32_t cache_bit)
244{
245 uint32_t reg;
246
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000247 reg = get_cr();
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000248
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200249 if (cache_bit == CR_C) {
Heiko Schocheraeb29912010-09-17 13:10:39 +0200250 /* if cache isn;t enabled no need to disable */
Heiko Schocheraeb29912010-09-17 13:10:39 +0200251 if ((reg & CR_C) != CR_C)
252 return;
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530253#ifdef CONFIG_SYS_ARM_MMU
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200254 /* if disabling data cache, disable mmu too */
255 cache_bit |= CR_M;
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530256#endif
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200257 }
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000258 reg = get_cr();
Lothar Waßmannbded0c82017-06-08 09:48:41 +0200259
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530260#ifdef CONFIG_SYS_ARM_MMU
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000261 if (cache_bit == (CR_C | CR_M))
Lokesh Vutla9ac1d172019-10-30 15:55:41 +0530262#elif defined(CONFIG_SYS_ARM_MPU)
263 if (cache_bit == CR_C)
264#endif
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000265 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200266 set_cr(reg & ~cache_bit);
267}
268#endif
269
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400270#if CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Simon Glassfbf091b2019-11-14 12:57:36 -0700271void icache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200272{
273 return;
274}
275
Simon Glassfbf091b2019-11-14 12:57:36 -0700276void icache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200277{
278 return;
279}
280
Simon Glassfbf091b2019-11-14 12:57:36 -0700281int icache_status(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200282{
283 return 0; /* always off */
284}
285#else
286void icache_enable(void)
287{
288 cache_enable(CR_I);
289}
290
291void icache_disable(void)
292{
293 cache_disable(CR_I);
294}
295
296int icache_status(void)
297{
298 return (get_cr() & CR_I) != 0;
299}
300#endif
301
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400302#if CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Simon Glassfbf091b2019-11-14 12:57:36 -0700303void dcache_enable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200304{
305 return;
306}
307
Simon Glassfbf091b2019-11-14 12:57:36 -0700308void dcache_disable(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200309{
310 return;
311}
312
Simon Glassfbf091b2019-11-14 12:57:36 -0700313int dcache_status(void)
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200314{
315 return 0; /* always off */
316}
317#else
318void dcache_enable(void)
319{
320 cache_enable(CR_C);
321}
322
323void dcache_disable(void)
324{
325 cache_disable(CR_C);
326}
327
328int dcache_status(void)
329{
330 return (get_cr() & CR_C) != 0;
331}
332#endif