blob: af3c4945ecbd532f51b79df09d244181d64d6a9a [file] [log] [blame]
David Feng0ae76532013-12-14 11:47:35 +08001/*
2 * (C) Copyright 2013
3 * David Feng <fenghua@phytium.com.cn>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <asm/system.h>
10#include <asm/armv8/mmu.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
14#ifndef CONFIG_SYS_DCACHE_OFF
York Sun22932ff2014-06-23 15:15:53 -070015void set_pgtable_section(u64 *page_table, u64 index, u64 section,
16 u64 memory_type)
David Feng0ae76532013-12-14 11:47:35 +080017{
David Feng0ae76532013-12-14 11:47:35 +080018 u64 value;
19
York Sun22932ff2014-06-23 15:15:53 -070020 value = section | PMD_TYPE_SECT | PMD_SECT_AF;
David Feng0ae76532013-12-14 11:47:35 +080021 value |= PMD_ATTRINDX(memory_type);
York Sun22932ff2014-06-23 15:15:53 -070022 page_table[index] = value;
David Feng0ae76532013-12-14 11:47:35 +080023}
24
25/* to activate the MMU we need to set up virtual memory */
26static void mmu_setup(void)
27{
28 int i, j, el;
29 bd_t *bd = gd->bd;
York Sun22932ff2014-06-23 15:15:53 -070030 u64 *page_table = (u64 *)gd->arch.tlb_addr;
David Feng0ae76532013-12-14 11:47:35 +080031
32 /* Setup an identity-mapping for all spaces */
York Sun22932ff2014-06-23 15:15:53 -070033 for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
34 set_pgtable_section(page_table, i, i << SECTION_SHIFT,
35 MT_DEVICE_NGNRNE);
36 }
David Feng0ae76532013-12-14 11:47:35 +080037
38 /* Setup an identity-mapping for all RAM space */
39 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
40 ulong start = bd->bi_dram[i].start;
41 ulong end = bd->bi_dram[i].start + bd->bi_dram[i].size;
42 for (j = start >> SECTION_SHIFT;
43 j < end >> SECTION_SHIFT; j++) {
York Sun22932ff2014-06-23 15:15:53 -070044 set_pgtable_section(page_table, j, j << SECTION_SHIFT,
45 MT_NORMAL);
David Feng0ae76532013-12-14 11:47:35 +080046 }
47 }
48
49 /* load TTBR0 */
50 el = current_el();
York Sunf5222cf2014-02-26 13:26:02 -080051 if (el == 1) {
York Sun22932ff2014-06-23 15:15:53 -070052 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
53 TCR_FLAGS | TCR_EL1_IPS_BITS,
54 MEMORY_ATTRIBUTES);
York Sunf5222cf2014-02-26 13:26:02 -080055 } else if (el == 2) {
York Sun22932ff2014-06-23 15:15:53 -070056 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
57 TCR_FLAGS | TCR_EL2_IPS_BITS,
58 MEMORY_ATTRIBUTES);
York Sunf5222cf2014-02-26 13:26:02 -080059 } else {
York Sun22932ff2014-06-23 15:15:53 -070060 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
61 TCR_FLAGS | TCR_EL3_IPS_BITS,
62 MEMORY_ATTRIBUTES);
York Sunf5222cf2014-02-26 13:26:02 -080063 }
David Feng0ae76532013-12-14 11:47:35 +080064 /* enable the mmu */
65 set_sctlr(get_sctlr() | CR_M);
66}
67
68/*
69 * Performs a invalidation of the entire data cache at all levels
70 */
71void invalidate_dcache_all(void)
72{
York Sun1e6ad552014-02-26 13:26:04 -080073 __asm_invalidate_dcache_all();
David Feng0ae76532013-12-14 11:47:35 +080074}
75
76/*
77 * Performs a clean & invalidation of the entire data cache at all levels
78 */
79void flush_dcache_all(void)
80{
81 __asm_flush_dcache_all();
82}
83
84/*
85 * Invalidates range in all levels of D-cache/unified cache
86 */
87void invalidate_dcache_range(unsigned long start, unsigned long stop)
88{
89 __asm_flush_dcache_range(start, stop);
90}
91
92/*
93 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
94 */
95void flush_dcache_range(unsigned long start, unsigned long stop)
96{
97 __asm_flush_dcache_range(start, stop);
98}
99
100void dcache_enable(void)
101{
102 /* The data cache is not active unless the mmu is enabled */
103 if (!(get_sctlr() & CR_M)) {
104 invalidate_dcache_all();
105 __asm_invalidate_tlb_all();
106 mmu_setup();
107 }
108
109 set_sctlr(get_sctlr() | CR_C);
110}
111
112void dcache_disable(void)
113{
114 uint32_t sctlr;
115
116 sctlr = get_sctlr();
117
118 /* if cache isn't enabled no need to disable */
119 if (!(sctlr & CR_C))
120 return;
121
122 set_sctlr(sctlr & ~(CR_C|CR_M));
123
124 flush_dcache_all();
125 __asm_invalidate_tlb_all();
126}
127
128int dcache_status(void)
129{
130 return (get_sctlr() & CR_C) != 0;
131}
132
133#else /* CONFIG_SYS_DCACHE_OFF */
134
135void invalidate_dcache_all(void)
136{
137}
138
139void flush_dcache_all(void)
140{
141}
142
143void invalidate_dcache_range(unsigned long start, unsigned long stop)
144{
145}
146
147void flush_dcache_range(unsigned long start, unsigned long stop)
148{
149}
150
151void dcache_enable(void)
152{
153}
154
155void dcache_disable(void)
156{
157}
158
159int dcache_status(void)
160{
161 return 0;
162}
163
164#endif /* CONFIG_SYS_DCACHE_OFF */
165
166#ifndef CONFIG_SYS_ICACHE_OFF
167
168void icache_enable(void)
169{
York Sun1e6ad552014-02-26 13:26:04 -0800170 __asm_invalidate_icache_all();
David Feng0ae76532013-12-14 11:47:35 +0800171 set_sctlr(get_sctlr() | CR_I);
172}
173
174void icache_disable(void)
175{
176 set_sctlr(get_sctlr() & ~CR_I);
177}
178
179int icache_status(void)
180{
181 return (get_sctlr() & CR_I) != 0;
182}
183
184void invalidate_icache_all(void)
185{
186 __asm_invalidate_icache_all();
187}
188
189#else /* CONFIG_SYS_ICACHE_OFF */
190
191void icache_enable(void)
192{
193}
194
195void icache_disable(void)
196{
197}
198
199int icache_status(void)
200{
201 return 0;
202}
203
204void invalidate_icache_all(void)
205{
206}
207
208#endif /* CONFIG_SYS_ICACHE_OFF */
209
210/*
211 * Enable dCache & iCache, whether cache is actually enabled
212 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
213 */
214void enable_caches(void)
215{
216 icache_enable();
217 dcache_enable();
218}
219
220/*
221 * Flush range from all levels of d-cache/unified-cache
222 */
223void flush_cache(unsigned long start, unsigned long size)
224{
225 flush_dcache_range(start, start + size);
226}