From patchwork Tue Feb 18 21:21:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergei Shtylyov X-Patchwork-Id: 1240373 X-Patchwork-Delegate: tudor.ambarus@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=cogentembedded.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20170209 header.b=FyL/t6xu; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=cogentembedded-com.20150623.gappssmtp.com header.i=@cogentembedded-com.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=Gs0twGbO; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48MYgn2WGwz9sRY for ; Wed, 19 Feb 2020 08:21:25 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date: Message-ID:References:To:From:Subject:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=BnHmDoVt9xA0eHctbGBGhR7qdbZiFtxE+bMw770lspU=; b=FyL/t6xuM9H1po5cpDFV9SEMh4 XoWwoLO6bfaN1DvoJio1fDsidtn1cy2Ya9EZEtMM/qpMwGGmDnG9IpcDLpCIGsAFO7k6cByOl0J6L DW6nEHAbBZT3jUyUW+WJKfgGasHUlfapSekBbxBYYB4IJ9HU8dkulxxLNI4HMG+Mky9DT+a2tX9Qr mOjaY0xbtm3tGEtFngU0iQO1NYoNSDXkT7ga0WxiTG4asaY9cIskIbOqaZpuIXv81Ngk7q2BKzYMK ulk/H7gy49Sa8Qj2ToTFYZF180ZPFn/KEmA60jUcjtp+DycZIH7Gs0KT4jbrGvqnijVleJdY0sE3h rNSnEhag==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1j4AIm-0003r5-Q7; Tue, 18 Feb 2020 21:21:08 +0000 Received: from mail-lf1-x143.google.com ([2a00:1450:4864:20::143]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1j4AIk-0003qe-5v for linux-mtd@lists.infradead.org; Tue, 18 Feb 2020 21:21:07 +0000 Received: by mail-lf1-x143.google.com with SMTP id f24so15620493lfh.3 for ; Tue, 18 Feb 2020 13:21:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cogentembedded-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:references:organization:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=QG5Z9bBu26B32PBFqYv2CDhlgCk/4uvdzhO/Cbs69do=; b=Gs0twGbOhahDrWWclDuwK9dSTGNQiPhdX+JKBUwHfaAPoSmWABkLFuYHAU9d0/i1uS oPT39S00mfjT6gEegqEaH1itkhfgr8+pQYCA7mV82kzACsFsOscUnALHuRmmkvk2V6w8 xNI4Pjf5P6ippP07+sdRpRvwBWDDqy4f5XRX46sNWIAMIj3HJ53dIWpeXB4+2tkYvL2R Nc6hHarpW1giGH3UMKT71EeB99VS2XUeJ91pKOl532TDRb3U6lDNh1IZ44aw4VJhXaSP fyWU8a0G3danfVFWbcYRdFjZKQaQT6Z37AgwBsew+q9qJ4dtjdQG3SS1ZwDdSZ0KtsRB 88JA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:references:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=QG5Z9bBu26B32PBFqYv2CDhlgCk/4uvdzhO/Cbs69do=; b=ElX/RCFWAGFwYAZ7YT0HrEFZGk3LC9tQ+4yJDul5rcHbjcpASeCVGfbp2VAabuhdi6 sx33R9XsFLyCysh/qeBMSC//+xlBHHgyVqOixNEmhy3d5PPcLwURmTLbEjPx46SGh/Ns MsYnhZ6mstQSEFXyQy1pgPWXAKsJCmzx40x97bXq/7ckbjS4o+zxHADLNomsdLN/eRU/ emxtHGEG/CwSEw8XxePcqtS6nJn8JmD4R3wDVdT3rVYN+QTSoBYaBD2jreDhi++KYrDj 3zjqUOtMrgNFuEVJEVu6wcXApFqZdfd30AbruiHb2sWkAs5gxHrgx+rOmA5wFsW1VQQL vdPQ== X-Gm-Message-State: APjAAAXzu9URsW9CEiH7PqlZlu4q3uUNn8u/yvLneceqmB9Md0dw8Gne 0G8YRXA9Eky/hjIFfzqunvbWfLsur+k= X-Google-Smtp-Source: APXvYqx9DLUmpHTD+iBAWOHz7sJX9+BekIHYeU1L4PzRMyro7TG5sIvypcOfVJeMLjCjypTWP3pa1w== X-Received: by 2002:a19:f00d:: with SMTP id p13mr11417923lfc.37.1582060862438; Tue, 18 Feb 2020 13:21:02 -0800 (PST) Received: from wasted.cogentembedded.com ([2a00:1fa0:4291:80f4:9357:4c36:a787:cdd4]) by smtp.gmail.com with ESMTPSA id l64sm2911093lfd.30.2020.02.18.13.21.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Feb 2020 13:21:01 -0800 (PST) Subject: [PATCH v5 1/2] mtd: spi-nor: split spi_nor_spimem_xfer_data() From: Sergei Shtylyov To: Tudor Ambarus , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , linux-mtd@lists.infradead.org References: <707c02a5-fc7b-851e-6f62-6e295d2fb40a@cogentembedded.com> Organization: Cogent Embedded Message-ID: <9c862aa2-04fb-f9d5-4d15-1938a8c0e4cb@cogentembedded.com> Date: Wed, 19 Feb 2020 00:21:00 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <707c02a5-fc7b-851e-6f62-6e295d2fb40a@cogentembedded.com> Content-Language: en-MW X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200218_132106_226335_09F0DE06 X-CRM114-Status: GOOD ( 21.09 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.3 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:143 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org spi_nor_spimem_xfer_data() being a helper function for the data reads/ writes contains 3 fragments that depend on the data direction; and I'm going to add another one to call the SPI dirmap API... I think this function should be split so that the common fragments are put into 2 functions, spi_nor_spimem_bounce() and spi_nor_spimem_exec_op() called from spi_nor_spimem_{read|write}_data(), and the data direction dependent bits moved back into those read/write functions -- that way we would be able to avoid *goto*s otherwise needed in the next patch adding the SPI dirmap support... Signed-off-by: Sergei Shtylyov --- Changes in version 5: - removed the 'usebouncebuf' local variable from spi_nor_spimem_write_data(); - refreshed the patch. Changes in version 4: - new patch. drivers/mtd/spi-nor/spi-nor.c | 89 +++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 40 deletions(-) Index: linux/drivers/mtd/spi-nor/spi-nor.c =================================================================== --- linux.orig/drivers/mtd/spi-nor/spi-nor.c +++ linux/drivers/mtd/spi-nor/spi-nor.c @@ -246,55 +246,45 @@ struct flash_info { #define JEDEC_MFR(info) ((info)->id[0]) /** - * spi_nor_spimem_xfer_data() - helper function to read/write data to - * flash's memory region + * spi_nor_spimem_bounce() - check if a bounce buffer is needed for the data + * transfer * @nor: pointer to 'struct spi_nor' * @op: pointer to 'struct spi_mem_op' template for transfer * - * Return: number of bytes transferred on success, -errno otherwise + * If we have to use the bounce buffer, the data field in @op will be updated. + * + * Return: true if the bounce buffer is needed, false if not */ -static ssize_t spi_nor_spimem_xfer_data(struct spi_nor *nor, - struct spi_mem_op *op) +static bool spi_nor_spimem_bounce(struct spi_nor *nor, struct spi_mem_op *op) { - bool usebouncebuf = false; - void *rdbuf = NULL; - const void *buf; - int ret; - - if (op->data.dir == SPI_MEM_DATA_IN) - buf = op->data.buf.in; - else - buf = op->data.buf.out; - - if (object_is_on_stack(buf) || !virt_addr_valid(buf)) - usebouncebuf = true; - - if (usebouncebuf) { + /* op->data.buf.in occupies the same memory as op->data.buf.out */ + if (object_is_on_stack(op->data.buf.in) || + !virt_addr_valid(op->data.buf.in)) { if (op->data.nbytes > nor->bouncebuf_size) op->data.nbytes = nor->bouncebuf_size; - - if (op->data.dir == SPI_MEM_DATA_IN) { - rdbuf = op->data.buf.in; - op->data.buf.in = nor->bouncebuf; - } else { - op->data.buf.out = nor->bouncebuf; - memcpy(nor->bouncebuf, buf, - op->data.nbytes); - } + op->data.buf.in = nor->bouncebuf; + return true; } - ret = spi_mem_adjust_op_size(nor->spimem, op); - if (ret) - return ret; - - ret = spi_mem_exec_op(nor->spimem, op); - if (ret) - return ret; + return false; +} - if (usebouncebuf && op->data.dir == SPI_MEM_DATA_IN) - memcpy(rdbuf, nor->bouncebuf, op->data.nbytes); +/** + * spi_nor_spimem_exec_op() - execute a memory operation + * @nor: pointer to 'struct spi_nor' + * @op: pointer to 'struct spi_mem_op' template for transfer + * + * Return: 0 on success, -error otherwise. + */ +static int spi_nor_spimem_exec_op(struct spi_nor *nor, struct spi_mem_op *op) +{ + int error; - return op->data.nbytes; + error = spi_mem_adjust_op_size(nor->spimem, op); + if (error) + return error; + + return spi_mem_exec_op(nor->spimem, op); } /** @@ -315,6 +305,8 @@ static ssize_t spi_nor_spimem_read_data( SPI_MEM_OP_ADDR(nor->addr_width, from, 1), SPI_MEM_OP_DUMMY(nor->read_dummy, 1), SPI_MEM_OP_DATA_IN(len, buf, 1)); + bool usebouncebuf; + int error; /* get transfer protocols. */ op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto); @@ -325,7 +317,16 @@ static ssize_t spi_nor_spimem_read_data( /* convert the dummy cycles to the number of bytes */ op.dummy.nbytes = (nor->read_dummy * op.dummy.buswidth) / 8; - return spi_nor_spimem_xfer_data(nor, &op); + usebouncebuf = spi_nor_spimem_bounce(nor, &op); + + error = spi_nor_spimem_exec_op(nor, &op); + if (error) + return error; + + if (usebouncebuf) + memcpy(buf, op.data.buf.in, op.data.nbytes); + + return op.data.nbytes; } /** @@ -364,6 +365,7 @@ static ssize_t spi_nor_spimem_write_data SPI_MEM_OP_ADDR(nor->addr_width, to, 1), SPI_MEM_OP_NO_DUMMY, SPI_MEM_OP_DATA_OUT(len, buf, 1)); + int error; op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto); op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto); @@ -372,7 +374,14 @@ static ssize_t spi_nor_spimem_write_data if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) op.addr.nbytes = 0; - return spi_nor_spimem_xfer_data(nor, &op); + if (spi_nor_spimem_bounce(nor, &op)) + memcpy(nor->bouncebuf, buf, op.data.nbytes); + + error = spi_nor_spimem_exec_op(nor, &op); + if (error) + return error; + + return op.data.nbytes; } /** From patchwork Tue Feb 18 21:24:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergei Shtylyov X-Patchwork-Id: 1240375 X-Patchwork-Delegate: tudor.ambarus@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=cogentembedded.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20170209 header.b=GYC3oG4r; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=cogentembedded-com.20150623.gappssmtp.com header.i=@cogentembedded-com.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=ZGAm1KJB; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48MYlS01GXz9sRY for ; Wed, 19 Feb 2020 08:24:35 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:Date: Message-ID:References:To:From:Subject:Reply-To:Cc:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=h8d59bnQZ7PB6Q4Pu4IVnwkDWU4QznlGfr5FtqKxd5g=; b=GYC3oG4rbPh/0kdKGTGwcZkU8k jsuzgCqMYTkv1brToYD7FuUwefSrNdO6NhXt1yd6/x6m/wWXBVkIbvdLutYWQ09QrAeDa3W11fUs7 KptGniGz1sCbr0TxyEuIo4iNG4dKKiPdwSW+/sFdySMDiFwYLl2AEoak3iPcue/00UT5WcGB9/Jwi NUMh65bjTb61dRoosvyO5CtfDa8a1oso9zd//WeHFh98mmjHfyAST3JY9J02hO1wLyiq4k/2PDPlj hyw/CjirhujrW7dKWduA/EeDlMzg4WyqC5rfzNcGHGEGM0fbIqvMfooDTOly8oChUWSkVdJdPIE+G 5XqPGYbA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1j4ALq-0004FH-Eq; Tue, 18 Feb 2020 21:24:18 +0000 Received: from mail-lj1-x242.google.com ([2a00:1450:4864:20::242]) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1j4ALn-0004EZ-HO for linux-mtd@lists.infradead.org; Tue, 18 Feb 2020 21:24:17 +0000 Received: by mail-lj1-x242.google.com with SMTP id o15so24691707ljg.6 for ; Tue, 18 Feb 2020 13:24:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cogentembedded-com.20150623.gappssmtp.com; s=20150623; h=subject:from:to:references:organization:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=T7Ke16SIMrbPFRg3soDuJETaTOOUJNMPo6V1rF6BvBY=; b=ZGAm1KJBBIufpXNvoJ24/P2msT7Of554/Vx//dlbmUOJhKIQozAhIEkAzVTN84bca6 thZ8wsoOGidgRQQidlzx/07TDEtwbJV3dl1Xus7tUe1uxz1sc8V+w43peuYRnvSRtKxJ ZxmGp1v3lrBl/qvJCphz48Ufa+D5cSZeHExhsXBF8qmoz+3LZJM2WLuVKYWIGMCKDIuy ExA+mi1hVJCTJ59IE63/GGGUwXvRGP9oxpshDxoSkbSoC/sxza/fkKqg8ZvZbt14OvVi KDS5aEi/Mam5hC+YaZNqxceHdwQF35ykCUJrStEJJPZIkc9+jCgKVlSGF01MGjPbTN1w JtBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:references:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=T7Ke16SIMrbPFRg3soDuJETaTOOUJNMPo6V1rF6BvBY=; b=c2OA5w2QzBmioT0jurmqELu1BGtqDdiDuyi3AWnF83UmStnKsFz0UDDXScZkvPorep hoZ4ddKIy+4UHd0nyLe5bC+t16M2In9OzimQ4GDaGzVwlKg1hdE4ylPnkNnlTsr9XC2b lDSwj9HRJ7q3FkRk+LdS884wNHYUNiLp7wSMy31o7X1Ijr3ZaSyzAudOm/vh/ao3B2+X OmFv9XiRO5AEjKSFYenB53KDJnpqQVu0igrjmzb0R+B5Q19riQOYpu1HVD8ntElsZEgd cGeKPQwbXDkTVfLZFZ7WWBM+6W74+tcCPtZdktryH+DIXk7CxBnEsknBViZ8RtQgCopy uxwA== X-Gm-Message-State: APjAAAVMJhUfnxHwKXHWpKgIf+Sr5AgsjBgjiqrbp/AStBV/eac8xImg zXTphCy7BZfShN5xjy7efkcMjkMggC4= X-Google-Smtp-Source: APXvYqzsvpQWXJTOyHqIuYZ7HdZyIrQ7TQKYKJRalKrv8E7G1MiNPMsuWw6cHTnUQOWkNfiXZBc9Ig== X-Received: by 2002:a2e:910b:: with SMTP id m11mr14149118ljg.213.1582061053175; Tue, 18 Feb 2020 13:24:13 -0800 (PST) Received: from wasted.cogentembedded.com ([2a00:1fa0:4291:80f4:9357:4c36:a787:cdd4]) by smtp.gmail.com with ESMTPSA id j7sm8161ljg.25.2020.02.18.13.24.11 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Feb 2020 13:24:11 -0800 (PST) Subject: [PATCH v5 2/2] mtd: spi-nor: use spi-mem dirmap API From: Sergei Shtylyov To: Tudor Ambarus , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , linux-mtd@lists.infradead.org References: <707c02a5-fc7b-851e-6f62-6e295d2fb40a@cogentembedded.com> Organization: Cogent Embedded Message-ID: Date: Wed, 19 Feb 2020 00:24:10 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <707c02a5-fc7b-851e-6f62-6e295d2fb40a@cogentembedded.com> Content-Language: en-MW X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200218_132415_580625_A2D2EDEB X-CRM114-Status: GOOD ( 19.28 ) X-Spam-Score: 0.0 (/) X-Spam-Report: SpamAssassin version 3.4.3 on bombadil.infradead.org summary: Content analysis details: (0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2a00:1450:4864:20:0:0:0:242 listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Make use of the spi-mem direct mapping API to let advanced controllers optimize read/write operations when they support direct mapping. Based on the original patch by Boris Brezillon . Signed-off-by: Sergei Shtylyov Reviewed-by: Boris Brezillon --- Changes in version 5: - transformed *if*/return* statements into an extra check before the memcpy() call and removed an empty line in spi_nor_spimem_read_data(); - resolved a reject when adding the 'nbytes' local variable and refreshed atop of the previous patch in spi_nor_spimem_write_data(); - removed the unnecessary *if*/return* statements in spi_nor_spimem_write_data(); - added the 'kernel-doc' comment for the 'dirmap' field of the 'struct spi_nor'; - refreshed the patch. Changes in version 4: - moved the spi_mem_dirmap_{read|write}() calls closer to the ending of spi_nor_{read|write}(), adapting to the chnages caused by the new patch splitting spi_nor_spimem_xfer_data()... Changes in version 3: - simplified the way spi_mem_dirmap_{read|write}() are called; - added Boris' tag. Changes in version 2: - moved the spi_mem_dirmap_{read|write}() calls from spi_nor_{read|write}() to spi_nor_spimem_{read|write}_data(). drivers/mtd/spi-nor/spi-nor.c | 94 +++++++++++++++++++++++++++++++++++++----- include/linux/mtd/spi-nor.h | 6 ++ 2 files changed, 90 insertions(+), 10 deletions(-) Index: linux/drivers/mtd/spi-nor/spi-nor.c =================================================================== --- linux.orig/drivers/mtd/spi-nor/spi-nor.c +++ linux/drivers/mtd/spi-nor/spi-nor.c @@ -306,6 +306,7 @@ static ssize_t spi_nor_spimem_read_data( SPI_MEM_OP_DUMMY(nor->read_dummy, 1), SPI_MEM_OP_DATA_IN(len, buf, 1)); bool usebouncebuf; + ssize_t nbytes; int error; /* get transfer protocols. */ @@ -319,14 +320,20 @@ static ssize_t spi_nor_spimem_read_data( usebouncebuf = spi_nor_spimem_bounce(nor, &op); - error = spi_nor_spimem_exec_op(nor, &op); - if (error) - return error; + if (nor->dirmap.rdesc) { + nbytes = spi_mem_dirmap_read(nor->dirmap.rdesc, op.addr.val, + op.data.nbytes, op.data.buf.in); + } else { + error = spi_nor_spimem_exec_op(nor, &op); + if (error) + return error; + nbytes = op.data.nbytes; + } - if (usebouncebuf) - memcpy(buf, op.data.buf.in, op.data.nbytes); + if (usebouncebuf && nbytes > 0) + memcpy(buf, op.data.buf.in, nbytes); - return op.data.nbytes; + return nbytes; } /** @@ -365,6 +372,7 @@ static ssize_t spi_nor_spimem_write_data SPI_MEM_OP_ADDR(nor->addr_width, to, 1), SPI_MEM_OP_NO_DUMMY, SPI_MEM_OP_DATA_OUT(len, buf, 1)); + ssize_t nbytes; int error; op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto); @@ -377,11 +385,17 @@ static ssize_t spi_nor_spimem_write_data if (spi_nor_spimem_bounce(nor, &op)) memcpy(nor->bouncebuf, buf, op.data.nbytes); - error = spi_nor_spimem_exec_op(nor, &op); - if (error) - return error; + if (nor->dirmap.wdesc) { + nbytes = spi_mem_dirmap_write(nor->dirmap.wdesc, op.addr.val, + op.data.nbytes, op.data.buf.out); + } else { + error = spi_nor_spimem_exec_op(nor, &op); + if (error) + return error; + nbytes = op.data.nbytes; + } - return op.data.nbytes; + return nbytes; } /** @@ -5265,6 +5279,58 @@ int spi_nor_scan(struct spi_nor *nor, co } EXPORT_SYMBOL_GPL(spi_nor_scan); +static int spi_nor_create_read_dirmap(struct spi_nor *nor) +{ + struct spi_mem_dirmap_info info = { + .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1), + SPI_MEM_OP_ADDR(nor->addr_width, 0, 1), + SPI_MEM_OP_DUMMY(nor->read_dummy, 1), + SPI_MEM_OP_DATA_IN(0, NULL, 1)), + .offset = 0, + .length = nor->mtd.size, + }; + struct spi_mem_op *op = &info.op_tmpl; + + /* get transfer protocols. */ + op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto); + op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto); + op->dummy.buswidth = op->addr.buswidth; + op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto); + + /* convert the dummy cycles to the number of bytes */ + op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8; + + nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, + &info); + return PTR_ERR_OR_ZERO(nor->dirmap.rdesc); +} + +static int spi_nor_create_write_dirmap(struct spi_nor *nor) +{ + struct spi_mem_dirmap_info info = { + .op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1), + SPI_MEM_OP_ADDR(nor->addr_width, 0, 1), + SPI_MEM_OP_NO_DUMMY, + SPI_MEM_OP_DATA_OUT(0, NULL, 1)), + .offset = 0, + .length = nor->mtd.size, + }; + struct spi_mem_op *op = &info.op_tmpl; + + /* get transfer protocols. */ + op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto); + op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto); + op->dummy.buswidth = op->addr.buswidth; + op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto); + + if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second) + op->addr.nbytes = 0; + + nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem, + &info); + return PTR_ERR_OR_ZERO(nor->dirmap.wdesc); +} + static int spi_nor_probe(struct spi_mem *spimem) { struct spi_device *spi = spimem->spi; @@ -5326,6 +5392,14 @@ static int spi_nor_probe(struct spi_mem return -ENOMEM; } + ret = spi_nor_create_read_dirmap(nor); + if (ret) + return ret; + + ret = spi_nor_create_write_dirmap(nor); + if (ret) + return ret; + return mtd_device_register(&nor->mtd, data ? data->parts : NULL, data ? data->nr_parts : 0); } Index: linux/include/linux/mtd/spi-nor.h =================================================================== --- linux.orig/include/linux/mtd/spi-nor.h +++ linux/include/linux/mtd/spi-nor.h @@ -580,6 +580,7 @@ struct flash_info; * The structure includes legacy flash parameters and * settings that can be overwritten by the spi_nor_fixups * hooks, or dynamically when parsing the SFDP tables. + * @dirmap: pointers to struct spi_mem_dirmap_desc for reads/writes. * @priv: the private data */ struct spi_nor { @@ -606,6 +607,11 @@ struct spi_nor { struct spi_nor_flash_parameter params; + struct { + struct spi_mem_dirmap_desc *rdesc; + struct spi_mem_dirmap_desc *wdesc; + } dirmap; + void *priv; };