nios2_iic.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * QEMU Altera Internal Interrupt Controller.
  3. *
  4. * Copyright (c) 2012 Chris Wulff <crwulff@gmail.com>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see
  18. * <http://www.gnu.org/licenses/lgpl-2.1.html>
  19. */
  20. #include "qemu/osdep.h"
  21. #include "qemu/module.h"
  22. #include "qapi/error.h"
  23. #include "hw/irq.h"
  24. #include "hw/sysbus.h"
  25. #include "cpu.h"
  26. #include "qom/object.h"
  27. #define TYPE_ALTERA_IIC "altera,iic"
  28. OBJECT_DECLARE_SIMPLE_TYPE(AlteraIIC, ALTERA_IIC)
  29. struct AlteraIIC {
  30. SysBusDevice parent_obj;
  31. void *cpu;
  32. qemu_irq parent_irq;
  33. };
  34. static void update_irq(AlteraIIC *pv)
  35. {
  36. CPUNios2State *env = &((Nios2CPU *)(pv->cpu))->env;
  37. qemu_set_irq(pv->parent_irq,
  38. env->regs[CR_IPENDING] & env->regs[CR_IENABLE]);
  39. }
  40. static void irq_handler(void *opaque, int irq, int level)
  41. {
  42. AlteraIIC *pv = opaque;
  43. CPUNios2State *env = &((Nios2CPU *)(pv->cpu))->env;
  44. env->regs[CR_IPENDING] &= ~(1 << irq);
  45. env->regs[CR_IPENDING] |= !!level << irq;
  46. update_irq(pv);
  47. }
  48. static void altera_iic_init(Object *obj)
  49. {
  50. AlteraIIC *pv = ALTERA_IIC(obj);
  51. qdev_init_gpio_in(DEVICE(pv), irq_handler, 32);
  52. sysbus_init_irq(SYS_BUS_DEVICE(obj), &pv->parent_irq);
  53. }
  54. static void altera_iic_realize(DeviceState *dev, Error **errp)
  55. {
  56. struct AlteraIIC *pv = ALTERA_IIC(dev);
  57. pv->cpu = object_property_get_link(OBJECT(dev), "cpu", &error_abort);
  58. }
  59. static void altera_iic_class_init(ObjectClass *klass, void *data)
  60. {
  61. DeviceClass *dc = DEVICE_CLASS(klass);
  62. /* Reason: needs to be wired up, e.g. by nios2_10m50_ghrd_init() */
  63. dc->user_creatable = false;
  64. dc->realize = altera_iic_realize;
  65. }
  66. static TypeInfo altera_iic_info = {
  67. .name = TYPE_ALTERA_IIC,
  68. .parent = TYPE_SYS_BUS_DEVICE,
  69. .instance_size = sizeof(AlteraIIC),
  70. .instance_init = altera_iic_init,
  71. .class_init = altera_iic_class_init,
  72. };
  73. static void altera_iic_register(void)
  74. {
  75. type_register_static(&altera_iic_info);
  76. }
  77. type_init(altera_iic_register)