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