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"
13 #include "hw/sysbus.h"
14 #include "migration/vmstate.h"
16 #include "qemu/module.h"
17 #include "qom/object.h"
19 //#define DEBUG_PL061 1
22 #define DPRINTF(fmt, ...) \
23 do { printf("pl061: " fmt , ## __VA_ARGS__); } while (0)
24 #define BADF(fmt, ...) \
25 do { fprintf(stderr, "pl061: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
27 #define DPRINTF(fmt, ...) do {} while(0)
28 #define BADF(fmt, ...) \
29 do { fprintf(stderr, "pl061: error: " fmt , ## __VA_ARGS__);} while (0)
32 static const uint8_t pl061_id
[12] =
33 { 0x00, 0x00, 0x00, 0x00, 0x61, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
34 static const uint8_t pl061_id_luminary
[12] =
35 { 0x00, 0x00, 0x00, 0x00, 0x61, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
37 #define TYPE_PL061 "pl061"
38 OBJECT_DECLARE_SIMPLE_TYPE(PL061State
, PL061
)
43 SysBusDevice parent_obj
;
48 uint32_t old_out_data
;
68 qemu_irq out
[N_GPIOS
];
69 const unsigned char *id
;
70 uint32_t rsvd_start
; /* reserved area: [rsvd_start, 0xfcc] */
73 static const VMStateDescription vmstate_pl061
= {
76 .minimum_version_id
= 4,
77 .fields
= (VMStateField
[]) {
78 VMSTATE_UINT32(locked
, PL061State
),
79 VMSTATE_UINT32(data
, PL061State
),
80 VMSTATE_UINT32(old_out_data
, PL061State
),
81 VMSTATE_UINT32(old_in_data
, PL061State
),
82 VMSTATE_UINT32(dir
, PL061State
),
83 VMSTATE_UINT32(isense
, PL061State
),
84 VMSTATE_UINT32(ibe
, PL061State
),
85 VMSTATE_UINT32(iev
, PL061State
),
86 VMSTATE_UINT32(im
, PL061State
),
87 VMSTATE_UINT32(istate
, PL061State
),
88 VMSTATE_UINT32(afsel
, PL061State
),
89 VMSTATE_UINT32(dr2r
, PL061State
),
90 VMSTATE_UINT32(dr4r
, PL061State
),
91 VMSTATE_UINT32(dr8r
, PL061State
),
92 VMSTATE_UINT32(odr
, PL061State
),
93 VMSTATE_UINT32(pur
, PL061State
),
94 VMSTATE_UINT32(pdr
, PL061State
),
95 VMSTATE_UINT32(slr
, PL061State
),
96 VMSTATE_UINT32(den
, PL061State
),
97 VMSTATE_UINT32(cr
, PL061State
),
98 VMSTATE_UINT32_V(amsel
, PL061State
, 2),
103 static void pl061_update(PL061State
*s
)
110 DPRINTF("dir = %d, data = %d\n", s
->dir
, s
->data
);
112 /* Outputs float high. */
113 /* FIXME: This is board dependent. */
114 out
= (s
->data
& s
->dir
) | ~s
->dir
;
115 changed
= s
->old_out_data
^ out
;
117 s
->old_out_data
= out
;
118 for (i
= 0; i
< N_GPIOS
; i
++) {
120 if (changed
& mask
) {
121 DPRINTF("Set output %d = %d\n", i
, (out
& mask
) != 0);
122 qemu_set_irq(s
->out
[i
], (out
& mask
) != 0);
128 changed
= (s
->old_in_data
^ s
->data
) & ~s
->dir
;
130 s
->old_in_data
= s
->data
;
131 for (i
= 0; i
< N_GPIOS
; i
++) {
133 if (changed
& mask
) {
134 DPRINTF("Changed input %d = %d\n", i
, (s
->data
& mask
) != 0);
136 if (!(s
->isense
& mask
)) {
139 /* Any edge triggers the interrupt */
142 /* Edge is selected by IEV */
143 s
->istate
|= ~(s
->data
^ s
->iev
) & mask
;
150 /* Level interrupt */
151 s
->istate
|= ~(s
->data
^ s
->iev
) & s
->isense
;
153 DPRINTF("istate = %02X\n", s
->istate
);
155 qemu_set_irq(s
->irq
, (s
->istate
& s
->im
) != 0);
158 static uint64_t pl061_read(void *opaque
, hwaddr offset
,
161 PL061State
*s
= (PL061State
*)opaque
;
163 if (offset
< 0x400) {
164 return s
->data
& (offset
>> 2);
166 if (offset
>= s
->rsvd_start
&& offset
<= 0xfcc) {
169 if (offset
>= 0xfd0 && offset
< 0x1000) {
170 return s
->id
[(offset
- 0xfd0) >> 2];
173 case 0x400: /* Direction */
175 case 0x404: /* Interrupt sense */
177 case 0x408: /* Interrupt both edges */
179 case 0x40c: /* Interrupt event */
181 case 0x410: /* Interrupt mask */
183 case 0x414: /* Raw interrupt status */
185 case 0x418: /* Masked interrupt status */
186 return s
->istate
& s
->im
;
187 case 0x420: /* Alternate function select */
189 case 0x500: /* 2mA drive */
191 case 0x504: /* 4mA drive */
193 case 0x508: /* 8mA drive */
195 case 0x50c: /* Open drain */
197 case 0x510: /* Pull-up */
199 case 0x514: /* Pull-down */
201 case 0x518: /* Slew rate control */
203 case 0x51c: /* Digital enable */
205 case 0x520: /* Lock */
207 case 0x524: /* Commit */
209 case 0x528: /* Analog mode select */
215 qemu_log_mask(LOG_GUEST_ERROR
,
216 "pl061_read: Bad offset %x\n", (int)offset
);
220 static void pl061_write(void *opaque
, hwaddr offset
,
221 uint64_t value
, unsigned size
)
223 PL061State
*s
= (PL061State
*)opaque
;
226 if (offset
< 0x400) {
227 mask
= (offset
>> 2) & s
->dir
;
228 s
->data
= (s
->data
& ~mask
) | (value
& mask
);
232 if (offset
>= s
->rsvd_start
) {
236 case 0x400: /* Direction */
237 s
->dir
= value
& 0xff;
239 case 0x404: /* Interrupt sense */
240 s
->isense
= value
& 0xff;
242 case 0x408: /* Interrupt both edges */
243 s
->ibe
= value
& 0xff;
245 case 0x40c: /* Interrupt event */
246 s
->iev
= value
& 0xff;
248 case 0x410: /* Interrupt mask */
249 s
->im
= value
& 0xff;
251 case 0x41c: /* Interrupt clear */
254 case 0x420: /* Alternate function select */
256 s
->afsel
= (s
->afsel
& ~mask
) | (value
& mask
);
258 case 0x500: /* 2mA drive */
259 s
->dr2r
= value
& 0xff;
261 case 0x504: /* 4mA drive */
262 s
->dr4r
= value
& 0xff;
264 case 0x508: /* 8mA drive */
265 s
->dr8r
= value
& 0xff;
267 case 0x50c: /* Open drain */
268 s
->odr
= value
& 0xff;
270 case 0x510: /* Pull-up */
271 s
->pur
= value
& 0xff;
273 case 0x514: /* Pull-down */
274 s
->pdr
= value
& 0xff;
276 case 0x518: /* Slew rate control */
277 s
->slr
= value
& 0xff;
279 case 0x51c: /* Digital enable */
280 s
->den
= value
& 0xff;
282 case 0x520: /* Lock */
283 s
->locked
= (value
!= 0xacce551);
285 case 0x524: /* Commit */
287 s
->cr
= value
& 0xff;
290 s
->amsel
= value
& 0xff;
298 qemu_log_mask(LOG_GUEST_ERROR
,
299 "pl061_write: Bad offset %x\n", (int)offset
);
302 static void pl061_reset(DeviceState
*dev
)
304 PL061State
*s
= PL061(dev
);
306 /* reset values from PL061 TRM, Stellaris LM3S5P31 & LM3S8962 Data Sheet */
330 static void pl061_set_irq(void * opaque
, int irq
, int level
)
332 PL061State
*s
= (PL061State
*)opaque
;
336 if ((s
->dir
& mask
) == 0) {
344 static const MemoryRegionOps pl061_ops
= {
346 .write
= pl061_write
,
347 .endianness
= DEVICE_NATIVE_ENDIAN
,
350 static void pl061_luminary_init(Object
*obj
)
352 PL061State
*s
= PL061(obj
);
354 s
->id
= pl061_id_luminary
;
355 s
->rsvd_start
= 0x52c;
358 static void pl061_init(Object
*obj
)
360 PL061State
*s
= PL061(obj
);
361 DeviceState
*dev
= DEVICE(obj
);
362 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
365 s
->rsvd_start
= 0x424;
367 memory_region_init_io(&s
->iomem
, obj
, &pl061_ops
, s
, "pl061", 0x1000);
368 sysbus_init_mmio(sbd
, &s
->iomem
);
369 sysbus_init_irq(sbd
, &s
->irq
);
370 qdev_init_gpio_in(dev
, pl061_set_irq
, N_GPIOS
);
371 qdev_init_gpio_out(dev
, s
->out
, N_GPIOS
);
374 static void pl061_class_init(ObjectClass
*klass
, void *data
)
376 DeviceClass
*dc
= DEVICE_CLASS(klass
);
378 dc
->vmsd
= &vmstate_pl061
;
379 dc
->reset
= &pl061_reset
;
382 static const TypeInfo pl061_info
= {
384 .parent
= TYPE_SYS_BUS_DEVICE
,
385 .instance_size
= sizeof(PL061State
),
386 .instance_init
= pl061_init
,
387 .class_init
= pl061_class_init
,
390 static const TypeInfo pl061_luminary_info
= {
391 .name
= "pl061_luminary",
392 .parent
= TYPE_PL061
,
393 .instance_init
= pl061_luminary_init
,
396 static void pl061_register_types(void)
398 type_register_static(&pl061_info
);
399 type_register_static(&pl061_luminary_info
);
402 type_init(pl061_register_types
)