xref: /rk3399_rockchip-uboot/board/spear/x600/fpga.c (revision 05d134b084590684bcf4d832c0035952727b7cd9)
1995b72ddSStefan Roese /*
2995b72ddSStefan Roese  * Copyright (C) 2012 Stefan Roese <sr@denx.de>
3995b72ddSStefan Roese  *
41a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
5995b72ddSStefan Roese  */
6995b72ddSStefan Roese 
7995b72ddSStefan Roese #include <common.h>
8995b72ddSStefan Roese #include <spartan3.h>
9995b72ddSStefan Roese #include <command.h>
10995b72ddSStefan Roese #include <asm/gpio.h>
11995b72ddSStefan Roese #include <asm/io.h>
12995b72ddSStefan Roese #include <asm/arch/hardware.h>
13995b72ddSStefan Roese #include <asm/arch/spr_misc.h>
14995b72ddSStefan Roese #include <asm/arch/spr_ssp.h>
15995b72ddSStefan Roese 
16995b72ddSStefan Roese /*
17995b72ddSStefan Roese  * FPGA program pin configuration on X600:
18995b72ddSStefan Roese  *
19995b72ddSStefan Roese  * Only PROG and DONE are connected to GPIOs. INIT is not connected to the
20995b72ddSStefan Roese  * SoC at all. And CLOCK and DATA are connected to the SSP2 port. We use
21995b72ddSStefan Roese  * 16bit serial writes via this SSP port to write the data bits into the
22995b72ddSStefan Roese  * FPGA.
23995b72ddSStefan Roese  */
24995b72ddSStefan Roese #define CONFIG_SYS_FPGA_PROG		2
25995b72ddSStefan Roese #define CONFIG_SYS_FPGA_DONE		3
26995b72ddSStefan Roese 
27995b72ddSStefan Roese /*
28995b72ddSStefan Roese  * Set the active-low FPGA reset signal.
29995b72ddSStefan Roese  */
fpga_reset(int assert)30995b72ddSStefan Roese static void fpga_reset(int assert)
31995b72ddSStefan Roese {
32995b72ddSStefan Roese 	/*
33995b72ddSStefan Roese 	 * On x600 we have no means to toggle the FPGA reset signal
34995b72ddSStefan Roese 	 */
35995b72ddSStefan Roese 	debug("%s:%d: RESET (%d)\n", __func__, __LINE__, assert);
36995b72ddSStefan Roese }
37995b72ddSStefan Roese 
38995b72ddSStefan Roese /*
39995b72ddSStefan Roese  * Set the FPGA's active-low SelectMap program line to the specified level
40995b72ddSStefan Roese  */
fpga_pgm_fn(int assert,int flush,int cookie)41995b72ddSStefan Roese static int fpga_pgm_fn(int assert, int flush, int cookie)
42995b72ddSStefan Roese {
43995b72ddSStefan Roese 	debug("%s:%d: FPGA PROG (%d)\n", __func__, __LINE__, assert);
44995b72ddSStefan Roese 
45995b72ddSStefan Roese 	gpio_set_value(CONFIG_SYS_FPGA_PROG, assert);
46995b72ddSStefan Roese 
47995b72ddSStefan Roese 	return assert;
48995b72ddSStefan Roese }
49995b72ddSStefan Roese 
50995b72ddSStefan Roese /*
51995b72ddSStefan Roese  * Test the state of the active-low FPGA INIT line.  Return 1 on INIT
52995b72ddSStefan Roese  * asserted (low).
53995b72ddSStefan Roese  */
fpga_init_fn(int cookie)54995b72ddSStefan Roese static int fpga_init_fn(int cookie)
55995b72ddSStefan Roese {
56995b72ddSStefan Roese 	static int state;
57995b72ddSStefan Roese 
58995b72ddSStefan Roese 	debug("%s:%d: init (state=%d)\n", __func__, __LINE__, state);
59995b72ddSStefan Roese 
60995b72ddSStefan Roese 	/*
61995b72ddSStefan Roese 	 * On x600, the FPGA INIT signal is not connected to the SoC.
62995b72ddSStefan Roese 	 * We can't read the INIT status. Let's return the "correct"
63995b72ddSStefan Roese 	 * INIT signal state generated via a local state-machine.
64995b72ddSStefan Roese 	 */
65995b72ddSStefan Roese 	if (++state == 1) {
66995b72ddSStefan Roese 		return 1;
67995b72ddSStefan Roese 	} else {
68995b72ddSStefan Roese 		state = 0;
69995b72ddSStefan Roese 		return 0;
70995b72ddSStefan Roese 	}
71995b72ddSStefan Roese }
72995b72ddSStefan Roese 
73995b72ddSStefan Roese /*
74995b72ddSStefan Roese  * Test the state of the active-high FPGA DONE pin
75995b72ddSStefan Roese  */
fpga_done_fn(int cookie)76995b72ddSStefan Roese static int fpga_done_fn(int cookie)
77995b72ddSStefan Roese {
78995b72ddSStefan Roese 	struct ssp_regs *ssp = (struct ssp_regs *)CONFIG_SSP2_BASE;
79995b72ddSStefan Roese 
80995b72ddSStefan Roese 	/*
81995b72ddSStefan Roese 	 * Wait for Tx-FIFO to become empty before looking for DONE
82995b72ddSStefan Roese 	 */
83995b72ddSStefan Roese 	while (!(readl(&ssp->sspsr) & SSPSR_TFE))
84995b72ddSStefan Roese 		;
85995b72ddSStefan Roese 
86995b72ddSStefan Roese 	if (gpio_get_value(CONFIG_SYS_FPGA_DONE))
87995b72ddSStefan Roese 		return 1;
88995b72ddSStefan Roese 	else
89995b72ddSStefan Roese 		return 0;
90995b72ddSStefan Roese }
91995b72ddSStefan Roese 
92995b72ddSStefan Roese /*
93995b72ddSStefan Roese  * FPGA pre-configuration function. Just make sure that
94995b72ddSStefan Roese  * FPGA reset is asserted to keep the FPGA from starting up after
95995b72ddSStefan Roese  * configuration.
96995b72ddSStefan Roese  */
fpga_pre_config_fn(int cookie)97995b72ddSStefan Roese static int fpga_pre_config_fn(int cookie)
98995b72ddSStefan Roese {
99995b72ddSStefan Roese 	debug("%s:%d: FPGA pre-configuration\n", __func__, __LINE__);
100472d5460SYork Sun 	fpga_reset(true);
101995b72ddSStefan Roese 
102995b72ddSStefan Roese 	return 0;
103995b72ddSStefan Roese }
104995b72ddSStefan Roese 
105995b72ddSStefan Roese /*
106995b72ddSStefan Roese  * FPGA post configuration function. Blip the FPGA reset line and then see if
107995b72ddSStefan Roese  * the FPGA appears to be running.
108995b72ddSStefan Roese  */
fpga_post_config_fn(int cookie)109995b72ddSStefan Roese static int fpga_post_config_fn(int cookie)
110995b72ddSStefan Roese {
111995b72ddSStefan Roese 	int rc = 0;
112995b72ddSStefan Roese 
113995b72ddSStefan Roese 	debug("%s:%d: FPGA post configuration\n", __func__, __LINE__);
114995b72ddSStefan Roese 
115472d5460SYork Sun 	fpga_reset(true);
116995b72ddSStefan Roese 	udelay(100);
117472d5460SYork Sun 	fpga_reset(false);
118995b72ddSStefan Roese 	udelay(100);
119995b72ddSStefan Roese 
120995b72ddSStefan Roese 	return rc;
121995b72ddSStefan Roese }
122995b72ddSStefan Roese 
fpga_clk_fn(int assert_clk,int flush,int cookie)123995b72ddSStefan Roese static int fpga_clk_fn(int assert_clk, int flush, int cookie)
124995b72ddSStefan Roese {
125995b72ddSStefan Roese 	/*
126995b72ddSStefan Roese 	 * No dedicated clock signal on x600 (data & clock generated)
127995b72ddSStefan Roese 	 * in SSP interface. So we don't have to do anything here.
128995b72ddSStefan Roese 	 */
129995b72ddSStefan Roese 	return assert_clk;
130995b72ddSStefan Roese }
131995b72ddSStefan Roese 
fpga_wr_fn(int assert_write,int flush,int cookie)132995b72ddSStefan Roese static int fpga_wr_fn(int assert_write, int flush, int cookie)
133995b72ddSStefan Roese {
134995b72ddSStefan Roese 	struct ssp_regs *ssp = (struct ssp_regs *)CONFIG_SSP2_BASE;
135995b72ddSStefan Roese 	static int count;
136995b72ddSStefan Roese 	static u16 data;
137995b72ddSStefan Roese 
138995b72ddSStefan Roese 	/*
139995b72ddSStefan Roese 	 * First collect 16 bits of data
140995b72ddSStefan Roese 	 */
141995b72ddSStefan Roese 	data = data << 1;
142995b72ddSStefan Roese 	if (assert_write)
143995b72ddSStefan Roese 		data |= 1;
144995b72ddSStefan Roese 
145995b72ddSStefan Roese 	/*
146995b72ddSStefan Roese 	 * If 16 bits are not available, return for more bits
147995b72ddSStefan Roese 	 */
148995b72ddSStefan Roese 	count++;
149995b72ddSStefan Roese 	if (count != 16)
150995b72ddSStefan Roese 		return assert_write;
151995b72ddSStefan Roese 
152995b72ddSStefan Roese 	count = 0;
153995b72ddSStefan Roese 
154995b72ddSStefan Roese 	/*
155995b72ddSStefan Roese 	 * Wait for Tx-FIFO to become ready
156995b72ddSStefan Roese 	 */
157995b72ddSStefan Roese 	while (!(readl(&ssp->sspsr) & SSPSR_TNF))
158995b72ddSStefan Roese 		;
159995b72ddSStefan Roese 
160995b72ddSStefan Roese 	/* Send 16 bits to FPGA via SSP bus */
161995b72ddSStefan Roese 	writel(data, &ssp->sspdr);
162995b72ddSStefan Roese 
163995b72ddSStefan Roese 	return assert_write;
164995b72ddSStefan Roese }
165995b72ddSStefan Roese 
1662a6e3869SMichal Simek static xilinx_spartan3_slave_serial_fns x600_fpga_fns = {
167995b72ddSStefan Roese 	fpga_pre_config_fn,
168995b72ddSStefan Roese 	fpga_pgm_fn,
169995b72ddSStefan Roese 	fpga_clk_fn,
170995b72ddSStefan Roese 	fpga_init_fn,
171995b72ddSStefan Roese 	fpga_done_fn,
172995b72ddSStefan Roese 	fpga_wr_fn,
173995b72ddSStefan Roese 	fpga_post_config_fn,
174995b72ddSStefan Roese };
175995b72ddSStefan Roese 
176*f8c1be98SMichal Simek static xilinx_desc fpga[CONFIG_FPGA_COUNT] = {
177995b72ddSStefan Roese 	XILINX_XC3S1200E_DESC(slave_serial, &x600_fpga_fns, 0)
178995b72ddSStefan Roese };
179995b72ddSStefan Roese 
180995b72ddSStefan Roese /*
181995b72ddSStefan Roese  * Initialize the SelectMap interface.  We assume that the mode and the
182995b72ddSStefan Roese  * initial state of all of the port pins have already been set!
183995b72ddSStefan Roese  */
fpga_serialslave_init(void)184995b72ddSStefan Roese static void fpga_serialslave_init(void)
185995b72ddSStefan Roese {
186995b72ddSStefan Roese 	debug("%s:%d: Initialize serial slave interface\n", __func__, __LINE__);
187472d5460SYork Sun 	fpga_pgm_fn(false, false, 0);	/* make sure program pin is inactive */
188995b72ddSStefan Roese }
189995b72ddSStefan Roese 
expi_setup(int freq)190995b72ddSStefan Roese static int expi_setup(int freq)
191995b72ddSStefan Roese {
192995b72ddSStefan Roese 	struct misc_regs *misc = (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
193995b72ddSStefan Roese 	int pll2_m, pll2_n, pll2_p, expi_x, expi_y;
194995b72ddSStefan Roese 
195995b72ddSStefan Roese 	pll2_m = (freq * 2) / 1000;
196995b72ddSStefan Roese 	pll2_n = 15;
197995b72ddSStefan Roese 	pll2_p = 1;
198995b72ddSStefan Roese 	expi_x = 1;
199995b72ddSStefan Roese 	expi_y = 2;
200995b72ddSStefan Roese 
201995b72ddSStefan Roese 	/*
202995b72ddSStefan Roese 	 * Disable reset, Low compression, Disable retiming, Enable Expi,
203995b72ddSStefan Roese 	 * Enable soft reset, DMA, PLL2, Internal
204995b72ddSStefan Roese 	 */
205995b72ddSStefan Roese 	writel(EXPI_CLK_CFG_LOW_COMPR | EXPI_CLK_CFG_CLK_EN | EXPI_CLK_CFG_RST |
206995b72ddSStefan Roese 	       EXPI_CLK_SYNT_EN | EXPI_CLK_CFG_SEL_PLL2 |
207995b72ddSStefan Roese 	       EXPI_CLK_CFG_INT_CLK_EN | (expi_y << 16) | (expi_x << 24),
208995b72ddSStefan Roese 	       &misc->expi_clk_cfg);
209995b72ddSStefan Roese 
210995b72ddSStefan Roese 	/*
211995b72ddSStefan Roese 	 * 6 uA, Internal feedback, 1st order, Non-dithered, Sample Parameters,
212995b72ddSStefan Roese 	 * Enable PLL2, Disable reset
213995b72ddSStefan Roese 	 */
214995b72ddSStefan Roese 	writel((pll2_m << 24) | (pll2_p << 8) | (pll2_n), &misc->pll2_frq);
215995b72ddSStefan Roese 	writel(PLL2_CNTL_6UA | PLL2_CNTL_SAMPLE | PLL2_CNTL_ENABLE |
216995b72ddSStefan Roese 	       PLL2_CNTL_RESETN | PLL2_CNTL_LOCK, &misc->pll2_cntl);
217995b72ddSStefan Roese 
218995b72ddSStefan Roese 	/*
219995b72ddSStefan Roese 	 * Disable soft reset
220995b72ddSStefan Roese 	 */
221995b72ddSStefan Roese 	clrbits_le32(&misc->expi_clk_cfg, EXPI_CLK_CFG_RST);
222995b72ddSStefan Roese 
223995b72ddSStefan Roese 	return 0;
224995b72ddSStefan Roese }
225995b72ddSStefan Roese 
226995b72ddSStefan Roese /*
227995b72ddSStefan Roese  * Initialize the fpga
228995b72ddSStefan Roese  */
x600_init_fpga(void)229995b72ddSStefan Roese int x600_init_fpga(void)
230995b72ddSStefan Roese {
231995b72ddSStefan Roese 	struct ssp_regs *ssp = (struct ssp_regs *)CONFIG_SSP2_BASE;
232995b72ddSStefan Roese 	struct misc_regs *misc = (struct misc_regs *)CONFIG_SPEAR_MISCBASE;
233995b72ddSStefan Roese 
234995b72ddSStefan Roese 	/* Enable SSP2 clock */
235995b72ddSStefan Roese 	writel(readl(&misc->periph1_clken) | MISC_SSP2ENB | MISC_GPIO4ENB,
236995b72ddSStefan Roese 	       &misc->periph1_clken);
237995b72ddSStefan Roese 
238995b72ddSStefan Roese 	/* Set EXPI clock to 45 MHz */
239995b72ddSStefan Roese 	expi_setup(45000);
240995b72ddSStefan Roese 
241995b72ddSStefan Roese 	/* Configure GPIO directions */
242995b72ddSStefan Roese 	gpio_direction_output(CONFIG_SYS_FPGA_PROG, 0);
243995b72ddSStefan Roese 	gpio_direction_input(CONFIG_SYS_FPGA_DONE);
244995b72ddSStefan Roese 
245995b72ddSStefan Roese 	writel(SSPCR0_DSS_16BITS, &ssp->sspcr0);
246995b72ddSStefan Roese 	writel(SSPCR1_SSE, &ssp->sspcr1);
247995b72ddSStefan Roese 
248995b72ddSStefan Roese 	/*
249995b72ddSStefan Roese 	 * Set lowest prescale divisor value (CPSDVSR) of 2 for max download
250995b72ddSStefan Roese 	 * speed.
251995b72ddSStefan Roese 	 *
252995b72ddSStefan Roese 	 * Actual data clock rate is: 80MHz / (CPSDVSR * (SCR + 1))
253995b72ddSStefan Roese 	 * With CPSDVSR at 2 and SCR at 0, the maximume clock rate is 40MHz.
254995b72ddSStefan Roese 	 */
255995b72ddSStefan Roese 	writel(2, &ssp->sspcpsr);
256995b72ddSStefan Roese 
257995b72ddSStefan Roese 	fpga_init();
258995b72ddSStefan Roese 	fpga_serialslave_init();
259995b72ddSStefan Roese 
260995b72ddSStefan Roese 	debug("%s:%d: Adding fpga 0\n", __func__, __LINE__);
261995b72ddSStefan Roese 	fpga_add(fpga_xilinx, &fpga[0]);
262995b72ddSStefan Roese 
263995b72ddSStefan Roese 	return 0;
264995b72ddSStefan Roese }
265