ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1 | /* |
pbrook | 69db0ac | 2006-07-17 18:45:34 +0000 | [diff] [blame] | 2 | * Arm PrimeCell PL050 Keyboard / Mouse Interface |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 3 | * |
pbrook | 9e61ec3 | 2007-04-30 02:39:55 +0000 | [diff] [blame] | 4 | * Copyright (c) 2006-2007 CodeSourcery. |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 5 | * Written by Paul Brook |
| 6 | * |
Matthew Fernandez | 8e31bf3 | 2011-06-26 12:21:35 +1000 | [diff] [blame] | 7 | * This code is licensed under the GPL. |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 10 | #include "hw/sysbus.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 11 | #include "hw/input/ps2.h" |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 12 | |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 13 | #define TYPE_PL050 "pl050" |
| 14 | #define PL050(obj) OBJECT_CHECK(PL050State, (obj), TYPE_PL050) |
| 15 | |
Andreas Färber | e607f25 | 2013-07-26 18:40:25 +0200 | [diff] [blame] | 16 | typedef struct PL050State { |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 17 | SysBusDevice parent_obj; |
| 18 | |
Avi Kivity | b8f7a73 | 2011-10-10 17:18:44 +0200 | [diff] [blame] | 19 | MemoryRegion iomem; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 20 | void *dev; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 21 | uint32_t cr; |
| 22 | uint32_t clk; |
| 23 | uint32_t last; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 24 | int pending; |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 25 | qemu_irq irq; |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 26 | bool is_mouse; |
Andreas Färber | e607f25 | 2013-07-26 18:40:25 +0200 | [diff] [blame] | 27 | } PL050State; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 28 | |
Peter Maydell | d6ac172 | 2010-12-23 17:19:54 +0000 | [diff] [blame] | 29 | static const VMStateDescription vmstate_pl050 = { |
| 30 | .name = "pl050", |
Peter Maydell | e8945b4 | 2013-04-05 16:17:58 +0100 | [diff] [blame] | 31 | .version_id = 2, |
| 32 | .minimum_version_id = 2, |
Peter Maydell | d6ac172 | 2010-12-23 17:19:54 +0000 | [diff] [blame] | 33 | .fields = (VMStateField[]) { |
Andreas Färber | e607f25 | 2013-07-26 18:40:25 +0200 | [diff] [blame] | 34 | VMSTATE_UINT32(cr, PL050State), |
| 35 | VMSTATE_UINT32(clk, PL050State), |
| 36 | VMSTATE_UINT32(last, PL050State), |
| 37 | VMSTATE_INT32(pending, PL050State), |
Peter Maydell | d6ac172 | 2010-12-23 17:19:54 +0000 | [diff] [blame] | 38 | VMSTATE_END_OF_LIST() |
| 39 | } |
| 40 | }; |
| 41 | |
pbrook | 9e61ec3 | 2007-04-30 02:39:55 +0000 | [diff] [blame] | 42 | #define PL050_TXEMPTY (1 << 6) |
| 43 | #define PL050_TXBUSY (1 << 5) |
| 44 | #define PL050_RXFULL (1 << 4) |
| 45 | #define PL050_RXBUSY (1 << 3) |
| 46 | #define PL050_RXPARITY (1 << 2) |
| 47 | #define PL050_KMIC (1 << 1) |
| 48 | #define PL050_KMID (1 << 0) |
| 49 | |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 50 | static const unsigned char pl050_id[] = |
| 51 | { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 }; |
| 52 | |
| 53 | static void pl050_update(void *opaque, int level) |
| 54 | { |
Andreas Färber | e607f25 | 2013-07-26 18:40:25 +0200 | [diff] [blame] | 55 | PL050State *s = (PL050State *)opaque; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 56 | int raise; |
| 57 | |
| 58 | s->pending = level; |
| 59 | raise = (s->pending && (s->cr & 0x10) != 0) |
| 60 | || (s->cr & 0x08) != 0; |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 61 | qemu_set_irq(s->irq, raise); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 64 | static uint64_t pl050_read(void *opaque, hwaddr offset, |
Avi Kivity | b8f7a73 | 2011-10-10 17:18:44 +0200 | [diff] [blame] | 65 | unsigned size) |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 66 | { |
Andreas Färber | e607f25 | 2013-07-26 18:40:25 +0200 | [diff] [blame] | 67 | PL050State *s = (PL050State *)opaque; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 68 | if (offset >= 0xfe0 && offset < 0x1000) |
| 69 | return pl050_id[(offset - 0xfe0) >> 2]; |
| 70 | |
| 71 | switch (offset >> 2) { |
| 72 | case 0: /* KMICR */ |
| 73 | return s->cr; |
| 74 | case 1: /* KMISTAT */ |
pbrook | 9e61ec3 | 2007-04-30 02:39:55 +0000 | [diff] [blame] | 75 | { |
| 76 | uint8_t val; |
| 77 | uint32_t stat; |
| 78 | |
| 79 | val = s->last; |
| 80 | val = val ^ (val >> 4); |
| 81 | val = val ^ (val >> 2); |
| 82 | val = (val ^ (val >> 1)) & 1; |
| 83 | |
| 84 | stat = PL050_TXEMPTY; |
| 85 | if (val) |
| 86 | stat |= PL050_RXPARITY; |
| 87 | if (s->pending) |
| 88 | stat |= PL050_RXFULL; |
| 89 | |
| 90 | return stat; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 91 | } |
| 92 | case 2: /* KMIDATA */ |
| 93 | if (s->pending) |
| 94 | s->last = ps2_read_data(s->dev); |
| 95 | return s->last; |
| 96 | case 3: /* KMICLKDIV */ |
| 97 | return s->clk; |
| 98 | case 4: /* KMIIR */ |
| 99 | return s->pending | 2; |
| 100 | default: |
Peter Maydell | fbfecf4 | 2012-10-30 07:45:08 +0000 | [diff] [blame] | 101 | qemu_log_mask(LOG_GUEST_ERROR, |
| 102 | "pl050_read: Bad offset %x\n", (int)offset); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 103 | return 0; |
| 104 | } |
| 105 | } |
| 106 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 107 | static void pl050_write(void *opaque, hwaddr offset, |
Avi Kivity | b8f7a73 | 2011-10-10 17:18:44 +0200 | [diff] [blame] | 108 | uint64_t value, unsigned size) |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 109 | { |
Andreas Färber | e607f25 | 2013-07-26 18:40:25 +0200 | [diff] [blame] | 110 | PL050State *s = (PL050State *)opaque; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 111 | switch (offset >> 2) { |
| 112 | case 0: /* KMICR */ |
| 113 | s->cr = value; |
| 114 | pl050_update(s, s->pending); |
| 115 | /* ??? Need to implement the enable/disable bit. */ |
| 116 | break; |
| 117 | case 2: /* KMIDATA */ |
| 118 | /* ??? This should toggle the TX interrupt line. */ |
| 119 | /* ??? This means kbd/mouse can block each other. */ |
| 120 | if (s->is_mouse) { |
| 121 | ps2_write_mouse(s->dev, value); |
| 122 | } else { |
| 123 | ps2_write_keyboard(s->dev, value); |
| 124 | } |
| 125 | break; |
| 126 | case 3: /* KMICLKDIV */ |
| 127 | s->clk = value; |
| 128 | return; |
| 129 | default: |
Peter Maydell | fbfecf4 | 2012-10-30 07:45:08 +0000 | [diff] [blame] | 130 | qemu_log_mask(LOG_GUEST_ERROR, |
| 131 | "pl050_write: Bad offset %x\n", (int)offset); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
Avi Kivity | b8f7a73 | 2011-10-10 17:18:44 +0200 | [diff] [blame] | 134 | static const MemoryRegionOps pl050_ops = { |
| 135 | .read = pl050_read, |
| 136 | .write = pl050_write, |
| 137 | .endianness = DEVICE_NATIVE_ENDIAN, |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 140 | static int pl050_initfn(SysBusDevice *dev) |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 141 | { |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 142 | PL050State *s = PL050(dev); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 143 | |
Paolo Bonzini | 1437c94 | 2013-06-06 21:25:08 -0400 | [diff] [blame] | 144 | memory_region_init_io(&s->iomem, OBJECT(s), &pl050_ops, s, "pl050", 0x1000); |
Avi Kivity | 750ecd4 | 2011-11-27 11:38:10 +0200 | [diff] [blame] | 145 | sysbus_init_mmio(dev, &s->iomem); |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 146 | sysbus_init_irq(dev, &s->irq); |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 147 | if (s->is_mouse) { |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 148 | s->dev = ps2_mouse_init(pl050_update, s); |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 149 | } else { |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 150 | s->dev = ps2_kbd_init(pl050_update, s); |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 151 | } |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 152 | return 0; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 153 | } |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 154 | |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 155 | static void pl050_keyboard_init(Object *obj) |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 156 | { |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 157 | PL050State *s = PL050(obj); |
| 158 | |
| 159 | s->is_mouse = false; |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 162 | static void pl050_mouse_init(Object *obj) |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 163 | { |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 164 | PL050State *s = PL050(obj); |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 165 | |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 166 | s->is_mouse = true; |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 167 | } |
| 168 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 169 | static const TypeInfo pl050_kbd_info = { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 170 | .name = "pl050_keyboard", |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 171 | .parent = TYPE_PL050, |
| 172 | .instance_init = pl050_keyboard_init, |
Peter Maydell | d6ac172 | 2010-12-23 17:19:54 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 175 | static const TypeInfo pl050_mouse_info = { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 176 | .name = "pl050_mouse", |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 177 | .parent = TYPE_PL050, |
| 178 | .instance_init = pl050_mouse_init, |
| 179 | }; |
| 180 | |
| 181 | static void pl050_class_init(ObjectClass *oc, void *data) |
| 182 | { |
| 183 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 184 | SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(oc); |
| 185 | |
| 186 | sdc->init = pl050_initfn; |
| 187 | dc->vmsd = &vmstate_pl050; |
| 188 | } |
| 189 | |
| 190 | static const TypeInfo pl050_type_info = { |
| 191 | .name = TYPE_PL050, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 192 | .parent = TYPE_SYS_BUS_DEVICE, |
Andreas Färber | e607f25 | 2013-07-26 18:40:25 +0200 | [diff] [blame] | 193 | .instance_size = sizeof(PL050State), |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 194 | .abstract = true, |
| 195 | .class_init = pl050_class_init, |
Peter Maydell | d6ac172 | 2010-12-23 17:19:54 +0000 | [diff] [blame] | 196 | }; |
| 197 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 198 | static void pl050_register_types(void) |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 199 | { |
Andreas Färber | 3e5dd36 | 2013-07-26 18:49:24 +0200 | [diff] [blame] | 200 | type_register_static(&pl050_type_info); |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 201 | type_register_static(&pl050_kbd_info); |
| 202 | type_register_static(&pl050_mouse_info); |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 205 | type_init(pl050_register_types) |