From nobody Fri May 17 19:54:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1601288037; cv=none; d=zohomail.com; s=zohoarc; b=kI5OgTuRnEdRh86m61/rEZLjr2oT8Y+cY26ztKeKT1lkrMOJk10gMtBkVLl+A+DKHtBAqSXtRpuSJrnncY6h7uSl3a2/VTD3So2dgcvyMuHNnExiUv4vzDyWooUOYLZIU5yYGnv5lAGwJNL+Y5kV9K96gN2rQ6dCrzpzEnVIUw8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601288037; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject; bh=nN8OUO8G/I5uJu9MqGsHVSz2gMnkdh9PLBBCTFybCKs=; b=jkJCEihofgRZ9JwqnF9PHa8Oz1KlvtScIXWmBBiZKIPXh//d2N9qIdM4br21d5gGd5DqNSRQxuhesGsUGKlR5aF7WhgzT/WuB2u95hCEVmZO4ZhqTBXIpSbL2lKMgW07rcATF68VSrl1WEDf0I8OIifGyqukNpIPrpg4IlPNHYo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601288037987806.549767323932; Mon, 28 Sep 2020 03:13:57 -0700 (PDT) Received: from localhost ([::1]:43412 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kMqAO-0005lA-M6 for importer@patchew.org; Mon, 28 Sep 2020 06:13:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33864) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kMq8c-0003dv-Hg for qemu-devel@nongnu.org; Mon, 28 Sep 2020 06:12:06 -0400 Received: from mail-pg1-x541.google.com ([2607:f8b0:4864:20::541]:40059) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kMq8Y-0001RX-Fr for qemu-devel@nongnu.org; Mon, 28 Sep 2020 06:12:05 -0400 Received: by mail-pg1-x541.google.com with SMTP id s31so436163pga.7 for ; Mon, 28 Sep 2020 03:12:01 -0700 (PDT) Received: from localhost.localdomain (111-241-115-7.dynamic-ip.hinet.net. [111.241.115.7]) by smtp.gmail.com with ESMTPSA id u2sm814747pji.50.2020.09.28.03.11.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 03:12:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nN8OUO8G/I5uJu9MqGsHVSz2gMnkdh9PLBBCTFybCKs=; b=aboCT2ArQYYDhf782qD0kk00pyozozQtQ2IR0olfKL4JReuQDZ5ntreBB8IFxArOEO xq2IhLlpTLCN92TP9cAzK1DCHCKrnsH+N42lqgKBjeaSzdTP3XoGLNflxcD6gx2fka57 3Z6Igdj9sWAZE+BN1KC++V+a0hoTtZjrlS9Ms4TsJVxnRKW3g7P9d0PXvMKG5AN0eigg xN6Hs/sIW/asTHGHgeIUNtCF9bXzbqr9QDVJ81eGt3fssClwyp4bGWZxdL8J7igUpFdK LENFXn9NRgI/u1tq6bbZvHtF1FZDG5CLRvPRjTpMBDyPp8FT9G+J0Cd4RWUPv1xwyBs5 3JsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=nN8OUO8G/I5uJu9MqGsHVSz2gMnkdh9PLBBCTFybCKs=; b=DGKmiN4i0iVhHtPnBP3GqPAqRXMhtJs9jVfJTGOzCxtRjoaHSWX7LLTor+kcVYQrkp d4x2EMIXqyoYM+vs79cvI4EFyM7Ikr+9IpwpemP/2JJz2BnOADApXZbwCiqMuHZTMgVy 6nvBFC8MFSZzicw+A4xEEj9LwKkzV0Y9Zb4aKBWJZOuSw+7O0zSJVegrDv66aRQbeLfM zxqJAvbzKqqa0vDjYBbbO7Wehe4miGN4Lt7zht4rzl8O0CWFD7qnyAXGznqdy+1TAd+a xRT6knw6IyGWK9uBnwzcao+B5hHFqCoilewMR7AMul/igKv8NJHKq8AjNrF2Nyf8a28G MVTw== X-Gm-Message-State: AOAM533HUAN09UMN+YBA6/sF3hRvZ/3gQ6/00TC+mziyrjKzfgmjj255 HYF9rwgLeUhDAFTq5+0+TwDYD+I5knGKuw== X-Google-Smtp-Source: ABdhPJz+VEMYAFr2ORSEiTPliIuseVDH3qacbT7EQWzjsw6BB6tDucwMcj2OGrTHzNo6S8vwF/2iVQ== X-Received: by 2002:a17:902:c394:b029:d2:4ca:2e22 with SMTP id g20-20020a170902c394b02900d204ca2e22mr883726plg.77.1601287920409; Mon, 28 Sep 2020 03:12:00 -0700 (PDT) From: Green Wan To: Subject: [RFC PATCH v6 1/2] hw/misc/sifive_u_otp: Add write function and write-once protection Date: Mon, 28 Sep 2020 18:11:45 +0800 Message-Id: <20200928101146.12786-2-green.wan@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200928101146.12786-1-green.wan@sifive.com> References: <20200928101146.12786-1-green.wan@sifive.com> Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::541; envelope-from=green.wan@sifive.com; helo=mail-pg1-x541.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alistair23@gmail.com, bmeng.cn@gmail.com, qemu-riscv@nongnu.org, qemu-devel@nongnu.org, Green Wan Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" - Add write operation to update fuse data bit when PWE bit is on. - Add array, fuse_wo, to store the 'written' status for all bits of OTP to block the write operation. Signed-off-by: Green Wan Reviewed-by: Alistair Francis --- hw/misc/sifive_u_otp.c | 30 +++++++++++++++++++++++++++++- include/hw/misc/sifive_u_otp.h | 3 +++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/hw/misc/sifive_u_otp.c b/hw/misc/sifive_u_otp.c index c2f3c8e129..685c1f8e07 100644 --- a/hw/misc/sifive_u_otp.c +++ b/hw/misc/sifive_u_otp.c @@ -25,6 +25,14 @@ #include "qemu/module.h" #include "hw/misc/sifive_u_otp.h" =20 +#define WRITTEN_BIT_ON 0x1 + +#define SET_FUSEARRAY_BIT(map, i, off, bit) \ + map[i] =3D bit ? (map[i] | bit << off) : (map[i] & ~(bit << off)) + +#define GET_FUSEARRAY_BIT(map, i, off) \ + ((map[i] >> off) & 0x1) + static uint64_t sifive_u_otp_read(void *opaque, hwaddr addr, unsigned int = size) { SiFiveUOTPState *s =3D opaque; @@ -123,7 +131,24 @@ static void sifive_u_otp_write(void *opaque, hwaddr ad= dr, s->ptrim =3D val32; break; case SIFIVE_U_OTP_PWE: - s->pwe =3D val32; + s->pwe =3D val32 & SIFIVE_U_OTP_PWE_EN; + + /* PWE is enabled. Ignore PAS=3D1 (no redundancy cell) */ + if (s->pwe && !s->pas) { + if (GET_FUSEARRAY_BIT(s->fuse_wo, s->pa, s->paio)) { + qemu_log_mask(LOG_GUEST_ERROR, + "Error: write idx<%u>, bit<%u>\n", + s->pa, s->paio); + break; + } + + /* write bit data */ + SET_FUSEARRAY_BIT(s->fuse, s->pa, s->paio, s->pdin); + + /* update written bit */ + SET_FUSEARRAY_BIT(s->fuse_wo, s->pa, s->paio, WRITTEN_BIT_ON); + } + break; default: qemu_log_mask(LOG_GUEST_ERROR, "%s: bad write: addr=3D0x%" HWADDR_= PRIx @@ -165,6 +190,9 @@ static void sifive_u_otp_reset(DeviceState *dev) /* Make a valid content of serial number */ s->fuse[SIFIVE_U_OTP_SERIAL_ADDR] =3D s->serial; s->fuse[SIFIVE_U_OTP_SERIAL_ADDR + 1] =3D ~(s->serial); + + /* Initialize write-once map */ + memset(s->fuse_wo, 0x00, sizeof(s->fuse_wo)); } =20 static void sifive_u_otp_class_init(ObjectClass *klass, void *data) diff --git a/include/hw/misc/sifive_u_otp.h b/include/hw/misc/sifive_u_otp.h index 82c9176c8f..ebffbc1fa5 100644 --- a/include/hw/misc/sifive_u_otp.h +++ b/include/hw/misc/sifive_u_otp.h @@ -36,6 +36,8 @@ #define SIFIVE_U_OTP_PTRIM 0x34 #define SIFIVE_U_OTP_PWE 0x38 =20 +#define SIFIVE_U_OTP_PWE_EN (1 << 0) + #define SIFIVE_U_OTP_PCE_EN (1 << 0) =20 #define SIFIVE_U_OTP_PDSTB_EN (1 << 0) @@ -75,6 +77,7 @@ struct SiFiveUOTPState { uint32_t ptrim; uint32_t pwe; uint32_t fuse[SIFIVE_U_OTP_NUM_FUSES]; + uint32_t fuse_wo[SIFIVE_U_OTP_NUM_FUSES]; /* config */ uint32_t serial; }; --=20 2.17.1 From nobody Fri May 17 19:54:24 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1601287999; cv=none; d=zohomail.com; s=zohoarc; b=DfVFDXRYePBuZUug7o1og/l/8EUd8zNA/l3qugduwuctgji5GZqcQ8WQdcfzcizmzQmUkZm9BDfRdXwqxcQQb4GluhU6zMHaeyg97o0CJ0OGMA60rPwk1gzJn0tuIDip8mg7zptLQ/tNE0bDA6hSAYBsaXmx5kqSVtnP+VSy7IM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1601287999; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject; bh=EcaAE9/QEvSGpNwNrLMhItuN/KVCOVDKbhwuItE368U=; b=lTsynnG0N5fDeSFhskVf+lAjNKIm22y4vk85tnVK2rwh0seRsDLkWPKOrrxaSWknc3sHknvyhHtVVhHFWmtZtSXVCD7YIBdJI+FfRFljysR91wf4Ac2Tpx5IAmDOD4gdGsqDnU0Nqrc8U5u9O8EZHB/UFHZXKnuyGqfC2Q2itV4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1601287999541364.94487561792835; Mon, 28 Sep 2020 03:13:19 -0700 (PDT) Received: from localhost ([::1]:40936 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kMq9m-0004ih-Am for importer@patchew.org; Mon, 28 Sep 2020 06:13:18 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33892) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kMq8d-0003f9-BN for qemu-devel@nongnu.org; Mon, 28 Sep 2020 06:12:07 -0400 Received: from mail-pj1-x1041.google.com ([2607:f8b0:4864:20::1041]:36350) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kMq8a-0001Rm-FL for qemu-devel@nongnu.org; Mon, 28 Sep 2020 06:12:07 -0400 Received: by mail-pj1-x1041.google.com with SMTP id b17so367232pji.1 for ; Mon, 28 Sep 2020 03:12:03 -0700 (PDT) Received: from localhost.localdomain (111-241-115-7.dynamic-ip.hinet.net. [111.241.115.7]) by smtp.gmail.com with ESMTPSA id u2sm814747pji.50.2020.09.28.03.12.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2020 03:12:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=EcaAE9/QEvSGpNwNrLMhItuN/KVCOVDKbhwuItE368U=; b=accCgtIVEt7kyhcRhwJzoIqD4dl+Qdyo620dRYLr3yTFvZP5DzNAJMkk/anLO2fd4C yImUlSPxSncBMu8Ui7zt46fn1FosRZRw2UulQco5wwrpvBtRjFH5PmkFJ7W3K28HT6AS fTzlpsYggcoTkQ35FLmcpaYXT+aqWOuTQMgTDRf2liRGzjdd4U1JRbZ080NJl4IjlySf GmTi9PSWr7YmvqYtvX9qx7Bl6XaYiTPhpR4dIArTlp/trBeB3hHOKgUhVpZJY6Pm7WjH 6RtbfFCr7dHdfF731eZCeXgR9JP8QX9W6LIusYpRyOuV/cKb9VIUEzgUiuYVkMvgKcI5 9dXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=EcaAE9/QEvSGpNwNrLMhItuN/KVCOVDKbhwuItE368U=; b=hFUMn1Qsx93XKi2vqe/Hio81ryM/y8V+mfARgYASgJ8EzMKFs1rjqYTs4IlKIDCWcd 6JmlqgwvoGmSlZRtgdU7o1rd37OtvT8pOWLHQBNJVZ5DJ7sPbzBHTcvEFcU3dmVTTDky WLGb5FrjyGqNBQfLJx7Njz4306yvm9fISB3kzloqimWm0+En1o4uYxhUhwMMykJQHNEE 4CY//VGjdlz0BpsNXYTKIFkGKm0X1qtxoX2lnG4HQ9oHtZjDpaUUzT4MCNankhjKZPcs WsEqzfZgySBg+g5tWnyOUXuo6MVPK/vf6gWAIyFJGRiR8qg8sBgFLV+4jbuhkaTJU0NN pmzg== X-Gm-Message-State: AOAM532XsAf9pjsron3SDMqpCq3vdS+PTQx3nq1KHVfVbWRVg7yxXHok ++B11N8dsPY5vzC+FO/NohKbQTyMH5nkCw== X-Google-Smtp-Source: ABdhPJweAkynNS0iQmDhVQb7EmBSftbfSG5SyCukTq6sk2TJKVUmQ/Ze04pvAL74/fyvcZLOinteEQ== X-Received: by 2002:a17:90a:ee0d:: with SMTP id e13mr673588pjy.227.1601287922058; Mon, 28 Sep 2020 03:12:02 -0700 (PDT) From: Green Wan To: Subject: [RFC PATCH v6 2/2] hw/misc/sifive_u_otp: Add backend drive support Date: Mon, 28 Sep 2020 18:11:46 +0800 Message-Id: <20200928101146.12786-3-green.wan@sifive.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200928101146.12786-1-green.wan@sifive.com> References: <20200928101146.12786-1-green.wan@sifive.com> Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1041; envelope-from=green.wan@sifive.com; helo=mail-pj1-x1041.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alistair23@gmail.com, bmeng.cn@gmail.com, qemu-riscv@nongnu.org, qemu-devel@nongnu.org, Green Wan Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add '-drive' support to OTP device. Allow users to assign a raw file as OTP image. test commands for 16k otp.img filled with zero: dd if=3D/dev/zero of=3D./otp.img bs=3D1k count=3D16 ./qemu-system-riscv64 -M sifive_u -m 256M -nographic -bios none \ -kernel ../opensbi/build/platform/sifive/fu540/firmware/fw_payload.elf \ -d guest_errors -drive if=3Dnone,format=3Draw,file=3Dotp.img Signed-off-by: Green Wan --- hw/misc/sifive_u_otp.c | 51 ++++++++++++++++++++++++++++++++++ include/hw/misc/sifive_u_otp.h | 2 ++ 2 files changed, 53 insertions(+) diff --git a/hw/misc/sifive_u_otp.c b/hw/misc/sifive_u_otp.c index 685c1f8e07..f2585c1ed7 100644 --- a/hw/misc/sifive_u_otp.c +++ b/hw/misc/sifive_u_otp.c @@ -19,11 +19,14 @@ */ =20 #include "qemu/osdep.h" +#include "qapi/error.h" #include "hw/qdev-properties.h" #include "hw/sysbus.h" #include "qemu/log.h" #include "qemu/module.h" #include "hw/misc/sifive_u_otp.h" +#include "sysemu/blockdev.h" +#include "sysemu/block-backend.h" =20 #define WRITTEN_BIT_ON 0x1 =20 @@ -54,6 +57,16 @@ static uint64_t sifive_u_otp_read(void *opaque, hwaddr a= ddr, unsigned int size) if ((s->pce & SIFIVE_U_OTP_PCE_EN) && (s->pdstb & SIFIVE_U_OTP_PDSTB_EN) && (s->ptrim & SIFIVE_U_OTP_PTRIM_EN)) { + + /* read from backend */ + if (s->blk) { + int32_t buf; + + blk_pread(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &buf, + SIFIVE_U_OTP_FUSE_WORD); + return buf; + } + return s->fuse[s->pa & SIFIVE_U_OTP_PA_MASK]; } else { return 0xff; @@ -145,6 +158,12 @@ static void sifive_u_otp_write(void *opaque, hwaddr ad= dr, /* write bit data */ SET_FUSEARRAY_BIT(s->fuse, s->pa, s->paio, s->pdin); =20 + /* write to backend */ + if (s->blk) { + blk_pwrite(s->blk, s->pa * SIFIVE_U_OTP_FUSE_WORD, &val32, + SIFIVE_U_OTP_FUSE_WORD, 0); + } + /* update written bit */ SET_FUSEARRAY_BIT(s->fuse_wo, s->pa, s->paio, WRITTEN_BIT_ON); } @@ -168,16 +187,48 @@ static const MemoryRegionOps sifive_u_otp_ops =3D { =20 static Property sifive_u_otp_properties[] =3D { DEFINE_PROP_UINT32("serial", SiFiveUOTPState, serial, 0), + DEFINE_PROP_DRIVE("drive", SiFiveUOTPState, blk), DEFINE_PROP_END_OF_LIST(), }; =20 static void sifive_u_otp_realize(DeviceState *dev, Error **errp) { SiFiveUOTPState *s =3D SIFIVE_U_OTP(dev); + DriveInfo *dinfo; =20 memory_region_init_io(&s->mmio, OBJECT(dev), &sifive_u_otp_ops, s, TYPE_SIFIVE_U_OTP, SIFIVE_U_OTP_REG_SIZE); sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio); + + dinfo =3D drive_get_next(IF_NONE); + if (dinfo) { + int ret; + uint64_t perm; + int filesize; + BlockBackend *blk; + + blk =3D blk_by_legacy_dinfo(dinfo); + filesize =3D SIFIVE_U_OTP_NUM_FUSES * SIFIVE_U_OTP_FUSE_WORD; + if (blk_getlength(blk) < filesize) { + error_setg(errp, "OTP drive size < 16K"); + return; + } + + qdev_prop_set_drive_err(dev, "drive", blk, errp); + + if (s->blk) { + perm =3D BLK_PERM_CONSISTENT_READ | + (blk_is_read_only(s->blk) ? 0 : BLK_PERM_WRITE); + ret =3D blk_set_perm(s->blk, perm, BLK_PERM_ALL, errp); + if (ret < 0) { + return; + } + + if (blk_pread(s->blk, 0, s->fuse, filesize) !=3D filesize) { + error_setg(errp, "failed to read the initial flash content= "); + } + } + } } =20 static void sifive_u_otp_reset(DeviceState *dev) diff --git a/include/hw/misc/sifive_u_otp.h b/include/hw/misc/sifive_u_otp.h index ebffbc1fa5..5d0d7df455 100644 --- a/include/hw/misc/sifive_u_otp.h +++ b/include/hw/misc/sifive_u_otp.h @@ -46,6 +46,7 @@ =20 #define SIFIVE_U_OTP_PA_MASK 0xfff #define SIFIVE_U_OTP_NUM_FUSES 0x1000 +#define SIFIVE_U_OTP_FUSE_WORD 4 #define SIFIVE_U_OTP_SERIAL_ADDR 0xfc =20 #define SIFIVE_U_OTP_REG_SIZE 0x1000 @@ -80,6 +81,7 @@ struct SiFiveUOTPState { uint32_t fuse_wo[SIFIVE_U_OTP_NUM_FUSES]; /* config */ uint32_t serial; + BlockBackend *blk; }; =20 #endif /* HW_SIFIVE_U_OTP_H */ --=20 2.17.1