xref: /qemu/hw/misc/imx7_snvs.c (revision bb2fc5b9958f183e63d839a6e6e10cdc4dad0981)
10a7bc1c0SAndrey Smirnov /*
20a7bc1c0SAndrey Smirnov  * IMX7 Secure Non-Volatile Storage
30a7bc1c0SAndrey Smirnov  *
40a7bc1c0SAndrey Smirnov  * Copyright (c) 2018, Impinj, Inc.
50a7bc1c0SAndrey Smirnov  *
60a7bc1c0SAndrey Smirnov  * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
70a7bc1c0SAndrey Smirnov  *
80a7bc1c0SAndrey Smirnov  * This work is licensed under the terms of the GNU GPL, version 2 or later.
90a7bc1c0SAndrey Smirnov  * See the COPYING file in the top-level directory.
100a7bc1c0SAndrey Smirnov  *
110a7bc1c0SAndrey Smirnov  * Bare minimum emulation code needed to support being able to shut
120a7bc1c0SAndrey Smirnov  * down linux guest gracefully.
130a7bc1c0SAndrey Smirnov  */
140a7bc1c0SAndrey Smirnov 
150a7bc1c0SAndrey Smirnov #include "qemu/osdep.h"
160a7bc1c0SAndrey Smirnov #include "hw/misc/imx7_snvs.h"
170b8fa32fSMarkus Armbruster #include "qemu/module.h"
1854d31236SMarkus Armbruster #include "sysemu/runstate.h"
19*bb2fc5b9SBernhard Beschow #include "trace.h"
200a7bc1c0SAndrey Smirnov 
210a7bc1c0SAndrey Smirnov static uint64_t imx7_snvs_read(void *opaque, hwaddr offset, unsigned size)
220a7bc1c0SAndrey Smirnov {
23*bb2fc5b9SBernhard Beschow     trace_imx7_snvs_read(offset, 0);
24*bb2fc5b9SBernhard Beschow 
250a7bc1c0SAndrey Smirnov     return 0;
260a7bc1c0SAndrey Smirnov }
270a7bc1c0SAndrey Smirnov 
280a7bc1c0SAndrey Smirnov static void imx7_snvs_write(void *opaque, hwaddr offset,
290a7bc1c0SAndrey Smirnov                             uint64_t v, unsigned size)
300a7bc1c0SAndrey Smirnov {
310a7bc1c0SAndrey Smirnov     const uint32_t value = v;
320a7bc1c0SAndrey Smirnov     const uint32_t mask  = SNVS_LPCR_TOP | SNVS_LPCR_DP_EN;
330a7bc1c0SAndrey Smirnov 
34*bb2fc5b9SBernhard Beschow     trace_imx7_snvs_write(offset, value);
35*bb2fc5b9SBernhard Beschow 
360a7bc1c0SAndrey Smirnov     if (offset == SNVS_LPCR && ((value & mask) == mask)) {
370a7bc1c0SAndrey Smirnov         qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
380a7bc1c0SAndrey Smirnov     }
390a7bc1c0SAndrey Smirnov }
400a7bc1c0SAndrey Smirnov 
410a7bc1c0SAndrey Smirnov static const struct MemoryRegionOps imx7_snvs_ops = {
420a7bc1c0SAndrey Smirnov     .read = imx7_snvs_read,
430a7bc1c0SAndrey Smirnov     .write = imx7_snvs_write,
440a7bc1c0SAndrey Smirnov     .endianness = DEVICE_NATIVE_ENDIAN,
450a7bc1c0SAndrey Smirnov     .impl = {
460a7bc1c0SAndrey Smirnov         /*
470a7bc1c0SAndrey Smirnov          * Our device would not work correctly if the guest was doing
480a7bc1c0SAndrey Smirnov          * unaligned access. This might not be a limitation on the real
490a7bc1c0SAndrey Smirnov          * device but in practice there is no reason for a guest to access
500a7bc1c0SAndrey Smirnov          * this device unaligned.
510a7bc1c0SAndrey Smirnov          */
520a7bc1c0SAndrey Smirnov         .min_access_size = 4,
530a7bc1c0SAndrey Smirnov         .max_access_size = 4,
540a7bc1c0SAndrey Smirnov         .unaligned = false,
550a7bc1c0SAndrey Smirnov     },
560a7bc1c0SAndrey Smirnov };
570a7bc1c0SAndrey Smirnov 
580a7bc1c0SAndrey Smirnov static void imx7_snvs_init(Object *obj)
590a7bc1c0SAndrey Smirnov {
600a7bc1c0SAndrey Smirnov     SysBusDevice *sd = SYS_BUS_DEVICE(obj);
610a7bc1c0SAndrey Smirnov     IMX7SNVSState *s = IMX7_SNVS(obj);
620a7bc1c0SAndrey Smirnov 
630a7bc1c0SAndrey Smirnov     memory_region_init_io(&s->mmio, obj, &imx7_snvs_ops, s,
640a7bc1c0SAndrey Smirnov                           TYPE_IMX7_SNVS, 0x1000);
650a7bc1c0SAndrey Smirnov 
660a7bc1c0SAndrey Smirnov     sysbus_init_mmio(sd, &s->mmio);
670a7bc1c0SAndrey Smirnov }
680a7bc1c0SAndrey Smirnov 
690a7bc1c0SAndrey Smirnov static void imx7_snvs_class_init(ObjectClass *klass, void *data)
700a7bc1c0SAndrey Smirnov {
710a7bc1c0SAndrey Smirnov     DeviceClass *dc = DEVICE_CLASS(klass);
720a7bc1c0SAndrey Smirnov 
730a7bc1c0SAndrey Smirnov     dc->desc  = "i.MX7 Secure Non-Volatile Storage Module";
740a7bc1c0SAndrey Smirnov }
750a7bc1c0SAndrey Smirnov 
760a7bc1c0SAndrey Smirnov static const TypeInfo imx7_snvs_info = {
770a7bc1c0SAndrey Smirnov     .name          = TYPE_IMX7_SNVS,
780a7bc1c0SAndrey Smirnov     .parent        = TYPE_SYS_BUS_DEVICE,
790a7bc1c0SAndrey Smirnov     .instance_size = sizeof(IMX7SNVSState),
800a7bc1c0SAndrey Smirnov     .instance_init = imx7_snvs_init,
810a7bc1c0SAndrey Smirnov     .class_init    = imx7_snvs_class_init,
820a7bc1c0SAndrey Smirnov };
830a7bc1c0SAndrey Smirnov 
840a7bc1c0SAndrey Smirnov static void imx7_snvs_register_type(void)
850a7bc1c0SAndrey Smirnov {
860a7bc1c0SAndrey Smirnov     type_register_static(&imx7_snvs_info);
870a7bc1c0SAndrey Smirnov }
880a7bc1c0SAndrey Smirnov type_init(imx7_snvs_register_type)
89