lm32_timer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * QEMU model of the LatticeMico32 timer block.
  3. *
  4. * Copyright (c) 2010 Michael Walle <michael@walle.cc>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. *
  20. * Specification available at:
  21. * http://www.latticesemi.com/documents/mico32timer.pdf
  22. */
  23. #include "qemu/osdep.h"
  24. #include "hw/hw.h"
  25. #include "hw/sysbus.h"
  26. #include "trace.h"
  27. #include "qemu/timer.h"
  28. #include "hw/ptimer.h"
  29. #include "qemu/error-report.h"
  30. #include "qemu/main-loop.h"
  31. #define DEFAULT_FREQUENCY (50*1000000)
  32. enum {
  33. R_SR = 0,
  34. R_CR,
  35. R_PERIOD,
  36. R_SNAPSHOT,
  37. R_MAX
  38. };
  39. enum {
  40. SR_TO = (1 << 0),
  41. SR_RUN = (1 << 1),
  42. };
  43. enum {
  44. CR_ITO = (1 << 0),
  45. CR_CONT = (1 << 1),
  46. CR_START = (1 << 2),
  47. CR_STOP = (1 << 3),
  48. };
  49. #define TYPE_LM32_TIMER "lm32-timer"
  50. #define LM32_TIMER(obj) OBJECT_CHECK(LM32TimerState, (obj), TYPE_LM32_TIMER)
  51. struct LM32TimerState {
  52. SysBusDevice parent_obj;
  53. MemoryRegion iomem;
  54. QEMUBH *bh;
  55. ptimer_state *ptimer;
  56. qemu_irq irq;
  57. uint32_t freq_hz;
  58. uint32_t regs[R_MAX];
  59. };
  60. typedef struct LM32TimerState LM32TimerState;
  61. static void timer_update_irq(LM32TimerState *s)
  62. {
  63. int state = (s->regs[R_SR] & SR_TO) && (s->regs[R_CR] & CR_ITO);
  64. trace_lm32_timer_irq_state(state);
  65. qemu_set_irq(s->irq, state);
  66. }
  67. static uint64_t timer_read(void *opaque, hwaddr addr, unsigned size)
  68. {
  69. LM32TimerState *s = opaque;
  70. uint32_t r = 0;
  71. addr >>= 2;
  72. switch (addr) {
  73. case R_SR:
  74. case R_CR:
  75. case R_PERIOD:
  76. r = s->regs[addr];
  77. break;
  78. case R_SNAPSHOT:
  79. r = (uint32_t)ptimer_get_count(s->ptimer);
  80. break;
  81. default:
  82. error_report("lm32_timer: read access to unknown register 0x"
  83. TARGET_FMT_plx, addr << 2);
  84. break;
  85. }
  86. trace_lm32_timer_memory_read(addr << 2, r);
  87. return r;
  88. }
  89. static void timer_write(void *opaque, hwaddr addr,
  90. uint64_t value, unsigned size)
  91. {
  92. LM32TimerState *s = opaque;
  93. trace_lm32_timer_memory_write(addr, value);
  94. addr >>= 2;
  95. switch (addr) {
  96. case R_SR:
  97. s->regs[R_SR] &= ~SR_TO;
  98. break;
  99. case R_CR:
  100. s->regs[R_CR] = value;
  101. if (s->regs[R_CR] & CR_START) {
  102. ptimer_run(s->ptimer, 1);
  103. }
  104. if (s->regs[R_CR] & CR_STOP) {
  105. ptimer_stop(s->ptimer);
  106. }
  107. break;
  108. case R_PERIOD:
  109. s->regs[R_PERIOD] = value;
  110. ptimer_set_count(s->ptimer, value);
  111. break;
  112. case R_SNAPSHOT:
  113. error_report("lm32_timer: write access to read only register 0x"
  114. TARGET_FMT_plx, addr << 2);
  115. break;
  116. default:
  117. error_report("lm32_timer: write access to unknown register 0x"
  118. TARGET_FMT_plx, addr << 2);
  119. break;
  120. }
  121. timer_update_irq(s);
  122. }
  123. static const MemoryRegionOps timer_ops = {
  124. .read = timer_read,
  125. .write = timer_write,
  126. .endianness = DEVICE_NATIVE_ENDIAN,
  127. .valid = {
  128. .min_access_size = 4,
  129. .max_access_size = 4,
  130. },
  131. };
  132. static void timer_hit(void *opaque)
  133. {
  134. LM32TimerState *s = opaque;
  135. trace_lm32_timer_hit();
  136. s->regs[R_SR] |= SR_TO;
  137. if (s->regs[R_CR] & CR_CONT) {
  138. ptimer_set_count(s->ptimer, s->regs[R_PERIOD]);
  139. ptimer_run(s->ptimer, 1);
  140. }
  141. timer_update_irq(s);
  142. }
  143. static void timer_reset(DeviceState *d)
  144. {
  145. LM32TimerState *s = LM32_TIMER(d);
  146. int i;
  147. for (i = 0; i < R_MAX; i++) {
  148. s->regs[i] = 0;
  149. }
  150. ptimer_stop(s->ptimer);
  151. }
  152. static void lm32_timer_init(Object *obj)
  153. {
  154. LM32TimerState *s = LM32_TIMER(obj);
  155. SysBusDevice *dev = SYS_BUS_DEVICE(obj);
  156. sysbus_init_irq(dev, &s->irq);
  157. s->bh = qemu_bh_new(timer_hit, s);
  158. s->ptimer = ptimer_init(s->bh, PTIMER_POLICY_DEFAULT);
  159. memory_region_init_io(&s->iomem, obj, &timer_ops, s,
  160. "timer", R_MAX * 4);
  161. sysbus_init_mmio(dev, &s->iomem);
  162. }
  163. static void lm32_timer_realize(DeviceState *dev, Error **errp)
  164. {
  165. LM32TimerState *s = LM32_TIMER(dev);
  166. ptimer_set_freq(s->ptimer, s->freq_hz);
  167. }
  168. static const VMStateDescription vmstate_lm32_timer = {
  169. .name = "lm32-timer",
  170. .version_id = 1,
  171. .minimum_version_id = 1,
  172. .fields = (VMStateField[]) {
  173. VMSTATE_PTIMER(ptimer, LM32TimerState),
  174. VMSTATE_UINT32(freq_hz, LM32TimerState),
  175. VMSTATE_UINT32_ARRAY(regs, LM32TimerState, R_MAX),
  176. VMSTATE_END_OF_LIST()
  177. }
  178. };
  179. static Property lm32_timer_properties[] = {
  180. DEFINE_PROP_UINT32("frequency", LM32TimerState, freq_hz, DEFAULT_FREQUENCY),
  181. DEFINE_PROP_END_OF_LIST(),
  182. };
  183. static void lm32_timer_class_init(ObjectClass *klass, void *data)
  184. {
  185. DeviceClass *dc = DEVICE_CLASS(klass);
  186. dc->realize = lm32_timer_realize;
  187. dc->reset = timer_reset;
  188. dc->vmsd = &vmstate_lm32_timer;
  189. dc->props = lm32_timer_properties;
  190. }
  191. static const TypeInfo lm32_timer_info = {
  192. .name = TYPE_LM32_TIMER,
  193. .parent = TYPE_SYS_BUS_DEVICE,
  194. .instance_size = sizeof(LM32TimerState),
  195. .instance_init = lm32_timer_init,
  196. .class_init = lm32_timer_class_init,
  197. };
  198. static void lm32_timer_register_types(void)
  199. {
  200. type_register_static(&lm32_timer_info);
  201. }
  202. type_init(lm32_timer_register_types)