2
0

imx_epit.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. * Updated by Axel Heider
  10. *
  11. * This code is licensed under GPL version 2 or later. See
  12. * the COPYING file in the top-level directory.
  13. *
  14. */
  15. #include "qemu/osdep.h"
  16. #include "hw/timer/imx_epit.h"
  17. #include "migration/vmstate.h"
  18. #include "hw/irq.h"
  19. #include "hw/misc/imx_ccm.h"
  20. #include "qemu/module.h"
  21. #include "qemu/log.h"
  22. #ifndef DEBUG_IMX_EPIT
  23. #define DEBUG_IMX_EPIT 0
  24. #endif
  25. #define DPRINTF(fmt, args...) \
  26. do { \
  27. if (DEBUG_IMX_EPIT) { \
  28. fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_EPIT, \
  29. __func__, ##args); \
  30. } \
  31. } while (0)
  32. static const char *imx_epit_reg_name(uint32_t reg)
  33. {
  34. switch (reg) {
  35. case 0:
  36. return "CR";
  37. case 1:
  38. return "SR";
  39. case 2:
  40. return "LR";
  41. case 3:
  42. return "CMP";
  43. case 4:
  44. return "CNT";
  45. default:
  46. return "[?]";
  47. }
  48. }
  49. /*
  50. * Exact clock frequencies vary from board to board.
  51. * These are typical.
  52. */
  53. static const IMXClk imx_epit_clocks[] = {
  54. CLK_NONE, /* 00 disabled */
  55. CLK_IPG, /* 01 ipg_clk, ~532MHz */
  56. CLK_IPG_HIGH, /* 10 ipg_clk_highfreq */
  57. CLK_32k, /* 11 ipg_clk_32k -- ~32kHz */
  58. };
  59. /*
  60. * Update interrupt status
  61. */
  62. static void imx_epit_update_int(IMXEPITState *s)
  63. {
  64. if ((s->sr & SR_OCIF) && (s->cr & CR_OCIEN) && (s->cr & CR_EN)) {
  65. qemu_irq_raise(s->irq);
  66. } else {
  67. qemu_irq_lower(s->irq);
  68. }
  69. }
  70. static uint32_t imx_epit_get_freq(IMXEPITState *s)
  71. {
  72. uint32_t clksrc = extract32(s->cr, CR_CLKSRC_SHIFT, CR_CLKSRC_BITS);
  73. uint32_t prescaler = 1 + extract32(s->cr, CR_PRESCALE_SHIFT, CR_PRESCALE_BITS);
  74. uint32_t f_in = imx_ccm_get_clock_frequency(s->ccm, imx_epit_clocks[clksrc]);
  75. uint32_t freq = f_in / prescaler;
  76. DPRINTF("ptimer frequency is %u\n", freq);
  77. return freq;
  78. }
  79. /*
  80. * This is called both on hardware (device) reset and software reset.
  81. */
  82. static void imx_epit_reset(IMXEPITState *s, bool is_hard_reset)
  83. {
  84. /* Soft reset doesn't touch some bits; hard reset clears them */
  85. if (is_hard_reset) {
  86. s->cr = 0;
  87. } else {
  88. s->cr &= (CR_EN|CR_ENMOD|CR_STOPEN|CR_DOZEN|CR_WAITEN|CR_DBGEN);
  89. }
  90. s->sr = 0;
  91. s->lr = EPIT_TIMER_MAX;
  92. s->cmp = 0;
  93. ptimer_transaction_begin(s->timer_cmp);
  94. ptimer_transaction_begin(s->timer_reload);
  95. /*
  96. * The reset switches off the input clock, so even if the CR.EN is still
  97. * set, the timers are no longer running.
  98. */
  99. assert(imx_epit_get_freq(s) == 0);
  100. ptimer_stop(s->timer_cmp);
  101. ptimer_stop(s->timer_reload);
  102. /* init both timers to EPIT_TIMER_MAX */
  103. ptimer_set_limit(s->timer_cmp, EPIT_TIMER_MAX, 1);
  104. ptimer_set_limit(s->timer_reload, EPIT_TIMER_MAX, 1);
  105. ptimer_transaction_commit(s->timer_cmp);
  106. ptimer_transaction_commit(s->timer_reload);
  107. }
  108. static uint64_t imx_epit_read(void *opaque, hwaddr offset, unsigned size)
  109. {
  110. IMXEPITState *s = IMX_EPIT(opaque);
  111. uint32_t reg_value = 0;
  112. switch (offset >> 2) {
  113. case 0: /* Control Register */
  114. reg_value = s->cr;
  115. break;
  116. case 1: /* Status Register */
  117. reg_value = s->sr;
  118. break;
  119. case 2: /* LR - ticks*/
  120. reg_value = s->lr;
  121. break;
  122. case 3: /* CMP */
  123. reg_value = s->cmp;
  124. break;
  125. case 4: /* CNT */
  126. reg_value = ptimer_get_count(s->timer_reload);
  127. break;
  128. default:
  129. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  130. HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset);
  131. break;
  132. }
  133. DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(offset >> 2), reg_value);
  134. return reg_value;
  135. }
  136. /*
  137. * Must be called from a ptimer_transaction_begin/commit block for
  138. * s->timer_cmp, but outside of a transaction block of s->timer_reload,
  139. * so the proper counter value is read.
  140. */
  141. static void imx_epit_update_compare_timer(IMXEPITState *s)
  142. {
  143. uint64_t counter = 0;
  144. bool is_oneshot = false;
  145. /*
  146. * The compare timer only has to run if the timer peripheral is active
  147. * and there is an input clock, Otherwise it can be switched off.
  148. */
  149. bool is_active = (s->cr & CR_EN) && imx_epit_get_freq(s);
  150. if (is_active) {
  151. /*
  152. * Calculate next timeout for compare timer. Reading the reload
  153. * counter returns proper results only if pending transactions
  154. * on it are committed here. Otherwise stale values are be read.
  155. */
  156. counter = ptimer_get_count(s->timer_reload);
  157. uint64_t limit = ptimer_get_limit(s->timer_cmp);
  158. /*
  159. * The compare timer is a periodic timer if the limit is at least
  160. * the compare value. Otherwise it may fire at most once in the
  161. * current round.
  162. */
  163. is_oneshot = (limit < s->cmp);
  164. if (counter >= s->cmp) {
  165. /* The compare timer fires in the current round. */
  166. counter -= s->cmp;
  167. } else if (!is_oneshot) {
  168. /*
  169. * The compare timer fires after a reload, as it is below the
  170. * compare value already in this round. Note that the counter
  171. * value calculated below can be above the 32-bit limit, which
  172. * is legal here because the compare timer is an internal
  173. * helper ptimer only.
  174. */
  175. counter += limit - s->cmp;
  176. } else {
  177. /*
  178. * The compare timer won't fire in this round, and the limit is
  179. * set to a value below the compare value. This practically means
  180. * it will never fire, so it can be switched off.
  181. */
  182. is_active = false;
  183. }
  184. }
  185. /*
  186. * Set the compare timer and let it run, or stop it. This is agnostic
  187. * of CR.OCIEN bit, as this bit affects interrupt generation only. The
  188. * compare timer needs to run even if no interrupts are to be generated,
  189. * because the SR.OCIF bit must be updated also.
  190. * Note that the timer might already be stopped or be running with
  191. * counter values. However, finding out when an update is needed and
  192. * when not is not trivial. It's much easier applying the setting again,
  193. * as this does not harm either and the overhead is negligible.
  194. */
  195. if (is_active) {
  196. ptimer_set_count(s->timer_cmp, counter);
  197. ptimer_run(s->timer_cmp, is_oneshot ? 1 : 0);
  198. } else {
  199. ptimer_stop(s->timer_cmp);
  200. }
  201. }
  202. static void imx_epit_write_cr(IMXEPITState *s, uint32_t value)
  203. {
  204. uint32_t oldcr = s->cr;
  205. s->cr = value & 0x03ffffff;
  206. if (s->cr & CR_SWR) {
  207. /*
  208. * Reset clears CR.SWR again. It does not touch CR.EN, but the timers
  209. * are still stopped because the input clock is disabled.
  210. */
  211. imx_epit_reset(s, false);
  212. } else {
  213. uint32_t freq;
  214. uint32_t toggled_cr_bits = oldcr ^ s->cr;
  215. /* re-initialize the limits if CR.RLD has changed */
  216. bool set_limit = toggled_cr_bits & CR_RLD;
  217. /* set the counter if the timer got just enabled and CR.ENMOD is set */
  218. bool is_switched_on = (toggled_cr_bits & s->cr) & CR_EN;
  219. bool set_counter = is_switched_on && (s->cr & CR_ENMOD);
  220. ptimer_transaction_begin(s->timer_cmp);
  221. ptimer_transaction_begin(s->timer_reload);
  222. freq = imx_epit_get_freq(s);
  223. if (freq) {
  224. ptimer_set_freq(s->timer_reload, freq);
  225. ptimer_set_freq(s->timer_cmp, freq);
  226. }
  227. if (set_limit || set_counter) {
  228. uint64_t limit = (s->cr & CR_RLD) ? s->lr : EPIT_TIMER_MAX;
  229. ptimer_set_limit(s->timer_reload, limit, set_counter ? 1 : 0);
  230. if (set_limit) {
  231. ptimer_set_limit(s->timer_cmp, limit, 0);
  232. }
  233. }
  234. /*
  235. * If there is an input clock and the peripheral is enabled, then
  236. * ensure the wall clock timer is ticking. Otherwise stop the timers.
  237. * The compare timer will be updated later.
  238. */
  239. if (freq && (s->cr & CR_EN)) {
  240. ptimer_run(s->timer_reload, 0);
  241. } else {
  242. ptimer_stop(s->timer_reload);
  243. }
  244. /* Commit changes to reload timer, so they can propagate. */
  245. ptimer_transaction_commit(s->timer_reload);
  246. /* Update compare timer based on the committed reload timer value. */
  247. imx_epit_update_compare_timer(s);
  248. ptimer_transaction_commit(s->timer_cmp);
  249. }
  250. /*
  251. * The interrupt state can change due to:
  252. * - reset clears both SR.OCIF and CR.OCIE
  253. * - write to CR.EN or CR.OCIE
  254. */
  255. imx_epit_update_int(s);
  256. }
  257. static void imx_epit_write_sr(IMXEPITState *s, uint32_t value)
  258. {
  259. /* writing 1 to SR.OCIF clears this bit and turns the interrupt off */
  260. if (value & SR_OCIF) {
  261. s->sr = 0; /* SR.OCIF is the only bit in this register anyway */
  262. imx_epit_update_int(s);
  263. }
  264. }
  265. static void imx_epit_write_lr(IMXEPITState *s, uint32_t value)
  266. {
  267. s->lr = value;
  268. ptimer_transaction_begin(s->timer_cmp);
  269. ptimer_transaction_begin(s->timer_reload);
  270. if (s->cr & CR_RLD) {
  271. /* Also set the limit if the LRD bit is set */
  272. /* If IOVW bit is set then set the timer value */
  273. ptimer_set_limit(s->timer_reload, s->lr, s->cr & CR_IOVW);
  274. ptimer_set_limit(s->timer_cmp, s->lr, 0);
  275. } else if (s->cr & CR_IOVW) {
  276. /* If IOVW bit is set then set the timer value */
  277. ptimer_set_count(s->timer_reload, s->lr);
  278. }
  279. /* Commit the changes to s->timer_reload, so they can propagate. */
  280. ptimer_transaction_commit(s->timer_reload);
  281. /* Update the compare timer based on the committed reload timer value. */
  282. imx_epit_update_compare_timer(s);
  283. ptimer_transaction_commit(s->timer_cmp);
  284. }
  285. static void imx_epit_write_cmp(IMXEPITState *s, uint32_t value)
  286. {
  287. s->cmp = value;
  288. /* Update the compare timer based on the committed reload timer value. */
  289. ptimer_transaction_begin(s->timer_cmp);
  290. imx_epit_update_compare_timer(s);
  291. ptimer_transaction_commit(s->timer_cmp);
  292. }
  293. static void imx_epit_write(void *opaque, hwaddr offset, uint64_t value,
  294. unsigned size)
  295. {
  296. IMXEPITState *s = IMX_EPIT(opaque);
  297. DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(offset >> 2),
  298. (uint32_t)value);
  299. switch (offset >> 2) {
  300. case 0: /* CR */
  301. imx_epit_write_cr(s, (uint32_t)value);
  302. break;
  303. case 1: /* SR */
  304. imx_epit_write_sr(s, (uint32_t)value);
  305. break;
  306. case 2: /* LR */
  307. imx_epit_write_lr(s, (uint32_t)value);
  308. break;
  309. case 3: /* CMP */
  310. imx_epit_write_cmp(s, (uint32_t)value);
  311. break;
  312. default:
  313. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  314. HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset);
  315. break;
  316. }
  317. }
  318. static void imx_epit_cmp(void *opaque)
  319. {
  320. IMXEPITState *s = IMX_EPIT(opaque);
  321. /* The cmp ptimer can't be running when the peripheral is disabled */
  322. assert(s->cr & CR_EN);
  323. DPRINTF("sr was %d\n", s->sr);
  324. /* Set interrupt status bit SR.OCIF and update the interrupt state */
  325. s->sr |= SR_OCIF;
  326. imx_epit_update_int(s);
  327. }
  328. static void imx_epit_reload(void *opaque)
  329. {
  330. /* No action required on rollover of timer_reload */
  331. }
  332. static const MemoryRegionOps imx_epit_ops = {
  333. .read = imx_epit_read,
  334. .write = imx_epit_write,
  335. .endianness = DEVICE_NATIVE_ENDIAN,
  336. };
  337. static const VMStateDescription vmstate_imx_timer_epit = {
  338. .name = TYPE_IMX_EPIT,
  339. .version_id = 3,
  340. .minimum_version_id = 3,
  341. .fields = (VMStateField[]) {
  342. VMSTATE_UINT32(cr, IMXEPITState),
  343. VMSTATE_UINT32(sr, IMXEPITState),
  344. VMSTATE_UINT32(lr, IMXEPITState),
  345. VMSTATE_UINT32(cmp, IMXEPITState),
  346. VMSTATE_PTIMER(timer_reload, IMXEPITState),
  347. VMSTATE_PTIMER(timer_cmp, IMXEPITState),
  348. VMSTATE_END_OF_LIST()
  349. }
  350. };
  351. static void imx_epit_realize(DeviceState *dev, Error **errp)
  352. {
  353. IMXEPITState *s = IMX_EPIT(dev);
  354. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  355. DPRINTF("\n");
  356. sysbus_init_irq(sbd, &s->irq);
  357. memory_region_init_io(&s->iomem, OBJECT(s), &imx_epit_ops, s, TYPE_IMX_EPIT,
  358. 0x00001000);
  359. sysbus_init_mmio(sbd, &s->iomem);
  360. /*
  361. * The reload timer keeps running when the peripheral is enabled. It is a
  362. * kind of wall clock that does not generate any interrupts. The callback
  363. * needs to be provided, but it does nothing as the ptimer already supports
  364. * all necessary reloading functionality.
  365. */
  366. s->timer_reload = ptimer_init(imx_epit_reload, s, PTIMER_POLICY_LEGACY);
  367. /*
  368. * The compare timer is running only when the peripheral configuration is
  369. * in a state that will generate compare interrupts.
  370. */
  371. s->timer_cmp = ptimer_init(imx_epit_cmp, s, PTIMER_POLICY_LEGACY);
  372. }
  373. static void imx_epit_dev_reset(DeviceState *dev)
  374. {
  375. IMXEPITState *s = IMX_EPIT(dev);
  376. imx_epit_reset(s, true);
  377. }
  378. static void imx_epit_class_init(ObjectClass *klass, void *data)
  379. {
  380. DeviceClass *dc = DEVICE_CLASS(klass);
  381. dc->realize = imx_epit_realize;
  382. dc->reset = imx_epit_dev_reset;
  383. dc->vmsd = &vmstate_imx_timer_epit;
  384. dc->desc = "i.MX periodic timer";
  385. }
  386. static const TypeInfo imx_epit_info = {
  387. .name = TYPE_IMX_EPIT,
  388. .parent = TYPE_SYS_BUS_DEVICE,
  389. .instance_size = sizeof(IMXEPITState),
  390. .class_init = imx_epit_class_init,
  391. };
  392. static void imx_epit_register_types(void)
  393. {
  394. type_register_static(&imx_epit_info);
  395. }
  396. type_init(imx_epit_register_types)