blob: c80a95764fcb994cbb1378f3da48a6335a7a7ab1 [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>
8#include <asm/system.h>
R Sricharan96fdbec2013-03-04 20:04:44 +00009#include <asm/cache.h>
10#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020011
Aneesh Ve47f2db2011-06-16 23:30:48 +000012#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
Heiko Schocher880eff52010-09-17 13:10:29 +020013
Heiko Schocher880eff52010-09-17 13:10:29 +020014DECLARE_GLOBAL_DATA_PTR;
15
Jeroen Hofsteefcfddfd2014-06-23 22:07:04 +020016__weak void arm_init_before_mmu(void)
Aneesh Vc2dd0d42011-06-16 23:30:49 +000017{
18}
Aneesh Vc2dd0d42011-06-16 23:30:49 +000019
R Sricharande63ac22013-03-04 20:04:45 +000020__weak void arm_init_domains(void)
21{
22}
23
Simon Glass0dde7f52012-10-17 13:24:53 +000024void set_section_dcache(int section, enum dcache_option option)
Heiko Schocherf1d2b312010-09-17 13:10:39 +020025{
Alexander Grafd990f5c2016-03-16 15:41:21 +010026#ifdef CONFIG_ARMV7_LPAE
27 u64 *page_table = (u64 *)gd->arch.tlb_addr;
28 /* Need to set the access flag to not fault */
29 u64 value = TTB_SECT_AP | TTB_SECT_AF;
30#else
Simon Glass34fd5d22012-12-13 20:48:39 +000031 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafd990f5c2016-03-16 15:41:21 +010032 u32 value = TTB_SECT_AP;
33#endif
Simon Glass0dde7f52012-10-17 13:24:53 +000034
Alexander Grafd990f5c2016-03-16 15:41:21 +010035 /* Add the page offset */
36 value |= ((u32)section << MMU_SECTION_SHIFT);
37
38 /* Add caching bits */
Simon Glass0dde7f52012-10-17 13:24:53 +000039 value |= option;
Alexander Grafd990f5c2016-03-16 15:41:21 +010040
41 /* Set PTE */
Simon Glass0dde7f52012-10-17 13:24:53 +000042 page_table[section] = value;
43}
44
Jeroen Hofsteefcfddfd2014-06-23 22:07:04 +020045__weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
Simon Glass0dde7f52012-10-17 13:24:53 +000046{
47 debug("%s: Warning: not implemented\n", __func__);
48}
49
Thierry Reding25026fa2014-08-26 17:34:21 +020050void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
Simon Glass0dde7f52012-10-17 13:24:53 +000051 enum dcache_option option)
52{
Stefan Agnerc5b3cab2016-08-14 21:33:00 -070053#ifdef CONFIG_ARMV7_LPAE
54 u64 *page_table = (u64 *)gd->arch.tlb_addr;
55#else
Simon Glass34fd5d22012-12-13 20:48:39 +000056 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Stefan Agnerc5b3cab2016-08-14 21:33:00 -070057#endif
Stefan Agner8f894a42016-08-14 21:33:01 -070058 unsigned long startpt, stoppt;
Thierry Reding25026fa2014-08-26 17:34:21 +020059 unsigned long upto, end;
Simon Glass0dde7f52012-10-17 13:24:53 +000060
61 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
62 start = start >> MMU_SECTION_SHIFT;
Keerthy06d43c82016-10-29 15:19:10 +053063#ifdef CONFIG_ARMV7_LPAE
64 debug("%s: start=%pa, size=%zu, option=%llx\n", __func__, &start, size,
65 option);
66#else
Keerthy2b373cb2016-10-29 15:19:09 +053067 debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size,
Simon Glass0dde7f52012-10-17 13:24:53 +000068 option);
Keerthy06d43c82016-10-29 15:19:10 +053069#endif
Simon Glass0dde7f52012-10-17 13:24:53 +000070 for (upto = start; upto < end; upto++)
71 set_section_dcache(upto, option);
Stefan Agner8f894a42016-08-14 21:33:01 -070072
73 /*
74 * Make sure range is cache line aligned
75 * Only CPU maintains page tables, hence it is safe to always
76 * flush complete cache lines...
77 */
78
79 startpt = (unsigned long)&page_table[start];
80 startpt &= ~(CONFIG_SYS_CACHELINE_SIZE - 1);
81 stoppt = (unsigned long)&page_table[end];
82 stoppt = ALIGN(stoppt, CONFIG_SYS_CACHELINE_SIZE);
83 mmu_page_table_flush(startpt, stoppt);
Simon Glass0dde7f52012-10-17 13:24:53 +000084}
85
R Sricharan96fdbec2013-03-04 20:04:44 +000086__weak void dram_bank_mmu_setup(int bank)
Simon Glass0dde7f52012-10-17 13:24:53 +000087{
Heiko Schocherf1d2b312010-09-17 13:10:39 +020088 bd_t *bd = gd->bd;
89 int i;
90
91 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafd990f5c2016-03-16 15:41:21 +010092 for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT;
93 i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) +
94 (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT);
Heiko Schocherf1d2b312010-09-17 13:10:39 +020095 i++) {
Simon Glass0dde7f52012-10-17 13:24:53 +000096#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
97 set_section_dcache(i, DCACHE_WRITETHROUGH);
Marek Vasutff7e9702014-09-15 02:44:36 +020098#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
99 set_section_dcache(i, DCACHE_WRITEALLOC);
Simon Glass0dde7f52012-10-17 13:24:53 +0000100#else
101 set_section_dcache(i, DCACHE_WRITEBACK);
102#endif
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200103 }
104}
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200105
106/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher880eff52010-09-17 13:10:29 +0200107static inline void mmu_setup(void)
108{
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200109 int i;
Heiko Schocher880eff52010-09-17 13:10:29 +0200110 u32 reg;
111
Aneesh Vc2dd0d42011-06-16 23:30:49 +0000112 arm_init_before_mmu();
Heiko Schocher880eff52010-09-17 13:10:29 +0200113 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafd990f5c2016-03-16 15:41:21 +0100114 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glass0dde7f52012-10-17 13:24:53 +0000115 set_section_dcache(i, DCACHE_OFF);
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200116
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200117 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
118 dram_bank_mmu_setup(i);
119 }
Heiko Schocher880eff52010-09-17 13:10:29 +0200120
Simon Glass10d602a2017-05-31 17:57:13 -0600121#if defined(CONFIG_ARMV7_LPAE) && __LINUX_ARM_ARCH__ != 4
Alexander Grafd990f5c2016-03-16 15:41:21 +0100122 /* Set up 4 PTE entries pointing to our 4 1GB page tables */
123 for (i = 0; i < 4; i++) {
124 u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4));
125 u64 tpt = gd->arch.tlb_addr + (4096 * i);
126 page_table[i] = tpt | TTB_PAGETABLE;
127 }
128
129 reg = TTBCR_EAE;
130#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
131 reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT;
132#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
133 reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA;
134#else
135 reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA;
136#endif
137
138 if (is_hyp()) {
Simon Glass579dfca2017-05-31 17:57:12 -0600139 /* Set HTCR to enable LPAE */
Alexander Grafd990f5c2016-03-16 15:41:21 +0100140 asm volatile("mcr p15, 4, %0, c2, c0, 2"
141 : : "r" (reg) : "memory");
142 /* Set HTTBR0 */
143 asm volatile("mcrr p15, 4, %0, %1, c2"
144 :
145 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
146 : "memory");
147 /* Set HMAIR */
148 asm volatile("mcr p15, 4, %0, c10, c2, 0"
149 : : "r" (MEMORY_ATTRIBUTES) : "memory");
150 } else {
151 /* Set TTBCR to enable LPAE */
152 asm volatile("mcr p15, 0, %0, c2, c0, 2"
153 : : "r" (reg) : "memory");
154 /* Set 64-bit TTBR0 */
155 asm volatile("mcrr p15, 0, %0, %1, c2"
156 :
157 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
158 : "memory");
159 /* Set MAIR */
160 asm volatile("mcr p15, 0, %0, c10, c2, 0"
161 : : "r" (MEMORY_ATTRIBUTES) : "memory");
162 }
163#elif defined(CONFIG_CPU_V7)
Simon Glass50a48862017-05-31 17:57:14 -0600164 if (is_hyp()) {
165 /* Set HTCR to disable LPAE */
166 asm volatile("mcr p15, 4, %0, c2, c0, 2"
167 : : "r" (0) : "memory");
168 } else {
169 /* Set TTBCR to disable LPAE */
170 asm volatile("mcr p15, 0, %0, c2, c0, 2"
171 : : "r" (0) : "memory");
172 }
Bryan Brinsko97840b52015-03-24 11:25:12 -0500173 /* Set TTBR0 */
174 reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
175#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
176 reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
177#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
178 reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
179#else
180 reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
181#endif
182 asm volatile("mcr p15, 0, %0, c2, c0, 0"
183 : : "r" (reg) : "memory");
184#else
Heiko Schocher880eff52010-09-17 13:10:29 +0200185 /* Copy the page table address to cp15 */
186 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass34fd5d22012-12-13 20:48:39 +0000187 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko97840b52015-03-24 11:25:12 -0500188#endif
Heiko Schocher880eff52010-09-17 13:10:29 +0200189 /* Set the access control to all-supervisor */
190 asm volatile("mcr p15, 0, %0, c3, c0, 0"
191 : : "r" (~0));
R Sricharande63ac22013-03-04 20:04:45 +0000192
193 arm_init_domains();
194
Heiko Schocher880eff52010-09-17 13:10:29 +0200195 /* and enable the mmu */
196 reg = get_cr(); /* get control reg. */
Heiko Schocher880eff52010-09-17 13:10:29 +0200197 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200198}
199
Aneesh Ve05f0072011-06-16 23:30:50 +0000200static int mmu_enabled(void)
201{
202 return get_cr() & CR_M;
203}
204
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200205/* cache_bit must be either CR_I or CR_C */
206static void cache_enable(uint32_t cache_bit)
207{
208 uint32_t reg;
209
Heiko Schocher880eff52010-09-17 13:10:29 +0200210 /* The data cache is not active unless the mmu is enabled too */
Aneesh Ve05f0072011-06-16 23:30:50 +0000211 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher880eff52010-09-17 13:10:29 +0200212 mmu_setup();
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200213 reg = get_cr(); /* get control reg. */
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200214 set_cr(reg | cache_bit);
215}
216
217/* cache_bit must be either CR_I or CR_C */
218static void cache_disable(uint32_t cache_bit)
219{
220 uint32_t reg;
221
SRICHARAN Rd702b082012-05-16 23:52:54 +0000222 reg = get_cr();
SRICHARAN Rd702b082012-05-16 23:52:54 +0000223
Heiko Schocher880eff52010-09-17 13:10:29 +0200224 if (cache_bit == CR_C) {
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200225 /* if cache isn;t enabled no need to disable */
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200226 if ((reg & CR_C) != CR_C)
227 return;
Heiko Schocher880eff52010-09-17 13:10:29 +0200228 /* if disabling data cache, disable mmu too */
229 cache_bit |= CR_M;
Heiko Schocher880eff52010-09-17 13:10:29 +0200230 }
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000231 reg = get_cr();
Lothar Waßmann53d4ed72017-06-08 09:48:41 +0200232
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000233 if (cache_bit == (CR_C | CR_M))
234 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200235 set_cr(reg & ~cache_bit);
236}
237#endif
238
Aneesh Ve47f2db2011-06-16 23:30:48 +0000239#ifdef CONFIG_SYS_ICACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200240void icache_enable (void)
241{
242 return;
243}
244
245void icache_disable (void)
246{
247 return;
248}
249
250int icache_status (void)
251{
252 return 0; /* always off */
253}
254#else
255void icache_enable(void)
256{
257 cache_enable(CR_I);
258}
259
260void icache_disable(void)
261{
262 cache_disable(CR_I);
263}
264
265int icache_status(void)
266{
267 return (get_cr() & CR_I) != 0;
268}
269#endif
270
Aneesh Ve47f2db2011-06-16 23:30:48 +0000271#ifdef CONFIG_SYS_DCACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200272void dcache_enable (void)
273{
274 return;
275}
276
277void dcache_disable (void)
278{
279 return;
280}
281
282int dcache_status (void)
283{
284 return 0; /* always off */
285}
286#else
287void dcache_enable(void)
288{
289 cache_enable(CR_C);
290}
291
292void dcache_disable(void)
293{
294 cache_disable(CR_C);
295}
296
297int dcache_status(void)
298{
299 return (get_cr() & CR_C) != 0;
300}
301#endif