stm32f405_soc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * STM32F405 SoC
  3. *
  4. * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me>
  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 "qapi/error.h"
  26. #include "qemu-common.h"
  27. #include "exec/address-spaces.h"
  28. #include "sysemu/sysemu.h"
  29. #include "hw/arm/stm32f405_soc.h"
  30. #include "hw/misc/unimp.h"
  31. #define SYSCFG_ADD 0x40013800
  32. static const uint32_t usart_addr[] = { 0x40011000, 0x40004400, 0x40004800,
  33. 0x40004C00, 0x40005000, 0x40011400,
  34. 0x40007800, 0x40007C00 };
  35. /* At the moment only Timer 2 to 5 are modelled */
  36. static const uint32_t timer_addr[] = { 0x40000000, 0x40000400,
  37. 0x40000800, 0x40000C00 };
  38. static const uint32_t adc_addr[] = { 0x40012000, 0x40012100, 0x40012200,
  39. 0x40012300, 0x40012400, 0x40012500 };
  40. static const uint32_t spi_addr[] = { 0x40013000, 0x40003800, 0x40003C00,
  41. 0x40013400, 0x40015000, 0x40015400 };
  42. #define EXTI_ADDR 0x40013C00
  43. #define SYSCFG_IRQ 71
  44. static const int usart_irq[] = { 37, 38, 39, 52, 53, 71, 82, 83 };
  45. static const int timer_irq[] = { 28, 29, 30, 50 };
  46. #define ADC_IRQ 18
  47. static const int spi_irq[] = { 35, 36, 51, 0, 0, 0 };
  48. static const int exti_irq[] = { 6, 7, 8, 9, 10, 23, 23, 23, 23, 23, 40,
  49. 40, 40, 40, 40, 40} ;
  50. static void stm32f405_soc_initfn(Object *obj)
  51. {
  52. STM32F405State *s = STM32F405_SOC(obj);
  53. int i;
  54. object_initialize_child(obj, "armv7m", &s->armv7m, TYPE_ARMV7M);
  55. object_initialize_child(obj, "syscfg", &s->syscfg, TYPE_STM32F4XX_SYSCFG);
  56. for (i = 0; i < STM_NUM_USARTS; i++) {
  57. object_initialize_child(obj, "usart[*]", &s->usart[i],
  58. TYPE_STM32F2XX_USART);
  59. }
  60. for (i = 0; i < STM_NUM_TIMERS; i++) {
  61. object_initialize_child(obj, "timer[*]", &s->timer[i],
  62. TYPE_STM32F2XX_TIMER);
  63. }
  64. for (i = 0; i < STM_NUM_ADCS; i++) {
  65. object_initialize_child(obj, "adc[*]", &s->adc[i], TYPE_STM32F2XX_ADC);
  66. }
  67. for (i = 0; i < STM_NUM_SPIS; i++) {
  68. object_initialize_child(obj, "spi[*]", &s->spi[i], TYPE_STM32F2XX_SPI);
  69. }
  70. object_initialize_child(obj, "exti", &s->exti, TYPE_STM32F4XX_EXTI);
  71. }
  72. static void stm32f405_soc_realize(DeviceState *dev_soc, Error **errp)
  73. {
  74. STM32F405State *s = STM32F405_SOC(dev_soc);
  75. MemoryRegion *system_memory = get_system_memory();
  76. DeviceState *dev, *armv7m;
  77. SysBusDevice *busdev;
  78. Error *err = NULL;
  79. int i;
  80. memory_region_init_rom(&s->flash, OBJECT(dev_soc), "STM32F405.flash",
  81. FLASH_SIZE, &err);
  82. if (err != NULL) {
  83. error_propagate(errp, err);
  84. return;
  85. }
  86. memory_region_init_alias(&s->flash_alias, OBJECT(dev_soc),
  87. "STM32F405.flash.alias", &s->flash, 0,
  88. FLASH_SIZE);
  89. memory_region_add_subregion(system_memory, FLASH_BASE_ADDRESS, &s->flash);
  90. memory_region_add_subregion(system_memory, 0, &s->flash_alias);
  91. memory_region_init_ram(&s->sram, NULL, "STM32F405.sram", SRAM_SIZE,
  92. &err);
  93. if (err != NULL) {
  94. error_propagate(errp, err);
  95. return;
  96. }
  97. memory_region_add_subregion(system_memory, SRAM_BASE_ADDRESS, &s->sram);
  98. armv7m = DEVICE(&s->armv7m);
  99. qdev_prop_set_uint32(armv7m, "num-irq", 96);
  100. qdev_prop_set_string(armv7m, "cpu-type", s->cpu_type);
  101. qdev_prop_set_bit(armv7m, "enable-bitband", true);
  102. object_property_set_link(OBJECT(&s->armv7m), "memory",
  103. OBJECT(system_memory), &error_abort);
  104. if (!sysbus_realize(SYS_BUS_DEVICE(&s->armv7m), errp)) {
  105. return;
  106. }
  107. /* System configuration controller */
  108. dev = DEVICE(&s->syscfg);
  109. if (!sysbus_realize(SYS_BUS_DEVICE(&s->syscfg), errp)) {
  110. return;
  111. }
  112. busdev = SYS_BUS_DEVICE(dev);
  113. sysbus_mmio_map(busdev, 0, SYSCFG_ADD);
  114. sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(armv7m, SYSCFG_IRQ));
  115. /* Attach UART (uses USART registers) and USART controllers */
  116. for (i = 0; i < STM_NUM_USARTS; i++) {
  117. dev = DEVICE(&(s->usart[i]));
  118. qdev_prop_set_chr(dev, "chardev", serial_hd(i));
  119. if (!sysbus_realize(SYS_BUS_DEVICE(&s->usart[i]), errp)) {
  120. return;
  121. }
  122. busdev = SYS_BUS_DEVICE(dev);
  123. sysbus_mmio_map(busdev, 0, usart_addr[i]);
  124. sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(armv7m, usart_irq[i]));
  125. }
  126. /* Timer 2 to 5 */
  127. for (i = 0; i < STM_NUM_TIMERS; i++) {
  128. dev = DEVICE(&(s->timer[i]));
  129. qdev_prop_set_uint64(dev, "clock-frequency", 1000000000);
  130. if (!sysbus_realize(SYS_BUS_DEVICE(&s->timer[i]), errp)) {
  131. return;
  132. }
  133. busdev = SYS_BUS_DEVICE(dev);
  134. sysbus_mmio_map(busdev, 0, timer_addr[i]);
  135. sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(armv7m, timer_irq[i]));
  136. }
  137. /* ADC device, the IRQs are ORed together */
  138. if (!object_initialize_child_with_props(OBJECT(s), "adc-orirq",
  139. &s->adc_irqs, sizeof(s->adc_irqs),
  140. TYPE_OR_IRQ, errp, NULL)) {
  141. return;
  142. }
  143. object_property_set_int(OBJECT(&s->adc_irqs), "num-lines", STM_NUM_ADCS,
  144. &error_abort);
  145. if (!qdev_realize(DEVICE(&s->adc_irqs), NULL, errp)) {
  146. return;
  147. }
  148. qdev_connect_gpio_out(DEVICE(&s->adc_irqs), 0,
  149. qdev_get_gpio_in(armv7m, ADC_IRQ));
  150. for (i = 0; i < STM_NUM_ADCS; i++) {
  151. dev = DEVICE(&(s->adc[i]));
  152. if (!sysbus_realize(SYS_BUS_DEVICE(&s->adc[i]), errp)) {
  153. return;
  154. }
  155. busdev = SYS_BUS_DEVICE(dev);
  156. sysbus_mmio_map(busdev, 0, adc_addr[i]);
  157. sysbus_connect_irq(busdev, 0,
  158. qdev_get_gpio_in(DEVICE(&s->adc_irqs), i));
  159. }
  160. /* SPI devices */
  161. for (i = 0; i < STM_NUM_SPIS; i++) {
  162. dev = DEVICE(&(s->spi[i]));
  163. if (!sysbus_realize(SYS_BUS_DEVICE(&s->spi[i]), errp)) {
  164. return;
  165. }
  166. busdev = SYS_BUS_DEVICE(dev);
  167. sysbus_mmio_map(busdev, 0, spi_addr[i]);
  168. sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(armv7m, spi_irq[i]));
  169. }
  170. /* EXTI device */
  171. dev = DEVICE(&s->exti);
  172. if (!sysbus_realize(SYS_BUS_DEVICE(&s->exti), errp)) {
  173. return;
  174. }
  175. busdev = SYS_BUS_DEVICE(dev);
  176. sysbus_mmio_map(busdev, 0, EXTI_ADDR);
  177. for (i = 0; i < 16; i++) {
  178. sysbus_connect_irq(busdev, i, qdev_get_gpio_in(armv7m, exti_irq[i]));
  179. }
  180. for (i = 0; i < 16; i++) {
  181. qdev_connect_gpio_out(DEVICE(&s->syscfg), i, qdev_get_gpio_in(dev, i));
  182. }
  183. create_unimplemented_device("timer[7]", 0x40001400, 0x400);
  184. create_unimplemented_device("timer[12]", 0x40001800, 0x400);
  185. create_unimplemented_device("timer[6]", 0x40001000, 0x400);
  186. create_unimplemented_device("timer[13]", 0x40001C00, 0x400);
  187. create_unimplemented_device("timer[14]", 0x40002000, 0x400);
  188. create_unimplemented_device("RTC and BKP", 0x40002800, 0x400);
  189. create_unimplemented_device("WWDG", 0x40002C00, 0x400);
  190. create_unimplemented_device("IWDG", 0x40003000, 0x400);
  191. create_unimplemented_device("I2S2ext", 0x40003000, 0x400);
  192. create_unimplemented_device("I2S3ext", 0x40004000, 0x400);
  193. create_unimplemented_device("I2C1", 0x40005400, 0x400);
  194. create_unimplemented_device("I2C2", 0x40005800, 0x400);
  195. create_unimplemented_device("I2C3", 0x40005C00, 0x400);
  196. create_unimplemented_device("CAN1", 0x40006400, 0x400);
  197. create_unimplemented_device("CAN2", 0x40006800, 0x400);
  198. create_unimplemented_device("PWR", 0x40007000, 0x400);
  199. create_unimplemented_device("DAC", 0x40007400, 0x400);
  200. create_unimplemented_device("timer[1]", 0x40010000, 0x400);
  201. create_unimplemented_device("timer[8]", 0x40010400, 0x400);
  202. create_unimplemented_device("SDIO", 0x40012C00, 0x400);
  203. create_unimplemented_device("timer[9]", 0x40014000, 0x400);
  204. create_unimplemented_device("timer[10]", 0x40014400, 0x400);
  205. create_unimplemented_device("timer[11]", 0x40014800, 0x400);
  206. create_unimplemented_device("GPIOA", 0x40020000, 0x400);
  207. create_unimplemented_device("GPIOB", 0x40020400, 0x400);
  208. create_unimplemented_device("GPIOC", 0x40020800, 0x400);
  209. create_unimplemented_device("GPIOD", 0x40020C00, 0x400);
  210. create_unimplemented_device("GPIOE", 0x40021000, 0x400);
  211. create_unimplemented_device("GPIOF", 0x40021400, 0x400);
  212. create_unimplemented_device("GPIOG", 0x40021800, 0x400);
  213. create_unimplemented_device("GPIOH", 0x40021C00, 0x400);
  214. create_unimplemented_device("GPIOI", 0x40022000, 0x400);
  215. create_unimplemented_device("CRC", 0x40023000, 0x400);
  216. create_unimplemented_device("RCC", 0x40023800, 0x400);
  217. create_unimplemented_device("Flash Int", 0x40023C00, 0x400);
  218. create_unimplemented_device("BKPSRAM", 0x40024000, 0x400);
  219. create_unimplemented_device("DMA1", 0x40026000, 0x400);
  220. create_unimplemented_device("DMA2", 0x40026400, 0x400);
  221. create_unimplemented_device("Ethernet", 0x40028000, 0x1400);
  222. create_unimplemented_device("USB OTG HS", 0x40040000, 0x30000);
  223. create_unimplemented_device("USB OTG FS", 0x50000000, 0x31000);
  224. create_unimplemented_device("DCMI", 0x50050000, 0x400);
  225. create_unimplemented_device("RNG", 0x50060800, 0x400);
  226. }
  227. static Property stm32f405_soc_properties[] = {
  228. DEFINE_PROP_STRING("cpu-type", STM32F405State, cpu_type),
  229. DEFINE_PROP_END_OF_LIST(),
  230. };
  231. static void stm32f405_soc_class_init(ObjectClass *klass, void *data)
  232. {
  233. DeviceClass *dc = DEVICE_CLASS(klass);
  234. dc->realize = stm32f405_soc_realize;
  235. device_class_set_props(dc, stm32f405_soc_properties);
  236. /* No vmstate or reset required: device has no internal state */
  237. }
  238. static const TypeInfo stm32f405_soc_info = {
  239. .name = TYPE_STM32F405_SOC,
  240. .parent = TYPE_SYS_BUS_DEVICE,
  241. .instance_size = sizeof(STM32F405State),
  242. .instance_init = stm32f405_soc_initfn,
  243. .class_init = stm32f405_soc_class_init,
  244. };
  245. static void stm32f405_soc_types(void)
  246. {
  247. type_register_static(&stm32f405_soc_info);
  248. }
  249. type_init(stm32f405_soc_types)