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
10 * This code is licensed under GPL version 2 or later. See
11 * the COPYING file in the top-level directory.
16 #include "qemu/bitops.h"
17 #include "qemu/timer.h"
18 #include "hw/ptimer.h"
19 #include "hw/sysbus.h"
20 #include "hw/arm/imx.h"
22 #define TYPE_IMX_GPT "imx.gpt"
25 * Define to 1 for debug messages
30 static char const *imx_gpt_reg_name(uint32_t reg
)
58 # define DPRINTF(fmt, args...) \
59 do { printf("%s: " fmt , __func__, ##args); } while (0)
61 # define DPRINTF(fmt, args...) do {} while (0)
65 * Define to 1 for messages about attempts to
66 * access unimplemented registers or similar.
68 #define DEBUG_IMPLEMENTATION 1
69 #if DEBUG_IMPLEMENTATION
70 # define IPRINTF(fmt, args...) \
71 do { fprintf(stderr, "%s: " fmt, __func__, ##args); } while (0)
73 # define IPRINTF(fmt, args...) do {} while (0)
76 #define IMX_GPT(obj) \
77 OBJECT_CHECK(IMXGPTState, (obj), TYPE_IMX_GPT)
79 * GPT : General purpose timer
81 * This timer counts up continuously while it is enabled, resetting itself
82 * to 0 when it reaches TIMER_MAX (in freerun mode) or when it
83 * reaches the value of one of the ocrX (in periodic mode).
86 #define TIMER_MAX 0XFFFFFFFFUL
88 /* Control register. Not all of these bits have any effect (yet) */
89 #define GPT_CR_EN (1 << 0) /* GPT Enable */
90 #define GPT_CR_ENMOD (1 << 1) /* GPT Enable Mode */
91 #define GPT_CR_DBGEN (1 << 2) /* GPT Debug mode enable */
92 #define GPT_CR_WAITEN (1 << 3) /* GPT Wait Mode Enable */
93 #define GPT_CR_DOZEN (1 << 4) /* GPT Doze mode enable */
94 #define GPT_CR_STOPEN (1 << 5) /* GPT Stop Mode Enable */
95 #define GPT_CR_CLKSRC_SHIFT (6)
96 #define GPT_CR_CLKSRC_MASK (0x7)
98 #define GPT_CR_FRR (1 << 9) /* Freerun or Restart */
99 #define GPT_CR_SWR (1 << 15) /* Software Reset */
100 #define GPT_CR_IM1 (3 << 16) /* Input capture channel 1 mode (2 bits) */
101 #define GPT_CR_IM2 (3 << 18) /* Input capture channel 2 mode (2 bits) */
102 #define GPT_CR_OM1 (7 << 20) /* Output Compare Channel 1 Mode (3 bits) */
103 #define GPT_CR_OM2 (7 << 23) /* Output Compare Channel 2 Mode (3 bits) */
104 #define GPT_CR_OM3 (7 << 26) /* Output Compare Channel 3 Mode (3 bits) */
105 #define GPT_CR_FO1 (1 << 29) /* Force Output Compare Channel 1 */
106 #define GPT_CR_FO2 (1 << 30) /* Force Output Compare Channel 2 */
107 #define GPT_CR_FO3 (1 << 31) /* Force Output Compare Channel 3 */
109 #define GPT_SR_OF1 (1 << 0)
110 #define GPT_SR_OF2 (1 << 1)
111 #define GPT_SR_OF3 (1 << 2)
112 #define GPT_SR_ROV (1 << 5)
114 #define GPT_IR_OF1IE (1 << 0)
115 #define GPT_IR_OF2IE (1 << 1)
116 #define GPT_IR_OF3IE (1 << 2)
117 #define GPT_IR_ROVIE (1 << 5)
136 uint32_t next_timeout
;
144 static const VMStateDescription vmstate_imx_timer_gpt
= {
145 .name
= TYPE_IMX_GPT
,
147 .minimum_version_id
= 3,
148 .minimum_version_id_old
= 3,
149 .fields
= (VMStateField
[]) {
150 VMSTATE_UINT32(cr
, IMXGPTState
),
151 VMSTATE_UINT32(pr
, IMXGPTState
),
152 VMSTATE_UINT32(sr
, IMXGPTState
),
153 VMSTATE_UINT32(ir
, IMXGPTState
),
154 VMSTATE_UINT32(ocr1
, IMXGPTState
),
155 VMSTATE_UINT32(ocr2
, IMXGPTState
),
156 VMSTATE_UINT32(ocr3
, IMXGPTState
),
157 VMSTATE_UINT32(icr1
, IMXGPTState
),
158 VMSTATE_UINT32(icr2
, IMXGPTState
),
159 VMSTATE_UINT32(cnt
, IMXGPTState
),
160 VMSTATE_UINT32(next_timeout
, IMXGPTState
),
161 VMSTATE_UINT32(next_int
, IMXGPTState
),
162 VMSTATE_UINT32(freq
, IMXGPTState
),
163 VMSTATE_PTIMER(timer
, IMXGPTState
),
164 VMSTATE_END_OF_LIST()
168 static const IMXClk imx_gpt_clocks
[] = {
169 NOCLK
, /* 000 No clock source */
170 IPG
, /* 001 ipg_clk, 532MHz*/
171 IPG
, /* 010 ipg_clk_highfreq */
172 NOCLK
, /* 011 not defined */
173 CLK_32k
, /* 100 ipg_clk_32k */
174 NOCLK
, /* 101 not defined */
175 NOCLK
, /* 110 not defined */
176 NOCLK
, /* 111 not defined */
179 static void imx_gpt_set_freq(IMXGPTState
*s
)
181 uint32_t clksrc
= extract32(s
->cr
, GPT_CR_CLKSRC_SHIFT
, 3);
182 uint32_t freq
= imx_clock_frequency(s
->ccm
, imx_gpt_clocks
[clksrc
])
186 DPRINTF("Setting clksrc %d to frequency %d\n", clksrc
, freq
);
189 ptimer_set_freq(s
->timer
, freq
);
193 static void imx_gpt_update_int(IMXGPTState
*s
)
195 if ((s
->sr
& s
->ir
) && (s
->cr
& GPT_CR_EN
)) {
196 qemu_irq_raise(s
->irq
);
198 qemu_irq_lower(s
->irq
);
202 static uint32_t imx_gpt_update_count(IMXGPTState
*s
)
204 s
->cnt
= s
->next_timeout
- (uint32_t)ptimer_get_count(s
->timer
);
209 static inline uint32_t imx_gpt_find_limit(uint32_t count
, uint32_t reg
,
212 if ((count
< reg
) && (timeout
> reg
)) {
219 static void imx_gpt_compute_next_timeout(IMXGPTState
*s
, bool event
)
221 uint32_t timeout
= TIMER_MAX
;
225 if (!(s
->cr
& GPT_CR_EN
)) {
226 /* if not enabled just return */
231 /* This is a timer event */
233 if ((s
->cr
& GPT_CR_FRR
) && (s
->next_timeout
!= TIMER_MAX
)) {
235 * if we are in free running mode and we have not reached
236 * the TIMER_MAX limit, then update the count
238 count
= imx_gpt_update_count(s
);
241 /* not a timer event, then just update the count */
243 count
= imx_gpt_update_count(s
);
246 /* now, find the next timeout related to count */
248 if (s
->ir
& GPT_IR_OF1IE
) {
249 timeout
= imx_gpt_find_limit(count
, s
->ocr1
, timeout
);
251 if (s
->ir
& GPT_IR_OF2IE
) {
252 timeout
= imx_gpt_find_limit(count
, s
->ocr2
, timeout
);
254 if (s
->ir
& GPT_IR_OF3IE
) {
255 timeout
= imx_gpt_find_limit(count
, s
->ocr3
, timeout
);
258 /* find the next set of interrupts to raise for next timer event */
261 if ((s
->ir
& GPT_IR_OF1IE
) && (timeout
== s
->ocr1
)) {
262 s
->next_int
|= GPT_SR_OF1
;
264 if ((s
->ir
& GPT_IR_OF2IE
) && (timeout
== s
->ocr2
)) {
265 s
->next_int
|= GPT_SR_OF2
;
267 if ((s
->ir
& GPT_IR_OF3IE
) && (timeout
== s
->ocr3
)) {
268 s
->next_int
|= GPT_SR_OF3
;
270 if ((s
->ir
& GPT_IR_ROVIE
) && (timeout
== TIMER_MAX
)) {
271 s
->next_int
|= GPT_SR_ROV
;
274 /* the new range to count down from */
275 limit
= timeout
- imx_gpt_update_count(s
);
279 * if we reach here, then QEMU is running too slow and we pass the
280 * timeout limit while computing it. Let's deliver the interrupt
281 * and compute a new limit.
283 s
->sr
|= s
->next_int
;
285 imx_gpt_compute_next_timeout(s
, event
);
287 imx_gpt_update_int(s
);
289 /* New timeout value */
290 s
->next_timeout
= timeout
;
292 /* reset the limit to the computed range */
293 ptimer_set_limit(s
->timer
, limit
, 1);
297 static uint64_t imx_gpt_read(void *opaque
, hwaddr offset
, unsigned size
)
299 IMXGPTState
*s
= IMX_GPT(opaque
);
300 uint32_t reg_value
= 0;
301 uint32_t reg
= offset
>> 2;
304 case 0: /* Control Register */
308 case 1: /* prescaler */
312 case 2: /* Status Register */
316 case 3: /* Interrupt Register */
320 case 4: /* Output Compare Register 1 */
324 case 5: /* Output Compare Register 2 */
328 case 6: /* Output Compare Register 3 */
332 case 7: /* input Capture Register 1 */
333 qemu_log_mask(LOG_UNIMP
, "icr1 feature is not implemented\n");
337 case 8: /* input Capture Register 2 */
338 qemu_log_mask(LOG_UNIMP
, "icr2 feature is not implemented\n");
343 imx_gpt_update_count(s
);
348 IPRINTF("Bad offset %x\n", reg
);
352 DPRINTF("(%s) = 0x%08x\n", imx_gpt_reg_name(reg
), reg_value
);
357 static void imx_gpt_reset(DeviceState
*dev
)
359 IMXGPTState
*s
= IMX_GPT(dev
);
362 ptimer_stop(s
->timer
);
365 * Soft reset doesn't touch some bits; hard reset clears them
367 s
->cr
&= ~(GPT_CR_EN
|GPT_CR_ENMOD
|GPT_CR_STOPEN
|GPT_CR_DOZEN
|
368 GPT_CR_WAITEN
|GPT_CR_DBGEN
);
379 s
->next_timeout
= TIMER_MAX
;
382 /* compute new freq */
385 /* reset the limit to TIMER_MAX */
386 ptimer_set_limit(s
->timer
, TIMER_MAX
, 1);
388 /* if the timer is still enabled, restart it */
389 if (s
->freq
&& (s
->cr
& GPT_CR_EN
)) {
390 ptimer_run(s
->timer
, 1);
394 static void imx_gpt_write(void *opaque
, hwaddr offset
, uint64_t value
,
397 IMXGPTState
*s
= IMX_GPT(opaque
);
399 uint32_t reg
= offset
>> 2;
401 DPRINTF("(%s, value = 0x%08x)\n", imx_gpt_reg_name(reg
),
407 s
->cr
= value
& ~0x7c14;
408 if (s
->cr
& GPT_CR_SWR
) { /* force reset */
409 /* handle the reset */
410 imx_gpt_reset(DEVICE(s
));
412 /* set our freq, as the source might have changed */
415 if ((oldreg
^ s
->cr
) & GPT_CR_EN
) {
416 if (s
->cr
& GPT_CR_EN
) {
417 if (s
->cr
& GPT_CR_ENMOD
) {
418 s
->next_timeout
= TIMER_MAX
;
419 ptimer_set_count(s
->timer
, TIMER_MAX
);
420 imx_gpt_compute_next_timeout(s
, false);
422 ptimer_run(s
->timer
, 1);
425 ptimer_stop(s
->timer
);
431 case 1: /* Prescaler */
432 s
->pr
= value
& 0xfff;
437 s
->sr
&= ~(value
& 0x3f);
438 imx_gpt_update_int(s
);
441 case 3: /* IR -- interrupt register */
442 s
->ir
= value
& 0x3f;
443 imx_gpt_update_int(s
);
445 imx_gpt_compute_next_timeout(s
, false);
449 case 4: /* OCR1 -- output compare register */
452 /* In non-freerun mode, reset count when this register is written */
453 if (!(s
->cr
& GPT_CR_FRR
)) {
454 s
->next_timeout
= TIMER_MAX
;
455 ptimer_set_limit(s
->timer
, TIMER_MAX
, 1);
458 /* compute the new timeout */
459 imx_gpt_compute_next_timeout(s
, false);
463 case 5: /* OCR2 -- output compare register */
466 /* compute the new timeout */
467 imx_gpt_compute_next_timeout(s
, false);
471 case 6: /* OCR3 -- output compare register */
474 /* compute the new timeout */
475 imx_gpt_compute_next_timeout(s
, false);
480 IPRINTF("Bad offset %x\n", reg
);
485 static void imx_gpt_timeout(void *opaque
)
487 IMXGPTState
*s
= IMX_GPT(opaque
);
491 s
->sr
|= s
->next_int
;
494 imx_gpt_compute_next_timeout(s
, true);
496 imx_gpt_update_int(s
);
498 if (s
->freq
&& (s
->cr
& GPT_CR_EN
)) {
499 ptimer_run(s
->timer
, 1);
503 static const MemoryRegionOps imx_gpt_ops
= {
504 .read
= imx_gpt_read
,
505 .write
= imx_gpt_write
,
506 .endianness
= DEVICE_NATIVE_ENDIAN
,
510 static void imx_gpt_realize(DeviceState
*dev
, Error
**errp
)
512 IMXGPTState
*s
= IMX_GPT(dev
);
513 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
516 sysbus_init_irq(sbd
, &s
->irq
);
517 memory_region_init_io(&s
->iomem
, OBJECT(s
), &imx_gpt_ops
, s
, TYPE_IMX_GPT
,
519 sysbus_init_mmio(sbd
, &s
->iomem
);
521 bh
= qemu_bh_new(imx_gpt_timeout
, s
);
522 s
->timer
= ptimer_init(bh
);
525 void imx_timerg_create(const hwaddr addr
, qemu_irq irq
, DeviceState
*ccm
)
530 dev
= sysbus_create_simple(TYPE_IMX_GPT
, addr
, irq
);
535 static void imx_gpt_class_init(ObjectClass
*klass
, void *data
)
537 DeviceClass
*dc
= DEVICE_CLASS(klass
);
539 dc
->realize
= imx_gpt_realize
;
540 dc
->reset
= imx_gpt_reset
;
541 dc
->vmsd
= &vmstate_imx_timer_gpt
;
542 dc
->desc
= "i.MX general timer";
545 static const TypeInfo imx_gpt_info
= {
546 .name
= TYPE_IMX_GPT
,
547 .parent
= TYPE_SYS_BUS_DEVICE
,
548 .instance_size
= sizeof(IMXGPTState
),
549 .class_init
= imx_gpt_class_init
,
552 static void imx_gpt_register_types(void)
554 type_register_static(&imx_gpt_info
);
557 type_init(imx_gpt_register_types
)