dv-bfin_wdog.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Blackfin Watchdog (WDOG) model.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by Analog Devices, Inc.
  4. This file is part of simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include "dv-sockser.h"
  19. #include "devices.h"
  20. #include "dv-bfin_wdog.h"
  21. /* XXX: Should we bother emulating the TX/RX FIFOs ? */
  22. struct bfin_wdog
  23. {
  24. bu32 base;
  25. /* Order after here is important -- matches hardware MMR layout. */
  26. bu16 BFIN_MMR_16(ctl);
  27. bu32 cnt, stat;
  28. };
  29. #define mmr_base() offsetof(struct bfin_wdog, ctl)
  30. #define mmr_offset(mmr) (offsetof(struct bfin_wdog, mmr) - mmr_base())
  31. static const char * const mmr_names[] =
  32. {
  33. "WDOG_CTL", "WDOG_CNT", "WDOG_STAT",
  34. };
  35. #define mmr_name(off) mmr_names[(off) / 4]
  36. static bool
  37. bfin_wdog_enabled (struct bfin_wdog *wdog)
  38. {
  39. return ((wdog->ctl & WDEN) != WDDIS);
  40. }
  41. static unsigned
  42. bfin_wdog_io_write_buffer (struct hw *me, const void *source,
  43. int space, address_word addr, unsigned nr_bytes)
  44. {
  45. struct bfin_wdog *wdog = hw_data (me);
  46. bu32 mmr_off;
  47. bu32 value;
  48. bu16 *value16p;
  49. bu32 *value32p;
  50. void *valuep;
  51. /* Invalid access mode is higher priority than missing register. */
  52. if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, true))
  53. return 0;
  54. if (nr_bytes == 4)
  55. value = dv_load_4 (source);
  56. else
  57. value = dv_load_2 (source);
  58. mmr_off = addr - wdog->base;
  59. valuep = (void *)((uintptr_t)wdog + mmr_base() + mmr_off);
  60. value16p = valuep;
  61. value32p = valuep;
  62. HW_TRACE_WRITE ();
  63. switch (mmr_off)
  64. {
  65. case mmr_offset(ctl):
  66. dv_w1c_2_partial (value16p, value, WDRO);
  67. /* XXX: Should enable an event here to handle timeouts. */
  68. break;
  69. case mmr_offset(cnt):
  70. /* Writes are discarded when enabeld. */
  71. if (!bfin_wdog_enabled (wdog))
  72. {
  73. *value32p = value;
  74. /* Writes to CNT preloads the STAT. */
  75. wdog->stat = wdog->cnt;
  76. }
  77. break;
  78. case mmr_offset(stat):
  79. /* When enabled, writes to STAT reload the counter. */
  80. if (bfin_wdog_enabled (wdog))
  81. wdog->stat = wdog->cnt;
  82. /* XXX: When disabled, are writes just ignored ? */
  83. break;
  84. }
  85. return nr_bytes;
  86. }
  87. static unsigned
  88. bfin_wdog_io_read_buffer (struct hw *me, void *dest,
  89. int space, address_word addr, unsigned nr_bytes)
  90. {
  91. struct bfin_wdog *wdog = hw_data (me);
  92. bu32 mmr_off;
  93. bu16 *value16p;
  94. bu32 *value32p;
  95. void *valuep;
  96. /* Invalid access mode is higher priority than missing register. */
  97. if (!dv_bfin_mmr_require_16_32 (me, addr, nr_bytes, false))
  98. return 0;
  99. mmr_off = addr - wdog->base;
  100. valuep = (void *)((uintptr_t)wdog + mmr_base() + mmr_off);
  101. value16p = valuep;
  102. value32p = valuep;
  103. HW_TRACE_READ ();
  104. switch (mmr_off)
  105. {
  106. case mmr_offset(ctl):
  107. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, false))
  108. return 0;
  109. dv_store_2 (dest, *value16p);
  110. break;
  111. case mmr_offset(cnt):
  112. case mmr_offset(stat):
  113. dv_store_4 (dest, *value32p);
  114. break;
  115. }
  116. return nr_bytes;
  117. }
  118. static const struct hw_port_descriptor bfin_wdog_ports[] =
  119. {
  120. { "reset", WDEV_RESET, 0, output_port, },
  121. { "nmi", WDEV_NMI, 0, output_port, },
  122. { "gpi", WDEV_GPI, 0, output_port, },
  123. { NULL, 0, 0, 0, },
  124. };
  125. static void
  126. bfin_wdog_port_event (struct hw *me, int my_port, struct hw *source,
  127. int source_port, int level)
  128. {
  129. struct bfin_wdog *wdog = hw_data (me);
  130. bu16 wdev;
  131. wdog->ctl |= WDRO;
  132. wdev = (wdog->ctl & WDEV);
  133. if (wdev != WDEV_NONE)
  134. hw_port_event (me, wdev, 1);
  135. }
  136. static void
  137. attach_bfin_wdog_regs (struct hw *me, struct bfin_wdog *wdog)
  138. {
  139. address_word attach_address;
  140. int attach_space;
  141. unsigned attach_size;
  142. reg_property_spec reg;
  143. if (hw_find_property (me, "reg") == NULL)
  144. hw_abort (me, "Missing \"reg\" property");
  145. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  146. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  147. hw_unit_address_to_attach_address (hw_parent (me),
  148. &reg.address,
  149. &attach_space, &attach_address, me);
  150. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  151. if (attach_size != BFIN_MMR_WDOG_SIZE)
  152. hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_WDOG_SIZE);
  153. hw_attach_address (hw_parent (me),
  154. 0, attach_space, attach_address, attach_size, me);
  155. wdog->base = attach_address;
  156. }
  157. static void
  158. bfin_wdog_finish (struct hw *me)
  159. {
  160. struct bfin_wdog *wdog;
  161. wdog = HW_ZALLOC (me, struct bfin_wdog);
  162. set_hw_data (me, wdog);
  163. set_hw_io_read_buffer (me, bfin_wdog_io_read_buffer);
  164. set_hw_io_write_buffer (me, bfin_wdog_io_write_buffer);
  165. set_hw_ports (me, bfin_wdog_ports);
  166. set_hw_port_event (me, bfin_wdog_port_event);
  167. attach_bfin_wdog_regs (me, wdog);
  168. /* Initialize the Watchdog. */
  169. wdog->ctl = WDDIS;
  170. }
  171. const struct hw_descriptor dv_bfin_wdog_descriptor[] =
  172. {
  173. {"bfin_wdog", bfin_wdog_finish,},
  174. {NULL, NULL},
  175. };