sifive_e.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * QEMU RISC-V Board Compatible with SiFive Freedom E SDK
  3. *
  4. * Copyright (c) 2017 SiFive, Inc.
  5. *
  6. * Provides a board compatible with the SiFive Freedom E SDK:
  7. *
  8. * 0) UART
  9. * 1) CLINT (Core Level Interruptor)
  10. * 2) PLIC (Platform Level Interrupt Controller)
  11. * 3) PRCI (Power, Reset, Clock, Interrupt)
  12. * 4) Registers emulated as RAM: AON, GPIO, QSPI, PWM
  13. * 5) Flash memory emulated as RAM
  14. *
  15. * The Mask ROM reset vector jumps to the flash payload at 0x2040_0000.
  16. * The OTP ROM and Flash boot code will be emulated in a future version.
  17. *
  18. * This program is free software; you can redistribute it and/or modify it
  19. * under the terms and conditions of the GNU General Public License,
  20. * version 2 or later, as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope it will be useful, but WITHOUT
  23. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  24. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  25. * more details.
  26. *
  27. * You should have received a copy of the GNU General Public License along with
  28. * this program. If not, see <http://www.gnu.org/licenses/>.
  29. */
  30. #include "qemu/osdep.h"
  31. #include "qemu/cutils.h"
  32. #include "qemu/error-report.h"
  33. #include "qapi/error.h"
  34. #include "hw/boards.h"
  35. #include "hw/loader.h"
  36. #include "hw/sysbus.h"
  37. #include "hw/misc/unimp.h"
  38. #include "target/riscv/cpu.h"
  39. #include "hw/riscv/riscv_hart.h"
  40. #include "hw/riscv/sifive_e.h"
  41. #include "hw/riscv/boot.h"
  42. #include "hw/char/sifive_uart.h"
  43. #include "hw/intc/riscv_aclint.h"
  44. #include "hw/intc/sifive_plic.h"
  45. #include "hw/misc/sifive_e_prci.h"
  46. #include "hw/misc/sifive_e_aon.h"
  47. #include "chardev/char.h"
  48. #include "system/system.h"
  49. static const MemMapEntry sifive_e_memmap[] = {
  50. [SIFIVE_E_DEV_DEBUG] = { 0x0, 0x1000 },
  51. [SIFIVE_E_DEV_MROM] = { 0x1000, 0x2000 },
  52. [SIFIVE_E_DEV_OTP] = { 0x20000, 0x2000 },
  53. [SIFIVE_E_DEV_CLINT] = { 0x2000000, 0x10000 },
  54. [SIFIVE_E_DEV_PLIC] = { 0xc000000, 0x4000000 },
  55. [SIFIVE_E_DEV_AON] = { 0x10000000, 0x8000 },
  56. [SIFIVE_E_DEV_PRCI] = { 0x10008000, 0x8000 },
  57. [SIFIVE_E_DEV_OTP_CTRL] = { 0x10010000, 0x1000 },
  58. [SIFIVE_E_DEV_GPIO0] = { 0x10012000, 0x1000 },
  59. [SIFIVE_E_DEV_UART0] = { 0x10013000, 0x1000 },
  60. [SIFIVE_E_DEV_QSPI0] = { 0x10014000, 0x1000 },
  61. [SIFIVE_E_DEV_PWM0] = { 0x10015000, 0x1000 },
  62. [SIFIVE_E_DEV_UART1] = { 0x10023000, 0x1000 },
  63. [SIFIVE_E_DEV_QSPI1] = { 0x10024000, 0x1000 },
  64. [SIFIVE_E_DEV_PWM1] = { 0x10025000, 0x1000 },
  65. [SIFIVE_E_DEV_QSPI2] = { 0x10034000, 0x1000 },
  66. [SIFIVE_E_DEV_PWM2] = { 0x10035000, 0x1000 },
  67. [SIFIVE_E_DEV_XIP] = { 0x20000000, 0x20000000 },
  68. [SIFIVE_E_DEV_DTIM] = { 0x80000000, 0x4000 }
  69. };
  70. static void sifive_e_machine_init(MachineState *machine)
  71. {
  72. MachineClass *mc = MACHINE_GET_CLASS(machine);
  73. const MemMapEntry *memmap = sifive_e_memmap;
  74. SiFiveEState *s = RISCV_E_MACHINE(machine);
  75. MemoryRegion *sys_mem = get_system_memory();
  76. int i;
  77. RISCVBootInfo boot_info;
  78. if (machine->ram_size != mc->default_ram_size) {
  79. char *sz = size_to_str(mc->default_ram_size);
  80. error_report("Invalid RAM size, should be %s", sz);
  81. g_free(sz);
  82. exit(EXIT_FAILURE);
  83. }
  84. /* Initialize SoC */
  85. object_initialize_child(OBJECT(machine), "soc", &s->soc, TYPE_RISCV_E_SOC);
  86. qdev_realize(DEVICE(&s->soc), NULL, &error_fatal);
  87. /* Data Tightly Integrated Memory */
  88. memory_region_add_subregion(sys_mem,
  89. memmap[SIFIVE_E_DEV_DTIM].base, machine->ram);
  90. /* Mask ROM reset vector */
  91. uint32_t reset_vec[4];
  92. if (s->revb) {
  93. reset_vec[1] = 0x200102b7; /* 0x1004: lui t0,0x20010 */
  94. } else {
  95. reset_vec[1] = 0x204002b7; /* 0x1004: lui t0,0x20400 */
  96. }
  97. reset_vec[2] = 0x00028067; /* 0x1008: jr t0 */
  98. reset_vec[0] = reset_vec[3] = 0;
  99. /* copy in the reset vector in little_endian byte order */
  100. for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
  101. reset_vec[i] = cpu_to_le32(reset_vec[i]);
  102. }
  103. rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
  104. memmap[SIFIVE_E_DEV_MROM].base, &address_space_memory);
  105. riscv_boot_info_init(&boot_info, &s->soc.cpus);
  106. if (machine->kernel_filename) {
  107. riscv_load_kernel(machine, &boot_info,
  108. memmap[SIFIVE_E_DEV_DTIM].base,
  109. false, NULL);
  110. }
  111. }
  112. static bool sifive_e_machine_get_revb(Object *obj, Error **errp)
  113. {
  114. SiFiveEState *s = RISCV_E_MACHINE(obj);
  115. return s->revb;
  116. }
  117. static void sifive_e_machine_set_revb(Object *obj, bool value, Error **errp)
  118. {
  119. SiFiveEState *s = RISCV_E_MACHINE(obj);
  120. s->revb = value;
  121. }
  122. static void sifive_e_machine_instance_init(Object *obj)
  123. {
  124. SiFiveEState *s = RISCV_E_MACHINE(obj);
  125. s->revb = false;
  126. }
  127. static void sifive_e_machine_class_init(ObjectClass *oc, void *data)
  128. {
  129. MachineClass *mc = MACHINE_CLASS(oc);
  130. mc->desc = "RISC-V Board compatible with SiFive E SDK";
  131. mc->init = sifive_e_machine_init;
  132. mc->max_cpus = 1;
  133. mc->default_cpu_type = SIFIVE_E_CPU;
  134. mc->default_ram_id = "riscv.sifive.e.ram";
  135. mc->default_ram_size = sifive_e_memmap[SIFIVE_E_DEV_DTIM].size;
  136. object_class_property_add_bool(oc, "revb", sifive_e_machine_get_revb,
  137. sifive_e_machine_set_revb);
  138. object_class_property_set_description(oc, "revb",
  139. "Set on to tell QEMU that it should model "
  140. "the revB HiFive1 board");
  141. }
  142. static const TypeInfo sifive_e_machine_typeinfo = {
  143. .name = MACHINE_TYPE_NAME("sifive_e"),
  144. .parent = TYPE_MACHINE,
  145. .class_init = sifive_e_machine_class_init,
  146. .instance_init = sifive_e_machine_instance_init,
  147. .instance_size = sizeof(SiFiveEState),
  148. };
  149. static void sifive_e_machine_init_register_types(void)
  150. {
  151. type_register_static(&sifive_e_machine_typeinfo);
  152. }
  153. type_init(sifive_e_machine_init_register_types)
  154. static void sifive_e_soc_init(Object *obj)
  155. {
  156. MachineState *ms = MACHINE(qdev_get_machine());
  157. SiFiveESoCState *s = RISCV_E_SOC(obj);
  158. object_initialize_child(obj, "cpus", &s->cpus, TYPE_RISCV_HART_ARRAY);
  159. object_property_set_int(OBJECT(&s->cpus), "num-harts", ms->smp.cpus,
  160. &error_abort);
  161. object_property_set_int(OBJECT(&s->cpus), "resetvec", 0x1004, &error_abort);
  162. object_initialize_child(obj, "riscv.sifive.e.gpio0", &s->gpio,
  163. TYPE_SIFIVE_GPIO);
  164. object_initialize_child(obj, "riscv.sifive.e.aon", &s->aon,
  165. TYPE_SIFIVE_E_AON);
  166. }
  167. static void sifive_e_soc_realize(DeviceState *dev, Error **errp)
  168. {
  169. MachineState *ms = MACHINE(qdev_get_machine());
  170. const MemMapEntry *memmap = sifive_e_memmap;
  171. SiFiveESoCState *s = RISCV_E_SOC(dev);
  172. MemoryRegion *sys_mem = get_system_memory();
  173. object_property_set_str(OBJECT(&s->cpus), "cpu-type", ms->cpu_type,
  174. &error_abort);
  175. sysbus_realize(SYS_BUS_DEVICE(&s->cpus), &error_fatal);
  176. /* Mask ROM */
  177. memory_region_init_rom(&s->mask_rom, OBJECT(dev), "riscv.sifive.e.mrom",
  178. memmap[SIFIVE_E_DEV_MROM].size, &error_fatal);
  179. memory_region_add_subregion(sys_mem,
  180. memmap[SIFIVE_E_DEV_MROM].base, &s->mask_rom);
  181. /* MMIO */
  182. s->plic = sifive_plic_create(memmap[SIFIVE_E_DEV_PLIC].base,
  183. (char *)SIFIVE_E_PLIC_HART_CONFIG, ms->smp.cpus, 0,
  184. SIFIVE_E_PLIC_NUM_SOURCES,
  185. SIFIVE_E_PLIC_NUM_PRIORITIES,
  186. SIFIVE_E_PLIC_PRIORITY_BASE,
  187. SIFIVE_E_PLIC_PENDING_BASE,
  188. SIFIVE_E_PLIC_ENABLE_BASE,
  189. SIFIVE_E_PLIC_ENABLE_STRIDE,
  190. SIFIVE_E_PLIC_CONTEXT_BASE,
  191. SIFIVE_E_PLIC_CONTEXT_STRIDE,
  192. memmap[SIFIVE_E_DEV_PLIC].size);
  193. riscv_aclint_swi_create(memmap[SIFIVE_E_DEV_CLINT].base,
  194. 0, ms->smp.cpus, false);
  195. riscv_aclint_mtimer_create(memmap[SIFIVE_E_DEV_CLINT].base +
  196. RISCV_ACLINT_SWI_SIZE,
  197. RISCV_ACLINT_DEFAULT_MTIMER_SIZE, 0, ms->smp.cpus,
  198. RISCV_ACLINT_DEFAULT_MTIMECMP, RISCV_ACLINT_DEFAULT_MTIME,
  199. SIFIVE_E_LFCLK_DEFAULT_FREQ, false);
  200. sifive_e_prci_create(memmap[SIFIVE_E_DEV_PRCI].base);
  201. /* AON */
  202. if (!sysbus_realize(SYS_BUS_DEVICE(&s->aon), errp)) {
  203. return;
  204. }
  205. /* Map AON registers */
  206. sysbus_mmio_map(SYS_BUS_DEVICE(&s->aon), 0, memmap[SIFIVE_E_DEV_AON].base);
  207. /* GPIO */
  208. if (!sysbus_realize(SYS_BUS_DEVICE(&s->gpio), errp)) {
  209. return;
  210. }
  211. /* Map GPIO registers */
  212. sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_E_DEV_GPIO0].base);
  213. /* Pass all GPIOs to the SOC layer so they are available to the board */
  214. qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
  215. /* Connect GPIO interrupts to the PLIC */
  216. for (int i = 0; i < 32; i++) {
  217. sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
  218. qdev_get_gpio_in(DEVICE(s->plic),
  219. SIFIVE_E_GPIO0_IRQ0 + i));
  220. }
  221. sysbus_connect_irq(SYS_BUS_DEVICE(&s->aon), 0,
  222. qdev_get_gpio_in(DEVICE(s->plic),
  223. SIFIVE_E_AON_WDT_IRQ));
  224. sifive_uart_create(sys_mem, memmap[SIFIVE_E_DEV_UART0].base,
  225. serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_E_UART0_IRQ));
  226. create_unimplemented_device("riscv.sifive.e.qspi0",
  227. memmap[SIFIVE_E_DEV_QSPI0].base, memmap[SIFIVE_E_DEV_QSPI0].size);
  228. create_unimplemented_device("riscv.sifive.e.pwm0",
  229. memmap[SIFIVE_E_DEV_PWM0].base, memmap[SIFIVE_E_DEV_PWM0].size);
  230. sifive_uart_create(sys_mem, memmap[SIFIVE_E_DEV_UART1].base,
  231. serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_E_UART1_IRQ));
  232. create_unimplemented_device("riscv.sifive.e.qspi1",
  233. memmap[SIFIVE_E_DEV_QSPI1].base, memmap[SIFIVE_E_DEV_QSPI1].size);
  234. create_unimplemented_device("riscv.sifive.e.pwm1",
  235. memmap[SIFIVE_E_DEV_PWM1].base, memmap[SIFIVE_E_DEV_PWM1].size);
  236. create_unimplemented_device("riscv.sifive.e.qspi2",
  237. memmap[SIFIVE_E_DEV_QSPI2].base, memmap[SIFIVE_E_DEV_QSPI2].size);
  238. create_unimplemented_device("riscv.sifive.e.pwm2",
  239. memmap[SIFIVE_E_DEV_PWM2].base, memmap[SIFIVE_E_DEV_PWM2].size);
  240. /* Flash memory */
  241. memory_region_init_rom(&s->xip_mem, OBJECT(dev), "riscv.sifive.e.xip",
  242. memmap[SIFIVE_E_DEV_XIP].size, &error_fatal);
  243. memory_region_add_subregion(sys_mem, memmap[SIFIVE_E_DEV_XIP].base,
  244. &s->xip_mem);
  245. }
  246. static void sifive_e_soc_class_init(ObjectClass *oc, void *data)
  247. {
  248. DeviceClass *dc = DEVICE_CLASS(oc);
  249. dc->realize = sifive_e_soc_realize;
  250. /* Reason: Uses serial_hds in realize function, thus can't be used twice */
  251. dc->user_creatable = false;
  252. }
  253. static const TypeInfo sifive_e_soc_type_info = {
  254. .name = TYPE_RISCV_E_SOC,
  255. .parent = TYPE_DEVICE,
  256. .instance_size = sizeof(SiFiveESoCState),
  257. .instance_init = sifive_e_soc_init,
  258. .class_init = sifive_e_soc_class_init,
  259. };
  260. static void sifive_e_soc_register_types(void)
  261. {
  262. type_register_static(&sifive_e_soc_type_info);
  263. }
  264. type_init(sifive_e_soc_register_types)