From patchwork Fri Dec 30 09:29:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Bornyakov X-Patchwork-Id: 37617 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2808288wrt; Fri, 30 Dec 2022 01:34:05 -0800 (PST) X-Google-Smtp-Source: AMrXdXt3XezUwgbnQ+PwvU6aYam1y3Y4jpVBfFMlpL+4VbG948HESdUJ5Ub+trrICSXjyiQyTDt9 X-Received: by 2002:a62:1852:0:b0:580:ff90:325a with SMTP id 79-20020a621852000000b00580ff90325amr22149253pfy.8.1672392844826; Fri, 30 Dec 2022 01:34:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672392844; cv=none; d=google.com; s=arc-20160816; b=diCCMcbrgkFq93KysCv2Yl3+iXmt2fVQ42J4TkfZWeNKxhfQbVdDIoYIOXTFCoCC31 b6TfNC/DBSpLepRWk/vDIh72FexVHi1gIEbGva/6IPV9VvyNsjFs0bvCDIFdpDbYK1tJ OG0tJNNkA/miTXUARmtq+DtC3VNA/8ffPYqsrq/t/gnIbwnmZAQ8LnPQMofTbGvwt/n6 l4DVfiFLfx2dYtH1kr0w0pZlUtS05/BCF29xKi97MLU9oJD/J3JCSA2wRocKz9Z8IX7u G5H15o3d0Ddw7uoZ5Jmy5owP+2JV5rtsLRQpG6MrBeHcdlk/rlNwnXsomcm4o1Cx8sQq MjMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=E+97m6JvyssMdHmCohBP8lkzhfpXA2XCzSvoMWD7Pkk=; b=s0yUbTItZAZJQN9vdTx0aDCqhT/3oKLLc3tCszdwwTcwLskA6rBTo+CWVzI9TubWyf kkVjfYdK2tK0InGmiz21aXxXsT3HrPcOXb99tfDRzDCsDS/MyEJExE0y0CmsZJ8BocF5 rKRFj0g89o7YXkmJGwy5OyCSCN605Ifi38L6ZhAyBJ1NkbtJJys5zLQZwf9vTvgQ5gTa HhDlxCx/3s1lvANUNjSCUBihMb/Rwev9lReQrIscvCcLLNd4bZRXD1D9zDqkoD2j1los T+y44WHP+PsX0a41CWtsMcm206ZsxHORaeyo5cgqUbsasMyYC86RRKF0WiaSVfd1HHrl NMOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@metrotek.ru header.s=mail header.b=TcRzognD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 128-20020a621886000000b005786897552esi21354363pfy.33.2022.12.30.01.33.52; Fri, 30 Dec 2022 01:34:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@metrotek.ru header.s=mail header.b=TcRzognD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234837AbiL3JcS (ORCPT + 99 others); Fri, 30 Dec 2022 04:32:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34516 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234815AbiL3JcQ (ORCPT ); Fri, 30 Dec 2022 04:32:16 -0500 Received: from mail.pr-group.ru (mail.pr-group.ru [178.18.215.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46CE21A207; Fri, 30 Dec 2022 01:32:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=metrotek.ru; s=mail; h=from:subject:date:message-id:to:cc:mime-version:content-transfer-encoding: in-reply-to:references; bh=VSd2LcWwTuqeoFl68RAmHo4OuKhql1HqrChr8YW56pw=; b=TcRzognDTEohbPbawNbU/JC6BphALjjW0akbUp7ZzTpW1fikunmI/0RVGfpywHCqfGh47O5OBawIN XgRGNPVMcAUu3x6vtdo8M/WsLJPUQjIkdwAW5PL1U0q/XRQNcrt7GrEAsqGrt/xljLKfIoRQEakTXQ GtDsSf4aVjvjWcyy7YXgIf8y+bIc1Y9cHPYmKjryvBSEXUSIzUwjLYtlkjHaCkyEONKiS/Hi8yRRpD nXehcVrlhBSxCA59a8EzC2eGsQL2pp6CyUV+nrKKtvOlU2LGlExI3BVEK/ssCbOWoYU9vekZxwMte8 fs5UsHtew9f79lYJC/q+MNMyTC421+w== X-Kerio-Anti-Spam: Build: [Engines: 2.16.5.1460, Stamp: 3], Multi: [Enabled, t: (0.000010,0.031093)], BW: [Enabled, t: (0.000022,0.000001)], RTDA: [Enabled, t: (0.089395), Hit: No, Details: v2.42.0; Id: 15.52k4o4.1glh5v8ld.1vh; mclb], total: 0(700) X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, URIBL_BLACK autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: X-Footer: bWV0cm90ZWsucnU= Received: from localhost.localdomain ([78.37.162.181]) (authenticated user i.bornyakov@metrotek.ru) by mail.pr-group.ru with ESMTPSA (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)); Fri, 30 Dec 2022 12:31:50 +0300 From: Ivan Bornyakov To: linux-fpga@vger.kernel.org Cc: Ivan Bornyakov , Conor Dooley , Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , linux-kernel@vger.kernel.org, system@metrotek.ru Subject: [PATCH v5 1/3] fpga: microchip-spi: move SPI I/O buffers out of stack Date: Fri, 30 Dec 2022 12:29:20 +0300 Message-Id: <20221230092922.18822-2-i.bornyakov@metrotek.ru> X-Mailer: git-send-email 2.38.2 In-Reply-To: <20221230092922.18822-1-i.bornyakov@metrotek.ru> References: <20221230092922.18822-1-i.bornyakov@metrotek.ru> MIME-Version: 1.0 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753630999734123320?= X-GMAIL-MSGID: =?utf-8?q?1753630999734123320?= As spi-summary doc says: > I/O buffers use the usual Linux rules, and must be DMA-safe. > You'd normally allocate them from the heap or free page pool. > Don't use the stack, or anything that's declared "static". Replace spi_write() with spi_write_then_read(), which is dma-safe for on-stack buffers. Use cacheline aligned buffers for transfers used in spi_sync_transfer(). Although everything works OK with stack-located I/O buffers, better follow the doc to be safe. Fixes: 5f8d4a900830 ("fpga: microchip-spi: add Microchip MPF FPGA manager") Signed-off-by: Ivan Bornyakov Acked-by: Conor Dooley --- drivers/fpga/microchip-spi.c | 93 ++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 46 deletions(-) diff --git a/drivers/fpga/microchip-spi.c b/drivers/fpga/microchip-spi.c index 7436976ea904..bb69f5beefe7 100644 --- a/drivers/fpga/microchip-spi.c +++ b/drivers/fpga/microchip-spi.c @@ -42,46 +42,55 @@ struct mpf_priv { struct spi_device *spi; bool program_mode; + u8 tx __aligned(ARCH_KMALLOC_MINALIGN); + u8 rx; }; -static int mpf_read_status(struct spi_device *spi) +static int mpf_read_status(struct mpf_priv *priv) { - u8 status = 0, status_command = MPF_SPI_READ_STATUS; - struct spi_transfer xfers[2] = { 0 }; - int ret; - /* * HW status is returned on MISO in the first byte after CS went * active. However, first reading can be inadequate, so we submit * two identical SPI transfers and use result of the later one. */ - xfers[0].tx_buf = &status_command; - xfers[1].tx_buf = &status_command; - xfers[0].rx_buf = &status; - xfers[1].rx_buf = &status; - xfers[0].len = 1; - xfers[1].len = 1; - xfers[0].cs_change = 1; + struct spi_transfer xfers[2] = { + { + .tx_buf = &priv->tx, + .rx_buf = &priv->rx, + .len = 1, + .cs_change = 1, + }, { + .tx_buf = &priv->tx, + .rx_buf = &priv->rx, + .len = 1, + }, + }; + u8 status; + int ret; + + priv->tx = MPF_SPI_READ_STATUS; + + ret = spi_sync_transfer(priv->spi, xfers, 2); + if (ret) + return ret; - ret = spi_sync_transfer(spi, xfers, 2); + status = priv->rx; if ((status & MPF_STATUS_SPI_VIOLATION) || (status & MPF_STATUS_SPI_ERROR)) - ret = -EIO; + return -EIO; - return ret ? : status; + return status; } static enum fpga_mgr_states mpf_ops_state(struct fpga_manager *mgr) { struct mpf_priv *priv = mgr->priv; - struct spi_device *spi; bool program_mode; int status; - spi = priv->spi; program_mode = priv->program_mode; - status = mpf_read_status(spi); + status = mpf_read_status(priv); if (!program_mode && !status) return FPGA_MGR_STATE_OPERATING; @@ -186,12 +195,12 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, } /* Poll HW status until busy bit is cleared and mask bits are set. */ -static int mpf_poll_status(struct spi_device *spi, u8 mask) +static int mpf_poll_status(struct mpf_priv *priv, u8 mask) { int status, retries = MPF_STATUS_POLL_RETRIES; while (retries--) { - status = mpf_read_status(spi); + status = mpf_read_status(priv); if (status < 0) return status; @@ -205,32 +214,32 @@ static int mpf_poll_status(struct spi_device *spi, u8 mask) return -EBUSY; } -static int mpf_spi_write(struct spi_device *spi, const void *buf, size_t buf_size) +static int mpf_spi_write(struct mpf_priv *priv, const void *buf, size_t buf_size) { - int status = mpf_poll_status(spi, 0); + int status = mpf_poll_status(priv, 0); if (status < 0) return status; - return spi_write(spi, buf, buf_size); + return spi_write_then_read(priv->spi, buf, buf_size, NULL, 0); } -static int mpf_spi_write_then_read(struct spi_device *spi, +static int mpf_spi_write_then_read(struct mpf_priv *priv, const void *txbuf, size_t txbuf_size, void *rxbuf, size_t rxbuf_size) { const u8 read_command[] = { MPF_SPI_READ_DATA }; int ret; - ret = mpf_spi_write(spi, txbuf, txbuf_size); + ret = mpf_spi_write(priv, txbuf, txbuf_size); if (ret) return ret; - ret = mpf_poll_status(spi, MPF_STATUS_READY); + ret = mpf_poll_status(priv, MPF_STATUS_READY); if (ret < 0) return ret; - return spi_write_then_read(spi, read_command, sizeof(read_command), + return spi_write_then_read(priv->spi, read_command, sizeof(read_command), rxbuf, rxbuf_size); } @@ -242,7 +251,6 @@ static int mpf_ops_write_init(struct fpga_manager *mgr, const u8 isc_en_command[] = { MPF_SPI_ISC_ENABLE }; struct mpf_priv *priv = mgr->priv; struct device *dev = &mgr->dev; - struct spi_device *spi; u32 isc_ret = 0; int ret; @@ -251,9 +259,7 @@ static int mpf_ops_write_init(struct fpga_manager *mgr, return -EOPNOTSUPP; } - spi = priv->spi; - - ret = mpf_spi_write_then_read(spi, isc_en_command, sizeof(isc_en_command), + ret = mpf_spi_write_then_read(priv, isc_en_command, sizeof(isc_en_command), &isc_ret, sizeof(isc_ret)); if (ret || isc_ret) { dev_err(dev, "Failed to enable ISC: spi_ret %d, isc_ret %u\n", @@ -261,7 +267,7 @@ static int mpf_ops_write_init(struct fpga_manager *mgr, return -EFAULT; } - ret = mpf_spi_write(spi, program_mode, sizeof(program_mode)); + ret = mpf_spi_write(priv, program_mode, sizeof(program_mode)); if (ret) { dev_err(dev, "Failed to enter program mode: %d\n", ret); return ret; @@ -274,11 +280,9 @@ static int mpf_ops_write_init(struct fpga_manager *mgr, static int mpf_ops_write(struct fpga_manager *mgr, const char *buf, size_t count) { - u8 spi_frame_command[] = { MPF_SPI_FRAME }; struct spi_transfer xfers[2] = { 0 }; struct mpf_priv *priv = mgr->priv; struct device *dev = &mgr->dev; - struct spi_device *spi; int ret, i; if (count % MPF_SPI_FRAME_SIZE) { @@ -287,18 +291,18 @@ static int mpf_ops_write(struct fpga_manager *mgr, const char *buf, size_t count return -EINVAL; } - spi = priv->spi; - - xfers[0].tx_buf = spi_frame_command; - xfers[0].len = sizeof(spi_frame_command); + xfers[0].tx_buf = &priv->tx; + xfers[0].len = 1; for (i = 0; i < count / MPF_SPI_FRAME_SIZE; i++) { xfers[1].tx_buf = buf + i * MPF_SPI_FRAME_SIZE; xfers[1].len = MPF_SPI_FRAME_SIZE; - ret = mpf_poll_status(spi, 0); - if (ret >= 0) - ret = spi_sync_transfer(spi, xfers, ARRAY_SIZE(xfers)); + ret = mpf_poll_status(priv, 0); + if (ret >= 0) { + priv->tx = MPF_SPI_FRAME; + ret = spi_sync_transfer(priv->spi, xfers, ARRAY_SIZE(xfers)); + } if (ret) { dev_err(dev, "Failed to write bitstream frame %d/%zu\n", @@ -317,12 +321,9 @@ static int mpf_ops_write_complete(struct fpga_manager *mgr, const u8 release_command[] = { MPF_SPI_RELEASE }; struct mpf_priv *priv = mgr->priv; struct device *dev = &mgr->dev; - struct spi_device *spi; int ret; - spi = priv->spi; - - ret = mpf_spi_write(spi, isc_dis_command, sizeof(isc_dis_command)); + ret = mpf_spi_write(priv, isc_dis_command, sizeof(isc_dis_command)); if (ret) { dev_err(dev, "Failed to disable ISC: %d\n", ret); return ret; @@ -330,7 +331,7 @@ static int mpf_ops_write_complete(struct fpga_manager *mgr, usleep_range(1000, 2000); - ret = mpf_spi_write(spi, release_command, sizeof(release_command)); + ret = mpf_spi_write(priv, release_command, sizeof(release_command)); if (ret) { dev_err(dev, "Failed to exit program mode: %d\n", ret); return ret; From patchwork Fri Dec 30 09:29:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ivan Bornyakov X-Patchwork-Id: 37619 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2808428wrt; Fri, 30 Dec 2022 01:34:27 -0800 (PST) X-Google-Smtp-Source: AMrXdXs6DPlKlSOrtoYhjRfBZT+Lmn78DdFj4F/F6yj9OVeFKwFzz8/Gy07fWEMS5VZuy3IuZjWo X-Received: by 2002:a05:6a20:8e0b:b0:a7:9f6:b7a3 with SMTP id y11-20020a056a208e0b00b000a709f6b7a3mr47834529pzj.12.1672392867478; Fri, 30 Dec 2022 01:34:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672392867; cv=none; d=google.com; s=arc-20160816; b=fmJZFeJ9iu2FO87ydQPISKZSICE69JjUY1xX1A1aHejJO+2klFgvzimMiutcge9R6E jzS0Rb5JWiepWJ+hneO5XMF1kv9oXN4xskRLKRakmHhLnZJRzC+PN1BcZK+NClxC9LLl 7I3ZMsPTcb2AbCNwush5VoUvRmzw1VdRQ/CENo0aaOEZvF8+IyXKuN3FNhokhf7mIYSZ VxNSSNVd1p24IAoHc7Ijylt+6LJetJtAbjqsXx/XOPN+g7b6wpyk5Ibs/zfTsyeR4Zh6 g78WqdKSoPGjEqSdBwZjwuvg+Bv7DSX+zp4EySO8Uoqs05aUZAsijHjHtl/VnYTkovOd y94g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7uKUSzeGOOg/IVaeqH2aBfnVISWafFg9j5OHukaeNyU=; b=oWWcSuRPrzCW4ghh0rq5brwEwLQQJPxhawe6Msqf24K/gWaD0xrbgB72/CBSOBogA1 s7VmS9DDVGx6rSRydMNp22PaAkiHWlE5BTWDs1GUH3WnofCkEM7qOwpp40uk4hB9CK8f sbPvKp9FDJAAXqcotDuqXwLXuC/B9UPSfY3Fu7UbkrgFOudqf8xGx4ZpS2HJ85H0fWRo Ed57OYq7JUoad7fSAsc8ycT8gh6Oah6dqyGOsXRuKpM7cQas6l7SUHptBLpQuMQSJmfh VEzC42ztv4lFzcZka7Z/N19jEa3xxB+Bb9aRlv+f/hh54HRL3i9Zzc3HtOjAJoLn0xcB CvLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@metrotek.ru header.s=mail header.b=YOTXmBc3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n7-20020a654cc7000000b004599da2b2b6si21661301pgt.716.2022.12.30.01.34.14; Fri, 30 Dec 2022 01:34:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@metrotek.ru header.s=mail header.b=YOTXmBc3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234859AbiL3JcY (ORCPT + 99 others); Fri, 30 Dec 2022 04:32:24 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34528 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234825AbiL3JcR (ORCPT ); Fri, 30 Dec 2022 04:32:17 -0500 Received: from mail.pr-group.ru (mail.pr-group.ru [178.18.215.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24AF71A390; Fri, 30 Dec 2022 01:32:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=metrotek.ru; s=mail; h=from:subject:date:message-id:to:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references; bh=PqfPOgB/H+V7BMAkAgppCtmusy/q2sCCkwNMJ1UA4RQ=; b=YOTXmBc3E2HaJKrrYoif2bESs6loympIFVF3Vb1uI/jZfpd1K5s6QijzTJkEjyGMt2YB9zvAYL4QO ALM9ynLqvSxd70oxDkmjhH6uiB3duLwWnvbISQH7Jcu2CdOfabVCRVotF/3bi43I3+1JY/4i1B6CTx VRrFKOuB7ViYd0NIvqtmc52/gML5GyT/smatsiJnfPxQ0tpuxFfhWEWuZpfNr4crzeQm+BnfHhQ+4M pBFiDg5GSQiZX8iK8lZ6TaDUxd3Zz8xZEVR5qfD7MtP3hIDxZ+Dj5R/fpLdZUPzq23Ex62fYMN4J4n zuEofRxTyBM+WwI5NHAb6r7HeU1ST8Q== X-Kerio-Anti-Spam: Build: [Engines: 2.16.5.1460, Stamp: 3], Multi: [Enabled, t: (0.000008,0.013650)], BW: [Enabled, t: (0.000025,0.000002)], RTDA: [Enabled, t: (0.080331), Hit: No, Details: v2.42.0; Id: 15.52k1i6.1glh5vaj9.15a61; mclb], total: 0(700) X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, URIBL_BLACK autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: X-Footer: bWV0cm90ZWsucnU= Received: from localhost.localdomain ([78.37.162.181]) (authenticated user i.bornyakov@metrotek.ru) by mail.pr-group.ru with ESMTPSA (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)); Fri, 30 Dec 2022 12:31:51 +0300 From: Ivan Bornyakov To: linux-fpga@vger.kernel.org Cc: Ivan Bornyakov , Conor Dooley , Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , linux-kernel@vger.kernel.org, system@metrotek.ru Subject: [PATCH v5 2/3] fpga: microchip-spi: rewrite status polling in a time measurable way Date: Fri, 30 Dec 2022 12:29:21 +0300 Message-Id: <20221230092922.18822-3-i.bornyakov@metrotek.ru> X-Mailer: git-send-email 2.38.2 In-Reply-To: <20221230092922.18822-1-i.bornyakov@metrotek.ru> References: <20221230092922.18822-1-i.bornyakov@metrotek.ru> MIME-Version: 1.0 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753631023500548716?= X-GMAIL-MSGID: =?utf-8?q?1753631023500548716?= Original busy loop with retries count in mpf_poll_status() is not too reliable, as it takes different times on different systems. Replace it with read_poll_timeout() macro. While at it, fix polling stop condition to met function's original intention declared in the comment. The issue with original polling stop condition is that it stops if any of mask bits is set, while intention was to stop if all mask bits is set. This was not noticible because only MPF_STATUS_READY is passed as mask argument and it is BIT(1). Fixes: 5f8d4a900830 ("fpga: microchip-spi: add Microchip MPF FPGA manager") Signed-off-by: Ivan Bornyakov Reviewed-by: Ilpo Järvinen Acked-by: Conor Dooley Acked-by: Xu Yilun --- drivers/fpga/microchip-spi.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/drivers/fpga/microchip-spi.c b/drivers/fpga/microchip-spi.c index bb69f5beefe7..137fafdf57a6 100644 --- a/drivers/fpga/microchip-spi.c +++ b/drivers/fpga/microchip-spi.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -33,7 +34,7 @@ #define MPF_BITS_PER_COMPONENT_SIZE 22 -#define MPF_STATUS_POLL_RETRIES 10000 +#define MPF_STATUS_POLL_TIMEOUT (2 * USEC_PER_SEC) #define MPF_STATUS_BUSY BIT(0) #define MPF_STATUS_READY BIT(1) #define MPF_STATUS_SPI_VIOLATION BIT(2) @@ -194,24 +195,25 @@ static int mpf_ops_parse_header(struct fpga_manager *mgr, return 0; } -/* Poll HW status until busy bit is cleared and mask bits are set. */ static int mpf_poll_status(struct mpf_priv *priv, u8 mask) { - int status, retries = MPF_STATUS_POLL_RETRIES; + int ret, status; - while (retries--) { - status = mpf_read_status(priv); - if (status < 0) - return status; - - if (status & MPF_STATUS_BUSY) - continue; - - if (!mask || (status & mask)) - return status; - } + /* + * Busy poll HW status. Polling stops if any of the following + * conditions are met: + * - timeout is reached + * - mpf_read_status() returns an error + * - busy bit is cleared AND mask bits are set + */ + ret = read_poll_timeout(mpf_read_status, status, + (status < 0) || + ((status & (MPF_STATUS_BUSY | mask)) == mask), + 0, MPF_STATUS_POLL_TIMEOUT, false, priv); + if (ret < 0) + return ret; - return -EBUSY; + return status; } static int mpf_spi_write(struct mpf_priv *priv, const void *buf, size_t buf_size) From patchwork Fri Dec 30 09:29:22 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ivan Bornyakov X-Patchwork-Id: 37618 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp2808392wrt; Fri, 30 Dec 2022 01:34:21 -0800 (PST) X-Google-Smtp-Source: AMrXdXtePf2Z9n9UKD3lYeqzw8jJoKyXKzKP0LHPvbnqcFqz+I3Wx3w8yHhk4Uz+gICqqS/PbvTF X-Received: by 2002:aa7:9154:0:b0:57b:30b6:9e85 with SMTP id 20-20020aa79154000000b0057b30b69e85mr31055026pfi.22.1672392861194; Fri, 30 Dec 2022 01:34:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672392861; cv=none; d=google.com; s=arc-20160816; b=gy6dT9wMG0M96BvFohVyYGlbVyuGLFQxLiWkcXAbtrXmO1gCM5ozVQr+7SxULCGVVv TXzXP3ct09WhfoMul1qHNQT2J3voc4CL15c2pO2Ngv4NH8TQzs21fybD1lBonZKdJFsK W26TmkrQBjCejs34zXPstZclhfWAyNYjDtPaSSqn1Jy2u63UwtSq8iL5aQLW2Zj0W+Fx nzwd6d9r4nuoN/ZikZHy8D8qVaSMNPqGYjXV7MCDT+J06XyqB26+F12UzD5FovFEHrNo zNM+0jnaOE0bCXlu+dL/qVNe1xZzcwDekQtbZbXbYdR2CqOvA70meq/JAGX0UX2Ut3PR 4SZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8JMBUGm0iPt8z8ss4vWixfUi2Pukc2i4UJGB3rk7ZPk=; b=IrTb5wo5DFWPKZOwqmH9XTxgae9CEjifTwti0d7sbA3TiN5p7Sq7faCCFlXjZimy9P BpWSWviCZfgr9ZBlyl86OALxwl07oxx4DIl7x57ybeUl4fEU8YyVt2Fpe0OZQbTKns9b lzWtYpBVPx1aqK73WKVSXtFFbT23tFV4wmyYUNNVotitRuayWuBD5TgfD21NSZt5IeXY Bk7tJdGVznuBJK/r1XXBMmUvf76RIWNWlFzdRHe1ElS/T4bIEhixgUtrrLKwmpd0uTrO 2Iq2Yd3FjDKcU1l8CiWlC6R3irC22bVOkgZcOrRzbDN5tf7QdDduYwObZUyCYYYD8bNI QuPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@metrotek.ru header.s=mail header.b="e/uOV8Cp"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v21-20020a056a00149500b00574e1e34522si23700365pfu.332.2022.12.30.01.34.09; Fri, 30 Dec 2022 01:34:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@metrotek.ru header.s=mail header.b="e/uOV8Cp"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234849AbiL3JcU (ORCPT + 99 others); Fri, 30 Dec 2022 04:32:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234805AbiL3JcQ (ORCPT ); Fri, 30 Dec 2022 04:32:16 -0500 Received: from mail.pr-group.ru (mail.pr-group.ru [178.18.215.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A40B1A20F; Fri, 30 Dec 2022 01:32:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=metrotek.ru; s=mail; h=from:subject:date:message-id:to:cc:mime-version:content-transfer-encoding: in-reply-to:references; bh=/ihIkO5snmzqBy+4+LJopWc7knE0i7yPw81w1MiqSmQ=; b=e/uOV8CpIKBh6A8A0UK17zK1onjA8l07TCIrdxGMNAmYX/YsXNUvFAFKZliDyUKTvjH3U6ga2ZNA7 Mq4Zr2uMh4xB7fSEE+HcOuhJLmpVbHWETu2ZXmWHn4I6wsbMM6QmGKLqzCuuc6c/SXCPC4jcsGSZLl IT+oeWVUzREv0eCVcOIoKc21d1Zu/9OiY4q1j/DfOOpgc5eTyqH7ydvuDc1OpXLhUrUgBNpLAfKAwx U1bAtvyRCU1k9uhhSQRupAqPwlPtVY3/bz1/M5e8PaScSSy/h3tOaA62JLbfypCQGYWWKp968LMOvb yYzLxL9xfZ9AnRMzssj+F0E46Vy+79A== X-Kerio-Anti-Spam: Build: [Engines: 2.16.5.1460, Stamp: 3], Multi: [Enabled, t: (0.000010,0.010605)], BW: [Enabled, t: (0.000030,0.000002)], RTDA: [Enabled, t: (0.085502), Hit: No, Details: v2.42.0; Id: 15.52k84j.1glh5vaj6.26g; mclb], total: 0(700) X-Spam-Status: No, score=-0.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS, URIBL_BLACK autolearn=no autolearn_force=no version=3.4.6 X-Spam-Level: X-Footer: bWV0cm90ZWsucnU= Received: from localhost.localdomain ([78.37.162.181]) (authenticated user i.bornyakov@metrotek.ru) by mail.pr-group.ru with ESMTPSA (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256 bits)); Fri, 30 Dec 2022 12:31:52 +0300 From: Ivan Bornyakov To: linux-fpga@vger.kernel.org Cc: Ivan Bornyakov , Conor Dooley , Moritz Fischer , Wu Hao , Xu Yilun , Tom Rix , =?utf-8?q?Ilpo_J=C3=A4rvinen?= , linux-kernel@vger.kernel.org, system@metrotek.ru Subject: [PATCH v5 3/3] fpga: microchip-spi: separate data frame write routine Date: Fri, 30 Dec 2022 12:29:22 +0300 Message-Id: <20221230092922.18822-4-i.bornyakov@metrotek.ru> X-Mailer: git-send-email 2.38.2 In-Reply-To: <20221230092922.18822-1-i.bornyakov@metrotek.ru> References: <20221230092922.18822-1-i.bornyakov@metrotek.ru> MIME-Version: 1.0 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753631016593638118?= X-GMAIL-MSGID: =?utf-8?q?1753631016593638118?= mpf_ops_write() function writes bitstream data to the FPGA by a smaller frames. Introduce mpf_spi_frame_write() function which is for writing a single data frame and use it in mpf_ops_write(). No functional changes intended. Signed-off-by: Ivan Bornyakov Acked-by: Conor Dooley Acked-by: Xu Yilun --- drivers/fpga/microchip-spi.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/drivers/fpga/microchip-spi.c b/drivers/fpga/microchip-spi.c index 137fafdf57a6..d6070e7f5205 100644 --- a/drivers/fpga/microchip-spi.c +++ b/drivers/fpga/microchip-spi.c @@ -280,9 +280,30 @@ static int mpf_ops_write_init(struct fpga_manager *mgr, return 0; } +static int mpf_spi_frame_write(struct mpf_priv *priv, const char *buf) +{ + struct spi_transfer xfers[2] = { + { + .tx_buf = &priv->tx, + .len = 1, + }, { + .tx_buf = buf, + .len = MPF_SPI_FRAME_SIZE, + }, + }; + int ret; + + ret = mpf_poll_status(priv, 0); + if (ret < 0) + return ret; + + priv->tx = MPF_SPI_FRAME; + + return spi_sync_transfer(priv->spi, xfers, ARRAY_SIZE(xfers)); +} + static int mpf_ops_write(struct fpga_manager *mgr, const char *buf, size_t count) { - struct spi_transfer xfers[2] = { 0 }; struct mpf_priv *priv = mgr->priv; struct device *dev = &mgr->dev; int ret, i; @@ -293,19 +314,8 @@ static int mpf_ops_write(struct fpga_manager *mgr, const char *buf, size_t count return -EINVAL; } - xfers[0].tx_buf = &priv->tx; - xfers[0].len = 1; - for (i = 0; i < count / MPF_SPI_FRAME_SIZE; i++) { - xfers[1].tx_buf = buf + i * MPF_SPI_FRAME_SIZE; - xfers[1].len = MPF_SPI_FRAME_SIZE; - - ret = mpf_poll_status(priv, 0); - if (ret >= 0) { - priv->tx = MPF_SPI_FRAME; - ret = spi_sync_transfer(priv->spi, xfers, ARRAY_SIZE(xfers)); - } - + ret = mpf_spi_frame_write(priv, buf + i * MPF_SPI_FRAME_SIZE); if (ret) { dev_err(dev, "Failed to write bitstream frame %d/%zu\n", i, count / MPF_SPI_FRAME_SIZE);