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 "migration/vmstate.h"
19 #include "hw/misc/imx_ccm.h"
20 #include "qemu/module.h"
23 #ifndef DEBUG_IMX_EPIT
24 #define DEBUG_IMX_EPIT 0
27 #define DPRINTF(fmt, args...) \
29 if (DEBUG_IMX_EPIT) { \
30 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_EPIT, \
35 static const char *imx_epit_reg_name(uint32_t reg
)
54 * Exact clock frequencies vary from board to board.
57 static const IMXClk imx_epit_clocks
[] = {
58 CLK_NONE
, /* 00 disabled */
59 CLK_IPG
, /* 01 ipg_clk, ~532MHz */
60 CLK_IPG_HIGH
, /* 10 ipg_clk_highfreq */
61 CLK_32k
, /* 11 ipg_clk_32k -- ~32kHz */
65 * Update interrupt status
67 static void imx_epit_update_int(IMXEPITState
*s
)
69 if (s
->sr
&& (s
->cr
& CR_OCIEN
) && (s
->cr
& CR_EN
)) {
70 qemu_irq_raise(s
->irq
);
72 qemu_irq_lower(s
->irq
);
77 * Must be called from within a ptimer_transaction_begin/commit block
78 * for both s->timer_cmp and s->timer_reload.
80 static void imx_epit_set_freq(IMXEPITState
*s
)
85 clksrc
= extract32(s
->cr
, CR_CLKSRC_SHIFT
, 2);
86 prescaler
= 1 + extract32(s
->cr
, CR_PRESCALE_SHIFT
, 12);
88 s
->freq
= imx_ccm_get_clock_frequency(s
->ccm
,
89 imx_epit_clocks
[clksrc
]) / prescaler
;
91 DPRINTF("Setting ptimer frequency to %u\n", s
->freq
);
94 ptimer_set_freq(s
->timer_reload
, s
->freq
);
95 ptimer_set_freq(s
->timer_cmp
, s
->freq
);
99 static void imx_epit_reset(DeviceState
*dev
)
101 IMXEPITState
*s
= IMX_EPIT(dev
);
104 * Soft reset doesn't touch some bits; hard reset clears them
106 s
->cr
&= (CR_EN
|CR_ENMOD
|CR_STOPEN
|CR_DOZEN
|CR_WAITEN
|CR_DBGEN
);
108 s
->lr
= EPIT_TIMER_MAX
;
111 ptimer_transaction_begin(s
->timer_cmp
);
112 ptimer_transaction_begin(s
->timer_reload
);
113 /* stop both timers */
114 ptimer_stop(s
->timer_cmp
);
115 ptimer_stop(s
->timer_reload
);
116 /* compute new frequency */
117 imx_epit_set_freq(s
);
118 /* init both timers to EPIT_TIMER_MAX */
119 ptimer_set_limit(s
->timer_cmp
, EPIT_TIMER_MAX
, 1);
120 ptimer_set_limit(s
->timer_reload
, EPIT_TIMER_MAX
, 1);
121 if (s
->freq
&& (s
->cr
& CR_EN
)) {
122 /* if the timer is still enabled, restart it */
123 ptimer_run(s
->timer_reload
, 0);
125 ptimer_transaction_commit(s
->timer_cmp
);
126 ptimer_transaction_commit(s
->timer_reload
);
129 static uint32_t imx_epit_update_count(IMXEPITState
*s
)
131 s
->cnt
= ptimer_get_count(s
->timer_reload
);
136 static uint64_t imx_epit_read(void *opaque
, hwaddr offset
, unsigned size
)
138 IMXEPITState
*s
= IMX_EPIT(opaque
);
139 uint32_t reg_value
= 0;
141 switch (offset
>> 2) {
142 case 0: /* Control Register */
146 case 1: /* Status Register */
150 case 2: /* LR - ticks*/
159 imx_epit_update_count(s
);
164 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
165 HWADDR_PRIx
"\n", TYPE_IMX_EPIT
, __func__
, offset
);
169 DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(offset
>> 2), reg_value
);
174 /* Must be called from ptimer_transaction_begin/commit block for s->timer_cmp */
175 static void imx_epit_reload_compare_timer(IMXEPITState
*s
)
177 if ((s
->cr
& (CR_EN
| CR_OCIEN
)) == (CR_EN
| CR_OCIEN
)) {
178 /* if the compare feature is on and timers are running */
179 uint32_t tmp
= imx_epit_update_count(s
);
182 /* It'll fire in this round of the timer */
184 } else { /* catch it next time around */
185 next
= tmp
- s
->cmp
+ ((s
->cr
& CR_RLD
) ? EPIT_TIMER_MAX
: s
->lr
);
187 ptimer_set_count(s
->timer_cmp
, next
);
191 static void imx_epit_write(void *opaque
, hwaddr offset
, uint64_t value
,
194 IMXEPITState
*s
= IMX_EPIT(opaque
);
197 DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(offset
>> 2),
200 switch (offset
>> 2) {
204 s
->cr
= value
& 0x03ffffff;
205 if (s
->cr
& CR_SWR
) {
206 /* handle the reset */
207 imx_epit_reset(DEVICE(s
));
209 * TODO: could we 'break' here? following operations appear
210 * to duplicate the work imx_epit_reset() already did.
214 ptimer_transaction_begin(s
->timer_cmp
);
215 ptimer_transaction_begin(s
->timer_reload
);
217 if (!(s
->cr
& CR_SWR
)) {
218 imx_epit_set_freq(s
);
221 if (s
->freq
&& (s
->cr
& CR_EN
) && !(oldcr
& CR_EN
)) {
222 if (s
->cr
& CR_ENMOD
) {
223 if (s
->cr
& CR_RLD
) {
224 ptimer_set_limit(s
->timer_reload
, s
->lr
, 1);
225 ptimer_set_limit(s
->timer_cmp
, s
->lr
, 1);
227 ptimer_set_limit(s
->timer_reload
, EPIT_TIMER_MAX
, 1);
228 ptimer_set_limit(s
->timer_cmp
, EPIT_TIMER_MAX
, 1);
232 imx_epit_reload_compare_timer(s
);
233 ptimer_run(s
->timer_reload
, 0);
234 if (s
->cr
& CR_OCIEN
) {
235 ptimer_run(s
->timer_cmp
, 0);
237 ptimer_stop(s
->timer_cmp
);
239 } else if (!(s
->cr
& CR_EN
)) {
240 /* stop both timers */
241 ptimer_stop(s
->timer_reload
);
242 ptimer_stop(s
->timer_cmp
);
243 } else if (s
->cr
& CR_OCIEN
) {
244 if (!(oldcr
& CR_OCIEN
)) {
245 imx_epit_reload_compare_timer(s
);
246 ptimer_run(s
->timer_cmp
, 0);
249 ptimer_stop(s
->timer_cmp
);
252 ptimer_transaction_commit(s
->timer_cmp
);
253 ptimer_transaction_commit(s
->timer_reload
);
256 case 1: /* SR - ACK*/
257 /* writing 1 to OCIF clear the OCIF bit */
260 imx_epit_update_int(s
);
264 case 2: /* LR - set ticks */
267 ptimer_transaction_begin(s
->timer_cmp
);
268 ptimer_transaction_begin(s
->timer_reload
);
269 if (s
->cr
& CR_RLD
) {
270 /* Also set the limit if the LRD bit is set */
271 /* If IOVW bit is set then set the timer value */
272 ptimer_set_limit(s
->timer_reload
, s
->lr
, s
->cr
& CR_IOVW
);
273 ptimer_set_limit(s
->timer_cmp
, s
->lr
, 0);
274 } else if (s
->cr
& CR_IOVW
) {
275 /* If IOVW bit is set then set the timer value */
276 ptimer_set_count(s
->timer_reload
, s
->lr
);
279 imx_epit_reload_compare_timer(s
);
280 ptimer_transaction_commit(s
->timer_cmp
);
281 ptimer_transaction_commit(s
->timer_reload
);
287 ptimer_transaction_begin(s
->timer_cmp
);
288 imx_epit_reload_compare_timer(s
);
289 ptimer_transaction_commit(s
->timer_cmp
);
294 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
295 HWADDR_PRIx
"\n", TYPE_IMX_EPIT
, __func__
, offset
);
300 static void imx_epit_cmp(void *opaque
)
302 IMXEPITState
*s
= IMX_EPIT(opaque
);
304 DPRINTF("sr was %d\n", s
->sr
);
307 imx_epit_update_int(s
);
310 static void imx_epit_reload(void *opaque
)
312 /* No action required on rollover of timer_reload */
315 static const MemoryRegionOps imx_epit_ops
= {
316 .read
= imx_epit_read
,
317 .write
= imx_epit_write
,
318 .endianness
= DEVICE_NATIVE_ENDIAN
,
321 static const VMStateDescription vmstate_imx_timer_epit
= {
322 .name
= TYPE_IMX_EPIT
,
324 .minimum_version_id
= 2,
325 .fields
= (VMStateField
[]) {
326 VMSTATE_UINT32(cr
, IMXEPITState
),
327 VMSTATE_UINT32(sr
, IMXEPITState
),
328 VMSTATE_UINT32(lr
, IMXEPITState
),
329 VMSTATE_UINT32(cmp
, IMXEPITState
),
330 VMSTATE_UINT32(cnt
, IMXEPITState
),
331 VMSTATE_UINT32(freq
, IMXEPITState
),
332 VMSTATE_PTIMER(timer_reload
, IMXEPITState
),
333 VMSTATE_PTIMER(timer_cmp
, IMXEPITState
),
334 VMSTATE_END_OF_LIST()
338 static void imx_epit_realize(DeviceState
*dev
, Error
**errp
)
340 IMXEPITState
*s
= IMX_EPIT(dev
);
341 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
345 sysbus_init_irq(sbd
, &s
->irq
);
346 memory_region_init_io(&s
->iomem
, OBJECT(s
), &imx_epit_ops
, s
, TYPE_IMX_EPIT
,
348 sysbus_init_mmio(sbd
, &s
->iomem
);
350 s
->timer_reload
= ptimer_init(imx_epit_reload
, s
, PTIMER_POLICY_DEFAULT
);
352 s
->timer_cmp
= ptimer_init(imx_epit_cmp
, s
, PTIMER_POLICY_DEFAULT
);
355 static void imx_epit_class_init(ObjectClass
*klass
, void *data
)
357 DeviceClass
*dc
= DEVICE_CLASS(klass
);
359 dc
->realize
= imx_epit_realize
;
360 dc
->reset
= imx_epit_reset
;
361 dc
->vmsd
= &vmstate_imx_timer_epit
;
362 dc
->desc
= "i.MX periodic timer";
365 static const TypeInfo imx_epit_info
= {
366 .name
= TYPE_IMX_EPIT
,
367 .parent
= TYPE_SYS_BUS_DEVICE
,
368 .instance_size
= sizeof(IMXEPITState
),
369 .class_init
= imx_epit_class_init
,
372 static void imx_epit_register_types(void)
374 type_register_static(&imx_epit_info
);
377 type_init(imx_epit_register_types
)