2
0

imx_gpt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. .minimum_version_id_old = 3,
  132. .fields = (VMStateField[]) {
  133. VMSTATE_UINT32(cr, IMXGPTState),
  134. VMSTATE_UINT32(pr, IMXGPTState),
  135. VMSTATE_UINT32(sr, IMXGPTState),
  136. VMSTATE_UINT32(ir, IMXGPTState),
  137. VMSTATE_UINT32(ocr1, IMXGPTState),
  138. VMSTATE_UINT32(ocr2, IMXGPTState),
  139. VMSTATE_UINT32(ocr3, IMXGPTState),
  140. VMSTATE_UINT32(icr1, IMXGPTState),
  141. VMSTATE_UINT32(icr2, IMXGPTState),
  142. VMSTATE_UINT32(cnt, IMXGPTState),
  143. VMSTATE_UINT32(next_timeout, IMXGPTState),
  144. VMSTATE_UINT32(next_int, IMXGPTState),
  145. VMSTATE_UINT32(freq, IMXGPTState),
  146. VMSTATE_PTIMER(timer, IMXGPTState),
  147. VMSTATE_END_OF_LIST()
  148. }
  149. };
  150. static const IMXClk imx_gpt_clocks[] = {
  151. NOCLK, /* 000 No clock source */
  152. IPG, /* 001 ipg_clk, 532MHz*/
  153. IPG, /* 010 ipg_clk_highfreq */
  154. NOCLK, /* 011 not defined */
  155. CLK_32k, /* 100 ipg_clk_32k */
  156. NOCLK, /* 101 not defined */
  157. NOCLK, /* 110 not defined */
  158. NOCLK, /* 111 not defined */
  159. };
  160. static void imx_gpt_set_freq(IMXGPTState *s)
  161. {
  162. uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
  163. uint32_t freq = imx_clock_frequency(s->ccm, imx_gpt_clocks[clksrc])
  164. / (1 + s->pr);
  165. s->freq = freq;
  166. DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, freq);
  167. if (freq) {
  168. ptimer_set_freq(s->timer, freq);
  169. }
  170. }
  171. static void imx_gpt_update_int(IMXGPTState *s)
  172. {
  173. if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
  174. qemu_irq_raise(s->irq);
  175. } else {
  176. qemu_irq_lower(s->irq);
  177. }
  178. }
  179. static uint32_t imx_gpt_update_count(IMXGPTState *s)
  180. {
  181. s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
  182. return s->cnt;
  183. }
  184. static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
  185. uint32_t timeout)
  186. {
  187. if ((count < reg) && (timeout > reg)) {
  188. timeout = reg;
  189. }
  190. return timeout;
  191. }
  192. static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
  193. {
  194. uint32_t timeout = TIMER_MAX;
  195. uint32_t count = 0;
  196. long long limit;
  197. if (!(s->cr & GPT_CR_EN)) {
  198. /* if not enabled just return */
  199. return;
  200. }
  201. if (event) {
  202. /* This is a timer event */
  203. if ((s->cr & GPT_CR_FRR) && (s->next_timeout != TIMER_MAX)) {
  204. /*
  205. * if we are in free running mode and we have not reached
  206. * the TIMER_MAX limit, then update the count
  207. */
  208. count = imx_gpt_update_count(s);
  209. }
  210. } else {
  211. /* not a timer event, then just update the count */
  212. count = imx_gpt_update_count(s);
  213. }
  214. /* now, find the next timeout related to count */
  215. if (s->ir & GPT_IR_OF1IE) {
  216. timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
  217. }
  218. if (s->ir & GPT_IR_OF2IE) {
  219. timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
  220. }
  221. if (s->ir & GPT_IR_OF3IE) {
  222. timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
  223. }
  224. /* find the next set of interrupts to raise for next timer event */
  225. s->next_int = 0;
  226. if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
  227. s->next_int |= GPT_SR_OF1;
  228. }
  229. if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
  230. s->next_int |= GPT_SR_OF2;
  231. }
  232. if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
  233. s->next_int |= GPT_SR_OF3;
  234. }
  235. if ((s->ir & GPT_IR_ROVIE) && (timeout == TIMER_MAX)) {
  236. s->next_int |= GPT_SR_ROV;
  237. }
  238. /* the new range to count down from */
  239. limit = timeout - imx_gpt_update_count(s);
  240. if (limit < 0) {
  241. /*
  242. * if we reach here, then QEMU is running too slow and we pass the
  243. * timeout limit while computing it. Let's deliver the interrupt
  244. * and compute a new limit.
  245. */
  246. s->sr |= s->next_int;
  247. imx_gpt_compute_next_timeout(s, event);
  248. imx_gpt_update_int(s);
  249. } else {
  250. /* New timeout value */
  251. s->next_timeout = timeout;
  252. /* reset the limit to the computed range */
  253. ptimer_set_limit(s->timer, limit, 1);
  254. }
  255. }
  256. static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
  257. {
  258. IMXGPTState *s = IMX_GPT(opaque);
  259. uint32_t reg_value = 0;
  260. uint32_t reg = offset >> 2;
  261. switch (reg) {
  262. case 0: /* Control Register */
  263. reg_value = s->cr;
  264. break;
  265. case 1: /* prescaler */
  266. reg_value = s->pr;
  267. break;
  268. case 2: /* Status Register */
  269. reg_value = s->sr;
  270. break;
  271. case 3: /* Interrupt Register */
  272. reg_value = s->ir;
  273. break;
  274. case 4: /* Output Compare Register 1 */
  275. reg_value = s->ocr1;
  276. break;
  277. case 5: /* Output Compare Register 2 */
  278. reg_value = s->ocr2;
  279. break;
  280. case 6: /* Output Compare Register 3 */
  281. reg_value = s->ocr3;
  282. break;
  283. case 7: /* input Capture Register 1 */
  284. qemu_log_mask(LOG_UNIMP, "icr1 feature is not implemented\n");
  285. reg_value = s->icr1;
  286. break;
  287. case 8: /* input Capture Register 2 */
  288. qemu_log_mask(LOG_UNIMP, "icr2 feature is not implemented\n");
  289. reg_value = s->icr2;
  290. break;
  291. case 9: /* cnt */
  292. imx_gpt_update_count(s);
  293. reg_value = s->cnt;
  294. break;
  295. default:
  296. IPRINTF("Bad offset %x\n", reg);
  297. break;
  298. }
  299. DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(reg), reg_value);
  300. return reg_value;
  301. }
  302. static void imx_gpt_reset(DeviceState *dev)
  303. {
  304. IMXGPTState *s = IMX_GPT(dev);
  305. /* stop timer */
  306. ptimer_stop(s->timer);
  307. /*
  308. * Soft reset doesn't touch some bits; hard reset clears them
  309. */
  310. s->cr &= ~(GPT_CR_EN|GPT_CR_ENMOD|GPT_CR_STOPEN|GPT_CR_DOZEN|
  311. GPT_CR_WAITEN|GPT_CR_DBGEN);
  312. s->sr = 0;
  313. s->pr = 0;
  314. s->ir = 0;
  315. s->cnt = 0;
  316. s->ocr1 = TIMER_MAX;
  317. s->ocr2 = TIMER_MAX;
  318. s->ocr3 = TIMER_MAX;
  319. s->icr1 = 0;
  320. s->icr2 = 0;
  321. s->next_timeout = TIMER_MAX;
  322. s->next_int = 0;
  323. /* compute new freq */
  324. imx_gpt_set_freq(s);
  325. /* reset the limit to TIMER_MAX */
  326. ptimer_set_limit(s->timer, TIMER_MAX, 1);
  327. /* if the timer is still enabled, restart it */
  328. if (s->freq && (s->cr & GPT_CR_EN)) {
  329. ptimer_run(s->timer, 1);
  330. }
  331. }
  332. static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
  333. unsigned size)
  334. {
  335. IMXGPTState *s = IMX_GPT(opaque);
  336. uint32_t oldreg;
  337. uint32_t reg = offset >> 2;
  338. DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(reg),
  339. (uint32_t)value);
  340. switch (reg) {
  341. case 0:
  342. oldreg = s->cr;
  343. s->cr = value & ~0x7c14;
  344. if (s->cr & GPT_CR_SWR) { /* force reset */
  345. /* handle the reset */
  346. imx_gpt_reset(DEVICE(s));
  347. } else {
  348. /* set our freq, as the source might have changed */
  349. imx_gpt_set_freq(s);
  350. if ((oldreg ^ s->cr) & GPT_CR_EN) {
  351. if (s->cr & GPT_CR_EN) {
  352. if (s->cr & GPT_CR_ENMOD) {
  353. s->next_timeout = TIMER_MAX;
  354. ptimer_set_count(s->timer, TIMER_MAX);
  355. imx_gpt_compute_next_timeout(s, false);
  356. }
  357. ptimer_run(s->timer, 1);
  358. } else {
  359. /* stop timer */
  360. ptimer_stop(s->timer);
  361. }
  362. }
  363. }
  364. break;
  365. case 1: /* Prescaler */
  366. s->pr = value & 0xfff;
  367. imx_gpt_set_freq(s);
  368. break;
  369. case 2: /* SR */
  370. s->sr &= ~(value & 0x3f);
  371. imx_gpt_update_int(s);
  372. break;
  373. case 3: /* IR -- interrupt register */
  374. s->ir = value & 0x3f;
  375. imx_gpt_update_int(s);
  376. imx_gpt_compute_next_timeout(s, false);
  377. break;
  378. case 4: /* OCR1 -- output compare register */
  379. s->ocr1 = value;
  380. /* In non-freerun mode, reset count when this register is written */
  381. if (!(s->cr & GPT_CR_FRR)) {
  382. s->next_timeout = TIMER_MAX;
  383. ptimer_set_limit(s->timer, TIMER_MAX, 1);
  384. }
  385. /* compute the new timeout */
  386. imx_gpt_compute_next_timeout(s, false);
  387. break;
  388. case 5: /* OCR2 -- output compare register */
  389. s->ocr2 = value;
  390. /* compute the new timeout */
  391. imx_gpt_compute_next_timeout(s, false);
  392. break;
  393. case 6: /* OCR3 -- output compare register */
  394. s->ocr3 = value;
  395. /* compute the new timeout */
  396. imx_gpt_compute_next_timeout(s, false);
  397. break;
  398. default:
  399. IPRINTF("Bad offset %x\n", reg);
  400. break;
  401. }
  402. }
  403. static void imx_gpt_timeout(void *opaque)
  404. {
  405. IMXGPTState *s = IMX_GPT(opaque);
  406. DPRINTF("\n");
  407. s->sr |= s->next_int;
  408. s->next_int = 0;
  409. imx_gpt_compute_next_timeout(s, true);
  410. imx_gpt_update_int(s);
  411. if (s->freq && (s->cr & GPT_CR_EN)) {
  412. ptimer_run(s->timer, 1);
  413. }
  414. }
  415. static const MemoryRegionOps imx_gpt_ops = {
  416. .read = imx_gpt_read,
  417. .write = imx_gpt_write,
  418. .endianness = DEVICE_NATIVE_ENDIAN,
  419. };
  420. static void imx_gpt_realize(DeviceState *dev, Error **errp)
  421. {
  422. IMXGPTState *s = IMX_GPT(dev);
  423. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  424. QEMUBH *bh;
  425. sysbus_init_irq(sbd, &s->irq);
  426. memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
  427. 0x00001000);
  428. sysbus_init_mmio(sbd, &s->iomem);
  429. bh = qemu_bh_new(imx_gpt_timeout, s);
  430. s->timer = ptimer_init(bh);
  431. }
  432. void imx_timerg_create(const hwaddr addr, qemu_irq irq, DeviceState *ccm)
  433. {
  434. IMXGPTState *pp;
  435. DeviceState *dev;
  436. dev = sysbus_create_simple(TYPE_IMX_GPT, addr, irq);
  437. pp = IMX_GPT(dev);
  438. pp->ccm = ccm;
  439. }
  440. static void imx_gpt_class_init(ObjectClass *klass, void *data)
  441. {
  442. DeviceClass *dc = DEVICE_CLASS(klass);
  443. dc->realize = imx_gpt_realize;
  444. dc->reset = imx_gpt_reset;
  445. dc->vmsd = &vmstate_imx_timer_gpt;
  446. dc->desc = "i.MX general timer";
  447. }
  448. static const TypeInfo imx_gpt_info = {
  449. .name = TYPE_IMX_GPT,
  450. .parent = TYPE_SYS_BUS_DEVICE,
  451. .instance_size = sizeof(IMXGPTState),
  452. .class_init = imx_gpt_class_init,
  453. };
  454. static void imx_gpt_register_types(void)
  455. {
  456. type_register_static(&imx_gpt_info);
  457. }
  458. type_init(imx_gpt_register_types)