From patchwork Wed Aug 3 07:01:36 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Xu, Hong" X-Patchwork-Id: 108025 X-Patchwork-Delegate: andreas.biessmann@googlemail.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 8632EB71E4 for ; Wed, 3 Aug 2011 17:02:00 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id C6ADA282DB; Wed, 3 Aug 2011 09:01:56 +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 gvwENggjrxNa; Wed, 3 Aug 2011 09:01:55 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id F26E6282D1; Wed, 3 Aug 2011 09:01:52 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 372CF282D1 for ; Wed, 3 Aug 2011 09:01:51 +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 rHB0lULzT-YG for ; Wed, 3 Aug 2011 09:01:49 +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 sjogate2.atmel.com (newsmtp5.atmel.com [204.2.163.5]) by theia.denx.de (Postfix) with ESMTP id 36340282C8 for ; Wed, 3 Aug 2011 09:01:45 +0200 (CEST) Received: from localhost.localdomain ([10.217.2.49]) by sjogate2.atmel.com (8.13.6/8.13.6) with ESMTP id p736wILl007199; Tue, 2 Aug 2011 23:58:19 -0700 (PDT) From: Hong Xu To: u-boot@emk-elektronik.de, u-boot@lists.denx.de Date: Wed, 3 Aug 2011 15:01:36 +0800 Message-Id: <1312354896-24951-1-git-send-email-hong.xu@atmel.com> X-Mailer: git-send-email 1.7.3.3 Subject: [U-Boot] [PATCH] AT91: Defer Dataflash access to env_relocate_spec 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 When env_init is called, the SPI is not actually initialized in U-Boot. So that we can not read Dataflash for its content. We simply mark it OK for now, and defer the real work to `env_relocate_spec'. (Idealy from env_nand.c) Signed-off-by: Hong Xu --- common/env_dataflash.c | 83 ++++++++++++++++++++++++++---------------------- 1 files changed, 45 insertions(+), 38 deletions(-) diff --git a/common/env_dataflash.c b/common/env_dataflash.c index 1d57079..55534a5 100644 --- a/common/env_dataflash.c +++ b/common/env_dataflash.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -50,11 +51,46 @@ uchar env_get_char_spec(int index) void env_relocate_spec(void) { - char buf[CONFIG_ENV_SIZE]; + ulong old_crc, new_crc = 0; + char *buf; - read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf); + gd->env_valid = 0; - env_import(buf, 1); + buf = (char *)malloc(CONFIG_ENV_SIZE); + if (buf == NULL) { + error("Can not allocate memory for env.\n"); + goto err_mem; + } + + AT91F_DataflashInit(); + + if (read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), + sizeof(ulong), (char *)&old_crc) != DATAFLASH_OK) { + error("Dataflash: Failed to read original 4-bytes CRC\n"); + goto err; + } + + if (read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, data), + ENV_SIZE, buf) != DATAFLASH_OK) { + error("Dataflash: Failed to read env string.\n"); + goto err; + } + + new_crc = crc32(new_crc, (uchar *)buf, ENV_SIZE); + + if (old_crc == new_crc) { + gd->env_addr = offsetof(env_t, data); + gd->env_valid = 1; + env_import(buf, 0); + goto out; + } + +err: + set_default_env("!bad CRC"); +out: + free(buf); +err_mem: + return; } #ifdef CONFIG_ENV_OFFSET_REDUND @@ -83,44 +119,15 @@ int saveenv(void) /* * Initialize environment use * - * We are still running from ROM, so data use is limited. - * Use a (moderately small) buffer on the stack + * When env_init is called, the SPI is not actually initialized in U-Boot. + * So that we can not read Dataflash for its content. + * We simply mark it OK for now, and defer the real work to + * `env_relocate_spec'. (Idealy from env_nand.c) */ int env_init(void) { - ulong crc, len, new; - unsigned off; - uchar buf[64]; - - if (gd->env_valid) - return 0; - - AT91F_DataflashInit(); /* prepare for DATAFLASH read/write */ - - /* read old CRC */ - read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc), - sizeof(ulong), (char *)&crc); - - new = 0; - len = ENV_SIZE; - off = offsetof(env_t,data); - while (len > 0) { - int n = (len > sizeof(buf)) ? sizeof(buf) : len; - - read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf); - - new = crc32 (new, buf, n); - len -= n; - off += n; - } - - if (crc == new) { - gd->env_addr = offsetof(env_t,data); - gd->env_valid = 1; - } else { - gd->env_addr = (ulong)&default_environment[0]; - gd->env_valid = 0; - } + gd->env_addr = (ulong)&default_environment[0]; + gd->env_valid = 1; return 0; }