sifive_e.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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/log.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/char/serial.h"
  38. #include "hw/misc/unimp.h"
  39. #include "target/riscv/cpu.h"
  40. #include "hw/riscv/riscv_hart.h"
  41. #include "hw/riscv/sifive_plic.h"
  42. #include "hw/riscv/sifive_clint.h"
  43. #include "hw/riscv/sifive_uart.h"
  44. #include "hw/riscv/sifive_e.h"
  45. #include "hw/riscv/sifive_e_prci.h"
  46. #include "hw/riscv/boot.h"
  47. #include "chardev/char.h"
  48. #include "sysemu/arch_init.h"
  49. #include "sysemu/sysemu.h"
  50. #include "exec/address-spaces.h"
  51. static const struct MemmapEntry {
  52. hwaddr base;
  53. hwaddr size;
  54. } sifive_e_memmap[] = {
  55. [SIFIVE_E_DEBUG] = { 0x0, 0x100 },
  56. [SIFIVE_E_MROM] = { 0x1000, 0x2000 },
  57. [SIFIVE_E_OTP] = { 0x20000, 0x2000 },
  58. [SIFIVE_E_CLINT] = { 0x2000000, 0x10000 },
  59. [SIFIVE_E_PLIC] = { 0xc000000, 0x4000000 },
  60. [SIFIVE_E_AON] = { 0x10000000, 0x8000 },
  61. [SIFIVE_E_PRCI] = { 0x10008000, 0x8000 },
  62. [SIFIVE_E_OTP_CTRL] = { 0x10010000, 0x1000 },
  63. [SIFIVE_E_GPIO0] = { 0x10012000, 0x1000 },
  64. [SIFIVE_E_UART0] = { 0x10013000, 0x1000 },
  65. [SIFIVE_E_QSPI0] = { 0x10014000, 0x1000 },
  66. [SIFIVE_E_PWM0] = { 0x10015000, 0x1000 },
  67. [SIFIVE_E_UART1] = { 0x10023000, 0x1000 },
  68. [SIFIVE_E_QSPI1] = { 0x10024000, 0x1000 },
  69. [SIFIVE_E_PWM1] = { 0x10025000, 0x1000 },
  70. [SIFIVE_E_QSPI2] = { 0x10034000, 0x1000 },
  71. [SIFIVE_E_PWM2] = { 0x10035000, 0x1000 },
  72. [SIFIVE_E_XIP] = { 0x20000000, 0x20000000 },
  73. [SIFIVE_E_DTIM] = { 0x80000000, 0x4000 }
  74. };
  75. static void riscv_sifive_e_init(MachineState *machine)
  76. {
  77. const struct MemmapEntry *memmap = sifive_e_memmap;
  78. SiFiveEState *s = g_new0(SiFiveEState, 1);
  79. MemoryRegion *sys_mem = get_system_memory();
  80. MemoryRegion *main_mem = g_new(MemoryRegion, 1);
  81. int i;
  82. /* Initialize SoC */
  83. object_initialize_child(OBJECT(machine), "soc", &s->soc,
  84. sizeof(s->soc), TYPE_RISCV_E_SOC,
  85. &error_abort, NULL);
  86. object_property_set_bool(OBJECT(&s->soc), true, "realized",
  87. &error_abort);
  88. /* Data Tightly Integrated Memory */
  89. memory_region_init_ram(main_mem, NULL, "riscv.sifive.e.ram",
  90. memmap[SIFIVE_E_DTIM].size, &error_fatal);
  91. memory_region_add_subregion(sys_mem,
  92. memmap[SIFIVE_E_DTIM].base, main_mem);
  93. /* Mask ROM reset vector */
  94. uint32_t reset_vec[2] = {
  95. 0x204002b7, /* 0x1000: lui t0,0x20400 */
  96. 0x00028067, /* 0x1004: jr t0 */
  97. };
  98. /* copy in the reset vector in little_endian byte order */
  99. for (i = 0; i < sizeof(reset_vec) >> 2; i++) {
  100. reset_vec[i] = cpu_to_le32(reset_vec[i]);
  101. }
  102. rom_add_blob_fixed_as("mrom.reset", reset_vec, sizeof(reset_vec),
  103. memmap[SIFIVE_E_MROM].base, &address_space_memory);
  104. if (machine->kernel_filename) {
  105. riscv_load_kernel(machine->kernel_filename, NULL);
  106. }
  107. }
  108. static void riscv_sifive_e_soc_init(Object *obj)
  109. {
  110. MachineState *ms = MACHINE(qdev_get_machine());
  111. SiFiveESoCState *s = RISCV_E_SOC(obj);
  112. object_initialize_child(obj, "cpus", &s->cpus,
  113. sizeof(s->cpus), TYPE_RISCV_HART_ARRAY,
  114. &error_abort, NULL);
  115. object_property_set_str(OBJECT(&s->cpus), SIFIVE_E_CPU, "cpu-type",
  116. &error_abort);
  117. object_property_set_int(OBJECT(&s->cpus), ms->smp.cpus, "num-harts",
  118. &error_abort);
  119. sysbus_init_child_obj(obj, "riscv.sifive.e.gpio0",
  120. &s->gpio, sizeof(s->gpio),
  121. TYPE_SIFIVE_GPIO);
  122. }
  123. static void riscv_sifive_e_soc_realize(DeviceState *dev, Error **errp)
  124. {
  125. MachineState *ms = MACHINE(qdev_get_machine());
  126. const struct MemmapEntry *memmap = sifive_e_memmap;
  127. Error *err = NULL;
  128. SiFiveESoCState *s = RISCV_E_SOC(dev);
  129. MemoryRegion *sys_mem = get_system_memory();
  130. object_property_set_bool(OBJECT(&s->cpus), true, "realized",
  131. &error_abort);
  132. /* Mask ROM */
  133. memory_region_init_rom(&s->mask_rom, NULL, "riscv.sifive.e.mrom",
  134. memmap[SIFIVE_E_MROM].size, &error_fatal);
  135. memory_region_add_subregion(sys_mem,
  136. memmap[SIFIVE_E_MROM].base, &s->mask_rom);
  137. /* MMIO */
  138. s->plic = sifive_plic_create(memmap[SIFIVE_E_PLIC].base,
  139. (char *)SIFIVE_E_PLIC_HART_CONFIG,
  140. SIFIVE_E_PLIC_NUM_SOURCES,
  141. SIFIVE_E_PLIC_NUM_PRIORITIES,
  142. SIFIVE_E_PLIC_PRIORITY_BASE,
  143. SIFIVE_E_PLIC_PENDING_BASE,
  144. SIFIVE_E_PLIC_ENABLE_BASE,
  145. SIFIVE_E_PLIC_ENABLE_STRIDE,
  146. SIFIVE_E_PLIC_CONTEXT_BASE,
  147. SIFIVE_E_PLIC_CONTEXT_STRIDE,
  148. memmap[SIFIVE_E_PLIC].size);
  149. sifive_clint_create(memmap[SIFIVE_E_CLINT].base,
  150. memmap[SIFIVE_E_CLINT].size, ms->smp.cpus,
  151. SIFIVE_SIP_BASE, SIFIVE_TIMECMP_BASE, SIFIVE_TIME_BASE);
  152. create_unimplemented_device("riscv.sifive.e.aon",
  153. memmap[SIFIVE_E_AON].base, memmap[SIFIVE_E_AON].size);
  154. sifive_e_prci_create(memmap[SIFIVE_E_PRCI].base);
  155. /* GPIO */
  156. object_property_set_bool(OBJECT(&s->gpio), true, "realized", &err);
  157. if (err) {
  158. error_propagate(errp, err);
  159. return;
  160. }
  161. /* Map GPIO registers */
  162. sysbus_mmio_map(SYS_BUS_DEVICE(&s->gpio), 0, memmap[SIFIVE_E_GPIO0].base);
  163. /* Pass all GPIOs to the SOC layer so they are available to the board */
  164. qdev_pass_gpios(DEVICE(&s->gpio), dev, NULL);
  165. /* Connect GPIO interrupts to the PLIC */
  166. for (int i = 0; i < 32; i++) {
  167. sysbus_connect_irq(SYS_BUS_DEVICE(&s->gpio), i,
  168. qdev_get_gpio_in(DEVICE(s->plic),
  169. SIFIVE_E_GPIO0_IRQ0 + i));
  170. }
  171. sifive_uart_create(sys_mem, memmap[SIFIVE_E_UART0].base,
  172. serial_hd(0), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_E_UART0_IRQ));
  173. create_unimplemented_device("riscv.sifive.e.qspi0",
  174. memmap[SIFIVE_E_QSPI0].base, memmap[SIFIVE_E_QSPI0].size);
  175. create_unimplemented_device("riscv.sifive.e.pwm0",
  176. memmap[SIFIVE_E_PWM0].base, memmap[SIFIVE_E_PWM0].size);
  177. sifive_uart_create(sys_mem, memmap[SIFIVE_E_UART1].base,
  178. serial_hd(1), qdev_get_gpio_in(DEVICE(s->plic), SIFIVE_E_UART1_IRQ));
  179. create_unimplemented_device("riscv.sifive.e.qspi1",
  180. memmap[SIFIVE_E_QSPI1].base, memmap[SIFIVE_E_QSPI1].size);
  181. create_unimplemented_device("riscv.sifive.e.pwm1",
  182. memmap[SIFIVE_E_PWM1].base, memmap[SIFIVE_E_PWM1].size);
  183. create_unimplemented_device("riscv.sifive.e.qspi2",
  184. memmap[SIFIVE_E_QSPI2].base, memmap[SIFIVE_E_QSPI2].size);
  185. create_unimplemented_device("riscv.sifive.e.pwm2",
  186. memmap[SIFIVE_E_PWM2].base, memmap[SIFIVE_E_PWM2].size);
  187. /* Flash memory */
  188. memory_region_init_ram(&s->xip_mem, NULL, "riscv.sifive.e.xip",
  189. memmap[SIFIVE_E_XIP].size, &error_fatal);
  190. memory_region_set_readonly(&s->xip_mem, true);
  191. memory_region_add_subregion(sys_mem, memmap[SIFIVE_E_XIP].base,
  192. &s->xip_mem);
  193. }
  194. static void riscv_sifive_e_machine_init(MachineClass *mc)
  195. {
  196. mc->desc = "RISC-V Board compatible with SiFive E SDK";
  197. mc->init = riscv_sifive_e_init;
  198. mc->max_cpus = 1;
  199. }
  200. DEFINE_MACHINE("sifive_e", riscv_sifive_e_machine_init)
  201. static void riscv_sifive_e_soc_class_init(ObjectClass *oc, void *data)
  202. {
  203. DeviceClass *dc = DEVICE_CLASS(oc);
  204. dc->realize = riscv_sifive_e_soc_realize;
  205. /* Reason: Uses serial_hds in realize function, thus can't be used twice */
  206. dc->user_creatable = false;
  207. }
  208. static const TypeInfo riscv_sifive_e_soc_type_info = {
  209. .name = TYPE_RISCV_E_SOC,
  210. .parent = TYPE_DEVICE,
  211. .instance_size = sizeof(SiFiveESoCState),
  212. .instance_init = riscv_sifive_e_soc_init,
  213. .class_init = riscv_sifive_e_soc_class_init,
  214. };
  215. static void riscv_sifive_e_soc_register_types(void)
  216. {
  217. type_register_static(&riscv_sifive_e_soc_type_info);
  218. }
  219. type_init(riscv_sifive_e_soc_register_types)