pl050.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Arm PrimeCell PL050 Keyboard / Mouse Interface
  3. *
  4. * Copyright (c) 2006-2007 CodeSourcery.
  5. * Written by Paul Brook
  6. *
  7. * This code is licensed under the GPL.
  8. */
  9. #include "qemu/osdep.h"
  10. #include "hw/sysbus.h"
  11. #include "migration/vmstate.h"
  12. #include "hw/input/ps2.h"
  13. #include "hw/irq.h"
  14. #include "qemu/log.h"
  15. #include "qemu/module.h"
  16. #define TYPE_PL050 "pl050"
  17. #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050)
  18. typedef struct PL050State {
  19. SysBusDevice parent_obj;
  20. MemoryRegion iomem;
  21. void *dev;
  22. uint32_t cr;
  23. uint32_t clk;
  24. uint32_t last;
  25. int pending;
  26. qemu_irq irq;
  27. bool is_mouse;
  28. } PL050State;
  29. static const VMStateDescription vmstate_pl050 = {
  30. .name = "pl050",
  31. .version_id = 2,
  32. .minimum_version_id = 2,
  33. .fields = (VMStateField[]) {
  34. VMSTATE_UINT32(cr, PL050State),
  35. VMSTATE_UINT32(clk, PL050State),
  36. VMSTATE_UINT32(last, PL050State),
  37. VMSTATE_INT32(pending, PL050State),
  38. VMSTATE_END_OF_LIST()
  39. }
  40. };
  41. #define PL050_TXEMPTY (1 << 6)
  42. #define PL050_TXBUSY (1 << 5)
  43. #define PL050_RXFULL (1 << 4)
  44. #define PL050_RXBUSY (1 << 3)
  45. #define PL050_RXPARITY (1 << 2)
  46. #define PL050_KMIC (1 << 1)
  47. #define PL050_KMID (1 << 0)
  48. static const unsigned char pl050_id[] =
  49. { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
  50. static void pl050_update(void *opaque, int level)
  51. {
  52. PL050State *s = (PL050State *)opaque;
  53. int raise;
  54. s->pending = level;
  55. raise = (s->pending && (s->cr & 0x10) != 0)
  56. || (s->cr & 0x08) != 0;
  57. qemu_set_irq(s->irq, raise);
  58. }
  59. static uint64_t pl050_read(void *opaque, hwaddr offset,
  60. unsigned size)
  61. {
  62. PL050State *s = (PL050State *)opaque;
  63. if (offset >= 0xfe0 && offset < 0x1000)
  64. return pl050_id[(offset - 0xfe0) >> 2];
  65. switch (offset >> 2) {
  66. case 0: /* KMICR */
  67. return s->cr;
  68. case 1: /* KMISTAT */
  69. {
  70. uint8_t val;
  71. uint32_t stat;
  72. val = s->last;
  73. val = val ^ (val >> 4);
  74. val = val ^ (val >> 2);
  75. val = (val ^ (val >> 1)) & 1;
  76. stat = PL050_TXEMPTY;
  77. if (val)
  78. stat |= PL050_RXPARITY;
  79. if (s->pending)
  80. stat |= PL050_RXFULL;
  81. return stat;
  82. }
  83. case 2: /* KMIDATA */
  84. if (s->pending)
  85. s->last = ps2_read_data(s->dev);
  86. return s->last;
  87. case 3: /* KMICLKDIV */
  88. return s->clk;
  89. case 4: /* KMIIR */
  90. return s->pending | 2;
  91. default:
  92. qemu_log_mask(LOG_GUEST_ERROR,
  93. "pl050_read: Bad offset %x\n", (int)offset);
  94. return 0;
  95. }
  96. }
  97. static void pl050_write(void *opaque, hwaddr offset,
  98. uint64_t value, unsigned size)
  99. {
  100. PL050State *s = (PL050State *)opaque;
  101. switch (offset >> 2) {
  102. case 0: /* KMICR */
  103. s->cr = value;
  104. pl050_update(s, s->pending);
  105. /* ??? Need to implement the enable/disable bit. */
  106. break;
  107. case 2: /* KMIDATA */
  108. /* ??? This should toggle the TX interrupt line. */
  109. /* ??? This means kbd/mouse can block each other. */
  110. if (s->is_mouse) {
  111. ps2_write_mouse(s->dev, value);
  112. } else {
  113. ps2_write_keyboard(s->dev, value);
  114. }
  115. break;
  116. case 3: /* KMICLKDIV */
  117. s->clk = value;
  118. return;
  119. default:
  120. qemu_log_mask(LOG_GUEST_ERROR,
  121. "pl050_write: Bad offset %x\n", (int)offset);
  122. }
  123. }
  124. static const MemoryRegionOps pl050_ops = {
  125. .read = pl050_read,
  126. .write = pl050_write,
  127. .endianness = DEVICE_NATIVE_ENDIAN,
  128. };
  129. static void pl050_realize(DeviceState *dev, Error **errp)
  130. {
  131. PL050State *s = PL050(dev);
  132. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  133. memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000);
  134. sysbus_init_mmio(sbd, &s->iomem);
  135. sysbus_init_irq(sbd, &s->irq);
  136. if (s->is_mouse) {
  137. s->dev = ps2_mouse_init(pl050_update, s);
  138. } else {
  139. s->dev = ps2_kbd_init(pl050_update, s);
  140. }
  141. }
  142. static void pl050_keyboard_init(Object *obj)
  143. {
  144. PL050State *s = PL050(obj);
  145. s->is_mouse = false;
  146. }
  147. static void pl050_mouse_init(Object *obj)
  148. {
  149. PL050State *s = PL050(obj);
  150. s->is_mouse = true;
  151. }
  152. static const TypeInfo pl050_kbd_info = {
  153. .name = "pl050_keyboard",
  154. .parent = TYPE_PL050,
  155. .instance_init = pl050_keyboard_init,
  156. };
  157. static const TypeInfo pl050_mouse_info = {
  158. .name = "pl050_mouse",
  159. .parent = TYPE_PL050,
  160. .instance_init = pl050_mouse_init,
  161. };
  162. static void pl050_class_init(ObjectClass *oc, void *data)
  163. {
  164. DeviceClass *dc = DEVICE_CLASS(oc);
  165. dc->realize = pl050_realize;
  166. dc->vmsd = &vmstate_pl050;
  167. }
  168. static const TypeInfo pl050_type_info = {
  169. .name = TYPE_PL050,
  170. .parent = TYPE_SYS_BUS_DEVICE,
  171. .instance_size = sizeof(PL050State),
  172. .abstract = true,
  173. .class_init = pl050_class_init,
  174. };
  175. static void pl050_register_types(void)
  176. {
  177. type_register_static(&pl050_type_info);
  178. type_register_static(&pl050_kbd_info);
  179. type_register_static(&pl050_mouse_info);
  180. }
  181. type_init(pl050_register_types)