2
0

imx_gpt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 imx6ul_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_NONE, /* 101 not defined */
  116. CLK_NONE, /* 110 not defined */
  117. CLK_NONE, /* 111 not defined */
  118. };
  119. static const IMXClk imx7_gpt_clocks[] = {
  120. CLK_NONE, /* 000 No clock source */
  121. CLK_IPG, /* 001 ipg_clk, 532MHz*/
  122. CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
  123. CLK_EXT, /* 011 External clock */
  124. CLK_32k, /* 100 ipg_clk_32k */
  125. CLK_HIGH, /* 101 reference clock */
  126. CLK_NONE, /* 110 not defined */
  127. CLK_NONE, /* 111 not defined */
  128. };
  129. /* Must be called from within ptimer_transaction_begin/commit block */
  130. static void imx_gpt_set_freq(IMXGPTState *s)
  131. {
  132. uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
  133. s->freq = imx_ccm_get_clock_frequency(s->ccm,
  134. s->clocks[clksrc]) / (1 + s->pr);
  135. DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq);
  136. if (s->freq) {
  137. ptimer_set_freq(s->timer, s->freq);
  138. }
  139. }
  140. static void imx_gpt_update_int(IMXGPTState *s)
  141. {
  142. if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
  143. qemu_irq_raise(s->irq);
  144. } else {
  145. qemu_irq_lower(s->irq);
  146. }
  147. }
  148. static uint32_t imx_gpt_update_count(IMXGPTState *s)
  149. {
  150. s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
  151. return s->cnt;
  152. }
  153. static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
  154. uint32_t timeout)
  155. {
  156. if ((count < reg) && (timeout > reg)) {
  157. timeout = reg;
  158. }
  159. return timeout;
  160. }
  161. /* Must be called from within ptimer_transaction_begin/commit block */
  162. static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
  163. {
  164. uint32_t timeout = GPT_TIMER_MAX;
  165. uint32_t count;
  166. long long limit;
  167. if (!(s->cr & GPT_CR_EN)) {
  168. /* if not enabled just return */
  169. return;
  170. }
  171. /* update the count */
  172. count = imx_gpt_update_count(s);
  173. if (event) {
  174. /*
  175. * This is an event (the ptimer reached 0 and stopped), and the
  176. * timer counter is now equal to s->next_timeout.
  177. */
  178. if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) {
  179. /* We are in restart mode and we crossed the compare channel 1
  180. * value. We need to reset the counter to 0.
  181. */
  182. count = s->cnt = s->next_timeout = 0;
  183. } else if (count == GPT_TIMER_MAX) {
  184. /* We reached GPT_TIMER_MAX so we need to rollover */
  185. count = s->cnt = s->next_timeout = 0;
  186. }
  187. }
  188. /* now, find the next timeout related to count */
  189. if (s->ir & GPT_IR_OF1IE) {
  190. timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
  191. }
  192. if (s->ir & GPT_IR_OF2IE) {
  193. timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
  194. }
  195. if (s->ir & GPT_IR_OF3IE) {
  196. timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
  197. }
  198. /* find the next set of interrupts to raise for next timer event */
  199. s->next_int = 0;
  200. if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
  201. s->next_int |= GPT_SR_OF1;
  202. }
  203. if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
  204. s->next_int |= GPT_SR_OF2;
  205. }
  206. if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
  207. s->next_int |= GPT_SR_OF3;
  208. }
  209. if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) {
  210. s->next_int |= GPT_SR_ROV;
  211. }
  212. /* the new range to count down from */
  213. limit = timeout - imx_gpt_update_count(s);
  214. if (limit < 0) {
  215. /*
  216. * if we reach here, then QEMU is running too slow and we pass the
  217. * timeout limit while computing it. Let's deliver the interrupt
  218. * and compute a new limit.
  219. */
  220. s->sr |= s->next_int;
  221. imx_gpt_compute_next_timeout(s, event);
  222. imx_gpt_update_int(s);
  223. } else {
  224. /* New timeout value */
  225. s->next_timeout = timeout;
  226. /* reset the limit to the computed range */
  227. ptimer_set_limit(s->timer, limit, 1);
  228. }
  229. }
  230. static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
  231. {
  232. IMXGPTState *s = IMX_GPT(opaque);
  233. uint32_t reg_value = 0;
  234. switch (offset >> 2) {
  235. case 0: /* Control Register */
  236. reg_value = s->cr;
  237. break;
  238. case 1: /* prescaler */
  239. reg_value = s->pr;
  240. break;
  241. case 2: /* Status Register */
  242. reg_value = s->sr;
  243. break;
  244. case 3: /* Interrupt Register */
  245. reg_value = s->ir;
  246. break;
  247. case 4: /* Output Compare Register 1 */
  248. reg_value = s->ocr1;
  249. break;
  250. case 5: /* Output Compare Register 2 */
  251. reg_value = s->ocr2;
  252. break;
  253. case 6: /* Output Compare Register 3 */
  254. reg_value = s->ocr3;
  255. break;
  256. case 7: /* input Capture Register 1 */
  257. qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n",
  258. TYPE_IMX_GPT, __func__);
  259. reg_value = s->icr1;
  260. break;
  261. case 8: /* input Capture Register 2 */
  262. qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n",
  263. TYPE_IMX_GPT, __func__);
  264. reg_value = s->icr2;
  265. break;
  266. case 9: /* cnt */
  267. imx_gpt_update_count(s);
  268. reg_value = s->cnt;
  269. break;
  270. default:
  271. qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
  272. HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
  273. break;
  274. }
  275. DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value);
  276. return reg_value;
  277. }
  278. static void imx_gpt_reset_common(IMXGPTState *s, bool is_soft_reset)
  279. {
  280. ptimer_transaction_begin(s->timer);
  281. /* stop timer */
  282. ptimer_stop(s->timer);
  283. /* Soft reset and hard reset differ only in their handling of the CR
  284. * register -- soft reset preserves the values of some bits there.
  285. */
  286. if (is_soft_reset) {
  287. /* Clear all CR bits except those that are preserved by soft reset. */
  288. s->cr &= GPT_CR_EN | GPT_CR_ENMOD | GPT_CR_STOPEN | GPT_CR_DOZEN |
  289. GPT_CR_WAITEN | GPT_CR_DBGEN |
  290. (GPT_CR_CLKSRC_MASK << GPT_CR_CLKSRC_SHIFT);
  291. } else {
  292. s->cr = 0;
  293. }
  294. s->sr = 0;
  295. s->pr = 0;
  296. s->ir = 0;
  297. s->cnt = 0;
  298. s->ocr1 = GPT_TIMER_MAX;
  299. s->ocr2 = GPT_TIMER_MAX;
  300. s->ocr3 = GPT_TIMER_MAX;
  301. s->icr1 = 0;
  302. s->icr2 = 0;
  303. s->next_timeout = GPT_TIMER_MAX;
  304. s->next_int = 0;
  305. /* compute new freq */
  306. imx_gpt_set_freq(s);
  307. /* reset the limit to GPT_TIMER_MAX */
  308. ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
  309. /* if the timer is still enabled, restart it */
  310. if (s->freq && (s->cr & GPT_CR_EN)) {
  311. ptimer_run(s->timer, 1);
  312. }
  313. ptimer_transaction_commit(s->timer);
  314. }
  315. static void imx_gpt_soft_reset(DeviceState *dev)
  316. {
  317. IMXGPTState *s = IMX_GPT(dev);
  318. imx_gpt_reset_common(s, true);
  319. }
  320. static void imx_gpt_reset(DeviceState *dev)
  321. {
  322. IMXGPTState *s = IMX_GPT(dev);
  323. imx_gpt_reset_common(s, false);
  324. }
  325. static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
  326. unsigned size)
  327. {
  328. IMXGPTState *s = IMX_GPT(opaque);
  329. uint32_t oldreg;
  330. DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2),
  331. (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. DPRINTF("\n");
  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. dc->reset = 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 const TypeInfo imx25_gpt_info = {
  469. .name = TYPE_IMX25_GPT,
  470. .parent = TYPE_SYS_BUS_DEVICE,
  471. .instance_size = sizeof(IMXGPTState),
  472. .instance_init = imx25_gpt_init,
  473. .class_init = imx_gpt_class_init,
  474. };
  475. static const TypeInfo imx31_gpt_info = {
  476. .name = TYPE_IMX31_GPT,
  477. .parent = TYPE_IMX25_GPT,
  478. .instance_init = imx31_gpt_init,
  479. };
  480. static const TypeInfo imx6_gpt_info = {
  481. .name = TYPE_IMX6_GPT,
  482. .parent = TYPE_IMX25_GPT,
  483. .instance_init = imx6_gpt_init,
  484. };
  485. static const TypeInfo imx6ul_gpt_info = {
  486. .name = TYPE_IMX6UL_GPT,
  487. .parent = TYPE_IMX25_GPT,
  488. .instance_init = imx6ul_gpt_init,
  489. };
  490. static const TypeInfo imx7_gpt_info = {
  491. .name = TYPE_IMX7_GPT,
  492. .parent = TYPE_IMX25_GPT,
  493. .instance_init = imx7_gpt_init,
  494. };
  495. static void imx_gpt_register_types(void)
  496. {
  497. type_register_static(&imx25_gpt_info);
  498. type_register_static(&imx31_gpt_info);
  499. type_register_static(&imx6_gpt_info);
  500. type_register_static(&imx6ul_gpt_info);
  501. type_register_static(&imx7_gpt_info);
  502. }
  503. type_init(imx_gpt_register_types)