Merge branch 'next'
[u-boot/qq2440-u-boot.git] / board / matrix_vision / mvblm7 / fpga.c
blobc0c5bedb2adfc8e799bacbf69fa3b4c1c3bda1a0
1 /*
2 * (C) Copyright 2002
3 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
4 * Keith Outwater, keith_outwater@mvis.com.
6 * (C) Copyright 2008
7 * Andre Schwarz, Matrix Vision GmbH, andre.schwarz@matrix-vision.de
9 * SPDX-License-Identifier: GPL-2.0+
12 #include <common.h>
13 #include <ACEX1K.h>
14 #include <command.h>
15 #include "fpga.h"
16 #include "mvblm7.h"
18 #ifdef FPGA_DEBUG
19 #define fpga_debug(fmt, args...) printf("%s: "fmt, __func__, ##args)
20 #else
21 #define fpga_debug(fmt, args...)
22 #endif
24 Altera_CYC2_Passive_Serial_fns altera_fns = {
25 fpga_null_fn,
26 fpga_config_fn,
27 fpga_status_fn,
28 fpga_done_fn,
29 fpga_wr_fn,
30 fpga_null_fn,
31 fpga_null_fn,
34 Altera_desc cyclone2 = {
35 Altera_CYC2,
36 passive_serial,
37 Altera_EP2C20_SIZE,
38 (void *) &altera_fns,
39 NULL,
43 DECLARE_GLOBAL_DATA_PTR;
45 int mvblm7_init_fpga(void)
47 fpga_debug("Initialize FPGA interface\n");
48 fpga_init();
49 fpga_add(fpga_altera, &cyclone2);
50 fpga_config_fn(0, 1, 0);
51 udelay(60);
53 return 1;
56 int fpga_null_fn(int cookie)
58 return 0;
61 int fpga_config_fn(int assert, int flush, int cookie)
63 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
64 volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
65 u32 dvo = gpio->dat;
67 fpga_debug("SET config : %s\n", assert ? "low" : "high");
68 if (assert)
69 dvo |= FPGA_CONFIG;
70 else
71 dvo &= ~FPGA_CONFIG;
73 if (flush)
74 gpio->dat = dvo;
76 return assert;
79 int fpga_done_fn(int cookie)
81 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
82 volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
83 int result = 0;
85 udelay(10);
86 fpga_debug("CONF_DONE check ... ");
87 if (gpio->dat & FPGA_CONF_DONE) {
88 fpga_debug("high\n");
89 result = 1;
90 } else
91 fpga_debug("low\n");
93 return result;
96 int fpga_status_fn(int cookie)
98 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
99 volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
100 int result = 0;
102 fpga_debug("STATUS check ... ");
103 if (gpio->dat & FPGA_STATUS) {
104 fpga_debug("high\n");
105 result = 1;
106 } else
107 fpga_debug("low\n");
109 return result;
112 int fpga_clk_fn(int assert_clk, int flush, int cookie)
114 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
115 volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
116 u32 dvo = gpio->dat;
118 fpga_debug("CLOCK %s\n", assert_clk ? "high" : "low");
119 if (assert_clk)
120 dvo |= FPGA_CCLK;
121 else
122 dvo &= ~FPGA_CCLK;
124 if (flush)
125 gpio->dat = dvo;
127 return assert_clk;
130 static inline int _write_fpga(u8 val, int dump)
132 volatile immap_t *im = (volatile immap_t *)CONFIG_SYS_IMMR;
133 volatile gpio83xx_t *gpio = (volatile gpio83xx_t *)&im->gpio[0];
134 int i;
135 u32 dvo = gpio->dat;
137 if (dump)
138 fpga_debug(" %02x -> ", val);
139 for (i = 0; i < 8; i++) {
140 dvo &= ~FPGA_CCLK;
141 gpio->dat = dvo;
142 dvo &= ~FPGA_DIN;
143 if (dump)
144 fpga_debug("%d ", val&1);
145 if (val & 1)
146 dvo |= FPGA_DIN;
147 gpio->dat = dvo;
148 dvo |= FPGA_CCLK;
149 gpio->dat = dvo;
150 val >>= 1;
152 if (dump)
153 fpga_debug("\n");
155 return 0;
158 int fpga_wr_fn(const void *buf, size_t len, int flush, int cookie)
160 unsigned char *data = (unsigned char *) buf;
161 int i;
163 fpga_debug("fpga_wr: buf %p / size %d\n", buf, len);
164 for (i = 0; i < len; i++)
165 _write_fpga(data[i], 0);
166 fpga_debug("\n");
168 return FPGA_SUCCESS;