From patchwork Mon Feb 21 07:59:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Claudiu Beznea X-Patchwork-Id: 12753206 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CBE0EC433F5 for ; Mon, 21 Feb 2022 08:00:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ytOIDA64cei+9sm4UnwOrBCumaKlpunuMVRl5C7yWMo=; b=qDPEQflZIVpv/f p0vt3LAWrsRmLjovP++t7rVg4GXHnPg2TlGthcJJR+koh5BJvDm6UatbewrEcdThKqyBuAVtyfZwo 3v3gExkyt+7LBYiesFwt1WMQyZ/IDryurCLhZwgAgK6CJrkIIx05xlHVHTKJJ/cqhM/RjVMTIOG7V yJ2jAdryj1ILME5prrTogJUEhm/VbRRC8EoG//VNPkzKXcquyM5CfcwphNpY5qtMfU8kJ/AKQ5+RV ScsCskKcUkwOnSxuOpP4JRfh/bBF3v3sDzfZeDlO3H/NkOCNcUD3H94P7JVzPXi9CEbXwjnyDqEJ6 pZsFYxyOl3xV9kwfKEfg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nM3as-0048Da-Hz; Mon, 21 Feb 2022 07:58:50 +0000 Received: from esa.microchip.iphmx.com ([68.232.154.123]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nM3aU-00481v-Ho for linux-arm-kernel@lists.infradead.org; Mon, 21 Feb 2022 07:58:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1645430306; x=1676966306; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bqtRma4i+aDRPSPwCrS3g3crpowmv6qvyEHjsN3/F0U=; b=Lc9GhhVPwY1B/W78OmkyFm5kAzt0bm19xjYBEKRptYZQFhMEoqzGIp99 phg0QuX8xJYrJCT95LOz+8C+r3vxCOnJTtu179+3fmplNf6fQAwCjlqI+ 8RVFoaqy4EXCv2Kdv+P2sJoZ6JT90tgNxqY/dLNLjhWl29Lgs4qSrEVBq VNqIV2M4ykq1H38+lmj2r+QIO1y6lQCWg5+EUPtKWPci9Tud/qHLlgl8R CdR3ZtqKFI5vTYW4YGMIK76VgtfIx8XPcD5MRZaeDvbtBxLhgSgZO/gXg yYp1UXPinFW+nAlJcAGr6O2NF0fciMTlVJfumQ9rEcPf48Lb2/mYx6Mqf A==; X-IronPort-AV: E=Sophos;i="5.88,385,1635231600"; d="scan'208";a="86422606" Received: from smtpout.microchip.com (HELO email.microchip.com) ([198.175.253.82]) by esa6.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 21 Feb 2022 00:58:24 -0700 Received: from chn-vm-ex02.mchp-main.com (10.10.85.144) by chn-vm-ex01.mchp-main.com (10.10.85.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Mon, 21 Feb 2022 00:58:24 -0700 Received: from localhost.localdomain (10.10.115.15) by chn-vm-ex02.mchp-main.com (10.10.85.144) with Microsoft SMTP Server id 15.1.2375.17 via Frontend Transport; Mon, 21 Feb 2022 00:58:22 -0700 From: Claudiu Beznea To: , , , CC: , , , Claudiu Beznea Subject: [PATCH v2 1/7] hwrng: atmel - add wait for ready support on read Date: Mon, 21 Feb 2022 09:59:22 +0200 Message-ID: <20220221075928.563806-2-claudiu.beznea@microchip.com> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20220221075928.563806-1-claudiu.beznea@microchip.com> References: <20220221075928.563806-1-claudiu.beznea@microchip.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220220_235826_684802_39BB05FE X-CRM114-Status: GOOD ( 16.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add wait for ready support on read. Signed-off-by: Claudiu Beznea --- drivers/char/hw_random/atmel-rng.c | 43 +++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/drivers/char/hw_random/atmel-rng.c b/drivers/char/hw_random/atmel-rng.c index ecb71c4317a5..1a4874668c04 100644 --- a/drivers/char/hw_random/atmel-rng.c +++ b/drivers/char/hw_random/atmel-rng.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -20,6 +21,7 @@ #define TRNG_CR 0x00 #define TRNG_MR 0x04 #define TRNG_ISR 0x1c +#define TRNG_ISR_DATRDY BIT(0) #define TRNG_ODATA 0x50 #define TRNG_KEY 0x524e4700 /* RNG */ @@ -36,25 +38,40 @@ struct atmel_trng { struct hwrng rng; }; +static bool atmel_trng_wait_ready(struct atmel_trng *trng, bool wait) +{ + int ready; + + ready = readl(trng->base + TRNG_ISR) & TRNG_ISR_DATRDY; + if (!ready && wait) + readl_poll_timeout(trng->base + TRNG_ISR, ready, + ready & TRNG_ISR_DATRDY, 1000, 20000); + + return !!ready; +} + static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait) { struct atmel_trng *trng = container_of(rng, struct atmel_trng, rng); u32 *data = buf; + int ret; + + ret = atmel_trng_wait_ready(trng, wait); + if (!ret) + goto out; - /* data ready? */ - if (readl(trng->base + TRNG_ISR) & 1) { - *data = readl(trng->base + TRNG_ODATA); - /* - ensure data ready is only set again AFTER the next data - word is ready in case it got set between checking ISR - and reading ODATA, so we don't risk re-reading the - same word - */ - readl(trng->base + TRNG_ISR); - return 4; - } else - return 0; + *data = readl(trng->base + TRNG_ODATA); + /* + * ensure data ready is only set again AFTER the next data word is ready + * in case it got set between checking ISR and reading ODATA, so we + * don't risk re-reading the same word + */ + readl(trng->base + TRNG_ISR); + ret = 4; + +out: + return ret; } static void atmel_trng_enable(struct atmel_trng *trng)