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 "hw/sysbus.h"
13 //#define DEBUG_PL061 1
16 #define DPRINTF(fmt, ...) \
17 do { printf("pl061: " fmt , ## __VA_ARGS__); } while (0)
18 #define BADF(fmt, ...) \
19 do { fprintf(stderr, "pl061: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
21 #define DPRINTF(fmt, ...) do {} while(0)
22 #define BADF(fmt, ...) \
23 do { fprintf(stderr, "pl061: error: " fmt , ## __VA_ARGS__);} while (0)
26 static const uint8_t pl061_id
[12] =
27 { 0x00, 0x00, 0x00, 0x00, 0x61, 0x10, 0x04, 0x00, 0x0d, 0xf0, 0x05, 0xb1 };
28 static const uint8_t pl061_id_luminary
[12] =
29 { 0x00, 0x00, 0x00, 0x00, 0x61, 0x00, 0x18, 0x01, 0x0d, 0xf0, 0x05, 0xb1 };
31 #define TYPE_PL061 "pl061"
32 #define PL061(obj) OBJECT_CHECK(PL061State, (obj), TYPE_PL061)
34 typedef struct PL061State
{
35 SysBusDevice parent_obj
;
40 uint32_t old_out_data
;
62 const unsigned char *id
;
65 static const VMStateDescription vmstate_pl061
= {
68 .minimum_version_id
= 3,
69 .fields
= (VMStateField
[]) {
70 VMSTATE_UINT32(locked
, PL061State
),
71 VMSTATE_UINT32(data
, PL061State
),
72 VMSTATE_UINT32(old_out_data
, PL061State
),
73 VMSTATE_UINT32(old_in_data
, PL061State
),
74 VMSTATE_UINT32(dir
, PL061State
),
75 VMSTATE_UINT32(isense
, PL061State
),
76 VMSTATE_UINT32(ibe
, PL061State
),
77 VMSTATE_UINT32(iev
, PL061State
),
78 VMSTATE_UINT32(im
, PL061State
),
79 VMSTATE_UINT32(istate
, PL061State
),
80 VMSTATE_UINT32(afsel
, PL061State
),
81 VMSTATE_UINT32(dr2r
, PL061State
),
82 VMSTATE_UINT32(dr4r
, PL061State
),
83 VMSTATE_UINT32(dr8r
, PL061State
),
84 VMSTATE_UINT32(odr
, PL061State
),
85 VMSTATE_UINT32(pur
, PL061State
),
86 VMSTATE_UINT32(pdr
, PL061State
),
87 VMSTATE_UINT32(slr
, PL061State
),
88 VMSTATE_UINT32(den
, PL061State
),
89 VMSTATE_UINT32(cr
, PL061State
),
90 VMSTATE_UINT32(float_high
, PL061State
),
91 VMSTATE_UINT32_V(amsel
, PL061State
, 2),
96 static void pl061_update(PL061State
*s
)
103 DPRINTF("dir = %d, data = %d\n", s
->dir
, s
->data
);
105 /* Outputs float high. */
106 /* FIXME: This is board dependent. */
107 out
= (s
->data
& s
->dir
) | ~s
->dir
;
108 changed
= s
->old_out_data
^ out
;
110 s
->old_out_data
= out
;
111 for (i
= 0; i
< 8; i
++) {
113 if (changed
& mask
) {
114 DPRINTF("Set output %d = %d\n", i
, (out
& mask
) != 0);
115 qemu_set_irq(s
->out
[i
], (out
& mask
) != 0);
121 changed
= (s
->old_in_data
^ s
->data
) & ~s
->dir
;
123 s
->old_in_data
= s
->data
;
124 for (i
= 0; i
< 8; i
++) {
126 if (changed
& mask
) {
127 DPRINTF("Changed input %d = %d\n", i
, (s
->data
& mask
) != 0);
129 if (!(s
->isense
& mask
)) {
132 /* Any edge triggers the interrupt */
135 /* Edge is selected by IEV */
136 s
->istate
|= ~(s
->data
^ s
->iev
) & mask
;
143 /* Level interrupt */
144 s
->istate
|= ~(s
->data
^ s
->iev
) & s
->isense
;
146 DPRINTF("istate = %02X\n", s
->istate
);
148 qemu_set_irq(s
->irq
, (s
->istate
& s
->im
) != 0);
151 static uint64_t pl061_read(void *opaque
, hwaddr offset
,
154 PL061State
*s
= (PL061State
*)opaque
;
156 if (offset
>= 0xfd0 && offset
< 0x1000) {
157 return s
->id
[(offset
- 0xfd0) >> 2];
159 if (offset
< 0x400) {
160 return s
->data
& (offset
>> 2);
163 case 0x400: /* Direction */
165 case 0x404: /* Interrupt sense */
167 case 0x408: /* Interrupt both edges */
169 case 0x40c: /* Interrupt event */
171 case 0x410: /* Interrupt mask */
173 case 0x414: /* Raw interrupt status */
175 case 0x418: /* Masked interrupt status */
176 return s
->istate
| s
->im
;
177 case 0x420: /* Alternate function select */
179 case 0x500: /* 2mA drive */
181 case 0x504: /* 4mA drive */
183 case 0x508: /* 8mA drive */
185 case 0x50c: /* Open drain */
187 case 0x510: /* Pull-up */
189 case 0x514: /* Pull-down */
191 case 0x518: /* Slew rate control */
193 case 0x51c: /* Digital enable */
195 case 0x520: /* Lock */
197 case 0x524: /* Commit */
199 case 0x528: /* Analog mode select */
202 qemu_log_mask(LOG_GUEST_ERROR
,
203 "pl061_read: Bad offset %x\n", (int)offset
);
208 static void pl061_write(void *opaque
, hwaddr offset
,
209 uint64_t value
, unsigned size
)
211 PL061State
*s
= (PL061State
*)opaque
;
214 if (offset
< 0x400) {
215 mask
= (offset
>> 2) & s
->dir
;
216 s
->data
= (s
->data
& ~mask
) | (value
& mask
);
221 case 0x400: /* Direction */
222 s
->dir
= value
& 0xff;
224 case 0x404: /* Interrupt sense */
225 s
->isense
= value
& 0xff;
227 case 0x408: /* Interrupt both edges */
228 s
->ibe
= value
& 0xff;
230 case 0x40c: /* Interrupt event */
231 s
->iev
= value
& 0xff;
233 case 0x410: /* Interrupt mask */
234 s
->im
= value
& 0xff;
236 case 0x41c: /* Interrupt clear */
239 case 0x420: /* Alternate function select */
241 s
->afsel
= (s
->afsel
& ~mask
) | (value
& mask
);
243 case 0x500: /* 2mA drive */
244 s
->dr2r
= value
& 0xff;
246 case 0x504: /* 4mA drive */
247 s
->dr4r
= value
& 0xff;
249 case 0x508: /* 8mA drive */
250 s
->dr8r
= value
& 0xff;
252 case 0x50c: /* Open drain */
253 s
->odr
= value
& 0xff;
255 case 0x510: /* Pull-up */
256 s
->pur
= value
& 0xff;
258 case 0x514: /* Pull-down */
259 s
->pdr
= value
& 0xff;
261 case 0x518: /* Slew rate control */
262 s
->slr
= value
& 0xff;
264 case 0x51c: /* Digital enable */
265 s
->den
= value
& 0xff;
267 case 0x520: /* Lock */
268 s
->locked
= (value
!= 0xacce551);
270 case 0x524: /* Commit */
272 s
->cr
= value
& 0xff;
275 s
->amsel
= value
& 0xff;
278 qemu_log_mask(LOG_GUEST_ERROR
,
279 "pl061_write: Bad offset %x\n", (int)offset
);
284 static void pl061_reset(PL061State
*s
)
290 static void pl061_set_irq(void * opaque
, int irq
, int level
)
292 PL061State
*s
= (PL061State
*)opaque
;
296 if ((s
->dir
& mask
) == 0) {
304 static const MemoryRegionOps pl061_ops
= {
306 .write
= pl061_write
,
307 .endianness
= DEVICE_NATIVE_ENDIAN
,
310 static int pl061_initfn(SysBusDevice
*sbd
)
312 DeviceState
*dev
= DEVICE(sbd
);
313 PL061State
*s
= PL061(dev
);
315 memory_region_init_io(&s
->iomem
, OBJECT(s
), &pl061_ops
, s
, "pl061", 0x1000);
316 sysbus_init_mmio(sbd
, &s
->iomem
);
317 sysbus_init_irq(sbd
, &s
->irq
);
318 qdev_init_gpio_in(dev
, pl061_set_irq
, 8);
319 qdev_init_gpio_out(dev
, s
->out
, 8);
324 static void pl061_luminary_init(Object
*obj
)
326 PL061State
*s
= PL061(obj
);
328 s
->id
= pl061_id_luminary
;
331 static void pl061_init(Object
*obj
)
333 PL061State
*s
= PL061(obj
);
338 static void pl061_class_init(ObjectClass
*klass
, void *data
)
340 DeviceClass
*dc
= DEVICE_CLASS(klass
);
341 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
343 k
->init
= pl061_initfn
;
344 dc
->vmsd
= &vmstate_pl061
;
347 static const TypeInfo pl061_info
= {
349 .parent
= TYPE_SYS_BUS_DEVICE
,
350 .instance_size
= sizeof(PL061State
),
351 .instance_init
= pl061_init
,
352 .class_init
= pl061_class_init
,
355 static const TypeInfo pl061_luminary_info
= {
356 .name
= "pl061_luminary",
357 .parent
= TYPE_PL061
,
358 .instance_init
= pl061_luminary_init
,
361 static void pl061_register_types(void)
363 type_register_static(&pl061_info
);
364 type_register_static(&pl061_luminary_info
);
367 type_init(pl061_register_types
)