blob: e9f9fc98f736d0e2f07cd3f9eb7b90279658fbce [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02006 */
7
8#include <common.h>
9#include <asm/system.h>
R Sricharan08716072013-03-04 20:04:44 +000010#include <asm/cache.h>
11#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020012
Aneesh Vecee9c82011-06-16 23:30:48 +000013#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020014
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020015DECLARE_GLOBAL_DATA_PTR;
16
Jeroen Hofsteed7460772014-06-23 22:07:04 +020017__weak void arm_init_before_mmu(void)
Aneesh V3e3bc1e2011-06-16 23:30:49 +000018{
19}
Aneesh V3e3bc1e2011-06-16 23:30:49 +000020
R Sricharan06396c12013-03-04 20:04:45 +000021__weak void arm_init_domains(void)
22{
23}
24
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020025static void cp_delay (void)
26{
27 volatile int i;
28
29 /* copro seems to need some delay between reading and writing */
30 for (i = 0; i < 100; i++)
31 nop();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020032 asm volatile("" : : : "memory");
33}
34
Simon Glassa4f20792012-10-17 13:24:53 +000035void set_section_dcache(int section, enum dcache_option option)
Heiko Schocheraeb29912010-09-17 13:10:39 +020036{
Alexander Grafae6c2bc2016-03-16 15:41:21 +010037#ifdef CONFIG_ARMV7_LPAE
38 u64 *page_table = (u64 *)gd->arch.tlb_addr;
39 /* Need to set the access flag to not fault */
40 u64 value = TTB_SECT_AP | TTB_SECT_AF;
41#else
Simon Glass6b4ee152012-12-13 20:48:39 +000042 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010043 u32 value = TTB_SECT_AP;
44#endif
45
46 /* Add the page offset */
47 value |= ((u32)section << MMU_SECTION_SHIFT);
Simon Glassa4f20792012-10-17 13:24:53 +000048
Alexander Grafae6c2bc2016-03-16 15:41:21 +010049 /* Add caching bits */
Simon Glassa4f20792012-10-17 13:24:53 +000050 value |= option;
Alexander Grafae6c2bc2016-03-16 15:41:21 +010051
52 /* Set PTE */
Simon Glassa4f20792012-10-17 13:24:53 +000053 page_table[section] = value;
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
Thierry Redingfe2007152014-08-26 17:34:21 +020061void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
Simon Glassa4f20792012-10-17 13:24:53 +000062 enum dcache_option option)
63{
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
72 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
73 start = start >> MMU_SECTION_SHIFT;
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);
76 for (upto = start; upto < end; upto++)
77 set_section_dcache(upto, option);
Stefan Agnerbae14802016-08-14 21:33:01 -070078
79 /*
80 * Make sure range is cache line aligned
81 * Only CPU maintains page tables, hence it is safe to always
82 * flush complete cache lines...
83 */
84
85 startpt = (unsigned long)&page_table[start];
86 startpt &= ~(CONFIG_SYS_CACHELINE_SIZE - 1);
87 stoppt = (unsigned long)&page_table[end];
88 stoppt = ALIGN(stoppt, CONFIG_SYS_CACHELINE_SIZE);
89 mmu_page_table_flush(startpt, stoppt);
Simon Glassa4f20792012-10-17 13:24:53 +000090}
91
R Sricharan08716072013-03-04 20:04:44 +000092__weak void dram_bank_mmu_setup(int bank)
Simon Glassa4f20792012-10-17 13:24:53 +000093{
Heiko Schocheraeb29912010-09-17 13:10:39 +020094 bd_t *bd = gd->bd;
95 int i;
96
97 debug("%s: bank: %d\n", __func__, bank);
Alexander Grafae6c2bc2016-03-16 15:41:21 +010098 for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT;
99 i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) +
100 (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200101 i++) {
Simon Glassa4f20792012-10-17 13:24:53 +0000102#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
103 set_section_dcache(i, DCACHE_WRITETHROUGH);
Marek Vasut79b90722014-09-15 02:44:36 +0200104#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
105 set_section_dcache(i, DCACHE_WRITEALLOC);
Simon Glassa4f20792012-10-17 13:24:53 +0000106#else
107 set_section_dcache(i, DCACHE_WRITEBACK);
108#endif
Heiko Schocheraeb29912010-09-17 13:10:39 +0200109 }
110}
Heiko Schocheraeb29912010-09-17 13:10:39 +0200111
112/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200113static inline void mmu_setup(void)
114{
Heiko Schocheraeb29912010-09-17 13:10:39 +0200115 int i;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200116 u32 reg;
117
Aneesh V3e3bc1e2011-06-16 23:30:49 +0000118 arm_init_before_mmu();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200119 /* Set up an identity-mapping for all 4GB, rw for everyone */
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100120 for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++)
Simon Glassa4f20792012-10-17 13:24:53 +0000121 set_section_dcache(i, DCACHE_OFF);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200122
Heiko Schocheraeb29912010-09-17 13:10:39 +0200123 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
124 dram_bank_mmu_setup(i);
125 }
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200126
Alexander Grafae6c2bc2016-03-16 15:41:21 +0100127#ifdef CONFIG_ARMV7_LPAE
128 /* Set up 4 PTE entries pointing to our 4 1GB page tables */
129 for (i = 0; i < 4; i++) {
130 u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4));
131 u64 tpt = gd->arch.tlb_addr + (4096 * i);
132 page_table[i] = tpt | TTB_PAGETABLE;
133 }
134
135 reg = TTBCR_EAE;
136#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
137 reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT;
138#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
139 reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA;
140#else
141 reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA;
142#endif
143
144 if (is_hyp()) {
145 /* Set HCTR to enable LPAE */
146 asm volatile("mcr p15, 4, %0, c2, c0, 2"
147 : : "r" (reg) : "memory");
148 /* Set HTTBR0 */
149 asm volatile("mcrr p15, 4, %0, %1, c2"
150 :
151 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
152 : "memory");
153 /* Set HMAIR */
154 asm volatile("mcr p15, 4, %0, c10, c2, 0"
155 : : "r" (MEMORY_ATTRIBUTES) : "memory");
156 } else {
157 /* Set TTBCR to enable LPAE */
158 asm volatile("mcr p15, 0, %0, c2, c0, 2"
159 : : "r" (reg) : "memory");
160 /* Set 64-bit TTBR0 */
161 asm volatile("mcrr p15, 0, %0, %1, c2"
162 :
163 : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0)
164 : "memory");
165 /* Set MAIR */
166 asm volatile("mcr p15, 0, %0, c10, c2, 0"
167 : : "r" (MEMORY_ATTRIBUTES) : "memory");
168 }
169#elif defined(CONFIG_CPU_V7)
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500170 /* Set TTBR0 */
171 reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
172#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
173 reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
174#elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
175 reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
176#else
177 reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
178#endif
179 asm volatile("mcr p15, 0, %0, c2, c0, 0"
180 : : "r" (reg) : "memory");
181#else
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200182 /* Copy the page table address to cp15 */
183 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass6b4ee152012-12-13 20:48:39 +0000184 : : "r" (gd->arch.tlb_addr) : "memory");
Bryan Brinsko29d23ec2015-03-24 11:25:12 -0500185#endif
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200186 /* Set the access control to all-supervisor */
187 asm volatile("mcr p15, 0, %0, c3, c0, 0"
188 : : "r" (~0));
R Sricharan06396c12013-03-04 20:04:45 +0000189
190 arm_init_domains();
191
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200192 /* and enable the mmu */
193 reg = get_cr(); /* get control reg. */
194 cp_delay();
195 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200196}
197
Aneesh V3bda3772011-06-16 23:30:50 +0000198static int mmu_enabled(void)
199{
200 return get_cr() & CR_M;
201}
202
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200203/* cache_bit must be either CR_I or CR_C */
204static void cache_enable(uint32_t cache_bit)
205{
206 uint32_t reg;
207
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200208 /* The data cache is not active unless the mmu is enabled too */
Aneesh V3bda3772011-06-16 23:30:50 +0000209 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200210 mmu_setup();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200211 reg = get_cr(); /* get control reg. */
212 cp_delay();
213 set_cr(reg | cache_bit);
214}
215
216/* cache_bit must be either CR_I or CR_C */
217static void cache_disable(uint32_t cache_bit)
218{
219 uint32_t reg;
220
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000221 reg = get_cr();
222 cp_delay();
223
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200224 if (cache_bit == CR_C) {
Heiko Schocheraeb29912010-09-17 13:10:39 +0200225 /* if cache isn;t enabled no need to disable */
Heiko Schocheraeb29912010-09-17 13:10:39 +0200226 if ((reg & CR_C) != CR_C)
227 return;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200228 /* if disabling data cache, disable mmu too */
229 cache_bit |= CR_M;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200230 }
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000231 reg = get_cr();
232 cp_delay();
233 if (cache_bit == (CR_C | CR_M))
234 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200235 set_cr(reg & ~cache_bit);
236}
237#endif
238
Aneesh Vecee9c82011-06-16 23:30:48 +0000239#ifdef CONFIG_SYS_ICACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-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 Vecee9c82011-06-16 23:30:48 +0000271#ifdef CONFIG_SYS_DCACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-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