move vga_io_address to VGA State (Glauber Costa)
[qemu/qemu-JZ.git] / hw / pxa2xx_gpio.c
blobe3a30bc45e336e814079a51e16cb84a9e47b13d8
1 /*
2 * Intel XScale PXA255/270 GPIO controller emulation.
4 * Copyright (c) 2006 Openedhand Ltd.
5 * Written by Andrzej Zaborowski <balrog@zabor.org>
7 * This code is licensed under the GPL.
8 */
10 #include "hw.h"
11 #include "pxa.h"
13 #define PXA2XX_GPIO_BANKS 4
15 struct pxa2xx_gpio_info_s {
16 target_phys_addr_t base;
17 qemu_irq *pic;
18 int lines;
19 CPUState *cpu_env;
20 qemu_irq *in;
22 /* XXX: GNU C vectors are more suitable */
23 uint32_t ilevel[PXA2XX_GPIO_BANKS];
24 uint32_t olevel[PXA2XX_GPIO_BANKS];
25 uint32_t dir[PXA2XX_GPIO_BANKS];
26 uint32_t rising[PXA2XX_GPIO_BANKS];
27 uint32_t falling[PXA2XX_GPIO_BANKS];
28 uint32_t status[PXA2XX_GPIO_BANKS];
29 uint32_t gpsr[PXA2XX_GPIO_BANKS];
30 uint32_t gafr[PXA2XX_GPIO_BANKS * 2];
32 uint32_t prev_level[PXA2XX_GPIO_BANKS];
33 qemu_irq handler[PXA2XX_GPIO_BANKS * 32];
34 qemu_irq read_notify;
37 static struct {
38 enum {
39 GPIO_NONE,
40 GPLR,
41 GPSR,
42 GPCR,
43 GPDR,
44 GRER,
45 GFER,
46 GEDR,
47 GAFR_L,
48 GAFR_U,
49 } reg;
50 int bank;
51 } pxa2xx_gpio_regs[0x200] = {
52 [0 ... 0x1ff] = { GPIO_NONE, 0 },
53 #define PXA2XX_REG(reg, a0, a1, a2, a3) \
54 [a0] = { reg, 0 }, [a1] = { reg, 1 }, [a2] = { reg, 2 }, [a3] = { reg, 3 },
56 PXA2XX_REG(GPLR, 0x000, 0x004, 0x008, 0x100)
57 PXA2XX_REG(GPSR, 0x018, 0x01c, 0x020, 0x118)
58 PXA2XX_REG(GPCR, 0x024, 0x028, 0x02c, 0x124)
59 PXA2XX_REG(GPDR, 0x00c, 0x010, 0x014, 0x10c)
60 PXA2XX_REG(GRER, 0x030, 0x034, 0x038, 0x130)
61 PXA2XX_REG(GFER, 0x03c, 0x040, 0x044, 0x13c)
62 PXA2XX_REG(GEDR, 0x048, 0x04c, 0x050, 0x148)
63 PXA2XX_REG(GAFR_L, 0x054, 0x05c, 0x064, 0x06c)
64 PXA2XX_REG(GAFR_U, 0x058, 0x060, 0x068, 0x070)
67 static void pxa2xx_gpio_irq_update(struct pxa2xx_gpio_info_s *s)
69 if (s->status[0] & (1 << 0))
70 qemu_irq_raise(s->pic[PXA2XX_PIC_GPIO_0]);
71 else
72 qemu_irq_lower(s->pic[PXA2XX_PIC_GPIO_0]);
74 if (s->status[0] & (1 << 1))
75 qemu_irq_raise(s->pic[PXA2XX_PIC_GPIO_1]);
76 else
77 qemu_irq_lower(s->pic[PXA2XX_PIC_GPIO_1]);
79 if ((s->status[0] & ~3) | s->status[1] | s->status[2] | s->status[3])
80 qemu_irq_raise(s->pic[PXA2XX_PIC_GPIO_X]);
81 else
82 qemu_irq_lower(s->pic[PXA2XX_PIC_GPIO_X]);
85 /* Bitmap of pins used as standby and sleep wake-up sources. */
86 static const int pxa2xx_gpio_wake[PXA2XX_GPIO_BANKS] = {
87 0x8003fe1b, 0x002001fc, 0xec080000, 0x0012007f,
90 static void pxa2xx_gpio_set(void *opaque, int line, int level)
92 struct pxa2xx_gpio_info_s *s = (struct pxa2xx_gpio_info_s *) opaque;
93 int bank;
94 uint32_t mask;
96 if (line >= s->lines) {
97 printf("%s: No GPIO pin %i\n", __FUNCTION__, line);
98 return;
101 bank = line >> 5;
102 mask = 1 << (line & 31);
104 if (level) {
105 s->status[bank] |= s->rising[bank] & mask &
106 ~s->ilevel[bank] & ~s->dir[bank];
107 s->ilevel[bank] |= mask;
108 } else {
109 s->status[bank] |= s->falling[bank] & mask &
110 s->ilevel[bank] & ~s->dir[bank];
111 s->ilevel[bank] &= ~mask;
114 if (s->status[bank] & mask)
115 pxa2xx_gpio_irq_update(s);
117 /* Wake-up GPIOs */
118 if (s->cpu_env->halted && (mask & ~s->dir[bank] & pxa2xx_gpio_wake[bank]))
119 cpu_interrupt(s->cpu_env, CPU_INTERRUPT_EXITTB);
122 static void pxa2xx_gpio_handler_update(struct pxa2xx_gpio_info_s *s) {
123 uint32_t level, diff;
124 int i, bit, line;
125 for (i = 0; i < PXA2XX_GPIO_BANKS; i ++) {
126 level = s->olevel[i] & s->dir[i];
128 for (diff = s->prev_level[i] ^ level; diff; diff ^= 1 << bit) {
129 bit = ffs(diff) - 1;
130 line = bit + 32 * i;
131 qemu_set_irq(s->handler[line], (level >> bit) & 1);
134 s->prev_level[i] = level;
138 static uint32_t pxa2xx_gpio_read(void *opaque, target_phys_addr_t offset)
140 struct pxa2xx_gpio_info_s *s = (struct pxa2xx_gpio_info_s *) opaque;
141 uint32_t ret;
142 int bank;
143 offset -= s->base;
144 if (offset >= 0x200)
145 return 0;
147 bank = pxa2xx_gpio_regs[offset].bank;
148 switch (pxa2xx_gpio_regs[offset].reg) {
149 case GPDR: /* GPIO Pin-Direction registers */
150 return s->dir[bank];
152 case GPSR: /* GPIO Pin-Output Set registers */
153 printf("%s: Read from a write-only register " REG_FMT "\n",
154 __FUNCTION__, offset);
155 return s->gpsr[bank]; /* Return last written value. */
157 case GPCR: /* GPIO Pin-Output Clear registers */
158 printf("%s: Read from a write-only register " REG_FMT "\n",
159 __FUNCTION__, offset);
160 return 31337; /* Specified as unpredictable in the docs. */
162 case GRER: /* GPIO Rising-Edge Detect Enable registers */
163 return s->rising[bank];
165 case GFER: /* GPIO Falling-Edge Detect Enable registers */
166 return s->falling[bank];
168 case GAFR_L: /* GPIO Alternate Function registers */
169 return s->gafr[bank * 2];
171 case GAFR_U: /* GPIO Alternate Function registers */
172 return s->gafr[bank * 2 + 1];
174 case GPLR: /* GPIO Pin-Level registers */
175 ret = (s->olevel[bank] & s->dir[bank]) |
176 (s->ilevel[bank] & ~s->dir[bank]);
177 qemu_irq_raise(s->read_notify);
178 return ret;
180 case GEDR: /* GPIO Edge Detect Status registers */
181 return s->status[bank];
183 default:
184 cpu_abort(cpu_single_env,
185 "%s: Bad offset " REG_FMT "\n", __FUNCTION__, offset);
188 return 0;
191 static void pxa2xx_gpio_write(void *opaque,
192 target_phys_addr_t offset, uint32_t value)
194 struct pxa2xx_gpio_info_s *s = (struct pxa2xx_gpio_info_s *) opaque;
195 int bank;
196 offset -= s->base;
197 if (offset >= 0x200)
198 return;
200 bank = pxa2xx_gpio_regs[offset].bank;
201 switch (pxa2xx_gpio_regs[offset].reg) {
202 case GPDR: /* GPIO Pin-Direction registers */
203 s->dir[bank] = value;
204 pxa2xx_gpio_handler_update(s);
205 break;
207 case GPSR: /* GPIO Pin-Output Set registers */
208 s->olevel[bank] |= value;
209 pxa2xx_gpio_handler_update(s);
210 s->gpsr[bank] = value;
211 break;
213 case GPCR: /* GPIO Pin-Output Clear registers */
214 s->olevel[bank] &= ~value;
215 pxa2xx_gpio_handler_update(s);
216 break;
218 case GRER: /* GPIO Rising-Edge Detect Enable registers */
219 s->rising[bank] = value;
220 break;
222 case GFER: /* GPIO Falling-Edge Detect Enable registers */
223 s->falling[bank] = value;
224 break;
226 case GAFR_L: /* GPIO Alternate Function registers */
227 s->gafr[bank * 2] = value;
228 break;
230 case GAFR_U: /* GPIO Alternate Function registers */
231 s->gafr[bank * 2 + 1] = value;
232 break;
234 case GEDR: /* GPIO Edge Detect Status registers */
235 s->status[bank] &= ~value;
236 pxa2xx_gpio_irq_update(s);
237 break;
239 default:
240 cpu_abort(cpu_single_env,
241 "%s: Bad offset " REG_FMT "\n", __FUNCTION__, offset);
245 static CPUReadMemoryFunc *pxa2xx_gpio_readfn[] = {
246 pxa2xx_gpio_read,
247 pxa2xx_gpio_read,
248 pxa2xx_gpio_read
251 static CPUWriteMemoryFunc *pxa2xx_gpio_writefn[] = {
252 pxa2xx_gpio_write,
253 pxa2xx_gpio_write,
254 pxa2xx_gpio_write
257 static void pxa2xx_gpio_save(QEMUFile *f, void *opaque)
259 struct pxa2xx_gpio_info_s *s = (struct pxa2xx_gpio_info_s *) opaque;
260 int i;
262 qemu_put_be32(f, s->lines);
264 for (i = 0; i < PXA2XX_GPIO_BANKS; i ++) {
265 qemu_put_be32s(f, &s->ilevel[i]);
266 qemu_put_be32s(f, &s->olevel[i]);
267 qemu_put_be32s(f, &s->dir[i]);
268 qemu_put_be32s(f, &s->rising[i]);
269 qemu_put_be32s(f, &s->falling[i]);
270 qemu_put_be32s(f, &s->status[i]);
271 qemu_put_be32s(f, &s->gafr[i * 2 + 0]);
272 qemu_put_be32s(f, &s->gafr[i * 2 + 1]);
274 qemu_put_be32s(f, &s->prev_level[i]);
278 static int pxa2xx_gpio_load(QEMUFile *f, void *opaque, int version_id)
280 struct pxa2xx_gpio_info_s *s = (struct pxa2xx_gpio_info_s *) opaque;
281 int i;
283 if (qemu_get_be32(f) != s->lines)
284 return -EINVAL;
286 for (i = 0; i < PXA2XX_GPIO_BANKS; i ++) {
287 qemu_get_be32s(f, &s->ilevel[i]);
288 qemu_get_be32s(f, &s->olevel[i]);
289 qemu_get_be32s(f, &s->dir[i]);
290 qemu_get_be32s(f, &s->rising[i]);
291 qemu_get_be32s(f, &s->falling[i]);
292 qemu_get_be32s(f, &s->status[i]);
293 qemu_get_be32s(f, &s->gafr[i * 2 + 0]);
294 qemu_get_be32s(f, &s->gafr[i * 2 + 1]);
296 qemu_get_be32s(f, &s->prev_level[i]);
299 return 0;
302 struct pxa2xx_gpio_info_s *pxa2xx_gpio_init(target_phys_addr_t base,
303 CPUState *env, qemu_irq *pic, int lines)
305 int iomemtype;
306 struct pxa2xx_gpio_info_s *s;
308 s = (struct pxa2xx_gpio_info_s *)
309 qemu_mallocz(sizeof(struct pxa2xx_gpio_info_s));
310 memset(s, 0, sizeof(struct pxa2xx_gpio_info_s));
311 s->base = base;
312 s->pic = pic;
313 s->lines = lines;
314 s->cpu_env = env;
315 s->in = qemu_allocate_irqs(pxa2xx_gpio_set, s, lines);
317 iomemtype = cpu_register_io_memory(0, pxa2xx_gpio_readfn,
318 pxa2xx_gpio_writefn, s);
319 cpu_register_physical_memory(base, 0x00001000, iomemtype);
321 register_savevm("pxa2xx_gpio", 0, 0,
322 pxa2xx_gpio_save, pxa2xx_gpio_load, s);
324 return s;
327 qemu_irq *pxa2xx_gpio_in_get(struct pxa2xx_gpio_info_s *s)
329 return s->in;
332 void pxa2xx_gpio_out_set(struct pxa2xx_gpio_info_s *s,
333 int line, qemu_irq handler)
335 if (line >= s->lines) {
336 printf("%s: No GPIO pin %i\n", __FUNCTION__, line);
337 return;
340 s->handler[line] = handler;
344 * Registers a callback to notify on GPLR reads. This normally
345 * shouldn't be needed but it is used for the hack on Spitz machines.
347 void pxa2xx_gpio_read_notifier(struct pxa2xx_gpio_info_s *s, qemu_irq handler)
349 s->read_notify = handler;