2 * Allwinner A10 timer device emulation
4 * Copyright (C) 2013 Li Guang
5 * Written by Li Guang <lig.fnst@cn.fujitsu.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "qemu/osdep.h"
19 #include "hw/sysbus.h"
20 #include "sysemu/sysemu.h"
21 #include "hw/timer/allwinner-a10-pit.h"
23 static void a10_pit_update_irq(AwA10PITState *s)
27 for (i = 0; i < AW_A10_PIT_TIMER_NR; i++) {
28 qemu_set_irq(s->irq[i], !!(s->irq_status & s->irq_enable & (1 << i)));
32 static uint64_t a10_pit_read(void *opaque, hwaddr offset, unsigned size)
34 AwA10PITState *s = AW_A10_PIT(opaque);
38 case AW_A10_PIT_TIMER_IRQ_EN:
40 case AW_A10_PIT_TIMER_IRQ_ST:
42 case AW_A10_PIT_TIMER_BASE ... AW_A10_PIT_TIMER_BASE_END:
43 index = offset & 0xf0;
46 switch (offset & 0x0f) {
47 case AW_A10_PIT_TIMER_CONTROL:
48 return s->control[index];
49 case AW_A10_PIT_TIMER_INTERVAL:
50 return s->interval[index];
51 case AW_A10_PIT_TIMER_COUNT:
52 s->count[index] = ptimer_get_count(s->timer[index]);
53 return s->count[index];
55 qemu_log_mask(LOG_GUEST_ERROR,
56 "%s: Bad offset 0x%x\n", __func__, (int)offset);
59 case AW_A10_PIT_WDOG_CONTROL:
61 case AW_A10_PIT_WDOG_MODE:
63 case AW_A10_PIT_COUNT_LO:
65 case AW_A10_PIT_COUNT_HI:
67 case AW_A10_PIT_COUNT_CTL:
70 qemu_log_mask(LOG_GUEST_ERROR,
71 "%s: Bad offset 0x%x\n", __func__, (int)offset);
78 static void a10_pit_set_freq(AwA10PITState *s, int index)
80 uint32_t prescaler, source, source_freq;
82 prescaler = 1 << extract32(s->control[index], 4, 3);
83 source = extract32(s->control[index], 2, 2);
84 source_freq = s->clk_freq[source];
87 ptimer_set_freq(s->timer[index], source_freq / prescaler);
89 qemu_log_mask(LOG_GUEST_ERROR, "%s: Invalid clock source %u\n",
94 static void a10_pit_write(void *opaque, hwaddr offset, uint64_t value,
97 AwA10PITState *s = AW_A10_PIT(opaque);
101 case AW_A10_PIT_TIMER_IRQ_EN:
102 s->irq_enable = value;
103 a10_pit_update_irq(s);
105 case AW_A10_PIT_TIMER_IRQ_ST:
106 s->irq_status &= ~value;
107 a10_pit_update_irq(s);
109 case AW_A10_PIT_TIMER_BASE ... AW_A10_PIT_TIMER_BASE_END:
110 index = offset & 0xf0;
113 switch (offset & 0x0f) {
114 case AW_A10_PIT_TIMER_CONTROL:
115 s->control[index] = value;
116 a10_pit_set_freq(s, index);
117 if (s->control[index] & AW_A10_PIT_TIMER_RELOAD) {
118 ptimer_set_count(s->timer[index], s->interval[index]);
120 if (s->control[index] & AW_A10_PIT_TIMER_EN) {
122 if (s->control[index] & AW_A10_PIT_TIMER_MODE) {
125 ptimer_run(s->timer[index], oneshot);
127 ptimer_stop(s->timer[index]);
130 case AW_A10_PIT_TIMER_INTERVAL:
131 s->interval[index] = value;
132 ptimer_set_limit(s->timer[index], s->interval[index], 1);
134 case AW_A10_PIT_TIMER_COUNT:
135 s->count[index] = value;
138 qemu_log_mask(LOG_GUEST_ERROR,
139 "%s: Bad offset 0x%x\n", __func__, (int)offset);
142 case AW_A10_PIT_WDOG_CONTROL:
143 s->watch_dog_control = value;
145 case AW_A10_PIT_WDOG_MODE:
146 s->watch_dog_mode = value;
148 case AW_A10_PIT_COUNT_LO:
151 case AW_A10_PIT_COUNT_HI:
154 case AW_A10_PIT_COUNT_CTL:
155 s->count_ctl = value;
156 if (s->count_ctl & AW_A10_PIT_COUNT_RL_EN) {
157 uint64_t tmp_count = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
159 s->count_lo = tmp_count;
160 s->count_hi = tmp_count >> 32;
161 s->count_ctl &= ~AW_A10_PIT_COUNT_RL_EN;
163 if (s->count_ctl & AW_A10_PIT_COUNT_CLR_EN) {
166 s->count_ctl &= ~AW_A10_PIT_COUNT_CLR_EN;
170 qemu_log_mask(LOG_GUEST_ERROR,
171 "%s: Bad offset 0x%x\n", __func__, (int)offset);
176 static const MemoryRegionOps a10_pit_ops = {
177 .read = a10_pit_read,
178 .write = a10_pit_write,
179 .endianness = DEVICE_NATIVE_ENDIAN,
182 static Property a10_pit_properties[] = {
183 DEFINE_PROP_UINT32("clk0-freq", AwA10PITState, clk_freq[0], 0),
184 DEFINE_PROP_UINT32("clk1-freq", AwA10PITState, clk_freq[1], 0),
185 DEFINE_PROP_UINT32("clk2-freq", AwA10PITState, clk_freq[2], 0),
186 DEFINE_PROP_UINT32("clk3-freq", AwA10PITState, clk_freq[3], 0),
187 DEFINE_PROP_END_OF_LIST(),
190 static const VMStateDescription vmstate_a10_pit = {
193 .minimum_version_id = 1,
194 .fields = (VMStateField[]) {
195 VMSTATE_UINT32(irq_enable, AwA10PITState),
196 VMSTATE_UINT32(irq_status, AwA10PITState),
197 VMSTATE_UINT32_ARRAY(control, AwA10PITState, AW_A10_PIT_TIMER_NR),
198 VMSTATE_UINT32_ARRAY(interval, AwA10PITState, AW_A10_PIT_TIMER_NR),
199 VMSTATE_UINT32_ARRAY(count, AwA10PITState, AW_A10_PIT_TIMER_NR),
200 VMSTATE_UINT32(watch_dog_mode, AwA10PITState),
201 VMSTATE_UINT32(watch_dog_control, AwA10PITState),
202 VMSTATE_UINT32(count_lo, AwA10PITState),
203 VMSTATE_UINT32(count_hi, AwA10PITState),
204 VMSTATE_UINT32(count_ctl, AwA10PITState),
205 VMSTATE_PTIMER_ARRAY(timer, AwA10PITState, AW_A10_PIT_TIMER_NR),
206 VMSTATE_END_OF_LIST()
210 static void a10_pit_reset(DeviceState *dev)
212 AwA10PITState *s = AW_A10_PIT(dev);
217 a10_pit_update_irq(s);
219 for (i = 0; i < 6; i++) {
220 s->control[i] = AW_A10_PIT_DEFAULT_CLOCK;
223 ptimer_stop(s->timer[i]);
224 a10_pit_set_freq(s, i);
226 s->watch_dog_mode = 0;
227 s->watch_dog_control = 0;
233 static void a10_pit_timer_cb(void *opaque)
235 AwA10TimerContext *tc = opaque;
236 AwA10PITState *s = tc->container;
237 uint8_t i = tc->index;
239 if (s->control[i] & AW_A10_PIT_TIMER_EN) {
240 s->irq_status |= 1 << i;
241 if (s->control[i] & AW_A10_PIT_TIMER_MODE) {
242 ptimer_stop(s->timer[i]);
243 s->control[i] &= ~AW_A10_PIT_TIMER_EN;
245 a10_pit_update_irq(s);
249 static void a10_pit_init(Object *obj)
251 AwA10PITState *s = AW_A10_PIT(obj);
252 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
253 QEMUBH * bh[AW_A10_PIT_TIMER_NR];
256 for (i = 0; i < AW_A10_PIT_TIMER_NR; i++) {
257 sysbus_init_irq(sbd, &s->irq[i]);
259 memory_region_init_io(&s->iomem, OBJECT(s), &a10_pit_ops, s,
260 TYPE_AW_A10_PIT, 0x400);
261 sysbus_init_mmio(sbd, &s->iomem);
263 for (i = 0; i < AW_A10_PIT_TIMER_NR; i++) {
264 AwA10TimerContext *tc = &s->timer_context[i];
268 bh[i] = qemu_bh_new(a10_pit_timer_cb, tc);
269 s->timer[i] = ptimer_init(bh[i]);
273 static void a10_pit_class_init(ObjectClass *klass, void *data)
275 DeviceClass *dc = DEVICE_CLASS(klass);
277 dc->reset = a10_pit_reset;
278 dc->props = a10_pit_properties;
279 dc->desc = "allwinner a10 timer";
280 dc->vmsd = &vmstate_a10_pit;
283 static const TypeInfo a10_pit_info = {
284 .name = TYPE_AW_A10_PIT,
285 .parent = TYPE_SYS_BUS_DEVICE,
286 .instance_size = sizeof(AwA10PITState),
287 .instance_init = a10_pit_init,
288 .class_init = a10_pit_class_init,
291 static void a10_register_types(void)
293 type_register_static(&a10_pit_info);
296 type_init(a10_register_types);