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 | |
| 29 | #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH) |
| 30 | #define CACHE_SETUP 0x1a |
| 31 | #else |
| 32 | #define CACHE_SETUP 0x1e |
| 33 | #endif |
| 34 | |
| 35 | DECLARE_GLOBAL_DATA_PTR; |
| 36 | |
Aneesh V | 3e3bc1e | 2011-06-16 23:30:49 +0000 | [diff] [blame^] | 37 | void __arm_init_before_mmu(void) |
| 38 | { |
| 39 | } |
| 40 | void arm_init_before_mmu(void) |
| 41 | __attribute__((weak, alias("__arm_init_before_mmu"))); |
| 42 | |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 43 | static 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 Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 50 | asm volatile("" : : : "memory"); |
| 51 | } |
| 52 | |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 53 | static inline void dram_bank_mmu_setup(int bank) |
| 54 | { |
| 55 | u32 *page_table = (u32 *)gd->tlb_addr; |
| 56 | bd_t *bd = gd->bd; |
| 57 | int i; |
| 58 | |
| 59 | debug("%s: bank: %d\n", __func__, bank); |
| 60 | for (i = bd->bi_dram[bank].start >> 20; |
| 61 | i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20; |
| 62 | i++) { |
| 63 | page_table[i] = i << 20 | (3 << 10) | CACHE_SETUP; |
| 64 | } |
| 65 | } |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 66 | |
| 67 | /* 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] | 68 | static inline void mmu_setup(void) |
| 69 | { |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 70 | u32 *page_table = (u32 *)gd->tlb_addr; |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 71 | int i; |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 72 | u32 reg; |
| 73 | |
Aneesh V | 3e3bc1e | 2011-06-16 23:30:49 +0000 | [diff] [blame^] | 74 | arm_init_before_mmu(); |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 75 | /* Set up an identity-mapping for all 4GB, rw for everyone */ |
| 76 | for (i = 0; i < 4096; i++) |
| 77 | page_table[i] = i << 20 | (3 << 10) | 0x12; |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 78 | |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 79 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 80 | dram_bank_mmu_setup(i); |
| 81 | } |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 82 | |
| 83 | /* Copy the page table address to cp15 */ |
| 84 | asm volatile("mcr p15, 0, %0, c2, c0, 0" |
| 85 | : : "r" (page_table) : "memory"); |
| 86 | /* Set the access control to all-supervisor */ |
| 87 | asm volatile("mcr p15, 0, %0, c3, c0, 0" |
| 88 | : : "r" (~0)); |
| 89 | /* and enable the mmu */ |
| 90 | reg = get_cr(); /* get control reg. */ |
| 91 | cp_delay(); |
| 92 | set_cr(reg | CR_M); |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /* cache_bit must be either CR_I or CR_C */ |
| 96 | static void cache_enable(uint32_t cache_bit) |
| 97 | { |
| 98 | uint32_t reg; |
| 99 | |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 100 | /* The data cache is not active unless the mmu is enabled too */ |
| 101 | if (cache_bit == CR_C) |
| 102 | mmu_setup(); |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 103 | reg = get_cr(); /* get control reg. */ |
| 104 | cp_delay(); |
| 105 | set_cr(reg | cache_bit); |
| 106 | } |
| 107 | |
| 108 | /* cache_bit must be either CR_I or CR_C */ |
| 109 | static void cache_disable(uint32_t cache_bit) |
| 110 | { |
| 111 | uint32_t reg; |
| 112 | |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 113 | if (cache_bit == CR_C) { |
Heiko Schocher | aeb2991 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 114 | /* if cache isn;t enabled no need to disable */ |
| 115 | reg = get_cr(); |
| 116 | if ((reg & CR_C) != CR_C) |
| 117 | return; |
Heiko Schocher | 69f1d0c | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 118 | /* if disabling data cache, disable mmu too */ |
| 119 | cache_bit |= CR_M; |
| 120 | flush_cache(0, ~0); |
| 121 | } |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 122 | reg = get_cr(); |
| 123 | cp_delay(); |
| 124 | set_cr(reg & ~cache_bit); |
| 125 | } |
| 126 | #endif |
| 127 | |
Aneesh V | ecee9c8 | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 128 | #ifdef CONFIG_SYS_ICACHE_OFF |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 129 | void icache_enable (void) |
| 130 | { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | void icache_disable (void) |
| 135 | { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | int icache_status (void) |
| 140 | { |
| 141 | return 0; /* always off */ |
| 142 | } |
| 143 | #else |
| 144 | void icache_enable(void) |
| 145 | { |
| 146 | cache_enable(CR_I); |
| 147 | } |
| 148 | |
| 149 | void icache_disable(void) |
| 150 | { |
| 151 | cache_disable(CR_I); |
| 152 | } |
| 153 | |
| 154 | int icache_status(void) |
| 155 | { |
| 156 | return (get_cr() & CR_I) != 0; |
| 157 | } |
| 158 | #endif |
| 159 | |
Aneesh V | ecee9c8 | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 160 | #ifdef CONFIG_SYS_DCACHE_OFF |
Jean-Christophe PLAGNIOL-VILLARD | e6b5f1b | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 161 | void dcache_enable (void) |
| 162 | { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | void dcache_disable (void) |
| 167 | { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | int dcache_status (void) |
| 172 | { |
| 173 | return 0; /* always off */ |
| 174 | } |
| 175 | #else |
| 176 | void dcache_enable(void) |
| 177 | { |
| 178 | cache_enable(CR_C); |
| 179 | } |
| 180 | |
| 181 | void dcache_disable(void) |
| 182 | { |
| 183 | cache_disable(CR_C); |
| 184 | } |
| 185 | |
| 186 | int dcache_status(void) |
| 187 | { |
| 188 | return (get_cr() & CR_C) != 0; |
| 189 | } |
| 190 | #endif |