]>
Commit | Line | Data |
---|---|---|
a7240d1e MC |
1 | /* |
2 | * QEMU RISC-V Board Compatible with SiFive Freedom U SDK | |
3 | * | |
4 | * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu | |
5 | * Copyright (c) 2017 SiFive, Inc. | |
7b6bb66f | 6 | * Copyright (c) 2019 Bin Meng <bmeng.cn@gmail.com> |
a7240d1e MC |
7 | * |
8 | * Provides a board compatible with the SiFive Freedom U SDK: | |
9 | * | |
10 | * 0) UART | |
11 | * 1) CLINT (Core Level Interruptor) | |
12 | * 2) PLIC (Platform Level Interrupt Controller) | |
af14c840 | 13 | * 3) PRCI (Power, Reset, Clock, Interrupt) |
8a88b9f5 BM |
14 | * 4) GPIO (General Purpose Input/Output Controller) |
15 | * 5) OTP (One-Time Programmable) memory with stored serial number | |
16 | * 6) GEM (Gigabit Ethernet Controller) and management block | |
834e027a | 17 | * 7) DMA (Direct Memory Access Controller) |
145b2991 | 18 | * 8) SPI0 connected to an SPI flash |
722f1352 | 19 | * 9) SPI2 connected to an SD card |
a7240d1e | 20 | * |
f3d47d58 | 21 | * This board currently generates devicetree dynamically that indicates at least |
ecdfe393 | 22 | * two harts and up to five harts. |
a7240d1e MC |
23 | * |
24 | * This program is free software; you can redistribute it and/or modify it | |
25 | * under the terms and conditions of the GNU General Public License, | |
26 | * version 2 or later, as published by the Free Software Foundation. | |
27 | * | |
28 | * This program is distributed in the hope it will be useful, but WITHOUT | |
29 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
30 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
31 | * more details. | |
32 | * | |
33 | * You should have received a copy of the GNU General Public License along with | |
34 | * this program. If not, see <http://www.gnu.org/licenses/>. | |
35 | */ | |
36 | ||
37 | #include "qemu/osdep.h" | |
a7240d1e MC |
38 | #include "qemu/error-report.h" |
39 | #include "qapi/error.h" | |
3ca109c3 | 40 | #include "qapi/visitor.h" |
a7240d1e | 41 | #include "hw/boards.h" |
5133ed17 | 42 | #include "hw/irq.h" |
a7240d1e MC |
43 | #include "hw/loader.h" |
44 | #include "hw/sysbus.h" | |
45 | #include "hw/char/serial.h" | |
ecdfe393 | 46 | #include "hw/cpu/cluster.h" |
7b6bb66f | 47 | #include "hw/misc/unimp.h" |
145b2991 | 48 | #include "hw/ssi/ssi.h" |
a7240d1e MC |
49 | #include "target/riscv/cpu.h" |
50 | #include "hw/riscv/riscv_hart.h" | |
a7240d1e | 51 | #include "hw/riscv/sifive_u.h" |
0ac24d56 | 52 | #include "hw/riscv/boot.h" |
b609b7e3 | 53 | #include "hw/char/sifive_uart.h" |
406fafd5 | 54 | #include "hw/intc/sifive_clint.h" |
84fcf3c1 | 55 | #include "hw/intc/sifive_plic.h" |
a7240d1e | 56 | #include "chardev/char.h" |
7b6bb66f | 57 | #include "net/eth.h" |
a7240d1e MC |
58 | #include "sysemu/arch_init.h" |
59 | #include "sysemu/device_tree.h" | |
5133ed17 | 60 | #include "sysemu/runstate.h" |
46517dd4 | 61 | #include "sysemu/sysemu.h" |
a7240d1e | 62 | |
5aec3247 MC |
63 | #include <libfdt.h> |
64 | ||
074ca702 BM |
65 | /* CLINT timebase frequency */ |
66 | #define CLINT_TIMEBASE_FREQ 1000000 | |
67 | ||
73261285 | 68 | static const MemMapEntry sifive_u_memmap[] = { |
13b8c354 EH |
69 | [SIFIVE_U_DEV_DEBUG] = { 0x0, 0x100 }, |
70 | [SIFIVE_U_DEV_MROM] = { 0x1000, 0xf000 }, | |
71 | [SIFIVE_U_DEV_CLINT] = { 0x2000000, 0x10000 }, | |
72 | [SIFIVE_U_DEV_L2CC] = { 0x2010000, 0x1000 }, | |
73 | [SIFIVE_U_DEV_PDMA] = { 0x3000000, 0x100000 }, | |
74 | [SIFIVE_U_DEV_L2LIM] = { 0x8000000, 0x2000000 }, | |
75 | [SIFIVE_U_DEV_PLIC] = { 0xc000000, 0x4000000 }, | |
76 | [SIFIVE_U_DEV_PRCI] = { 0x10000000, 0x1000 }, | |
77 | [SIFIVE_U_DEV_UART0] = { 0x10010000, 0x1000 }, | |
78 | [SIFIVE_U_DEV_UART1] = { 0x10011000, 0x1000 }, | |
145b2991 | 79 | [SIFIVE_U_DEV_QSPI0] = { 0x10040000, 0x1000 }, |
722f1352 | 80 | [SIFIVE_U_DEV_QSPI2] = { 0x10050000, 0x1000 }, |
13b8c354 EH |
81 | [SIFIVE_U_DEV_GPIO] = { 0x10060000, 0x1000 }, |
82 | [SIFIVE_U_DEV_OTP] = { 0x10070000, 0x1000 }, | |
83 | [SIFIVE_U_DEV_GEM] = { 0x10090000, 0x2000 }, | |
84 | [SIFIVE_U_DEV_GEM_MGMT] = { 0x100a0000, 0x1000 }, | |
85 | [SIFIVE_U_DEV_DMC] = { 0x100b0000, 0x10000 }, | |
86 | [SIFIVE_U_DEV_FLASH0] = { 0x20000000, 0x10000000 }, | |
87 | [SIFIVE_U_DEV_DRAM] = { 0x80000000, 0x0 }, | |
a7240d1e MC |
88 | }; |
89 | ||
5461c4fe | 90 | #define OTP_SERIAL 1 |
5a7f76a3 AF |
91 | #define GEM_REVISION 0x10070109 |
92 | ||
73261285 | 93 | static void create_fdt(SiFiveUState *s, const MemMapEntry *memmap, |
2206ffa6 | 94 | uint64_t mem_size, const char *cmdline, bool is_32_bit) |
a7240d1e | 95 | { |
ecdfe393 | 96 | MachineState *ms = MACHINE(qdev_get_machine()); |
a7240d1e MC |
97 | void *fdt; |
98 | int cpu; | |
99 | uint32_t *cells; | |
100 | char *nodename; | |
5133ed17 | 101 | uint32_t plic_phandle, prci_phandle, gpio_phandle, phandle = 1; |
7b6bb66f | 102 | uint32_t hfclk_phandle, rtcclk_phandle, phy_phandle; |
cb53b283 | 103 | static const char * const ethclk_names[2] = { "pclk", "hclk" }; |
7cfbb17f BM |
104 | static const char * const clint_compat[2] = { |
105 | "sifive,clint0", "riscv,clint0" | |
106 | }; | |
60bb5407 BM |
107 | static const char * const plic_compat[2] = { |
108 | "sifive,plic-1.0.0", "riscv,plic0" | |
109 | }; | |
a7240d1e | 110 | |
f2ce39b4 PB |
111 | if (ms->dtb) { |
112 | fdt = s->fdt = load_device_tree(ms->dtb, &s->fdt_size); | |
d5c90cf3 AP |
113 | if (!fdt) { |
114 | error_report("load_device_tree() failed"); | |
115 | exit(1); | |
116 | } | |
117 | goto update_bootargs; | |
118 | } else { | |
119 | fdt = s->fdt = create_device_tree(&s->fdt_size); | |
120 | if (!fdt) { | |
121 | error_report("create_device_tree() failed"); | |
122 | exit(1); | |
123 | } | |
a7240d1e MC |
124 | } |
125 | ||
d372e748 BM |
126 | qemu_fdt_setprop_string(fdt, "/", "model", "SiFive HiFive Unleashed A00"); |
127 | qemu_fdt_setprop_string(fdt, "/", "compatible", | |
128 | "sifive,hifive-unleashed-a00"); | |
a7240d1e MC |
129 | qemu_fdt_setprop_cell(fdt, "/", "#size-cells", 0x2); |
130 | qemu_fdt_setprop_cell(fdt, "/", "#address-cells", 0x2); | |
131 | ||
132 | qemu_fdt_add_subnode(fdt, "/soc"); | |
133 | qemu_fdt_setprop(fdt, "/soc", "ranges", NULL, 0); | |
2a1a6f6d | 134 | qemu_fdt_setprop_string(fdt, "/soc", "compatible", "simple-bus"); |
a7240d1e MC |
135 | qemu_fdt_setprop_cell(fdt, "/soc", "#size-cells", 0x2); |
136 | qemu_fdt_setprop_cell(fdt, "/soc", "#address-cells", 0x2); | |
137 | ||
e1724d09 BM |
138 | hfclk_phandle = phandle++; |
139 | nodename = g_strdup_printf("/hfclk"); | |
140 | qemu_fdt_add_subnode(fdt, nodename); | |
141 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", hfclk_phandle); | |
142 | qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "hfclk"); | |
143 | qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", | |
144 | SIFIVE_U_HFCLK_FREQ); | |
145 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); | |
146 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); | |
147 | g_free(nodename); | |
148 | ||
149 | rtcclk_phandle = phandle++; | |
150 | nodename = g_strdup_printf("/rtcclk"); | |
151 | qemu_fdt_add_subnode(fdt, nodename); | |
152 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", rtcclk_phandle); | |
153 | qemu_fdt_setprop_string(fdt, nodename, "clock-output-names", "rtcclk"); | |
154 | qemu_fdt_setprop_cell(fdt, nodename, "clock-frequency", | |
155 | SIFIVE_U_RTCCLK_FREQ); | |
156 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "fixed-clock"); | |
157 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x0); | |
158 | g_free(nodename); | |
159 | ||
a7240d1e | 160 | nodename = g_strdup_printf("/memory@%lx", |
13b8c354 | 161 | (long)memmap[SIFIVE_U_DEV_DRAM].base); |
a7240d1e MC |
162 | qemu_fdt_add_subnode(fdt, nodename); |
163 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
13b8c354 | 164 | memmap[SIFIVE_U_DEV_DRAM].base >> 32, memmap[SIFIVE_U_DEV_DRAM].base, |
a7240d1e MC |
165 | mem_size >> 32, mem_size); |
166 | qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory"); | |
167 | g_free(nodename); | |
168 | ||
169 | qemu_fdt_add_subnode(fdt, "/cpus"); | |
2a8756ed | 170 | qemu_fdt_setprop_cell(fdt, "/cpus", "timebase-frequency", |
074ca702 | 171 | CLINT_TIMEBASE_FREQ); |
a7240d1e MC |
172 | qemu_fdt_setprop_cell(fdt, "/cpus", "#size-cells", 0x0); |
173 | qemu_fdt_setprop_cell(fdt, "/cpus", "#address-cells", 0x1); | |
174 | ||
ecdfe393 | 175 | for (cpu = ms->smp.cpus - 1; cpu >= 0; cpu--) { |
382cb439 | 176 | int cpu_phandle = phandle++; |
a7240d1e MC |
177 | nodename = g_strdup_printf("/cpus/cpu@%d", cpu); |
178 | char *intc = g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); | |
ecdfe393 | 179 | char *isa; |
a7240d1e | 180 | qemu_fdt_add_subnode(fdt, nodename); |
ecdfe393 BM |
181 | /* cpu 0 is the management hart that does not have mmu */ |
182 | if (cpu != 0) { | |
2206ffa6 AF |
183 | if (is_32_bit) { |
184 | qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv32"); | |
185 | } else { | |
186 | qemu_fdt_setprop_string(fdt, nodename, "mmu-type", "riscv,sv48"); | |
187 | } | |
ecdfe393 BM |
188 | isa = riscv_isa_string(&s->soc.u_cpus.harts[cpu - 1]); |
189 | } else { | |
190 | isa = riscv_isa_string(&s->soc.e_cpus.harts[0]); | |
191 | } | |
a7240d1e MC |
192 | qemu_fdt_setprop_string(fdt, nodename, "riscv,isa", isa); |
193 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "riscv"); | |
194 | qemu_fdt_setprop_string(fdt, nodename, "status", "okay"); | |
195 | qemu_fdt_setprop_cell(fdt, nodename, "reg", cpu); | |
196 | qemu_fdt_setprop_string(fdt, nodename, "device_type", "cpu"); | |
197 | qemu_fdt_add_subnode(fdt, intc); | |
382cb439 | 198 | qemu_fdt_setprop_cell(fdt, intc, "phandle", cpu_phandle); |
a7240d1e MC |
199 | qemu_fdt_setprop_string(fdt, intc, "compatible", "riscv,cpu-intc"); |
200 | qemu_fdt_setprop(fdt, intc, "interrupt-controller", NULL, 0); | |
201 | qemu_fdt_setprop_cell(fdt, intc, "#interrupt-cells", 1); | |
202 | g_free(isa); | |
203 | g_free(intc); | |
204 | g_free(nodename); | |
205 | } | |
206 | ||
ecdfe393 BM |
207 | cells = g_new0(uint32_t, ms->smp.cpus * 4); |
208 | for (cpu = 0; cpu < ms->smp.cpus; cpu++) { | |
a7240d1e MC |
209 | nodename = |
210 | g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); | |
211 | uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); | |
212 | cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); | |
213 | cells[cpu * 4 + 1] = cpu_to_be32(IRQ_M_SOFT); | |
214 | cells[cpu * 4 + 2] = cpu_to_be32(intc_phandle); | |
215 | cells[cpu * 4 + 3] = cpu_to_be32(IRQ_M_TIMER); | |
216 | g_free(nodename); | |
217 | } | |
218 | nodename = g_strdup_printf("/soc/clint@%lx", | |
13b8c354 | 219 | (long)memmap[SIFIVE_U_DEV_CLINT].base); |
a7240d1e | 220 | qemu_fdt_add_subnode(fdt, nodename); |
7cfbb17f BM |
221 | qemu_fdt_setprop_string_array(fdt, nodename, "compatible", |
222 | (char **)&clint_compat, ARRAY_SIZE(clint_compat)); | |
a7240d1e | 223 | qemu_fdt_setprop_cells(fdt, nodename, "reg", |
13b8c354 EH |
224 | 0x0, memmap[SIFIVE_U_DEV_CLINT].base, |
225 | 0x0, memmap[SIFIVE_U_DEV_CLINT].size); | |
a7240d1e | 226 | qemu_fdt_setprop(fdt, nodename, "interrupts-extended", |
ecdfe393 | 227 | cells, ms->smp.cpus * sizeof(uint32_t) * 4); |
a7240d1e MC |
228 | g_free(cells); |
229 | g_free(nodename); | |
230 | ||
ea85f27d | 231 | nodename = g_strdup_printf("/soc/otp@%lx", |
13b8c354 | 232 | (long)memmap[SIFIVE_U_DEV_OTP].base); |
ea85f27d BM |
233 | qemu_fdt_add_subnode(fdt, nodename); |
234 | qemu_fdt_setprop_cell(fdt, nodename, "fuse-count", SIFIVE_U_OTP_REG_SIZE); | |
235 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
13b8c354 EH |
236 | 0x0, memmap[SIFIVE_U_DEV_OTP].base, |
237 | 0x0, memmap[SIFIVE_U_DEV_OTP].size); | |
ea85f27d BM |
238 | qemu_fdt_setprop_string(fdt, nodename, "compatible", |
239 | "sifive,fu540-c000-otp"); | |
240 | g_free(nodename); | |
241 | ||
af14c840 BM |
242 | prci_phandle = phandle++; |
243 | nodename = g_strdup_printf("/soc/clock-controller@%lx", | |
13b8c354 | 244 | (long)memmap[SIFIVE_U_DEV_PRCI].base); |
af14c840 BM |
245 | qemu_fdt_add_subnode(fdt, nodename); |
246 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", prci_phandle); | |
247 | qemu_fdt_setprop_cell(fdt, nodename, "#clock-cells", 0x1); | |
248 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", | |
249 | hfclk_phandle, rtcclk_phandle); | |
250 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
13b8c354 EH |
251 | 0x0, memmap[SIFIVE_U_DEV_PRCI].base, |
252 | 0x0, memmap[SIFIVE_U_DEV_PRCI].size); | |
af14c840 BM |
253 | qemu_fdt_setprop_string(fdt, nodename, "compatible", |
254 | "sifive,fu540-c000-prci"); | |
255 | g_free(nodename); | |
256 | ||
382cb439 | 257 | plic_phandle = phandle++; |
ecdfe393 BM |
258 | cells = g_new0(uint32_t, ms->smp.cpus * 4 - 2); |
259 | for (cpu = 0; cpu < ms->smp.cpus; cpu++) { | |
a7240d1e MC |
260 | nodename = |
261 | g_strdup_printf("/cpus/cpu@%d/interrupt-controller", cpu); | |
262 | uint32_t intc_phandle = qemu_fdt_get_phandle(fdt, nodename); | |
ecdfe393 BM |
263 | /* cpu 0 is the management hart that does not have S-mode */ |
264 | if (cpu == 0) { | |
265 | cells[0] = cpu_to_be32(intc_phandle); | |
266 | cells[1] = cpu_to_be32(IRQ_M_EXT); | |
267 | } else { | |
268 | cells[cpu * 4 - 2] = cpu_to_be32(intc_phandle); | |
269 | cells[cpu * 4 - 1] = cpu_to_be32(IRQ_M_EXT); | |
270 | cells[cpu * 4 + 0] = cpu_to_be32(intc_phandle); | |
271 | cells[cpu * 4 + 1] = cpu_to_be32(IRQ_S_EXT); | |
272 | } | |
a7240d1e MC |
273 | g_free(nodename); |
274 | } | |
275 | nodename = g_strdup_printf("/soc/interrupt-controller@%lx", | |
13b8c354 | 276 | (long)memmap[SIFIVE_U_DEV_PLIC].base); |
a7240d1e MC |
277 | qemu_fdt_add_subnode(fdt, nodename); |
278 | qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 1); | |
60bb5407 BM |
279 | qemu_fdt_setprop_string_array(fdt, nodename, "compatible", |
280 | (char **)&plic_compat, ARRAY_SIZE(plic_compat)); | |
a7240d1e MC |
281 | qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); |
282 | qemu_fdt_setprop(fdt, nodename, "interrupts-extended", | |
ecdfe393 | 283 | cells, (ms->smp.cpus * 4 - 2) * sizeof(uint32_t)); |
a7240d1e | 284 | qemu_fdt_setprop_cells(fdt, nodename, "reg", |
13b8c354 EH |
285 | 0x0, memmap[SIFIVE_U_DEV_PLIC].base, |
286 | 0x0, memmap[SIFIVE_U_DEV_PLIC].size); | |
98ceee7f | 287 | qemu_fdt_setprop_cell(fdt, nodename, "riscv,ndev", 0x35); |
04e7edd1 | 288 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", plic_phandle); |
a7240d1e MC |
289 | plic_phandle = qemu_fdt_get_phandle(fdt, nodename); |
290 | g_free(cells); | |
291 | g_free(nodename); | |
292 | ||
5133ed17 | 293 | gpio_phandle = phandle++; |
8a88b9f5 | 294 | nodename = g_strdup_printf("/soc/gpio@%lx", |
13b8c354 | 295 | (long)memmap[SIFIVE_U_DEV_GPIO].base); |
8a88b9f5 | 296 | qemu_fdt_add_subnode(fdt, nodename); |
5133ed17 | 297 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", gpio_phandle); |
8a88b9f5 BM |
298 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", |
299 | prci_phandle, PRCI_CLK_TLCLK); | |
300 | qemu_fdt_setprop_cell(fdt, nodename, "#interrupt-cells", 2); | |
301 | qemu_fdt_setprop(fdt, nodename, "interrupt-controller", NULL, 0); | |
302 | qemu_fdt_setprop_cell(fdt, nodename, "#gpio-cells", 2); | |
303 | qemu_fdt_setprop(fdt, nodename, "gpio-controller", NULL, 0); | |
304 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
13b8c354 EH |
305 | 0x0, memmap[SIFIVE_U_DEV_GPIO].base, |
306 | 0x0, memmap[SIFIVE_U_DEV_GPIO].size); | |
8a88b9f5 BM |
307 | qemu_fdt_setprop_cells(fdt, nodename, "interrupts", SIFIVE_U_GPIO_IRQ0, |
308 | SIFIVE_U_GPIO_IRQ1, SIFIVE_U_GPIO_IRQ2, SIFIVE_U_GPIO_IRQ3, | |
309 | SIFIVE_U_GPIO_IRQ4, SIFIVE_U_GPIO_IRQ5, SIFIVE_U_GPIO_IRQ6, | |
310 | SIFIVE_U_GPIO_IRQ7, SIFIVE_U_GPIO_IRQ8, SIFIVE_U_GPIO_IRQ9, | |
311 | SIFIVE_U_GPIO_IRQ10, SIFIVE_U_GPIO_IRQ11, SIFIVE_U_GPIO_IRQ12, | |
312 | SIFIVE_U_GPIO_IRQ13, SIFIVE_U_GPIO_IRQ14, SIFIVE_U_GPIO_IRQ15); | |
313 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); | |
314 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,gpio0"); | |
315 | g_free(nodename); | |
316 | ||
5133ed17 BM |
317 | nodename = g_strdup_printf("/gpio-restart"); |
318 | qemu_fdt_add_subnode(fdt, nodename); | |
319 | qemu_fdt_setprop_cells(fdt, nodename, "gpios", gpio_phandle, 10, 1); | |
320 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "gpio-restart"); | |
321 | g_free(nodename); | |
322 | ||
834e027a | 323 | nodename = g_strdup_printf("/soc/dma@%lx", |
13b8c354 | 324 | (long)memmap[SIFIVE_U_DEV_PDMA].base); |
834e027a BM |
325 | qemu_fdt_add_subnode(fdt, nodename); |
326 | qemu_fdt_setprop_cell(fdt, nodename, "#dma-cells", 1); | |
327 | qemu_fdt_setprop_cells(fdt, nodename, "interrupts", | |
328 | SIFIVE_U_PDMA_IRQ0, SIFIVE_U_PDMA_IRQ1, SIFIVE_U_PDMA_IRQ2, | |
329 | SIFIVE_U_PDMA_IRQ3, SIFIVE_U_PDMA_IRQ4, SIFIVE_U_PDMA_IRQ5, | |
330 | SIFIVE_U_PDMA_IRQ6, SIFIVE_U_PDMA_IRQ7); | |
331 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); | |
332 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
13b8c354 EH |
333 | 0x0, memmap[SIFIVE_U_DEV_PDMA].base, |
334 | 0x0, memmap[SIFIVE_U_DEV_PDMA].size); | |
834e027a BM |
335 | qemu_fdt_setprop_string(fdt, nodename, "compatible", |
336 | "sifive,fu540-c000-pdma"); | |
337 | g_free(nodename); | |
338 | ||
6eaf9cf5 | 339 | nodename = g_strdup_printf("/soc/cache-controller@%lx", |
13b8c354 | 340 | (long)memmap[SIFIVE_U_DEV_L2CC].base); |
6eaf9cf5 BM |
341 | qemu_fdt_add_subnode(fdt, nodename); |
342 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
13b8c354 EH |
343 | 0x0, memmap[SIFIVE_U_DEV_L2CC].base, |
344 | 0x0, memmap[SIFIVE_U_DEV_L2CC].size); | |
6eaf9cf5 BM |
345 | qemu_fdt_setprop_cells(fdt, nodename, "interrupts", |
346 | SIFIVE_U_L2CC_IRQ0, SIFIVE_U_L2CC_IRQ1, SIFIVE_U_L2CC_IRQ2); | |
347 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); | |
348 | qemu_fdt_setprop(fdt, nodename, "cache-unified", NULL, 0); | |
349 | qemu_fdt_setprop_cell(fdt, nodename, "cache-size", 2097152); | |
350 | qemu_fdt_setprop_cell(fdt, nodename, "cache-sets", 1024); | |
351 | qemu_fdt_setprop_cell(fdt, nodename, "cache-level", 2); | |
352 | qemu_fdt_setprop_cell(fdt, nodename, "cache-block-size", 64); | |
353 | qemu_fdt_setprop_string(fdt, nodename, "compatible", | |
354 | "sifive,fu540-c000-ccache"); | |
355 | g_free(nodename); | |
356 | ||
722f1352 BM |
357 | nodename = g_strdup_printf("/soc/spi@%lx", |
358 | (long)memmap[SIFIVE_U_DEV_QSPI2].base); | |
359 | qemu_fdt_add_subnode(fdt, nodename); | |
360 | qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); | |
361 | qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); | |
362 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", | |
363 | prci_phandle, PRCI_CLK_TLCLK); | |
364 | qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI2_IRQ); | |
365 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); | |
366 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
367 | 0x0, memmap[SIFIVE_U_DEV_QSPI2].base, | |
368 | 0x0, memmap[SIFIVE_U_DEV_QSPI2].size); | |
369 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0"); | |
370 | g_free(nodename); | |
371 | ||
372 | nodename = g_strdup_printf("/soc/spi@%lx/mmc@0", | |
373 | (long)memmap[SIFIVE_U_DEV_QSPI2].base); | |
374 | qemu_fdt_add_subnode(fdt, nodename); | |
375 | qemu_fdt_setprop(fdt, nodename, "disable-wp", NULL, 0); | |
376 | qemu_fdt_setprop_cells(fdt, nodename, "voltage-ranges", 3300, 3300); | |
377 | qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 20000000); | |
378 | qemu_fdt_setprop_cell(fdt, nodename, "reg", 0); | |
379 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "mmc-spi-slot"); | |
380 | g_free(nodename); | |
381 | ||
145b2991 BM |
382 | nodename = g_strdup_printf("/soc/spi@%lx", |
383 | (long)memmap[SIFIVE_U_DEV_QSPI0].base); | |
384 | qemu_fdt_add_subnode(fdt, nodename); | |
385 | qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); | |
386 | qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); | |
387 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", | |
388 | prci_phandle, PRCI_CLK_TLCLK); | |
389 | qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_QSPI0_IRQ); | |
390 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); | |
391 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
392 | 0x0, memmap[SIFIVE_U_DEV_QSPI0].base, | |
393 | 0x0, memmap[SIFIVE_U_DEV_QSPI0].size); | |
394 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,spi0"); | |
395 | g_free(nodename); | |
396 | ||
397 | nodename = g_strdup_printf("/soc/spi@%lx/flash@0", | |
398 | (long)memmap[SIFIVE_U_DEV_QSPI0].base); | |
399 | qemu_fdt_add_subnode(fdt, nodename); | |
400 | qemu_fdt_setprop_cell(fdt, nodename, "spi-rx-bus-width", 4); | |
401 | qemu_fdt_setprop_cell(fdt, nodename, "spi-tx-bus-width", 4); | |
402 | qemu_fdt_setprop(fdt, nodename, "m25p,fast-read", NULL, 0); | |
403 | qemu_fdt_setprop_cell(fdt, nodename, "spi-max-frequency", 50000000); | |
404 | qemu_fdt_setprop_cell(fdt, nodename, "reg", 0); | |
405 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "jedec,spi-nor"); | |
406 | g_free(nodename); | |
407 | ||
7b6bb66f | 408 | phy_phandle = phandle++; |
5a7f76a3 | 409 | nodename = g_strdup_printf("/soc/ethernet@%lx", |
13b8c354 | 410 | (long)memmap[SIFIVE_U_DEV_GEM].base); |
5a7f76a3 | 411 | qemu_fdt_add_subnode(fdt, nodename); |
7b6bb66f BM |
412 | qemu_fdt_setprop_string(fdt, nodename, "compatible", |
413 | "sifive,fu540-c000-gem"); | |
5a7f76a3 | 414 | qemu_fdt_setprop_cells(fdt, nodename, "reg", |
13b8c354 EH |
415 | 0x0, memmap[SIFIVE_U_DEV_GEM].base, |
416 | 0x0, memmap[SIFIVE_U_DEV_GEM].size, | |
417 | 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].base, | |
418 | 0x0, memmap[SIFIVE_U_DEV_GEM_MGMT].size); | |
5a7f76a3 AF |
419 | qemu_fdt_setprop_string(fdt, nodename, "reg-names", "control"); |
420 | qemu_fdt_setprop_string(fdt, nodename, "phy-mode", "gmii"); | |
7b6bb66f | 421 | qemu_fdt_setprop_cell(fdt, nodename, "phy-handle", phy_phandle); |
04e7edd1 BM |
422 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); |
423 | qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_GEM_IRQ); | |
fe93582c | 424 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", |
806c64b7 | 425 | prci_phandle, PRCI_CLK_GEMGXLPLL, prci_phandle, PRCI_CLK_GEMGXLPLL); |
cb53b283 BM |
426 | qemu_fdt_setprop_string_array(fdt, nodename, "clock-names", |
427 | (char **)ðclk_names, ARRAY_SIZE(ethclk_names)); | |
7b6bb66f BM |
428 | qemu_fdt_setprop(fdt, nodename, "local-mac-address", |
429 | s->soc.gem.conf.macaddr.a, ETH_ALEN); | |
04e7edd1 BM |
430 | qemu_fdt_setprop_cell(fdt, nodename, "#address-cells", 1); |
431 | qemu_fdt_setprop_cell(fdt, nodename, "#size-cells", 0); | |
c3a28b5d BM |
432 | |
433 | qemu_fdt_add_subnode(fdt, "/aliases"); | |
434 | qemu_fdt_setprop_string(fdt, "/aliases", "ethernet0", nodename); | |
435 | ||
5a7f76a3 AF |
436 | g_free(nodename); |
437 | ||
438 | nodename = g_strdup_printf("/soc/ethernet@%lx/ethernet-phy@0", | |
13b8c354 | 439 | (long)memmap[SIFIVE_U_DEV_GEM].base); |
5a7f76a3 | 440 | qemu_fdt_add_subnode(fdt, nodename); |
7b6bb66f | 441 | qemu_fdt_setprop_cell(fdt, nodename, "phandle", phy_phandle); |
04e7edd1 | 442 | qemu_fdt_setprop_cell(fdt, nodename, "reg", 0x0); |
5a7f76a3 AF |
443 | g_free(nodename); |
444 | ||
10b43754 AP |
445 | nodename = g_strdup_printf("/soc/serial@%lx", |
446 | (long)memmap[SIFIVE_U_DEV_UART1].base); | |
447 | qemu_fdt_add_subnode(fdt, nodename); | |
448 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); | |
449 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
450 | 0x0, memmap[SIFIVE_U_DEV_UART1].base, | |
451 | 0x0, memmap[SIFIVE_U_DEV_UART1].size); | |
452 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", | |
453 | prci_phandle, PRCI_CLK_TLCLK); | |
454 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); | |
455 | qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART1_IRQ); | |
456 | ||
457 | qemu_fdt_setprop_string(fdt, "/aliases", "serial1", nodename); | |
458 | g_free(nodename); | |
459 | ||
5f7134d3 | 460 | nodename = g_strdup_printf("/soc/serial@%lx", |
13b8c354 | 461 | (long)memmap[SIFIVE_U_DEV_UART0].base); |
a7240d1e MC |
462 | qemu_fdt_add_subnode(fdt, nodename); |
463 | qemu_fdt_setprop_string(fdt, nodename, "compatible", "sifive,uart0"); | |
464 | qemu_fdt_setprop_cells(fdt, nodename, "reg", | |
13b8c354 EH |
465 | 0x0, memmap[SIFIVE_U_DEV_UART0].base, |
466 | 0x0, memmap[SIFIVE_U_DEV_UART0].size); | |
806c64b7 BM |
467 | qemu_fdt_setprop_cells(fdt, nodename, "clocks", |
468 | prci_phandle, PRCI_CLK_TLCLK); | |
04e7edd1 BM |
469 | qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); |
470 | qemu_fdt_setprop_cell(fdt, nodename, "interrupts", SIFIVE_U_UART0_IRQ); | |
a7240d1e MC |
471 | |
472 | qemu_fdt_add_subnode(fdt, "/chosen"); | |
473 | qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); | |
44e6dcd3 GR |
474 | qemu_fdt_setprop_string(fdt, "/aliases", "serial0", nodename); |
475 | ||
a7240d1e | 476 | g_free(nodename); |
d5c90cf3 AP |
477 | |
478 | update_bootargs: | |
479 | if (cmdline) { | |
480 | qemu_fdt_setprop_string(fdt, "/chosen", "bootargs", cmdline); | |
481 | } | |
a7240d1e MC |
482 | } |
483 | ||
5133ed17 BM |
484 | static void sifive_u_machine_reset(void *opaque, int n, int level) |
485 | { | |
486 | /* gpio pin active low triggers reset */ | |
487 | if (!level) { | |
488 | qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET); | |
489 | } | |
490 | } | |
491 | ||
523e3464 | 492 | static void sifive_u_machine_init(MachineState *machine) |
a7240d1e | 493 | { |
73261285 | 494 | const MemMapEntry *memmap = sifive_u_memmap; |
687caef1 | 495 | SiFiveUState *s = RISCV_U_MACHINE(machine); |
5aec3247 | 496 | MemoryRegion *system_memory = get_system_memory(); |
a7240d1e | 497 | MemoryRegion *main_mem = g_new(MemoryRegion, 1); |
1b3a2308 | 498 | MemoryRegion *flash0 = g_new(MemoryRegion, 1); |
13b8c354 | 499 | target_ulong start_addr = memmap[SIFIVE_U_DEV_DRAM].base; |
38bc4e34 | 500 | target_ulong firmware_end_addr, kernel_start_addr; |
8590f536 | 501 | uint32_t start_addr_hi32 = 0x00000000; |
5aec3247 | 502 | int i; |
66b1205b | 503 | uint32_t fdt_load_addr; |
dc144fe1 | 504 | uint64_t kernel_entry; |
145b2991 | 505 | DriveInfo *dinfo; |
722f1352 BM |
506 | DeviceState *flash_dev, *sd_dev; |
507 | qemu_irq flash_cs, sd_cs; | |
a7240d1e | 508 | |
2308092b | 509 | /* Initialize SoC */ |
9fc7fc4d | 510 | object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_U_SOC); |
5325cc34 MA |
511 | object_property_set_uint(OBJECT(&s->soc), "serial", s->serial, |
512 | &error_abort); | |
099be035 AF |
513 | object_property_set_str(OBJECT(&s->soc), "cpu-type", machine->cpu_type, |
514 | &error_abort); | |
ce189ab2 | 515 | qdev_realize(DEVICE(&s->soc), NULL, &error_abort); |
a7240d1e MC |
516 | |
517 | /* register RAM */ | |
518 | memory_region_init_ram(main_mem, NULL, "riscv.sifive.u.ram", | |
519 | machine->ram_size, &error_fatal); | |
13b8c354 | 520 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_DRAM].base, |
2308092b | 521 | main_mem); |
a7240d1e | 522 | |
1b3a2308 AF |
523 | /* register QSPI0 Flash */ |
524 | memory_region_init_ram(flash0, NULL, "riscv.sifive.u.flash0", | |
13b8c354 EH |
525 | memmap[SIFIVE_U_DEV_FLASH0].size, &error_fatal); |
526 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_FLASH0].base, | |
1b3a2308 AF |
527 | flash0); |
528 | ||
5133ed17 BM |
529 | /* register gpio-restart */ |
530 | qdev_connect_gpio_out(DEVICE(&(s->soc.gpio)), 10, | |
531 | qemu_allocate_irq(sifive_u_machine_reset, NULL, 0)); | |
532 | ||
a7240d1e | 533 | /* create device tree */ |
2206ffa6 | 534 | create_fdt(s, memmap, machine->ram_size, machine->kernel_cmdline, |
a8259b53 | 535 | riscv_is_32bit(&s->soc.u_cpus)); |
a7240d1e | 536 | |
17aad9f2 BM |
537 | if (s->start_in_flash) { |
538 | /* | |
539 | * If start_in_flash property is given, assign s->msel to a value | |
540 | * that representing booting from QSPI0 memory-mapped flash. | |
541 | * | |
542 | * This also means that when both start_in_flash and msel properties | |
543 | * are given, start_in_flash takes the precedence over msel. | |
544 | * | |
545 | * Note this is to keep backward compatibility not to break existing | |
546 | * users that use start_in_flash property. | |
547 | */ | |
548 | s->msel = MSEL_MEMMAP_QSPI0_FLASH; | |
549 | } | |
550 | ||
551 | switch (s->msel) { | |
552 | case MSEL_MEMMAP_QSPI0_FLASH: | |
13b8c354 | 553 | start_addr = memmap[SIFIVE_U_DEV_FLASH0].base; |
17aad9f2 BM |
554 | break; |
555 | case MSEL_L2LIM_QSPI0_FLASH: | |
556 | case MSEL_L2LIM_QSPI2_SD: | |
13b8c354 | 557 | start_addr = memmap[SIFIVE_U_DEV_L2LIM].base; |
17aad9f2 BM |
558 | break; |
559 | default: | |
13b8c354 | 560 | start_addr = memmap[SIFIVE_U_DEV_DRAM].base; |
17aad9f2 BM |
561 | break; |
562 | } | |
563 | ||
a8259b53 | 564 | if (riscv_is_32bit(&s->soc.u_cpus)) { |
2206ffa6 | 565 | firmware_end_addr = riscv_find_and_load_firmware(machine, |
a0acd0a1 | 566 | RISCV32_BIOS_BIN, start_addr, NULL); |
2206ffa6 AF |
567 | } else { |
568 | firmware_end_addr = riscv_find_and_load_firmware(machine, | |
a0acd0a1 | 569 | RISCV64_BIOS_BIN, start_addr, NULL); |
2206ffa6 | 570 | } |
b3042223 | 571 | |
a7240d1e | 572 | if (machine->kernel_filename) { |
a8259b53 | 573 | kernel_start_addr = riscv_calc_kernel_start_addr(&s->soc.u_cpus, |
38bc4e34 AF |
574 | firmware_end_addr); |
575 | ||
576 | kernel_entry = riscv_load_kernel(machine->kernel_filename, | |
577 | kernel_start_addr, NULL); | |
0f8d4462 GR |
578 | |
579 | if (machine->initrd_filename) { | |
580 | hwaddr start; | |
581 | hwaddr end = riscv_load_initrd(machine->initrd_filename, | |
582 | machine->ram_size, kernel_entry, | |
583 | &start); | |
9f79638e | 584 | qemu_fdt_setprop_cell(s->fdt, "/chosen", |
0f8d4462 | 585 | "linux,initrd-start", start); |
9f79638e | 586 | qemu_fdt_setprop_cell(s->fdt, "/chosen", "linux,initrd-end", |
0f8d4462 GR |
587 | end); |
588 | } | |
dc144fe1 AP |
589 | } else { |
590 | /* | |
591 | * If dynamic firmware is used, it doesn't know where is the next mode | |
592 | * if kernel argument is not set. | |
593 | */ | |
594 | kernel_entry = 0; | |
a7240d1e MC |
595 | } |
596 | ||
66b1205b | 597 | /* Compute the fdt load address in dram */ |
13b8c354 | 598 | fdt_load_addr = riscv_load_fdt(memmap[SIFIVE_U_DEV_DRAM].base, |
66b1205b | 599 | machine->ram_size, s->fdt); |
a8259b53 | 600 | if (!riscv_is_32bit(&s->soc.u_cpus)) { |
2206ffa6 AF |
601 | start_addr_hi32 = (uint64_t)start_addr >> 32; |
602 | } | |
66b1205b | 603 | |
a7240d1e | 604 | /* reset vector */ |
66b1205b | 605 | uint32_t reset_vec[11] = { |
17aad9f2 | 606 | s->msel, /* MSEL pin state */ |
dc144fe1 AP |
607 | 0x00000297, /* 1: auipc t0, %pcrel_hi(fw_dyn) */ |
608 | 0x02828613, /* addi a2, t0, %pcrel_lo(1b) */ | |
a7240d1e | 609 | 0xf1402573, /* csrr a0, mhartid */ |
2206ffa6 AF |
610 | 0, |
611 | 0, | |
a7240d1e | 612 | 0x00028067, /* jr t0 */ |
fc41ae23 | 613 | start_addr, /* start: .dword */ |
8590f536 | 614 | start_addr_hi32, |
66b1205b AP |
615 | fdt_load_addr, /* fdt_laddr: .dword */ |
616 | 0x00000000, | |
dc144fe1 | 617 | /* fw_dyn: */ |
a7240d1e | 618 | }; |
a8259b53 | 619 | if (riscv_is_32bit(&s->soc.u_cpus)) { |
2206ffa6 AF |
620 | reset_vec[4] = 0x0202a583; /* lw a1, 32(t0) */ |
621 | reset_vec[5] = 0x0182a283; /* lw t0, 24(t0) */ | |
622 | } else { | |
623 | reset_vec[4] = 0x0202b583; /* ld a1, 32(t0) */ | |
624 | reset_vec[5] = 0x0182b283; /* ld t0, 24(t0) */ | |
625 | } | |
626 | ||
a7240d1e | 627 | |
5aec3247 | 628 | /* copy in the reset vector in little_endian byte order */ |
66b1205b | 629 | for (i = 0; i < ARRAY_SIZE(reset_vec); i++) { |
5aec3247 MC |
630 | reset_vec[i] = cpu_to_le32(reset_vec[i]); |
631 | } | |
632 | rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec), | |
13b8c354 | 633 | memmap[SIFIVE_U_DEV_MROM].base, &address_space_memory); |
dc144fe1 | 634 | |
78936771 | 635 | riscv_rom_copy_firmware_info(machine, memmap[SIFIVE_U_DEV_MROM].base, |
13b8c354 | 636 | memmap[SIFIVE_U_DEV_MROM].size, |
dc144fe1 | 637 | sizeof(reset_vec), kernel_entry); |
145b2991 BM |
638 | |
639 | /* Connect an SPI flash to SPI0 */ | |
640 | flash_dev = qdev_new("is25wp256"); | |
641 | dinfo = drive_get_next(IF_MTD); | |
642 | if (dinfo) { | |
643 | qdev_prop_set_drive_err(flash_dev, "drive", | |
644 | blk_by_legacy_dinfo(dinfo), | |
645 | &error_fatal); | |
646 | } | |
647 | qdev_realize_and_unref(flash_dev, BUS(s->soc.spi0.spi), &error_fatal); | |
648 | ||
649 | flash_cs = qdev_get_gpio_in_named(flash_dev, SSI_GPIO_CS, 0); | |
650 | sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi0), 1, flash_cs); | |
722f1352 BM |
651 | |
652 | /* Connect an SD card to SPI2 */ | |
653 | sd_dev = ssi_create_peripheral(s->soc.spi2.spi, "ssi-sd"); | |
654 | ||
655 | sd_cs = qdev_get_gpio_in_named(sd_dev, SSI_GPIO_CS, 0); | |
656 | sysbus_connect_irq(SYS_BUS_DEVICE(&s->soc.spi2), 1, sd_cs); | |
2308092b AF |
657 | } |
658 | ||
523e3464 AF |
659 | static bool sifive_u_machine_get_start_in_flash(Object *obj, Error **errp) |
660 | { | |
661 | SiFiveUState *s = RISCV_U_MACHINE(obj); | |
662 | ||
663 | return s->start_in_flash; | |
664 | } | |
665 | ||
666 | static void sifive_u_machine_set_start_in_flash(Object *obj, bool value, Error **errp) | |
667 | { | |
668 | SiFiveUState *s = RISCV_U_MACHINE(obj); | |
669 | ||
670 | s->start_in_flash = value; | |
671 | } | |
672 | ||
3e9667cd BM |
673 | static void sifive_u_machine_get_uint32_prop(Object *obj, Visitor *v, |
674 | const char *name, void *opaque, | |
675 | Error **errp) | |
3ca109c3 BM |
676 | { |
677 | visit_type_uint32(v, name, (uint32_t *)opaque, errp); | |
678 | } | |
679 | ||
3e9667cd BM |
680 | static void sifive_u_machine_set_uint32_prop(Object *obj, Visitor *v, |
681 | const char *name, void *opaque, | |
682 | Error **errp) | |
3ca109c3 BM |
683 | { |
684 | visit_type_uint32(v, name, (uint32_t *)opaque, errp); | |
685 | } | |
686 | ||
523e3464 AF |
687 | static void sifive_u_machine_instance_init(Object *obj) |
688 | { | |
689 | SiFiveUState *s = RISCV_U_MACHINE(obj); | |
690 | ||
691 | s->start_in_flash = false; | |
cfa32630 BM |
692 | s->msel = 0; |
693 | object_property_add(obj, "msel", "uint32", | |
694 | sifive_u_machine_get_uint32_prop, | |
695 | sifive_u_machine_set_uint32_prop, NULL, &s->msel); | |
696 | object_property_set_description(obj, "msel", | |
697 | "Mode Select (MSEL[3:0]) pin state"); | |
698 | ||
3ca109c3 | 699 | s->serial = OTP_SERIAL; |
d2623129 | 700 | object_property_add(obj, "serial", "uint32", |
3e9667cd BM |
701 | sifive_u_machine_get_uint32_prop, |
702 | sifive_u_machine_set_uint32_prop, NULL, &s->serial); | |
7eecec7d | 703 | object_property_set_description(obj, "serial", "Board serial number"); |
523e3464 AF |
704 | } |
705 | ||
706 | static void sifive_u_machine_class_init(ObjectClass *oc, void *data) | |
707 | { | |
708 | MachineClass *mc = MACHINE_CLASS(oc); | |
709 | ||
710 | mc->desc = "RISC-V Board compatible with SiFive U SDK"; | |
711 | mc->init = sifive_u_machine_init; | |
712 | mc->max_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + SIFIVE_U_COMPUTE_CPU_COUNT; | |
713 | mc->min_cpus = SIFIVE_U_MANAGEMENT_CPU_COUNT + 1; | |
1eaada8a | 714 | mc->default_cpu_type = SIFIVE_U_CPU; |
523e3464 | 715 | mc->default_cpus = mc->min_cpus; |
418b473e EH |
716 | |
717 | object_class_property_add_bool(oc, "start-in-flash", | |
718 | sifive_u_machine_get_start_in_flash, | |
719 | sifive_u_machine_set_start_in_flash); | |
720 | object_class_property_set_description(oc, "start-in-flash", | |
721 | "Set on to tell QEMU's ROM to jump to " | |
722 | "flash. Otherwise QEMU will jump to DRAM " | |
723 | "or L2LIM depending on the msel value"); | |
523e3464 AF |
724 | } |
725 | ||
726 | static const TypeInfo sifive_u_machine_typeinfo = { | |
727 | .name = MACHINE_TYPE_NAME("sifive_u"), | |
728 | .parent = TYPE_MACHINE, | |
729 | .class_init = sifive_u_machine_class_init, | |
730 | .instance_init = sifive_u_machine_instance_init, | |
731 | .instance_size = sizeof(SiFiveUState), | |
732 | }; | |
733 | ||
734 | static void sifive_u_machine_init_register_types(void) | |
735 | { | |
736 | type_register_static(&sifive_u_machine_typeinfo); | |
737 | } | |
738 | ||
739 | type_init(sifive_u_machine_init_register_types) | |
740 | ||
139177b1 | 741 | static void sifive_u_soc_instance_init(Object *obj) |
2308092b AF |
742 | { |
743 | SiFiveUSoCState *s = RISCV_U_SOC(obj); | |
744 | ||
9fc7fc4d | 745 | object_initialize_child(obj, "e-cluster", &s->e_cluster, TYPE_CPU_CLUSTER); |
ecdfe393 BM |
746 | qdev_prop_set_uint32(DEVICE(&s->e_cluster), "cluster-id", 0); |
747 | ||
db873cc5 MA |
748 | object_initialize_child(OBJECT(&s->e_cluster), "e-cpus", &s->e_cpus, |
749 | TYPE_RISCV_HART_ARRAY); | |
ecdfe393 BM |
750 | qdev_prop_set_uint32(DEVICE(&s->e_cpus), "num-harts", 1); |
751 | qdev_prop_set_uint32(DEVICE(&s->e_cpus), "hartid-base", 0); | |
752 | qdev_prop_set_string(DEVICE(&s->e_cpus), "cpu-type", SIFIVE_E_CPU); | |
73f6ed97 | 753 | qdev_prop_set_uint64(DEVICE(&s->e_cpus), "resetvec", 0x1004); |
ecdfe393 | 754 | |
9fc7fc4d | 755 | object_initialize_child(obj, "u-cluster", &s->u_cluster, TYPE_CPU_CLUSTER); |
ecdfe393 BM |
756 | qdev_prop_set_uint32(DEVICE(&s->u_cluster), "cluster-id", 1); |
757 | ||
db873cc5 MA |
758 | object_initialize_child(OBJECT(&s->u_cluster), "u-cpus", &s->u_cpus, |
759 | TYPE_RISCV_HART_ARRAY); | |
5a7f76a3 | 760 | |
db873cc5 MA |
761 | object_initialize_child(obj, "prci", &s->prci, TYPE_SIFIVE_U_PRCI); |
762 | object_initialize_child(obj, "otp", &s->otp, TYPE_SIFIVE_U_OTP); | |
763 | object_initialize_child(obj, "gem", &s->gem, TYPE_CADENCE_GEM); | |
8a88b9f5 | 764 | object_initialize_child(obj, "gpio", &s->gpio, TYPE_SIFIVE_GPIO); |
834e027a | 765 | object_initialize_child(obj, "pdma", &s->dma, TYPE_SIFIVE_PDMA); |
145b2991 | 766 | object_initialize_child(obj, "spi0", &s->spi0, TYPE_SIFIVE_SPI); |
722f1352 | 767 | object_initialize_child(obj, "spi2", &s->spi2, TYPE_SIFIVE_SPI); |
2308092b AF |
768 | } |
769 | ||
139177b1 | 770 | static void sifive_u_soc_realize(DeviceState *dev, Error **errp) |
2308092b | 771 | { |
c4473127 | 772 | MachineState *ms = MACHINE(qdev_get_machine()); |
2308092b | 773 | SiFiveUSoCState *s = RISCV_U_SOC(dev); |
73261285 | 774 | const MemMapEntry *memmap = sifive_u_memmap; |
2308092b AF |
775 | MemoryRegion *system_memory = get_system_memory(); |
776 | MemoryRegion *mask_rom = g_new(MemoryRegion, 1); | |
a6902ef0 | 777 | MemoryRegion *l2lim_mem = g_new(MemoryRegion, 1); |
05446f41 BM |
778 | char *plic_hart_config; |
779 | size_t plic_hart_config_len; | |
5a7f76a3 | 780 | int i; |
5a7f76a3 | 781 | NICInfo *nd = &nd_table[0]; |
2308092b | 782 | |
099be035 AF |
783 | qdev_prop_set_uint32(DEVICE(&s->u_cpus), "num-harts", ms->smp.cpus - 1); |
784 | qdev_prop_set_uint32(DEVICE(&s->u_cpus), "hartid-base", 1); | |
785 | qdev_prop_set_string(DEVICE(&s->u_cpus), "cpu-type", s->cpu_type); | |
786 | qdev_prop_set_uint64(DEVICE(&s->u_cpus), "resetvec", 0x1004); | |
787 | ||
db873cc5 MA |
788 | sysbus_realize(SYS_BUS_DEVICE(&s->e_cpus), &error_abort); |
789 | sysbus_realize(SYS_BUS_DEVICE(&s->u_cpus), &error_abort); | |
ecdfe393 BM |
790 | /* |
791 | * The cluster must be realized after the RISC-V hart array container, | |
792 | * as the container's CPU object is only created on realize, and the | |
793 | * CPU must exist and have been parented into the cluster before the | |
794 | * cluster is realized. | |
795 | */ | |
ce189ab2 MA |
796 | qdev_realize(DEVICE(&s->e_cluster), NULL, &error_abort); |
797 | qdev_realize(DEVICE(&s->u_cluster), NULL, &error_abort); | |
2308092b AF |
798 | |
799 | /* boot rom */ | |
414c47d2 | 800 | memory_region_init_rom(mask_rom, OBJECT(dev), "riscv.sifive.u.mrom", |
13b8c354 EH |
801 | memmap[SIFIVE_U_DEV_MROM].size, &error_fatal); |
802 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_MROM].base, | |
2308092b | 803 | mask_rom); |
a7240d1e | 804 | |
a6902ef0 AF |
805 | /* |
806 | * Add L2-LIM at reset size. | |
807 | * This should be reduced in size as the L2 Cache Controller WayEnable | |
808 | * register is incremented. Unfortunately I don't see a nice (or any) way | |
809 | * to handle reducing or blocking out the L2 LIM while still allowing it | |
810 | * be re returned to all enabled after a reset. For the time being, just | |
811 | * leave it enabled all the time. This won't break anything, but will be | |
812 | * too generous to misbehaving guests. | |
813 | */ | |
814 | memory_region_init_ram(l2lim_mem, NULL, "riscv.sifive.u.l2lim", | |
13b8c354 EH |
815 | memmap[SIFIVE_U_DEV_L2LIM].size, &error_fatal); |
816 | memory_region_add_subregion(system_memory, memmap[SIFIVE_U_DEV_L2LIM].base, | |
a6902ef0 AF |
817 | l2lim_mem); |
818 | ||
05446f41 | 819 | /* create PLIC hart topology configuration string */ |
c4473127 LX |
820 | plic_hart_config_len = (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1) * |
821 | ms->smp.cpus; | |
05446f41 | 822 | plic_hart_config = g_malloc0(plic_hart_config_len); |
c4473127 | 823 | for (i = 0; i < ms->smp.cpus; i++) { |
05446f41 | 824 | if (i != 0) { |
ef965ce2 BM |
825 | strncat(plic_hart_config, "," SIFIVE_U_PLIC_HART_CONFIG, |
826 | plic_hart_config_len); | |
827 | } else { | |
828 | strncat(plic_hart_config, "M", plic_hart_config_len); | |
05446f41 | 829 | } |
05446f41 BM |
830 | plic_hart_config_len -= (strlen(SIFIVE_U_PLIC_HART_CONFIG) + 1); |
831 | } | |
832 | ||
a7240d1e | 833 | /* MMIO */ |
13b8c354 | 834 | s->plic = sifive_plic_create(memmap[SIFIVE_U_DEV_PLIC].base, |
c9270e10 | 835 | plic_hart_config, 0, |
a7240d1e MC |
836 | SIFIVE_U_PLIC_NUM_SOURCES, |
837 | SIFIVE_U_PLIC_NUM_PRIORITIES, | |
838 | SIFIVE_U_PLIC_PRIORITY_BASE, | |
839 | SIFIVE_U_PLIC_PENDING_BASE, | |
840 | SIFIVE_U_PLIC_ENABLE_BASE, | |
841 | SIFIVE_U_PLIC_ENABLE_STRIDE, | |
842 | SIFIVE_U_PLIC_CONTEXT_BASE, | |
843 | SIFIVE_U_PLIC_CONTEXT_STRIDE, | |
13b8c354 | 844 | memmap[SIFIVE_U_DEV_PLIC].size); |
bb8136df | 845 | g_free(plic_hart_config); |
13b8c354 | 846 | sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART0].base, |
647a70a1 | 847 | serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART0_IRQ)); |
13b8c354 | 848 | sifive_uart_create(system_memory, memmap[SIFIVE_U_DEV_UART1].base, |
194eef09 | 849 | serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_UART1_IRQ)); |
13b8c354 EH |
850 | sifive_clint_create(memmap[SIFIVE_U_DEV_CLINT].base, |
851 | memmap[SIFIVE_U_DEV_CLINT].size, 0, ms->smp.cpus, | |
a47ef6e9 | 852 | SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE, |
074ca702 | 853 | CLINT_TIMEBASE_FREQ, false); |
5a7f76a3 | 854 | |
cbe3a8c5 MA |
855 | if (!sysbus_realize(SYS_BUS_DEVICE(&s->prci), errp)) { |
856 | return; | |
857 | } | |
13b8c354 | 858 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->prci), 0, memmap[SIFIVE_U_DEV_PRCI].base); |
af14c840 | 859 | |
8a88b9f5 | 860 | qdev_prop_set_uint32(DEVICE(&s->gpio), "ngpio", 16); |
cbe3a8c5 MA |
861 | if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) { |
862 | return; | |
863 | } | |
13b8c354 | 864 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_U_DEV_GPIO].base); |
8a88b9f5 BM |
865 | |
866 | /* Pass all GPIOs to the SOC layer so they are available to the board */ | |
867 | qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL); | |
868 | ||
869 | /* Connect GPIO interrupts to the PLIC */ | |
870 | for (i = 0; i < 16; i++) { | |
871 | sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i, | |
872 | qdev_get_gpio_in(DEVICE(s->plic), | |
873 | SIFIVE_U_GPIO_IRQ0 + i)); | |
834e027a BM |
874 | } |
875 | ||
876 | /* PDMA */ | |
877 | sysbus_realize(SYS_BUS_DEVICE(&s->dma), errp); | |
13b8c354 | 878 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->dma), 0, memmap[SIFIVE_U_DEV_PDMA].base); |
834e027a BM |
879 | |
880 | /* Connect PDMA interrupts to the PLIC */ | |
881 | for (i = 0; i < SIFIVE_PDMA_IRQS; i++) { | |
882 | sysbus_connect_irq(SYS_BUS_DEVICE(&s->dma), i, | |
883 | qdev_get_gpio_in(DEVICE(s->plic), | |
884 | SIFIVE_U_PDMA_IRQ0 + i)); | |
8a88b9f5 BM |
885 | } |
886 | ||
fda5b000 | 887 | qdev_prop_set_uint32(DEVICE(&s->otp), "serial", s->serial); |
cbe3a8c5 MA |
888 | if (!sysbus_realize(SYS_BUS_DEVICE(&s->otp), errp)) { |
889 | return; | |
890 | } | |
13b8c354 | 891 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->otp), 0, memmap[SIFIVE_U_DEV_OTP].base); |
5461c4fe | 892 | |
7ad36e2e | 893 | /* FIXME use qdev NIC properties instead of nd_table[] */ |
5a7f76a3 AF |
894 | if (nd->used) { |
895 | qemu_check_nic_model(nd, TYPE_CADENCE_GEM); | |
896 | qdev_set_nic_properties(DEVICE(&s->gem), nd); | |
897 | } | |
5325cc34 | 898 | object_property_set_int(OBJECT(&s->gem), "revision", GEM_REVISION, |
5a7f76a3 | 899 | &error_abort); |
668f62ec | 900 | if (!sysbus_realize(SYS_BUS_DEVICE(&s->gem), errp)) { |
5a7f76a3 AF |
901 | return; |
902 | } | |
13b8c354 | 903 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->gem), 0, memmap[SIFIVE_U_DEV_GEM].base); |
5a7f76a3 | 904 | sysbus_connect_irq(SYS_BUS_DEVICE(&s->gem), 0, |
5874f0a7 | 905 | qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_GEM_IRQ)); |
7b6bb66f BM |
906 | |
907 | create_unimplemented_device("riscv.sifive.u.gem-mgmt", | |
13b8c354 | 908 | memmap[SIFIVE_U_DEV_GEM_MGMT].base, memmap[SIFIVE_U_DEV_GEM_MGMT].size); |
3eaea6eb BM |
909 | |
910 | create_unimplemented_device("riscv.sifive.u.dmc", | |
13b8c354 | 911 | memmap[SIFIVE_U_DEV_DMC].base, memmap[SIFIVE_U_DEV_DMC].size); |
6eaf9cf5 BM |
912 | |
913 | create_unimplemented_device("riscv.sifive.u.l2cc", | |
13b8c354 | 914 | memmap[SIFIVE_U_DEV_L2CC].base, memmap[SIFIVE_U_DEV_L2CC].size); |
145b2991 BM |
915 | |
916 | sysbus_realize(SYS_BUS_DEVICE(&s->spi0), errp); | |
917 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi0), 0, | |
918 | memmap[SIFIVE_U_DEV_QSPI0].base); | |
919 | sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi0), 0, | |
920 | qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI0_IRQ)); | |
722f1352 BM |
921 | sysbus_realize(SYS_BUS_DEVICE(&s->spi2), errp); |
922 | sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi2), 0, | |
923 | memmap[SIFIVE_U_DEV_QSPI2].base); | |
924 | sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi2), 0, | |
925 | qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_U_QSPI2_IRQ)); | |
a7240d1e MC |
926 | } |
927 | ||
139177b1 | 928 | static Property sifive_u_soc_props[] = { |
fda5b000 | 929 | DEFINE_PROP_UINT32("serial", SiFiveUSoCState, serial, OTP_SERIAL), |
099be035 | 930 | DEFINE_PROP_STRING("cpu-type", SiFiveUSoCState, cpu_type), |
fda5b000 AF |
931 | DEFINE_PROP_END_OF_LIST() |
932 | }; | |
933 | ||
139177b1 | 934 | static void sifive_u_soc_class_init(ObjectClass *oc, void *data) |
2308092b AF |
935 | { |
936 | DeviceClass *dc = DEVICE_CLASS(oc); | |
937 | ||
139177b1 BM |
938 | device_class_set_props(dc, sifive_u_soc_props); |
939 | dc->realize = sifive_u_soc_realize; | |
2308092b AF |
940 | /* Reason: Uses serial_hds in realize function, thus can't be used twice */ |
941 | dc->user_creatable = false; | |
942 | } | |
943 | ||
139177b1 | 944 | static const TypeInfo sifive_u_soc_type_info = { |
2308092b AF |
945 | .name = TYPE_RISCV_U_SOC, |
946 | .parent = TYPE_DEVICE, | |
947 | .instance_size = sizeof(SiFiveUSoCState), | |
139177b1 BM |
948 | .instance_init = sifive_u_soc_instance_init, |
949 | .class_init = sifive_u_soc_class_init, | |
2308092b AF |
950 | }; |
951 | ||
139177b1 | 952 | static void sifive_u_soc_register_types(void) |
2308092b | 953 | { |
139177b1 | 954 | type_register_static(&sifive_u_soc_type_info); |
2308092b AF |
955 | } |
956 | ||
139177b1 | 957 | type_init(sifive_u_soc_register_types) |