]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/lib/cache-cp15.c
arm: remove bogus cp_delay() function
[karo-tx-uboot.git] / arch / arm / lib / cache-cp15.c
1 /*
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
15 DECLARE_GLOBAL_DATA_PTR;
16
17 __weak void arm_init_before_mmu(void)
18 {
19 }
20
21 __weak void arm_init_domains(void)
22 {
23 }
24
25 void set_section_dcache(int section, enum dcache_option option)
26 {
27         u32 *page_table = (u32 *)gd->arch.tlb_addr;
28         u32 value;
29
30         value = (section << MMU_SECTION_SHIFT) | (3 << 10);
31         value |= option;
32         page_table[section] = value;
33 }
34
35 __weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
36 {
37         debug("%s: Warning: not implemented\n", __func__);
38 }
39
40 void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
41                                      enum dcache_option option)
42 {
43         u32 *page_table = (u32 *)gd->arch.tlb_addr;
44         unsigned long upto, end;
45
46         end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
47         start = start >> MMU_SECTION_SHIFT;
48         debug("%s: start=%pa, size=%zu, option=%d\n", __func__, &start, size,
49               option);
50         for (upto = start; upto < end; upto++)
51                 set_section_dcache(upto, option);
52         mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]);
53 }
54
55 __weak void dram_bank_mmu_setup(int bank)
56 {
57         bd_t *bd = gd->bd;
58         int     i;
59
60         debug("%s: bank: %d\n", __func__, bank);
61         for (i = bd->bi_dram[bank].start >> 20;
62              i < (bd->bi_dram[bank].start >> 20) + (bd->bi_dram[bank].size >> 20);
63              i++) {
64 #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
65                 set_section_dcache(i, DCACHE_WRITETHROUGH);
66 #elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
67                 set_section_dcache(i, DCACHE_WRITEALLOC);
68 #else
69                 set_section_dcache(i, DCACHE_WRITEBACK);
70 #endif
71         }
72 }
73
74 /* to activate the MMU we need to set up virtual memory: use 1M areas */
75 static inline void mmu_setup(void)
76 {
77         int i;
78         u32 reg;
79
80         arm_init_before_mmu();
81         /* Set up an identity-mapping for all 4GB, rw for everyone */
82         for (i = 0; i < 4096; i++)
83                 set_section_dcache(i, DCACHE_OFF);
84
85         for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
86                 dram_bank_mmu_setup(i);
87         }
88
89 #ifdef CONFIG_ARMV7
90         /* Set TTBR0 */
91         reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
92 #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
93         reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
94 #elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
95         reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
96 #else
97         reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
98 #endif
99         asm volatile("mcr p15, 0, %0, c2, c0, 0"
100                      : : "r" (reg) : "memory");
101 #else
102         /* Copy the page table address to cp15 */
103         asm volatile("mcr p15, 0, %0, c2, c0, 0"
104                      : : "r" (gd->arch.tlb_addr) : "memory");
105 #endif
106         /* Set the access control to all-supervisor */
107         asm volatile("mcr p15, 0, %0, c3, c0, 0"
108                      : : "r" (~0));
109
110         arm_init_domains();
111
112         /* and enable the mmu */
113         reg = get_cr(); /* get control reg. */
114         set_cr(reg | CR_M);
115 }
116
117 static int mmu_enabled(void)
118 {
119         return get_cr() & CR_M;
120 }
121
122 /* cache_bit must be either CR_I or CR_C */
123 static void cache_enable(uint32_t cache_bit)
124 {
125         uint32_t reg;
126
127         /* The data cache is not active unless the mmu is enabled too */
128         if ((cache_bit == CR_C) && !mmu_enabled())
129                 mmu_setup();
130         reg = get_cr(); /* get control reg. */
131         set_cr(reg | cache_bit);
132 }
133
134 /* cache_bit must be either CR_I or CR_C */
135 static void cache_disable(uint32_t cache_bit)
136 {
137         uint32_t reg;
138
139         reg = get_cr();
140
141         if (cache_bit == CR_C) {
142                 /* if cache isn;t enabled no need to disable */
143                 if ((reg & CR_C) != CR_C)
144                         return;
145                 /* if disabling data cache, disable mmu too */
146                 cache_bit |= CR_M;
147         }
148         reg = get_cr();
149         if (cache_bit == (CR_C | CR_M))
150                 flush_dcache_all();
151         set_cr(reg & ~cache_bit);
152 }
153 #endif
154
155 #ifdef CONFIG_SYS_ICACHE_OFF
156 void icache_enable (void)
157 {
158         return;
159 }
160
161 void icache_disable (void)
162 {
163         return;
164 }
165
166 int icache_status (void)
167 {
168         return 0;                                       /* always off */
169 }
170 #else
171 void icache_enable(void)
172 {
173         cache_enable(CR_I);
174 }
175
176 void icache_disable(void)
177 {
178         cache_disable(CR_I);
179 }
180
181 int icache_status(void)
182 {
183         return (get_cr() & CR_I) != 0;
184 }
185 #endif
186
187 #ifdef CONFIG_SYS_DCACHE_OFF
188 void dcache_enable (void)
189 {
190         return;
191 }
192
193 void dcache_disable (void)
194 {
195         return;
196 }
197
198 int dcache_status (void)
199 {
200         return 0;                                       /* always off */
201 }
202 #else
203 void dcache_enable(void)
204 {
205         cache_enable(CR_C);
206 }
207
208 void dcache_disable(void)
209 {
210         cache_disable(CR_C);
211 }
212
213 int dcache_status(void)
214 {
215         return (get_cr() & CR_C) != 0;
216 }
217 #endif