blob: 1cab27c22629fe5bd034d8278efa8d09e36f7378 [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-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>
26
Aneesh Ve47f2db2011-06-16 23:30:48 +000027#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
Heiko Schocher880eff52010-09-17 13:10:29 +020028
Heiko Schocher880eff52010-09-17 13:10:29 +020029DECLARE_GLOBAL_DATA_PTR;
30
Aneesh Vc2dd0d42011-06-16 23:30:49 +000031void __arm_init_before_mmu(void)
32{
33}
34void arm_init_before_mmu(void)
35 __attribute__((weak, alias("__arm_init_before_mmu")));
36
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +020037static void cp_delay (void)
38{
39 volatile int i;
40
41 /* copro seems to need some delay between reading and writing */
42 for (i = 0; i < 100; i++)
43 nop();
Heiko Schocher880eff52010-09-17 13:10:29 +020044 asm volatile("" : : : "memory");
45}
46
Simon Glass0dde7f52012-10-17 13:24:53 +000047void set_section_dcache(int section, enum dcache_option option)
Heiko Schocherf1d2b312010-09-17 13:10:39 +020048{
49 u32 *page_table = (u32 *)gd->tlb_addr;
Simon Glass0dde7f52012-10-17 13:24:53 +000050 u32 value;
51
52 value = (section << MMU_SECTION_SHIFT) | (3 << 10);
53 value |= option;
54 page_table[section] = value;
55}
56
57void __mmu_page_table_flush(unsigned long start, unsigned long stop)
58{
59 debug("%s: Warning: not implemented\n", __func__);
60}
61
62void mmu_page_table_flush(unsigned long start, unsigned long stop)
63 __attribute__((weak, alias("__mmu_page_table_flush")));
64
65void mmu_set_region_dcache_behaviour(u32 start, int size,
66 enum dcache_option option)
67{
68 u32 *page_table = (u32 *)gd->tlb_addr;
69 u32 upto, end;
70
71 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
72 start = start >> MMU_SECTION_SHIFT;
73 debug("%s: start=%x, size=%x, option=%d\n", __func__, start, size,
74 option);
75 for (upto = start; upto < end; upto++)
76 set_section_dcache(upto, option);
77 mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]);
78}
79
80static inline void dram_bank_mmu_setup(int bank)
81{
Heiko Schocherf1d2b312010-09-17 13:10:39 +020082 bd_t *bd = gd->bd;
83 int i;
84
85 debug("%s: bank: %d\n", __func__, bank);
86 for (i = bd->bi_dram[bank].start >> 20;
87 i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
88 i++) {
Simon Glass0dde7f52012-10-17 13:24:53 +000089#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
90 set_section_dcache(i, DCACHE_WRITETHROUGH);
91#else
92 set_section_dcache(i, DCACHE_WRITEBACK);
93#endif
Heiko Schocherf1d2b312010-09-17 13:10:39 +020094 }
95}
Heiko Schocherf1d2b312010-09-17 13:10:39 +020096
97/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher880eff52010-09-17 13:10:29 +020098static inline void mmu_setup(void)
99{
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200100 int i;
Heiko Schocher880eff52010-09-17 13:10:29 +0200101 u32 reg;
102
Aneesh Vc2dd0d42011-06-16 23:30:49 +0000103 arm_init_before_mmu();
Heiko Schocher880eff52010-09-17 13:10:29 +0200104 /* Set up an identity-mapping for all 4GB, rw for everyone */
105 for (i = 0; i < 4096; i++)
Simon Glass0dde7f52012-10-17 13:24:53 +0000106 set_section_dcache(i, DCACHE_OFF);
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200107
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200108 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
109 dram_bank_mmu_setup(i);
110 }
Heiko Schocher880eff52010-09-17 13:10:29 +0200111
112 /* Copy the page table address to cp15 */
113 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass0dde7f52012-10-17 13:24:53 +0000114 : : "r" (gd->tlb_addr) : "memory");
Heiko Schocher880eff52010-09-17 13:10:29 +0200115 /* Set the access control to all-supervisor */
116 asm volatile("mcr p15, 0, %0, c3, c0, 0"
117 : : "r" (~0));
118 /* and enable the mmu */
119 reg = get_cr(); /* get control reg. */
120 cp_delay();
121 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200122}
123
Aneesh Ve05f0072011-06-16 23:30:50 +0000124static int mmu_enabled(void)
125{
126 return get_cr() & CR_M;
127}
128
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200129/* cache_bit must be either CR_I or CR_C */
130static void cache_enable(uint32_t cache_bit)
131{
132 uint32_t reg;
133
Heiko Schocher880eff52010-09-17 13:10:29 +0200134 /* The data cache is not active unless the mmu is enabled too */
Aneesh Ve05f0072011-06-16 23:30:50 +0000135 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher880eff52010-09-17 13:10:29 +0200136 mmu_setup();
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200137 reg = get_cr(); /* get control reg. */
138 cp_delay();
139 set_cr(reg | cache_bit);
140}
141
142/* cache_bit must be either CR_I or CR_C */
143static void cache_disable(uint32_t cache_bit)
144{
145 uint32_t reg;
146
SRICHARAN Rd702b082012-05-16 23:52:54 +0000147 reg = get_cr();
148 cp_delay();
149
Heiko Schocher880eff52010-09-17 13:10:29 +0200150 if (cache_bit == CR_C) {
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200151 /* if cache isn;t enabled no need to disable */
Heiko Schocherf1d2b312010-09-17 13:10:39 +0200152 if ((reg & CR_C) != CR_C)
153 return;
Heiko Schocher880eff52010-09-17 13:10:29 +0200154 /* if disabling data cache, disable mmu too */
155 cache_bit |= CR_M;
Heiko Schocher880eff52010-09-17 13:10:29 +0200156 }
Arun Mankuzhi44df5e82012-11-30 13:01:14 +0000157 reg = get_cr();
158 cp_delay();
159 if (cache_bit == (CR_C | CR_M))
160 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200161 set_cr(reg & ~cache_bit);
162}
163#endif
164
Aneesh Ve47f2db2011-06-16 23:30:48 +0000165#ifdef CONFIG_SYS_ICACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200166void icache_enable (void)
167{
168 return;
169}
170
171void icache_disable (void)
172{
173 return;
174}
175
176int icache_status (void)
177{
178 return 0; /* always off */
179}
180#else
181void icache_enable(void)
182{
183 cache_enable(CR_I);
184}
185
186void icache_disable(void)
187{
188 cache_disable(CR_I);
189}
190
191int icache_status(void)
192{
193 return (get_cr() & CR_I) != 0;
194}
195#endif
196
Aneesh Ve47f2db2011-06-16 23:30:48 +0000197#ifdef CONFIG_SYS_DCACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDb3acb6c2009-04-05 13:06:31 +0200198void dcache_enable (void)
199{
200 return;
201}
202
203void dcache_disable (void)
204{
205 return;
206}
207
208int dcache_status (void)
209{
210 return 0; /* always off */
211}
212#else
213void dcache_enable(void)
214{
215 cache_enable(CR_C);
216}
217
218void dcache_disable(void)
219{
220 cache_disable(CR_C);
221}
222
223int dcache_status(void)
224{
225 return (get_cr() & CR_C) != 0;
226}
227#endif