2 * QEMU model of the LatticeMico32 timer block.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Specification available at:
21 * http://www.latticesemi.com/documents/mico32timer.pdf
24 #include "qemu/osdep.h"
26 #include "hw/sysbus.h"
27 #include "migration/vmstate.h"
29 #include "qemu/timer.h"
30 #include "hw/ptimer.h"
31 #include "hw/qdev-properties.h"
32 #include "qemu/error-report.h"
33 #include "qemu/module.h"
34 #include "qom/object.h"
36 #define DEFAULT_FREQUENCY (50*1000000)
58 #define TYPE_LM32_TIMER "lm32-timer"
59 typedef struct LM32TimerState LM32TimerState
;
60 DECLARE_INSTANCE_CHECKER(LM32TimerState
, LM32_TIMER
,
63 struct LM32TimerState
{
64 SysBusDevice parent_obj
;
76 static void timer_update_irq(LM32TimerState
*s
)
78 int state
= (s
->regs
[R_SR
] & SR_TO
) && (s
->regs
[R_CR
] & CR_ITO
);
80 trace_lm32_timer_irq_state(state
);
81 qemu_set_irq(s
->irq
, state
);
84 static uint64_t timer_read(void *opaque
, hwaddr addr
, unsigned size
)
86 LM32TimerState
*s
= opaque
;
97 r
= (uint32_t)ptimer_get_count(s
->ptimer
);
100 error_report("lm32_timer: read access to unknown register 0x"
101 TARGET_FMT_plx
, addr
<< 2);
105 trace_lm32_timer_memory_read(addr
<< 2, r
);
109 static void timer_write(void *opaque
, hwaddr addr
,
110 uint64_t value
, unsigned size
)
112 LM32TimerState
*s
= opaque
;
114 trace_lm32_timer_memory_write(addr
, value
);
119 s
->regs
[R_SR
] &= ~SR_TO
;
122 ptimer_transaction_begin(s
->ptimer
);
123 s
->regs
[R_CR
] = value
;
124 if (s
->regs
[R_CR
] & CR_START
) {
125 ptimer_run(s
->ptimer
, 1);
127 if (s
->regs
[R_CR
] & CR_STOP
) {
128 ptimer_stop(s
->ptimer
);
130 ptimer_transaction_commit(s
->ptimer
);
133 s
->regs
[R_PERIOD
] = value
;
134 ptimer_transaction_begin(s
->ptimer
);
135 ptimer_set_count(s
->ptimer
, value
);
136 ptimer_transaction_commit(s
->ptimer
);
139 error_report("lm32_timer: write access to read only register 0x"
140 TARGET_FMT_plx
, addr
<< 2);
143 error_report("lm32_timer: write access to unknown register 0x"
144 TARGET_FMT_plx
, addr
<< 2);
150 static const MemoryRegionOps timer_ops
= {
152 .write
= timer_write
,
153 .endianness
= DEVICE_NATIVE_ENDIAN
,
155 .min_access_size
= 4,
156 .max_access_size
= 4,
160 static void timer_hit(void *opaque
)
162 LM32TimerState
*s
= opaque
;
164 trace_lm32_timer_hit();
166 s
->regs
[R_SR
] |= SR_TO
;
168 if (s
->regs
[R_CR
] & CR_CONT
) {
169 ptimer_set_count(s
->ptimer
, s
->regs
[R_PERIOD
]);
170 ptimer_run(s
->ptimer
, 1);
175 static void timer_reset(DeviceState
*d
)
177 LM32TimerState
*s
= LM32_TIMER(d
);
180 for (i
= 0; i
< R_MAX
; i
++) {
183 ptimer_transaction_begin(s
->ptimer
);
184 ptimer_stop(s
->ptimer
);
185 ptimer_transaction_commit(s
->ptimer
);
188 static void lm32_timer_init(Object
*obj
)
190 LM32TimerState
*s
= LM32_TIMER(obj
);
191 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
193 sysbus_init_irq(dev
, &s
->irq
);
195 memory_region_init_io(&s
->iomem
, obj
, &timer_ops
, s
,
197 sysbus_init_mmio(dev
, &s
->iomem
);
200 static void lm32_timer_realize(DeviceState
*dev
, Error
**errp
)
202 LM32TimerState
*s
= LM32_TIMER(dev
);
204 s
->ptimer
= ptimer_init(timer_hit
, s
, PTIMER_POLICY_DEFAULT
);
206 ptimer_transaction_begin(s
->ptimer
);
207 ptimer_set_freq(s
->ptimer
, s
->freq_hz
);
208 ptimer_transaction_commit(s
->ptimer
);
211 static const VMStateDescription vmstate_lm32_timer
= {
212 .name
= "lm32-timer",
214 .minimum_version_id
= 1,
215 .fields
= (VMStateField
[]) {
216 VMSTATE_PTIMER(ptimer
, LM32TimerState
),
217 VMSTATE_UINT32(freq_hz
, LM32TimerState
),
218 VMSTATE_UINT32_ARRAY(regs
, LM32TimerState
, R_MAX
),
219 VMSTATE_END_OF_LIST()
223 static Property lm32_timer_properties
[] = {
224 DEFINE_PROP_UINT32("frequency", LM32TimerState
, freq_hz
, DEFAULT_FREQUENCY
),
225 DEFINE_PROP_END_OF_LIST(),
228 static void lm32_timer_class_init(ObjectClass
*klass
, void *data
)
230 DeviceClass
*dc
= DEVICE_CLASS(klass
);
232 dc
->realize
= lm32_timer_realize
;
233 dc
->reset
= timer_reset
;
234 dc
->vmsd
= &vmstate_lm32_timer
;
235 device_class_set_props(dc
, lm32_timer_properties
);
238 static const TypeInfo lm32_timer_info
= {
239 .name
= TYPE_LM32_TIMER
,
240 .parent
= TYPE_SYS_BUS_DEVICE
,
241 .instance_size
= sizeof(LM32TimerState
),
242 .instance_init
= lm32_timer_init
,
243 .class_init
= lm32_timer_class_init
,
246 static void lm32_timer_register_types(void)
248 type_register_static(&lm32_timer_info
);
251 type_init(lm32_timer_register_types
)