From nobody Tue Feb 10 23:33:03 2026 Received: from mx1.sberdevices.ru (mx2.sberdevices.ru [45.89.224.132]) (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 6795A1D63D8 for ; Thu, 26 Dec 2024 13:56:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.89.224.132 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735221407; cv=none; b=gEfW5ZEx++jaEXh5dKYl7rTrl9tvAsC0uNk0cA7/rRIN8hH7Z+NF9engX3zsUb5ciUiChOJDn88bqNpgObw9wKI66UJ9QXaqO/qqF92OilPQYPNC8uJwtf+uc073h+D8uCe7j/eNag1vbHandJ5tl3JdNKuG6gRl+g4ceeDR4Cc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735221407; c=relaxed/simple; bh=KvRMPRqF3hhepSO5mrN4yE0cor8JL8oqAOhimGICBco=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gNPhUOGhVxBRC52Pm/X+SbL7l1TxxtiZxq8JqJ+D+QhHPInYTVMxM61FEvZiJ9n8voGKVHw89601cusQLvlfSic8ITfuF6dd5CYUCdHCB+a+hLYvhI4xt6yj3OacPCnRTzWNe7DPX0TeFpQdon76m2sg5BdUj9aN8Mae2EI2rDk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=salutedevices.com; spf=pass smtp.mailfrom=salutedevices.com; dkim=pass (2048-bit key) header.d=salutedevices.com header.i=@salutedevices.com header.b=cjToTACr; arc=none smtp.client-ip=45.89.224.132 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=salutedevices.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=salutedevices.com header.i=@salutedevices.com header.b="cjToTACr" Received: from p-infra-ksmg-sc-msk02.sberdevices.ru (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id 70A0612001E; Thu, 26 Dec 2024 16:56:37 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru 70A0612001E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1735221397; bh=XnP9caPq4vXDueT/dnDu27xM9UZnwpPDLHw6palnojI=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=cjToTACrl35T6k8k0n3zPCJioUQlQR7V9UDFAzzV53+APR0y9oNLK/jmCWKheLeai +yCKdFskWL8YtTlbyqWctKjAN0n30sh9HEZ14tw5MAznByCjSFY+yguiDInkqC2Y+F xr1Msd4OViPmw/U8oTtUxnoUgqFYSD8Q4+BYYGZCgwb+b73Ngzr6Yj3FitYPzSOz8X xTP8MexNul/hu/6Yq6vuSvff95q4Wh8MGJ854irCPfacBiHby0zKbziiM+DhNvUCt6 nn7uD4Q9YeNxzZOSXe9CMOT3RosP6WxkW3+aT4I+FMeSlKvmgu+nc52Agx00mdcBXp oafnaU7Q6XPZA== Received: from smtp.sberdevices.ru (p-i-exch-sc-m01.sberdevices.ru [172.16.192.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.sberdevices.ru (Postfix) with ESMTPS; Thu, 26 Dec 2024 16:56:37 +0300 (MSK) From: Martin Kurbanov To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Ezra Buehler , Alexey Romanov , Frieder Schrempf CC: , , , Martin Kurbanov Subject: [PATCH v3 5/7] mtd: spinand: otp: add helpers functions Date: Thu, 26 Dec 2024 16:55:50 +0300 Message-ID: <20241226135623.43098-6-mmkurbanov@salutedevices.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241226135623.43098-1-mmkurbanov@salutedevices.com> References: <20241226135623.43098-1-mmkurbanov@salutedevices.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: p-i-exch-a-m2.sberdevices.ru (172.24.196.120) To p-i-exch-sc-m01.sberdevices.ru (172.16.192.107) X-KSMG-Rule-ID: 10 X-KSMG-Message-Action: clean X-KSMG-AntiSpam-Lua-Profiles: 190075 [Dec 26 2024] X-KSMG-AntiSpam-Version: 6.1.1.7 X-KSMG-AntiSpam-Envelope-From: mmkurbanov@salutedevices.com X-KSMG-AntiSpam-Rate: 0 X-KSMG-AntiSpam-Status: not_detected X-KSMG-AntiSpam-Method: none X-KSMG-AntiSpam-Auth: dkim=none X-KSMG-AntiSpam-Info: LuaCore: 49 0.3.49 28b3b64a43732373258a371bd1554adb2caa23cb, {Tracking_from_domain_doesnt_match_to}, 127.0.0.199:7.1.2;smtp.sberdevices.ru:7.1.1,5.0.1;salutedevices.com:7.1.1;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1, FromAlignment: s X-MS-Exchange-Organization-SCL: -1 X-KSMG-AntiSpam-Interceptor-Info: scan successful X-KSMG-AntiPhishing: Clean X-KSMG-LinksScanning: Clean X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.0.1.6960, bases: 2024/12/26 12:50:00 #26919000 X-KSMG-AntiVirus-Status: Clean, skipped Content-Type: text/plain; charset="utf-8" The global functions spinand_otp_read() and spinand_otp_write() have been introduced. Since most SPI-NAND flashes read/write OTP in the same way, let's define global functions to avoid code duplication. Signed-off-by: Martin Kurbanov --- drivers/mtd/nand/spi/otp.c | 89 +++++++++++++++++++++++++++++++++++++ include/linux/mtd/spinand.h | 5 +++ 2 files changed, 94 insertions(+) diff --git a/drivers/mtd/nand/spi/otp.c b/drivers/mtd/nand/spi/otp.c index a50d6af818613..b8204bb6d848e 100644 --- a/drivers/mtd/nand/spi/otp.c +++ b/drivers/mtd/nand/spi/otp.c @@ -41,6 +41,95 @@ static int spinand_otp_check_bounds(struct spinand_devic= e *spinand, loff_t ofs, return 0; } =20 +static int spinand_otp_rw(struct spinand_device *spinand, loff_t ofs, + size_t len, size_t *retlen, u8 *buf, bool is_write) +{ + struct nand_page_io_req req =3D {}; + unsigned long long page; + size_t copied =3D 0; + size_t otp_pagesize =3D spinand_otp_page_size(spinand); + int ret; + + if (!len) + return 0; + + ret =3D spinand_otp_check_bounds(spinand, ofs, len); + if (ret) + return ret; + + ret =3D spinand_upd_cfg(spinand, CFG_OTP_ENABLE, CFG_OTP_ENABLE); + if (ret) + return ret; + + page =3D ofs; + req.dataoffs =3D do_div(page, otp_pagesize); + req.pos.page =3D page + spinand->otp->layout.start_page; + req.type =3D is_write ? NAND_PAGE_WRITE : NAND_PAGE_READ; + req.mode =3D MTD_OPS_RAW; + req.databuf.in =3D buf; + + while (copied < len) { + req.datalen =3D min_t(unsigned int, + otp_pagesize - req.dataoffs, + len - copied); + + if (is_write) + ret =3D spinand_write_page(spinand, &req); + else + ret =3D spinand_read_page(spinand, &req); + + if (ret < 0) + break; + + req.databuf.in +=3D req.datalen; + req.pos.page++; + req.dataoffs =3D 0; + copied +=3D req.datalen; + } + + *retlen =3D copied; + + if (spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0)) { + dev_warn(&spinand_to_mtd(spinand)->dev, + "Can not disable OTP mode\n"); + ret =3D -EIO; + } + + return ret; +} + +/** + * spinand_otp_read() - Read from OTP area + * @spinand: the spinand device + * @ofs: the offset to read + * @len: the number of data bytes to read + * @retlen: the pointer to variable to store the number of read bytes + * @buf: the buffer to store the read data + * + * Return: 0 on success, an error code otherwise. + */ +int spinand_otp_read(struct spinand_device *spinand, loff_t ofs, size_t le= n, + size_t *retlen, u8 *buf) +{ + return spinand_otp_rw(spinand, ofs, len, retlen, buf, false); +} + +/** + * spinand_otp_write() - Write to OTP area + * @spinand: the spinand device + * @ofs: the offset to write to + * @len: the number of bytes to write + * @retlen: the pointer to variable to store the number of written bytes + * @buf: the buffer with data to write + * + * Return: 0 on success, an error code otherwise. + */ +int spinand_otp_write(struct spinand_device *spinand, loff_t ofs, size_t l= en, + size_t *retlen, const u8 *buf) +{ + return spinand_otp_rw(spinand, ofs, len, retlen, (u8 *)buf, true); +} + static int spinand_mtd_otp_info(struct mtd_info *mtd, size_t len, size_t *retlen, struct otp_info *buf) { diff --git a/include/linux/mtd/spinand.h b/include/linux/mtd/spinand.h index d6dbb85094283..dcb1811e55d25 100644 --- a/include/linux/mtd/spinand.h +++ b/include/linux/mtd/spinand.h @@ -582,6 +582,11 @@ int spinand_write_page(struct spinand_device *spinand, size_t spinand_otp_page_size(struct spinand_device *spinand); size_t spinand_otp_size(struct spinand_device *spinand); =20 +int spinand_otp_read(struct spinand_device *spinand, loff_t ofs, size_t le= n, + size_t *retlen, u8 *buf); +int spinand_otp_write(struct spinand_device *spinand, loff_t ofs, size_t l= en, + size_t *retlen, const u8 *buf); + int spinand_set_mtd_otp_ops(struct spinand_device *spinand); =20 #endif /* __LINUX_MTD_SPINAND_H */ --=20 2.43.0