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 licensed under the GPL.
11 #include "qemu/osdep.h"
12 #include "hw/sysbus.h"
14 #include "qemu/module.h"
16 //#define DEBUG_PL061 1
19 #define DPRINTF(fmt, ...) \
20 do { printf("pl061: " fmt , ## __VA_ARGS__); } while (0)
21 #define BADF(fmt, ...) \
22 do { fprintf(stderr, "pl061: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
24 #define DPRINTF(fmt, ...) do {} while(0)
25 #define BADF(fmt, ...) \
26 do { fprintf(stderr, "pl061: error: " fmt , ## __VA_ARGS__);} while (0)
29 static const uint8_t pl061_id
[12] =
30 { 0x00, 0x00, 0x00, 0x00, 0x61, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
31 static const uint8_t pl061_id_luminary
[12] =
32 { 0x00, 0x00, 0x00, 0x00, 0x61, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
34 #define TYPE_PL061 "pl061"
35 #define PL061(obj) OBJECT_CHECK(PL061State, (obj), TYPE_PL061)
37 typedef struct PL061State
{
38 SysBusDevice parent_obj
;
43 uint32_t old_out_data
;
64 const unsigned char *id
;
65 uint32_t rsvd_start
; /* reserved area: [rsvd_start, 0xfcc] */
68 static const VMStateDescription vmstate_pl061
= {
71 .minimum_version_id
= 4,
72 .fields
= (VMStateField
[]) {
73 VMSTATE_UINT32(locked
, PL061State
),
74 VMSTATE_UINT32(data
, PL061State
),
75 VMSTATE_UINT32(old_out_data
, PL061State
),
76 VMSTATE_UINT32(old_in_data
, PL061State
),
77 VMSTATE_UINT32(dir
, PL061State
),
78 VMSTATE_UINT32(isense
, PL061State
),
79 VMSTATE_UINT32(ibe
, PL061State
),
80 VMSTATE_UINT32(iev
, PL061State
),
81 VMSTATE_UINT32(im
, PL061State
),
82 VMSTATE_UINT32(istate
, PL061State
),
83 VMSTATE_UINT32(afsel
, PL061State
),
84 VMSTATE_UINT32(dr2r
, PL061State
),
85 VMSTATE_UINT32(dr4r
, PL061State
),
86 VMSTATE_UINT32(dr8r
, PL061State
),
87 VMSTATE_UINT32(odr
, PL061State
),
88 VMSTATE_UINT32(pur
, PL061State
),
89 VMSTATE_UINT32(pdr
, PL061State
),
90 VMSTATE_UINT32(slr
, PL061State
),
91 VMSTATE_UINT32(den
, PL061State
),
92 VMSTATE_UINT32(cr
, PL061State
),
93 VMSTATE_UINT32_V(amsel
, PL061State
, 2),
98 static void pl061_update(PL061State
*s
)
105 DPRINTF("dir = %d, data = %d\n", s
->dir
, s
->data
);
107 /* Outputs float high. */
108 /* FIXME: This is board dependent. */
109 out
= (s
->data
& s
->dir
) | ~s
->dir
;
110 changed
= s
->old_out_data
^ out
;
112 s
->old_out_data
= out
;
113 for (i
= 0; i
< 8; i
++) {
115 if (changed
& mask
) {
116 DPRINTF("Set output %d = %d\n", i
, (out
& mask
) != 0);
117 qemu_set_irq(s
->out
[i
], (out
& mask
) != 0);
123 changed
= (s
->old_in_data
^ s
->data
) & ~s
->dir
;
125 s
->old_in_data
= s
->data
;
126 for (i
= 0; i
< 8; i
++) {
128 if (changed
& mask
) {
129 DPRINTF("Changed input %d = %d\n", i
, (s
->data
& mask
) != 0);
131 if (!(s
->isense
& mask
)) {
134 /* Any edge triggers the interrupt */
137 /* Edge is selected by IEV */
138 s
->istate
|= ~(s
->data
^ s
->iev
) & mask
;
145 /* Level interrupt */
146 s
->istate
|= ~(s
->data
^ s
->iev
) & s
->isense
;
148 DPRINTF("istate = %02X\n", s
->istate
);
150 qemu_set_irq(s
->irq
, (s
->istate
& s
->im
) != 0);
153 static uint64_t pl061_read(void *opaque
, hwaddr offset
,
156 PL061State
*s
= (PL061State
*)opaque
;
158 if (offset
< 0x400) {
159 return s
->data
& (offset
>> 2);
161 if (offset
>= s
->rsvd_start
&& offset
<= 0xfcc) {
164 if (offset
>= 0xfd0 && offset
< 0x1000) {
165 return s
->id
[(offset
- 0xfd0) >> 2];
168 case 0x400: /* Direction */
170 case 0x404: /* Interrupt sense */
172 case 0x408: /* Interrupt both edges */
174 case 0x40c: /* Interrupt event */
176 case 0x410: /* Interrupt mask */
178 case 0x414: /* Raw interrupt status */
180 case 0x418: /* Masked interrupt status */
181 return s
->istate
& s
->im
;
182 case 0x420: /* Alternate function select */
184 case 0x500: /* 2mA drive */
186 case 0x504: /* 4mA drive */
188 case 0x508: /* 8mA drive */
190 case 0x50c: /* Open drain */
192 case 0x510: /* Pull-up */
194 case 0x514: /* Pull-down */
196 case 0x518: /* Slew rate control */
198 case 0x51c: /* Digital enable */
200 case 0x520: /* Lock */
202 case 0x524: /* Commit */
204 case 0x528: /* Analog mode select */
210 qemu_log_mask(LOG_GUEST_ERROR
,
211 "pl061_read: Bad offset %x\n", (int)offset
);
215 static void pl061_write(void *opaque
, hwaddr offset
,
216 uint64_t value
, unsigned size
)
218 PL061State
*s
= (PL061State
*)opaque
;
221 if (offset
< 0x400) {
222 mask
= (offset
>> 2) & s
->dir
;
223 s
->data
= (s
->data
& ~mask
) | (value
& mask
);
227 if (offset
>= s
->rsvd_start
) {
231 case 0x400: /* Direction */
232 s
->dir
= value
& 0xff;
234 case 0x404: /* Interrupt sense */
235 s
->isense
= value
& 0xff;
237 case 0x408: /* Interrupt both edges */
238 s
->ibe
= value
& 0xff;
240 case 0x40c: /* Interrupt event */
241 s
->iev
= value
& 0xff;
243 case 0x410: /* Interrupt mask */
244 s
->im
= value
& 0xff;
246 case 0x41c: /* Interrupt clear */
249 case 0x420: /* Alternate function select */
251 s
->afsel
= (s
->afsel
& ~mask
) | (value
& mask
);
253 case 0x500: /* 2mA drive */
254 s
->dr2r
= value
& 0xff;
256 case 0x504: /* 4mA drive */
257 s
->dr4r
= value
& 0xff;
259 case 0x508: /* 8mA drive */
260 s
->dr8r
= value
& 0xff;
262 case 0x50c: /* Open drain */
263 s
->odr
= value
& 0xff;
265 case 0x510: /* Pull-up */
266 s
->pur
= value
& 0xff;
268 case 0x514: /* Pull-down */
269 s
->pdr
= value
& 0xff;
271 case 0x518: /* Slew rate control */
272 s
->slr
= value
& 0xff;
274 case 0x51c: /* Digital enable */
275 s
->den
= value
& 0xff;
277 case 0x520: /* Lock */
278 s
->locked
= (value
!= 0xacce551);
280 case 0x524: /* Commit */
282 s
->cr
= value
& 0xff;
285 s
->amsel
= value
& 0xff;
293 qemu_log_mask(LOG_GUEST_ERROR
,
294 "pl061_write: Bad offset %x\n", (int)offset
);
297 static void pl061_reset(DeviceState
*dev
)
299 PL061State
*s
= PL061(dev
);
301 /* reset values from PL061 TRM, Stellaris LM3S5P31 & LM3S8962 Data Sheet */
325 static void pl061_set_irq(void * opaque
, int irq
, int level
)
327 PL061State
*s
= (PL061State
*)opaque
;
331 if ((s
->dir
& mask
) == 0) {
339 static const MemoryRegionOps pl061_ops
= {
341 .write
= pl061_write
,
342 .endianness
= DEVICE_NATIVE_ENDIAN
,
345 static void pl061_luminary_init(Object
*obj
)
347 PL061State
*s
= PL061(obj
);
349 s
->id
= pl061_id_luminary
;
350 s
->rsvd_start
= 0x52c;
353 static void pl061_init(Object
*obj
)
355 PL061State
*s
= PL061(obj
);
356 DeviceState
*dev
= DEVICE(obj
);
357 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
360 s
->rsvd_start
= 0x424;
362 memory_region_init_io(&s
->iomem
, obj
, &pl061_ops
, s
, "pl061", 0x1000);
363 sysbus_init_mmio(sbd
, &s
->iomem
);
364 sysbus_init_irq(sbd
, &s
->irq
);
365 qdev_init_gpio_in(dev
, pl061_set_irq
, 8);
366 qdev_init_gpio_out(dev
, s
->out
, 8);
369 static void pl061_class_init(ObjectClass
*klass
, void *data
)
371 DeviceClass
*dc
= DEVICE_CLASS(klass
);
373 dc
->vmsd
= &vmstate_pl061
;
374 dc
->reset
= &pl061_reset
;
377 static const TypeInfo pl061_info
= {
379 .parent
= TYPE_SYS_BUS_DEVICE
,
380 .instance_size
= sizeof(PL061State
),
381 .instance_init
= pl061_init
,
382 .class_init
= pl061_class_init
,
385 static const TypeInfo pl061_luminary_info
= {
386 .name
= "pl061_luminary",
387 .parent
= TYPE_PL061
,
388 .instance_init
= pl061_luminary_init
,
391 static void pl061_register_types(void)
393 type_register_static(&pl061_info
);
394 type_register_static(&pl061_luminary_info
);
397 type_init(pl061_register_types
)