gpio.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * PowerMac NewWorld MacIO GPIO emulation
  3. *
  4. * Copyright (c) 2016 Benjamin Herrenschmidt
  5. * Copyright (c) 2018 Mark Cave-Ayland
  6. *
  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:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  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
  23. * THE SOFTWARE.
  24. */
  25. #include "qemu/osdep.h"
  26. #include "hw/qdev-properties.h"
  27. #include "migration/vmstate.h"
  28. #include "hw/misc/macio/macio.h"
  29. #include "hw/misc/macio/gpio.h"
  30. #include "hw/irq.h"
  31. #include "hw/nmi.h"
  32. #include "qemu/log.h"
  33. #include "qemu/module.h"
  34. #include "trace.h"
  35. void macio_set_gpio(MacIOGPIOState *s, uint32_t gpio, bool state)
  36. {
  37. uint8_t new_reg;
  38. trace_macio_set_gpio(gpio, state);
  39. if (s->gpio_regs[gpio] & 4) {
  40. qemu_log_mask(LOG_GUEST_ERROR,
  41. "GPIO: Setting GPIO %d while it's an output\n", gpio);
  42. }
  43. new_reg = s->gpio_regs[gpio] & ~2;
  44. if (state) {
  45. new_reg |= 2;
  46. }
  47. if (new_reg == s->gpio_regs[gpio]) {
  48. return;
  49. }
  50. s->gpio_regs[gpio] = new_reg;
  51. /*
  52. * Note that we probably need to get access to the MPIC config to
  53. * decode polarity since qemu always use "raise" regardless.
  54. *
  55. * For now, we hard wire known GPIOs
  56. */
  57. switch (gpio) {
  58. case 1:
  59. /* Level low */
  60. if (!state) {
  61. trace_macio_gpio_irq_assert(gpio);
  62. qemu_irq_raise(s->gpio_extirqs[gpio]);
  63. } else {
  64. trace_macio_gpio_irq_deassert(gpio);
  65. qemu_irq_lower(s->gpio_extirqs[gpio]);
  66. }
  67. break;
  68. case 9:
  69. /* Edge, triggered by NMI below */
  70. if (state) {
  71. trace_macio_gpio_irq_assert(gpio);
  72. qemu_irq_raise(s->gpio_extirqs[gpio]);
  73. } else {
  74. trace_macio_gpio_irq_deassert(gpio);
  75. qemu_irq_lower(s->gpio_extirqs[gpio]);
  76. }
  77. break;
  78. default:
  79. qemu_log_mask(LOG_UNIMP, "GPIO: setting unimplemented GPIO %d", gpio);
  80. }
  81. }
  82. static void macio_gpio_write(void *opaque, hwaddr addr, uint64_t value,
  83. unsigned size)
  84. {
  85. MacIOGPIOState *s = opaque;
  86. uint8_t ibit;
  87. trace_macio_gpio_write(addr, value);
  88. /* Levels regs are read-only */
  89. if (addr < 8) {
  90. return;
  91. }
  92. addr -= 8;
  93. if (addr < 36) {
  94. value &= ~2;
  95. if (value & 4) {
  96. ibit = (value & 1) << 1;
  97. } else {
  98. ibit = s->gpio_regs[addr] & 2;
  99. }
  100. s->gpio_regs[addr] = value | ibit;
  101. }
  102. }
  103. static uint64_t macio_gpio_read(void *opaque, hwaddr addr, unsigned size)
  104. {
  105. MacIOGPIOState *s = opaque;
  106. uint64_t val = 0;
  107. /* Levels regs */
  108. if (addr < 8) {
  109. val = s->gpio_levels[addr];
  110. } else {
  111. addr -= 8;
  112. if (addr < 36) {
  113. val = s->gpio_regs[addr];
  114. }
  115. }
  116. trace_macio_gpio_write(addr, val);
  117. return val;
  118. }
  119. static const MemoryRegionOps macio_gpio_ops = {
  120. .read = macio_gpio_read,
  121. .write = macio_gpio_write,
  122. .endianness = DEVICE_LITTLE_ENDIAN,
  123. .impl = {
  124. .min_access_size = 1,
  125. .max_access_size = 1,
  126. },
  127. };
  128. static void macio_gpio_init(Object *obj)
  129. {
  130. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  131. MacIOGPIOState *s = MACIO_GPIO(obj);
  132. int i;
  133. for (i = 0; i < 10; i++) {
  134. sysbus_init_irq(sbd, &s->gpio_extirqs[i]);
  135. }
  136. memory_region_init_io(&s->gpiomem, OBJECT(s), &macio_gpio_ops, obj,
  137. "gpio", 0x30);
  138. sysbus_init_mmio(sbd, &s->gpiomem);
  139. }
  140. static const VMStateDescription vmstate_macio_gpio = {
  141. .name = "macio_gpio",
  142. .version_id = 0,
  143. .minimum_version_id = 0,
  144. .fields = (VMStateField[]) {
  145. VMSTATE_UINT8_ARRAY(gpio_levels, MacIOGPIOState, 8),
  146. VMSTATE_UINT8_ARRAY(gpio_regs, MacIOGPIOState, 36),
  147. VMSTATE_END_OF_LIST()
  148. }
  149. };
  150. static void macio_gpio_reset(DeviceState *dev)
  151. {
  152. MacIOGPIOState *s = MACIO_GPIO(dev);
  153. /* GPIO 1 is up by default */
  154. macio_set_gpio(s, 1, true);
  155. }
  156. static void macio_gpio_nmi(NMIState *n, int cpu_index, Error **errp)
  157. {
  158. macio_set_gpio(MACIO_GPIO(n), 9, true);
  159. macio_set_gpio(MACIO_GPIO(n), 9, false);
  160. }
  161. static void macio_gpio_class_init(ObjectClass *oc, void *data)
  162. {
  163. DeviceClass *dc = DEVICE_CLASS(oc);
  164. NMIClass *nc = NMI_CLASS(oc);
  165. dc->reset = macio_gpio_reset;
  166. dc->vmsd = &vmstate_macio_gpio;
  167. nc->nmi_monitor_handler = macio_gpio_nmi;
  168. }
  169. static const TypeInfo macio_gpio_init_info = {
  170. .name = TYPE_MACIO_GPIO,
  171. .parent = TYPE_SYS_BUS_DEVICE,
  172. .instance_size = sizeof(MacIOGPIOState),
  173. .instance_init = macio_gpio_init,
  174. .class_init = macio_gpio_class_init,
  175. .interfaces = (InterfaceInfo[]) {
  176. { TYPE_NMI },
  177. { }
  178. },
  179. };
  180. static void macio_gpio_register_types(void)
  181. {
  182. type_register_static(&macio_gpio_init_info);
  183. }
  184. type_init(macio_gpio_register_types)