From patchwork Tue Jun 28 17:38:09 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Frysinger X-Patchwork-Id: 102436 X-Patchwork-Delegate: wd@denx.de 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 45F34B6F62 for ; Wed, 29 Jun 2011 03:39:39 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 0427B28127; Tue, 28 Jun 2011 19:39:07 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de 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 avB+o7bRqdxV; Tue, 28 Jun 2011 19:39:06 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 16A3028128; Tue, 28 Jun 2011 19:38:34 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 8F59E2809C for ; Tue, 28 Jun 2011 19:38:27 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at theia.denx.de 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 4-dxBKNewHI5 for ; Tue, 28 Jun 2011 19:38:24 +0200 (CEST) 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 smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) by theia.denx.de (Postfix) with ESMTPS id 1258A280F2 for ; Tue, 28 Jun 2011 19:38:21 +0200 (CEST) Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id 6AF051BC02A for ; Tue, 28 Jun 2011 17:38:18 +0000 (UTC) From: Mike Frysinger To: u-boot@lists.denx.de Date: Tue, 28 Jun 2011 13:38:09 -0400 Message-Id: <1309282694-27103-5-git-send-email-vapier@gentoo.org> X-Mailer: git-send-email 1.7.5.3 In-Reply-To: <1309282694-27103-1-git-send-email-vapier@gentoo.org> References: <1309282694-27103-1-git-send-email-vapier@gentoo.org> Subject: [U-Boot] [PATCH 4/9] sf: unify write funcs X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.9 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: u-boot-bounces@lists.denx.de Errors-To: u-boot-bounces@lists.denx.de Once we add a new page_size field for write lengths, we can unify the write methods for most of the spi flash drivers. Signed-off-by: Mike Frysinger --- drivers/mtd/spi/eon.c | 66 +------------------------------ drivers/mtd/spi/macronix.c | 66 +------------------------------ drivers/mtd/spi/spansion.c | 66 +------------------------------ drivers/mtd/spi/spi_flash.c | 57 +++++++++++++++++++++++++++ drivers/mtd/spi/spi_flash_internal.h | 8 ++++ drivers/mtd/spi/stmicro.c | 66 +------------------------------ drivers/mtd/spi/winbond.c | 71 +-------------------------------- include/spi_flash.h | 5 ++- 8 files changed, 80 insertions(+), 325 deletions(-) diff --git a/drivers/mtd/spi/eon.c b/drivers/mtd/spi/eon.c index 6826708..036855b 100644 --- a/drivers/mtd/spi/eon.c +++ b/drivers/mtd/spi/eon.c @@ -56,69 +56,6 @@ static const struct eon_spi_flash_params eon_spi_flash_table[] = { }, }; -static int eon_write(struct spi_flash *flash, - u32 offset, size_t len, const void *buf) -{ - struct eon_spi_flash *eon = to_eon_spi_flash(flash); - unsigned long page_addr; - unsigned long byte_addr; - unsigned long page_size; - size_t chunk_len; - size_t actual; - int ret; - u8 cmd[4]; - - page_size = eon->params->page_size; - page_addr = offset / page_size; - byte_addr = offset % page_size; - - ret = spi_claim_bus(flash->spi); - if (ret) { - debug("SF: Unable to claim SPI bus\n"); - return ret; - } - - ret = 0; - for (actual = 0; actual < len; actual += chunk_len) { - chunk_len = min(len - actual, page_size - byte_addr); - - cmd[0] = CMD_EN25Q128_PP; - cmd[1] = page_addr >> 8; - cmd[2] = page_addr; - cmd[3] = byte_addr; - - debug - ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n", - buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); - - ret = spi_flash_cmd_write_enable(flash); - if (ret < 0) { - debug("SF: Enabling Write failed\n"); - break; - } - - ret = spi_flash_cmd_write(flash->spi, cmd, 4, - buf + actual, chunk_len); - if (ret < 0) { - debug("SF: EON Page Program failed\n"); - break; - } - - ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); - if (ret) - break; - - page_addr++; - byte_addr = 0; - } - - debug("SF: EON: Successfully programmed %u bytes @ 0x%x\n", - len, offset); - - spi_release_bus(flash->spi); - return ret; -} - static int eon_erase(struct spi_flash *flash, u32 offset, size_t len) { return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len); @@ -151,9 +88,10 @@ struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode) eon->flash.spi = spi; eon->flash.name = params->name; - eon->flash.write = eon_write; + eon->flash.write = spi_flash_cmd_write_multi; eon->flash.erase = eon_erase; eon->flash.read = spi_flash_cmd_read_fast; + eon->flash.page_size = params->page_size; eon->flash.sector_size = params->page_size * params->pages_per_sector * params->sectors_per_block; eon->flash.size = params->page_size * params->pages_per_sector diff --git a/drivers/mtd/spi/macronix.c b/drivers/mtd/spi/macronix.c index f1c2bbb..cb06476 100644 --- a/drivers/mtd/spi/macronix.c +++ b/drivers/mtd/spi/macronix.c @@ -128,69 +128,6 @@ static const struct macronix_spi_flash_params macronix_spi_flash_table[] = { }, }; -static int macronix_write(struct spi_flash *flash, - u32 offset, size_t len, const void *buf) -{ - struct macronix_spi_flash *mcx = to_macronix_spi_flash(flash); - unsigned long page_addr; - unsigned long byte_addr; - unsigned long page_size; - size_t chunk_len; - size_t actual; - int ret; - u8 cmd[4]; - - page_size = mcx->params->page_size; - page_addr = offset / page_size; - byte_addr = offset % page_size; - - ret = spi_claim_bus(flash->spi); - if (ret) { - debug("SF: Unable to claim SPI bus\n"); - return ret; - } - - ret = 0; - for (actual = 0; actual < len; actual += chunk_len) { - chunk_len = min(len - actual, page_size - byte_addr); - - cmd[0] = CMD_MX25XX_PP; - cmd[1] = page_addr >> 8; - cmd[2] = page_addr; - cmd[3] = byte_addr; - - debug - ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n", - buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); - - ret = spi_flash_cmd_write_enable(flash); - if (ret < 0) { - debug("SF: Enabling Write failed\n"); - break; - } - - ret = spi_flash_cmd_write(flash->spi, cmd, 4, - buf + actual, chunk_len); - if (ret < 0) { - debug("SF: Macronix Page Program failed\n"); - break; - } - - ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); - if (ret) - break; - - page_addr++; - byte_addr = 0; - } - - debug("SF: Macronix: Successfully programmed %u bytes @ 0x%x\n", - len, offset); - - spi_release_bus(flash->spi); - return ret; -} - static int macronix_erase(struct spi_flash *flash, u32 offset, size_t len) { return spi_flash_cmd_erase(flash, CMD_MX25XX_BE, offset, len); @@ -224,9 +161,10 @@ struct spi_flash *spi_flash_probe_macronix(struct spi_slave *spi, u8 *idcode) mcx->flash.spi = spi; mcx->flash.name = params->name; - mcx->flash.write = macronix_write; + mcx->flash.write = spi_flash_cmd_write_multi; mcx->flash.erase = macronix_erase; mcx->flash.read = spi_flash_cmd_read_fast; + mcx->flash.page_size = params->page_size; mcx->flash.sector_size = params->page_size * params->pages_per_sector * params->sectors_per_block; mcx->flash.size = mcx->flash.sector_size * params->nr_blocks; diff --git a/drivers/mtd/spi/spansion.c b/drivers/mtd/spi/spansion.c index 9dbab5d..1ef8c82 100644 --- a/drivers/mtd/spi/spansion.c +++ b/drivers/mtd/spi/spansion.c @@ -142,69 +142,6 @@ static const struct spansion_spi_flash_params spansion_spi_flash_table[] = { }, }; -static int spansion_write(struct spi_flash *flash, - u32 offset, size_t len, const void *buf) -{ - struct spansion_spi_flash *spsn = to_spansion_spi_flash(flash); - unsigned long page_addr; - unsigned long byte_addr; - unsigned long page_size; - size_t chunk_len; - size_t actual; - int ret; - u8 cmd[4]; - - page_size = spsn->params->page_size; - page_addr = offset / page_size; - byte_addr = offset % page_size; - - ret = spi_claim_bus(flash->spi); - if (ret) { - debug("SF: Unable to claim SPI bus\n"); - return ret; - } - - ret = 0; - for (actual = 0; actual < len; actual += chunk_len) { - chunk_len = min(len - actual, page_size - byte_addr); - - cmd[0] = CMD_S25FLXX_PP; - cmd[1] = page_addr >> 8; - cmd[2] = page_addr; - cmd[3] = byte_addr; - - debug - ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n", - buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); - - ret = spi_flash_cmd_write_enable(flash); - if (ret < 0) { - debug("SF: Enabling Write failed\n"); - break; - } - - ret = spi_flash_cmd_write(flash->spi, cmd, 4, - buf + actual, chunk_len); - if (ret < 0) { - debug("SF: SPANSION Page Program failed\n"); - break; - } - - ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); - if (ret) - break; - - page_addr++; - byte_addr = 0; - } - - debug("SF: SPANSION: Successfully programmed %u bytes @ 0x%x\n", - len, offset); - - spi_release_bus(flash->spi); - return ret; -} - static int spansion_erase(struct spi_flash *flash, u32 offset, size_t len) { return spi_flash_cmd_erase(flash, CMD_S25FLXX_SE, offset, len); @@ -243,9 +180,10 @@ struct spi_flash *spi_flash_probe_spansion(struct spi_slave *spi, u8 *idcode) spsn->flash.spi = spi; spsn->flash.name = params->name; - spsn->flash.write = spansion_write; + spsn->flash.write = spi_flash_cmd_write_multi; spsn->flash.erase = spansion_erase; spsn->flash.read = spi_flash_cmd_read_fast; + spsn->flash.page_size = params->page_size; spsn->flash.sector_size = params->page_size * params->pages_per_sector; spsn->flash.size = spsn->flash.sector_size * params->nr_sectors; diff --git a/drivers/mtd/spi/spi_flash.c b/drivers/mtd/spi/spi_flash.c index 3e0d02d..730c009 100644 --- a/drivers/mtd/spi/spi_flash.c +++ b/drivers/mtd/spi/spi_flash.c @@ -65,6 +65,63 @@ int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len, return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len); } +int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset, + size_t len, const void *buf) +{ + unsigned long page_addr, byte_addr, page_size; + size_t chunk_len, actual; + int ret; + u8 cmd[4]; + + page_size = flash->page_size; + page_addr = offset / page_size; + byte_addr = offset % page_size; + + ret = spi_claim_bus(flash->spi); + if (ret) { + debug("SF: unable to claim SPI bus\n"); + return ret; + } + + cmd[0] = CMD_PAGE_PROGRAM; + for (actual = 0; actual < len; actual += chunk_len) { + chunk_len = min(len - actual, page_size - byte_addr); + + cmd[1] = page_addr >> 8; + cmd[2] = page_addr; + cmd[3] = byte_addr; + + debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n", + buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); + + ret = spi_flash_cmd_write_enable(flash); + if (ret < 0) { + debug("SF: enabling write failed\n"); + break; + } + + ret = spi_flash_cmd_write(flash->spi, cmd, 4, + buf + actual, chunk_len); + if (ret < 0) { + debug("SF: write failed\n"); + break; + } + + ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); + if (ret) + break; + + page_addr++; + byte_addr = 0; + } + + debug("SF: program %s %zu bytes @ %#x\n", + ret ? "failure" : "success", len, offset); + + spi_release_bus(flash->spi); + return ret; +} + int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd, size_t cmd_len, void *data, size_t data_len) { diff --git a/drivers/mtd/spi/spi_flash_internal.h b/drivers/mtd/spi/spi_flash_internal.h index 6665bed..f80f717 100644 --- a/drivers/mtd/spi/spi_flash_internal.h +++ b/drivers/mtd/spi/spi_flash_internal.h @@ -19,6 +19,7 @@ #define CMD_READ_ARRAY_FAST 0x0b #define CMD_READ_ARRAY_LEGACY 0xe8 +#define CMD_PAGE_PROGRAM 0x02 #define CMD_READ_STATUS 0x05 #define CMD_WRITE_ENABLE 0x06 @@ -46,6 +47,13 @@ int spi_flash_cmd_write(struct spi_slave *spi, const u8 *cmd, size_t cmd_len, const void *data, size_t data_len); /* + * Write the requested data out breaking it up into multiple write + * commands as needed per the write size. + */ +int spi_flash_cmd_write_multi(struct spi_flash *flash, u32 offset, + size_t len, const void *buf); + +/* * Enable writing on the SPI flash. */ static inline int spi_flash_cmd_write_enable(struct spi_flash *flash) diff --git a/drivers/mtd/spi/stmicro.c b/drivers/mtd/spi/stmicro.c index 80d97b4..8106e7c 100644 --- a/drivers/mtd/spi/stmicro.c +++ b/drivers/mtd/spi/stmicro.c @@ -134,69 +134,6 @@ static const struct stmicro_spi_flash_params stmicro_spi_flash_table[] = { }, }; -static int stmicro_write(struct spi_flash *flash, - u32 offset, size_t len, const void *buf) -{ - struct stmicro_spi_flash *stm = to_stmicro_spi_flash(flash); - unsigned long page_addr; - unsigned long byte_addr; - unsigned long page_size; - size_t chunk_len; - size_t actual; - int ret; - u8 cmd[4]; - - page_size = stm->params->page_size; - page_addr = offset / page_size; - byte_addr = offset % page_size; - - ret = spi_claim_bus(flash->spi); - if (ret) { - debug("SF: Unable to claim SPI bus\n"); - return ret; - } - - ret = 0; - for (actual = 0; actual < len; actual += chunk_len) { - chunk_len = min(len - actual, page_size - byte_addr); - - cmd[0] = CMD_M25PXX_PP; - cmd[1] = page_addr >> 8; - cmd[2] = page_addr; - cmd[3] = byte_addr; - - debug - ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n", - buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); - - ret = spi_flash_cmd_write_enable(flash); - if (ret < 0) { - debug("SF: Enabling Write failed\n"); - break; - } - - ret = spi_flash_cmd_write(flash->spi, cmd, 4, - buf + actual, chunk_len); - if (ret < 0) { - debug("SF: STMicro Page Program failed\n"); - break; - } - - ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); - if (ret) - break; - - page_addr++; - byte_addr = 0; - } - - debug("SF: STMicro: Successfully programmed %u bytes @ 0x%x\n", - len, offset); - - spi_release_bus(flash->spi); - return ret; -} - static int stmicro_erase(struct spi_flash *flash, u32 offset, size_t len) { return spi_flash_cmd_erase(flash, CMD_M25PXX_SE, offset, len); @@ -243,9 +180,10 @@ struct spi_flash *spi_flash_probe_stmicro(struct spi_slave *spi, u8 * idcode) stm->flash.spi = spi; stm->flash.name = params->name; - stm->flash.write = stmicro_write; + stm->flash.write = spi_flash_cmd_write_multi; stm->flash.erase = stmicro_erase; stm->flash.read = spi_flash_cmd_read_fast; + stm->flash.page_size = params->page_size; stm->flash.sector_size = params->page_size * params->pages_per_sector; stm->flash.size = stm->flash.sector_size * params->nr_sectors; diff --git a/drivers/mtd/spi/winbond.c b/drivers/mtd/spi/winbond.c index 10fabf5..3db0cb0 100644 --- a/drivers/mtd/spi/winbond.c +++ b/drivers/mtd/spi/winbond.c @@ -105,71 +105,6 @@ static const struct winbond_spi_flash_params winbond_spi_flash_table[] = { }, }; -static int winbond_write(struct spi_flash *flash, - u32 offset, size_t len, const void *buf) -{ - struct winbond_spi_flash *stm = to_winbond_spi_flash(flash); - unsigned long page_addr; - unsigned long byte_addr; - unsigned long page_size; - unsigned int page_shift; - size_t chunk_len; - size_t actual; - int ret; - u8 cmd[4]; - - page_shift = stm->params->l2_page_size; - page_size = (1 << page_shift); - page_addr = offset / page_size; - byte_addr = offset % page_size; - - ret = spi_claim_bus(flash->spi); - if (ret) { - debug("SF: Unable to claim SPI bus\n"); - return ret; - } - - for (actual = 0; actual < len; actual += chunk_len) { - chunk_len = min(len - actual, page_size - byte_addr); - - cmd[0] = CMD_W25_PP; - cmd[1] = page_addr >> (16 - page_shift); - cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8); - cmd[3] = byte_addr; - debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n", - buf + actual, - cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); - - ret = spi_flash_cmd_write_enable(flash); - if (ret < 0) { - debug("SF: Enabling Write failed\n"); - goto out; - } - - ret = spi_flash_cmd_write(flash->spi, cmd, 4, - buf + actual, chunk_len); - if (ret < 0) { - debug("SF: Winbond Page Program failed\n"); - goto out; - } - - ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); - if (ret) - goto out; - - page_addr++; - byte_addr = 0; - } - - debug("SF: Winbond: Successfully programmed %u bytes @ 0x%x\n", - len, offset); - ret = 0; - -out: - spi_release_bus(flash->spi); - return ret; -} - static int winbond_erase(struct spi_flash *flash, u32 offset, size_t len) { return spi_flash_cmd_erase(flash, CMD_W25_SE, offset, len); @@ -207,11 +142,11 @@ struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode) /* Assuming power-of-two page size initially. */ page_size = 1 << params->l2_page_size; - stm->flash.write = winbond_write; + stm->flash.write = spi_flash_cmd_write_multi; stm->flash.erase = winbond_erase; stm->flash.read = spi_flash_cmd_read_fast; - stm->flash.sector_size = (1 << stm->params->l2_page_size) * - stm->params->pages_per_sector; + stm->flash.page_size = page_size; + stm->flash.sector_size = page_size * stm->params->pages_per_sector; stm->flash.size = page_size * params->pages_per_sector * params->sectors_per_block * params->nr_blocks; diff --git a/include/spi_flash.h b/include/spi_flash.h index a384071..2671ab5 100644 --- a/include/spi_flash.h +++ b/include/spi_flash.h @@ -31,8 +31,11 @@ struct spi_flash { const char *name; + /* Total flash size */ u32 size; - + /* Write (page) size */ + u32 page_size; + /* Erase (sector) size */ u32 sector_size; int (*read)(struct spi_flash *flash, u32 offset,