dv-bfin_twi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* Blackfin Two Wire Interface (TWI) model
  2. Copyright (C) 2010-2022 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. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include "sim-main.h"
  18. #include "devices.h"
  19. #include "dv-bfin_twi.h"
  20. /* XXX: This is merely a stub. */
  21. struct bfin_twi
  22. {
  23. /* This top portion matches common dv_bfin struct. */
  24. bu32 base;
  25. struct hw *dma_master;
  26. bool acked;
  27. struct hw_event *handler;
  28. char saved_byte;
  29. int saved_count;
  30. bu16 xmt_fifo, rcv_fifo;
  31. /* Order after here is important -- matches hardware MMR layout. */
  32. bu16 BFIN_MMR_16(clkdiv);
  33. bu16 BFIN_MMR_16(control);
  34. bu16 BFIN_MMR_16(slave_ctl);
  35. bu16 BFIN_MMR_16(slave_stat);
  36. bu16 BFIN_MMR_16(slave_addr);
  37. bu16 BFIN_MMR_16(master_ctl);
  38. bu16 BFIN_MMR_16(master_stat);
  39. bu16 BFIN_MMR_16(master_addr);
  40. bu16 BFIN_MMR_16(int_stat);
  41. bu16 BFIN_MMR_16(int_mask);
  42. bu16 BFIN_MMR_16(fifo_ctl);
  43. bu16 BFIN_MMR_16(fifo_stat);
  44. bu32 _pad0[20];
  45. bu16 BFIN_MMR_16(xmt_data8);
  46. bu16 BFIN_MMR_16(xmt_data16);
  47. bu16 BFIN_MMR_16(rcv_data8);
  48. bu16 BFIN_MMR_16(rcv_data16);
  49. };
  50. #define mmr_base() offsetof(struct bfin_twi, clkdiv)
  51. #define mmr_offset(mmr) (offsetof(struct bfin_twi, mmr) - mmr_base())
  52. #define mmr_idx(mmr) (mmr_offset (mmr) / 4)
  53. static const char * const mmr_names[] =
  54. {
  55. "TWI_CLKDIV", "TWI_CONTROL", "TWI_SLAVE_CTL", "TWI_SLAVE_STAT",
  56. "TWI_SLAVE_ADDR", "TWI_MASTER_CTL", "TWI_MASTER_STAT", "TWI_MASTER_ADDR",
  57. "TWI_INT_STAT", "TWI_INT_MASK", "TWI_FIFO_CTL", "TWI_FIFO_STAT",
  58. [mmr_idx (xmt_data8)] = "TWI_XMT_DATA8", "TWI_XMT_DATA16", "TWI_RCV_DATA8",
  59. "TWI_RCV_DATA16",
  60. };
  61. #define mmr_name(off) (mmr_names[(off) / 4] ? : "<INV>")
  62. static unsigned
  63. bfin_twi_io_write_buffer (struct hw *me, const void *source, int space,
  64. address_word addr, unsigned nr_bytes)
  65. {
  66. struct bfin_twi *twi = hw_data (me);
  67. bu32 mmr_off;
  68. bu32 value;
  69. bu16 *valuep;
  70. /* Invalid access mode is higher priority than missing register. */
  71. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
  72. return 0;
  73. value = dv_load_2 (source);
  74. mmr_off = addr - twi->base;
  75. valuep = (void *)((uintptr_t)twi + mmr_base() + mmr_off);
  76. HW_TRACE_WRITE ();
  77. switch (mmr_off)
  78. {
  79. case mmr_offset(clkdiv):
  80. case mmr_offset(control):
  81. case mmr_offset(slave_ctl):
  82. case mmr_offset(slave_addr):
  83. case mmr_offset(master_ctl):
  84. case mmr_offset(master_addr):
  85. case mmr_offset(int_mask):
  86. case mmr_offset(fifo_ctl):
  87. *valuep = value;
  88. break;
  89. case mmr_offset(int_stat):
  90. dv_w1c_2 (valuep, value, -1);
  91. break;
  92. case mmr_offset(master_stat):
  93. dv_w1c_2 (valuep, value, BUFWRERR | BUFRDERR | DNAK | ANAK | LOSTARB);
  94. break;
  95. case mmr_offset(slave_stat):
  96. case mmr_offset(fifo_stat):
  97. case mmr_offset(rcv_data8):
  98. case mmr_offset(rcv_data16):
  99. /* These are all RO. XXX: Does these throw error ? */
  100. break;
  101. case mmr_offset(xmt_data8):
  102. value &= 0xff;
  103. case mmr_offset(xmt_data16):
  104. twi->xmt_fifo = value;
  105. break;
  106. default:
  107. dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
  108. return 0;
  109. }
  110. return nr_bytes;
  111. }
  112. static unsigned
  113. bfin_twi_io_read_buffer (struct hw *me, void *dest, int space,
  114. address_word addr, unsigned nr_bytes)
  115. {
  116. struct bfin_twi *twi = hw_data (me);
  117. bu32 mmr_off;
  118. bu16 *valuep;
  119. /* Invalid access mode is higher priority than missing register. */
  120. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, false))
  121. return 0;
  122. mmr_off = addr - twi->base;
  123. valuep = (void *)((uintptr_t)twi + mmr_base() + mmr_off);
  124. HW_TRACE_READ ();
  125. switch (mmr_off)
  126. {
  127. case mmr_offset(clkdiv):
  128. case mmr_offset(control):
  129. case mmr_offset(slave_ctl):
  130. case mmr_offset(slave_stat):
  131. case mmr_offset(slave_addr):
  132. case mmr_offset(master_ctl):
  133. case mmr_offset(master_stat):
  134. case mmr_offset(master_addr):
  135. case mmr_offset(int_stat):
  136. case mmr_offset(int_mask):
  137. case mmr_offset(fifo_ctl):
  138. case mmr_offset(fifo_stat):
  139. dv_store_2 (dest, *valuep);
  140. break;
  141. case mmr_offset(rcv_data8):
  142. case mmr_offset(rcv_data16):
  143. dv_store_2 (dest, twi->rcv_fifo);
  144. break;
  145. case mmr_offset(xmt_data8):
  146. case mmr_offset(xmt_data16):
  147. /* These always read as 0. */
  148. dv_store_2 (dest, 0);
  149. break;
  150. default:
  151. dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
  152. return 0;
  153. }
  154. return nr_bytes;
  155. }
  156. static const struct hw_port_descriptor bfin_twi_ports[] =
  157. {
  158. { "stat", 0, 0, output_port, },
  159. { NULL, 0, 0, 0, },
  160. };
  161. static void
  162. attach_bfin_twi_regs (struct hw *me, struct bfin_twi *twi)
  163. {
  164. address_word attach_address;
  165. int attach_space;
  166. unsigned attach_size;
  167. reg_property_spec reg;
  168. if (hw_find_property (me, "reg") == NULL)
  169. hw_abort (me, "Missing \"reg\" property");
  170. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  171. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  172. hw_unit_address_to_attach_address (hw_parent (me),
  173. &reg.address,
  174. &attach_space, &attach_address, me);
  175. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  176. if (attach_size != BFIN_MMR_TWI_SIZE)
  177. hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_TWI_SIZE);
  178. hw_attach_address (hw_parent (me),
  179. 0, attach_space, attach_address, attach_size, me);
  180. twi->base = attach_address;
  181. }
  182. static void
  183. bfin_twi_finish (struct hw *me)
  184. {
  185. struct bfin_twi *twi;
  186. twi = HW_ZALLOC (me, struct bfin_twi);
  187. set_hw_data (me, twi);
  188. set_hw_io_read_buffer (me, bfin_twi_io_read_buffer);
  189. set_hw_io_write_buffer (me, bfin_twi_io_write_buffer);
  190. set_hw_ports (me, bfin_twi_ports);
  191. attach_bfin_twi_regs (me, twi);
  192. }
  193. const struct hw_descriptor dv_bfin_twi_descriptor[] =
  194. {
  195. {"bfin_twi", bfin_twi_finish,},
  196. {NULL, NULL},
  197. };