xref: /openbmc/qemu/hw/misc/imx7_snvs.c (revision 806f71ee)
1 /*
2  * IMX7 Secure Non-Volatile Storage
3  *
4  * Copyright (c) 2018, Impinj, Inc.
5  *
6  * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or later.
9  * See the COPYING file in the top-level directory.
10  *
11  * Bare minimum emulation code needed to support being able to shut
12  * down linux guest gracefully.
13  */
14 
15 #include "qemu/osdep.h"
16 #include "hw/misc/imx7_snvs.h"
17 #include "qemu/module.h"
18 #include "sysemu/runstate.h"
19 #include "trace.h"
20 
21 static uint64_t imx7_snvs_read(void *opaque, hwaddr offset, unsigned size)
22 {
23     trace_imx7_snvs_read(offset, 0);
24 
25     return 0;
26 }
27 
28 static void imx7_snvs_write(void *opaque, hwaddr offset,
29                             uint64_t v, unsigned size)
30 {
31     const uint32_t value = v;
32     const uint32_t mask  = SNVS_LPCR_TOP | SNVS_LPCR_DP_EN;
33 
34     trace_imx7_snvs_write(offset, value);
35 
36     if (offset == SNVS_LPCR && ((value & mask) == mask)) {
37         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
38     }
39 }
40 
41 static const struct MemoryRegionOps imx7_snvs_ops = {
42     .read = imx7_snvs_read,
43     .write = imx7_snvs_write,
44     .endianness = DEVICE_NATIVE_ENDIAN,
45     .impl = {
46         /*
47          * Our device would not work correctly if the guest was doing
48          * unaligned access. This might not be a limitation on the real
49          * device but in practice there is no reason for a guest to access
50          * this device unaligned.
51          */
52         .min_access_size = 4,
53         .max_access_size = 4,
54         .unaligned = false,
55     },
56 };
57 
58 static void imx7_snvs_init(Object *obj)
59 {
60     SysBusDevice *sd = SYS_BUS_DEVICE(obj);
61     IMX7SNVSState *s = IMX7_SNVS(obj);
62 
63     memory_region_init_io(&s->mmio, obj, &imx7_snvs_ops, s,
64                           TYPE_IMX7_SNVS, 0x1000);
65 
66     sysbus_init_mmio(sd, &s->mmio);
67 }
68 
69 static void imx7_snvs_class_init(ObjectClass *klass, void *data)
70 {
71     DeviceClass *dc = DEVICE_CLASS(klass);
72 
73     dc->desc  = "i.MX7 Secure Non-Volatile Storage Module";
74 }
75 
76 static const TypeInfo imx7_snvs_info = {
77     .name          = TYPE_IMX7_SNVS,
78     .parent        = TYPE_SYS_BUS_DEVICE,
79     .instance_size = sizeof(IMX7SNVSState),
80     .instance_init = imx7_snvs_init,
81     .class_init    = imx7_snvs_class_init,
82 };
83 
84 static void imx7_snvs_register_type(void)
85 {
86     type_register_static(&imx7_snvs_info);
87 }
88 type_init(imx7_snvs_register_type)
89