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 | * |
| 7 | * This code is licenced under the GPL. |
| 8 | */ |
| 9 | |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 10 | #include "sysbus.h" |
pbrook | 87ecb68 | 2007-11-17 17:14:51 +0000 | [diff] [blame] | 11 | #include "ps2.h" |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 12 | |
| 13 | typedef struct { |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 14 | SysBusDevice busdev; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 15 | void *dev; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 16 | uint32_t cr; |
| 17 | uint32_t clk; |
| 18 | uint32_t last; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 19 | int pending; |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 20 | qemu_irq irq; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 21 | int is_mouse; |
| 22 | } pl050_state; |
| 23 | |
pbrook | 9e61ec3 | 2007-04-30 02:39:55 +0000 | [diff] [blame] | 24 | #define PL050_TXEMPTY (1 << 6) |
| 25 | #define PL050_TXBUSY (1 << 5) |
| 26 | #define PL050_RXFULL (1 << 4) |
| 27 | #define PL050_RXBUSY (1 << 3) |
| 28 | #define PL050_RXPARITY (1 << 2) |
| 29 | #define PL050_KMIC (1 << 1) |
| 30 | #define PL050_KMID (1 << 0) |
| 31 | |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 32 | static const unsigned char pl050_id[] = |
| 33 | { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 }; |
| 34 | |
| 35 | static void pl050_update(void *opaque, int level) |
| 36 | { |
| 37 | pl050_state *s = (pl050_state *)opaque; |
| 38 | int raise; |
| 39 | |
| 40 | s->pending = level; |
| 41 | raise = (s->pending && (s->cr & 0x10) != 0) |
| 42 | || (s->cr & 0x08) != 0; |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 43 | qemu_set_irq(s->irq, raise); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 46 | static uint32_t pl050_read(void *opaque, target_phys_addr_t offset) |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 47 | { |
| 48 | pl050_state *s = (pl050_state *)opaque; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 49 | if (offset >= 0xfe0 && offset < 0x1000) |
| 50 | return pl050_id[(offset - 0xfe0) >> 2]; |
| 51 | |
| 52 | switch (offset >> 2) { |
| 53 | case 0: /* KMICR */ |
| 54 | return s->cr; |
| 55 | case 1: /* KMISTAT */ |
pbrook | 9e61ec3 | 2007-04-30 02:39:55 +0000 | [diff] [blame] | 56 | { |
| 57 | uint8_t val; |
| 58 | uint32_t stat; |
| 59 | |
| 60 | val = s->last; |
| 61 | val = val ^ (val >> 4); |
| 62 | val = val ^ (val >> 2); |
| 63 | val = (val ^ (val >> 1)) & 1; |
| 64 | |
| 65 | stat = PL050_TXEMPTY; |
| 66 | if (val) |
| 67 | stat |= PL050_RXPARITY; |
| 68 | if (s->pending) |
| 69 | stat |= PL050_RXFULL; |
| 70 | |
| 71 | return stat; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 72 | } |
| 73 | case 2: /* KMIDATA */ |
| 74 | if (s->pending) |
| 75 | s->last = ps2_read_data(s->dev); |
| 76 | return s->last; |
| 77 | case 3: /* KMICLKDIV */ |
| 78 | return s->clk; |
| 79 | case 4: /* KMIIR */ |
| 80 | return s->pending | 2; |
| 81 | default: |
Paul Brook | 2ac7117 | 2009-05-08 02:35:15 +0100 | [diff] [blame] | 82 | hw_error("pl050_read: Bad offset %x\n", (int)offset); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | } |
| 86 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 87 | static void pl050_write(void *opaque, target_phys_addr_t offset, |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 88 | uint32_t value) |
| 89 | { |
| 90 | pl050_state *s = (pl050_state *)opaque; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 91 | switch (offset >> 2) { |
| 92 | case 0: /* KMICR */ |
| 93 | s->cr = value; |
| 94 | pl050_update(s, s->pending); |
| 95 | /* ??? Need to implement the enable/disable bit. */ |
| 96 | break; |
| 97 | case 2: /* KMIDATA */ |
| 98 | /* ??? This should toggle the TX interrupt line. */ |
| 99 | /* ??? This means kbd/mouse can block each other. */ |
| 100 | if (s->is_mouse) { |
| 101 | ps2_write_mouse(s->dev, value); |
| 102 | } else { |
| 103 | ps2_write_keyboard(s->dev, value); |
| 104 | } |
| 105 | break; |
| 106 | case 3: /* KMICLKDIV */ |
| 107 | s->clk = value; |
| 108 | return; |
| 109 | default: |
Paul Brook | 2ac7117 | 2009-05-08 02:35:15 +0100 | [diff] [blame] | 110 | hw_error("pl050_write: Bad offset %x\n", (int)offset); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 113 | static CPUReadMemoryFunc * const pl050_readfn[] = { |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 114 | pl050_read, |
| 115 | pl050_read, |
| 116 | pl050_read |
| 117 | }; |
| 118 | |
Blue Swirl | d60efc6 | 2009-08-25 18:29:31 +0000 | [diff] [blame] | 119 | static CPUWriteMemoryFunc * const pl050_writefn[] = { |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 120 | pl050_write, |
| 121 | pl050_write, |
| 122 | pl050_write |
| 123 | }; |
| 124 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 125 | static int pl050_init(SysBusDevice *dev, int is_mouse) |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 126 | { |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 127 | pl050_state *s = FROM_SYSBUS(pl050_state, dev); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 128 | int iomemtype; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 129 | |
Avi Kivity | 1eed09c | 2009-06-14 11:38:51 +0300 | [diff] [blame] | 130 | iomemtype = cpu_register_io_memory(pl050_readfn, |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 131 | pl050_writefn, s); |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 132 | sysbus_init_mmio(dev, 0x1000, iomemtype); |
| 133 | sysbus_init_irq(dev, &s->irq); |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 134 | s->is_mouse = is_mouse; |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 135 | if (s->is_mouse) |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 136 | s->dev = ps2_mouse_init(pl050_update, s); |
| 137 | else |
| 138 | s->dev = ps2_kbd_init(pl050_update, s); |
| 139 | /* ??? Save/restore. */ |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 140 | return 0; |
pbrook | cdbdb64 | 2006-04-09 01:32:52 +0000 | [diff] [blame] | 141 | } |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 142 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 143 | static int pl050_init_keyboard(SysBusDevice *dev) |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 144 | { |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 145 | return pl050_init(dev, 0); |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 148 | static int pl050_init_mouse(SysBusDevice *dev) |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 149 | { |
Gerd Hoffmann | 81a322d | 2009-08-14 10:36:05 +0200 | [diff] [blame] | 150 | return pl050_init(dev, 1); |
Paul Brook | 86394e9 | 2009-05-14 22:35:07 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static void pl050_register_devices(void) |
| 154 | { |
| 155 | sysbus_register_dev("pl050_keyboard", sizeof(pl050_state), |
| 156 | pl050_init_keyboard); |
| 157 | sysbus_register_dev("pl050_mouse", sizeof(pl050_state), |
| 158 | pl050_init_mouse); |
| 159 | } |
| 160 | |
| 161 | device_init(pl050_register_devices) |