2 * nRF51 System-on-Chip general purpose input/output register definition
4 * Reference Manual: http://infocenter.nordicsemi.com/pdf/nRF51_RM_v3.0.pdf
5 * Product Spec: http://infocenter.nordicsemi.com/pdf/nRF51822_PS_v3.1.pdf
7 * Copyright 2018 Steffen Görtz <contrib@steffen-goertz.de>
9 * This code is licensed under the GPL version 2 or later. See
10 * the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
15 #include "hw/gpio/nrf51_gpio.h"
19 * Check if the output driver is connected to the direction switch
20 * given the current configuration and logic level.
21 * It is not differentiated between standard and "high"(-power) drive modes.
23 static bool is_connected(uint32_t config
, uint32_t level
)
26 uint32_t drive_config
= extract32(config
, 8, 3);
28 switch (drive_config
) {
39 g_assert_not_reached();
46 static void update_output_irq(NRF51GPIOState
*s
, size_t i
,
47 bool connected
, bool level
)
49 int64_t irq_level
= connected
? level
: -1;
50 bool old_connected
= extract32(s
->old_out_connected
, i
, 1);
51 bool old_level
= extract32(s
->old_out
, i
, 1);
53 if ((old_connected
!= connected
) || (old_level
!= level
)) {
54 qemu_set_irq(s
->output
[i
], irq_level
);
55 trace_nrf51_gpio_update_output_irq(i
, irq_level
);
58 s
->old_out
= deposit32(s
->old_out
, i
, 1, level
);
59 s
->old_out_connected
= deposit32(s
->old_out_connected
, i
, 1, connected
);
62 static void update_state(NRF51GPIOState
*s
)
66 bool connected_out
, dir
, connected_in
, out
, input
;
68 for (i
= 0; i
< NRF51_GPIO_PINS
; i
++) {
69 pull
= extract32(s
->cnf
[i
], 2, 2);
70 dir
= extract32(s
->cnf
[i
], 0, 1);
71 connected_in
= extract32(s
->in_mask
, i
, 1);
72 out
= extract32(s
->out
, i
, 1);
73 input
= !extract32(s
->cnf
[i
], 1, 1);
74 connected_out
= is_connected(s
->cnf
[i
], out
) && dir
;
76 update_output_irq(s
, i
, connected_out
, out
);
78 /* Pin both driven externally and internally */
79 if (connected_out
&& connected_in
) {
80 qemu_log_mask(LOG_GUEST_ERROR
, "GPIO pin %zu short circuited\n", i
);
84 * Input buffer disconnected from internal/external drives, so
85 * pull-up/pull-down becomes relevant
87 if (!input
|| (input
&& !connected_in
&& !connected_out
)) {
88 if (pull
== NRF51_GPIO_PULLDOWN
) {
89 s
->in
= deposit32(s
->in
, i
, 1, 0);
90 } else if (pull
== NRF51_GPIO_PULLUP
) {
91 s
->in
= deposit32(s
->in
, i
, 1, 1);
95 /* Self stimulation through internal output driver */
96 if (connected_out
&& !connected_in
&& input
) {
97 s
->in
= deposit32(s
->in
, i
, 1, out
);
104 * Direction is exposed in both the DIR register and the DIR bit
105 * of each PINs CNF configuration register. Reflect bits for pins in DIR
106 * to individual pin configuration registers.
108 static void reflect_dir_bit_in_cnf(NRF51GPIOState
*s
)
112 uint32_t value
= s
->dir
;
114 for (i
= 0; i
< NRF51_GPIO_PINS
; i
++) {
115 s
->cnf
[i
] = (s
->cnf
[i
] & ~(1UL)) | ((value
>> i
) & 0x01);
119 static uint64_t nrf51_gpio_read(void *opaque
, hwaddr offset
, unsigned int size
)
121 NRF51GPIOState
*s
= NRF51_GPIO(opaque
);
126 case NRF51_GPIO_REG_OUT
... NRF51_GPIO_REG_OUTCLR
:
130 case NRF51_GPIO_REG_IN
:
134 case NRF51_GPIO_REG_DIR
... NRF51_GPIO_REG_DIRCLR
:
138 case NRF51_GPIO_REG_CNF_START
... NRF51_GPIO_REG_CNF_END
:
139 idx
= (offset
- NRF51_GPIO_REG_CNF_START
) / 4;
144 qemu_log_mask(LOG_GUEST_ERROR
,
145 "%s: bad read offset 0x%" HWADDR_PRIx
"\n",
149 trace_nrf51_gpio_read(offset
, r
);
154 static void nrf51_gpio_write(void *opaque
, hwaddr offset
,
155 uint64_t value
, unsigned int size
)
157 NRF51GPIOState
*s
= NRF51_GPIO(opaque
);
160 trace_nrf51_gpio_write(offset
, value
);
163 case NRF51_GPIO_REG_OUT
:
167 case NRF51_GPIO_REG_OUTSET
:
171 case NRF51_GPIO_REG_OUTCLR
:
175 case NRF51_GPIO_REG_DIR
:
177 reflect_dir_bit_in_cnf(s
);
180 case NRF51_GPIO_REG_DIRSET
:
182 reflect_dir_bit_in_cnf(s
);
185 case NRF51_GPIO_REG_DIRCLR
:
187 reflect_dir_bit_in_cnf(s
);
190 case NRF51_GPIO_REG_CNF_START
... NRF51_GPIO_REG_CNF_END
:
191 idx
= (offset
- NRF51_GPIO_REG_CNF_START
) / 4;
194 * direction is exposed in both the DIR register and the DIR bit
195 * of each PINs CNF configuration register.
197 s
->dir
= (s
->dir
& ~(1UL << idx
)) | ((value
& 0x01) << idx
);
201 qemu_log_mask(LOG_GUEST_ERROR
,
202 "%s: bad write offset 0x%" HWADDR_PRIx
"\n",
209 static const MemoryRegionOps gpio_ops
= {
210 .read
= nrf51_gpio_read
,
211 .write
= nrf51_gpio_write
,
212 .endianness
= DEVICE_LITTLE_ENDIAN
,
213 .impl
.min_access_size
= 4,
214 .impl
.max_access_size
= 4,
217 static void nrf51_gpio_set(void *opaque
, int line
, int value
)
219 NRF51GPIOState
*s
= NRF51_GPIO(opaque
);
221 trace_nrf51_gpio_set(line
, value
);
223 assert(line
>= 0 && line
< NRF51_GPIO_PINS
);
225 s
->in_mask
= deposit32(s
->in_mask
, line
, 1, value
>= 0);
227 s
->in
= deposit32(s
->in
, line
, 1, value
!= 0);
233 static void nrf51_gpio_reset(DeviceState
*dev
)
235 NRF51GPIOState
*s
= NRF51_GPIO(dev
);
240 s
->old_out_connected
= 0;
245 for (i
= 0; i
< NRF51_GPIO_PINS
; i
++) {
246 s
->cnf
[i
] = 0x00000002;
250 static const VMStateDescription vmstate_nrf51_gpio
= {
251 .name
= TYPE_NRF51_GPIO
,
253 .minimum_version_id
= 1,
254 .fields
= (VMStateField
[]) {
255 VMSTATE_UINT32(out
, NRF51GPIOState
),
256 VMSTATE_UINT32(in
, NRF51GPIOState
),
257 VMSTATE_UINT32(in_mask
, NRF51GPIOState
),
258 VMSTATE_UINT32(dir
, NRF51GPIOState
),
259 VMSTATE_UINT32_ARRAY(cnf
, NRF51GPIOState
, NRF51_GPIO_PINS
),
260 VMSTATE_UINT32(old_out
, NRF51GPIOState
),
261 VMSTATE_UINT32(old_out_connected
, NRF51GPIOState
),
262 VMSTATE_END_OF_LIST()
266 static void nrf51_gpio_init(Object
*obj
)
268 NRF51GPIOState
*s
= NRF51_GPIO(obj
);
270 memory_region_init_io(&s
->mmio
, obj
, &gpio_ops
, s
,
271 TYPE_NRF51_GPIO
, NRF51_GPIO_SIZE
);
272 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->mmio
);
274 qdev_init_gpio_in(DEVICE(s
), nrf51_gpio_set
, NRF51_GPIO_PINS
);
275 qdev_init_gpio_out(DEVICE(s
), s
->output
, NRF51_GPIO_PINS
);
278 static void nrf51_gpio_class_init(ObjectClass
*klass
, void *data
)
280 DeviceClass
*dc
= DEVICE_CLASS(klass
);
282 dc
->vmsd
= &vmstate_nrf51_gpio
;
283 dc
->reset
= nrf51_gpio_reset
;
284 dc
->desc
= "nRF51 GPIO";
287 static const TypeInfo nrf51_gpio_info
= {
288 .name
= TYPE_NRF51_GPIO
,
289 .parent
= TYPE_SYS_BUS_DEVICE
,
290 .instance_size
= sizeof(NRF51GPIOState
),
291 .instance_init
= nrf51_gpio_init
,
292 .class_init
= nrf51_gpio_class_init
295 static void nrf51_gpio_register_types(void)
297 type_register_static(&nrf51_gpio_info
);
300 type_init(nrf51_gpio_register_types
)