xilinx_timer.c 6.8 KB

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