root / hw / pl050.c @ caa88be0
History | View | Annotate | Download (3.6 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 | 87ecb68b | pbrook | #include "hw.h" |
11 | 87ecb68b | pbrook | #include "primecell.h" |
12 | 87ecb68b | pbrook | #include "ps2.h" |
13 | cdbdb648 | pbrook | |
14 | cdbdb648 | pbrook | typedef struct { |
15 | cdbdb648 | pbrook | void *dev;
|
16 | cdbdb648 | pbrook | uint32_t base; |
17 | cdbdb648 | pbrook | uint32_t cr; |
18 | cdbdb648 | pbrook | uint32_t clk; |
19 | cdbdb648 | pbrook | uint32_t last; |
20 | cdbdb648 | pbrook | int pending;
|
21 | d537cf6c | pbrook | qemu_irq irq; |
22 | cdbdb648 | pbrook | int is_mouse;
|
23 | cdbdb648 | pbrook | } pl050_state; |
24 | cdbdb648 | pbrook | |
25 | 9e61ec31 | pbrook | #define PL050_TXEMPTY (1 << 6) |
26 | 9e61ec31 | pbrook | #define PL050_TXBUSY (1 << 5) |
27 | 9e61ec31 | pbrook | #define PL050_RXFULL (1 << 4) |
28 | 9e61ec31 | pbrook | #define PL050_RXBUSY (1 << 3) |
29 | 9e61ec31 | pbrook | #define PL050_RXPARITY (1 << 2) |
30 | 9e61ec31 | pbrook | #define PL050_KMIC (1 << 1) |
31 | 9e61ec31 | pbrook | #define PL050_KMID (1 << 0) |
32 | 9e61ec31 | pbrook | |
33 | cdbdb648 | pbrook | static const unsigned char pl050_id[] = |
34 | cdbdb648 | pbrook | { 0x50, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 }; |
35 | cdbdb648 | pbrook | |
36 | cdbdb648 | pbrook | static void pl050_update(void *opaque, int level) |
37 | cdbdb648 | pbrook | { |
38 | cdbdb648 | pbrook | pl050_state *s = (pl050_state *)opaque; |
39 | cdbdb648 | pbrook | int raise;
|
40 | cdbdb648 | pbrook | |
41 | cdbdb648 | pbrook | s->pending = level; |
42 | cdbdb648 | pbrook | raise = (s->pending && (s->cr & 0x10) != 0) |
43 | cdbdb648 | pbrook | || (s->cr & 0x08) != 0; |
44 | d537cf6c | pbrook | qemu_set_irq(s->irq, raise); |
45 | cdbdb648 | pbrook | } |
46 | cdbdb648 | pbrook | |
47 | cdbdb648 | pbrook | static uint32_t pl050_read(void *opaque, target_phys_addr_t offset) |
48 | cdbdb648 | pbrook | { |
49 | cdbdb648 | pbrook | pl050_state *s = (pl050_state *)opaque; |
50 | cdbdb648 | pbrook | offset -= s->base; |
51 | cdbdb648 | pbrook | if (offset >= 0xfe0 && offset < 0x1000) |
52 | cdbdb648 | pbrook | return pl050_id[(offset - 0xfe0) >> 2]; |
53 | cdbdb648 | pbrook | |
54 | cdbdb648 | pbrook | switch (offset >> 2) { |
55 | cdbdb648 | pbrook | case 0: /* KMICR */ |
56 | cdbdb648 | pbrook | return s->cr;
|
57 | cdbdb648 | pbrook | case 1: /* KMISTAT */ |
58 | 9e61ec31 | pbrook | { |
59 | 9e61ec31 | pbrook | uint8_t val; |
60 | 9e61ec31 | pbrook | uint32_t stat; |
61 | 9e61ec31 | pbrook | |
62 | 9e61ec31 | pbrook | val = s->last; |
63 | 9e61ec31 | pbrook | val = val ^ (val >> 4);
|
64 | 9e61ec31 | pbrook | val = val ^ (val >> 2);
|
65 | 9e61ec31 | pbrook | val = (val ^ (val >> 1)) & 1; |
66 | 9e61ec31 | pbrook | |
67 | 9e61ec31 | pbrook | stat = PL050_TXEMPTY; |
68 | 9e61ec31 | pbrook | if (val)
|
69 | 9e61ec31 | pbrook | stat |= PL050_RXPARITY; |
70 | 9e61ec31 | pbrook | if (s->pending)
|
71 | 9e61ec31 | pbrook | stat |= PL050_RXFULL; |
72 | 9e61ec31 | pbrook | |
73 | 9e61ec31 | pbrook | return stat;
|
74 | cdbdb648 | pbrook | } |
75 | cdbdb648 | pbrook | case 2: /* KMIDATA */ |
76 | cdbdb648 | pbrook | if (s->pending)
|
77 | cdbdb648 | pbrook | s->last = ps2_read_data(s->dev); |
78 | cdbdb648 | pbrook | return s->last;
|
79 | cdbdb648 | pbrook | case 3: /* KMICLKDIV */ |
80 | cdbdb648 | pbrook | return s->clk;
|
81 | cdbdb648 | pbrook | case 4: /* KMIIR */ |
82 | cdbdb648 | pbrook | return s->pending | 2; |
83 | cdbdb648 | pbrook | default:
|
84 | 4d1165fa | pbrook | cpu_abort (cpu_single_env, "pl050_read: Bad offset %x\n", (int)offset); |
85 | cdbdb648 | pbrook | return 0; |
86 | cdbdb648 | pbrook | } |
87 | cdbdb648 | pbrook | } |
88 | cdbdb648 | pbrook | |
89 | cdbdb648 | pbrook | static void pl050_write(void *opaque, target_phys_addr_t offset, |
90 | cdbdb648 | pbrook | uint32_t value) |
91 | cdbdb648 | pbrook | { |
92 | cdbdb648 | pbrook | pl050_state *s = (pl050_state *)opaque; |
93 | cdbdb648 | pbrook | offset -= s->base; |
94 | cdbdb648 | pbrook | switch (offset >> 2) { |
95 | cdbdb648 | pbrook | case 0: /* KMICR */ |
96 | cdbdb648 | pbrook | s->cr = value; |
97 | cdbdb648 | pbrook | pl050_update(s, s->pending); |
98 | cdbdb648 | pbrook | /* ??? Need to implement the enable/disable bit. */
|
99 | cdbdb648 | pbrook | break;
|
100 | cdbdb648 | pbrook | case 2: /* KMIDATA */ |
101 | cdbdb648 | pbrook | /* ??? This should toggle the TX interrupt line. */
|
102 | cdbdb648 | pbrook | /* ??? This means kbd/mouse can block each other. */
|
103 | cdbdb648 | pbrook | if (s->is_mouse) {
|
104 | cdbdb648 | pbrook | ps2_write_mouse(s->dev, value); |
105 | cdbdb648 | pbrook | } else {
|
106 | cdbdb648 | pbrook | ps2_write_keyboard(s->dev, value); |
107 | cdbdb648 | pbrook | } |
108 | cdbdb648 | pbrook | break;
|
109 | cdbdb648 | pbrook | case 3: /* KMICLKDIV */ |
110 | cdbdb648 | pbrook | s->clk = value; |
111 | cdbdb648 | pbrook | return;
|
112 | cdbdb648 | pbrook | default:
|
113 | 4d1165fa | pbrook | cpu_abort (cpu_single_env, "pl050_write: Bad offset %x\n", (int)offset); |
114 | cdbdb648 | pbrook | } |
115 | cdbdb648 | pbrook | } |
116 | cdbdb648 | pbrook | static CPUReadMemoryFunc *pl050_readfn[] = {
|
117 | cdbdb648 | pbrook | pl050_read, |
118 | cdbdb648 | pbrook | pl050_read, |
119 | cdbdb648 | pbrook | pl050_read |
120 | cdbdb648 | pbrook | }; |
121 | cdbdb648 | pbrook | |
122 | cdbdb648 | pbrook | static CPUWriteMemoryFunc *pl050_writefn[] = {
|
123 | cdbdb648 | pbrook | pl050_write, |
124 | cdbdb648 | pbrook | pl050_write, |
125 | cdbdb648 | pbrook | pl050_write |
126 | cdbdb648 | pbrook | }; |
127 | cdbdb648 | pbrook | |
128 | d537cf6c | pbrook | void pl050_init(uint32_t base, qemu_irq irq, int is_mouse) |
129 | cdbdb648 | pbrook | { |
130 | cdbdb648 | pbrook | int iomemtype;
|
131 | cdbdb648 | pbrook | pl050_state *s; |
132 | cdbdb648 | pbrook | |
133 | cdbdb648 | pbrook | s = (pl050_state *)qemu_mallocz(sizeof(pl050_state));
|
134 | cdbdb648 | pbrook | iomemtype = cpu_register_io_memory(0, pl050_readfn,
|
135 | cdbdb648 | pbrook | pl050_writefn, s); |
136 | 187337f8 | pbrook | cpu_register_physical_memory(base, 0x00001000, iomemtype);
|
137 | cdbdb648 | pbrook | s->base = base; |
138 | cdbdb648 | pbrook | s->irq = irq; |
139 | cdbdb648 | pbrook | s->is_mouse = is_mouse; |
140 | cdbdb648 | pbrook | if (is_mouse)
|
141 | cdbdb648 | pbrook | s->dev = ps2_mouse_init(pl050_update, s); |
142 | cdbdb648 | pbrook | else
|
143 | cdbdb648 | pbrook | s->dev = ps2_kbd_init(pl050_update, s); |
144 | cdbdb648 | pbrook | /* ??? Save/restore. */
|
145 | cdbdb648 | pbrook | } |