2
0

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. #define D(x)
  32. #define R_TCSR 0
  33. #define R_TLR 1
  34. #define R_TCR 2
  35. #define R_MAX 4
  36. #define TCSR_MDT (1<<0)
  37. #define TCSR_UDT (1<<1)
  38. #define TCSR_GENT (1<<2)
  39. #define TCSR_CAPT (1<<3)
  40. #define TCSR_ARHT (1<<4)
  41. #define TCSR_LOAD (1<<5)
  42. #define TCSR_ENIT (1<<6)
  43. #define TCSR_ENT (1<<7)
  44. #define TCSR_TINT (1<<8)
  45. #define TCSR_PWMA (1<<9)
  46. #define TCSR_ENALL (1<<10)
  47. struct xlx_timer
  48. {
  49. ptimer_state *ptimer;
  50. void *parent;
  51. int nr; /* for debug. */
  52. unsigned long timer_div;
  53. uint32_t regs[R_MAX];
  54. };
  55. #define TYPE_XILINX_TIMER "xlnx.xps-timer"
  56. #define XILINX_TIMER(obj) \
  57. OBJECT_CHECK(struct timerblock, (obj), TYPE_XILINX_TIMER)
  58. struct timerblock
  59. {
  60. SysBusDevice parent_obj;
  61. MemoryRegion mmio;
  62. qemu_irq irq;
  63. uint8_t one_timer_only;
  64. uint32_t freq_hz;
  65. struct xlx_timer *timers;
  66. };
  67. static inline unsigned int num_timers(struct timerblock *t)
  68. {
  69. return 2 - t->one_timer_only;
  70. }
  71. static inline unsigned int timer_from_addr(hwaddr addr)
  72. {
  73. /* Timers get a 4x32bit control reg area each. */
  74. return addr >> 2;
  75. }
  76. static void timer_update_irq(struct timerblock *t)
  77. {
  78. unsigned int i, irq = 0;
  79. uint32_t csr;
  80. for (i = 0; i < num_timers(t); i++) {
  81. csr = t->timers[i].regs[R_TCSR];
  82. irq |= (csr & TCSR_TINT) && (csr & TCSR_ENIT);
  83. }
  84. /* All timers within the same slave share a single IRQ line. */
  85. qemu_set_irq(t->irq, !!irq);
  86. }
  87. static uint64_t
  88. timer_read(void *opaque, hwaddr addr, unsigned int size)
  89. {
  90. struct timerblock *t = opaque;
  91. struct xlx_timer *xt;
  92. uint32_t r = 0;
  93. unsigned int timer;
  94. addr >>= 2;
  95. timer = timer_from_addr(addr);
  96. xt = &t->timers[timer];
  97. /* Further decoding to address a specific timers reg. */
  98. addr &= 0x3;
  99. switch (addr)
  100. {
  101. case R_TCR:
  102. r = ptimer_get_count(xt->ptimer);
  103. if (!(xt->regs[R_TCSR] & TCSR_UDT))
  104. r = ~r;
  105. D(qemu_log("xlx_timer t=%d read counter=%x udt=%d\n",
  106. timer, r, xt->regs[R_TCSR] & TCSR_UDT));
  107. break;
  108. default:
  109. if (addr < ARRAY_SIZE(xt->regs))
  110. r = xt->regs[addr];
  111. break;
  112. }
  113. D(fprintf(stderr, "%s timer=%d %x=%x\n", __func__, timer, addr * 4, r));
  114. return r;
  115. }
  116. /* Must be called inside ptimer transaction block */
  117. static void timer_enable(struct xlx_timer *xt)
  118. {
  119. uint64_t count;
  120. D(fprintf(stderr, "%s timer=%d down=%d\n", __func__,
  121. xt->nr, xt->regs[R_TCSR] & TCSR_UDT));
  122. ptimer_stop(xt->ptimer);
  123. if (xt->regs[R_TCSR] & TCSR_UDT)
  124. count = xt->regs[R_TLR];
  125. else
  126. count = ~0 - xt->regs[R_TLR];
  127. ptimer_set_limit(xt->ptimer, count, 1);
  128. ptimer_run(xt->ptimer, 1);
  129. }
  130. static void
  131. timer_write(void *opaque, hwaddr addr,
  132. uint64_t val64, unsigned int size)
  133. {
  134. struct timerblock *t = opaque;
  135. struct xlx_timer *xt;
  136. unsigned int timer;
  137. uint32_t value = val64;
  138. addr >>= 2;
  139. timer = timer_from_addr(addr);
  140. xt = &t->timers[timer];
  141. D(fprintf(stderr, "%s addr=%x val=%x (timer=%d off=%d)\n",
  142. __func__, addr * 4, value, timer, addr & 3));
  143. /* Further decoding to address a specific timers reg. */
  144. addr &= 3;
  145. switch (addr)
  146. {
  147. case R_TCSR:
  148. if (value & TCSR_TINT)
  149. value &= ~TCSR_TINT;
  150. xt->regs[addr] = value & 0x7ff;
  151. if (value & TCSR_ENT) {
  152. ptimer_transaction_begin(xt->ptimer);
  153. timer_enable(xt);
  154. ptimer_transaction_commit(xt->ptimer);
  155. }
  156. break;
  157. default:
  158. if (addr < ARRAY_SIZE(xt->regs))
  159. xt->regs[addr] = value;
  160. break;
  161. }
  162. timer_update_irq(t);
  163. }
  164. static const MemoryRegionOps timer_ops = {
  165. .read = timer_read,
  166. .write = timer_write,
  167. .endianness = DEVICE_NATIVE_ENDIAN,
  168. .valid = {
  169. .min_access_size = 4,
  170. .max_access_size = 4
  171. }
  172. };
  173. static void timer_hit(void *opaque)
  174. {
  175. struct xlx_timer *xt = opaque;
  176. struct timerblock *t = xt->parent;
  177. D(fprintf(stderr, "%s %d\n", __func__, xt->nr));
  178. xt->regs[R_TCSR] |= TCSR_TINT;
  179. if (xt->regs[R_TCSR] & TCSR_ARHT)
  180. timer_enable(xt);
  181. timer_update_irq(t);
  182. }
  183. static void xilinx_timer_realize(DeviceState *dev, Error **errp)
  184. {
  185. struct timerblock *t = XILINX_TIMER(dev);
  186. unsigned int i;
  187. /* Init all the ptimers. */
  188. t->timers = g_malloc0(sizeof t->timers[0] * num_timers(t));
  189. for (i = 0; i < num_timers(t); i++) {
  190. struct xlx_timer *xt = &t->timers[i];
  191. xt->parent = t;
  192. xt->nr = i;
  193. xt->ptimer = ptimer_init(timer_hit, xt, PTIMER_POLICY_DEFAULT);
  194. ptimer_transaction_begin(xt->ptimer);
  195. ptimer_set_freq(xt->ptimer, t->freq_hz);
  196. ptimer_transaction_commit(xt->ptimer);
  197. }
  198. memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t, "xlnx.xps-timer",
  199. R_MAX * 4 * num_timers(t));
  200. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &t->mmio);
  201. }
  202. static void xilinx_timer_init(Object *obj)
  203. {
  204. struct timerblock *t = XILINX_TIMER(obj);
  205. /* All timers share a single irq line. */
  206. sysbus_init_irq(SYS_BUS_DEVICE(obj), &t->irq);
  207. }
  208. static Property xilinx_timer_properties[] = {
  209. DEFINE_PROP_UINT32("clock-frequency", struct timerblock, freq_hz,
  210. 62 * 1000000),
  211. DEFINE_PROP_UINT8("one-timer-only", struct timerblock, 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. dc->props = 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(struct timerblock),
  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)