From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56996606A31C for ; Mon, 30 Nov 2026 10:54:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=QD9Ha1Kv; dkim=pass (2048-bit key) header.d=NXP1.onmicrosoft.com header.i=@NXP1.onmicrosoft.com header.a=rsa-sha256 header.s=selector1-NXP1-onmicrosoft-com header.b=o8O99Onp 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:In-Reply-To: Content-Type:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=2D1XoaBIer39q24WE9gYQst217jfgena73Gbj8rUdqc=; b=QD9Ha1KvDnchKh2x5v2qnGZ0ui DHz4h1nb+X1R1VQyo+7V/qEJ/gveitU00w0zikkM73Nee5NSiMM2bEuQLXr7dZ21/sfIRgX0dYLE6 YG8JGxawUk1v+R3T87J2mWn8sqpJDWYKWGH7ffmEs/FTHB7oNuMnLCuFF6kvIJomDuUvunczN5Djx W3YqWd1mVGMJehbYNeqXZrV6Cho8mTP5JhLxKDVTP3wR9sKkKUasj7k08F96NmdXx1JljJZQtu7Ja of+Ags5h5xVizq/u8dO+UosPQlG6dZWb+qvz5a0/mo88DgxafudzTe7UMBNXpd01P9zqYRAcf9QFm fKgMD0jA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXz1m-00000004DMG-0WRZ; Mon, 30 Nov 2026 10:54:34 +0000 Received: from mail-swedencentralazlp170130007.outbound.protection.outlook.com ([2a01:111:f403:c202::7] helo=GVXPR05CU001.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXz1i-00000004DJu-49fE; Mon, 30 Nov 2026 10:54:32 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=HPsaLnxh2KjO/Z0znB35gpZgS9A+1xFuPbgHZ6wULrkACA2NUs9G4nluqNFphW6lB+rOBW8QUbcdJ7Oq0xiSVOOiRNYCtrVZebh38TgXX94HAu16aW2Cq5jH0AjJtBYuAdFE1mWK8qf6kKlz9WvxQO4K8kMjry/3okNIalr6Ah3LxxLN1AAX7XjkuqyrELMmkT6QB8SdLLiKBW9UmutvuZ+808Yq+dH85jp+Itax1BG3MVeBu/YX/dRpMptVab68bCZ/dzNOKezTDJQszCoj1Yo7Gq39P7/wx5+MVBKTPXZ69qfIaTuoihhQ7OULAiQPMLFLhkoJvitqFJdmK73irg== 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=2D1XoaBIer39q24WE9gYQst217jfgena73Gbj8rUdqc=; b=wRTcoQpxvuWJHdUoaGb7iowY7wsQfPReWU69qTScDBdCEiDcGTFk97m8JKvaV4jKwutHfyhODbvrvSdqYwbRNM5FDJ4SrHFPlarCcGfHDU7sptkpcw8WfhGlX2XeSF33Lfv9Ng+Hb67zdmSOm4pxuNA0g5+kh7x7DUlNASVuvTRMlqtw51K/zfSFKZ0ikkNmVCFcJoxXiLRdcG1oBUyORn7cY4pHV0JaJfdbIBYb+lF2wTEUjzEEwgCGIMRQkBG1mQd8tQZ7u/o3wAzJxjcYcO2Lu/ih1mVIVCbXmJmMhavImS8jwuNzWCHnwemgHsnKo5xO2ae265iQSDjS19Y4zw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oss.nxp.com; dmarc=pass action=none header.from=oss.nxp.com; dkim=pass header.d=oss.nxp.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=NXP1.onmicrosoft.com; s=selector1-NXP1-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=2D1XoaBIer39q24WE9gYQst217jfgena73Gbj8rUdqc=; b=o8O99OnpJgLT3suWBQTmVVXMv8XU6C1SuZoaYg7dxSnvWyqidpogteLdo75uqTGWNr1Qzbuo3KcD6Ok9zWqYeXG4TnRBWXGEwFURYk5iQUQUZz0fCl+Y9tkgogmFVCQbVsS6yLsoDAVRr+oy5xQyK8Gbt7W7ABkC90XBrvw8s63gfND5j8XQ5ALB7NI99nc338OQJxWVBcAJPmamor3OAAQMG8I8gOsknsgaVzxGwsUsQd7LZMmzedqbdmeJREk6ntco4yJjuatstL3HjvrtImSUvO7kaJsOoBfffALZDi4r7fFmSN7EjyGTvtyqikLqtjhz5lMlBok+2WPlYwouHA== Authentication-Results: mx.microsoft.com 1; dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=oss.nxp.com; Received: from PA6PR04MB11909.eurprd04.prod.outlook.com (2603:10a6:102:51c::22) by DB8PR04MB6953.eurprd04.prod.outlook.com with HTTP via P2P; Mon, 30 Nov 2026 10:54:16 +0000 Received: from PA6PR04MB11909.eurprd04.prod.outlook.com ([fe80::a4b:fa4e:7fe7:e6a2]) by PA6PR04MB11909.eurprd04.prod.outlook.com ([fe80::a4b:fa4e:7fe7:e6a2%7]) with mapi id 15.21.0662.007; Mon, 30 Nov 2026 10:54:16 +0000 Date: Mon, 30 Nov 2026 18:59:24 +0800 From: Bough Chen To: Miquel Raynal Cc: Mark Brown , Liang Yang , Feng Chen , Xianwei Zhao , Han Xu , Conor Dooley , Valentina Fernandez , Daire McNamara , Haibo Chen , Jyan Chou , Yu-Chun Lin , Maxime Coquelin , Alexandre Torgue , Pratyush Yadav , Michael Walle , Takahiro Kuwano , Richard Weinberger , Vignesh Raghavendra , Tudor Ambarus , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Nicolas Ferre , Alexandre Belloni , Claudiu Beznea , Matthias Brugger , AngeloGioacchino Del Regno , Yogesh Gaur , Chris Packham , Jonathan =?utf-8?Q?Neusch=C3=A4fer?= , Michal Simek , Thomas Petazzoni , Alvin Zhou , Cheng Ming Lin , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-amlogic@lists.infradead.org, imx@lists.linux.dev, linux-riscv@lists.infradead.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-mtd@lists.infradead.org, linux-trace-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org, openbmc@lists.ozlabs.org Subject: Re: [PATCH RFC v2 06/35] spi: spi-mem: Add support for dummy cycles Message-ID: <20261130105924.qv7z4ednx7bygdur@shlinux89> References: <20261127-macronix-upstreaming-spi-mem-cycles-v2-0-5c93f5e5d630@bootlin.com> <20261127-macronix-upstreaming-spi-mem-cycles-v2-6-5c93f5e5d630@bootlin.com> Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261127-macronix-upstreaming-spi-mem-cycles-v2-6-5c93f5e5d630@bootlin.com> X-ClientProxiedBy: SG2PR06CA0235.apcprd06.prod.outlook.com (2603:1096:4:c8::17) To PA6PR04MB11909.eurprd04.prod.outlook.com (2603:10a6:102:51c::22) X-Archived-By: MIME-Version: 1.0 X-MS-TrafficTypeDiagnostic: PA6PR04MB11909:EE_|DB8PR04MB6953:EE_ X-MS-PublicTrafficType: Email client-request-id: 9c8ae5cc-2498-4663-a9e8-219878cb6ec6 request-id: f4106347-59ac-4030-a239-08df4ed04718 X-MS-Office365-Filtering-Correlation-Id: f4106347-59ac-4030-a239-08df4ed04718 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|1800799024|7416014|376014|23010399003|19092799006|10067099003|56012099006|5023799004|11063799006|261021121427099003|261021123027099003|261102130727099003|261009223027099003|261102123227099003|261022105027099003|261022075727099003|261028144727099003|261026140227099003|261026121527099003|18002099003|22082099003|4143699003|6133799003|13003099007; X-Microsoft-Antispam-Message-Info: AdL2KLasMcEzjpTyfKNL6xrQMEb9kvkzZK8JMnAdjxKQ/BoWlU932jaicSd1GaCcANm9SYh4fAnGjU2PKnIIbUPRj1DHUIDWmuAZfgm3+Rj965z9e6TENwmGrZc7jXkVoKtREDFjDlMPDOC0HtAk6SEX74QeHxDtMUZKl+sr044KLresoigsZoDw1FxJJbrQZmNNWAB62pXL0+fb5LDqe5bVWGbF9jn+2UOmRDxos4s+JNDJQUgVpMVpD9EO4lKuoClhidvLlPbhCtAJrYIqPyBgUWNrzh7sVEItks8vOEN8a5naH0JADSlluqtT3UziLMgIzlqnBayJOsT0ginNLXu8pX8K8oZw+EJu55DXGbiq4jOj1cqVHqrh0vaB+HwyoxkTKbHohQbZ7qVCRW7Ha0LwWl2Z1LYRnUkAaA71/vQyG8SiZ9lEWM9p2dAyT6u6PWLHId9iQtiBlWeu//mOaDVnWPIFDH4XfVh41WtZ9jvbiLZJoQ121Yhwo8SnDoHpJeTNEPrDla/XR6EJhkoppJPD/CW501eG2bo99hpdutbNYjcNO7IV1esqW3+n7KDeeg5Pk24h7azj8Gd3HAN6C2mVnE/lTybSRaByFHPZzbX/fHJ3grAjmugYFRmMiHbt X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:;PTR:;CAT:NONE;SFS:(13230040)(366016)(1800799024)(7416014)(376014)(23010399003)(19092799006)(10067099003)(56012099006)(5023799004)(11063799006)(261021121427099003)(261021123027099003)(261102130727099003)(261009223027099003)(261102123227099003)(261022105027099003)(261022075727099003)(261028144727099003)(261026140227099003)(261026121527099003)(18002099003)(22082099003)(4143699003)(6133799003)(13003099007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?cL3d1o8kwTKrrvHEAP51ehZyONH5n77EpEUSNZpQG9+3b/Z/h7MRtwy4VcjX?= =?us-ascii?Q?FWUKXvMYa+6sLldMiPOOXsVNdL4FByn4wYWYAbe8gdwISP8hhkmUMYYD8c5+?= =?us-ascii?Q?g6rfWkT214RiIpP4nwwqP7wBj/kai24cyTIkpprTd1xF5RNdGgIjUiPrsK6C?= =?us-ascii?Q?rp42U3g+i7V1kKCJX3dYo/5MC7VAqQvvqyFvk8VZWQICuvLlDwDECKKQS95v?= =?us-ascii?Q?hnrrDvJlb72P8YN+hTjw2GEsaPddJeRd7y7uERpQWLtiiqvo/syc5EApqyhM?= =?us-ascii?Q?RRd8mvrTKgmxKAP9CIyvEFiRXjlulFsp6s50i3o+ltHLwVwLcuERMj3tv46e?= =?us-ascii?Q?mU0IqGoQKC2m48d5a1bUH0FKzeTG0d+ANTjHik0dQ+vW1i0eYiU2h4g2TmBJ?= =?us-ascii?Q?S3NrAIVwdb5YUQSSqq5W2mDQuCmMZW/zPRvG/LPj+eyqObhuvxJPC7sjZwoE?= =?us-ascii?Q?kdT9KHg9zJ025ndqL2vGhjl7o3TLTafmJSRgplf+XSq13D6YWzF4R38spds9?= =?us-ascii?Q?lFgVr/pOoqv6rcFQoGhTNbUQeGmCTxPcgtMsCfuGb8AR/egLZc0HddSzp65h?= =?us-ascii?Q?jyoXCpOAsnGvBYzqOPdfNdzV6nFQpuuc4WTGTeqwSNat2yM6/wDfCOpCqnu6?= =?us-ascii?Q?J2trvMZszk3+u4OlROXIAC5dDTOnDiI3DLyai/Wv9eNtEEoohhRnCiK5nlwc?= =?us-ascii?Q?/n5QXBVQKSFOecKXcChNxKEkKCekhkqOJqseXeTW9+cQR+8qRxW2WIGxcJoW?= =?us-ascii?Q?szLVHrb49vtHi7vhykhVzHHqdRbsXfqInONvDg/AzhAk2sjnTAlyQtjBQXLn?= =?us-ascii?Q?00TSZEGGXnQ8C4GX7/QYD2tF31ncU23PwH46KxjwgEHLgm/Bevn2tNLxwuuv?= =?us-ascii?Q?a9cF2+Gifgfh+2ka5pVvXn2tBbTTW082fFUaFEEgp8TGVsnbB35uyUoOB440?= =?us-ascii?Q?vGU4L4G6/s7TffHjo3ICp6aUZhRaIdp1d1+/qDbhFjpoTvHrrTun2RhfjoSJ?= =?us-ascii?Q?xa+9dgRGPljXEGg5Sl7Xix+gYJzZzDrG740/l5xYIJQKLwKvNUqfoz0sLh5B?= =?us-ascii?Q?6Wm7OfBuIqs9SQS6HcoysjVgYPZNxYYEpJ4MbLd4W2jUvHxrAUe1HQncmU94?= =?us-ascii?Q?ispNFSHRcEaYMJ2p46k5lfCEIeLBX9/pyhnhOtvtKcKPaf2r9LcLaEe1//zm?= =?us-ascii?Q?gOrL8HMVqSk0HRMudQtwwQFRkw+0gsk45EOQ7mcfoIDpd2BlcALE39iyP4DF?= =?us-ascii?Q?qLDuDoEU6PSCTTUJOEwSUEIxs118qbBfRpnx1v3RTDBRBSb9e+ks6fIEk+yz?= =?us-ascii?Q?zMb/+3yn8tZXk/76lB89I8pOS08FsqoVNzXdrYCwDq2wT60NywIOs/wXEKL0?= =?us-ascii?Q?tV1HGSg1+lx50//He+pPUVWJF2/gQR4idD/ucxCQLUkL99X4XVPzKI/O7m3G?= =?us-ascii?Q?FVogVplEQvB1fSm/3xlZ29KWjZvBiCebXBuszwcW6VtJasopYlKb/yY1u9cy?= =?us-ascii?Q?tsycm8wLp6RGbc+h+BTaAwDpiqqdU5CCPH47S+PcGTE89jNZLbRVF97X+i+8?= =?us-ascii?Q?tYc+7x+LSc+KHp6ifxFvjpohrRXT3IpklEEHH1bUtxBAfq5iOfOWtCfkOPBc?= =?us-ascii?Q?sIuHnpdIjHn+YKuyyfi4+IoVB71levQaDoTncEVMkkQ7nRdDsSWUKaDw3aSb?= =?us-ascii?Q?zzTsksGwsgPQvlROdDEImlyb7qGCGL3SWtxZQQaa1sqEhDE+7QjLOKpOtjDM?= =?us-ascii?Q?puGT++E4G8IQ57mhZ1YIrgKIQl0Cbr2dxiIPUwj7/B+oGYV85xW5?= X-OriginatorOrg: oss.nxp.com X-MS-Exchange-CrossTenant-Network-Message-Id: f4106347-59ac-4030-a239-08df4ed04718 X-MS-Exchange-CrossTenant-AuthSource: PA6PR04MB11909.eurprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 30 Nov 2026 10:54:16.4398 (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: ZBTKFRnGd9HFWXc1R9IiQZT2kVLHKhvVGgIlOCadOpL5n4Ptf7MuJsHmwWhI98XhCKJpQHBJLwV9l69pYY4ikowDylZNeSwSe8laMEGrUkvu2r1m+7PKFCOpWP6hde5M X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB8PR04MB6953 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261130_025431_199269_0CF4F687 X-CRM114-Status: GOOD ( 30.10 ) X-Spam-Score: -1.0 (-) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-13.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Fri, Nov 27, 2026 at 03:22:28PM +0100, Miquel Raynal wrote: > The dummy phase is a number of clock cycles, not a number of bytes. SPI > NOR flashes commonly require a number of dummy cycles that is not a > multiple of 8, and the byte-based representation cannot express it. > > Add a dummy.ncycles field and make it the primary representation. Keep > dummy.nbytes for the legacy byte-oriented controllers and users. The two > fields are mutually exclusive and spi_mem_check_op() enforces that rule. > > Controllers that can program an arbitrary number of cycles advertise the > dummy_cycles capability and consume dummy.ncycles directly. The core > converts the legacy dummy.nbytes to cycles for them, so the users that > still describe the dummy phase in bytes keep working. If the byte count > cannot be expressed as a whole number of cycles, the operation is > rejected. > > For the other controllers the core converts dummy.ncycles to the legacy > dummy.nbytes representation using the usual maths. If the cycle count > cannot be represented exactly, the operation is rejected instead of > being silently truncated. > > Link: https://lore.kernel.org/all/20220911174551.653599-1-sergiu.moga@microchip.com/ > Link: https://lore.kernel.org/all/20250115134158.19908-1-pratyush@kernel.org/ > > Link: https://lore.kernel.org/all/20220911174551.653599-1-sergiu.moga@microchip.com/ > Link: https://lore.kernel.org/all/20250115134158.19908-1-pratyush@kernel.org/ Duplicate Link. With this removed: Reviewed-by: Haibo Chen > Signed-off-by: Miquel Raynal > --- > drivers/spi/spi-mem.c | 224 ++++++++++++++++++++++++++++++++++++++--- > include/linux/spi/spi-mem.h | 39 ++++++- > include/trace/events/spi-mem.h | 5 +- > 3 files changed, 245 insertions(+), 23 deletions(-) > > diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c > index a1f129c47698..1642388b7004 100644 > --- a/drivers/spi/spi-mem.c > +++ b/drivers/spi/spi-mem.c > @@ -12,6 +12,24 @@ > #include > #include > > +static unsigned int > +spi_mem_get_dummy_nbytes(const struct spi_mem_op *op) > +{ > + u32 bits; > + > + if (!op->dummy.ncycles) > + return op->dummy.nbytes; > + > + if (!op->dummy.buswidth) > + return 0; > + > + bits = (u32)op->dummy.ncycles * op->dummy.buswidth; > + if (op->dummy.dtr) > + bits *= 2; > + > + return DIV_ROUND_UP(bits, 8); > +} > + > #define CREATE_TRACE_POINTS > #include > > @@ -252,6 +270,111 @@ static bool spi_mem_buswidth_is_valid(u8 buswidth) > return true; > } > > +/** > + * spi_mem_dummy_cycles_to_bytes() - Convert the dummy phase cycles to bytes > + * @op: the SPI memory operation > + * > + * Return: the number of dummy bytes on success, a negative error code if the > + * dummy phase cannot be represented as a whole number of bytes. > + * If @op->dummy.ncycles is zero, @op->dummy.nbytes is returned. > + * > + * This helper can be used to convert dummy cycles into dummy bytes for > + * controllers that can only insert a whole number of dummy bytes. > + */ > +static int spi_mem_dummy_cycles_to_bytes(const struct spi_mem_op *op) > +{ > + u32 bits; > + > + if (!op->dummy.ncycles) > + return op->dummy.nbytes; > + > + if (!op->dummy.buswidth) > + return -EOPNOTSUPP; > + > + bits = (u32)op->dummy.ncycles * op->dummy.buswidth; > + if (op->dummy.dtr) > + bits *= 2; > + > + if (bits % 8 || bits / 8 > U8_MAX) > + return -EOPNOTSUPP; > + > + return bits / 8; > +} > + > +/** > + * spi_mem_dummy_bytes_to_cycles() - Convert the dummy phase bytes to cycles > + * @op: the SPI memory operation > + * > + * Return: the number of dummy clock cycles on success, a negative error code > + * if the dummy phase cannot be represented as a whole number of clock > + * cycles. If @op->dummy.nbytes is zero, @op->dummy.ncycles is returned. > + * > + * This helper can be used to convert inlet operations using dummy bytes > + * into operations using dummy cycles. > + */ > +static int spi_mem_dummy_bytes_to_cycles(const struct spi_mem_op *op) > +{ > + u32 bits, bits_per_cycle; > + > + if (!op->dummy.nbytes) > + return op->dummy.ncycles; > + > + bits_per_cycle = op->dummy.buswidth; > + if (op->dummy.dtr) > + bits_per_cycle *= 2; > + if (!bits_per_cycle) > + return -EOPNOTSUPP; > + > + bits = (u32)op->dummy.nbytes * 8; > + if (bits % bits_per_cycle) > + return -EOPNOTSUPP; > + > + bits /= bits_per_cycle; > + if (bits > U8_MAX) > + return -EOPNOTSUPP; > + > + return bits; > +} > + > +/* > + * Controllers either program the dummy phase in clock cycles or in whole > + * bytes. Convert the representation used by the operation to the one the > + * controller expects. > + * > + * Return: 0 if the operation is lossless, -EOPNOTSUPP otherwise. > + */ > +static int spi_mem_convert_dummy(struct spi_mem *mem, struct spi_mem_op *op) > +{ > + struct spi_controller *ctlr = mem->spi->controller; > + int n; > + > + if (op->dummy.ncycles) { > + if (spi_mem_controller_is_capable(ctlr, dummy_cycles)) > + return 0; > + > + n = spi_mem_dummy_cycles_to_bytes(op); > + if (n < 0) > + return n; > + > + op->dummy.nbytes = n; > + op->dummy.ncycles = 0; > + } else if (op->dummy.nbytes) { > + if (!spi_mem_controller_is_capable(ctlr, dummy_cycles)) > + return 0; > + > + n = spi_mem_dummy_bytes_to_cycles(op); > + if (n < 0) > + return n; > + > + op->dummy.ncycles = n; > + op->dummy.nbytes = 0; > + > + return 0; > + } > + > + return 0; > +} > + > static int spi_mem_check_op(const struct spi_mem_op *op) > { > if (!op->cmd.buswidth || !op->cmd.nbytes) > @@ -259,9 +382,13 @@ static int spi_mem_check_op(const struct spi_mem_op *op) > > if ((op->addr.nbytes && !op->addr.buswidth) || > (op->dummy.nbytes && !op->dummy.buswidth) || > + (op->dummy.ncycles && !op->dummy.buswidth) || > (op->data.nbytes && !op->data.buswidth)) > return -EINVAL; > > + if (op->dummy.nbytes && op->dummy.ncycles) > + return -EINVAL; > + > if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) || > !spi_mem_buswidth_is_valid(op->addr.buswidth) || > !spi_mem_buswidth_is_valid(op->dummy.buswidth) || > @@ -321,6 +448,9 @@ bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op) > if (spi_mem_check_op(&eval_op)) > return false; > > + if (spi_mem_convert_dummy(mem, &eval_op)) > + return false; > + > return spi_mem_internal_supports_op(mem, &eval_op); > } > EXPORT_SYMBOL_GPL(spi_mem_supports_op); > @@ -366,6 +496,7 @@ static void spi_mem_access_end(struct spi_mem *mem) > static void spi_mem_add_op_stats(struct spi_statistics __percpu *pcpu_stats, > const struct spi_mem_op *op, int exec_op_ret) > { > + int dummy_nbytes = spi_mem_get_dummy_nbytes(op); > struct spi_statistics *stats; > u64 len, l2len; > > @@ -382,7 +513,7 @@ static void spi_mem_add_op_stats(struct spi_statistics __percpu *pcpu_stats, > > /* Use the sum of all lengths as bytes count and histogram value. */ > len = op->cmd.nbytes + op->addr.nbytes; > - len += op->dummy.nbytes + op->data.nbytes; > + len += dummy_nbytes + op->data.nbytes; > u64_stats_add(&stats->bytes, len); > l2len = min(fls(len), SPI_STATISTICS_HISTO_SIZE) - 1; > u64_stats_inc(&stats->transfer_bytes_histo[l2len]); > @@ -409,31 +540,41 @@ static void spi_mem_add_op_stats(struct spi_statistics __percpu *pcpu_stats, > /** > * spi_mem_exec_op() - Execute a memory operation > * @mem: the SPI memory > - * @op: the memory operation to execute > + * @ro_op: the memory operation to execute (shall not be edited) > * > * Executes a memory operation. > * > - * This function first checks that @op is supported and then tries to execute > - * it. > + * This function first checks that @ro_op is supported and then tries to > + * execute it. > * > * Return: 0 in case of success, a negative error code otherwise. > */ > -int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) > +int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *ro_op) > { > unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0; > struct spi_controller *ctlr = mem->spi->controller; > + struct spi_mem_op wr_op = *ro_op, *op = &wr_op; > struct spi_transfer xfers[4] = { }; > struct spi_message msg; > u8 *tmpbuf; > int ret; > > /* Make sure the operation frequency is correct before going futher */ > - spi_mem_adjust_op_freq(mem, (struct spi_mem_op *)op); > + spi_mem_adjust_op_freq(mem, op); > + > + ret = spi_mem_check_op(op); > + if (ret) > + return ret; > + > + /* Also make sure the dummy phase can be consumed by the controller */ > + ret = spi_mem_convert_dummy(mem, op); > + if (ret) > + return ret; > > dev_vdbg(&mem->spi->dev, "[cmd: 0x%02x][%dB addr: %#8llx][%2dB dummy][%4dB data %s] %d%c-%d%c-%d%c-%d%c @ %uHz\n", > op->cmd.opcode, > op->addr.nbytes, (op->addr.nbytes ? op->addr.val : 0), > - op->dummy.nbytes, > + spi_mem_get_dummy_nbytes(op), > op->data.nbytes, (op->data.nbytes ? (op->data.dir == SPI_MEM_DATA_IN ? " read" : "write") : " "), > op->cmd.buswidth, op->cmd.dtr ? 'D' : 'S', > op->addr.buswidth, op->addr.dtr ? 'D' : 'S', > @@ -441,10 +582,6 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) > op->data.buswidth, op->data.dtr ? 'D' : 'S', > op->max_freq ? op->max_freq : mem->spi->max_speed_hz); > > - ret = spi_mem_check_op(op); > - if (ret) > - return ret; > - > if (!spi_mem_internal_supports_op(mem, op)) > return -EOPNOTSUPP; > > @@ -472,6 +609,22 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) > } > } > > + /* > + * spi_mem_convert_dummy() converted the dummy phase to cycles because > + * the controller advertised the dummy_cycles capability. The regular > + * SPI transfer path below builds spi_transfer segments and can only > + * express the dummy phase in bytes, so convert it back. Reject the > + * operation if the cycle count cannot be represented exactly. > + */ > + if (op->dummy.ncycles) { > + ret = spi_mem_dummy_cycles_to_bytes(op); > + if (ret < 0) > + return ret; > + > + op->dummy.nbytes = ret; > + op->dummy.ncycles = 0; > + } > + > tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; > > /* > @@ -625,12 +778,22 @@ int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) > { > struct spi_controller *ctlr = mem->spi->controller; > size_t len; > + int ret; > + > + ret = spi_mem_convert_dummy(mem, op); > + if (ret) > + return ret; > > if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size) > return ctlr->mem_ops->adjust_op_size(mem, op); > > if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) { > - len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; > + int dummy_nbytes = spi_mem_dummy_cycles_to_bytes(op); > + > + if (dummy_nbytes < 0) > + return dummy_nbytes; > + > + len = op->cmd.nbytes + op->addr.nbytes + dummy_nbytes; > > if (len > spi_max_transfer_size(mem->spi)) > return -EINVAL; > @@ -701,9 +864,12 @@ u64 spi_mem_calc_op_duration(struct spi_mem *mem, struct spi_mem_op *op) > ncycles += ((op->cmd.nbytes * 8) / op->cmd.buswidth) / (op->cmd.dtr ? 2 : 1); > ncycles += ((op->addr.nbytes * 8) / op->addr.buswidth) / (op->addr.dtr ? 2 : 1); > > - /* Dummy bytes are optional for some SPI flash memory operations */ > - if (op->dummy.nbytes) > - ncycles += ((op->dummy.nbytes * 8) / op->dummy.buswidth) / (op->dummy.dtr ? 2 : 1); > + /* Dummy cycles are optional for some SPI flash memory operations */ > + if (op->dummy.ncycles) > + ncycles += op->dummy.ncycles; > + else if (op->dummy.nbytes) > + ncycles += ((op->dummy.nbytes * 8) / op->dummy.buswidth) / > + (op->dummy.dtr ? 2 : 1); > > ncycles += ((op->data.nbytes * 8) / op->data.buswidth) / (op->data.dtr ? 2 : 1); > > @@ -809,6 +975,21 @@ spi_mem_dirmap_create(struct spi_mem *mem, > desc->mem = mem; > desc->info = *info; > desc->info.op_tmpl = &desc->info.primary_op_tmpl; > + > + ret = spi_mem_convert_dummy(mem, &desc->info.primary_op_tmpl); > + if (ret) { > + kfree(desc); > + return ERR_PTR(ret); > + } > + > + if (desc->info.secondary_op_tmpl.cmd.opcode) { > + ret = spi_mem_convert_dummy(mem, &desc->info.secondary_op_tmpl); > + if (ret) { > + kfree(desc); > + return ERR_PTR(ret); > + } > + } > + > if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create) { > ret = spi_mem_access_start(mem); > if (ret) { > @@ -819,6 +1000,8 @@ spi_mem_dirmap_create(struct spi_mem *mem, > ret = ctlr->mem_ops->dirmap_create(desc); > > spi_mem_access_end(mem); > + } else { > + ret = -EOPNOTSUPP; > } > > if (ret) { > @@ -1065,6 +1248,7 @@ int spi_mem_poll_status(struct spi_mem *mem, > u16 timeout_ms) > { > struct spi_controller *ctlr = mem->spi->controller; > + struct spi_mem_op eval_op = *op; > int ret = -EOPNOTSUPP; > int read_status_ret; > u16 status; > @@ -1074,11 +1258,19 @@ int spi_mem_poll_status(struct spi_mem *mem, > return -EINVAL; > > if (ctlr->mem_ops && ctlr->mem_ops->poll_status && !spi_get_csgpiod(mem->spi, 0)) { > + ret = spi_mem_check_op(&eval_op); > + if (ret) > + return ret; > + > + ret = spi_mem_convert_dummy(mem, &eval_op); > + if (ret) > + return ret; > + > ret = spi_mem_access_start(mem); > if (ret) > return ret; > > - ret = ctlr->mem_ops->poll_status(mem, op, mask, match, > + ret = ctlr->mem_ops->poll_status(mem, &eval_op, mask, match, > initial_delay_us, polling_delay_us, > timeout_ms); > > diff --git a/include/linux/spi/spi-mem.h b/include/linux/spi/spi-mem.h > index 0cce6b57242a..fc9f75c0609f 100644 > --- a/include/linux/spi/spi-mem.h > +++ b/include/linux/spi/spi-mem.h > @@ -11,6 +11,7 @@ > #ifndef __LINUX_SPI_MEM_H > #define __LINUX_SPI_MEM_H > > +#include > #include > > #define SPI_MEM_OP_CMD(__opcode, __buswidth) \ > @@ -75,6 +76,12 @@ > .buswidth = __buswidth, \ > } > > +#define SPI_MEM_OP_DUMMY_CYCLES(__ncycles, __buswidth) \ > + { \ > + .ncycles = __ncycles, \ > + .buswidth = __buswidth, \ > + } > + > #define SPI_MEM_DTR_OP_DUMMY(__nbytes, __buswidth) \ > { \ > .nbytes = __nbytes, \ > @@ -82,6 +89,13 @@ > .dtr = true, \ > } > > +#define SPI_MEM_DTR_OP_DUMMY_CYCLES(__ncycles, __buswidth) \ > + { \ > + .ncycles = __ncycles, \ > + .buswidth = __buswidth, \ > + .dtr = true, \ > + } > + > #define SPI_MEM_OP_NO_DUMMY { } > > #define SPI_MEM_OP_DATA_IN(__nbytes, __buf, __buswidth) \ > @@ -154,10 +168,18 @@ enum spi_mem_data_dir { > * address value, so users should make sure the value fits in the > * assigned number of bytes. > * @dummy: data for dummy operation > - * @dummy.nbytes: number of dummy bytes to send after an opcode or address. Can > - * be zero if the operation does not require dummy bytes > - * @dummy.buswidth: number of IO lanes used to transmit the dummy bytes > - * @dummy.dtr: whether the dummy bytes should be sent in DTR mode or not > + * @dummy.ncycles: number of dummy clock cycles to insert after an opcode or > + * address. Can be zero if the operation does not require > + * dummy cycles. Mutually exclusive with @dummy.nbytes. > + * @dummy.nbytes: legacy number of dummy bytes to send after an opcode or > + * address. Can be zero if the operation does not require dummy > + * bytes. Mutually exclusive with @dummy.ncycles. > + * @dummy.buswidth: number of IO lanes used during the dummy phase. It is > + * also used to convert @dummy.ncycles to bytes for controllers > + * that can only insert a whole number of dummy bytes. > + * @dummy.dtr: whether the dummy phase is clocked in DTR mode. This only > + * describes the pin state and does not change the number of > + * clock cycles. > * @data: the data attributes > * @data.buswidth: number of IO lanes used to send/receive the data > * @data.dtr: whether the data should be sent in DTR mode or not > @@ -191,6 +213,7 @@ struct spi_mem_op { > } addr; > > struct { > + u8 ncycles; > u8 nbytes; > u8 buswidth; > u8 dtr : 1; > @@ -392,6 +415,11 @@ struct spi_controller_mem_ops { > * is a pause in the transfer (eg. internal bus contention or > * DMA arbitration on an interconnect). Features such as NAND > * continuous reads shall not be leveraged. > + * @dummy_cycles: Supports programming the exact number of dummy clock cycles. > + * When set, the core converts the legacy @dummy.nbytes to clock > + * cycles. When unset, the core converts @dummy.ncycles to whole > + * dummy bytes for the controller and rejects operations that > + * cannot be converted exactly. > */ > struct spi_controller_mem_caps { > bool dtr; > @@ -400,6 +428,7 @@ struct spi_controller_mem_caps { > bool per_op_freq; > bool secondary_op_tmpl; > bool no_cs_assertion; > + bool dummy_cycles; > }; > > #define spi_mem_controller_is_capable(ctlr, cap) \ > @@ -473,7 +502,7 @@ bool spi_mem_supports_op(struct spi_mem *mem, > const struct spi_mem_op *op); > > int spi_mem_exec_op(struct spi_mem *mem, > - const struct spi_mem_op *op); > + const struct spi_mem_op *ro_op); > > const char *spi_mem_get_name(struct spi_mem *mem); > > diff --git a/include/trace/events/spi-mem.h b/include/trace/events/spi-mem.h > index d13f0bcff5e7..9df571d7799d 100644 > --- a/include/trace/events/spi-mem.h > +++ b/include/trace/events/spi-mem.h > @@ -22,7 +22,8 @@ TRACE_EVENT(spi_mem_start_op, > > TP_STRUCT__entry( > __string(name, mem->name) > - __dynamic_array(u8, op, 1 + op->addr.nbytes + op->dummy.nbytes) > + __dynamic_array(u8, op, 1 + op->addr.nbytes + > + spi_mem_get_dummy_nbytes(op)) > __dynamic_array(u8, data, op->data.dir == SPI_MEM_DATA_OUT ? > min(op->data.nbytes, 64) : 0) > __field(u32, data_len) > @@ -53,7 +54,7 @@ TRACE_EVENT(spi_mem_start_op, > op->addr.val >> (8 * (op->addr.nbytes - i - 1)); > > memset(((u8 *)__get_dynamic_array(op)) + op->addr.nbytes + 1, > - 0xff, op->dummy.nbytes); > + 0xff, spi_mem_get_dummy_nbytes(op)); > > __entry->data_len = op->data.nbytes; > __entry->data_buswidth = op->data.buswidth; > > -- > 2.56.0 >