Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp814060pxj; Thu, 20 May 2021 23:06:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzwtaWIKZVABTvzb7gIy7CRl6vBWKZIGGhqeeIg/CbuS1LBwsl2nRnZc/F73nlrBRJ3WkfV X-Received: by 2002:a17:906:f01:: with SMTP id z1mr8357336eji.535.1621577206488; Thu, 20 May 2021 23:06:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621577206; cv=none; d=google.com; s=arc-20160816; b=ucLcoBvb+Wai4JIAxBJUDD0DCphDkZBN+jiRzgGFdKF4wqOZsDPCISeKx7dGwWrD+/ Y9PXrIht8bCReyTOPhYKgMiIry7eP7z3wDRmhB4gAZtpeMfafUW3/LOPlW4L61BGf+TK OmHovgMyExpZxAlvThoYmCSIuflxqPF29mFELcEb/98ePbu9KulWTVR2yC/MDlq8fZjr U332SaK/gfXeFDncxAQS0cycjXF599uju9nuFr4Kd3A70jJuhvcFvkaDb7RwE23iKUEq QQBZUFM6LtqrQynEZ+Rk4vQN1765MlsDVt0QNNX3s+Nh84lvgSjal3iQQb4qJzSrjNGH ZLng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:user-agent:references:in-reply-to :subject:cc:to:from:date:content-transfer-encoding:mime-version :dkim-signature; bh=4mZw769Li8RrpESRx7xXi26AWpGToFQSlDv5uLSxVFM=; b=m6ZOh3wLWpLRmu9REsJtCGk1ac6bfAhJzemBW8Qp0EH6E+cKDuZABhRCTeCWcD1K3d grZAHSzT6hxzaNIcy3b7jWseSSS9BPhlI+muuW1NU0pPm2C/NsVXX0Q/N32bWmOtDfPV piGpWSjoFpx4i4kOpBXCLqyg3DJYRs2a2kpBTCQaor0E+fkUzN55hn+jO+Iq5hmHU0cH +xvhOaD5fVD1boSvVHcsB95864oCuucGv1f7MzOTqmH9Hzt1ulQ3SDCQNhEIrFWyBiOm 2DqjhgCCC6eMPLN6WqGPyK1cpzGaPlAWguXKNAZ1j59b8/2yvaCU/gcKOMPeIHoL84PN FiaQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@walle.cc header.s=mail2016061301 header.b=YVH55aXL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l3si5675636ejd.313.2021.05.20.23.06.23; Thu, 20 May 2021 23:06:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@walle.cc header.s=mail2016061301 header.b=YVH55aXL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242827AbhETPXI (ORCPT + 99 others); Thu, 20 May 2021 11:23:08 -0400 Received: from ssl.serverraum.org ([176.9.125.105]:40617 "EHLO ssl.serverraum.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235472AbhETPW5 (ORCPT ); Thu, 20 May 2021 11:22:57 -0400 Received: from ssl.serverraum.org (web.serverraum.org [172.16.0.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ssl.serverraum.org (Postfix) with ESMTPSA id 445162224A; Thu, 20 May 2021 17:21:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=walle.cc; s=mail2016061301; t=1621524094; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4mZw769Li8RrpESRx7xXi26AWpGToFQSlDv5uLSxVFM=; b=YVH55aXL69KVBr8AcSFoOJFg7ZZ+rkD3sy6OEZEzRbixvcBB4jlK1QJsFfgkM8XyBKHgTC cInnYQEtKLnVrRMgVNAW0ObrDII/MUUZDytqJHjDyceirt+yCfJ6r1nr7Dk0jUtHImjvrh VwdLapQiaYOUZHPPm5gHdoPHbIObdK4= MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Thu, 20 May 2021 17:21:34 +0200 From: Michael Walle To: Pratyush Yadav Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Tudor Ambarus , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra Subject: Re: [PATCH v2] mtd: spi-nor: implement OTP erase for Winbond and similar flashes In-Reply-To: <20210520134940.fpyt52mwpdotrp4a@ti.com> References: <20210510202056.30000-1-michael@walle.cc> <20210520122256.fhkzpqmu7nxwjoqt@ti.com> <20a7e9725d54c9566ca06c062c15f015@walle.cc> <20210520134940.fpyt52mwpdotrp4a@ti.com> User-Agent: Roundcube Webmail/1.4.11 Message-ID: X-Sender: michael@walle.cc Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Am 2021-05-20 15:49, schrieb Pratyush Yadav: > On 20/05/21 03:20PM, Michael Walle wrote: >> Am 2021-05-20 14:22, schrieb Pratyush Yadav: >> > On 10/05/21 10:20PM, Michael Walle wrote: .. >> > > +int spi_nor_otp_erase_secr(struct spi_nor *nor, loff_t addr) >> > > +{ >> > > + int ret; >> > > + >> > > + ret = spi_nor_write_enable(nor); >> > > + if (ret) >> > > + return ret; >> > > + >> > > + if (nor->spimem) { >> > > + struct spi_mem_op op = >> > > + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_ESECR, 0), >> > > + SPI_MEM_OP_ADDR(3, addr, 0), >> > >> > Only 3 address bytes needed? Can the OTP region ever require 4 byte >> > addressing? For example, say the flash is switched to 4 byte addressing >> > for the main region. Would it still expect 3 byte addressing for OTP >> > ops? >> >> It seems you're right. Looking at larger flashes there are sometimes >> 4 bytes. See for example ch 8.2.44 >> >> https://www.winbond.com/resource-files/w25m512jwxiq%20spi%20rev%20c%2012242018.pdf >> >> Thus it seems it should be fixed for the programming and reading, >> too. Unfortunately, I cannot test any of this. > > I don't think any such flash is supported currently, right? So in that > case we won't at least introduce any new regressions when making this > untested change. Whenever someone adds support for one of these > flashes, > we can ask them to test this as well. yep. .. >> > > +static int spi_nor_mtd_otp_lock_erase(struct mtd_info *mtd, loff_t >> > > from, >> > >> > spi_nor_mtd_otp_lock_or_erase()? Or would it make it too long? >> >> I'm fine with both, its just that the read/write doesn't have an >> "or" neither ;) >> >> > Anyway, maybe I am bikeshedding too much, but I don't like that you >> > combine two completely independent operations into the same function >> > because they have some common parts. You should make them two separate >> > functions and see how many of the common parts can be split into >> > independent subroutines. >> >> Given that the whole boilerplate before and after the erase/lock is >> exactly the same, even the while loop is the same, I don't see how >> it can easily be split. Well, you could rename the function to some >> generic spi_nor_mtd_walk() - which would imply it might also be >> used for read/write, which is not true - and provide a callback >> function. But I don't see how this is would make it easier to read. >> And this is just an implemention local to this module. > > My suggestion was to make two copies of the same code, and then see if > you can consolidate some in a clean subroutine. If that is not > possible, > then you can just leave the code duplicated in two places. It is not > that much duplication so it should be fine IMO. > > But I won't press too much on this point. I will leave it to your > judgement on what works better. Just want to make sure you understand > my > point completely. I get your point. But I really don't like the code duplication. >> > > + size_t len, bool is_erase) >> > > { >> > > struct spi_nor *nor = mtd_to_spi_nor(mtd); >> > > const struct spi_nor_otp_ops *ops = nor->params->otp.ops; >> > > const size_t rlen = spi_nor_otp_region_len(nor); >> > > unsigned int region; >> > > + loff_t rstart; >> > > int ret; >> > > >> > > if (from < 0 || (from + len) > spi_nor_otp_size(nor)) >> > > @@ -337,7 +382,13 @@ static int spi_nor_mtd_otp_lock(struct mtd_info >> > > *mtd, loff_t from, size_t len) >> > > >> > > while (len) { >> > > region = spi_nor_otp_offset_to_region(nor, from); >> > > - ret = ops->lock(nor, region); >> > > + >> > > + if (is_erase) { >> > > + rstart = spi_nor_otp_region_start(nor, region); >> > > + ret = ops->erase(nor, rstart); >> > >> > This further highlights my point. There are subtle differences between >> > erase and lock and having them in the same function might not be the >> > best idea. Maybe the argument for the locking is wrong. Future will tell. The start address of a region and the number of a region is actually equivalent. So maybe ->lock should also take the start address. But then you'd go from address -> region -> address -> region. At the moment its modelled after how winbond and macronix flashes implement these ops. See here for an old version of the support for macronix(-like) flashes: https://lore.kernel.org/linux-mtd/20200911222634.31804-4-michael@walle.cc/ -michael