blob: b155cc07b6e6b25fbbd597087c0739acc52ccd85 [file] [log] [blame]
ths5fafdf22007-09-16 21:08:06 +00001/*
pbrook69db0ac2006-07-17 18:45:34 +00002 * Arm PrimeCell PL050 Keyboard / Mouse Interface
pbrookcdbdb642006-04-09 01:32:52 +00003 *
pbrook9e61ec32007-04-30 02:39:55 +00004 * Copyright (c) 2006-2007 CodeSourcery.
pbrookcdbdb642006-04-09 01:32:52 +00005 * Written by Paul Brook
6 *
7 * This code is licenced under the GPL.
8 */
9
Paul Brook86394e92009-05-14 22:35:07 +010010#include "sysbus.h"
pbrook87ecb682007-11-17 17:14:51 +000011#include "ps2.h"
pbrookcdbdb642006-04-09 01:32:52 +000012
13typedef struct {
Paul Brook86394e92009-05-14 22:35:07 +010014 SysBusDevice busdev;
pbrookcdbdb642006-04-09 01:32:52 +000015 void *dev;
pbrookcdbdb642006-04-09 01:32:52 +000016 uint32_t cr;
17 uint32_t clk;
18 uint32_t last;
pbrookcdbdb642006-04-09 01:32:52 +000019 int pending;
pbrookd537cf62007-04-07 18:14:41 +000020 qemu_irq irq;
pbrookcdbdb642006-04-09 01:32:52 +000021 int is_mouse;
22} pl050_state;
23
Peter Maydelld6ac1722010-12-23 17:19:54 +000024static const VMStateDescription vmstate_pl050 = {
25 .name = "pl050",
26 .version_id = 1,
27 .minimum_version_id = 1,
28 .fields = (VMStateField[]) {
29 VMSTATE_UINT32(cr, pl050_state),
30 VMSTATE_UINT32(clk, pl050_state),
31 VMSTATE_UINT32(last, pl050_state),
32 VMSTATE_INT32(pending, pl050_state),
33 VMSTATE_INT32(is_mouse, pl050_state),
34 VMSTATE_END_OF_LIST()
35 }
36};
37
pbrook9e61ec32007-04-30 02:39:55 +000038#define PL050_TXEMPTY (1 << 6)
39#define PL050_TXBUSY (1 << 5)
40#define PL050_RXFULL (1 << 4)
41#define PL050_RXBUSY (1 << 3)
42#define PL050_RXPARITY (1 << 2)
43#define PL050_KMIC (1 << 1)
44#define PL050_KMID (1 << 0)
45
pbrookcdbdb642006-04-09 01:32:52 +000046static const unsigned char pl050_id[] =
47{ 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
48
49static void pl050_update(void *opaque, int level)
50{
51 pl050_state *s = (pl050_state *)opaque;
52 int raise;
53
54 s->pending = level;
55 raise = (s->pending && (s->cr & 0x10) != 0)
56 || (s->cr & 0x08) != 0;
pbrookd537cf62007-04-07 18:14:41 +000057 qemu_set_irq(s->irq, raise);
pbrookcdbdb642006-04-09 01:32:52 +000058}
59
Anthony Liguoric227f092009-10-01 16:12:16 -050060static uint32_t pl050_read(void *opaque, target_phys_addr_t offset)
pbrookcdbdb642006-04-09 01:32:52 +000061{
62 pl050_state *s = (pl050_state *)opaque;
pbrookcdbdb642006-04-09 01:32:52 +000063 if (offset >= 0xfe0 && offset < 0x1000)
64 return pl050_id[(offset - 0xfe0) >> 2];
65
66 switch (offset >> 2) {
67 case 0: /* KMICR */
68 return s->cr;
69 case 1: /* KMISTAT */
pbrook9e61ec32007-04-30 02:39:55 +000070 {
71 uint8_t val;
72 uint32_t stat;
73
74 val = s->last;
75 val = val ^ (val >> 4);
76 val = val ^ (val >> 2);
77 val = (val ^ (val >> 1)) & 1;
78
79 stat = PL050_TXEMPTY;
80 if (val)
81 stat |= PL050_RXPARITY;
82 if (s->pending)
83 stat |= PL050_RXFULL;
84
85 return stat;
pbrookcdbdb642006-04-09 01:32:52 +000086 }
87 case 2: /* KMIDATA */
88 if (s->pending)
89 s->last = ps2_read_data(s->dev);
90 return s->last;
91 case 3: /* KMICLKDIV */
92 return s->clk;
93 case 4: /* KMIIR */
94 return s->pending | 2;
95 default:
Paul Brook2ac71172009-05-08 02:35:15 +010096 hw_error("pl050_read: Bad offset %x\n", (int)offset);
pbrookcdbdb642006-04-09 01:32:52 +000097 return 0;
98 }
99}
100
Anthony Liguoric227f092009-10-01 16:12:16 -0500101static void pl050_write(void *opaque, target_phys_addr_t offset,
pbrookcdbdb642006-04-09 01:32:52 +0000102 uint32_t value)
103{
104 pl050_state *s = (pl050_state *)opaque;
pbrookcdbdb642006-04-09 01:32:52 +0000105 switch (offset >> 2) {
106 case 0: /* KMICR */
107 s->cr = value;
108 pl050_update(s, s->pending);
109 /* ??? Need to implement the enable/disable bit. */
110 break;
111 case 2: /* KMIDATA */
112 /* ??? This should toggle the TX interrupt line. */
113 /* ??? This means kbd/mouse can block each other. */
114 if (s->is_mouse) {
115 ps2_write_mouse(s->dev, value);
116 } else {
117 ps2_write_keyboard(s->dev, value);
118 }
119 break;
120 case 3: /* KMICLKDIV */
121 s->clk = value;
122 return;
123 default:
Paul Brook2ac71172009-05-08 02:35:15 +0100124 hw_error("pl050_write: Bad offset %x\n", (int)offset);
pbrookcdbdb642006-04-09 01:32:52 +0000125 }
126}
Blue Swirld60efc62009-08-25 18:29:31 +0000127static CPUReadMemoryFunc * const pl050_readfn[] = {
pbrookcdbdb642006-04-09 01:32:52 +0000128 pl050_read,
129 pl050_read,
130 pl050_read
131};
132
Blue Swirld60efc62009-08-25 18:29:31 +0000133static CPUWriteMemoryFunc * const pl050_writefn[] = {
pbrookcdbdb642006-04-09 01:32:52 +0000134 pl050_write,
135 pl050_write,
136 pl050_write
137};
138
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200139static int pl050_init(SysBusDevice *dev, int is_mouse)
pbrookcdbdb642006-04-09 01:32:52 +0000140{
Paul Brook86394e92009-05-14 22:35:07 +0100141 pl050_state *s = FROM_SYSBUS(pl050_state, dev);
pbrookcdbdb642006-04-09 01:32:52 +0000142 int iomemtype;
pbrookcdbdb642006-04-09 01:32:52 +0000143
Avi Kivity1eed09c2009-06-14 11:38:51 +0300144 iomemtype = cpu_register_io_memory(pl050_readfn,
Alexander Graf2507c122010-12-08 12:05:37 +0100145 pl050_writefn, s,
146 DEVICE_NATIVE_ENDIAN);
Paul Brook86394e92009-05-14 22:35:07 +0100147 sysbus_init_mmio(dev, 0x1000, iomemtype);
148 sysbus_init_irq(dev, &s->irq);
pbrookcdbdb642006-04-09 01:32:52 +0000149 s->is_mouse = is_mouse;
Paul Brook86394e92009-05-14 22:35:07 +0100150 if (s->is_mouse)
pbrookcdbdb642006-04-09 01:32:52 +0000151 s->dev = ps2_mouse_init(pl050_update, s);
152 else
153 s->dev = ps2_kbd_init(pl050_update, s);
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200154 return 0;
pbrookcdbdb642006-04-09 01:32:52 +0000155}
Paul Brook86394e92009-05-14 22:35:07 +0100156
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200157static int pl050_init_keyboard(SysBusDevice *dev)
Paul Brook86394e92009-05-14 22:35:07 +0100158{
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200159 return pl050_init(dev, 0);
Paul Brook86394e92009-05-14 22:35:07 +0100160}
161
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200162static int pl050_init_mouse(SysBusDevice *dev)
Paul Brook86394e92009-05-14 22:35:07 +0100163{
Gerd Hoffmann81a322d2009-08-14 10:36:05 +0200164 return pl050_init(dev, 1);
Paul Brook86394e92009-05-14 22:35:07 +0100165}
166
Peter Maydelld6ac1722010-12-23 17:19:54 +0000167static SysBusDeviceInfo pl050_kbd_info = {
168 .init = pl050_init_keyboard,
169 .qdev.name = "pl050_keyboard",
170 .qdev.size = sizeof(pl050_state),
171 .qdev.vmsd = &vmstate_pl050,
172};
173
174static SysBusDeviceInfo pl050_mouse_info = {
175 .init = pl050_init_mouse,
176 .qdev.name = "pl050_mouse",
177 .qdev.size = sizeof(pl050_state),
178 .qdev.vmsd = &vmstate_pl050,
179};
180
Paul Brook86394e92009-05-14 22:35:07 +0100181static void pl050_register_devices(void)
182{
Peter Maydelld6ac1722010-12-23 17:19:54 +0000183 sysbus_register_withprop(&pl050_kbd_info);
184 sysbus_register_withprop(&pl050_mouse_info);
Paul Brook86394e92009-05-14 22:35:07 +0100185}
186
187device_init(pl050_register_devices)