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