1f4427280SJean-Christophe Dubois /* 2f4427280SJean-Christophe Dubois * i.MX processors GPIO emulation. 3f4427280SJean-Christophe Dubois * 4f4427280SJean-Christophe Dubois * Copyright (C) 2015 Jean-Christophe Dubois <jcd@tribudubois.net> 5f4427280SJean-Christophe Dubois * 6f4427280SJean-Christophe Dubois * This program is free software; you can redistribute it and/or 7f4427280SJean-Christophe Dubois * modify it under the terms of the GNU General Public License as 8f4427280SJean-Christophe Dubois * published by the Free Software Foundation; either version 2 or 9f4427280SJean-Christophe Dubois * (at your option) version 3 of the License. 10f4427280SJean-Christophe Dubois * 11f4427280SJean-Christophe Dubois * This program is distributed in the hope that it will be useful, 12f4427280SJean-Christophe Dubois * but WITHOUT ANY WARRANTY; without even the implied warranty of 13f4427280SJean-Christophe Dubois * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14f4427280SJean-Christophe Dubois * GNU General Public License for more details. 15f4427280SJean-Christophe Dubois * 16f4427280SJean-Christophe Dubois * You should have received a copy of the GNU General Public License along 17f4427280SJean-Christophe Dubois * with this program; if not, see <http://www.gnu.org/licenses/>. 18f4427280SJean-Christophe Dubois */ 19f4427280SJean-Christophe Dubois 20f4427280SJean-Christophe Dubois #include "hw/gpio/imx_gpio.h" 21f4427280SJean-Christophe Dubois 22f4427280SJean-Christophe Dubois #ifndef DEBUG_IMX_GPIO 23f4427280SJean-Christophe Dubois #define DEBUG_IMX_GPIO 0 24f4427280SJean-Christophe Dubois #endif 25f4427280SJean-Christophe Dubois 26f4427280SJean-Christophe Dubois typedef enum IMXGPIOLevel { 27f4427280SJean-Christophe Dubois IMX_GPIO_LEVEL_LOW = 0, 28f4427280SJean-Christophe Dubois IMX_GPIO_LEVEL_HIGH = 1, 29f4427280SJean-Christophe Dubois } IMXGPIOLevel; 30f4427280SJean-Christophe Dubois 31f4427280SJean-Christophe Dubois #define DPRINTF(fmt, args...) \ 32f4427280SJean-Christophe Dubois do { \ 33f4427280SJean-Christophe Dubois if (DEBUG_IMX_GPIO) { \ 34*56411125SJean-Christophe Dubois fprintf(stderr, "[%s]%s: " fmt , TYPE_IMX_GPIO, \ 35*56411125SJean-Christophe Dubois __func__, ##args); \ 36f4427280SJean-Christophe Dubois } \ 37f4427280SJean-Christophe Dubois } while (0) 38f4427280SJean-Christophe Dubois 39f4427280SJean-Christophe Dubois static const char *imx_gpio_reg_name(uint32_t reg) 40f4427280SJean-Christophe Dubois { 41f4427280SJean-Christophe Dubois switch (reg) { 42f4427280SJean-Christophe Dubois case DR_ADDR: 43f4427280SJean-Christophe Dubois return "DR"; 44f4427280SJean-Christophe Dubois case GDIR_ADDR: 45f4427280SJean-Christophe Dubois return "GDIR"; 46f4427280SJean-Christophe Dubois case PSR_ADDR: 47f4427280SJean-Christophe Dubois return "PSR"; 48f4427280SJean-Christophe Dubois case ICR1_ADDR: 49f4427280SJean-Christophe Dubois return "ICR1"; 50f4427280SJean-Christophe Dubois case ICR2_ADDR: 51f4427280SJean-Christophe Dubois return "ICR2"; 52f4427280SJean-Christophe Dubois case IMR_ADDR: 53f4427280SJean-Christophe Dubois return "IMR"; 54f4427280SJean-Christophe Dubois case ISR_ADDR: 55f4427280SJean-Christophe Dubois return "ISR"; 56f4427280SJean-Christophe Dubois case EDGE_SEL_ADDR: 57f4427280SJean-Christophe Dubois return "EDGE_SEL"; 58f4427280SJean-Christophe Dubois default: 59f4427280SJean-Christophe Dubois return "[?]"; 60f4427280SJean-Christophe Dubois } 61f4427280SJean-Christophe Dubois } 62f4427280SJean-Christophe Dubois 63f4427280SJean-Christophe Dubois static void imx_gpio_update_int(IMXGPIOState *s) 64f4427280SJean-Christophe Dubois { 65f4427280SJean-Christophe Dubois qemu_set_irq(s->irq, (s->isr & s->imr) ? 1 : 0); 66f4427280SJean-Christophe Dubois } 67f4427280SJean-Christophe Dubois 68f4427280SJean-Christophe Dubois static void imx_gpio_set_int_line(IMXGPIOState *s, int line, IMXGPIOLevel level) 69f4427280SJean-Christophe Dubois { 70f4427280SJean-Christophe Dubois /* if this signal isn't configured as an input signal, nothing to do */ 71f4427280SJean-Christophe Dubois if (!extract32(s->gdir, line, 1)) { 72f4427280SJean-Christophe Dubois return; 73f4427280SJean-Christophe Dubois } 74f4427280SJean-Christophe Dubois 75f4427280SJean-Christophe Dubois /* When set, EDGE_SEL overrides the ICR config */ 76f4427280SJean-Christophe Dubois if (extract32(s->edge_sel, line, 1)) { 77f4427280SJean-Christophe Dubois /* we detect interrupt on rising and falling edge */ 78f4427280SJean-Christophe Dubois if (extract32(s->psr, line, 1) != level) { 79f4427280SJean-Christophe Dubois /* level changed */ 80f4427280SJean-Christophe Dubois s->isr = deposit32(s->isr, line, 1, 1); 81f4427280SJean-Christophe Dubois } 82f4427280SJean-Christophe Dubois } else if (extract64(s->icr, 2*line + 1, 1)) { 83f4427280SJean-Christophe Dubois /* interrupt is edge sensitive */ 84f4427280SJean-Christophe Dubois if (extract32(s->psr, line, 1) != level) { 85f4427280SJean-Christophe Dubois /* level changed */ 86f4427280SJean-Christophe Dubois if (extract64(s->icr, 2*line, 1) != level) { 87f4427280SJean-Christophe Dubois s->isr = deposit32(s->isr, line, 1, 1); 88f4427280SJean-Christophe Dubois } 89f4427280SJean-Christophe Dubois } 90f4427280SJean-Christophe Dubois } else { 91f4427280SJean-Christophe Dubois /* interrupt is level sensitive */ 92f4427280SJean-Christophe Dubois if (extract64(s->icr, 2*line, 1) == level) { 93f4427280SJean-Christophe Dubois s->isr = deposit32(s->isr, line, 1, 1); 94f4427280SJean-Christophe Dubois } 95f4427280SJean-Christophe Dubois } 96f4427280SJean-Christophe Dubois } 97f4427280SJean-Christophe Dubois 98f4427280SJean-Christophe Dubois static void imx_gpio_set(void *opaque, int line, int level) 99f4427280SJean-Christophe Dubois { 100f4427280SJean-Christophe Dubois IMXGPIOState *s = IMX_GPIO(opaque); 101f4427280SJean-Christophe Dubois IMXGPIOLevel imx_level = level ? IMX_GPIO_LEVEL_HIGH : IMX_GPIO_LEVEL_LOW; 102f4427280SJean-Christophe Dubois 103f4427280SJean-Christophe Dubois imx_gpio_set_int_line(s, line, imx_level); 104f4427280SJean-Christophe Dubois 105f4427280SJean-Christophe Dubois /* this is an input signal, so set PSR */ 106f4427280SJean-Christophe Dubois s->psr = deposit32(s->psr, line, 1, imx_level); 107f4427280SJean-Christophe Dubois 108f4427280SJean-Christophe Dubois imx_gpio_update_int(s); 109f4427280SJean-Christophe Dubois } 110f4427280SJean-Christophe Dubois 111f4427280SJean-Christophe Dubois static void imx_gpio_set_all_int_lines(IMXGPIOState *s) 112f4427280SJean-Christophe Dubois { 113f4427280SJean-Christophe Dubois int i; 114f4427280SJean-Christophe Dubois 115f4427280SJean-Christophe Dubois for (i = 0; i < IMX_GPIO_PIN_COUNT; i++) { 116f4427280SJean-Christophe Dubois IMXGPIOLevel imx_level = extract32(s->psr, i, 1); 117f4427280SJean-Christophe Dubois imx_gpio_set_int_line(s, i, imx_level); 118f4427280SJean-Christophe Dubois } 119f4427280SJean-Christophe Dubois 120f4427280SJean-Christophe Dubois imx_gpio_update_int(s); 121f4427280SJean-Christophe Dubois } 122f4427280SJean-Christophe Dubois 123f4427280SJean-Christophe Dubois static inline void imx_gpio_set_all_output_lines(IMXGPIOState *s) 124f4427280SJean-Christophe Dubois { 125f4427280SJean-Christophe Dubois int i; 126f4427280SJean-Christophe Dubois 127f4427280SJean-Christophe Dubois for (i = 0; i < IMX_GPIO_PIN_COUNT; i++) { 128f4427280SJean-Christophe Dubois /* 129f4427280SJean-Christophe Dubois * if the line is set as output, then forward the line 130f4427280SJean-Christophe Dubois * level to its user. 131f4427280SJean-Christophe Dubois */ 132f4427280SJean-Christophe Dubois if (extract32(s->gdir, i, 1) && s->output[i]) { 133f4427280SJean-Christophe Dubois qemu_set_irq(s->output[i], extract32(s->dr, i, 1)); 134f4427280SJean-Christophe Dubois } 135f4427280SJean-Christophe Dubois } 136f4427280SJean-Christophe Dubois } 137f4427280SJean-Christophe Dubois 138f4427280SJean-Christophe Dubois static uint64_t imx_gpio_read(void *opaque, hwaddr offset, unsigned size) 139f4427280SJean-Christophe Dubois { 140f4427280SJean-Christophe Dubois IMXGPIOState *s = IMX_GPIO(opaque); 141f4427280SJean-Christophe Dubois uint32_t reg_value = 0; 142f4427280SJean-Christophe Dubois 143f4427280SJean-Christophe Dubois switch (offset) { 144f4427280SJean-Christophe Dubois case DR_ADDR: 145f4427280SJean-Christophe Dubois /* 146f4427280SJean-Christophe Dubois * depending on the "line" configuration, the bit values 147f4427280SJean-Christophe Dubois * are coming either from DR or PSR 148f4427280SJean-Christophe Dubois */ 149f4427280SJean-Christophe Dubois reg_value = (s->dr & s->gdir) | (s->psr & ~s->gdir); 150f4427280SJean-Christophe Dubois break; 151f4427280SJean-Christophe Dubois 152f4427280SJean-Christophe Dubois case GDIR_ADDR: 153f4427280SJean-Christophe Dubois reg_value = s->gdir; 154f4427280SJean-Christophe Dubois break; 155f4427280SJean-Christophe Dubois 156f4427280SJean-Christophe Dubois case PSR_ADDR: 157f4427280SJean-Christophe Dubois reg_value = s->psr & ~s->gdir; 158f4427280SJean-Christophe Dubois break; 159f4427280SJean-Christophe Dubois 160f4427280SJean-Christophe Dubois case ICR1_ADDR: 161f4427280SJean-Christophe Dubois reg_value = extract64(s->icr, 0, 32); 162f4427280SJean-Christophe Dubois break; 163f4427280SJean-Christophe Dubois 164f4427280SJean-Christophe Dubois case ICR2_ADDR: 165f4427280SJean-Christophe Dubois reg_value = extract64(s->icr, 32, 32); 166f4427280SJean-Christophe Dubois break; 167f4427280SJean-Christophe Dubois 168f4427280SJean-Christophe Dubois case IMR_ADDR: 169f4427280SJean-Christophe Dubois reg_value = s->imr; 170f4427280SJean-Christophe Dubois break; 171f4427280SJean-Christophe Dubois 172f4427280SJean-Christophe Dubois case ISR_ADDR: 173f4427280SJean-Christophe Dubois reg_value = s->isr; 174f4427280SJean-Christophe Dubois break; 175f4427280SJean-Christophe Dubois 176f4427280SJean-Christophe Dubois case EDGE_SEL_ADDR: 177f4427280SJean-Christophe Dubois if (s->has_edge_sel) { 178f4427280SJean-Christophe Dubois reg_value = s->edge_sel; 179f4427280SJean-Christophe Dubois } else { 180*56411125SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: EDGE_SEL register not " 181f4427280SJean-Christophe Dubois "present on this version of GPIO device\n", 182f4427280SJean-Christophe Dubois TYPE_IMX_GPIO, __func__); 183f4427280SJean-Christophe Dubois } 184f4427280SJean-Christophe Dubois break; 185f4427280SJean-Christophe Dubois 186f4427280SJean-Christophe Dubois default: 187*56411125SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 188*56411125SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPIO, __func__, offset); 189f4427280SJean-Christophe Dubois break; 190f4427280SJean-Christophe Dubois } 191f4427280SJean-Christophe Dubois 192f4427280SJean-Christophe Dubois DPRINTF("(%s) = 0x%" PRIx32 "\n", imx_gpio_reg_name(offset), reg_value); 193f4427280SJean-Christophe Dubois 194f4427280SJean-Christophe Dubois return reg_value; 195f4427280SJean-Christophe Dubois } 196f4427280SJean-Christophe Dubois 197f4427280SJean-Christophe Dubois static void imx_gpio_write(void *opaque, hwaddr offset, uint64_t value, 198f4427280SJean-Christophe Dubois unsigned size) 199f4427280SJean-Christophe Dubois { 200f4427280SJean-Christophe Dubois IMXGPIOState *s = IMX_GPIO(opaque); 201f4427280SJean-Christophe Dubois 202f4427280SJean-Christophe Dubois DPRINTF("(%s, value = 0x%" PRIx32 ")\n", imx_gpio_reg_name(offset), 203f4427280SJean-Christophe Dubois (uint32_t)value); 204f4427280SJean-Christophe Dubois 205f4427280SJean-Christophe Dubois switch (offset) { 206f4427280SJean-Christophe Dubois case DR_ADDR: 207f4427280SJean-Christophe Dubois s->dr = value; 208f4427280SJean-Christophe Dubois imx_gpio_set_all_output_lines(s); 209f4427280SJean-Christophe Dubois break; 210f4427280SJean-Christophe Dubois 211f4427280SJean-Christophe Dubois case GDIR_ADDR: 212f4427280SJean-Christophe Dubois s->gdir = value; 213f4427280SJean-Christophe Dubois imx_gpio_set_all_output_lines(s); 214f4427280SJean-Christophe Dubois imx_gpio_set_all_int_lines(s); 215f4427280SJean-Christophe Dubois break; 216f4427280SJean-Christophe Dubois 217f4427280SJean-Christophe Dubois case ICR1_ADDR: 218f4427280SJean-Christophe Dubois s->icr = deposit64(s->icr, 0, 32, value); 219f4427280SJean-Christophe Dubois imx_gpio_set_all_int_lines(s); 220f4427280SJean-Christophe Dubois break; 221f4427280SJean-Christophe Dubois 222f4427280SJean-Christophe Dubois case ICR2_ADDR: 223f4427280SJean-Christophe Dubois s->icr = deposit64(s->icr, 32, 32, value); 224f4427280SJean-Christophe Dubois imx_gpio_set_all_int_lines(s); 225f4427280SJean-Christophe Dubois break; 226f4427280SJean-Christophe Dubois 227f4427280SJean-Christophe Dubois case IMR_ADDR: 228f4427280SJean-Christophe Dubois s->imr = value; 229f4427280SJean-Christophe Dubois imx_gpio_update_int(s); 230f4427280SJean-Christophe Dubois break; 231f4427280SJean-Christophe Dubois 232f4427280SJean-Christophe Dubois case ISR_ADDR: 233f4427280SJean-Christophe Dubois s->isr |= ~value; 234f4427280SJean-Christophe Dubois imx_gpio_set_all_int_lines(s); 235f4427280SJean-Christophe Dubois break; 236f4427280SJean-Christophe Dubois 237f4427280SJean-Christophe Dubois case EDGE_SEL_ADDR: 238f4427280SJean-Christophe Dubois if (s->has_edge_sel) { 239f4427280SJean-Christophe Dubois s->edge_sel = value; 240f4427280SJean-Christophe Dubois imx_gpio_set_all_int_lines(s); 241f4427280SJean-Christophe Dubois } else { 242*56411125SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: EDGE_SEL register not " 243f4427280SJean-Christophe Dubois "present on this version of GPIO device\n", 244f4427280SJean-Christophe Dubois TYPE_IMX_GPIO, __func__); 245f4427280SJean-Christophe Dubois } 246f4427280SJean-Christophe Dubois break; 247f4427280SJean-Christophe Dubois 248f4427280SJean-Christophe Dubois default: 249*56411125SJean-Christophe Dubois qemu_log_mask(LOG_GUEST_ERROR, "[%s]%s: Bad register at offset 0x%" 250*56411125SJean-Christophe Dubois HWADDR_PRIx "\n", TYPE_IMX_GPIO, __func__, offset); 251f4427280SJean-Christophe Dubois break; 252f4427280SJean-Christophe Dubois } 253f4427280SJean-Christophe Dubois 254f4427280SJean-Christophe Dubois return; 255f4427280SJean-Christophe Dubois } 256f4427280SJean-Christophe Dubois 257f4427280SJean-Christophe Dubois static const MemoryRegionOps imx_gpio_ops = { 258f4427280SJean-Christophe Dubois .read = imx_gpio_read, 259f4427280SJean-Christophe Dubois .write = imx_gpio_write, 260f4427280SJean-Christophe Dubois .valid.min_access_size = 4, 261f4427280SJean-Christophe Dubois .valid.max_access_size = 4, 262f4427280SJean-Christophe Dubois .endianness = DEVICE_NATIVE_ENDIAN, 263f4427280SJean-Christophe Dubois }; 264f4427280SJean-Christophe Dubois 265f4427280SJean-Christophe Dubois static const VMStateDescription vmstate_imx_gpio = { 266f4427280SJean-Christophe Dubois .name = TYPE_IMX_GPIO, 267f4427280SJean-Christophe Dubois .version_id = 1, 268f4427280SJean-Christophe Dubois .minimum_version_id = 1, 269f4427280SJean-Christophe Dubois .minimum_version_id_old = 1, 270f4427280SJean-Christophe Dubois .fields = (VMStateField[]) { 271f4427280SJean-Christophe Dubois VMSTATE_UINT32(dr, IMXGPIOState), 272f4427280SJean-Christophe Dubois VMSTATE_UINT32(gdir, IMXGPIOState), 273f4427280SJean-Christophe Dubois VMSTATE_UINT32(psr, IMXGPIOState), 274f4427280SJean-Christophe Dubois VMSTATE_UINT64(icr, IMXGPIOState), 275f4427280SJean-Christophe Dubois VMSTATE_UINT32(imr, IMXGPIOState), 276f4427280SJean-Christophe Dubois VMSTATE_UINT32(isr, IMXGPIOState), 277f4427280SJean-Christophe Dubois VMSTATE_BOOL(has_edge_sel, IMXGPIOState), 278f4427280SJean-Christophe Dubois VMSTATE_UINT32(edge_sel, IMXGPIOState), 279f4427280SJean-Christophe Dubois VMSTATE_END_OF_LIST() 280f4427280SJean-Christophe Dubois } 281f4427280SJean-Christophe Dubois }; 282f4427280SJean-Christophe Dubois 283f4427280SJean-Christophe Dubois static Property imx_gpio_properties[] = { 284f4427280SJean-Christophe Dubois DEFINE_PROP_BOOL("has-edge-sel", IMXGPIOState, has_edge_sel, true), 285f4427280SJean-Christophe Dubois DEFINE_PROP_END_OF_LIST(), 286f4427280SJean-Christophe Dubois }; 287f4427280SJean-Christophe Dubois 288f4427280SJean-Christophe Dubois static void imx_gpio_reset(DeviceState *dev) 289f4427280SJean-Christophe Dubois { 290f4427280SJean-Christophe Dubois IMXGPIOState *s = IMX_GPIO(dev); 291f4427280SJean-Christophe Dubois 292f4427280SJean-Christophe Dubois s->dr = 0; 293f4427280SJean-Christophe Dubois s->gdir = 0; 294f4427280SJean-Christophe Dubois s->psr = 0; 295f4427280SJean-Christophe Dubois s->icr = 0; 296f4427280SJean-Christophe Dubois s->imr = 0; 297f4427280SJean-Christophe Dubois s->isr = 0; 298f4427280SJean-Christophe Dubois s->edge_sel = 0; 299f4427280SJean-Christophe Dubois 300f4427280SJean-Christophe Dubois imx_gpio_set_all_output_lines(s); 301f4427280SJean-Christophe Dubois imx_gpio_update_int(s); 302f4427280SJean-Christophe Dubois } 303f4427280SJean-Christophe Dubois 304f4427280SJean-Christophe Dubois static void imx_gpio_realize(DeviceState *dev, Error **errp) 305f4427280SJean-Christophe Dubois { 306f4427280SJean-Christophe Dubois IMXGPIOState *s = IMX_GPIO(dev); 307f4427280SJean-Christophe Dubois 308f4427280SJean-Christophe Dubois memory_region_init_io(&s->iomem, OBJECT(s), &imx_gpio_ops, s, 309f4427280SJean-Christophe Dubois TYPE_IMX_GPIO, IMX_GPIO_MEM_SIZE); 310f4427280SJean-Christophe Dubois 311f4427280SJean-Christophe Dubois qdev_init_gpio_in(DEVICE(s), imx_gpio_set, IMX_GPIO_PIN_COUNT); 312f4427280SJean-Christophe Dubois qdev_init_gpio_out(DEVICE(s), s->output, IMX_GPIO_PIN_COUNT); 313f4427280SJean-Christophe Dubois 314f4427280SJean-Christophe Dubois sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq); 315f4427280SJean-Christophe Dubois sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem); 316f4427280SJean-Christophe Dubois } 317f4427280SJean-Christophe Dubois 318f4427280SJean-Christophe Dubois static void imx_gpio_class_init(ObjectClass *klass, void *data) 319f4427280SJean-Christophe Dubois { 320f4427280SJean-Christophe Dubois DeviceClass *dc = DEVICE_CLASS(klass); 321f4427280SJean-Christophe Dubois 322f4427280SJean-Christophe Dubois dc->realize = imx_gpio_realize; 323f4427280SJean-Christophe Dubois dc->reset = imx_gpio_reset; 324f4427280SJean-Christophe Dubois dc->props = imx_gpio_properties; 325f4427280SJean-Christophe Dubois dc->vmsd = &vmstate_imx_gpio; 326f4427280SJean-Christophe Dubois dc->desc = "i.MX GPIO controller"; 327f4427280SJean-Christophe Dubois } 328f4427280SJean-Christophe Dubois 329f4427280SJean-Christophe Dubois static const TypeInfo imx_gpio_info = { 330f4427280SJean-Christophe Dubois .name = TYPE_IMX_GPIO, 331f4427280SJean-Christophe Dubois .parent = TYPE_SYS_BUS_DEVICE, 332f4427280SJean-Christophe Dubois .instance_size = sizeof(IMXGPIOState), 333f4427280SJean-Christophe Dubois .class_init = imx_gpio_class_init, 334f4427280SJean-Christophe Dubois }; 335f4427280SJean-Christophe Dubois 336f4427280SJean-Christophe Dubois static void imx_gpio_register_types(void) 337f4427280SJean-Christophe Dubois { 338f4427280SJean-Christophe Dubois type_register_static(&imx_gpio_info); 339f4427280SJean-Christophe Dubois } 340f4427280SJean-Christophe Dubois 341f4427280SJean-Christophe Dubois type_init(imx_gpio_register_types) 342