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 "hw/sysbus.h"
24 #define TYPE_MPC8XXX_GPIO "mpc8xxx_gpio"
25 #define MPC8XXX_GPIO(obj) OBJECT_CHECK(MPC8XXXGPIOState, (obj), TYPE_MPC8XXX_GPIO)
27 typedef struct MPC8XXXGPIOState
{
28 SysBusDevice parent_obj
;
42 static const VMStateDescription vmstate_mpc8xxx_gpio
= {
43 .name
= "mpc8xxx_gpio",
45 .minimum_version_id
= 1,
46 .fields
= (VMStateField
[]) {
47 VMSTATE_UINT32(dir
, MPC8XXXGPIOState
),
48 VMSTATE_UINT32(odr
, MPC8XXXGPIOState
),
49 VMSTATE_UINT32(dat
, MPC8XXXGPIOState
),
50 VMSTATE_UINT32(ier
, MPC8XXXGPIOState
),
51 VMSTATE_UINT32(imr
, MPC8XXXGPIOState
),
52 VMSTATE_UINT32(icr
, MPC8XXXGPIOState
),
57 static void mpc8xxx_gpio_update(MPC8XXXGPIOState
*s
)
59 qemu_set_irq(s
->irq
, !!(s
->ier
& s
->imr
));
62 static uint64_t mpc8xxx_gpio_read(void *opaque
, hwaddr offset
,
65 MPC8XXXGPIOState
*s
= (MPC8XXXGPIOState
*)opaque
;
68 /* All registers are 32bit */
73 case 0x0: /* Direction */
75 case 0x4: /* Open Drain */
79 case 0xC: /* Interrupt Event */
81 case 0x10: /* Interrupt Mask */
83 case 0x14: /* Interrupt Control */
90 static void mpc8xxx_write_data(MPC8XXXGPIOState
*s
, uint32_t new_data
)
92 uint32_t old_data
= s
->dat
;
93 uint32_t diff
= old_data
^ new_data
;
96 for (i
= 0; i
< 32; i
++) {
97 uint32_t mask
= 0x80000000 >> i
;
104 qemu_set_irq(s
->out
[i
], (new_data
& mask
) != 0);
111 static void mpc8xxx_gpio_write(void *opaque
, hwaddr offset
,
112 uint64_t value
, unsigned size
)
114 MPC8XXXGPIOState
*s
= (MPC8XXXGPIOState
*)opaque
;
117 /* All registers are 32bit */
122 case 0x0: /* Direction */
125 case 0x4: /* Open Drain */
129 mpc8xxx_write_data(s
, value
);
131 case 0xC: /* Interrupt Event */
134 case 0x10: /* Interrupt Mask */
137 case 0x14: /* Interrupt Control */
142 mpc8xxx_gpio_update(s
);
145 static void mpc8xxx_gpio_reset(MPC8XXXGPIOState
*s
)
155 static void mpc8xxx_gpio_set_irq(void * opaque
, int irq
, int level
)
157 MPC8XXXGPIOState
*s
= (MPC8XXXGPIOState
*)opaque
;
160 mask
= 0x80000000 >> irq
;
161 if ((s
->dir
& mask
) == 0) {
162 uint32_t old_value
= s
->dat
& mask
;
168 if (!(s
->icr
& irq
) || (old_value
&& !level
)) {
172 mpc8xxx_gpio_update(s
);
176 static const MemoryRegionOps mpc8xxx_gpio_ops
= {
177 .read
= mpc8xxx_gpio_read
,
178 .write
= mpc8xxx_gpio_write
,
179 .endianness
= DEVICE_BIG_ENDIAN
,
182 static int mpc8xxx_gpio_initfn(SysBusDevice
*sbd
)
184 DeviceState
*dev
= DEVICE(sbd
);
185 MPC8XXXGPIOState
*s
= MPC8XXX_GPIO(dev
);
187 memory_region_init_io(&s
->iomem
, OBJECT(s
), &mpc8xxx_gpio_ops
, s
, "mpc8xxx_gpio", 0x1000);
188 sysbus_init_mmio(sbd
, &s
->iomem
);
189 sysbus_init_irq(sbd
, &s
->irq
);
190 qdev_init_gpio_in(dev
, mpc8xxx_gpio_set_irq
, 32);
191 qdev_init_gpio_out(dev
, s
->out
, 32);
192 mpc8xxx_gpio_reset(s
);
196 static void mpc8xxx_gpio_class_init(ObjectClass
*klass
, void *data
)
198 DeviceClass
*dc
= DEVICE_CLASS(klass
);
199 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
201 k
->init
= mpc8xxx_gpio_initfn
;
202 dc
->vmsd
= &vmstate_mpc8xxx_gpio
;
205 static const TypeInfo mpc8xxx_gpio_info
= {
206 .name
= TYPE_MPC8XXX_GPIO
,
207 .parent
= TYPE_SYS_BUS_DEVICE
,
208 .instance_size
= sizeof(MPC8XXXGPIOState
),
209 .class_init
= mpc8xxx_gpio_class_init
,
212 static void mpc8xxx_gpio_register_types(void)
214 type_register_static(&mpc8xxx_gpio_info
);
217 type_init(mpc8xxx_gpio_register_types
)