From patchwork Wed Dec 26 22:28:23 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 208228 X-Patchwork-Delegate: sjg@chromium.org 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 83E712C00BE for ; Thu, 27 Dec 2012 09:29:53 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A0F364A138; Wed, 26 Dec 2012 23:29:51 +0100 (CET) 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 xis4c83qvpXh; Wed, 26 Dec 2012 23:29:51 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 2886F4A0F1; Wed, 26 Dec 2012 23:29:16 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id E09264A0AB for ; Wed, 26 Dec 2012 23:29:11 +0100 (CET) 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 LuEhy45QghFa for ; Wed, 26 Dec 2012 23:29:10 +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 mail-ea0-f201.google.com (mail-ea0-f201.google.com [209.85.215.201]) by theia.denx.de (Postfix) with ESMTPS id 488AC4A09C for ; Wed, 26 Dec 2012 23:29:02 +0100 (CET) Received: by mail-ea0-f201.google.com with SMTP id b14so524987eaa.4 for ; Wed, 26 Dec 2012 14:29:01 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references:x-gm-message-state; bh=xmTmJB4WdVAOSpUrIhYzF7+YU4h2kY2qSH4GWrr+TAk=; b=TaCg9iFVdVnX1Y/7TYTlIlPs5UVT35jrnRP9XgT5y2R7Anh6NIzq2hS4ha/0F6UFhR hJXewg7WlOMRtjPyD1OBwioMiHnqT3t7qJhUpjrSXLWwLidt+gAZWoqVzJq4ydQuX6zF eFQGkUJa29qBHYi4GZuRCSSlK+SvohnC+V5s9u61JqLkkrsQLYfLAkWC3yTzHDynBxQY H9DgmaoJPi1oFwCUe27SXhcDsTrgdo/rfFsXnfT/afjHoM51iOUeubEch+FdPyCxMsRk nVZfJi5+JbSN79ZFqgt82mmWsnHIaygyCVnCnc5X7tiinD340Uy8huL4+C8pFH5KqcS3 cdCg== X-Received: by 10.14.214.197 with SMTP id c45mr36519444eep.7.1356560941777; Wed, 26 Dec 2012 14:29:01 -0800 (PST) Received: from hpza10.eem.corp.google.com ([74.125.121.33]) by gmr-mx.google.com with ESMTPS id z44si10602403een.0.2012.12.26.14.29.01 (version=TLSv1/SSLv3 cipher=AES128-SHA); Wed, 26 Dec 2012 14:29:01 -0800 (PST) Received: from kaka.mtv.corp.google.com (kaka.mtv.corp.google.com [172.22.73.79]) by hpza10.eem.corp.google.com (Postfix) with ESMTP id 691CF20004E; Wed, 26 Dec 2012 14:29:01 -0800 (PST) Received: by kaka.mtv.corp.google.com (Postfix, from userid 121222) id BD10D160C72; Wed, 26 Dec 2012 14:29:00 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Date: Wed, 26 Dec 2012 14:28:23 -0800 Message-Id: <1356560913-2205-6-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.7.7.3 In-Reply-To: <1356560913-2205-1-git-send-email-sjg@chromium.org> References: <1356560913-2205-1-git-send-email-sjg@chromium.org> X-Gm-Message-State: ALoCoQmCfv24985zb5h73e5x3rLczi6X3RJDZbkcyrJnS5djH+1wEZ8GrZw+QEQAe2dqBALooPF5po9jjiQr24uhjhea7fuNI4jSeoEU0iJZcMKAs2LuI84WTNYdEyMh5bkmuGQxx5gGD9Z14E+lyY2RpARN7E3pyk2IjhEl/hQKe9nqNZcsYD4KBVomBo2G3rFnIMlwDJK/ Cc: Graeme Russ , Tom Warren Subject: [U-Boot] [PATCH 05/15] sf: Add spi_flash_alloc() to create a new SPI flash struct X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.11 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 At present it is difficult to extend the SPI flash structure since all devices allocate it themselves, and few of them zero all fields. Add a new function spi_flash_alloc() which can be used by SPI devices to perform this allocation, and thus ensure that all devices can better cope with SPI structure changes. Signed-off-by: Simon Glass --- drivers/mtd/spi/spi_flash.c | 25 +++++++++++++++++++++++++ include/spi_flash.h | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 0 deletions(-) diff --git a/drivers/mtd/spi/spi_flash.c b/drivers/mtd/spi/spi_flash.c index 00aece9..17f3d3c 100644 --- a/drivers/mtd/spi/spi_flash.c +++ b/drivers/mtd/spi/spi_flash.c @@ -401,6 +401,31 @@ err_claim_bus: return NULL; } +void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi, + const char *name) +{ + struct spi_flash *flash; + void *ptr; + + ptr = malloc(size); + if (!ptr) { + debug("SF: Failed to allocate memory\n"); + return NULL; + } + memset(ptr, '\0', size); + flash = (struct spi_flash *)(ptr + offset); + + /* Set up some basic fields - caller will sort out sizes */ + flash->spi = spi; + flash->name = name; + + flash->read = spi_flash_cmd_read_fast; + flash->write = spi_flash_cmd_write_multi; + flash->erase = spi_flash_cmd_erase; + + return flash; +} + void spi_flash_free(struct spi_flash *flash) { spi_free_slave(flash->spi); diff --git a/include/spi_flash.h b/include/spi_flash.h index 9da9062..030d49c 100644 --- a/include/spi_flash.h +++ b/include/spi_flash.h @@ -47,6 +47,44 @@ struct spi_flash { size_t len); }; +/** + * spi_flash_do_alloc - Allocate a new spi flash structure + * + * The structure is allocated and cleared with default values for + * read, write and erase, which the caller can modify. The caller must set + * up size, page_size and sector_size. + * + * Use the helper macro spi_flash_alloc() to call this. + * + * @offset: Offset of struct spi_slave within slave structure + * @size: Size of slave structure + * @spi: SPI slave + * @name: Name of SPI flash device + */ +void *spi_flash_do_alloc(int offset, int size, struct spi_slave *spi, + const char *name); + +/** + * spi_flash_alloc - Allocate a new SPI flash structure + * + * @_struct: Name of structure to allocate (e.g. struct ramtron_spi_fram). This + * structure must contain a member 'struct spi_flash *flash'. + * @spi: SPI slave + * @name: Name of SPI flash device + */ +#define spi_flash_alloc(_struct, spi, name) \ + spi_flash_do_alloc(offsetof(_struct, flash), sizeof(_struct), \ + spi, name) + +/** + * spi_flash_alloc_base - Allocate a new SPI flash structure with no private data + * + * @spi: SPI slave + * @name: Name of SPI flash device + */ +#define spi_flash_alloc_base(spi, name) \ + spi_flash_do_alloc(0, sizeof(struct spi_flash), spi, name) + struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs, unsigned int max_hz, unsigned int spi_mode); void spi_flash_free(struct spi_flash *flash);