2
0

mss-timer.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Block model of System timer present in
  3. * Microsemi's SmartFusion2 and SmartFusion SoCs.
  4. *
  5. * Copyright (c) 2017 Subbaraya Sundeep <sundeep.lkml@gmail.com>.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/module.h"
  27. #include "qemu/log.h"
  28. #include "hw/irq.h"
  29. #include "hw/qdev-properties.h"
  30. #include "hw/timer/mss-timer.h"
  31. #include "migration/vmstate.h"
  32. #ifndef MSS_TIMER_ERR_DEBUG
  33. #define MSS_TIMER_ERR_DEBUG 0
  34. #endif
  35. #define DB_PRINT_L(lvl, fmt, args...) do { \
  36. if (MSS_TIMER_ERR_DEBUG >= lvl) { \
  37. qemu_log("%s: " fmt "\n", __func__, ## args); \
  38. } \
  39. } while (0)
  40. #define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
  41. #define R_TIM_VAL 0
  42. #define R_TIM_LOADVAL 1
  43. #define R_TIM_BGLOADVAL 2
  44. #define R_TIM_CTRL 3
  45. #define R_TIM_RIS 4
  46. #define R_TIM_MIS 5
  47. #define TIMER_CTRL_ENBL (1 << 0)
  48. #define TIMER_CTRL_ONESHOT (1 << 1)
  49. #define TIMER_CTRL_INTR (1 << 2)
  50. #define TIMER_RIS_ACK (1 << 0)
  51. #define TIMER_RST_CLR (1 << 6)
  52. #define TIMER_MODE (1 << 0)
  53. static void timer_update_irq(struct Msf2Timer *st)
  54. {
  55. bool isr, ier;
  56. isr = !!(st->regs[R_TIM_RIS] & TIMER_RIS_ACK);
  57. ier = !!(st->regs[R_TIM_CTRL] & TIMER_CTRL_INTR);
  58. qemu_set_irq(st->irq, (ier && isr));
  59. }
  60. /* Must be called from within a ptimer_transaction_begin/commit block */
  61. static void timer_update(struct Msf2Timer *st)
  62. {
  63. uint64_t count;
  64. if (!(st->regs[R_TIM_CTRL] & TIMER_CTRL_ENBL)) {
  65. ptimer_stop(st->ptimer);
  66. return;
  67. }
  68. count = st->regs[R_TIM_LOADVAL];
  69. ptimer_set_limit(st->ptimer, count, 1);
  70. ptimer_run(st->ptimer, 1);
  71. }
  72. static uint64_t
  73. timer_read(void *opaque, hwaddr offset, unsigned int size)
  74. {
  75. MSSTimerState *t = opaque;
  76. hwaddr addr;
  77. struct Msf2Timer *st;
  78. uint32_t ret = 0;
  79. int timer = 0;
  80. int isr;
  81. int ier;
  82. addr = offset >> 2;
  83. /*
  84. * Two independent timers has same base address.
  85. * Based on address passed figure out which timer is being used.
  86. */
  87. if ((addr >= R_TIM1_MAX) && (addr < NUM_TIMERS * R_TIM1_MAX)) {
  88. timer = 1;
  89. addr -= R_TIM1_MAX;
  90. }
  91. st = &t->timers[timer];
  92. switch (addr) {
  93. case R_TIM_VAL:
  94. ret = ptimer_get_count(st->ptimer);
  95. break;
  96. case R_TIM_MIS:
  97. isr = !!(st->regs[R_TIM_RIS] & TIMER_RIS_ACK);
  98. ier = !!(st->regs[R_TIM_CTRL] & TIMER_CTRL_INTR);
  99. ret = ier & isr;
  100. break;
  101. default:
  102. if (addr < R_TIM1_MAX) {
  103. ret = st->regs[addr];
  104. } else {
  105. qemu_log_mask(LOG_GUEST_ERROR,
  106. TYPE_MSS_TIMER": 64-bit mode not supported\n");
  107. return ret;
  108. }
  109. break;
  110. }
  111. DB_PRINT("timer=%d 0x%" HWADDR_PRIx "=0x%" PRIx32, timer, offset,
  112. ret);
  113. return ret;
  114. }
  115. static void
  116. timer_write(void *opaque, hwaddr offset,
  117. uint64_t val64, unsigned int size)
  118. {
  119. MSSTimerState *t = opaque;
  120. hwaddr addr;
  121. struct Msf2Timer *st;
  122. int timer = 0;
  123. uint32_t value = val64;
  124. addr = offset >> 2;
  125. /*
  126. * Two independent timers has same base address.
  127. * Based on addr passed figure out which timer is being used.
  128. */
  129. if ((addr >= R_TIM1_MAX) && (addr < NUM_TIMERS * R_TIM1_MAX)) {
  130. timer = 1;
  131. addr -= R_TIM1_MAX;
  132. }
  133. st = &t->timers[timer];
  134. DB_PRINT("addr=0x%" HWADDR_PRIx " val=0x%" PRIx32 " (timer=%d)", offset,
  135. value, timer);
  136. switch (addr) {
  137. case R_TIM_CTRL:
  138. st->regs[R_TIM_CTRL] = value;
  139. ptimer_transaction_begin(st->ptimer);
  140. timer_update(st);
  141. ptimer_transaction_commit(st->ptimer);
  142. break;
  143. case R_TIM_RIS:
  144. if (value & TIMER_RIS_ACK) {
  145. st->regs[R_TIM_RIS] &= ~TIMER_RIS_ACK;
  146. }
  147. break;
  148. case R_TIM_LOADVAL:
  149. st->regs[R_TIM_LOADVAL] = value;
  150. if (st->regs[R_TIM_CTRL] & TIMER_CTRL_ENBL) {
  151. ptimer_transaction_begin(st->ptimer);
  152. timer_update(st);
  153. ptimer_transaction_commit(st->ptimer);
  154. }
  155. break;
  156. case R_TIM_BGLOADVAL:
  157. st->regs[R_TIM_BGLOADVAL] = value;
  158. st->regs[R_TIM_LOADVAL] = value;
  159. break;
  160. case R_TIM_VAL:
  161. case R_TIM_MIS:
  162. break;
  163. default:
  164. if (addr < R_TIM1_MAX) {
  165. st->regs[addr] = value;
  166. } else {
  167. qemu_log_mask(LOG_GUEST_ERROR,
  168. TYPE_MSS_TIMER": 64-bit mode not supported\n");
  169. return;
  170. }
  171. break;
  172. }
  173. timer_update_irq(st);
  174. }
  175. static const MemoryRegionOps timer_ops = {
  176. .read = timer_read,
  177. .write = timer_write,
  178. .endianness = DEVICE_NATIVE_ENDIAN,
  179. .valid = {
  180. .min_access_size = 1,
  181. .max_access_size = 4
  182. }
  183. };
  184. static void timer_hit(void *opaque)
  185. {
  186. struct Msf2Timer *st = opaque;
  187. st->regs[R_TIM_RIS] |= TIMER_RIS_ACK;
  188. if (!(st->regs[R_TIM_CTRL] & TIMER_CTRL_ONESHOT)) {
  189. timer_update(st);
  190. }
  191. timer_update_irq(st);
  192. }
  193. static void mss_timer_init(Object *obj)
  194. {
  195. MSSTimerState *t = MSS_TIMER(obj);
  196. int i;
  197. /* Init all the ptimers. */
  198. for (i = 0; i < NUM_TIMERS; i++) {
  199. struct Msf2Timer *st = &t->timers[i];
  200. st->ptimer = ptimer_init(timer_hit, st, PTIMER_POLICY_LEGACY);
  201. ptimer_transaction_begin(st->ptimer);
  202. ptimer_set_freq(st->ptimer, t->freq_hz);
  203. ptimer_transaction_commit(st->ptimer);
  204. sysbus_init_irq(SYS_BUS_DEVICE(obj), &st->irq);
  205. }
  206. memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t, TYPE_MSS_TIMER,
  207. NUM_TIMERS * R_TIM1_MAX * 4);
  208. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &t->mmio);
  209. }
  210. static void mss_timer_finalize(Object *obj)
  211. {
  212. MSSTimerState *t = MSS_TIMER(obj);
  213. int i;
  214. for (i = 0; i < NUM_TIMERS; i++) {
  215. struct Msf2Timer *st = &t->timers[i];
  216. ptimer_free(st->ptimer);
  217. }
  218. }
  219. static const VMStateDescription vmstate_timers = {
  220. .name = "mss-timer-block",
  221. .version_id = 1,
  222. .minimum_version_id = 1,
  223. .fields = (const VMStateField[]) {
  224. VMSTATE_PTIMER(ptimer, struct Msf2Timer),
  225. VMSTATE_UINT32_ARRAY(regs, struct Msf2Timer, R_TIM1_MAX),
  226. VMSTATE_END_OF_LIST()
  227. }
  228. };
  229. static const VMStateDescription vmstate_mss_timer = {
  230. .name = TYPE_MSS_TIMER,
  231. .version_id = 1,
  232. .minimum_version_id = 1,
  233. .fields = (const VMStateField[]) {
  234. VMSTATE_UINT32(freq_hz, MSSTimerState),
  235. VMSTATE_STRUCT_ARRAY(timers, MSSTimerState, NUM_TIMERS, 0,
  236. vmstate_timers, struct Msf2Timer),
  237. VMSTATE_END_OF_LIST()
  238. }
  239. };
  240. static const Property mss_timer_properties[] = {
  241. /* Libero GUI shows 100Mhz as default for clocks */
  242. DEFINE_PROP_UINT32("clock-frequency", MSSTimerState, freq_hz,
  243. 100 * 1000000),
  244. };
  245. static void mss_timer_class_init(ObjectClass *klass, void *data)
  246. {
  247. DeviceClass *dc = DEVICE_CLASS(klass);
  248. device_class_set_props(dc, mss_timer_properties);
  249. dc->vmsd = &vmstate_mss_timer;
  250. }
  251. static const TypeInfo mss_timer_info = {
  252. .name = TYPE_MSS_TIMER,
  253. .parent = TYPE_SYS_BUS_DEVICE,
  254. .instance_size = sizeof(MSSTimerState),
  255. .instance_init = mss_timer_init,
  256. .instance_finalize = mss_timer_finalize,
  257. .class_init = mss_timer_class_init,
  258. };
  259. static void mss_timer_register_types(void)
  260. {
  261. type_register_static(&mss_timer_info);
  262. }
  263. type_init(mss_timer_register_types)