mpc8xxx.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * GPIO Controller for a lot of Freescale SoCs
  3. *
  4. * Copyright (C) 2014 Freescale Semiconductor, Inc. All rights reserved.
  5. *
  6. * Author: Alexander Graf, <agraf@suse.de>
  7. *
  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.
  12. *
  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.
  17. *
  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/>.
  20. */
  21. #include "qemu/osdep.h"
  22. #include "hw/irq.h"
  23. #include "hw/sysbus.h"
  24. #include "migration/vmstate.h"
  25. #include "qemu/module.h"
  26. #define TYPE_MPC8XXX_GPIO "mpc8xxx_gpio"
  27. #define MPC8XXX_GPIO(obj) OBJECT_CHECK(MPC8XXXGPIOState, (obj), TYPE_MPC8XXX_GPIO)
  28. typedef struct MPC8XXXGPIOState {
  29. SysBusDevice parent_obj;
  30. MemoryRegion iomem;
  31. qemu_irq irq;
  32. qemu_irq out[32];
  33. uint32_t dir;
  34. uint32_t odr;
  35. uint32_t dat;
  36. uint32_t ier;
  37. uint32_t imr;
  38. uint32_t icr;
  39. } MPC8XXXGPIOState;
  40. static const VMStateDescription vmstate_mpc8xxx_gpio = {
  41. .name = "mpc8xxx_gpio",
  42. .version_id = 1,
  43. .minimum_version_id = 1,
  44. .fields = (VMStateField[]) {
  45. VMSTATE_UINT32(dir, MPC8XXXGPIOState),
  46. VMSTATE_UINT32(odr, MPC8XXXGPIOState),
  47. VMSTATE_UINT32(dat, MPC8XXXGPIOState),
  48. VMSTATE_UINT32(ier, MPC8XXXGPIOState),
  49. VMSTATE_UINT32(imr, MPC8XXXGPIOState),
  50. VMSTATE_UINT32(icr, MPC8XXXGPIOState),
  51. VMSTATE_END_OF_LIST()
  52. }
  53. };
  54. static void mpc8xxx_gpio_update(MPC8XXXGPIOState *s)
  55. {
  56. qemu_set_irq(s->irq, !!(s->ier & s->imr));
  57. }
  58. static uint64_t mpc8xxx_gpio_read(void *opaque, hwaddr offset,
  59. unsigned size)
  60. {
  61. MPC8XXXGPIOState *s = (MPC8XXXGPIOState *)opaque;
  62. if (size != 4) {
  63. /* All registers are 32bit */
  64. return 0;
  65. }
  66. switch (offset) {
  67. case 0x0: /* Direction */
  68. return s->dir;
  69. case 0x4: /* Open Drain */
  70. return s->odr;
  71. case 0x8: /* Data */
  72. return s->dat;
  73. case 0xC: /* Interrupt Event */
  74. return s->ier;
  75. case 0x10: /* Interrupt Mask */
  76. return s->imr;
  77. case 0x14: /* Interrupt Control */
  78. return s->icr;
  79. default:
  80. return 0;
  81. }
  82. }
  83. static void mpc8xxx_write_data(MPC8XXXGPIOState *s, uint32_t new_data)
  84. {
  85. uint32_t old_data = s->dat;
  86. uint32_t diff = old_data ^ new_data;
  87. int i;
  88. for (i = 0; i < 32; i++) {
  89. uint32_t mask = 0x80000000 >> i;
  90. if (!(diff & mask)) {
  91. continue;
  92. }
  93. if (s->dir & mask) {
  94. /* Output */
  95. qemu_set_irq(s->out[i], (new_data & mask) != 0);
  96. }
  97. }
  98. s->dat = new_data;
  99. }
  100. static void mpc8xxx_gpio_write(void *opaque, hwaddr offset,
  101. uint64_t value, unsigned size)
  102. {
  103. MPC8XXXGPIOState *s = (MPC8XXXGPIOState *)opaque;
  104. if (size != 4) {
  105. /* All registers are 32bit */
  106. return;
  107. }
  108. switch (offset) {
  109. case 0x0: /* Direction */
  110. s->dir = value;
  111. break;
  112. case 0x4: /* Open Drain */
  113. s->odr = value;
  114. break;
  115. case 0x8: /* Data */
  116. mpc8xxx_write_data(s, value);
  117. break;
  118. case 0xC: /* Interrupt Event */
  119. s->ier &= ~value;
  120. break;
  121. case 0x10: /* Interrupt Mask */
  122. s->imr = value;
  123. break;
  124. case 0x14: /* Interrupt Control */
  125. s->icr = value;
  126. break;
  127. }
  128. mpc8xxx_gpio_update(s);
  129. }
  130. static void mpc8xxx_gpio_reset(DeviceState *dev)
  131. {
  132. MPC8XXXGPIOState *s = MPC8XXX_GPIO(dev);
  133. s->dir = 0;
  134. s->odr = 0;
  135. s->dat = 0;
  136. s->ier = 0;
  137. s->imr = 0;
  138. s->icr = 0;
  139. }
  140. static void mpc8xxx_gpio_set_irq(void * opaque, int irq, int level)
  141. {
  142. MPC8XXXGPIOState *s = (MPC8XXXGPIOState *)opaque;
  143. uint32_t mask;
  144. mask = 0x80000000 >> irq;
  145. if ((s->dir & mask) == 0) {
  146. uint32_t old_value = s->dat & mask;
  147. s->dat &= ~mask;
  148. if (level)
  149. s->dat |= mask;
  150. if (!(s->icr & irq) || (old_value && !level)) {
  151. s->ier |= mask;
  152. }
  153. mpc8xxx_gpio_update(s);
  154. }
  155. }
  156. static const MemoryRegionOps mpc8xxx_gpio_ops = {
  157. .read = mpc8xxx_gpio_read,
  158. .write = mpc8xxx_gpio_write,
  159. .endianness = DEVICE_BIG_ENDIAN,
  160. };
  161. static void mpc8xxx_gpio_initfn(Object *obj)
  162. {
  163. DeviceState *dev = DEVICE(obj);
  164. MPC8XXXGPIOState *s = MPC8XXX_GPIO(obj);
  165. SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
  166. memory_region_init_io(&s->iomem, obj, &mpc8xxx_gpio_ops,
  167. s, "mpc8xxx_gpio", 0x1000);
  168. sysbus_init_mmio(sbd, &s->iomem);
  169. sysbus_init_irq(sbd, &s->irq);
  170. qdev_init_gpio_in(dev, mpc8xxx_gpio_set_irq, 32);
  171. qdev_init_gpio_out(dev, s->out, 32);
  172. }
  173. static void mpc8xxx_gpio_class_init(ObjectClass *klass, void *data)
  174. {
  175. DeviceClass *dc = DEVICE_CLASS(klass);
  176. dc->vmsd = &vmstate_mpc8xxx_gpio;
  177. dc->reset = mpc8xxx_gpio_reset;
  178. }
  179. static const TypeInfo mpc8xxx_gpio_info = {
  180. .name = TYPE_MPC8XXX_GPIO,
  181. .parent = TYPE_SYS_BUS_DEVICE,
  182. .instance_size = sizeof(MPC8XXXGPIOState),
  183. .instance_init = mpc8xxx_gpio_initfn,
  184. .class_init = mpc8xxx_gpio_class_init,
  185. };
  186. static void mpc8xxx_gpio_register_types(void)
  187. {
  188. type_register_static(&mpc8xxx_gpio_info);
  189. }
  190. type_init(mpc8xxx_gpio_register_types)