From mboxrd@z Thu Jan 1 00:00:00 1970 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 34E84CFD376 for ; Tue, 2 Dec 2025 07:55:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding:References:In-Reply-To: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:List-Owner; bh=3dnI4qh/0aH4OV9bjqxyLDoFu9l9WIC4Cg3Yx8x3/FU=; b=t98hSScWgUpU0LpIQSPmo62Avx +6AOR5oqm7oH71EVoKdaD8vx2yL3jLEAZuP4pKVgbmh0AaonxgIUlDJpEUCjW4N4KLHbSlOiDdk3a Zu8twgDwmJIUy1QrwKILGY+QFLlm+9insoCg2hDx3j2ACD8+EuYLXMapeiv8RppMS5T+e9EKMeLzX rOm05Crnmm6TYLcuKTH+uQ5sTmAV3uxTnNmV0jVgmWPKZVIUMUICov0u1RARKjw8UuucLniQigD95 RBLVtCb6SZuo1q/UIKPJUl7m88K+jB8KS4vGSetGeY4GgebzbFOCAv9zREDe7wjj4wKsDHQ6Fa9h+ qFErr/Xw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1vQLEo-000000050Qm-2rNY; Tue, 02 Dec 2025 07:55:54 +0000 Received: from mail-francesouthazon11011039.outbound.protection.outlook.com ([40.107.130.39] helo=MRWPR03CU001.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1vQLEn-000000050PN-0MFG for linux-arm-kernel@lists.infradead.org; Tue, 02 Dec 2025 07:55:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=jW21Up1416/YcCf+YttVqe9QVqUo6Mk3B+V/b0cwwoBzYPIST0ptfFo7Hk1DT7qLzH9zNg/7EpvBrzaAbehoeofc61q1OM8k3WRkLnNLtx7KjPm02mI4mD5V0dPySrsaL2OHqxFdwsRiV1vLQuqV7zmN03A7pEHfw7brBM9o+nnBQAmpw+qM1pALqoE+VdTvn7uoNa4vJyZcDEUQRcnxvwc63trY8Vrshqcyi0N7HI0I9bXT8KNOyOuRhQX9w2a/ychbToEPjdNG8CcEEISkpfuPi0ggivkIlMMN9NI50u+7oG6JbnLioXkmIswhG+eHE1W3Pq8hUZZ3QRa/eaNuMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=3dnI4qh/0aH4OV9bjqxyLDoFu9l9WIC4Cg3Yx8x3/FU=; b=RWo09zJs0Vqqlvy3oCtpLPkGBZX/NIMz+4PiyrW6dGiz48lhqm/hk0N0DXG2VAYJNMrVAEEVHGWFZSgLnMD2fDEN/m8eCKte8mImOBvZDGIKANcF+4hS/j+8CxLvhzQfbuC3OJkEfmBxOmL+tVZwM1QshmT1MaDHHi7t9zRYy984C8CmYMeB35CJO5Sjus0PICpddZvGItluxuuPnwHopf6wWvqoRpgggAECKGgVc1OmXBVZx9XtOh++8GoAs3m5vChAWXif4EJLDbkqHH1yFNjtZh6ByQDPI12Vhyv92T3X60va/o8mr/2XFKhIcU24hXUlYea4g6S43AunsVrf6Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=nxp.com; dmarc=pass action=none header.from=nxp.com; dkim=pass header.d=nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nxp.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3dnI4qh/0aH4OV9bjqxyLDoFu9l9WIC4Cg3Yx8x3/FU=; b=XMD8DHAKQ7fgBMm2xJSHYkLVdNLETilPEHwIBGh8NUBQaPiGSgR2kJQvIzxuBve61tNgeoDxwsPQsRJTjIApSVewgbc8n43TfjHb9uiq6EEj1rD7K7nygzYwsgdo17CDSJZEJFFwCByNccO0Y2jWZEncWplTGgZoRH0fH/x1ObsDmqJSHIA7q2MWsXUwUrVixckeqQmt6hKoqC3NxcPRsyPVbM5plAqB7VNzzLciIEMoHUG6XA0KncqdgQMb4V67Xo6s8ybj5B/79RB07RICP6C2TcWKWkJaJ2BRlmL8ig7FXfeDz81P0eetlXgv7NlznWMqFG9xLg5x0cYWytmmnA== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=nxp.com; Received: from VI2PR04MB11147.eurprd04.prod.outlook.com (2603:10a6:800:293::14) by VI2PR04MB11171.eurprd04.prod.outlook.com (2603:10a6:800:29a::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9366.17; Tue, 2 Dec 2025 07:55:49 +0000 Received: from VI2PR04MB11147.eurprd04.prod.outlook.com ([fe80::75ad:fac7:cfe7:b687]) by VI2PR04MB11147.eurprd04.prod.outlook.com ([fe80::75ad:fac7:cfe7:b687%6]) with mapi id 15.20.9366.012; Tue, 2 Dec 2025 07:55:49 +0000 From: Carlos Song To: frank.li@nxp.com, mkl@pengutronix.de, broonie@kernel.org, shawnguo@kernel.org, s.hauer@pengutronix.de, kernel@pengutronix.de, festevam@gmail.com, kees@kernel.org, gustavoars@kernel.org Cc: linux-spi@vger.kernel.org, imx@lists.linux.dev, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-hardening@vger.kernel.org, Carlos Song , Frank Li Subject: [PATCH v2 2/6] spi: imx: introduce helper to clear DMA mode logic Date: Tue, 2 Dec 2025 15:54:59 +0800 Message-Id: <20251202075503.2448339-3-carlos.song@nxp.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20251202075503.2448339-1-carlos.song@nxp.com> References: <20251202075503.2448339-1-carlos.song@nxp.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: AS4P189CA0052.EURP189.PROD.OUTLOOK.COM (2603:10a6:20b:659::21) To VI2PR04MB11147.eurprd04.prod.outlook.com (2603:10a6:800:293::14) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: VI2PR04MB11147:EE_|VI2PR04MB11171:EE_ X-MS-Office365-Filtering-Correlation-Id: bde3e993-181c-4eab-633c-08de31783507 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|19092799006|7416014|376014|52116014|366016|38350700014; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?ZAh/uUeg9UI9whtu9KnasCgEnySjXE5cBxMUmV6QIhj0Q0TYyAWNEak/qztn?= =?us-ascii?Q?5iuf9GFtxyVRoC5scns/G9SGYOL0tQXpqEoVu9dJz0zfI95wwFh7XpbCrU0y?= =?us-ascii?Q?xNUZaYMryK7Sm3lhAkQiSwpvYoPWGlYUx07tStl97a2e9FR9apq3uRSHZNCp?= =?us-ascii?Q?mmaycMwuODDFrVPTQwUiaHsdpo5MQGI5qxmnWwzTZnsY5aKjmbyVVWV/5xYx?= =?us-ascii?Q?B592YQvSDtSt01GBz5UPWBberCq5Bj28z9CLv82oLoztC7CxxCqJgjYHUSud?= =?us-ascii?Q?OtWH7ofK6BwAdGZejK8tZ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ii?Q?rhLyNMsJBGAUUOWdfwiIrEiktkKdCuICtFTIPp4dUBBHMVk4rmtK57YORlSU?= =?us-ascii?Q?lRox5WpIEHLEttmJl5+bXrA8aXYEKn1WOOBF6RYTWXQHLodYcJu8SySKdp7j?= =?us-ascii?Q?vJvUzncFE7DG7mxmESoJlkSm0xZMNBFkOfVwd3hsFHiyO/9hEyFA4v41BcBD?= =?us-ascii?Q?VQ+R3y4CjzOniHz7jbaShji2RuKNJdLgl0nk9pp/TwDuwo+dntPay9T9qEyD?= =?us-ascii?Q?UWCjEbkLZPCjPa89cHa0ha9Xj2lGi+kxgd2wQxCuIOgZSCFBEaMYguADJFuj?= =?us-ascii?Q?G4sJo8vrubsveZxb/CQQgTWwMRgJw1jaGmM1?= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:VI2PR04MB11147.eurprd04.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(19092799006)(7416014)(376014)(52116014)(366016)(38350700014);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?ASV1a/rVpl3TFramRDoydviAQwtoOOEBpevClGUVcJGHFaX00+q8NH0AoIfY?= =?us-ascii?Q?ASwuKJ4uTenaPtSr7mUoicQIMSdOdniR0TCATh/msnnPZjGTNf0bzo3hQEGr?= =?us-ascii?Q?8dThcTERH1KyilLt7kJei2Cs78kHG2iPtizaLnH+AVmVnsaf47Nqd3PVIYL3?= =?us-ascii?Q?jhhpJ6nkv2HkhrP9XnTFxNo0RTW4zrhBeEP4htmUuO0BKq4DARwZ/yMdVa43?= =?us-ascii?Q?s8UQ7APYik/gsVrH2CAFSZuBPDQ+xWx3Pr6v/DTUIL0cJHuI2kWNjQepYN4t?= =?us-ascii?Q?p+vCUXM/t1Df1rJ4v5hYf429Z9Y52+/7L9337XDxrsg6Dc7orHFhciK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xYkGM0Fiya7pmCS4KhA/WDE3eHd?= =?us-ascii?Q?7oIPK3O7rYOeTwVXfMUgOeDGup0QbKY8ydEGL8dFignI0KhisJoKhS1jtrnZ?= =?us-ascii?Q?nufOKbKw4TKKclHk4qrq4b52bEcDkUnf09J0yg4Mk0dNiiIRbWZ0BUhf2puq?= =?us-ascii?Q?Vl3GO/3xFbwdN78biXFYchyiCvMESYiEIJjmVU/MtK83zSxoR37yu5MqDpZB?= =?us-ascii?Q?QBwWLrJ8E156jhAIOcQSy+ogxgaj3OQ2re+Yb7hfmnYVrTZjDKLMaeiiswRV?= =?us-ascii?Q?KzAcNAIIZ5wcCgYGGbJ8pXaCFkFUWkA3U6voGsiVHymRtszu/6f9c5VWo0wy?= =?us-ascii?Q?0EPunJtv5oUVvJfBUpfxDbJY7/SJDJFguRvXVGzZ?= X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: bde3e993-181c-4eab-633c-08de31783507 X-MS-Exchange-CrossTenant-AuthSource: VI2PR04MB11147.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Dec 2025 07:55:49.2841 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 686ea1d3-bc2b-4c6f-a92c-d99c5c301635 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Fkp631rkUel99lpTBX8J25jIpD7hITeqCCrR7B8mcB3qt8T4TjqDhfVcvKOl8AHa2luvyGQkTYxkeTUdRK/1Gg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI2PR04MB11171 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20251201_235553_303447_044DDD5B X-CRM114-Status: GOOD ( 17.68 ) 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 Add a helper function to handle clearing DMA mode, including getting the maximum watermark length and submitting the DMA request. This refactoring makes the code more concise and improves readability. No functional changes. Signed-off-by: Carlos Song Reviewed-by: Frank Li --- drivers/spi/spi-imx.c | 164 +++++++++++++++++++++++------------------- 1 file changed, 92 insertions(+), 72 deletions(-) diff --git a/drivers/spi/spi-imx.c b/drivers/spi/spi-imx.c index e78e02a84b50..012f5bcbf73f 100644 --- a/drivers/spi/spi-imx.c +++ b/drivers/spi/spi-imx.c @@ -1437,6 +1437,94 @@ static int spi_imx_calculate_timeout(struct spi_imx_data *spi_imx, int size) return secs_to_jiffies(2 * timeout); } +static int spi_imx_dma_submit(struct spi_imx_data *spi_imx, + struct spi_transfer *transfer) +{ + struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg; + struct spi_controller *controller = spi_imx->controller; + struct dma_async_tx_descriptor *desc_tx, *desc_rx; + unsigned long transfer_timeout; + unsigned long time_left; + + /* + * The TX DMA setup starts the transfer, so make sure RX is configured + * before TX. + */ + desc_rx = dmaengine_prep_slave_sg(controller->dma_rx, + rx->sgl, rx->nents, DMA_DEV_TO_MEM, + DMA_PREP_INTERRUPT | DMA_CTRL_ACK); + if (!desc_rx) { + transfer->error |= SPI_TRANS_FAIL_NO_START; + return -EINVAL; + } + + desc_rx->callback = spi_imx_dma_rx_callback; + desc_rx->callback_param = (void *)spi_imx; + dmaengine_submit(desc_rx); + reinit_completion(&spi_imx->dma_rx_completion); + dma_async_issue_pending(controller->dma_rx); + + desc_tx = dmaengine_prep_slave_sg(controller->dma_tx, + tx->sgl, tx->nents, DMA_MEM_TO_DEV, + DMA_PREP_INTERRUPT | DMA_CTRL_ACK); + if (!desc_tx) { + dmaengine_terminate_all(controller->dma_tx); + dmaengine_terminate_all(controller->dma_rx); + return -EINVAL; + } + + desc_tx->callback = spi_imx_dma_tx_callback; + desc_tx->callback_param = (void *)spi_imx; + dmaengine_submit(desc_tx); + reinit_completion(&spi_imx->dma_tx_completion); + dma_async_issue_pending(controller->dma_tx); + + spi_imx->devtype_data->trigger(spi_imx); + + transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len); + + /* Wait SDMA to finish the data transfer.*/ + time_left = wait_for_completion_timeout(&spi_imx->dma_tx_completion, + transfer_timeout); + if (!time_left) { + dev_err(spi_imx->dev, "I/O Error in DMA TX\n"); + dmaengine_terminate_all(controller->dma_tx); + dmaengine_terminate_all(controller->dma_rx); + return -ETIMEDOUT; + } + + time_left = wait_for_completion_timeout(&spi_imx->dma_rx_completion, + transfer_timeout); + if (!time_left) { + dev_err(&controller->dev, "I/O Error in DMA RX\n"); + spi_imx->devtype_data->reset(spi_imx); + dmaengine_terminate_all(controller->dma_rx); + return -ETIMEDOUT; + } + + return 0; +} + +static void spi_imx_dma_max_wml_find(struct spi_imx_data *spi_imx, + struct spi_transfer *transfer) +{ + struct sg_table *rx = &transfer->rx_sg; + struct scatterlist *last_sg = sg_last(rx->sgl, rx->nents); + unsigned int bytes_per_word, i; + + /* Get the right burst length from the last sg to ensure no tail data */ + bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word); + for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) { + if (!(sg_dma_len(last_sg) % (i * bytes_per_word))) + break; + } + /* Use 1 as wml in case no available burst length got */ + if (i == 0) + i = 1; + + spi_imx->wml = i; +} + static int spi_imx_dma_configure(struct spi_controller *controller) { int ret; @@ -1484,26 +1572,10 @@ static int spi_imx_dma_configure(struct spi_controller *controller) static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx, struct spi_transfer *transfer) { - struct dma_async_tx_descriptor *desc_tx, *desc_rx; - unsigned long transfer_timeout; - unsigned long time_left; struct spi_controller *controller = spi_imx->controller; - struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg; - struct scatterlist *last_sg = sg_last(rx->sgl, rx->nents); - unsigned int bytes_per_word, i; int ret; - /* Get the right burst length from the last sg to ensure no tail data */ - bytes_per_word = spi_imx_bytes_per_word(transfer->bits_per_word); - for (i = spi_imx->devtype_data->fifo_size / 2; i > 0; i--) { - if (!(sg_dma_len(last_sg) % (i * bytes_per_word))) - break; - } - /* Use 1 as wml in case no available burst length got */ - if (i == 0) - i = 1; - - spi_imx->wml = i; + spi_imx_dma_max_wml_find(spi_imx, transfer); ret = spi_imx_dma_configure(controller); if (ret) @@ -1516,61 +1588,9 @@ static int spi_imx_dma_transfer(struct spi_imx_data *spi_imx, } spi_imx->devtype_data->setup_wml(spi_imx); - /* - * The TX DMA setup starts the transfer, so make sure RX is configured - * before TX. - */ - desc_rx = dmaengine_prep_slave_sg(controller->dma_rx, - rx->sgl, rx->nents, DMA_DEV_TO_MEM, - DMA_PREP_INTERRUPT | DMA_CTRL_ACK); - if (!desc_rx) { - ret = -EINVAL; - goto dma_failure_no_start; - } - - desc_rx->callback = spi_imx_dma_rx_callback; - desc_rx->callback_param = (void *)spi_imx; - dmaengine_submit(desc_rx); - reinit_completion(&spi_imx->dma_rx_completion); - dma_async_issue_pending(controller->dma_rx); - - desc_tx = dmaengine_prep_slave_sg(controller->dma_tx, - tx->sgl, tx->nents, DMA_MEM_TO_DEV, - DMA_PREP_INTERRUPT | DMA_CTRL_ACK); - if (!desc_tx) { - dmaengine_terminate_all(controller->dma_tx); - dmaengine_terminate_all(controller->dma_rx); - return -EINVAL; - } - - desc_tx->callback = spi_imx_dma_tx_callback; - desc_tx->callback_param = (void *)spi_imx; - dmaengine_submit(desc_tx); - reinit_completion(&spi_imx->dma_tx_completion); - dma_async_issue_pending(controller->dma_tx); - - spi_imx->devtype_data->trigger(spi_imx); - - transfer_timeout = spi_imx_calculate_timeout(spi_imx, transfer->len); - - /* Wait SDMA to finish the data transfer.*/ - time_left = wait_for_completion_timeout(&spi_imx->dma_tx_completion, - transfer_timeout); - if (!time_left) { - dev_err(spi_imx->dev, "I/O Error in DMA TX\n"); - dmaengine_terminate_all(controller->dma_tx); - dmaengine_terminate_all(controller->dma_rx); - return -ETIMEDOUT; - } - - time_left = wait_for_completion_timeout(&spi_imx->dma_rx_completion, - transfer_timeout); - if (!time_left) { - dev_err(&controller->dev, "I/O Error in DMA RX\n"); - spi_imx->devtype_data->reset(spi_imx); - dmaengine_terminate_all(controller->dma_rx); - return -ETIMEDOUT; - } + ret = spi_imx_dma_submit(spi_imx, transfer); + if (ret) + return ret; return 0; /* fallback to pio */ -- 2.34.1