imx_epit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * IMX EPIT Timer
  3. *
  4. * Copyright (c) 2008 OK Labs
  5. * Copyright (c) 2011 NICTA Pty Ltd
  6. * Originally written by Hans Jiang
  7. * Updated by Peter Chubb
  8. * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
  9. *
  10. * This code is licensed under GPL version 2 or later. See
  11. * the COPYING file in the top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include "hw/timer/imx_epit.h"
  16. #include "migration/vmstate.h"
  17. #include "hw/irq.h"
  18. #include "hw/misc/imx_ccm.h"
  19. #include "qemu/module.h"
  20. #include "qemu/log.h"
  21. #ifndef DEBUG_IMX_EPIT
  22. #define DEBUG_IMX_EPIT 0
  23. #endif
  24. #define DPRINTF(fmt, args...) \
  25. do { \
  26. if (DEBUG_IMX_EPIT) { \
  27. fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_EPIT, \
  28. __func__, ##args); \
  29. } \
  30. } while (0)
  31. static const char *imx_epit_reg_name(uint32_t reg)
  32. {
  33. switch (reg) {
  34. case 0:
  35. return "CR";
  36. case 1:
  37. return "SR";
  38. case 2:
  39. return "LR";
  40. case 3:
  41. return "CMP";
  42. case 4:
  43. return "CNT";
  44. default:
  45. return "[?]";
  46. }
  47. }
  48. /*
  49. * Exact clock frequencies vary from board to board.
  50. * These are typical.
  51. */
  52. static const IMXClk imx_epit_clocks[] = {
  53. CLK_NONE, /* 00 disabled */
  54. CLK_IPG, /* 01 ipg_clk, ~532MHz */
  55. CLK_IPG_HIGH, /* 10 ipg_clk_highfreq */
  56. CLK_32k, /* 11 ipg_clk_32k -- ~32kHz */
  57. };
  58. /*
  59. * Update interrupt status
  60. */
  61. static void imx_epit_update_int(IMXEPITState *s)
  62. {
  63. if (s->sr && (s->cr & CR_OCIEN) && (s->cr & CR_EN)) {
  64. qemu_irq_raise(s->irq);
  65. } else {
  66. qemu_irq_lower(s->irq);
  67. }
  68. }
  69. /*
  70. * Must be called from within a ptimer_transaction_begin/commit block
  71. * for both s->timer_cmp and s->timer_reload.
  72. */
  73. static void imx_epit_set_freq(IMXEPITState *s)
  74. {
  75. uint32_t clksrc;
  76. uint32_t prescaler;
  77. clksrc = extract32(s->cr, CR_CLKSRC_SHIFT, 2);
  78. prescaler = 1 + extract32(s->cr, CR_PRESCALE_SHIFT, 12);
  79. s->freq = imx_ccm_get_clock_frequency(s->ccm,
  80. imx_epit_clocks[clksrc]) / prescaler;
  81. DPRINTF("Setting ptimer frequency to %u\n", s->freq);
  82. if (s->freq) {
  83. ptimer_set_freq(s->timer_reload, s->freq);
  84. ptimer_set_freq(s->timer_cmp, s->freq);
  85. }
  86. }
  87. /*
  88. * This is called both on hardware (device) reset and software reset.
  89. */
  90. static void imx_epit_reset(DeviceState *dev)
  91. {
  92. IMXEPITState *s = IMX_EPIT(dev);
  93. /* Soft reset doesn't touch some bits; hard reset clears them */
  94. s->cr &= (CR_EN|CR_ENMOD|CR_STOPEN|CR_DOZEN|CR_WAITEN|CR_DBGEN);
  95. s->sr = 0;
  96. s->lr = EPIT_TIMER_MAX;
  97. s->cmp = 0;
  98. s->cnt = 0;
  99. ptimer_transaction_begin(s->timer_cmp);
  100. ptimer_transaction_begin(s->timer_reload);
  101. /* stop both timers */
  102. ptimer_stop(s->timer_cmp);
  103. ptimer_stop(s->timer_reload);
  104. /* compute new frequency */
  105. imx_epit_set_freq(s);
  106. /* init both timers to EPIT_TIMER_MAX */
  107. ptimer_set_limit(s->timer_cmp, EPIT_TIMER_MAX, 1);
  108. ptimer_set_limit(s->timer_reload, EPIT_TIMER_MAX, 1);
  109. if (s->freq && (s->cr & CR_EN)) {
  110. /* if the timer is still enabled, restart it */
  111. ptimer_run(s->timer_reload, 0);
  112. }
  113. ptimer_transaction_commit(s->timer_cmp);
  114. ptimer_transaction_commit(s->timer_reload);
  115. }
  116. static uint32_t imx_epit_update_count(IMXEPITState *s)
  117. {
  118. s->cnt = ptimer_get_count(s->timer_reload);
  119. return s->cnt;
  120. }
  121. static uint64_t imx_epit_read(void *opaque, hwaddr offset, unsigned size)
  122. {
  123. IMXEPITState *s = IMX_EPIT(opaque);
  124. uint32_t reg_value = 0;
  125. switch (offset >> 2) {
  126. case 0: /* Control Register */
  127. reg_value = s->cr;
  128. break;
  129. case 1: /* Status Register */
  130. reg_value = s->sr;
  131. break;
  132. case 2: /* LR - ticks*/
  133. reg_value = s->lr;
  134. break;
  135. case 3: /* CMP */
  136. reg_value = s->cmp;
  137. break;
  138. case 4: /* CNT */
  139. imx_epit_update_count(s);
  140. reg_value = s->cnt;
  141. break;
  142. default:
  143. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  144. HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset);
  145. break;
  146. }
  147. DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(offset >> 2), reg_value);
  148. return reg_value;
  149. }
  150. /* Must be called from ptimer_transaction_begin/commit block for s->timer_cmp */
  151. static void imx_epit_reload_compare_timer(IMXEPITState *s)
  152. {
  153. if ((s->cr & (CR_EN | CR_OCIEN)) == (CR_EN | CR_OCIEN)) {
  154. /* if the compare feature is on and timers are running */
  155. uint32_t tmp = imx_epit_update_count(s);
  156. uint64_t next;
  157. if (tmp > s->cmp) {
  158. /* It'll fire in this round of the timer */
  159. next = tmp - s->cmp;
  160. } else { /* catch it next time around */
  161. next = tmp - s->cmp + ((s->cr & CR_RLD) ? EPIT_TIMER_MAX : s->lr);
  162. }
  163. ptimer_set_count(s->timer_cmp, next);
  164. }
  165. }
  166. static void imx_epit_write(void *opaque, hwaddr offset, uint64_t value,
  167. unsigned size)
  168. {
  169. IMXEPITState *s = IMX_EPIT(opaque);
  170. uint64_t oldcr;
  171. DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(offset >> 2),
  172. (uint32_t)value);
  173. switch (offset >> 2) {
  174. case 0: /* CR */
  175. oldcr = s->cr;
  176. s->cr = value & 0x03ffffff;
  177. if (s->cr & CR_SWR) {
  178. /* handle the reset */
  179. imx_epit_reset(DEVICE(s));
  180. /*
  181. * TODO: could we 'break' here? following operations appear
  182. * to duplicate the work imx_epit_reset() already did.
  183. */
  184. }
  185. ptimer_transaction_begin(s->timer_cmp);
  186. ptimer_transaction_begin(s->timer_reload);
  187. /* Update the frequency. Has been done already in case of a reset. */
  188. if (!(s->cr & CR_SWR)) {
  189. imx_epit_set_freq(s);
  190. }
  191. if (s->freq && (s->cr & CR_EN) && !(oldcr & CR_EN)) {
  192. if (s->cr & CR_ENMOD) {
  193. if (s->cr & CR_RLD) {
  194. ptimer_set_limit(s->timer_reload, s->lr, 1);
  195. ptimer_set_limit(s->timer_cmp, s->lr, 1);
  196. } else {
  197. ptimer_set_limit(s->timer_reload, EPIT_TIMER_MAX, 1);
  198. ptimer_set_limit(s->timer_cmp, EPIT_TIMER_MAX, 1);
  199. }
  200. }
  201. imx_epit_reload_compare_timer(s);
  202. ptimer_run(s->timer_reload, 0);
  203. if (s->cr & CR_OCIEN) {
  204. ptimer_run(s->timer_cmp, 0);
  205. } else {
  206. ptimer_stop(s->timer_cmp);
  207. }
  208. } else if (!(s->cr & CR_EN)) {
  209. /* stop both timers */
  210. ptimer_stop(s->timer_reload);
  211. ptimer_stop(s->timer_cmp);
  212. } else if (s->cr & CR_OCIEN) {
  213. if (!(oldcr & CR_OCIEN)) {
  214. imx_epit_reload_compare_timer(s);
  215. ptimer_run(s->timer_cmp, 0);
  216. }
  217. } else {
  218. ptimer_stop(s->timer_cmp);
  219. }
  220. ptimer_transaction_commit(s->timer_cmp);
  221. ptimer_transaction_commit(s->timer_reload);
  222. break;
  223. case 1: /* SR - ACK*/
  224. /* writing 1 to OCIF clears the OCIF bit */
  225. if (value & 0x01) {
  226. s->sr = 0;
  227. imx_epit_update_int(s);
  228. }
  229. break;
  230. case 2: /* LR - set ticks */
  231. s->lr = value;
  232. ptimer_transaction_begin(s->timer_cmp);
  233. ptimer_transaction_begin(s->timer_reload);
  234. if (s->cr & CR_RLD) {
  235. /* Also set the limit if the LRD bit is set */
  236. /* If IOVW bit is set then set the timer value */
  237. ptimer_set_limit(s->timer_reload, s->lr, s->cr & CR_IOVW);
  238. ptimer_set_limit(s->timer_cmp, s->lr, 0);
  239. } else if (s->cr & CR_IOVW) {
  240. /* If IOVW bit is set then set the timer value */
  241. ptimer_set_count(s->timer_reload, s->lr);
  242. }
  243. /*
  244. * Commit the change to s->timer_reload, so it can propagate. Otherwise
  245. * the timer interrupt may not fire properly. The commit must happen
  246. * before calling imx_epit_reload_compare_timer(), which reads
  247. * s->timer_reload internally again.
  248. */
  249. ptimer_transaction_commit(s->timer_reload);
  250. imx_epit_reload_compare_timer(s);
  251. ptimer_transaction_commit(s->timer_cmp);
  252. break;
  253. case 3: /* CMP */
  254. s->cmp = value;
  255. ptimer_transaction_begin(s->timer_cmp);
  256. imx_epit_reload_compare_timer(s);
  257. ptimer_transaction_commit(s->timer_cmp);
  258. break;
  259. default:
  260. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  261. HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset);
  262. break;
  263. }
  264. }
  265. static void imx_epit_cmp(void *opaque)
  266. {
  267. IMXEPITState *s = IMX_EPIT(opaque);
  268. DPRINTF("sr was %d\n", s->sr);
  269. s->sr = 1;
  270. imx_epit_update_int(s);
  271. }
  272. static void imx_epit_reload(void *opaque)
  273. {
  274. /* No action required on rollover of timer_reload */
  275. }
  276. static const MemoryRegionOps imx_epit_ops = {
  277. .read = imx_epit_read,
  278. .write = imx_epit_write,
  279. .endianness = DEVICE_NATIVE_ENDIAN,
  280. };
  281. static const VMStateDescription vmstate_imx_timer_epit = {
  282. .name = TYPE_IMX_EPIT,
  283. .version_id = 2,
  284. .minimum_version_id = 2,
  285. .fields = (VMStateField[]) {
  286. VMSTATE_UINT32(cr, IMXEPITState),
  287. VMSTATE_UINT32(sr, IMXEPITState),
  288. VMSTATE_UINT32(lr, IMXEPITState),
  289. VMSTATE_UINT32(cmp, IMXEPITState),
  290. VMSTATE_UINT32(cnt, IMXEPITState),
  291. VMSTATE_UINT32(freq, IMXEPITState),
  292. VMSTATE_PTIMER(timer_reload, IMXEPITState),
  293. VMSTATE_PTIMER(timer_cmp, IMXEPITState),
  294. VMSTATE_END_OF_LIST()
  295. }
  296. };
  297. static void imx_epit_realize(DeviceState *dev, Error **errp)
  298. {
  299. IMXEPITState *s = IMX_EPIT(dev);
  300. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  301. DPRINTF("\n");
  302. sysbus_init_irq(sbd, &s->irq);
  303. memory_region_init_io(&s->iomem, OBJECT(s), &imx_epit_ops, s, TYPE_IMX_EPIT,
  304. 0x00001000);
  305. sysbus_init_mmio(sbd, &s->iomem);
  306. /*
  307. * The reload timer keeps running when the peripheral is enabled. It is a
  308. * kind of wall clock that does not generate any interrupts. The callback
  309. * needs to be provided, but it does nothing as the ptimer already supports
  310. * all necessary reloading functionality.
  311. */
  312. s->timer_reload = ptimer_init(imx_epit_reload, s, PTIMER_POLICY_LEGACY);
  313. /*
  314. * The compare timer is running only when the peripheral configuration is
  315. * in a state that will generate compare interrupts.
  316. */
  317. s->timer_cmp = ptimer_init(imx_epit_cmp, s, PTIMER_POLICY_LEGACY);
  318. }
  319. static void imx_epit_class_init(ObjectClass *klass, void *data)
  320. {
  321. DeviceClass *dc = DEVICE_CLASS(klass);
  322. dc->realize = imx_epit_realize;
  323. dc->reset = imx_epit_reset;
  324. dc->vmsd = &vmstate_imx_timer_epit;
  325. dc->desc = "i.MX periodic timer";
  326. }
  327. static const TypeInfo imx_epit_info = {
  328. .name = TYPE_IMX_EPIT,
  329. .parent = TYPE_SYS_BUS_DEVICE,
  330. .instance_size = sizeof(IMXEPITState),
  331. .class_init = imx_epit_class_init,
  332. };
  333. static void imx_epit_register_types(void)
  334. {
  335. type_register_static(&imx_epit_info);
  336. }
  337. type_init(imx_epit_register_types)