2
0

stm32f4xx_syscfg.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * STM32F4xx SYSCFG
  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 "qemu/log.h"
  26. #include "trace.h"
  27. #include "hw/irq.h"
  28. #include "migration/vmstate.h"
  29. #include "hw/misc/stm32f4xx_syscfg.h"
  30. static void stm32f4xx_syscfg_reset(DeviceState *dev)
  31. {
  32. STM32F4xxSyscfgState *s = STM32F4XX_SYSCFG(dev);
  33. s->syscfg_memrmp = 0x00000000;
  34. s->syscfg_pmc = 0x00000000;
  35. s->syscfg_exticr[0] = 0x00000000;
  36. s->syscfg_exticr[1] = 0x00000000;
  37. s->syscfg_exticr[2] = 0x00000000;
  38. s->syscfg_exticr[3] = 0x00000000;
  39. s->syscfg_cmpcr = 0x00000000;
  40. }
  41. static void stm32f4xx_syscfg_set_irq(void *opaque, int irq, int level)
  42. {
  43. STM32F4xxSyscfgState *s = opaque;
  44. int icrreg = irq / 4;
  45. int startbit = (irq & 3) * 4;
  46. uint8_t config = irq / 16;
  47. trace_stm32f4xx_syscfg_set_irq(irq / 16, irq % 16, level);
  48. g_assert(icrreg < SYSCFG_NUM_EXTICR);
  49. if (extract32(s->syscfg_exticr[icrreg], startbit, 4) == config) {
  50. qemu_set_irq(s->gpio_out[irq], level);
  51. trace_stm32f4xx_pulse_exti(irq);
  52. }
  53. }
  54. static uint64_t stm32f4xx_syscfg_read(void *opaque, hwaddr addr,
  55. unsigned int size)
  56. {
  57. STM32F4xxSyscfgState *s = opaque;
  58. trace_stm32f4xx_syscfg_read(addr);
  59. switch (addr) {
  60. case SYSCFG_MEMRMP:
  61. return s->syscfg_memrmp;
  62. case SYSCFG_PMC:
  63. return s->syscfg_pmc;
  64. case SYSCFG_EXTICR1...SYSCFG_EXTICR4:
  65. return s->syscfg_exticr[addr / 4 - SYSCFG_EXTICR1 / 4];
  66. case SYSCFG_CMPCR:
  67. return s->syscfg_cmpcr;
  68. default:
  69. qemu_log_mask(LOG_GUEST_ERROR,
  70. "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
  71. return 0;
  72. }
  73. }
  74. static void stm32f4xx_syscfg_write(void *opaque, hwaddr addr,
  75. uint64_t val64, unsigned int size)
  76. {
  77. STM32F4xxSyscfgState *s = opaque;
  78. uint32_t value = val64;
  79. trace_stm32f4xx_syscfg_write(value, addr);
  80. switch (addr) {
  81. case SYSCFG_MEMRMP:
  82. qemu_log_mask(LOG_UNIMP,
  83. "%s: Changing the memory mapping isn't supported " \
  84. "in QEMU\n", __func__);
  85. return;
  86. case SYSCFG_PMC:
  87. qemu_log_mask(LOG_UNIMP,
  88. "%s: Changing the memory mapping isn't supported " \
  89. "in QEMU\n", __func__);
  90. return;
  91. case SYSCFG_EXTICR1...SYSCFG_EXTICR4:
  92. s->syscfg_exticr[addr / 4 - SYSCFG_EXTICR1 / 4] = (value & 0xFFFF);
  93. return;
  94. case SYSCFG_CMPCR:
  95. s->syscfg_cmpcr = value;
  96. return;
  97. default:
  98. qemu_log_mask(LOG_GUEST_ERROR,
  99. "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
  100. }
  101. }
  102. static const MemoryRegionOps stm32f4xx_syscfg_ops = {
  103. .read = stm32f4xx_syscfg_read,
  104. .write = stm32f4xx_syscfg_write,
  105. .endianness = DEVICE_NATIVE_ENDIAN,
  106. };
  107. static void stm32f4xx_syscfg_init(Object *obj)
  108. {
  109. STM32F4xxSyscfgState *s = STM32F4XX_SYSCFG(obj);
  110. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
  111. memory_region_init_io(&s->mmio, obj, &stm32f4xx_syscfg_ops, s,
  112. TYPE_STM32F4XX_SYSCFG, 0x400);
  113. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  114. qdev_init_gpio_in(DEVICE(obj), stm32f4xx_syscfg_set_irq, 16 * 9);
  115. qdev_init_gpio_out(DEVICE(obj), s->gpio_out, 16);
  116. }
  117. static const VMStateDescription vmstate_stm32f4xx_syscfg = {
  118. .name = TYPE_STM32F4XX_SYSCFG,
  119. .version_id = 1,
  120. .minimum_version_id = 1,
  121. .fields = (VMStateField[]) {
  122. VMSTATE_UINT32(syscfg_memrmp, STM32F4xxSyscfgState),
  123. VMSTATE_UINT32(syscfg_pmc, STM32F4xxSyscfgState),
  124. VMSTATE_UINT32_ARRAY(syscfg_exticr, STM32F4xxSyscfgState,
  125. SYSCFG_NUM_EXTICR),
  126. VMSTATE_UINT32(syscfg_cmpcr, STM32F4xxSyscfgState),
  127. VMSTATE_END_OF_LIST()
  128. }
  129. };
  130. static void stm32f4xx_syscfg_class_init(ObjectClass *klass, void *data)
  131. {
  132. DeviceClass *dc = DEVICE_CLASS(klass);
  133. dc->reset = stm32f4xx_syscfg_reset;
  134. dc->vmsd = &vmstate_stm32f4xx_syscfg;
  135. }
  136. static const TypeInfo stm32f4xx_syscfg_info = {
  137. .name = TYPE_STM32F4XX_SYSCFG,
  138. .parent = TYPE_SYS_BUS_DEVICE,
  139. .instance_size = sizeof(STM32F4xxSyscfgState),
  140. .instance_init = stm32f4xx_syscfg_init,
  141. .class_init = stm32f4xx_syscfg_class_init,
  142. };
  143. static void stm32f4xx_syscfg_register_types(void)
  144. {
  145. type_register_static(&stm32f4xx_syscfg_info);
  146. }
  147. type_init(stm32f4xx_syscfg_register_types)