Received: by 2002:ab2:6a05:0:b0:1f8:1780:a4ed with SMTP id w5csp2723337lqo; Tue, 14 May 2024 07:25:34 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVAuXU5iOV+fnAmn6srS+tV62sx44or5+v2ErQHEBiWN4uIiwa8qdqmfiP3JJBUoBrRBcjE0q8xMjUaJDI3m8V9yEIFS0VYQFuyd5/a8w== X-Google-Smtp-Source: AGHT+IE9iNTvuOeBMpMIfAOoUe9JA7nDn0Ofw3cy7lAzwGpVtM0trTNb1x0ApvxizRZ/EG9KfK0r X-Received: by 2002:a05:620a:244a:b0:792:b0ad:eaeb with SMTP id af79cd13be357-792c757907cmr1591690985a.5.1715696734185; Tue, 14 May 2024 07:25:34 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715696734; cv=pass; d=google.com; s=arc-20160816; b=ESS0QRQhrrPmNQGI8NLwbEnqfi8p5ihZ5fVSmMTy7ULD0l2mX186tVSer2l8vrcNtH 428H/ZWZbvxolcOgdZPHP47kHSFfvvWHL5Uq2G/vhRyDm5M0osbTEHsNp9ox+NPFyKwn Mb5N3UI0wxnqN6R5+Q4/LNkwv5VeY1yWenFvjcsJbX9CvgaYtZixfvnjN6rSv5jwSchR cw4+t4BzqGgZ7R0Vxf79vowBO1frd81lbB+OwSQy7Dtf1DqpUtsB7JL8HDhzvTEg/qhM xFtb4qQQLNOAOfYaViXu7CQzCc4ymrMsfCJ6ncNNtGN2NkrpG12/Yhf9UIiinHh0Z+6j BcMg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from; bh=VsVxz6n8bNt3AIurytu1HvygG5MzTPU0Hd/o4NRb9Qo=; fh=A6vTKu2Ae1sKKtFnOKaQ4OlMrHeYs/jXUw2105gYFpM=; b=qaHqi7o8KybgPDQeiaBwuAZg6GfZJTVhv/L4SpyBTd/qfWA5XDdG74aPjTv9wC8gIn Yqac6PVb7qB9ikcvk3ZH5bbZ/gbkdUT+r+bo0WGvCwDYAQ69dcVWV2yl8Nma//7yQopQ SEl0WAzN11Rqrd2wyGQFOPyqlXFb6l4/N6Z+YxS6lf1xCXni/P4UM8mR5Jk0aleDAnzM c7b2Wd1YDbcKBanwXy2f3INGP1Dg8B4V8IgEvVKg7KIdp1Afuu792HerraRoPoWkjZF1 9KWcVvZ9qjftt5ViQQVaJPdQPwpWAlCRqykpqAS7726hiyTGNiGE/7ADJFhBdKZPmOUI QFXw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=pengutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-178802-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178802-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-792bf33b3a7si1272361485a.395.2024.05.14.07.25.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 07:25:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-178802-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=pengutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-178802-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-178802-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id B82F81C21DB5 for ; Tue, 14 May 2024 14:25:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 441C7144D39; Tue, 14 May 2024 14:25:28 +0000 (UTC) Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (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 07278144D0B for ; Tue, 14 May 2024 14:25:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715696727; cv=none; b=f+cKdDWYDmF3xcWZTbYWdQGUbfH6wEWgbX1rVnLa7SxPzmGL+S7DRFi6FVrfOXAT0yb3pVEwOOUT0DjYLbkHPJpb25WTs3RWRrzngr+V3Bjh8ZRsbcNDIxJimNA74CI1K9WOl9Aj0BxIKfSqJYdGTXbJvcE4de356XBFmISccNw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715696727; c=relaxed/simple; bh=fFUDAF5lVPhHSvNzxUUCBmqbwiK0hi2y6kNbxl0InYA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=abRwIhAa2Tnh8y7Snq4AVkdHCZeJ1XUsj68d12dUMBXljM2BoG1hgD5tagr6ZGgF8ENctIuTTh9lyDa/3TLWRy5GX9Vn4/jVdC7WmyEmsU2OiFG52t673BlaIkkDItiLPpuSxtUouJf5ZugXdG0jue9rYALe+ivSo+3qvhj20Ew= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1s6t5m-00044O-7m; Tue, 14 May 2024 16:25:22 +0200 Received: from [2a0a:edc0:0:1101:1d::28] (helo=dude02.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1s6t5l-001N6X-6h; Tue, 14 May 2024 16:25:21 +0200 Received: from localhost ([::1] helo=dude02.red.stw.pengutronix.de) by dude02.red.stw.pengutronix.de with esmtp (Exim 4.96) (envelope-from ) id 1s6t5l-004mXh-0Q; Tue, 14 May 2024 16:25:21 +0200 From: Sascha Hauer Date: Tue, 14 May 2024 16:25:19 +0200 Subject: [PATCH v3 3/3] mtd: nand: mxc_nand: support software ECC Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240514-mtd-nand-mxc-nand-exec-op-v3-3-3e6f45fd3d82@pengutronix.de> References: <20240514-mtd-nand-mxc-nand-exec-op-v3-0-3e6f45fd3d82@pengutronix.de> In-Reply-To: <20240514-mtd-nand-mxc-nand-exec-op-v3-0-3e6f45fd3d82@pengutronix.de> To: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Sascha Hauer X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1715696721; l=5082; i=s.hauer@pengutronix.de; s=20230412; h=from:subject:message-id; bh=fFUDAF5lVPhHSvNzxUUCBmqbwiK0hi2y6kNbxl0InYA=; b=tj+4TIwISXBORWmh2MPTpURPBXKBF+9LJxh4lMhXFqbOwW+GbhgFW330wF+Nw7AwulilNg3xE GJVIJBKANLCBa9lJ/4sDYhcTtPGghY2dTCeqAy1aoVLkgpKM+QG8l28 X-Developer-Key: i=s.hauer@pengutronix.de; a=ed25519; pk=4kuc9ocmECiBJKWxYgqyhtZOHj5AWi7+d0n/UjhkwTg= X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: s.hauer@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-kernel@vger.kernel.org With these changes the driver can be used with software BCH ECC which is useful for NAND chips that require a stronger ECC than the i.MX hardware supports. The controller normally interleaves user data with OOB data when accessing the NAND chip. With Software BCH ECC we write the data to the NAND in a way that the raw data on the NAND chip matches the way the NAND layer sees it. This way commands like NAND_CMD_RNDOUT work as expected. This was tested on i.MX27 but should work on the other SoCs supported by this driver as well. Signed-off-by: Sascha Hauer --- drivers/mtd/nand/raw/mxc_nand.c | 73 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/drivers/mtd/nand/raw/mxc_nand.c b/drivers/mtd/nand/raw/mxc_nand.c index 6f8b8f4b118ec..ae4cff2584a2d 100644 --- a/drivers/mtd/nand/raw/mxc_nand.c +++ b/drivers/mtd/nand/raw/mxc_nand.c @@ -1404,10 +1404,10 @@ static int mxcnd_attach_chip(struct nand_chip *chip) chip->ecc.bytes = host->devtype_data->eccbytes; host->eccsize = host->devtype_data->eccsize; chip->ecc.size = 512; - mtd_set_ooblayout(mtd, host->devtype_data->ooblayout); switch (chip->ecc.engine_type) { case NAND_ECC_ENGINE_TYPE_ON_HOST: + mtd_set_ooblayout(mtd, host->devtype_data->ooblayout); chip->ecc.read_page = mxc_nand_read_page; chip->ecc.read_page_raw = mxc_nand_read_page_raw; chip->ecc.read_oob = mxc_nand_read_oob; @@ -1417,6 +1417,8 @@ static int mxcnd_attach_chip(struct nand_chip *chip) break; case NAND_ECC_ENGINE_TYPE_SOFT: + chip->ecc.write_page_raw = nand_monolithic_write_page_raw; + chip->ecc.read_page_raw = nand_monolithic_read_page_raw; break; default: @@ -1472,6 +1474,59 @@ static int mxcnd_setup_interface(struct nand_chip *chip, int chipnr, return host->devtype_data->setup_interface(chip, chipnr, conf); } +static void copy_page_to_sram(struct mtd_info *mtd, const void *buf, int buf_len) +{ + struct nand_chip *this = mtd_to_nand(mtd); + struct mxc_nand_host *host = nand_get_controller_data(this); + unsigned int no_subpages = mtd->writesize / 512; + int oob_per_subpage, i; + + oob_per_subpage = (mtd->oobsize / no_subpages) & ~1; + + /* + * During a page write the i.MX NAND controller will read 512b from + * main_area0 SRAM, then oob_per_subpage bytes from spare0 SRAM, then + * 512b from main_area1 SRAM and so on until the full page is written. + * For software ECC we want to have a 1:1 mapping between the raw page + * data on the NAND chip and the view of the NAND core. This is + * necessary to make the NAND_CMD_RNDOUT read the data it expects. + * To accomplish this we have to write the data in the order the controller + * reads it. This is reversed in copy_page_from_sram() below. + */ + for (i = 0; i < no_subpages; i++) { + memcpy16_toio(host->main_area0 + i * 512, buf, 512); + buf += 512; + + memcpy16_toio(host->spare0 + i * host->devtype_data->spare_len, buf, + oob_per_subpage); + buf += oob_per_subpage; + } +} + +static void copy_page_from_sram(struct mtd_info *mtd) +{ + struct nand_chip *this = mtd_to_nand(mtd); + struct mxc_nand_host *host = nand_get_controller_data(this); + void *buf = host->data_buf; + unsigned int no_subpages = mtd->writesize / 512; + int oob_per_subpage, i; + + /* mtd->writesize is not set during ident scanning */ + if (!no_subpages) + no_subpages = 1; + + oob_per_subpage = (mtd->oobsize / no_subpages) & ~1; + + for (i = 0; i < no_subpages; i++) { + memcpy16_fromio(buf, host->main_area0 + i * 512, 512); + buf += 512; + + memcpy16_fromio(buf, host->spare0 + i * host->devtype_data->spare_len, + oob_per_subpage); + buf += oob_per_subpage; + } +} + static int mxcnd_do_exec_op(struct nand_chip *chip, const struct nand_subop *op) { @@ -1510,7 +1565,10 @@ static int mxcnd_do_exec_op(struct nand_chip *chip, buf_write = instr->ctx.data.buf.out; buf_len = instr->ctx.data.len; - memcpy32_toio(host->main_area0, buf_write, buf_len); + if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) + memcpy32_toio(host->main_area0, buf_write, buf_len); + else + copy_page_to_sram(mtd, buf_write, buf_len); host->devtype_data->send_page(mtd, NFC_INPUT); @@ -1545,10 +1603,15 @@ static int mxcnd_do_exec_op(struct nand_chip *chip, host->devtype_data->read_page(chip); - if (IS_ALIGNED(buf_len, 4)) { - memcpy32_fromio(buf_read, host->main_area0, buf_len); + if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) { + if (IS_ALIGNED(buf_len, 4)) { + memcpy32_fromio(buf_read, host->main_area0, buf_len); + } else { + memcpy32_fromio(host->data_buf, host->main_area0, mtd->writesize); + memcpy(buf_read, host->data_buf, buf_len); + } } else { - memcpy32_fromio(host->data_buf, host->main_area0, mtd->writesize); + copy_page_from_sram(mtd); memcpy(buf_read, host->data_buf, buf_len); } -- 2.39.2