blob: 4abe1cf061a879d65170ad8c6697d9c9e1954751 [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <common.h>
25#include <asm/system.h>
R Sricharan08716072013-03-04 20:04:44 +000026#include <asm/cache.h>
27#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020028
Aneesh Vecee9c82011-06-16 23:30:48 +000029#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020030
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020031DECLARE_GLOBAL_DATA_PTR;
32
Aneesh V3e3bc1e2011-06-16 23:30:49 +000033void __arm_init_before_mmu(void)
34{
35}
36void arm_init_before_mmu(void)
37 __attribute__((weak, alias("__arm_init_before_mmu")));
38
R Sricharan06396c12013-03-04 20:04:45 +000039__weak void arm_init_domains(void)
40{
41}
42
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020043static void cp_delay (void)
44{
45 volatile int i;
46
47 /* copro seems to need some delay between reading and writing */
48 for (i = 0; i < 100; i++)
49 nop();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020050 asm volatile("" : : : "memory");
51}
52
Simon Glassa4f20792012-10-17 13:24:53 +000053void set_section_dcache(int section, enum dcache_option option)
Heiko Schocheraeb29912010-09-17 13:10:39 +020054{
Simon Glass6b4ee152012-12-13 20:48:39 +000055 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Simon Glassa4f20792012-10-17 13:24:53 +000056 u32 value;
57
58 value = (section << MMU_SECTION_SHIFT) | (3 << 10);
59 value |= option;
60 page_table[section] = value;
61}
62
63void __mmu_page_table_flush(unsigned long start, unsigned long stop)
64{
65 debug("%s: Warning: not implemented\n", __func__);
66}
67
68void mmu_page_table_flush(unsigned long start, unsigned long stop)
69 __attribute__((weak, alias("__mmu_page_table_flush")));
70
71void mmu_set_region_dcache_behaviour(u32 start, int size,
72 enum dcache_option option)
73{
Simon Glass6b4ee152012-12-13 20:48:39 +000074 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Simon Glassa4f20792012-10-17 13:24:53 +000075 u32 upto, end;
76
77 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
78 start = start >> MMU_SECTION_SHIFT;
79 debug("%s: start=%x, size=%x, option=%d\n", __func__, start, size,
80 option);
81 for (upto = start; upto < end; upto++)
82 set_section_dcache(upto, option);
83 mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]);
84}
85
R Sricharan08716072013-03-04 20:04:44 +000086__weak void dram_bank_mmu_setup(int bank)
Simon Glassa4f20792012-10-17 13:24:53 +000087{
Heiko Schocheraeb29912010-09-17 13:10:39 +020088 bd_t *bd = gd->bd;
89 int i;
90
91 debug("%s: bank: %d\n", __func__, bank);
92 for (i = bd->bi_dram[bank].start >> 20;
93 i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
94 i++) {
Simon Glassa4f20792012-10-17 13:24:53 +000095#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
96 set_section_dcache(i, DCACHE_WRITETHROUGH);
97#else
98 set_section_dcache(i, DCACHE_WRITEBACK);
99#endif
Heiko Schocheraeb29912010-09-17 13:10:39 +0200100 }
101}
Heiko Schocheraeb29912010-09-17 13:10:39 +0200102
103/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200104static inline void mmu_setup(void)
105{
Heiko Schocheraeb29912010-09-17 13:10:39 +0200106 int i;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200107 u32 reg;
108
Aneesh V3e3bc1e2011-06-16 23:30:49 +0000109 arm_init_before_mmu();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200110 /* Set up an identity-mapping for all 4GB, rw for everyone */
111 for (i = 0; i < 4096; i++)
Simon Glassa4f20792012-10-17 13:24:53 +0000112 set_section_dcache(i, DCACHE_OFF);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200113
Heiko Schocheraeb29912010-09-17 13:10:39 +0200114 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
115 dram_bank_mmu_setup(i);
116 }
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200117
118 /* Copy the page table address to cp15 */
119 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass6b4ee152012-12-13 20:48:39 +0000120 : : "r" (gd->arch.tlb_addr) : "memory");
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200121 /* Set the access control to all-supervisor */
122 asm volatile("mcr p15, 0, %0, c3, c0, 0"
123 : : "r" (~0));
R Sricharan06396c12013-03-04 20:04:45 +0000124
125 arm_init_domains();
126
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200127 /* and enable the mmu */
128 reg = get_cr(); /* get control reg. */
129 cp_delay();
130 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200131}
132
Aneesh V3bda3772011-06-16 23:30:50 +0000133static int mmu_enabled(void)
134{
135 return get_cr() & CR_M;
136}
137
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200138/* cache_bit must be either CR_I or CR_C */
139static void cache_enable(uint32_t cache_bit)
140{
141 uint32_t reg;
142
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200143 /* The data cache is not active unless the mmu is enabled too */
Aneesh V3bda3772011-06-16 23:30:50 +0000144 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200145 mmu_setup();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200146 reg = get_cr(); /* get control reg. */
147 cp_delay();
148 set_cr(reg | cache_bit);
149}
150
151/* cache_bit must be either CR_I or CR_C */
152static void cache_disable(uint32_t cache_bit)
153{
154 uint32_t reg;
155
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000156 reg = get_cr();
157 cp_delay();
158
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200159 if (cache_bit == CR_C) {
Heiko Schocheraeb29912010-09-17 13:10:39 +0200160 /* if cache isn;t enabled no need to disable */
Heiko Schocheraeb29912010-09-17 13:10:39 +0200161 if ((reg & CR_C) != CR_C)
162 return;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200163 /* if disabling data cache, disable mmu too */
164 cache_bit |= CR_M;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200165 }
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000166 reg = get_cr();
167 cp_delay();
168 if (cache_bit == (CR_C | CR_M))
169 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200170 set_cr(reg & ~cache_bit);
171}
172#endif
173
Aneesh Vecee9c82011-06-16 23:30:48 +0000174#ifdef CONFIG_SYS_ICACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200175void icache_enable (void)
176{
177 return;
178}
179
180void icache_disable (void)
181{
182 return;
183}
184
185int icache_status (void)
186{
187 return 0; /* always off */
188}
189#else
190void icache_enable(void)
191{
192 cache_enable(CR_I);
193}
194
195void icache_disable(void)
196{
197 cache_disable(CR_I);
198}
199
200int icache_status(void)
201{
202 return (get_cr() & CR_I) != 0;
203}
204#endif
205
Aneesh Vecee9c82011-06-16 23:30:48 +0000206#ifdef CONFIG_SYS_DCACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200207void dcache_enable (void)
208{
209 return;
210}
211
212void dcache_disable (void)
213{
214 return;
215}
216
217int dcache_status (void)
218{
219 return 0; /* always off */
220}
221#else
222void dcache_enable(void)
223{
224 cache_enable(CR_C);
225}
226
227void dcache_disable(void)
228{
229 cache_disable(CR_C);
230}
231
232int dcache_status(void)
233{
234 return (get_cr() & CR_C) != 0;
235}
236#endif