imx_gpt.c 16 KB

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