stm32f100_soc.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * STM32F100 SoC
  3. *
  4. * Copyright (c) 2021 Alexandre Iooss <erdnaxe@crans.org>
  5. * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qapi/error.h"
  27. #include "qemu/module.h"
  28. #include "hw/arm/boot.h"
  29. #include "exec/address-spaces.h"
  30. #include "hw/arm/stm32f100_soc.h"
  31. #include "hw/qdev-properties.h"
  32. #include "hw/qdev-clock.h"
  33. #include "hw/misc/unimp.h"
  34. #include "system/system.h"
  35. /* stm32f100_soc implementation is derived from stm32f205_soc */
  36. static const uint32_t usart_addr[STM_NUM_USARTS] = { 0x40013800, 0x40004400,
  37. 0x40004800 };
  38. static const uint32_t spi_addr[STM_NUM_SPIS] = { 0x40013000, 0x40003800 };
  39. static const int usart_irq[STM_NUM_USARTS] = {37, 38, 39};
  40. static const int spi_irq[STM_NUM_SPIS] = {35, 36};
  41. static void stm32f100_soc_initfn(Object *obj)
  42. {
  43. STM32F100State *s = STM32F100_SOC(obj);
  44. int i;
  45. object_initialize_child(obj, "armv7m", &s->armv7m, TYPE_ARMV7M);
  46. for (i = 0; i < STM_NUM_USARTS; i++) {
  47. object_initialize_child(obj, "usart[*]", &s->usart[i],
  48. TYPE_STM32F2XX_USART);
  49. }
  50. for (i = 0; i < STM_NUM_SPIS; i++) {
  51. object_initialize_child(obj, "spi[*]", &s->spi[i], TYPE_STM32F2XX_SPI);
  52. }
  53. s->sysclk = qdev_init_clock_in(DEVICE(s), "sysclk", NULL, NULL, 0);
  54. s->refclk = qdev_init_clock_in(DEVICE(s), "refclk", NULL, NULL, 0);
  55. }
  56. static void stm32f100_soc_realize(DeviceState *dev_soc, Error **errp)
  57. {
  58. STM32F100State *s = STM32F100_SOC(dev_soc);
  59. DeviceState *dev, *armv7m;
  60. SysBusDevice *busdev;
  61. int i;
  62. MemoryRegion *system_memory = get_system_memory();
  63. /*
  64. * We use s->refclk internally and only define it with qdev_init_clock_in()
  65. * so it is correctly parented and not leaked on an init/deinit; it is not
  66. * intended as an externally exposed clock.
  67. */
  68. if (clock_has_source(s->refclk)) {
  69. error_setg(errp, "refclk clock must not be wired up by the board code");
  70. return;
  71. }
  72. if (!clock_has_source(s->sysclk)) {
  73. error_setg(errp, "sysclk clock must be wired up by the board code");
  74. return;
  75. }
  76. /*
  77. * TODO: ideally we should model the SoC RCC and its ability to
  78. * change the sysclk frequency and define different sysclk sources.
  79. */
  80. /* The refclk always runs at frequency HCLK / 8 */
  81. clock_set_mul_div(s->refclk, 8, 1);
  82. clock_set_source(s->refclk, s->sysclk);
  83. /*
  84. * Init flash region
  85. * Flash starts at 0x08000000 and then is aliased to boot memory at 0x0
  86. */
  87. memory_region_init_rom(&s->flash, OBJECT(dev_soc), "STM32F100.flash",
  88. FLASH_SIZE, &error_fatal);
  89. memory_region_init_alias(&s->flash_alias, OBJECT(dev_soc),
  90. "STM32F100.flash.alias", &s->flash, 0, FLASH_SIZE);
  91. memory_region_add_subregion(system_memory, FLASH_BASE_ADDRESS, &s->flash);
  92. memory_region_add_subregion(system_memory, 0, &s->flash_alias);
  93. /* Init SRAM region */
  94. memory_region_init_ram(&s->sram, NULL, "STM32F100.sram", SRAM_SIZE,
  95. &error_fatal);
  96. memory_region_add_subregion(system_memory, SRAM_BASE_ADDRESS, &s->sram);
  97. /* Init ARMv7m */
  98. armv7m = DEVICE(&s->armv7m);
  99. qdev_prop_set_uint32(armv7m, "num-irq", 61);
  100. qdev_prop_set_uint8(armv7m, "num-prio-bits", 4);
  101. qdev_prop_set_string(armv7m, "cpu-type", ARM_CPU_TYPE_NAME("cortex-m3"));
  102. qdev_prop_set_bit(armv7m, "enable-bitband", true);
  103. qdev_connect_clock_in(armv7m, "cpuclk", s->sysclk);
  104. qdev_connect_clock_in(armv7m, "refclk", s->refclk);
  105. object_property_set_link(OBJECT(&s->armv7m), "memory",
  106. OBJECT(get_system_memory()), &error_abort);
  107. if (!sysbus_realize(SYS_BUS_DEVICE(&s->armv7m), errp)) {
  108. return;
  109. }
  110. /* Attach UART (uses USART registers) and USART controllers */
  111. for (i = 0; i < STM_NUM_USARTS; i++) {
  112. dev = DEVICE(&(s->usart[i]));
  113. qdev_prop_set_chr(dev, "chardev", serial_hd(i));
  114. if (!sysbus_realize(SYS_BUS_DEVICE(&s->usart[i]), errp)) {
  115. return;
  116. }
  117. busdev = SYS_BUS_DEVICE(dev);
  118. sysbus_mmio_map(busdev, 0, usart_addr[i]);
  119. sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(armv7m, usart_irq[i]));
  120. }
  121. /* SPI 1 and 2 */
  122. for (i = 0; i < STM_NUM_SPIS; i++) {
  123. dev = DEVICE(&(s->spi[i]));
  124. if (!sysbus_realize(SYS_BUS_DEVICE(&s->spi[i]), errp)) {
  125. return;
  126. }
  127. busdev = SYS_BUS_DEVICE(dev);
  128. sysbus_mmio_map(busdev, 0, spi_addr[i]);
  129. sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(armv7m, spi_irq[i]));
  130. }
  131. create_unimplemented_device("timer[2]", 0x40000000, 0x400);
  132. create_unimplemented_device("timer[3]", 0x40000400, 0x400);
  133. create_unimplemented_device("timer[4]", 0x40000800, 0x400);
  134. create_unimplemented_device("timer[6]", 0x40001000, 0x400);
  135. create_unimplemented_device("timer[7]", 0x40001400, 0x400);
  136. create_unimplemented_device("RTC", 0x40002800, 0x400);
  137. create_unimplemented_device("WWDG", 0x40002C00, 0x400);
  138. create_unimplemented_device("IWDG", 0x40003000, 0x400);
  139. create_unimplemented_device("I2C1", 0x40005400, 0x400);
  140. create_unimplemented_device("I2C2", 0x40005800, 0x400);
  141. create_unimplemented_device("BKP", 0x40006C00, 0x400);
  142. create_unimplemented_device("PWR", 0x40007000, 0x400);
  143. create_unimplemented_device("DAC", 0x40007400, 0x400);
  144. create_unimplemented_device("CEC", 0x40007800, 0x400);
  145. create_unimplemented_device("AFIO", 0x40010000, 0x400);
  146. create_unimplemented_device("EXTI", 0x40010400, 0x400);
  147. create_unimplemented_device("GPIOA", 0x40010800, 0x400);
  148. create_unimplemented_device("GPIOB", 0x40010C00, 0x400);
  149. create_unimplemented_device("GPIOC", 0x40011000, 0x400);
  150. create_unimplemented_device("GPIOD", 0x40011400, 0x400);
  151. create_unimplemented_device("GPIOE", 0x40011800, 0x400);
  152. create_unimplemented_device("ADC1", 0x40012400, 0x400);
  153. create_unimplemented_device("timer[1]", 0x40012C00, 0x400);
  154. create_unimplemented_device("timer[15]", 0x40014000, 0x400);
  155. create_unimplemented_device("timer[16]", 0x40014400, 0x400);
  156. create_unimplemented_device("timer[17]", 0x40014800, 0x400);
  157. create_unimplemented_device("DMA", 0x40020000, 0x400);
  158. create_unimplemented_device("RCC", 0x40021000, 0x400);
  159. create_unimplemented_device("Flash Int", 0x40022000, 0x400);
  160. create_unimplemented_device("CRC", 0x40023000, 0x400);
  161. }
  162. static void stm32f100_soc_class_init(ObjectClass *klass, void *data)
  163. {
  164. DeviceClass *dc = DEVICE_CLASS(klass);
  165. dc->realize = stm32f100_soc_realize;
  166. /* No vmstate or reset required: device has no internal state */
  167. }
  168. static const TypeInfo stm32f100_soc_info = {
  169. .name = TYPE_STM32F100_SOC,
  170. .parent = TYPE_SYS_BUS_DEVICE,
  171. .instance_size = sizeof(STM32F100State),
  172. .instance_init = stm32f100_soc_initfn,
  173. .class_init = stm32f100_soc_class_init,
  174. };
  175. static void stm32f100_soc_types(void)
  176. {
  177. type_register_static(&stm32f100_soc_info);
  178. }
  179. type_init(stm32f100_soc_types)