stm32f2xx_usart.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * STM32F2XX USART
  3. *
  4. * Copyright (c) 2014 Alistair Francis <alistair@alistair23.me>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "hw/char/stm32f2xx_usart.h"
  26. #include "hw/irq.h"
  27. #include "hw/qdev-properties.h"
  28. #include "hw/qdev-properties-system.h"
  29. #include "qemu/log.h"
  30. #include "qemu/module.h"
  31. #include "trace.h"
  32. static int stm32f2xx_usart_can_receive(void *opaque)
  33. {
  34. STM32F2XXUsartState *s = opaque;
  35. if (!(s->usart_sr & USART_SR_RXNE)) {
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. static void stm32f2xx_update_irq(STM32F2XXUsartState *s)
  41. {
  42. uint32_t mask = s->usart_sr & s->usart_cr1;
  43. if (mask & (USART_SR_TXE | USART_SR_TC | USART_SR_RXNE)) {
  44. qemu_set_irq(s->irq, 1);
  45. } else {
  46. qemu_set_irq(s->irq, 0);
  47. }
  48. }
  49. static void stm32f2xx_usart_receive(void *opaque, const uint8_t *buf, int size)
  50. {
  51. STM32F2XXUsartState *s = opaque;
  52. DeviceState *d = DEVICE(s);
  53. if (!(s->usart_cr1 & USART_CR1_UE && s->usart_cr1 & USART_CR1_RE)) {
  54. /* USART not enabled - drop the chars */
  55. trace_stm32f2xx_usart_drop(d->id);
  56. return;
  57. }
  58. s->usart_dr = *buf;
  59. s->usart_sr |= USART_SR_RXNE;
  60. stm32f2xx_update_irq(s);
  61. trace_stm32f2xx_usart_receive(d->id, *buf);
  62. }
  63. static void stm32f2xx_usart_reset(DeviceState *dev)
  64. {
  65. STM32F2XXUsartState *s = STM32F2XX_USART(dev);
  66. s->usart_sr = USART_SR_RESET;
  67. s->usart_dr = 0x00000000;
  68. s->usart_brr = 0x00000000;
  69. s->usart_cr1 = 0x00000000;
  70. s->usart_cr2 = 0x00000000;
  71. s->usart_cr3 = 0x00000000;
  72. s->usart_gtpr = 0x00000000;
  73. stm32f2xx_update_irq(s);
  74. }
  75. static uint64_t stm32f2xx_usart_read(void *opaque, hwaddr addr,
  76. unsigned int size)
  77. {
  78. STM32F2XXUsartState *s = opaque;
  79. DeviceState *d = DEVICE(s);
  80. uint64_t retvalue = 0;
  81. switch (addr) {
  82. case USART_SR:
  83. retvalue = s->usart_sr;
  84. qemu_chr_fe_accept_input(&s->chr);
  85. break;
  86. case USART_DR:
  87. retvalue = s->usart_dr & 0x3FF;
  88. s->usart_sr &= ~USART_SR_RXNE;
  89. qemu_chr_fe_accept_input(&s->chr);
  90. stm32f2xx_update_irq(s);
  91. break;
  92. case USART_BRR:
  93. retvalue = s->usart_brr;
  94. break;
  95. case USART_CR1:
  96. retvalue = s->usart_cr1;
  97. break;
  98. case USART_CR2:
  99. retvalue = s->usart_cr2;
  100. break;
  101. case USART_CR3:
  102. retvalue = s->usart_cr3;
  103. break;
  104. case USART_GTPR:
  105. retvalue = s->usart_gtpr;
  106. break;
  107. default:
  108. qemu_log_mask(LOG_GUEST_ERROR,
  109. "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
  110. return 0;
  111. }
  112. trace_stm32f2xx_usart_read(d->id, size, addr, retvalue);
  113. return retvalue;
  114. }
  115. static void stm32f2xx_usart_write(void *opaque, hwaddr addr,
  116. uint64_t val64, unsigned int size)
  117. {
  118. STM32F2XXUsartState *s = opaque;
  119. DeviceState *d = DEVICE(s);
  120. uint32_t value = val64;
  121. unsigned char ch;
  122. trace_stm32f2xx_usart_write(d->id, size, addr, val64);
  123. switch (addr) {
  124. case USART_SR:
  125. if (value <= 0x3FF) {
  126. /* I/O being synchronous, TXE is always set. In addition, it may
  127. only be set by hardware, so keep it set here. */
  128. s->usart_sr = value | USART_SR_TXE;
  129. } else {
  130. s->usart_sr &= value;
  131. }
  132. stm32f2xx_update_irq(s);
  133. return;
  134. case USART_DR:
  135. if (value < 0xF000) {
  136. ch = value;
  137. /* XXX this blocks entire thread. Rewrite to use
  138. * qemu_chr_fe_write and background I/O callbacks */
  139. qemu_chr_fe_write_all(&s->chr, &ch, 1);
  140. /* XXX I/O are currently synchronous, making it impossible for
  141. software to observe transient states where TXE or TC aren't
  142. set. Unlike TXE however, which is read-only, software may
  143. clear TC by writing 0 to the SR register, so set it again
  144. on each write. */
  145. s->usart_sr |= USART_SR_TC;
  146. stm32f2xx_update_irq(s);
  147. }
  148. return;
  149. case USART_BRR:
  150. s->usart_brr = value;
  151. return;
  152. case USART_CR1:
  153. s->usart_cr1 = value;
  154. stm32f2xx_update_irq(s);
  155. return;
  156. case USART_CR2:
  157. s->usart_cr2 = value;
  158. return;
  159. case USART_CR3:
  160. s->usart_cr3 = value;
  161. return;
  162. case USART_GTPR:
  163. s->usart_gtpr = value;
  164. return;
  165. default:
  166. qemu_log_mask(LOG_GUEST_ERROR,
  167. "%s: Bad offset 0x%"HWADDR_PRIx"\n", __func__, addr);
  168. }
  169. }
  170. static const MemoryRegionOps stm32f2xx_usart_ops = {
  171. .read = stm32f2xx_usart_read,
  172. .write = stm32f2xx_usart_write,
  173. .endianness = DEVICE_NATIVE_ENDIAN,
  174. };
  175. static const Property stm32f2xx_usart_properties[] = {
  176. DEFINE_PROP_CHR("chardev", STM32F2XXUsartState, chr),
  177. };
  178. static void stm32f2xx_usart_init(Object *obj)
  179. {
  180. STM32F2XXUsartState *s = STM32F2XX_USART(obj);
  181. sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
  182. memory_region_init_io(&s->mmio, obj, &stm32f2xx_usart_ops, s,
  183. TYPE_STM32F2XX_USART, 0x400);
  184. sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
  185. }
  186. static void stm32f2xx_usart_realize(DeviceState *dev, Error **errp)
  187. {
  188. STM32F2XXUsartState *s = STM32F2XX_USART(dev);
  189. qemu_chr_fe_set_handlers(&s->chr, stm32f2xx_usart_can_receive,
  190. stm32f2xx_usart_receive, NULL, NULL,
  191. s, NULL, true);
  192. }
  193. static void stm32f2xx_usart_class_init(ObjectClass *klass, void *data)
  194. {
  195. DeviceClass *dc = DEVICE_CLASS(klass);
  196. device_class_set_legacy_reset(dc, stm32f2xx_usart_reset);
  197. device_class_set_props(dc, stm32f2xx_usart_properties);
  198. dc->realize = stm32f2xx_usart_realize;
  199. }
  200. static const TypeInfo stm32f2xx_usart_info = {
  201. .name = TYPE_STM32F2XX_USART,
  202. .parent = TYPE_SYS_BUS_DEVICE,
  203. .instance_size = sizeof(STM32F2XXUsartState),
  204. .instance_init = stm32f2xx_usart_init,
  205. .class_init = stm32f2xx_usart_class_init,
  206. };
  207. static void stm32f2xx_usart_register_types(void)
  208. {
  209. type_register_static(&stm32f2xx_usart_info);
  210. }
  211. type_init(stm32f2xx_usart_register_types)