]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/lib/cache-cp15.c
Merge branch 'karo-tx-uboot' into kc-merge
[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         asm volatile(
90                 /* Copy the page table address to cp15 */
91                 "mcr p15, 0, %0, c2, c0, 0\n"
92                 /* Set the access control to all-supervisor */
93                 "mcr p15, 0, %1, c3, c0, 0\n"
94                 :
95                 : "r"(gd->arch.tlb_addr), "r"(~0)
96                 : "memory"
97                 );
98
99         arm_init_domains();
100
101         /* and enable the mmu */
102         reg = get_cr(); /* get control reg. */
103         set_cr(reg | CR_M);
104 }
105
106 static int mmu_enabled(void)
107 {
108         return get_cr() & CR_M;
109 }
110
111 /* cache_bit must be either CR_I or CR_C */
112 static void cache_enable(uint32_t cache_bit)
113 {
114         uint32_t reg;
115
116         /* The data cache is not active unless the mmu is enabled too */
117         if ((cache_bit == CR_C) && !mmu_enabled())
118                 mmu_setup();
119         reg = get_cr(); /* get control reg. */
120         set_cr(reg | cache_bit);
121 }
122
123 /* cache_bit must be either CR_I or CR_C */
124 static void cache_disable(uint32_t cache_bit)
125 {
126         uint32_t reg;
127
128         reg = get_cr();
129
130         if (cache_bit == CR_C) {
131                 /* if cache isn;t enabled no need to disable */
132                 if ((reg & CR_C) != CR_C)
133                         return;
134                 /* if disabling data cache, disable mmu too */
135                 cache_bit |= CR_M;
136         }
137         reg = get_cr();
138         if (cache_bit == (CR_C | CR_M))
139                 flush_dcache_all();
140         set_cr(reg & ~cache_bit);
141 }
142 #endif
143
144 #ifdef CONFIG_SYS_ICACHE_OFF
145 void icache_enable (void)
146 {
147         return;
148 }
149
150 void icache_disable (void)
151 {
152         return;
153 }
154
155 int icache_status (void)
156 {
157         return 0;                                       /* always off */
158 }
159 #else
160 void icache_enable(void)
161 {
162         cache_enable(CR_I);
163 }
164
165 void icache_disable(void)
166 {
167         cache_disable(CR_I);
168 }
169
170 int icache_status(void)
171 {
172         return (get_cr() & CR_I) != 0;
173 }
174 #endif
175
176 #ifdef CONFIG_SYS_DCACHE_OFF
177 void dcache_enable (void)
178 {
179         return;
180 }
181
182 void dcache_disable (void)
183 {
184         return;
185 }
186
187 int dcache_status (void)
188 {
189         return 0;                                       /* always off */
190 }
191 #else
192 void dcache_enable(void)
193 {
194         cache_enable(CR_C);
195 }
196
197 void dcache_disable(void)
198 {
199         cache_disable(CR_C);
200 }
201
202 int dcache_status(void)
203 {
204         return (get_cr() & CR_C) != 0;
205 }
206 #endif