dv-lm32uart.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* Lattice Mico32 UART model.
  2. Contributed by Jon Beniston <jon@beniston.com>
  3. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  4. This file is part of GDB.
  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 "sim-main.h"
  16. #include "hw-main.h"
  17. #include "sim-assert.h"
  18. #include <stdio.h>
  19. #include <sys/time.h>
  20. struct lm32uart
  21. {
  22. unsigned base; /* Base address of this UART. */
  23. unsigned limit; /* Limit address of this UART. */
  24. unsigned char rbr;
  25. unsigned char thr;
  26. unsigned char ier;
  27. unsigned char iir;
  28. unsigned char lcr;
  29. unsigned char mcr;
  30. unsigned char lsr;
  31. unsigned char msr;
  32. unsigned char div;
  33. struct hw_event *event;
  34. };
  35. /* UART registers. */
  36. #define LM32_UART_RBR 0x0
  37. #define LM32_UART_THR 0x0
  38. #define LM32_UART_IER 0x4
  39. #define LM32_UART_IIR 0x8
  40. #define LM32_UART_LCR 0xc
  41. #define LM32_UART_MCR 0x10
  42. #define LM32_UART_LSR 0x14
  43. #define LM32_UART_MSR 0x18
  44. #define LM32_UART_DIV 0x1c
  45. #define LM32_UART_IER_RX_INT 0x1
  46. #define LM32_UART_IER_TX_INT 0x2
  47. #define MICOUART_IIR_TXRDY 0x2
  48. #define MICOUART_IIR_RXRDY 0x4
  49. #define LM32_UART_LSR_RX_RDY 0x01
  50. #define LM32_UART_LSR_TX_RDY 0x20
  51. #define LM32_UART_LCR_WLS_MASK 0x3
  52. #define LM32_UART_LCR_WLS_5 0x0
  53. #define LM32_UART_LCR_WLS_6 0x1
  54. #define LM32_UART_LCR_WLS_7 0x2
  55. #define LM32_UART_LCR_WLS_8 0x3
  56. /* UART ports. */
  57. enum
  58. {
  59. INT_PORT
  60. };
  61. static const struct hw_port_descriptor lm32uart_ports[] = {
  62. {"int", INT_PORT, 0, output_port},
  63. {}
  64. };
  65. static void
  66. do_uart_tx_event (struct hw *me, void *data)
  67. {
  68. struct lm32uart *uart = hw_data (me);
  69. char c;
  70. /* Generate interrupt when transmission is complete. */
  71. if (uart->ier & LM32_UART_IER_TX_INT)
  72. {
  73. /* Generate interrupt */
  74. hw_port_event (me, INT_PORT, 1);
  75. }
  76. /* Indicate which interrupt has occured. */
  77. uart->iir = MICOUART_IIR_TXRDY;
  78. /* Indicate THR is empty. */
  79. uart->lsr |= LM32_UART_LSR_TX_RDY;
  80. /* Output the character in the THR. */
  81. c = (char) uart->thr;
  82. /* WLS field in LCR register specifies the number of bits to output. */
  83. switch (uart->lcr & LM32_UART_LCR_WLS_MASK)
  84. {
  85. case LM32_UART_LCR_WLS_5:
  86. c &= 0x1f;
  87. break;
  88. case LM32_UART_LCR_WLS_6:
  89. c &= 0x3f;
  90. break;
  91. case LM32_UART_LCR_WLS_7:
  92. c &= 0x7f;
  93. break;
  94. }
  95. printf ("%c", c);
  96. }
  97. static unsigned
  98. lm32uart_io_write_buffer (struct hw *me,
  99. const void *source,
  100. int space, unsigned_word base, unsigned nr_bytes)
  101. {
  102. struct lm32uart *uart = hw_data (me);
  103. int uart_reg;
  104. const unsigned char *source_bytes = source;
  105. int value = 0;
  106. HW_TRACE ((me, "write to 0x%08lx length %d with 0x%x", (long) base,
  107. (int) nr_bytes, value));
  108. if (nr_bytes == 4)
  109. value = (source_bytes[0] << 24)
  110. | (source_bytes[1] << 16) | (source_bytes[2] << 8) | (source_bytes[3]);
  111. else
  112. hw_abort (me, "write of unsupported number of bytes: %d.", nr_bytes);
  113. uart_reg = base - uart->base;
  114. switch (uart_reg)
  115. {
  116. case LM32_UART_THR:
  117. /* Buffer the character to output. */
  118. uart->thr = value;
  119. /* Indicate the THR is full. */
  120. uart->lsr &= ~LM32_UART_LSR_TX_RDY;
  121. /* deassert interrupt when IER is loaded. */
  122. uart->iir &= ~MICOUART_IIR_TXRDY;
  123. /* schedule an event to output the character. */
  124. hw_event_queue_schedule (me, 1, do_uart_tx_event, 0);
  125. break;
  126. case LM32_UART_IER:
  127. uart->ier = value;
  128. if ((value & LM32_UART_IER_TX_INT)
  129. && (uart->lsr & LM32_UART_LSR_TX_RDY))
  130. {
  131. /* hw_event_queue_schedule (me, 1, do_uart_tx_event, 0); */
  132. uart->lsr |= LM32_UART_LSR_TX_RDY;
  133. uart->iir |= MICOUART_IIR_TXRDY;
  134. hw_port_event (me, INT_PORT, 1);
  135. }
  136. else if ((value & LM32_UART_IER_TX_INT) == 0)
  137. {
  138. hw_port_event (me, INT_PORT, 0);
  139. }
  140. break;
  141. case LM32_UART_IIR:
  142. uart->iir = value;
  143. break;
  144. case LM32_UART_LCR:
  145. uart->lcr = value;
  146. break;
  147. case LM32_UART_MCR:
  148. uart->mcr = value;
  149. break;
  150. case LM32_UART_LSR:
  151. uart->lsr = value;
  152. break;
  153. case LM32_UART_MSR:
  154. uart->msr = value;
  155. break;
  156. case LM32_UART_DIV:
  157. uart->div = value;
  158. break;
  159. default:
  160. hw_abort (me, "write to invalid register address: 0x%x.", uart_reg);
  161. }
  162. return nr_bytes;
  163. }
  164. static unsigned
  165. lm32uart_io_read_buffer (struct hw *me,
  166. void *dest,
  167. int space, unsigned_word base, unsigned nr_bytes)
  168. {
  169. struct lm32uart *uart = hw_data (me);
  170. int uart_reg;
  171. int value;
  172. unsigned char *dest_bytes = dest;
  173. fd_set fd;
  174. struct timeval tv;
  175. HW_TRACE ((me, "read 0x%08lx length %d", (long) base, (int) nr_bytes));
  176. uart_reg = base - uart->base;
  177. switch (uart_reg)
  178. {
  179. case LM32_UART_RBR:
  180. value = getchar ();
  181. uart->lsr &= ~LM32_UART_LSR_RX_RDY;
  182. break;
  183. case LM32_UART_IER:
  184. value = uart->ier;
  185. break;
  186. case LM32_UART_IIR:
  187. value = uart->iir;
  188. break;
  189. case LM32_UART_LCR:
  190. value = uart->lcr;
  191. break;
  192. case LM32_UART_MCR:
  193. value = uart->mcr;
  194. break;
  195. case LM32_UART_LSR:
  196. /* Check to see if any data waiting in stdin. */
  197. FD_ZERO (&fd);
  198. FD_SET (fileno (stdin), &fd);
  199. tv.tv_sec = 0;
  200. tv.tv_usec = 1;
  201. if (select (fileno (stdin) + 1, &fd, NULL, NULL, &tv))
  202. uart->lsr |= LM32_UART_LSR_RX_RDY;
  203. value = uart->lsr;
  204. break;
  205. case LM32_UART_MSR:
  206. value = uart->msr;
  207. break;
  208. case LM32_UART_DIV:
  209. value = uart->div;
  210. break;
  211. default:
  212. hw_abort (me, "read from invalid register address: 0x%x.", uart_reg);
  213. }
  214. if (nr_bytes == 4)
  215. {
  216. dest_bytes[0] = value >> 24;
  217. dest_bytes[1] = value >> 16;
  218. dest_bytes[2] = value >> 8;
  219. dest_bytes[3] = value;
  220. }
  221. else
  222. hw_abort (me, "read of unsupported number of bytes: %d", nr_bytes);
  223. return nr_bytes;
  224. }
  225. static void
  226. attach_lm32uart_regs (struct hw *me, struct lm32uart *uart)
  227. {
  228. unsigned_word attach_address;
  229. int attach_space;
  230. unsigned attach_size;
  231. reg_property_spec reg;
  232. if (hw_find_property (me, "reg") == NULL)
  233. hw_abort (me, "Missing \"reg\" property");
  234. if (!hw_find_reg_array_property (me, "reg", 0, &reg))
  235. hw_abort (me, "\"reg\" property must contain three addr/size entries");
  236. hw_unit_address_to_attach_address (hw_parent (me),
  237. &reg.address,
  238. &attach_space, &attach_address, me);
  239. uart->base = attach_address;
  240. hw_unit_size_to_attach_size (hw_parent (me), &reg.size, &attach_size, me);
  241. uart->limit = attach_address + (attach_size - 1);
  242. hw_attach_address (hw_parent (me),
  243. 0, attach_space, attach_address, attach_size, me);
  244. }
  245. static void
  246. lm32uart_finish (struct hw *me)
  247. {
  248. struct lm32uart *uart;
  249. int i;
  250. uart = HW_ZALLOC (me, struct lm32uart);
  251. set_hw_data (me, uart);
  252. set_hw_io_read_buffer (me, lm32uart_io_read_buffer);
  253. set_hw_io_write_buffer (me, lm32uart_io_write_buffer);
  254. set_hw_ports (me, lm32uart_ports);
  255. /* Attach ourself to our parent bus. */
  256. attach_lm32uart_regs (me, uart);
  257. /* Initialize the UART. */
  258. uart->rbr = 0;
  259. uart->thr = 0;
  260. uart->ier = 0;
  261. uart->iir = 0;
  262. uart->lcr = 0;
  263. uart->mcr = 0;
  264. uart->lsr = LM32_UART_LSR_TX_RDY;
  265. uart->msr = 0;
  266. uart->div = 0; /* By setting to zero, characters are output immediately. */
  267. }
  268. const struct hw_descriptor dv_lm32uart_descriptor[] = {
  269. {"lm32uart", lm32uart_finish,},
  270. {NULL},
  271. };