xref: /openbmc/qemu/hw/gpio/gpio_pwr.c (revision d2dfe0b5)
1 /*
2  * GPIO qemu power controller
3  *
4  * Copyright (c) 2020 Linaro Limited
5  *
6  * Author: Maxim Uvarov <maxim.uvarov@linaro.org>
7  *
8  * Virtual gpio driver which can be used on top of pl061
9  * to reboot and shutdown qemu virtual machine. One of use
10  * case is gpio driver for secure world application (ARM
11  * Trusted Firmware.).
12  *
13  * This work is licensed under the terms of the GNU GPL, version 2 or later.
14  * See the COPYING file in the top-level directory.
15  * SPDX-License-Identifier: GPL-2.0-or-later
16  */
17 
18 /*
19  * QEMU interface:
20  * two named input GPIO lines:
21  *   'reset' : when asserted, trigger system reset
22  *   'shutdown' : when asserted, trigger system shutdown
23  */
24 
25 #include "qemu/osdep.h"
26 #include "hw/sysbus.h"
27 #include "sysemu/runstate.h"
28 
29 #define TYPE_GPIOPWR "gpio-pwr"
30 OBJECT_DECLARE_SIMPLE_TYPE(GPIO_PWR_State, GPIOPWR)
31 
32 struct GPIO_PWR_State {
33     SysBusDevice parent_obj;
34 };
35 
36 static void gpio_pwr_reset(void *opaque, int n, int level)
37 {
38     if (level) {
39         qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
40     }
41 }
42 
43 static void gpio_pwr_shutdown(void *opaque, int n, int level)
44 {
45     if (level) {
46         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
47     }
48 }
49 
50 static void gpio_pwr_init(Object *obj)
51 {
52     DeviceState *dev = DEVICE(obj);
53 
54     qdev_init_gpio_in_named(dev, gpio_pwr_reset, "reset", 1);
55     qdev_init_gpio_in_named(dev, gpio_pwr_shutdown, "shutdown", 1);
56 }
57 
58 static const TypeInfo gpio_pwr_info = {
59     .name          = TYPE_GPIOPWR,
60     .parent        = TYPE_SYS_BUS_DEVICE,
61     .instance_size = sizeof(GPIO_PWR_State),
62     .instance_init = gpio_pwr_init,
63 };
64 
65 static void gpio_pwr_register_types(void)
66 {
67     type_register_static(&gpio_pwr_info);
68 }
69 
70 type_init(gpio_pwr_register_types)
71