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