1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2012 Stefan Roese <sr@denx.de>
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <spartan3.h>
9*4882a593Smuzhiyun #include <command.h>
10*4882a593Smuzhiyun #include <asm/gpio.h>
11*4882a593Smuzhiyun #include <asm/io.h>
12*4882a593Smuzhiyun #include <asm/arch/hardware.h>
13*4882a593Smuzhiyun #include <asm/arch/spr_misc.h>
14*4882a593Smuzhiyun #include <asm/arch/spr_ssp.h>
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun /*
17*4882a593Smuzhiyun * FPGA program pin configuration on X600:
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Only PROG and DONE are connected to GPIOs. INIT is not connected to the
20*4882a593Smuzhiyun * SoC at all. And CLOCK and DATA are connected to the SSP2 port. We use
21*4882a593Smuzhiyun * 16bit serial writes via this SSP port to write the data bits into the
22*4882a593Smuzhiyun * FPGA.
23*4882a593Smuzhiyun */
24*4882a593Smuzhiyun #define CONFIG_SYS_FPGA_PROG 2
25*4882a593Smuzhiyun #define CONFIG_SYS_FPGA_DONE 3
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun * Set the active-low FPGA reset signal.
29*4882a593Smuzhiyun */
fpga_reset(int assert)30*4882a593Smuzhiyun static void fpga_reset(int assert)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun /*
33*4882a593Smuzhiyun * On x600 we have no means to toggle the FPGA reset signal
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun debug("%s:%d: RESET (%d)\n", __func__, __LINE__, assert);
36*4882a593Smuzhiyun }
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun /*
39*4882a593Smuzhiyun * Set the FPGA's active-low SelectMap program line to the specified level
40*4882a593Smuzhiyun */
fpga_pgm_fn(int assert,int flush,int cookie)41*4882a593Smuzhiyun static int fpga_pgm_fn(int assert, int flush, int cookie)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun debug("%s:%d: FPGA PROG (%d)\n", __func__, __LINE__, assert);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun gpio_set_value(CONFIG_SYS_FPGA_PROG, assert);
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun return assert;
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun /*
51*4882a593Smuzhiyun * Test the state of the active-low FPGA INIT line. Return 1 on INIT
52*4882a593Smuzhiyun * asserted (low).
53*4882a593Smuzhiyun */
fpga_init_fn(int cookie)54*4882a593Smuzhiyun static int fpga_init_fn(int cookie)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun static int state;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun debug("%s:%d: init (state=%d)\n", __func__, __LINE__, state);
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * On x600, the FPGA INIT signal is not connected to the SoC.
62*4882a593Smuzhiyun * We can't read the INIT status. Let's return the "correct"
63*4882a593Smuzhiyun * INIT signal state generated via a local state-machine.
64*4882a593Smuzhiyun */
65*4882a593Smuzhiyun if (++state == 1) {
66*4882a593Smuzhiyun return 1;
67*4882a593Smuzhiyun } else {
68*4882a593Smuzhiyun state = 0;
69*4882a593Smuzhiyun return 0;
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun * Test the state of the active-high FPGA DONE pin
75*4882a593Smuzhiyun */
fpga_done_fn(int cookie)76*4882a593Smuzhiyun static int fpga_done_fn(int cookie)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun struct ssp_regs *ssp = (struct ssp_regs *)CONFIG_SSP2_BASE;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /*
81*4882a593Smuzhiyun * Wait for Tx-FIFO to become empty before looking for DONE
82*4882a593Smuzhiyun */
83*4882a593Smuzhiyun while (!(readl(&ssp->sspsr) & SSPSR_TFE))
84*4882a593Smuzhiyun ;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun if (gpio_get_value(CONFIG_SYS_FPGA_DONE))
87*4882a593Smuzhiyun return 1;
88*4882a593Smuzhiyun else
89*4882a593Smuzhiyun return 0;
90*4882a593Smuzhiyun }
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /*
93*4882a593Smuzhiyun * FPGA pre-configuration function. Just make sure that
94*4882a593Smuzhiyun * FPGA reset is asserted to keep the FPGA from starting up after
95*4882a593Smuzhiyun * configuration.
96*4882a593Smuzhiyun */
fpga_pre_config_fn(int cookie)97*4882a593Smuzhiyun static int fpga_pre_config_fn(int cookie)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun debug("%s:%d: FPGA pre-configuration\n", __func__, __LINE__);
100*4882a593Smuzhiyun fpga_reset(true);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun return 0;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun * FPGA post configuration function. Blip the FPGA reset line and then see if
107*4882a593Smuzhiyun * the FPGA appears to be running.
108*4882a593Smuzhiyun */
fpga_post_config_fn(int cookie)109*4882a593Smuzhiyun static int fpga_post_config_fn(int cookie)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun int rc = 0;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun debug("%s:%d: FPGA post configuration\n", __func__, __LINE__);
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun fpga_reset(true);
116*4882a593Smuzhiyun udelay(100);
117*4882a593Smuzhiyun fpga_reset(false);
118*4882a593Smuzhiyun udelay(100);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun return rc;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
fpga_clk_fn(int assert_clk,int flush,int cookie)123*4882a593Smuzhiyun static int fpga_clk_fn(int assert_clk, int flush, int cookie)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun /*
126*4882a593Smuzhiyun * No dedicated clock signal on x600 (data & clock generated)
127*4882a593Smuzhiyun * in SSP interface. So we don't have to do anything here.
128*4882a593Smuzhiyun */
129*4882a593Smuzhiyun return assert_clk;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
fpga_wr_fn(int assert_write,int flush,int cookie)132*4882a593Smuzhiyun static int fpga_wr_fn(int assert_write, int flush, int cookie)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun struct ssp_regs *ssp = (struct ssp_regs *)CONFIG_SSP2_BASE;
135*4882a593Smuzhiyun static int count;
136*4882a593Smuzhiyun static u16 data;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun * First collect 16 bits of data
140*4882a593Smuzhiyun */
141*4882a593Smuzhiyun data = data << 1;
142*4882a593Smuzhiyun if (assert_write)
143*4882a593Smuzhiyun data |= 1;
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun * If 16 bits are not available, return for more bits
147*4882a593Smuzhiyun */
148*4882a593Smuzhiyun count++;
149*4882a593Smuzhiyun if (count != 16)
150*4882a593Smuzhiyun return assert_write;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun count = 0;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun * Wait for Tx-FIFO to become ready
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun while (!(readl(&ssp->sspsr) & SSPSR_TNF))
158*4882a593Smuzhiyun ;
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /* Send 16 bits to FPGA via SSP bus */
161*4882a593Smuzhiyun writel(data, &ssp->sspdr);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun return assert_write;
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun static xilinx_spartan3_slave_serial_fns x600_fpga_fns = {
167*4882a593Smuzhiyun fpga_pre_config_fn,
168*4882a593Smuzhiyun fpga_pgm_fn,
169*4882a593Smuzhiyun fpga_clk_fn,
170*4882a593Smuzhiyun fpga_init_fn,
171*4882a593Smuzhiyun fpga_done_fn,
172*4882a593Smuzhiyun fpga_wr_fn,
173*4882a593Smuzhiyun fpga_post_config_fn,
174*4882a593Smuzhiyun };
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun static xilinx_desc fpga[CONFIG_FPGA_COUNT] = {
177*4882a593Smuzhiyun XILINX_XC3S1200E_DESC(slave_serial, &x600_fpga_fns, 0)
178*4882a593Smuzhiyun };
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /*
181*4882a593Smuzhiyun * Initialize the SelectMap interface. We assume that the mode and the
182*4882a593Smuzhiyun * initial state of all of the port pins have already been set!
183*4882a593Smuzhiyun */
fpga_serialslave_init(void)184*4882a593Smuzhiyun static void fpga_serialslave_init(void)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun debug("%s:%d: Initialize serial slave interface\n", __func__, __LINE__);
187*4882a593Smuzhiyun fpga_pgm_fn(false, false, 0); /* make sure program pin is inactive */
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
expi_setup(int freq)190*4882a593Smuzhiyun static int expi_setup(int freq)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun struct misc_regs *misc = (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
193*4882a593Smuzhiyun int pll2_m, pll2_n, pll2_p, expi_x, expi_y;
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun pll2_m = (freq * 2) / 1000;
196*4882a593Smuzhiyun pll2_n = 15;
197*4882a593Smuzhiyun pll2_p = 1;
198*4882a593Smuzhiyun expi_x = 1;
199*4882a593Smuzhiyun expi_y = 2;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun /*
202*4882a593Smuzhiyun * Disable reset, Low compression, Disable retiming, Enable Expi,
203*4882a593Smuzhiyun * Enable soft reset, DMA, PLL2, Internal
204*4882a593Smuzhiyun */
205*4882a593Smuzhiyun writel(EXPI_CLK_CFG_LOW_COMPR | EXPI_CLK_CFG_CLK_EN | EXPI_CLK_CFG_RST |
206*4882a593Smuzhiyun EXPI_CLK_SYNT_EN | EXPI_CLK_CFG_SEL_PLL2 |
207*4882a593Smuzhiyun EXPI_CLK_CFG_INT_CLK_EN | (expi_y << 16) | (expi_x << 24),
208*4882a593Smuzhiyun &misc->expi_clk_cfg);
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /*
211*4882a593Smuzhiyun * 6 uA, Internal feedback, 1st order, Non-dithered, Sample Parameters,
212*4882a593Smuzhiyun * Enable PLL2, Disable reset
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun writel((pll2_m << 24) | (pll2_p << 8) | (pll2_n), &misc->pll2_frq);
215*4882a593Smuzhiyun writel(PLL2_CNTL_6UA | PLL2_CNTL_SAMPLE | PLL2_CNTL_ENABLE |
216*4882a593Smuzhiyun PLL2_CNTL_RESETN | PLL2_CNTL_LOCK, &misc->pll2_cntl);
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun /*
219*4882a593Smuzhiyun * Disable soft reset
220*4882a593Smuzhiyun */
221*4882a593Smuzhiyun clrbits_le32(&misc->expi_clk_cfg, EXPI_CLK_CFG_RST);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun return 0;
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun * Initialize the fpga
228*4882a593Smuzhiyun */
x600_init_fpga(void)229*4882a593Smuzhiyun int x600_init_fpga(void)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun struct ssp_regs *ssp = (struct ssp_regs *)CONFIG_SSP2_BASE;
232*4882a593Smuzhiyun struct misc_regs *misc = (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /* Enable SSP2 clock */
235*4882a593Smuzhiyun writel(readl(&misc->periph1_clken) | MISC_SSP2ENB | MISC_GPIO4ENB,
236*4882a593Smuzhiyun &misc->periph1_clken);
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /* Set EXPI clock to 45 MHz */
239*4882a593Smuzhiyun expi_setup(45000);
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun /* Configure GPIO directions */
242*4882a593Smuzhiyun gpio_direction_output(CONFIG_SYS_FPGA_PROG, 0);
243*4882a593Smuzhiyun gpio_direction_input(CONFIG_SYS_FPGA_DONE);
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun writel(SSPCR0_DSS_16BITS, &ssp->sspcr0);
246*4882a593Smuzhiyun writel(SSPCR1_SSE, &ssp->sspcr1);
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /*
249*4882a593Smuzhiyun * Set lowest prescale divisor value (CPSDVSR) of 2 for max download
250*4882a593Smuzhiyun * speed.
251*4882a593Smuzhiyun *
252*4882a593Smuzhiyun * Actual data clock rate is: 80MHz / (CPSDVSR * (SCR + 1))
253*4882a593Smuzhiyun * With CPSDVSR at 2 and SCR at 0, the maximume clock rate is 40MHz.
254*4882a593Smuzhiyun */
255*4882a593Smuzhiyun writel(2, &ssp->sspcpsr);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun fpga_init();
258*4882a593Smuzhiyun fpga_serialslave_init();
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun debug("%s:%d: Adding fpga 0\n", __func__, __LINE__);
261*4882a593Smuzhiyun fpga_add(fpga_xilinx, &fpga[0]);
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun return 0;
264*4882a593Smuzhiyun }
265