dv-bfin_pint.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Blackfin Pin Interrupt (PINT) model
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by Analog Devices, Inc. and Mike Frysinger.
  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 "devices.h"
  19. #include "dv-bfin_pint.h"
  20. struct bfin_pint
  21. {
  22. bu32 base;
  23. /* Only accessed indirectly via the associated set/clear MMRs. */
  24. bu32 mask, edge, invert;
  25. /* Order after here is important -- matches hardware MMR layout. */
  26. bu32 mask_set;
  27. bu32 mask_clear;
  28. bu32 request;
  29. bu32 assign;
  30. bu32 edge_set;
  31. bu32 edge_clear;
  32. bu32 invert_set;
  33. bu32 invert_clear;
  34. bu32 pinstate;
  35. bu32 latch;
  36. };
  37. #define mmr_base() offsetof(struct bfin_pint, mask_set)
  38. #define mmr_offset(mmr) (offsetof(struct bfin_pint, mmr) - mmr_base())
  39. static const char * const mmr_names[] =
  40. {
  41. "PINT_MASK_SET", "PINT_MASK_CLEAR", "PINT_REQUEST", "PINT_ASSIGN",
  42. "PINT_EDGE_SET", "PINT_EDGE_CLEAR", "PINT_INVERT_SET",
  43. "PINT_INVERT_CLEAR", "PINT_PINSTATE", "PINT_LATCH",
  44. };
  45. #define mmr_name(off) mmr_names[(off) / 4]
  46. static unsigned
  47. bfin_pint_io_write_buffer (struct hw *me, const void *source, int space,
  48. address_word addr, unsigned nr_bytes)
  49. {
  50. struct bfin_pint *pint = hw_data (me);
  51. bu32 mmr_off;
  52. bu32 value;
  53. bu32 *valuep;
  54. /* Invalid access mode is higher priority than missing register. */
  55. /* XXX: The hardware allows 16 or 32 bit accesses ... */
  56. if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, true))
  57. return 0;
  58. if (nr_bytes == 4)
  59. value = dv_load_4 (source);
  60. else
  61. value = dv_load_2 (source);
  62. mmr_off = addr - pint->base;
  63. valuep = (void *)((uintptr_t)pint + mmr_base() + mmr_off);
  64. HW_TRACE_WRITE ();
  65. switch (mmr_off)
  66. {
  67. case mmr_offset(request):
  68. case mmr_offset(assign):
  69. case mmr_offset(pinstate):
  70. case mmr_offset(latch):
  71. *valuep = value;
  72. break;
  73. case mmr_offset(mask_set):
  74. dv_w1c_4 (&pint->mask, value, -1);
  75. break;
  76. case mmr_offset(mask_clear):
  77. pint->mask |= value;
  78. break;
  79. case mmr_offset(edge_set):
  80. dv_w1c_4 (&pint->edge, value, -1);
  81. break;
  82. case mmr_offset(edge_clear):
  83. pint->edge |= value;
  84. break;
  85. case mmr_offset(invert_set):
  86. dv_w1c_4 (&pint->invert, value, -1);
  87. break;
  88. case mmr_offset(invert_clear):
  89. pint->invert |= value;
  90. break;
  91. default:
  92. dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
  93. return 0;
  94. }
  95. #if 0
  96. /* If updating masks, make sure we send updated port info. */
  97. switch (mmr_off)
  98. {
  99. case mmr_offset(dir):
  100. case mmr_offset(data) ... mmr_offset(toggle):
  101. bfin_pint_forward_ouput (me, pint, data);
  102. break;
  103. case mmr_offset(maska) ... mmr_offset(maska_toggle):
  104. bfin_pint_forward_int (me, pint, pint->maska, 0);
  105. break;
  106. case mmr_offset(maskb) ... mmr_offset(maskb_toggle):
  107. bfin_pint_forward_int (me, pint, pint->maskb, 1);
  108. break;
  109. }
  110. #endif
  111. return nr_bytes;
  112. }
  113. static unsigned
  114. bfin_pint_io_read_buffer (struct hw *me, void *dest, int space,
  115. address_word addr, unsigned nr_bytes)
  116. {
  117. struct bfin_pint *pint = hw_data (me);
  118. bu32 mmr_off;
  119. bu32 *valuep;
  120. /* Invalid access mode is higher priority than missing register. */
  121. /* XXX: The hardware allows 16 or 32 bit accesses ... */
  122. if (!dv_bfin_mmr_require_32 (me, addr, nr_bytes, false))
  123. return 0;
  124. mmr_off = addr - pint->base;
  125. valuep = (void *)((uintptr_t)pint + mmr_base() + mmr_off);
  126. HW_TRACE_READ ();
  127. switch (mmr_off)
  128. {
  129. case mmr_offset(request):
  130. case mmr_offset(assign):
  131. case mmr_offset(pinstate):
  132. case mmr_offset(latch):
  133. dv_store_4 (dest, *valuep);
  134. break;
  135. case mmr_offset(mask_set):
  136. case mmr_offset(mask_clear):
  137. dv_store_4 (dest, pint->mask);
  138. break;
  139. case mmr_offset(edge_set):
  140. case mmr_offset(edge_clear):
  141. dv_store_4 (dest, pint->edge);
  142. break;
  143. case mmr_offset(invert_set):
  144. case mmr_offset(invert_clear):
  145. dv_store_4 (dest, pint->invert);
  146. break;
  147. default:
  148. dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
  149. return 0;
  150. }
  151. return nr_bytes;
  152. }
  153. #define ENC(bmap, piq) (((bmap) << 8) + (piq))
  154. #define PIQ_PORTS(n) \
  155. { "piq0@"#n, ENC(n, 0), 0, input_port, }, \
  156. { "piq1@"#n, ENC(n, 1), 0, input_port, }, \
  157. { "piq2@"#n, ENC(n, 2), 0, input_port, }, \
  158. { "piq3@"#n, ENC(n, 3), 0, input_port, }, \
  159. { "piq4@"#n, ENC(n, 4), 0, input_port, }, \
  160. { "piq5@"#n, ENC(n, 5), 0, input_port, }, \
  161. { "piq6@"#n, ENC(n, 6), 0, input_port, }, \
  162. { "piq7@"#n, ENC(n, 7), 0, input_port, }, \
  163. { "piq8@"#n, ENC(n, 8), 0, input_port, }, \
  164. { "piq9@"#n, ENC(n, 9), 0, input_port, }, \
  165. { "piq10@"#n, ENC(n, 10), 0, input_port, }, \
  166. { "piq11@"#n, ENC(n, 11), 0, input_port, }, \
  167. { "piq12@"#n, ENC(n, 12), 0, input_port, }, \
  168. { "piq13@"#n, ENC(n, 13), 0, input_port, }, \
  169. { "piq14@"#n, ENC(n, 14), 0, input_port, }, \
  170. { "piq15@"#n, ENC(n, 15), 0, input_port, }, \
  171. { "piq16@"#n, ENC(n, 16), 0, input_port, }, \
  172. { "piq17@"#n, ENC(n, 17), 0, input_port, }, \
  173. { "piq18@"#n, ENC(n, 18), 0, input_port, }, \
  174. { "piq19@"#n, ENC(n, 19), 0, input_port, }, \
  175. { "piq20@"#n, ENC(n, 20), 0, input_port, }, \
  176. { "piq21@"#n, ENC(n, 21), 0, input_port, }, \
  177. { "piq22@"#n, ENC(n, 22), 0, input_port, }, \
  178. { "piq23@"#n, ENC(n, 23), 0, input_port, }, \
  179. { "piq24@"#n, ENC(n, 24), 0, input_port, }, \
  180. { "piq25@"#n, ENC(n, 25), 0, input_port, }, \
  181. { "piq26@"#n, ENC(n, 26), 0, input_port, }, \
  182. { "piq27@"#n, ENC(n, 27), 0, input_port, }, \
  183. { "piq28@"#n, ENC(n, 28), 0, input_port, }, \
  184. { "piq29@"#n, ENC(n, 29), 0, input_port, }, \
  185. { "piq30@"#n, ENC(n, 30), 0, input_port, }, \
  186. { "piq31@"#n, ENC(n, 31), 0, input_port, },
  187. static const struct hw_port_descriptor bfin_pint_ports[] =
  188. {
  189. { "stat", 0, 0, output_port, },
  190. PIQ_PORTS(0)
  191. PIQ_PORTS(1)
  192. PIQ_PORTS(2)
  193. PIQ_PORTS(3)
  194. PIQ_PORTS(4)
  195. PIQ_PORTS(5)
  196. PIQ_PORTS(6)
  197. PIQ_PORTS(7)
  198. { NULL, 0, 0, 0, },
  199. };
  200. static void
  201. bfin_pint_port_event (struct hw *me, int my_port, struct hw *source,
  202. int source_port, int level)
  203. {
  204. /* XXX: TODO. */
  205. }
  206. static void
  207. attach_bfin_pint_regs (struct hw *me, struct bfin_pint *pint)
  208. {
  209. address_word attach_address;
  210. int attach_space;
  211. unsigned attach_size;
  212. reg_property_spec reg;
  213. if (hw_find_property (me, "reg") == NULL)
  214. hw_abort (me, "Missing \"reg\" property");
  215. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  216. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  217. hw_unit_address_to_attach_address (hw_parent (me),
  218. &reg.address,
  219. &attach_space, &attach_address, me);
  220. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  221. if (attach_size != BFIN_MMR_PINT_SIZE)
  222. hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_PINT_SIZE);
  223. hw_attach_address (hw_parent (me),
  224. 0, attach_space, attach_address, attach_size, me);
  225. pint->base = attach_address;
  226. }
  227. static void
  228. bfin_pint_finish (struct hw *me)
  229. {
  230. struct bfin_pint *pint;
  231. pint = HW_ZALLOC (me, struct bfin_pint);
  232. set_hw_data (me, pint);
  233. set_hw_io_read_buffer (me, bfin_pint_io_read_buffer);
  234. set_hw_io_write_buffer (me, bfin_pint_io_write_buffer);
  235. set_hw_ports (me, bfin_pint_ports);
  236. set_hw_port_event (me, bfin_pint_port_event);
  237. /* Initialize the PINT. */
  238. switch (dv_get_bus_num (me))
  239. {
  240. case 0:
  241. pint->assign = 0x00000101;
  242. break;
  243. case 1:
  244. pint->assign = 0x01010000;
  245. break;
  246. case 2:
  247. pint->assign = 0x00000101;
  248. break;
  249. case 3:
  250. pint->assign = 0x02020303;
  251. break;
  252. default:
  253. /* XXX: Should move this default into device tree. */
  254. hw_abort (me, "no support for PINT at this address yet");
  255. }
  256. attach_bfin_pint_regs (me, pint);
  257. }
  258. const struct hw_descriptor dv_bfin_pint_descriptor[] =
  259. {
  260. {"bfin_pint", bfin_pint_finish,},
  261. {NULL, NULL},
  262. };