| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Virtual hardware watchdog. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (C) 2009 Red Hat Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							| 
									
										
										
										
											2009-07-16 20:47:01 +00:00
										 |  |  |  * along with this program; if not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * By Richard W.M. Jones (rjones@redhat.com). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-26 18:17:30 +00:00
										 |  |  | #include "qemu/osdep.h"
 | 
					
						
							| 
									
										
										
										
											2019-05-23 16:35:07 +02:00
										 |  |  | #include "qemu/module.h"
 | 
					
						
							| 
									
										
										
										
											2012-12-17 18:20:00 +01:00
										 |  |  | #include "qemu/timer.h"
 | 
					
						
							| 
									
										
										
										
											2013-02-05 17:06:20 +01:00
										 |  |  | #include "sysemu/watchdog.h"
 | 
					
						
							|  |  |  | #include "hw/isa/isa.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-12 07:23:45 +02:00
										 |  |  | #include "migration/vmstate.h"
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | #include "qom/object.h"
 | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*#define IB700_DEBUG 1*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef IB700_DEBUG
 | 
					
						
							| 
									
										
										
										
											2023-03-15 11:26:49 +08:00
										 |  |  | #define ib700_debug(fs,...)                                    \
 | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  |     fprintf(stderr,"ib700: %s: "fs,__func__,##__VA_ARGS__) | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define ib700_debug(fs,...)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-27 22:18:55 +02:00
										 |  |  | #define TYPE_IB700 "ib700"
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | typedef struct IB700state IB700State; | 
					
						
							| 
									
										
										
										
											2020-08-31 17:07:33 -04:00
										 |  |  | DECLARE_INSTANCE_CHECKER(IB700State, IB700, | 
					
						
							|  |  |  |                          TYPE_IB700) | 
					
						
							| 
									
										
										
										
											2013-04-27 22:18:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | struct IB700state { | 
					
						
							| 
									
										
										
										
											2013-04-27 22:18:55 +02:00
										 |  |  |     ISADevice parent_obj; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 00:54:28 +02:00
										 |  |  |     QEMUTimer *timer; | 
					
						
							| 
									
										
										
										
											2014-04-29 17:38:39 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     PortioList port_list; | 
					
						
							| 
									
										
										
										
											2020-09-03 16:43:22 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-10-15 00:49:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | /* This is the timer.  We use a global here because the watchdog
 | 
					
						
							|  |  |  |  * code ensures there is only one watchdog (it is located at a fixed, | 
					
						
							| 
									
										
										
										
											2011-11-29 16:52:39 +08:00
										 |  |  |  * unchangeable IO port, so there could only ever be one anyway). | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* A write to this register enables the timer. */ | 
					
						
							|  |  |  | static void ib700_write_enable_reg(void *vp, uint32_t addr, uint32_t data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-15 00:54:28 +02:00
										 |  |  |     IB700State *s = vp; | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  |     static int time_map[] = { | 
					
						
							|  |  |  |         30, 28, 26, 24, 22, 20, 18, 16, | 
					
						
							|  |  |  |         14, 12, 10,  8,  6,  4,  2,  0 | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2010-12-19 17:22:40 +01:00
										 |  |  |     int64_t timeout; | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ib700_debug("addr = %x, data = %x\n", addr, data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 21:32:30 +05:30
										 |  |  |     timeout = (int64_t) time_map[data & 0xF] * NANOSECONDS_PER_SECOND; | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |     timer_mod(s->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + timeout); | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* A write (of any value) to this register disables the timer. */ | 
					
						
							|  |  |  | static void ib700_write_disable_reg(void *vp, uint32_t addr, uint32_t data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-15 00:54:28 +02:00
										 |  |  |     IB700State *s = vp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  |     ib700_debug("addr = %x, data = %x\n", addr, data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |     timer_del(s->timer); | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* This is called when the watchdog expires. */ | 
					
						
							|  |  |  | static void ib700_timer_expired(void *vp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-15 00:54:28 +02:00
										 |  |  |     IB700State *s = vp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  |     ib700_debug("watchdog expired\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     watchdog_perform_action(); | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |     timer_del(s->timer); | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 00:57:35 +02:00
										 |  |  | static const VMStateDescription vmstate_ib700 = { | 
					
						
							|  |  |  |     .name = "ib700_wdt", | 
					
						
							|  |  |  |     .version_id = 0, | 
					
						
							|  |  |  |     .minimum_version_id = 0, | 
					
						
							| 
									
										
										
										
											2014-04-16 15:32:32 +02:00
										 |  |  |     .fields = (VMStateField[]) { | 
					
						
							| 
									
										
										
										
											2015-01-08 10:18:59 +01:00
										 |  |  |         VMSTATE_TIMER_PTR(timer, IB700State), | 
					
						
							| 
									
										
										
										
											2009-10-15 00:57:35 +02:00
										 |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-22 08:06:56 +02:00
										 |  |  | static const MemoryRegionPortio wdt_portio_list[] = { | 
					
						
							|  |  |  |     { 0x441, 2, 1, .write = ib700_write_disable_reg, }, | 
					
						
							|  |  |  |     { 0x443, 2, 1, .write = ib700_write_enable_reg, }, | 
					
						
							|  |  |  |     PORTIO_END_OF_LIST(), | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-11-25 02:37:14 +01:00
										 |  |  | static void wdt_ib700_realize(DeviceState *dev, Error **errp) | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2013-04-27 22:18:55 +02:00
										 |  |  |     IB700State *s = IB700(dev); | 
					
						
							| 
									
										
										
										
											2009-10-15 00:49:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-24 16:08:06 +01:00
										 |  |  |     ib700_debug("watchdog init\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |     s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ib700_timer_expired, s); | 
					
						
							| 
									
										
										
										
											2013-06-22 08:06:56 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-29 17:38:39 +04:00
										 |  |  |     portio_list_init(&s->port_list, OBJECT(s), wdt_portio_list, s, "ib700"); | 
					
						
							|  |  |  |     portio_list_add(&s->port_list, isa_address_space_io(&s->parent_obj), 0); | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-09-24 16:08:06 +01:00
										 |  |  | static void wdt_ib700_reset(DeviceState *dev) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2013-04-27 22:18:55 +02:00
										 |  |  |     IB700State *s = IB700(dev); | 
					
						
							| 
									
										
										
										
											2010-09-24 16:08:06 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ib700_debug("watchdog reset\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-21 16:03:08 +01:00
										 |  |  |     timer_del(s->timer); | 
					
						
							| 
									
										
										
										
											2010-09-24 16:08:06 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-12-04 11:52:49 -06:00
										 |  |  | static void wdt_ib700_class_init(ObjectClass *klass, void *data) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  |     DeviceClass *dc = DEVICE_CLASS(klass); | 
					
						
							| 
									
										
										
										
											2012-11-25 02:37:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     dc->realize = wdt_ib700_realize; | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  |     dc->reset = wdt_ib700_reset; | 
					
						
							|  |  |  |     dc->vmsd = &vmstate_ib700; | 
					
						
							| 
									
										
										
										
											2021-10-27 14:34:53 +02:00
										 |  |  |     set_bit(DEVICE_CATEGORY_WATCHDOG, dc->categories); | 
					
						
							|  |  |  |     dc->desc = "iBASE 700"; | 
					
						
							| 
									
										
										
										
											2011-12-04 11:52:49 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-10 16:19:07 +01:00
										 |  |  | static const TypeInfo wdt_ib700_info = { | 
					
						
							| 
									
										
										
										
											2013-04-27 22:18:55 +02:00
										 |  |  |     .name          = TYPE_IB700, | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  |     .parent        = TYPE_ISA_DEVICE, | 
					
						
							|  |  |  |     .instance_size = sizeof(IB700State), | 
					
						
							|  |  |  |     .class_init    = wdt_ib700_class_init, | 
					
						
							| 
									
										
										
										
											2009-08-21 10:31:34 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 15:20:55 +01:00
										 |  |  | static void wdt_ib700_register_types(void) | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-12-07 21:34:16 -06:00
										 |  |  |     type_register_static(&wdt_ib700_info); | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-08-21 10:31:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-09 15:20:55 +01:00
										 |  |  | type_init(wdt_ib700_register_types) |