stm32l4x5_syscfg.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/clock.h"
  28. #include "hw/qdev-clock.h"
  29. #include "qapi/error.h"
  30. #include "hw/misc/stm32l4x5_syscfg.h"
  31. #include "hw/gpio/stm32l4x5_gpio.h"
  32. #define SYSCFG_MEMRMP 0x00
  33. #define SYSCFG_CFGR1 0x04
  34. #define SYSCFG_EXTICR1 0x08
  35. #define SYSCFG_EXTICR2 0x0C
  36. #define SYSCFG_EXTICR3 0x10
  37. #define SYSCFG_EXTICR4 0x14
  38. #define SYSCFG_SCSR 0x18
  39. #define SYSCFG_CFGR2 0x1C
  40. #define SYSCFG_SWPR 0x20
  41. #define SYSCFG_SKR 0x24
  42. #define SYSCFG_SWPR2 0x28
  43. /* 00000000_00000000_00000001_00000111 */
  44. #define ACTIVABLE_BITS_MEMRP 0x00000107
  45. /* 11111100_11111111_00000001_00000000 */
  46. #define ACTIVABLE_BITS_CFGR1 0xFCFF0100
  47. /* 00000000_00000000_00000000_00000001 */
  48. #define FIREWALL_DISABLE_CFGR1 0x00000001
  49. /* 00000000_00000000_11111111_11111111 */
  50. #define ACTIVABLE_BITS_EXTICR 0x0000FFFF
  51. /* 00000000_00000000_00000000_00000011 */
  52. /* #define ACTIVABLE_BITS_SCSR 0x00000003 */
  53. /* 00000000_00000000_00000000_00001111 */
  54. #define ECC_LOCK_CFGR2 0x0000000F
  55. /* 00000000_00000000_00000001_00000000 */
  56. #define SRAM2_PARITY_ERROR_FLAG_CFGR2 0x00000100
  57. /* 00000000_00000000_00000000_11111111 */
  58. #define ACTIVABLE_BITS_SKR 0x000000FF
  59. #define NUM_LINES_PER_EXTICR_REG 4
  60. static void stm32l4x5_syscfg_hold_reset(Object *obj, ResetType type)
  61. {
  62. Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(obj);
  63. s->memrmp = 0x00000000;
  64. s->cfgr1 = 0x7C000001;
  65. s->exticr[0] = 0x00000000;
  66. s->exticr[1] = 0x00000000;
  67. s->exticr[2] = 0x00000000;
  68. s->exticr[3] = 0x00000000;
  69. s->scsr = 0x00000000;
  70. s->cfgr2 = 0x00000000;
  71. s->swpr = 0x00000000;
  72. s->skr = 0x00000000;
  73. s->swpr2 = 0x00000000;
  74. }
  75. static void stm32l4x5_syscfg_set_irq(void *opaque, int irq, int level)
  76. {
  77. Stm32l4x5SyscfgState *s = opaque;
  78. const uint8_t gpio = irq / GPIO_NUM_PINS;
  79. const int line = irq % GPIO_NUM_PINS;
  80. const int exticr_reg = line / NUM_LINES_PER_EXTICR_REG;
  81. const int startbit = (line % NUM_LINES_PER_EXTICR_REG) * 4;
  82. g_assert(gpio < NUM_GPIOS);
  83. trace_stm32l4x5_syscfg_set_irq(gpio, line, level);
  84. if (extract32(s->exticr[exticr_reg], startbit, 4) == gpio) {
  85. trace_stm32l4x5_syscfg_forward_exti(line);
  86. qemu_set_irq(s->gpio_out[line], level);
  87. }
  88. }
  89. static uint64_t stm32l4x5_syscfg_read(void *opaque, hwaddr addr,
  90. unsigned int size)
  91. {
  92. Stm32l4x5SyscfgState *s = opaque;
  93. trace_stm32l4x5_syscfg_read(addr);
  94. switch (addr) {
  95. case SYSCFG_MEMRMP:
  96. return s->memrmp;
  97. case SYSCFG_CFGR1:
  98. return s->cfgr1;
  99. case SYSCFG_EXTICR1...SYSCFG_EXTICR4:
  100. return s->exticr[(addr - SYSCFG_EXTICR1) / 4];
  101. case SYSCFG_SCSR:
  102. return s->scsr;
  103. case SYSCFG_CFGR2:
  104. return s->cfgr2;
  105. case SYSCFG_SWPR:
  106. return s->swpr;
  107. case SYSCFG_SKR:
  108. return s->skr;
  109. case SYSCFG_SWPR2:
  110. return s->swpr2;
  111. default:
  112. qemu_log_mask(LOG_GUEST_ERROR,
  113. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr);
  114. return 0;
  115. }
  116. }
  117. static void stm32l4x5_syscfg_write(void *opaque, hwaddr addr,
  118. uint64_t value, unsigned int size)
  119. {
  120. Stm32l4x5SyscfgState *s = opaque;
  121. trace_stm32l4x5_syscfg_write(addr, value);
  122. switch (addr) {
  123. case SYSCFG_MEMRMP:
  124. qemu_log_mask(LOG_UNIMP,
  125. "%s: Changing the memory mapping isn't supported\n",
  126. __func__);
  127. s->memrmp = value & ACTIVABLE_BITS_MEMRP;
  128. return;
  129. case SYSCFG_CFGR1:
  130. qemu_log_mask(LOG_UNIMP,
  131. "%s: Functions in CFGRx aren't supported\n",
  132. __func__);
  133. /* bit 0 (firewall dis.) is cleared by software, set only by reset. */
  134. s->cfgr1 = (s->cfgr1 & value & FIREWALL_DISABLE_CFGR1) |
  135. (value & ACTIVABLE_BITS_CFGR1);
  136. return;
  137. case SYSCFG_EXTICR1...SYSCFG_EXTICR4:
  138. s->exticr[(addr - SYSCFG_EXTICR1) / 4] =
  139. (value & ACTIVABLE_BITS_EXTICR);
  140. return;
  141. case SYSCFG_SCSR:
  142. qemu_log_mask(LOG_UNIMP,
  143. "%s: Erasing SRAM2 isn't supported\n",
  144. __func__);
  145. /*
  146. * only non reserved bits are :
  147. * bit 0 (write-protected by a passkey), bit 1 (meant to be read)
  148. * so it serves no purpose yet to add :
  149. * s->scsr = value & 0x3;
  150. */
  151. return;
  152. case SYSCFG_CFGR2:
  153. qemu_log_mask(LOG_UNIMP,
  154. "%s: Functions in CFGRx aren't supported\n",
  155. __func__);
  156. /* bit 8 (SRAM2 PEF) is cleared by software by writing a '1'.*/
  157. /* bits[3:0] (ECC Lock) are set by software, cleared only by reset.*/
  158. s->cfgr2 = (s->cfgr2 | (value & ECC_LOCK_CFGR2)) &
  159. ~(value & SRAM2_PARITY_ERROR_FLAG_CFGR2);
  160. return;
  161. case SYSCFG_SWPR:
  162. qemu_log_mask(LOG_UNIMP,
  163. "%s: Write protecting SRAM2 isn't supported\n",
  164. __func__);
  165. /* These bits are set by software and cleared only by reset.*/
  166. s->swpr |= value;
  167. return;
  168. case SYSCFG_SKR:
  169. qemu_log_mask(LOG_UNIMP,
  170. "%s: Erasing SRAM2 isn't supported\n",
  171. __func__);
  172. s->skr = value & ACTIVABLE_BITS_SKR;
  173. return;
  174. case SYSCFG_SWPR2:
  175. qemu_log_mask(LOG_UNIMP,
  176. "%s: Write protecting SRAM2 isn't supported\n",
  177. __func__);
  178. /* These bits are set by software and cleared only by reset.*/
  179. s->swpr2 |= value;
  180. return;
  181. default:
  182. qemu_log_mask(LOG_GUEST_ERROR,
  183. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, addr);
  184. }
  185. }
  186. static const MemoryRegionOps stm32l4x5_syscfg_ops = {
  187. .read = stm32l4x5_syscfg_read,
  188. .write = stm32l4x5_syscfg_write,
  189. .endianness = DEVICE_NATIVE_ENDIAN,
  190. .impl.min_access_size = 4,
  191. .impl.max_access_size = 4,
  192. .impl.unaligned = false,
  193. .valid.min_access_size = 4,
  194. .valid.max_access_size = 4,
  195. .valid.unaligned = false,
  196. };
  197. static void stm32l4x5_syscfg_init(Object *obj)
  198. {
  199. Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(obj);
  200. memory_region_init_io(&s->mmio, obj, &stm32l4x5_syscfg_ops, s,
  201. TYPE_STM32L4X5_SYSCFG, 0x400);
  202. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  203. qdev_init_gpio_in(DEVICE(obj), stm32l4x5_syscfg_set_irq,
  204. GPIO_NUM_PINS * NUM_GPIOS);
  205. qdev_init_gpio_out(DEVICE(obj), s->gpio_out, GPIO_NUM_PINS);
  206. s->clk = qdev_init_clock_in(DEVICE(s), "clk", NULL, s, 0);
  207. }
  208. static void stm32l4x5_syscfg_realize(DeviceState *dev, Error **errp)
  209. {
  210. Stm32l4x5SyscfgState *s = STM32L4X5_SYSCFG(dev);
  211. if (!clock_has_source(s->clk)) {
  212. error_setg(errp, "SYSCFG: clk input must be connected");
  213. return;
  214. }
  215. }
  216. static const VMStateDescription vmstate_stm32l4x5_syscfg = {
  217. .name = TYPE_STM32L4X5_SYSCFG,
  218. .version_id = 2,
  219. .minimum_version_id = 2,
  220. .fields = (VMStateField[]) {
  221. VMSTATE_UINT32(memrmp, Stm32l4x5SyscfgState),
  222. VMSTATE_UINT32(cfgr1, Stm32l4x5SyscfgState),
  223. VMSTATE_UINT32_ARRAY(exticr, Stm32l4x5SyscfgState,
  224. SYSCFG_NUM_EXTICR),
  225. VMSTATE_UINT32(scsr, Stm32l4x5SyscfgState),
  226. VMSTATE_UINT32(cfgr2, Stm32l4x5SyscfgState),
  227. VMSTATE_UINT32(swpr, Stm32l4x5SyscfgState),
  228. VMSTATE_UINT32(skr, Stm32l4x5SyscfgState),
  229. VMSTATE_UINT32(swpr2, Stm32l4x5SyscfgState),
  230. VMSTATE_CLOCK(clk, Stm32l4x5SyscfgState),
  231. VMSTATE_END_OF_LIST()
  232. }
  233. };
  234. static void stm32l4x5_syscfg_class_init(ObjectClass *klass, void *data)
  235. {
  236. DeviceClass *dc = DEVICE_CLASS(klass);
  237. ResettableClass *rc = RESETTABLE_CLASS(klass);
  238. dc->vmsd = &vmstate_stm32l4x5_syscfg;
  239. dc->realize = stm32l4x5_syscfg_realize;
  240. rc->phases.hold = stm32l4x5_syscfg_hold_reset;
  241. }
  242. static const TypeInfo stm32l4x5_syscfg_info[] = {
  243. {
  244. .name = TYPE_STM32L4X5_SYSCFG,
  245. .parent = TYPE_SYS_BUS_DEVICE,
  246. .instance_size = sizeof(Stm32l4x5SyscfgState),
  247. .instance_init = stm32l4x5_syscfg_init,
  248. .class_init = stm32l4x5_syscfg_class_init,
  249. }
  250. };
  251. DEFINE_TYPES(stm32l4x5_syscfg_info)