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 <jcd@tribudubois.net>
10 * This code is licensed under GPL version 2 or later. See
11 * the COPYING file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "hw/timer/imx_epit.h"
17 #include "hw/misc/imx_ccm.h"
18 #include "qemu/main-loop.h"
20 #ifndef DEBUG_IMX_EPIT
21 #define DEBUG_IMX_EPIT 0
24 #define DPRINTF(fmt, args...) \
26 if (DEBUG_IMX_EPIT) { \
27 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_EPIT, \
32 static char const *imx_epit_reg_name(uint32_t reg
)
51 * Exact clock frequencies vary from board to board.
54 static const IMXClk imx_epit_clocks
[] = {
55 CLK_NONE
, /* 00 disabled */
56 CLK_IPG
, /* 01 ipg_clk, ~532MHz */
57 CLK_IPG_HIGH
, /* 10 ipg_clk_highfreq */
58 CLK_32k
, /* 11 ipg_clk_32k -- ~32kHz */
62 * Update interrupt status
64 static void imx_epit_update_int(IMXEPITState
*s
)
66 if (s
->sr
&& (s
->cr
& CR_OCIEN
) && (s
->cr
& CR_EN
)) {
67 qemu_irq_raise(s
->irq
);
69 qemu_irq_lower(s
->irq
);
73 static void imx_epit_set_freq(IMXEPITState
*s
)
78 clksrc
= extract32(s
->cr
, CR_CLKSRC_SHIFT
, 2);
79 prescaler
= 1 + extract32(s
->cr
, CR_PRESCALE_SHIFT
, 12);
81 s
->freq
= imx_ccm_get_clock_frequency(s
->ccm
,
82 imx_epit_clocks
[clksrc
]) / prescaler
;
84 DPRINTF("Setting ptimer frequency to %u\n", s
->freq
);
87 ptimer_set_freq(s
->timer_reload
, s
->freq
);
88 ptimer_set_freq(s
->timer_cmp
, s
->freq
);
92 static void imx_epit_reset(DeviceState
*dev
)
94 IMXEPITState
*s
= IMX_EPIT(dev
);
97 * Soft reset doesn't touch some bits; hard reset clears them
99 s
->cr
&= (CR_EN
|CR_ENMOD
|CR_STOPEN
|CR_DOZEN
|CR_WAITEN
|CR_DBGEN
);
101 s
->lr
= EPIT_TIMER_MAX
;
104 /* stop both timers */
105 ptimer_stop(s
->timer_cmp
);
106 ptimer_stop(s
->timer_reload
);
107 /* compute new frequency */
108 imx_epit_set_freq(s
);
109 /* init both timers to EPIT_TIMER_MAX */
110 ptimer_set_limit(s
->timer_cmp
, EPIT_TIMER_MAX
, 1);
111 ptimer_set_limit(s
->timer_reload
, EPIT_TIMER_MAX
, 1);
112 if (s
->freq
&& (s
->cr
& CR_EN
)) {
113 /* if the timer is still enabled, restart it */
114 ptimer_run(s
->timer_reload
, 0);
118 static uint32_t imx_epit_update_count(IMXEPITState
*s
)
120 s
->cnt
= ptimer_get_count(s
->timer_reload
);
125 static uint64_t imx_epit_read(void *opaque
, hwaddr offset
, unsigned size
)
127 IMXEPITState
*s
= IMX_EPIT(opaque
);
128 uint32_t reg_value
= 0;
130 switch (offset
>> 2) {
131 case 0: /* Control Register */
135 case 1: /* Status Register */
139 case 2: /* LR - ticks*/
148 imx_epit_update_count(s
);
153 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
154 HWADDR_PRIx
"\n", TYPE_IMX_EPIT
, __func__
, offset
);
158 DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(offset
>> 2), reg_value
);
163 static void imx_epit_reload_compare_timer(IMXEPITState
*s
)
165 if ((s
->cr
& (CR_EN
| CR_OCIEN
)) == (CR_EN
| CR_OCIEN
)) {
166 /* if the compare feature is on and timers are running */
167 uint32_t tmp
= imx_epit_update_count(s
);
170 /* It'll fire in this round of the timer */
172 } else { /* catch it next time around */
173 next
= tmp
- s
->cmp
+ ((s
->cr
& CR_RLD
) ? EPIT_TIMER_MAX
: s
->lr
);
175 ptimer_set_count(s
->timer_cmp
, next
);
179 static void imx_epit_write(void *opaque
, hwaddr offset
, uint64_t value
,
182 IMXEPITState
*s
= IMX_EPIT(opaque
);
185 DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(offset
>> 2),
188 switch (offset
>> 2) {
192 s
->cr
= value
& 0x03ffffff;
193 if (s
->cr
& CR_SWR
) {
194 /* handle the reset */
195 imx_epit_reset(DEVICE(s
));
197 imx_epit_set_freq(s
);
200 if (s
->freq
&& (s
->cr
& CR_EN
) && !(oldcr
& CR_EN
)) {
201 if (s
->cr
& CR_ENMOD
) {
202 if (s
->cr
& CR_RLD
) {
203 ptimer_set_limit(s
->timer_reload
, s
->lr
, 1);
204 ptimer_set_limit(s
->timer_cmp
, s
->lr
, 1);
206 ptimer_set_limit(s
->timer_reload
, EPIT_TIMER_MAX
, 1);
207 ptimer_set_limit(s
->timer_cmp
, EPIT_TIMER_MAX
, 1);
211 imx_epit_reload_compare_timer(s
);
212 ptimer_run(s
->timer_reload
, 0);
213 if (s
->cr
& CR_OCIEN
) {
214 ptimer_run(s
->timer_cmp
, 0);
216 ptimer_stop(s
->timer_cmp
);
218 } else if (!(s
->cr
& CR_EN
)) {
219 /* stop both timers */
220 ptimer_stop(s
->timer_reload
);
221 ptimer_stop(s
->timer_cmp
);
222 } else if (s
->cr
& CR_OCIEN
) {
223 if (!(oldcr
& CR_OCIEN
)) {
224 imx_epit_reload_compare_timer(s
);
225 ptimer_run(s
->timer_cmp
, 0);
228 ptimer_stop(s
->timer_cmp
);
232 case 1: /* SR - ACK*/
233 /* writing 1 to OCIF clear the OCIF bit */
236 imx_epit_update_int(s
);
240 case 2: /* LR - set ticks */
243 if (s
->cr
& CR_RLD
) {
244 /* Also set the limit if the LRD bit is set */
245 /* If IOVW bit is set then set the timer value */
246 ptimer_set_limit(s
->timer_reload
, s
->lr
, s
->cr
& CR_IOVW
);
247 ptimer_set_limit(s
->timer_cmp
, s
->lr
, 0);
248 } else if (s
->cr
& CR_IOVW
) {
249 /* If IOVW bit is set then set the timer value */
250 ptimer_set_count(s
->timer_reload
, s
->lr
);
253 imx_epit_reload_compare_timer(s
);
259 imx_epit_reload_compare_timer(s
);
264 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
265 HWADDR_PRIx
"\n", TYPE_IMX_EPIT
, __func__
, offset
);
270 static void imx_epit_cmp(void *opaque
)
272 IMXEPITState
*s
= IMX_EPIT(opaque
);
274 DPRINTF("sr was %d\n", s
->sr
);
277 imx_epit_update_int(s
);
280 static const MemoryRegionOps imx_epit_ops
= {
281 .read
= imx_epit_read
,
282 .write
= imx_epit_write
,
283 .endianness
= DEVICE_NATIVE_ENDIAN
,
286 static const VMStateDescription vmstate_imx_timer_epit
= {
287 .name
= TYPE_IMX_EPIT
,
289 .minimum_version_id
= 2,
290 .fields
= (VMStateField
[]) {
291 VMSTATE_UINT32(cr
, IMXEPITState
),
292 VMSTATE_UINT32(sr
, IMXEPITState
),
293 VMSTATE_UINT32(lr
, IMXEPITState
),
294 VMSTATE_UINT32(cmp
, IMXEPITState
),
295 VMSTATE_UINT32(cnt
, IMXEPITState
),
296 VMSTATE_UINT32(freq
, IMXEPITState
),
297 VMSTATE_PTIMER(timer_reload
, IMXEPITState
),
298 VMSTATE_PTIMER(timer_cmp
, IMXEPITState
),
299 VMSTATE_END_OF_LIST()
303 static void imx_epit_realize(DeviceState
*dev
, Error
**errp
)
305 IMXEPITState
*s
= IMX_EPIT(dev
);
306 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
311 sysbus_init_irq(sbd
, &s
->irq
);
312 memory_region_init_io(&s
->iomem
, OBJECT(s
), &imx_epit_ops
, s
, TYPE_IMX_EPIT
,
314 sysbus_init_mmio(sbd
, &s
->iomem
);
316 s
->timer_reload
= ptimer_init(NULL
);
318 bh
= qemu_bh_new(imx_epit_cmp
, s
);
319 s
->timer_cmp
= ptimer_init(bh
);
322 static void imx_epit_class_init(ObjectClass
*klass
, void *data
)
324 DeviceClass
*dc
= DEVICE_CLASS(klass
);
326 dc
->realize
= imx_epit_realize
;
327 dc
->reset
= imx_epit_reset
;
328 dc
->vmsd
= &vmstate_imx_timer_epit
;
329 dc
->desc
= "i.MX periodic timer";
332 static const TypeInfo imx_epit_info
= {
333 .name
= TYPE_IMX_EPIT
,
334 .parent
= TYPE_SYS_BUS_DEVICE
,
335 .instance_size
= sizeof(IMXEPITState
),
336 .class_init
= imx_epit_class_init
,
339 static void imx_epit_register_types(void)
341 type_register_static(&imx_epit_info
);
344 type_init(imx_epit_register_types
)