]> git.proxmox.com Git - qemu.git/blame - hw/pxa2xx_gpio.c
Merge remote branch 'kwolf/for-anthony' into staging
[qemu.git] / hw / pxa2xx_gpio.c
CommitLineData
c1713132
AZ
1/*
2 * Intel XScale PXA255/270 GPIO controller emulation.
3 *
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
6 *
7 * This code is licensed under the GPL.
8 */
9
87ecb68b
PB
10#include "hw.h"
11#include "pxa.h"
c1713132
AZ
12
13#define PXA2XX_GPIO_BANKS 4
14
bc24a225 15struct PXA2xxGPIOInfo {
c1713132
AZ
16 qemu_irq *pic;
17 int lines;
18 CPUState *cpu_env;
38641a52 19 qemu_irq *in;
c1713132
AZ
20
21 /* XXX: GNU C vectors are more suitable */
22 uint32_t ilevel[PXA2XX_GPIO_BANKS];
23 uint32_t olevel[PXA2XX_GPIO_BANKS];
24 uint32_t dir[PXA2XX_GPIO_BANKS];
25 uint32_t rising[PXA2XX_GPIO_BANKS];
26 uint32_t falling[PXA2XX_GPIO_BANKS];
27 uint32_t status[PXA2XX_GPIO_BANKS];
2b76bdc9 28 uint32_t gpsr[PXA2XX_GPIO_BANKS];
c1713132
AZ
29 uint32_t gafr[PXA2XX_GPIO_BANKS * 2];
30
31 uint32_t prev_level[PXA2XX_GPIO_BANKS];
38641a52
AZ
32 qemu_irq handler[PXA2XX_GPIO_BANKS * 32];
33 qemu_irq read_notify;
c1713132
AZ
34};
35
36static struct {
37 enum {
38 GPIO_NONE,
39 GPLR,
40 GPSR,
41 GPCR,
42 GPDR,
43 GRER,
44 GFER,
45 GEDR,
46 GAFR_L,
47 GAFR_U,
48 } reg;
49 int bank;
50} pxa2xx_gpio_regs[0x200] = {
51 [0 ... 0x1ff] = { GPIO_NONE, 0 },
52#define PXA2XX_REG(reg, a0, a1, a2, a3) \
5fafdf24 53 [a0] = { reg, 0 }, [a1] = { reg, 1 }, [a2] = { reg, 2 }, [a3] = { reg, 3 },
c1713132
AZ
54
55 PXA2XX_REG(GPLR, 0x000, 0x004, 0x008, 0x100)
56 PXA2XX_REG(GPSR, 0x018, 0x01c, 0x020, 0x118)
57 PXA2XX_REG(GPCR, 0x024, 0x028, 0x02c, 0x124)
58 PXA2XX_REG(GPDR, 0x00c, 0x010, 0x014, 0x10c)
59 PXA2XX_REG(GRER, 0x030, 0x034, 0x038, 0x130)
60 PXA2XX_REG(GFER, 0x03c, 0x040, 0x044, 0x13c)
61 PXA2XX_REG(GEDR, 0x048, 0x04c, 0x050, 0x148)
62 PXA2XX_REG(GAFR_L, 0x054, 0x05c, 0x064, 0x06c)
63 PXA2XX_REG(GAFR_U, 0x058, 0x060, 0x068, 0x070)
64};
65
bc24a225 66static void pxa2xx_gpio_irq_update(PXA2xxGPIOInfo *s)
c1713132
AZ
67{
68 if (s->status[0] & (1 << 0))
69 qemu_irq_raise(s->pic[PXA2XX_PIC_GPIO_0]);
70 else
71 qemu_irq_lower(s->pic[PXA2XX_PIC_GPIO_0]);
72
73 if (s->status[0] & (1 << 1))
74 qemu_irq_raise(s->pic[PXA2XX_PIC_GPIO_1]);
75 else
76 qemu_irq_lower(s->pic[PXA2XX_PIC_GPIO_1]);
77
78 if ((s->status[0] & ~3) | s->status[1] | s->status[2] | s->status[3])
79 qemu_irq_raise(s->pic[PXA2XX_PIC_GPIO_X]);
80 else
81 qemu_irq_lower(s->pic[PXA2XX_PIC_GPIO_X]);
82}
83
84/* Bitmap of pins used as standby and sleep wake-up sources. */
38641a52 85static const int pxa2xx_gpio_wake[PXA2XX_GPIO_BANKS] = {
c1713132
AZ
86 0x8003fe1b, 0x002001fc, 0xec080000, 0x0012007f,
87};
88
38641a52 89static void pxa2xx_gpio_set(void *opaque, int line, int level)
c1713132 90{
bc24a225 91 PXA2xxGPIOInfo *s = (PXA2xxGPIOInfo *) opaque;
c1713132
AZ
92 int bank;
93 uint32_t mask;
94
95 if (line >= s->lines) {
96 printf("%s: No GPIO pin %i\n", __FUNCTION__, line);
97 return;
98 }
99
100 bank = line >> 5;
101 mask = 1 << (line & 31);
102
103 if (level) {
104 s->status[bank] |= s->rising[bank] & mask &
105 ~s->ilevel[bank] & ~s->dir[bank];
106 s->ilevel[bank] |= mask;
107 } else {
108 s->status[bank] |= s->falling[bank] & mask &
109 s->ilevel[bank] & ~s->dir[bank];
110 s->ilevel[bank] &= ~mask;
111 }
112
113 if (s->status[bank] & mask)
114 pxa2xx_gpio_irq_update(s);
115
116 /* Wake-up GPIOs */
117 if (s->cpu_env->halted && (mask & ~s->dir[bank] & pxa2xx_gpio_wake[bank]))
118 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_EXITTB);
119}
120
bc24a225 121static void pxa2xx_gpio_handler_update(PXA2xxGPIOInfo *s) {
c1713132
AZ
122 uint32_t level, diff;
123 int i, bit, line;
124 for (i = 0; i < PXA2XX_GPIO_BANKS; i ++) {
125 level = s->olevel[i] & s->dir[i];
126
127 for (diff = s->prev_level[i] ^ level; diff; diff ^= 1 << bit) {
128 bit = ffs(diff) - 1;
129 line = bit + 32 * i;
38641a52 130 qemu_set_irq(s->handler[line], (level >> bit) & 1);
c1713132
AZ
131 }
132
133 s->prev_level[i] = level;
134 }
135}
136
c227f099 137static uint32_t pxa2xx_gpio_read(void *opaque, target_phys_addr_t offset)
c1713132 138{
bc24a225 139 PXA2xxGPIOInfo *s = (PXA2xxGPIOInfo *) opaque;
c1713132
AZ
140 uint32_t ret;
141 int bank;
c1713132
AZ
142 if (offset >= 0x200)
143 return 0;
144
145 bank = pxa2xx_gpio_regs[offset].bank;
146 switch (pxa2xx_gpio_regs[offset].reg) {
147 case GPDR: /* GPIO Pin-Direction registers */
148 return s->dir[bank];
149
2b76bdc9
AZ
150 case GPSR: /* GPIO Pin-Output Set registers */
151 printf("%s: Read from a write-only register " REG_FMT "\n",
152 __FUNCTION__, offset);
153 return s->gpsr[bank]; /* Return last written value. */
154
e1dad5a6
AZ
155 case GPCR: /* GPIO Pin-Output Clear registers */
156 printf("%s: Read from a write-only register " REG_FMT "\n",
157 __FUNCTION__, offset);
158 return 31337; /* Specified as unpredictable in the docs. */
159
c1713132
AZ
160 case GRER: /* GPIO Rising-Edge Detect Enable registers */
161 return s->rising[bank];
162
163 case GFER: /* GPIO Falling-Edge Detect Enable registers */
164 return s->falling[bank];
165
166 case GAFR_L: /* GPIO Alternate Function registers */
167 return s->gafr[bank * 2];
168
169 case GAFR_U: /* GPIO Alternate Function registers */
170 return s->gafr[bank * 2 + 1];
171
172 case GPLR: /* GPIO Pin-Level registers */
173 ret = (s->olevel[bank] & s->dir[bank]) |
174 (s->ilevel[bank] & ~s->dir[bank]);
38641a52 175 qemu_irq_raise(s->read_notify);
c1713132
AZ
176 return ret;
177
178 case GEDR: /* GPIO Edge Detect Status registers */
179 return s->status[bank];
180
181 default:
2ac71179 182 hw_error("%s: Bad offset " REG_FMT "\n", __FUNCTION__, offset);
c1713132
AZ
183 }
184
185 return 0;
186}
187
188static void pxa2xx_gpio_write(void *opaque,
c227f099 189 target_phys_addr_t offset, uint32_t value)
c1713132 190{
bc24a225 191 PXA2xxGPIOInfo *s = (PXA2xxGPIOInfo *) opaque;
c1713132 192 int bank;
c1713132
AZ
193 if (offset >= 0x200)
194 return;
195
196 bank = pxa2xx_gpio_regs[offset].bank;
197 switch (pxa2xx_gpio_regs[offset].reg) {
198 case GPDR: /* GPIO Pin-Direction registers */
199 s->dir[bank] = value;
200 pxa2xx_gpio_handler_update(s);
201 break;
202
203 case GPSR: /* GPIO Pin-Output Set registers */
204 s->olevel[bank] |= value;
205 pxa2xx_gpio_handler_update(s);
2b76bdc9 206 s->gpsr[bank] = value;
c1713132
AZ
207 break;
208
209 case GPCR: /* GPIO Pin-Output Clear registers */
210 s->olevel[bank] &= ~value;
211 pxa2xx_gpio_handler_update(s);
212 break;
213
214 case GRER: /* GPIO Rising-Edge Detect Enable registers */
215 s->rising[bank] = value;
216 break;
217
218 case GFER: /* GPIO Falling-Edge Detect Enable registers */
219 s->falling[bank] = value;
220 break;
221
222 case GAFR_L: /* GPIO Alternate Function registers */
223 s->gafr[bank * 2] = value;
224 break;
225
226 case GAFR_U: /* GPIO Alternate Function registers */
227 s->gafr[bank * 2 + 1] = value;
228 break;
229
230 case GEDR: /* GPIO Edge Detect Status registers */
231 s->status[bank] &= ~value;
232 pxa2xx_gpio_irq_update(s);
233 break;
234
235 default:
2ac71179 236 hw_error("%s: Bad offset " REG_FMT "\n", __FUNCTION__, offset);
c1713132
AZ
237 }
238}
239
d60efc6b 240static CPUReadMemoryFunc * const pxa2xx_gpio_readfn[] = {
c1713132
AZ
241 pxa2xx_gpio_read,
242 pxa2xx_gpio_read,
243 pxa2xx_gpio_read
244};
245
d60efc6b 246static CPUWriteMemoryFunc * const pxa2xx_gpio_writefn[] = {
c1713132
AZ
247 pxa2xx_gpio_write,
248 pxa2xx_gpio_write,
249 pxa2xx_gpio_write
250};
251
aa941b94
AZ
252static void pxa2xx_gpio_save(QEMUFile *f, void *opaque)
253{
bc24a225 254 PXA2xxGPIOInfo *s = (PXA2xxGPIOInfo *) opaque;
aa941b94
AZ
255 int i;
256
257 qemu_put_be32(f, s->lines);
258
259 for (i = 0; i < PXA2XX_GPIO_BANKS; i ++) {
260 qemu_put_be32s(f, &s->ilevel[i]);
261 qemu_put_be32s(f, &s->olevel[i]);
262 qemu_put_be32s(f, &s->dir[i]);
263 qemu_put_be32s(f, &s->rising[i]);
264 qemu_put_be32s(f, &s->falling[i]);
265 qemu_put_be32s(f, &s->status[i]);
266 qemu_put_be32s(f, &s->gafr[i * 2 + 0]);
267 qemu_put_be32s(f, &s->gafr[i * 2 + 1]);
268
269 qemu_put_be32s(f, &s->prev_level[i]);
270 }
271}
272
273static int pxa2xx_gpio_load(QEMUFile *f, void *opaque, int version_id)
274{
bc24a225 275 PXA2xxGPIOInfo *s = (PXA2xxGPIOInfo *) opaque;
aa941b94
AZ
276 int i;
277
278 if (qemu_get_be32(f) != s->lines)
279 return -EINVAL;
280
281 for (i = 0; i < PXA2XX_GPIO_BANKS; i ++) {
282 qemu_get_be32s(f, &s->ilevel[i]);
283 qemu_get_be32s(f, &s->olevel[i]);
284 qemu_get_be32s(f, &s->dir[i]);
285 qemu_get_be32s(f, &s->rising[i]);
286 qemu_get_be32s(f, &s->falling[i]);
287 qemu_get_be32s(f, &s->status[i]);
288 qemu_get_be32s(f, &s->gafr[i * 2 + 0]);
289 qemu_get_be32s(f, &s->gafr[i * 2 + 1]);
290
291 qemu_get_be32s(f, &s->prev_level[i]);
292 }
293
294 return 0;
295}
296
c227f099 297PXA2xxGPIOInfo *pxa2xx_gpio_init(target_phys_addr_t base,
c1713132
AZ
298 CPUState *env, qemu_irq *pic, int lines)
299{
300 int iomemtype;
bc24a225 301 PXA2xxGPIOInfo *s;
c1713132 302
bc24a225
PB
303 s = (PXA2xxGPIOInfo *)
304 qemu_mallocz(sizeof(PXA2xxGPIOInfo));
305 memset(s, 0, sizeof(PXA2xxGPIOInfo));
c1713132
AZ
306 s->pic = pic;
307 s->lines = lines;
308 s->cpu_env = env;
38641a52 309 s->in = qemu_allocate_irqs(pxa2xx_gpio_set, s, lines);
c1713132 310
1eed09cb 311 iomemtype = cpu_register_io_memory(pxa2xx_gpio_readfn,
c1713132 312 pxa2xx_gpio_writefn, s);
187337f8 313 cpu_register_physical_memory(base, 0x00001000, iomemtype);
c1713132 314
0be71e32 315 register_savevm(NULL, "pxa2xx_gpio", 0, 0,
aa941b94
AZ
316 pxa2xx_gpio_save, pxa2xx_gpio_load, s);
317
c1713132
AZ
318 return s;
319}
320
bc24a225 321qemu_irq *pxa2xx_gpio_in_get(PXA2xxGPIOInfo *s)
38641a52
AZ
322{
323 return s->in;
324}
325
bc24a225 326void pxa2xx_gpio_out_set(PXA2xxGPIOInfo *s,
38641a52
AZ
327 int line, qemu_irq handler)
328{
c1713132
AZ
329 if (line >= s->lines) {
330 printf("%s: No GPIO pin %i\n", __FUNCTION__, line);
331 return;
332 }
333
38641a52 334 s->handler[line] = handler;
c1713132
AZ
335}
336
337/*
338 * Registers a callback to notify on GPLR reads. This normally
339 * shouldn't be needed but it is used for the hack on Spitz machines.
340 */
bc24a225 341void pxa2xx_gpio_read_notifier(PXA2xxGPIOInfo *s, qemu_irq handler)
38641a52 342{
c1713132 343 s->read_notify = handler;
c1713132 344}