2 * QEMU model of the Altera timer.
4 * Copyright (c) 2012 Chris Wulff <crwulff@gmail.com>
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.1 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
18 * <http://www.gnu.org/licenses/lgpl-2.1.html>
21 #include "qemu/osdep.h"
22 #include "qemu/module.h"
23 #include "qapi/error.h"
25 #include "hw/sysbus.h"
27 #include "hw/ptimer.h"
28 #include "hw/qdev-properties.h"
29 #include "qom/object.h"
39 #define STATUS_TO 0x0001
40 #define STATUS_RUN 0x0002
42 #define CONTROL_ITO 0x0001
43 #define CONTROL_CONT 0x0002
44 #define CONTROL_START 0x0004
45 #define CONTROL_STOP 0x0008
47 #define TYPE_ALTERA_TIMER "ALTR.timer"
48 typedef struct AlteraTimer AlteraTimer
;
49 #define ALTERA_TIMER(obj) \
50 OBJECT_CHECK(AlteraTimer, (obj), TYPE_ALTERA_TIMER)
61 static int timer_irq_state(AlteraTimer
*t
)
63 bool irq
= (t
->regs
[R_STATUS
] & STATUS_TO
) &&
64 (t
->regs
[R_CONTROL
] & CONTROL_ITO
);
68 static uint64_t timer_read(void *opaque
, hwaddr addr
,
71 AlteraTimer
*t
= opaque
;
78 r
= t
->regs
[R_CONTROL
] & (CONTROL_ITO
| CONTROL_CONT
);
82 if (addr
< ARRAY_SIZE(t
->regs
)) {
91 static void timer_write(void *opaque
, hwaddr addr
,
92 uint64_t value
, unsigned int size
)
94 AlteraTimer
*t
= opaque
;
97 int irqState
= timer_irq_state(t
);
103 /* The timeout bit is cleared by writing the status register. */
104 t
->regs
[R_STATUS
] &= ~STATUS_TO
;
108 ptimer_transaction_begin(t
->ptimer
);
109 t
->regs
[R_CONTROL
] = value
& (CONTROL_ITO
| CONTROL_CONT
);
110 if ((value
& CONTROL_START
) &&
111 !(t
->regs
[R_STATUS
] & STATUS_RUN
)) {
112 ptimer_run(t
->ptimer
, 1);
113 t
->regs
[R_STATUS
] |= STATUS_RUN
;
115 if ((value
& CONTROL_STOP
) && (t
->regs
[R_STATUS
] & STATUS_RUN
)) {
116 ptimer_stop(t
->ptimer
);
117 t
->regs
[R_STATUS
] &= ~STATUS_RUN
;
119 ptimer_transaction_commit(t
->ptimer
);
124 ptimer_transaction_begin(t
->ptimer
);
125 t
->regs
[addr
] = value
& 0xFFFF;
126 if (t
->regs
[R_STATUS
] & STATUS_RUN
) {
127 ptimer_stop(t
->ptimer
);
128 t
->regs
[R_STATUS
] &= ~STATUS_RUN
;
130 tvalue
= (t
->regs
[R_PERIODH
] << 16) | t
->regs
[R_PERIODL
];
131 ptimer_set_limit(t
->ptimer
, tvalue
+ 1, 1);
132 ptimer_transaction_commit(t
->ptimer
);
137 count
= ptimer_get_count(t
->ptimer
);
138 t
->regs
[R_SNAPL
] = count
& 0xFFFF;
139 t
->regs
[R_SNAPH
] = count
>> 16;
146 if (irqState
!= timer_irq_state(t
)) {
147 qemu_set_irq(t
->irq
, timer_irq_state(t
));
151 static const MemoryRegionOps timer_ops
= {
153 .write
= timer_write
,
154 .endianness
= DEVICE_NATIVE_ENDIAN
,
156 .min_access_size
= 1,
161 static void timer_hit(void *opaque
)
163 AlteraTimer
*t
= opaque
;
164 const uint64_t tvalue
= (t
->regs
[R_PERIODH
] << 16) | t
->regs
[R_PERIODL
];
166 t
->regs
[R_STATUS
] |= STATUS_TO
;
168 ptimer_set_limit(t
->ptimer
, tvalue
+ 1, 1);
170 if (!(t
->regs
[R_CONTROL
] & CONTROL_CONT
)) {
171 t
->regs
[R_STATUS
] &= ~STATUS_RUN
;
172 ptimer_set_count(t
->ptimer
, tvalue
);
174 ptimer_run(t
->ptimer
, 1);
177 qemu_set_irq(t
->irq
, timer_irq_state(t
));
180 static void altera_timer_realize(DeviceState
*dev
, Error
**errp
)
182 AlteraTimer
*t
= ALTERA_TIMER(dev
);
183 SysBusDevice
*sbd
= SYS_BUS_DEVICE(dev
);
185 if (t
->freq_hz
== 0) {
186 error_setg(errp
, "\"clock-frequency\" property must be provided.");
190 t
->ptimer
= ptimer_init(timer_hit
, t
, PTIMER_POLICY_DEFAULT
);
191 ptimer_transaction_begin(t
->ptimer
);
192 ptimer_set_freq(t
->ptimer
, t
->freq_hz
);
193 ptimer_transaction_commit(t
->ptimer
);
195 memory_region_init_io(&t
->mmio
, OBJECT(t
), &timer_ops
, t
,
196 TYPE_ALTERA_TIMER
, R_MAX
* sizeof(uint32_t));
197 sysbus_init_mmio(sbd
, &t
->mmio
);
200 static void altera_timer_init(Object
*obj
)
202 AlteraTimer
*t
= ALTERA_TIMER(obj
);
203 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
205 sysbus_init_irq(sbd
, &t
->irq
);
208 static void altera_timer_reset(DeviceState
*dev
)
210 AlteraTimer
*t
= ALTERA_TIMER(dev
);
212 ptimer_transaction_begin(t
->ptimer
);
213 ptimer_stop(t
->ptimer
);
214 ptimer_set_limit(t
->ptimer
, 0xffffffff, 1);
215 ptimer_transaction_commit(t
->ptimer
);
216 memset(t
->regs
, 0, sizeof(t
->regs
));
219 static Property altera_timer_properties
[] = {
220 DEFINE_PROP_UINT32("clock-frequency", AlteraTimer
, freq_hz
, 0),
221 DEFINE_PROP_END_OF_LIST(),
224 static void altera_timer_class_init(ObjectClass
*klass
, void *data
)
226 DeviceClass
*dc
= DEVICE_CLASS(klass
);
228 dc
->realize
= altera_timer_realize
;
229 device_class_set_props(dc
, altera_timer_properties
);
230 dc
->reset
= altera_timer_reset
;
233 static const TypeInfo altera_timer_info
= {
234 .name
= TYPE_ALTERA_TIMER
,
235 .parent
= TYPE_SYS_BUS_DEVICE
,
236 .instance_size
= sizeof(AlteraTimer
),
237 .instance_init
= altera_timer_init
,
238 .class_init
= altera_timer_class_init
,
241 static void altera_timer_register(void)
243 type_register_static(&altera_timer_info
);
246 type_init(altera_timer_register
)