blob: 8642010a17500cc8675b5ec03f851e1e8ba929bb [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02001/*
2 * (C) Copyright 2002
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +02006 */
7
8#include <common.h>
9#include <asm/system.h>
R Sricharan08716072013-03-04 20:04:44 +000010#include <asm/cache.h>
11#include <linux/compiler.h>
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020012
Aneesh Vecee9c82011-06-16 23:30:48 +000013#if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020014
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020015DECLARE_GLOBAL_DATA_PTR;
16
Aneesh V3e3bc1e2011-06-16 23:30:49 +000017void __arm_init_before_mmu(void)
18{
19}
20void arm_init_before_mmu(void)
21 __attribute__((weak, alias("__arm_init_before_mmu")));
22
R Sricharan06396c12013-03-04 20:04:45 +000023__weak void arm_init_domains(void)
24{
25}
26
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +020027static void cp_delay (void)
28{
29 volatile int i;
30
31 /* copro seems to need some delay between reading and writing */
32 for (i = 0; i < 100; i++)
33 nop();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020034 asm volatile("" : : : "memory");
35}
36
Simon Glassa4f20792012-10-17 13:24:53 +000037void set_section_dcache(int section, enum dcache_option option)
Heiko Schocheraeb29912010-09-17 13:10:39 +020038{
Simon Glass6b4ee152012-12-13 20:48:39 +000039 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Simon Glassa4f20792012-10-17 13:24:53 +000040 u32 value;
41
42 value = (section << MMU_SECTION_SHIFT) | (3 << 10);
43 value |= option;
44 page_table[section] = value;
45}
46
47void __mmu_page_table_flush(unsigned long start, unsigned long stop)
48{
49 debug("%s: Warning: not implemented\n", __func__);
50}
51
52void mmu_page_table_flush(unsigned long start, unsigned long stop)
53 __attribute__((weak, alias("__mmu_page_table_flush")));
54
55void mmu_set_region_dcache_behaviour(u32 start, int size,
56 enum dcache_option option)
57{
Simon Glass6b4ee152012-12-13 20:48:39 +000058 u32 *page_table = (u32 *)gd->arch.tlb_addr;
Simon Glassa4f20792012-10-17 13:24:53 +000059 u32 upto, end;
60
61 end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
62 start = start >> MMU_SECTION_SHIFT;
63 debug("%s: start=%x, size=%x, option=%d\n", __func__, start, size,
64 option);
65 for (upto = start; upto < end; upto++)
66 set_section_dcache(upto, option);
67 mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]);
68}
69
R Sricharan08716072013-03-04 20:04:44 +000070__weak void dram_bank_mmu_setup(int bank)
Simon Glassa4f20792012-10-17 13:24:53 +000071{
Heiko Schocheraeb29912010-09-17 13:10:39 +020072 bd_t *bd = gd->bd;
73 int i;
74
75 debug("%s: bank: %d\n", __func__, bank);
76 for (i = bd->bi_dram[bank].start >> 20;
77 i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
78 i++) {
Simon Glassa4f20792012-10-17 13:24:53 +000079#if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
80 set_section_dcache(i, DCACHE_WRITETHROUGH);
81#else
82 set_section_dcache(i, DCACHE_WRITEBACK);
83#endif
Heiko Schocheraeb29912010-09-17 13:10:39 +020084 }
85}
Heiko Schocheraeb29912010-09-17 13:10:39 +020086
87/* to activate the MMU we need to set up virtual memory: use 1M areas */
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020088static inline void mmu_setup(void)
89{
Heiko Schocheraeb29912010-09-17 13:10:39 +020090 int i;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020091 u32 reg;
92
Aneesh V3e3bc1e2011-06-16 23:30:49 +000093 arm_init_before_mmu();
Heiko Schocher69f1d0c2010-09-17 13:10:29 +020094 /* Set up an identity-mapping for all 4GB, rw for everyone */
95 for (i = 0; i < 4096; i++)
Simon Glassa4f20792012-10-17 13:24:53 +000096 set_section_dcache(i, DCACHE_OFF);
Heiko Schocheraeb29912010-09-17 13:10:39 +020097
Heiko Schocheraeb29912010-09-17 13:10:39 +020098 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
99 dram_bank_mmu_setup(i);
100 }
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200101
102 /* Copy the page table address to cp15 */
103 asm volatile("mcr p15, 0, %0, c2, c0, 0"
Simon Glass6b4ee152012-12-13 20:48:39 +0000104 : : "r" (gd->arch.tlb_addr) : "memory");
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200105 /* Set the access control to all-supervisor */
106 asm volatile("mcr p15, 0, %0, c3, c0, 0"
107 : : "r" (~0));
R Sricharan06396c12013-03-04 20:04:45 +0000108
109 arm_init_domains();
110
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200111 /* and enable the mmu */
112 reg = get_cr(); /* get control reg. */
113 cp_delay();
114 set_cr(reg | CR_M);
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200115}
116
Aneesh V3bda3772011-06-16 23:30:50 +0000117static int mmu_enabled(void)
118{
119 return get_cr() & CR_M;
120}
121
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200122/* cache_bit must be either CR_I or CR_C */
123static void cache_enable(uint32_t cache_bit)
124{
125 uint32_t reg;
126
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200127 /* The data cache is not active unless the mmu is enabled too */
Aneesh V3bda3772011-06-16 23:30:50 +0000128 if ((cache_bit == CR_C) && !mmu_enabled())
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200129 mmu_setup();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200130 reg = get_cr(); /* get control reg. */
131 cp_delay();
132 set_cr(reg | cache_bit);
133}
134
135/* cache_bit must be either CR_I or CR_C */
136static void cache_disable(uint32_t cache_bit)
137{
138 uint32_t reg;
139
SRICHARAN R88f4bf22012-05-16 23:52:54 +0000140 reg = get_cr();
141 cp_delay();
142
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200143 if (cache_bit == CR_C) {
Heiko Schocheraeb29912010-09-17 13:10:39 +0200144 /* if cache isn;t enabled no need to disable */
Heiko Schocheraeb29912010-09-17 13:10:39 +0200145 if ((reg & CR_C) != CR_C)
146 return;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200147 /* if disabling data cache, disable mmu too */
148 cache_bit |= CR_M;
Heiko Schocher69f1d0c2010-09-17 13:10:29 +0200149 }
Arun Mankuzhi7a7825f2012-11-30 13:01:14 +0000150 reg = get_cr();
151 cp_delay();
152 if (cache_bit == (CR_C | CR_M))
153 flush_dcache_all();
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200154 set_cr(reg & ~cache_bit);
155}
156#endif
157
Aneesh Vecee9c82011-06-16 23:30:48 +0000158#ifdef CONFIG_SYS_ICACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200159void icache_enable (void)
160{
161 return;
162}
163
164void icache_disable (void)
165{
166 return;
167}
168
169int icache_status (void)
170{
171 return 0; /* always off */
172}
173#else
174void icache_enable(void)
175{
176 cache_enable(CR_I);
177}
178
179void icache_disable(void)
180{
181 cache_disable(CR_I);
182}
183
184int icache_status(void)
185{
186 return (get_cr() & CR_I) != 0;
187}
188#endif
189
Aneesh Vecee9c82011-06-16 23:30:48 +0000190#ifdef CONFIG_SYS_DCACHE_OFF
Jean-Christophe PLAGNIOL-VILLARDe6b5f1b2009-04-05 13:06:31 +0200191void dcache_enable (void)
192{
193 return;
194}
195
196void dcache_disable (void)
197{
198 return;
199}
200
201int dcache_status (void)
202{
203 return 0; /* always off */
204}
205#else
206void dcache_enable(void)
207{
208 cache_enable(CR_C);
209}
210
211void dcache_disable(void)
212{
213 cache_disable(CR_C);
214}
215
216int dcache_status(void)
217{
218 return (get_cr() & CR_C) != 0;
219}
220#endif