armv7m_systick.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * ARMv7M SysTick timer
  3. *
  4. * Copyright (c) 2006-2007 CodeSourcery.
  5. * Written by Paul Brook
  6. * Copyright (c) 2017 Linaro Ltd
  7. * Written by Peter Maydell
  8. *
  9. * This code is licensed under the GPL (version 2 or later).
  10. */
  11. #include "qemu/osdep.h"
  12. #include "hw/timer/armv7m_systick.h"
  13. #include "migration/vmstate.h"
  14. #include "hw/irq.h"
  15. #include "hw/sysbus.h"
  16. #include "hw/qdev-clock.h"
  17. #include "qemu/timer.h"
  18. #include "qemu/log.h"
  19. #include "qemu/module.h"
  20. #include "qapi/error.h"
  21. #include "trace.h"
  22. #define SYSTICK_ENABLE (1 << 0)
  23. #define SYSTICK_TICKINT (1 << 1)
  24. #define SYSTICK_CLKSOURCE (1 << 2)
  25. #define SYSTICK_COUNTFLAG (1 << 16)
  26. #define SYSCALIB_NOREF (1U << 31)
  27. #define SYSCALIB_SKEW (1U << 30)
  28. #define SYSCALIB_TENMS ((1U << 24) - 1)
  29. static void systick_set_period_from_clock(SysTickState *s)
  30. {
  31. /*
  32. * Set the ptimer period from whichever clock is selected.
  33. * Must be called from within a ptimer transaction block.
  34. */
  35. if (s->control & SYSTICK_CLKSOURCE) {
  36. ptimer_set_period_from_clock(s->ptimer, s->cpuclk, 1);
  37. } else {
  38. ptimer_set_period_from_clock(s->ptimer, s->refclk, 1);
  39. }
  40. }
  41. static void systick_timer_tick(void *opaque)
  42. {
  43. SysTickState *s = (SysTickState *)opaque;
  44. trace_systick_timer_tick();
  45. s->control |= SYSTICK_COUNTFLAG;
  46. if (s->control & SYSTICK_TICKINT) {
  47. /* Tell the NVIC to pend the SysTick exception */
  48. qemu_irq_pulse(s->irq);
  49. }
  50. if (ptimer_get_limit(s->ptimer) == 0) {
  51. /*
  52. * Timer expiry with SYST_RVR zero disables the timer
  53. * (but doesn't clear SYST_CSR.ENABLE)
  54. */
  55. ptimer_stop(s->ptimer);
  56. }
  57. }
  58. static MemTxResult systick_read(void *opaque, hwaddr addr, uint64_t *data,
  59. unsigned size, MemTxAttrs attrs)
  60. {
  61. SysTickState *s = opaque;
  62. uint32_t val;
  63. if (attrs.user) {
  64. /* Generate BusFault for unprivileged accesses */
  65. return MEMTX_ERROR;
  66. }
  67. switch (addr) {
  68. case 0x0: /* SysTick Control and Status. */
  69. val = s->control;
  70. s->control &= ~SYSTICK_COUNTFLAG;
  71. break;
  72. case 0x4: /* SysTick Reload Value. */
  73. val = ptimer_get_limit(s->ptimer);
  74. break;
  75. case 0x8: /* SysTick Current Value. */
  76. val = ptimer_get_count(s->ptimer);
  77. break;
  78. case 0xc: /* SysTick Calibration Value. */
  79. /*
  80. * In real hardware it is possible to make this register report
  81. * a different value from what the reference clock is actually
  82. * running at. We don't model that (which usually happens due
  83. * to integration errors in the real hardware) and instead always
  84. * report the theoretical correct value as described in the
  85. * knowledgebase article at
  86. * https://developer.arm.com/documentation/ka001325/latest
  87. * If necessary, we could implement an extra QOM property on this
  88. * device to force the STCALIB value to something different from
  89. * the "correct" value.
  90. */
  91. if (!clock_has_source(s->refclk)) {
  92. val = SYSCALIB_NOREF;
  93. break;
  94. }
  95. val = clock_ns_to_ticks(s->refclk, 10 * SCALE_MS) - 1;
  96. val &= SYSCALIB_TENMS;
  97. if (clock_ticks_to_ns(s->refclk, val + 1) != 10 * SCALE_MS) {
  98. /* report that tick count does not yield exactly 10ms */
  99. val |= SYSCALIB_SKEW;
  100. }
  101. break;
  102. default:
  103. val = 0;
  104. qemu_log_mask(LOG_GUEST_ERROR,
  105. "SysTick: Bad read offset 0x%" HWADDR_PRIx "\n", addr);
  106. break;
  107. }
  108. trace_systick_read(addr, val, size);
  109. *data = val;
  110. return MEMTX_OK;
  111. }
  112. static MemTxResult systick_write(void *opaque, hwaddr addr,
  113. uint64_t value, unsigned size,
  114. MemTxAttrs attrs)
  115. {
  116. SysTickState *s = opaque;
  117. if (attrs.user) {
  118. /* Generate BusFault for unprivileged accesses */
  119. return MEMTX_ERROR;
  120. }
  121. trace_systick_write(addr, value, size);
  122. switch (addr) {
  123. case 0x0: /* SysTick Control and Status. */
  124. {
  125. uint32_t oldval;
  126. if (!clock_has_source(s->refclk)) {
  127. /* This bit is always 1 if there is no external refclk */
  128. value |= SYSTICK_CLKSOURCE;
  129. }
  130. ptimer_transaction_begin(s->ptimer);
  131. oldval = s->control;
  132. s->control &= 0xfffffff8;
  133. s->control |= value & 7;
  134. if ((oldval ^ value) & SYSTICK_CLKSOURCE) {
  135. systick_set_period_from_clock(s);
  136. }
  137. if ((oldval ^ value) & SYSTICK_ENABLE) {
  138. if (value & SYSTICK_ENABLE) {
  139. ptimer_run(s->ptimer, 0);
  140. } else {
  141. ptimer_stop(s->ptimer);
  142. }
  143. }
  144. ptimer_transaction_commit(s->ptimer);
  145. break;
  146. }
  147. case 0x4: /* SysTick Reload Value. */
  148. ptimer_transaction_begin(s->ptimer);
  149. ptimer_set_limit(s->ptimer, value & 0xffffff, 0);
  150. ptimer_transaction_commit(s->ptimer);
  151. break;
  152. case 0x8: /* SysTick Current Value. */
  153. /*
  154. * Writing any value clears SYST_CVR to zero and clears
  155. * SYST_CSR.COUNTFLAG. The counter will then reload from SYST_RVR
  156. * on the next clock edge unless SYST_RVR is zero.
  157. */
  158. ptimer_transaction_begin(s->ptimer);
  159. if (ptimer_get_limit(s->ptimer) == 0) {
  160. ptimer_stop(s->ptimer);
  161. }
  162. ptimer_set_count(s->ptimer, 0);
  163. s->control &= ~SYSTICK_COUNTFLAG;
  164. ptimer_transaction_commit(s->ptimer);
  165. break;
  166. default:
  167. qemu_log_mask(LOG_GUEST_ERROR,
  168. "SysTick: Bad write offset 0x%" HWADDR_PRIx "\n", addr);
  169. }
  170. return MEMTX_OK;
  171. }
  172. static const MemoryRegionOps systick_ops = {
  173. .read_with_attrs = systick_read,
  174. .write_with_attrs = systick_write,
  175. .endianness = DEVICE_NATIVE_ENDIAN,
  176. .valid.min_access_size = 4,
  177. .valid.max_access_size = 4,
  178. };
  179. static void systick_reset(DeviceState *dev)
  180. {
  181. SysTickState *s = SYSTICK(dev);
  182. ptimer_transaction_begin(s->ptimer);
  183. s->control = 0;
  184. if (!clock_has_source(s->refclk)) {
  185. /* This bit is always 1 if there is no external refclk */
  186. s->control |= SYSTICK_CLKSOURCE;
  187. }
  188. ptimer_stop(s->ptimer);
  189. ptimer_set_count(s->ptimer, 0);
  190. ptimer_set_limit(s->ptimer, 0, 0);
  191. systick_set_period_from_clock(s);
  192. ptimer_transaction_commit(s->ptimer);
  193. }
  194. static void systick_cpuclk_update(void *opaque, ClockEvent event)
  195. {
  196. SysTickState *s = SYSTICK(opaque);
  197. if (!(s->control & SYSTICK_CLKSOURCE)) {
  198. /* currently using refclk, we can ignore cpuclk changes */
  199. }
  200. ptimer_transaction_begin(s->ptimer);
  201. ptimer_set_period_from_clock(s->ptimer, s->cpuclk, 1);
  202. ptimer_transaction_commit(s->ptimer);
  203. }
  204. static void systick_refclk_update(void *opaque, ClockEvent event)
  205. {
  206. SysTickState *s = SYSTICK(opaque);
  207. if (s->control & SYSTICK_CLKSOURCE) {
  208. /* currently using cpuclk, we can ignore refclk changes */
  209. }
  210. ptimer_transaction_begin(s->ptimer);
  211. ptimer_set_period_from_clock(s->ptimer, s->refclk, 1);
  212. ptimer_transaction_commit(s->ptimer);
  213. }
  214. static void systick_instance_init(Object *obj)
  215. {
  216. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  217. SysTickState *s = SYSTICK(obj);
  218. memory_region_init_io(&s->iomem, obj, &systick_ops, s, "systick", 0xe0);
  219. sysbus_init_mmio(sbd, &s->iomem);
  220. sysbus_init_irq(sbd, &s->irq);
  221. s->refclk = qdev_init_clock_in(DEVICE(obj), "refclk",
  222. systick_refclk_update, s, ClockUpdate);
  223. s->cpuclk = qdev_init_clock_in(DEVICE(obj), "cpuclk",
  224. systick_cpuclk_update, s, ClockUpdate);
  225. }
  226. static void systick_realize(DeviceState *dev, Error **errp)
  227. {
  228. SysTickState *s = SYSTICK(dev);
  229. s->ptimer = ptimer_init(systick_timer_tick, s,
  230. PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD |
  231. PTIMER_POLICY_NO_COUNTER_ROUND_DOWN |
  232. PTIMER_POLICY_NO_IMMEDIATE_RELOAD |
  233. PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT);
  234. if (!clock_has_source(s->cpuclk)) {
  235. error_setg(errp, "systick: cpuclk must be connected");
  236. return;
  237. }
  238. /* It's OK not to connect the refclk */
  239. }
  240. static const VMStateDescription vmstate_systick = {
  241. .name = "armv7m_systick",
  242. .version_id = 3,
  243. .minimum_version_id = 3,
  244. .fields = (const VMStateField[]) {
  245. VMSTATE_CLOCK(refclk, SysTickState),
  246. VMSTATE_CLOCK(cpuclk, SysTickState),
  247. VMSTATE_UINT32(control, SysTickState),
  248. VMSTATE_INT64(tick, SysTickState),
  249. VMSTATE_PTIMER(ptimer, SysTickState),
  250. VMSTATE_END_OF_LIST()
  251. }
  252. };
  253. static void systick_class_init(ObjectClass *klass, void *data)
  254. {
  255. DeviceClass *dc = DEVICE_CLASS(klass);
  256. dc->vmsd = &vmstate_systick;
  257. device_class_set_legacy_reset(dc, systick_reset);
  258. dc->realize = systick_realize;
  259. }
  260. static const TypeInfo armv7m_systick_info = {
  261. .name = TYPE_SYSTICK,
  262. .parent = TYPE_SYS_BUS_DEVICE,
  263. .instance_init = systick_instance_init,
  264. .instance_size = sizeof(SysTickState),
  265. .class_init = systick_class_init,
  266. };
  267. static void armv7m_systick_register_types(void)
  268. {
  269. type_register_static(&armv7m_systick_info);
  270. }
  271. type_init(armv7m_systick_register_types)