SyterKit 0.4.0.x
SyterKit is a bare-metal framework
Loading...
Searching...
No Matches
mmu.h
Go to the documentation of this file.
1
8/* SPDX-License-Identifier: GPL-2.0+ */
9
10#ifndef __MMU_H__
11#define __MMU_H__
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
17#include "timer.h"
18#include "barrier.h"
19#include "cache.h"
20#include "interrupt.h"
21
36
45static inline uint32_t arm32_read_p15_c1(void) {
47
48 __asm__ __volatile__("mrc p15, 0, %0, c1, c0, 0"
49 : "=r"(value)
50 :
51 : "memory");
52
53 return value;
54}
55
65static inline void arm32_write_p15_c1(uint32_t value) {
66 __asm__ __volatile__("mcr p15, 0, %0, c1, c0, 0"
67 :
68 : "r"(value)
69 : "memory");
71}
72
85static inline void arm32_mmu_enable(const uint32_t dram_base, uint32_t dram_size) {
86 uint32_t mmu_base;
87
88 /* use dram high 16M */
89 if (dram_size > 2048)
90 dram_size = 2048;
91
92 uint32_t *mmu_base_addr = (uint32_t *) (dram_base + ((dram_size - 1) << 20));
93 uint32_t *page_table = mmu_base_addr;
94
95 int i;
96 uint32_t reg;
97
98 /* the front 1M contain BROM/SRAM */
99#ifdef CONFIG_CHIP_DCACHE
100 page_table[0] = (3 << 10) | (15 << 5) | (1 << 3) | (1 << 2) | 0x2;
101#else
102 page_table[0] = (3 << 10) | (15 << 5) | (1 << 3) | (0 << 2) | 0x2;
103#endif
104 /* the front 1G of memory(treated as 4G for all) is set up as none cacheable */
105 for (i = 1; i < (dram_base >> 20); i++) { page_table[i] = (i << 20) | (3 << 10) | (15 << 5) | (0 << 3) | 0x2; }
106 /* Set up as write back and buffered for other 3GB, rw for everyone */
107 for (i = (dram_base >> 20); i < 4096; i++) {
108#ifdef CONFIG_CHIP_DCACHE
109 page_table[i] = (i << 20) | (3 << 10) | (15 << 5) | (1 << 3) | (1 << 2) | 0x2;
110#else
111 page_table[i] = (i << 20) | (3 << 10) | (15 << 5) | (1 << 3) | (0 << 2) | 0x2;
112#endif
113 }
114 /* flush tlb */
115 asm volatile("mcr p15, 0, %0, c8, c7, 0"
116 :
117 : "r"(0));
118 /* Copy the page table address to cp15 */
119 mmu_base = (uint32_t) mmu_base_addr;
120 mmu_base |= (1 << 0) | (1 << 1) | (2 << 3);
121 asm volatile("mcr p15, 0, %0, c2, c0, 0"
122 :
123 : "r"(mmu_base)
124 : "memory");
125 asm volatile("mcr p15, 0, %0, c2, c0, 1"
126 :
127 : "r"(mmu_base)
128 : "memory");
129 /* Set the access control to all-supervisor */
130 asm volatile("mcr p15, 0, %0, c3, c0, 0"
131 :
132 : "r"(0x55555555));//modified, origin value is (~0)
133 asm volatile("isb");
134
135#ifdef CONFIG_CHIP_DCACHE
136 /* enable smp */
137 asm volatile("mrc p15, 0, r0, c1, c0, 1");
138 asm volatile("orr r0, r0, #0x040");
139 asm volatile("mcr p15, 0, r0, c1, c0, 1");
140#endif
141
142 /* and enable the mmu */
143 asm volatile("mrc p15, 0, %0, c1, c0, 0 @ get CR"
144 : "=r"(reg)
145 :
146 : "cc");
147
148 sdelay(100);
149 reg |= ((1 << 0) | (1 << 12));// enable mmu, icache
150 reg &= ~(1 << 2); // disable dcache
151
152 printk_trace("MMU: CR = 0x%08x\n", reg);
153 asm volatile("mcr p15, 0, %0, c1, c0, 0 @ set CR"
154 :
155 : "r"(reg)
156 : "cc");
157 asm volatile("isb");
158}
159
167static inline void arm32_mmu_disable(void) {
168 uint32_t reg;
169
170 /* and disable the mmu */
171 asm volatile("mrc p15, 0, %0, c1, c0, 0 @ get CR"
172 : "=r"(reg)
173 :
174 : "cc");
175 sdelay(100);
176 reg &= ~((7 << 0) | (1 << 12));//disable mmu, icache, dcache
177 asm volatile("mcr p15, 0, %0, c1, c0, 0 @ set CR"
178 :
179 : "r"(reg)
180 : "cc");
181 asm volatile("isb");
182 /*
183 * Invalidate all instruction caches to PoU.
184 * Also flushes branch target cache.
185 */
186 asm volatile("mcr p15, 0, %0, c7, c5, 0"
187 :
188 : "r"(0));
189 /* Invalidate entire branch predictor array */
190 asm volatile("mcr p15, 0, %0, c7, c5, 6"
191 :
192 : "r"(0));
193 /* Full system DSB - make sure that the invalidation is complete */
194 asm volatile("dsb");
195 /* ISB - make sure the instruction stream sees it */
196 asm volatile("isb");
197}
198
199#ifdef __cplusplus
200}
201#endif
202
203#endif /* __MMU_H__ */
void sdelay(unsigned long us)
Definition sys-clock.c:18
static void arm32_mmu_disable(void)
Disable the ARM32 MMU and clear caches.
Definition mmu.h:167
static void arm32_mmu_enable(const uint32_t dram_base, uint32_t dram_size)
Enable the ARM32 MMU with specific memory configuration.
Definition mmu.h:85
static uint32_t arm32_read_p15_c1(void)
Read the ARM32 system control register (CP15, c1).
Definition mmu.h:45
static void arm32_write_p15_c1(uint32_t value)
Write to the ARM32 system control register (CP15, c1).
Definition mmu.h:65
u32_t uint32_t
Definition stdint.h:13
static uint32_t dram_size
Definition sys-dram.c:22
static uint8_t value
Definition io.h:144
#define printk_trace(fmt,...)
Definition log.h:44
Memory barrier definitions for RISC-V architecture.
Cache control functions for RISC-V architecture.
Interrupt control functions for RISC-V architecture.
ARM32 register structure.
Definition mmu.h:28
uint32_t r[13]
General purpose registers R0-R12.
Definition mmu.h:31
uint32_t pc
Program counter (R15)
Definition mmu.h:34
uint32_t lr
Link register (R14)
Definition mmu.h:33
uint32_t esp
Extended stack pointer.
Definition mmu.h:29
uint32_t cpsr
Current Program Status Register.
Definition mmu.h:30
uint32_t sp
Stack pointer (R13)
Definition mmu.h:32