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 "hw/timer/imx_epit.h"
16 #include "hw/misc/imx_ccm.h"
17 #include "qemu/main-loop.h"
22 static char const *imx_epit_reg_name(uint32_t reg
)
40 # define DPRINTF(fmt, args...) \
41 do { fprintf(stderr, "%s: " fmt , __func__, ##args); } while (0)
43 # define DPRINTF(fmt, args...) do {} while (0)
47 * Define to 1 for messages about attempts to
48 * access unimplemented registers or similar.
50 #define DEBUG_IMPLEMENTATION 1
51 #if DEBUG_IMPLEMENTATION
52 # define IPRINTF(fmt, args...) \
53 do { fprintf(stderr, "%s: " fmt, __func__, ##args); } while (0)
55 # define IPRINTF(fmt, args...) do {} while (0)
59 * Exact clock frequencies vary from board to board.
62 static const IMXClk imx_epit_clocks
[] = {
64 IPG
, /* 01 ipg_clk, ~532MHz */
65 IPG
, /* 10 ipg_clk_highfreq */
66 CLK_32k
, /* 11 ipg_clk_32k -- ~32kHz */
70 * Update interrupt status
72 static void imx_epit_update_int(IMXEPITState
*s
)
74 if (s
->sr
&& (s
->cr
& CR_OCIEN
) && (s
->cr
& CR_EN
)) {
75 qemu_irq_raise(s
->irq
);
77 qemu_irq_lower(s
->irq
);
81 static void imx_epit_set_freq(IMXEPITState
*s
)
87 clksrc
= extract32(s
->cr
, CR_CLKSRC_SHIFT
, 2);
88 prescaler
= 1 + extract32(s
->cr
, CR_PRESCALE_SHIFT
, 12);
90 freq
= imx_clock_frequency(s
->ccm
, imx_epit_clocks
[clksrc
]) / prescaler
;
94 DPRINTF("Setting ptimer frequency to %u\n", freq
);
97 ptimer_set_freq(s
->timer_reload
, freq
);
98 ptimer_set_freq(s
->timer_cmp
, freq
);
102 static void imx_epit_reset(DeviceState
*dev
)
104 IMXEPITState
*s
= IMX_EPIT(dev
);
107 * Soft reset doesn't touch some bits; hard reset clears them
109 s
->cr
&= (CR_EN
|CR_ENMOD
|CR_STOPEN
|CR_DOZEN
|CR_WAITEN
|CR_DBGEN
);
111 s
->lr
= EPIT_TIMER_MAX
;
114 /* stop both timers */
115 ptimer_stop(s
->timer_cmp
);
116 ptimer_stop(s
->timer_reload
);
117 /* compute new frequency */
118 imx_epit_set_freq(s
);
119 /* init both timers to EPIT_TIMER_MAX */
120 ptimer_set_limit(s
->timer_cmp
, EPIT_TIMER_MAX
, 1);
121 ptimer_set_limit(s
->timer_reload
, EPIT_TIMER_MAX
, 1);
122 if (s
->freq
&& (s
->cr
& CR_EN
)) {
123 /* if the timer is still enabled, restart it */
124 ptimer_run(s
->timer_reload
, 0);
128 static uint32_t imx_epit_update_count(IMXEPITState
*s
)
130 s
->cnt
= ptimer_get_count(s
->timer_reload
);
135 static uint64_t imx_epit_read(void *opaque
, hwaddr offset
, unsigned size
)
137 IMXEPITState
*s
= IMX_EPIT(opaque
);
138 uint32_t reg_value
= 0;
139 uint32_t reg
= offset
>> 2;
142 case 0: /* Control Register */
146 case 1: /* Status Register */
150 case 2: /* LR - ticks*/
159 imx_epit_update_count(s
);
164 IPRINTF("Bad offset %x\n", reg
);
168 DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(reg
), reg_value
);
173 static void imx_epit_reload_compare_timer(IMXEPITState
*s
)
175 if ((s
->cr
& (CR_EN
| CR_OCIEN
)) == (CR_EN
| CR_OCIEN
)) {
176 /* if the compare feature is on and timers are running */
177 uint32_t tmp
= imx_epit_update_count(s
);
180 /* It'll fire in this round of the timer */
182 } else { /* catch it next time around */
183 next
= tmp
- s
->cmp
+ ((s
->cr
& CR_RLD
) ? EPIT_TIMER_MAX
: s
->lr
);
185 ptimer_set_count(s
->timer_cmp
, next
);
189 static void imx_epit_write(void *opaque
, hwaddr offset
, uint64_t value
,
192 IMXEPITState
*s
= IMX_EPIT(opaque
);
193 uint32_t reg
= offset
>> 2;
196 DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(reg
), (uint32_t)value
);
202 s
->cr
= value
& 0x03ffffff;
203 if (s
->cr
& CR_SWR
) {
204 /* handle the reset */
205 imx_epit_reset(DEVICE(s
));
207 imx_epit_set_freq(s
);
210 if (s
->freq
&& (s
->cr
& CR_EN
) && !(oldcr
& CR_EN
)) {
211 if (s
->cr
& CR_ENMOD
) {
212 if (s
->cr
& CR_RLD
) {
213 ptimer_set_limit(s
->timer_reload
, s
->lr
, 1);
214 ptimer_set_limit(s
->timer_cmp
, s
->lr
, 1);
216 ptimer_set_limit(s
->timer_reload
, EPIT_TIMER_MAX
, 1);
217 ptimer_set_limit(s
->timer_cmp
, EPIT_TIMER_MAX
, 1);
221 imx_epit_reload_compare_timer(s
);
222 ptimer_run(s
->timer_reload
, 0);
223 if (s
->cr
& CR_OCIEN
) {
224 ptimer_run(s
->timer_cmp
, 0);
226 ptimer_stop(s
->timer_cmp
);
228 } else if (!(s
->cr
& CR_EN
)) {
229 /* stop both timers */
230 ptimer_stop(s
->timer_reload
);
231 ptimer_stop(s
->timer_cmp
);
232 } else if (s
->cr
& CR_OCIEN
) {
233 if (!(oldcr
& CR_OCIEN
)) {
234 imx_epit_reload_compare_timer(s
);
235 ptimer_run(s
->timer_cmp
, 0);
238 ptimer_stop(s
->timer_cmp
);
242 case 1: /* SR - ACK*/
243 /* writing 1 to OCIF clear the OCIF bit */
246 imx_epit_update_int(s
);
250 case 2: /* LR - set ticks */
253 if (s
->cr
& CR_RLD
) {
254 /* Also set the limit if the LRD bit is set */
255 /* If IOVW bit is set then set the timer value */
256 ptimer_set_limit(s
->timer_reload
, s
->lr
, s
->cr
& CR_IOVW
);
257 ptimer_set_limit(s
->timer_cmp
, s
->lr
, 0);
258 } else if (s
->cr
& CR_IOVW
) {
259 /* If IOVW bit is set then set the timer value */
260 ptimer_set_count(s
->timer_reload
, s
->lr
);
263 imx_epit_reload_compare_timer(s
);
269 imx_epit_reload_compare_timer(s
);
274 IPRINTF("Bad offset %x\n", reg
);
279 static void imx_epit_cmp(void *opaque
)
281 IMXEPITState
*s
= IMX_EPIT(opaque
);
283 DPRINTF("sr was %d\n", s
->sr
);
286 imx_epit_update_int(s
);
289 static const MemoryRegionOps imx_epit_ops
= {
290 .read
= imx_epit_read
,
291 .write
= imx_epit_write
,
292 .endianness
= DEVICE_NATIVE_ENDIAN
,
295 static const VMStateDescription vmstate_imx_timer_epit
= {
296 .name
= TYPE_IMX_EPIT
,
298 .minimum_version_id
= 2,
299 .fields
= (VMStateField
[]) {
300 VMSTATE_UINT32(cr
, IMXEPITState
),
301 VMSTATE_UINT32(sr
, IMXEPITState
),
302 VMSTATE_UINT32(lr
, IMXEPITState
),
303 VMSTATE_UINT32(cmp
, IMXEPITState
),
304 VMSTATE_UINT32(cnt
, IMXEPITState
),
305 VMSTATE_UINT32(freq
, IMXEPITState
),
306 VMSTATE_PTIMER(timer_reload
, IMXEPITState
),
307 VMSTATE_PTIMER(timer_cmp
, IMXEPITState
),
308 VMSTATE_END_OF_LIST()
312 static void imx_epit_realize(DeviceState
*dev
, Error
**errp
)
314 IMXEPITState
*s
= IMX_EPIT(dev
);
315 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
320 sysbus_init_irq(sbd
, &s
->irq
);
321 memory_region_init_io(&s
->iomem
, OBJECT(s
), &imx_epit_ops
, s
, TYPE_IMX_EPIT
,
323 sysbus_init_mmio(sbd
, &s
->iomem
);
325 s
->timer_reload
= ptimer_init(NULL
);
327 bh
= qemu_bh_new(imx_epit_cmp
, s
);
328 s
->timer_cmp
= ptimer_init(bh
);
331 static void imx_epit_class_init(ObjectClass
*klass
, void *data
)
333 DeviceClass
*dc
= DEVICE_CLASS(klass
);
335 dc
->realize
= imx_epit_realize
;
336 dc
->reset
= imx_epit_reset
;
337 dc
->vmsd
= &vmstate_imx_timer_epit
;
338 dc
->desc
= "i.MX periodic timer";
341 static const TypeInfo imx_epit_info
= {
342 .name
= TYPE_IMX_EPIT
,
343 .parent
= TYPE_SYS_BUS_DEVICE
,
344 .instance_size
= sizeof(IMXEPITState
),
345 .class_init
= imx_epit_class_init
,
348 static void imx_epit_register_types(void)
350 type_register_static(&imx_epit_info
);
353 type_init(imx_epit_register_types
)