blob: 5c0d9a269ceb044bfd7c348876e31c945173ad84 [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"
Markus Armbruster64552b62019-08-12 07:23:42 +020016#include "hw/irq.h"
Jean-Christophe Duboisd647b262015-08-13 11:26:20 +010017#include "hw/timer/imx_gpt.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020018#include "migration/vmstate.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020019#include "qemu/module.h"
Paolo Bonzini03dd0242015-12-15 13:16:16 +010020#include "qemu/log.h"
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010021
Jean-Christophe Dubois05453522015-10-25 15:16:26 +010022#ifndef DEBUG_IMX_GPT
23#define DEBUG_IMX_GPT 0
24#endif
25
26#define DPRINTF(fmt, args...) \
27 do { \
28 if (DEBUG_IMX_GPT) { \
29 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPT, \
30 __func__, ##args); \
31 } \
32 } while (0)
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010033
Peter Maydelld6757652016-09-22 18:13:09 +010034static const char *imx_gpt_reg_name(uint32_t reg)
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010035{
36 switch (reg) {
37 case 0:
38 return "CR";
39 case 1:
40 return "PR";
41 case 2:
42 return "SR";
43 case 3:
44 return "IR";
45 case 4:
46 return "OCR1";
47 case 5:
48 return "OCR2";
49 case 6:
50 return "OCR3";
51 case 7:
52 return "ICR1";
53 case 8:
54 return "ICR2";
55 case 9:
56 return "CNT";
57 default:
58 return "[?]";
59 }
60}
61
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010062static const VMStateDescription vmstate_imx_timer_gpt = {
Jean-Christophe Dubois68b85292015-08-13 11:26:21 +010063 .name = TYPE_IMX_GPT,
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +010064 .version_id = 3,
65 .minimum_version_id = 3,
Juan Quintela8f1e8842014-05-13 16:09:35 +010066 .fields = (VMStateField[]) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +010067 VMSTATE_UINT32(cr, IMXGPTState),
68 VMSTATE_UINT32(pr, IMXGPTState),
69 VMSTATE_UINT32(sr, IMXGPTState),
70 VMSTATE_UINT32(ir, IMXGPTState),
71 VMSTATE_UINT32(ocr1, IMXGPTState),
72 VMSTATE_UINT32(ocr2, IMXGPTState),
73 VMSTATE_UINT32(ocr3, IMXGPTState),
74 VMSTATE_UINT32(icr1, IMXGPTState),
75 VMSTATE_UINT32(icr2, IMXGPTState),
76 VMSTATE_UINT32(cnt, IMXGPTState),
77 VMSTATE_UINT32(next_timeout, IMXGPTState),
78 VMSTATE_UINT32(next_int, IMXGPTState),
79 VMSTATE_UINT32(freq, IMXGPTState),
80 VMSTATE_PTIMER(timer, IMXGPTState),
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010081 VMSTATE_END_OF_LIST()
82 }
83};
84
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +010085static const IMXClk imx25_gpt_clocks[] = {
86 CLK_NONE, /* 000 No clock source */
87 CLK_IPG, /* 001 ipg_clk, 532MHz*/
88 CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
89 CLK_NONE, /* 011 not defined */
90 CLK_32k, /* 100 ipg_clk_32k */
91 CLK_32k, /* 101 ipg_clk_32k */
92 CLK_32k, /* 110 ipg_clk_32k */
93 CLK_32k, /* 111 ipg_clk_32k */
94};
95
96static const IMXClk imx31_gpt_clocks[] = {
Jean-Christophe Duboisd552f672016-03-16 17:06:00 +000097 CLK_NONE, /* 000 No clock source */
98 CLK_IPG, /* 001 ipg_clk, 532MHz*/
99 CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
100 CLK_NONE, /* 011 not defined */
101 CLK_32k, /* 100 ipg_clk_32k */
102 CLK_NONE, /* 101 not defined */
103 CLK_NONE, /* 110 not defined */
104 CLK_NONE, /* 111 not defined */
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100105};
106
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100107static const IMXClk imx6_gpt_clocks[] = {
108 CLK_NONE, /* 000 No clock source */
109 CLK_IPG, /* 001 ipg_clk, 532MHz*/
110 CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
111 CLK_EXT, /* 011 External clock */
112 CLK_32k, /* 100 ipg_clk_32k */
113 CLK_HIGH_DIV, /* 101 reference clock / 8 */
114 CLK_NONE, /* 110 not defined */
115 CLK_HIGH, /* 111 reference clock */
116};
117
Andrey Smirnova62bf592018-02-09 10:40:30 +0000118static const IMXClk imx7_gpt_clocks[] = {
119 CLK_NONE, /* 000 No clock source */
120 CLK_IPG, /* 001 ipg_clk, 532MHz*/
121 CLK_IPG_HIGH, /* 010 ipg_clk_highfreq */
122 CLK_EXT, /* 011 External clock */
123 CLK_32k, /* 100 ipg_clk_32k */
124 CLK_HIGH, /* 101 reference clock */
125 CLK_NONE, /* 110 not defined */
126 CLK_NONE, /* 111 not defined */
127};
128
Peter Maydell1b914992019-10-08 18:17:37 +0100129/* Must be called from within ptimer_transaction_begin/commit block */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100130static void imx_gpt_set_freq(IMXGPTState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100131{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100132 uint32_t clksrc = extract32(s->cr, GPT_CR_CLKSRC_SHIFT, 3);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100133
Jean-Christophe Duboisaaa9ec32015-12-17 13:37:15 +0000134 s->freq = imx_ccm_get_clock_frequency(s->ccm,
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100135 s->clocks[clksrc]) / (1 + s->pr);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100136
Jean-Christophe Duboisaaa9ec32015-12-17 13:37:15 +0000137 DPRINTF("Setting clksrc %d to frequency %d\n", clksrc, s->freq);
138
139 if (s->freq) {
140 ptimer_set_freq(s->timer, s->freq);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100141 }
142}
143
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100144static void imx_gpt_update_int(IMXGPTState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100145{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100146 if ((s->sr & s->ir) && (s->cr & GPT_CR_EN)) {
147 qemu_irq_raise(s->irq);
148 } else {
149 qemu_irq_lower(s->irq);
150 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100151}
152
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100153static uint32_t imx_gpt_update_count(IMXGPTState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100154{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100155 s->cnt = s->next_timeout - (uint32_t)ptimer_get_count(s->timer);
156
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100157 return s->cnt;
158}
159
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100160static inline uint32_t imx_gpt_find_limit(uint32_t count, uint32_t reg,
Jean-Christophe Dubois68b85292015-08-13 11:26:21 +0100161 uint32_t timeout)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100162{
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100163 if ((count < reg) && (timeout > reg)) {
164 timeout = reg;
165 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100166
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100167 return timeout;
168}
169
Peter Maydell1b914992019-10-08 18:17:37 +0100170/* Must be called from within ptimer_transaction_begin/commit block */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100171static void imx_gpt_compute_next_timeout(IMXGPTState *s, bool event)
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100172{
Michael Tokarev203d65a2014-08-02 00:14:48 +0400173 uint32_t timeout = GPT_TIMER_MAX;
Jean-Christophe Dubois4833e152016-03-16 17:05:59 +0000174 uint32_t count;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100175 long long limit;
176
177 if (!(s->cr & GPT_CR_EN)) {
178 /* if not enabled just return */
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100179 return;
180 }
181
Jean-Christophe Dubois4833e152016-03-16 17:05:59 +0000182 /* update the count */
183 count = imx_gpt_update_count(s);
184
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100185 if (event) {
Jean-Christophe Dubois4833e152016-03-16 17:05:59 +0000186 /*
187 * This is an event (the ptimer reached 0 and stopped), and the
188 * timer counter is now equal to s->next_timeout.
189 */
190 if (!(s->cr & GPT_CR_FRR) && (count == s->ocr1)) {
191 /* We are in restart mode and we crossed the compare channel 1
192 * value. We need to reset the counter to 0.
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100193 */
Jean-Christophe Dubois4833e152016-03-16 17:05:59 +0000194 count = s->cnt = s->next_timeout = 0;
195 } else if (count == GPT_TIMER_MAX) {
196 /* We reached GPT_TIMER_MAX so we need to rollover */
197 count = s->cnt = s->next_timeout = 0;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100198 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100199 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100200
201 /* now, find the next timeout related to count */
202
203 if (s->ir & GPT_IR_OF1IE) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100204 timeout = imx_gpt_find_limit(count, s->ocr1, timeout);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100205 }
206 if (s->ir & GPT_IR_OF2IE) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100207 timeout = imx_gpt_find_limit(count, s->ocr2, timeout);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100208 }
209 if (s->ir & GPT_IR_OF3IE) {
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100210 timeout = imx_gpt_find_limit(count, s->ocr3, timeout);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100211 }
212
213 /* find the next set of interrupts to raise for next timer event */
214
215 s->next_int = 0;
216 if ((s->ir & GPT_IR_OF1IE) && (timeout == s->ocr1)) {
217 s->next_int |= GPT_SR_OF1;
218 }
219 if ((s->ir & GPT_IR_OF2IE) && (timeout == s->ocr2)) {
220 s->next_int |= GPT_SR_OF2;
221 }
222 if ((s->ir & GPT_IR_OF3IE) && (timeout == s->ocr3)) {
223 s->next_int |= GPT_SR_OF3;
224 }
Michael Tokarev203d65a2014-08-02 00:14:48 +0400225 if ((s->ir & GPT_IR_ROVIE) && (timeout == GPT_TIMER_MAX)) {
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100226 s->next_int |= GPT_SR_ROV;
227 }
228
229 /* the new range to count down from */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100230 limit = timeout - imx_gpt_update_count(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100231
232 if (limit < 0) {
233 /*
234 * if we reach here, then QEMU is running too slow and we pass the
235 * timeout limit while computing it. Let's deliver the interrupt
236 * and compute a new limit.
237 */
238 s->sr |= s->next_int;
239
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100240 imx_gpt_compute_next_timeout(s, event);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100241
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100242 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100243 } else {
244 /* New timeout value */
245 s->next_timeout = timeout;
246
247 /* reset the limit to the computed range */
248 ptimer_set_limit(s->timer, limit, 1);
249 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100250}
251
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100252static uint64_t imx_gpt_read(void *opaque, hwaddr offset, unsigned size)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100253{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100254 IMXGPTState *s = IMX_GPT(opaque);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100255 uint32_t reg_value = 0;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100256
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100257 switch (offset >> 2) {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100258 case 0: /* Control Register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100259 reg_value = s->cr;
260 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100261
262 case 1: /* prescaler */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100263 reg_value = s->pr;
264 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100265
266 case 2: /* Status Register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100267 reg_value = s->sr;
268 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100269
270 case 3: /* Interrupt Register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100271 reg_value = s->ir;
272 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100273
274 case 4: /* Output Compare Register 1 */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100275 reg_value = s->ocr1;
276 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100277
278 case 5: /* Output Compare Register 2 */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100279 reg_value = s->ocr2;
280 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100281
282 case 6: /* Output Compare Register 3 */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100283 reg_value = s->ocr3;
284 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100285
286 case 7: /* input Capture Register 1 */
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100287 qemu_log_mask(LOG_UNIMP, "[%s]%s: icr1 feature is not implemented\n",
288 TYPE_IMX_GPT, __func__);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100289 reg_value = s->icr1;
290 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100291
292 case 8: /* input Capture Register 2 */
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100293 qemu_log_mask(LOG_UNIMP, "[%s]%s: icr2 feature is not implemented\n",
294 TYPE_IMX_GPT, __func__);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100295 reg_value = s->icr2;
296 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100297
298 case 9: /* cnt */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100299 imx_gpt_update_count(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100300 reg_value = s->cnt;
301 break;
302
303 default:
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100304 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
305 HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100306 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100307 }
308
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100309 DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(offset >> 2), reg_value);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100310
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100311 return reg_value;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100312}
313
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100314
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000315static void imx_gpt_reset_common(IMXGPTState *s, bool is_soft_reset)
316{
Peter Maydell1b914992019-10-08 18:17:37 +0100317 ptimer_transaction_begin(s->timer);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100318 /* stop timer */
319 ptimer_stop(s->timer);
320
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000321 /* Soft reset and hard reset differ only in their handling of the CR
322 * register -- soft reset preserves the values of some bits there.
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100323 */
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000324 if (is_soft_reset) {
325 /* Clear all CR bits except those that are preserved by soft reset. */
326 s->cr &= GPT_CR_EN | GPT_CR_ENMOD | GPT_CR_STOPEN | GPT_CR_DOZEN |
327 GPT_CR_WAITEN | GPT_CR_DBGEN |
328 (GPT_CR_CLKSRC_MASK << GPT_CR_CLKSRC_SHIFT);
329 } else {
330 s->cr = 0;
331 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100332 s->sr = 0;
333 s->pr = 0;
334 s->ir = 0;
335 s->cnt = 0;
Michael Tokarev203d65a2014-08-02 00:14:48 +0400336 s->ocr1 = GPT_TIMER_MAX;
337 s->ocr2 = GPT_TIMER_MAX;
338 s->ocr3 = GPT_TIMER_MAX;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100339 s->icr1 = 0;
340 s->icr2 = 0;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100341
Michael Tokarev203d65a2014-08-02 00:14:48 +0400342 s->next_timeout = GPT_TIMER_MAX;
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100343 s->next_int = 0;
344
345 /* compute new freq */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100346 imx_gpt_set_freq(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100347
Michael Tokarev203d65a2014-08-02 00:14:48 +0400348 /* reset the limit to GPT_TIMER_MAX */
349 ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100350
351 /* if the timer is still enabled, restart it */
352 if (s->freq && (s->cr & GPT_CR_EN)) {
353 ptimer_run(s->timer, 1);
354 }
Peter Maydell1b914992019-10-08 18:17:37 +0100355 ptimer_transaction_commit(s->timer);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100356}
357
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000358static void imx_gpt_soft_reset(DeviceState *dev)
359{
360 IMXGPTState *s = IMX_GPT(dev);
361 imx_gpt_reset_common(s, true);
362}
363
364static void imx_gpt_reset(DeviceState *dev)
365{
366 IMXGPTState *s = IMX_GPT(dev);
367 imx_gpt_reset_common(s, false);
368}
369
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100370static void imx_gpt_write(void *opaque, hwaddr offset, uint64_t value,
371 unsigned size)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100372{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100373 IMXGPTState *s = IMX_GPT(opaque);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100374 uint32_t oldreg;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100375
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100376 DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(offset >> 2),
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100377 (uint32_t)value);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100378
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100379 switch (offset >> 2) {
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100380 case 0:
381 oldreg = s->cr;
382 s->cr = value & ~0x7c14;
383 if (s->cr & GPT_CR_SWR) { /* force reset */
384 /* handle the reset */
Kurban Mallachievc98c9eb2017-02-28 12:08:16 +0000385 imx_gpt_soft_reset(DEVICE(s));
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100386 } else {
387 /* set our freq, as the source might have changed */
Peter Maydell1b914992019-10-08 18:17:37 +0100388 ptimer_transaction_begin(s->timer);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100389 imx_gpt_set_freq(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100390
391 if ((oldreg ^ s->cr) & GPT_CR_EN) {
392 if (s->cr & GPT_CR_EN) {
393 if (s->cr & GPT_CR_ENMOD) {
Michael Tokarev203d65a2014-08-02 00:14:48 +0400394 s->next_timeout = GPT_TIMER_MAX;
395 ptimer_set_count(s->timer, GPT_TIMER_MAX);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100396 imx_gpt_compute_next_timeout(s, false);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100397 }
398 ptimer_run(s->timer, 1);
399 } else {
400 /* stop timer */
401 ptimer_stop(s->timer);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100402 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100403 }
Peter Maydell1b914992019-10-08 18:17:37 +0100404 ptimer_transaction_commit(s->timer);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100405 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100406 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100407
408 case 1: /* Prescaler */
409 s->pr = value & 0xfff;
Peter Maydell1b914992019-10-08 18:17:37 +0100410 ptimer_transaction_begin(s->timer);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100411 imx_gpt_set_freq(s);
Peter Maydell1b914992019-10-08 18:17:37 +0100412 ptimer_transaction_commit(s->timer);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100413 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100414
415 case 2: /* SR */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100416 s->sr &= ~(value & 0x3f);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100417 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100418 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100419
420 case 3: /* IR -- interrupt register */
421 s->ir = value & 0x3f;
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100422 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100423
Peter Maydell1b914992019-10-08 18:17:37 +0100424 ptimer_transaction_begin(s->timer);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100425 imx_gpt_compute_next_timeout(s, false);
Peter Maydell1b914992019-10-08 18:17:37 +0100426 ptimer_transaction_commit(s->timer);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100427
428 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100429
430 case 4: /* OCR1 -- output compare register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100431 s->ocr1 = value;
432
Peter Maydell1b914992019-10-08 18:17:37 +0100433 ptimer_transaction_begin(s->timer);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100434 /* In non-freerun mode, reset count when this register is written */
435 if (!(s->cr & GPT_CR_FRR)) {
Michael Tokarev203d65a2014-08-02 00:14:48 +0400436 s->next_timeout = GPT_TIMER_MAX;
437 ptimer_set_limit(s->timer, GPT_TIMER_MAX, 1);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100438 }
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100439
440 /* compute the new timeout */
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100441 imx_gpt_compute_next_timeout(s, false);
Peter Maydell1b914992019-10-08 18:17:37 +0100442 ptimer_transaction_commit(s->timer);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100443
444 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100445
446 case 5: /* OCR2 -- output compare register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100447 s->ocr2 = value;
448
449 /* compute the new timeout */
Peter Maydell1b914992019-10-08 18:17:37 +0100450 ptimer_transaction_begin(s->timer);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100451 imx_gpt_compute_next_timeout(s, false);
Peter Maydell1b914992019-10-08 18:17:37 +0100452 ptimer_transaction_commit(s->timer);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100453
454 break;
455
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100456 case 6: /* OCR3 -- output compare register */
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100457 s->ocr3 = value;
458
459 /* compute the new timeout */
Peter Maydell1b914992019-10-08 18:17:37 +0100460 ptimer_transaction_begin(s->timer);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100461 imx_gpt_compute_next_timeout(s, false);
Peter Maydell1b914992019-10-08 18:17:37 +0100462 ptimer_transaction_commit(s->timer);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100463
464 break;
465
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100466 default:
Jean-Christophe Dubois05453522015-10-25 15:16:26 +0100467 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
468 HWADDR_PRIx "\n", TYPE_IMX_GPT, __func__, offset);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100469 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100470 }
471}
472
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100473static void imx_gpt_timeout(void *opaque)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100474{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100475 IMXGPTState *s = IMX_GPT(opaque);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100476
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100477 DPRINTF("\n");
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100478
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100479 s->sr |= s->next_int;
480 s->next_int = 0;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100481
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100482 imx_gpt_compute_next_timeout(s, true);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100483
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100484 imx_gpt_update_int(s);
Jean-Christophe DUBOIS5ec694b2013-06-25 18:34:13 +0100485
486 if (s->freq && (s->cr & GPT_CR_EN)) {
487 ptimer_run(s->timer, 1);
488 }
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100489}
490
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100491static const MemoryRegionOps imx_gpt_ops = {
492 .read = imx_gpt_read,
493 .write = imx_gpt_write,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100494 .endianness = DEVICE_NATIVE_ENDIAN,
495};
496
497
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100498static void imx_gpt_realize(DeviceState *dev, Error **errp)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100499{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100500 IMXGPTState *s = IMX_GPT(dev);
501 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100502
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100503 sysbus_init_irq(sbd, &s->irq);
Paolo Bonzini853dca12013-06-06 21:25:08 -0400504 memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpt_ops, s, TYPE_IMX_GPT,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100505 0x00001000);
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100506 sysbus_init_mmio(sbd, &s->iomem);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100507
Peter Maydell1b914992019-10-08 18:17:37 +0100508 s->timer = ptimer_init(imx_gpt_timeout, s, PTIMER_POLICY_DEFAULT);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100509}
510
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100511static void imx_gpt_class_init(ObjectClass *klass, void *data)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100512{
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100513 DeviceClass *dc = DEVICE_CLASS(klass);
514
515 dc->realize = imx_gpt_realize;
516 dc->reset = imx_gpt_reset;
517 dc->vmsd = &vmstate_imx_timer_gpt;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100518 dc->desc = "i.MX general timer";
519}
520
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100521static void imx25_gpt_init(Object *obj)
522{
523 IMXGPTState *s = IMX_GPT(obj);
524
525 s->clocks = imx25_gpt_clocks;
526}
527
528static void imx31_gpt_init(Object *obj)
529{
530 IMXGPTState *s = IMX_GPT(obj);
531
532 s->clocks = imx31_gpt_clocks;
533}
534
535static void imx6_gpt_init(Object *obj)
536{
537 IMXGPTState *s = IMX_GPT(obj);
538
539 s->clocks = imx6_gpt_clocks;
540}
541
Andrey Smirnova62bf592018-02-09 10:40:30 +0000542static void imx7_gpt_init(Object *obj)
543{
544 IMXGPTState *s = IMX_GPT(obj);
545
546 s->clocks = imx7_gpt_clocks;
547}
548
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100549static const TypeInfo imx25_gpt_info = {
550 .name = TYPE_IMX25_GPT,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100551 .parent = TYPE_SYS_BUS_DEVICE,
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100552 .instance_size = sizeof(IMXGPTState),
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100553 .instance_init = imx25_gpt_init,
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100554 .class_init = imx_gpt_class_init,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100555};
556
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100557static const TypeInfo imx31_gpt_info = {
558 .name = TYPE_IMX31_GPT,
559 .parent = TYPE_IMX25_GPT,
560 .instance_init = imx31_gpt_init,
561};
562
563static const TypeInfo imx6_gpt_info = {
564 .name = TYPE_IMX6_GPT,
565 .parent = TYPE_IMX25_GPT,
566 .instance_init = imx6_gpt_init,
567};
568
Andrey Smirnova62bf592018-02-09 10:40:30 +0000569static const TypeInfo imx7_gpt_info = {
570 .name = TYPE_IMX7_GPT,
571 .parent = TYPE_IMX25_GPT,
572 .instance_init = imx7_gpt_init,
573};
574
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100575static void imx_gpt_register_types(void)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100576{
Jean-Christophe Dubois66542f62016-07-07 13:47:01 +0100577 type_register_static(&imx25_gpt_info);
578 type_register_static(&imx31_gpt_info);
579 type_register_static(&imx6_gpt_info);
Andrey Smirnova62bf592018-02-09 10:40:30 +0000580 type_register_static(&imx7_gpt_info);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100581}
582
Jean-Christophe DUBOIS67110c32013-06-25 18:34:13 +0100583type_init(imx_gpt_register_types)