xilinx_timer.c 7.0 KB

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