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.
13 #define PXA2XX_GPIO_BANKS 4
15 struct pxa2xx_gpio_info_s
{
16 target_phys_addr_t base
;
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];
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
]);
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
]);
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
]);
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
;
96 if (line
>= s
->lines
) {
97 printf("%s: No GPIO pin %i\n", __FUNCTION__
, line
);
102 mask
= 1 << (line
& 31);
105 s
->status
[bank
] |= s
->rising
[bank
] & mask
&
106 ~s
->ilevel
[bank
] & ~s
->dir
[bank
];
107 s
->ilevel
[bank
] |= mask
;
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
);
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
;
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
) {
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
;
147 bank
= pxa2xx_gpio_regs
[offset
].bank
;
148 switch (pxa2xx_gpio_regs
[offset
].reg
) {
149 case GPDR
: /* GPIO Pin-Direction registers */
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
);
180 case GEDR
: /* GPIO Edge Detect Status registers */
181 return s
->status
[bank
];
184 cpu_abort(cpu_single_env
,
185 "%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
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
;
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
);
207 case GPSR
: /* GPIO Pin-Output Set registers */
208 s
->olevel
[bank
] |= value
;
209 pxa2xx_gpio_handler_update(s
);
210 s
->gpsr
[bank
] = value
;
213 case GPCR
: /* GPIO Pin-Output Clear registers */
214 s
->olevel
[bank
] &= ~value
;
215 pxa2xx_gpio_handler_update(s
);
218 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
219 s
->rising
[bank
] = value
;
222 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
223 s
->falling
[bank
] = value
;
226 case GAFR_L
: /* GPIO Alternate Function registers */
227 s
->gafr
[bank
* 2] = value
;
230 case GAFR_U
: /* GPIO Alternate Function registers */
231 s
->gafr
[bank
* 2 + 1] = value
;
234 case GEDR
: /* GPIO Edge Detect Status registers */
235 s
->status
[bank
] &= ~value
;
236 pxa2xx_gpio_irq_update(s
);
240 cpu_abort(cpu_single_env
,
241 "%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
245 static CPUReadMemoryFunc
*pxa2xx_gpio_readfn
[] = {
251 static CPUWriteMemoryFunc
*pxa2xx_gpio_writefn
[] = {
257 static void pxa2xx_gpio_save(QEMUFile
*f
, void *opaque
)
259 struct pxa2xx_gpio_info_s
*s
= (struct pxa2xx_gpio_info_s
*) opaque
;
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
;
283 if (qemu_get_be32(f
) != s
->lines
)
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
]);
302 struct pxa2xx_gpio_info_s
*pxa2xx_gpio_init(target_phys_addr_t base
,
303 CPUState
*env
, qemu_irq
*pic
, int lines
)
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
));
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
);
327 qemu_irq
*pxa2xx_gpio_in_get(struct pxa2xx_gpio_info_s
*s
)
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
);
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
;