U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: dinesh.maniyam@intel.com
To: u-boot@lists.denx.de
Cc: Marek <marex@denx.de>, Simon <simon.k.r.goldschmidt@gmail.com>,
	Tom Rini <trini@konsulko.com>,
	Dario Binacchi <dario.binacchi@amarulasolutions.com>,
	Michael Trimarchi <michael@amarulasolutions.com>,
	Johan Jonker <jbx6244@gmail.com>,
	Michal Simek <michal.simek@amd.com>,
	Arseniy Krasnov <avkrasnov@salutedevices.com>,
	Alexander Dahl <ada@thorsis.com>,
	William Zhang <william.zhang@broadcom.com>,
	Igor Prusov <ivprusov@salutedevices.com>,
	Tien Fong <tien.fong.chee@intel.com>,
	Kok Kiang <kok.kiang.hea@intel.com>,
	Dinesh <dinesh.maniyam@intel.com>,
	Boon Khai <boon.khai.ng@intel.com>,
	Alif <alif.zakuan.yuslaimi@intel.com>,
	Teik Heng <teik.heng.chong@intel.com>,
	Hazim <muhammad.hazim.izzat.zamri@intel.com>,
	Tingting Meng <tingting.meng@intel.com>,
	Jit Loon Lim <jit.loon.lim@intel.com>,
	Sieu Mun Tang <sieu.mun.tang@intel.com>
Subject: [PATCH 16/19] drivers: mtd: nand: spl: Add support for nand SPL load image
Date: Thu, 19 Sep 2024 11:55:09 +0800	[thread overview]
Message-ID: <20240919035512.13854-17-dinesh.maniyam@intel.com> (raw)
In-Reply-To: <20240919035512.13854-1-dinesh.maniyam@intel.com>

From: Dinesh Maniyam <dinesh.maniyam@intel.com>

This patch is add support for spl nand to load binary image from NAND
to RAM.

Signed-off-by: Dinesh Maniyam <dinesh.maniyam@intel.com>
---
 drivers/mtd/nand/raw/cadence_spl.c | 96 ++++++++++++++++++++++++++++++
 1 file changed, 96 insertions(+)
 create mode 100644 drivers/mtd/nand/raw/cadence_spl.c

diff --git a/drivers/mtd/nand/raw/cadence_spl.c b/drivers/mtd/nand/raw/cadence_spl.c
new file mode 100644
index 0000000000..841801420d
--- /dev/null
+++ b/drivers/mtd/nand/raw/cadence_spl.c
@@ -0,0 +1,96 @@
+// SPDX-License-Identifier:     GPL-2.0
+/*
+ * Copyright (C) 2024 Intel Corporation <www.intel.com>
+ */
+
+#include <cadence-nand.h>
+#include <dm.h>
+#include <hang.h>
+#include <malloc.h>
+#include <memalign.h>
+#include <nand.h>
+#include <linux/bitfield.h>
+
+int nand_spl_load_image(u32 offset, u32 len, void *dst)
+{
+	size_t count = len, actual = 0, page_align_overhead = 0;
+	u32 page_align_offset = 0;
+	u8 *page_buffer;
+	int err = 0;
+	struct mtd_info *mtd;
+
+	if (!len || !dst)
+		return -EINVAL;
+
+	mtd = get_nand_dev_by_index(nand_curr_device);
+	if (!mtd)
+		hang();
+
+	if ((offset & (mtd->writesize - 1)) != 0) {
+		page_buffer = malloc_cache_aligned(mtd->writesize);
+		if (!page_buffer) {
+			debug("Error: allocating buffer\n");
+			return -ENOMEM;
+		}
+
+		page_align_overhead = offset % mtd->writesize;
+		page_align_offset = (offset / mtd->writesize) * mtd->writesize;
+		count = mtd->writesize;
+
+		err = nand_read_skip_bad(mtd, page_align_offset, &count,
+					 &actual, mtd->size, page_buffer);
+
+		if (err) {
+			free(page_buffer);
+			return err;
+		}
+
+		count -= page_align_overhead;
+		count = min((size_t)len, count);
+		memcpy(dst, page_buffer + page_align_overhead, count);
+		free(page_buffer);
+
+		len -= count;
+		if (!len)
+			return err;
+
+		offset += count;
+		dst += count;
+		count = len;
+	}
+
+	return nand_read_skip_bad(mtd, offset, &count, &actual, mtd->size, dst);
+}
+
+/*
+ * The offset at which the image to be loaded from NAND is located is
+ * retrieved from the itb header. The presence of bad blocks in the area
+ * of the NAND where the itb image is located could invalidate the offset
+ * which must therefore be adjusted taking into account the state of the
+ * sectors concerned
+ */
+u32 nand_spl_adjust_offset(u32 sector, u32 offs)
+{
+	u32 sector_align_offset, sector_align_end_offset;
+	struct mtd_info *mtd;
+
+	mtd = get_nand_dev_by_index(nand_curr_device);
+	if (!mtd)
+		hang();
+
+	sector_align_offset = sector & (~(mtd->erasesize - 1));
+
+	sector_align_end_offset = (sector + offs) & (~(mtd->erasesize - 1));
+
+	while (sector_align_offset <= sector_align_end_offset) {
+		if (nand_block_isbad(mtd, sector_align_offset)) {
+			offs += mtd->erasesize;
+			sector_align_end_offset += mtd->erasesize;
+		}
+		sector_align_offset += mtd->erasesize;
+	}
+
+	return offs;
+}
+
+void nand_deselect(void) {}
-- 
2.26.2


  parent reply	other threads:[~2024-09-19  3:58 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-19  3:54 [PATCH 00/19] Add Cadence NAND Driver support dinesh.maniyam
2024-09-19  3:54 ` [PATCH 01/19] dt: nand: add cadence nand dt-bindings dinesh.maniyam
2024-09-19  3:54 ` [PATCH 02/19] arm: dts: agilex5: Enabled cdns-nand dts setting dinesh.maniyam
2024-09-19  3:54 ` [PATCH 03/19] include: asm: Add support to read/write 64-bit dinesh.maniyam
2024-09-19  3:54 ` [PATCH 04/19] drivers: mtd: nand: Add driver for Cadence Nand dinesh.maniyam
2024-09-19  3:54 ` [PATCH 05/19] drivers: mtd: nand: cadence: Add support for read status command dinesh.maniyam
2024-09-19  3:54 ` [PATCH 06/19] drivers: mtd: nand: cadence: Add support for readid command dinesh.maniyam
2024-09-19  3:55 ` [PATCH 07/19] drivers: mtd: nand: cadence: Add support for NAND_CMD_PARAM dinesh.maniyam
2024-09-19  3:55 ` [PATCH 08/19] drivers: mtd: nand: cadence: Support NAND_CMD_RESET dinesh.maniyam
2024-09-19  3:55 ` [PATCH 09/19] drivers: mtd: nand: cadence: Support cmd SET_FEATURES & GET_FEATURES dinesh.maniyam
2024-09-19  3:55 ` [PATCH 10/19] drivers: mtd: nand: cadence: Flush dma descriptor dinesh.maniyam
2024-09-19  3:55 ` [PATCH 11/19] drivers: mtd: nand: cadence: Poll for desc complete status dinesh.maniyam
2024-09-19  3:55 ` [PATCH 12/19] drivers: mtd: nand: cadence: Use bounce buffer dinesh.maniyam
2024-09-19  3:55 ` [PATCH 13/19] drivers: nand: Enabled Kconfig and Makefile for cdns-nand dinesh.maniyam
2024-09-19  3:55 ` [PATCH 14/19] configs: nand2_defconfig: Enable configs for nand boot dinesh.maniyam
2024-10-05 15:16   ` Michael Nazzareno Trimarchi
2024-10-05 15:20     ` Tom Rini
2024-10-07  2:23       ` Maniyam, Dinesh
2024-10-07 17:01         ` Michael Nazzareno Trimarchi
2024-10-08  9:43           ` Maniyam, Dinesh
2024-09-19  3:55 ` [PATCH 15/19] drivers: mtd: nand: base: Add support for Hardware ECC for check bad block dinesh.maniyam
2024-09-19  3:55 ` dinesh.maniyam [this message]
2024-10-05 15:22   ` [PATCH 16/19] drivers: mtd: nand: spl: Add support for nand SPL load image Michael Nazzareno Trimarchi
2024-10-07  2:27     ` Maniyam, Dinesh
2024-09-19  3:55 ` [PATCH 17/19] drivers: mtd: nand: Enabled Kconfig and Makefile for Cadence-SPL dinesh.maniyam
2024-09-19  3:55 ` [PATCH 18/19] drivers: mtd: nand: Kconfig: Remove SYS_NAND_BLOCK_SIZE dependency dinesh.maniyam
2024-09-19  3:55 ` [PATCH 19/19] drivers: mtd: nand: Kconfig: Enabled self-init for cdns-nand SPL dinesh.maniyam
2024-09-19  6:05 ` [PATCH 00/19] Add Cadence NAND Driver support Alexander Dahl
2024-10-07  2:16   ` Maniyam, Dinesh

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240919035512.13854-17-dinesh.maniyam@intel.com \
    --to=dinesh.maniyam@intel.com \
    --cc=ada@thorsis.com \
    --cc=alif.zakuan.yuslaimi@intel.com \
    --cc=avkrasnov@salutedevices.com \
    --cc=boon.khai.ng@intel.com \
    --cc=dario.binacchi@amarulasolutions.com \
    --cc=ivprusov@salutedevices.com \
    --cc=jbx6244@gmail.com \
    --cc=jit.loon.lim@intel.com \
    --cc=kok.kiang.hea@intel.com \
    --cc=marex@denx.de \
    --cc=michael@amarulasolutions.com \
    --cc=michal.simek@amd.com \
    --cc=muhammad.hazim.izzat.zamri@intel.com \
    --cc=sieu.mun.tang@intel.com \
    --cc=simon.k.r.goldschmidt@gmail.com \
    --cc=teik.heng.chong@intel.com \
    --cc=tien.fong.chee@intel.com \
    --cc=tingting.meng@intel.com \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    --cc=william.zhang@broadcom.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox