dv-bfin_gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /* Blackfin General Purpose Ports (GPIO) 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 "devices.h"
  18. #include "dv-bfin_gpio.h"
  19. struct bfin_gpio
  20. {
  21. bu32 base;
  22. bu16 int_state;
  23. /* Order after here is important -- matches hardware MMR layout. */
  24. bu16 BFIN_MMR_16(data);
  25. bu16 BFIN_MMR_16(clear);
  26. bu16 BFIN_MMR_16(set);
  27. bu16 BFIN_MMR_16(toggle);
  28. bu16 BFIN_MMR_16(maska);
  29. bu16 BFIN_MMR_16(maska_clear);
  30. bu16 BFIN_MMR_16(maska_set);
  31. bu16 BFIN_MMR_16(maska_toggle);
  32. bu16 BFIN_MMR_16(maskb);
  33. bu16 BFIN_MMR_16(maskb_clear);
  34. bu16 BFIN_MMR_16(maskb_set);
  35. bu16 BFIN_MMR_16(maskb_toggle);
  36. bu16 BFIN_MMR_16(dir);
  37. bu16 BFIN_MMR_16(polar);
  38. bu16 BFIN_MMR_16(edge);
  39. bu16 BFIN_MMR_16(both);
  40. bu16 BFIN_MMR_16(inen);
  41. };
  42. #define mmr_base() offsetof(struct bfin_gpio, data)
  43. #define mmr_offset(mmr) (offsetof(struct bfin_gpio, mmr) - mmr_base())
  44. static const char * const mmr_names[] =
  45. {
  46. "PORTIO", "PORTIO_CLEAR", "PORTIO_SET", "PORTIO_TOGGLE", "PORTIO_MASKA",
  47. "PORTIO_MASKA_CLEAR", "PORTIO_MASKA_SET", "PORTIO_MASKA_TOGGLE",
  48. "PORTIO_MASKB", "PORTIO_MASKB_CLEAR", "PORTIO_MASKB_SET",
  49. "PORTIO_MASKB_TOGGLE", "PORTIO_DIR", "PORTIO_POLAR", "PORTIO_EDGE",
  50. "PORTIO_BOTH", "PORTIO_INEN",
  51. };
  52. #define mmr_name(off) mmr_names[(off) / 4]
  53. static void
  54. bfin_gpio_forward_int (struct hw *me, struct bfin_gpio *port, bu32 mask,
  55. int dst_port)
  56. {
  57. HW_TRACE ((me, "resending levels on port %c", 'a' + dst_port));
  58. hw_port_event (me, dst_port, !!(port->int_state & mask));
  59. }
  60. static void
  61. bfin_gpio_forward_ints (struct hw *me, struct bfin_gpio *port)
  62. {
  63. bfin_gpio_forward_int (me, port, port->maska, 0);
  64. bfin_gpio_forward_int (me, port, port->maskb, 1);
  65. }
  66. static void
  67. bfin_gpio_forward_ouput (struct hw *me, struct bfin_gpio *port, bu32 odata)
  68. {
  69. int pin, value, ovalue, bit;
  70. for (pin = 0; pin < 16; ++pin)
  71. {
  72. bit = 1 << pin;
  73. /* Make sure this is an output pin. */
  74. if (!(port->dir & bit))
  75. continue;
  76. /* Only signal port if the pin changes value. */
  77. value = !!(port->data & bit);
  78. ovalue = !!(odata & bit);
  79. if (value == ovalue)
  80. continue;
  81. HW_TRACE ((me, "outputting gpio %i changed to %i", pin, value));
  82. hw_port_event (me, pin, value);
  83. }
  84. }
  85. static unsigned
  86. bfin_gpio_io_write_buffer (struct hw *me, const void *source, int space,
  87. address_word addr, unsigned nr_bytes)
  88. {
  89. struct bfin_gpio *port = hw_data (me);
  90. bu32 mmr_off;
  91. bu16 value;
  92. bu16 *valuep;
  93. bu32 data = port->data;
  94. value = dv_load_2 (source);
  95. mmr_off = addr - port->base;
  96. valuep = (void *)((unsigned long)port + mmr_base() + mmr_off);
  97. HW_TRACE_WRITE ();
  98. dv_bfin_mmr_require_16 (me, addr, nr_bytes, true);
  99. switch (mmr_off)
  100. {
  101. case mmr_offset(data):
  102. case mmr_offset(maska):
  103. case mmr_offset(maskb):
  104. case mmr_offset(dir):
  105. case mmr_offset(polar):
  106. case mmr_offset(edge):
  107. case mmr_offset(both):
  108. case mmr_offset(inen):
  109. *valuep = value;
  110. break;
  111. case mmr_offset(clear):
  112. case mmr_offset(maska_clear):
  113. case mmr_offset(maskb_clear):
  114. /* We want to clear the related data MMR. */
  115. valuep -= 2;
  116. dv_w1c_2 (valuep, value, -1);
  117. break;
  118. case mmr_offset(set):
  119. case mmr_offset(maska_set):
  120. case mmr_offset(maskb_set):
  121. /* We want to set the related data MMR. */
  122. valuep -= 4;
  123. *valuep |= value;
  124. break;
  125. case mmr_offset(toggle):
  126. case mmr_offset(maska_toggle):
  127. case mmr_offset(maskb_toggle):
  128. /* We want to toggle the related data MMR. */
  129. valuep -= 6;
  130. *valuep ^= value;
  131. break;
  132. default:
  133. dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
  134. break;
  135. }
  136. /* If updating masks, make sure we send updated port info. */
  137. switch (mmr_off)
  138. {
  139. case mmr_offset(dir):
  140. case mmr_offset(data) ... mmr_offset(toggle):
  141. bfin_gpio_forward_ouput (me, port, data);
  142. break;
  143. case mmr_offset(maska) ... mmr_offset(maska_toggle):
  144. bfin_gpio_forward_int (me, port, port->maska, 0);
  145. break;
  146. case mmr_offset(maskb) ... mmr_offset(maskb_toggle):
  147. bfin_gpio_forward_int (me, port, port->maskb, 1);
  148. break;
  149. }
  150. return nr_bytes;
  151. }
  152. static unsigned
  153. bfin_gpio_io_read_buffer (struct hw *me, void *dest, int space,
  154. address_word addr, unsigned nr_bytes)
  155. {
  156. struct bfin_gpio *port = hw_data (me);
  157. bu32 mmr_off;
  158. bu16 *valuep;
  159. mmr_off = addr - port->base;
  160. valuep = (void *)((unsigned long)port + mmr_base() + mmr_off);
  161. HW_TRACE_READ ();
  162. dv_bfin_mmr_require_16 (me, addr, nr_bytes, false);
  163. switch (mmr_off)
  164. {
  165. case mmr_offset(data):
  166. case mmr_offset(clear):
  167. case mmr_offset(set):
  168. case mmr_offset(toggle):
  169. dv_store_2 (dest, port->data);
  170. break;
  171. case mmr_offset(maska):
  172. case mmr_offset(maska_clear):
  173. case mmr_offset(maska_set):
  174. case mmr_offset(maska_toggle):
  175. dv_store_2 (dest, port->maska);
  176. break;
  177. case mmr_offset(maskb):
  178. case mmr_offset(maskb_clear):
  179. case mmr_offset(maskb_set):
  180. case mmr_offset(maskb_toggle):
  181. dv_store_2 (dest, port->maskb);
  182. break;
  183. case mmr_offset(dir):
  184. case mmr_offset(polar):
  185. case mmr_offset(edge):
  186. case mmr_offset(both):
  187. case mmr_offset(inen):
  188. dv_store_2 (dest, *valuep);
  189. break;
  190. default:
  191. dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
  192. break;
  193. }
  194. return nr_bytes;
  195. }
  196. static const struct hw_port_descriptor bfin_gpio_ports[] =
  197. {
  198. { "mask_a", 0, 0, output_port, },
  199. { "mask_b", 1, 0, output_port, },
  200. { "p0", 0, 0, bidirect_port, },
  201. { "p1", 1, 0, bidirect_port, },
  202. { "p2", 2, 0, bidirect_port, },
  203. { "p3", 3, 0, bidirect_port, },
  204. { "p4", 4, 0, bidirect_port, },
  205. { "p5", 5, 0, bidirect_port, },
  206. { "p6", 6, 0, bidirect_port, },
  207. { "p7", 7, 0, bidirect_port, },
  208. { "p8", 8, 0, bidirect_port, },
  209. { "p9", 9, 0, bidirect_port, },
  210. { "p10", 10, 0, bidirect_port, },
  211. { "p11", 11, 0, bidirect_port, },
  212. { "p12", 12, 0, bidirect_port, },
  213. { "p13", 13, 0, bidirect_port, },
  214. { "p14", 14, 0, bidirect_port, },
  215. { "p15", 15, 0, bidirect_port, },
  216. { NULL, 0, 0, 0, },
  217. };
  218. static void
  219. bfin_gpio_port_event (struct hw *me, int my_port, struct hw *source,
  220. int source_port, int level)
  221. {
  222. struct bfin_gpio *port = hw_data (me);
  223. bool olvl, nlvl;
  224. bu32 bit = (1 << my_port);
  225. /* Normalize the level value. A simulated device can send any value
  226. it likes to us, but in reality we only care about 0 and 1. This
  227. lets us assume only those two values below. */
  228. level = !!level;
  229. HW_TRACE ((me, "pin %i set to %i", my_port, level));
  230. /* Only screw with state if this pin is set as an input, and the
  231. input is actually enabled. */
  232. if ((port->dir & bit) || !(port->inen & bit))
  233. {
  234. HW_TRACE ((me, "ignoring level/int due to DIR=%i INEN=%i",
  235. !!(port->dir & bit), !!(port->inen & bit)));
  236. return;
  237. }
  238. /* Get the old pin state for calculating an interrupt. */
  239. olvl = !!(port->data & bit);
  240. /* Update the new pin state. */
  241. port->data = (port->data & ~bit) | (level << my_port);
  242. /* See if this state transition will generate an interrupt. */
  243. nlvl = !!(port->data & bit);
  244. if (port->edge & bit)
  245. {
  246. /* Pin is edge triggered. */
  247. if (port->both & bit)
  248. {
  249. /* Both edges. */
  250. if (olvl == nlvl)
  251. {
  252. HW_TRACE ((me, "ignoring int due to EDGE=%i BOTH=%i lvl=%i->%i",
  253. !!(port->edge & bit), !!(port->both & bit),
  254. olvl, nlvl));
  255. return;
  256. }
  257. }
  258. else
  259. {
  260. /* Just one edge. */
  261. if (!(((port->polar & bit) && olvl > nlvl)
  262. || (!(port->polar & bit) && olvl < nlvl)))
  263. {
  264. HW_TRACE ((me, "ignoring int due to EDGE=%i POLAR=%i lvl=%i->%i",
  265. !!(port->edge & bit), !!(port->polar & bit),
  266. olvl, nlvl));
  267. return;
  268. }
  269. }
  270. /* Send the signal up, and then fall through to clear it. */
  271. port->int_state |= bit;
  272. bfin_gpio_forward_ints (me, port);
  273. port->int_state &= ~bit;
  274. }
  275. else
  276. {
  277. /* Pin is level triggered. */
  278. if (nlvl == !!(port->polar & bit))
  279. {
  280. HW_TRACE ((me, "ignoring int due to EDGE=%i POLAR=%i lvl=%i",
  281. !!(port->edge & bit), !!(port->polar & bit), nlvl));
  282. /* We still need to signal SIC to clear the int, so don't return. */
  283. port->int_state &= ~bit;
  284. }
  285. else
  286. port->int_state |= bit;
  287. }
  288. bfin_gpio_forward_ints (me, port);
  289. }
  290. static void
  291. attach_bfin_gpio_regs (struct hw *me, struct bfin_gpio *port)
  292. {
  293. address_word attach_address;
  294. int attach_space;
  295. unsigned attach_size;
  296. reg_property_spec reg;
  297. if (hw_find_property (me, "reg") == NULL)
  298. hw_abort (me, "Missing \"reg\" property");
  299. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  300. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  301. hw_unit_address_to_attach_address (hw_parent (me),
  302. &reg.address,
  303. &attach_space, &attach_address, me);
  304. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  305. if (attach_size != BFIN_MMR_GPIO_SIZE)
  306. hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_GPIO_SIZE);
  307. hw_attach_address (hw_parent (me),
  308. 0, attach_space, attach_address, attach_size, me);
  309. port->base = attach_address;
  310. }
  311. static void
  312. bfin_gpio_finish (struct hw *me)
  313. {
  314. struct bfin_gpio *port;
  315. port = HW_ZALLOC (me, struct bfin_gpio);
  316. set_hw_data (me, port);
  317. set_hw_io_read_buffer (me, bfin_gpio_io_read_buffer);
  318. set_hw_io_write_buffer (me, bfin_gpio_io_write_buffer);
  319. set_hw_ports (me, bfin_gpio_ports);
  320. set_hw_port_event (me, bfin_gpio_port_event);
  321. attach_bfin_gpio_regs (me, port);
  322. }
  323. const struct hw_descriptor dv_bfin_gpio_descriptor[] =
  324. {
  325. {"bfin_gpio", bfin_gpio_finish,},
  326. {NULL, NULL},
  327. };