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: 1001643 X-Patchwork-Delegate: jagannadh.teki@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.b="w9NaqW/6"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 430w2d100Hz9s5c for ; Thu, 22 Nov 2018 21:01:45 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id C4E73C21F02; Thu, 22 Nov 2018 10:01:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_MSPIKE_H2, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 36733C21F19; Thu, 22 Nov 2018 10:01:13 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id F1DB4C21EC2; Thu, 22 Nov 2018 10:01:09 +0000 (UTC) Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by lists.denx.de (Postfix) with ESMTPS id 98D34C21EBE for ; Thu, 22 Nov 2018 10:01:09 +0000 (UTC) Received: by mail-wm1-f67.google.com with SMTP id y139so8258181wmc.5 for ; Thu, 22 Nov 2018 02:01:09 -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=MO8xcZJoOIHI9flHOeprKhA54Si2rDEd6ioouoaq0gnpxSNKbEkRZSP1Pgbcr0TWGv Kqv7afbtw7S8zSG7hZnZSR+jnaQuU5+xlGRKwu1qPNEyT6SDJ+q0LXubn0OZu/IRF1w3 iT60mbj3Z77WuEchqdsACNj/sEWLvEwFTSVq26JqdcLReV6Hhs15iY7MLBkxP/3B1zpN qBbYcGelO51lI+b1I7FiIbda+FRFHPQ9FnAeKD+kV8iFSKiXYjJIxDc8ojFfcf+mUOBw hrmZ4tH7VSKWrqzBnOzxltCzHBYvOcOzV4pp8orntuGYlwhfIELmFiN14xHw1blVx9SN MYNQ== X-Gm-Message-State: AA+aEWbbkQ9L7STxuzFSZuW3TxmjyffMnT2fCRjMcHHTVlATy7nmErWn 0NnuTDyEfyRGm+jfi4aDT1sJQQ== 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 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 Cc: u-boot@lists.denx.de, linux-amlogic@lists.infradead.org Subject: [U-Boot] [PATCH u-boot v3 1/3] regmap: add regmap_read_poll_timeout() helper X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" 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: 1001645 X-Patchwork-Delegate: jagannadh.teki@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.b="zMZN3GEi"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 430w4Q0qNVz9s5c for ; Thu, 22 Nov 2018 21:03:18 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id C9639C21F31; Thu, 22 Nov 2018 10:02:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_MSPIKE_H2, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id CF742C21F52; Thu, 22 Nov 2018 10:01:24 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id F0F28C21F1C; Thu, 22 Nov 2018 10:01:14 +0000 (UTC) Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by lists.denx.de (Postfix) with ESMTPS id 260BFC21EC5 for ; Thu, 22 Nov 2018 10:01:11 +0000 (UTC) Received: by mail-wr1-f65.google.com with SMTP id z5so4308926wrt.11 for ; Thu, 22 Nov 2018 02:01:11 -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=EuUb8xnayYy2d7c9FAuNC3na7Mz7bD5hNwuFdo/WjKL6GHwEDULfMEYE65U+ZqwcDM 0eyYUEXTaDY6DZIEveUF/Jw5WK3AfsPsUgYdDOAiEesoeJQCtoxrqBwPXpqWCT+udiVl 72fV+gXmqthlicPgBhebkC/47mHfyGMSiIJTLA9UiiVB6b+Pxbg0SkBXD01tC6k1FSap ZsWSi9hDzls4Y13vB76aBEuVwpK2oPBgpddzqv68JQywqY5gV4RLYSd203cEyj4Ea4Ag URQLewhbssnxIP+jDi6KtuQFnFL9Ul59F8sd8vOrj8LloEKE2HyDaCg5Bq3kxLStPSdM A5Ug== X-Gm-Message-State: AA+aEWaV3uswNNu4Ar7pshrOHtGnDWes75WWsxvEq35wdLAhxEXkwUTc 1sx8zbQihMG5h8/o1Vnybvp8Aw== 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 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 Cc: u-boot@lists.denx.de, linux-amlogic@lists.infradead.org Subject: [U-Boot] [PATCH u-boot v3 2/3] test: regmap: add regmap_read_poll_timeout test X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" 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: 1001644 X-Patchwork-Delegate: jagannadh.teki@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.b="G1mw/c7K"; dkim-atps=neutral Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 430w3p47J6z9sB5 for ; Thu, 22 Nov 2018 21:02:46 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 530A3C21F4E; Thu, 22 Nov 2018 10:01:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_MSPIKE_H2, T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 098D5C21F47; Thu, 22 Nov 2018 10:01:21 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id AA640C21F05; Thu, 22 Nov 2018 10:01:16 +0000 (UTC) Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by lists.denx.de (Postfix) with ESMTPS id 94613C21F17 for ; Thu, 22 Nov 2018 10:01:12 +0000 (UTC) Received: by mail-wr1-f66.google.com with SMTP id c14so2977533wrr.0 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=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=BtpSljx/o9KvJRA4aAsPkBlHmntO28xQGhM0TfdL4yiGfTF4jntg0VBJ9z+HY36ulQ 60ocUken7BcWLaPk1TwPGMHHO8chOqoIw57K98r14bSLel9p39c924Y4Oq1mtg6rBtJr 5SoZO8BqyztcKGcwLl61vPDXyM+sbadWZUWY2Jda9tiKgNO/Fdg3eUxJsKFl8rXS3wMW xMYesqLRy53BI9LmXpcEhCG+T4a7U1G2drVtX6F9KETwI+6qepvKEMd7XDygGy86mVgC sbDw/rK53ZxfXOE8GCXwfAkpJs5B9gyJ4Y18uUA/Ff7rtgnQ+jPQnGzfFHc/hdNxIi82 V0qw== X-Gm-Message-State: AA+aEWYwkbFQiSOXd6hhKt1F8Whi5zGsolreKrWx9Gb3+5c0Wo5R3BaP SGiDe3x9fmB0TqoliUNP2ZJQaA== 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 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 Cc: u-boot@lists.denx.de, linux-amlogic@lists.infradead.org Subject: [U-Boot] [PATCH u-boot v3 3/3] spi: Add Amlogic Meson SPI Flash Controller driver X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" 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), +};