]>
git.ipfire.org Git - people/ms/u-boot.git/blob - board/siemens/common/fpga.c
3 * Wolfgang Grandegger, DENX Software Engineering, wg@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <linux/ctype.h>
32 int power_on_reset(void);
34 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
37 static int fpga_get_version(fpga_t
* fpga
, char* name
)
41 * Net-list string format:
42 * "vvvvvvvvddddddddn...".
44 * "0000000322042002PUMA" = PUMA version 3 from 22.04.2002.
46 if (strlen(name
) < (16 + strlen(fpga
->name
)))
49 if (strcmp(&name
[16], fpga
->name
) != 0)
51 /* Get version number */
52 memcpy(vname
, name
, 8);
54 return simple_strtoul(vname
, NULL
, 16);
57 printf("Image name %s is invalid\n", name
);
61 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
63 static fpga_t
* fpga_get(char* fpga_name
)
65 char name
[FPGA_NAME_LEN
];
68 if (strlen(fpga_name
) >= FPGA_NAME_LEN
)
70 for (i
= 0; i
< strlen(fpga_name
); i
++)
71 name
[i
] = toupper(fpga_name
[i
]);
73 for (i
= 0; i
< fpga_count
; i
++) {
74 if (strcmp(name
, fpga_list
[i
].name
) == 0)
78 printf("FPGA: name %s is invalid\n", fpga_name
);
82 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
84 static void fpga_status (fpga_t
* fpga
)
87 if (fpga_control(fpga
, FPGA_DONE_IS_HIGH
))
88 printf ("%s is loaded (%08lx)\n",
89 fpga
->name
, fpga_control(fpga
, FPGA_GET_ID
));
91 printf ("%s is NOT loaded\n", fpga
->name
);
94 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
96 #define FPGA_RESET_TIMEOUT 100 /* = 10 ms */
98 static int fpga_reset (fpga_t
* fpga
)
102 /* Set PROG to low and wait til INIT goes low */
103 fpga_control(fpga
, FPGA_PROG_SET_LOW
);
104 for (i
= 0; i
< FPGA_RESET_TIMEOUT
; i
++) {
106 if (!fpga_control(fpga
, FPGA_INIT_IS_HIGH
))
109 if (i
== FPGA_RESET_TIMEOUT
)
112 /* Set PROG to high and wait til INIT goes high */
113 fpga_control(fpga
, FPGA_PROG_SET_HIGH
);
114 for (i
= 0; i
< FPGA_RESET_TIMEOUT
; i
++) {
116 if (fpga_control(fpga
, FPGA_INIT_IS_HIGH
))
119 if (i
== FPGA_RESET_TIMEOUT
)
127 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
129 #define FPGA_LOAD_TIMEOUT 100 /* = 10 ms */
131 static int fpga_load (fpga_t
* fpga
, ulong addr
, int checkall
)
133 volatile uchar
*fpga_addr
= (volatile uchar
*)fpga
->conf_base
;
136 uchar
*data
= (uchar
*)&hdr
;
141 * Check the image header and data of the net-list
143 memcpy (&hdr
, (char *)addr
, sizeof(image_header_t
));
145 if (hdr
.ih_magic
!= IH_MAGIC
) {
146 strcpy (msg
, "Bad Image Magic Number");
150 len
= sizeof(image_header_t
);
152 checksum
= hdr
.ih_hcrc
;
155 if (crc32 (0, data
, len
) != checksum
) {
156 strcpy (msg
, "Bad Image Header CRC");
160 data
= (uchar
*)(addr
+ sizeof(image_header_t
));
163 s
= getenv ("verify");
164 verify
= (s
&& (*s
== 'n')) ? 0 : 1;
166 if (crc32 (0, data
, len
) != hdr
.ih_dcrc
) {
167 strcpy (msg
, "Bad Image Data CRC");
172 if (checkall
&& fpga_get_version(fpga
, (char *)(hdr
.ih_name
)) < 0)
180 * Reset FPGA and wait for completion
182 if (fpga_reset(fpga
)) {
183 strcpy (msg
, "Reset Timeout");
187 printf ("(%s)... ", hdr
.ih_name
);
191 fpga_control (fpga
, FPGA_LOAD_MODE
);
193 *fpga_addr
= *data
++;
195 fpga_control (fpga
, FPGA_READ_MODE
);
198 * Wait for completion and check error status if timeout
200 for (i
= 0; i
< FPGA_LOAD_TIMEOUT
; i
++) {
202 if (fpga_control (fpga
, FPGA_DONE_IS_HIGH
))
205 if (i
== FPGA_LOAD_TIMEOUT
) {
206 if (fpga_control(fpga
, FPGA_INIT_IS_HIGH
))
207 strcpy(msg
, "Invalid Size");
209 strcpy(msg
, "CRC Error");
218 printf("ERROR: %s\n", msg
);
222 #if defined(CONFIG_CMD_BSP)
224 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
226 int do_fpga (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
235 if (strncmp(argv
[1], "stat", 4) == 0) { /* status */
237 for (i
= 0; i
< fpga_count
; i
++) {
238 fpga_status (&fpga_list
[i
]);
241 else if (argc
== 3) {
242 if ((fpga
= fpga_get(argv
[2])) == 0)
249 else if (strcmp(argv
[1],"load") == 0) { /* load */
250 if (argc
== 3 && fpga_count
== 1) {
251 fpga
= &fpga_list
[0];
253 else if (argc
== 4) {
254 if ((fpga
= fpga_get(argv
[2])) == 0)
260 addr
= simple_strtoul(argv
[argc
-1], NULL
, 16);
262 printf ("FPGA load %s: addr %08lx: ",
264 fpga_load (fpga
, addr
, 1);
267 else if (strncmp(argv
[1], "rese", 4) == 0) { /* reset */
268 if (argc
== 2 && fpga_count
== 1) {
269 fpga
= &fpga_list
[0];
271 else if (argc
== 3) {
272 if ((fpga
= fpga_get(argv
[2])) == 0)
278 printf ("FPGA reset %s: ", fpga
->name
);
279 if (fpga_reset(fpga
))
280 printf ("ERROR: Timeout\n");
290 printf ("Usage:\n%s\n", cmdtp
->usage
);
296 "fpga - access FPGA(s)\n",
297 "fpga status [name] - print FPGA status\n"
298 "fpga reset [name] - reset FPGA\n"
299 "fpga load [name] addr - load FPGA configuration data\n"
304 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
309 ulong new_id
, old_id
= 0;
316 * Port setup for FPGA control
318 for (i
= 0; i
< fpga_count
; i
++) {
319 fpga_control(&fpga_list
[i
], FPGA_INIT_PORTS
);
323 * Load FPGA(s): a new net-list is loaded if the FPGA is
324 * empty, Power-on-Reset or the old one is not up-to-date
326 for (i
= 0; i
< fpga_count
; i
++) {
327 fpga
= &fpga_list
[i
];
328 printf ("%s: ", fpga
->name
);
330 for (j
= 0; j
< strlen(fpga
->name
); j
++)
331 name
[j
] = tolower(fpga
->name
[j
]);
333 sprintf(name
, "%s_addr", name
);
335 if ((s
= getenv(name
)) != NULL
)
336 addr
= simple_strtoul(s
, NULL
, 16);
339 printf ("env. variable %s undefined\n", name
);
343 hdr
= (image_header_t
*)addr
;
344 if ((new_id
= fpga_get_version(fpga
, (char *)(hdr
->ih_name
))) == -1)
349 if (!power_on_reset() && fpga_control(fpga
, FPGA_DONE_IS_HIGH
)) {
350 old_id
= fpga_control(fpga
, FPGA_GET_ID
);
351 if (new_id
== old_id
)
357 fpga_load (fpga
, addr
, 0);
359 printf ("loaded (%08lx)\n", old_id
);