]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/siemens/common/fpga.c
9d719460dc0f99ea444c8bcaff509d71d96407bc
[karo-tx-uboot.git] / board / siemens / common / fpga.c
1 /*
2  * (C) Copyright 2002
3  * Wolfgang Grandegger, DENX Software Engineering, wg@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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,
21  * MA 02111-1307 USA
22  */
23
24
25 #include <common.h>
26 #include <command.h>
27 #include <linux/ctype.h>
28 #include <common.h>
29
30 #include "fpga.h"
31
32 int  power_on_reset(void);
33
34 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
35
36
37 static int fpga_get_version(fpga_t* fpga, char* name)
38 {
39     char vname[12];
40     /*
41      * Net-list string format:
42      *     "vvvvvvvvddddddddn...".
43      *     Version Date    Name
44      *     "0000000322042002PUMA" = PUMA version 3 from 22.04.2002.
45      */
46     if (strlen(name) < (16 + strlen(fpga->name)))
47         goto failure;
48     /* Check FPGA name */
49     if (strcmp(&name[16], fpga->name) != 0)
50         goto failure;
51     /* Get version number */
52     memcpy(vname, name, 8);
53     vname[8] = '\0';
54     return simple_strtoul(vname, NULL, 16);
55
56  failure:
57     printf("Image name %s is invalid\n", name);
58     return -1;
59 }
60
61 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
62
63 static fpga_t* fpga_get(char* fpga_name)
64 {
65     char name[FPGA_NAME_LEN];
66     int i;
67
68     if (strlen(fpga_name) >= FPGA_NAME_LEN)
69         goto failure;
70     for (i = 0; i < strlen(fpga_name); i++)
71         name[i] = toupper(fpga_name[i]);
72     name[i] = '\0';
73     for (i = 0; i < fpga_count; i++) {
74         if (strcmp(name, fpga_list[i].name) == 0)
75             return &fpga_list[i];
76     }
77  failure:
78     printf("FPGA: name %s is invalid\n", fpga_name);
79     return NULL;
80 }
81
82 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
83
84 static void fpga_status (fpga_t* fpga)
85 {
86     /* Check state */
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));
90     else
91         printf ("%s is NOT loaded\n", fpga->name);
92 }
93
94 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
95
96 #define FPGA_RESET_TIMEOUT 100 /* = 10 ms */
97
98 static int fpga_reset (fpga_t* fpga)
99 {
100     int i;
101
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++) {
105         udelay (100);
106         if (!fpga_control(fpga, FPGA_INIT_IS_HIGH))
107             break;
108     }
109     if (i == FPGA_RESET_TIMEOUT)
110         goto failure;
111
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++) {
115         udelay (100);
116         if (fpga_control(fpga, FPGA_INIT_IS_HIGH))
117             break;
118     }
119     if (i == FPGA_RESET_TIMEOUT)
120         goto failure;
121
122     return 0;
123  failure:
124     return 1;
125 }
126
127 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
128
129 #define FPGA_LOAD_TIMEOUT 100 /* = 10 ms */
130
131 static int fpga_load (fpga_t* fpga, ulong addr, int checkall)
132 {
133     volatile uchar *fpga_addr = (volatile uchar *)fpga->conf_base;
134     image_header_t *hdr = (image_header_t *)addr;
135     ulong len;
136     uchar *data;
137     char msg[32];
138     int verify, i;
139
140     /*
141      * Check the image header and data of the net-list
142      */
143     if (!image_check_magic (hdr)) {
144         strcpy (msg, "Bad Image Magic Number");
145         goto failure;
146     }
147
148     if (!image_check_hcrc (hdr)) {
149         strcpy (msg, "Bad Image Header CRC");
150         goto failure;
151     }
152
153     data = (uchar*)image_get_data (hdr);
154     len  = image_get_data_size (hdr);
155
156     verify = getenv_verify ();
157     if (verify) {
158         if (!image_check_dcrc (hdr)) {
159             strcpy (msg, "Bad Image Data CRC");
160             goto failure;
161         }
162     }
163
164     if (checkall && fpga_get_version(fpga, image_get_name (hdr)) < 0)
165         return 1;
166
167     /* align length */
168     if (len & 1)
169         ++len;
170
171     /*
172      * Reset FPGA and wait for completion
173      */
174     if (fpga_reset(fpga)) {
175         strcpy (msg, "Reset Timeout");
176         goto failure;
177     }
178
179     printf ("(%s)... ", image_get_name (hdr));
180     /*
181      * Copy data to FPGA
182      */
183     fpga_control (fpga, FPGA_LOAD_MODE);
184     while (len--) {
185         *fpga_addr = *data++;
186     }
187     fpga_control (fpga, FPGA_READ_MODE);
188
189     /*
190      * Wait for completion and check error status if timeout
191      */
192     for (i = 0; i < FPGA_LOAD_TIMEOUT; i++) {
193         udelay (100);
194         if (fpga_control (fpga, FPGA_DONE_IS_HIGH))
195             break;
196     }
197     if (i == FPGA_LOAD_TIMEOUT) {
198         if (fpga_control(fpga, FPGA_INIT_IS_HIGH))
199             strcpy(msg, "Invalid Size");
200         else
201             strcpy(msg, "CRC Error");
202         goto failure;
203     }
204
205     printf("done\n");
206     return 0;
207
208  failure:
209
210     printf("ERROR: %s\n", msg);
211     return 1;
212 }
213
214 #if defined(CONFIG_CMD_BSP)
215
216 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
217
218 int do_fpga (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
219 {
220     ulong addr = 0;
221     int i;
222     fpga_t* fpga;
223
224     if (argc < 2)
225         goto failure;
226
227     if (strncmp(argv[1], "stat", 4) == 0) {             /* status */
228         if (argc == 2) {
229             for (i = 0; i < fpga_count; i++) {
230                 fpga_status (&fpga_list[i]);
231             }
232         }
233         else if (argc == 3) {
234             if ((fpga = fpga_get(argv[2])) == 0)
235                 goto failure;
236             fpga_status (fpga);
237         }
238         else
239             goto failure;
240     }
241     else if (strcmp(argv[1],"load") == 0) {             /* load */
242         if (argc == 3 && fpga_count == 1) {
243             fpga = &fpga_list[0];
244         }
245         else if (argc == 4) {
246             if ((fpga = fpga_get(argv[2])) == 0)
247                 goto failure;
248         }
249         else
250             goto failure;
251
252         addr = simple_strtoul(argv[argc-1], NULL, 16);
253
254         printf ("FPGA load %s: addr %08lx: ",
255                 fpga->name, addr);
256         fpga_load (fpga, addr, 1);
257
258     }
259     else if (strncmp(argv[1], "rese", 4) == 0) {        /* reset */
260         if (argc == 2 && fpga_count == 1) {
261             fpga = &fpga_list[0];
262         }
263         else if (argc == 3) {
264             if ((fpga = fpga_get(argv[2])) == 0)
265                 goto failure;
266         }
267         else
268             goto failure;
269
270         printf ("FPGA reset %s: ", fpga->name);
271         if (fpga_reset(fpga))
272             printf ("ERROR: Timeout\n");
273         else
274             printf ("done\n");
275     }
276     else
277         goto failure;
278
279     return 0;
280
281  failure:
282     printf ("Usage:\n%s\n", cmdtp->usage);
283     return 1;
284 }
285
286 U_BOOT_CMD(
287         fpga,   4,      1,      do_fpga,
288         "fpga    - access FPGA(s)\n",
289         "fpga status [name] - print FPGA status\n"
290         "fpga reset  [name] - reset FPGA\n"
291         "fpga load [name] addr - load FPGA configuration data\n"
292 );
293
294 #endif
295
296 /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
297
298 int fpga_init (void)
299 {
300     ulong addr;
301     ulong new_id, old_id = 0;
302     image_header_t *hdr;
303     fpga_t* fpga;
304     int do_load, i, j;
305     char name[16], *s;
306
307     /*
308      *  Port setup for FPGA control
309      */
310     for (i = 0; i < fpga_count; i++) {
311         fpga_control(&fpga_list[i], FPGA_INIT_PORTS);
312     }
313
314     /*
315      * Load FPGA(s): a new net-list is loaded if the FPGA is
316      * empty, Power-on-Reset or the old one is not up-to-date
317      */
318     for (i = 0; i < fpga_count; i++) {
319         fpga = &fpga_list[i];
320         printf ("%s:  ", fpga->name);
321
322         for (j = 0; j < strlen(fpga->name); j++)
323             name[j] = tolower(fpga->name[j]);
324         name[j] = '\0';
325         sprintf(name, "%s_addr", name);
326         addr = 0;
327         if ((s = getenv(name)) != NULL)
328             addr = simple_strtoul(s, NULL, 16);
329
330         if (!addr) {
331             printf ("env. variable %s undefined\n", name);
332             return 1;
333         }
334
335         hdr = (image_header_t *)addr;
336         if ((new_id = fpga_get_version(fpga, image_get_name (hdr))) == -1)
337             return 1;
338
339         do_load = 1;
340
341         if (!power_on_reset() && fpga_control(fpga, FPGA_DONE_IS_HIGH)) {
342             old_id = fpga_control(fpga, FPGA_GET_ID);
343             if (new_id == old_id)
344                 do_load = 0;
345         }
346
347         if (do_load) {
348             printf ("loading ");
349             fpga_load (fpga, addr, 0);
350         } else {
351             printf ("loaded (%08lx)\n", old_id);
352         }
353     }
354
355     return 0;
356 }