blob: bc994a4c32e46e92470cd3f6d2f0a6a2a98d5df5 [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"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/timer.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010024#include "sysemu/watchdog.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010025#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010026#include "hw/isa/isa.h"
27#include "hw/i386/pc.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010028
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
Andreas Färber61e477f2013-04-27 22:18:55 +020038#define TYPE_IB700 "ib700"
39#define IB700(obj) OBJECT_CHECK(IB700State, (obj), TYPE_IB700)
40
Juan Quintelaf3092702009-10-15 00:49:17 +020041typedef struct IB700state {
Andreas Färber61e477f2013-04-27 22:18:55 +020042 ISADevice parent_obj;
43
Juan Quintelae8f27c72009-10-15 00:54:28 +020044 QEMUTimer *timer;
Juan Quintelaf3092702009-10-15 00:49:17 +020045} IB700State;
46
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010047/* 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,
Dong Xu Wang66a0a2c2011-11-29 16:52:39 +080049 * unchangeable IO port, so there could only ever be one anyway).
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010050 */
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010051
52/* A write to this register enables the timer. */
53static void ib700_write_enable_reg(void *vp, uint32_t addr, uint32_t data)
54{
Juan Quintelae8f27c72009-10-15 00:54:28 +020055 IB700State *s = vp;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010056 static int time_map[] = {
57 30, 28, 26, 24, 22, 20, 18, 16,
58 14, 12, 10, 8, 6, 4, 2, 0
59 };
Andreas Färberc910cf92010-12-19 17:22:40 +010060 int64_t timeout;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010061
62 ib700_debug("addr = %x, data = %x\n", addr, data);
63
Juan Quintela6ee093c2009-09-10 03:04:26 +020064 timeout = (int64_t) time_map[data & 0xF] * get_ticks_per_sec();
Alex Blighbc72ad62013-08-21 16:03:08 +010065 timer_mod(s->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + timeout);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010066}
67
68/* A write (of any value) to this register disables the timer. */
69static void ib700_write_disable_reg(void *vp, uint32_t addr, uint32_t data)
70{
Juan Quintelae8f27c72009-10-15 00:54:28 +020071 IB700State *s = vp;
72
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010073 ib700_debug("addr = %x, data = %x\n", addr, data);
74
Alex Blighbc72ad62013-08-21 16:03:08 +010075 timer_del(s->timer);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010076}
77
78/* This is called when the watchdog expires. */
79static void ib700_timer_expired(void *vp)
80{
Juan Quintelae8f27c72009-10-15 00:54:28 +020081 IB700State *s = vp;
82
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010083 ib700_debug("watchdog expired\n");
84
85 watchdog_perform_action();
Alex Blighbc72ad62013-08-21 16:03:08 +010086 timer_del(s->timer);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010087}
88
Juan Quintela99580682009-10-15 00:57:35 +020089static const VMStateDescription vmstate_ib700 = {
90 .name = "ib700_wdt",
91 .version_id = 0,
92 .minimum_version_id = 0,
93 .minimum_version_id_old = 0,
94 .fields = (VMStateField []) {
95 VMSTATE_TIMER(timer, IB700State),
96 VMSTATE_END_OF_LIST()
97 }
98};
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +010099
Jan Kiszka0c6c4e22013-06-22 08:06:56 +0200100static 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(),
104};
105
Andreas Färberdb895a12012-11-25 02:37:14 +0100106static void wdt_ib700_realize(DeviceState *dev, Error **errp)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100107{
Andreas Färber61e477f2013-04-27 22:18:55 +0200108 IB700State *s = IB700(dev);
Jan Kiszka0c6c4e22013-06-22 08:06:56 +0200109 PortioList *port_list = g_new(PortioList, 1);
Juan Quintelaf3092702009-10-15 00:49:17 +0200110
Richard W.M. Jones36888c62010-09-24 16:08:06 +0100111 ib700_debug("watchdog init\n");
112
Alex Blighbc72ad62013-08-21 16:03:08 +0100113 s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ib700_timer_expired, s);
Jan Kiszka0c6c4e22013-06-22 08:06:56 +0200114
Paolo Bonzinidb10ca92013-06-06 21:19:53 -0400115 portio_list_init(port_list, OBJECT(s), wdt_portio_list, s, "ib700");
Jan Kiszka0c6c4e22013-06-22 08:06:56 +0200116 portio_list_add(port_list, isa_address_space_io(&s->parent_obj), 0);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100117}
118
Richard W.M. Jones36888c62010-09-24 16:08:06 +0100119static void wdt_ib700_reset(DeviceState *dev)
120{
Andreas Färber61e477f2013-04-27 22:18:55 +0200121 IB700State *s = IB700(dev);
Richard W.M. Jones36888c62010-09-24 16:08:06 +0100122
123 ib700_debug("watchdog reset\n");
124
Alex Blighbc72ad62013-08-21 16:03:08 +0100125 timer_del(s->timer);
Richard W.M. Jones36888c62010-09-24 16:08:06 +0100126}
127
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100128static WatchdogTimerModel model = {
129 .wdt_name = "ib700",
130 .wdt_description = "iBASE 700",
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100131};
132
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600133static void wdt_ib700_class_init(ObjectClass *klass, void *data)
134{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600135 DeviceClass *dc = DEVICE_CLASS(klass);
Andreas Färberdb895a12012-11-25 02:37:14 +0100136
137 dc->realize = wdt_ib700_realize;
Anthony Liguori39bffca2011-12-07 21:34:16 -0600138 dc->reset = wdt_ib700_reset;
139 dc->vmsd = &vmstate_ib700;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300140 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600141}
142
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100143static const TypeInfo wdt_ib700_info = {
Andreas Färber61e477f2013-04-27 22:18:55 +0200144 .name = TYPE_IB700,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600145 .parent = TYPE_ISA_DEVICE,
146 .instance_size = sizeof(IB700State),
147 .class_init = wdt_ib700_class_init,
Markus Armbruster09aaa162009-08-21 10:31:34 +0200148};
149
Andreas Färber83f7d432012-02-09 15:20:55 +0100150static void wdt_ib700_register_types(void)
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100151{
152 watchdog_add_model(&model);
Anthony Liguori39bffca2011-12-07 21:34:16 -0600153 type_register_static(&wdt_ib700_info);
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100154}
Markus Armbruster09aaa162009-08-21 10:31:34 +0200155
Andreas Färber83f7d432012-02-09 15:20:55 +0100156type_init(wdt_ib700_register_types)