From patchwork Wed Jan 25 17:31:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 646562 Delivered-To: patch@linaro.org Received: by 2002:a17:522:b9de:b0:4b9:b062:db3b with SMTP id fj30csp401545pvb; Wed, 25 Jan 2023 09:32:21 -0800 (PST) X-Google-Smtp-Source: AMrXdXsSTVphiC8SdpK0TToh5ulddejDBHyAfwGv3Dq3b6mPL4QUt8TbxbjJtfOfDdnzYo+Y8kAB X-Received: by 2002:a05:6870:e9a:b0:15f:5713:6407 with SMTP id mm26-20020a0568700e9a00b0015f57136407mr16583739oab.1.1674667940850; Wed, 25 Jan 2023 09:32:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674667940; cv=none; d=google.com; s=arc-20160816; b=nfx8xzcbLXl/fQBl4bqhCYoWk1+y2T8Yp5Ko/88+9RukiBDfRrCeVwCuIFfVQeHPYm 9wsE7NEp8jdbSPskGe9GLO68aAhvLSQawXcPKZOPk9fDhZNSvIlmzemSVbj95Slrdl28 t5H6PoleYEHV/13ZEFtRgr5NX4cDdHbJ+EFaD/ALO8XSKmZgq69TdcfEutzK5G0Z3+2V bHIJciGyK5HOdEjim6NWMaVPlne/bRNWoFJnmngwXMb86QuAHKviky7PcEFWx+eesHt5 jSTT0lWcPkFLPtCE6ihsdRs5bikiUQk3G7Of2CGuBIdRtob6RvLFIcuKn4hntAWir4Iw 3SUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=lykCEayJRiZG1+5JbNN5UQvX8FOES4udJby3A5OW1Bw=; b=f9t+u/mm8COszlzzIIuKxmV9PxlhCz8OrscZo2qW6tiCug0MQoz/TY5NVNFIkg349n YQrCDMDqVERxwnx5huc8XW6ft6zs2gkLW4NPbo5XparZ6sMQriSVQwvELbL5bFdyuqRc WUndBFeIfLchhVl66XOWZGbC6hWvuxQ8X6XOMOh6eLhiNmdP+FdAJ85cMIhXYChh7tYx IRtxvcd0j8mhyW4bweF1KMnLRI8ue7XPiUyCQSz3wFcooX0BnD3HeNUUlURBoMbBXpv4 xM+humckHMXz2B44fPB/pMb8/ETUVmk/q0zSVOUpR6JIGIbZ1pOi/ans6CEqpPuuej4L djwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ARSomLLB; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id u66-20020a4a2145000000b004a4a373b6c0si6341711oou.27.2023.01.25.09.32.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jan 2023 09:32:20 -0800 (PST) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ARSomLLB; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 4B0CC856CE; Wed, 25 Jan 2023 18:32:15 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="ARSomLLB"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E01648551E; Wed, 25 Jan 2023 18:32:05 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x429.google.com (mail-wr1-x429.google.com [IPv6:2a00:1450:4864:20::429]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 8791D85480 for ; Wed, 25 Jan 2023 18:31:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=loic.poulain@linaro.org Received: by mail-wr1-x429.google.com with SMTP id q10so865008wrm.4 for ; Wed, 25 Jan 2023 09:31:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lykCEayJRiZG1+5JbNN5UQvX8FOES4udJby3A5OW1Bw=; b=ARSomLLBBddujUq1O9hMU2kIkDN3+VltPzXFPhWgp/CQ8TAOkPbTG4nDcV8UvtMoCK kFJduq1IlcXlH3rf0tZmXjpuUUoZlBHTafQCoj3LeapMJIhtAYWLSr/6GsdV4PNHO76P q8lcVxD8rd8570N7nFnNhraxy6zOO0dkU/BGiFbtq/Crdv0LqVU2lWYmKfRd8DFmrAV+ 96iPdE41nDCdCt+p1hr9vXwK+LnpaT2TKv12M5MYYckLyevhvBnIRQ7WMIRspXuKp/op xAZGJlFIEXmypiA3UCpDJgyAqRnYxsc/O/CKi0AP6oBHsgi8MYf4R98VPot8IXspVjj4 tNAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lykCEayJRiZG1+5JbNN5UQvX8FOES4udJby3A5OW1Bw=; b=gBPWWfSZochNYffffXD1L6jhcw+IvTUuA1TmtEy1qZV09eYwm94uqX6cpLCaUlpa5v Lu0cNYxArJa0hmq6KtHAP548FihTKbRx1874GAVuIgP/gBl9iq45g0G4cFrvUrS3PtWk DINWQ8nCEhqgjwRzXwlLh98u051Fhj2dGysBZhJOtCOg69elAjr8vEN3AkXS10Wq+/7S 03r+9fBvVw0r28xCFJZhLmgzRu3eKhxa8lm9EiQhAARNF8CLZ2kplar6wP9bllPz5hKF T7Z+LDuAWU/kK3+WfFfHKDhokYyRLP6ERINVDPUp9NzLAt/j9LvnS2E7sJ6sWOE+Hqs1 JYng== X-Gm-Message-State: AFqh2kqIuuIL3Grx8FuC6ir75I0dW6WbEZqrwZ1HLw4G5YU03e37JS87 nHyuMh+7t6BGHBAZZ/WZU4TaAQ== X-Received: by 2002:adf:dc41:0:b0:2be:5c3b:9d28 with SMTP id m1-20020adfdc41000000b002be5c3b9d28mr18324718wrj.67.1674667915921; Wed, 25 Jan 2023 09:31:55 -0800 (PST) Received: from loic-ThinkPad-T470p.. ([2a01:e0a:82c:5f0:bda1:2bc4:dac2:c66f]) by smtp.gmail.com with ESMTPSA id u36-20020a05600c4d2400b003daf681d05dsm2421533wmp.26.2023.01.25.09.31.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jan 2023 09:31:55 -0800 (PST) From: Loic Poulain To: peng.fan@nxp.com, sjg@chromium.org, jh80.chung@samsung.com Cc: u-boot@lists.denx.de, Loic Poulain Subject: [PATCH 2/2] mmc: erase: Use TRIM erase when available Date: Wed, 25 Jan 2023 18:31:47 +0100 Message-Id: <20230125173147.477383-2-loic.poulain@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230125173147.477383-1-loic.poulain@linaro.org> References: <20230125173147.477383-1-loic.poulain@linaro.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean The default erase command applies on erase group unit, and simply round down to erase group size. When the start block is not aligned to erase group size (e.g. erasing partition) it causes unwanted erasing of the previous blocks, part of the same erase group (e.g. owned by other logical partition, or by the partition table itself). To prevent this issue, a simple solution is to use TRIM as argument of the Erase command, which is usually supported with eMMC > 4.0, and allow to apply erase operation to write blocks instead of erase group Signed-off-by: Loic Poulain --- drivers/mmc/mmc_write.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/drivers/mmc/mmc_write.c b/drivers/mmc/mmc_write.c index 5b7aeeb012..a6f93380dd 100644 --- a/drivers/mmc/mmc_write.c +++ b/drivers/mmc/mmc_write.c @@ -15,7 +15,7 @@ #include #include "mmc_private.h" -static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) +static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt, u32 args) { struct mmc_cmd cmd; ulong end; @@ -52,7 +52,7 @@ static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt) goto err_out; cmd.cmdidx = MMC_CMD_ERASE; - cmd.cmdarg = MMC_ERASE_ARG; + cmd.cmdarg = args ? args : MMC_ERASE_ARG; cmd.resp_type = MMC_RSP_R1b; err = mmc_send_cmd(mmc, &cmd, NULL); @@ -77,7 +77,7 @@ ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt) #endif int dev_num = block_dev->devnum; int err = 0; - u32 start_rem, blkcnt_rem; + u32 start_rem, blkcnt_rem, erase_args = 0; struct mmc *mmc = find_mmc_device(dev_num); lbaint_t blk = 0, blk_r = 0; int timeout_ms = 1000; @@ -97,13 +97,25 @@ ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt) */ err = div_u64_rem(start, mmc->erase_grp_size, &start_rem); err = div_u64_rem(blkcnt, mmc->erase_grp_size, &blkcnt_rem); - if (start_rem || blkcnt_rem) - printf("\n\nCaution! Your devices Erase group is 0x%x\n" - "The erase range would be change to " - "0x" LBAF "~0x" LBAF "\n\n", - mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), - ((start + blkcnt + mmc->erase_grp_size - 1) - & ~(mmc->erase_grp_size - 1)) - 1); + if (start_rem || blkcnt_rem) { + if (mmc->can_trim) { + /* Trim function applies the erase operation to write + * blocks instead of erase groups. + */ + erase_args = MMC_TRIM_ARG; + } else { + /* The card ignores all LSB's below the erase group + * size, rounding down the addess to a erase group + * boundary. + */ + printf("\n\nCaution! Your devices Erase group is 0x%x\n" + "The erase range would be change to " + "0x" LBAF "~0x" LBAF "\n\n", + mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1), + ((start + blkcnt + mmc->erase_grp_size - 1) + & ~(mmc->erase_grp_size - 1)) - 1); + } + } while (blk < blkcnt) { if (IS_SD(mmc) && mmc->ssr.au) { @@ -113,7 +125,7 @@ ulong mmc_berase(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt) blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ? mmc->erase_grp_size : (blkcnt - blk); } - err = mmc_erase_t(mmc, start + blk, blk_r); + err = mmc_erase_t(mmc, start + blk, blk_r, erase_args); if (err) break;