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 | { |
Simon Glass | 34fd5d2 | 2012-12-13 20:48:39 +0000 | [diff] [blame] | 37 | u32 *page_table = (u32 *)gd->arch.tlb_addr; |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 38 | u32 value; |
| 39 | |
| 40 | value = (section << MMU_SECTION_SHIFT) | (3 << 10); |
| 41 | value |= option; |
| 42 | page_table[section] = value; |
| 43 | } |
| 44 | |
Jeroen Hofstee | fcfddfd | 2014-06-23 22:07:04 +0200 | [diff] [blame^] | 45 | __weak void mmu_page_table_flush(unsigned long start, unsigned long stop) |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 46 | { |
| 47 | debug("%s: Warning: not implemented\n", __func__); |
| 48 | } |
| 49 | |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 50 | void mmu_set_region_dcache_behaviour(u32 start, int size, |
| 51 | enum dcache_option option) |
| 52 | { |
Simon Glass | 34fd5d2 | 2012-12-13 20:48:39 +0000 | [diff] [blame] | 53 | u32 *page_table = (u32 *)gd->arch.tlb_addr; |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 54 | u32 upto, end; |
| 55 | |
| 56 | end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT; |
| 57 | start = start >> MMU_SECTION_SHIFT; |
| 58 | debug("%s: start=%x, size=%x, option=%d\n", __func__, start, size, |
| 59 | option); |
| 60 | for (upto = start; upto < end; upto++) |
| 61 | set_section_dcache(upto, option); |
| 62 | mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]); |
| 63 | } |
| 64 | |
R Sricharan | 96fdbec | 2013-03-04 20:04:44 +0000 | [diff] [blame] | 65 | __weak void dram_bank_mmu_setup(int bank) |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 66 | { |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 67 | bd_t *bd = gd->bd; |
| 68 | int i; |
| 69 | |
| 70 | debug("%s: bank: %d\n", __func__, bank); |
| 71 | for (i = bd->bi_dram[bank].start >> 20; |
| 72 | i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20; |
| 73 | i++) { |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 74 | #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH) |
| 75 | set_section_dcache(i, DCACHE_WRITETHROUGH); |
| 76 | #else |
| 77 | set_section_dcache(i, DCACHE_WRITEBACK); |
| 78 | #endif |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 79 | } |
| 80 | } |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 81 | |
| 82 | /* 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] | 83 | static inline void mmu_setup(void) |
| 84 | { |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 85 | int i; |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 86 | u32 reg; |
| 87 | |
Aneesh V | c2dd0d4 | 2011-06-16 23:30:49 +0000 | [diff] [blame] | 88 | arm_init_before_mmu(); |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 89 | /* Set up an identity-mapping for all 4GB, rw for everyone */ |
| 90 | for (i = 0; i < 4096; i++) |
Simon Glass | 0dde7f5 | 2012-10-17 13:24:53 +0000 | [diff] [blame] | 91 | set_section_dcache(i, DCACHE_OFF); |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 92 | |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 93 | for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) { |
| 94 | dram_bank_mmu_setup(i); |
| 95 | } |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 96 | |
| 97 | /* Copy the page table address to cp15 */ |
| 98 | asm volatile("mcr p15, 0, %0, c2, c0, 0" |
Simon Glass | 34fd5d2 | 2012-12-13 20:48:39 +0000 | [diff] [blame] | 99 | : : "r" (gd->arch.tlb_addr) : "memory"); |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 100 | /* Set the access control to all-supervisor */ |
| 101 | asm volatile("mcr p15, 0, %0, c3, c0, 0" |
| 102 | : : "r" (~0)); |
R Sricharan | de63ac2 | 2013-03-04 20:04:45 +0000 | [diff] [blame] | 103 | |
| 104 | arm_init_domains(); |
| 105 | |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 106 | /* and enable the mmu */ |
| 107 | reg = get_cr(); /* get control reg. */ |
| 108 | cp_delay(); |
| 109 | set_cr(reg | CR_M); |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Aneesh V | e05f007 | 2011-06-16 23:30:50 +0000 | [diff] [blame] | 112 | static int mmu_enabled(void) |
| 113 | { |
| 114 | return get_cr() & CR_M; |
| 115 | } |
| 116 | |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 117 | /* cache_bit must be either CR_I or CR_C */ |
| 118 | static void cache_enable(uint32_t cache_bit) |
| 119 | { |
| 120 | uint32_t reg; |
| 121 | |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 122 | /* The data cache is not active unless the mmu is enabled too */ |
Aneesh V | e05f007 | 2011-06-16 23:30:50 +0000 | [diff] [blame] | 123 | if ((cache_bit == CR_C) && !mmu_enabled()) |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 124 | mmu_setup(); |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 125 | reg = get_cr(); /* get control reg. */ |
| 126 | cp_delay(); |
| 127 | set_cr(reg | cache_bit); |
| 128 | } |
| 129 | |
| 130 | /* cache_bit must be either CR_I or CR_C */ |
| 131 | static void cache_disable(uint32_t cache_bit) |
| 132 | { |
| 133 | uint32_t reg; |
| 134 | |
SRICHARAN R | d702b08 | 2012-05-16 23:52:54 +0000 | [diff] [blame] | 135 | reg = get_cr(); |
| 136 | cp_delay(); |
| 137 | |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 138 | if (cache_bit == CR_C) { |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 139 | /* if cache isn;t enabled no need to disable */ |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 140 | if ((reg & CR_C) != CR_C) |
| 141 | return; |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 142 | /* if disabling data cache, disable mmu too */ |
| 143 | cache_bit |= CR_M; |
Heiko Schocher | 880eff5 | 2010-09-17 13:10:29 +0200 | [diff] [blame] | 144 | } |
Arun Mankuzhi | 44df5e8 | 2012-11-30 13:01:14 +0000 | [diff] [blame] | 145 | reg = get_cr(); |
| 146 | cp_delay(); |
| 147 | if (cache_bit == (CR_C | CR_M)) |
| 148 | flush_dcache_all(); |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 149 | set_cr(reg & ~cache_bit); |
| 150 | } |
| 151 | #endif |
| 152 | |
Aneesh V | e47f2db | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 153 | #ifdef CONFIG_SYS_ICACHE_OFF |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 154 | void icache_enable (void) |
| 155 | { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | void icache_disable (void) |
| 160 | { |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | int icache_status (void) |
| 165 | { |
| 166 | return 0; /* always off */ |
| 167 | } |
| 168 | #else |
| 169 | void icache_enable(void) |
| 170 | { |
| 171 | cache_enable(CR_I); |
| 172 | } |
| 173 | |
| 174 | void icache_disable(void) |
| 175 | { |
| 176 | cache_disable(CR_I); |
| 177 | } |
| 178 | |
| 179 | int icache_status(void) |
| 180 | { |
| 181 | return (get_cr() & CR_I) != 0; |
| 182 | } |
| 183 | #endif |
| 184 | |
Aneesh V | e47f2db | 2011-06-16 23:30:48 +0000 | [diff] [blame] | 185 | #ifdef CONFIG_SYS_DCACHE_OFF |
Jean-Christophe PLAGNIOL-VILLARD | b3acb6c | 2009-04-05 13:06:31 +0200 | [diff] [blame] | 186 | void dcache_enable (void) |
| 187 | { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | void dcache_disable (void) |
| 192 | { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | int dcache_status (void) |
| 197 | { |
| 198 | return 0; /* always off */ |
| 199 | } |
| 200 | #else |
| 201 | void dcache_enable(void) |
| 202 | { |
| 203 | cache_enable(CR_C); |
| 204 | } |
| 205 | |
| 206 | void dcache_disable(void) |
| 207 | { |
| 208 | cache_disable(CR_C); |
| 209 | } |
| 210 | |
| 211 | int dcache_status(void) |
| 212 | { |
| 213 | return (get_cr() & CR_C) != 0; |
| 214 | } |
| 215 | #endif |