dv-lm32timer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* Lattice Mico32 timer model.
  2. Contributed by Jon Beniston <jon@beniston.com>
  3. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  4. This file is part of GDB.
  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 "hw-main.h"
  19. #include "sim-assert.h"
  20. struct lm32timer
  21. {
  22. unsigned base; /* Base address of this timer. */
  23. unsigned limit; /* Limit address of this timer. */
  24. unsigned int status;
  25. unsigned int control;
  26. unsigned int period;
  27. unsigned int snapshot;
  28. struct hw_event *event;
  29. };
  30. /* Timer registers. */
  31. #define LM32_TIMER_STATUS 0x0
  32. #define LM32_TIMER_CONTROL 0x4
  33. #define LM32_TIMER_PERIOD 0x8
  34. #define LM32_TIMER_SNAPSHOT 0xc
  35. /* Timer ports. */
  36. enum
  37. {
  38. INT_PORT
  39. };
  40. static const struct hw_port_descriptor lm32timer_ports[] = {
  41. {"int", INT_PORT, 0, output_port},
  42. {}
  43. };
  44. static void
  45. do_timer_event (struct hw *me, void *data)
  46. {
  47. struct lm32timer *timer = hw_data (me);
  48. /* Is timer started? */
  49. if (timer->control & 0x4)
  50. {
  51. if (timer->snapshot)
  52. {
  53. /* Decrement timer. */
  54. timer->snapshot--;
  55. }
  56. else if (timer->control & 1)
  57. {
  58. /* Restart timer. */
  59. timer->snapshot = timer->period;
  60. }
  61. }
  62. /* Generate interrupt when timer is at 0, and interrupt enable is 1. */
  63. if ((timer->snapshot == 0) && (timer->control & 1))
  64. {
  65. /* Generate interrupt. */
  66. hw_port_event (me, INT_PORT, 1);
  67. }
  68. /* If timer is started, schedule another event to decrement the timer again. */
  69. if (timer->control & 4)
  70. hw_event_queue_schedule (me, 1, do_timer_event, 0);
  71. }
  72. static unsigned
  73. lm32timer_io_write_buffer (struct hw *me,
  74. const void *source,
  75. int space, unsigned_word base, unsigned nr_bytes)
  76. {
  77. struct lm32timer *timers = hw_data (me);
  78. int timer_reg;
  79. const unsigned char *source_bytes = source;
  80. int value = 0;
  81. HW_TRACE ((me, "write to 0x%08lx length %d with 0x%x", (long) base,
  82. (int) nr_bytes, value));
  83. if (nr_bytes == 4)
  84. value = (source_bytes[0] << 24)
  85. | (source_bytes[1] << 16) | (source_bytes[2] << 8) | (source_bytes[3]);
  86. else
  87. hw_abort (me, "write with invalid number of bytes: %d", nr_bytes);
  88. timer_reg = base - timers->base;
  89. switch (timer_reg)
  90. {
  91. case LM32_TIMER_STATUS:
  92. timers->status = value;
  93. break;
  94. case LM32_TIMER_CONTROL:
  95. timers->control = value;
  96. if (timers->control & 0x4)
  97. {
  98. /* Timer is started. */
  99. hw_event_queue_schedule (me, 1, do_timer_event, 0);
  100. }
  101. break;
  102. case LM32_TIMER_PERIOD:
  103. timers->period = value;
  104. break;
  105. default:
  106. hw_abort (me, "invalid register address: 0x%x.", timer_reg);
  107. }
  108. return nr_bytes;
  109. }
  110. static unsigned
  111. lm32timer_io_read_buffer (struct hw *me,
  112. void *dest,
  113. int space, unsigned_word base, unsigned nr_bytes)
  114. {
  115. struct lm32timer *timers = hw_data (me);
  116. int timer_reg;
  117. int value;
  118. unsigned char *dest_bytes = dest;
  119. HW_TRACE ((me, "read 0x%08lx length %d", (long) base, (int) nr_bytes));
  120. timer_reg = base - timers->base;
  121. switch (timer_reg)
  122. {
  123. case LM32_TIMER_STATUS:
  124. value = timers->status;
  125. break;
  126. case LM32_TIMER_CONTROL:
  127. value = timers->control;
  128. break;
  129. case LM32_TIMER_PERIOD:
  130. value = timers->period;
  131. break;
  132. case LM32_TIMER_SNAPSHOT:
  133. value = timers->snapshot;
  134. break;
  135. default:
  136. hw_abort (me, "invalid register address: 0x%x.", timer_reg);
  137. }
  138. if (nr_bytes == 4)
  139. {
  140. dest_bytes[0] = value >> 24;
  141. dest_bytes[1] = value >> 16;
  142. dest_bytes[2] = value >> 8;
  143. dest_bytes[3] = value;
  144. }
  145. else
  146. hw_abort (me, "read of unsupported number of bytes: %d", nr_bytes);
  147. return nr_bytes;
  148. }
  149. static void
  150. attach_lm32timer_regs (struct hw *me, struct lm32timer *timers)
  151. {
  152. unsigned_word attach_address;
  153. int attach_space;
  154. unsigned attach_size;
  155. reg_property_spec reg;
  156. if (hw_find_property (me, "reg") == NULL)
  157. hw_abort (me, "Missing \"reg\" property");
  158. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  159. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  160. hw_unit_address_to_attach_address (hw_parent (me),
  161. &reg.address,
  162. &attach_space, &attach_address, me);
  163. timers->base = attach_address;
  164. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  165. timers->limit = attach_address + (attach_size - 1);
  166. hw_attach_address (hw_parent (me),
  167. 0, attach_space, attach_address, attach_size, me);
  168. }
  169. static void
  170. lm32timer_finish (struct hw *me)
  171. {
  172. struct lm32timer *timers;
  173. int i;
  174. timers = HW_ZALLOC (me, struct lm32timer);
  175. set_hw_data (me, timers);
  176. set_hw_io_read_buffer (me, lm32timer_io_read_buffer);
  177. set_hw_io_write_buffer (me, lm32timer_io_write_buffer);
  178. set_hw_ports (me, lm32timer_ports);
  179. /* Attach ourself to our parent bus. */
  180. attach_lm32timer_regs (me, timers);
  181. /* Initialize the timers. */
  182. timers->status = 0;
  183. timers->control = 0;
  184. timers->period = 0;
  185. timers->snapshot = 0;
  186. }
  187. const struct hw_descriptor dv_lm32timer_descriptor[] = {
  188. {"lm32timer", lm32timer_finish,},
  189. {NULL},
  190. };