dv-bfin_uart2.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /* Blackfin Universal Asynchronous Receiver/Transmitter (UART) model.
  2. For "new style" UARTs on BF50x/BF54x parts.
  3. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  4. Contributed by Analog Devices, Inc.
  5. This file is part of simulators.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. /* This must come before any other includes. */
  17. #include "defs.h"
  18. #include "sim-main.h"
  19. #include "devices.h"
  20. #include "dv-bfin_uart2.h"
  21. /* XXX: Should we bother emulating the TX/RX FIFOs ? */
  22. /* Internal state needs to be the same as bfin_uart. */
  23. struct bfin_uart
  24. {
  25. /* This top portion matches common dv_bfin struct. */
  26. bu32 base;
  27. struct hw *dma_master;
  28. bool acked;
  29. struct hw_event *handler;
  30. char saved_byte;
  31. int saved_count;
  32. /* Accessed indirectly by ier_{set,clear}. */
  33. bu16 ier;
  34. /* Order after here is important -- matches hardware MMR layout. */
  35. bu16 BFIN_MMR_16(dll);
  36. bu16 BFIN_MMR_16(dlh);
  37. bu16 BFIN_MMR_16(gctl);
  38. bu16 BFIN_MMR_16(lcr);
  39. bu16 BFIN_MMR_16(mcr);
  40. bu16 BFIN_MMR_16(lsr);
  41. bu16 BFIN_MMR_16(msr);
  42. bu16 BFIN_MMR_16(scr);
  43. bu16 BFIN_MMR_16(ier_set);
  44. bu16 BFIN_MMR_16(ier_clear);
  45. bu16 BFIN_MMR_16(thr);
  46. bu16 BFIN_MMR_16(rbr);
  47. };
  48. #define mmr_base() offsetof(struct bfin_uart, dll)
  49. #define mmr_offset(mmr) (offsetof(struct bfin_uart, mmr) - mmr_base())
  50. static const char * const mmr_names[] =
  51. {
  52. "UART_DLL", "UART_DLH", "UART_GCTL", "UART_LCR", "UART_MCR", "UART_LSR",
  53. "UART_MSR", "UART_SCR", "UART_IER_SET", "UART_IER_CLEAR", "UART_THR",
  54. "UART_RBR",
  55. };
  56. #define mmr_name(off) mmr_names[(off) / 4]
  57. static unsigned
  58. bfin_uart_io_write_buffer (struct hw *me, const void *source,
  59. int space, address_word addr, unsigned nr_bytes)
  60. {
  61. struct bfin_uart *uart = hw_data (me);
  62. bu32 mmr_off;
  63. bu32 value;
  64. bu16 *valuep;
  65. /* Invalid access mode is higher priority than missing register. */
  66. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, true))
  67. return 0;
  68. value = dv_load_2 (source);
  69. mmr_off = addr - uart->base;
  70. valuep = (void *)((uintptr_t)uart + mmr_base() + mmr_off);
  71. HW_TRACE_WRITE ();
  72. /* XXX: All MMRs are "8bit" ... what happens to high 8bits ? */
  73. switch (mmr_off)
  74. {
  75. case mmr_offset(thr):
  76. uart->thr = bfin_uart_write_byte (me, value, uart->mcr);
  77. if (uart->ier & ETBEI)
  78. hw_port_event (me, DV_PORT_TX, 1);
  79. break;
  80. case mmr_offset(ier_set):
  81. uart->ier |= value;
  82. break;
  83. case mmr_offset(ier_clear):
  84. dv_w1c_2 (&uart->ier, value, -1);
  85. break;
  86. case mmr_offset(lsr):
  87. dv_w1c_2 (valuep, value, TFI | BI | FE | PE | OE);
  88. break;
  89. case mmr_offset(rbr):
  90. /* XXX: Writes are ignored ? */
  91. break;
  92. case mmr_offset(msr):
  93. dv_w1c_2 (valuep, value, SCTS);
  94. break;
  95. case mmr_offset(dll):
  96. case mmr_offset(dlh):
  97. case mmr_offset(gctl):
  98. case mmr_offset(lcr):
  99. case mmr_offset(mcr):
  100. case mmr_offset(scr):
  101. *valuep = value;
  102. break;
  103. default:
  104. dv_bfin_mmr_invalid (me, addr, nr_bytes, true);
  105. return 0;
  106. }
  107. return nr_bytes;
  108. }
  109. static unsigned
  110. bfin_uart_io_read_buffer (struct hw *me, void *dest,
  111. int space, address_word addr, unsigned nr_bytes)
  112. {
  113. struct bfin_uart *uart = hw_data (me);
  114. bu32 mmr_off;
  115. bu16 *valuep;
  116. /* Invalid access mode is higher priority than missing register. */
  117. if (!dv_bfin_mmr_require_16 (me, addr, nr_bytes, false))
  118. return 0;
  119. mmr_off = addr - uart->base;
  120. valuep = (void *)((uintptr_t)uart + mmr_base() + mmr_off);
  121. HW_TRACE_READ ();
  122. switch (mmr_off)
  123. {
  124. case mmr_offset(rbr):
  125. uart->rbr = bfin_uart_get_next_byte (me, uart->rbr, uart->mcr, NULL);
  126. dv_store_2 (dest, uart->rbr);
  127. break;
  128. case mmr_offset(ier_set):
  129. case mmr_offset(ier_clear):
  130. dv_store_2 (dest, uart->ier);
  131. bfin_uart_reschedule (me);
  132. break;
  133. case mmr_offset(lsr):
  134. uart->lsr &= ~(DR | THRE | TEMT);
  135. uart->lsr |= bfin_uart_get_status (me);
  136. case mmr_offset(thr):
  137. case mmr_offset(msr):
  138. case mmr_offset(dll):
  139. case mmr_offset(dlh):
  140. case mmr_offset(gctl):
  141. case mmr_offset(lcr):
  142. case mmr_offset(mcr):
  143. case mmr_offset(scr):
  144. dv_store_2 (dest, *valuep);
  145. break;
  146. default:
  147. dv_bfin_mmr_invalid (me, addr, nr_bytes, false);
  148. return 0;
  149. }
  150. return nr_bytes;
  151. }
  152. static unsigned
  153. bfin_uart_dma_read_buffer (struct hw *me, void *dest, int space,
  154. unsigned_word addr, unsigned nr_bytes)
  155. {
  156. HW_TRACE_DMA_READ ();
  157. return bfin_uart_read_buffer (me, dest, nr_bytes);
  158. }
  159. static unsigned
  160. bfin_uart_dma_write_buffer (struct hw *me, const void *source,
  161. int space, unsigned_word addr,
  162. unsigned nr_bytes,
  163. int violate_read_only_section)
  164. {
  165. struct bfin_uart *uart = hw_data (me);
  166. unsigned ret;
  167. HW_TRACE_DMA_WRITE ();
  168. ret = bfin_uart_write_buffer (me, source, nr_bytes);
  169. if (ret == nr_bytes && (uart->ier & ETBEI))
  170. hw_port_event (me, DV_PORT_TX, 1);
  171. return ret;
  172. }
  173. static const struct hw_port_descriptor bfin_uart_ports[] =
  174. {
  175. { "tx", DV_PORT_TX, 0, output_port, },
  176. { "rx", DV_PORT_RX, 0, output_port, },
  177. { "stat", DV_PORT_STAT, 0, output_port, },
  178. { NULL, 0, 0, 0, },
  179. };
  180. static void
  181. attach_bfin_uart_regs (struct hw *me, struct bfin_uart *uart)
  182. {
  183. address_word attach_address;
  184. int attach_space;
  185. unsigned attach_size;
  186. reg_property_spec reg;
  187. if (hw_find_property (me, "reg") == NULL)
  188. hw_abort (me, "Missing \"reg\" property");
  189. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  190. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  191. hw_unit_address_to_attach_address (hw_parent (me),
  192. &reg.address,
  193. &attach_space, &attach_address, me);
  194. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  195. if (attach_size != BFIN_MMR_UART2_SIZE)
  196. hw_abort (me, "\"reg\" size must be %#x", BFIN_MMR_UART2_SIZE);
  197. hw_attach_address (hw_parent (me),
  198. 0, attach_space, attach_address, attach_size, me);
  199. uart->base = attach_address;
  200. }
  201. static void
  202. bfin_uart_finish (struct hw *me)
  203. {
  204. struct bfin_uart *uart;
  205. uart = HW_ZALLOC (me, struct bfin_uart);
  206. set_hw_data (me, uart);
  207. set_hw_io_read_buffer (me, bfin_uart_io_read_buffer);
  208. set_hw_io_write_buffer (me, bfin_uart_io_write_buffer);
  209. set_hw_dma_read_buffer (me, bfin_uart_dma_read_buffer);
  210. set_hw_dma_write_buffer (me, bfin_uart_dma_write_buffer);
  211. set_hw_ports (me, bfin_uart_ports);
  212. attach_bfin_uart_regs (me, uart);
  213. /* Initialize the UART. */
  214. uart->dll = 0x0001;
  215. uart->lsr = 0x0060;
  216. }
  217. const struct hw_descriptor dv_bfin_uart2_descriptor[] =
  218. {
  219. {"bfin_uart2", bfin_uart_finish,},
  220. {NULL, NULL},
  221. };