msf2-soc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * SmartFusion2 SoC emulation.
  3. *
  4. * Copyright (c) 2017-2020 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-mm.h"
  29. #include "hw/arm/msf2-soc.h"
  30. #include "hw/misc/unimp.h"
  31. #include "hw/qdev-clock.h"
  32. #include "sysemu/sysemu.h"
  33. #define MSF2_TIMER_BASE 0x40004000
  34. #define MSF2_SYSREG_BASE 0x40038000
  35. #define MSF2_EMAC_BASE 0x40041000
  36. #define ENVM_BASE_ADDRESS 0x60000000
  37. #define SRAM_BASE_ADDRESS 0x20000000
  38. #define MSF2_EMAC_IRQ 12
  39. #define MSF2_ENVM_MAX_SIZE (512 * KiB)
  40. /*
  41. * eSRAM max size is 80k without SECDED(Single error correction and
  42. * dual error detection) feature and 64k with SECDED.
  43. * We do not support SECDED now.
  44. */
  45. #define MSF2_ESRAM_MAX_SIZE (80 * KiB)
  46. static const uint32_t spi_addr[MSF2_NUM_SPIS] = { 0x40001000 , 0x40011000 };
  47. static const uint32_t uart_addr[MSF2_NUM_UARTS] = { 0x40000000 , 0x40010000 };
  48. static const int spi_irq[MSF2_NUM_SPIS] = { 2, 3 };
  49. static const int uart_irq[MSF2_NUM_UARTS] = { 10, 11 };
  50. static const int timer_irq[MSF2_NUM_TIMERS] = { 14, 15 };
  51. static void m2sxxx_soc_initfn(Object *obj)
  52. {
  53. MSF2State *s = MSF2_SOC(obj);
  54. int i;
  55. object_initialize_child(obj, "armv7m", &s->armv7m, TYPE_ARMV7M);
  56. object_initialize_child(obj, "sysreg", &s->sysreg, TYPE_MSF2_SYSREG);
  57. object_initialize_child(obj, "timer", &s->timer, TYPE_MSS_TIMER);
  58. for (i = 0; i < MSF2_NUM_SPIS; i++) {
  59. object_initialize_child(obj, "spi[*]", &s->spi[i], TYPE_MSS_SPI);
  60. }
  61. object_initialize_child(obj, "emac", &s->emac, TYPE_MSS_EMAC);
  62. s->m3clk = qdev_init_clock_in(DEVICE(obj), "m3clk", NULL, NULL, 0);
  63. s->refclk = qdev_init_clock_in(DEVICE(obj), "refclk", NULL, NULL, 0);
  64. }
  65. static void m2sxxx_soc_realize(DeviceState *dev_soc, Error **errp)
  66. {
  67. MSF2State *s = MSF2_SOC(dev_soc);
  68. DeviceState *dev, *armv7m;
  69. SysBusDevice *busdev;
  70. int i;
  71. MemoryRegion *system_memory = get_system_memory();
  72. if (!clock_has_source(s->m3clk)) {
  73. error_setg(errp, "m3clk must be wired up by the board code");
  74. return;
  75. }
  76. /*
  77. * We use s->refclk internally and only define it with qdev_init_clock_in()
  78. * so it is correctly parented and not leaked on an init/deinit; it is not
  79. * intended as an externally exposed clock.
  80. */
  81. if (clock_has_source(s->refclk)) {
  82. error_setg(errp, "refclk must not be wired up by the board code");
  83. return;
  84. }
  85. /*
  86. * TODO: ideally we should model the SoC SYSTICK_CR register at 0xe0042038,
  87. * which allows the guest to program the divisor between the m3clk and
  88. * the systick refclk to either /4, /8, /16 or /32, as well as setting
  89. * the value the guest can read in the STCALIB register. Currently we
  90. * implement the divisor as a fixed /32, which matches the reset value
  91. * of SYSTICK_CR.
  92. */
  93. clock_set_mul_div(s->refclk, 32, 1);
  94. clock_set_source(s->refclk, s->m3clk);
  95. memory_region_init_rom(&s->nvm, OBJECT(dev_soc), "MSF2.eNVM", s->envm_size,
  96. &error_fatal);
  97. /*
  98. * On power-on, the eNVM region 0x60000000 is automatically
  99. * remapped to the Cortex-M3 processor executable region
  100. * start address (0x0). We do not support remapping other eNVM,
  101. * eSRAM and DDR regions by guest(via Sysreg) currently.
  102. */
  103. memory_region_init_alias(&s->nvm_alias, OBJECT(dev_soc), "MSF2.eNVM",
  104. &s->nvm, 0, s->envm_size);
  105. memory_region_add_subregion(system_memory, ENVM_BASE_ADDRESS, &s->nvm);
  106. memory_region_add_subregion(system_memory, 0, &s->nvm_alias);
  107. memory_region_init_ram(&s->sram, NULL, "MSF2.eSRAM", s->esram_size,
  108. &error_fatal);
  109. memory_region_add_subregion(system_memory, SRAM_BASE_ADDRESS, &s->sram);
  110. armv7m = DEVICE(&s->armv7m);
  111. qdev_prop_set_uint32(armv7m, "num-irq", 81);
  112. qdev_prop_set_string(armv7m, "cpu-type", ARM_CPU_TYPE_NAME("cortex-m3"));
  113. qdev_prop_set_bit(armv7m, "enable-bitband", true);
  114. qdev_connect_clock_in(armv7m, "cpuclk", s->m3clk);
  115. qdev_connect_clock_in(armv7m, "refclk", s->refclk);
  116. object_property_set_link(OBJECT(&s->armv7m), "memory",
  117. OBJECT(get_system_memory()), &error_abort);
  118. if (!sysbus_realize(SYS_BUS_DEVICE(&s->armv7m), errp)) {
  119. return;
  120. }
  121. for (i = 0; i < MSF2_NUM_UARTS; i++) {
  122. if (serial_hd(i)) {
  123. serial_mm_init(get_system_memory(), uart_addr[i], 2,
  124. qdev_get_gpio_in(armv7m, uart_irq[i]),
  125. 115200, serial_hd(i), DEVICE_NATIVE_ENDIAN);
  126. }
  127. }
  128. dev = DEVICE(&s->timer);
  129. /*
  130. * APB0 clock is the timer input clock.
  131. * TODO: ideally the MSF2 timer device should use a Clock rather than a
  132. * clock-frequency integer property.
  133. */
  134. qdev_prop_set_uint32(dev, "clock-frequency",
  135. clock_get_hz(s->m3clk) / s->apb0div);
  136. if (!sysbus_realize(SYS_BUS_DEVICE(&s->timer), errp)) {
  137. return;
  138. }
  139. busdev = SYS_BUS_DEVICE(dev);
  140. sysbus_mmio_map(busdev, 0, MSF2_TIMER_BASE);
  141. sysbus_connect_irq(busdev, 0,
  142. qdev_get_gpio_in(armv7m, timer_irq[0]));
  143. sysbus_connect_irq(busdev, 1,
  144. qdev_get_gpio_in(armv7m, timer_irq[1]));
  145. dev = DEVICE(&s->sysreg);
  146. qdev_prop_set_uint32(dev, "apb0divisor", s->apb0div);
  147. qdev_prop_set_uint32(dev, "apb1divisor", s->apb1div);
  148. if (!sysbus_realize(SYS_BUS_DEVICE(&s->sysreg), errp)) {
  149. return;
  150. }
  151. busdev = SYS_BUS_DEVICE(dev);
  152. sysbus_mmio_map(busdev, 0, MSF2_SYSREG_BASE);
  153. for (i = 0; i < MSF2_NUM_SPIS; i++) {
  154. gchar *bus_name;
  155. if (!sysbus_realize(SYS_BUS_DEVICE(&s->spi[i]), errp)) {
  156. return;
  157. }
  158. sysbus_mmio_map(SYS_BUS_DEVICE(&s->spi[i]), 0, spi_addr[i]);
  159. sysbus_connect_irq(SYS_BUS_DEVICE(&s->spi[i]), 0,
  160. qdev_get_gpio_in(armv7m, spi_irq[i]));
  161. /* Alias controller SPI bus to the SoC itself */
  162. bus_name = g_strdup_printf("spi%d", i);
  163. object_property_add_alias(OBJECT(s), bus_name,
  164. OBJECT(&s->spi[i]), "spi");
  165. g_free(bus_name);
  166. }
  167. dev = DEVICE(&s->emac);
  168. qemu_configure_nic_device(dev, true, NULL);
  169. object_property_set_link(OBJECT(&s->emac), "ahb-bus",
  170. OBJECT(get_system_memory()), &error_abort);
  171. if (!sysbus_realize(SYS_BUS_DEVICE(&s->emac), errp)) {
  172. return;
  173. }
  174. busdev = SYS_BUS_DEVICE(dev);
  175. sysbus_mmio_map(busdev, 0, MSF2_EMAC_BASE);
  176. sysbus_connect_irq(busdev, 0,
  177. qdev_get_gpio_in(armv7m, MSF2_EMAC_IRQ));
  178. /* Below devices are not modelled yet. */
  179. create_unimplemented_device("i2c_0", 0x40002000, 0x1000);
  180. create_unimplemented_device("dma", 0x40003000, 0x1000);
  181. create_unimplemented_device("watchdog", 0x40005000, 0x1000);
  182. create_unimplemented_device("i2c_1", 0x40012000, 0x1000);
  183. create_unimplemented_device("gpio", 0x40013000, 0x1000);
  184. create_unimplemented_device("hs-dma", 0x40014000, 0x1000);
  185. create_unimplemented_device("can", 0x40015000, 0x1000);
  186. create_unimplemented_device("rtc", 0x40017000, 0x1000);
  187. create_unimplemented_device("apb_config", 0x40020000, 0x10000);
  188. create_unimplemented_device("usb", 0x40043000, 0x1000);
  189. }
  190. static Property m2sxxx_soc_properties[] = {
  191. /*
  192. * part name specifies the type of SmartFusion2 device variant(this
  193. * property is for information purpose only.
  194. */
  195. DEFINE_PROP_STRING("part-name", MSF2State, part_name),
  196. DEFINE_PROP_UINT64("eNVM-size", MSF2State, envm_size, MSF2_ENVM_MAX_SIZE),
  197. DEFINE_PROP_UINT64("eSRAM-size", MSF2State, esram_size,
  198. MSF2_ESRAM_MAX_SIZE),
  199. /* default divisors in Libero GUI */
  200. DEFINE_PROP_UINT8("apb0div", MSF2State, apb0div, 2),
  201. DEFINE_PROP_UINT8("apb1div", MSF2State, apb1div, 2),
  202. DEFINE_PROP_END_OF_LIST(),
  203. };
  204. static void m2sxxx_soc_class_init(ObjectClass *klass, void *data)
  205. {
  206. DeviceClass *dc = DEVICE_CLASS(klass);
  207. dc->realize = m2sxxx_soc_realize;
  208. device_class_set_props(dc, m2sxxx_soc_properties);
  209. }
  210. static const TypeInfo m2sxxx_soc_info = {
  211. .name = TYPE_MSF2_SOC,
  212. .parent = TYPE_SYS_BUS_DEVICE,
  213. .instance_size = sizeof(MSF2State),
  214. .instance_init = m2sxxx_soc_initfn,
  215. .class_init = m2sxxx_soc_class_init,
  216. };
  217. static void m2sxxx_soc_types(void)
  218. {
  219. type_register_static(&m2sxxx_soc_info);
  220. }
  221. type_init(m2sxxx_soc_types)