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
27 #include "qemu-timer.h"
29 #include "qemu-error.h"
31 #define DEFAULT_FREQUENCY (50*1000000)
53 struct LM32TimerState
{
65 typedef struct LM32TimerState LM32TimerState
;
67 static void timer_update_irq(LM32TimerState
*s
)
69 int state
= (s
->regs
[R_SR
] & SR_TO
) && (s
->regs
[R_CR
] & CR_ITO
);
71 trace_lm32_timer_irq_state(state
);
72 qemu_set_irq(s
->irq
, state
);
75 static uint64_t timer_read(void *opaque
, target_phys_addr_t addr
, unsigned size
)
77 LM32TimerState
*s
= opaque
;
88 r
= (uint32_t)ptimer_get_count(s
->ptimer
);
91 error_report("lm32_timer: read access to unknown register 0x"
92 TARGET_FMT_plx
, addr
<< 2);
96 trace_lm32_timer_memory_read(addr
<< 2, r
);
100 static void timer_write(void *opaque
, target_phys_addr_t addr
,
101 uint64_t value
, unsigned size
)
103 LM32TimerState
*s
= opaque
;
105 trace_lm32_timer_memory_write(addr
, value
);
110 s
->regs
[R_SR
] &= ~SR_TO
;
113 s
->regs
[R_CR
] = value
;
114 if (s
->regs
[R_CR
] & CR_START
) {
115 ptimer_run(s
->ptimer
, 1);
117 if (s
->regs
[R_CR
] & CR_STOP
) {
118 ptimer_stop(s
->ptimer
);
122 s
->regs
[R_PERIOD
] = value
;
123 ptimer_set_count(s
->ptimer
, value
);
126 error_report("lm32_timer: write access to read only register 0x"
127 TARGET_FMT_plx
, addr
<< 2);
130 error_report("lm32_timer: write access to unknown register 0x"
131 TARGET_FMT_plx
, addr
<< 2);
137 static const MemoryRegionOps timer_ops
= {
139 .write
= timer_write
,
140 .endianness
= DEVICE_NATIVE_ENDIAN
,
142 .min_access_size
= 4,
143 .max_access_size
= 4,
147 static void timer_hit(void *opaque
)
149 LM32TimerState
*s
= opaque
;
151 trace_lm32_timer_hit();
153 s
->regs
[R_SR
] |= SR_TO
;
155 if (s
->regs
[R_CR
] & CR_CONT
) {
156 ptimer_set_count(s
->ptimer
, s
->regs
[R_PERIOD
]);
157 ptimer_run(s
->ptimer
, 1);
162 static void timer_reset(DeviceState
*d
)
164 LM32TimerState
*s
= container_of(d
, LM32TimerState
, busdev
.qdev
);
167 for (i
= 0; i
< R_MAX
; i
++) {
170 ptimer_stop(s
->ptimer
);
173 static int lm32_timer_init(SysBusDevice
*dev
)
175 LM32TimerState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
177 sysbus_init_irq(dev
, &s
->irq
);
179 s
->bh
= qemu_bh_new(timer_hit
, s
);
180 s
->ptimer
= ptimer_init(s
->bh
);
181 ptimer_set_freq(s
->ptimer
, s
->freq_hz
);
183 memory_region_init_io(&s
->iomem
, &timer_ops
, s
, "timer", R_MAX
* 4);
184 sysbus_init_mmio(dev
, &s
->iomem
);
189 static const VMStateDescription vmstate_lm32_timer
= {
190 .name
= "lm32-timer",
192 .minimum_version_id
= 1,
193 .minimum_version_id_old
= 1,
194 .fields
= (VMStateField
[]) {
195 VMSTATE_PTIMER(ptimer
, LM32TimerState
),
196 VMSTATE_UINT32(freq_hz
, LM32TimerState
),
197 VMSTATE_UINT32_ARRAY(regs
, LM32TimerState
, R_MAX
),
198 VMSTATE_END_OF_LIST()
202 static SysBusDeviceInfo lm32_timer_info
= {
203 .init
= lm32_timer_init
,
204 .qdev
.name
= "lm32-timer",
205 .qdev
.size
= sizeof(LM32TimerState
),
206 .qdev
.vmsd
= &vmstate_lm32_timer
,
207 .qdev
.reset
= timer_reset
,
208 .qdev
.props
= (Property
[]) {
210 "frequency", LM32TimerState
, freq_hz
, DEFAULT_FREQUENCY
212 DEFINE_PROP_END_OF_LIST(),
216 static void lm32_timer_register(void)
218 sysbus_register_withprop(&lm32_timer_info
);
221 device_init(lm32_timer_register
)