blob: 8109e39c8e7a68e92eecbbd1964444388778528c [file] [log] [blame]
Travis Geiselbrecht1d0df692008-09-01 02:26:09 -07001/*
2 * Copyright (c) 2008 Travis Geiselbrecht
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <debug.h>
24#include <sys/types.h>
25#include <compiler.h>
26#include <arch.h>
27#include <arch/arm.h>
28#include <arch/arm/mmu.h>
29
30#if ARM_WITH_MMU
31
32#define MB (1024*1024)
33
34/* the location of the table may be brought in from outside */
35#if WITH_EXTERNAL_TRANSLATION_TABLE
36#if !defined(MMU_TRANSLATION_TABLE_ADDR)
37#error must set MMU_TRANSLATION_TABLE_ADDR in the make configuration
38#endif
39static uint32_t *tt = (void *)MMU_TRANSLATION_TABLE_ADDR;
40#else
41/* the main translation table */
42static uint32_t tt[4096] __ALIGNED(16384);
43#endif
44
45#define MMU_FLAG_CACHED 0x1
46#define MMU_FLAG_BUFFERED 0x2
47#define MMU_FLAG_READWRITE 0x4
48
49void arm_mmu_map_section(addr_t paddr, addr_t vaddr, uint flags)
50{
51 int index;
52 uint AP;
53 uint CB;
54
55 AP = (flags & MMU_FLAG_READWRITE) ? 0x3 : 0x2;
56 CB = ((flags & MMU_FLAG_CACHED) ? 0x2 : 0) | ((flags & MMU_FLAG_BUFFERED) ? 0x1 : 0);
57
58 index = vaddr / MB;
59 tt[index] = (paddr & ~(MB-1)) | (AP << 10) | (0<<5) | (CB << 2) | (2<<0); // section mapping
60
61 arm_invalidate_tlb();
62}
63
64void arm_mmu_init(void)
65{
66 int i;
67
68 /* set some mmu specific control bits */
69 arm_write_cr1(arm_read_cr1() & ~((1<<29)|(1<<28)|(1<<0))); // access flag disabled, TEX remap disabled, mmu disabled
70
71 /* set up an identity-mapped translation table with cache disabled */
72 for (i=0; i < 4096; i++) {
73 arm_mmu_map_section(i * MB, i * MB, MMU_FLAG_READWRITE); // map everything uncached
74 }
75
76 /* set up the translation table base */
77 arm_write_ttbr((uint32_t)tt);
78
79 /* set up the domain access register */
80 arm_write_dacr(0x00000001);
81
82 /* turn on the mmu */
83 arm_write_cr1(arm_read_cr1() | 0x1);
84}
85
86void arch_disable_mmu(void)
87{
88 arm_write_cr1(arm_read_cr1() & ~(1<<0)); // access flag disabled, TEX remap disabled, mmu disabled
89}
90
91#endif // ARM_WITH_MMU
92