lm32_timer.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "qemu-error.h"
  28. #define DEFAULT_FREQUENCY (50*1000000)
  29. enum {
  30. R_SR = 0,
  31. R_CR,
  32. R_PERIOD,
  33. R_SNAPSHOT,
  34. R_MAX
  35. };
  36. enum {
  37. SR_TO = (1 << 0),
  38. SR_RUN = (1 << 1),
  39. };
  40. enum {
  41. CR_ITO = (1 << 0),
  42. CR_CONT = (1 << 1),
  43. CR_START = (1 << 2),
  44. CR_STOP = (1 << 3),
  45. };
  46. struct LM32TimerState {
  47. SysBusDevice busdev;
  48. QEMUBH *bh;
  49. ptimer_state *ptimer;
  50. qemu_irq irq;
  51. uint32_t freq_hz;
  52. uint32_t regs[R_MAX];
  53. };
  54. typedef struct LM32TimerState LM32TimerState;
  55. static void timer_update_irq(LM32TimerState *s)
  56. {
  57. int state = (s->regs[R_SR] & SR_TO) && (s->regs[R_CR] & CR_ITO);
  58. trace_lm32_timer_irq_state(state);
  59. qemu_set_irq(s->irq, state);
  60. }
  61. static uint32_t timer_read(void *opaque, target_phys_addr_t addr)
  62. {
  63. LM32TimerState *s = opaque;
  64. uint32_t r = 0;
  65. addr >>= 2;
  66. switch (addr) {
  67. case R_SR:
  68. case R_CR:
  69. case R_PERIOD:
  70. r = s->regs[addr];
  71. break;
  72. case R_SNAPSHOT:
  73. r = (uint32_t)ptimer_get_count(s->ptimer);
  74. break;
  75. default:
  76. error_report("lm32_timer: read access to unknown register 0x"
  77. TARGET_FMT_plx, addr << 2);
  78. break;
  79. }
  80. trace_lm32_timer_memory_read(addr << 2, r);
  81. return r;
  82. }
  83. static void timer_write(void *opaque, target_phys_addr_t addr, uint32_t value)
  84. {
  85. LM32TimerState *s = opaque;
  86. trace_lm32_timer_memory_write(addr, value);
  87. addr >>= 2;
  88. switch (addr) {
  89. case R_SR:
  90. s->regs[R_SR] &= ~SR_TO;
  91. break;
  92. case R_CR:
  93. s->regs[R_CR] = value;
  94. if (s->regs[R_CR] & CR_START) {
  95. ptimer_run(s->ptimer, 1);
  96. }
  97. if (s->regs[R_CR] & CR_STOP) {
  98. ptimer_stop(s->ptimer);
  99. }
  100. break;
  101. case R_PERIOD:
  102. s->regs[R_PERIOD] = value;
  103. ptimer_set_count(s->ptimer, value);
  104. break;
  105. case R_SNAPSHOT:
  106. error_report("lm32_timer: write access to read only register 0x"
  107. TARGET_FMT_plx, addr << 2);
  108. break;
  109. default:
  110. error_report("lm32_timer: write access to unknown register 0x"
  111. TARGET_FMT_plx, addr << 2);
  112. break;
  113. }
  114. timer_update_irq(s);
  115. }
  116. static CPUReadMemoryFunc * const timer_read_fn[] = {
  117. NULL,
  118. NULL,
  119. &timer_read,
  120. };
  121. static CPUWriteMemoryFunc * const timer_write_fn[] = {
  122. NULL,
  123. NULL,
  124. &timer_write,
  125. };
  126. static void timer_hit(void *opaque)
  127. {
  128. LM32TimerState *s = opaque;
  129. trace_lm32_timer_hit();
  130. s->regs[R_SR] |= SR_TO;
  131. if (s->regs[R_CR] & CR_CONT) {
  132. ptimer_set_count(s->ptimer, s->regs[R_PERIOD]);
  133. ptimer_run(s->ptimer, 1);
  134. }
  135. timer_update_irq(s);
  136. }
  137. static void timer_reset(DeviceState *d)
  138. {
  139. LM32TimerState *s = container_of(d, LM32TimerState, busdev.qdev);
  140. int i;
  141. for (i = 0; i < R_MAX; i++) {
  142. s->regs[i] = 0;
  143. }
  144. ptimer_stop(s->ptimer);
  145. }
  146. static int lm32_timer_init(SysBusDevice *dev)
  147. {
  148. LM32TimerState *s = FROM_SYSBUS(typeof(*s), dev);
  149. int timer_regs;
  150. sysbus_init_irq(dev, &s->irq);
  151. s->bh = qemu_bh_new(timer_hit, s);
  152. s->ptimer = ptimer_init(s->bh);
  153. ptimer_set_freq(s->ptimer, s->freq_hz);
  154. timer_regs = cpu_register_io_memory(timer_read_fn, timer_write_fn, s,
  155. DEVICE_NATIVE_ENDIAN);
  156. sysbus_init_mmio(dev, R_MAX * 4, timer_regs);
  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)