digic-timer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * QEMU model of the Canon DIGIC timer block.
  3. *
  4. * Copyright (C) 2013 Antony Pavlov <antonynpavlov@gmail.com>
  5. *
  6. * This model is based on reverse engineering efforts
  7. * made by CHDK (http://chdk.wikia.com) and
  8. * Magic Lantern (http://www.magiclantern.fm) projects
  9. * contributors.
  10. *
  11. * See "Timer/Clock Module" docs here:
  12. * http://magiclantern.wikia.com/wiki/Register_Map
  13. *
  14. * The QEMU model of the OSTimer in PKUnity SoC by Guan Xuetao
  15. * is used as a template.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. */
  28. #include "qemu/osdep.h"
  29. #include "hw/sysbus.h"
  30. #include "hw/ptimer.h"
  31. #include "qemu/module.h"
  32. #include "qemu/log.h"
  33. #include "hw/timer/digic-timer.h"
  34. #include "migration/vmstate.h"
  35. static const VMStateDescription vmstate_digic_timer = {
  36. .name = "digic.timer",
  37. .version_id = 1,
  38. .minimum_version_id = 1,
  39. .fields = (VMStateField[]) {
  40. VMSTATE_PTIMER(ptimer, DigicTimerState),
  41. VMSTATE_UINT32(control, DigicTimerState),
  42. VMSTATE_UINT32(relvalue, DigicTimerState),
  43. VMSTATE_END_OF_LIST()
  44. }
  45. };
  46. static void digic_timer_reset(DeviceState *dev)
  47. {
  48. DigicTimerState *s = DIGIC_TIMER(dev);
  49. ptimer_transaction_begin(s->ptimer);
  50. ptimer_stop(s->ptimer);
  51. ptimer_transaction_commit(s->ptimer);
  52. s->control = 0;
  53. s->relvalue = 0;
  54. }
  55. static uint64_t digic_timer_read(void *opaque, hwaddr offset, unsigned size)
  56. {
  57. DigicTimerState *s = opaque;
  58. uint64_t ret = 0;
  59. switch (offset) {
  60. case DIGIC_TIMER_CONTROL:
  61. ret = s->control;
  62. break;
  63. case DIGIC_TIMER_RELVALUE:
  64. ret = s->relvalue;
  65. break;
  66. case DIGIC_TIMER_VALUE:
  67. ret = ptimer_get_count(s->ptimer) & 0xffff;
  68. break;
  69. default:
  70. qemu_log_mask(LOG_UNIMP,
  71. "digic-timer: read access to unknown register 0x"
  72. TARGET_FMT_plx "\n", offset);
  73. }
  74. return ret;
  75. }
  76. static void digic_timer_write(void *opaque, hwaddr offset,
  77. uint64_t value, unsigned size)
  78. {
  79. DigicTimerState *s = opaque;
  80. switch (offset) {
  81. case DIGIC_TIMER_CONTROL:
  82. if (value & DIGIC_TIMER_CONTROL_RST) {
  83. digic_timer_reset((DeviceState *)s);
  84. break;
  85. }
  86. ptimer_transaction_begin(s->ptimer);
  87. if (value & DIGIC_TIMER_CONTROL_EN) {
  88. ptimer_run(s->ptimer, 0);
  89. }
  90. s->control = (uint32_t)value;
  91. ptimer_transaction_commit(s->ptimer);
  92. break;
  93. case DIGIC_TIMER_RELVALUE:
  94. s->relvalue = extract32(value, 0, 16);
  95. ptimer_transaction_begin(s->ptimer);
  96. ptimer_set_limit(s->ptimer, s->relvalue, 1);
  97. ptimer_transaction_commit(s->ptimer);
  98. break;
  99. case DIGIC_TIMER_VALUE:
  100. break;
  101. default:
  102. qemu_log_mask(LOG_UNIMP,
  103. "digic-timer: read access to unknown register 0x"
  104. TARGET_FMT_plx "\n", offset);
  105. }
  106. }
  107. static const MemoryRegionOps digic_timer_ops = {
  108. .read = digic_timer_read,
  109. .write = digic_timer_write,
  110. .impl = {
  111. .min_access_size = 4,
  112. .max_access_size = 4,
  113. },
  114. .endianness = DEVICE_NATIVE_ENDIAN,
  115. };
  116. static void digic_timer_tick(void *opaque)
  117. {
  118. /* Nothing to do on timer rollover */
  119. }
  120. static void digic_timer_init(Object *obj)
  121. {
  122. DigicTimerState *s = DIGIC_TIMER(obj);
  123. s->ptimer = ptimer_init(digic_timer_tick, NULL, PTIMER_POLICY_LEGACY);
  124. /*
  125. * FIXME: there is no documentation on Digic timer
  126. * frequency setup so let it always run at 1 MHz
  127. */
  128. ptimer_transaction_begin(s->ptimer);
  129. ptimer_set_freq(s->ptimer, 1 * 1000 * 1000);
  130. ptimer_transaction_commit(s->ptimer);
  131. memory_region_init_io(&s->iomem, OBJECT(s), &digic_timer_ops, s,
  132. TYPE_DIGIC_TIMER, 0x100);
  133. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
  134. }
  135. static void digic_timer_finalize(Object *obj)
  136. {
  137. DigicTimerState *s = DIGIC_TIMER(obj);
  138. ptimer_free(s->ptimer);
  139. }
  140. static void digic_timer_class_init(ObjectClass *klass, void *class_data)
  141. {
  142. DeviceClass *dc = DEVICE_CLASS(klass);
  143. dc->reset = digic_timer_reset;
  144. dc->vmsd = &vmstate_digic_timer;
  145. }
  146. static const TypeInfo digic_timer_info = {
  147. .name = TYPE_DIGIC_TIMER,
  148. .parent = TYPE_SYS_BUS_DEVICE,
  149. .instance_size = sizeof(DigicTimerState),
  150. .instance_init = digic_timer_init,
  151. .instance_finalize = digic_timer_finalize,
  152. .class_init = digic_timer_class_init,
  153. };
  154. static void digic_timer_register_type(void)
  155. {
  156. type_register_static(&digic_timer_info);
  157. }
  158. type_init(digic_timer_register_type)