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"
21 #ifndef DEBUG_IMX_EPIT
22 #define DEBUG_IMX_EPIT 0
25 #define DPRINTF(fmt, args...) \
27 if (DEBUG_IMX_EPIT) { \
28 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_EPIT, \
33 static const char *imx_epit_reg_name(uint32_t reg
)
52 * Exact clock frequencies vary from board to board.
55 static const IMXClk imx_epit_clocks
[] = {
56 CLK_NONE
, /* 00 disabled */
57 CLK_IPG
, /* 01 ipg_clk, ~532MHz */
58 CLK_IPG_HIGH
, /* 10 ipg_clk_highfreq */
59 CLK_32k
, /* 11 ipg_clk_32k -- ~32kHz */
63 * Update interrupt status
65 static void imx_epit_update_int(IMXEPITState
*s
)
67 if (s
->sr
&& (s
->cr
& CR_OCIEN
) && (s
->cr
& CR_EN
)) {
68 qemu_irq_raise(s
->irq
);
70 qemu_irq_lower(s
->irq
);
74 static void imx_epit_set_freq(IMXEPITState
*s
)
79 clksrc
= extract32(s
->cr
, CR_CLKSRC_SHIFT
, 2);
80 prescaler
= 1 + extract32(s
->cr
, CR_PRESCALE_SHIFT
, 12);
82 s
->freq
= imx_ccm_get_clock_frequency(s
->ccm
,
83 imx_epit_clocks
[clksrc
]) / prescaler
;
85 DPRINTF("Setting ptimer frequency to %u\n", s
->freq
);
88 ptimer_set_freq(s
->timer_reload
, s
->freq
);
89 ptimer_set_freq(s
->timer_cmp
, s
->freq
);
93 static void imx_epit_reset(DeviceState
*dev
)
95 IMXEPITState
*s
= IMX_EPIT(dev
);
98 * Soft reset doesn't touch some bits; hard reset clears them
100 s
->cr
&= (CR_EN
|CR_ENMOD
|CR_STOPEN
|CR_DOZEN
|CR_WAITEN
|CR_DBGEN
);
102 s
->lr
= EPIT_TIMER_MAX
;
105 /* stop both timers */
106 ptimer_stop(s
->timer_cmp
);
107 ptimer_stop(s
->timer_reload
);
108 /* compute new frequency */
109 imx_epit_set_freq(s
);
110 /* init both timers to EPIT_TIMER_MAX */
111 ptimer_set_limit(s
->timer_cmp
, EPIT_TIMER_MAX
, 1);
112 ptimer_set_limit(s
->timer_reload
, EPIT_TIMER_MAX
, 1);
113 if (s
->freq
&& (s
->cr
& CR_EN
)) {
114 /* if the timer is still enabled, restart it */
115 ptimer_run(s
->timer_reload
, 0);
119 static uint32_t imx_epit_update_count(IMXEPITState
*s
)
121 s
->cnt
= ptimer_get_count(s
->timer_reload
);
126 static uint64_t imx_epit_read(void *opaque
, hwaddr offset
, unsigned size
)
128 IMXEPITState
*s
= IMX_EPIT(opaque
);
129 uint32_t reg_value
= 0;
131 switch (offset
>> 2) {
132 case 0: /* Control Register */
136 case 1: /* Status Register */
140 case 2: /* LR - ticks*/
149 imx_epit_update_count(s
);
154 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
155 HWADDR_PRIx
"\n", TYPE_IMX_EPIT
, __func__
, offset
);
159 DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(offset
>> 2), reg_value
);
164 static void imx_epit_reload_compare_timer(IMXEPITState
*s
)
166 if ((s
->cr
& (CR_EN
| CR_OCIEN
)) == (CR_EN
| CR_OCIEN
)) {
167 /* if the compare feature is on and timers are running */
168 uint32_t tmp
= imx_epit_update_count(s
);
171 /* It'll fire in this round of the timer */
173 } else { /* catch it next time around */
174 next
= tmp
- s
->cmp
+ ((s
->cr
& CR_RLD
) ? EPIT_TIMER_MAX
: s
->lr
);
176 ptimer_set_count(s
->timer_cmp
, next
);
180 static void imx_epit_write(void *opaque
, hwaddr offset
, uint64_t value
,
183 IMXEPITState
*s
= IMX_EPIT(opaque
);
186 DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(offset
>> 2),
189 switch (offset
>> 2) {
193 s
->cr
= value
& 0x03ffffff;
194 if (s
->cr
& CR_SWR
) {
195 /* handle the reset */
196 imx_epit_reset(DEVICE(s
));
198 imx_epit_set_freq(s
);
201 if (s
->freq
&& (s
->cr
& CR_EN
) && !(oldcr
& CR_EN
)) {
202 if (s
->cr
& CR_ENMOD
) {
203 if (s
->cr
& CR_RLD
) {
204 ptimer_set_limit(s
->timer_reload
, s
->lr
, 1);
205 ptimer_set_limit(s
->timer_cmp
, s
->lr
, 1);
207 ptimer_set_limit(s
->timer_reload
, EPIT_TIMER_MAX
, 1);
208 ptimer_set_limit(s
->timer_cmp
, EPIT_TIMER_MAX
, 1);
212 imx_epit_reload_compare_timer(s
);
213 ptimer_run(s
->timer_reload
, 0);
214 if (s
->cr
& CR_OCIEN
) {
215 ptimer_run(s
->timer_cmp
, 0);
217 ptimer_stop(s
->timer_cmp
);
219 } else if (!(s
->cr
& CR_EN
)) {
220 /* stop both timers */
221 ptimer_stop(s
->timer_reload
);
222 ptimer_stop(s
->timer_cmp
);
223 } else if (s
->cr
& CR_OCIEN
) {
224 if (!(oldcr
& CR_OCIEN
)) {
225 imx_epit_reload_compare_timer(s
);
226 ptimer_run(s
->timer_cmp
, 0);
229 ptimer_stop(s
->timer_cmp
);
233 case 1: /* SR - ACK*/
234 /* writing 1 to OCIF clear the OCIF bit */
237 imx_epit_update_int(s
);
241 case 2: /* LR - set ticks */
244 if (s
->cr
& CR_RLD
) {
245 /* Also set the limit if the LRD bit is set */
246 /* If IOVW bit is set then set the timer value */
247 ptimer_set_limit(s
->timer_reload
, s
->lr
, s
->cr
& CR_IOVW
);
248 ptimer_set_limit(s
->timer_cmp
, s
->lr
, 0);
249 } else if (s
->cr
& CR_IOVW
) {
250 /* If IOVW bit is set then set the timer value */
251 ptimer_set_count(s
->timer_reload
, s
->lr
);
254 imx_epit_reload_compare_timer(s
);
260 imx_epit_reload_compare_timer(s
);
265 qemu_log_mask(LOG_GUEST_ERROR
, "[%s]%s: Bad register at offset 0x%"
266 HWADDR_PRIx
"\n", TYPE_IMX_EPIT
, __func__
, offset
);
271 static void imx_epit_cmp(void *opaque
)
273 IMXEPITState
*s
= IMX_EPIT(opaque
);
275 DPRINTF("sr was %d\n", s
->sr
);
278 imx_epit_update_int(s
);
281 static const MemoryRegionOps imx_epit_ops
= {
282 .read
= imx_epit_read
,
283 .write
= imx_epit_write
,
284 .endianness
= DEVICE_NATIVE_ENDIAN
,
287 static const VMStateDescription vmstate_imx_timer_epit
= {
288 .name
= TYPE_IMX_EPIT
,
290 .minimum_version_id
= 2,
291 .fields
= (VMStateField
[]) {
292 VMSTATE_UINT32(cr
, IMXEPITState
),
293 VMSTATE_UINT32(sr
, IMXEPITState
),
294 VMSTATE_UINT32(lr
, IMXEPITState
),
295 VMSTATE_UINT32(cmp
, IMXEPITState
),
296 VMSTATE_UINT32(cnt
, IMXEPITState
),
297 VMSTATE_UINT32(freq
, IMXEPITState
),
298 VMSTATE_PTIMER(timer_reload
, IMXEPITState
),
299 VMSTATE_PTIMER(timer_cmp
, IMXEPITState
),
300 VMSTATE_END_OF_LIST()
304 static void imx_epit_realize(DeviceState
*dev
, Error
**errp
)
306 IMXEPITState
*s
= IMX_EPIT(dev
);
307 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
312 sysbus_init_irq(sbd
, &s
->irq
);
313 memory_region_init_io(&s
->iomem
, OBJECT(s
), &imx_epit_ops
, s
, TYPE_IMX_EPIT
,
315 sysbus_init_mmio(sbd
, &s
->iomem
);
317 s
->timer_reload
= ptimer_init(NULL
, PTIMER_POLICY_DEFAULT
);
319 bh
= qemu_bh_new(imx_epit_cmp
, s
);
320 s
->timer_cmp
= ptimer_init(bh
, PTIMER_POLICY_DEFAULT
);
323 static void imx_epit_class_init(ObjectClass
*klass
, void *data
)
325 DeviceClass
*dc
= DEVICE_CLASS(klass
);
327 dc
->realize
= imx_epit_realize
;
328 dc
->reset
= imx_epit_reset
;
329 dc
->vmsd
= &vmstate_imx_timer_epit
;
330 dc
->desc
= "i.MX periodic timer";
333 static const TypeInfo imx_epit_info
= {
334 .name
= TYPE_IMX_EPIT
,
335 .parent
= TYPE_SYS_BUS_DEVICE
,
336 .instance_size
= sizeof(IMXEPITState
),
337 .class_init
= imx_epit_class_init
,
340 static void imx_epit_register_types(void)
342 type_register_static(&imx_epit_info
);
345 type_init(imx_epit_register_types
)