dv-lm32timer.c 5.6 KB

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