stm32f2xx_timer.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * STM32F2XX Timer
  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 "hw/irq.h"
  26. #include "hw/qdev-properties.h"
  27. #include "hw/timer/stm32f2xx_timer.h"
  28. #include "migration/vmstate.h"
  29. #include "qemu/log.h"
  30. #include "qemu/module.h"
  31. #ifndef STM_TIMER_ERR_DEBUG
  32. #define STM_TIMER_ERR_DEBUG 0
  33. #endif
  34. #define DB_PRINT_L(lvl, fmt, args...) do { \
  35. if (STM_TIMER_ERR_DEBUG >= lvl) { \
  36. qemu_log("%s: " fmt, __func__, ## args); \
  37. } \
  38. } while (0)
  39. #define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
  40. static void stm32f2xx_timer_set_alarm(STM32F2XXTimerState *s, int64_t now);
  41. static void stm32f2xx_timer_interrupt(void *opaque)
  42. {
  43. STM32F2XXTimerState *s = opaque;
  44. DB_PRINT("Interrupt\n");
  45. if (s->tim_dier & TIM_DIER_UIE && s->tim_cr1 & TIM_CR1_CEN) {
  46. s->tim_sr |= 1;
  47. qemu_irq_pulse(s->irq);
  48. stm32f2xx_timer_set_alarm(s, s->hit_time);
  49. }
  50. if (s->tim_ccmr1 & (TIM_CCMR1_OC2M2 | TIM_CCMR1_OC2M1) &&
  51. !(s->tim_ccmr1 & TIM_CCMR1_OC2M0) &&
  52. s->tim_ccmr1 & TIM_CCMR1_OC2PE &&
  53. s->tim_ccer & TIM_CCER_CC2E) {
  54. /* PWM 2 - Mode 1 */
  55. DB_PRINT("PWM2 Duty Cycle: %d%%\n",
  56. s->tim_ccr2 / (100 * (s->tim_psc + 1)));
  57. }
  58. }
  59. static inline int64_t stm32f2xx_ns_to_ticks(STM32F2XXTimerState *s, int64_t t)
  60. {
  61. return muldiv64(t, s->freq_hz, 1000000000ULL) / (s->tim_psc + 1);
  62. }
  63. static void stm32f2xx_timer_set_alarm(STM32F2XXTimerState *s, int64_t now)
  64. {
  65. uint64_t ticks;
  66. int64_t now_ticks;
  67. if (s->tim_arr == 0) {
  68. return;
  69. }
  70. DB_PRINT("Alarm set at: 0x%x\n", s->tim_cr1);
  71. now_ticks = stm32f2xx_ns_to_ticks(s, now);
  72. ticks = s->tim_arr - (now_ticks - s->tick_offset);
  73. DB_PRINT("Alarm set in %d ticks\n", (int) ticks);
  74. s->hit_time = muldiv64((ticks + (uint64_t) now_ticks) * (s->tim_psc + 1),
  75. 1000000000ULL, s->freq_hz);
  76. timer_mod(s->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->hit_time);
  77. DB_PRINT("Wait Time: %" PRId64 " ticks\n", s->hit_time);
  78. }
  79. static void stm32f2xx_timer_reset(DeviceState *dev)
  80. {
  81. STM32F2XXTimerState *s = STM32F2XXTIMER(dev);
  82. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  83. s->tim_cr1 = 0;
  84. s->tim_cr2 = 0;
  85. s->tim_smcr = 0;
  86. s->tim_dier = 0;
  87. s->tim_sr = 0;
  88. s->tim_egr = 0;
  89. s->tim_ccmr1 = 0;
  90. s->tim_ccmr2 = 0;
  91. s->tim_ccer = 0;
  92. s->tim_psc = 0;
  93. s->tim_arr = 0;
  94. s->tim_ccr1 = 0;
  95. s->tim_ccr2 = 0;
  96. s->tim_ccr3 = 0;
  97. s->tim_ccr4 = 0;
  98. s->tim_dcr = 0;
  99. s->tim_dmar = 0;
  100. s->tim_or = 0;
  101. s->tick_offset = stm32f2xx_ns_to_ticks(s, now);
  102. }
  103. static uint64_t stm32f2xx_timer_read(void *opaque, hwaddr offset,
  104. unsigned size)
  105. {
  106. STM32F2XXTimerState *s = opaque;
  107. DB_PRINT("Read 0x%"HWADDR_PRIx"\n", offset);
  108. switch (offset) {
  109. case TIM_CR1:
  110. return s->tim_cr1;
  111. case TIM_CR2:
  112. return s->tim_cr2;
  113. case TIM_SMCR:
  114. return s->tim_smcr;
  115. case TIM_DIER:
  116. return s->tim_dier;
  117. case TIM_SR:
  118. return s->tim_sr;
  119. case TIM_EGR:
  120. return s->tim_egr;
  121. case TIM_CCMR1:
  122. return s->tim_ccmr1;
  123. case TIM_CCMR2:
  124. return s->tim_ccmr2;
  125. case TIM_CCER:
  126. return s->tim_ccer;
  127. case TIM_CNT:
  128. return stm32f2xx_ns_to_ticks(s, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) -
  129. s->tick_offset;
  130. case TIM_PSC:
  131. return s->tim_psc;
  132. case TIM_ARR:
  133. return s->tim_arr;
  134. case TIM_CCR1:
  135. return s->tim_ccr1;
  136. case TIM_CCR2:
  137. return s->tim_ccr2;
  138. case TIM_CCR3:
  139. return s->tim_ccr3;
  140. case TIM_CCR4:
  141. return s->tim_ccr4;
  142. case TIM_DCR:
  143. return s->tim_dcr;
  144. case TIM_DMAR:
  145. return s->tim_dmar;
  146. case TIM_OR:
  147. return s->tim_or;
  148. default:
  149. qemu_log_mask(LOG_GUEST_ERROR,
  150. "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, offset);
  151. }
  152. return 0;
  153. }
  154. static void stm32f2xx_timer_write(void *opaque, hwaddr offset,
  155. uint64_t val64, unsigned size)
  156. {
  157. STM32F2XXTimerState *s = opaque;
  158. uint32_t value = val64;
  159. int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  160. uint32_t timer_val = 0;
  161. DB_PRINT("Write 0x%x, 0x%"HWADDR_PRIx"\n", value, offset);
  162. switch (offset) {
  163. case TIM_CR1:
  164. s->tim_cr1 = value;
  165. return;
  166. case TIM_CR2:
  167. s->tim_cr2 = value;
  168. return;
  169. case TIM_SMCR:
  170. s->tim_smcr = value;
  171. return;
  172. case TIM_DIER:
  173. s->tim_dier = value;
  174. return;
  175. case TIM_SR:
  176. /* This is set by hardware and cleared by software */
  177. s->tim_sr &= value;
  178. return;
  179. case TIM_EGR:
  180. s->tim_egr = value;
  181. if (s->tim_egr & TIM_EGR_UG) {
  182. timer_val = 0;
  183. break;
  184. }
  185. return;
  186. case TIM_CCMR1:
  187. s->tim_ccmr1 = value;
  188. return;
  189. case TIM_CCMR2:
  190. s->tim_ccmr2 = value;
  191. return;
  192. case TIM_CCER:
  193. s->tim_ccer = value;
  194. return;
  195. case TIM_PSC:
  196. timer_val = stm32f2xx_ns_to_ticks(s, now) - s->tick_offset;
  197. s->tim_psc = value & 0xFFFF;
  198. value = timer_val;
  199. break;
  200. case TIM_CNT:
  201. timer_val = value;
  202. break;
  203. case TIM_ARR:
  204. s->tim_arr = value;
  205. stm32f2xx_timer_set_alarm(s, now);
  206. return;
  207. case TIM_CCR1:
  208. s->tim_ccr1 = value;
  209. return;
  210. case TIM_CCR2:
  211. s->tim_ccr2 = value;
  212. return;
  213. case TIM_CCR3:
  214. s->tim_ccr3 = value;
  215. return;
  216. case TIM_CCR4:
  217. s->tim_ccr4 = value;
  218. return;
  219. case TIM_DCR:
  220. s->tim_dcr = value;
  221. return;
  222. case TIM_DMAR:
  223. s->tim_dmar = value;
  224. return;
  225. case TIM_OR:
  226. s->tim_or = value;
  227. return;
  228. default:
  229. qemu_log_mask(LOG_GUEST_ERROR,
  230. "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, offset);
  231. return;
  232. }
  233. /* This means that a register write has affected the timer in a way that
  234. * requires a refresh of both tick_offset and the alarm.
  235. */
  236. s->tick_offset = stm32f2xx_ns_to_ticks(s, now) - timer_val;
  237. stm32f2xx_timer_set_alarm(s, now);
  238. }
  239. static const MemoryRegionOps stm32f2xx_timer_ops = {
  240. .read = stm32f2xx_timer_read,
  241. .write = stm32f2xx_timer_write,
  242. .endianness = DEVICE_NATIVE_ENDIAN,
  243. };
  244. static const VMStateDescription vmstate_stm32f2xx_timer = {
  245. .name = TYPE_STM32F2XX_TIMER,
  246. .version_id = 1,
  247. .minimum_version_id = 1,
  248. .fields = (VMStateField[]) {
  249. VMSTATE_INT64(tick_offset, STM32F2XXTimerState),
  250. VMSTATE_UINT32(tim_cr1, STM32F2XXTimerState),
  251. VMSTATE_UINT32(tim_cr2, STM32F2XXTimerState),
  252. VMSTATE_UINT32(tim_smcr, STM32F2XXTimerState),
  253. VMSTATE_UINT32(tim_dier, STM32F2XXTimerState),
  254. VMSTATE_UINT32(tim_sr, STM32F2XXTimerState),
  255. VMSTATE_UINT32(tim_egr, STM32F2XXTimerState),
  256. VMSTATE_UINT32(tim_ccmr1, STM32F2XXTimerState),
  257. VMSTATE_UINT32(tim_ccmr2, STM32F2XXTimerState),
  258. VMSTATE_UINT32(tim_ccer, STM32F2XXTimerState),
  259. VMSTATE_UINT32(tim_psc, STM32F2XXTimerState),
  260. VMSTATE_UINT32(tim_arr, STM32F2XXTimerState),
  261. VMSTATE_UINT32(tim_ccr1, STM32F2XXTimerState),
  262. VMSTATE_UINT32(tim_ccr2, STM32F2XXTimerState),
  263. VMSTATE_UINT32(tim_ccr3, STM32F2XXTimerState),
  264. VMSTATE_UINT32(tim_ccr4, STM32F2XXTimerState),
  265. VMSTATE_UINT32(tim_dcr, STM32F2XXTimerState),
  266. VMSTATE_UINT32(tim_dmar, STM32F2XXTimerState),
  267. VMSTATE_UINT32(tim_or, STM32F2XXTimerState),
  268. VMSTATE_END_OF_LIST()
  269. }
  270. };
  271. static Property stm32f2xx_timer_properties[] = {
  272. DEFINE_PROP_UINT64("clock-frequency", struct STM32F2XXTimerState,
  273. freq_hz, 1000000000),
  274. DEFINE_PROP_END_OF_LIST(),
  275. };
  276. static void stm32f2xx_timer_init(Object *obj)
  277. {
  278. STM32F2XXTimerState *s = STM32F2XXTIMER(obj);
  279. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
  280. memory_region_init_io(&s->iomem, obj, &stm32f2xx_timer_ops, s,
  281. "stm32f2xx_timer", 0x400);
  282. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
  283. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, stm32f2xx_timer_interrupt, s);
  284. }
  285. static void stm32f2xx_timer_class_init(ObjectClass *klass, void *data)
  286. {
  287. DeviceClass *dc = DEVICE_CLASS(klass);
  288. dc->reset = stm32f2xx_timer_reset;
  289. dc->props = stm32f2xx_timer_properties;
  290. dc->vmsd = &vmstate_stm32f2xx_timer;
  291. }
  292. static const TypeInfo stm32f2xx_timer_info = {
  293. .name = TYPE_STM32F2XX_TIMER,
  294. .parent = TYPE_SYS_BUS_DEVICE,
  295. .instance_size = sizeof(STM32F2XXTimerState),
  296. .instance_init = stm32f2xx_timer_init,
  297. .class_init = stm32f2xx_timer_class_init,
  298. };
  299. static void stm32f2xx_timer_register_types(void)
  300. {
  301. type_register_static(&stm32f2xx_timer_info);
  302. }
  303. type_init(stm32f2xx_timer_register_types)