2 * Arm PrimeCell PL061 General Purpose IO with additional
3 * Luminary Micro Stellaris bits.
5 * Copyright (c) 2007 CodeSourcery.
6 * Written by Paul Brook
8 * This code is licenced under the GPL.
12 #include "primecell.h"
14 //#define DEBUG_PL061 1
17 #define DPRINTF(fmt, ...) \
18 do { printf("pl061: " fmt , ## __VA_ARGS__); } while (0)
19 #define BADF(fmt, ...) \
20 do { fprintf(stderr, "pl061: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
22 #define DPRINTF(fmt, ...) do {} while(0)
23 #define BADF(fmt, ...) \
24 do { fprintf(stderr, "pl061: error: " fmt , ## __VA_ARGS__);} while (0)
27 static const uint8_t pl061_id
[12] =
28 { 0x00, 0x00, 0x00, 0x00, 0x61, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
55 static void pl061_update(pl061_state
*s
)
62 /* Outputs float high. */
63 /* FIXME: This is board dependent. */
64 out
= (s
->data
& s
->dir
) | ~s
->dir
;
65 changed
= s
->old_data
^ out
;
70 for (i
= 0; i
< 8; i
++) {
72 if ((changed
& mask
) && s
->out
) {
73 DPRINTF("Set output %d = %d\n", i
, (out
& mask
) != 0);
74 qemu_set_irq(s
->out
[i
], (out
& mask
) != 0);
78 /* FIXME: Implement input interrupts. */
81 static uint32_t pl061_read(void *opaque
, target_phys_addr_t offset
)
83 pl061_state
*s
= (pl061_state
*)opaque
;
85 if (offset
>= 0xfd0 && offset
< 0x1000) {
86 return pl061_id
[(offset
- 0xfd0) >> 2];
89 return s
->data
& (offset
>> 2);
92 case 0x400: /* Direction */
94 case 0x404: /* Interrupt sense */
96 case 0x408: /* Interrupt both edges */
98 case 0x40c: /* Interupt event */
100 case 0x410: /* Interrupt mask */
102 case 0x414: /* Raw interrupt status */
104 case 0x418: /* Masked interrupt status */
105 return s
->istate
| s
->im
;
106 case 0x420: /* Alternate function select */
108 case 0x500: /* 2mA drive */
110 case 0x504: /* 4mA drive */
112 case 0x508: /* 8mA drive */
114 case 0x50c: /* Open drain */
116 case 0x510: /* Pull-up */
118 case 0x514: /* Pull-down */
120 case 0x518: /* Slew rate control */
122 case 0x51c: /* Digital enable */
124 case 0x520: /* Lock */
126 case 0x524: /* Commit */
129 hw_error("pl061_read: Bad offset %x\n", (int)offset
);
134 static void pl061_write(void *opaque
, target_phys_addr_t offset
,
137 pl061_state
*s
= (pl061_state
*)opaque
;
140 if (offset
< 0x400) {
141 mask
= (offset
>> 2) & s
->dir
;
142 s
->data
= (s
->data
& ~mask
) | (value
& mask
);
147 case 0x400: /* Direction */
150 case 0x404: /* Interrupt sense */
153 case 0x408: /* Interrupt both edges */
156 case 0x40c: /* Interupt event */
159 case 0x410: /* Interrupt mask */
162 case 0x41c: /* Interrupt clear */
165 case 0x420: /* Alternate function select */
167 s
->afsel
= (s
->afsel
& ~mask
) | (value
& mask
);
169 case 0x500: /* 2mA drive */
172 case 0x504: /* 4mA drive */
175 case 0x508: /* 8mA drive */
178 case 0x50c: /* Open drain */
181 case 0x510: /* Pull-up */
184 case 0x514: /* Pull-down */
187 case 0x518: /* Slew rate control */
190 case 0x51c: /* Digital enable */
193 case 0x520: /* Lock */
194 s
->locked
= (value
!= 0xacce551);
196 case 0x524: /* Commit */
201 hw_error("pl061_write: Bad offset %x\n", (int)offset
);
206 static void pl061_reset(pl061_state
*s
)
212 static void pl061_set_irq(void * opaque
, int irq
, int level
)
214 pl061_state
*s
= (pl061_state
*)opaque
;
218 if ((s
->dir
& mask
) == 0) {
226 static CPUReadMemoryFunc
*pl061_readfn
[] = {
232 static CPUWriteMemoryFunc
*pl061_writefn
[] = {
238 static void pl061_save(QEMUFile
*f
, void *opaque
)
240 pl061_state
*s
= (pl061_state
*)opaque
;
242 qemu_put_be32(f
, s
->locked
);
243 qemu_put_be32(f
, s
->data
);
244 qemu_put_be32(f
, s
->old_data
);
245 qemu_put_be32(f
, s
->dir
);
246 qemu_put_be32(f
, s
->isense
);
247 qemu_put_be32(f
, s
->ibe
);
248 qemu_put_be32(f
, s
->iev
);
249 qemu_put_be32(f
, s
->im
);
250 qemu_put_be32(f
, s
->istate
);
251 qemu_put_be32(f
, s
->afsel
);
252 qemu_put_be32(f
, s
->dr2r
);
253 qemu_put_be32(f
, s
->dr4r
);
254 qemu_put_be32(f
, s
->dr8r
);
255 qemu_put_be32(f
, s
->odr
);
256 qemu_put_be32(f
, s
->pur
);
257 qemu_put_be32(f
, s
->pdr
);
258 qemu_put_be32(f
, s
->slr
);
259 qemu_put_be32(f
, s
->den
);
260 qemu_put_be32(f
, s
->cr
);
261 qemu_put_be32(f
, s
->float_high
);
264 static int pl061_load(QEMUFile
*f
, void *opaque
, int version_id
)
266 pl061_state
*s
= (pl061_state
*)opaque
;
270 s
->locked
= qemu_get_be32(f
);
271 s
->data
= qemu_get_be32(f
);
272 s
->old_data
= qemu_get_be32(f
);
273 s
->dir
= qemu_get_be32(f
);
274 s
->isense
= qemu_get_be32(f
);
275 s
->ibe
= qemu_get_be32(f
);
276 s
->iev
= qemu_get_be32(f
);
277 s
->im
= qemu_get_be32(f
);
278 s
->istate
= qemu_get_be32(f
);
279 s
->afsel
= qemu_get_be32(f
);
280 s
->dr2r
= qemu_get_be32(f
);
281 s
->dr4r
= qemu_get_be32(f
);
282 s
->dr8r
= qemu_get_be32(f
);
283 s
->odr
= qemu_get_be32(f
);
284 s
->pur
= qemu_get_be32(f
);
285 s
->pdr
= qemu_get_be32(f
);
286 s
->slr
= qemu_get_be32(f
);
287 s
->den
= qemu_get_be32(f
);
288 s
->cr
= qemu_get_be32(f
);
289 s
->float_high
= qemu_get_be32(f
);
294 /* Returns an array of inputs. */
295 qemu_irq
*pl061_init(uint32_t base
, qemu_irq irq
, qemu_irq
**out
)
300 s
= (pl061_state
*)qemu_mallocz(sizeof(pl061_state
));
301 iomemtype
= cpu_register_io_memory(0, pl061_readfn
,
303 cpu_register_physical_memory(base
, 0x00001000, iomemtype
);
309 register_savevm("pl061_gpio", -1, 1, pl061_save
, pl061_load
, s
);
310 return qemu_allocate_irqs(pl061_set_irq
, s
, 8);