memory: pass MemoryRegion to access_with_adjusted_size
[qemu/ar7.git] / hw / timer / imx_epit.c
blob117dc7bcbb7553777112fd486ca58e35a9f30f2e
1 /*
2 * IMX EPIT Timer
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
10 * This code is licensed under GPL version 2 or later. See
11 * the COPYING file in the top-level directory.
15 #include "hw/hw.h"
16 #include "qemu/bitops.h"
17 #include "qemu/timer.h"
18 #include "hw/ptimer.h"
19 #include "hw/sysbus.h"
20 #include "hw/arm/imx.h"
22 #define TYPE_IMX_EPIT "imx.epit"
24 #define DEBUG_TIMER 0
25 #if DEBUG_TIMER
27 static char const *imx_epit_reg_name(uint32_t reg)
29 switch (reg) {
30 case 0:
31 return "CR";
32 case 1:
33 return "SR";
34 case 2:
35 return "LR";
36 case 3:
37 return "CMP";
38 case 4:
39 return "CNT";
40 default:
41 return "[?]";
45 # define DPRINTF(fmt, args...) \
46 do { printf("%s: " fmt , __func__, ##args); } while (0)
47 #else
48 # define DPRINTF(fmt, args...) do {} while (0)
49 #endif
52 * Define to 1 for messages about attempts to
53 * access unimplemented registers or similar.
55 #define DEBUG_IMPLEMENTATION 1
56 #if DEBUG_IMPLEMENTATION
57 # define IPRINTF(fmt, args...) \
58 do { fprintf(stderr, "%s: " fmt, __func__, ##args); } while (0)
59 #else
60 # define IPRINTF(fmt, args...) do {} while (0)
61 #endif
63 #define IMX_EPIT(obj) \
64 OBJECT_CHECK(IMXEPITState, (obj), TYPE_IMX_EPIT)
67 * EPIT: Enhanced periodic interrupt timer
70 #define CR_EN (1 << 0)
71 #define CR_ENMOD (1 << 1)
72 #define CR_OCIEN (1 << 2)
73 #define CR_RLD (1 << 3)
74 #define CR_PRESCALE_SHIFT (4)
75 #define CR_PRESCALE_MASK (0xfff)
76 #define CR_SWR (1 << 16)
77 #define CR_IOVW (1 << 17)
78 #define CR_DBGEN (1 << 18)
79 #define CR_WAITEN (1 << 19)
80 #define CR_DOZEN (1 << 20)
81 #define CR_STOPEN (1 << 21)
82 #define CR_CLKSRC_SHIFT (24)
83 #define CR_CLKSRC_MASK (0x3 << CR_CLKSRC_SHIFT)
85 #define TIMER_MAX 0XFFFFFFFFUL
88 * Exact clock frequencies vary from board to board.
89 * These are typical.
91 static const IMXClk imx_epit_clocks[] = {
92 0, /* 00 disabled */
93 IPG, /* 01 ipg_clk, ~532MHz */
94 IPG, /* 10 ipg_clk_highfreq */
95 CLK_32k, /* 11 ipg_clk_32k -- ~32kHz */
98 typedef struct {
99 SysBusDevice busdev;
100 ptimer_state *timer_reload;
101 ptimer_state *timer_cmp;
102 MemoryRegion iomem;
103 DeviceState *ccm;
105 uint32_t cr;
106 uint32_t sr;
107 uint32_t lr;
108 uint32_t cmp;
109 uint32_t cnt;
111 uint32_t freq;
112 qemu_irq irq;
113 } IMXEPITState;
116 * Update interrupt status
118 static void imx_epit_update_int(IMXEPITState *s)
120 if (s->sr && (s->cr & CR_OCIEN) && (s->cr & CR_EN)) {
121 qemu_irq_raise(s->irq);
122 } else {
123 qemu_irq_lower(s->irq);
127 static void imx_epit_set_freq(IMXEPITState *s)
129 uint32_t clksrc;
130 uint32_t prescaler;
131 uint32_t freq;
133 clksrc = extract32(s->cr, CR_CLKSRC_SHIFT, 2);
134 prescaler = 1 + extract32(s->cr, CR_PRESCALE_SHIFT, 12);
136 freq = imx_clock_frequency(s->ccm, imx_epit_clocks[clksrc]) / prescaler;
138 s->freq = freq;
140 DPRINTF("Setting ptimer frequency to %u\n", freq);
142 if (freq) {
143 ptimer_set_freq(s->timer_reload, freq);
144 ptimer_set_freq(s->timer_cmp, freq);
148 static void imx_epit_reset(DeviceState *dev)
150 IMXEPITState *s = IMX_EPIT(dev);
153 * Soft reset doesn't touch some bits; hard reset clears them
155 s->cr &= ~(CR_EN|CR_ENMOD|CR_STOPEN|CR_DOZEN|CR_WAITEN|CR_DBGEN);
156 s->sr = 0;
157 s->lr = TIMER_MAX;
158 s->cmp = 0;
159 s->cnt = 0;
160 /* stop both timers */
161 ptimer_stop(s->timer_cmp);
162 ptimer_stop(s->timer_reload);
163 /* compute new frequency */
164 imx_epit_set_freq(s);
165 /* init both timers to TIMER_MAX */
166 ptimer_set_limit(s->timer_cmp, TIMER_MAX, 1);
167 ptimer_set_limit(s->timer_reload, TIMER_MAX, 1);
168 if (s->freq && (s->cr & CR_EN)) {
169 /* if the timer is still enabled, restart it */
170 ptimer_run(s->timer_reload, 1);
174 static uint32_t imx_epit_update_count(IMXEPITState *s)
176 s->cnt = ptimer_get_count(s->timer_reload);
178 return s->cnt;
181 static uint64_t imx_epit_read(void *opaque, hwaddr offset, unsigned size)
183 IMXEPITState *s = IMX_EPIT(opaque);
184 uint32_t reg_value = 0;
185 uint32_t reg = offset >> 2;
187 switch (reg) {
188 case 0: /* Control Register */
189 reg_value = s->cr;
190 break;
192 case 1: /* Status Register */
193 reg_value = s->sr;
194 break;
196 case 2: /* LR - ticks*/
197 reg_value = s->lr;
198 break;
200 case 3: /* CMP */
201 reg_value = s->cmp;
202 break;
204 case 4: /* CNT */
205 imx_epit_update_count(s);
206 reg_value = s->cnt;
207 break;
209 default:
210 IPRINTF("Bad offset %x\n", reg);
211 break;
214 DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(reg), reg_value);
216 return reg_value;
219 static void imx_epit_reload_compare_timer(IMXEPITState *s)
221 if ((s->cr & CR_OCIEN) && s->cmp) {
222 /* if the compare feature is on */
223 uint32_t tmp = imx_epit_update_count(s);
224 if (tmp > s->cmp) {
225 /* reinit the cmp timer if required */
226 ptimer_set_count(s->timer_cmp, tmp - s->cmp);
227 if ((s->cr & CR_EN)) {
228 /* Restart the cmp timer if required */
229 ptimer_run(s->timer_cmp, 0);
235 static void imx_epit_write(void *opaque, hwaddr offset, uint64_t value,
236 unsigned size)
238 IMXEPITState *s = IMX_EPIT(opaque);
239 uint32_t reg = offset >> 2;
241 DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(reg), (uint32_t)value);
243 switch (reg) {
244 case 0: /* CR */
245 s->cr = value & 0x03ffffff;
246 if (s->cr & CR_SWR) {
247 /* handle the reset */
248 imx_epit_reset(DEVICE(s));
249 } else {
250 imx_epit_set_freq(s);
253 if (s->freq && (s->cr & CR_EN)) {
254 if (s->cr & CR_ENMOD) {
255 if (s->cr & CR_RLD) {
256 ptimer_set_limit(s->timer_reload, s->lr, 1);
257 } else {
258 ptimer_set_limit(s->timer_reload, TIMER_MAX, 1);
262 imx_epit_reload_compare_timer(s);
264 ptimer_run(s->timer_reload, 1);
265 } else {
266 /* stop both timers */
267 ptimer_stop(s->timer_reload);
268 ptimer_stop(s->timer_cmp);
270 break;
272 case 1: /* SR - ACK*/
273 /* writing 1 to OCIF clear the OCIF bit */
274 if (value & 0x01) {
275 s->sr = 0;
276 imx_epit_update_int(s);
278 break;
280 case 2: /* LR - set ticks */
281 s->lr = value;
283 if (s->cr & CR_RLD) {
284 /* Also set the limit if the LRD bit is set */
285 /* If IOVW bit is set then set the timer value */
286 ptimer_set_limit(s->timer_reload, s->lr, s->cr & CR_IOVW);
287 } else if (s->cr & CR_IOVW) {
288 /* If IOVW bit is set then set the timer value */
289 ptimer_set_count(s->timer_reload, s->lr);
292 imx_epit_reload_compare_timer(s);
294 break;
296 case 3: /* CMP */
297 s->cmp = value;
299 imx_epit_reload_compare_timer(s);
301 break;
303 default:
304 IPRINTF("Bad offset %x\n", reg);
306 break;
310 static void imx_epit_timeout(void *opaque)
312 IMXEPITState *s = IMX_EPIT(opaque);
314 DPRINTF("\n");
316 if (!(s->cr & CR_EN)) {
317 return;
320 if (s->cr & CR_RLD) {
321 ptimer_set_limit(s->timer_reload, s->lr, 1);
322 } else {
323 ptimer_set_limit(s->timer_reload, TIMER_MAX, 1);
326 if (s->cr & CR_OCIEN) {
327 /* if compare register is 0 then we handle the interrupt here */
328 if (s->cmp == 0) {
329 s->sr = 1;
330 imx_epit_update_int(s);
331 } else if (s->cmp <= s->lr) {
332 /* We should launch the compare register */
333 ptimer_set_count(s->timer_cmp, s->lr - s->cmp);
334 ptimer_run(s->timer_cmp, 0);
335 } else {
336 IPRINTF("s->lr < s->cmp\n");
341 static void imx_epit_cmp(void *opaque)
343 IMXEPITState *s = IMX_EPIT(opaque);
345 DPRINTF("\n");
347 ptimer_stop(s->timer_cmp);
349 /* compare register is not 0 */
350 if (s->cmp) {
351 s->sr = 1;
352 imx_epit_update_int(s);
356 void imx_timerp_create(const hwaddr addr, qemu_irq irq, DeviceState *ccm)
358 IMXEPITState *pp;
359 DeviceState *dev;
361 dev = sysbus_create_simple(TYPE_IMX_EPIT, addr, irq);
362 pp = IMX_EPIT(dev);
363 pp->ccm = ccm;
366 static const MemoryRegionOps imx_epit_ops = {
367 .read = imx_epit_read,
368 .write = imx_epit_write,
369 .endianness = DEVICE_NATIVE_ENDIAN,
372 static const VMStateDescription vmstate_imx_timer_epit = {
373 .name = "imx.epit",
374 .version_id = 2,
375 .minimum_version_id = 2,
376 .minimum_version_id_old = 2,
377 .fields = (VMStateField[]) {
378 VMSTATE_UINT32(cr, IMXEPITState),
379 VMSTATE_UINT32(sr, IMXEPITState),
380 VMSTATE_UINT32(lr, IMXEPITState),
381 VMSTATE_UINT32(cmp, IMXEPITState),
382 VMSTATE_UINT32(cnt, IMXEPITState),
383 VMSTATE_UINT32(freq, IMXEPITState),
384 VMSTATE_PTIMER(timer_reload, IMXEPITState),
385 VMSTATE_PTIMER(timer_cmp, IMXEPITState),
386 VMSTATE_END_OF_LIST()
390 static void imx_epit_realize(DeviceState *dev, Error **errp)
392 IMXEPITState *s = IMX_EPIT(dev);
393 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
394 QEMUBH *bh;
396 DPRINTF("\n");
398 sysbus_init_irq(sbd, &s->irq);
399 memory_region_init_io(&s->iomem, OBJECT(s), &imx_epit_ops, s, TYPE_IMX_EPIT,
400 0x00001000);
401 sysbus_init_mmio(sbd, &s->iomem);
403 bh = qemu_bh_new(imx_epit_timeout, s);
404 s->timer_reload = ptimer_init(bh);
406 bh = qemu_bh_new(imx_epit_cmp, s);
407 s->timer_cmp = ptimer_init(bh);
410 static void imx_epit_class_init(ObjectClass *klass, void *data)
412 DeviceClass *dc = DEVICE_CLASS(klass);
414 dc->realize = imx_epit_realize;
415 dc->reset = imx_epit_reset;
416 dc->vmsd = &vmstate_imx_timer_epit;
417 dc->desc = "i.MX periodic timer";
420 static const TypeInfo imx_epit_info = {
421 .name = TYPE_IMX_EPIT,
422 .parent = TYPE_SYS_BUS_DEVICE,
423 .instance_size = sizeof(IMXEPITState),
424 .class_init = imx_epit_class_init,
427 static void imx_epit_register_types(void)
429 type_register_static(&imx_epit_info);
432 type_init(imx_epit_register_types)