From nobody Tue Feb 10 10:18:56 2026 Received: from mx1.sberdevices.ru (mx1.sberdevices.ru [37.18.73.165]) (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 1901213D246 for ; Mon, 27 Jan 2025 15:39:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=37.18.73.165 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737992369; cv=none; b=furdBH1kKIolPJfoG+VX112Qmnvbhpfr5I7iBVRMIMmsKQtYcuiE4z/2turaVbxX4IWO8YYa9m0lVSCWVGq5pngN4wPf+d3fe7I0Zqh8EMKqo53rwwFJ/3eeoh5WQxwmwtInkaK1zx94ePHWbRmpiumr6bECqfmQMEN++duLbUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737992369; c=relaxed/simple; bh=GbB5YU2X5QfE4+YC6HAxFDzQ4/BNU7lSoWvViLQETDY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Tn6MO+Yhg/wBK0+YWPWlHe7IScXhLvaKtnMxFsbevjUAsgTA+sgKrzsUG5o0nqXxGo1RlUgiEBa8cyp1im8JK3JDgTN9mxpYT5c/iaZn7Lr+ClsMiAIChNjY1htSqUWBPvp2DQuEiFcplBPNouWXAcgDe8mRwE3aswJKssvEuhg= 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=qKXOTFYF; arc=none smtp.client-ip=37.18.73.165 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="qKXOTFYF" Received: from p-infra-ksmg-sc-msk01.sberdevices.ru (localhost [127.0.0.1]) by mx1.sberdevices.ru (Postfix) with ESMTP id 1E740100004; Mon, 27 Jan 2025 18:39:15 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.sberdevices.ru 1E740100004 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=salutedevices.com; s=mail; t=1737992355; bh=P8oBy0hM+/nsKiAeOgpi2vWfWfXqaiBXCDoH11AkK84=; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type:From; b=qKXOTFYFT+5jwXrcR/mhR3wKSeWDqBOdZptd1hLCIgxqCJwnuXqWZ9QFdkh/gx013 u3aDXJ/yCrLUcCimiY8VA+kUVjtt4zSr0t5E5SE1VqkpZhgvyjLImfUQeBh/Z6Ho0W hsEQsVTqHo8k1P2yfLvG5912Ta2nJWGRuMkUZi9hZNUGeGyCfSjqdku5WKwXEunOwy 8eP6P8WhgVRKDy4Rxk+VU5+z/aqr/pxscbMba3KdJhspTm8pekDI9n9374iIWiQWXy zbt0w9uk1qFj8TBFDn6sxxd1aDV63kpW0S7VUkLOvXP55TLp1c5DP8SN7TQcxLiEzg qnyIXcQK9LQnw== 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; Mon, 27 Jan 2025 18:39:14 +0300 (MSK) From: Martin Kurbanov To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Ezra Buehler , Cheng Ming Lin , Pratyush Yadav , Daniel Golle , Frieder Schrempf , Alexey Romanov CC: , , , Martin Kurbanov Subject: [PATCH v4 5/6] mtd: spinand: micron: OTP access for MT29F2G01ABAGD Date: Mon, 27 Jan 2025 18:38:23 +0300 Message-ID: <20250127153848.199526-6-mmkurbanov@salutedevices.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250127153848.199526-1-mmkurbanov@salutedevices.com> References: <20250127153848.199526-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-m1.sberdevices.ru (172.24.196.116) 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: 190596 [Jan 27 2025] 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: 50 0.3.50 df4aeb250ed63fd3baa80a493fa6caee5dd9e10f, {Tracking_from_domain_doesnt_match_to}, salutedevices.com:7.1.1;smtp.sberdevices.ru:5.0.1,7.1.1;d41d8cd98f00b204e9800998ecf8427e.com:7.1.1;127.0.0.199:7.1.2, 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: 2025/01/27 09:37:00 #27129213 X-KSMG-AntiVirus-Status: Clean, skipped Content-Type: text/plain; charset="utf-8" Support for OTP area access on Micron MT29F2G01ABAGD chip. Signed-off-by: Martin Kurbanov --- drivers/mtd/nand/spi/micron.c | 135 +++++++++++++++++++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/nand/spi/micron.c b/drivers/mtd/nand/spi/micron.c index 8d741be6d5f3e..4d089eca1c337 100644 --- a/drivers/mtd/nand/spi/micron.c +++ b/drivers/mtd/nand/spi/micron.c @@ -9,6 +9,8 @@ #include #include #include +#include +#include =20 #define SPINAND_MFR_MICRON 0x2c =20 @@ -28,6 +30,10 @@ =20 #define MICRON_SELECT_DIE(x) ((x) << 6) =20 +#define MICRON_MT29F2G01ABAGD_CFG_OTP_STATE BIT(7) +#define MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK \ + (CFG_OTP_ENABLE | MICRON_MT29F2G01ABAGD_CFG_OTP_STATE) + static SPINAND_OP_VARIANTS(quadio_read_cache_variants, //SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0), SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0), @@ -182,6 +188,131 @@ static int micron_8_ecc_get_status(struct spinand_dev= ice *spinand, return -EINVAL; } =20 +static int mt29f2g01abagd_otp_is_locked(struct spinand_device *spinand) +{ + size_t bufsize =3D spinand_otp_page_size(spinand); + size_t retlen; + u8 *buf; + int ret; + + buf =3D kmalloc(bufsize, GFP_KERNEL); + if (!buf) + return -ENOMEM; + + ret =3D spinand_upd_cfg(spinand, + MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK, + MICRON_MT29F2G01ABAGD_CFG_OTP_STATE); + if (ret) + goto free_buf; + + ret =3D spinand_user_otp_read(spinand, 0, bufsize, &retlen, buf); + + if (spinand_upd_cfg(spinand, MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK, + 0)) { + dev_warn(&spinand_to_mtd(spinand)->dev, + "Can not disable OTP mode\n"); + ret =3D -EIO; + } + + if (ret) + goto free_buf; + + /* If all zeros, then the OTP area is locked. */ + if (mem_is_zero(buf, bufsize)) + ret =3D 1; + +free_buf: + kfree(buf); + return ret; +} + +static int mt29f2g01abagd_otp_info(struct spinand_device *spinand, size_t = len, + struct otp_info *buf, size_t *retlen, + bool user) +{ + int locked; + + if (len < sizeof(*buf)) + return -EINVAL; + + locked =3D mt29f2g01abagd_otp_is_locked(spinand); + if (locked < 0) + return locked; + + buf->locked =3D locked; + buf->start =3D 0; + buf->length =3D user ? spinand_user_otp_size(spinand) : + spinand_fact_otp_size(spinand); + + *retlen =3D sizeof(*buf); + return 0; +} + +static int mt29f2g01abagd_fact_otp_info(struct spinand_device *spinand, + size_t len, struct otp_info *buf, + size_t *retlen) +{ + return mt29f2g01abagd_otp_info(spinand, len, buf, retlen, false); +} + +static int mt29f2g01abagd_user_otp_info(struct spinand_device *spinand, + size_t len, struct otp_info *buf, + size_t *retlen) +{ + return mt29f2g01abagd_otp_info(spinand, len, buf, retlen, true); +} + +static int mt29f2g01abagd_otp_lock(struct spinand_device *spinand, loff_t = from, + size_t len) +{ + struct spi_mem_op write_op =3D SPINAND_WR_EN_DIS_OP(true); + struct spi_mem_op exec_op =3D SPINAND_PROG_EXEC_OP(0); + u8 status; + int ret; + + ret =3D spinand_upd_cfg(spinand, + MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK, + MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK); + if (!ret) + return ret; + + ret =3D spi_mem_exec_op(spinand->spimem, &write_op); + if (!ret) + goto out; + + ret =3D spi_mem_exec_op(spinand->spimem, &exec_op); + if (!ret) + goto out; + + ret =3D spinand_wait(spinand, + SPINAND_WRITE_INITIAL_DELAY_US, + SPINAND_WRITE_POLL_DELAY_US, + &status); + if (!ret && (status & STATUS_PROG_FAILED)) + ret =3D -EIO; + +out: + if (spinand_upd_cfg(spinand, MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK, 0)) { + dev_warn(&spinand_to_mtd(spinand)->dev, + "Can not disable OTP mode\n"); + ret =3D -EIO; + } + + return ret; +} + +static const struct spinand_user_otp_ops mt29f2g01abagd_user_otp_ops =3D { + .info =3D mt29f2g01abagd_user_otp_info, + .lock =3D mt29f2g01abagd_otp_lock, + .read =3D spinand_user_otp_read, + .write =3D spinand_user_otp_write, +}; + +static const struct spinand_fact_otp_ops mt29f2g01abagd_fact_otp_ops =3D { + .info =3D mt29f2g01abagd_fact_otp_info, + .read =3D spinand_fact_otp_read, +}; + static const struct spinand_info micron_spinand_table[] =3D { /* M79A 2Gb 3.3V */ SPINAND_INFO("MT29F2G01ABAGD", @@ -193,7 +324,9 @@ static const struct spinand_info micron_spinand_table[]= =3D { &x4_update_cache_variants), 0, SPINAND_ECCINFO(µn_8_ooblayout, - micron_8_ecc_get_status)), + micron_8_ecc_get_status), + SPINAND_USER_OTP_INFO(12, 2, &mt29f2g01abagd_user_otp_ops), + SPINAND_FACT_OTP_INFO(2, 0, &mt29f2g01abagd_fact_otp_ops)), /* M79A 2Gb 1.8V */ SPINAND_INFO("MT29F2G01ABBGD", SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x25), --=20 2.43.0