2 * Virtual hardware watchdog.
4 * Copyright (C) 2009 Red Hat Inc.
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 * By Richard W.M. Jones (rjones@redhat.com).
22 #include "qemu-common.h"
23 #include "qemu/timer.h"
24 #include "sysemu/watchdog.h"
26 #include "hw/isa/isa.h"
27 #include "hw/i386/pc.h"
29 /*#define IB700_DEBUG 1*/
32 #define ib700_debug(fs,...) \
33 fprintf(stderr,"ib700: %s: "fs,__func__,##__VA_ARGS__)
35 #define ib700_debug(fs,...)
38 #define TYPE_IB700 "ib700"
39 #define IB700(obj) OBJECT_CHECK(IB700State, (obj), TYPE_IB700)
41 typedef struct IB700state
{
47 /* This is the timer. We use a global here because the watchdog
48 * code ensures there is only one watchdog (it is located at a fixed,
49 * unchangeable IO port, so there could only ever be one anyway).
52 /* A write to this register enables the timer. */
53 static void ib700_write_enable_reg(void *vp
, uint32_t addr
, uint32_t data
)
56 static int time_map
[] = {
57 30, 28, 26, 24, 22, 20, 18, 16,
58 14, 12, 10, 8, 6, 4, 2, 0
62 ib700_debug("addr = %x, data = %x\n", addr
, data
);
64 timeout
= (int64_t) time_map
[data
& 0xF] * get_ticks_per_sec();
65 timer_mod(s
->timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) + timeout
);
68 /* A write (of any value) to this register disables the timer. */
69 static void ib700_write_disable_reg(void *vp
, uint32_t addr
, uint32_t data
)
73 ib700_debug("addr = %x, data = %x\n", addr
, data
);
78 /* This is called when the watchdog expires. */
79 static void ib700_timer_expired(void *vp
)
83 ib700_debug("watchdog expired\n");
85 watchdog_perform_action();
89 static const VMStateDescription vmstate_ib700
= {
92 .minimum_version_id
= 0,
93 .minimum_version_id_old
= 0,
94 .fields
= (VMStateField
[]) {
95 VMSTATE_TIMER(timer
, IB700State
),
100 static const MemoryRegionPortio wdt_portio_list
[] = {
101 { 0x441, 2, 1, .write
= ib700_write_disable_reg
, },
102 { 0x443, 2, 1, .write
= ib700_write_enable_reg
, },
103 PORTIO_END_OF_LIST(),
106 static void wdt_ib700_realize(DeviceState
*dev
, Error
**errp
)
108 IB700State
*s
= IB700(dev
);
109 PortioList
*port_list
= g_new(PortioList
, 1);
111 ib700_debug("watchdog init\n");
113 s
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, ib700_timer_expired
, s
);
115 portio_list_init(port_list
, OBJECT(s
), wdt_portio_list
, s
, "ib700");
116 portio_list_add(port_list
, isa_address_space_io(&s
->parent_obj
), 0);
119 static void wdt_ib700_reset(DeviceState
*dev
)
121 IB700State
*s
= IB700(dev
);
123 ib700_debug("watchdog reset\n");
128 static WatchdogTimerModel model
= {
130 .wdt_description
= "iBASE 700",
133 static void wdt_ib700_class_init(ObjectClass
*klass
, void *data
)
135 DeviceClass
*dc
= DEVICE_CLASS(klass
);
137 dc
->realize
= wdt_ib700_realize
;
138 dc
->reset
= wdt_ib700_reset
;
139 dc
->vmsd
= &vmstate_ib700
;
140 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
143 static const TypeInfo wdt_ib700_info
= {
145 .parent
= TYPE_ISA_DEVICE
,
146 .instance_size
= sizeof(IB700State
),
147 .class_init
= wdt_ib700_class_init
,
150 static void wdt_ib700_register_types(void)
152 watchdog_add_model(&model
);
153 type_register_static(&wdt_ib700_info
);
156 type_init(wdt_ib700_register_types
)