From patchwork Tue Mar 15 18:12:30 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyrille Pitchen X-Patchwork-Id: 597767 X-Patchwork-Delegate: jagannadh.teki@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 3qPjh41bFjz9sDC for ; Wed, 16 Mar 2016 05:24:36 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id CFBE7A7774; Tue, 15 Mar 2016 19:20:44 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZqtRuDBvuwSx; Tue, 15 Mar 2016 19:20:44 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 221AAA76A7; Tue, 15 Mar 2016 19:19:53 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 3D191A7518 for ; Tue, 15 Mar 2016 19:17:58 +0100 (CET) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id T5S6sVq6BslY for ; Tue, 15 Mar 2016 19:17:58 +0100 (CET) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from eusmtp01.atmel.com (eusmtp01.atmel.com [212.144.249.243]) by theia.denx.de (Postfix) with ESMTPS id 08E9DA745C for ; Tue, 15 Mar 2016 19:17:57 +0100 (CET) Received: from tenerife.corp.atmel.com (10.161.101.13) by eusmtp01.atmel.com (10.161.101.31) with Microsoft SMTP Server id 14.3.235.1; Tue, 15 Mar 2016 19:12:28 +0100 From: Cyrille Pitchen To: Date: Tue, 15 Mar 2016 19:12:30 +0100 Message-ID: <4c8dc93194d30d39127537d3ed57a22c71f24a34.1458063638.git.cyrille.pitchen@atmel.com> X-Mailer: git-send-email 1.8.2.2 In-Reply-To: References: MIME-Version: 1.0 X-Mailman-Approved-At: Tue, 15 Mar 2016 19:18:56 +0100 Cc: marex@denx.de, u-boot@lists.denx.de, Cyrille Pitchen Subject: [U-Boot] [PATCH 08/18] sf: share read generic algorithm X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 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" This patch splits the generic algorithm to read data from the actual implementation which is specific to each UCLASS_SPI_FLASH driver. For now, the sf_probe.c driver is the only instance of this driver class using this generic algorithm but other driver instances are to come. This patch will ease their implementation. Please note that the sf_dataflash.c driver has its own implementation of the .read hook (of the struct dm_spi_flash_ops) which is not compatible with this generic algorithm. This is why we can't simply change the prototype of .read hook and create a spi_flash_read() wrapper. Signed-off-by: Cyrille Pitchen --- drivers/mtd/spi/sf_internal.h | 4 ++++ drivers/mtd/spi/spi_flash.c | 48 ++++++++++++++++++++++++++++--------------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/drivers/mtd/spi/sf_internal.h b/drivers/mtd/spi/sf_internal.h index 592c23cd4ce1..569863a31684 100644 --- a/drivers/mtd/spi/sf_internal.h +++ b/drivers/mtd/spi/sf_internal.h @@ -223,6 +223,10 @@ int spi_flash_write_alg(struct spi_flash *flash, u32 offset, size_t len, int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset, size_t len, void *data); +typedef int (*spi_flash_read_fn)(struct spi_flash *, u32, size_t, void *); +int spi_flash_read_alg(struct spi_flash *flash, u32 offset, size_t len, + void *data, spi_flash_read_fn read_fn); + #ifdef CONFIG_SPI_FLASH_MTD int spi_flash_mtd_register(struct spi_flash *flash); void spi_flash_mtd_unregister(void); diff --git a/drivers/mtd/spi/spi_flash.c b/drivers/mtd/spi/spi_flash.c index 3c8224c2b084..4be99ea0b424 100644 --- a/drivers/mtd/spi/spi_flash.c +++ b/drivers/mtd/spi/spi_flash.c @@ -473,11 +473,10 @@ void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len) memcpy(data, offset, len); } -int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset, - size_t len, void *data) +int spi_flash_read_alg(struct spi_flash *flash, u32 offset, size_t len, + void *data, spi_flash_read_fn read_fn) { struct spi_slave *spi = flash->spi; - u8 *cmd, cmdsz; u32 remain_len, read_len, read_addr; int bank_sel = 0; int ret = -1; @@ -496,15 +495,6 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset, goto release; } - cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte; - cmd = calloc(1, cmdsz); - if (!cmd) { - debug("SF: Failed to allocate cmd\n"); - ret = -ENOMEM; - goto release; - } - - cmd[0] = flash->read_cmd; while (len) { read_addr = offset; @@ -525,9 +515,7 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset, else read_len = remain_len; - spi_flash_addr(read_addr, cmd); - - ret = spi_flash_cmd_read(spi, cmd, cmdsz, data, read_len); + ret = read_fn(flash, read_addr, read_len, data); if (ret < 0) { debug("SF: read failed\n"); break; @@ -538,14 +526,40 @@ int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset, data += read_len; } - free(cmd); - release: spi_release_bus(spi); return ret; } +static int spi_flash_read_impl(struct spi_flash *flash, u32 offset, + size_t len, void *buf) +{ + struct spi_slave *spi = flash->spi; + u8 *cmd; + size_t cmdsz; + int ret; + + cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte; + cmd = calloc(1, cmdsz); + if (!cmd) { + debug("SF: Failed to allocate cmd\n"); + return -ENOMEM; + } + + cmd[0] = flash->read_cmd; + spi_flash_addr(offset, cmd); + ret = spi_flash_cmd_read(spi, cmd, cmdsz, buf, len); + free(cmd); + return ret; +} + +int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset, + size_t len, void *buf) +{ + return spi_flash_read_alg(flash, offset, len, buf, spi_flash_read_impl); +} + #ifdef CONFIG_SPI_FLASH_SST static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf) {