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.
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 };
57 const unsigned char *id
;
60 static const VMStateDescription vmstate_pl061
= {
63 .minimum_version_id
= 1,
64 .fields
= (VMStateField
[]) {
65 VMSTATE_UINT32(locked
, pl061_state
),
66 VMSTATE_UINT32(data
, pl061_state
),
67 VMSTATE_UINT32(old_data
, pl061_state
),
68 VMSTATE_UINT32(dir
, pl061_state
),
69 VMSTATE_UINT32(isense
, pl061_state
),
70 VMSTATE_UINT32(ibe
, pl061_state
),
71 VMSTATE_UINT32(iev
, pl061_state
),
72 VMSTATE_UINT32(im
, pl061_state
),
73 VMSTATE_UINT32(istate
, pl061_state
),
74 VMSTATE_UINT32(afsel
, pl061_state
),
75 VMSTATE_UINT32(dr2r
, pl061_state
),
76 VMSTATE_UINT32(dr4r
, pl061_state
),
77 VMSTATE_UINT32(dr8r
, pl061_state
),
78 VMSTATE_UINT32(odr
, pl061_state
),
79 VMSTATE_UINT32(pur
, pl061_state
),
80 VMSTATE_UINT32(pdr
, pl061_state
),
81 VMSTATE_UINT32(slr
, pl061_state
),
82 VMSTATE_UINT32(den
, pl061_state
),
83 VMSTATE_UINT32(cr
, pl061_state
),
84 VMSTATE_UINT32(float_high
, pl061_state
),
85 VMSTATE_UINT32_V(amsel
, pl061_state
, 2),
90 static void pl061_update(pl061_state
*s
)
97 /* Outputs float high. */
98 /* FIXME: This is board dependent. */
99 out
= (s
->data
& s
->dir
) | ~s
->dir
;
100 changed
= s
->old_data
^ out
;
105 for (i
= 0; i
< 8; i
++) {
107 if (changed
& mask
) {
108 DPRINTF("Set output %d = %d\n", i
, (out
& mask
) != 0);
109 qemu_set_irq(s
->out
[i
], (out
& mask
) != 0);
113 /* FIXME: Implement input interrupts. */
116 static uint64_t pl061_read(void *opaque
, target_phys_addr_t offset
,
119 pl061_state
*s
= (pl061_state
*)opaque
;
121 if (offset
>= 0xfd0 && offset
< 0x1000) {
122 return s
->id
[(offset
- 0xfd0) >> 2];
124 if (offset
< 0x400) {
125 return s
->data
& (offset
>> 2);
128 case 0x400: /* Direction */
130 case 0x404: /* Interrupt sense */
132 case 0x408: /* Interrupt both edges */
134 case 0x40c: /* Interrupt event */
136 case 0x410: /* Interrupt mask */
138 case 0x414: /* Raw interrupt status */
140 case 0x418: /* Masked interrupt status */
141 return s
->istate
| s
->im
;
142 case 0x420: /* Alternate function select */
144 case 0x500: /* 2mA drive */
146 case 0x504: /* 4mA drive */
148 case 0x508: /* 8mA drive */
150 case 0x50c: /* Open drain */
152 case 0x510: /* Pull-up */
154 case 0x514: /* Pull-down */
156 case 0x518: /* Slew rate control */
158 case 0x51c: /* Digital enable */
160 case 0x520: /* Lock */
162 case 0x524: /* Commit */
164 case 0x528: /* Analog mode select */
167 hw_error("pl061_read: Bad offset %x\n", (int)offset
);
172 static void pl061_write(void *opaque
, target_phys_addr_t offset
,
173 uint64_t value
, unsigned size
)
175 pl061_state
*s
= (pl061_state
*)opaque
;
178 if (offset
< 0x400) {
179 mask
= (offset
>> 2) & s
->dir
;
180 s
->data
= (s
->data
& ~mask
) | (value
& mask
);
185 case 0x400: /* Direction */
186 s
->dir
= value
& 0xff;
188 case 0x404: /* Interrupt sense */
189 s
->isense
= value
& 0xff;
191 case 0x408: /* Interrupt both edges */
192 s
->ibe
= value
& 0xff;
194 case 0x40c: /* Interrupt event */
195 s
->iev
= value
& 0xff;
197 case 0x410: /* Interrupt mask */
198 s
->im
= value
& 0xff;
200 case 0x41c: /* Interrupt clear */
203 case 0x420: /* Alternate function select */
205 s
->afsel
= (s
->afsel
& ~mask
) | (value
& mask
);
207 case 0x500: /* 2mA drive */
208 s
->dr2r
= value
& 0xff;
210 case 0x504: /* 4mA drive */
211 s
->dr4r
= value
& 0xff;
213 case 0x508: /* 8mA drive */
214 s
->dr8r
= value
& 0xff;
216 case 0x50c: /* Open drain */
217 s
->odr
= value
& 0xff;
219 case 0x510: /* Pull-up */
220 s
->pur
= value
& 0xff;
222 case 0x514: /* Pull-down */
223 s
->pdr
= value
& 0xff;
225 case 0x518: /* Slew rate control */
226 s
->slr
= value
& 0xff;
228 case 0x51c: /* Digital enable */
229 s
->den
= value
& 0xff;
231 case 0x520: /* Lock */
232 s
->locked
= (value
!= 0xacce551);
234 case 0x524: /* Commit */
236 s
->cr
= value
& 0xff;
239 s
->amsel
= value
& 0xff;
242 hw_error("pl061_write: Bad offset %x\n", (int)offset
);
247 static void pl061_reset(pl061_state
*s
)
253 static void pl061_set_irq(void * opaque
, int irq
, int level
)
255 pl061_state
*s
= (pl061_state
*)opaque
;
259 if ((s
->dir
& mask
) == 0) {
267 static const MemoryRegionOps pl061_ops
= {
269 .write
= pl061_write
,
270 .endianness
= DEVICE_NATIVE_ENDIAN
,
273 static int pl061_init(SysBusDevice
*dev
, const unsigned char *id
)
275 pl061_state
*s
= FROM_SYSBUS(pl061_state
, dev
);
277 memory_region_init_io(&s
->iomem
, &pl061_ops
, s
, "pl061", 0x1000);
278 sysbus_init_mmio(dev
, &s
->iomem
);
279 sysbus_init_irq(dev
, &s
->irq
);
280 qdev_init_gpio_in(&dev
->qdev
, pl061_set_irq
, 8);
281 qdev_init_gpio_out(&dev
->qdev
, s
->out
, 8);
286 static int pl061_init_luminary(SysBusDevice
*dev
)
288 return pl061_init(dev
, pl061_id_luminary
);
291 static int pl061_init_arm(SysBusDevice
*dev
)
293 return pl061_init(dev
, pl061_id
);
296 static SysBusDeviceInfo pl061_info
= {
297 .init
= pl061_init_arm
,
298 .qdev
.name
= "pl061",
299 .qdev
.size
= sizeof(pl061_state
),
300 .qdev
.vmsd
= &vmstate_pl061
,
303 static SysBusDeviceInfo pl061_luminary_info
= {
304 .init
= pl061_init_luminary
,
305 .qdev
.name
= "pl061_luminary",
306 .qdev
.size
= sizeof(pl061_state
),
307 .qdev
.vmsd
= &vmstate_pl061
,
310 static void pl061_register_devices(void)
312 sysbus_register_withprop(&pl061_info
);
313 sysbus_register_withprop(&pl061_luminary_info
);
316 device_init(pl061_register_devices
)