wdt_ib700.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. * By Richard W.M. Jones (rjones@redhat.com).
  20. */
  21. #include "qemu/osdep.h"
  22. #include "qemu/module.h"
  23. #include "qemu/timer.h"
  24. #include "system/watchdog.h"
  25. #include "hw/isa/isa.h"
  26. #include "migration/vmstate.h"
  27. #include "qom/object.h"
  28. /*#define IB700_DEBUG 1*/
  29. #ifdef IB700_DEBUG
  30. #define ib700_debug(fs,...) \
  31. fprintf(stderr,"ib700: %s: "fs,__func__,##__VA_ARGS__)
  32. #else
  33. #define ib700_debug(fs,...)
  34. #endif
  35. #define TYPE_IB700 "ib700"
  36. typedef struct IB700state IB700State;
  37. DECLARE_INSTANCE_CHECKER(IB700State, IB700,
  38. TYPE_IB700)
  39. struct IB700state {
  40. ISADevice parent_obj;
  41. QEMUTimer *timer;
  42. PortioList port_list;
  43. };
  44. /* This is the timer. We use a global here because the watchdog
  45. * code ensures there is only one watchdog (it is located at a fixed,
  46. * unchangeable IO port, so there could only ever be one anyway).
  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. {
  51. IB700State *s = vp;
  52. static int time_map[] = {
  53. 30, 28, 26, 24, 22, 20, 18, 16,
  54. 14, 12, 10, 8, 6, 4, 2, 0
  55. };
  56. int64_t timeout;
  57. ib700_debug("addr = %x, data = %x\n", addr, data);
  58. timeout = (int64_t) time_map[data & 0xF] * NANOSECONDS_PER_SECOND;
  59. timer_mod(s->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + timeout);
  60. }
  61. /* A write (of any value) to this register disables the timer. */
  62. static void ib700_write_disable_reg(void *vp, uint32_t addr, uint32_t data)
  63. {
  64. IB700State *s = vp;
  65. ib700_debug("addr = %x, data = %x\n", addr, data);
  66. timer_del(s->timer);
  67. }
  68. /* This is called when the watchdog expires. */
  69. static void ib700_timer_expired(void *vp)
  70. {
  71. IB700State *s = vp;
  72. ib700_debug("watchdog expired\n");
  73. watchdog_perform_action();
  74. timer_del(s->timer);
  75. }
  76. static const VMStateDescription vmstate_ib700 = {
  77. .name = "ib700_wdt",
  78. .version_id = 0,
  79. .minimum_version_id = 0,
  80. .fields = (const VMStateField[]) {
  81. VMSTATE_TIMER_PTR(timer, IB700State),
  82. VMSTATE_END_OF_LIST()
  83. }
  84. };
  85. static const MemoryRegionPortio wdt_portio_list[] = {
  86. { 0x441, 2, 1, .write = ib700_write_disable_reg, },
  87. { 0x443, 2, 1, .write = ib700_write_enable_reg, },
  88. PORTIO_END_OF_LIST(),
  89. };
  90. static void wdt_ib700_realize(DeviceState *dev, Error **errp)
  91. {
  92. IB700State *s = IB700(dev);
  93. ib700_debug("watchdog init\n");
  94. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ib700_timer_expired, s);
  95. portio_list_init(&s->port_list, OBJECT(s), wdt_portio_list, s, "ib700");
  96. portio_list_add(&s->port_list, isa_address_space_io(&s->parent_obj), 0);
  97. }
  98. static void wdt_ib700_reset(DeviceState *dev)
  99. {
  100. IB700State *s = IB700(dev);
  101. ib700_debug("watchdog reset\n");
  102. timer_del(s->timer);
  103. }
  104. static void wdt_ib700_class_init(ObjectClass *klass, void *data)
  105. {
  106. DeviceClass *dc = DEVICE_CLASS(klass);
  107. dc->realize = wdt_ib700_realize;
  108. device_class_set_legacy_reset(dc, wdt_ib700_reset);
  109. dc->vmsd = &vmstate_ib700;
  110. set_bit(DEVICE_CATEGORY_WATCHDOG, dc->categories);
  111. dc->desc = "iBASE 700";
  112. }
  113. static const TypeInfo wdt_ib700_info = {
  114. .name = TYPE_IB700,
  115. .parent = TYPE_ISA_DEVICE,
  116. .instance_size = sizeof(IB700State),
  117. .class_init = wdt_ib700_class_init,
  118. };
  119. static void wdt_ib700_register_types(void)
  120. {
  121. type_register_static(&wdt_ib700_info);
  122. }
  123. type_init(wdt_ib700_register_types)