blob: a5bfe5dce19a3dce36d35053031bfe4bf24c775e [file] [log] [blame]
wdenk5b845b62002-08-21 21:57:24 +00001/*
wdenk5da627a2003-10-09 20:09:04 +00002 * (C) Copyright 2003
3 * Steven Scholz, imc Measurement & Control, steven.scholz@imc-berlin.de
4 *
wdenk5b845b62002-08-21 21:57:24 +00005 * (C) Copyright 2002
6 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
7 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk5b845b62002-08-21 21:57:24 +00009 */
10
11/*
wdenk5b845b62002-08-21 21:57:24 +000012 * Altera FPGA support
13 */
14#include <common.h>
Marek Vasutfda915a2014-09-16 20:33:54 +020015#include <errno.h>
wdenk5da627a2003-10-09 20:09:04 +000016#include <ACEX1K.h>
eran liberty3c735e72008-03-27 00:50:49 +010017#include <stratixII.h>
wdenk5b845b62002-08-21 21:57:24 +000018
Marek Vasut0ae16cb2014-09-16 20:21:42 +020019/* Define FPGA_DEBUG to 1 to get debug printf's */
20#define FPGA_DEBUG 0
wdenk5b845b62002-08-21 21:57:24 +000021
Marek Vasut2012f232014-09-16 21:17:51 +020022static const struct altera_fpga {
23 enum altera_family family;
24 const char *name;
25 int (*load)(Altera_desc *, const void *, size_t);
26 int (*dump)(Altera_desc *, const void *, size_t);
27 int (*info)(Altera_desc *);
28} altera_fpga[] = {
29#if defined(CONFIG_FPGA_ACEX1K)
30 { Altera_ACEX1K, "ACEX1K", ACEX1K_load, ACEX1K_dump, ACEX1K_info },
31 { Altera_CYC2, "ACEX1K", ACEX1K_load, ACEX1K_dump, ACEX1K_info },
32#elif defined(CONFIG_FPGA_CYCLON2)
33 { Altera_ACEX1K, "CycloneII", CYC2_load, CYC2_dump, CYC2_info },
34 { Altera_CYC2, "CycloneII", CYC2_load, CYC2_dump, CYC2_info },
35#endif
36#if defined(CONFIG_FPGA_STRATIX_II)
37 { Altera_StratixII, "StratixII", StratixII_load,
38 StratixII_dump, StratixII_info },
39#endif
Pavel Machek230fe9b2014-09-08 14:08:45 +020040#if defined(CONFIG_FPGA_SOCFPGA)
41 { Altera_SoCFPGA, "SoC FPGA", socfpga_load, NULL, NULL },
42#endif
Marek Vasut2012f232014-09-16 21:17:51 +020043};
44
Marek Vasut54c96b12014-09-16 20:32:51 +020045static int altera_validate(Altera_desc *desc, const char *fn)
46{
47 if (!desc) {
48 printf("%s: NULL descriptor!\n", fn);
Marek Vasutfda915a2014-09-16 20:33:54 +020049 return -EINVAL;
Marek Vasut54c96b12014-09-16 20:32:51 +020050 }
51
52 if ((desc->family < min_altera_type) ||
53 (desc->family > max_altera_type)) {
54 printf("%s: Invalid family type, %d\n", fn, desc->family);
Marek Vasutfda915a2014-09-16 20:33:54 +020055 return -EINVAL;
Marek Vasut54c96b12014-09-16 20:32:51 +020056 }
57
58 if ((desc->iface < min_altera_iface_type) ||
59 (desc->iface > max_altera_iface_type)) {
60 printf("%s: Invalid Interface type, %d\n", fn, desc->iface);
Marek Vasutfda915a2014-09-16 20:33:54 +020061 return -EINVAL;
Marek Vasut54c96b12014-09-16 20:32:51 +020062 }
63
64 if (!desc->size) {
65 printf("%s: NULL part size\n", fn);
Marek Vasutfda915a2014-09-16 20:33:54 +020066 return -EINVAL;
Marek Vasut54c96b12014-09-16 20:32:51 +020067 }
68
Marek Vasutfda915a2014-09-16 20:33:54 +020069 return 0;
Marek Vasut54c96b12014-09-16 20:32:51 +020070}
wdenk5da627a2003-10-09 20:09:04 +000071
Marek Vasut2012f232014-09-16 21:17:51 +020072static const struct altera_fpga *
73altera_desc_to_fpga(Altera_desc *desc, const char *fn)
74{
75 int i;
76
77 if (altera_validate(desc, fn)) {
78 printf("%s: Invalid device descriptor\n", fn);
79 return NULL;
80 }
81
82 for (i = 0; i < ARRAY_SIZE(altera_fpga); i++) {
83 if (desc->family == altera_fpga[i].family)
84 break;
85 }
86
87 if (i == ARRAY_SIZE(altera_fpga)) {
88 printf("%s: Unsupported family type, %d\n", fn, desc->family);
89 return NULL;
90 }
91
92 return &altera_fpga[i];
93}
94
Wolfgang Denke6a857d2011-07-30 13:33:49 +000095int altera_load(Altera_desc *desc, const void *buf, size_t bsize)
wdenk5b845b62002-08-21 21:57:24 +000096{
Marek Vasut2012f232014-09-16 21:17:51 +020097 const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__);
wdenk5da627a2003-10-09 20:09:04 +000098
Marek Vasut2012f232014-09-16 21:17:51 +020099 if (!fpga)
Marek Vasut4a4c0a52014-09-16 20:29:24 +0200100 return FPGA_FAIL;
Marek Vasut4a4c0a52014-09-16 20:29:24 +0200101
Marek Vasut2012f232014-09-16 21:17:51 +0200102 debug_cond(FPGA_DEBUG, "%s: Launching the %s Loader...\n",
103 __func__, fpga->name);
104 if (fpga->load)
105 return fpga->load(desc, buf, bsize);
106 return 0;
wdenk5b845b62002-08-21 21:57:24 +0000107}
108
Wolfgang Denke6a857d2011-07-30 13:33:49 +0000109int altera_dump(Altera_desc *desc, const void *buf, size_t bsize)
wdenk5b845b62002-08-21 21:57:24 +0000110{
Marek Vasut2012f232014-09-16 21:17:51 +0200111 const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__);
wdenk5da627a2003-10-09 20:09:04 +0000112
Marek Vasut2012f232014-09-16 21:17:51 +0200113 if (!fpga)
Marek Vasut4a4c0a52014-09-16 20:29:24 +0200114 return FPGA_FAIL;
Marek Vasut4a4c0a52014-09-16 20:29:24 +0200115
Marek Vasut2012f232014-09-16 21:17:51 +0200116 debug_cond(FPGA_DEBUG, "%s: Launching the %s Reader...\n",
117 __func__, fpga->name);
118 if (fpga->dump)
119 return fpga->dump(desc, buf, bsize);
120 return 0;
wdenk5b845b62002-08-21 21:57:24 +0000121}
122
Marek Vasut4a4c0a52014-09-16 20:29:24 +0200123int altera_info(Altera_desc *desc)
wdenk5b845b62002-08-21 21:57:24 +0000124{
Marek Vasut2012f232014-09-16 21:17:51 +0200125 const struct altera_fpga *fpga = altera_desc_to_fpga(desc, __func__);
wdenk5da627a2003-10-09 20:09:04 +0000126
Marek Vasut2012f232014-09-16 21:17:51 +0200127 if (!fpga)
Marek Vasut4a4c0a52014-09-16 20:29:24 +0200128 return FPGA_FAIL;
Marek Vasut4a4c0a52014-09-16 20:29:24 +0200129
Marek Vasut2012f232014-09-16 21:17:51 +0200130 printf("Family: \t%s\n", fpga->name);
Marek Vasut4a4c0a52014-09-16 20:29:24 +0200131
132 printf("Interface type:\t");
133 switch (desc->iface) {
134 case passive_serial:
135 printf("Passive Serial (PS)\n");
136 break;
137 case passive_parallel_synchronous:
138 printf("Passive Parallel Synchronous (PPS)\n");
139 break;
140 case passive_parallel_asynchronous:
141 printf("Passive Parallel Asynchronous (PPA)\n");
142 break;
143 case passive_serial_asynchronous:
144 printf("Passive Serial Asynchronous (PSA)\n");
145 break;
146 case altera_jtag_mode: /* Not used */
147 printf("JTAG Mode\n");
148 break;
149 case fast_passive_parallel:
150 printf("Fast Passive Parallel (FPP)\n");
151 break;
152 case fast_passive_parallel_security:
153 printf("Fast Passive Parallel with Security (FPPS)\n");
154 break;
155 /* Add new interface types here */
156 default:
157 printf("Unsupported interface type, %d\n", desc->iface);
158 }
159
160 printf("Device Size: \t%zd bytes\n"
161 "Cookie: \t0x%x (%d)\n",
162 desc->size, desc->cookie, desc->cookie);
163
164 if (desc->iface_fns) {
165 printf("Device Function Table @ 0x%p\n", desc->iface_fns);
Marek Vasut2012f232014-09-16 21:17:51 +0200166 if (fpga->info)
167 fpga->info(desc);
wdenk5da627a2003-10-09 20:09:04 +0000168 } else {
Marek Vasut4a4c0a52014-09-16 20:29:24 +0200169 printf("No Device Function Table.\n");
wdenk5da627a2003-10-09 20:09:04 +0000170 }
171
Marek Vasut2012f232014-09-16 21:17:51 +0200172 return FPGA_SUCCESS;
wdenk5da627a2003-10-09 20:09:04 +0000173}