imx_gpt.c 12 KB

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