From nobody Sun Nov 24 02:45:09 2024 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 46F9A212D0C for ; Thu, 7 Nov 2024 15:35:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730993737; cv=none; b=sep+MJiCVsG/WkHZiDWmVluX4FyuEgiCEgw8fkZcW6SIU1uXtJAKh+Q+FISf8qVc/wfn7truIb69cT9VxkdXFSqHHpMNkzEeasmikOVulm1oEuO8PSfIVAIX57AEzbP8qeCrKIqq6aHtAmF/Ab1SZ2u6E3HTJqAGDZAtafxVNz8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730993737; c=relaxed/simple; bh=CnC2sd8Oao1J21P/qBvL/HWz3WS+2cdCs0teDxRpgrQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cI04242knuYz5/959NLueMc639Vp0CKeS2+oUoGP/9+6gOdTULN8arwYZDcTKO/Wo2T8GICvDVbQyyu37452UCT1iPRqS1BDq3CJeMc3UQZWJJULWbTm40TpgzvdUH3VTBINqkMihncyPYZLXwddpRfe8xYwTZ3Dxm1A7WPStv8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by metis.whiteo.stw.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1t94Xf-0005DD-RL; Thu, 07 Nov 2024 16:35:27 +0100 From: Jonas Rebmann Date: Thu, 07 Nov 2024 16:35:20 +0100 Subject: [PATCH 1/2] spi: imx: pass struct spi_transfer to prepare_transfer() Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241107-imx-spi-word-delay-v1-1-2a969214d796@pengutronix.de> References: <20241107-imx-spi-word-delay-v1-0-2a969214d796@pengutronix.de> In-Reply-To: <20241107-imx-spi-word-delay-v1-0-2a969214d796@pengutronix.de> To: Mark Brown , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-spi@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.14.2 X-SA-Exim-Connect-IP: 2a0a:edc0:0:1101:1d::ac X-SA-Exim-Mail-From: jre@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org In an upcoming patch, mx51_ecspi_prepare_transfer() needs access to the word_delay parameter. To enable controller-specific handling of such per-transfer parameters, extend the prepare_transfer() function of the spi_imx_devtype_data interface to take a struct spi_transfer argument, update all controller-specific implementations accordingly. Signed-off-by: Jonas Rebmann --- drivers/spi/spi-imx.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/spi/spi-imx.c b/drivers/spi/spi-imx.c index 85bd1a82a34eb4bc76a4b4528e087fc2ebfa8b85..65a8303b80b1191cd2c19d61f88= 836e7fd3c7ae9 100644 --- a/drivers/spi/spi-imx.c +++ b/drivers/spi/spi-imx.c @@ -71,7 +71,8 @@ struct spi_imx_data; struct spi_imx_devtype_data { void (*intctrl)(struct spi_imx_data *spi_imx, int enable); int (*prepare_message)(struct spi_imx_data *spi_imx, struct spi_message *= msg); - int (*prepare_transfer)(struct spi_imx_data *spi_imx, struct spi_device *= spi); + int (*prepare_transfer)(struct spi_imx_data *spi_imx, struct spi_device *= spi, + struct spi_transfer *t); void (*trigger)(struct spi_imx_data *spi_imx); int (*rx_available)(struct spi_imx_data *spi_imx); void (*reset)(struct spi_imx_data *spi_imx); @@ -649,7 +650,7 @@ static void mx51_configure_cpha(struct spi_imx_data *sp= i_imx, } =20 static int mx51_ecspi_prepare_transfer(struct spi_imx_data *spi_imx, - struct spi_device *spi) + struct spi_device *spi, struct spi_transfer *t) { u32 ctrl =3D readl(spi_imx->base + MX51_ECSPI_CTRL); u32 clk; @@ -774,7 +775,7 @@ static int mx31_prepare_message(struct spi_imx_data *sp= i_imx, } =20 static int mx31_prepare_transfer(struct spi_imx_data *spi_imx, - struct spi_device *spi) + struct spi_device *spi, struct spi_transfer *t) { unsigned int reg =3D MX31_CSPICTRL_ENABLE | MX31_CSPICTRL_HOST; unsigned int clk; @@ -878,7 +879,7 @@ static int mx21_prepare_message(struct spi_imx_data *sp= i_imx, } =20 static int mx21_prepare_transfer(struct spi_imx_data *spi_imx, - struct spi_device *spi) + struct spi_device *spi, struct spi_transfer *t) { unsigned int reg =3D MX21_CSPICTRL_ENABLE | MX21_CSPICTRL_HOST; unsigned int max =3D is_imx27_cspi(spi_imx) ? 16 : 18; @@ -953,7 +954,7 @@ static int mx1_prepare_message(struct spi_imx_data *spi= _imx, } =20 static int mx1_prepare_transfer(struct spi_imx_data *spi_imx, - struct spi_device *spi) + struct spi_device *spi, struct spi_transfer *t) { unsigned int reg =3D MX1_CSPICTRL_ENABLE | MX1_CSPICTRL_HOST; unsigned int clk; @@ -1304,7 +1305,7 @@ static int spi_imx_setupxfer(struct spi_device *spi, spi_imx->target_burst =3D t->len; } =20 - spi_imx->devtype_data->prepare_transfer(spi_imx, spi); + spi_imx->devtype_data->prepare_transfer(spi_imx, spi, t); =20 return 0; } --=20 2.39.5 From nobody Sun Nov 24 02:45:09 2024 Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CCCD52139B0 for ; Thu, 7 Nov 2024 15:35:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730993739; cv=none; b=jxpXAvXRFodtvbM5PzPwX8qm3LCWBI48IsvB0MFKoKOqbzBGBG5G3qlyEDc+dv7R/Ruu7ZmhBZ45yNRWrVpQSUZiAfbBlMK9OqrW3VFAPZSqf2rlEwrNzEfkHgnfOYe4+fM9O0Q+JQp43vqv8qSvCRaPqNYMELYLK1cKcsGYyS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730993739; c=relaxed/simple; bh=UwJuonefhCSNk8TslM5o7v8lxMqBjlG5gdXrIvC7l2o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nRcvTp26OA1PWY9VCTF6P8IZwIOY5NOGB0HVNL/gNWCap0mQpTEqfsZ2PPVkfKZIF0y41b6HsHmZnipdgUbRssRNbqAxqP9H8S8JqDbqy45zv9ht3+8ysGz9gcLcKKXFXuSslQGuMW0ksS+9e39uPrId0dL/AURIrEYwQAAlsRo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from dude04.red.stw.pengutronix.de ([2a0a:edc0:0:1101:1d::ac]) by metis.whiteo.stw.pengutronix.de with esmtp (Exim 4.92) (envelope-from ) id 1t94Xf-0005DD-T8; Thu, 07 Nov 2024 16:35:27 +0100 From: Jonas Rebmann Date: Thu, 07 Nov 2024 16:35:21 +0100 Subject: [PATCH 2/2] spi: imx: support word delay Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20241107-imx-spi-word-delay-v1-2-2a969214d796@pengutronix.de> References: <20241107-imx-spi-word-delay-v1-0-2a969214d796@pengutronix.de> In-Reply-To: <20241107-imx-spi-word-delay-v1-0-2a969214d796@pengutronix.de> To: Mark Brown , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: linux-spi@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, kernel@pengutronix.de, Jonas Rebmann X-Mailer: b4 0.14.2 X-SA-Exim-Connect-IP: 2a0a:edc0:0:1101:1d::ac X-SA-Exim-Mail-From: jre@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org Implement support for the word delay feature of i.MX51 (and onwards) via the ECSPI interface. Convert the requested delay to SPI cycles and account for an extra inter-word delay inserted by the controller in addition to the requested number of cycles, which was observed when testing this patch. Disable dynamic burst when word delay is set. As the configurable delay period in the controller is inserted after bursts, the burst length must equal the word length. Account for word delay in the transfer time estimation for polling_limit_us. Signed-off-by: Jonas Rebmann --- drivers/spi/spi-imx.c | 93 +++++++++++++++++++++++++++++++++++++++++++++--= ---- 1 file changed, 83 insertions(+), 10 deletions(-) diff --git a/drivers/spi/spi-imx.c b/drivers/spi/spi-imx.c index 65a8303b80b1191cd2c19d61f88836e7fd3c7ae9..c50c256e6e695659c23d5292949= a98bda0a2cd1a 100644 --- a/drivers/spi/spi-imx.c +++ b/drivers/spi/spi-imx.c @@ -13,7 +13,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -302,6 +304,18 @@ static bool spi_imx_can_dma(struct spi_controller *con= troller, struct spi_device #define MX51_ECSPI_STAT 0x18 #define MX51_ECSPI_STAT_RR (1 << 3) =20 +#define MX51_ECSPI_PERIOD 0x1c +#define MX51_ECSPI_PERIOD_MASK 0x7fff +/* + * As measured on the i.MX6, the SPI host controller inserts a 4 SPI-Clock + * (SCLK) delay after each burst if the PERIOD reg is 0x0. This value will= be + * called MX51_ECSPI_PERIOD_MIN_DELAY_SCK. + * + * If the PERIOD register is !=3D 0, the controller inserts a delay of + * MX51_ECSPI_PERIOD_MIN_DELAY_SCK + register value + 1 SCLK after each bu= rst. + */ +#define MX51_ECSPI_PERIOD_MIN_DELAY_SCK 4 + #define MX51_ECSPI_TESTREG 0x20 #define MX51_ECSPI_TESTREG_LBC BIT(31) =20 @@ -653,6 +667,7 @@ static int mx51_ecspi_prepare_transfer(struct spi_imx_d= ata *spi_imx, struct spi_device *spi, struct spi_transfer *t) { u32 ctrl =3D readl(spi_imx->base + MX51_ECSPI_CTRL); + u64 word_delay_sck; u32 clk; =20 /* Clear BL field and set the right value */ @@ -684,6 +699,49 @@ static int mx51_ecspi_prepare_transfer(struct spi_imx_= data *spi_imx, =20 writel(ctrl, spi_imx->base + MX51_ECSPI_CTRL); =20 + /* calculate word delay in SPI Clock (SCLK) cycles */ + if (t->word_delay.value =3D=3D 0) { + word_delay_sck =3D 0; + } else if (t->word_delay.unit =3D=3D SPI_DELAY_UNIT_SCK) { + word_delay_sck =3D t->word_delay.value; + + if (word_delay_sck <=3D MX51_ECSPI_PERIOD_MIN_DELAY_SCK) + word_delay_sck =3D 0; + else if (word_delay_sck <=3D MX51_ECSPI_PERIOD_MIN_DELAY_SCK + 1) + word_delay_sck =3D 1; + else + word_delay_sck -=3D MX51_ECSPI_PERIOD_MIN_DELAY_SCK + 1; + } else { + int word_delay_ns; + + word_delay_ns =3D spi_delay_to_ns(&t->word_delay, t); + if (word_delay_ns < 0) + return word_delay_ns; + + if (word_delay_ns <=3D mul_u64_u32_div(NSEC_PER_SEC, + MX51_ECSPI_PERIOD_MIN_DELAY_SCK, + spi_imx->spi_bus_clk)) { + word_delay_sck =3D 0; + } else if (word_delay_ns <=3D mul_u64_u32_div(NSEC_PER_SEC, + MX51_ECSPI_PERIOD_MIN_DELAY_SCK + 1, + spi_imx->spi_bus_clk)) { + word_delay_sck =3D 1; + } else { + word_delay_ns -=3D mul_u64_u32_div(NSEC_PER_SEC, + MX51_ECSPI_PERIOD_MIN_DELAY_SCK + 1, + spi_imx->spi_bus_clk); + + word_delay_sck =3D DIV_U64_ROUND_UP((u64)word_delay_ns * spi_imx->spi_b= us_clk, + NSEC_PER_SEC); + } + } + + if (!FIELD_FIT(MX51_ECSPI_PERIOD_MASK, word_delay_sck)) + return -EINVAL; + + writel(FIELD_PREP(MX51_ECSPI_PERIOD_MASK, word_delay_sck), + spi_imx->base + MX51_ECSPI_PERIOD); + return 0; } =20 @@ -1264,11 +1322,13 @@ static int spi_imx_setupxfer(struct spi_device *spi, =20 /* * Initialize the functions for transfer. To transfer non byte-aligned - * words, we have to use multiple word-size bursts, we can't use - * dynamic_burst in that case. + * words, we have to use multiple word-size bursts. To insert word + * delay, the burst size has to equal the word size. We can't use + * dynamic_burst in these cases. */ if (spi_imx->devtype_data->dynamic_burst && !spi_imx->target_mode && !(spi->mode & SPI_CS_WORD) && + !(t->word_delay.value) && (spi_imx->bits_per_word =3D=3D 8 || spi_imx->bits_per_word =3D=3D 16 || spi_imx->bits_per_word =3D=3D 32)) { @@ -1611,12 +1671,30 @@ static int spi_imx_pio_transfer_target(struct spi_d= evice *spi, return ret; } =20 +static unsigned int spi_imx_transfer_estimate_time_us(struct spi_transfer = *transfer) +{ + u64 result; + + result =3D DIV_U64_ROUND_CLOSEST((u64)USEC_PER_SEC * transfer->len * BITS= _PER_BYTE, + transfer->effective_speed_hz); + if (transfer->word_delay.value) { + unsigned int word_delay_us; + unsigned int words; + + words =3D DIV_ROUND_UP(transfer->len * BITS_PER_BYTE, transfer->bits_per= _word); + word_delay_us =3D DIV_ROUND_CLOSEST(spi_delay_to_ns(&transfer->word_dela= y, transfer), + NSEC_PER_USEC); + result +=3D words * word_delay_us; + } + + return min(result, U32_MAX); +} + static int spi_imx_transfer_one(struct spi_controller *controller, struct spi_device *spi, struct spi_transfer *transfer) { struct spi_imx_data *spi_imx =3D spi_controller_get_devdata(spi->controll= er); - unsigned long hz_per_byte, byte_limit; =20 spi_imx_setupxfer(spi, transfer); transfer->effective_speed_hz =3D spi_imx->spi_bus_clk; @@ -1635,15 +1713,10 @@ static int spi_imx_transfer_one(struct spi_controll= er *controller, */ if (spi_imx->usedma) return spi_imx_dma_transfer(spi_imx, transfer); - /* - * Calculate the estimated time in us the transfer runs. Find - * the number of Hz per byte per polling limit. - */ - hz_per_byte =3D polling_limit_us ? ((8 + 4) * USEC_PER_SEC) / polling_lim= it_us : 0; - byte_limit =3D hz_per_byte ? transfer->effective_speed_hz / hz_per_byte := 1; =20 /* run in polling mode for short transfers */ - if (transfer->len < byte_limit) + if (transfer->len =3D=3D 1 || (polling_limit_us && + spi_imx_transfer_estimate_time_us(transfer) < polling_limit_us)) return spi_imx_poll_transfer(spi, transfer); =20 return spi_imx_pio_transfer(spi, transfer); --=20 2.39.5