blob: 7257f4201afdde9f7638ae587b744481f6c54eb6 [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
Jean-Christophe Duboisd647b262015-08-13 11:26:20 +010015#include "hw/timer/imx_gpt.h"
16#include "hw/misc/imx_ccm.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010017#include "qemu/main-loop.h"
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010018
Jean-Christophe Dubois05453522015-10-25 15:16:26 +010019#ifndef DEBUG_IMX_GPT
20#define DEBUG_IMX_GPT 0
21#endif
22
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)
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010030
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010031static char const *imx_gpt_reg_name(uint32_t reg)
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010032{
33 switch (reg) {
34 case 0:
35 return "CR";
36 case 1:
37 return "PR";
38 case 2:
39 return "SR";
40 case 3:
41 return "IR";
42 case 4:
43 return "OCR1";
44 case 5:
45 return "OCR2";
46 case 6:
47 return "OCR3";
48 case 7:
49 return "ICR1";
50 case 8:
51 return "ICR2";
52 case 9:
53 return "CNT";
54 default:
55 return "[?]";
56 }
57}
58
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010059static const VMStateDescription vmstate_imx_timer_gpt = {
Jean-Christophe Dubois68b85292015-08-13 11:26:21 +010060 .name = TYPE_IMX_GPT,
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010061 .version_id = 3,
62 .minimum_version_id = 3,
Juan Quintela8f1e8842014-05-13 16:09:35 +010063 .fields = (VMStateField[]) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010064 VMSTATE_UINT32(cr, IMXGPTState),
65 VMSTATE_UINT32(pr, IMXGPTState),
66 VMSTATE_UINT32(sr, IMXGPTState),
67 VMSTATE_UINT32(ir, IMXGPTState),
68 VMSTATE_UINT32(ocr1, IMXGPTState),
69 VMSTATE_UINT32(ocr2, IMXGPTState),
70 VMSTATE_UINT32(ocr3, IMXGPTState),
71 VMSTATE_UINT32(icr1, IMXGPTState),
72 VMSTATE_UINT32(icr2, IMXGPTState),
73 VMSTATE_UINT32(cnt, IMXGPTState),
74 VMSTATE_UINT32(next_timeout, IMXGPTState),
75 VMSTATE_UINT32(next_int, IMXGPTState),
76 VMSTATE_UINT32(freq, IMXGPTState),
77 VMSTATE_PTIMER(timer, IMXGPTState),
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010078 VMSTATE_END_OF_LIST()
79 }
80};
81
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010082static const IMXClk imx_gpt_clocks[] = {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010083 NOCLK, /* 000 No clock source */
84 IPG, /* 001 ipg_clk, 532MHz*/
85 IPG, /* 010 ipg_clk_highfreq */
86 NOCLK, /* 011 not defined */
87 CLK_32k, /* 100 ipg_clk_32k */
88 NOCLK, /* 101 not defined */
89 NOCLK, /* 110 not defined */
90 NOCLK, /* 111 not defined */
91};
92
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010093static void imx_gpt_set_freq(IMXGPTState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010094{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010095 uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010096 uint32_t freq = imx_clock_frequency(s->ccm, imx_gpt_clocks[clksrc])
Jean-Christophe Dubois68b85292015-08-13 11:26:21 +010097 / (1 + s->pr);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010098 s->freq = freq;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010099
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100100 DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, freq);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100101
102 if (freq) {
103 ptimer_set_freq(s->timer, freq);
104 }
105}
106
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100107static void imx_gpt_update_int(IMXGPTState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100108{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100109 if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
110 qemu_irq_raise(s->irq);
111 } else {
112 qemu_irq_lower(s->irq);
113 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100114}
115
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100116static uint32_t imx_gpt_update_count(IMXGPTState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100117{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100118 s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
119
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100120 return s->cnt;
121}
122
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100123static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
Jean-Christophe Dubois68b85292015-08-13 11:26:21 +0100124 uint32_t timeout)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100125{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100126 if ((count < reg) && (timeout > reg)) {
127 timeout = reg;
128 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100129
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100130 return timeout;
131}
132
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100133static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100134{
Michael Tokarev203d65a2014-08-02 00:14:48 +0400135 uint32_t timeout = GPT_TIMER_MAX;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100136 uint32_t count = 0;
137 long long limit;
138
139 if (!(s->cr & GPT_CR_EN)) {
140 /* if not enabled just return */
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100141 return;
142 }
143
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100144 if (event) {
145 /* This is a timer event */
146
Michael Tokarev203d65a2014-08-02 00:14:48 +0400147 if ((s->cr & GPT_CR_FRR) && (s->next_timeout != GPT_TIMER_MAX)) {
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100148 /*
149 * if we are in free running mode and we have not reached
Michael Tokarev203d65a2014-08-02 00:14:48 +0400150 * the GPT_TIMER_MAX limit, then update the count
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100151 */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100152 count = imx_gpt_update_count(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100153 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100154 } else {
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100155 /* not a timer event, then just update the count */
156
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100157 count = imx_gpt_update_count(s);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100158 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100159
160 /* now, find the next timeout related to count */
161
162 if (s->ir & GPT_IR_OF1IE) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100163 timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100164 }
165 if (s->ir & GPT_IR_OF2IE) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100166 timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100167 }
168 if (s->ir & GPT_IR_OF3IE) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100169 timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100170 }
171
172 /* find the next set of interrupts to raise for next timer event */
173
174 s->next_int = 0;
175 if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
176 s->next_int |= GPT_SR_OF1;
177 }
178 if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
179 s->next_int |= GPT_SR_OF2;
180 }
181 if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
182 s->next_int |= GPT_SR_OF3;
183 }
Michael Tokarev203d65a2014-08-02 00:14:48 +0400184 if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) {
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100185 s->next_int |= GPT_SR_ROV;
186 }
187
188 /* the new range to count down from */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100189 limit = timeout - imx_gpt_update_count(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100190
191 if (limit < 0) {
192 /*
193 * if we reach here, then QEMU is running too slow and we pass the
194 * timeout limit while computing it. Let's deliver the interrupt
195 * and compute a new limit.
196 */
197 s->sr |= s->next_int;
198
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100199 imx_gpt_compute_next_timeout(s, event);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100200
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100201 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100202 } else {
203 /* New timeout value */
204 s->next_timeout = timeout;
205
206 /* reset the limit to the computed range */
207 ptimer_set_limit(s->timer, limit, 1);
208 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100209}
210
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100211static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100212{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100213 IMXGPTState *s = IMX_GPT(opaque);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100214 uint32_t reg_value = 0;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100215
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100216 switch (offset >> 2) {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100217 case 0: /* Control Register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100218 reg_value = s->cr;
219 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100220
221 case 1: /* prescaler */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100222 reg_value = s->pr;
223 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100224
225 case 2: /* Status Register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100226 reg_value = s->sr;
227 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100228
229 case 3: /* Interrupt Register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100230 reg_value = s->ir;
231 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100232
233 case 4: /* Output Compare Register 1 */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100234 reg_value = s->ocr1;
235 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100236
237 case 5: /* Output Compare Register 2 */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100238 reg_value = s->ocr2;
239 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100240
241 case 6: /* Output Compare Register 3 */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100242 reg_value = s->ocr3;
243 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100244
245 case 7: /* input Capture Register 1 */
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100246 qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n",
247 TYPE_IMX_GPT, __func__);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100248 reg_value = s->icr1;
249 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100250
251 case 8: /* input Capture Register 2 */
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100252 qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n",
253 TYPE_IMX_GPT, __func__);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100254 reg_value = s->icr2;
255 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100256
257 case 9: /* cnt */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100258 imx_gpt_update_count(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100259 reg_value = s->cnt;
260 break;
261
262 default:
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100263 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
264 HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100265 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100266 }
267
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100268 DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100269
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100270 return reg_value;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100271}
272
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100273static void imx_gpt_reset(DeviceState *dev)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100274{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100275 IMXGPTState *s = IMX_GPT(dev);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100276
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100277 /* stop timer */
278 ptimer_stop(s->timer);
279
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100280 /*
281 * Soft reset doesn't touch some bits; hard reset clears them
282 */
283 s->cr &= ~(GPT_CR_EN|GPT_CR_ENMOD|GPT_CR_STOPEN|GPT_CR_DOZEN|
284 GPT_CR_WAITEN|GPT_CR_DBGEN);
285 s->sr = 0;
286 s->pr = 0;
287 s->ir = 0;
288 s->cnt = 0;
Michael Tokarev203d65a2014-08-02 00:14:48 +0400289 s->ocr1 = GPT_TIMER_MAX;
290 s->ocr2 = GPT_TIMER_MAX;
291 s->ocr3 = GPT_TIMER_MAX;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100292 s->icr1 = 0;
293 s->icr2 = 0;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100294
Michael Tokarev203d65a2014-08-02 00:14:48 +0400295 s->next_timeout = GPT_TIMER_MAX;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100296 s->next_int = 0;
297
298 /* compute new freq */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100299 imx_gpt_set_freq(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100300
Michael Tokarev203d65a2014-08-02 00:14:48 +0400301 /* reset the limit to GPT_TIMER_MAX */
302 ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100303
304 /* if the timer is still enabled, restart it */
305 if (s->freq && (s->cr & GPT_CR_EN)) {
306 ptimer_run(s->timer, 1);
307 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100308}
309
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100310static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
311 unsigned size)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100312{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100313 IMXGPTState *s = IMX_GPT(opaque);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100314 uint32_t oldreg;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100315
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100316 DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2),
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100317 (uint32_t)value);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100318
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100319 switch (offset >> 2) {
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100320 case 0:
321 oldreg = s->cr;
322 s->cr = value & ~0x7c14;
323 if (s->cr & GPT_CR_SWR) { /* force reset */
324 /* handle the reset */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100325 imx_gpt_reset(DEVICE(s));
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100326 } else {
327 /* set our freq, as the source might have changed */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100328 imx_gpt_set_freq(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100329
330 if ((oldreg ^ s->cr) & GPT_CR_EN) {
331 if (s->cr & GPT_CR_EN) {
332 if (s->cr & GPT_CR_ENMOD) {
Michael Tokarev203d65a2014-08-02 00:14:48 +0400333 s->next_timeout = GPT_TIMER_MAX;
334 ptimer_set_count(s->timer, GPT_TIMER_MAX);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100335 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100336 }
337 ptimer_run(s->timer, 1);
338 } else {
339 /* stop timer */
340 ptimer_stop(s->timer);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100341 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100342 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100343 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100344 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100345
346 case 1: /* Prescaler */
347 s->pr = value & 0xfff;
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100348 imx_gpt_set_freq(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100349 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100350
351 case 2: /* SR */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100352 s->sr &= ~(value & 0x3f);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100353 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100354 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100355
356 case 3: /* IR -- interrupt register */
357 s->ir = value & 0x3f;
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100358 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100359
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100360 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100361
362 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100363
364 case 4: /* OCR1 -- output compare register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100365 s->ocr1 = value;
366
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100367 /* In non-freerun mode, reset count when this register is written */
368 if (!(s->cr & GPT_CR_FRR)) {
Michael Tokarev203d65a2014-08-02 00:14:48 +0400369 s->next_timeout = GPT_TIMER_MAX;
370 ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100371 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100372
373 /* compute the new timeout */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100374 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100375
376 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100377
378 case 5: /* OCR2 -- output compare register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100379 s->ocr2 = value;
380
381 /* compute the new timeout */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100382 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100383
384 break;
385
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100386 case 6: /* OCR3 -- output compare register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100387 s->ocr3 = value;
388
389 /* compute the new timeout */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100390 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100391
392 break;
393
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100394 default:
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100395 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
396 HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100397 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100398 }
399}
400
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100401static void imx_gpt_timeout(void *opaque)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100402{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100403 IMXGPTState *s = IMX_GPT(opaque);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100404
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100405 DPRINTF("\n");
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100406
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100407 s->sr |= s->next_int;
408 s->next_int = 0;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100409
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100410 imx_gpt_compute_next_timeout(s, true);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100411
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100412 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100413
414 if (s->freq && (s->cr & GPT_CR_EN)) {
415 ptimer_run(s->timer, 1);
416 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100417}
418
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100419static const MemoryRegionOps imx_gpt_ops = {
420 .read = imx_gpt_read,
421 .write = imx_gpt_write,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100422 .endianness = DEVICE_NATIVE_ENDIAN,
423};
424
425
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100426static void imx_gpt_realize(DeviceState *dev, Error **errp)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100427{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100428 IMXGPTState *s = IMX_GPT(dev);
429 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100430 QEMUBH *bh;
431
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100432 sysbus_init_irq(sbd, &s->irq);
Paolo Bonzini853dca12013-06-06 21:25:08 -0400433 memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100434 0x00001000);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100435 sysbus_init_mmio(sbd, &s->iomem);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100436
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100437 bh = qemu_bh_new(imx_gpt_timeout, s);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100438 s->timer = ptimer_init(bh);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100439}
440
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100441static void imx_gpt_class_init(ObjectClass *klass, void *data)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100442{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100443 DeviceClass *dc = DEVICE_CLASS(klass);
444
445 dc->realize = imx_gpt_realize;
446 dc->reset = imx_gpt_reset;
447 dc->vmsd = &vmstate_imx_timer_gpt;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100448 dc->desc = "i.MX general timer";
449}
450
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100451static const TypeInfo imx_gpt_info = {
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100452 .name = TYPE_IMX_GPT,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100453 .parent = TYPE_SYS_BUS_DEVICE,
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100454 .instance_size = sizeof(IMXGPTState),
455 .class_init = imx_gpt_class_init,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100456};
457
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100458static void imx_gpt_register_types(void)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100459{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100460 type_register_static(&imx_gpt_info);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100461}
462
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100463type_init(imx_gpt_register_types)