blob: 967be4ad277294bcb4988e5e2f372dd3366bb4f3 [file] [log] [blame]
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +01001/*
2 * IMX EPIT Timer
3 *
4 * Copyright (c) 2008 OK Labs
5 * Copyright (c) 2011 NICTA Pty Ltd
6 * Originally written by Hans Jiang
7 * Updated by Peter Chubb
Jean-Christophe Dubois951cd002015-08-13 11:26:20 +01008 * Updated by Jean-Christophe Dubois <jcd@tribudubois.net>
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +01009 *
10 * This code is licensed under GPL version 2 or later. See
11 * the COPYING file in the top-level directory.
12 *
13 */
14
Jean-Christophe Dubois951cd002015-08-13 11:26:20 +010015#include "hw/timer/imx_epit.h"
16#include "hw/misc/imx_ccm.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010017#include "qemu/main-loop.h"
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010018
Jean-Christophe Dubois4929f652015-10-25 15:16:24 +010019#ifndef DEBUG_IMX_EPIT
20#define DEBUG_IMX_EPIT 0
21#endif
22
23#define DPRINTF(fmt, args...) \
24 do { \
25 if (DEBUG_IMX_EPIT) { \
26 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_EPIT, \
27 __func__, ##args); \
28 } \
29 } while (0)
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010030
31static char const *imx_epit_reg_name(uint32_t reg)
32{
33 switch (reg) {
34 case 0:
35 return "CR";
36 case 1:
37 return "SR";
38 case 2:
39 return "LR";
40 case 3:
41 return "CMP";
42 case 4:
43 return "CNT";
44 default:
45 return "[?]";
46 }
47}
48
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010049/*
50 * Exact clock frequencies vary from board to board.
51 * These are typical.
52 */
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010053static const IMXClk imx_epit_clocks[] = {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010054 0, /* 00 disabled */
55 IPG, /* 01 ipg_clk, ~532MHz */
56 IPG, /* 10 ipg_clk_highfreq */
57 CLK_32k, /* 11 ipg_clk_32k -- ~32kHz */
58};
59
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010060/*
61 * Update interrupt status
62 */
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010063static void imx_epit_update_int(IMXEPITState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010064{
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010065 if (s->sr && (s->cr & CR_OCIEN) && (s->cr & CR_EN)) {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010066 qemu_irq_raise(s->irq);
67 } else {
68 qemu_irq_lower(s->irq);
69 }
70}
71
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010072static void imx_epit_set_freq(IMXEPITState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010073{
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010074 uint32_t clksrc;
75 uint32_t prescaler;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010076 uint32_t freq;
77
78 clksrc = extract32(s->cr, CR_CLKSRC_SHIFT, 2);
79 prescaler = 1 + extract32(s->cr, CR_PRESCALE_SHIFT, 12);
80
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010081 freq = imx_clock_frequency(s->ccm, imx_epit_clocks[clksrc]) / prescaler;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010082
83 s->freq = freq;
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010084
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010085 DPRINTF("Setting ptimer frequency to %u\n", freq);
86
87 if (freq) {
88 ptimer_set_freq(s->timer_reload, freq);
89 ptimer_set_freq(s->timer_cmp, freq);
90 }
91}
92
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010093static void imx_epit_reset(DeviceState *dev)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010094{
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +010095 IMXEPITState *s = IMX_EPIT(dev);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +010096
97 /*
98 * Soft reset doesn't touch some bits; hard reset clears them
99 */
Peter Chubb23005812013-08-20 14:54:32 +0100100 s->cr &= (CR_EN|CR_ENMOD|CR_STOPEN|CR_DOZEN|CR_WAITEN|CR_DBGEN);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100101 s->sr = 0;
Michael Tokarev203d65a2014-08-02 00:14:48 +0400102 s->lr = EPIT_TIMER_MAX;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100103 s->cmp = 0;
104 s->cnt = 0;
105 /* stop both timers */
106 ptimer_stop(s->timer_cmp);
107 ptimer_stop(s->timer_reload);
108 /* compute new frequency */
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100109 imx_epit_set_freq(s);
Michael Tokarev203d65a2014-08-02 00:14:48 +0400110 /* 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);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100113 if (s->freq && (s->cr & CR_EN)) {
114 /* if the timer is still enabled, restart it */
Peter Chubb23005812013-08-20 14:54:32 +0100115 ptimer_run(s->timer_reload, 0);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100116 }
117}
118
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100119static uint32_t imx_epit_update_count(IMXEPITState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100120{
Jean-Christophe Dubois565328f2015-08-13 11:26:20 +0100121 s->cnt = ptimer_get_count(s->timer_reload);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100122
Jean-Christophe Dubois565328f2015-08-13 11:26:20 +0100123 return s->cnt;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100124}
125
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100126static uint64_t imx_epit_read(void *opaque, hwaddr offset, unsigned size)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100127{
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100128 IMXEPITState *s = IMX_EPIT(opaque);
129 uint32_t reg_value = 0;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100130
Jean-Christophe Dubois4929f652015-10-25 15:16:24 +0100131 switch (offset >> 2) {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100132 case 0: /* Control Register */
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100133 reg_value = s->cr;
134 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100135
136 case 1: /* Status Register */
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100137 reg_value = s->sr;
138 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100139
140 case 2: /* LR - ticks*/
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100141 reg_value = s->lr;
142 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100143
144 case 3: /* CMP */
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100145 reg_value = s->cmp;
146 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100147
148 case 4: /* CNT */
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100149 imx_epit_update_count(s);
150 reg_value = s->cnt;
151 break;
152
153 default:
Jean-Christophe Dubois4929f652015-10-25 15:16:24 +0100154 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
155 HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset);
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100156 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100157 }
158
Jean-Christophe Dubois4929f652015-10-25 15:16:24 +0100159 DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(offset >> 2), reg_value);
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100160
161 return reg_value;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100162}
163
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100164static void imx_epit_reload_compare_timer(IMXEPITState *s)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100165{
Peter Chubb23005812013-08-20 14:54:32 +0100166 if ((s->cr & (CR_EN | CR_OCIEN)) == (CR_EN | CR_OCIEN)) {
167 /* if the compare feature is on and timers are running */
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100168 uint32_t tmp = imx_epit_update_count(s);
Peter Chubb23005812013-08-20 14:54:32 +0100169 uint64_t next;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100170 if (tmp > s->cmp) {
Peter Chubb23005812013-08-20 14:54:32 +0100171 /* It'll fire in this round of the timer */
172 next = tmp - s->cmp;
173 } else { /* catch it next time around */
Michael Tokarev203d65a2014-08-02 00:14:48 +0400174 next = tmp - s->cmp + ((s->cr & CR_RLD) ? EPIT_TIMER_MAX : s->lr);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100175 }
Peter Chubb23005812013-08-20 14:54:32 +0100176 ptimer_set_count(s->timer_cmp, next);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100177 }
178}
179
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100180static void imx_epit_write(void *opaque, hwaddr offset, uint64_t value,
181 unsigned size)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100182{
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100183 IMXEPITState *s = IMX_EPIT(opaque);
Peter Chubb23005812013-08-20 14:54:32 +0100184 uint64_t oldcr;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100185
Jean-Christophe Dubois4929f652015-10-25 15:16:24 +0100186 DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(offset >> 2),
187 (uint32_t)value);
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100188
Jean-Christophe Dubois4929f652015-10-25 15:16:24 +0100189 switch (offset >> 2) {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100190 case 0: /* CR */
Peter Chubb23005812013-08-20 14:54:32 +0100191
192 oldcr = s->cr;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100193 s->cr = value & 0x03ffffff;
194 if (s->cr & CR_SWR) {
195 /* handle the reset */
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100196 imx_epit_reset(DEVICE(s));
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100197 } else {
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100198 imx_epit_set_freq(s);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100199 }
200
Peter Chubb23005812013-08-20 14:54:32 +0100201 if (s->freq && (s->cr & CR_EN) && !(oldcr & CR_EN)) {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100202 if (s->cr & CR_ENMOD) {
203 if (s->cr & CR_RLD) {
204 ptimer_set_limit(s->timer_reload, s->lr, 1);
Peter Chubb23005812013-08-20 14:54:32 +0100205 ptimer_set_limit(s->timer_cmp, s->lr, 1);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100206 } else {
Michael Tokarev203d65a2014-08-02 00:14:48 +0400207 ptimer_set_limit(s->timer_reload, EPIT_TIMER_MAX, 1);
208 ptimer_set_limit(s->timer_cmp, EPIT_TIMER_MAX, 1);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100209 }
210 }
211
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100212 imx_epit_reload_compare_timer(s);
Peter Chubb23005812013-08-20 14:54:32 +0100213 ptimer_run(s->timer_reload, 0);
214 if (s->cr & CR_OCIEN) {
215 ptimer_run(s->timer_cmp, 0);
216 } else {
217 ptimer_stop(s->timer_cmp);
218 }
219 } else if (!(s->cr & CR_EN)) {
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100220 /* stop both timers */
221 ptimer_stop(s->timer_reload);
222 ptimer_stop(s->timer_cmp);
Peter Chubb23005812013-08-20 14:54:32 +0100223 } 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);
227 }
228 } else {
229 ptimer_stop(s->timer_cmp);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100230 }
231 break;
232
233 case 1: /* SR - ACK*/
234 /* writing 1 to OCIF clear the OCIF bit */
235 if (value & 0x01) {
236 s->sr = 0;
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100237 imx_epit_update_int(s);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100238 }
239 break;
240
241 case 2: /* LR - set ticks */
242 s->lr = value;
243
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);
Peter Chubb23005812013-08-20 14:54:32 +0100248 ptimer_set_limit(s->timer_cmp, s->lr, 0);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100249 } 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);
252 }
253
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100254 imx_epit_reload_compare_timer(s);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100255 break;
256
257 case 3: /* CMP */
258 s->cmp = value;
259
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100260 imx_epit_reload_compare_timer(s);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100261
262 break;
263
264 default:
Jean-Christophe Dubois4929f652015-10-25 15:16:24 +0100265 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
266 HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset);
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100267
268 break;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100269 }
270}
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100271static void imx_epit_cmp(void *opaque)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100272{
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100273 IMXEPITState *s = IMX_EPIT(opaque);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100274
Peter Chubb23005812013-08-20 14:54:32 +0100275 DPRINTF("sr was %d\n", s->sr);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100276
Peter Chubb23005812013-08-20 14:54:32 +0100277 s->sr = 1;
278 imx_epit_update_int(s);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100279}
280
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100281static const MemoryRegionOps imx_epit_ops = {
Jean-Christophe Dubois565328f2015-08-13 11:26:20 +0100282 .read = imx_epit_read,
283 .write = imx_epit_write,
284 .endianness = DEVICE_NATIVE_ENDIAN,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100285};
286
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100287static const VMStateDescription vmstate_imx_timer_epit = {
Jean-Christophe Dubois565328f2015-08-13 11:26:20 +0100288 .name = TYPE_IMX_EPIT,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100289 .version_id = 2,
290 .minimum_version_id = 2,
Juan Quintela8f1e8842014-05-13 16:09:35 +0100291 .fields = (VMStateField[]) {
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100292 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),
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100300 VMSTATE_END_OF_LIST()
301 }
302};
303
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100304static void imx_epit_realize(DeviceState *dev, Error **errp)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100305{
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100306 IMXEPITState *s = IMX_EPIT(dev);
307 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100308 QEMUBH *bh;
309
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100310 DPRINTF("\n");
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100311
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100312 sysbus_init_irq(sbd, &s->irq);
Paolo Bonzini853dca12013-06-06 21:25:08 -0400313 memory_region_init_io(&s->iomem, OBJECT(s), &imx_epit_ops, s, TYPE_IMX_EPIT,
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100314 0x00001000);
315 sysbus_init_mmio(sbd, &s->iomem);
316
Peter Chubb23005812013-08-20 14:54:32 +0100317 s->timer_reload = ptimer_init(NULL);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100318
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100319 bh = qemu_bh_new(imx_epit_cmp, s);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100320 s->timer_cmp = ptimer_init(bh);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100321}
322
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100323static void imx_epit_class_init(ObjectClass *klass, void *data)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100324{
325 DeviceClass *dc = DEVICE_CLASS(klass);
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100326
327 dc->realize = imx_epit_realize;
328 dc->reset = imx_epit_reset;
329 dc->vmsd = &vmstate_imx_timer_epit;
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100330 dc->desc = "i.MX periodic timer";
331}
332
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100333static const TypeInfo imx_epit_info = {
334 .name = TYPE_IMX_EPIT,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100335 .parent = TYPE_SYS_BUS_DEVICE,
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100336 .instance_size = sizeof(IMXEPITState),
337 .class_init = imx_epit_class_init,
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100338};
339
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100340static void imx_epit_register_types(void)
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100341{
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100342 type_register_static(&imx_epit_info);
Jean-Christophe DUBOISa50c0d62013-06-03 17:17:45 +0100343}
344
Jean-Christophe DUBOIS95669e62013-06-03 17:17:46 +0100345type_init(imx_epit_register_types)