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" 170a7bc1c0SAndrey Smirnov #include "qemu/log.h" 180b8fa32fSMarkus Armbruster #include "qemu/module.h" 19*54d31236SMarkus Armbruster #include "sysemu/runstate.h" 200a7bc1c0SAndrey Smirnov 210a7bc1c0SAndrey Smirnov static uint64_t imx7_snvs_read(void *opaque, hwaddr offset, unsigned size) 220a7bc1c0SAndrey Smirnov { 230a7bc1c0SAndrey Smirnov return 0; 240a7bc1c0SAndrey Smirnov } 250a7bc1c0SAndrey Smirnov 260a7bc1c0SAndrey Smirnov static void imx7_snvs_write(void *opaque, hwaddr offset, 270a7bc1c0SAndrey Smirnov uint64_t v, unsigned size) 280a7bc1c0SAndrey Smirnov { 290a7bc1c0SAndrey Smirnov const uint32_t value = v; 300a7bc1c0SAndrey Smirnov const uint32_t mask = SNVS_LPCR_TOP | SNVS_LPCR_DP_EN; 310a7bc1c0SAndrey Smirnov 320a7bc1c0SAndrey Smirnov if (offset == SNVS_LPCR && ((value & mask) == mask)) { 330a7bc1c0SAndrey Smirnov qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN); 340a7bc1c0SAndrey Smirnov } 350a7bc1c0SAndrey Smirnov } 360a7bc1c0SAndrey Smirnov 370a7bc1c0SAndrey Smirnov static const struct MemoryRegionOps imx7_snvs_ops = { 380a7bc1c0SAndrey Smirnov .read = imx7_snvs_read, 390a7bc1c0SAndrey Smirnov .write = imx7_snvs_write, 400a7bc1c0SAndrey Smirnov .endianness = DEVICE_NATIVE_ENDIAN, 410a7bc1c0SAndrey Smirnov .impl = { 420a7bc1c0SAndrey Smirnov /* 430a7bc1c0SAndrey Smirnov * Our device would not work correctly if the guest was doing 440a7bc1c0SAndrey Smirnov * unaligned access. This might not be a limitation on the real 450a7bc1c0SAndrey Smirnov * device but in practice there is no reason for a guest to access 460a7bc1c0SAndrey Smirnov * this device unaligned. 470a7bc1c0SAndrey Smirnov */ 480a7bc1c0SAndrey Smirnov .min_access_size = 4, 490a7bc1c0SAndrey Smirnov .max_access_size = 4, 500a7bc1c0SAndrey Smirnov .unaligned = false, 510a7bc1c0SAndrey Smirnov }, 520a7bc1c0SAndrey Smirnov }; 530a7bc1c0SAndrey Smirnov 540a7bc1c0SAndrey Smirnov static void imx7_snvs_init(Object *obj) 550a7bc1c0SAndrey Smirnov { 560a7bc1c0SAndrey Smirnov SysBusDevice *sd = SYS_BUS_DEVICE(obj); 570a7bc1c0SAndrey Smirnov IMX7SNVSState *s = IMX7_SNVS(obj); 580a7bc1c0SAndrey Smirnov 590a7bc1c0SAndrey Smirnov memory_region_init_io(&s->mmio, obj, &imx7_snvs_ops, s, 600a7bc1c0SAndrey Smirnov TYPE_IMX7_SNVS, 0x1000); 610a7bc1c0SAndrey Smirnov 620a7bc1c0SAndrey Smirnov sysbus_init_mmio(sd, &s->mmio); 630a7bc1c0SAndrey Smirnov } 640a7bc1c0SAndrey Smirnov 650a7bc1c0SAndrey Smirnov static void imx7_snvs_class_init(ObjectClass *klass, void *data) 660a7bc1c0SAndrey Smirnov { 670a7bc1c0SAndrey Smirnov DeviceClass *dc = DEVICE_CLASS(klass); 680a7bc1c0SAndrey Smirnov 690a7bc1c0SAndrey Smirnov dc->desc = "i.MX7 Secure Non-Volatile Storage Module"; 700a7bc1c0SAndrey Smirnov } 710a7bc1c0SAndrey Smirnov 720a7bc1c0SAndrey Smirnov static const TypeInfo imx7_snvs_info = { 730a7bc1c0SAndrey Smirnov .name = TYPE_IMX7_SNVS, 740a7bc1c0SAndrey Smirnov .parent = TYPE_SYS_BUS_DEVICE, 750a7bc1c0SAndrey Smirnov .instance_size = sizeof(IMX7SNVSState), 760a7bc1c0SAndrey Smirnov .instance_init = imx7_snvs_init, 770a7bc1c0SAndrey Smirnov .class_init = imx7_snvs_class_init, 780a7bc1c0SAndrey Smirnov }; 790a7bc1c0SAndrey Smirnov 800a7bc1c0SAndrey Smirnov static void imx7_snvs_register_type(void) 810a7bc1c0SAndrey Smirnov { 820a7bc1c0SAndrey Smirnov type_register_static(&imx7_snvs_info); 830a7bc1c0SAndrey Smirnov } 840a7bc1c0SAndrey Smirnov type_init(imx7_snvs_register_type) 85