2
0

sifive_u.c 40 KB

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