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 PXA2xxGPIOInfo
{
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
];
28 uint32_t gpsr
[PXA2XX_GPIO_BANKS
];
29 uint32_t gafr
[PXA2XX_GPIO_BANKS
* 2];
31 uint32_t prev_level
[PXA2XX_GPIO_BANKS
];
32 qemu_irq handler
[PXA2XX_GPIO_BANKS
* 32];
50 } pxa2xx_gpio_regs
[0x200] = {
51 [0 ... 0x1ff] = { GPIO_NONE
, 0 },
52 #define PXA2XX_REG(reg, a0, a1, a2, a3) \
53 [a0] = { reg, 0 }, [a1] = { reg, 1 }, [a2] = { reg, 2 }, [a3] = { reg, 3 },
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)
66 static void pxa2xx_gpio_irq_update(PXA2xxGPIOInfo
*s
)
68 if (s
->status
[0] & (1 << 0))
69 qemu_irq_raise(s
->pic
[PXA2XX_PIC_GPIO_0
]);
71 qemu_irq_lower(s
->pic
[PXA2XX_PIC_GPIO_0
]);
73 if (s
->status
[0] & (1 << 1))
74 qemu_irq_raise(s
->pic
[PXA2XX_PIC_GPIO_1
]);
76 qemu_irq_lower(s
->pic
[PXA2XX_PIC_GPIO_1
]);
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
]);
81 qemu_irq_lower(s
->pic
[PXA2XX_PIC_GPIO_X
]);
84 /* Bitmap of pins used as standby and sleep wake-up sources. */
85 static const int pxa2xx_gpio_wake
[PXA2XX_GPIO_BANKS
] = {
86 0x8003fe1b, 0x002001fc, 0xec080000, 0x0012007f,
89 static void pxa2xx_gpio_set(void *opaque
, int line
, int level
)
91 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
95 if (line
>= s
->lines
) {
96 printf("%s: No GPIO pin %i\n", __FUNCTION__
, line
);
101 mask
= 1 << (line
& 31);
104 s
->status
[bank
] |= s
->rising
[bank
] & mask
&
105 ~s
->ilevel
[bank
] & ~s
->dir
[bank
];
106 s
->ilevel
[bank
] |= mask
;
108 s
->status
[bank
] |= s
->falling
[bank
] & mask
&
109 s
->ilevel
[bank
] & ~s
->dir
[bank
];
110 s
->ilevel
[bank
] &= ~mask
;
113 if (s
->status
[bank
] & mask
)
114 pxa2xx_gpio_irq_update(s
);
117 if (s
->cpu_env
->halted
&& (mask
& ~s
->dir
[bank
] & pxa2xx_gpio_wake
[bank
]))
118 cpu_interrupt(s
->cpu_env
, CPU_INTERRUPT_EXITTB
);
121 static void pxa2xx_gpio_handler_update(PXA2xxGPIOInfo
*s
) {
122 uint32_t level
, diff
;
124 for (i
= 0; i
< PXA2XX_GPIO_BANKS
; i
++) {
125 level
= s
->olevel
[i
] & s
->dir
[i
];
127 for (diff
= s
->prev_level
[i
] ^ level
; diff
; diff
^= 1 << bit
) {
130 qemu_set_irq(s
->handler
[line
], (level
>> bit
) & 1);
133 s
->prev_level
[i
] = level
;
137 static uint32_t pxa2xx_gpio_read(void *opaque
, target_phys_addr_t offset
)
139 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
145 bank
= pxa2xx_gpio_regs
[offset
].bank
;
146 switch (pxa2xx_gpio_regs
[offset
].reg
) {
147 case GPDR
: /* GPIO Pin-Direction registers */
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. */
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. */
160 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
161 return s
->rising
[bank
];
163 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
164 return s
->falling
[bank
];
166 case GAFR_L
: /* GPIO Alternate Function registers */
167 return s
->gafr
[bank
* 2];
169 case GAFR_U
: /* GPIO Alternate Function registers */
170 return s
->gafr
[bank
* 2 + 1];
172 case GPLR
: /* GPIO Pin-Level registers */
173 ret
= (s
->olevel
[bank
] & s
->dir
[bank
]) |
174 (s
->ilevel
[bank
] & ~s
->dir
[bank
]);
175 qemu_irq_raise(s
->read_notify
);
178 case GEDR
: /* GPIO Edge Detect Status registers */
179 return s
->status
[bank
];
182 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
188 static void pxa2xx_gpio_write(void *opaque
,
189 target_phys_addr_t offset
, uint32_t value
)
191 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
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
);
203 case GPSR
: /* GPIO Pin-Output Set registers */
204 s
->olevel
[bank
] |= value
;
205 pxa2xx_gpio_handler_update(s
);
206 s
->gpsr
[bank
] = value
;
209 case GPCR
: /* GPIO Pin-Output Clear registers */
210 s
->olevel
[bank
] &= ~value
;
211 pxa2xx_gpio_handler_update(s
);
214 case GRER
: /* GPIO Rising-Edge Detect Enable registers */
215 s
->rising
[bank
] = value
;
218 case GFER
: /* GPIO Falling-Edge Detect Enable registers */
219 s
->falling
[bank
] = value
;
222 case GAFR_L
: /* GPIO Alternate Function registers */
223 s
->gafr
[bank
* 2] = value
;
226 case GAFR_U
: /* GPIO Alternate Function registers */
227 s
->gafr
[bank
* 2 + 1] = value
;
230 case GEDR
: /* GPIO Edge Detect Status registers */
231 s
->status
[bank
] &= ~value
;
232 pxa2xx_gpio_irq_update(s
);
236 hw_error("%s: Bad offset " REG_FMT
"\n", __FUNCTION__
, offset
);
240 static CPUReadMemoryFunc
* const pxa2xx_gpio_readfn
[] = {
246 static CPUWriteMemoryFunc
* const pxa2xx_gpio_writefn
[] = {
252 static void pxa2xx_gpio_save(QEMUFile
*f
, void *opaque
)
254 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
257 qemu_put_be32(f
, s
->lines
);
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]);
269 qemu_put_be32s(f
, &s
->prev_level
[i
]);
273 static int pxa2xx_gpio_load(QEMUFile
*f
, void *opaque
, int version_id
)
275 PXA2xxGPIOInfo
*s
= (PXA2xxGPIOInfo
*) opaque
;
278 if (qemu_get_be32(f
) != s
->lines
)
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]);
291 qemu_get_be32s(f
, &s
->prev_level
[i
]);
297 PXA2xxGPIOInfo
*pxa2xx_gpio_init(target_phys_addr_t base
,
298 CPUState
*env
, qemu_irq
*pic
, int lines
)
303 s
= (PXA2xxGPIOInfo
*)
304 qemu_mallocz(sizeof(PXA2xxGPIOInfo
));
305 memset(s
, 0, sizeof(PXA2xxGPIOInfo
));
309 s
->in
= qemu_allocate_irqs(pxa2xx_gpio_set
, s
, lines
);
311 iomemtype
= cpu_register_io_memory(pxa2xx_gpio_readfn
,
312 pxa2xx_gpio_writefn
, s
);
313 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
315 register_savevm(NULL
, "pxa2xx_gpio", 0, 0,
316 pxa2xx_gpio_save
, pxa2xx_gpio_load
, s
);
321 qemu_irq
*pxa2xx_gpio_in_get(PXA2xxGPIOInfo
*s
)
326 void pxa2xx_gpio_out_set(PXA2xxGPIOInfo
*s
,
327 int line
, qemu_irq handler
)
329 if (line
>= s
->lines
) {
330 printf("%s: No GPIO pin %i\n", __FUNCTION__
, line
);
334 s
->handler
[line
] = handler
;
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.
341 void pxa2xx_gpio_read_notifier(PXA2xxGPIOInfo
*s
, qemu_irq handler
)
343 s
->read_notify
= handler
;