puv3_gpio.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * GPIO device simulation in PKUnity SoC
  3. *
  4. * Copyright (C) 2010-2012 Guan Xuetao
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation, or any later version.
  9. * See the COPYING file in the top-level directory.
  10. */
  11. #include "hw.h"
  12. #include "sysbus.h"
  13. #undef DEBUG_PUV3
  14. #include "puv3.h"
  15. typedef struct {
  16. SysBusDevice busdev;
  17. MemoryRegion iomem;
  18. qemu_irq irq[9];
  19. uint32_t reg_GPLR;
  20. uint32_t reg_GPDR;
  21. uint32_t reg_GPIR;
  22. } PUV3GPIOState;
  23. static uint64_t puv3_gpio_read(void *opaque, hwaddr offset,
  24. unsigned size)
  25. {
  26. PUV3GPIOState *s = opaque;
  27. uint32_t ret = 0;
  28. switch (offset) {
  29. case 0x00:
  30. ret = s->reg_GPLR;
  31. break;
  32. case 0x04:
  33. ret = s->reg_GPDR;
  34. break;
  35. case 0x20:
  36. ret = s->reg_GPIR;
  37. break;
  38. default:
  39. DPRINTF("Bad offset 0x%x\n", offset);
  40. }
  41. DPRINTF("offset 0x%x, value 0x%x\n", offset, ret);
  42. return ret;
  43. }
  44. static void puv3_gpio_write(void *opaque, hwaddr offset,
  45. uint64_t value, unsigned size)
  46. {
  47. PUV3GPIOState *s = opaque;
  48. DPRINTF("offset 0x%x, value 0x%x\n", offset, value);
  49. switch (offset) {
  50. case 0x04:
  51. s->reg_GPDR = value;
  52. break;
  53. case 0x08:
  54. if (s->reg_GPDR & value) {
  55. s->reg_GPLR |= value;
  56. } else {
  57. DPRINTF("Write gpio input port error!");
  58. }
  59. break;
  60. case 0x0c:
  61. if (s->reg_GPDR & value) {
  62. s->reg_GPLR &= ~value;
  63. } else {
  64. DPRINTF("Write gpio input port error!");
  65. }
  66. break;
  67. case 0x10: /* GRER */
  68. case 0x14: /* GFER */
  69. case 0x18: /* GEDR */
  70. break;
  71. case 0x20: /* GPIR */
  72. s->reg_GPIR = value;
  73. break;
  74. default:
  75. DPRINTF("Bad offset 0x%x\n", offset);
  76. }
  77. }
  78. static const MemoryRegionOps puv3_gpio_ops = {
  79. .read = puv3_gpio_read,
  80. .write = puv3_gpio_write,
  81. .impl = {
  82. .min_access_size = 4,
  83. .max_access_size = 4,
  84. },
  85. .endianness = DEVICE_NATIVE_ENDIAN,
  86. };
  87. static int puv3_gpio_init(SysBusDevice *dev)
  88. {
  89. PUV3GPIOState *s = FROM_SYSBUS(PUV3GPIOState, dev);
  90. s->reg_GPLR = 0;
  91. s->reg_GPDR = 0;
  92. /* FIXME: these irqs not handled yet */
  93. sysbus_init_irq(dev, &s->irq[PUV3_IRQS_GPIOLOW0]);
  94. sysbus_init_irq(dev, &s->irq[PUV3_IRQS_GPIOLOW1]);
  95. sysbus_init_irq(dev, &s->irq[PUV3_IRQS_GPIOLOW2]);
  96. sysbus_init_irq(dev, &s->irq[PUV3_IRQS_GPIOLOW3]);
  97. sysbus_init_irq(dev, &s->irq[PUV3_IRQS_GPIOLOW4]);
  98. sysbus_init_irq(dev, &s->irq[PUV3_IRQS_GPIOLOW5]);
  99. sysbus_init_irq(dev, &s->irq[PUV3_IRQS_GPIOLOW6]);
  100. sysbus_init_irq(dev, &s->irq[PUV3_IRQS_GPIOLOW7]);
  101. sysbus_init_irq(dev, &s->irq[PUV3_IRQS_GPIOHIGH]);
  102. memory_region_init_io(&s->iomem, &puv3_gpio_ops, s, "puv3_gpio",
  103. PUV3_REGS_OFFSET);
  104. sysbus_init_mmio(dev, &s->iomem);
  105. return 0;
  106. }
  107. static void puv3_gpio_class_init(ObjectClass *klass, void *data)
  108. {
  109. SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
  110. sdc->init = puv3_gpio_init;
  111. }
  112. static const TypeInfo puv3_gpio_info = {
  113. .name = "puv3_gpio",
  114. .parent = TYPE_SYS_BUS_DEVICE,
  115. .instance_size = sizeof(PUV3GPIOState),
  116. .class_init = puv3_gpio_class_init,
  117. };
  118. static void puv3_gpio_register_type(void)
  119. {
  120. type_register_static(&puv3_gpio_info);
  121. }
  122. type_init(puv3_gpio_register_type)