msf2-soc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * SmartFusion2 SoC emulation.
  3. *
  4. * Copyright (c) 2017 Subbaraya Sundeep <sundeep.lkml@gmail.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/units.h"
  26. #include "qapi/error.h"
  27. #include "exec/address-spaces.h"
  28. #include "hw/char/serial.h"
  29. #include "hw/irq.h"
  30. #include "hw/arm/msf2-soc.h"
  31. #include "hw/misc/unimp.h"
  32. #include "sysemu/runstate.h"
  33. #include "sysemu/sysemu.h"
  34. #define MSF2_TIMER_BASE 0x40004000
  35. #define MSF2_SYSREG_BASE 0x40038000
  36. #define ENVM_BASE_ADDRESS 0x60000000
  37. #define SRAM_BASE_ADDRESS 0x20000000
  38. #define MSF2_ENVM_MAX_SIZE (512 * KiB)
  39. /*
  40. * eSRAM max size is 80k without SECDED(Single error correction and
  41. * dual error detection) feature and 64k with SECDED.
  42. * We do not support SECDED now.
  43. */
  44. #define MSF2_ESRAM_MAX_SIZE (80 * KiB)
  45. static const uint32_t spi_addr[MSF2_NUM_SPIS] = { 0x40001000 , 0x40011000 };
  46. static const uint32_t uart_addr[MSF2_NUM_UARTS] = { 0x40000000 , 0x40010000 };
  47. static const int spi_irq[MSF2_NUM_SPIS] = { 2, 3 };
  48. static const int uart_irq[MSF2_NUM_UARTS] = { 10, 11 };
  49. static const int timer_irq[MSF2_NUM_TIMERS] = { 14, 15 };
  50. static void do_sys_reset(void *opaque, int n, int level)
  51. {
  52. if (level) {
  53. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  54. }
  55. }
  56. static void m2sxxx_soc_initfn(Object *obj)
  57. {
  58. MSF2State *s = MSF2_SOC(obj);
  59. int i;
  60. sysbus_init_child_obj(obj, "armv7m", &s->armv7m, sizeof(s->armv7m),
  61. TYPE_ARMV7M);
  62. sysbus_init_child_obj(obj, "sysreg", &s->sysreg, sizeof(s->sysreg),
  63. TYPE_MSF2_SYSREG);
  64. sysbus_init_child_obj(obj, "timer", &s->timer, sizeof(s->timer),
  65. TYPE_MSS_TIMER);
  66. for (i = 0; i < MSF2_NUM_SPIS; i++) {
  67. sysbus_init_child_obj(obj, "spi[*]", &s->spi[i], sizeof(s->spi[i]),
  68. TYPE_MSS_SPI);
  69. }
  70. }
  71. static void m2sxxx_soc_realize(DeviceState *dev_soc, Error **errp)
  72. {
  73. MSF2State *s = MSF2_SOC(dev_soc);
  74. DeviceState *dev, *armv7m;
  75. SysBusDevice *busdev;
  76. Error *err = NULL;
  77. int i;
  78. MemoryRegion *system_memory = get_system_memory();
  79. MemoryRegion *nvm = g_new(MemoryRegion, 1);
  80. MemoryRegion *nvm_alias = g_new(MemoryRegion, 1);
  81. MemoryRegion *sram = g_new(MemoryRegion, 1);
  82. memory_region_init_rom(nvm, NULL, "MSF2.eNVM", s->envm_size,
  83. &error_fatal);
  84. /*
  85. * On power-on, the eNVM region 0x60000000 is automatically
  86. * remapped to the Cortex-M3 processor executable region
  87. * start address (0x0). We do not support remapping other eNVM,
  88. * eSRAM and DDR regions by guest(via Sysreg) currently.
  89. */
  90. memory_region_init_alias(nvm_alias, NULL, "MSF2.eNVM",
  91. nvm, 0, s->envm_size);
  92. memory_region_add_subregion(system_memory, ENVM_BASE_ADDRESS, nvm);
  93. memory_region_add_subregion(system_memory, 0, nvm_alias);
  94. memory_region_init_ram(sram, NULL, "MSF2.eSRAM", s->esram_size,
  95. &error_fatal);
  96. memory_region_add_subregion(system_memory, SRAM_BASE_ADDRESS, sram);
  97. armv7m = DEVICE(&s->armv7m);
  98. qdev_prop_set_uint32(armv7m, "num-irq", 81);
  99. qdev_prop_set_string(armv7m, "cpu-type", s->cpu_type);
  100. qdev_prop_set_bit(armv7m, "enable-bitband", true);
  101. object_property_set_link(OBJECT(&s->armv7m), OBJECT(get_system_memory()),
  102. "memory", &error_abort);
  103. object_property_set_bool(OBJECT(&s->armv7m), true, "realized", &err);
  104. if (err != NULL) {
  105. error_propagate(errp, err);
  106. return;
  107. }
  108. if (!s->m3clk) {
  109. error_setg(errp, "Invalid m3clk value");
  110. error_append_hint(errp, "m3clk can not be zero\n");
  111. return;
  112. }
  113. qdev_connect_gpio_out_named(DEVICE(&s->armv7m.nvic), "SYSRESETREQ", 0,
  114. qemu_allocate_irq(&do_sys_reset, NULL, 0));
  115. system_clock_scale = NANOSECONDS_PER_SECOND / s->m3clk;
  116. for (i = 0; i < MSF2_NUM_UARTS; i++) {
  117. if (serial_hd(i)) {
  118. serial_mm_init(get_system_memory(), uart_addr[i], 2,
  119. qdev_get_gpio_in(armv7m, uart_irq[i]),
  120. 115200, serial_hd(i), DEVICE_NATIVE_ENDIAN);
  121. }
  122. }
  123. dev = DEVICE(&s->timer);
  124. /* APB0 clock is the timer input clock */
  125. qdev_prop_set_uint32(dev, "clock-frequency", s->m3clk / s->apb0div);
  126. object_property_set_bool(OBJECT(&s->timer), true, "realized", &err);
  127. if (err != NULL) {
  128. error_propagate(errp, err);
  129. return;
  130. }
  131. busdev = SYS_BUS_DEVICE(dev);
  132. sysbus_mmio_map(busdev, 0, MSF2_TIMER_BASE);
  133. sysbus_connect_irq(busdev, 0,
  134. qdev_get_gpio_in(armv7m, timer_irq[0]));
  135. sysbus_connect_irq(busdev, 1,
  136. qdev_get_gpio_in(armv7m, timer_irq[1]));
  137. dev = DEVICE(&s->sysreg);
  138. qdev_prop_set_uint32(dev, "apb0divisor", s->apb0div);
  139. qdev_prop_set_uint32(dev, "apb1divisor", s->apb1div);
  140. object_property_set_bool(OBJECT(&s->sysreg), true, "realized", &err);
  141. if (err != NULL) {
  142. error_propagate(errp, err);
  143. return;
  144. }
  145. busdev = SYS_BUS_DEVICE(dev);
  146. sysbus_mmio_map(busdev, 0, MSF2_SYSREG_BASE);
  147. for (i = 0; i < MSF2_NUM_SPIS; i++) {
  148. gchar *bus_name;
  149. object_property_set_bool(OBJECT(&s->spi[i]), true, "realized", &err);
  150. if (err != NULL) {
  151. error_propagate(errp, err);
  152. return;
  153. }
  154. sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi[i]), 0, spi_addr[i]);
  155. sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi[i]), 0,
  156. qdev_get_gpio_in(armv7m, spi_irq[i]));
  157. /* Alias controller SPI bus to the SoC itself */
  158. bus_name = g_strdup_printf("spi%d", i);
  159. object_property_add_alias(OBJECT(s), bus_name,
  160. OBJECT(&s->spi[i]), "spi",
  161. &error_abort);
  162. g_free(bus_name);
  163. }
  164. /* Below devices are not modelled yet. */
  165. create_unimplemented_device("i2c_0", 0x40002000, 0x1000);
  166. create_unimplemented_device("dma", 0x40003000, 0x1000);
  167. create_unimplemented_device("watchdog", 0x40005000, 0x1000);
  168. create_unimplemented_device("i2c_1", 0x40012000, 0x1000);
  169. create_unimplemented_device("gpio", 0x40013000, 0x1000);
  170. create_unimplemented_device("hs-dma", 0x40014000, 0x1000);
  171. create_unimplemented_device("can", 0x40015000, 0x1000);
  172. create_unimplemented_device("rtc", 0x40017000, 0x1000);
  173. create_unimplemented_device("apb_config", 0x40020000, 0x10000);
  174. create_unimplemented_device("emac", 0x40041000, 0x1000);
  175. create_unimplemented_device("usb", 0x40043000, 0x1000);
  176. }
  177. static Property m2sxxx_soc_properties[] = {
  178. /*
  179. * part name specifies the type of SmartFusion2 device variant(this
  180. * property is for information purpose only.
  181. */
  182. DEFINE_PROP_STRING("cpu-type", MSF2State, cpu_type),
  183. DEFINE_PROP_STRING("part-name", MSF2State, part_name),
  184. DEFINE_PROP_UINT64("eNVM-size", MSF2State, envm_size, MSF2_ENVM_MAX_SIZE),
  185. DEFINE_PROP_UINT64("eSRAM-size", MSF2State, esram_size,
  186. MSF2_ESRAM_MAX_SIZE),
  187. /* Libero GUI shows 100Mhz as default for clocks */
  188. DEFINE_PROP_UINT32("m3clk", MSF2State, m3clk, 100 * 1000000),
  189. /* default divisors in Libero GUI */
  190. DEFINE_PROP_UINT8("apb0div", MSF2State, apb0div, 2),
  191. DEFINE_PROP_UINT8("apb1div", MSF2State, apb1div, 2),
  192. DEFINE_PROP_END_OF_LIST(),
  193. };
  194. static void m2sxxx_soc_class_init(ObjectClass *klass, void *data)
  195. {
  196. DeviceClass *dc = DEVICE_CLASS(klass);
  197. dc->realize = m2sxxx_soc_realize;
  198. dc->props = m2sxxx_soc_properties;
  199. }
  200. static const TypeInfo m2sxxx_soc_info = {
  201. .name = TYPE_MSF2_SOC,
  202. .parent = TYPE_SYS_BUS_DEVICE,
  203. .instance_size = sizeof(MSF2State),
  204. .instance_init = m2sxxx_soc_initfn,
  205. .class_init = m2sxxx_soc_class_init,
  206. };
  207. static void m2sxxx_soc_types(void)
  208. {
  209. type_register_static(&m2sxxx_soc_info);
  210. }
  211. type_init(m2sxxx_soc_types)