2
0

imx7_src.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * IMX7 System Reset Controller
  3. *
  4. * Copyright (c) 2023 Jean-Christophe Dubois <jcd@tribudubois.net>
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. */
  10. #include "qemu/osdep.h"
  11. #include "hw/misc/imx7_src.h"
  12. #include "migration/vmstate.h"
  13. #include "qemu/bitops.h"
  14. #include "qemu/log.h"
  15. #include "qemu/main-loop.h"
  16. #include "qemu/module.h"
  17. #include "target/arm/arm-powerctl.h"
  18. #include "hw/core/cpu.h"
  19. #include "hw/registerfields.h"
  20. #include "trace.h"
  21. static const char *imx7_src_reg_name(uint32_t reg)
  22. {
  23. static char unknown[20];
  24. switch (reg) {
  25. case SRC_SCR:
  26. return "SRC_SCR";
  27. case SRC_A7RCR0:
  28. return "SRC_A7RCR0";
  29. case SRC_A7RCR1:
  30. return "SRC_A7RCR1";
  31. case SRC_M4RCR:
  32. return "SRC_M4RCR";
  33. case SRC_ERCR:
  34. return "SRC_ERCR";
  35. case SRC_HSICPHY_RCR:
  36. return "SRC_HSICPHY_RCR";
  37. case SRC_USBOPHY1_RCR:
  38. return "SRC_USBOPHY1_RCR";
  39. case SRC_USBOPHY2_RCR:
  40. return "SRC_USBOPHY2_RCR";
  41. case SRC_PCIEPHY_RCR:
  42. return "SRC_PCIEPHY_RCR";
  43. case SRC_SBMR1:
  44. return "SRC_SBMR1";
  45. case SRC_SRSR:
  46. return "SRC_SRSR";
  47. case SRC_SISR:
  48. return "SRC_SISR";
  49. case SRC_SIMR:
  50. return "SRC_SIMR";
  51. case SRC_SBMR2:
  52. return "SRC_SBMR2";
  53. case SRC_GPR1:
  54. return "SRC_GPR1";
  55. case SRC_GPR2:
  56. return "SRC_GPR2";
  57. case SRC_GPR3:
  58. return "SRC_GPR3";
  59. case SRC_GPR4:
  60. return "SRC_GPR4";
  61. case SRC_GPR5:
  62. return "SRC_GPR5";
  63. case SRC_GPR6:
  64. return "SRC_GPR6";
  65. case SRC_GPR7:
  66. return "SRC_GPR7";
  67. case SRC_GPR8:
  68. return "SRC_GPR8";
  69. case SRC_GPR9:
  70. return "SRC_GPR9";
  71. case SRC_GPR10:
  72. return "SRC_GPR10";
  73. default:
  74. snprintf(unknown, sizeof(unknown), "%u ?", reg);
  75. return unknown;
  76. }
  77. }
  78. static const VMStateDescription vmstate_imx7_src = {
  79. .name = TYPE_IMX7_SRC,
  80. .version_id = 1,
  81. .minimum_version_id = 1,
  82. .fields = (const VMStateField[]) {
  83. VMSTATE_UINT32_ARRAY(regs, IMX7SRCState, SRC_MAX),
  84. VMSTATE_END_OF_LIST()
  85. },
  86. };
  87. static void imx7_src_reset(DeviceState *dev)
  88. {
  89. IMX7SRCState *s = IMX7_SRC(dev);
  90. memset(s->regs, 0, sizeof(s->regs));
  91. /* Set reset values */
  92. s->regs[SRC_SCR] = 0xA0;
  93. s->regs[SRC_SRSR] = 0x1;
  94. s->regs[SRC_SIMR] = 0x1F;
  95. }
  96. static uint64_t imx7_src_read(void *opaque, hwaddr offset, unsigned size)
  97. {
  98. uint32_t value = 0;
  99. IMX7SRCState *s = (IMX7SRCState *)opaque;
  100. uint32_t index = offset >> 2;
  101. if (index < SRC_MAX) {
  102. value = s->regs[index];
  103. } else {
  104. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  105. HWADDR_PRIx "\n", TYPE_IMX7_SRC, __func__, offset);
  106. }
  107. trace_imx7_src_read(imx7_src_reg_name(index), value);
  108. return value;
  109. }
  110. /*
  111. * The reset is asynchronous so we need to defer clearing the reset
  112. * bit until the work is completed.
  113. */
  114. struct SRCSCRResetInfo {
  115. IMX7SRCState *s;
  116. uint32_t reset_bit;
  117. };
  118. static void imx7_clear_reset_bit(CPUState *cpu, run_on_cpu_data data)
  119. {
  120. struct SRCSCRResetInfo *ri = data.host_ptr;
  121. IMX7SRCState *s = ri->s;
  122. assert(bql_locked());
  123. s->regs[SRC_A7RCR0] = deposit32(s->regs[SRC_A7RCR0], ri->reset_bit, 1, 0);
  124. trace_imx7_src_write(imx7_src_reg_name(SRC_A7RCR0), s->regs[SRC_A7RCR0]);
  125. g_free(ri);
  126. }
  127. static void imx7_defer_clear_reset_bit(uint32_t cpuid,
  128. IMX7SRCState *s,
  129. uint32_t reset_shift)
  130. {
  131. struct SRCSCRResetInfo *ri;
  132. CPUState *cpu = arm_get_cpu_by_id(cpuid);
  133. if (!cpu) {
  134. return;
  135. }
  136. ri = g_new(struct SRCSCRResetInfo, 1);
  137. ri->s = s;
  138. ri->reset_bit = reset_shift;
  139. async_run_on_cpu(cpu, imx7_clear_reset_bit, RUN_ON_CPU_HOST_PTR(ri));
  140. }
  141. static void imx7_src_write(void *opaque, hwaddr offset, uint64_t value,
  142. unsigned size)
  143. {
  144. IMX7SRCState *s = (IMX7SRCState *)opaque;
  145. uint32_t index = offset >> 2;
  146. long unsigned int change_mask;
  147. uint32_t current_value = value;
  148. if (index >= SRC_MAX) {
  149. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  150. HWADDR_PRIx "\n", TYPE_IMX7_SRC, __func__, offset);
  151. return;
  152. }
  153. trace_imx7_src_write(imx7_src_reg_name(SRC_A7RCR0), s->regs[SRC_A7RCR0]);
  154. change_mask = s->regs[index] ^ (uint32_t)current_value;
  155. switch (index) {
  156. case SRC_A7RCR0:
  157. if (FIELD_EX32(change_mask, CORE0, RST)) {
  158. arm_reset_cpu(0);
  159. imx7_defer_clear_reset_bit(0, s, R_CORE0_RST_SHIFT);
  160. }
  161. if (FIELD_EX32(change_mask, CORE1, RST)) {
  162. arm_reset_cpu(1);
  163. imx7_defer_clear_reset_bit(1, s, R_CORE1_RST_SHIFT);
  164. }
  165. s->regs[index] = current_value;
  166. break;
  167. case SRC_A7RCR1:
  168. /*
  169. * On real hardware when the system reset controller starts a
  170. * secondary CPU it runs through some boot ROM code which reads
  171. * the SRC_GPRX registers controlling the start address and branches
  172. * to it.
  173. * Here we are taking a short cut and branching directly to the
  174. * requested address (we don't want to run the boot ROM code inside
  175. * QEMU)
  176. */
  177. if (FIELD_EX32(change_mask, CORE1, ENABLE)) {
  178. if (FIELD_EX32(current_value, CORE1, ENABLE)) {
  179. /* CORE 1 is brought up */
  180. arm_set_cpu_on(1, s->regs[SRC_GPR3], s->regs[SRC_GPR4],
  181. 3, false);
  182. } else {
  183. /* CORE 1 is shut down */
  184. arm_set_cpu_off(1);
  185. }
  186. /* We clear the reset bits as the processor changed state */
  187. imx7_defer_clear_reset_bit(1, s, R_CORE1_RST_SHIFT);
  188. clear_bit(R_CORE1_RST_SHIFT, &change_mask);
  189. }
  190. s->regs[index] = current_value;
  191. break;
  192. default:
  193. s->regs[index] = current_value;
  194. break;
  195. }
  196. }
  197. static const struct MemoryRegionOps imx7_src_ops = {
  198. .read = imx7_src_read,
  199. .write = imx7_src_write,
  200. .endianness = DEVICE_NATIVE_ENDIAN,
  201. .valid = {
  202. /*
  203. * Our device would not work correctly if the guest was doing
  204. * unaligned access. This might not be a limitation on the real
  205. * device but in practice there is no reason for a guest to access
  206. * this device unaligned.
  207. */
  208. .min_access_size = 4,
  209. .max_access_size = 4,
  210. .unaligned = false,
  211. },
  212. };
  213. static void imx7_src_realize(DeviceState *dev, Error **errp)
  214. {
  215. IMX7SRCState *s = IMX7_SRC(dev);
  216. memory_region_init_io(&s->iomem, OBJECT(dev), &imx7_src_ops, s,
  217. TYPE_IMX7_SRC, 0x1000);
  218. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
  219. }
  220. static void imx7_src_class_init(ObjectClass *klass, void *data)
  221. {
  222. DeviceClass *dc = DEVICE_CLASS(klass);
  223. dc->realize = imx7_src_realize;
  224. device_class_set_legacy_reset(dc, imx7_src_reset);
  225. dc->vmsd = &vmstate_imx7_src;
  226. dc->desc = "i.MX6 System Reset Controller";
  227. }
  228. static const TypeInfo imx7_src_info = {
  229. .name = TYPE_IMX7_SRC,
  230. .parent = TYPE_SYS_BUS_DEVICE,
  231. .instance_size = sizeof(IMX7SRCState),
  232. .class_init = imx7_src_class_init,
  233. };
  234. static void imx7_src_register_types(void)
  235. {
  236. type_register_static(&imx7_src_info);
  237. }
  238. type_init(imx7_src_register_types)