Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 1 | /* |
| 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 V | ecee9c8 | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 27 | #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 28 | |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Aneesh V | 3e3bc1e | 2011-06-16 23:30:49 +0000 | [diff] [blame] | 31 | void __arm_init_before_mmu(void) |
| 32 | { |
| 33 | } |
| 34 | void arm_init_before_mmu(void) |
| 35 | __attribute__((weak, alias("__arm_init_before_mmu"))); |
| 36 | |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 37 | static 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 Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 44 | asm volatile("" : : : "memory"); |
| 45 | } |
| 46 | |
Simon Glass | a4f2079 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 47 | void set_section_dcache(int section, enum dcache_option option) |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 48 | { |
| 49 | u32 *page_table = (u32 *)gd->tlb_addr; |
Simon Glass | a4f2079 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 50 | u32 value; |
| 51 | |
| 52 | value = (section << MMU_SECTION_SHIFT) | (3 << 10); |
| 53 | value |= option; |
| 54 | page_table[section] = value; |
| 55 | } |
| 56 | |
| 57 | void __mmu_page_table_flush(unsigned long start, unsigned long stop) |
| 58 | { |
| 59 | debug("%s: Warning: not implemented\n", __func__); |
| 60 | } |
| 61 | |
| 62 | void mmu_page_table_flush(unsigned long start, unsigned long stop) |
| 63 | __attribute__((weak, alias("__mmu_page_table_flush"))); |
| 64 | |
| 65 | void 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 | |
| 80 | static inline void dram_bank_mmu_setup(int bank) |
| 81 | { |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 82 | 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 Glass | a4f2079 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 89 | #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 Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 94 | } |
| 95 | } |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 96 | |
| 97 | /* to activate the MMU we need to set up virtual memory: use 1M areas */ |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 98 | static inline void mmu_setup(void) |
| 99 | { |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 100 | int i; |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 101 | u32 reg; |
| 102 | |
Aneesh V | 3e3bc1e | 2011-06-16 23:30:49 +0000 | [diff] [blame] | 103 | arm_init_before_mmu(); |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 104 | /* Set up an identity-mapping for all 4GB, rw for everyone */ |
| 105 | for (i = 0; i < 4096; i++) |
Simon Glass | a4f2079 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 106 | set_section_dcache(i, DCACHE_OFF); |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 107 | |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 108 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 109 | dram_bank_mmu_setup(i); |
| 110 | } |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 111 | |
| 112 | /* Copy the page table address to cp15 */ |
| 113 | asm volatile("mcr p15, 0, %0, c2, c0, 0" |
Simon Glass | a4f2079 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 114 | : : "r" (gd->tlb_addr) : "memory"); |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 115 | /* 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-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Aneesh V | 3bda377 | 2011-06-16 23:30:50 +0000 | [diff] [blame] | 124 | static int mmu_enabled(void) |
| 125 | { |
| 126 | return get_cr() & CR_M; |
| 127 | } |
| 128 | |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 129 | /* cache_bit must be either CR_I or CR_C */ |
| 130 | static void cache_enable(uint32_t cache_bit) |
| 131 | { |
| 132 | uint32_t reg; |
| 133 | |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 134 | /* The data cache is not active unless the mmu is enabled too */ |
Aneesh V | 3bda377 | 2011-06-16 23:30:50 +0000 | [diff] [blame] | 135 | if ((cache_bit == CR_C) && !mmu_enabled()) |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 136 | mmu_setup(); |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 137 | 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 */ |
| 143 | static void cache_disable(uint32_t cache_bit) |
| 144 | { |
| 145 | uint32_t reg; |
| 146 | |
SRICHARAN R | 88f4bf2 | 2012-05-16 23:52:54 +0000 | [diff] [blame] | 147 | reg = get_cr(); |
| 148 | cp_delay(); |
| 149 | |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 150 | if (cache_bit == CR_C) { |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 151 | /* if cache isn;t enabled no need to disable */ |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 152 | if ((reg & CR_C) != CR_C) |
| 153 | return; |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 154 | /* if disabling data cache, disable mmu too */ |
| 155 | cache_bit |= CR_M; |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 156 | } |
Arun Mankuzhi | 7a7825f | 2012-11-30 13:01:14 +0000 | [diff] [blame] | 157 | reg = get_cr(); |
| 158 | cp_delay(); |
| 159 | if (cache_bit == (CR_C | CR_M)) |
| 160 | flush_dcache_all(); |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 161 | set_cr(reg & ~cache_bit); |
| 162 | } |
| 163 | #endif |
| 164 | |
Aneesh V | ecee9c8 | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 165 | #ifdef CONFIG_SYS_ICACHE_OFF |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 166 | void icache_enable (void) |
| 167 | { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | void icache_disable (void) |
| 172 | { |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | int icache_status (void) |
| 177 | { |
| 178 | return 0; /* always off */ |
| 179 | } |
| 180 | #else |
| 181 | void icache_enable(void) |
| 182 | { |
| 183 | cache_enable(CR_I); |
| 184 | } |
| 185 | |
| 186 | void icache_disable(void) |
| 187 | { |
| 188 | cache_disable(CR_I); |
| 189 | } |
| 190 | |
| 191 | int icache_status(void) |
| 192 | { |
| 193 | return (get_cr() & CR_I) != 0; |
| 194 | } |
| 195 | #endif |
| 196 | |
Aneesh V | ecee9c8 | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 197 | #ifdef CONFIG_SYS_DCACHE_OFF |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 198 | void dcache_enable (void) |
| 199 | { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | void dcache_disable (void) |
| 204 | { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | int dcache_status (void) |
| 209 | { |
| 210 | return 0; /* always off */ |
| 211 | } |
| 212 | #else |
| 213 | void dcache_enable(void) |
| 214 | { |
| 215 | cache_enable(CR_C); |
| 216 | } |
| 217 | |
| 218 | void dcache_disable(void) |
| 219 | { |
| 220 | cache_disable(CR_C); |
| 221 | } |
| 222 | |
| 223 | int dcache_status(void) |
| 224 | { |
| 225 | return (get_cr() & CR_C) != 0; |
| 226 | } |
| 227 | #endif |