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" |
| 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 Quintela | f309270 | 2009-10-15 00:49:17 +0200 | [diff] [blame] | 38 | typedef struct IB700state { |
| 39 | ISADevice dev; |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 40 | QEMUTimer *timer; |
Juan Quintela | f309270 | 2009-10-15 00:49:17 +0200 | [diff] [blame] | 41 | } IB700State; |
| 42 | |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 43 | /* 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. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 47 | |
| 48 | /* A write to this register enables the timer. */ |
| 49 | static void ib700_write_enable_reg(void *vp, uint32_t addr, uint32_t data) |
| 50 | { |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 51 | IB700State *s = vp; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 52 | 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ärber | c910cf9 | 2010-12-19 17:22:40 +0100 | [diff] [blame] | 56 | int64_t timeout; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 57 | |
| 58 | ib700_debug("addr = %x, data = %x\n", addr, data); |
| 59 | |
Juan Quintela | 6ee093c | 2009-09-10 03:04:26 +0200 | [diff] [blame] | 60 | timeout = (int64_t) time_map[data & 0xF] * get_ticks_per_sec(); |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 61 | qemu_mod_timer(s->timer, qemu_get_clock_ns (vm_clock) + timeout); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /* A write (of any value) to this register disables the timer. */ |
| 65 | static void ib700_write_disable_reg(void *vp, uint32_t addr, uint32_t data) |
| 66 | { |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 67 | IB700State *s = vp; |
| 68 | |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 69 | ib700_debug("addr = %x, data = %x\n", addr, data); |
| 70 | |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 71 | qemu_del_timer(s->timer); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* This is called when the watchdog expires. */ |
| 75 | static void ib700_timer_expired(void *vp) |
| 76 | { |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 77 | IB700State *s = vp; |
| 78 | |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 79 | ib700_debug("watchdog expired\n"); |
| 80 | |
| 81 | watchdog_perform_action(); |
Juan Quintela | e8f27c7 | 2009-10-15 00:54:28 +0200 | [diff] [blame] | 82 | qemu_del_timer(s->timer); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Juan Quintela | 9958068 | 2009-10-15 00:57:35 +0200 | [diff] [blame] | 85 | static 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. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 95 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 96 | static int wdt_ib700_init(ISADevice *dev) |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 97 | { |
Juan Quintela | f309270 | 2009-10-15 00:49:17 +0200 | [diff] [blame] | 98 | IB700State *s = DO_UPCAST(IB700State, dev, dev); |
| 99 | |
Richard W.M. Jones | 36888c6 | 2010-09-24 16:08:06 +0100 | [diff] [blame] | 100 | ib700_debug("watchdog init\n"); |
| 101 | |
Paolo Bonzini | 7447545 | 2011-03-11 16:47:48 +0100 | [diff] [blame] | 102 | s->timer = qemu_new_timer_ns(vm_clock, ib700_timer_expired, s); |
Juan Quintela | f309270 | 2009-10-15 00:49:17 +0200 | [diff] [blame] | 103 | register_ioport_write(0x441, 2, 1, ib700_write_disable_reg, s); |
| 104 | register_ioport_write(0x443, 2, 1, ib700_write_enable_reg, s); |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 105 | |
| 106 | return 0; |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Richard W.M. Jones | 36888c6 | 2010-09-24 16:08:06 +0100 | [diff] [blame] | 109 | static 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. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 118 | static WatchdogTimerModel model = { |
| 119 | .wdt_name = "ib700", |
| 120 | .wdt_description = "iBASE 700", |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 121 | }; |
| 122 | |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 123 | static ISADeviceInfo wdt_ib700_info = { |
Richard W.M. Jones | 36888c6 | 2010-09-24 16:08:06 +0100 | [diff] [blame] | 124 | .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 Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static void wdt_ib700_register_devices(void) |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 132 | { |
| 133 | watchdog_add_model(&model); |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 134 | isa_qdev_register(&wdt_ib700_info); |
Richard W.M. Jones | 9dd986c | 2009-04-25 13:56:19 +0100 | [diff] [blame] | 135 | } |
Markus Armbruster | 09aaa16 | 2009-08-21 10:31:34 +0200 | [diff] [blame] | 136 | |
| 137 | device_init(wdt_ib700_register_devices); |