imx_gpt.c 15 KB

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