stm32l4x5_syscfg.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * STM32L4x5 SYSCFG (System Configuration Controller)
  3. *
  4. * Copyright (c) 2023 Arnaud Minier <arnaud.minier@telecom-paris.fr>
  5. * Copyright (c) 2023 Inès Varhol <ines.varhol@telecom-paris.fr>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0-or-later
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  10. * See the COPYING file in the top-level directory.
  11. *
  12. * This work is based on the stm32f4xx_syscfg by Alistair Francis.
  13. * Original code is licensed under the MIT License:
  14. *
  15. * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me>
  16. */
  17. /*
  18. * The reference used is the STMicroElectronics RM0351 Reference manual
  19. * for STM32L4x5 and STM32L4x6 advanced Arm ® -based 32-bit MCUs.
  20. * https://www.st.com/en/microcontrollers-microprocessors/stm32l4x5/documentation.html
  21. */
  22. #include "qemu/osdep.h"
  23. #include "qemu/log.h"
  24. #include "trace.h"
  25. #include "hw/irq.h"
  26. #include "migration/vmstate.h"
  27. #include "hw/misc/stm32l4x5_syscfg.h"
  28. #include "hw/gpio/stm32l4x5_gpio.h"
  29. #define SYSCFG_MEMRMP 0x00
  30. #define SYSCFG_CFGR1 0x04
  31. #define SYSCFG_EXTICR1 0x08
  32. #define SYSCFG_EXTICR2 0x0C
  33. #define SYSCFG_EXTICR3 0x10
  34. #define SYSCFG_EXTICR4 0x14
  35. #define SYSCFG_SCSR 0x18
  36. #define SYSCFG_CFGR2 0x1C
  37. #define SYSCFG_SWPR 0x20
  38. #define SYSCFG_SKR 0x24
  39. #define SYSCFG_SWPR2 0x28
  40. /* 00000000_00000000_00000001_00000111 */
  41. #define ACTIVABLE_BITS_MEMRP 0x00000107
  42. /* 11111100_11111111_00000001_00000000 */
  43. #define ACTIVABLE_BITS_CFGR1 0xFCFF0100
  44. /* 00000000_00000000_00000000_00000001 */
  45. #define FIREWALL_DISABLE_CFGR1 0x00000001
  46. /* 00000000_00000000_11111111_11111111 */
  47. #define ACTIVABLE_BITS_EXTICR 0x0000FFFF
  48. /* 00000000_00000000_00000000_00000011 */
  49. /* #define ACTIVABLE_BITS_SCSR 0x00000003 */
  50. /* 00000000_00000000_00000000_00001111 */
  51. #define ECC_LOCK_CFGR2 0x0000000F
  52. /* 00000000_00000000_00000001_00000000 */
  53. #define SRAM2_PARITY_ERROR_FLAG_CFGR2 0x00000100
  54. /* 00000000_00000000_00000000_11111111 */
  55. #define ACTIVABLE_BITS_SKR 0x000000FF
  56. #define NUM_LINES_PER_EXTICR_REG 4
  57. static void stm32l4x5_syscfg_hold_reset(Object *obj, ResetType type)
  58. {
  59. Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(obj);
  60. s->memrmp = 0x00000000;
  61. s->cfgr1 = 0x7C000001;
  62. s->exticr[0] = 0x00000000;
  63. s->exticr[1] = 0x00000000;
  64. s->exticr[2] = 0x00000000;
  65. s->exticr[3] = 0x00000000;
  66. s->scsr = 0x00000000;
  67. s->cfgr2 = 0x00000000;
  68. s->swpr = 0x00000000;
  69. s->skr = 0x00000000;
  70. s->swpr2 = 0x00000000;
  71. }
  72. static void stm32l4x5_syscfg_set_irq(void *opaque, int irq, int level)
  73. {
  74. Stm32l4x5SyscfgState *s = opaque;
  75. const uint8_t gpio = irq / GPIO_NUM_PINS;
  76. const int line = irq % GPIO_NUM_PINS;
  77. const int exticr_reg = line / NUM_LINES_PER_EXTICR_REG;
  78. const int startbit = (line % NUM_LINES_PER_EXTICR_REG) * 4;
  79. g_assert(gpio < NUM_GPIOS);
  80. trace_stm32l4x5_syscfg_set_irq(gpio, line, level);
  81. if (extract32(s->exticr[exticr_reg], startbit, 4) == gpio) {
  82. trace_stm32l4x5_syscfg_forward_exti(line);
  83. qemu_set_irq(s->gpio_out[line], level);
  84. }
  85. }
  86. static uint64_t stm32l4x5_syscfg_read(void *opaque, hwaddr addr,
  87. unsigned int size)
  88. {
  89. Stm32l4x5SyscfgState *s = opaque;
  90. trace_stm32l4x5_syscfg_read(addr);
  91. switch (addr) {
  92. case SYSCFG_MEMRMP:
  93. return s->memrmp;
  94. case SYSCFG_CFGR1:
  95. return s->cfgr1;
  96. case SYSCFG_EXTICR1...SYSCFG_EXTICR4:
  97. return s->exticr[(addr - SYSCFG_EXTICR1) / 4];
  98. case SYSCFG_SCSR:
  99. return s->scsr;
  100. case SYSCFG_CFGR2:
  101. return s->cfgr2;
  102. case SYSCFG_SWPR:
  103. return s->swpr;
  104. case SYSCFG_SKR:
  105. return s->skr;
  106. case SYSCFG_SWPR2:
  107. return s->swpr2;
  108. default:
  109. qemu_log_mask(LOG_GUEST_ERROR,
  110. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr);
  111. return 0;
  112. }
  113. }
  114. static void stm32l4x5_syscfg_write(void *opaque, hwaddr addr,
  115. uint64_t value, unsigned int size)
  116. {
  117. Stm32l4x5SyscfgState *s = opaque;
  118. trace_stm32l4x5_syscfg_write(addr, value);
  119. switch (addr) {
  120. case SYSCFG_MEMRMP:
  121. qemu_log_mask(LOG_UNIMP,
  122. "%s: Changing the memory mapping isn't supported\n",
  123. __func__);
  124. s->memrmp = value & ACTIVABLE_BITS_MEMRP;
  125. return;
  126. case SYSCFG_CFGR1:
  127. qemu_log_mask(LOG_UNIMP,
  128. "%s: Functions in CFGRx aren't supported\n",
  129. __func__);
  130. /* bit 0 (firewall dis.) is cleared by software, set only by reset. */
  131. s->cfgr1 = (s->cfgr1 & value & FIREWALL_DISABLE_CFGR1) |
  132. (value & ACTIVABLE_BITS_CFGR1);
  133. return;
  134. case SYSCFG_EXTICR1...SYSCFG_EXTICR4:
  135. s->exticr[(addr - SYSCFG_EXTICR1) / 4] =
  136. (value & ACTIVABLE_BITS_EXTICR);
  137. return;
  138. case SYSCFG_SCSR:
  139. qemu_log_mask(LOG_UNIMP,
  140. "%s: Erasing SRAM2 isn't supported\n",
  141. __func__);
  142. /*
  143. * only non reserved bits are :
  144. * bit 0 (write-protected by a passkey), bit 1 (meant to be read)
  145. * so it serves no purpose yet to add :
  146. * s->scsr = value & 0x3;
  147. */
  148. return;
  149. case SYSCFG_CFGR2:
  150. qemu_log_mask(LOG_UNIMP,
  151. "%s: Functions in CFGRx aren't supported\n",
  152. __func__);
  153. /* bit 8 (SRAM2 PEF) is cleared by software by writing a '1'.*/
  154. /* bits[3:0] (ECC Lock) are set by software, cleared only by reset.*/
  155. s->cfgr2 = (s->cfgr2 | (value & ECC_LOCK_CFGR2)) &
  156. ~(value & SRAM2_PARITY_ERROR_FLAG_CFGR2);
  157. return;
  158. case SYSCFG_SWPR:
  159. qemu_log_mask(LOG_UNIMP,
  160. "%s: Write protecting SRAM2 isn't supported\n",
  161. __func__);
  162. /* These bits are set by software and cleared only by reset.*/
  163. s->swpr |= value;
  164. return;
  165. case SYSCFG_SKR:
  166. qemu_log_mask(LOG_UNIMP,
  167. "%s: Erasing SRAM2 isn't supported\n",
  168. __func__);
  169. s->skr = value & ACTIVABLE_BITS_SKR;
  170. return;
  171. case SYSCFG_SWPR2:
  172. qemu_log_mask(LOG_UNIMP,
  173. "%s: Write protecting SRAM2 isn't supported\n",
  174. __func__);
  175. /* These bits are set by software and cleared only by reset.*/
  176. s->swpr2 |= value;
  177. return;
  178. default:
  179. qemu_log_mask(LOG_GUEST_ERROR,
  180. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr);
  181. }
  182. }
  183. static const MemoryRegionOps stm32l4x5_syscfg_ops = {
  184. .read = stm32l4x5_syscfg_read,
  185. .write = stm32l4x5_syscfg_write,
  186. .endianness = DEVICE_NATIVE_ENDIAN,
  187. .impl.min_access_size = 4,
  188. .impl.max_access_size = 4,
  189. .impl.unaligned = false,
  190. .valid.min_access_size = 4,
  191. .valid.max_access_size = 4,
  192. .valid.unaligned = false,
  193. };
  194. static void stm32l4x5_syscfg_init(Object *obj)
  195. {
  196. Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(obj);
  197. memory_region_init_io(&s->mmio, obj, &stm32l4x5_syscfg_ops, s,
  198. TYPE_STM32L4X5_SYSCFG, 0x400);
  199. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  200. qdev_init_gpio_in(DEVICE(obj), stm32l4x5_syscfg_set_irq,
  201. GPIO_NUM_PINS * NUM_GPIOS);
  202. qdev_init_gpio_out(DEVICE(obj), s->gpio_out, GPIO_NUM_PINS);
  203. }
  204. static const VMStateDescription vmstate_stm32l4x5_syscfg = {
  205. .name = TYPE_STM32L4X5_SYSCFG,
  206. .version_id = 1,
  207. .minimum_version_id = 1,
  208. .fields = (VMStateField[]) {
  209. VMSTATE_UINT32(memrmp, Stm32l4x5SyscfgState),
  210. VMSTATE_UINT32(cfgr1, Stm32l4x5SyscfgState),
  211. VMSTATE_UINT32_ARRAY(exticr, Stm32l4x5SyscfgState,
  212. SYSCFG_NUM_EXTICR),
  213. VMSTATE_UINT32(scsr, Stm32l4x5SyscfgState),
  214. VMSTATE_UINT32(cfgr2, Stm32l4x5SyscfgState),
  215. VMSTATE_UINT32(swpr, Stm32l4x5SyscfgState),
  216. VMSTATE_UINT32(skr, Stm32l4x5SyscfgState),
  217. VMSTATE_UINT32(swpr2, Stm32l4x5SyscfgState),
  218. VMSTATE_END_OF_LIST()
  219. }
  220. };
  221. static void stm32l4x5_syscfg_class_init(ObjectClass *klass, void *data)
  222. {
  223. DeviceClass *dc = DEVICE_CLASS(klass);
  224. ResettableClass *rc = RESETTABLE_CLASS(klass);
  225. dc->vmsd = &vmstate_stm32l4x5_syscfg;
  226. rc->phases.hold = stm32l4x5_syscfg_hold_reset;
  227. }
  228. static const TypeInfo stm32l4x5_syscfg_info[] = {
  229. {
  230. .name = TYPE_STM32L4X5_SYSCFG,
  231. .parent = TYPE_SYS_BUS_DEVICE,
  232. .instance_size = sizeof(Stm32l4x5SyscfgState),
  233. .instance_init = stm32l4x5_syscfg_init,
  234. .class_init = stm32l4x5_syscfg_class_init,
  235. }
  236. };
  237. DEFINE_TYPES(stm32l4x5_syscfg_info)