2 * Block model of System timer present in
3 * Microsemi's SmartFusion2 and SmartFusion SoCs.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "qemu/module.h"
30 #include "hw/qdev-properties.h"
31 #include "hw/timer/mss-timer.h"
32 #include "migration/vmstate.h"
34 #ifndef MSS_TIMER_ERR_DEBUG
35 #define MSS_TIMER_ERR_DEBUG 0
38 #define DB_PRINT_L(lvl, fmt, args...) do { \
39 if (MSS_TIMER_ERR_DEBUG >= lvl) { \
40 qemu_log("%s: " fmt "\n", __func__, ## args); \
44 #define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
47 #define R_TIM_LOADVAL 1
48 #define R_TIM_BGLOADVAL 2
53 #define TIMER_CTRL_ENBL (1 << 0)
54 #define TIMER_CTRL_ONESHOT (1 << 1)
55 #define TIMER_CTRL_INTR (1 << 2)
56 #define TIMER_RIS_ACK (1 << 0)
57 #define TIMER_RST_CLR (1 << 6)
58 #define TIMER_MODE (1 << 0)
60 static void timer_update_irq(struct Msf2Timer *st)
64 isr = !!(st->regs[R_TIM_RIS] & TIMER_RIS_ACK);
65 ier = !!(st->regs[R_TIM_CTRL] & TIMER_CTRL_INTR);
66 qemu_set_irq(st->irq, (ier && isr));
69 /* Must be called from within a ptimer_transaction_begin/commit block */
70 static void timer_update(struct Msf2Timer *st)
74 if (!(st->regs[R_TIM_CTRL] & TIMER_CTRL_ENBL)) {
75 ptimer_stop(st->ptimer);
79 count = st->regs[R_TIM_LOADVAL];
80 ptimer_set_limit(st->ptimer, count, 1);
81 ptimer_run(st->ptimer, 1);
85 timer_read(void *opaque, hwaddr offset, unsigned int size)
87 MSSTimerState *t = opaque;
97 * Two independent timers has same base address.
98 * Based on address passed figure out which timer is being used.
100 if ((addr >= R_TIM1_MAX) && (addr < NUM_TIMERS * R_TIM1_MAX)) {
105 st = &t->timers[timer];
109 ret = ptimer_get_count(st->ptimer);
113 isr = !!(st->regs[R_TIM_RIS] & TIMER_RIS_ACK);
114 ier = !!(st->regs[R_TIM_CTRL] & TIMER_CTRL_INTR);
119 if (addr < R_TIM1_MAX) {
120 ret = st->regs[addr];
122 qemu_log_mask(LOG_GUEST_ERROR,
123 TYPE_MSS_TIMER": 64-bit mode not supported\n");
129 DB_PRINT("timer=%d 0x%" HWADDR_PRIx "=0x%" PRIx32, timer, offset,
135 timer_write(void *opaque, hwaddr offset,
136 uint64_t val64, unsigned int size)
138 MSSTimerState *t = opaque;
140 struct Msf2Timer *st;
142 uint32_t value = val64;
146 * Two independent timers has same base address.
147 * Based on addr passed figure out which timer is being used.
149 if ((addr >= R_TIM1_MAX) && (addr < NUM_TIMERS * R_TIM1_MAX)) {
154 st = &t->timers[timer];
156 DB_PRINT("addr=0x%" HWADDR_PRIx " val=0x%" PRIx32 " (timer=%d)", offset,
161 st->regs[R_TIM_CTRL] = value;
162 ptimer_transaction_begin(st->ptimer);
164 ptimer_transaction_commit(st->ptimer);
168 if (value & TIMER_RIS_ACK) {
169 st->regs[R_TIM_RIS] &= ~TIMER_RIS_ACK;
174 st->regs[R_TIM_LOADVAL] = value;
175 if (st->regs[R_TIM_CTRL] & TIMER_CTRL_ENBL) {
176 ptimer_transaction_begin(st->ptimer);
178 ptimer_transaction_commit(st->ptimer);
182 case R_TIM_BGLOADVAL:
183 st->regs[R_TIM_BGLOADVAL] = value;
184 st->regs[R_TIM_LOADVAL] = value;
192 if (addr < R_TIM1_MAX) {
193 st->regs[addr] = value;
195 qemu_log_mask(LOG_GUEST_ERROR,
196 TYPE_MSS_TIMER": 64-bit mode not supported\n");
201 timer_update_irq(st);
204 static const MemoryRegionOps timer_ops = {
206 .write = timer_write,
207 .endianness = DEVICE_NATIVE_ENDIAN,
209 .min_access_size = 1,
214 static void timer_hit(void *opaque)
216 struct Msf2Timer *st = opaque;
218 st->regs[R_TIM_RIS] |= TIMER_RIS_ACK;
220 if (!(st->regs[R_TIM_CTRL] & TIMER_CTRL_ONESHOT)) {
223 timer_update_irq(st);
226 static void mss_timer_init(Object *obj)
228 MSSTimerState *t = MSS_TIMER(obj);
231 /* Init all the ptimers. */
232 for (i = 0; i < NUM_TIMERS; i++) {
233 struct Msf2Timer *st = &t->timers[i];
235 st->ptimer = ptimer_init(timer_hit, st, PTIMER_POLICY_DEFAULT);
236 ptimer_transaction_begin(st->ptimer);
237 ptimer_set_freq(st->ptimer, t->freq_hz);
238 ptimer_transaction_commit(st->ptimer);
239 sysbus_init_irq(SYS_BUS_DEVICE(obj), &st->irq);
242 memory_region_init_io(&t->mmio, OBJECT(t), &timer_ops, t, TYPE_MSS_TIMER,
243 NUM_TIMERS * R_TIM1_MAX * 4);
244 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &t->mmio);
247 static const VMStateDescription vmstate_timers = {
248 .name = "mss-timer-block",
250 .minimum_version_id = 1,
251 .fields = (VMStateField[]) {
252 VMSTATE_PTIMER(ptimer, struct Msf2Timer),
253 VMSTATE_UINT32_ARRAY(regs, struct Msf2Timer, R_TIM1_MAX),
254 VMSTATE_END_OF_LIST()
258 static const VMStateDescription vmstate_mss_timer = {
259 .name = TYPE_MSS_TIMER,
261 .minimum_version_id = 1,
262 .fields = (VMStateField[]) {
263 VMSTATE_UINT32(freq_hz, MSSTimerState),
264 VMSTATE_STRUCT_ARRAY(timers, MSSTimerState, NUM_TIMERS, 0,
265 vmstate_timers, struct Msf2Timer),
266 VMSTATE_END_OF_LIST()
270 static Property mss_timer_properties[] = {
271 /* Libero GUI shows 100Mhz as default for clocks */
272 DEFINE_PROP_UINT32("clock-frequency", MSSTimerState, freq_hz,
274 DEFINE_PROP_END_OF_LIST(),
277 static void mss_timer_class_init(ObjectClass *klass, void *data)
279 DeviceClass *dc = DEVICE_CLASS(klass);
281 dc->props = mss_timer_properties;
282 dc->vmsd = &vmstate_mss_timer;
285 static const TypeInfo mss_timer_info = {
286 .name = TYPE_MSS_TIMER,
287 .parent = TYPE_SYS_BUS_DEVICE,
288 .instance_size = sizeof(MSSTimerState),
289 .instance_init = mss_timer_init,
290 .class_init = mss_timer_class_init,
293 static void mss_timer_register_types(void)
295 type_register_static(&mss_timer_info);
298 type_init(mss_timer_register_types)