blob: a73e7f96e351b4a5e2db3c68aaf5550644ee9286 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
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#include <boot_mode.h>
27#include <mpu.h>
28
29#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
30
31#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
32#define CACHE_SETUP 0x1a
33#else
34#define CACHE_SETUP 0x1e
35#endif
36
37DECLARE_GLOBAL_DATA_PTR;
38extern void invalidate_dcache_all(void);
39
40void __arm_init_before_mmu(void)
41{
42}
43void arm_init_before_mmu(void)
44 __attribute__((weak, alias("__arm_init_before_mmu")));
45
46static void cp_delay (void)
47{
48 volatile int i;
49
50 /* copro seems to need some delay between reading and writing */
51 for (i = 0; i < 100; i++)
52 nop();
53 asm volatile("" : : : "memory");
54}
55
56static inline void dram_bank_mmu_setup(int bank)
57{
58 u32 *page_table = (u32 *)gd->tlb_addr;
59 bd_t *bd = gd->bd;
60 int i;
61
62 debug("%s: bank: %d\n", __func__, bank);
63
64 for (i = bd->bi_dram[bank].start >> 20;
65 i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
66 i++)
67 {
68 page_table[i] = i << 20 | (3 << 10) |
69 (0 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1);
70 /* D C B */
71 }
72
73 /* for nand DMA addr ,no cache 21B00000--21C00000 */
74 i = CONFIG_NAND_DMA_BUF_ADDR >> 20;
75 page_table[i] = i << 20 | (3 << 10) |
76 (0 << 5) | (1 << 4) | (0 << 3) | (0 << 2) | (1 << 1);
77 /* D C B */
78 /* for USB addr ,no cache 22000000--24000000*/
79#if 0
80 for (i = CONFIG_USB_DMA_BUF_ADDR >> 20;i <= ((CONFIG_USB_DMA_BUF_ADDR+32*1024*1024) >> 20);i++)
81 {
82 page_table[i] = i << 20 | (3 << 10) |
83 (0 << 5) | (1 << 4) | (0 << 3) | (0 << 2) | (1 << 1);
84 /* D C B */
85 }
86#endif
87}
88
89
90/* to activate the MMU we need to set up virtual memory: use 1M areas */
91static inline void mmu_setup(void)
92{
93 u32 *page_table = (u32 *)gd->tlb_addr;
94 int i;
95 u32 reg;
96
97 arm_init_before_mmu();
98 /* Set up an identity-mapping for all 4GB, rw for everyone */
99
100 for (i = 0; i < 4096; i++)
101 page_table[i] = i << 20 | (3 << 10) |
102 (0 << 5) | (1 << 4) | (0 << 3) | (0 << 2) | (1 << 1);
103 /* D C B */
104 #if 1
105 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
106 dram_bank_mmu_setup(i);
107 }
108 #endif
109
110 /* Copy the page table address to cp15 */
111 asm volatile("mcr p15, 0, %0, c2, c0, 0"
112 : : "r" (page_table) : "memory");
113 /* Set the access control to all-supervisor */
114 asm volatile("mcr p15, 0, %0, c3, c0, 0"
115 : : "r" (~0));
116 /* and enable the mmu */
117 reg = get_cr(); /* get control reg. */
118 cp_delay();
119 set_cr(reg | CR_M);
120}
121
122static int mmu_enabled(void)
123{
124 return get_cr() & CR_M;
125}
126
127/* cache_bit must be either CR_I or CR_C */
128static void cache_enable(uint32_t cache_bit)
129{
130 uint32_t reg;
131
132 invalidate_dcache_all();
133 /* The data cache is not active unless the mmu is enabled too */
134 if ((cache_bit == CR_C) && !mmu_enabled())
135 {
136 mmu_setup();
137 }
138 reg = get_cr(); /* get control reg. */
139 cp_delay();
140 set_cr(reg | cache_bit);
141 return;
142}
143
144/* cache_bit must be either CR_I or CR_C */
145static void cache_disable(uint32_t cache_bit)
146{
147 uint32_t reg;
148
149 if (cache_bit == CR_C) {
150 /* if cache isn;t enabled no need to disable */
151 reg = get_cr();
152 if ((reg & CR_C) != CR_C)
153 {
154 return;
155 }
156
157 /* if disabling data cache, disable mmu too */
158 cache_bit |= CR_M;
159 flush_dcache_all();
160 }
161 reg = get_cr();
162 cp_delay();
163 set_cr(reg & ~cache_bit);
164}
165#endif
166
167#ifdef CONFIG_SYS_ICACHE_OFF
168void icache_enable (void)
169{
170 return;
171}
172
173void icache_disable (void)
174{
175 return;
176}
177
178int icache_status (void)
179{
180 return 0; /* always off */
181}
182#else
183void icache_enable(void)
184{
185 cache_enable(CR_I);
186}
187
188void icache_disable(void)
189{
190 cache_disable(CR_I);
191}
192
193int icache_status(void)
194{
195 return (get_cr() & CR_I) != 0;
196}
197#endif
198
199#ifdef CONFIG_SYS_DCACHE_OFF
200void dcache_enable (void)
201{
202 return;
203}
204
205void dcache_disable (void)
206{
207 return;
208}
209
210int dcache_status (void)
211{
212 return 0; /* always off */
213}
214#else
215void dcache_enable(void)
216{
217 cache_enable(CR_C);
218}
219
220void dcache_disable(void)
221{
222 cache_disable(CR_C);
223}
224
225int dcache_status(void)
226{
227 return (get_cr() & CR_C) != 0;
228}
229#endif