| 
									
										
										
										
											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). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "qemu-common.h"
 | 
					
						
							|  |  |  | #include "qemu-timer.h"
 | 
					
						
							|  |  |  | #include "watchdog.h"
 | 
					
						
							|  |  |  | #include "hw.h"
 | 
					
						
							|  |  |  | #include "isa.h"
 | 
					
						
							|  |  |  | #include "pc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*#define IB700_DEBUG 1*/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef IB700_DEBUG
 | 
					
						
							|  |  |  | #define ib700_debug(fs,...)					\
 | 
					
						
							|  |  |  |     fprintf(stderr,"ib700: %s: "fs,__func__,##__VA_ARGS__) | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define ib700_debug(fs,...)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 00:49:17 +02:00
										 |  |  | typedef struct IB700state { | 
					
						
							|  |  |  |     ISADevice dev; | 
					
						
							| 
									
										
										
										
											2009-10-15 00:54:28 +02:00
										 |  |  |     QEMUTimer *timer; | 
					
						
							| 
									
										
										
										
											2009-10-15 00:49:17 +02:00
										 |  |  | } IB700State; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							|  |  |  |  * unchangable IO port, so there could only ever be one anyway). | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* 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 | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     int64 timeout; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ib700_debug("addr = %x, data = %x\n", addr, data); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-09-10 03:04:26 +02:00
										 |  |  |     timeout = (int64_t) time_map[data & 0xF] * get_ticks_per_sec(); | 
					
						
							| 
									
										
										
										
											2009-10-15 00:54:28 +02:00
										 |  |  |     qemu_mod_timer(s->timer, qemu_get_clock (vm_clock) + 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 00:54:28 +02:00
										 |  |  |     qemu_del_timer(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(); | 
					
						
							| 
									
										
										
										
											2009-10-15 00:54:28 +02:00
										 |  |  |     qemu_del_timer(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, | 
					
						
							|  |  |  |     .minimum_version_id_old = 0, | 
					
						
							|  |  |  |     .fields      = (VMStateField []) { | 
					
						
							|  |  |  |         VMSTATE_TIMER(timer, IB700State), | 
					
						
							|  |  |  |         VMSTATE_END_OF_LIST() | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-14 10:36:05 +02:00
										 |  |  | static int wdt_ib700_init(ISADevice *dev) | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-15 00:49:17 +02:00
										 |  |  |     IB700State *s = DO_UPCAST(IB700State, dev, dev); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-15 00:54:28 +02:00
										 |  |  |     s->timer = qemu_new_timer(vm_clock, ib700_timer_expired, s); | 
					
						
							| 
									
										
										
										
											2009-10-15 00:49:17 +02:00
										 |  |  |     register_ioport_write(0x441, 2, 1, ib700_write_disable_reg, s); | 
					
						
							|  |  |  |     register_ioport_write(0x443, 2, 1, ib700_write_enable_reg, s); | 
					
						
							| 
									
										
										
										
											2009-08-14 10:36:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static WatchdogTimerModel model = { | 
					
						
							|  |  |  |     .wdt_name = "ib700", | 
					
						
							|  |  |  |     .wdt_description = "iBASE 700", | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-21 10:31:34 +02:00
										 |  |  | static ISADeviceInfo wdt_ib700_info = { | 
					
						
							|  |  |  |     .qdev.name = "ib700", | 
					
						
							| 
									
										
										
										
											2009-10-15 00:49:17 +02:00
										 |  |  |     .qdev.size = sizeof(IB700State), | 
					
						
							| 
									
										
										
										
											2009-12-02 12:36:46 +01:00
										 |  |  |     .qdev.vmsd = &vmstate_ib700, | 
					
						
							| 
									
										
										
										
											2009-08-21 10:31:34 +02:00
										 |  |  |     .init      = wdt_ib700_init, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void wdt_ib700_register_devices(void) | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     watchdog_add_model(&model); | 
					
						
							| 
									
										
										
										
											2009-08-21 10:31:34 +02:00
										 |  |  |     isa_qdev_register(&wdt_ib700_info); | 
					
						
							| 
									
										
										
										
											2009-04-25 13:56:19 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-08-21 10:31:34 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | device_init(wdt_ib700_register_devices); |