blob: f9f8a2f278000cc4dab15b9ecf16a571cb7a968d [file] [log] [blame]
Mark Kettenis58d51272021-10-23 16:58:03 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2021 Mark Kettenis <kettenis@openbsd.org>
4 */
5
6#include <common.h>
7#include <dm.h>
8#include <efi_loader.h>
9
10#include <asm/armv8/mmu.h>
11#include <asm/global_data.h>
12#include <asm/io.h>
13#include <asm/system.h>
14
15DECLARE_GLOBAL_DATA_PTR;
16
Mark Kettenis73c82c82022-02-08 22:00:09 +010017/* Apple M1 */
18
19static struct mm_region t8103_mem_map[] = {
Mark Kettenis58d51272021-10-23 16:58:03 +020020 {
21 /* I/O */
22 .virt = 0x200000000,
23 .phys = 0x200000000,
Mark Kettenis73c82c82022-02-08 22:00:09 +010024 .size = 2UL * SZ_1G,
25 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
26 PTE_BLOCK_NON_SHARE |
27 PTE_BLOCK_PXN | PTE_BLOCK_UXN
28 }, {
29 /* I/O */
30 .virt = 0x380000000,
31 .phys = 0x380000000,
32 .size = SZ_1G,
Mark Kettenis58d51272021-10-23 16:58:03 +020033 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
34 PTE_BLOCK_NON_SHARE |
35 PTE_BLOCK_PXN | PTE_BLOCK_UXN
36 }, {
37 /* I/O */
38 .virt = 0x500000000,
39 .phys = 0x500000000,
Mark Kettenis73c82c82022-02-08 22:00:09 +010040 .size = SZ_1G,
Mark Kettenis58d51272021-10-23 16:58:03 +020041 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
42 PTE_BLOCK_NON_SHARE |
43 PTE_BLOCK_PXN | PTE_BLOCK_UXN
44 }, {
45 /* I/O */
46 .virt = 0x680000000,
47 .phys = 0x680000000,
48 .size = SZ_512M,
49 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
50 PTE_BLOCK_NON_SHARE |
51 PTE_BLOCK_PXN | PTE_BLOCK_UXN
52 }, {
53 /* PCIE */
54 .virt = 0x6a0000000,
55 .phys = 0x6a0000000,
56 .size = SZ_512M,
57 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE) |
58 PTE_BLOCK_INNER_SHARE |
59 PTE_BLOCK_PXN | PTE_BLOCK_UXN
60 }, {
61 /* PCIE */
62 .virt = 0x6c0000000,
63 .phys = 0x6c0000000,
64 .size = SZ_1G,
65 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE) |
66 PTE_BLOCK_INNER_SHARE |
67 PTE_BLOCK_PXN | PTE_BLOCK_UXN
68 }, {
69 /* RAM */
70 .virt = 0x800000000,
71 .phys = 0x800000000,
72 .size = 8UL * SZ_1G,
73 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
74 PTE_BLOCK_INNER_SHARE
75 }, {
Mark Kettenis73c82c82022-02-08 22:00:09 +010076 /* Framebuffer */
77 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL_NC) |
78 PTE_BLOCK_INNER_SHARE |
79 PTE_BLOCK_PXN | PTE_BLOCK_UXN
80 }, {
81 /* List terminator */
Mark Kettenis58d51272021-10-23 16:58:03 +020082 0,
Mark Kettenis73c82c82022-02-08 22:00:09 +010083 }
84};
85
86/* Apple M1 Pro/Max */
87
88static struct mm_region t6000_mem_map[] = {
89 {
90 /* I/O */
91 .virt = 0x280000000,
92 .phys = 0x280000000,
93 .size = SZ_1G,
94 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
95 PTE_BLOCK_NON_SHARE |
96 PTE_BLOCK_PXN | PTE_BLOCK_UXN
97 }, {
98 /* I/O */
99 .virt = 0x380000000,
100 .phys = 0x380000000,
101 .size = SZ_1G,
102 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
103 PTE_BLOCK_NON_SHARE |
104 PTE_BLOCK_PXN | PTE_BLOCK_UXN
105 }, {
106 /* I/O */
107 .virt = 0x580000000,
108 .phys = 0x580000000,
109 .size = SZ_512M,
110 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
111 PTE_BLOCK_NON_SHARE |
112 PTE_BLOCK_PXN | PTE_BLOCK_UXN
113 }, {
114 /* PCIE */
115 .virt = 0x5a0000000,
116 .phys = 0x5a0000000,
117 .size = SZ_512M,
118 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE) |
119 PTE_BLOCK_INNER_SHARE |
120 PTE_BLOCK_PXN | PTE_BLOCK_UXN
121 }, {
122 /* PCIE */
123 .virt = 0x5c0000000,
124 .phys = 0x5c0000000,
125 .size = SZ_1G,
126 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE) |
127 PTE_BLOCK_INNER_SHARE |
128 PTE_BLOCK_PXN | PTE_BLOCK_UXN
129 }, {
130 /* I/O */
131 .virt = 0x700000000,
132 .phys = 0x700000000,
133 .size = SZ_1G,
134 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
135 PTE_BLOCK_NON_SHARE |
136 PTE_BLOCK_PXN | PTE_BLOCK_UXN
137 }, {
138 /* I/O */
139 .virt = 0xb00000000,
140 .phys = 0xb00000000,
141 .size = SZ_1G,
142 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
143 PTE_BLOCK_NON_SHARE |
144 PTE_BLOCK_PXN | PTE_BLOCK_UXN
145 }, {
146 /* I/O */
147 .virt = 0xf00000000,
148 .phys = 0xf00000000,
149 .size = SZ_1G,
150 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
151 PTE_BLOCK_NON_SHARE |
152 PTE_BLOCK_PXN | PTE_BLOCK_UXN
153 }, {
154 /* I/O */
155 .virt = 0x1300000000,
156 .phys = 0x1300000000,
157 .size = SZ_1G,
158 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
159 PTE_BLOCK_NON_SHARE |
160 PTE_BLOCK_PXN | PTE_BLOCK_UXN
161 }, {
162 /* RAM */
163 .virt = 0x10000000000,
164 .phys = 0x10000000000,
165 .size = 16UL * SZ_1G,
166 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
167 PTE_BLOCK_INNER_SHARE
168 }, {
169 /* Framebuffer */
170 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL_NC) |
171 PTE_BLOCK_INNER_SHARE |
172 PTE_BLOCK_PXN | PTE_BLOCK_UXN
Mark Kettenis58d51272021-10-23 16:58:03 +0200173 }, {
174 /* List terminator */
175 0,
176 }
177};
178
Mark Kettenis73c82c82022-02-08 22:00:09 +0100179struct mm_region *mem_map;
Mark Kettenis58d51272021-10-23 16:58:03 +0200180
181int board_init(void)
182{
183 return 0;
184}
185
186int dram_init(void)
187{
Mark Kettenis73c82c82022-02-08 22:00:09 +0100188 return fdtdec_setup_mem_size_base();
189}
190
191int dram_init_banksize(void)
192{
193 return fdtdec_setup_memory_banksize();
194}
195
196extern long fw_dtb_pointer;
197
198void *board_fdt_blob_setup(int *err)
199{
200 /* Return DTB pointer passed by m1n1 */
201 *err = 0;
202 return (void *)fw_dtb_pointer;
203}
204
205void build_mem_map(void)
206{
Mark Kettenis58d51272021-10-23 16:58:03 +0200207 ofnode node;
Mark Kettenis58d51272021-10-23 16:58:03 +0200208 fdt_addr_t base;
209 fdt_size_t size;
Mark Kettenis73c82c82022-02-08 22:00:09 +0100210 int i;
211
212 if (of_machine_is_compatible("apple,t8103"))
213 mem_map = t8103_mem_map;
214 else if (of_machine_is_compatible("apple,t6000"))
215 mem_map = t6000_mem_map;
216 else if (of_machine_is_compatible("apple,t6001"))
217 mem_map = t6000_mem_map;
218 else
219 panic("Unsupported SoC\n");
Mark Kettenis58d51272021-10-23 16:58:03 +0200220
Mark Kettenis73c82c82022-02-08 22:00:09 +0100221 /* Find list terminator. */
222 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
223 ;
224
225 /* Align RAM mapping to page boundaries */
226 base = gd->bd->bi_dram[0].start;
227 size = gd->bd->bi_dram[0].size;
228 size += (base - ALIGN_DOWN(base, SZ_4K));
229 base = ALIGN_DOWN(base, SZ_4K);
230 size = ALIGN(size, SZ_4K);
Mark Kettenis58d51272021-10-23 16:58:03 +0200231
232 /* Update RAM mapping */
Mark Kettenis73c82c82022-02-08 22:00:09 +0100233 mem_map[i - 2].virt = base;
234 mem_map[i - 2].phys = base;
235 mem_map[i - 2].size = size;
Mark Kettenis58d51272021-10-23 16:58:03 +0200236
237 node = ofnode_path("/chosen/framebuffer");
238 if (!ofnode_valid(node))
Mark Kettenis73c82c82022-02-08 22:00:09 +0100239 return;
Mark Kettenis58d51272021-10-23 16:58:03 +0200240
241 base = ofnode_get_addr_size(node, "reg", &size);
242 if (base == FDT_ADDR_T_NONE)
Mark Kettenis73c82c82022-02-08 22:00:09 +0100243 return;
Mark Kettenis58d51272021-10-23 16:58:03 +0200244
Mark Kettenis73c82c82022-02-08 22:00:09 +0100245 /* Align framebuffer mapping to page boundaries */
246 size += (base - ALIGN_DOWN(base, SZ_4K));
247 base = ALIGN_DOWN(base, SZ_4K);
248 size = ALIGN(size, SZ_4K);
Mark Kettenis58d51272021-10-23 16:58:03 +0200249
Mark Kettenis73c82c82022-02-08 22:00:09 +0100250 /* Add framebuffer mapping */
251 mem_map[i - 1].virt = base;
252 mem_map[i - 1].phys = base;
253 mem_map[i - 1].size = size;
Mark Kettenis58d51272021-10-23 16:58:03 +0200254}
255
Mark Kettenis73c82c82022-02-08 22:00:09 +0100256void enable_caches(void)
Mark Kettenis58d51272021-10-23 16:58:03 +0200257{
Mark Kettenis73c82c82022-02-08 22:00:09 +0100258 build_mem_map();
Mark Kettenis58d51272021-10-23 16:58:03 +0200259
Mark Kettenis73c82c82022-02-08 22:00:09 +0100260 icache_enable();
261 dcache_enable();
Mark Kettenis58d51272021-10-23 16:58:03 +0200262}
263
Mark Kettenis73c82c82022-02-08 22:00:09 +0100264u64 get_page_table_size(void)
Mark Kettenis58d51272021-10-23 16:58:03 +0200265{
Mark Kettenis73c82c82022-02-08 22:00:09 +0100266 return SZ_256K;
Mark Kettenis58d51272021-10-23 16:58:03 +0200267}