blob: 6edf815d4d7b85f9b6197260b9b870acb01b586f [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>
26
Aneesh Vecee9c82011-06-16 23:30:48 +000027#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020028
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020029DECLARE_GLOBAL_DATA_PTR;
30
Aneesh V3e3bc1e2011-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-VILLARDe6b5f1b2009-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 Schocher69f1d0c2010-09-17 13:10:29 +020044 asm volatile("" : : : "memory");
45}
46
Simon Glassa4f20792012-10-17 13:24:53 +000047void set_section_dcache(int section, enum dcache_option option)
Heiko Schocheraeb29912010-09-17 13:10:39 +020048{
49 u32 *page_table = (u32 *)gd->tlb_addr;
Simon Glassa4f20792012-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 Schocheraeb29912010-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 Glassa4f20792012-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 Schocheraeb29912010-09-17 13:10:39 +020094 }
95}
Heiko Schocheraeb29912010-09-17 13:10:39 +020096
97/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020098static inline void mmu_setup(void)
99{
Heiko Schocheraeb29912010-09-17 13:10:39 +0200100 int i;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200101 u32 reg;
102
Aneesh V3e3bc1e2011-06-16 23:30:49 +0000103 arm_init_before_mmu();
Heiko Schocher69f1d0c2010-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 Glassa4f20792012-10-17 13:24:53 +0000106 set_section_dcache(i, DCACHE_OFF);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200107
Heiko Schocheraeb29912010-09-17 13:10:39 +0200108 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
109 dram_bank_mmu_setup(i);
110 }
Heiko Schocher69f1d0c2010-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 Glassa4f20792012-10-17 13:24:53 +0000114 : : "r" (gd->tlb_addr) : "memory");
Heiko Schocher69f1d0c2010-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-VILLARDe6b5f1b2009-04-05 13:06:31 +0200122}
123
Aneesh V3bda3772011-06-16 23:30:50 +0000124static int mmu_enabled(void)
125{
126 return get_cr() & CR_M;
127}
128
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-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 Schocher69f1d0c2010-09-17 13:10:29 +0200134 /* The data cache is not active unless the mmu is enabled too */
Aneesh V3bda3772011-06-16 23:30:50 +0000135 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200136 mmu_setup();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-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 R88f4bf22012-05-16 23:52:54 +0000147 reg = get_cr();
148 cp_delay();
149
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200150 if (cache_bit == CR_C) {
Heiko Schocheraeb29912010-09-17 13:10:39 +0200151 /* if cache isn;t enabled no need to disable */
Heiko Schocheraeb29912010-09-17 13:10:39 +0200152 if ((reg & CR_C) != CR_C)
153 return;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200154 /* if disabling data cache, disable mmu too */
155 cache_bit |= CR_M;
Aneesh V3bda3772011-06-16 23:30:50 +0000156 flush_dcache_all();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200157 }
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200158 set_cr(reg & ~cache_bit);
159}
160#endif
161
Aneesh Vecee9c82011-06-16 23:30:48 +0000162#ifdef CONFIG_SYS_ICACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200163void icache_enable (void)
164{
165 return;
166}
167
168void icache_disable (void)
169{
170 return;
171}
172
173int icache_status (void)
174{
175 return 0; /* always off */
176}
177#else
178void icache_enable(void)
179{
180 cache_enable(CR_I);
181}
182
183void icache_disable(void)
184{
185 cache_disable(CR_I);
186}
187
188int icache_status(void)
189{
190 return (get_cr() & CR_I) != 0;
191}
192#endif
193
Aneesh Vecee9c82011-06-16 23:30:48 +0000194#ifdef CONFIG_SYS_DCACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200195void dcache_enable (void)
196{
197 return;
198}
199
200void dcache_disable (void)
201{
202 return;
203}
204
205int dcache_status (void)
206{
207 return 0; /* always off */
208}
209#else
210void dcache_enable(void)
211{
212 cache_enable(CR_C);
213}
214
215void dcache_disable(void)
216{
217 cache_disable(CR_C);
218}
219
220int dcache_status(void)
221{
222 return (get_cr() & CR_C) != 0;
223}
224#endif