2
0

sifive_u.c 41 KB

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