Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <asm/system.h> |
R Sricharan | 96fdbec | 2013-03-04 20:04:44 +0000 | [diff] [blame] | 10 | #include <asm/cache.h> |
| 11 | #include <linux/compiler.h> |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 12 | |
Aneesh V | e47f2db | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 13 | #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF)) |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 14 | |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Jeroen Hofstee | fcfddfd | 2014-06-23 22:07:04 +0200 | [diff] [blame] | 17 | __weak void arm_init_before_mmu(void) |
Aneesh V | c2dd0d4 | 2011-06-16 23:30:49 +0000 | [diff] [blame] | 18 | { |
| 19 | } |
Aneesh V | c2dd0d4 | 2011-06-16 23:30:49 +0000 | [diff] [blame] | 20 | |
R Sricharan | de63ac2 | 2013-03-04 20:04:45 +0000 | [diff] [blame] | 21 | __weak void arm_init_domains(void) |
| 22 | { |
| 23 | } |
| 24 | |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 25 | static void cp_delay (void) |
| 26 | { |
| 27 | volatile int i; |
| 28 | |
| 29 | /* copro seems to need some delay between reading and writing */ |
| 30 | for (i = 0; i < 100; i++) |
| 31 | nop(); |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 32 | asm volatile("" : : : "memory"); |
| 33 | } |
| 34 | |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 35 | void set_section_dcache(int section, enum dcache_option option) |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 36 | { |
Alexander Graf | d990f5c | 2016-03-16 15:41:21 +0100 | [diff] [blame] | 37 | #ifdef CONFIG_ARMV7_LPAE |
| 38 | u64 *page_table = (u64 *)gd->arch.tlb_addr; |
| 39 | /* Need to set the access flag to not fault */ |
| 40 | u64 value = TTB_SECT_AP | TTB_SECT_AF; |
| 41 | #else |
Simon Glass | 34fd5d2 | 2012-12-13 20:48:39 +0000 | [diff] [blame] | 42 | u32 *page_table = (u32 *)gd->arch.tlb_addr; |
Alexander Graf | d990f5c | 2016-03-16 15:41:21 +0100 | [diff] [blame] | 43 | u32 value = TTB_SECT_AP; |
| 44 | #endif |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 45 | |
Alexander Graf | d990f5c | 2016-03-16 15:41:21 +0100 | [diff] [blame] | 46 | /* Add the page offset */ |
| 47 | value |= ((u32)section << MMU_SECTION_SHIFT); |
| 48 | |
| 49 | /* Add caching bits */ |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 50 | value |= option; |
Alexander Graf | d990f5c | 2016-03-16 15:41:21 +0100 | [diff] [blame] | 51 | |
| 52 | /* Set PTE */ |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 53 | page_table[section] = value; |
| 54 | } |
| 55 | |
Jeroen Hofstee | fcfddfd | 2014-06-23 22:07:04 +0200 | [diff] [blame] | 56 | __weak void mmu_page_table_flush(unsigned long start, unsigned long stop) |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 57 | { |
| 58 | debug("%s: Warning: not implemented\n", __func__); |
| 59 | } |
| 60 | |
Thierry Reding | 25026fa | 2014-08-26 17:34:21 +0200 | [diff] [blame] | 61 | void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size, |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 62 | enum dcache_option option) |
| 63 | { |
Stefan Agner | c5b3cab | 2016-08-14 21:33:00 -0700 | [diff] [blame] | 64 | #ifdef CONFIG_ARMV7_LPAE |
| 65 | u64 *page_table = (u64 *)gd->arch.tlb_addr; |
| 66 | #else |
Simon Glass | 34fd5d2 | 2012-12-13 20:48:39 +0000 | [diff] [blame] | 67 | u32 *page_table = (u32 *)gd->arch.tlb_addr; |
Stefan Agner | c5b3cab | 2016-08-14 21:33:00 -0700 | [diff] [blame] | 68 | #endif |
Stefan Agner | 8f894a4 | 2016-08-14 21:33:01 -0700 | [diff] [blame] | 69 | unsigned long startpt, stoppt; |
Thierry Reding | 25026fa | 2014-08-26 17:34:21 +0200 | [diff] [blame] | 70 | unsigned long upto, end; |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 71 | |
| 72 | end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT; |
| 73 | start = start >> MMU_SECTION_SHIFT; |
Keerthy | 06d43c8 | 2016-10-29 15:19:10 +0530 | [diff] [blame^] | 74 | #ifdef CONFIG_ARMV7_LPAE |
| 75 | debug("%s: start=%pa, size=%zu, option=%llx\n", __func__, &start, size, |
| 76 | option); |
| 77 | #else |
Keerthy | 2b373cb | 2016-10-29 15:19:09 +0530 | [diff] [blame] | 78 | debug("%s: start=%pa, size=%zu, option=0x%x\n", __func__, &start, size, |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 79 | option); |
Keerthy | 06d43c8 | 2016-10-29 15:19:10 +0530 | [diff] [blame^] | 80 | #endif |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 81 | for (upto = start; upto < end; upto++) |
| 82 | set_section_dcache(upto, option); |
Stefan Agner | 8f894a4 | 2016-08-14 21:33:01 -0700 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * Make sure range is cache line aligned |
| 86 | * Only CPU maintains page tables, hence it is safe to always |
| 87 | * flush complete cache lines... |
| 88 | */ |
| 89 | |
| 90 | startpt = (unsigned long)&page_table[start]; |
| 91 | startpt &= ~(CONFIG_SYS_CACHELINE_SIZE - 1); |
| 92 | stoppt = (unsigned long)&page_table[end]; |
| 93 | stoppt = ALIGN(stoppt, CONFIG_SYS_CACHELINE_SIZE); |
| 94 | mmu_page_table_flush(startpt, stoppt); |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 95 | } |
| 96 | |
R Sricharan | 96fdbec | 2013-03-04 20:04:44 +0000 | [diff] [blame] | 97 | __weak void dram_bank_mmu_setup(int bank) |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 98 | { |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 99 | bd_t *bd = gd->bd; |
| 100 | int i; |
| 101 | |
| 102 | debug("%s: bank: %d\n", __func__, bank); |
Alexander Graf | d990f5c | 2016-03-16 15:41:21 +0100 | [diff] [blame] | 103 | for (i = bd->bi_dram[bank].start >> MMU_SECTION_SHIFT; |
| 104 | i < (bd->bi_dram[bank].start >> MMU_SECTION_SHIFT) + |
| 105 | (bd->bi_dram[bank].size >> MMU_SECTION_SHIFT); |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 106 | i++) { |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 107 | #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH) |
| 108 | set_section_dcache(i, DCACHE_WRITETHROUGH); |
Marek Vasut | ff7e970 | 2014-09-15 02:44:36 +0200 | [diff] [blame] | 109 | #elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC) |
| 110 | set_section_dcache(i, DCACHE_WRITEALLOC); |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 111 | #else |
| 112 | set_section_dcache(i, DCACHE_WRITEBACK); |
| 113 | #endif |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 114 | } |
| 115 | } |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 116 | |
| 117 | /* to activate the MMU we need to set up virtual memory: use 1M areas */ |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 118 | static inline void mmu_setup(void) |
| 119 | { |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 120 | int i; |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 121 | u32 reg; |
| 122 | |
Aneesh V | c2dd0d4 | 2011-06-16 23:30:49 +0000 | [diff] [blame] | 123 | arm_init_before_mmu(); |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 124 | /* Set up an identity-mapping for all 4GB, rw for everyone */ |
Alexander Graf | d990f5c | 2016-03-16 15:41:21 +0100 | [diff] [blame] | 125 | for (i = 0; i < ((4096ULL * 1024 * 1024) >> MMU_SECTION_SHIFT); i++) |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 126 | set_section_dcache(i, DCACHE_OFF); |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 127 | |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 128 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 129 | dram_bank_mmu_setup(i); |
| 130 | } |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 131 | |
Alexander Graf | d990f5c | 2016-03-16 15:41:21 +0100 | [diff] [blame] | 132 | #ifdef CONFIG_ARMV7_LPAE |
| 133 | /* Set up 4 PTE entries pointing to our 4 1GB page tables */ |
| 134 | for (i = 0; i < 4; i++) { |
| 135 | u64 *page_table = (u64 *)(gd->arch.tlb_addr + (4096 * 4)); |
| 136 | u64 tpt = gd->arch.tlb_addr + (4096 * i); |
| 137 | page_table[i] = tpt | TTB_PAGETABLE; |
| 138 | } |
| 139 | |
| 140 | reg = TTBCR_EAE; |
| 141 | #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH) |
| 142 | reg |= TTBCR_ORGN0_WT | TTBCR_IRGN0_WT; |
| 143 | #elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC) |
| 144 | reg |= TTBCR_ORGN0_WBWA | TTBCR_IRGN0_WBWA; |
| 145 | #else |
| 146 | reg |= TTBCR_ORGN0_WBNWA | TTBCR_IRGN0_WBNWA; |
| 147 | #endif |
| 148 | |
| 149 | if (is_hyp()) { |
| 150 | /* Set HCTR to enable LPAE */ |
| 151 | asm volatile("mcr p15, 4, %0, c2, c0, 2" |
| 152 | : : "r" (reg) : "memory"); |
| 153 | /* Set HTTBR0 */ |
| 154 | asm volatile("mcrr p15, 4, %0, %1, c2" |
| 155 | : |
| 156 | : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0) |
| 157 | : "memory"); |
| 158 | /* Set HMAIR */ |
| 159 | asm volatile("mcr p15, 4, %0, c10, c2, 0" |
| 160 | : : "r" (MEMORY_ATTRIBUTES) : "memory"); |
| 161 | } else { |
| 162 | /* Set TTBCR to enable LPAE */ |
| 163 | asm volatile("mcr p15, 0, %0, c2, c0, 2" |
| 164 | : : "r" (reg) : "memory"); |
| 165 | /* Set 64-bit TTBR0 */ |
| 166 | asm volatile("mcrr p15, 0, %0, %1, c2" |
| 167 | : |
| 168 | : "r"(gd->arch.tlb_addr + (4096 * 4)), "r"(0) |
| 169 | : "memory"); |
| 170 | /* Set MAIR */ |
| 171 | asm volatile("mcr p15, 0, %0, c10, c2, 0" |
| 172 | : : "r" (MEMORY_ATTRIBUTES) : "memory"); |
| 173 | } |
| 174 | #elif defined(CONFIG_CPU_V7) |
Bryan Brinsko | 97840b5 | 2015-03-24 11:25:12 -0500 | [diff] [blame] | 175 | /* Set TTBR0 */ |
| 176 | reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK; |
| 177 | #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH) |
| 178 | reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT; |
| 179 | #elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC) |
| 180 | reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA; |
| 181 | #else |
| 182 | reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB; |
| 183 | #endif |
| 184 | asm volatile("mcr p15, 0, %0, c2, c0, 0" |
| 185 | : : "r" (reg) : "memory"); |
| 186 | #else |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 187 | /* Copy the page table address to cp15 */ |
| 188 | asm volatile("mcr p15, 0, %0, c2, c0, 0" |
Simon Glass | 34fd5d2 | 2012-12-13 20:48:39 +0000 | [diff] [blame] | 189 | : : "r" (gd->arch.tlb_addr) : "memory"); |
Bryan Brinsko | 97840b5 | 2015-03-24 11:25:12 -0500 | [diff] [blame] | 190 | #endif |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 191 | /* Set the access control to all-supervisor */ |
| 192 | asm volatile("mcr p15, 0, %0, c3, c0, 0" |
| 193 | : : "r" (~0)); |
R Sricharan | de63ac2 | 2013-03-04 20:04:45 +0000 | [diff] [blame] | 194 | |
| 195 | arm_init_domains(); |
| 196 | |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 197 | /* and enable the mmu */ |
| 198 | reg = get_cr(); /* get control reg. */ |
| 199 | cp_delay(); |
| 200 | set_cr(reg | CR_M); |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Aneesh V | e05f007 | 2011-06-16 23:30:50 +0000 | [diff] [blame] | 203 | static int mmu_enabled(void) |
| 204 | { |
| 205 | return get_cr() & CR_M; |
| 206 | } |
| 207 | |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 208 | /* cache_bit must be either CR_I or CR_C */ |
| 209 | static void cache_enable(uint32_t cache_bit) |
| 210 | { |
| 211 | uint32_t reg; |
| 212 | |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 213 | /* The data cache is not active unless the mmu is enabled too */ |
Aneesh V | e05f007 | 2011-06-16 23:30:50 +0000 | [diff] [blame] | 214 | if ((cache_bit == CR_C) && !mmu_enabled()) |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 215 | mmu_setup(); |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 216 | reg = get_cr(); /* get control reg. */ |
| 217 | cp_delay(); |
| 218 | set_cr(reg | cache_bit); |
| 219 | } |
| 220 | |
| 221 | /* cache_bit must be either CR_I or CR_C */ |
| 222 | static void cache_disable(uint32_t cache_bit) |
| 223 | { |
| 224 | uint32_t reg; |
| 225 | |
SRICHARAN R | d702b08 | 2012-05-16 23:52:54 +0000 | [diff] [blame] | 226 | reg = get_cr(); |
| 227 | cp_delay(); |
| 228 | |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 229 | if (cache_bit == CR_C) { |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 230 | /* if cache isn;t enabled no need to disable */ |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 231 | if ((reg & CR_C) != CR_C) |
| 232 | return; |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 233 | /* if disabling data cache, disable mmu too */ |
| 234 | cache_bit |= CR_M; |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 235 | } |
Arun Mankuzhi | 44df5e8 | 2012-11-30 13:01:14 +0000 | [diff] [blame] | 236 | reg = get_cr(); |
| 237 | cp_delay(); |
| 238 | if (cache_bit == (CR_C | CR_M)) |
| 239 | flush_dcache_all(); |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 240 | set_cr(reg & ~cache_bit); |
| 241 | } |
| 242 | #endif |
| 243 | |
Aneesh V | e47f2db | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 244 | #ifdef CONFIG_SYS_ICACHE_OFF |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 245 | void icache_enable (void) |
| 246 | { |
| 247 | return; |
| 248 | } |
| 249 | |
| 250 | void icache_disable (void) |
| 251 | { |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | int icache_status (void) |
| 256 | { |
| 257 | return 0; /* always off */ |
| 258 | } |
| 259 | #else |
| 260 | void icache_enable(void) |
| 261 | { |
| 262 | cache_enable(CR_I); |
| 263 | } |
| 264 | |
| 265 | void icache_disable(void) |
| 266 | { |
| 267 | cache_disable(CR_I); |
| 268 | } |
| 269 | |
| 270 | int icache_status(void) |
| 271 | { |
| 272 | return (get_cr() & CR_I) != 0; |
| 273 | } |
| 274 | #endif |
| 275 | |
Aneesh V | e47f2db | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 276 | #ifdef CONFIG_SYS_DCACHE_OFF |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 277 | void dcache_enable (void) |
| 278 | { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | void dcache_disable (void) |
| 283 | { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | int dcache_status (void) |
| 288 | { |
| 289 | return 0; /* always off */ |
| 290 | } |
| 291 | #else |
| 292 | void dcache_enable(void) |
| 293 | { |
| 294 | cache_enable(CR_C); |
| 295 | } |
| 296 | |
| 297 | void dcache_disable(void) |
| 298 | { |
| 299 | cache_disable(CR_C); |
| 300 | } |
| 301 | |
| 302 | int dcache_status(void) |
| 303 | { |
| 304 | return (get_cr() & CR_C) != 0; |
| 305 | } |
| 306 | #endif |