From patchwork Fri Jun 2 11:57:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rasmus Villemoes X-Patchwork-Id: 13265199 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 8527CC7EE29 for ; Fri, 2 Jun 2023 11:58: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: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:In-Reply-To:References: List-Owner; bh=6ZEPFA8zO+YSNPBpN3o/zJZIps8XuSSl6mmP7GDvhTQ=; b=xWOV3t4Q3jggAI dGWh2F60DlBSNrxeFQcUShSPSbvNeEYS8OvJ1XbPYL5zoCuNJyWOcgJIv29gecF3Sq6mj+Tlb0SGQ ncr8gAarRt+5kfGLIHHUX0PWdJm5SigM+7yIf/YkqB/1f4PA0xA1AxgwFZneEYftaYXQNwnzoFrVu 4trlvI7rswgHdSy/IiAbSvHfKdJYj8wrUVvTUxjcPuLlwwp+9CCXpoVrGQINockXITAaAaUxjGGWg X2vMMFoQW+/uv6jXXlHCvdxMXLUtRyft3EmQNVQgImGeMiTytof8JGnzrl2ab8XLStQ2picnYr2bf gLXcnvEodOrPW6KtR2cA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q53PX-006pEn-2J; Fri, 02 Jun 2023 11:57:39 +0000 Received: from mail-lf1-x131.google.com ([2a00:1450:4864:20::131]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q53PU-006pDl-2u for linux-arm-kernel@lists.infradead.org; Fri, 02 Jun 2023 11:57:38 +0000 Received: by mail-lf1-x131.google.com with SMTP id 2adb3069b0e04-4f3bb395e69so2675329e87.2 for ; Fri, 02 Jun 2023 04:57:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rasmusvillemoes.dk; s=google; t=1685707054; x=1688299054; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=evrwcymqMNKTbAB9nnVcN2soWGAlTnOdSH5Owxy9OYQ=; b=EdZFtjCv0OOg290UqgCv24tqwEyrrNi3aaROlD2uMvEhdZj5TBD7sET7MpjcTA4v+n lBObkojN9igzCLsZp4AfqDTO5FwE+JE77ByU7wKckHKYQg62f2f1fDAjk9Xt7CIQP8+Y HS8cPT2m6rzhoImXthNDxpX3rzRLYZJxe33Vo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685707054; x=1688299054; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=evrwcymqMNKTbAB9nnVcN2soWGAlTnOdSH5Owxy9OYQ=; b=ZPYKAfBoEwb3ZDnZGWawe783Gg9m3KNPeDzUE5C9QwJPB4QfpIL3B0sRFeJN7JjRIG YRecPOY+gg3TQOhmEdQUKco01fxSyqKT3eXPEJLB29T+449al5ebvgX/Dd8wBqusGu5M cm6zj6w5l3mlTLE3MDpBqVuVg27SfJdRgLhvM+gfMNBM9s59FTIX06CC7v8uJdU4FDvk ZJpJERdmvVtQkp9JB+g+NScHrnGPuIyrzx0dlJtrJIGufG+TRssIm/ygEyzTc8KcaOuo FzF/JC5EWdXFMcdIFmTa+ikHTkbNx/ThC9GDsH94iA9qj5ZvRfABm3djP/iownyQuYIo 5NXw== X-Gm-Message-State: AC+VfDyxxKlj2RYpWFzFkRGkGNIgseFXefjvAjzCn9OOnHYtGAyCjbVv GmTw4ytM7y3lh00eKLmHUntIIQ== X-Google-Smtp-Source: ACHHUZ5V3Nt2S6IKerF7pPam5XtnNoVjNy9QC/LXBJc5lXRLxmZTqM8KDhn3IwFNGNvt3Fh8ShpeTw== X-Received: by 2002:ac2:4831:0:b0:4f3:b9b1:5683 with SMTP id 17-20020ac24831000000b004f3b9b15683mr1706934lft.62.1685707054242; Fri, 02 Jun 2023 04:57:34 -0700 (PDT) Received: from prevas-ravi.prevas.se ([81.216.59.226]) by smtp.gmail.com with ESMTPSA id e20-20020ac25474000000b004db0d26adb4sm140316lfn.182.2023.06.02.04.57.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 02 Jun 2023 04:57:33 -0700 (PDT) From: Rasmus Villemoes To: Mark Brown , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team Cc: Boerge Struempfel , "Mahapatra, Amit Kumar" , Rasmus Villemoes , linux-spi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] spi: spi-imx: fix mixing of native and gpio chipselects for imx51/imx53/imx6 variants Date: Fri, 2 Jun 2023 13:57:30 +0200 Message-Id: <20230602115731.708883-1-linux@rasmusvillemoes.dk> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230602_045736_944382_A8D452C2 X-CRM114-Status: GOOD ( 17.61 ) 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 Commit 87c614175bbf (spi: spi-imx: fix MX51_ECSPI_* macros when cs > 3) ensured that the argument passed to the macros was masked with &3, so that we no longer write outside the intended fields in the various control registers. When all chip selects are gpios, this works just fine. However, when a mix of native and gpio chip selects are in use, that masking is too naive. Say, for example, that SS0 is muxed as native chip select, and there is also a chip at 4 (obviously with a gpio cs). In that case, when accessing the latter chip, both the SS0 pin and the gpio pin will be asserted low. The fix for this is to use the ->unused_native_cs value as channel number for any spi device which uses a gpio as chip select. Signed-off-by: Rasmus Villemoes --- Previous submission: https://lore.kernel.org/lkml/20230425134527.483607-1-linux@rasmusvillemoes.dk/ The first two patches have already been applied in -next. This adapts 3/3 to -next as of next-20230602, taking both 6a983ff5102f and 9e264f3f85a5 into account. drivers/spi/spi-imx.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/drivers/spi/spi-imx.c b/drivers/spi/spi-imx.c index 6f4d3cb81fdf..528ae46c087f 100644 --- a/drivers/spi/spi-imx.c +++ b/drivers/spi/spi-imx.c @@ -517,6 +517,13 @@ static void mx51_ecspi_disable(struct spi_imx_data *spi_imx) writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL); } +static int mx51_ecspi_channel(const struct spi_device *spi) +{ + if (!spi_get_csgpiod(spi, 0)) + return spi_get_chipselect(spi, 0); + return spi->controller->unused_native_cs; +} + static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx, struct spi_message *msg) { @@ -527,6 +534,7 @@ static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx, u32 testreg, delay; u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG); u32 current_cfg = cfg; + int channel = mx51_ecspi_channel(spi); /* set Master or Slave mode */ if (spi_imx->slave_mode) @@ -541,7 +549,7 @@ static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx, ctrl |= MX51_ECSPI_CTRL_DRCTL(spi_imx->spi_drctl); /* set chip select to use */ - ctrl |= MX51_ECSPI_CTRL_CS(spi_get_chipselect(spi, 0)); + ctrl |= MX51_ECSPI_CTRL_CS(channel); /* * The ctrl register must be written first, with the EN bit set other @@ -562,27 +570,27 @@ static int mx51_ecspi_prepare_message(struct spi_imx_data *spi_imx, * BURST_LENGTH + 1 bits are received */ if (spi_imx->slave_mode && is_imx53_ecspi(spi_imx)) - cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(spi_get_chipselect(spi, 0)); + cfg &= ~MX51_ECSPI_CONFIG_SBBCTRL(channel); else - cfg |= MX51_ECSPI_CONFIG_SBBCTRL(spi_get_chipselect(spi, 0)); + cfg |= MX51_ECSPI_CONFIG_SBBCTRL(channel); if (spi->mode & SPI_CPOL) { - cfg |= MX51_ECSPI_CONFIG_SCLKPOL(spi_get_chipselect(spi, 0)); - cfg |= MX51_ECSPI_CONFIG_SCLKCTL(spi_get_chipselect(spi, 0)); + cfg |= MX51_ECSPI_CONFIG_SCLKPOL(channel); + cfg |= MX51_ECSPI_CONFIG_SCLKCTL(channel); } else { - cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(spi_get_chipselect(spi, 0)); - cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(spi_get_chipselect(spi, 0)); + cfg &= ~MX51_ECSPI_CONFIG_SCLKPOL(channel); + cfg &= ~MX51_ECSPI_CONFIG_SCLKCTL(channel); } if (spi->mode & SPI_MOSI_IDLE_LOW) - cfg |= MX51_ECSPI_CONFIG_DATACTL(spi_get_chipselect(spi, 0)); + cfg |= MX51_ECSPI_CONFIG_DATACTL(channel); else - cfg &= ~MX51_ECSPI_CONFIG_DATACTL(spi_get_chipselect(spi, 0)); + cfg &= ~MX51_ECSPI_CONFIG_DATACTL(channel); if (spi->mode & SPI_CS_HIGH) - cfg |= MX51_ECSPI_CONFIG_SSBPOL(spi_get_chipselect(spi, 0)); + cfg |= MX51_ECSPI_CONFIG_SSBPOL(channel); else - cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(spi_get_chipselect(spi, 0)); + cfg &= ~MX51_ECSPI_CONFIG_SSBPOL(channel); if (cfg == current_cfg) return 0; @@ -627,14 +635,15 @@ static void mx51_configure_cpha(struct spi_imx_data *spi_imx, bool cpha = (spi->mode & SPI_CPHA); bool flip_cpha = (spi->mode & SPI_RX_CPHA_FLIP) && spi_imx->rx_only; u32 cfg = readl(spi_imx->base + MX51_ECSPI_CONFIG); + int channel = mx51_ecspi_channel(spi); /* Flip cpha logical value iff flip_cpha */ cpha ^= flip_cpha; if (cpha) - cfg |= MX51_ECSPI_CONFIG_SCLKPHA(spi_get_chipselect(spi, 0)); + cfg |= MX51_ECSPI_CONFIG_SCLKPHA(channel); else - cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(spi_get_chipselect(spi, 0)); + cfg &= ~MX51_ECSPI_CONFIG_SCLKPHA(channel); writel(cfg, spi_imx->base + MX51_ECSPI_CONFIG); }