dv-bfin_wdog.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* Blackfin Watchdog (WDOG) model.
  2. Copyright (C) 2010-2015 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. #include "config.h"
  16. #include "sim-main.h"
  17. #include "dv-sockser.h"
  18. #include "devices.h"
  19. #include "dv-bfin_wdog.h"
  20. /* XXX: Should we bother emulating the TX/RX FIFOs ? */
  21. struct bfin_wdog
  22. {
  23. bu32 base;
  24. /* Order after here is important -- matches hardware MMR layout. */
  25. bu16 BFIN_MMR_16(ctl);
  26. bu32 cnt, stat;
  27. };
  28. #define mmr_base() offsetof(struct bfin_wdog, ctl)
  29. #define mmr_offset(mmr) (offsetof(struct bfin_wdog, mmr) - mmr_base())
  30. static const char * const mmr_names[] =
  31. {
  32. "WDOG_CTL", "WDOG_CNT", "WDOG_STAT",
  33. };
  34. #define mmr_name(off) mmr_names[(off) / 4]
  35. static bool
  36. bfin_wdog_enabled (struct bfin_wdog *wdog)
  37. {
  38. return ((wdog->ctl & WDEN) != WDDIS);
  39. }
  40. static unsigned
  41. bfin_wdog_io_write_buffer (struct hw *me, const void *source,
  42. int space, address_word addr, unsigned nr_bytes)
  43. {
  44. struct bfin_wdog *wdog = hw_data (me);
  45. bu32 mmr_off;
  46. bu32 value;
  47. bu16 *value16p;
  48. bu32 *value32p;
  49. void *valuep;
  50. if (nr_bytes == 4)
  51. value = dv_load_4 (source);
  52. else
  53. value = dv_load_2 (source);
  54. mmr_off = addr - wdog->base;
  55. valuep = (void *)((unsigned long)wdog + mmr_base() + mmr_off);
  56. value16p = valuep;
  57. value32p = valuep;
  58. HW_TRACE_WRITE ();
  59. switch (mmr_off)
  60. {
  61. case mmr_offset(ctl):
  62. dv_w1c_2_partial (value16p, value, WDRO);
  63. /* XXX: Should enable an event here to handle timeouts. */
  64. break;
  65. case mmr_offset(cnt):
  66. /* Writes are discarded when enabeld. */
  67. if (!bfin_wdog_enabled (wdog))
  68. {
  69. *value32p = value;
  70. /* Writes to CNT preloads the STAT. */
  71. wdog->stat = wdog->cnt;
  72. }
  73. break;
  74. case mmr_offset(stat):
  75. /* When enabled, writes to STAT reload the counter. */
  76. if (bfin_wdog_enabled (wdog))
  77. wdog->stat = wdog->cnt;
  78. /* XXX: When disabled, are writes just ignored ? */
  79. break;
  80. }
  81. return nr_bytes;
  82. }
  83. static unsigned
  84. bfin_wdog_io_read_buffer (struct hw *me, void *dest,
  85. int space, address_word addr, unsigned nr_bytes)
  86. {
  87. struct bfin_wdog *wdog = hw_data (me);
  88. bu32 mmr_off;
  89. bu16 *value16p;
  90. bu32 *value32p;
  91. void *valuep;
  92. mmr_off = addr - wdog->base;
  93. valuep = (void *)((unsigned long)wdog + mmr_base() + mmr_off);
  94. value16p = valuep;
  95. value32p = valuep;
  96. HW_TRACE_READ ();
  97. switch (mmr_off)
  98. {
  99. case mmr_offset(ctl):
  100. dv_bfin_mmr_require_16 (me, addr, nr_bytes, false);
  101. dv_store_2 (dest, *value16p);
  102. break;
  103. case mmr_offset(cnt):
  104. case mmr_offset(stat):
  105. dv_store_4 (dest, *value32p);
  106. break;
  107. }
  108. return nr_bytes;
  109. }
  110. static const struct hw_port_descriptor bfin_wdog_ports[] =
  111. {
  112. { "reset", WDEV_RESET, 0, output_port, },
  113. { "nmi", WDEV_NMI, 0, output_port, },
  114. { "gpi", WDEV_GPI, 0, output_port, },
  115. { NULL, 0, 0, 0, },
  116. };
  117. static void
  118. bfin_wdog_port_event (struct hw *me, int my_port, struct hw *source,
  119. int source_port, int level)
  120. {
  121. struct bfin_wdog *wdog = hw_data (me);
  122. bu16 wdev;
  123. wdog->ctl |= WDRO;
  124. wdev = (wdog->ctl & WDEV);
  125. if (wdev != WDEV_NONE)
  126. hw_port_event (me, wdev, 1);
  127. }
  128. static void
  129. attach_bfin_wdog_regs (struct hw *me, struct bfin_wdog *wdog)
  130. {
  131. address_word attach_address;
  132. int attach_space;
  133. unsigned attach_size;
  134. reg_property_spec reg;
  135. if (hw_find_property (me, "reg") == NULL)
  136. hw_abort (me, "Missing \"reg\" property");
  137. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  138. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  139. hw_unit_address_to_attach_address (hw_parent (me),
  140. &reg.address,
  141. &attach_space, &attach_address, me);
  142. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  143. if (attach_size != BFIN_MMR_WDOG_SIZE)
  144. hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_WDOG_SIZE);
  145. hw_attach_address (hw_parent (me),
  146. 0, attach_space, attach_address, attach_size, me);
  147. wdog->base = attach_address;
  148. }
  149. static void
  150. bfin_wdog_finish (struct hw *me)
  151. {
  152. struct bfin_wdog *wdog;
  153. wdog = HW_ZALLOC (me, struct bfin_wdog);
  154. set_hw_data (me, wdog);
  155. set_hw_io_read_buffer (me, bfin_wdog_io_read_buffer);
  156. set_hw_io_write_buffer (me, bfin_wdog_io_write_buffer);
  157. set_hw_ports (me, bfin_wdog_ports);
  158. set_hw_port_event (me, bfin_wdog_port_event);
  159. attach_bfin_wdog_regs (me, wdog);
  160. /* Initialize the Watchdog. */
  161. wdog->ctl = WDDIS;
  162. }
  163. const struct hw_descriptor dv_bfin_wdog_descriptor[] =
  164. {
  165. {"bfin_wdog", bfin_wdog_finish,},
  166. {NULL, NULL},
  167. };