lasips2.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * QEMU HP Lasi PS/2 interface emulation
  3. *
  4. * Copyright (c) 2019 Sven Schnelle
  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 "qemu/log.h"
  26. #include "hw/qdev-properties.h"
  27. #include "hw/hw.h"
  28. #include "hw/input/ps2.h"
  29. #include "hw/input/lasips2.h"
  30. #include "hw/sysbus.h"
  31. #include "exec/hwaddr.h"
  32. #include "sysemu/sysemu.h"
  33. #include "trace.h"
  34. #include "exec/address-spaces.h"
  35. #include "migration/vmstate.h"
  36. #include "hw/irq.h"
  37. struct LASIPS2State;
  38. typedef struct LASIPS2Port {
  39. struct LASIPS2State *parent;
  40. MemoryRegion reg;
  41. void *dev;
  42. uint8_t id;
  43. uint8_t control;
  44. uint8_t buf;
  45. bool loopback_rbne;
  46. bool irq;
  47. } LASIPS2Port;
  48. typedef struct LASIPS2State {
  49. LASIPS2Port kbd;
  50. LASIPS2Port mouse;
  51. qemu_irq irq;
  52. } LASIPS2State;
  53. static const VMStateDescription vmstate_lasips2 = {
  54. .name = "lasips2",
  55. .version_id = 0,
  56. .minimum_version_id = 0,
  57. .fields = (VMStateField[]) {
  58. VMSTATE_UINT8(kbd.control, LASIPS2State),
  59. VMSTATE_UINT8(kbd.id, LASIPS2State),
  60. VMSTATE_BOOL(kbd.irq, LASIPS2State),
  61. VMSTATE_UINT8(mouse.control, LASIPS2State),
  62. VMSTATE_UINT8(mouse.id, LASIPS2State),
  63. VMSTATE_BOOL(mouse.irq, LASIPS2State),
  64. VMSTATE_END_OF_LIST()
  65. }
  66. };
  67. typedef enum {
  68. REG_PS2_ID = 0,
  69. REG_PS2_RCVDATA = 4,
  70. REG_PS2_CONTROL = 8,
  71. REG_PS2_STATUS = 12,
  72. } lasips2_read_reg_t;
  73. typedef enum {
  74. REG_PS2_RESET = 0,
  75. REG_PS2_XMTDATA = 4,
  76. } lasips2_write_reg_t;
  77. typedef enum {
  78. LASIPS2_CONTROL_ENABLE = 0x01,
  79. LASIPS2_CONTROL_LOOPBACK = 0x02,
  80. LASIPS2_CONTROL_DIAG = 0x20,
  81. LASIPS2_CONTROL_DATDIR = 0x40,
  82. LASIPS2_CONTROL_CLKDIR = 0x80,
  83. } lasips2_control_reg_t;
  84. typedef enum {
  85. LASIPS2_STATUS_RBNE = 0x01,
  86. LASIPS2_STATUS_TBNE = 0x02,
  87. LASIPS2_STATUS_TERR = 0x04,
  88. LASIPS2_STATUS_PERR = 0x08,
  89. LASIPS2_STATUS_CMPINTR = 0x10,
  90. LASIPS2_STATUS_DATSHD = 0x40,
  91. LASIPS2_STATUS_CLKSHD = 0x80,
  92. } lasips2_status_reg_t;
  93. static const char *artist_read_reg_name(uint64_t addr)
  94. {
  95. switch (addr & 0xc) {
  96. case REG_PS2_ID:
  97. return " PS2_ID";
  98. case REG_PS2_RCVDATA:
  99. return " PS2_RCVDATA";
  100. case REG_PS2_CONTROL:
  101. return " PS2_CONTROL";
  102. case REG_PS2_STATUS:
  103. return " PS2_STATUS";
  104. default:
  105. return "";
  106. }
  107. }
  108. static const char *artist_write_reg_name(uint64_t addr)
  109. {
  110. switch (addr & 0x0c) {
  111. case REG_PS2_RESET:
  112. return " PS2_RESET";
  113. case REG_PS2_XMTDATA:
  114. return " PS2_XMTDATA";
  115. case REG_PS2_CONTROL:
  116. return " PS2_CONTROL";
  117. default:
  118. return "";
  119. }
  120. }
  121. static void lasips2_update_irq(LASIPS2State *s)
  122. {
  123. trace_lasips2_intr(s->kbd.irq | s->mouse.irq);
  124. qemu_set_irq(s->irq, s->kbd.irq | s->mouse.irq);
  125. }
  126. static void lasips2_reg_write(void *opaque, hwaddr addr, uint64_t val,
  127. unsigned size)
  128. {
  129. LASIPS2Port *port = opaque;
  130. trace_lasips2_reg_write(size, port->id, addr,
  131. artist_write_reg_name(addr), val);
  132. switch (addr & 0xc) {
  133. case REG_PS2_CONTROL:
  134. port->control = val;
  135. break;
  136. case REG_PS2_XMTDATA:
  137. if (port->control & LASIPS2_CONTROL_LOOPBACK) {
  138. port->buf = val;
  139. port->irq = true;
  140. port->loopback_rbne = true;
  141. lasips2_update_irq(port->parent);
  142. break;
  143. }
  144. if (port->id) {
  145. ps2_write_mouse(port->dev, val);
  146. } else {
  147. ps2_write_keyboard(port->dev, val);
  148. }
  149. break;
  150. case REG_PS2_RESET:
  151. break;
  152. default:
  153. qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
  154. __func__, addr);
  155. break;
  156. }
  157. }
  158. static uint64_t lasips2_reg_read(void *opaque, hwaddr addr, unsigned size)
  159. {
  160. LASIPS2Port *port = opaque;
  161. uint64_t ret = 0;
  162. switch (addr & 0xc) {
  163. case REG_PS2_ID:
  164. ret = port->id;
  165. break;
  166. case REG_PS2_RCVDATA:
  167. if (port->control & LASIPS2_CONTROL_LOOPBACK) {
  168. port->irq = false;
  169. port->loopback_rbne = false;
  170. lasips2_update_irq(port->parent);
  171. ret = port->buf;
  172. break;
  173. }
  174. ret = ps2_read_data(port->dev);
  175. break;
  176. case REG_PS2_CONTROL:
  177. ret = port->control;
  178. break;
  179. case REG_PS2_STATUS:
  180. ret = LASIPS2_STATUS_DATSHD | LASIPS2_STATUS_CLKSHD;
  181. if (port->control & LASIPS2_CONTROL_DIAG) {
  182. if (!(port->control & LASIPS2_CONTROL_DATDIR)) {
  183. ret &= ~LASIPS2_STATUS_DATSHD;
  184. }
  185. if (!(port->control & LASIPS2_CONTROL_CLKDIR)) {
  186. ret &= ~LASIPS2_STATUS_CLKSHD;
  187. }
  188. }
  189. if (port->control & LASIPS2_CONTROL_LOOPBACK) {
  190. if (port->loopback_rbne) {
  191. ret |= LASIPS2_STATUS_RBNE;
  192. }
  193. } else {
  194. if (!ps2_queue_empty(port->dev)) {
  195. ret |= LASIPS2_STATUS_RBNE;
  196. }
  197. }
  198. if (port->parent->kbd.irq || port->parent->mouse.irq) {
  199. ret |= LASIPS2_STATUS_CMPINTR;
  200. }
  201. break;
  202. default:
  203. qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
  204. __func__, addr);
  205. break;
  206. }
  207. trace_lasips2_reg_read(size, port->id, addr,
  208. artist_read_reg_name(addr), ret);
  209. return ret;
  210. }
  211. static const MemoryRegionOps lasips2_reg_ops = {
  212. .read = lasips2_reg_read,
  213. .write = lasips2_reg_write,
  214. .impl = {
  215. .min_access_size = 1,
  216. .max_access_size = 4,
  217. },
  218. .endianness = DEVICE_NATIVE_ENDIAN,
  219. };
  220. static void ps2dev_update_irq(void *opaque, int level)
  221. {
  222. LASIPS2Port *port = opaque;
  223. port->irq = level;
  224. lasips2_update_irq(port->parent);
  225. }
  226. void lasips2_init(MemoryRegion *address_space,
  227. hwaddr base, qemu_irq irq)
  228. {
  229. LASIPS2State *s;
  230. s = g_malloc0(sizeof(LASIPS2State));
  231. s->irq = irq;
  232. s->mouse.id = 1;
  233. s->kbd.parent = s;
  234. s->mouse.parent = s;
  235. vmstate_register(NULL, base, &vmstate_lasips2, s);
  236. s->kbd.dev = ps2_kbd_init(ps2dev_update_irq, &s->kbd);
  237. s->mouse.dev = ps2_mouse_init(ps2dev_update_irq, &s->mouse);
  238. memory_region_init_io(&s->kbd.reg, NULL, &lasips2_reg_ops, &s->kbd,
  239. "lasips2-kbd", 0x100);
  240. memory_region_add_subregion(address_space, base, &s->kbd.reg);
  241. memory_region_init_io(&s->mouse.reg, NULL, &lasips2_reg_ops, &s->mouse,
  242. "lasips2-mouse", 0x100);
  243. memory_region_add_subregion(address_space, base + 0x100, &s->mouse.reg);
  244. }