From patchwork Thu Nov 22 10:01:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neil Armstrong X-Patchwork-Id: 10693777 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 837A75A4 for ; Thu, 22 Nov 2018 10:01:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 749582C9B0 for ; Thu, 22 Nov 2018 10:01:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 68A2A2CA35; Thu, 22 Nov 2018 10:01:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 3363D2CA0B for ; Thu, 22 Nov 2018 10:01:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=75MfCpWdWdy6/7FPJVMiwO04s2vZ/JnG0h/ces4t+B0=; b=iRlbhm4RK0luYh lkYhXEkKPXba0nEKet5X2ddRr+3jHNQsKJOZQcz5qEAuBL930FZvwLwgMBSzOueut2oxzbpGCISzq oPh4n0kI3FfRRbuTV9J89GenaZwdJSY8SxnBqh5FeF4rRyxhuYBXYIxh0ng6xLF2eiKQ7VbTIggSB BPxQsACDOvMpHtp+z/bx+fIEPBWkfdylexD/RQSqdyKKa2G6YFKzlY3eDjVUJ6nktdgdLfodqgNhX 1XewD0L9QdsiSfvbFGrqrybo//F9fe3MMiHqgP5rTiNuSHIueZLz+ZZ+VWu7B4jWfJI4Jjym5aoF5 y4gIdFKRCMkoq029+Gsg==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gPlnd-0002sn-OL; Thu, 22 Nov 2018 10:01:29 +0000 Received: from mail-wm1-x341.google.com ([2a00:1450:4864:20::341]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gPlnU-0002nS-Sv for linux-amlogic@lists.infradead.org; Thu, 22 Nov 2018 10:01:28 +0000 Received: by mail-wm1-x341.google.com with SMTP id y1so5069706wmi.3 for ; Thu, 22 Nov 2018 02:01:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+/d3fQ0KUjaUiMVLjI7Wm3yOqDGWkDypZ+4hsTrjFwk=; b=w9NaqW/64Nss1nd4TCyx/RCxwnV9W64hc8s/PjnYLhnJXWX5NTMh3t0Tha2GKX4SkA jyEffGklcytNPW49y/F27P0bOYg2PlsJ4Gj29R8qSBBiPNaPxGSEYjTrrizWSaxETM7m EPxTMFAtWRb9BgMu2Len/s5uY/ymqHW8Fh+r2yF9GeO78pw89T3D5w9OLuEMci8Q9ohW WuO1TUVUgJR9tjs1hIDuvwcOQx6PnYbb6mUNlZEf27tfK1PyhG3rAcy4+p9IEo6wj10t a9fO+kTidswy4aRE1U/W72fDm04ng3vHO/ZnMP3TR9iqoCWR/2/YtAe3+xQ45sKoWdRb bp3Q== 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:mime-version:content-transfer-encoding; bh=+/d3fQ0KUjaUiMVLjI7Wm3yOqDGWkDypZ+4hsTrjFwk=; b=L7AmCb1+TH8bs3FygRX0Vc9CRn4kz+XfC8NZQbFTr7/HRvrOiOZYsQN6YZt+JCbCmH oQymi9WNG4GIzSyAqyvn8BVRLApK1bQEoT3t+KPrJ2SCi1Fq1C8tHSzuKMTyIVX3f14s qFXoOEwcgJMQUkYvvJc3mL7vpyt2l+kouwakDbEKAoib8J2IoJj43mqf5O6S8pNd9Pbr UsLdJZLjJY2vK5CMu56pXNp8N1em/Bp+ikBQVUtgvRykvNmrXENB6fN/DdBjwM7UaUGZ 17i3u8n/nyL1dtxNQqKMjkS/sEo3yCE/gGwIRnYU6LNwa/mdYpTd/1vaYE8fuTqySa5Z uA+g== X-Gm-Message-State: AA+aEWZmjVcgG41v9Rr0+37zT2QiseGca9bEZJtNRMrMIIuc4MPYotb/ JtHelJ8pKbQx5xQTU2NofNVDvA== X-Google-Smtp-Source: AJdET5dXdzU7zxLEksariGHodwIfzZTVHLIZT+Hhs3HYsyB18iKncguAZfrQqi7B8ZO3Vu9n3ZjhBA== X-Received: by 2002:a1c:7c0b:: with SMTP id x11mr8862070wmc.20.1542880868997; Thu, 22 Nov 2018 02:01:08 -0800 (PST) Received: from bender.baylibre.local (lmontsouris-657-1-212-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id t82-v6sm4677822wme.30.2018.11.22.02.01.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 22 Nov 2018 02:01:08 -0800 (PST) From: Neil Armstrong To: jagan@openedev.com, sjg@chromium.org Subject: [PATCH u-boot v3 1/3] regmap: add regmap_read_poll_timeout() helper Date: Thu, 22 Nov 2018 11:01:03 +0100 Message-Id: <20181122100105.14829-2-narmstrong@baylibre.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181122100105.14829-1-narmstrong@baylibre.com> References: <20181122100105.14829-1-narmstrong@baylibre.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181122_020120_929177_C97C0E29 X-CRM114-Status: GOOD ( 11.86 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: u-boot@lists.denx.de, linux-amlogic@lists.infradead.org, Neil Armstrong Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add the regmap_read_poll_timeout() macro based on the Linux implementation to simplify register polling with configurable timeout and sleep. Acked-by: Jagan Teki Signed-off-by: Neil Armstrong --- include/regmap.h | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/include/regmap.h b/include/regmap.h index b2b733fda6..a3afb72df5 100644 --- a/include/regmap.h +++ b/include/regmap.h @@ -239,6 +239,44 @@ int regmap_raw_read_range(struct regmap *map, uint range_num, uint offset, #define regmap_get(map, type, member, valp) \ regmap_range_get(map, 0, type, member, valp) +/** + * regmap_read_poll_timeout - Poll until a condition is met or a timeout occurs + * + * @map: Regmap to read from + * @addr: Offset to poll + * @val: Unsigned integer variable to read the value into + * @cond: Break condition (usually involving @val) + * @sleep_us: Maximum time to sleep between reads in us (0 tight-loops). + * @timeout_ms: Timeout in ms, 0 means never timeout + * + * Returns 0 on success and -ETIMEDOUT upon a timeout or the regmap_read + * error return value in case of a error read. In the two former cases, + * the last read value at @addr is stored in @val. Must not be called + * from atomic context if sleep_us or timeout_us are used. + * + * This is modelled after the regmap_read_poll_timeout macros in linux but + * with millisecond timeout. + */ +#define regmap_read_poll_timeout(map, addr, val, cond, sleep_us, timeout_ms) \ +({ \ + unsigned long __start = get_timer(0); \ + int __ret; \ + for (;;) { \ + __ret = regmap_read((map), (addr), &(val)); \ + if (__ret) \ + break; \ + if (cond) \ + break; \ + if ((timeout_ms) && get_timer(__start) > (timeout_ms)) { \ + __ret = regmap_read((map), (addr), &(val)); \ + break; \ + } \ + if ((sleep_us)) \ + udelay((sleep_us)); \ + } \ + __ret ?: ((cond) ? 0 : -ETIMEDOUT); \ +}) + /** * regmap_update_bits() - Perform a read/modify/write using a mask * From patchwork Thu Nov 22 10:01:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neil Armstrong X-Patchwork-Id: 10693775 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6E4F613AD for ; Thu, 22 Nov 2018 10:01:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5F3DF2C9A0 for ; Thu, 22 Nov 2018 10:01:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 53B0D2C9D3; Thu, 22 Nov 2018 10:01:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E7D202CA44 for ; Thu, 22 Nov 2018 10:01:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PeZsM2qirnAvrKjGVpJEd2WLzZvpqdOW7LfETs8kxg4=; b=QsSckqIx82N81U UUSSGcSGB3L4gJBKVa3LHsSBYpNMkRSsZ4YX8W5klZHL5vGJJcs3bwgTc1MKT1Dn+1571mGQW0odX 5bPe2lyCCfxYz643BbcBlkttWamVBp4DAd7Tasat6viLaAKID6MIY3ptcoR9xdUIu2H53tAxKGhkK NauVb/WiUywIKeLGaLIFPG2i5/zo3EXlgy6TUnI+43es8hC2e8xqtHoCRrr6vb2sc06x4NI+9dBx7 nfUipYwl0kNpXydnmzZyEV/D7KHFdx1M95LArOQZ4eh0RDy6irAteRAZ2rGtdKNl3fX7sBQdltx3v hlVbSjXIa7NaLWjPigYQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gPlne-0002tR-CN; Thu, 22 Nov 2018 10:01:30 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gPlnW-0002nT-8R for linux-amlogic@lists.infradead.org; Thu, 22 Nov 2018 10:01:29 +0000 Received: by mail-wr1-x443.google.com with SMTP id l9so8592798wrt.13 for ; Thu, 22 Nov 2018 02:01:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=n6QpmUiuy9vsWoqNGN+ET2oQAk3w6LQ7jXJJ09eRuBc=; b=zMZN3GEiA1udsCkAURA3mtGCIUmZqNqe1uKhFxaTJ0Lb3TXSgrh7Vwq8N/DkPXbbRb klZlQ2hKVZq5k7yitji4uJUKkIvKyf4IIIWsXgxm4M1NfgwQE05x787eIVpnQ6mAmrLz T5BUslBfPvO2BgC7GmnZzo2cV57j8u/PRM+Ri3JtMmo3p9Fo9ko2F8a2ID79M9A0WBVm i34T3QYsF4/J1OtJm7amxqzAB7HsQXbCIDKpJqs6luZM50rFL5eWUAkTY0yZcTtzuHKB qhEkA0M99T5rG33KBLSqocREepWs8vIOalf5cKC3QXFzlKCDwYwU5toCb6WO+Ikx0PXt zSuQ== 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:mime-version:content-transfer-encoding; bh=n6QpmUiuy9vsWoqNGN+ET2oQAk3w6LQ7jXJJ09eRuBc=; b=jssUjgbecROvVgvQa+OmFSBRCmsyY7BQjmUtC8fbGge8tYBbpj6IRXiVnuus1rsuZj ipe4VILvTj3KaGASRe5OwlfMDN0mQyogzs1yqI+ogT6sY4m8mdzJyCaW71E24d1iMLCF D5i6Ikm8VknUgQhIkQUqInyGpK2icmaZpoXskPSThRTt+5/hTYUJIp2qnthqGgF36Uid 1I1zKKFZUfcJktfKzTqVu/B2TLiafuRqUGg20lpvFPKDyMjdI56OO1J17YPWpuNZ5Epl gzss8O1hTcHqVAZpsg5ma+sJPEBIpxUxNQ4K5fkuOVCp/ct7/ddyzPhnnkU5P1X3GoyA DYNQ== X-Gm-Message-State: AA+aEWZIhNZOpt+qE9X7/8ihu7pAbNmlyf/nxmw86JExcVNTx376uYru 6/4Uc7UIN/GhMH1/lHM/Rb7D2g== X-Google-Smtp-Source: AFSGD/U4aZwgBtlx4SWO5Cfu/XPZXkWqLk6aM0g4q8F00pk3r9T5jK+MSpNjTfhJVA2Y8VFEF5Si3g== X-Received: by 2002:a5d:6411:: with SMTP id z17mr8862442wru.256.1542880870529; Thu, 22 Nov 2018 02:01:10 -0800 (PST) Received: from bender.baylibre.local (lmontsouris-657-1-212-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id t82-v6sm4677822wme.30.2018.11.22.02.01.09 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 22 Nov 2018 02:01:09 -0800 (PST) From: Neil Armstrong To: jagan@openedev.com, sjg@chromium.org Subject: [PATCH u-boot v3 2/3] test: regmap: add regmap_read_poll_timeout test Date: Thu, 22 Nov 2018 11:01:04 +0100 Message-Id: <20181122100105.14829-3-narmstrong@baylibre.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181122100105.14829-1-narmstrong@baylibre.com> References: <20181122100105.14829-1-narmstrong@baylibre.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181122_020122_292610_D40483FF X-CRM114-Status: GOOD ( 10.04 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: u-boot@lists.denx.de, linux-amlogic@lists.infradead.org, Neil Armstrong Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Add test to regmap_read_poll_timeout() helper to check the timeout works properly but cannot test proper condition matching since read/write calls are not executed in sandbox. Acked-by: Jagan Teki Signed-off-by: Neil Armstrong --- test/dm/regmap.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/dm/regmap.c b/test/dm/regmap.c index a8d7e6829e..9a70c159dd 100644 --- a/test/dm/regmap.c +++ b/test/dm/regmap.c @@ -144,3 +144,29 @@ static int dm_test_regmap_getset(struct unit_test_state *uts) } DM_TEST(dm_test_regmap_getset, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + +/* Read polling test */ +static int dm_test_regmap_poll(struct unit_test_state *uts) +{ + struct udevice *dev; + struct regmap *map; + uint reg; + unsigned long start; + + ut_assertok(uclass_get_device(UCLASS_SYSCON, 0, &dev)); + map = syscon_get_regmap(dev); + ut_assertok_ptr(map); + + start = get_timer(0); + + ut_asserteq(-ETIMEDOUT, + regmap_read_poll_timeout(map, 0, reg, + (reg == 0xcacafafa), + 1, 5 * CONFIG_SYS_HZ)); + + ut_assert(get_timer(start) > (5 * CONFIG_SYS_HZ)); + + return 0; +} + +DM_TEST(dm_test_regmap_poll, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); From patchwork Thu Nov 22 10:01:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neil Armstrong X-Patchwork-Id: 10693771 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B30C65A4 for ; Thu, 22 Nov 2018 10:01:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A1E622CA42 for ; Thu, 22 Nov 2018 10:01:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 957462CA57; Thu, 22 Nov 2018 10:01:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.6 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8520C2CA42 for ; Thu, 22 Nov 2018 10:01:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=zRYs7/nHAq5tr5ea8YX0ybS9NkqdtNMOULRmFpr08BE=; b=TgvMCY9x8B/vvT hT73UUewCbN5x0s+cgkwnDFg7t79dj6Uvv81CLJ+q8+T4vxzcRVwQAPxCsDxPcAPFcJ1JcdPaaVef LihOI1AZVnaL6/IM9AlzFeqPEtlHgK1XGdCsOtbfca+68KD4XnRVmCuun669HMpHM5WzD9z+0mE5r BCfCgiY2EU4WJpLyIrPMxsi0gQR760SJfa4jj2ZoPpFBTcanvWjLAl+nR+FiEro641K4RvBSiPB1G gynuzTy0nFwl41R+iAhlGPR4BLSWyv/beLAVzPpoE6VuFEfR9FfjiRETrD+2kv9JArK+taBHzWd+o hnl8XO3u/oOKoRKylQRA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gPlnX-0002oq-IQ; Thu, 22 Nov 2018 10:01:23 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gPlnU-0002nW-K5 for linux-amlogic@lists.infradead.org; Thu, 22 Nov 2018 10:01:22 +0000 Received: by mail-wr1-x441.google.com with SMTP id j2so8622964wrw.1 for ; Thu, 22 Nov 2018 02:01:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=CHp8GoUu5l8G5ZbVRLx76HqR4DCKJXDnr+H6Bk1fxWc=; b=G1mw/c7KV8thtLZC5oytp3RurAFRDVhUx8PVCnKJ3EBgtl5prBebsdCe1D7dL9U4EK hB2wiQDIdqiYTKuWFTPEF+M/ugEFa9Pxsd7VG8n7uDjK9HyxblZafoK4DLosyTUCnkDd 1Glu50eK6gETeDK0zmvd2jl0JDOLW5dhWexTRxRpn5XEW/OlzhnPQ88MeJvhjlPP5ENt +5ter5TTZUYyWSRmXYDLFiLlNMsWXwEhwZiEa7AUa4VdU2WSuseXNo/sdm8pLNPaBiy/ PHFi88CR/kL5CM4e0kbAUvHVjPIRkcZNgUV3GtEKw5OlXucOo1hgH0Ciz0UqgvhV/7J8 bHtg== 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:mime-version:content-transfer-encoding; bh=CHp8GoUu5l8G5ZbVRLx76HqR4DCKJXDnr+H6Bk1fxWc=; b=n9r7E4qp5czP3c8uRgc/Qglvwrm1ZGrRoC1yLUhZ5NGYYG3UG58lYSlh23DGjlisvh T5EMqixPB7fg3i0lV923wGzEoT79QVXYHwbqftD/OdSCThakllypzZ8ZTTc2kqzuC9wk 8vekIfZNvkM1GWUE6jT9GLBXs3tkjwu4uJn0FoV/UV40Gtu8dJHx4fSdgHZ6QuuI76+U agfYlGvcMUmy/xFzA/hFJBg+s5eoXjuFnb4z+ehsi2GVsZxBxgZHyL3ed0+1koI3zhy/ lPYIhUT+XbBE4VfMxgXKcMvRJdgOMTFZRRMldfM43BjfsM9NPTIFDOuOTdAYmNn88ErD OZdA== X-Gm-Message-State: AA+aEWZIVUHt7c5OSw/Mt2bqrAmYtHTywalZEFQ5G2IpVj/v3ZN0m6sb 0mDB0qd8yu3nZ0/dDFP77LrjdA== X-Google-Smtp-Source: AFSGD/WqgXtWB61h6HqNMNLXad+I1ZxhuKiUliF+i5HObn9ODkOASJ4oID/nhOfjRo2nJi7Qrp2JRQ== X-Received: by 2002:a5d:5284:: with SMTP id c4-v6mr8557741wrv.210.1542880871840; Thu, 22 Nov 2018 02:01:11 -0800 (PST) Received: from bender.baylibre.local (lmontsouris-657-1-212-31.w90-63.abo.wanadoo.fr. [90.63.244.31]) by smtp.gmail.com with ESMTPSA id t82-v6sm4677822wme.30.2018.11.22.02.01.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 22 Nov 2018 02:01:10 -0800 (PST) From: Neil Armstrong To: jagan@openedev.com, sjg@chromium.org Subject: [PATCH u-boot v3 3/3] spi: Add Amlogic Meson SPI Flash Controller driver Date: Thu, 22 Nov 2018 11:01:05 +0100 Message-Id: <20181122100105.14829-4-narmstrong@baylibre.com> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20181122100105.14829-1-narmstrong@baylibre.com> References: <20181122100105.14829-1-narmstrong@baylibre.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20181122_020120_655421_BA9919DA X-CRM114-Status: GOOD ( 21.17 ) X-BeenThere: linux-amlogic@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: u-boot@lists.denx.de, linux-amlogic@lists.infradead.org, Neil Armstrong Sender: "linux-amlogic" Errors-To: linux-amlogic-bounces+patchwork-linux-amlogic=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP The Amlogic Meson SoCs embeds a Flash oriented SPI Controller name SPIFC. This driver, ported from the Linux meson-spi-spifc driver, add support for this controller on the Amlogic Meson GX SoCs in U-Boot. Reviewed-by: Jagan Teki Signed-off-by: Neil Armstrong --- drivers/spi/Kconfig | 8 + drivers/spi/Makefile | 1 + drivers/spi/meson_spifc.c | 320 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 329 insertions(+) create mode 100644 drivers/spi/meson_spifc.c diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 516188ea88..6085788481 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -116,6 +116,14 @@ config ICH_SPI access the SPI NOR flash on platforms embedding this Intel ICH IP core. +config MESON_SPIFC + bool "Amlogic Meson SPI Flash Controller driver" + depends on ARCH_MESON + help + Enable the Amlogic Meson SPI Flash Controller SPIFC) driver. + This driver can be used to access the SPI NOR flash chips on + Amlogic Meson SoCs. + config MT7621_SPI bool "MediaTek MT7621 SPI driver" depends on ARCH_MT7620 diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index 7242ea7e40..67b42daf4e 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -31,6 +31,7 @@ obj-$(CONFIG_FSL_QSPI) += fsl_qspi.o obj-$(CONFIG_ICH_SPI) += ich.o obj-$(CONFIG_KIRKWOOD_SPI) += kirkwood_spi.o obj-$(CONFIG_LPC32XX_SSP) += lpc32xx_ssp.o +obj-$(CONFIG_MESON_SPIFC) += meson_spifc.o obj-$(CONFIG_MPC8XX_SPI) += mpc8xx_spi.o obj-$(CONFIG_MPC8XXX_SPI) += mpc8xxx_spi.o obj-$(CONFIG_MT7621_SPI) += mt7621_spi.o diff --git a/drivers/spi/meson_spifc.c b/drivers/spi/meson_spifc.c new file mode 100644 index 0000000000..3d551694cb --- /dev/null +++ b/drivers/spi/meson_spifc.c @@ -0,0 +1,320 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2014 Beniamino Galvani + * Copyright (C) 2018 BayLibre, SAS + * Author: Neil Armstrong + * + * Amlogic Meson SPI Flash Controller driver + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* register map */ +#define REG_CMD 0x00 +#define REG_ADDR 0x04 +#define REG_CTRL 0x08 +#define REG_CTRL1 0x0c +#define REG_STATUS 0x10 +#define REG_CTRL2 0x14 +#define REG_CLOCK 0x18 +#define REG_USER 0x1c +#define REG_USER1 0x20 +#define REG_USER2 0x24 +#define REG_USER3 0x28 +#define REG_USER4 0x2c +#define REG_SLAVE 0x30 +#define REG_SLAVE1 0x34 +#define REG_SLAVE2 0x38 +#define REG_SLAVE3 0x3c +#define REG_C0 0x40 +#define REG_B8 0x60 +#define REG_MAX 0x7c + +/* register fields */ +#define CMD_USER BIT(18) +#define CTRL_ENABLE_AHB BIT(17) +#define CLOCK_SOURCE BIT(31) +#define CLOCK_DIV_SHIFT 12 +#define CLOCK_DIV_MASK (0x3f << CLOCK_DIV_SHIFT) +#define CLOCK_CNT_HIGH_SHIFT 6 +#define CLOCK_CNT_HIGH_MASK (0x3f << CLOCK_CNT_HIGH_SHIFT) +#define CLOCK_CNT_LOW_SHIFT 0 +#define CLOCK_CNT_LOW_MASK (0x3f << CLOCK_CNT_LOW_SHIFT) +#define USER_DIN_EN_MS BIT(0) +#define USER_CMP_MODE BIT(2) +#define USER_CLK_NOT_INV BIT(7) +#define USER_UC_DOUT_SEL BIT(27) +#define USER_UC_DIN_SEL BIT(28) +#define USER_UC_MASK ((BIT(5) - 1) << 27) +#define USER1_BN_UC_DOUT_SHIFT 17 +#define USER1_BN_UC_DOUT_MASK (0xff << 16) +#define USER1_BN_UC_DIN_SHIFT 8 +#define USER1_BN_UC_DIN_MASK (0xff << 8) +#define USER4_CS_POL_HIGH BIT(23) +#define USER4_IDLE_CLK_HIGH BIT(29) +#define USER4_CS_ACT BIT(30) +#define SLAVE_TRST_DONE BIT(4) +#define SLAVE_OP_MODE BIT(30) +#define SLAVE_SW_RST BIT(31) + +#define SPIFC_BUFFER_SIZE 64 + +struct meson_spifc_priv { + struct regmap *regmap; + struct clk clk; +}; + +/** + * meson_spifc_drain_buffer() - copy data from device buffer to memory + * @spifc: the Meson SPI device + * @buf: the destination buffer + * @len: number of bytes to copy + */ +static void meson_spifc_drain_buffer(struct meson_spifc_priv *spifc, + u8 *buf, int len) +{ + u32 data; + int i = 0; + + while (i < len) { + regmap_read(spifc->regmap, REG_C0 + i, &data); + + if (len - i >= 4) { + *((u32 *)buf) = data; + buf += 4; + } else { + memcpy(buf, &data, len - i); + break; + } + i += 4; + } +} + +/** + * meson_spifc_fill_buffer() - copy data from memory to device buffer + * @spifc: the Meson SPI device + * @buf: the source buffer + * @len: number of bytes to copy + */ +static void meson_spifc_fill_buffer(struct meson_spifc_priv *spifc, + const u8 *buf, int len) +{ + u32 data = 0; + int i = 0; + + while (i < len) { + if (len - i >= 4) + data = *(u32 *)buf; + else + memcpy(&data, buf, len - i); + + regmap_write(spifc->regmap, REG_C0 + i, data); + + buf += 4; + i += 4; + } +} + +/** + * meson_spifc_txrx() - transfer a chunk of data + * @spifc: the Meson SPI device + * @dout: data buffer for TX + * @din: data buffer for RX + * @offset: offset of the data to transfer + * @len: length of the data to transfer + * @last_xfer: whether this is the last transfer of the message + * @last_chunk: whether this is the last chunk of the transfer + * Return: 0 on success, a negative value on error + */ +static int meson_spifc_txrx(struct meson_spifc_priv *spifc, + const u8 *dout, u8 *din, int offset, + int len, bool last_xfer, bool last_chunk) +{ + bool keep_cs = true; + u32 data; + int ret; + + if (dout) + meson_spifc_fill_buffer(spifc, dout + offset, len); + + /* enable DOUT stage */ + regmap_update_bits(spifc->regmap, REG_USER, USER_UC_MASK, + USER_UC_DOUT_SEL); + regmap_write(spifc->regmap, REG_USER1, + (8 * len - 1) << USER1_BN_UC_DOUT_SHIFT); + + /* enable data input during DOUT */ + regmap_update_bits(spifc->regmap, REG_USER, USER_DIN_EN_MS, + USER_DIN_EN_MS); + + if (last_chunk && last_xfer) + keep_cs = false; + + regmap_update_bits(spifc->regmap, REG_USER4, USER4_CS_ACT, + keep_cs ? USER4_CS_ACT : 0); + + /* clear transition done bit */ + regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_TRST_DONE, 0); + /* start transfer */ + regmap_update_bits(spifc->regmap, REG_CMD, CMD_USER, CMD_USER); + + /* wait for the current operation to terminate */ + ret = regmap_read_poll_timeout(spifc->regmap, REG_SLAVE, data, + (data & SLAVE_TRST_DONE), + 0, 5 * CONFIG_SYS_HZ); + + if (!ret && din) + meson_spifc_drain_buffer(spifc, din + offset, len); + + return ret; +} + +/** + * meson_spifc_xfer() - perform a single transfer + * @dev: the SPI controller device + * @bitlen: length of the transfer + * @dout: data buffer for TX + * @din: data buffer for RX + * @flags: transfer flags + * Return: 0 on success, a negative value on error + */ +static int meson_spifc_xfer(struct udevice *slave, unsigned int bitlen, + const void *dout, void *din, unsigned long flags) +{ + struct meson_spifc_priv *spifc = dev_get_priv(slave->parent); + int blen = bitlen / 8; + int len, done = 0, ret = 0; + + if (bitlen % 8) + return -EINVAL; + + debug("xfer len %d (%d) dout %p din %p\n", bitlen, blen, dout, din); + + regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB, 0); + + while (done < blen && !ret) { + len = min_t(int, blen - done, SPIFC_BUFFER_SIZE); + ret = meson_spifc_txrx(spifc, dout, din, done, len, + flags & SPI_XFER_END, + done + len >= blen); + done += len; + } + + regmap_update_bits(spifc->regmap, REG_CTRL, CTRL_ENABLE_AHB, + CTRL_ENABLE_AHB); + + return ret; +} + +/** + * meson_spifc_set_speed() - program the clock divider + * @dev: the SPI controller device + * @speed: desired speed in Hz + */ +static int meson_spifc_set_speed(struct udevice *dev, uint speed) +{ + struct meson_spifc_priv *spifc = dev_get_priv(dev); + unsigned long parent, value; + int n; + + parent = clk_get_rate(&spifc->clk); + n = max_t(int, parent / speed - 1, 1); + + debug("parent %lu, speed %u, n %d\n", parent, speed, n); + + value = (n << CLOCK_DIV_SHIFT) & CLOCK_DIV_MASK; + value |= (n << CLOCK_CNT_LOW_SHIFT) & CLOCK_CNT_LOW_MASK; + value |= (((n + 1) / 2 - 1) << CLOCK_CNT_HIGH_SHIFT) & + CLOCK_CNT_HIGH_MASK; + + regmap_write(spifc->regmap, REG_CLOCK, value); + + return 0; +} + +/** + * meson_spifc_set_mode() - setups the SPI bus mode + * @dev: the SPI controller device + * @mode: desired mode bitfield + * Return: 0 on success, -ENODEV on error + */ +static int meson_spifc_set_mode(struct udevice *dev, uint mode) +{ + struct meson_spifc_priv *spifc = dev_get_priv(dev); + + if (mode & (SPI_CPHA | SPI_RX_QUAD | SPI_RX_DUAL | + SPI_TX_QUAD | SPI_TX_DUAL)) + return -ENODEV; + + regmap_update_bits(spifc->regmap, REG_USER, USER_CLK_NOT_INV, + mode & SPI_CPOL ? USER_CLK_NOT_INV : 0); + + regmap_update_bits(spifc->regmap, REG_USER4, USER4_CS_POL_HIGH, + mode & SPI_CS_HIGH ? USER4_CS_POL_HIGH : 0); + + return 0; +} + +/** + * meson_spifc_hw_init() - reset and initialize the SPI controller + * @spifc: the Meson SPI device + */ +static void meson_spifc_hw_init(struct meson_spifc_priv *spifc) +{ + /* reset device */ + regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_SW_RST, + SLAVE_SW_RST); + /* disable compatible mode */ + regmap_update_bits(spifc->regmap, REG_USER, USER_CMP_MODE, 0); + /* set master mode */ + regmap_update_bits(spifc->regmap, REG_SLAVE, SLAVE_OP_MODE, 0); +} + +static const struct dm_spi_ops meson_spifc_ops = { + .xfer = meson_spifc_xfer, + .set_speed = meson_spifc_set_speed, + .set_mode = meson_spifc_set_mode, +}; + +static int meson_spifc_probe(struct udevice *dev) +{ + struct meson_spifc_priv *priv = dev_get_priv(dev); + int ret; + + ret = regmap_init_mem(dev_ofnode(dev), &priv->regmap); + if (ret) + return ret; + + ret = clk_get_by_index(dev, 0, &priv->clk); + if (ret) + return ret; + + ret = clk_enable(&priv->clk); + if (ret) + return ret; + + meson_spifc_hw_init(priv); + + return 0; +} + +static const struct udevice_id meson_spifc_ids[] = { + { .compatible = "amlogic,meson-gxbb-spifc", }, + { } +}; + +U_BOOT_DRIVER(meson_spifc) = { + .name = "meson_spifc", + .id = UCLASS_SPI, + .of_match = meson_spifc_ids, + .ops = &meson_spifc_ops, + .probe = meson_spifc_probe, + .priv_auto_alloc_size = sizeof(struct meson_spifc_priv), +};