2 * PowerMac NewWorld MacIO GPIO emulation
4 * Copyright (c) 2016 Benjamin Herrenschmidt
5 * Copyright (c) 2018 Mark Cave-Ayland
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "hw/ppc/mac.h"
28 #include "hw/qdev-properties.h"
29 #include "migration/vmstate.h"
30 #include "hw/misc/macio/macio.h"
31 #include "hw/misc/macio/gpio.h"
34 #include "qemu/module.h"
38 void macio_set_gpio(MacIOGPIOState
*s
, uint32_t gpio
, bool state
)
42 trace_macio_set_gpio(gpio
, state
);
44 if (s
->gpio_regs
[gpio
] & 4) {
45 qemu_log_mask(LOG_GUEST_ERROR
,
46 "GPIO: Setting GPIO %d while it's an output\n", gpio
);
49 new_reg
= s
->gpio_regs
[gpio
] & ~2;
54 if (new_reg
== s
->gpio_regs
[gpio
]) {
58 s
->gpio_regs
[gpio
] = new_reg
;
60 /* This is will work until we fix the binding between MacIO and
61 * the MPIC properly so we can route all GPIOs and avoid going
62 * via the top level platform code.
64 * Note that we probably need to get access to the MPIC config to
65 * decode polarity since qemu always use "raise" regardless.
67 * For now, we hard wire known GPIOs
74 trace_macio_gpio_irq_assert(gpio
);
75 qemu_irq_raise(s
->gpio_extirqs
[gpio
]);
77 trace_macio_gpio_irq_deassert(gpio
);
78 qemu_irq_lower(s
->gpio_extirqs
[gpio
]);
83 /* Edge, triggered by NMI below */
85 trace_macio_gpio_irq_assert(gpio
);
86 qemu_irq_raise(s
->gpio_extirqs
[gpio
]);
88 trace_macio_gpio_irq_deassert(gpio
);
89 qemu_irq_lower(s
->gpio_extirqs
[gpio
]);
94 qemu_log_mask(LOG_UNIMP
, "GPIO: setting unimplemented GPIO %d", gpio
);
98 static void macio_gpio_write(void *opaque
, hwaddr addr
, uint64_t value
,
101 MacIOGPIOState
*s
= opaque
;
104 trace_macio_gpio_write(addr
, value
);
106 /* Levels regs are read-only */
116 ibit
= (value
& 1) << 1;
118 ibit
= s
->gpio_regs
[addr
] & 2;
121 s
->gpio_regs
[addr
] = value
| ibit
;
125 static uint64_t macio_gpio_read(void *opaque
, hwaddr addr
, unsigned size
)
127 MacIOGPIOState
*s
= opaque
;
132 val
= s
->gpio_levels
[addr
];
137 val
= s
->gpio_regs
[addr
];
141 trace_macio_gpio_write(addr
, val
);
145 static const MemoryRegionOps macio_gpio_ops
= {
146 .read
= macio_gpio_read
,
147 .write
= macio_gpio_write
,
148 .endianness
= DEVICE_LITTLE_ENDIAN
,
150 .min_access_size
= 1,
151 .max_access_size
= 1,
155 static void macio_gpio_realize(DeviceState
*dev
, Error
**errp
)
157 MacIOGPIOState
*s
= MACIO_GPIO(dev
);
159 s
->gpio_extirqs
[1] = qdev_get_gpio_in(DEVICE(s
->pic
),
160 NEWWORLD_EXTING_GPIO1
);
161 s
->gpio_extirqs
[9] = qdev_get_gpio_in(DEVICE(s
->pic
),
162 NEWWORLD_EXTING_GPIO9
);
165 static void macio_gpio_init(Object
*obj
)
167 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
168 MacIOGPIOState
*s
= MACIO_GPIO(obj
);
170 object_property_add_link(obj
, "pic", TYPE_OPENPIC
,
172 qdev_prop_allow_set_link_before_realize
,
175 memory_region_init_io(&s
->gpiomem
, OBJECT(s
), &macio_gpio_ops
, obj
,
177 sysbus_init_mmio(sbd
, &s
->gpiomem
);
180 static const VMStateDescription vmstate_macio_gpio
= {
181 .name
= "macio_gpio",
183 .minimum_version_id
= 0,
184 .fields
= (VMStateField
[]) {
185 VMSTATE_UINT8_ARRAY(gpio_levels
, MacIOGPIOState
, 8),
186 VMSTATE_UINT8_ARRAY(gpio_regs
, MacIOGPIOState
, 36),
187 VMSTATE_END_OF_LIST()
191 static void macio_gpio_reset(DeviceState
*dev
)
193 MacIOGPIOState
*s
= MACIO_GPIO(dev
);
195 /* GPIO 1 is up by default */
196 macio_set_gpio(s
, 1, true);
199 static void macio_gpio_nmi(NMIState
*n
, int cpu_index
, Error
**errp
)
201 macio_set_gpio(MACIO_GPIO(n
), 9, true);
202 macio_set_gpio(MACIO_GPIO(n
), 9, false);
205 static void macio_gpio_class_init(ObjectClass
*oc
, void *data
)
207 DeviceClass
*dc
= DEVICE_CLASS(oc
);
208 NMIClass
*nc
= NMI_CLASS(oc
);
210 dc
->realize
= macio_gpio_realize
;
211 dc
->reset
= macio_gpio_reset
;
212 dc
->vmsd
= &vmstate_macio_gpio
;
213 nc
->nmi_monitor_handler
= macio_gpio_nmi
;
216 static const TypeInfo macio_gpio_init_info
= {
217 .name
= TYPE_MACIO_GPIO
,
218 .parent
= TYPE_SYS_BUS_DEVICE
,
219 .instance_size
= sizeof(MacIOGPIOState
),
220 .instance_init
= macio_gpio_init
,
221 .class_init
= macio_gpio_class_init
,
222 .interfaces
= (InterfaceInfo
[]) {
228 static void macio_gpio_register_types(void)
230 type_register_static(&macio_gpio_init_info
);
233 type_init(macio_gpio_register_types
)