From patchwork Mon Mar 11 16:08:02 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 226577 X-Patchwork-Delegate: trini@ti.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 C69E82C02A6 for ; Tue, 12 Mar 2013 03:09:32 +1100 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 09D974A213; Mon, 11 Mar 2013 17:09:31 +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 NEj8A6Q5pP-E; Mon, 11 Mar 2013 17:09:30 +0100 (CET) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id AA7074A1F6; Mon, 11 Mar 2013 17:09:17 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 813EE4A176 for ; Mon, 11 Mar 2013 17:09:08 +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 IJP9+kP+85vf for ; Mon, 11 Mar 2013 17:09:03 +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-ve0-f201.google.com (mail-ve0-f201.google.com [209.85.128.201]) by theia.denx.de (Postfix) with ESMTPS id E7DB54A15F for ; Mon, 11 Mar 2013 17:09:00 +0100 (CET) Received: by mail-ve0-f201.google.com with SMTP id 14so393931vea.2 for ; Mon, 11 Mar 2013 09:08:59 -0700 (PDT) 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=DsG2xjJra3b2kiI1gNmLSYKl8/B4RZ31e9WTHMjEbYA=; b=OxK1xQ9XnUbRXa0IpWpDlSXSFudiWjmO2zAhzq7dgUy/E1LBEVlwD57md8GlQ6PqBx E1KEW7DNNJtiXhEKgyrRHCtEhjhivOK8VgXTsKdm+I1dANYacq5aX5ByEJ067oWQEDFk LPqKxG3/A1tVvqSwj/D0W8Aby0+CflDnPbD+B+vyGjaxiOlda0YcmwwkuhXcQ/JRjcmF yT5gizaphz2P8ALMl94Vmuaxf+Dsmt+wixkdSOQI4JK5zvE7sEg7w4G7S5pFFU5jmU7+ c/t60FZLCch5KTCY5T71p48Uea2iBolSaVFbsvHyTtpeHV+imtM+nCzV1diIENF2NM// 7L0Q== X-Received: by 10.236.124.235 with SMTP id x71mr7927415yhh.25.1363018139018; Mon, 11 Mar 2013 09:08:59 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id k21si1088057yhh.1.2013.03.11.09.08.58 (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Mon, 11 Mar 2013 09:08:59 -0700 (PDT) Received: from kaka.mtv.corp.google.com (kaka.mtv.corp.google.com [172.22.73.79]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id C242131C26C; Mon, 11 Mar 2013 09:08:58 -0700 (PDT) Received: by kaka.mtv.corp.google.com (Postfix, from userid 121222) id 6B7A1160CF9; Mon, 11 Mar 2013 09:08:58 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Date: Mon, 11 Mar 2013 09:08:02 -0700 Message-Id: <1363018093-28979-5-git-send-email-sjg@chromium.org> X-Mailer: git-send-email 1.8.1.3 In-Reply-To: <1363018093-28979-1-git-send-email-sjg@chromium.org> References: <1363018093-28979-1-git-send-email-sjg@chromium.org> X-Gm-Message-State: ALoCoQlZbgGbn0MW63hWdQJl45xsf89wZ3YspNjKjCOXu/aetteiW5Dcsg+/c5ncpnbllq8AiSWeUCoyJH9OlxCPVMctxBo1XRTWGmh4PPY7OFehrqH9uuNaLcB95qmkVtKxOYC4Sywq8fRVpaTZIWbcXXJ3j1SxiyBCWy6COHXNGtKzTVkb16EdUMuSBnVVBVAMOdj6bCHQ Cc: Graeme Russ , Tom Warren Subject: [U-Boot] [PATCH v2 04/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 --- Changes in v2: None drivers/mtd/spi/spi_flash.c | 25 +++++++++++++++++++++++++ include/spi_flash.h | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) 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);