blob: 65e4ee6bcf950aadebeb78d1b69be9c7e2d0c91d [file] [log] [blame]
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +01001/*
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
Jean-Christophe Duboisd647b262015-08-13 11:26:20 +01008 * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +01009 *
10 * This code is licensed under GPL version 2 or later. See
11 * the COPYING file in the top-level directory.
12 *
13 */
14
Peter Maydell8ef94f02016-01-26 18:17:05 +000015#include "qemu/osdep.h"
Jean-Christophe Duboisd647b262015-08-13 11:26:20 +010016#include "hw/timer/imx_gpt.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010017#include "qemu/main-loop.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010018#include "qemu/log.h"
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010019
Jean-Christophe Dubois05453522015-10-25 15:16:26 +010020#ifndef DEBUG_IMX_GPT
21#define DEBUG_IMX_GPT 0
22#endif
23
24#define DPRINTF(fmt, args...) \
25 do { \
26 if (DEBUG_IMX_GPT) { \
27 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPT, \
28 __func__, ##args); \
29 } \
30 } while (0)
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010031
Peter Maydelld6757652016-09-22 18:13:09 +010032static const char *imx_gpt_reg_name(uint32_t reg)
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010033{
34 switch (reg) {
35 case 0:
36 return "CR";
37 case 1:
38 return "PR";
39 case 2:
40 return "SR";
41 case 3:
42 return "IR";
43 case 4:
44 return "OCR1";
45 case 5:
46 return "OCR2";
47 case 6:
48 return "OCR3";
49 case 7:
50 return "ICR1";
51 case 8:
52 return "ICR2";
53 case 9:
54 return "CNT";
55 default:
56 return "[?]";
57 }
58}
59
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010060static const VMStateDescription vmstate_imx_timer_gpt = {
Jean-Christophe Dubois68b85292015-08-13 11:26:21 +010061 .name = TYPE_IMX_GPT,
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010062 .version_id = 3,
63 .minimum_version_id = 3,
Juan Quintela8f1e8842014-05-13 16:09:35 +010064 .fields = (VMStateField[]) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010065 VMSTATE_UINT32(cr, IMXGPTState),
66 VMSTATE_UINT32(pr, IMXGPTState),
67 VMSTATE_UINT32(sr, IMXGPTState),
68 VMSTATE_UINT32(ir, IMXGPTState),
69 VMSTATE_UINT32(ocr1, IMXGPTState),
70 VMSTATE_UINT32(ocr2, IMXGPTState),
71 VMSTATE_UINT32(ocr3, IMXGPTState),
72 VMSTATE_UINT32(icr1, IMXGPTState),
73 VMSTATE_UINT32(icr2, IMXGPTState),
74 VMSTATE_UINT32(cnt, IMXGPTState),
75 VMSTATE_UINT32(next_timeout, IMXGPTState),
76 VMSTATE_UINT32(next_int, IMXGPTState),
77 VMSTATE_UINT32(freq, IMXGPTState),
78 VMSTATE_PTIMER(timer, IMXGPTState),
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010079 VMSTATE_END_OF_LIST()
80 }
81};
82
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +010083static const IMXClk imx25_gpt_clocks[] = {
84 CLK_NONE, /* 000 No clock source */
85 CLK_IPG, /* 001 ipg_clk, 532MHz*/
86 CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
87 CLK_NONE, /* 011 not defined */
88 CLK_32k, /* 100 ipg_clk_32k */
89 CLK_32k, /* 101 ipg_clk_32k */
90 CLK_32k, /* 110 ipg_clk_32k */
91 CLK_32k, /* 111 ipg_clk_32k */
92};
93
94static const IMXClk imx31_gpt_clocks[] = {
Jean-Christophe Duboisd552f672016-03-16 17:06:00 +000095 CLK_NONE, /* 000 No clock source */
96 CLK_IPG, /* 001 ipg_clk, 532MHz*/
97 CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
98 CLK_NONE, /* 011 not defined */
99 CLK_32k, /* 100 ipg_clk_32k */
100 CLK_NONE, /* 101 not defined */
101 CLK_NONE, /* 110 not defined */
102 CLK_NONE, /* 111 not defined */
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100103};
104
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100105static const IMXClk imx6_gpt_clocks[] = {
106 CLK_NONE, /* 000 No clock source */
107 CLK_IPG, /* 001 ipg_clk, 532MHz*/
108 CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
109 CLK_EXT, /* 011 External clock */
110 CLK_32k, /* 100 ipg_clk_32k */
111 CLK_HIGH_DIV, /* 101 reference clock / 8 */
112 CLK_NONE, /* 110 not defined */
113 CLK_HIGH, /* 111 reference clock */
114};
115
Andrey Smirnova62bf592018-02-09 10:40:30 +0000116static const IMXClk imx7_gpt_clocks[] = {
117 CLK_NONE, /* 000 No clock source */
118 CLK_IPG, /* 001 ipg_clk, 532MHz*/
119 CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
120 CLK_EXT, /* 011 External clock */
121 CLK_32k, /* 100 ipg_clk_32k */
122 CLK_HIGH, /* 101 reference clock */
123 CLK_NONE, /* 110 not defined */
124 CLK_NONE, /* 111 not defined */
125};
126
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100127static void imx_gpt_set_freq(IMXGPTState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100128{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100129 uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100130
Jean-Christophe Duboisaaa9ec32015-12-17 13:37:15 +0000131 s->freq = imx_ccm_get_clock_frequency(s->ccm,
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100132 s->clocks[clksrc]) / (1 + s->pr);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100133
Jean-Christophe Duboisaaa9ec32015-12-17 13:37:15 +0000134 DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq);
135
136 if (s->freq) {
137 ptimer_set_freq(s->timer, s->freq);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100138 }
139}
140
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100141static void imx_gpt_update_int(IMXGPTState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100142{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100143 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 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100148}
149
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100150static uint32_t imx_gpt_update_count(IMXGPTState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100151{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100152 s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
153
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100154 return s->cnt;
155}
156
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100157static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
Jean-Christophe Dubois68b85292015-08-13 11:26:21 +0100158 uint32_t timeout)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100159{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100160 if ((count < reg) && (timeout > reg)) {
161 timeout = reg;
162 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100163
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100164 return timeout;
165}
166
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100167static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100168{
Michael Tokarev203d65a2014-08-02 00:14:48 +0400169 uint32_t timeout = GPT_TIMER_MAX;
Jean-Christophe Dubois4833e152016-03-16 17:05:59 +0000170 uint32_t count;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100171 long long limit;
172
173 if (!(s->cr & GPT_CR_EN)) {
174 /* if not enabled just return */
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100175 return;
176 }
177
Jean-Christophe Dubois4833e152016-03-16 17:05:59 +0000178 /* update the count */
179 count = imx_gpt_update_count(s);
180
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100181 if (event) {
Jean-Christophe Dubois4833e152016-03-16 17:05:59 +0000182 /*
183 * This is an event (the ptimer reached 0 and stopped), and the
184 * timer counter is now equal to s->next_timeout.
185 */
186 if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) {
187 /* We are in restart mode and we crossed the compare channel 1
188 * value. We need to reset the counter to 0.
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100189 */
Jean-Christophe Dubois4833e152016-03-16 17:05:59 +0000190 count = s->cnt = s->next_timeout = 0;
191 } else if (count == GPT_TIMER_MAX) {
192 /* We reached GPT_TIMER_MAX so we need to rollover */
193 count = s->cnt = s->next_timeout = 0;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100194 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100195 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100196
197 /* now, find the next timeout related to count */
198
199 if (s->ir & GPT_IR_OF1IE) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100200 timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100201 }
202 if (s->ir & GPT_IR_OF2IE) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100203 timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100204 }
205 if (s->ir & GPT_IR_OF3IE) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100206 timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100207 }
208
209 /* find the next set of interrupts to raise for next timer event */
210
211 s->next_int = 0;
212 if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
213 s->next_int |= GPT_SR_OF1;
214 }
215 if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
216 s->next_int |= GPT_SR_OF2;
217 }
218 if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
219 s->next_int |= GPT_SR_OF3;
220 }
Michael Tokarev203d65a2014-08-02 00:14:48 +0400221 if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) {
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100222 s->next_int |= GPT_SR_ROV;
223 }
224
225 /* the new range to count down from */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100226 limit = timeout - imx_gpt_update_count(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100227
228 if (limit < 0) {
229 /*
230 * if we reach here, then QEMU is running too slow and we pass the
231 * timeout limit while computing it. Let's deliver the interrupt
232 * and compute a new limit.
233 */
234 s->sr |= s->next_int;
235
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100236 imx_gpt_compute_next_timeout(s, event);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100237
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100238 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100239 } else {
240 /* New timeout value */
241 s->next_timeout = timeout;
242
243 /* reset the limit to the computed range */
244 ptimer_set_limit(s->timer, limit, 1);
245 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100246}
247
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100248static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100249{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100250 IMXGPTState *s = IMX_GPT(opaque);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100251 uint32_t reg_value = 0;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100252
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100253 switch (offset >> 2) {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100254 case 0: /* Control Register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100255 reg_value = s->cr;
256 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100257
258 case 1: /* prescaler */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100259 reg_value = s->pr;
260 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100261
262 case 2: /* Status Register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100263 reg_value = s->sr;
264 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100265
266 case 3: /* Interrupt Register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100267 reg_value = s->ir;
268 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100269
270 case 4: /* Output Compare Register 1 */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100271 reg_value = s->ocr1;
272 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100273
274 case 5: /* Output Compare Register 2 */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100275 reg_value = s->ocr2;
276 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100277
278 case 6: /* Output Compare Register 3 */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100279 reg_value = s->ocr3;
280 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100281
282 case 7: /* input Capture Register 1 */
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100283 qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n",
284 TYPE_IMX_GPT, __func__);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100285 reg_value = s->icr1;
286 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100287
288 case 8: /* input Capture Register 2 */
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100289 qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n",
290 TYPE_IMX_GPT, __func__);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100291 reg_value = s->icr2;
292 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100293
294 case 9: /* cnt */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100295 imx_gpt_update_count(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100296 reg_value = s->cnt;
297 break;
298
299 default:
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100300 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
301 HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100302 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100303 }
304
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100305 DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100306
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100307 return reg_value;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100308}
309
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100310
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000311static void imx_gpt_reset_common(IMXGPTState *s, bool is_soft_reset)
312{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100313 /* stop timer */
314 ptimer_stop(s->timer);
315
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000316 /* Soft reset and hard reset differ only in their handling of the CR
317 * register -- soft reset preserves the values of some bits there.
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100318 */
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000319 if (is_soft_reset) {
320 /* Clear all CR bits except those that are preserved by soft reset. */
321 s->cr &= GPT_CR_EN | GPT_CR_ENMOD | GPT_CR_STOPEN | GPT_CR_DOZEN |
322 GPT_CR_WAITEN | GPT_CR_DBGEN |
323 (GPT_CR_CLKSRC_MASK << GPT_CR_CLKSRC_SHIFT);
324 } else {
325 s->cr = 0;
326 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100327 s->sr = 0;
328 s->pr = 0;
329 s->ir = 0;
330 s->cnt = 0;
Michael Tokarev203d65a2014-08-02 00:14:48 +0400331 s->ocr1 = GPT_TIMER_MAX;
332 s->ocr2 = GPT_TIMER_MAX;
333 s->ocr3 = GPT_TIMER_MAX;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100334 s->icr1 = 0;
335 s->icr2 = 0;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100336
Michael Tokarev203d65a2014-08-02 00:14:48 +0400337 s->next_timeout = GPT_TIMER_MAX;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100338 s->next_int = 0;
339
340 /* compute new freq */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100341 imx_gpt_set_freq(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100342
Michael Tokarev203d65a2014-08-02 00:14:48 +0400343 /* reset the limit to GPT_TIMER_MAX */
344 ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100345
346 /* if the timer is still enabled, restart it */
347 if (s->freq && (s->cr & GPT_CR_EN)) {
348 ptimer_run(s->timer, 1);
349 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100350}
351
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000352static void imx_gpt_soft_reset(DeviceState *dev)
353{
354 IMXGPTState *s = IMX_GPT(dev);
355 imx_gpt_reset_common(s, true);
356}
357
358static void imx_gpt_reset(DeviceState *dev)
359{
360 IMXGPTState *s = IMX_GPT(dev);
361 imx_gpt_reset_common(s, false);
362}
363
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100364static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
365 unsigned size)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100366{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100367 IMXGPTState *s = IMX_GPT(opaque);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100368 uint32_t oldreg;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100369
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100370 DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2),
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100371 (uint32_t)value);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100372
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100373 switch (offset >> 2) {
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100374 case 0:
375 oldreg = s->cr;
376 s->cr = value & ~0x7c14;
377 if (s->cr & GPT_CR_SWR) { /* force reset */
378 /* handle the reset */
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000379 imx_gpt_soft_reset(DEVICE(s));
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100380 } else {
381 /* set our freq, as the source might have changed */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100382 imx_gpt_set_freq(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100383
384 if ((oldreg ^ s->cr) & GPT_CR_EN) {
385 if (s->cr & GPT_CR_EN) {
386 if (s->cr & GPT_CR_ENMOD) {
Michael Tokarev203d65a2014-08-02 00:14:48 +0400387 s->next_timeout = GPT_TIMER_MAX;
388 ptimer_set_count(s->timer, GPT_TIMER_MAX);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100389 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100390 }
391 ptimer_run(s->timer, 1);
392 } else {
393 /* stop timer */
394 ptimer_stop(s->timer);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100395 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100396 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100397 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100398 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100399
400 case 1: /* Prescaler */
401 s->pr = value & 0xfff;
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100402 imx_gpt_set_freq(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100403 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100404
405 case 2: /* SR */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100406 s->sr &= ~(value & 0x3f);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100407 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100408 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100409
410 case 3: /* IR -- interrupt register */
411 s->ir = value & 0x3f;
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100412 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100413
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100414 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100415
416 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100417
418 case 4: /* OCR1 -- output compare register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100419 s->ocr1 = value;
420
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100421 /* In non-freerun mode, reset count when this register is written */
422 if (!(s->cr & GPT_CR_FRR)) {
Michael Tokarev203d65a2014-08-02 00:14:48 +0400423 s->next_timeout = GPT_TIMER_MAX;
424 ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100425 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100426
427 /* compute the new timeout */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100428 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100429
430 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100431
432 case 5: /* OCR2 -- output compare register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100433 s->ocr2 = value;
434
435 /* compute the new timeout */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100436 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100437
438 break;
439
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100440 case 6: /* OCR3 -- output compare register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100441 s->ocr3 = value;
442
443 /* compute the new timeout */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100444 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100445
446 break;
447
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100448 default:
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100449 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
450 HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100451 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100452 }
453}
454
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100455static void imx_gpt_timeout(void *opaque)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100456{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100457 IMXGPTState *s = IMX_GPT(opaque);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100458
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100459 DPRINTF("\n");
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100460
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100461 s->sr |= s->next_int;
462 s->next_int = 0;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100463
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100464 imx_gpt_compute_next_timeout(s, true);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100465
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100466 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100467
468 if (s->freq && (s->cr & GPT_CR_EN)) {
469 ptimer_run(s->timer, 1);
470 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100471}
472
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100473static const MemoryRegionOps imx_gpt_ops = {
474 .read = imx_gpt_read,
475 .write = imx_gpt_write,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100476 .endianness = DEVICE_NATIVE_ENDIAN,
477};
478
479
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100480static void imx_gpt_realize(DeviceState *dev, Error **errp)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100481{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100482 IMXGPTState *s = IMX_GPT(dev);
483 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100484 QEMUBH *bh;
485
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100486 sysbus_init_irq(sbd, &s->irq);
Paolo Bonzini853dca12013-06-06 21:25:08 -0400487 memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100488 0x00001000);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100489 sysbus_init_mmio(sbd, &s->iomem);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100490
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100491 bh = qemu_bh_new(imx_gpt_timeout, s);
Dmitry Osipenkoe7ea81c2016-09-22 18:13:06 +0100492 s->timer = ptimer_init(bh, PTIMER_POLICY_DEFAULT);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100493}
494
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100495static void imx_gpt_class_init(ObjectClass *klass, void *data)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100496{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100497 DeviceClass *dc = DEVICE_CLASS(klass);
498
499 dc->realize = imx_gpt_realize;
500 dc->reset = imx_gpt_reset;
501 dc->vmsd = &vmstate_imx_timer_gpt;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100502 dc->desc = "i.MX general timer";
503}
504
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100505static void imx25_gpt_init(Object *obj)
506{
507 IMXGPTState *s = IMX_GPT(obj);
508
509 s->clocks = imx25_gpt_clocks;
510}
511
512static void imx31_gpt_init(Object *obj)
513{
514 IMXGPTState *s = IMX_GPT(obj);
515
516 s->clocks = imx31_gpt_clocks;
517}
518
519static void imx6_gpt_init(Object *obj)
520{
521 IMXGPTState *s = IMX_GPT(obj);
522
523 s->clocks = imx6_gpt_clocks;
524}
525
Andrey Smirnova62bf592018-02-09 10:40:30 +0000526static void imx7_gpt_init(Object *obj)
527{
528 IMXGPTState *s = IMX_GPT(obj);
529
530 s->clocks = imx7_gpt_clocks;
531}
532
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100533static const TypeInfo imx25_gpt_info = {
534 .name = TYPE_IMX25_GPT,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100535 .parent = TYPE_SYS_BUS_DEVICE,
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100536 .instance_size = sizeof(IMXGPTState),
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100537 .instance_init = imx25_gpt_init,
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100538 .class_init = imx_gpt_class_init,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100539};
540
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100541static const TypeInfo imx31_gpt_info = {
542 .name = TYPE_IMX31_GPT,
543 .parent = TYPE_IMX25_GPT,
544 .instance_init = imx31_gpt_init,
545};
546
547static const TypeInfo imx6_gpt_info = {
548 .name = TYPE_IMX6_GPT,
549 .parent = TYPE_IMX25_GPT,
550 .instance_init = imx6_gpt_init,
551};
552
Andrey Smirnova62bf592018-02-09 10:40:30 +0000553static const TypeInfo imx7_gpt_info = {
554 .name = TYPE_IMX7_GPT,
555 .parent = TYPE_IMX25_GPT,
556 .instance_init = imx7_gpt_init,
557};
558
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100559static void imx_gpt_register_types(void)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100560{
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100561 type_register_static(&imx25_gpt_info);
562 type_register_static(&imx31_gpt_info);
563 type_register_static(&imx6_gpt_info);
Andrey Smirnova62bf592018-02-09 10:40:30 +0000564 type_register_static(&imx7_gpt_info);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100565}
566
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100567type_init(imx_gpt_register_types)