Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 1 | /* |
| 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 Swirl | 8167ee8 | 2009-07-16 20:47:01 +0000 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 18 | * |
| 19 | * By Richard W.M. Jones (rjones@redhat.com). |
| 20 | */ |
| 21 | |
| 22 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 23 | #include "qemu/timer.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 24 | #include "sysemu/watchdog.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 25 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 26 | #include "hw/isa/isa.h" |
| 27 | #include "hw/i386/pc.h" |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 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 | |
Andreas Färber | 61e477f | 2013-04-27 22:18:55 +0200 | [diff] [blame] | 38 | #define TYPE_IB700 "ib700" |
| 39 | #define IB700(obj) OBJECT_CHECK(IB700State, (obj), TYPE_IB700) |
| 40 | |
Juan Quintela | f309270 | 2009-10-15 00:49:17 +0200 | [diff] [blame] | 41 | typedef struct IB700state { |
Andreas Färber | 61e477f | 2013-04-27 22:18:55 +0200 | [diff] [blame] | 42 | ISADevice parent_obj; |
| 43 | |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 44 | QEMUTimer *timer; |
Juan Quintela | f309270 | 2009-10-15 00:49:17 +0200 | [diff] [blame] | 45 | } IB700State; |
| 46 | |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 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, |
Dong Xu Wang | 66a0a2c | 2011-11-29 16:52:39 +0800 | [diff] [blame] | 49 | * unchangeable IO port, so there could only ever be one anyway). |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 50 | */ |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 51 | |
| 52 | /* A write to this register enables the timer. */ |
| 53 | static void ib700_write_enable_reg(void *vp, uint32_t addr, uint32_t data) |
| 54 | { |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 55 | IB700State *s = vp; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 56 | 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ärber | c910cf9 | 2010-12-19 17:22:40 +0100 | [diff] [blame] | 60 | int64_t timeout; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 61 | |
| 62 | ib700_debug("addr = %x, data = %x\n", addr, data); |
| 63 | |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 64 | timeout = (int64_t) time_map[data & 0xF] * get_ticks_per_sec(); |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 65 | timer_mod(s->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + timeout); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 66 | } |
| 67 | |
| 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) |
| 70 | { |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 71 | IB700State *s = vp; |
| 72 | |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 73 | ib700_debug("addr = %x, data = %x\n", addr, data); |
| 74 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 75 | timer_del(s->timer); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /* This is called when the watchdog expires. */ |
| 79 | static void ib700_timer_expired(void *vp) |
| 80 | { |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 81 | IB700State *s = vp; |
| 82 | |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 83 | ib700_debug("watchdog expired\n"); |
| 84 | |
| 85 | watchdog_perform_action(); |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 86 | timer_del(s->timer); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Juan Quintela | 9958068 | 2009-10-15 00:57:35 +0200 | [diff] [blame] | 89 | static 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. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 99 | |
Jan Kiszka | 0c6c4e2 | 2013-06-22 08:06:56 +0200 | [diff] [blame] | 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(), |
| 104 | }; |
| 105 | |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 106 | static void wdt_ib700_realize(DeviceState *dev, Error **errp) |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 107 | { |
Andreas Färber | 61e477f | 2013-04-27 22:18:55 +0200 | [diff] [blame] | 108 | IB700State *s = IB700(dev); |
Jan Kiszka | 0c6c4e2 | 2013-06-22 08:06:56 +0200 | [diff] [blame] | 109 | PortioList *port_list = g_new(PortioList, 1); |
Juan Quintela | f309270 | 2009-10-15 00:49:17 +0200 | [diff] [blame] | 110 | |
Richard W.M. Jones | 36888c6 | 2010-09-24 16:08:06 +0100 | [diff] [blame] | 111 | ib700_debug("watchdog init\n"); |
| 112 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 113 | s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ib700_timer_expired, s); |
Jan Kiszka | 0c6c4e2 | 2013-06-22 08:06:56 +0200 | [diff] [blame] | 114 | |
Paolo Bonzini | db10ca9 | 2013-06-06 21:19:53 -0400 | [diff] [blame] | 115 | portio_list_init(port_list, OBJECT(s), wdt_portio_list, s, "ib700"); |
Jan Kiszka | 0c6c4e2 | 2013-06-22 08:06:56 +0200 | [diff] [blame] | 116 | portio_list_add(port_list, isa_address_space_io(&s->parent_obj), 0); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Richard W.M. Jones | 36888c6 | 2010-09-24 16:08:06 +0100 | [diff] [blame] | 119 | static void wdt_ib700_reset(DeviceState *dev) |
| 120 | { |
Andreas Färber | 61e477f | 2013-04-27 22:18:55 +0200 | [diff] [blame] | 121 | IB700State *s = IB700(dev); |
Richard W.M. Jones | 36888c6 | 2010-09-24 16:08:06 +0100 | [diff] [blame] | 122 | |
| 123 | ib700_debug("watchdog reset\n"); |
| 124 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 125 | timer_del(s->timer); |
Richard W.M. Jones | 36888c6 | 2010-09-24 16:08:06 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 128 | static WatchdogTimerModel model = { |
| 129 | .wdt_name = "ib700", |
| 130 | .wdt_description = "iBASE 700", |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 131 | }; |
| 132 | |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 133 | static void wdt_ib700_class_init(ObjectClass *klass, void *data) |
| 134 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 135 | DeviceClass *dc = DEVICE_CLASS(klass); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 136 | |
| 137 | dc->realize = wdt_ib700_realize; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 138 | dc->reset = wdt_ib700_reset; |
| 139 | dc->vmsd = &vmstate_ib700; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 140 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 141 | } |
| 142 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 143 | static const TypeInfo wdt_ib700_info = { |
Andreas Färber | 61e477f | 2013-04-27 22:18:55 +0200 | [diff] [blame] | 144 | .name = TYPE_IB700, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 145 | .parent = TYPE_ISA_DEVICE, |
| 146 | .instance_size = sizeof(IB700State), |
| 147 | .class_init = wdt_ib700_class_init, |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 150 | static void wdt_ib700_register_types(void) |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 151 | { |
| 152 | watchdog_add_model(&model); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 153 | type_register_static(&wdt_ib700_info); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 154 | } |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 155 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 156 | type_init(wdt_ib700_register_types) |