]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/fpga/altera.c
karo: tx6ul: fix bogus GPIO numbers
[karo-tx-uboot.git] / drivers / fpga / altera.c
index 09dc0b22e1b4a92124536c1c24ebe88948a34035..a5bfe5dce19a3dce36d35053031bfe4bf24c775e 100644 (file)
  * (C) Copyright 2002
  * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
- *
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
  *  Altera FPGA support
  */
 #include <common.h>
+#include <errno.h>
 #include <ACEX1K.h>
 #include <stratixII.h>
 
-/* Define FPGA_DEBUG to get debug printf's */
-/* #define FPGA_DEBUG */
-
-#ifdef FPGA_DEBUG
-#define        PRINTF(fmt,args...)     printf (fmt ,##args)
-#else
-#define PRINTF(fmt,args...)
-#endif
-
-/* Local Static Functions */
-static int altera_validate (Altera_desc * desc, const char *fn);
-
-/* ------------------------------------------------------------------------- */
-int altera_load( Altera_desc *desc, void *buf, size_t bsize )
-{
-       int ret_val = FPGA_FAIL;        /* assume a failure */
+/* Define FPGA_DEBUG to 1 to get debug printf's */
+#define FPGA_DEBUG     0
 
-       if (!altera_validate (desc, (char *)__FUNCTION__)) {
-               printf ("%s: Invalid device descriptor\n", __FUNCTION__);
-       } else {
-               switch (desc->family) {
-               case Altera_ACEX1K:
-               case Altera_CYC2:
+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)
-                       PRINTF ("%s: Launching the ACEX1K Loader...\n",
-                                       __FUNCTION__);
-                       ret_val = ACEX1K_load (desc, buf, bsize);
+       { Altera_ACEX1K, "ACEX1K", ACEX1K_load, ACEX1K_dump, ACEX1K_info },
+       { Altera_CYC2,   "ACEX1K", ACEX1K_load, ACEX1K_dump, ACEX1K_info },
 #elif defined(CONFIG_FPGA_CYCLON2)
-                       PRINTF ("%s: Launching the CYCLON II Loader...\n",
-                                       __FUNCTION__);
-                       ret_val = CYC2_load (desc, buf, bsize);
-#else
-                       printf ("%s: No support for ACEX1K devices.\n",
-                                       __FUNCTION__);
+       { Altera_ACEX1K, "CycloneII", CYC2_load, CYC2_dump, CYC2_info },
+       { Altera_CYC2,   "CycloneII", CYC2_load, CYC2_dump, CYC2_info },
 #endif
-                       break;
-
 #if defined(CONFIG_FPGA_STRATIX_II)
-               case Altera_StratixII:
-                       PRINTF ("%s: Launching the Stratix II Loader...\n",
-                               __FUNCTION__);
-                       ret_val = StratixII_load (desc, buf, bsize);
-                       break;
+       { Altera_StratixII, "StratixII", StratixII_load,
+         StratixII_dump, StratixII_info },
 #endif
-               default:
-                       printf ("%s: Unsupported family type, %d\n",
-                                       __FUNCTION__, desc->family);
-               }
-       }
-
-       return ret_val;
-}
+#if defined(CONFIG_FPGA_SOCFPGA)
+       { Altera_SoCFPGA, "SoC FPGA", socfpga_load, NULL, NULL },
+#endif
+};
 
-int altera_dump( Altera_desc *desc, void *buf, size_t bsize )
+static int altera_validate(Altera_desc *desc, const char *fn)
 {
-       int ret_val = FPGA_FAIL;        /* assume a failure */
+       if (!desc) {
+               printf("%s: NULL descriptor!\n", fn);
+               return -EINVAL;
+       }
 
-       if (!altera_validate (desc, (char *)__FUNCTION__)) {
-               printf ("%s: Invalid device descriptor\n", __FUNCTION__);
-       } else {
-               switch (desc->family) {
-               case Altera_ACEX1K:
-#if defined(CONFIG_FPGA_ACEX)
-                       PRINTF ("%s: Launching the ACEX1K Reader...\n",
-                                       __FUNCTION__);
-                       ret_val = ACEX1K_dump (desc, buf, bsize);
-#else
-                       printf ("%s: No support for ACEX1K devices.\n",
-                                       __FUNCTION__);
-#endif
-                       break;
+       if ((desc->family < min_altera_type) ||
+           (desc->family > max_altera_type)) {
+               printf("%s: Invalid family type, %d\n", fn, desc->family);
+               return -EINVAL;
+       }
 
-#if defined(CONFIG_FPGA_STRATIX_II)
-               case Altera_StratixII:
-                       PRINTF ("%s: Launching the Stratix II Reader...\n",
-                               __FUNCTION__);
-                       ret_val = StratixII_dump (desc, buf, bsize);
-                       break;
-#endif
-               default:
-                       printf ("%s: Unsupported family type, %d\n",
-                                       __FUNCTION__, desc->family);
-               }
+       if ((desc->iface < min_altera_iface_type) ||
+           (desc->iface > max_altera_iface_type)) {
+               printf("%s: Invalid Interface type, %d\n", fn, desc->iface);
+               return -EINVAL;
+       }
+
+       if (!desc->size) {
+               printf("%s: NULL part size\n", fn);
+               return -EINVAL;
        }
 
-       return ret_val;
+       return 0;
 }
 
-int altera_info( Altera_desc *desc )
+static const struct altera_fpga *
+altera_desc_to_fpga(Altera_desc *desc, const char *fn)
 {
-       int ret_val = FPGA_FAIL;
+       int i;
 
-       if (altera_validate (desc, (char *)__FUNCTION__)) {
-               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);
-               }
+       if (altera_validate(desc, fn)) {
+               printf("%s: Invalid device descriptor\n", fn);
+               return NULL;
+       }
 
-               printf ("Interface type:\t");
-               switch (desc->iface) {
-               case passive_serial:
-                       printf ("Passive Serial (PS)\n");
-                       break;
-               case passive_parallel_synchronous:
-                       printf ("Passive Parallel Synchronous (PPS)\n");
+       for (i = 0; i < ARRAY_SIZE(altera_fpga); i++) {
+               if (desc->family == altera_fpga[i].family)
                        break;
-               case passive_parallel_asynchronous:
-                       printf ("Passive Parallel Asynchronous (PPA)\n");
-                       break;
-               case passive_serial_asynchronous:
-                       printf ("Passive Serial Asynchronous (PSA)\n");
-                       break;
-               case altera_jtag_mode:          /* Not used */
-                       printf ("JTAG Mode\n");
-                       break;
-               case fast_passive_parallel:
-                       printf ("Fast Passive Parallel (FPP)\n");
-                       break;
-               case fast_passive_parallel_security:
-                       printf
-                           ("Fast Passive Parallel with Security (FPPS) \n");
-                       break;
-                       /* Add new interface types here */
-               default:
-                       printf ("Unsupported interface type, %d\n", desc->iface);
-               }
-
-               printf ("Device Size:   \t%d bytes\n"
-                               "Cookie:        \t0x%x (%d)\n",
-                               desc->size, desc->cookie, desc->cookie);
-
-               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",
-                                               __FUNCTION__);
-#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;
-                       }
-               } else {
-                       printf ("No Device Function Table.\n");
-               }
+       }
 
-               ret_val = FPGA_SUCCESS;
-       } else {
-               printf ("%s: Invalid device descriptor\n", __FUNCTION__);
+       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_reloc( Altera_desc *desc, ulong reloc_offset)
+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 *)__FUNCTION__)) {
-               printf ("%s: Invalid device descriptor\n", __FUNCTION__);
-       } else {
-               switch (desc->family) {
-               case Altera_ACEX1K:
-#if defined(CONFIG_FPGA_ACEX1K)
-                       ret_val = ACEX1K_reloc (desc, reloc_offset);
-#else
-                       printf ("%s: No support for ACEX devices.\n",
-                                       __FUNCTION__);
-#endif
-                       break;
-#if defined(CONFIG_FPGA_STRATIX_II)
-               case Altera_StratixII:
-                       ret_val = StratixII_reloc (desc, reloc_offset);
-                       break;
-#endif
-               case Altera_CYC2:
-#if defined(CONFIG_FPGA_CYCLON2)
-                       ret_val = CYC2_reloc (desc, reloc_offset);
-#else
-                       printf ("%s: No support for CYCLON II devices.\n",
-                                       __FUNCTION__);
-#endif
-                       break;
-                       /* Add new family types here */
-               default:
-                       printf ("%s: Unsupported family type, %d\n",
-                                       __FUNCTION__, desc->family);
-               }
-       }
+       if (!fpga)
+               return FPGA_FAIL;
 
-       return ret_val;
+       debug_cond(FPGA_DEBUG, "%s: Launching the %s Loader...\n",
+                  __func__, fpga->name);
+       if (fpga->load)
+               return fpga->load(desc, buf, bsize);
+       return 0;
 }
 
-/* ------------------------------------------------------------------------- */
+int altera_dump(Altera_desc *desc, const void *buf, size_t bsize)
+{
+       const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__);
+
+       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;
+}
 
-static int altera_validate (Altera_desc * desc, const char *fn)
+int altera_info(Altera_desc *desc)
 {
-       int ret_val = FALSE;
+       const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__);
+
+       if (!fpga)
+               return FPGA_FAIL;
+
+       printf("Family:        \t%s\n", fpga->name);
+
+       printf("Interface type:\t");
+       switch (desc->iface) {
+       case passive_serial:
+               printf("Passive Serial (PS)\n");
+               break;
+       case passive_parallel_synchronous:
+               printf("Passive Parallel Synchronous (PPS)\n");
+               break;
+       case passive_parallel_asynchronous:
+               printf("Passive Parallel Asynchronous (PPA)\n");
+               break;
+       case passive_serial_asynchronous:
+               printf("Passive Serial Asynchronous (PSA)\n");
+               break;
+       case altera_jtag_mode:          /* Not used */
+               printf("JTAG Mode\n");
+               break;
+       case fast_passive_parallel:
+               printf("Fast Passive Parallel (FPP)\n");
+               break;
+       case fast_passive_parallel_security:
+               printf("Fast Passive Parallel with Security (FPPS)\n");
+               break;
+               /* Add new interface types here */
+       default:
+               printf("Unsupported interface type, %d\n", desc->iface);
+       }
+
+       printf("Device Size:   \t%zd bytes\n"
+              "Cookie:        \t0x%x (%d)\n",
+              desc->size, desc->cookie, desc->cookie);
 
-       if (desc) {
-               if ((desc->family > min_altera_type) &&
-                       (desc->family < max_altera_type)) {
-                       if ((desc->iface > min_altera_iface_type) &&
-                               (desc->iface < max_altera_iface_type)) {
-                               if (desc->size) {
-                                       ret_val = TRUE;
-                               } else {
-                                       printf ("%s: NULL part size\n", fn);
-                               }
-                       } else {
-                               printf ("%s: Invalid Interface type, %d\n",
-                                       fn, desc->iface);
-                       }
-               } else {
-                       printf ("%s: Invalid family type, %d\n", fn, desc->family);
-               }
+       if (desc->iface_fns) {
+               printf("Device Function Table @ 0x%p\n", desc->iface_fns);
+               if (fpga->info)
+                       fpga->info(desc);
        } else {
-               printf ("%s: NULL descriptor!\n", fn);
+               printf("No Device Function Table.\n");
        }
 
-       return ret_val;
+       return FPGA_SUCCESS;
 }
-
-/* ------------------------------------------------------------------------- */