stm32f2xx_timer.c 9.7 KB

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