imx_gpt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * IMX GPT 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_gpt.h"
  16. #include "hw/misc/imx_ccm.h"
  17. #include "qemu/main-loop.h"
  18. #include "qemu/log.h"
  19. #ifndef DEBUG_IMX_GPT
  20. #define DEBUG_IMX_GPT 0
  21. #endif
  22. #define DPRINTF(fmt, args...) \
  23. do { \
  24. if (DEBUG_IMX_GPT) { \
  25. fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPT, \
  26. __func__, ##args); \
  27. } \
  28. } while (0)
  29. static char const *imx_gpt_reg_name(uint32_t reg)
  30. {
  31. switch (reg) {
  32. case 0:
  33. return "CR";
  34. case 1:
  35. return "PR";
  36. case 2:
  37. return "SR";
  38. case 3:
  39. return "IR";
  40. case 4:
  41. return "OCR1";
  42. case 5:
  43. return "OCR2";
  44. case 6:
  45. return "OCR3";
  46. case 7:
  47. return "ICR1";
  48. case 8:
  49. return "ICR2";
  50. case 9:
  51. return "CNT";
  52. default:
  53. return "[?]";
  54. }
  55. }
  56. static const VMStateDescription vmstate_imx_timer_gpt = {
  57. .name = TYPE_IMX_GPT,
  58. .version_id = 3,
  59. .minimum_version_id = 3,
  60. .fields = (VMStateField[]) {
  61. VMSTATE_UINT32(cr, IMXGPTState),
  62. VMSTATE_UINT32(pr, IMXGPTState),
  63. VMSTATE_UINT32(sr, IMXGPTState),
  64. VMSTATE_UINT32(ir, IMXGPTState),
  65. VMSTATE_UINT32(ocr1, IMXGPTState),
  66. VMSTATE_UINT32(ocr2, IMXGPTState),
  67. VMSTATE_UINT32(ocr3, IMXGPTState),
  68. VMSTATE_UINT32(icr1, IMXGPTState),
  69. VMSTATE_UINT32(icr2, IMXGPTState),
  70. VMSTATE_UINT32(cnt, IMXGPTState),
  71. VMSTATE_UINT32(next_timeout, IMXGPTState),
  72. VMSTATE_UINT32(next_int, IMXGPTState),
  73. VMSTATE_UINT32(freq, IMXGPTState),
  74. VMSTATE_PTIMER(timer, IMXGPTState),
  75. VMSTATE_END_OF_LIST()
  76. }
  77. };
  78. static const IMXClk imx_gpt_clocks[] = {
  79. CLK_NONE, /* 000 No clock source */
  80. CLK_IPG, /* 001 ipg_clk, 532MHz*/
  81. CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
  82. CLK_NONE, /* 011 not defined */
  83. CLK_32k, /* 100 ipg_clk_32k */
  84. CLK_NONE, /* 101 not defined */
  85. CLK_NONE, /* 110 not defined */
  86. CLK_NONE, /* 111 not defined */
  87. };
  88. static void imx_gpt_set_freq(IMXGPTState *s)
  89. {
  90. uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
  91. s->freq = imx_ccm_get_clock_frequency(s->ccm,
  92. imx_gpt_clocks[clksrc]) / (1 + s->pr);
  93. DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq);
  94. if (s->freq) {
  95. ptimer_set_freq(s->timer, s->freq);
  96. }
  97. }
  98. static void imx_gpt_update_int(IMXGPTState *s)
  99. {
  100. if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
  101. qemu_irq_raise(s->irq);
  102. } else {
  103. qemu_irq_lower(s->irq);
  104. }
  105. }
  106. static uint32_t imx_gpt_update_count(IMXGPTState *s)
  107. {
  108. s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
  109. return s->cnt;
  110. }
  111. static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
  112. uint32_t timeout)
  113. {
  114. if ((count < reg) && (timeout > reg)) {
  115. timeout = reg;
  116. }
  117. return timeout;
  118. }
  119. static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
  120. {
  121. uint32_t timeout = GPT_TIMER_MAX;
  122. uint32_t count;
  123. long long limit;
  124. if (!(s->cr & GPT_CR_EN)) {
  125. /* if not enabled just return */
  126. return;
  127. }
  128. /* update the count */
  129. count = imx_gpt_update_count(s);
  130. if (event) {
  131. /*
  132. * This is an event (the ptimer reached 0 and stopped), and the
  133. * timer counter is now equal to s->next_timeout.
  134. */
  135. if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) {
  136. /* We are in restart mode and we crossed the compare channel 1
  137. * value. We need to reset the counter to 0.
  138. */
  139. count = s->cnt = s->next_timeout = 0;
  140. } else if (count == GPT_TIMER_MAX) {
  141. /* We reached GPT_TIMER_MAX so we need to rollover */
  142. count = s->cnt = s->next_timeout = 0;
  143. }
  144. }
  145. /* now, find the next timeout related to count */
  146. if (s->ir & GPT_IR_OF1IE) {
  147. timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
  148. }
  149. if (s->ir & GPT_IR_OF2IE) {
  150. timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
  151. }
  152. if (s->ir & GPT_IR_OF3IE) {
  153. timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
  154. }
  155. /* find the next set of interrupts to raise for next timer event */
  156. s->next_int = 0;
  157. if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
  158. s->next_int |= GPT_SR_OF1;
  159. }
  160. if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
  161. s->next_int |= GPT_SR_OF2;
  162. }
  163. if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
  164. s->next_int |= GPT_SR_OF3;
  165. }
  166. if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) {
  167. s->next_int |= GPT_SR_ROV;
  168. }
  169. /* the new range to count down from */
  170. limit = timeout - imx_gpt_update_count(s);
  171. if (limit < 0) {
  172. /*
  173. * if we reach here, then QEMU is running too slow and we pass the
  174. * timeout limit while computing it. Let's deliver the interrupt
  175. * and compute a new limit.
  176. */
  177. s->sr |= s->next_int;
  178. imx_gpt_compute_next_timeout(s, event);
  179. imx_gpt_update_int(s);
  180. } else {
  181. /* New timeout value */
  182. s->next_timeout = timeout;
  183. /* reset the limit to the computed range */
  184. ptimer_set_limit(s->timer, limit, 1);
  185. }
  186. }
  187. static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
  188. {
  189. IMXGPTState *s = IMX_GPT(opaque);
  190. uint32_t reg_value = 0;
  191. switch (offset >> 2) {
  192. case 0: /* Control Register */
  193. reg_value = s->cr;
  194. break;
  195. case 1: /* prescaler */
  196. reg_value = s->pr;
  197. break;
  198. case 2: /* Status Register */
  199. reg_value = s->sr;
  200. break;
  201. case 3: /* Interrupt Register */
  202. reg_value = s->ir;
  203. break;
  204. case 4: /* Output Compare Register 1 */
  205. reg_value = s->ocr1;
  206. break;
  207. case 5: /* Output Compare Register 2 */
  208. reg_value = s->ocr2;
  209. break;
  210. case 6: /* Output Compare Register 3 */
  211. reg_value = s->ocr3;
  212. break;
  213. case 7: /* input Capture Register 1 */
  214. qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n",
  215. TYPE_IMX_GPT, __func__);
  216. reg_value = s->icr1;
  217. break;
  218. case 8: /* input Capture Register 2 */
  219. qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n",
  220. TYPE_IMX_GPT, __func__);
  221. reg_value = s->icr2;
  222. break;
  223. case 9: /* cnt */
  224. imx_gpt_update_count(s);
  225. reg_value = s->cnt;
  226. break;
  227. default:
  228. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  229. HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
  230. break;
  231. }
  232. DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value);
  233. return reg_value;
  234. }
  235. static void imx_gpt_reset(DeviceState *dev)
  236. {
  237. IMXGPTState *s = IMX_GPT(dev);
  238. /* stop timer */
  239. ptimer_stop(s->timer);
  240. /*
  241. * Soft reset doesn't touch some bits; hard reset clears them
  242. */
  243. s->cr &= ~(GPT_CR_EN|GPT_CR_ENMOD|GPT_CR_STOPEN|GPT_CR_DOZEN|
  244. GPT_CR_WAITEN|GPT_CR_DBGEN);
  245. s->sr = 0;
  246. s->pr = 0;
  247. s->ir = 0;
  248. s->cnt = 0;
  249. s->ocr1 = GPT_TIMER_MAX;
  250. s->ocr2 = GPT_TIMER_MAX;
  251. s->ocr3 = GPT_TIMER_MAX;
  252. s->icr1 = 0;
  253. s->icr2 = 0;
  254. s->next_timeout = GPT_TIMER_MAX;
  255. s->next_int = 0;
  256. /* compute new freq */
  257. imx_gpt_set_freq(s);
  258. /* reset the limit to GPT_TIMER_MAX */
  259. ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
  260. /* if the timer is still enabled, restart it */
  261. if (s->freq && (s->cr & GPT_CR_EN)) {
  262. ptimer_run(s->timer, 1);
  263. }
  264. }
  265. static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
  266. unsigned size)
  267. {
  268. IMXGPTState *s = IMX_GPT(opaque);
  269. uint32_t oldreg;
  270. DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2),
  271. (uint32_t)value);
  272. switch (offset >> 2) {
  273. case 0:
  274. oldreg = s->cr;
  275. s->cr = value & ~0x7c14;
  276. if (s->cr & GPT_CR_SWR) { /* force reset */
  277. /* handle the reset */
  278. imx_gpt_reset(DEVICE(s));
  279. } else {
  280. /* set our freq, as the source might have changed */
  281. imx_gpt_set_freq(s);
  282. if ((oldreg ^ s->cr) & GPT_CR_EN) {
  283. if (s->cr & GPT_CR_EN) {
  284. if (s->cr & GPT_CR_ENMOD) {
  285. s->next_timeout = GPT_TIMER_MAX;
  286. ptimer_set_count(s->timer, GPT_TIMER_MAX);
  287. imx_gpt_compute_next_timeout(s, false);
  288. }
  289. ptimer_run(s->timer, 1);
  290. } else {
  291. /* stop timer */
  292. ptimer_stop(s->timer);
  293. }
  294. }
  295. }
  296. break;
  297. case 1: /* Prescaler */
  298. s->pr = value & 0xfff;
  299. imx_gpt_set_freq(s);
  300. break;
  301. case 2: /* SR */
  302. s->sr &= ~(value & 0x3f);
  303. imx_gpt_update_int(s);
  304. break;
  305. case 3: /* IR -- interrupt register */
  306. s->ir = value & 0x3f;
  307. imx_gpt_update_int(s);
  308. imx_gpt_compute_next_timeout(s, false);
  309. break;
  310. case 4: /* OCR1 -- output compare register */
  311. s->ocr1 = value;
  312. /* In non-freerun mode, reset count when this register is written */
  313. if (!(s->cr & GPT_CR_FRR)) {
  314. s->next_timeout = GPT_TIMER_MAX;
  315. ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
  316. }
  317. /* compute the new timeout */
  318. imx_gpt_compute_next_timeout(s, false);
  319. break;
  320. case 5: /* OCR2 -- output compare register */
  321. s->ocr2 = value;
  322. /* compute the new timeout */
  323. imx_gpt_compute_next_timeout(s, false);
  324. break;
  325. case 6: /* OCR3 -- output compare register */
  326. s->ocr3 = value;
  327. /* compute the new timeout */
  328. imx_gpt_compute_next_timeout(s, false);
  329. break;
  330. default:
  331. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  332. HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
  333. break;
  334. }
  335. }
  336. static void imx_gpt_timeout(void *opaque)
  337. {
  338. IMXGPTState *s = IMX_GPT(opaque);
  339. DPRINTF("\n");
  340. s->sr |= s->next_int;
  341. s->next_int = 0;
  342. imx_gpt_compute_next_timeout(s, true);
  343. imx_gpt_update_int(s);
  344. if (s->freq && (s->cr & GPT_CR_EN)) {
  345. ptimer_run(s->timer, 1);
  346. }
  347. }
  348. static const MemoryRegionOps imx_gpt_ops = {
  349. .read = imx_gpt_read,
  350. .write = imx_gpt_write,
  351. .endianness = DEVICE_NATIVE_ENDIAN,
  352. };
  353. static void imx_gpt_realize(DeviceState *dev, Error **errp)
  354. {
  355. IMXGPTState *s = IMX_GPT(dev);
  356. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  357. QEMUBH *bh;
  358. sysbus_init_irq(sbd, &s->irq);
  359. memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
  360. 0x00001000);
  361. sysbus_init_mmio(sbd, &s->iomem);
  362. bh = qemu_bh_new(imx_gpt_timeout, s);
  363. s->timer = ptimer_init(bh);
  364. }
  365. static void imx_gpt_class_init(ObjectClass *klass, void *data)
  366. {
  367. DeviceClass *dc = DEVICE_CLASS(klass);
  368. dc->realize = imx_gpt_realize;
  369. dc->reset = imx_gpt_reset;
  370. dc->vmsd = &vmstate_imx_timer_gpt;
  371. dc->desc = "i.MX general timer";
  372. }
  373. static const TypeInfo imx_gpt_info = {
  374. .name = TYPE_IMX_GPT,
  375. .parent = TYPE_SYS_BUS_DEVICE,
  376. .instance_size = sizeof(IMXGPTState),
  377. .class_init = imx_gpt_class_init,
  378. };
  379. static void imx_gpt_register_types(void)
  380. {
  381. type_register_static(&imx_gpt_info);
  382. }
  383. type_init(imx_gpt_register_types)