hw/sd: sd: Remove duplicated codes in single/multiple block read/write
[qemu/ar7.git] / hw / timer / digic-timer.c
blobe3aae4a45a48b39a84c228bacfc5a730bddce57c
1 /*
2 * QEMU model of the Canon DIGIC timer block.
4 * Copyright (C) 2013 Antony Pavlov <antonynpavlov@gmail.com>
6 * This model is based on reverse engineering efforts
7 * made by CHDK (http://chdk.wikia.com) and
8 * Magic Lantern (http://www.magiclantern.fm) projects
9 * contributors.
11 * See "Timer/Clock Module" docs here:
12 * http://magiclantern.wikia.com/wiki/Register_Map
14 * The QEMU model of the OSTimer in PKUnity SoC by Guan Xuetao
15 * is used as a template.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
29 #include "qemu/osdep.h"
30 #include "hw/sysbus.h"
31 #include "hw/ptimer.h"
32 #include "qemu/module.h"
33 #include "qemu/log.h"
35 #include "hw/timer/digic-timer.h"
36 #include "migration/vmstate.h"
38 static const VMStateDescription vmstate_digic_timer = {
39 .name = "digic.timer",
40 .version_id = 1,
41 .minimum_version_id = 1,
42 .fields = (VMStateField[]) {
43 VMSTATE_PTIMER(ptimer, DigicTimerState),
44 VMSTATE_UINT32(control, DigicTimerState),
45 VMSTATE_UINT32(relvalue, DigicTimerState),
46 VMSTATE_END_OF_LIST()
50 static void digic_timer_reset(DeviceState *dev)
52 DigicTimerState *s = DIGIC_TIMER(dev);
54 ptimer_transaction_begin(s->ptimer);
55 ptimer_stop(s->ptimer);
56 ptimer_transaction_commit(s->ptimer);
57 s->control = 0;
58 s->relvalue = 0;
61 static uint64_t digic_timer_read(void *opaque, hwaddr offset, unsigned size)
63 DigicTimerState *s = opaque;
64 uint64_t ret = 0;
66 switch (offset) {
67 case DIGIC_TIMER_CONTROL:
68 ret = s->control;
69 break;
70 case DIGIC_TIMER_RELVALUE:
71 ret = s->relvalue;
72 break;
73 case DIGIC_TIMER_VALUE:
74 ret = ptimer_get_count(s->ptimer) & 0xffff;
75 break;
76 default:
77 qemu_log_mask(LOG_UNIMP,
78 "digic-timer: read access to unknown register 0x"
79 TARGET_FMT_plx "\n", offset);
82 return ret;
85 static void digic_timer_write(void *opaque, hwaddr offset,
86 uint64_t value, unsigned size)
88 DigicTimerState *s = opaque;
90 switch (offset) {
91 case DIGIC_TIMER_CONTROL:
92 if (value & DIGIC_TIMER_CONTROL_RST) {
93 digic_timer_reset((DeviceState *)s);
94 break;
97 ptimer_transaction_begin(s->ptimer);
98 if (value & DIGIC_TIMER_CONTROL_EN) {
99 ptimer_run(s->ptimer, 0);
102 s->control = (uint32_t)value;
103 ptimer_transaction_commit(s->ptimer);
104 break;
106 case DIGIC_TIMER_RELVALUE:
107 s->relvalue = extract32(value, 0, 16);
108 ptimer_transaction_begin(s->ptimer);
109 ptimer_set_limit(s->ptimer, s->relvalue, 1);
110 ptimer_transaction_commit(s->ptimer);
111 break;
113 case DIGIC_TIMER_VALUE:
114 break;
116 default:
117 qemu_log_mask(LOG_UNIMP,
118 "digic-timer: read access to unknown register 0x"
119 TARGET_FMT_plx "\n", offset);
123 static const MemoryRegionOps digic_timer_ops = {
124 .read = digic_timer_read,
125 .write = digic_timer_write,
126 .impl = {
127 .min_access_size = 4,
128 .max_access_size = 4,
130 .endianness = DEVICE_NATIVE_ENDIAN,
133 static void digic_timer_tick(void *opaque)
135 /* Nothing to do on timer rollover */
138 static void digic_timer_init(Object *obj)
140 DigicTimerState *s = DIGIC_TIMER(obj);
142 s->ptimer = ptimer_init(digic_timer_tick, NULL, PTIMER_POLICY_DEFAULT);
145 * FIXME: there is no documentation on Digic timer
146 * frequency setup so let it always run at 1 MHz
148 ptimer_transaction_begin(s->ptimer);
149 ptimer_set_freq(s->ptimer, 1 * 1000 * 1000);
150 ptimer_transaction_commit(s->ptimer);
152 memory_region_init_io(&s->iomem, OBJECT(s), &digic_timer_ops, s,
153 TYPE_DIGIC_TIMER, 0x100);
154 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
157 static void digic_timer_finalize(Object *obj)
159 DigicTimerState *s = DIGIC_TIMER(obj);
161 ptimer_free(s->ptimer);
164 static void digic_timer_class_init(ObjectClass *klass, void *class_data)
166 DeviceClass *dc = DEVICE_CLASS(klass);
168 dc->reset = digic_timer_reset;
169 dc->vmsd = &vmstate_digic_timer;
172 static const TypeInfo digic_timer_info = {
173 .name = TYPE_DIGIC_TIMER,
174 .parent = TYPE_SYS_BUS_DEVICE,
175 .instance_size = sizeof(DigicTimerState),
176 .instance_init = digic_timer_init,
177 .instance_finalize = digic_timer_finalize,
178 .class_init = digic_timer_class_init,
181 static void digic_timer_register_type(void)
183 type_register_static(&digic_timer_info);
186 type_init(digic_timer_register_type)