avr_power.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * AVR Power Reduction Management
  3. *
  4. * Copyright (c) 2019-2020 Michael Rolnik
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "hw/misc/avr_power.h"
  26. #include "qemu/log.h"
  27. #include "hw/qdev-properties.h"
  28. #include "hw/irq.h"
  29. #include "trace.h"
  30. static void avr_mask_reset(DeviceState *dev)
  31. {
  32. AVRMaskState *s = AVR_MASK(dev);
  33. s->val = 0x00;
  34. for (int i = 0; i < 8; i++) {
  35. qemu_set_irq(s->irq[i], 0);
  36. }
  37. }
  38. static uint64_t avr_mask_read(void *opaque, hwaddr offset, unsigned size)
  39. {
  40. assert(size == 1);
  41. assert(offset == 0);
  42. AVRMaskState *s = opaque;
  43. trace_avr_power_read(s->val);
  44. return (uint64_t)s->val;
  45. }
  46. static void avr_mask_write(void *opaque, hwaddr offset,
  47. uint64_t val64, unsigned size)
  48. {
  49. assert(size == 1);
  50. assert(offset == 0);
  51. AVRMaskState *s = opaque;
  52. uint8_t val8 = val64;
  53. trace_avr_power_write(val8);
  54. s->val = val8;
  55. for (int i = 0; i < 8; i++) {
  56. qemu_set_irq(s->irq[i], (val8 & (1 << i)) != 0);
  57. }
  58. }
  59. static const MemoryRegionOps avr_mask_ops = {
  60. .read = avr_mask_read,
  61. .write = avr_mask_write,
  62. .endianness = DEVICE_NATIVE_ENDIAN,
  63. .impl = {
  64. .max_access_size = 1,
  65. },
  66. };
  67. static void avr_mask_init(Object *dev)
  68. {
  69. AVRMaskState *s = AVR_MASK(dev);
  70. SysBusDevice *busdev = SYS_BUS_DEVICE(dev);
  71. memory_region_init_io(&s->iomem, dev, &avr_mask_ops, s, TYPE_AVR_MASK,
  72. 0x01);
  73. sysbus_init_mmio(busdev, &s->iomem);
  74. for (int i = 0; i < 8; i++) {
  75. sysbus_init_irq(busdev, &s->irq[i]);
  76. }
  77. s->val = 0x00;
  78. }
  79. static void avr_mask_class_init(ObjectClass *klass, void *data)
  80. {
  81. DeviceClass *dc = DEVICE_CLASS(klass);
  82. device_class_set_legacy_reset(dc, avr_mask_reset);
  83. }
  84. static const TypeInfo avr_mask_info = {
  85. .name = TYPE_AVR_MASK,
  86. .parent = TYPE_SYS_BUS_DEVICE,
  87. .instance_size = sizeof(AVRMaskState),
  88. .class_init = avr_mask_class_init,
  89. .instance_init = avr_mask_init,
  90. };
  91. static void avr_mask_register_types(void)
  92. {
  93. type_register_static(&avr_mask_info);
  94. }
  95. type_init(avr_mask_register_types)