4 * Copyright (c) 2008 OK Labs
5 * Copyright (c) 2011 NICTA Pty Ltd
6 * Originally written by Hans Jiang
7 * Updated by Peter Chubb
9 * This code is licensed under GPL version 2 or later. See
10 * the COPYING file in the top-level directory.
15 #include "qemu/timer.h"
20 //#define DEBUG_TIMER 1
22 # define DPRINTF(fmt, args...) \
23 do { printf("imx_timer: " fmt , ##args); } while (0)
25 # define DPRINTF(fmt, args...) do {} while (0)
29 * Define to 1 for messages about attempts to
30 * access unimplemented registers or similar.
32 #define DEBUG_IMPLEMENTATION 1
33 #if DEBUG_IMPLEMENTATION
34 # define IPRINTF(fmt, args...) \
35 do { fprintf(stderr, "imx_timer: " fmt, ##args); } while (0)
37 # define IPRINTF(fmt, args...) do {} while (0)
41 * GPT : General purpose timer
43 * This timer counts up continuously while it is enabled, resetting itself
44 * to 0 when it reaches TIMER_MAX (in freerun mode) or when it
45 * reaches the value of ocr1 (in periodic mode). WE simulate this using a
46 * QEMU ptimer counting down from ocr1 and reloading from ocr1 in
47 * periodic mode, or counting from ocr1 to zero, then TIMER_MAX - ocr1.
48 * waiting_rov is set when counting from TIMER_MAX.
50 * In the real hardware, there are three comparison registers that can
51 * trigger interrupts, and compare channel 1 can be used to
52 * force-reset the timer. However, this is a `bare-bones'
53 * implementation: only what Linux 3.x uses has been implemented
54 * (free-running timer from 0 to OCR1 or TIMER_MAX) .
58 #define TIMER_MAX 0XFFFFFFFFUL
60 /* Control register. Not all of these bits have any effect (yet) */
61 #define GPT_CR_EN (1 << 0) /* GPT Enable */
62 #define GPT_CR_ENMOD (1 << 1) /* GPT Enable Mode */
63 #define GPT_CR_DBGEN (1 << 2) /* GPT Debug mode enable */
64 #define GPT_CR_WAITEN (1 << 3) /* GPT Wait Mode Enable */
65 #define GPT_CR_DOZEN (1 << 4) /* GPT Doze mode enable */
66 #define GPT_CR_STOPEN (1 << 5) /* GPT Stop Mode Enable */
67 #define GPT_CR_CLKSRC_SHIFT (6)
68 #define GPT_CR_CLKSRC_MASK (0x7)
70 #define GPT_CR_FRR (1 << 9) /* Freerun or Restart */
71 #define GPT_CR_SWR (1 << 15) /* Software Reset */
72 #define GPT_CR_IM1 (3 << 16) /* Input capture channel 1 mode (2 bits) */
73 #define GPT_CR_IM2 (3 << 18) /* Input capture channel 2 mode (2 bits) */
74 #define GPT_CR_OM1 (7 << 20) /* Output Compare Channel 1 Mode (3 bits) */
75 #define GPT_CR_OM2 (7 << 23) /* Output Compare Channel 2 Mode (3 bits) */
76 #define GPT_CR_OM3 (7 << 26) /* Output Compare Channel 3 Mode (3 bits) */
77 #define GPT_CR_FO1 (1 << 29) /* Force Output Compare Channel 1 */
78 #define GPT_CR_FO2 (1 << 30) /* Force Output Compare Channel 2 */
79 #define GPT_CR_FO3 (1 << 31) /* Force Output Compare Channel 3 */
81 #define GPT_SR_OF1 (1 << 0)
82 #define GPT_SR_ROV (1 << 5)
84 #define GPT_IR_OF1IE (1 << 0)
85 #define GPT_IR_ROVIE (1 << 5)
100 uint32_t waiting_rov
;
104 static const VMStateDescription vmstate_imx_timerg
= {
105 .name
= "imx-timerg",
107 .minimum_version_id
= 1,
108 .minimum_version_id_old
= 1,
109 .fields
= (VMStateField
[]) {
110 VMSTATE_UINT32(cr
, IMXTimerGState
),
111 VMSTATE_UINT32(pr
, IMXTimerGState
),
112 VMSTATE_UINT32(sr
, IMXTimerGState
),
113 VMSTATE_UINT32(ir
, IMXTimerGState
),
114 VMSTATE_UINT32(ocr1
, IMXTimerGState
),
115 VMSTATE_UINT32(cnt
, IMXTimerGState
),
116 VMSTATE_UINT32(waiting_rov
, IMXTimerGState
),
117 VMSTATE_PTIMER(timer
, IMXTimerGState
),
118 VMSTATE_END_OF_LIST()
122 static const IMXClk imx_timerg_clocks
[] = {
123 NOCLK
, /* 000 No clock source */
124 IPG
, /* 001 ipg_clk, 532MHz*/
125 IPG
, /* 010 ipg_clk_highfreq */
126 NOCLK
, /* 011 not defined */
127 CLK_32k
, /* 100 ipg_clk_32k */
128 NOCLK
, /* 101 not defined */
129 NOCLK
, /* 110 not defined */
130 NOCLK
, /* 111 not defined */
134 static void imx_timerg_set_freq(IMXTimerGState
*s
)
139 clksrc
= (s
->cr
>> GPT_CR_CLKSRC_SHIFT
) & GPT_CR_CLKSRC_MASK
;
140 freq
= imx_clock_frequency(s
->ccm
, imx_timerg_clocks
[clksrc
]) / (1 + s
->pr
);
142 DPRINTF("Setting gtimer clksrc %d to frequency %d\n", clksrc
, freq
);
144 ptimer_set_freq(s
->timer
, freq
);
148 static void imx_timerg_update(IMXTimerGState
*s
)
150 uint32_t flags
= s
->sr
& s
->ir
& (GPT_SR_OF1
| GPT_SR_ROV
);
152 DPRINTF("g-timer SR: %s %s IR=%s %s, %s\n",
153 s
->sr
& GPT_SR_OF1
? "OF1" : "",
154 s
->sr
& GPT_SR_ROV
? "ROV" : "",
155 s
->ir
& GPT_SR_OF1
? "OF1" : "",
156 s
->ir
& GPT_SR_ROV
? "ROV" : "",
157 s
->cr
& GPT_CR_EN
? "CR_EN" : "Not Enabled");
160 qemu_set_irq(s
->irq
, (s
->cr
& GPT_CR_EN
) && flags
);
163 static uint32_t imx_timerg_update_counts(IMXTimerGState
*s
)
165 uint64_t target
= s
->waiting_rov
? TIMER_MAX
: s
->ocr1
;
166 uint64_t cnt
= ptimer_get_count(s
->timer
);
167 s
->cnt
= target
- cnt
;
171 static void imx_timerg_reload(IMXTimerGState
*s
, uint32_t timeout
)
175 if (!(s
->cr
& GPT_CR_FRR
)) {
176 IPRINTF("IMX_timerg_reload --- called in reset-mode\n");
181 * For small timeouts, qemu sometimes runs too slow.
182 * Better deliver a late interrupt than none.
184 * In Reset mode (FRR bit clear)
185 * the ptimer reloads itself from OCR1;
186 * in free-running mode we need to fake
187 * running from 0 to ocr1 to TIMER_MAX
189 if (timeout
> s
->cnt
) {
190 diff_cnt
= timeout
- s
->cnt
;
194 ptimer_set_count(s
->timer
, diff_cnt
);
197 static uint64_t imx_timerg_read(void *opaque
, hwaddr offset
,
200 IMXTimerGState
*s
= (IMXTimerGState
*)opaque
;
202 DPRINTF("g-read(offset=%x)", offset
>> 2);
203 switch (offset
>> 2) {
204 case 0: /* Control Register */
205 DPRINTF(" cr = %x\n", s
->cr
);
208 case 1: /* prescaler */
209 DPRINTF(" pr = %x\n", s
->pr
);
212 case 2: /* Status Register */
213 DPRINTF(" sr = %x\n", s
->sr
);
216 case 3: /* Interrupt Register */
217 DPRINTF(" ir = %x\n", s
->ir
);
220 case 4: /* Output Compare Register 1 */
221 DPRINTF(" ocr1 = %x\n", s
->ocr1
);
226 imx_timerg_update_counts(s
);
227 DPRINTF(" cnt = %x\n", s
->cnt
);
231 IPRINTF("imx_timerg_read: Bad offset %x\n",
236 static void imx_timerg_reset(DeviceState
*dev
)
238 IMXTimerGState
*s
= container_of(dev
, IMXTimerGState
, busdev
.qdev
);
241 * Soft reset doesn't touch some bits; hard reset clears them
243 s
->cr
&= ~(GPT_CR_EN
|GPT_CR_DOZEN
|GPT_CR_WAITEN
|GPT_CR_DBGEN
);
249 ptimer_stop(s
->timer
);
250 ptimer_set_limit(s
->timer
, TIMER_MAX
, 1);
251 imx_timerg_set_freq(s
);
254 static void imx_timerg_write(void *opaque
, hwaddr offset
,
255 uint64_t value
, unsigned size
)
257 IMXTimerGState
*s
= (IMXTimerGState
*)opaque
;
258 DPRINTF("g-write(offset=%x, value = 0x%x)\n", (unsigned int)offset
>> 2,
259 (unsigned int)value
);
261 switch (offset
>> 2) {
263 uint32_t oldcr
= s
->cr
;
265 if (value
& GPT_CR_SWR
) { /* force reset */
266 value
&= ~GPT_CR_SWR
;
267 imx_timerg_reset(&s
->busdev
.qdev
);
268 imx_timerg_update(s
);
271 s
->cr
= value
& ~0x7c00;
272 imx_timerg_set_freq(s
);
273 if ((oldcr
^ value
) & GPT_CR_EN
) {
274 if (value
& GPT_CR_EN
) {
275 if (value
& GPT_CR_ENMOD
) {
276 ptimer_set_count(s
->timer
, s
->ocr1
);
280 (value
& GPT_CR_FRR
) && (s
->ocr1
!= TIMER_MAX
));
282 ptimer_stop(s
->timer
);
288 case 1: /* Prescaler */
289 s
->pr
= value
& 0xfff;
290 imx_timerg_set_freq(s
);
295 * No point in implementing the status register bits to do with
296 * external interrupt sources.
298 value
&= GPT_SR_OF1
| GPT_SR_ROV
;
300 imx_timerg_update(s
);
303 case 3: /* IR -- interrupt register */
304 s
->ir
= value
& 0x3f;
305 imx_timerg_update(s
);
308 case 4: /* OCR1 -- output compare register */
309 /* In non-freerun mode, reset count when this register is written */
310 if (!(s
->cr
& GPT_CR_FRR
)) {
312 ptimer_set_limit(s
->timer
, value
, 1);
314 imx_timerg_update_counts(s
);
315 if (value
> s
->cnt
) {
317 imx_timerg_reload(s
, value
);
320 imx_timerg_reload(s
, TIMER_MAX
- s
->cnt
);
327 IPRINTF("imx_timerg_write: Bad offset %x\n",
332 static void imx_timerg_timeout(void *opaque
)
334 IMXTimerGState
*s
= (IMXTimerGState
*)opaque
;
336 DPRINTF("imx_timerg_timeout, waiting rov=%d\n", s
->waiting_rov
);
337 if (s
->cr
& GPT_CR_FRR
) {
339 * Free running timer from 0 -> TIMERMAX
340 * Generates interrupt at TIMER_MAX and at cnt==ocr1
341 * If ocr1 == TIMER_MAX, then no need to reload timer.
343 if (s
->ocr1
== TIMER_MAX
) {
344 DPRINTF("s->ocr1 == TIMER_MAX, FRR\n");
345 s
->sr
|= GPT_SR_OF1
| GPT_SR_ROV
;
346 imx_timerg_update(s
);
350 if (s
->waiting_rov
) {
352 * We were waiting for cnt==TIMER_MAX
357 imx_timerg_reload(s
, s
->ocr1
);
359 /* Must have got a cnt==ocr1 timeout. */
363 imx_timerg_reload(s
, TIMER_MAX
);
365 imx_timerg_update(s
);
370 imx_timerg_update(s
);
373 static const MemoryRegionOps imx_timerg_ops
= {
374 .read
= imx_timerg_read
,
375 .write
= imx_timerg_write
,
376 .endianness
= DEVICE_NATIVE_ENDIAN
,
380 static int imx_timerg_init(SysBusDevice
*dev
)
382 IMXTimerGState
*s
= FROM_SYSBUS(IMXTimerGState
, dev
);
385 sysbus_init_irq(dev
, &s
->irq
);
386 memory_region_init_io(&s
->iomem
, &imx_timerg_ops
,
389 sysbus_init_mmio(dev
, &s
->iomem
);
391 bh
= qemu_bh_new(imx_timerg_timeout
, s
);
392 s
->timer
= ptimer_init(bh
);
394 /* Hard reset resets extra bits in CR */
402 * EPIT: Enhanced periodic interrupt timer
405 #define CR_EN (1 << 0)
406 #define CR_ENMOD (1 << 1)
407 #define CR_OCIEN (1 << 2)
408 #define CR_RLD (1 << 3)
409 #define CR_PRESCALE_SHIFT (4)
410 #define CR_PRESCALE_MASK (0xfff)
411 #define CR_SWR (1 << 16)
412 #define CR_IOVW (1 << 17)
413 #define CR_DBGEN (1 << 18)
414 #define CR_EPIT (1 << 19)
415 #define CR_DOZEN (1 << 20)
416 #define CR_STOPEN (1 << 21)
417 #define CR_CLKSRC_SHIFT (24)
418 #define CR_CLKSRC_MASK (0x3 << CR_CLKSRC_SHIFT)
422 * Exact clock frequencies vary from board to board.
425 static const IMXClk imx_timerp_clocks
[] = {
427 IPG
, /* ipg_clk, ~532MHz */
428 IPG
, /* ipg_clk_highfreq */
429 CLK_32k
, /* ipg_clk_32k -- ~32kHz */
448 * Update interrupt status
450 static void imx_timerp_update(IMXTimerPState
*s
)
452 if (s
->int_level
&& (s
->cr
& CR_OCIEN
)) {
453 qemu_irq_raise(s
->irq
);
455 qemu_irq_lower(s
->irq
);
459 static void imx_timerp_reset(DeviceState
*dev
)
461 IMXTimerPState
*s
= container_of(dev
, IMXTimerPState
, busdev
.qdev
);
467 ptimer_stop(s
->timer
);
468 ptimer_set_count(s
->timer
, TIMER_MAX
);
471 static uint64_t imx_timerp_read(void *opaque
, hwaddr offset
,
474 IMXTimerPState
*s
= (IMXTimerPState
*)opaque
;
476 DPRINTF("p-read(offset=%x)", offset
>> 2);
477 switch (offset
>> 2) {
478 case 0: /* Control Register */
479 DPRINTF("cr %x\n", s
->cr
);
482 case 1: /* Status Register */
483 DPRINTF("int_level %x\n", s
->int_level
);
486 case 2: /* LR - ticks*/
487 DPRINTF("lr %x\n", s
->lr
);
491 DPRINTF("cmp %x\n", s
->cmp
);
495 return ptimer_get_count(s
->timer
);
497 IPRINTF("imx_timerp_read: Bad offset %x\n",
502 static void set_timerp_freq(IMXTimerPState
*s
)
508 clksrc
= (s
->cr
& CR_CLKSRC_MASK
) >> CR_CLKSRC_SHIFT
;
509 prescaler
= 1 + ((s
->cr
>> CR_PRESCALE_SHIFT
) & CR_PRESCALE_MASK
);
510 freq
= imx_clock_frequency(s
->ccm
, imx_timerp_clocks
[clksrc
]) / prescaler
;
513 DPRINTF("Setting ptimer frequency to %u\n", freq
);
516 ptimer_set_freq(s
->timer
, freq
);
520 static void imx_timerp_write(void *opaque
, hwaddr offset
,
521 uint64_t value
, unsigned size
)
523 IMXTimerPState
*s
= (IMXTimerPState
*)opaque
;
524 DPRINTF("p-write(offset=%x, value = %x)\n", (unsigned int)offset
>> 2,
525 (unsigned int)value
);
527 switch (offset
>> 2) {
529 if (value
& CR_SWR
) {
530 imx_timerp_reset(&s
->busdev
.qdev
);
533 s
->cr
= value
& 0x03ffffff;
536 if (s
->freq
&& (s
->cr
& CR_EN
)) {
537 if (!(s
->cr
& CR_ENMOD
)) {
538 ptimer_set_count(s
->timer
, s
->lr
);
540 ptimer_run(s
->timer
, 0);
542 ptimer_stop(s
->timer
);
546 case 1: /* SR - ACK*/
548 imx_timerp_update(s
);
551 case 2: /* LR - set ticks */
553 ptimer_set_limit(s
->timer
, value
, !!(s
->cr
& CR_IOVW
));
560 "Values for EPIT comparison other than zero not supported\n"
566 IPRINTF("imx_timerp_write: Bad offset %x\n",
571 static void imx_timerp_tick(void *opaque
)
573 IMXTimerPState
*s
= (IMXTimerPState
*)opaque
;
575 DPRINTF("imxp tick\n");
576 if (!(s
->cr
& CR_RLD
)) {
577 ptimer_set_count(s
->timer
, TIMER_MAX
);
580 imx_timerp_update(s
);
583 void imx_timerp_create(const hwaddr addr
,
590 dev
= sysbus_create_simple("imx_timerp", addr
, irq
);
591 pp
= container_of(dev
, IMXTimerPState
, busdev
.qdev
);
595 static const MemoryRegionOps imx_timerp_ops
= {
596 .read
= imx_timerp_read
,
597 .write
= imx_timerp_write
,
598 .endianness
= DEVICE_NATIVE_ENDIAN
,
601 static const VMStateDescription vmstate_imx_timerp
= {
602 .name
= "imx-timerp",
604 .minimum_version_id
= 1,
605 .minimum_version_id_old
= 1,
606 .fields
= (VMStateField
[]) {
607 VMSTATE_UINT32(cr
, IMXTimerPState
),
608 VMSTATE_UINT32(lr
, IMXTimerPState
),
609 VMSTATE_UINT32(cmp
, IMXTimerPState
),
610 VMSTATE_UINT32(freq
, IMXTimerPState
),
611 VMSTATE_INT32(int_level
, IMXTimerPState
),
612 VMSTATE_PTIMER(timer
, IMXTimerPState
),
613 VMSTATE_END_OF_LIST()
617 static int imx_timerp_init(SysBusDevice
*dev
)
619 IMXTimerPState
*s
= FROM_SYSBUS(IMXTimerPState
, dev
);
622 DPRINTF("imx_timerp_init\n");
624 sysbus_init_irq(dev
, &s
->irq
);
625 memory_region_init_io(&s
->iomem
, &imx_timerp_ops
,
628 sysbus_init_mmio(dev
, &s
->iomem
);
630 bh
= qemu_bh_new(imx_timerp_tick
, s
);
631 s
->timer
= ptimer_init(bh
);
637 void imx_timerg_create(const hwaddr addr
,
644 dev
= sysbus_create_simple("imx_timerg", addr
, irq
);
645 pp
= container_of(dev
, IMXTimerGState
, busdev
.qdev
);
649 static void imx_timerg_class_init(ObjectClass
*klass
, void *data
)
651 DeviceClass
*dc
= DEVICE_CLASS(klass
);
652 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
653 k
->init
= imx_timerg_init
;
654 dc
->vmsd
= &vmstate_imx_timerg
;
655 dc
->reset
= imx_timerg_reset
;
656 dc
->desc
= "i.MX general timer";
659 static void imx_timerp_class_init(ObjectClass
*klass
, void *data
)
661 DeviceClass
*dc
= DEVICE_CLASS(klass
);
662 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
663 k
->init
= imx_timerp_init
;
664 dc
->vmsd
= &vmstate_imx_timerp
;
665 dc
->reset
= imx_timerp_reset
;
666 dc
->desc
= "i.MX periodic timer";
669 static const TypeInfo imx_timerp_info
= {
670 .name
= "imx_timerp",
671 .parent
= TYPE_SYS_BUS_DEVICE
,
672 .instance_size
= sizeof(IMXTimerPState
),
673 .class_init
= imx_timerp_class_init
,
676 static const TypeInfo imx_timerg_info
= {
677 .name
= "imx_timerg",
678 .parent
= TYPE_SYS_BUS_DEVICE
,
679 .instance_size
= sizeof(IMXTimerGState
),
680 .class_init
= imx_timerg_class_init
,
683 static void imx_timer_register_types(void)
685 type_register_static(&imx_timerp_info
);
686 type_register_static(&imx_timerg_info
);
689 type_init(imx_timer_register_types
)