blob: 81f22d026114f1e79f6da4e6b455b2e632d28b16 [file] [log] [blame]
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01001/*
2 * Virtual hardware watchdog.
3 *
4 * Copyright (C) 2009 Red Hat Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program 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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010018 *
19 * By Richard W.M. Jones (rjones@redhat.com).
20 */
21
22#include "qemu-common.h"
23#include "qemu-timer.h"
24#include "watchdog.h"
25#include "hw.h"
26#include "isa.h"
27#include "pc.h"
28
29/*#define IB700_DEBUG 1*/
30
31#ifdef IB700_DEBUG
32#define ib700_debug(fs,...) \
33 fprintf(stderr,"ib700: %s: "fs,__func__,##__VA_ARGS__)
34#else
35#define ib700_debug(fs,...)
36#endif
37
Juan Quintelaf3092702009-10-15 00:49:17 +020038typedef struct IB700state {
39 ISADevice dev;
Juan Quintelae8f27c72009-10-15 00:54:28 +020040 QEMUTimer *timer;
Juan Quintelaf3092702009-10-15 00:49:17 +020041} IB700State;
42
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010043/* This is the timer. We use a global here because the watchdog
44 * code ensures there is only one watchdog (it is located at a fixed,
45 * unchangable IO port, so there could only ever be one anyway).
46 */
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010047
48/* A write to this register enables the timer. */
49static void ib700_write_enable_reg(void *vp, uint32_t addr, uint32_t data)
50{
Juan Quintelae8f27c72009-10-15 00:54:28 +020051 IB700State *s = vp;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010052 static int time_map[] = {
53 30, 28, 26, 24, 22, 20, 18, 16,
54 14, 12, 10, 8, 6, 4, 2, 0
55 };
Andreas Färberc910cf92010-12-19 17:22:40 +010056 int64_t timeout;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010057
58 ib700_debug("addr = %x, data = %x\n", addr, data);
59
Juan Quintela6ee093c2009-09-10 03:04:26 +020060 timeout = (int64_t) time_map[data & 0xF] * get_ticks_per_sec();
Paolo Bonzini74475452011-03-11 16:47:48 +010061 qemu_mod_timer(s->timer, qemu_get_clock_ns (vm_clock) + timeout);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010062}
63
64/* A write (of any value) to this register disables the timer. */
65static void ib700_write_disable_reg(void *vp, uint32_t addr, uint32_t data)
66{
Juan Quintelae8f27c72009-10-15 00:54:28 +020067 IB700State *s = vp;
68
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010069 ib700_debug("addr = %x, data = %x\n", addr, data);
70
Juan Quintelae8f27c72009-10-15 00:54:28 +020071 qemu_del_timer(s->timer);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010072}
73
74/* This is called when the watchdog expires. */
75static void ib700_timer_expired(void *vp)
76{
Juan Quintelae8f27c72009-10-15 00:54:28 +020077 IB700State *s = vp;
78
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010079 ib700_debug("watchdog expired\n");
80
81 watchdog_perform_action();
Juan Quintelae8f27c72009-10-15 00:54:28 +020082 qemu_del_timer(s->timer);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010083}
84
Juan Quintela99580682009-10-15 00:57:35 +020085static const VMStateDescription vmstate_ib700 = {
86 .name = "ib700_wdt",
87 .version_id = 0,
88 .minimum_version_id = 0,
89 .minimum_version_id_old = 0,
90 .fields = (VMStateField []) {
91 VMSTATE_TIMER(timer, IB700State),
92 VMSTATE_END_OF_LIST()
93 }
94};
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010095
Gerd Hoffmann81a322d2009-08-14 10:36:05 +020096static int wdt_ib700_init(ISADevice *dev)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010097{
Juan Quintelaf3092702009-10-15 00:49:17 +020098 IB700State *s = DO_UPCAST(IB700State, dev, dev);
99
Richard W.M. Jones36888c62010-09-24 16:08:06 +0100100 ib700_debug("watchdog init\n");
101
Paolo Bonzini74475452011-03-11 16:47:48 +0100102 s->timer = qemu_new_timer_ns(vm_clock, ib700_timer_expired, s);
Juan Quintelaf3092702009-10-15 00:49:17 +0200103 register_ioport_write(0x441, 2, 1, ib700_write_disable_reg, s);
104 register_ioport_write(0x443, 2, 1, ib700_write_enable_reg, s);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200105
106 return 0;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100107}
108
Richard W.M. Jones36888c62010-09-24 16:08:06 +0100109static void wdt_ib700_reset(DeviceState *dev)
110{
111 IB700State *s = DO_UPCAST(IB700State, dev.qdev, dev);
112
113 ib700_debug("watchdog reset\n");
114
115 qemu_del_timer(s->timer);
116}
117
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100118static WatchdogTimerModel model = {
119 .wdt_name = "ib700",
120 .wdt_description = "iBASE 700",
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100121};
122
Markus Armbruster09aaa162009-08-21 10:31:34 +0200123static ISADeviceInfo wdt_ib700_info = {
Richard W.M. Jones36888c62010-09-24 16:08:06 +0100124 .qdev.name = "ib700",
125 .qdev.size = sizeof(IB700State),
126 .qdev.vmsd = &vmstate_ib700,
127 .qdev.reset = wdt_ib700_reset,
128 .init = wdt_ib700_init,
Markus Armbruster09aaa162009-08-21 10:31:34 +0200129};
130
131static void wdt_ib700_register_devices(void)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100132{
133 watchdog_add_model(&model);
Markus Armbruster09aaa162009-08-21 10:31:34 +0200134 isa_qdev_register(&wdt_ib700_info);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100135}
Markus Armbruster09aaa162009-08-21 10:31:34 +0200136
137device_init(wdt_ib700_register_devices);