summaryrefslogtreecommitdiff
path: root/board/w7o/fpga.c
blob: 97af9242946dc3fb73747542245b6e6530ec85ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * (C) Copyright 2001
 * Erik Theisen, Wave 7 Optics, etheisen@mindspring.com
 *  and
 * Bill Hunter, Wave 7 Optics, william.hunter@mediaone.net
 *
 * 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
 */
#include <config.h>
#include <common.h>
#include "w7o.h"
#include <asm/processor.h>
#include "errors.h"

static void
fpga_img_write(unsigned long *src, unsigned long len, unsigned short *daddr)
{
    unsigned long i;
    volatile unsigned long val;
    volatile unsigned short *dest = daddr;	/* volatile-bypass optimizer */

    for (i = 0; i < len; i++, src++) {
	val = *src;
	*dest = (unsigned short)((val & 0xff000000L) >> 16);
	*dest = (unsigned short)((val & 0x00ff0000L) >> 8);
	*dest = (unsigned short)(val & 0x0000ff00L);
	*dest = (unsigned short)((val & 0x000000ffL) << 8);
    }

    /* Terminate programming with 4 C clocks */
    dest = daddr;
    val = *(unsigned short *)dest;
    val = *(unsigned short *)dest;
    val = *(unsigned short *)dest;
    val = *(unsigned short *)dest;

}


int
fpgaDownload(unsigned char *saddr,
	     unsigned long size,
	     unsigned short *daddr)
{
    int i;					/* index, intr disable flag */
    int start;					/* timer */
    unsigned long greg, grego;			/* GPIO & output register */
    unsigned long length;			/* image size in words */
    unsigned long *source;			/* image source addr */
    unsigned short *dest;			/* destination FPGA addr */
    volatile unsigned short *ndest;		/* temp dest FPGA addr */
    volatile unsigned short val;		/* temp val */
    unsigned long cnfg = GPIO_XCV_CNFG;		/* FPGA CNFG */
    unsigned long eirq = GPIO_XCV_IRQ;
    int retval = -1;				/* Function return value */

    /* Setup some basic values */
    length = (size / 4) + 1;			/* size in words, rounding UP
						    is OK */
    source = (unsigned long *)saddr;
    dest = (unsigned short *)daddr;

    /* Get DCR output register */
    grego = in32(IBM405GP_GPIO0_OR);

    /* Reset FPGA */
    grego &= ~GPIO_XCV_PROG;			/* PROG line low */
    out32(IBM405GP_GPIO0_OR, grego);

    /* Setup timeout timer */
    start = get_timer(0);

    /* Wait for FPGA init line */
    while(in32(IBM405GP_GPIO0_IR) & GPIO_XCV_INIT) { /* Wait INIT line low */
	/* Check for timeout - 100us max, so use 3ms */
	if (get_timer(start) > 3) {
	    printf("     failed to start init.\n");
	    log_warn(ERR_XINIT0);		/* Don't halt */

	    /* Reset line stays low */
	    goto done;				/* I like gotos... */
	}
    }

    /* Unreset FPGA */
    grego |= GPIO_XCV_PROG;			/* PROG line high */
    out32(IBM405GP_GPIO0_OR, grego);

    /* Wait for FPGA end of init period .  */
    while(!(in32(IBM405GP_GPIO0_IR) & GPIO_XCV_INIT)) { /* Wait for INIT hi */

	/* Check for timeout */
	if (get_timer(start) > 3) {
	    printf("     failed to exit init.\n");
	    log_warn(ERR_XINIT1);

	    /* Reset FPGA */
	    grego &= ~GPIO_XCV_PROG;		/* PROG line low */
	    out32(IBM405GP_GPIO0_OR, grego);

	    goto done;
	}
    }

    /* Now program FPGA ... */
    ndest = dest;
    for (i = 0; i < CONFIG_NUM_FPGAS; i++) {
	/* Toggle IRQ/GPIO */
	greg = mfdcr(CPC0_CR0);			/* get chip ctrl register */
	greg |= eirq;				/* toggle irq/gpio */
	mtdcr(CPC0_CR0, greg);			/*  ... just do it */

	/* turn on open drain for CNFG */
	greg = in32(IBM405GP_GPIO0_ODR);	/* get open drain register */
	greg |= cnfg;				/* CNFG open drain */
	out32(IBM405GP_GPIO0_ODR, greg);	/*  .. just do it */

	/* Turn output enable on for CNFG */
	greg = in32(IBM405GP_GPIO0_TCR);	/* get tristate register */
	greg |= cnfg;				/* CNFG tristate inactive */
	out32(IBM405GP_GPIO0_TCR, greg);	/*  ... just do it */

	/* Setup FPGA for programming */
	grego &= ~cnfg;				/* CONFIG line low */
	out32(IBM405GP_GPIO0_OR, grego);

	/*
	 * Program the FPGA
	 */
	printf("\n       destination: 0x%lx ", (unsigned long)ndest);

	fpga_img_write(source,  length,  (unsigned short *)ndest);

	/* Done programming */
	grego |= cnfg;				/* CONFIG line high */
	out32(IBM405GP_GPIO0_OR, grego);

	/* Turn output enable OFF for CNFG */
	greg = in32(IBM405GP_GPIO0_TCR);	/* get tristate register */
	greg &= ~cnfg;				/* CNFG tristate inactive */
	out32(IBM405GP_GPIO0_TCR, greg);	/*  ... just do it */

	/* Toggle IRQ/GPIO */
	greg = mfdcr(CPC0_CR0);			/* get chip ctrl register */
	greg &= ~eirq;				/* toggle irq/gpio */
	mtdcr(CPC0_CR0, greg);			/*  ... just do it */

	ndest = (unsigned short *)((char *)ndest + 0x00100000L); /* XXX - Next FPGA addr */
	cnfg >>= 1;				/* XXX - Next  */
	eirq >>= 1;
    }

    /* Terminate programming with 4 C clocks */
    ndest = dest;
    for (i = 0; i < CONFIG_NUM_FPGAS; i++) {
	val = *ndest;
	val = *ndest;
	val = *ndest;
	val = *ndest;
	ndest = (unsigned short *)((char *)ndest + 0x00100000L);
    }

    /* Setup timer */
    start = get_timer(0);

    /* Wait for FPGA end of programming period .  */
    while(!(in32(IBM405GP_GPIO0_IR) & GPIO_XCV_DONE)) { /* Test DONE low */

	/* Check for timeout */
	if (get_timer(start) > 3) {
	    printf("     done failed to come high.\n");
	    log_warn(ERR_XDONE1);

	    /* Reset FPGA */
	    grego &= ~GPIO_XCV_PROG;		/* PROG line low */
	    out32(IBM405GP_GPIO0_OR, grego);

	    goto done;
	}
    }

    printf("\n       FPGA load succeeded\n");
    retval = 0;					/* Program OK */

done:
    return retval;
}

/* FPGA image is stored in flash */
extern flash_info_t    flash_info[];

int init_fpga(void)
{
    unsigned int i,j,ptr;			/* General purpose */
    unsigned char bufchar;			/* General purpose character */
    unsigned char *buf;				/* Start of image pointer */
    unsigned long len;				/* Length of image */
    unsigned char *fn_buf;			/* Start of filename string */
    unsigned int fn_len;			/* Length of filename string */
    unsigned char *xcv_buf;			/* Pointer to start of image */
    unsigned long xcv_len;			/* Length of image */
    unsigned long crc;				/* 30bit crc in image */
    unsigned long calc_crc;			/* Calc'd 30bit crc */
    int retval = -1;

    /* Tell the world what we are doing */
    printf("FPGA:  ");

    /*
     * Get address of first sector where the FPGA
     * image is stored.
     */
    buf = (unsigned char *)flash_info[1].start[0];

    /*
     * Get the stored image's CRC & length.
     */
    crc = *(unsigned long *)(buf+4);		/* CRC is first long word */
    len = *(unsigned long *)(buf+8);		/* Image len is next long */

    /* Pedantic */
    if ((len < 0x133A4) || (len > 0x80000))
	goto bad_image;

    /*
     * Get the file name pointer and length.
     */
    fn_len = (*(unsigned short *)(buf+12) & 0xff); /* filename length
						      is next short */
    fn_buf = buf + 14;

    /*
     * Get the FPGA image pointer and length length.
     */
    xcv_buf = fn_buf + fn_len;			/* pointer to fpga image */
    xcv_len = len - 14 - fn_len;		/* fpga image length */

    /* Check for uninitialized FLASH */
    if ((strncmp(buf, "w7o", 3)!=0) || (len > 0x0007ffffL) || (len == 0))
	goto bad_image;

    /*
     * Calculate and Check the image's CRC.
     */
    calc_crc = crc32(0, xcv_buf, xcv_len);
    if (crc != calc_crc) {
	printf("\nfailed - bad CRC\n");
	goto done;
    }

    /* Output the file name */
    printf("file name  : ");
    for (i=0;i<fn_len;i++) {
	bufchar = fn_buf[+i];
	if (bufchar<' ' || bufchar>'~') bufchar = '.';
	putc(bufchar);
    }

    /*
     * find rest of display data
     */
    ptr = 15;					/* Offset to ncd filename
						   length in fpga image */
    j = xcv_buf[ptr];				/* Get len of ncd filename */
    if (j > 32) goto bad_image;
    ptr = ptr + j + 3;				/* skip ncd filename string +
						   3 bytes more bytes */

    /*
     * output target device string
     */
    j = xcv_buf[ptr++] - 1;			/* len of targ str less term */
    if (j > 32) goto bad_image;
    printf("\n       target     : ");
    for (i = 0; i < j; i++) {
	bufchar = (xcv_buf[ptr++]);
	if (bufchar<' ' || bufchar>'~') bufchar = '.';
	putc(bufchar);
    }

    /*
     * output compilation date string and time string
     */
    ptr += 3;					/* skip 2 bytes */
    printf("\n       synth time : ");
    j = (xcv_buf[ptr++] - 1);			/* len of date str less term */
    if (j > 32) goto bad_image;
    for (i = 0; i < j; i++) {
	bufchar = (xcv_buf[ptr++]);
	if (bufchar<' ' || bufchar>'~') bufchar = '.';
	putc(bufchar);
    }

    ptr += 3;					/* Skip 2 bytes */
    printf(" - ");
    j = (xcv_buf[ptr++] - 1);			/* slen = targ dev str len */
    if (j > 32) goto bad_image;
    for (i = 0; i < j; i++) {
	bufchar = (xcv_buf[ptr++]);
	if (bufchar<' ' || bufchar>'~') bufchar = '.';
	putc(bufchar);
    }

    /*
     * output crc and length strings
     */
    printf("\n       len & crc  : 0x%lx  0x%lx", len, crc);

    /*
     * Program the FPGA.
     */
    retval = fpgaDownload((unsigned char*)xcv_buf, xcv_len,
			  (unsigned short *)0xfd000000L);
    return retval;

bad_image:
    printf("\n       BAD FPGA image format @ %lx\n", flash_info[1].start[0]);
    log_warn(ERR_XIMAGE);
done:
    return retval;
}

void test_fpga(unsigned short *daddr)
{
    int i;
    volatile unsigned short *ndest = daddr;

    for (i = 0; i < CONFIG_NUM_FPGAS; i++) {
#if defined(CONFIG_W7OLMG)
	ndest[0x7e] = 0x55aa;
	if (ndest[0x7e] != 0x55aa)
	    log_warn(ERR_XRW1 + i);
	ndest[0x7e] = 0xaa55;
	if (ndest[0x7e] != 0xaa55)
	    log_warn(ERR_XRW1 + i);
	ndest[0x7e] = 0xc318;
	if (ndest[0x7e] != 0xc318)
	    log_warn(ERR_XRW1 + i);

#elif defined(CONFIG_W7OLMC)
	ndest[0x800] = 0x55aa;
	ndest[0x801] = 0xaa55;
	ndest[0x802] = 0xc318;
	ndest[0x4800] = 0x55aa;
	ndest[0x4801] = 0xaa55;
	ndest[0x4802] = 0xc318;
	if ((ndest[0x800] != 0x55aa) ||
	    (ndest[0x801] != 0xaa55) ||
	    (ndest[0x802] != 0xc318))
	    log_warn(ERR_XRW1 + (2 * i));       /* Auto gen error code */
	if ((ndest[0x4800] != 0x55aa) ||
	    (ndest[0x4801] != 0xaa55) ||
	    (ndest[0x4802] != 0xc318))
	    log_warn(ERR_XRW2 + (2 * i));       /* Auto gen error code */

#else
# error "Unknown W7O board configuration"
#endif
    }

    printf("       FPGA ready\n");
    return;
}