xilinx_timer.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * QEMU model of the Xilinx timer block.
  3. *
  4. * Copyright (c) 2009 Edgar E. Iglesias.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "hw/sysbus.h"
  26. #include "hw/irq.h"
  27. #include "hw/ptimer.h"
  28. #include "hw/qdev-properties.h"
  29. #include "qemu/log.h"
  30. #include "qemu/module.h"
  31. #include "qom/object.h"
  32. #define D(x)
  33. #define R_TCSR 0
  34. #define R_TLR 1
  35. #define R_TCR 2
  36. #define R_MAX 4
  37. #define TCSR_MDT (1<<0)
  38. #define TCSR_UDT (1<<1)
  39. #define TCSR_GENT (1<<2)
  40. #define TCSR_CAPT (1<<3)
  41. #define TCSR_ARHT (1<<4)
  42. #define TCSR_LOAD (1<<5)
  43. #define TCSR_ENIT (1<<6)
  44. #define TCSR_ENT (1<<7)
  45. #define TCSR_TINT (1<<8)
  46. #define TCSR_PWMA (1<<9)
  47. #define TCSR_ENALL (1<<10)
  48. struct xlx_timer
  49. {
  50. ptimer_state *ptimer;
  51. void *parent;
  52. int nr; /* for debug. */
  53. unsigned long timer_div;
  54. uint32_t regs[R_MAX];
  55. };
  56. #define TYPE_XILINX_TIMER "xlnx.xps-timer"
  57. typedef struct XpsTimerState XpsTimerState;
  58. DECLARE_INSTANCE_CHECKER(XpsTimerState, XILINX_TIMER, TYPE_XILINX_TIMER)
  59. struct XpsTimerState
  60. {
  61. SysBusDevice parent_obj;
  62. MemoryRegion mmio;
  63. qemu_irq irq;
  64. uint8_t one_timer_only;
  65. uint32_t freq_hz;
  66. struct xlx_timer *timers;
  67. };
  68. static inline unsigned int num_timers(XpsTimerState *t)
  69. {
  70. return 2 - t->one_timer_only;
  71. }
  72. static inline unsigned int timer_from_addr(hwaddr addr)
  73. {
  74. /* Timers get a 4x32bit control reg area each. */
  75. return addr >> 2;
  76. }
  77. static void timer_update_irq(XpsTimerState *t)
  78. {
  79. unsigned int i, irq = 0;
  80. uint32_t csr;
  81. for (i = 0; i < num_timers(t); i++) {
  82. csr = t->timers[i].regs[R_TCSR];
  83. irq |= (csr & TCSR_TINT) && (csr & TCSR_ENIT);
  84. }
  85. /* All timers within the same slave share a single IRQ line. */
  86. qemu_set_irq(t->irq, !!irq);
  87. }
  88. static uint64_t
  89. timer_read(void *opaque, hwaddr addr, unsigned int size)
  90. {
  91. XpsTimerState *t = opaque;
  92. struct xlx_timer *xt;
  93. uint32_t r = 0;
  94. unsigned int timer;
  95. addr >>= 2;
  96. timer = timer_from_addr(addr);
  97. xt = &t->timers[timer];
  98. /* Further decoding to address a specific timers reg. */
  99. addr &= 0x3;
  100. switch (addr)
  101. {
  102. case R_TCR:
  103. r = ptimer_get_count(xt->ptimer);
  104. if (!(xt->regs[R_TCSR] & TCSR_UDT))
  105. r = ~r;
  106. D(qemu_log("xlx_timer t=%d read counter=%x udt=%d\n",
  107. timer, r, xt->regs[R_TCSR] & TCSR_UDT));
  108. break;
  109. default:
  110. if (addr < ARRAY_SIZE(xt->regs))
  111. r = xt->regs[addr];
  112. break;
  113. }
  114. D(fprintf(stderr, "%s timer=%d %x=%x\n", __func__, timer, addr * 4, r));
  115. return r;
  116. }
  117. /* Must be called inside ptimer transaction block */
  118. static void timer_enable(struct xlx_timer *xt)
  119. {
  120. uint64_t count;
  121. D(fprintf(stderr, "%s timer=%d down=%d\n", __func__,
  122. xt->nr, xt->regs[R_TCSR] & TCSR_UDT));
  123. ptimer_stop(xt->ptimer);
  124. if (xt->regs[R_TCSR] & TCSR_UDT)
  125. count = xt->regs[R_TLR];
  126. else
  127. count = ~0 - xt->regs[R_TLR];
  128. ptimer_set_limit(xt->ptimer, count, 1);
  129. ptimer_run(xt->ptimer, 1);
  130. }
  131. static void
  132. timer_write(void *opaque, hwaddr addr,
  133. uint64_t val64, unsigned int size)
  134. {
  135. XpsTimerState *t = opaque;
  136. struct xlx_timer *xt;
  137. unsigned int timer;
  138. uint32_t value = val64;
  139. addr >>= 2;
  140. timer = timer_from_addr(addr);
  141. xt = &t->timers[timer];
  142. D(fprintf(stderr, "%s addr=%x val=%x (timer=%d off=%d)\n",
  143. __func__, addr * 4, value, timer, addr & 3));
  144. /* Further decoding to address a specific timers reg. */
  145. addr &= 3;
  146. switch (addr)
  147. {
  148. case R_TCSR:
  149. if (value & TCSR_TINT)
  150. value &= ~TCSR_TINT;
  151. xt->regs[addr] = value & 0x7ff;
  152. if (value & TCSR_ENT) {
  153. ptimer_transaction_begin(xt->ptimer);
  154. timer_enable(xt);
  155. ptimer_transaction_commit(xt->ptimer);
  156. }
  157. break;
  158. default:
  159. if (addr < ARRAY_SIZE(xt->regs))
  160. xt->regs[addr] = value;
  161. break;
  162. }
  163. timer_update_irq(t);
  164. }
  165. static const MemoryRegionOps timer_ops = {
  166. .read = timer_read,
  167. .write = timer_write,
  168. .endianness = DEVICE_NATIVE_ENDIAN,
  169. .valid = {
  170. .min_access_size = 4,
  171. .max_access_size = 4
  172. }
  173. };
  174. static void timer_hit(void *opaque)
  175. {
  176. struct xlx_timer *xt = opaque;
  177. XpsTimerState *t = xt->parent;
  178. D(fprintf(stderr, "%s %d\n", __func__, xt->nr));
  179. xt->regs[R_TCSR] |= TCSR_TINT;
  180. if (xt->regs[R_TCSR] & TCSR_ARHT)
  181. timer_enable(xt);
  182. timer_update_irq(t);
  183. }
  184. static void xilinx_timer_realize(DeviceState *dev, Error **errp)
  185. {
  186. XpsTimerState *t = XILINX_TIMER(dev);
  187. unsigned int i;
  188. /* Init all the ptimers. */
  189. t->timers = g_malloc0(sizeof t->timers[0] * num_timers(t));
  190. for (i = 0; i < num_timers(t); i++) {
  191. struct xlx_timer *xt = &t->timers[i];
  192. xt->parent = t;
  193. xt->nr = i;
  194. xt->ptimer = ptimer_init(timer_hit, xt, PTIMER_POLICY_LEGACY);
  195. ptimer_transaction_begin(xt->ptimer);
  196. ptimer_set_freq(xt->ptimer, t->freq_hz);
  197. ptimer_transaction_commit(xt->ptimer);
  198. }
  199. memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t, "xlnx.xps-timer",
  200. R_MAX * 4 * num_timers(t));
  201. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &t->mmio);
  202. }
  203. static void xilinx_timer_init(Object *obj)
  204. {
  205. XpsTimerState *t = XILINX_TIMER(obj);
  206. /* All timers share a single irq line. */
  207. sysbus_init_irq(SYS_BUS_DEVICE(obj), &t->irq);
  208. }
  209. static Property xilinx_timer_properties[] = {
  210. DEFINE_PROP_UINT32("clock-frequency", XpsTimerState, freq_hz, 62 * 1000000),
  211. DEFINE_PROP_UINT8("one-timer-only", XpsTimerState, one_timer_only, 0),
  212. DEFINE_PROP_END_OF_LIST(),
  213. };
  214. static void xilinx_timer_class_init(ObjectClass *klass, void *data)
  215. {
  216. DeviceClass *dc = DEVICE_CLASS(klass);
  217. dc->realize = xilinx_timer_realize;
  218. device_class_set_props(dc, xilinx_timer_properties);
  219. }
  220. static const TypeInfo xilinx_timer_info = {
  221. .name = TYPE_XILINX_TIMER,
  222. .parent = TYPE_SYS_BUS_DEVICE,
  223. .instance_size = sizeof(XpsTimerState),
  224. .instance_init = xilinx_timer_init,
  225. .class_init = xilinx_timer_class_init,
  226. };
  227. static void xilinx_timer_register_types(void)
  228. {
  229. type_register_static(&xilinx_timer_info);
  230. }
  231. type_init(xilinx_timer_register_types)