xilinx_timer.c 8.1 KB

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