dv-tx3904irc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /* This file is part of the program GDB, the GNU debugger.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Cygnus Solutions.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include "hw-main.h"
  19. /* DEVICE
  20. tx3904irc - tx3904 interrupt controller
  21. DESCRIPTION
  22. Implements the tx3904 interrupt controller described in the tx3904
  23. user guide. It does not include the interrupt detection circuit
  24. that preprocesses the eight external interrupts, so assumes that
  25. each event on an input interrupt port signals a new interrupt.
  26. That is, it implements edge- rather than level-triggered
  27. interrupts.
  28. This implementation does not support multiple concurrent
  29. interrupts.
  30. PROPERTIES
  31. reg <base> <length>
  32. Base of IRC control register bank. <length> must equal 0x20.
  33. Registers offsets: 0: ISR: interrupt status register
  34. 4: IMR: interrupt mask register
  35. 16: ILR0: interrupt level register 3..0
  36. 20: ILR1: interrupt level register 7..4
  37. 24: ILR2: interrupt level register 11..8
  38. 28: ILR3: interrupt level register 15..12
  39. PORTS
  40. ip (output)
  41. Interrupt priority port. An event is generated when an interrupt
  42. of a sufficient priority is passed through the IRC. The value
  43. associated with the event is the interrupt level (16-31), as given
  44. for bits IP[5:0] in the book TMPR3904F Rev. 2.0, pg. 11-3. Note
  45. that even though INT[0] is tied externally to IP[5], we simulate
  46. it as passing through the controller.
  47. An output level of zero signals the clearing of a level interrupt.
  48. int0-7 (input)
  49. External interrupts. Level = 0 -> level interrupt cleared.
  50. dmac0-3 (input)
  51. DMA internal interrupts, correspond to DMA channels 0-3. Level = 0 -> level interrupt cleared.
  52. sio0-1 (input)
  53. SIO internal interrupts. Level = 0 -> level interrupt cleared.
  54. tmr0-2 (input)
  55. Timer internal interrupts. Level = 0 -> level interrupt cleared.
  56. */
  57. /* register numbers; each is one word long */
  58. enum
  59. {
  60. ISR_REG = 0,
  61. IMR_REG = 1,
  62. ILR0_REG = 4,
  63. ILR1_REG = 5,
  64. ILR2_REG = 6,
  65. ILR3_REG = 7,
  66. };
  67. /* port ID's */
  68. enum
  69. {
  70. /* inputs, ordered to correspond to interrupt sources 0..15 */
  71. INT1_PORT = 0, INT2_PORT, INT3_PORT, INT4_PORT, INT5_PORT, INT6_PORT, INT7_PORT,
  72. DMAC3_PORT, DMAC2_PORT, DMAC1_PORT, DMAC0_PORT, SIO0_PORT, SIO1_PORT,
  73. TMR0_PORT, TMR1_PORT, TMR2_PORT,
  74. /* special INT[0] port */
  75. INT0_PORT,
  76. /* reset */
  77. RESET_PORT,
  78. /* output */
  79. IP_PORT
  80. };
  81. static const struct hw_port_descriptor tx3904irc_ports[] = {
  82. /* interrupt output */
  83. { "ip", IP_PORT, 0, output_port, },
  84. /* interrupt inputs (as names) */
  85. /* in increasing order of level number */
  86. { "int1", INT1_PORT, 0, input_port, },
  87. { "int2", INT2_PORT, 0, input_port, },
  88. { "int3", INT3_PORT, 0, input_port, },
  89. { "int4", INT4_PORT, 0, input_port, },
  90. { "int5", INT5_PORT, 0, input_port, },
  91. { "int6", INT6_PORT, 0, input_port, },
  92. { "int7", INT7_PORT, 0, input_port, },
  93. { "dmac3", DMAC3_PORT, 0, input_port, },
  94. { "dmac2", DMAC2_PORT, 0, input_port, },
  95. { "dmac1", DMAC1_PORT, 0, input_port, },
  96. { "dmac0", DMAC0_PORT, 0, input_port, },
  97. { "sio0", SIO0_PORT, 0, input_port, },
  98. { "sio1", SIO1_PORT, 0, input_port, },
  99. { "tmr0", TMR0_PORT, 0, input_port, },
  100. { "tmr1", TMR1_PORT, 0, input_port, },
  101. { "tmr2", TMR2_PORT, 0, input_port, },
  102. { "reset", RESET_PORT, 0, input_port, },
  103. { "int0", INT0_PORT, 0, input_port, },
  104. { NULL, },
  105. };
  106. #define NR_SOURCES (TMR3_PORT - INT1_PORT + 1) /* 16: number of interrupt sources */
  107. /* The interrupt controller register internal state. Note that we
  108. store state using the control register images, in host endian
  109. order. */
  110. struct tx3904irc {
  111. address_word base_address; /* control register base */
  112. unsigned_4 isr;
  113. #define ISR_SET(c, s) ((c)->isr &= ~(1 << (s)))
  114. unsigned_4 imr;
  115. #define IMR_GET(c) ((c)->imr)
  116. unsigned_4 ilr[4];
  117. #define ILR_GET(c, s) LSEXTRACTED32((c)->ilr[(s) / 4], (s) % 4 * 8 + 2, (s) % 4 * 8)
  118. };
  119. /* Finish off the partially created hw device. Attach our local
  120. callbacks. Wire up our port names etc */
  121. static hw_io_read_buffer_method tx3904irc_io_read_buffer;
  122. static hw_io_write_buffer_method tx3904irc_io_write_buffer;
  123. static hw_port_event_method tx3904irc_port_event;
  124. static void
  125. attach_tx3904irc_regs (struct hw *me,
  126. struct tx3904irc *controller)
  127. {
  128. unsigned_word attach_address;
  129. int attach_space;
  130. unsigned attach_size;
  131. reg_property_spec reg;
  132. if (hw_find_property (me, "reg") == NULL)
  133. hw_abort (me, "Missing \"reg\" property");
  134. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  135. hw_abort (me, "\"reg\" property must contain one addr/size entry");
  136. hw_unit_address_to_attach_address (hw_parent (me),
  137. &reg.address,
  138. &attach_space,
  139. &attach_address,
  140. me);
  141. hw_unit_size_to_attach_size (hw_parent (me),
  142. &reg.size,
  143. &attach_size, me);
  144. hw_attach_address (hw_parent (me), 0,
  145. attach_space, attach_address, attach_size,
  146. me);
  147. controller->base_address = attach_address;
  148. }
  149. static void
  150. tx3904irc_finish (struct hw *me)
  151. {
  152. struct tx3904irc *controller;
  153. controller = HW_ZALLOC (me, struct tx3904irc);
  154. set_hw_data (me, controller);
  155. set_hw_io_read_buffer (me, tx3904irc_io_read_buffer);
  156. set_hw_io_write_buffer (me, tx3904irc_io_write_buffer);
  157. set_hw_ports (me, tx3904irc_ports);
  158. set_hw_port_event (me, tx3904irc_port_event);
  159. /* Attach ourself to our parent bus */
  160. attach_tx3904irc_regs (me, controller);
  161. /* Initialize to reset state */
  162. controller->isr = 0x0000ffff;
  163. controller->imr = 0;
  164. controller->ilr[0] =
  165. controller->ilr[1] =
  166. controller->ilr[2] =
  167. controller->ilr[3] = 0;
  168. }
  169. /* An event arrives on an interrupt port */
  170. static void
  171. tx3904irc_port_event (struct hw *me,
  172. int my_port,
  173. struct hw *source_dev,
  174. int source_port,
  175. int level)
  176. {
  177. struct tx3904irc *controller = hw_data (me);
  178. /* handle deactivated interrupt */
  179. if (level == 0)
  180. {
  181. HW_TRACE ((me, "interrupt cleared on port %d", my_port));
  182. hw_port_event(me, IP_PORT, 0);
  183. return;
  184. }
  185. switch (my_port)
  186. {
  187. case INT0_PORT:
  188. {
  189. int ip_number = 32; /* compute IP[5:0] */
  190. HW_TRACE ((me, "port-event INT[0]"));
  191. hw_port_event(me, IP_PORT, ip_number);
  192. break;
  193. }
  194. case INT1_PORT: case INT2_PORT: case INT3_PORT: case INT4_PORT:
  195. case INT5_PORT: case INT6_PORT: case INT7_PORT: case DMAC3_PORT:
  196. case DMAC2_PORT: case DMAC1_PORT: case DMAC0_PORT: case SIO0_PORT:
  197. case SIO1_PORT: case TMR0_PORT: case TMR1_PORT: case TMR2_PORT:
  198. {
  199. int source = my_port - INT1_PORT;
  200. HW_TRACE ((me, "interrupt asserted on port %d", source));
  201. ISR_SET(controller, source);
  202. if (ILR_GET(controller, source) > IMR_GET(controller))
  203. {
  204. int ip_number = 16 + source; /* compute IP[4:0] */
  205. HW_TRACE ((me, "interrupt level %d", ILR_GET(controller, source)));
  206. hw_port_event(me, IP_PORT, ip_number);
  207. }
  208. break;
  209. }
  210. case RESET_PORT:
  211. {
  212. HW_TRACE ((me, "reset"));
  213. controller->isr = 0x0000ffff;
  214. controller->imr = 0;
  215. controller->ilr[0] =
  216. controller->ilr[1] =
  217. controller->ilr[2] =
  218. controller->ilr[3] = 0;
  219. break;
  220. }
  221. case IP_PORT:
  222. hw_abort (me, "Event on output port %d", my_port);
  223. break;
  224. default:
  225. hw_abort (me, "Event on unknown port %d", my_port);
  226. break;
  227. }
  228. }
  229. /* generic read/write */
  230. static unsigned
  231. tx3904irc_io_read_buffer (struct hw *me,
  232. void *dest,
  233. int space,
  234. unsigned_word base,
  235. unsigned nr_bytes)
  236. {
  237. struct tx3904irc *controller = hw_data (me);
  238. unsigned byte;
  239. HW_TRACE ((me, "read 0x%08lx %d", (long) base, (int) nr_bytes));
  240. for (byte = 0; byte < nr_bytes; byte++)
  241. {
  242. address_word address = base + byte;
  243. int reg_number = (address - controller->base_address) / 4;
  244. int reg_offset = (address - controller->base_address) % 4;
  245. unsigned_4 register_value; /* in target byte order */
  246. /* fill in entire register_value word */
  247. switch (reg_number)
  248. {
  249. case ISR_REG: register_value = controller->isr; break;
  250. case IMR_REG: register_value = controller->imr; break;
  251. case ILR0_REG: register_value = controller->ilr[0]; break;
  252. case ILR1_REG: register_value = controller->ilr[1]; break;
  253. case ILR2_REG: register_value = controller->ilr[2]; break;
  254. case ILR3_REG: register_value = controller->ilr[3]; break;
  255. default: register_value = 0;
  256. }
  257. /* write requested byte out */
  258. register_value = H2T_4(register_value);
  259. memcpy ((char*) dest + byte, ((char*)& register_value)+reg_offset, 1);
  260. }
  261. return nr_bytes;
  262. }
  263. static unsigned
  264. tx3904irc_io_write_buffer (struct hw *me,
  265. const void *source,
  266. int space,
  267. unsigned_word base,
  268. unsigned nr_bytes)
  269. {
  270. struct tx3904irc *controller = hw_data (me);
  271. unsigned byte;
  272. HW_TRACE ((me, "write 0x%08lx %d", (long) base, (int) nr_bytes));
  273. for (byte = 0; byte < nr_bytes; byte++)
  274. {
  275. address_word address = base + byte;
  276. int reg_number = (address - controller->base_address) / 4;
  277. int reg_offset = (address - controller->base_address) % 4;
  278. unsigned_4* register_ptr;
  279. unsigned_4 register_value = 0;
  280. /* fill in entire register_value word */
  281. switch (reg_number)
  282. {
  283. case ISR_REG: register_ptr = & controller->isr; break;
  284. case IMR_REG: register_ptr = & controller->imr; break;
  285. case ILR0_REG: register_ptr = & controller->ilr[0]; break;
  286. case ILR1_REG: register_ptr = & controller->ilr[1]; break;
  287. case ILR2_REG: register_ptr = & controller->ilr[2]; break;
  288. case ILR3_REG: register_ptr = & controller->ilr[3]; break;
  289. default: register_ptr = & register_value; /* used as a dummy */
  290. }
  291. /* HW_TRACE ((me, "reg %d pre: %08lx", reg_number, (long) *register_ptr)); */
  292. /* overwrite requested byte */
  293. register_value = H2T_4(* register_ptr);
  294. memcpy (((char*)&register_value)+reg_offset, (const char*)source + byte, 1);
  295. * register_ptr = T2H_4(register_value);
  296. /* HW_TRACE ((me, "post: %08lx", (long) *register_ptr)); */
  297. }
  298. return nr_bytes;
  299. }
  300. const struct hw_descriptor dv_tx3904irc_descriptor[] = {
  301. { "tx3904irc", tx3904irc_finish, },
  302. { NULL },
  303. };