BCM WL 6.30.102.9 (r366174)
[tomato.git] / release / src-rt / cfe / cfe / arch / mips / board / carmel / src / xilinx.c
blob6d049cfd0d8f9843063e1941f228472b460a2a05
1 /* *********************************************************************
2 * SB1125 Board Support Package
3 *
4 * Xilinx programming module File: xilinx.c
6 * Routines to program Xilinx FPGAs.
7 *
8 * Author: Stefan Ludwig, Mitch Lichtenberg
9 *
10 *********************************************************************
12 * Copyright 2000,2001,2002,2003
13 * Broadcom Corporation. All rights reserved.
15 * This software is furnished under license and may be used and
16 * copied only in accordance with the following terms and
17 * conditions. Subject to these conditions, you may download,
18 * copy, install, use, modify and distribute modified or unmodified
19 * copies of this software in source and/or binary form. No title
20 * or ownership is transferred hereby.
22 * 1) Any source code used, modified or distributed must reproduce
23 * and retain this copyright notice and list of conditions
24 * as they appear in the source file.
26 * 2) No right is granted to use any trade name, trademark, or
27 * logo of Broadcom Corporation. The "Broadcom Corporation"
28 * name may not be used to endorse or promote products derived
29 * from this software without the prior written permission of
30 * Broadcom Corporation.
32 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44 * THE POSSIBILITY OF SUCH DAMAGE.
45 ********************************************************************* */
48 #include "lib_types.h"
49 #include "lib_printf.h"
50 #include "sbmips.h"
51 #include "sb1250_defs.h"
52 #include "sb1250_regs.h"
54 #include "xilinx.h"
56 #define DEBUG
58 static int xilinx_open(xpgminfo_t *xi)
60 uint64_t val = SBREADCSR(A_GPIO_DIRECTION);
63 * Save old direction register and set things the way
64 * we want for programming.
67 xi->olddir = val;
69 /* set GPIO PROGRAM_pin, DIN_pin and CCLK_pin to output */
70 val |= (xi->PGMpin | xi->DINpin | xi->CCLKpin);
72 SBWRITECSR(A_GPIO_DIRECTION,val);
73 return 0;
76 static int xilinx_close(xpgminfo_t *xi)
79 * Set the direction bits back the way they were.
82 SBWRITECSR(A_GPIO_DIRECTION,(xi->olddir));
84 return 0;
87 static int xilinx_reset(xpgminfo_t *xi)
89 int i;
91 /* pull PROG and CCLK low */
92 SBWRITECSR(A_GPIO_PIN_CLR,(xi->PGMpin | xi->CCLKpin));
94 /* wait for INIT low */
95 for (i = 0; i != XPGM_TIMEOUT; i++) {
96 if (!(SBREADCSR(A_GPIO_READ) & (xi->INITpin))) break;
99 if (i == XPGM_TIMEOUT) {
100 return XPGM_ERR_INIT_NEVER_LOW;
103 /* pull prog high */
104 SBWRITECSR(A_GPIO_PIN_SET,(xi->PGMpin));
106 /* wait for INIT high */
107 for (i = 0; i != XPGM_TIMEOUT; i++) {
108 if ((SBREADCSR(A_GPIO_READ) & (xi->INITpin))) break;
111 if (i == XPGM_TIMEOUT) {
112 return XPGM_ERR_INIT_NEVER_HIGH;
114 return 0;
118 int xilinx_program(xpgminfo_t *xi,uint8_t *configBits, int nofBits)
121 int i, rc;
123 if (xilinx_open(xi)) {
124 return XPGM_ERR_OPEN_FAILED;
127 rc = xilinx_reset(xi);
128 if (rc) {
129 xilinx_close(xi);
130 return rc;
133 /* wait for 4 us */
134 for (i = 0; i < 4000; i++) {
135 SBREADCSR(A_GPIO_READ);
138 /* load bitstream */
139 for (i = 0; i < nofBits; i++) {
140 if ((configBits[i / 8] << (i % 8)) & 0x80) {
141 SBWRITECSR(A_GPIO_PIN_SET,xi->DINpin);
143 else {
144 SBWRITECSR(A_GPIO_PIN_CLR,xi->DINpin);
146 SBWRITECSR(A_GPIO_PIN_CLR,xi->CCLKpin);
147 SBWRITECSR(A_GPIO_PIN_SET,xi->CCLKpin);
150 /* ensure INIT high */
151 #ifndef DEBUG
152 if (!(SBREADCSR(A_GPIO_READ) & xi->INITpin)) {
153 xilinx_close(xi);
154 return XPGM_ERR_INIT_NOT_HIGH;
156 #endif
159 /* wait for 4 us */
160 for (i = 0; i < 4000; i++) {
161 SBREADCSR(A_GPIO_READ);
164 /* if DONE not high, give it a few CCLKs */
165 if (!(SBREADCSR(A_GPIO_READ) & xi->DONEpin)) {
167 SBWRITECSR(A_GPIO_PIN_SET,xi->DINpin);
168 for (i = 0; i < 10; i++) {
169 SBWRITECSR(A_GPIO_PIN_CLR,xi->CCLKpin);
170 SBWRITECSR(A_GPIO_PIN_SET,xi->CCLKpin);
174 if (!(SBREADCSR(A_GPIO_READ) & xi->DONEpin)) {
175 xilinx_close(xi);
176 return XPGM_ERR_DONE_NOT_HIGH;
179 xilinx_close(xi);
180 return 0;