ati-vga: Fix check for blt outside vram
[qemu/ar7.git] / hw / timer / imx_epit.c
blob8677b753b1f7c75bda86c31992ade2e929166207
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 <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"
19 #include "qemu/log.h"
21 #ifndef DEBUG_IMX_EPIT
22 #define DEBUG_IMX_EPIT 0
23 #endif
25 #define DPRINTF(fmt, args...) \
26 do { \
27 if (DEBUG_IMX_EPIT) { \
28 fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_EPIT, \
29 __func__, ##args); \
30 } \
31 } while (0)
33 static const char *imx_epit_reg_name(uint32_t reg)
35 switch (reg) {
36 case 0:
37 return "CR";
38 case 1:
39 return "SR";
40 case 2:
41 return "LR";
42 case 3:
43 return "CMP";
44 case 4:
45 return "CNT";
46 default:
47 return "[?]";
52 * Exact clock frequencies vary from board to board.
53 * These are typical.
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);
69 } else {
70 qemu_irq_lower(s->irq);
74 static void imx_epit_set_freq(IMXEPITState *s)
76 uint32_t clksrc;
77 uint32_t prescaler;
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);
87 if (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);
101 s->sr = 0;
102 s->lr = EPIT_TIMER_MAX;
103 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 */
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);
123 return s->cnt;
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 */
133 reg_value = s->cr;
134 break;
136 case 1: /* Status Register */
137 reg_value = s->sr;
138 break;
140 case 2: /* LR - ticks*/
141 reg_value = s->lr;
142 break;
144 case 3: /* CMP */
145 reg_value = s->cmp;
146 break;
148 case 4: /* CNT */
149 imx_epit_update_count(s);
150 reg_value = s->cnt;
151 break;
153 default:
154 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
155 HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset);
156 break;
159 DPRINTF("(%s) = 0x%08x\n", imx_epit_reg_name(offset >> 2), reg_value);
161 return 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);
169 uint64_t next;
170 if (tmp > s->cmp) {
171 /* It'll fire in this round of the timer */
172 next = tmp - s->cmp;
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,
181 unsigned size)
183 IMXEPITState *s = IMX_EPIT(opaque);
184 uint64_t oldcr;
186 DPRINTF("(%s, value = 0x%08x)\n", imx_epit_reg_name(offset >> 2),
187 (uint32_t)value);
189 switch (offset >> 2) {
190 case 0: /* CR */
192 oldcr = s->cr;
193 s->cr = value & 0x03ffffff;
194 if (s->cr & CR_SWR) {
195 /* handle the reset */
196 imx_epit_reset(DEVICE(s));
197 } else {
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);
206 } else {
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);
216 } else {
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);
228 } else {
229 ptimer_stop(s->timer_cmp);
231 break;
233 case 1: /* SR - ACK*/
234 /* writing 1 to OCIF clear the OCIF bit */
235 if (value & 0x01) {
236 s->sr = 0;
237 imx_epit_update_int(s);
239 break;
241 case 2: /* LR - set ticks */
242 s->lr = value;
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);
255 break;
257 case 3: /* CMP */
258 s->cmp = value;
260 imx_epit_reload_compare_timer(s);
262 break;
264 default:
265 qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%"
266 HWADDR_PRIx "\n", TYPE_IMX_EPIT, __func__, offset);
268 break;
271 static void imx_epit_cmp(void *opaque)
273 IMXEPITState *s = IMX_EPIT(opaque);
275 DPRINTF("sr was %d\n", s->sr);
277 s->sr = 1;
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,
289 .version_id = 2,
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);
308 QEMUBH *bh;
310 DPRINTF("\n");
312 sysbus_init_irq(sbd, &s->irq);
313 memory_region_init_io(&s->iomem, OBJECT(s), &imx_epit_ops, s, TYPE_IMX_EPIT,
314 0x00001000);
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)