blob: 2436414ad9d84d3baa2db7dbb4c358444ef2e387 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <asm/system.h>
10#include <asm/cache.h>
11#include <linux/compiler.h>
12
13#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
14
15DECLARE_GLOBAL_DATA_PTR;
16
17void __arm_init_before_mmu(void)
18{
19}
20void arm_init_before_mmu(void)
21 __attribute__((weak, alias("__arm_init_before_mmu")));
22
23__weak void arm_init_domains(void)
24{
25}
26
27static void cp_delay (void)
28{
29 volatile int i;
30
31 /* copro seems to need some delay between reading and writing */
32 for (i = 0; i < 100; i++)
33 nop();
34 asm volatile("" : : : "memory");
35}
36
37void set_section_dcache(int section, enum dcache_option option)
38{
39 u32 *page_table = (u32 *)gd->arch.tlb_addr;
40 u32 value;
41
42 value = (section << MMU_SECTION_SHIFT) | (3 << 10);
43 value |= option;
44 page_table[section] = value;
45}
46
47void __mmu_page_table_flush(unsigned long start, unsigned long stop)
48{
49 debug("%s: Warning: not implemented\n", __func__);
50}
51
52void mmu_page_table_flush(unsigned long start, unsigned long stop)
53 __attribute__((weak, alias("__mmu_page_table_flush")));
54
55void mmu_set_region_dcache_behaviour(u32 start, int size,
56 enum dcache_option option)
57{
58 u32 *page_table = (u32 *)gd->arch.tlb_addr;
59 u32 upto, end;
60
61 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
62 start = start >> MMU_SECTION_SHIFT;
63 debug("%s: start=%x, size=%x, option=%d\n", __func__, start, size,
64 option);
65 for (upto = start; upto < end; upto++)
66 set_section_dcache(upto, option);
67 mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]);
68}
69
70__weak void dram_bank_mmu_setup(int bank)
71{
72 bd_t *bd = gd->bd;
73 int i, size;
74
75 debug("%s: bank: %d\n", __func__, bank);
76
77#if defined(CONFIG_ASR1802S) || defined(CONFIG_PXA182X)
78 /* force to use 128MB for cpload if DDR < 128MB */
79 if (bd->bi_dram[bank].size < (0x100000 * 128))
80 size = (0x100000 * 128);
81 else
82 size = bd->bi_dram[bank].size;
83
84 for (i = bd->bi_dram[bank].start >> 20;
85 i < (bd->bi_dram[bank].start + size) >> 20;
86 i++) {
87#else
88 for (i = bd->bi_dram[bank].start >> 20;
89 i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
90 i++) {
91#endif
92#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
93 set_section_dcache(i, DCACHE_WRITETHROUGH);
94#else
95 set_section_dcache(i, DCACHE_WRITEBACK);
96#endif
97 }
98}
99
100/* to activate the MMU we need to set up virtual memory: use 1M areas */
101static inline void mmu_setup(void)
102{
103 int i;
104 u32 reg;
105
106 arm_init_before_mmu();
107 /* Set up an identity-mapping for all 4GB, rw for everyone */
108 for (i = 0; i < 4096; i++)
109 set_section_dcache(i, DCACHE_OFF);
110
111 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
112 dram_bank_mmu_setup(i);
113 }
114
115 /* Copy the page table address to cp15 */
116 asm volatile("mcr p15, 0, %0, c2, c0, 0"
117 : : "r" (gd->arch.tlb_addr) : "memory");
118 /* Set the access control to all-supervisor */
119 asm volatile("mcr p15, 0, %0, c3, c0, 0"
120 : : "r" (~0));
121
122 arm_init_domains();
123
124 /* and enable the mmu */
125 reg = get_cr(); /* get control reg. */
126 cp_delay();
127 set_cr(reg | CR_M);
128}
129
130static int mmu_enabled(void)
131{
132 return get_cr() & CR_M;
133}
134
135/* cache_bit must be either CR_I or CR_C */
136static void cache_enable(uint32_t cache_bit)
137{
138 uint32_t reg;
139
140 /* The data cache is not active unless the mmu is enabled too */
141 if ((cache_bit == CR_C) && !mmu_enabled())
142 mmu_setup();
143 reg = get_cr(); /* get control reg. */
144 cp_delay();
145 set_cr(reg | cache_bit);
146}
147
148/* cache_bit must be either CR_I or CR_C */
149static void cache_disable(uint32_t cache_bit)
150{
151 uint32_t reg;
152
153 reg = get_cr();
154 cp_delay();
155
156 if (cache_bit == CR_C) {
157 /* if cache isn;t enabled no need to disable */
158 if ((reg & CR_C) != CR_C)
159 return;
160 /* if disabling data cache, disable mmu too */
161 cache_bit |= CR_M;
162 }
163 reg = get_cr();
164 cp_delay();
165 if (cache_bit == (CR_C | CR_M))
166 flush_dcache_all();
167 set_cr(reg & ~cache_bit);
168}
169#endif
170
171#ifdef CONFIG_SYS_ICACHE_OFF
172void icache_enable (void)
173{
174 return;
175}
176
177void icache_disable (void)
178{
179 return;
180}
181
182int icache_status (void)
183{
184 return 0; /* always off */
185}
186#else
187void icache_enable(void)
188{
189 cache_enable(CR_I);
190}
191
192void icache_disable(void)
193{
194 cache_disable(CR_I);
195}
196
197int icache_status(void)
198{
199 return (get_cr() & CR_I) != 0;
200}
201#endif
202
203#ifdef CONFIG_SYS_DCACHE_OFF
204void dcache_enable (void)
205{
206 return;
207}
208
209void dcache_disable (void)
210{
211 return;
212}
213
214int dcache_status (void)
215{
216 return 0; /* always off */
217}
218#else
219void dcache_enable(void)
220{
221 cache_enable(CR_C);
222}
223
224void dcache_disable(void)
225{
226 cache_disable(CR_C);
227}
228
229int dcache_status(void)
230{
231 return (get_cr() & CR_C) != 0;
232}
233#endif