imx_gpt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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
  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 "hw/hw.h"
  15. #include "qemu/bitops.h"
  16. #include "qemu/timer.h"
  17. #include "hw/ptimer.h"
  18. #include "hw/sysbus.h"
  19. #include "hw/arm/imx.h"
  20. #include "qemu/main-loop.h"
  21. #define TYPE_IMX_GPT "imx.gpt"
  22. /*
  23. * Define to 1 for debug messages
  24. */
  25. #define DEBUG_TIMER 0
  26. #if DEBUG_TIMER
  27. static char const *imx_gpt_reg_name(uint32_t reg)
  28. {
  29. switch (reg) {
  30. case 0:
  31. return "CR";
  32. case 1:
  33. return "PR";
  34. case 2:
  35. return "SR";
  36. case 3:
  37. return "IR";
  38. case 4:
  39. return "OCR1";
  40. case 5:
  41. return "OCR2";
  42. case 6:
  43. return "OCR3";
  44. case 7:
  45. return "ICR1";
  46. case 8:
  47. return "ICR2";
  48. case 9:
  49. return "CNT";
  50. default:
  51. return "[?]";
  52. }
  53. }
  54. # define DPRINTF(fmt, args...) \
  55. do { printf("%s: " fmt , __func__, ##args); } while (0)
  56. #else
  57. # define DPRINTF(fmt, args...) do {} while (0)
  58. #endif
  59. /*
  60. * Define to 1 for messages about attempts to
  61. * access unimplemented registers or similar.
  62. */
  63. #define DEBUG_IMPLEMENTATION 1
  64. #if DEBUG_IMPLEMENTATION
  65. # define IPRINTF(fmt, args...) \
  66. do { fprintf(stderr, "%s: " fmt, __func__, ##args); } while (0)
  67. #else
  68. # define IPRINTF(fmt, args...) do {} while (0)
  69. #endif
  70. #define IMX_GPT(obj) \
  71. OBJECT_CHECK(IMXGPTState, (obj), TYPE_IMX_GPT)
  72. /*
  73. * GPT : General purpose timer
  74. *
  75. * This timer counts up continuously while it is enabled, resetting itself
  76. * to 0 when it reaches TIMER_MAX (in freerun mode) or when it
  77. * reaches the value of one of the ocrX (in periodic mode).
  78. */
  79. #define TIMER_MAX 0XFFFFFFFFUL
  80. /* Control register. Not all of these bits have any effect (yet) */
  81. #define GPT_CR_EN (1 << 0) /* GPT Enable */
  82. #define GPT_CR_ENMOD (1 << 1) /* GPT Enable Mode */
  83. #define GPT_CR_DBGEN (1 << 2) /* GPT Debug mode enable */
  84. #define GPT_CR_WAITEN (1 << 3) /* GPT Wait Mode Enable */
  85. #define GPT_CR_DOZEN (1 << 4) /* GPT Doze mode enable */
  86. #define GPT_CR_STOPEN (1 << 5) /* GPT Stop Mode Enable */
  87. #define GPT_CR_CLKSRC_SHIFT (6)
  88. #define GPT_CR_CLKSRC_MASK (0x7)
  89. #define GPT_CR_FRR (1 << 9) /* Freerun or Restart */
  90. #define GPT_CR_SWR (1 << 15) /* Software Reset */
  91. #define GPT_CR_IM1 (3 << 16) /* Input capture channel 1 mode (2 bits) */
  92. #define GPT_CR_IM2 (3 << 18) /* Input capture channel 2 mode (2 bits) */
  93. #define GPT_CR_OM1 (7 << 20) /* Output Compare Channel 1 Mode (3 bits) */
  94. #define GPT_CR_OM2 (7 << 23) /* Output Compare Channel 2 Mode (3 bits) */
  95. #define GPT_CR_OM3 (7 << 26) /* Output Compare Channel 3 Mode (3 bits) */
  96. #define GPT_CR_FO1 (1 << 29) /* Force Output Compare Channel 1 */
  97. #define GPT_CR_FO2 (1 << 30) /* Force Output Compare Channel 2 */
  98. #define GPT_CR_FO3 (1 << 31) /* Force Output Compare Channel 3 */
  99. #define GPT_SR_OF1 (1 << 0)
  100. #define GPT_SR_OF2 (1 << 1)
  101. #define GPT_SR_OF3 (1 << 2)
  102. #define GPT_SR_ROV (1 << 5)
  103. #define GPT_IR_OF1IE (1 << 0)
  104. #define GPT_IR_OF2IE (1 << 1)
  105. #define GPT_IR_OF3IE (1 << 2)
  106. #define GPT_IR_ROVIE (1 << 5)
  107. typedef struct {
  108. SysBusDevice busdev;
  109. ptimer_state *timer;
  110. MemoryRegion iomem;
  111. DeviceState *ccm;
  112. uint32_t cr;
  113. uint32_t pr;
  114. uint32_t sr;
  115. uint32_t ir;
  116. uint32_t ocr1;
  117. uint32_t ocr2;
  118. uint32_t ocr3;
  119. uint32_t icr1;
  120. uint32_t icr2;
  121. uint32_t cnt;
  122. uint32_t next_timeout;
  123. uint32_t next_int;
  124. uint32_t freq;
  125. qemu_irq irq;
  126. } IMXGPTState;
  127. static const VMStateDescription vmstate_imx_timer_gpt = {
  128. .name = "imx.gpt",
  129. .version_id = 3,
  130. .minimum_version_id = 3,
  131. .fields = (VMStateField[]) {
  132. VMSTATE_UINT32(cr, IMXGPTState),
  133. VMSTATE_UINT32(pr, IMXGPTState),
  134. VMSTATE_UINT32(sr, IMXGPTState),
  135. VMSTATE_UINT32(ir, IMXGPTState),
  136. VMSTATE_UINT32(ocr1, IMXGPTState),
  137. VMSTATE_UINT32(ocr2, IMXGPTState),
  138. VMSTATE_UINT32(ocr3, IMXGPTState),
  139. VMSTATE_UINT32(icr1, IMXGPTState),
  140. VMSTATE_UINT32(icr2, IMXGPTState),
  141. VMSTATE_UINT32(cnt, IMXGPTState),
  142. VMSTATE_UINT32(next_timeout, IMXGPTState),
  143. VMSTATE_UINT32(next_int, IMXGPTState),
  144. VMSTATE_UINT32(freq, IMXGPTState),
  145. VMSTATE_PTIMER(timer, IMXGPTState),
  146. VMSTATE_END_OF_LIST()
  147. }
  148. };
  149. static const IMXClk imx_gpt_clocks[] = {
  150. NOCLK, /* 000 No clock source */
  151. IPG, /* 001 ipg_clk, 532MHz*/
  152. IPG, /* 010 ipg_clk_highfreq */
  153. NOCLK, /* 011 not defined */
  154. CLK_32k, /* 100 ipg_clk_32k */
  155. NOCLK, /* 101 not defined */
  156. NOCLK, /* 110 not defined */
  157. NOCLK, /* 111 not defined */
  158. };
  159. static void imx_gpt_set_freq(IMXGPTState *s)
  160. {
  161. uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
  162. uint32_t freq = imx_clock_frequency(s->ccm, imx_gpt_clocks[clksrc])
  163. / (1 + s->pr);
  164. s->freq = freq;
  165. DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, freq);
  166. if (freq) {
  167. ptimer_set_freq(s->timer, freq);
  168. }
  169. }
  170. static void imx_gpt_update_int(IMXGPTState *s)
  171. {
  172. if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
  173. qemu_irq_raise(s->irq);
  174. } else {
  175. qemu_irq_lower(s->irq);
  176. }
  177. }
  178. static uint32_t imx_gpt_update_count(IMXGPTState *s)
  179. {
  180. s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
  181. return s->cnt;
  182. }
  183. static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
  184. uint32_t timeout)
  185. {
  186. if ((count < reg) && (timeout > reg)) {
  187. timeout = reg;
  188. }
  189. return timeout;
  190. }
  191. static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
  192. {
  193. uint32_t timeout = TIMER_MAX;
  194. uint32_t count = 0;
  195. long long limit;
  196. if (!(s->cr & GPT_CR_EN)) {
  197. /* if not enabled just return */
  198. return;
  199. }
  200. if (event) {
  201. /* This is a timer event */
  202. if ((s->cr & GPT_CR_FRR) && (s->next_timeout != TIMER_MAX)) {
  203. /*
  204. * if we are in free running mode and we have not reached
  205. * the TIMER_MAX limit, then update the count
  206. */
  207. count = imx_gpt_update_count(s);
  208. }
  209. } else {
  210. /* not a timer event, then just update the count */
  211. count = imx_gpt_update_count(s);
  212. }
  213. /* now, find the next timeout related to count */
  214. if (s->ir & GPT_IR_OF1IE) {
  215. timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
  216. }
  217. if (s->ir & GPT_IR_OF2IE) {
  218. timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
  219. }
  220. if (s->ir & GPT_IR_OF3IE) {
  221. timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
  222. }
  223. /* find the next set of interrupts to raise for next timer event */
  224. s->next_int = 0;
  225. if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
  226. s->next_int |= GPT_SR_OF1;
  227. }
  228. if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
  229. s->next_int |= GPT_SR_OF2;
  230. }
  231. if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
  232. s->next_int |= GPT_SR_OF3;
  233. }
  234. if ((s->ir & GPT_IR_ROVIE) && (timeout == TIMER_MAX)) {
  235. s->next_int |= GPT_SR_ROV;
  236. }
  237. /* the new range to count down from */
  238. limit = timeout - imx_gpt_update_count(s);
  239. if (limit < 0) {
  240. /*
  241. * if we reach here, then QEMU is running too slow and we pass the
  242. * timeout limit while computing it. Let's deliver the interrupt
  243. * and compute a new limit.
  244. */
  245. s->sr |= s->next_int;
  246. imx_gpt_compute_next_timeout(s, event);
  247. imx_gpt_update_int(s);
  248. } else {
  249. /* New timeout value */
  250. s->next_timeout = timeout;
  251. /* reset the limit to the computed range */
  252. ptimer_set_limit(s->timer, limit, 1);
  253. }
  254. }
  255. static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
  256. {
  257. IMXGPTState *s = IMX_GPT(opaque);
  258. uint32_t reg_value = 0;
  259. uint32_t reg = offset >> 2;
  260. switch (reg) {
  261. case 0: /* Control Register */
  262. reg_value = s->cr;
  263. break;
  264. case 1: /* prescaler */
  265. reg_value = s->pr;
  266. break;
  267. case 2: /* Status Register */
  268. reg_value = s->sr;
  269. break;
  270. case 3: /* Interrupt Register */
  271. reg_value = s->ir;
  272. break;
  273. case 4: /* Output Compare Register 1 */
  274. reg_value = s->ocr1;
  275. break;
  276. case 5: /* Output Compare Register 2 */
  277. reg_value = s->ocr2;
  278. break;
  279. case 6: /* Output Compare Register 3 */
  280. reg_value = s->ocr3;
  281. break;
  282. case 7: /* input Capture Register 1 */
  283. qemu_log_mask(LOG_UNIMP, "icr1 feature is not implemented\n");
  284. reg_value = s->icr1;
  285. break;
  286. case 8: /* input Capture Register 2 */
  287. qemu_log_mask(LOG_UNIMP, "icr2 feature is not implemented\n");
  288. reg_value = s->icr2;
  289. break;
  290. case 9: /* cnt */
  291. imx_gpt_update_count(s);
  292. reg_value = s->cnt;
  293. break;
  294. default:
  295. IPRINTF("Bad offset %x\n", reg);
  296. break;
  297. }
  298. DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(reg), reg_value);
  299. return reg_value;
  300. }
  301. static void imx_gpt_reset(DeviceState *dev)
  302. {
  303. IMXGPTState *s = IMX_GPT(dev);
  304. /* stop timer */
  305. ptimer_stop(s->timer);
  306. /*
  307. * Soft reset doesn't touch some bits; hard reset clears them
  308. */
  309. s->cr &= ~(GPT_CR_EN|GPT_CR_ENMOD|GPT_CR_STOPEN|GPT_CR_DOZEN|
  310. GPT_CR_WAITEN|GPT_CR_DBGEN);
  311. s->sr = 0;
  312. s->pr = 0;
  313. s->ir = 0;
  314. s->cnt = 0;
  315. s->ocr1 = TIMER_MAX;
  316. s->ocr2 = TIMER_MAX;
  317. s->ocr3 = TIMER_MAX;
  318. s->icr1 = 0;
  319. s->icr2 = 0;
  320. s->next_timeout = TIMER_MAX;
  321. s->next_int = 0;
  322. /* compute new freq */
  323. imx_gpt_set_freq(s);
  324. /* reset the limit to TIMER_MAX */
  325. ptimer_set_limit(s->timer, TIMER_MAX, 1);
  326. /* if the timer is still enabled, restart it */
  327. if (s->freq && (s->cr & GPT_CR_EN)) {
  328. ptimer_run(s->timer, 1);
  329. }
  330. }
  331. static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
  332. unsigned size)
  333. {
  334. IMXGPTState *s = IMX_GPT(opaque);
  335. uint32_t oldreg;
  336. uint32_t reg = offset >> 2;
  337. DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(reg),
  338. (uint32_t)value);
  339. switch (reg) {
  340. case 0:
  341. oldreg = s->cr;
  342. s->cr = value & ~0x7c14;
  343. if (s->cr & GPT_CR_SWR) { /* force reset */
  344. /* handle the reset */
  345. imx_gpt_reset(DEVICE(s));
  346. } else {
  347. /* set our freq, as the source might have changed */
  348. imx_gpt_set_freq(s);
  349. if ((oldreg ^ s->cr) & GPT_CR_EN) {
  350. if (s->cr & GPT_CR_EN) {
  351. if (s->cr & GPT_CR_ENMOD) {
  352. s->next_timeout = TIMER_MAX;
  353. ptimer_set_count(s->timer, TIMER_MAX);
  354. imx_gpt_compute_next_timeout(s, false);
  355. }
  356. ptimer_run(s->timer, 1);
  357. } else {
  358. /* stop timer */
  359. ptimer_stop(s->timer);
  360. }
  361. }
  362. }
  363. break;
  364. case 1: /* Prescaler */
  365. s->pr = value & 0xfff;
  366. imx_gpt_set_freq(s);
  367. break;
  368. case 2: /* SR */
  369. s->sr &= ~(value & 0x3f);
  370. imx_gpt_update_int(s);
  371. break;
  372. case 3: /* IR -- interrupt register */
  373. s->ir = value & 0x3f;
  374. imx_gpt_update_int(s);
  375. imx_gpt_compute_next_timeout(s, false);
  376. break;
  377. case 4: /* OCR1 -- output compare register */
  378. s->ocr1 = value;
  379. /* In non-freerun mode, reset count when this register is written */
  380. if (!(s->cr & GPT_CR_FRR)) {
  381. s->next_timeout = TIMER_MAX;
  382. ptimer_set_limit(s->timer, TIMER_MAX, 1);
  383. }
  384. /* compute the new timeout */
  385. imx_gpt_compute_next_timeout(s, false);
  386. break;
  387. case 5: /* OCR2 -- output compare register */
  388. s->ocr2 = value;
  389. /* compute the new timeout */
  390. imx_gpt_compute_next_timeout(s, false);
  391. break;
  392. case 6: /* OCR3 -- output compare register */
  393. s->ocr3 = value;
  394. /* compute the new timeout */
  395. imx_gpt_compute_next_timeout(s, false);
  396. break;
  397. default:
  398. IPRINTF("Bad offset %x\n", reg);
  399. break;
  400. }
  401. }
  402. static void imx_gpt_timeout(void *opaque)
  403. {
  404. IMXGPTState *s = IMX_GPT(opaque);
  405. DPRINTF("\n");
  406. s->sr |= s->next_int;
  407. s->next_int = 0;
  408. imx_gpt_compute_next_timeout(s, true);
  409. imx_gpt_update_int(s);
  410. if (s->freq && (s->cr & GPT_CR_EN)) {
  411. ptimer_run(s->timer, 1);
  412. }
  413. }
  414. static const MemoryRegionOps imx_gpt_ops = {
  415. .read = imx_gpt_read,
  416. .write = imx_gpt_write,
  417. .endianness = DEVICE_NATIVE_ENDIAN,
  418. };
  419. static void imx_gpt_realize(DeviceState *dev, Error **errp)
  420. {
  421. IMXGPTState *s = IMX_GPT(dev);
  422. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  423. QEMUBH *bh;
  424. sysbus_init_irq(sbd, &s->irq);
  425. memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
  426. 0x00001000);
  427. sysbus_init_mmio(sbd, &s->iomem);
  428. bh = qemu_bh_new(imx_gpt_timeout, s);
  429. s->timer = ptimer_init(bh);
  430. }
  431. void imx_timerg_create(const hwaddr addr, qemu_irq irq, DeviceState *ccm)
  432. {
  433. IMXGPTState *pp;
  434. DeviceState *dev;
  435. dev = sysbus_create_simple(TYPE_IMX_GPT, addr, irq);
  436. pp = IMX_GPT(dev);
  437. pp->ccm = ccm;
  438. }
  439. static void imx_gpt_class_init(ObjectClass *klass, void *data)
  440. {
  441. DeviceClass *dc = DEVICE_CLASS(klass);
  442. dc->realize = imx_gpt_realize;
  443. dc->reset = imx_gpt_reset;
  444. dc->vmsd = &vmstate_imx_timer_gpt;
  445. dc->desc = "i.MX general timer";
  446. }
  447. static const TypeInfo imx_gpt_info = {
  448. .name = TYPE_IMX_GPT,
  449. .parent = TYPE_SYS_BUS_DEVICE,
  450. .instance_size = sizeof(IMXGPTState),
  451. .class_init = imx_gpt_class_init,
  452. };
  453. static void imx_gpt_register_types(void)
  454. {
  455. type_register_static(&imx_gpt_info);
  456. }
  457. type_init(imx_gpt_register_types)