Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp2456879pxb; Wed, 9 Feb 2022 20:16:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJyMVfiQ2vWlw9EpictPvARwJBsVhahDMzT4/fmwLtrpBp/cRz7d25ZTdwW/rL1W4S8ghfTK X-Received: by 2002:a05:6a00:134b:: with SMTP id k11mr5737448pfu.33.1644466576529; Wed, 09 Feb 2022 20:16:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644466576; cv=none; d=google.com; s=arc-20160816; b=AZomdmhk6Q738i5kYS+NRmk46pIfClZZk+FrwylRF39UF7quTbaoaXczKsuOyGbEe2 Zs10Avwb7M2Fm/7PcNYeme3HrVdEQ8/7bvZq1TGf+vFihxAaln5dYpmyy4zGSdeWi3OK vWSssOrB0WxzEvxNPugUh45J+1peTooig+vjpf8iP5RMajEAAM8U7yKEXPNjsp0gPtz9 nyBfjeQe65CmF7/4RT90HcLJrcrcriDkV2t6sFIctAi9DoIPthHlXIkojQTNIKO6QgR4 a0SyCxrTcmtdLDImYML2vQdT0ItTO5c388v3cs92bXgprOYKRKVTUgIrMX8WJXZ7Ct0Z ocgA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:dkim-signature; bh=8GyYfENOvGhspZP2mBfPLbP9O1a6FEoBSjXy402IuGU=; b=gM6Xi3U7aOYrzvChu6DSxF7Ah2dBNTtzKgmK3NcKS+iAmuqJukCewNf3UAhsODOHGR RRH/tP3NCfL+AFH3jxGyMd8wQ+jG3LRQCvzM0lUD5xAD5SaZa8OMgjpBHKMDs9EckwJy f2ZSjLNDe48iUH/KirCNZVDvhyW7ciFWDKJ7N2OWBObMZPgbltbP+C5FSWnjUjo0iqek odaEOHT24kyLZoDuTmwZ4B7bla2u4THYfwzTH/rYIJmckyVB8n9SoijBIGLwItma49T/ mJcB1f0QC89km4yRhO03a3RmYiwTCIdmYa7qhkWSNnSU4O7FFN8nz5SA/l1PU0l/250C jSZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@microchip.com header.s=mchp header.b="quc/E562"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p9si1015242plf.197.2022.02.09.20.16.01; Wed, 09 Feb 2022 20:16:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=fail header.i=@microchip.com header.s=mchp header.b="quc/E562"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=microchip.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230372AbiBJCd4 (ORCPT + 99 others); Wed, 9 Feb 2022 21:33:56 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:46444 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230115AbiBJCds (ORCPT ); Wed, 9 Feb 2022 21:33:48 -0500 Received: from esa.microchip.iphmx.com (esa.microchip.iphmx.com [68.232.153.233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0523237D3 for ; Wed, 9 Feb 2022 18:33:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=microchip.com; i=@microchip.com; q=dns/txt; s=mchp; t=1644460430; x=1675996430; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=OJcQJqyhbuCp95qSOXqEnvvtLWnuWeuQjHIdq0m2sLE=; b=quc/E5625NBnHZVpkN7w6bD/iuExOjMi9qyTn0V9k4hL4zTk75tO28f2 5YZS0bcp5AkILWt+h1X/0Q5PV/GXCK7up9qfEYqq6xID4JP8XKEte7wme 1eZS5JSgAA73sr6Yvxiub5GbPJR88EA7RcDyInoamctFOoPUmdns8T/KP vuK6tOecN0jwgrOtLHrNjOKGRAHdLs4Ymb8Aze91Y2J7q79yimNvZDqjH XagyjmIKyq+/11UTh8vvpWqNLltbBF136Pc7Dcu7XY5ieJjQXty4bj7Sq MZbs6onf93NEaVKpMml3R8ZuJVg8KVsM+hYaiHvnGUOWJ+FlXc/iCswHO w==; IronPort-SDR: 6n6mGUXPPlrhnZ91i9Q0PH0YBYzRYRyIJ/ElJUiDTL1XUPa/uUlgKqkqdgEwls+gG87Wu/+wZ8 cs8781Yz5fX0ilKa91r6r2SMrn5KXqlVE+YYXT3tkyOh3mATsmvpbCIQmpBULhlAlVe7I6i8BS /wsSVE3Vb7j0rrG6IfbUw9+1MxNVY4VlTd/WBCOGdgbr9mcchi/CtjPzQ6huhxectShnNx+b+a 4+OwegeTs7v9rt/hT+0cCVVr1x1uCyk9JFr6uEefssWMRBVo3N2T9NJiilFkN4bGHEFqwxIPA3 3wvTO12a6y+ItTFvONwPwjAj X-IronPort-AV: E=Sophos;i="5.88,357,1635231600"; d="scan'208";a="161726856" Received: from smtpout.microchip.com (HELO email.microchip.com) ([198.175.253.82]) by esa1.microchip.iphmx.com with ESMTP/TLS/AES256-SHA256; 09 Feb 2022 19:33:50 -0700 Received: from chn-vm-ex03.mchp-main.com (10.10.85.151) by chn-vm-ex04.mchp-main.com (10.10.85.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.17; Wed, 9 Feb 2022 19:33:49 -0700 Received: from ROB-ULT-M18064N.mchp-main.com (10.10.115.15) by chn-vm-ex03.mchp-main.com (10.10.85.151) with Microsoft SMTP Server id 15.1.2375.17 via Frontend Transport; Wed, 9 Feb 2022 19:33:45 -0700 From: Tudor Ambarus To: , , CC: , , , , , , , , , Tudor Ambarus Subject: [PATCH 2/3] mtd: spi-nor: micron-st: Rework spi_nor_micron_octal_dtr_enable() Date: Thu, 10 Feb 2022 04:33:33 +0200 Message-ID: <20220210023334.408926-3-tudor.ambarus@microchip.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220210023334.408926-1-tudor.ambarus@microchip.com> References: <20220210023334.408926-1-tudor.ambarus@microchip.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_PASS,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Introduce template operation to remove code duplication. Split spi_nor_micron_octal_dtr_enable() in spi_nor_micron_octal_dtr_en() and spi_nor_micron_octal_dtr_dis() as it no longer made sense to try to keep everything alltogether: too many "if (enable)" throughout the code, which made the code difficult to follow. Signed-off-by: Tudor Ambarus --- drivers/mtd/spi-nor/micron-st.c | 105 +++++++++++++++++--------------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/drivers/mtd/spi-nor/micron-st.c b/drivers/mtd/spi-nor/micron-st.c index 7f66b5943ceb..013aa6a52737 100644 --- a/drivers/mtd/spi-nor/micron-st.c +++ b/drivers/mtd/spi-nor/micron-st.c @@ -17,73 +17,72 @@ #define SPINOR_MT_OCT_DTR 0xe7 /* Enable Octal DTR. */ #define SPINOR_MT_EXSPI 0xff /* Enable Extended SPI (default) */ -static int spi_nor_micron_octal_dtr_enable(struct spi_nor *nor, bool enable) +/* Micron ST SPI NOR flash operations. */ +#define SPI_NOR_MICRON_WR_ANY_REG_OP(naddr, addr, ndata, buf) \ + SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 0), \ + SPI_MEM_OP_ADDR(naddr, addr, 0), \ + SPI_MEM_OP_NO_DUMMY, \ + SPI_MEM_OP_DATA_OUT(ndata, buf, 0)) + +static int spi_nor_micron_octal_dtr_en(struct spi_nor *nor) { struct spi_mem_op op; u8 *buf = nor->bouncebuf; int ret; - if (enable) { - /* Use 20 dummy cycles for memory array reads. */ - ret = spi_nor_write_enable(nor); - if (ret) - return ret; - - *buf = 20; - op = (struct spi_mem_op) - SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 1), - SPI_MEM_OP_ADDR(3, SPINOR_REG_MT_CFR1V, 1), - SPI_MEM_OP_NO_DUMMY, - SPI_MEM_OP_DATA_OUT(1, buf, 1)); - - ret = spi_mem_exec_op(nor->spimem, &op); - if (ret) - return ret; + /* Use 20 dummy cycles for memory array reads. */ + *buf = 20; + op = (struct spi_mem_op) + SPI_NOR_MICRON_WR_ANY_REG_OP(3, SPINOR_REG_MT_CFR1V, 1, buf); + ret = spi_nor_write_reg(nor, &op, nor->reg_proto); + if (ret) + return ret; + ret = spi_nor_wait_till_ready(nor); + if (ret) + return ret; - ret = spi_nor_wait_till_ready(nor); - if (ret) - return ret; - } + buf[0] = SPINOR_MT_OCT_DTR; + op = (struct spi_mem_op) + SPI_NOR_MICRON_WR_ANY_REG_OP(3, SPINOR_REG_MT_CFR0V, 1, buf); + ret = spi_nor_write_reg(nor, &op, nor->reg_proto); + if (ret) + return ret; - ret = spi_nor_write_enable(nor); + /* Read flash ID to make sure the switch was successful. */ + ret = spi_nor_read_id(nor, 0, 8, buf, SNOR_PROTO_8_8_8_DTR); if (ret) return ret; - if (enable) { - buf[0] = SPINOR_MT_OCT_DTR; - } else { - /* - * The register is 1-byte wide, but 1-byte transactions are not - * allowed in 8D-8D-8D mode. The next register is the dummy - * cycle configuration register. Since the transaction needs to - * be at least 2 bytes wide, set the next register to its - * default value. This also makes sense because the value was - * changed when enabling 8D-8D-8D mode, it should be reset when - * disabling. - */ - buf[0] = SPINOR_MT_EXSPI; - buf[1] = SPINOR_REG_MT_CFR1V_DEF; - } + if (memcmp(buf, nor->info->id, nor->info->id_len)) + return -EINVAL; - op = (struct spi_mem_op) - SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_MT_WR_ANY_REG, 1), - SPI_MEM_OP_ADDR(enable ? 3 : 4, - SPINOR_REG_MT_CFR0V, 1), - SPI_MEM_OP_NO_DUMMY, - SPI_MEM_OP_DATA_OUT(enable ? 1 : 2, buf, 1)); + return 0; +} - if (!enable) - spi_nor_spimem_setup_op(nor, &op, SNOR_PROTO_8_8_8_DTR); +static int spi_nor_micron_octal_dtr_dis(struct spi_nor *nor) +{ + struct spi_mem_op op; + u8 *buf = nor->bouncebuf; + int ret; - ret = spi_mem_exec_op(nor->spimem, &op); + /* + * The register is 1-byte wide, but 1-byte transactions are not allowed + * in 8D-8D-8D mode. The next register is the dummy cycle configuration + * register. Since the transaction needs to be at least 2 bytes wide, + * set the next register to its default value. This also makes sense + * because the value was changed when enabling 8D-8D-8D mode, it should + * be reset when disabling. + */ + buf[0] = SPINOR_MT_EXSPI; + buf[1] = SPINOR_REG_MT_CFR1V_DEF; + op = (struct spi_mem_op) + SPI_NOR_MICRON_WR_ANY_REG_OP(4, SPINOR_REG_MT_CFR0V, 2, buf); + ret = spi_nor_write_reg(nor, &op, SNOR_PROTO_8_8_8_DTR); if (ret) return ret; /* Read flash ID to make sure the switch was successful. */ - if (enable) - ret = spi_nor_read_id(nor, 0, 8, buf, SNOR_PROTO_8_8_8_DTR); - else - ret = spi_nor_read_id(nor, 0, 0, buf, nor->reg_proto); + ret = spi_nor_read_id(nor, 0, 0, buf, nor->reg_proto); if (ret) return ret; @@ -93,6 +92,12 @@ static int spi_nor_micron_octal_dtr_enable(struct spi_nor *nor, bool enable) return 0; } +static int spi_nor_micron_octal_dtr_enable(struct spi_nor *nor, bool enable) +{ + return enable ? spi_nor_micron_octal_dtr_en(nor) : + spi_nor_micron_octal_dtr_dis(nor); +} + static void mt35xu512aba_default_init(struct spi_nor *nor) { nor->params->octal_dtr_enable = spi_nor_micron_octal_dtr_enable; -- 2.25.1