3 * David Feng <fenghua@phytium.com.cn>
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/system.h>
10 #include <asm/armv8/mmu.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 #ifndef CONFIG_SYS_DCACHE_OFF
16 static void set_pgtable_section(u64 section, u64 memory_type)
18 u64 *page_table = (u64 *)gd->arch.tlb_addr;
21 value = (section << SECTION_SHIFT) | PMD_TYPE_SECT | PMD_SECT_AF;
22 value |= PMD_ATTRINDX(memory_type);
23 page_table[section] = value;
26 /* to activate the MMU we need to set up virtual memory */
27 static void mmu_setup(void)
32 /* Setup an identity-mapping for all spaces */
33 for (i = 0; i < (PGTABLE_SIZE >> 3); i++)
34 set_pgtable_section(i, MT_DEVICE_NGNRNE);
36 /* Setup an identity-mapping for all RAM space */
37 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
38 ulong start = bd->bi_dram[i].start;
39 ulong end = bd->bi_dram[i].start + bd->bi_dram[i].size;
40 for (j = start >> SECTION_SHIFT;
41 j < end >> SECTION_SHIFT; j++) {
42 set_pgtable_section(j, MT_NORMAL);
49 asm volatile("msr ttbr0_el1, %0"
50 : : "r" (gd->arch.tlb_addr) : "memory");
52 asm volatile("msr ttbr0_el2, %0"
53 : : "r" (gd->arch.tlb_addr) : "memory");
55 asm volatile("msr ttbr0_el3, %0"
56 : : "r" (gd->arch.tlb_addr) : "memory");
59 set_sctlr(get_sctlr() | CR_M);
63 * Performs a invalidation of the entire data cache at all levels
65 void invalidate_dcache_all(void)
67 __asm_flush_dcache_all();
71 * Performs a clean & invalidation of the entire data cache at all levels
73 void flush_dcache_all(void)
75 __asm_flush_dcache_all();
79 * Invalidates range in all levels of D-cache/unified cache
81 void invalidate_dcache_range(unsigned long start, unsigned long stop)
83 __asm_flush_dcache_range(start, stop);
87 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
89 void flush_dcache_range(unsigned long start, unsigned long stop)
91 __asm_flush_dcache_range(start, stop);
94 void dcache_enable(void)
96 /* The data cache is not active unless the mmu is enabled */
97 if (!(get_sctlr() & CR_M)) {
98 invalidate_dcache_all();
99 __asm_invalidate_tlb_all();
103 set_sctlr(get_sctlr() | CR_C);
106 void dcache_disable(void)
112 /* if cache isn't enabled no need to disable */
116 set_sctlr(sctlr & ~(CR_C|CR_M));
119 __asm_invalidate_tlb_all();
122 int dcache_status(void)
124 return (get_sctlr() & CR_C) != 0;
127 #else /* CONFIG_SYS_DCACHE_OFF */
129 void invalidate_dcache_all(void)
133 void flush_dcache_all(void)
137 void invalidate_dcache_range(unsigned long start, unsigned long stop)
141 void flush_dcache_range(unsigned long start, unsigned long stop)
145 void dcache_enable(void)
149 void dcache_disable(void)
153 int dcache_status(void)
158 #endif /* CONFIG_SYS_DCACHE_OFF */
160 #ifndef CONFIG_SYS_ICACHE_OFF
162 void icache_enable(void)
164 set_sctlr(get_sctlr() | CR_I);
167 void icache_disable(void)
169 set_sctlr(get_sctlr() & ~CR_I);
172 int icache_status(void)
174 return (get_sctlr() & CR_I) != 0;
177 void invalidate_icache_all(void)
179 __asm_invalidate_icache_all();
182 #else /* CONFIG_SYS_ICACHE_OFF */
184 void icache_enable(void)
188 void icache_disable(void)
192 int icache_status(void)
197 void invalidate_icache_all(void)
201 #endif /* CONFIG_SYS_ICACHE_OFF */
204 * Enable dCache & iCache, whether cache is actually enabled
205 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
207 void enable_caches(void)
214 * Flush range from all levels of d-cache/unified-cache
216 void flush_cache(unsigned long start, unsigned long size)
218 flush_dcache_range(start, start + size);