2 * GPIO Controller for a lot of Freescale SoCs
4 * Copyright (C) 2014 Freescale Semiconductor, Inc. All rights reserved.
6 * Author: Alexander Graf, <agraf@suse.de>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "hw/sysbus.h"
25 #define TYPE_MPC8XXX_GPIO "mpc8xxx_gpio"
26 #define MPC8XXX_GPIO(obj) OBJECT_CHECK(MPC8XXXGPIOState, (obj), TYPE_MPC8XXX_GPIO)
28 typedef struct MPC8XXXGPIOState
{
29 SysBusDevice parent_obj
;
43 static const VMStateDescription vmstate_mpc8xxx_gpio
= {
44 .name
= "mpc8xxx_gpio",
46 .minimum_version_id
= 1,
47 .fields
= (VMStateField
[]) {
48 VMSTATE_UINT32(dir
, MPC8XXXGPIOState
),
49 VMSTATE_UINT32(odr
, MPC8XXXGPIOState
),
50 VMSTATE_UINT32(dat
, MPC8XXXGPIOState
),
51 VMSTATE_UINT32(ier
, MPC8XXXGPIOState
),
52 VMSTATE_UINT32(imr
, MPC8XXXGPIOState
),
53 VMSTATE_UINT32(icr
, MPC8XXXGPIOState
),
58 static void mpc8xxx_gpio_update(MPC8XXXGPIOState
*s
)
60 qemu_set_irq(s
->irq
, !!(s
->ier
& s
->imr
));
63 static uint64_t mpc8xxx_gpio_read(void *opaque
, hwaddr offset
,
66 MPC8XXXGPIOState
*s
= (MPC8XXXGPIOState
*)opaque
;
69 /* All registers are 32bit */
74 case 0x0: /* Direction */
76 case 0x4: /* Open Drain */
80 case 0xC: /* Interrupt Event */
82 case 0x10: /* Interrupt Mask */
84 case 0x14: /* Interrupt Control */
91 static void mpc8xxx_write_data(MPC8XXXGPIOState
*s
, uint32_t new_data
)
93 uint32_t old_data
= s
->dat
;
94 uint32_t diff
= old_data
^ new_data
;
97 for (i
= 0; i
< 32; i
++) {
98 uint32_t mask
= 0x80000000 >> i
;
105 qemu_set_irq(s
->out
[i
], (new_data
& mask
) != 0);
112 static void mpc8xxx_gpio_write(void *opaque
, hwaddr offset
,
113 uint64_t value
, unsigned size
)
115 MPC8XXXGPIOState
*s
= (MPC8XXXGPIOState
*)opaque
;
118 /* All registers are 32bit */
123 case 0x0: /* Direction */
126 case 0x4: /* Open Drain */
130 mpc8xxx_write_data(s
, value
);
132 case 0xC: /* Interrupt Event */
135 case 0x10: /* Interrupt Mask */
138 case 0x14: /* Interrupt Control */
143 mpc8xxx_gpio_update(s
);
146 static void mpc8xxx_gpio_reset(MPC8XXXGPIOState
*s
)
156 static void mpc8xxx_gpio_set_irq(void * opaque
, int irq
, int level
)
158 MPC8XXXGPIOState
*s
= (MPC8XXXGPIOState
*)opaque
;
161 mask
= 0x80000000 >> irq
;
162 if ((s
->dir
& mask
) == 0) {
163 uint32_t old_value
= s
->dat
& mask
;
169 if (!(s
->icr
& irq
) || (old_value
&& !level
)) {
173 mpc8xxx_gpio_update(s
);
177 static const MemoryRegionOps mpc8xxx_gpio_ops
= {
178 .read
= mpc8xxx_gpio_read
,
179 .write
= mpc8xxx_gpio_write
,
180 .endianness
= DEVICE_BIG_ENDIAN
,
183 static int mpc8xxx_gpio_initfn(SysBusDevice
*sbd
)
185 DeviceState
*dev
= DEVICE(sbd
);
186 MPC8XXXGPIOState
*s
= MPC8XXX_GPIO(dev
);
188 memory_region_init_io(&s
->iomem
, OBJECT(s
), &mpc8xxx_gpio_ops
, s
, "mpc8xxx_gpio", 0x1000);
189 sysbus_init_mmio(sbd
, &s
->iomem
);
190 sysbus_init_irq(sbd
, &s
->irq
);
191 qdev_init_gpio_in(dev
, mpc8xxx_gpio_set_irq
, 32);
192 qdev_init_gpio_out(dev
, s
->out
, 32);
193 mpc8xxx_gpio_reset(s
);
197 static void mpc8xxx_gpio_class_init(ObjectClass
*klass
, void *data
)
199 DeviceClass
*dc
= DEVICE_CLASS(klass
);
200 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
202 k
->init
= mpc8xxx_gpio_initfn
;
203 dc
->vmsd
= &vmstate_mpc8xxx_gpio
;
206 static const TypeInfo mpc8xxx_gpio_info
= {
207 .name
= TYPE_MPC8XXX_GPIO
,
208 .parent
= TYPE_SYS_BUS_DEVICE
,
209 .instance_size
= sizeof(MPC8XXXGPIOState
),
210 .class_init
= mpc8xxx_gpio_class_init
,
213 static void mpc8xxx_gpio_register_types(void)
215 type_register_static(&mpc8xxx_gpio_info
);
218 type_init(mpc8xxx_gpio_register_types
)