From patchwork Sun Sep 21 13:12:14 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Vasut X-Patchwork-Id: 391680 X-Patchwork-Delegate: marek.vasut@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 2FA2C1400BE for ; Sun, 21 Sep 2014 23:14:13 +1000 (EST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id D0C58A74CE; Sun, 21 Sep 2014 15:14:11 +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 uoJnVnF0KbTp; Sun, 21 Sep 2014 15:14:11 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A73BAA74E4; Sun, 21 Sep 2014 15:13:08 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id A95D44B696 for ; Sun, 21 Sep 2014 15:12:49 +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 5x8pQwAlROi1 for ; Sun, 21 Sep 2014 15:12:48 +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 mail-out.m-online.net (mail-out.m-online.net [212.18.0.9]) by theia.denx.de (Postfix) with ESMTPS id E0BC94B676 for ; Sun, 21 Sep 2014 15:12:47 +0200 (CEST) Received: from mail.nefkom.net (unknown [192.168.8.184]) by mail-out.m-online.net (Postfix) with ESMTP id 3j18My2tJZz3hjhZ; Sun, 21 Sep 2014 15:12:46 +0200 (CEST) X-Auth-Info: CTFSAn1ThEQSovz+dbSlqxjymFJVjWTZkQDNBZmBWXo= Received: from chi.lan (host-82-135-33-74.customer.m-online.net [82.135.33.74]) by smtp-auth.mnet-online.de (Postfix) with ESMTPA id 3j18Mx5bfnzvhTZ; Sun, 21 Sep 2014 15:12:45 +0200 (CEST) From: Marek Vasut To: u-boot@lists.denx.de Date: Sun, 21 Sep 2014 15:12:14 +0200 Message-Id: <1411305152-11690-6-git-send-email-marex@denx.de> X-Mailer: git-send-email 2.0.0 In-Reply-To: <1411305152-11690-1-git-send-email-marex@denx.de> References: <1411304339-11348-1-git-send-email-marex@denx.de> <1411305152-11690-1-git-send-email-marex@denx.de> Cc: Marek Vasut , Pavel Machek , Chin Liang See , Tom Rini , sr@denx.de, Dinh Nguyen Subject: [U-Boot] [PATCH 33/51] fpga: altera: Turn the switches into table lookup 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 Add a table of FPGA family with matching functions associated with it and make all the code just look up the family in that table and call the associated function instead of the horrible switch voodoo which was duplicated all over the place. Signed-off-by: Marek Vasut Cc: Chin Liang See Cc: Dinh Nguyen Cc: Albert Aribaud Cc: Tom Rini Cc: Wolfgang Denk Cc: Pavel Machek --- drivers/fpga/altera.c | 171 ++++++++++++++++++-------------------------------- 1 file changed, 60 insertions(+), 111 deletions(-) diff --git a/drivers/fpga/altera.c b/drivers/fpga/altera.c index 941e7c8..fd2b4f0 100644 --- a/drivers/fpga/altera.c +++ b/drivers/fpga/altera.c @@ -19,6 +19,26 @@ /* Define FPGA_DEBUG to 1 to get debug printf's */ #define FPGA_DEBUG 0 +static const struct altera_fpga { + enum altera_family family; + const char *name; + int (*load)(Altera_desc *, const void *, size_t); + int (*dump)(Altera_desc *, const void *, size_t); + int (*info)(Altera_desc *); +} altera_fpga[] = { +#if defined(CONFIG_FPGA_ACEX1K) + { Altera_ACEX1K, "ACEX1K", ACEX1K_load, ACEX1K_dump, ACEX1K_info }, + { Altera_CYC2, "ACEX1K", ACEX1K_load, ACEX1K_dump, ACEX1K_info }, +#elif defined(CONFIG_FPGA_CYCLON2) + { Altera_ACEX1K, "CycloneII", CYC2_load, CYC2_dump, CYC2_info }, + { Altera_CYC2, "CycloneII", CYC2_load, CYC2_dump, CYC2_info }, +#endif +#if defined(CONFIG_FPGA_STRATIX_II) + { Altera_StratixII, "StratixII", StratixII_load, + StratixII_dump, StratixII_info }, +#endif +}; + static int altera_validate(Altera_desc *desc, const char *fn) { if (!desc) { @@ -46,113 +66,65 @@ static int altera_validate(Altera_desc *desc, const char *fn) return 0; } -/* ------------------------------------------------------------------------- */ -int altera_load(Altera_desc *desc, const void *buf, size_t bsize) +static const struct altera_fpga * +altera_desc_to_fpga(Altera_desc *desc, const char *fn) { - int ret_val = FPGA_FAIL; /* assume a failure */ + int i; - if (altera_validate(desc, (char *)__func__)) { - printf("%s: Invalid device descriptor\n", __func__); - return FPGA_FAIL; + if (altera_validate(desc, fn)) { + printf("%s: Invalid device descriptor\n", fn); + return NULL; } - switch (desc->family) { - case Altera_ACEX1K: - case Altera_CYC2: -#if defined(CONFIG_FPGA_ACEX1K) - debug_cond(FPGA_DEBUG, - "%s: Launching the ACEX1K Loader...\n", - __func__); - ret_val = ACEX1K_load (desc, buf, bsize); -#elif defined(CONFIG_FPGA_CYCLON2) - debug_cond(FPGA_DEBUG, - "%s: Launching the CYCLONE II Loader...\n", - __func__); - ret_val = CYC2_load (desc, buf, bsize); -#else - printf("%s: No support for ACEX1K devices.\n", - __func__); -#endif - break; + for (i = 0; i < ARRAY_SIZE(altera_fpga); i++) { + if (desc->family == altera_fpga[i].family) + break; + } -#if defined(CONFIG_FPGA_STRATIX_II) - case Altera_StratixII: - debug_cond(FPGA_DEBUG, - "%s: Launching the Stratix II Loader...\n", - __func__); - ret_val = StratixII_load (desc, buf, bsize); - break; -#endif - default: - printf("%s: Unsupported family type, %d\n", - __func__, desc->family); + if (i == ARRAY_SIZE(altera_fpga)) { + printf("%s: Unsupported family type, %d\n", fn, desc->family); + return NULL; } - return ret_val; + return &altera_fpga[i]; } -int altera_dump(Altera_desc *desc, const void *buf, size_t bsize) +int altera_load(Altera_desc *desc, const void *buf, size_t bsize) { - int ret_val = FPGA_FAIL; /* assume a failure */ + const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__); - if (altera_validate(desc, (char *)__func__)) { - printf("%s: Invalid device descriptor\n", __func__); + if (!fpga) return FPGA_FAIL; - } - switch (desc->family) { - case Altera_ACEX1K: -#if defined(CONFIG_FPGA_ACEX) - debug_cond(FPGA_DEBUG, - "%s: Launching the ACEX1K Reader...\n", - __func__); - ret_val = ACEX1K_dump (desc, buf, bsize); -#else - printf("%s: No support for ACEX1K devices.\n", - __func__); -#endif - break; + debug_cond(FPGA_DEBUG, "%s: Launching the %s Loader...\n", + __func__, fpga->name); + if (fpga->load) + return fpga->load(desc, buf, bsize); + return 0; +} -#if defined(CONFIG_FPGA_STRATIX_II) - case Altera_StratixII: - debug_cond(FPGA_DEBUG, - "%s: Launching the Stratix II Reader...\n", - __func__); - ret_val = StratixII_dump (desc, buf, bsize); - break; -#endif - default: - printf("%s: Unsupported family type, %d\n", - __func__, desc->family); - } +int altera_dump(Altera_desc *desc, const void *buf, size_t bsize) +{ + const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__); - return ret_val; + if (!fpga) + return FPGA_FAIL; + + debug_cond(FPGA_DEBUG, "%s: Launching the %s Reader...\n", + __func__, fpga->name); + if (fpga->dump) + return fpga->dump(desc, buf, bsize); + return 0; } int altera_info(Altera_desc *desc) { - int ret_val = FPGA_FAIL; + const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__); - if (altera_validate (desc, (char *)__func__)) { - printf("%s: Invalid device descriptor\n", __func__); + if (!fpga) return FPGA_FAIL; - } - printf("Family: \t"); - switch (desc->family) { - case Altera_ACEX1K: - printf("ACEX1K\n"); - break; - case Altera_CYC2: - printf("CYCLON II\n"); - break; - case Altera_StratixII: - printf("Stratix II\n"); - break; - /* Add new family types here */ - default: - printf("Unknown family type, %d\n", desc->family); - } + printf("Family: \t%s\n", fpga->name); printf("Interface type:\t"); switch (desc->iface) { @@ -188,34 +160,11 @@ int altera_info(Altera_desc *desc) if (desc->iface_fns) { printf("Device Function Table @ 0x%p\n", desc->iface_fns); - switch (desc->family) { - case Altera_ACEX1K: - case Altera_CYC2: -#if defined(CONFIG_FPGA_ACEX1K) - ACEX1K_info(desc); -#elif defined(CONFIG_FPGA_CYCLON2) - CYC2_info(desc); -#else - /* just in case */ - printf("%s: No support for ACEX1K devices.\n", - __func__); -#endif - break; -#if defined(CONFIG_FPGA_STRATIX_II) - case Altera_StratixII: - StratixII_info(desc); - break; -#endif - /* Add new family types here */ - default: - /* we don't need a message here - we give one up above */ - break; - } + if (fpga->info) + fpga->info(desc); } else { printf("No Device Function Table.\n"); } - ret_val = FPGA_SUCCESS; - - return ret_val; + return FPGA_SUCCESS; }