2 * Private peripheral timer/watchdog blocks for ARM 11MPCore and A9MP
4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Copyright (c) 2011 Linaro Limited
6 * Written by Paul Brook, Peter Maydell
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "qemu-timer.h"
25 /* This device implements the per-cpu private timer and watchdog block
26 * which is used in both the ARM11MPCore and Cortex-A9MP.
31 /* State of a single timer or watchdog block */
46 timerblock timerblock
[MAX_CPUS
* 2];
47 MemoryRegion iomem
[2];
50 static inline int get_current_cpu(arm_mptimer_state
*s
)
52 if (cpu_single_env
->cpu_index
>= s
->num_cpu
) {
53 hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
54 s
->num_cpu
, cpu_single_env
->cpu_index
);
56 return cpu_single_env
->cpu_index
;
59 static inline void timerblock_update_irq(timerblock
*tb
)
61 qemu_set_irq(tb
->irq
, tb
->status
);
64 /* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
65 static inline uint32_t timerblock_scale(timerblock
*tb
)
67 return (((tb
->control
>> 8) & 0xff) + 1) * 10;
70 static void timerblock_reload(timerblock
*tb
, int restart
)
76 tb
->tick
= qemu_get_clock_ns(vm_clock
);
78 tb
->tick
+= (int64_t)tb
->count
* timerblock_scale(tb
);
79 qemu_mod_timer(tb
->timer
, tb
->tick
);
82 static void timerblock_tick(void *opaque
)
84 timerblock
*tb
= (timerblock
*)opaque
;
86 if (tb
->control
& 2) {
88 timerblock_reload(tb
, 0);
92 timerblock_update_irq(tb
);
95 static uint64_t timerblock_read(void *opaque
, target_phys_addr_t addr
,
98 timerblock
*tb
= (timerblock
*)opaque
;
104 case 4: /* Counter. */
105 if (((tb
->control
& 1) == 0) || (tb
->count
== 0)) {
108 /* Slow and ugly, but hopefully won't happen too often. */
109 val
= tb
->tick
- qemu_get_clock_ns(vm_clock
);
110 val
/= timerblock_scale(tb
);
115 case 8: /* Control. */
117 case 12: /* Interrupt status. */
124 static void timerblock_write(void *opaque
, target_phys_addr_t addr
,
125 uint64_t value
, unsigned size
)
127 timerblock
*tb
= (timerblock
*)opaque
;
134 case 4: /* Counter. */
135 if ((tb
->control
& 1) && tb
->count
) {
136 /* Cancel the previous timer. */
137 qemu_del_timer(tb
->timer
);
140 if (tb
->control
& 1) {
141 timerblock_reload(tb
, 1);
144 case 8: /* Control. */
147 if (((old
& 1) == 0) && (value
& 1)) {
148 if (tb
->count
== 0 && (tb
->control
& 2)) {
149 tb
->count
= tb
->load
;
151 timerblock_reload(tb
, 1);
154 case 12: /* Interrupt status. */
155 tb
->status
&= ~value
;
156 timerblock_update_irq(tb
);
161 /* Wrapper functions to implement the "read timer/watchdog for
162 * the current CPU" memory regions.
164 static uint64_t arm_thistimer_read(void *opaque
, target_phys_addr_t addr
,
167 arm_mptimer_state
*s
= (arm_mptimer_state
*)opaque
;
168 int id
= get_current_cpu(s
);
169 return timerblock_read(&s
->timerblock
[id
* 2], addr
, size
);
172 static void arm_thistimer_write(void *opaque
, target_phys_addr_t addr
,
173 uint64_t value
, unsigned size
)
175 arm_mptimer_state
*s
= (arm_mptimer_state
*)opaque
;
176 int id
= get_current_cpu(s
);
177 timerblock_write(&s
->timerblock
[id
* 2], addr
, value
, size
);
180 static uint64_t arm_thiswdog_read(void *opaque
, target_phys_addr_t addr
,
183 arm_mptimer_state
*s
= (arm_mptimer_state
*)opaque
;
184 int id
= get_current_cpu(s
);
185 return timerblock_read(&s
->timerblock
[id
* 2 + 1], addr
, size
);
188 static void arm_thiswdog_write(void *opaque
, target_phys_addr_t addr
,
189 uint64_t value
, unsigned size
)
191 arm_mptimer_state
*s
= (arm_mptimer_state
*)opaque
;
192 int id
= get_current_cpu(s
);
193 timerblock_write(&s
->timerblock
[id
* 2 + 1], addr
, value
, size
);
196 static const MemoryRegionOps arm_thistimer_ops
= {
197 .read
= arm_thistimer_read
,
198 .write
= arm_thistimer_write
,
200 .min_access_size
= 4,
201 .max_access_size
= 4,
203 .endianness
= DEVICE_NATIVE_ENDIAN
,
206 static const MemoryRegionOps arm_thiswdog_ops
= {
207 .read
= arm_thiswdog_read
,
208 .write
= arm_thiswdog_write
,
210 .min_access_size
= 4,
211 .max_access_size
= 4,
213 .endianness
= DEVICE_NATIVE_ENDIAN
,
216 static const MemoryRegionOps timerblock_ops
= {
217 .read
= timerblock_read
,
218 .write
= timerblock_write
,
220 .min_access_size
= 4,
221 .max_access_size
= 4,
223 .endianness
= DEVICE_NATIVE_ENDIAN
,
226 static void timerblock_reset(timerblock
*tb
)
235 static void arm_mptimer_reset(DeviceState
*dev
)
237 arm_mptimer_state
*s
=
238 FROM_SYSBUS(arm_mptimer_state
, sysbus_from_qdev(dev
));
240 /* We reset every timer in the array, not just the ones we're using,
241 * because vmsave will look at every array element.
243 for (i
= 0; i
< ARRAY_SIZE(s
->timerblock
); i
++) {
244 timerblock_reset(&s
->timerblock
[i
]);
248 static int arm_mptimer_init(SysBusDevice
*dev
)
250 arm_mptimer_state
*s
= FROM_SYSBUS(arm_mptimer_state
, dev
);
252 if (s
->num_cpu
< 1 || s
->num_cpu
> MAX_CPUS
) {
253 hw_error("%s: num-cpu must be between 1 and %d\n", __func__
, MAX_CPUS
);
255 /* We implement one timer and one watchdog block per CPU, and
256 * expose multiple MMIO regions:
257 * * region 0 is "timer for this core"
258 * * region 1 is "watchdog for this core"
259 * * region 2 is "timer for core 0"
260 * * region 3 is "watchdog for core 0"
261 * * region 4 is "timer for core 1"
262 * * region 5 is "watchdog for core 1"
264 * The outgoing interrupt lines are
266 * * watchdog for core 0
268 * * watchdog for core 1
271 memory_region_init_io(&s
->iomem
[0], &arm_thistimer_ops
, s
,
272 "arm_mptimer_timer", 0x20);
273 sysbus_init_mmio(dev
, &s
->iomem
[0]);
274 memory_region_init_io(&s
->iomem
[1], &arm_thiswdog_ops
, s
,
275 "arm_mptimer_wdog", 0x20);
276 sysbus_init_mmio(dev
, &s
->iomem
[1]);
277 for (i
= 0; i
< (s
->num_cpu
* 2); i
++) {
278 timerblock
*tb
= &s
->timerblock
[i
];
279 tb
->timer
= qemu_new_timer_ns(vm_clock
, timerblock_tick
, tb
);
280 sysbus_init_irq(dev
, &tb
->irq
);
281 memory_region_init_io(&tb
->iomem
, &timerblock_ops
, tb
,
282 "arm_mptimer_timerblock", 0x20);
283 sysbus_init_mmio(dev
, &tb
->iomem
);
289 static const VMStateDescription vmstate_timerblock
= {
290 .name
= "arm_mptimer_timerblock",
292 .minimum_version_id
= 1,
293 .fields
= (VMStateField
[]) {
294 VMSTATE_UINT32(count
, timerblock
),
295 VMSTATE_UINT32(load
, timerblock
),
296 VMSTATE_UINT32(control
, timerblock
),
297 VMSTATE_UINT32(status
, timerblock
),
298 VMSTATE_INT64(tick
, timerblock
),
299 VMSTATE_END_OF_LIST()
303 static const VMStateDescription vmstate_arm_mptimer
= {
304 .name
= "arm_mptimer",
306 .minimum_version_id
= 1,
307 .fields
= (VMStateField
[]) {
308 VMSTATE_STRUCT_ARRAY(timerblock
, arm_mptimer_state
, (MAX_CPUS
* 2),
309 1, vmstate_timerblock
, timerblock
),
310 VMSTATE_END_OF_LIST()
314 static SysBusDeviceInfo arm_mptimer_info
= {
315 .init
= arm_mptimer_init
,
316 .qdev
.name
= "arm_mptimer",
317 .qdev
.size
= sizeof(arm_mptimer_state
),
318 .qdev
.vmsd
= &vmstate_arm_mptimer
,
319 .qdev
.reset
= arm_mptimer_reset
,
321 .qdev
.props
= (Property
[]) {
322 DEFINE_PROP_UINT32("num-cpu", arm_mptimer_state
, num_cpu
, 0),
323 DEFINE_PROP_END_OF_LIST()
327 static void arm_mptimer_register_devices(void)
329 sysbus_register_withprop(&arm_mptimer_info
);
332 device_init(arm_mptimer_register_devices
)