lm32_timer.c 5.2 KB

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