2
0

lm32_timer.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 "hw/hw.h"
  24. #include "hw/sysbus.h"
  25. #include "trace.h"
  26. #include "qemu/timer.h"
  27. #include "hw/ptimer.h"
  28. #include "qemu/error-report.h"
  29. #include "qemu/main-loop.h"
  30. #define DEFAULT_FREQUENCY (50*1000000)
  31. enum {
  32. R_SR = 0,
  33. R_CR,
  34. R_PERIOD,
  35. R_SNAPSHOT,
  36. R_MAX
  37. };
  38. enum {
  39. SR_TO = (1 << 0),
  40. SR_RUN = (1 << 1),
  41. };
  42. enum {
  43. CR_ITO = (1 << 0),
  44. CR_CONT = (1 << 1),
  45. CR_START = (1 << 2),
  46. CR_STOP = (1 << 3),
  47. };
  48. #define TYPE_LM32_TIMER "lm32-timer"
  49. #define LM32_TIMER(obj) OBJECT_CHECK(LM32TimerState, (obj), TYPE_LM32_TIMER)
  50. struct LM32TimerState {
  51. SysBusDevice parent_obj;
  52. MemoryRegion iomem;
  53. QEMUBH *bh;
  54. ptimer_state *ptimer;
  55. qemu_irq irq;
  56. uint32_t freq_hz;
  57. uint32_t regs[R_MAX];
  58. };
  59. typedef struct LM32TimerState LM32TimerState;
  60. static void timer_update_irq(LM32TimerState *s)
  61. {
  62. int state = (s->regs[R_SR] & SR_TO) && (s->regs[R_CR] & CR_ITO);
  63. trace_lm32_timer_irq_state(state);
  64. qemu_set_irq(s->irq, state);
  65. }
  66. static uint64_t timer_read(void *opaque, hwaddr addr, unsigned size)
  67. {
  68. LM32TimerState *s = opaque;
  69. uint32_t r = 0;
  70. addr >>= 2;
  71. switch (addr) {
  72. case R_SR:
  73. case R_CR:
  74. case R_PERIOD:
  75. r = s->regs[addr];
  76. break;
  77. case R_SNAPSHOT:
  78. r = (uint32_t)ptimer_get_count(s->ptimer);
  79. break;
  80. default:
  81. error_report("lm32_timer: read access to unknown register 0x"
  82. TARGET_FMT_plx, addr << 2);
  83. break;
  84. }
  85. trace_lm32_timer_memory_read(addr << 2, r);
  86. return r;
  87. }
  88. static void timer_write(void *opaque, hwaddr addr,
  89. uint64_t value, unsigned size)
  90. {
  91. LM32TimerState *s = opaque;
  92. trace_lm32_timer_memory_write(addr, value);
  93. addr >>= 2;
  94. switch (addr) {
  95. case R_SR:
  96. s->regs[R_SR] &= ~SR_TO;
  97. break;
  98. case R_CR:
  99. s->regs[R_CR] = value;
  100. if (s->regs[R_CR] & CR_START) {
  101. ptimer_run(s->ptimer, 1);
  102. }
  103. if (s->regs[R_CR] & CR_STOP) {
  104. ptimer_stop(s->ptimer);
  105. }
  106. break;
  107. case R_PERIOD:
  108. s->regs[R_PERIOD] = value;
  109. ptimer_set_count(s->ptimer, value);
  110. break;
  111. case R_SNAPSHOT:
  112. error_report("lm32_timer: write access to read only register 0x"
  113. TARGET_FMT_plx, addr << 2);
  114. break;
  115. default:
  116. error_report("lm32_timer: write access to unknown register 0x"
  117. TARGET_FMT_plx, addr << 2);
  118. break;
  119. }
  120. timer_update_irq(s);
  121. }
  122. static const MemoryRegionOps timer_ops = {
  123. .read = timer_read,
  124. .write = timer_write,
  125. .endianness = DEVICE_NATIVE_ENDIAN,
  126. .valid = {
  127. .min_access_size = 4,
  128. .max_access_size = 4,
  129. },
  130. };
  131. static void timer_hit(void *opaque)
  132. {
  133. LM32TimerState *s = opaque;
  134. trace_lm32_timer_hit();
  135. s->regs[R_SR] |= SR_TO;
  136. if (s->regs[R_CR] & CR_CONT) {
  137. ptimer_set_count(s->ptimer, s->regs[R_PERIOD]);
  138. ptimer_run(s->ptimer, 1);
  139. }
  140. timer_update_irq(s);
  141. }
  142. static void timer_reset(DeviceState *d)
  143. {
  144. LM32TimerState *s = LM32_TIMER(d);
  145. int i;
  146. for (i = 0; i < R_MAX; i++) {
  147. s->regs[i] = 0;
  148. }
  149. ptimer_stop(s->ptimer);
  150. }
  151. static int lm32_timer_init(SysBusDevice *dev)
  152. {
  153. LM32TimerState *s = LM32_TIMER(dev);
  154. sysbus_init_irq(dev, &s->irq);
  155. s->bh = qemu_bh_new(timer_hit, s);
  156. s->ptimer = ptimer_init(s->bh);
  157. ptimer_set_freq(s->ptimer, s->freq_hz);
  158. memory_region_init_io(&s->iomem, OBJECT(s), &timer_ops, s,
  159. "timer", R_MAX * 4);
  160. sysbus_init_mmio(dev, &s->iomem);
  161. return 0;
  162. }
  163. static const VMStateDescription vmstate_lm32_timer = {
  164. .name = "lm32-timer",
  165. .version_id = 1,
  166. .minimum_version_id = 1,
  167. .minimum_version_id_old = 1,
  168. .fields = (VMStateField[]) {
  169. VMSTATE_PTIMER(ptimer, LM32TimerState),
  170. VMSTATE_UINT32(freq_hz, LM32TimerState),
  171. VMSTATE_UINT32_ARRAY(regs, LM32TimerState, R_MAX),
  172. VMSTATE_END_OF_LIST()
  173. }
  174. };
  175. static Property lm32_timer_properties[] = {
  176. DEFINE_PROP_UINT32("frequency", LM32TimerState, freq_hz, DEFAULT_FREQUENCY),
  177. DEFINE_PROP_END_OF_LIST(),
  178. };
  179. static void lm32_timer_class_init(ObjectClass *klass, void *data)
  180. {
  181. DeviceClass *dc = DEVICE_CLASS(klass);
  182. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  183. k->init = lm32_timer_init;
  184. dc->reset = timer_reset;
  185. dc->vmsd = &vmstate_lm32_timer;
  186. dc->props = lm32_timer_properties;
  187. }
  188. static const TypeInfo lm32_timer_info = {
  189. .name = TYPE_LM32_TIMER,
  190. .parent = TYPE_SYS_BUS_DEVICE,
  191. .instance_size = sizeof(LM32TimerState),
  192. .class_init = lm32_timer_class_init,
  193. };
  194. static void lm32_timer_register_types(void)
  195. {
  196. type_register_static(&lm32_timer_info);
  197. }
  198. type_init(lm32_timer_register_types)