1 // SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (C) 2012 Altera Corporation <www.altera.com>
9 #include <linux/errno.h>
10 #include <asm/arch/fpga_manager.h>
11 #include <asm/arch/reset_manager.h>
12 #include <asm/arch/system_manager.h>
14 #define FPGA_TIMEOUT_CNT 0x1000000
16 static struct socfpga_fpga_manager *fpgamgr_regs =
17 (struct socfpga_fpga_manager *)SOCFPGA_FPGAMGRREGS_ADDRESS;
18 static struct socfpga_system_manager *sysmgr_regs =
19 (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
22 static void fpgamgr_set_cd_ratio(unsigned long ratio)
24 clrsetbits_le32(&fpgamgr_regs->ctrl,
25 0x3 << FPGAMGRREGS_CTRL_CDRATIO_LSB,
26 (ratio & 0x3) << FPGAMGRREGS_CTRL_CDRATIO_LSB);
29 /* Start the FPGA programming by initialize the FPGA Manager */
30 static int fpgamgr_program_init(void)
32 unsigned long msel, i;
34 /* Get the MSEL value */
35 msel = readl(&fpgamgr_regs->stat);
36 msel &= FPGAMGRREGS_STAT_MSEL_MASK;
37 msel >>= FPGAMGRREGS_STAT_MSEL_LSB;
41 * If MSEL[3] = 1, cfg width = 32 bit
44 setbits_le32(&fpgamgr_regs->ctrl,
45 FPGAMGRREGS_CTRL_CFGWDTH_MASK);
47 /* To determine the CD ratio */
48 /* MSEL[1:0] = 0, CD Ratio = 1 */
49 if ((msel & 0x3) == 0x0)
50 fpgamgr_set_cd_ratio(CDRATIO_x1);
51 /* MSEL[1:0] = 1, CD Ratio = 4 */
52 else if ((msel & 0x3) == 0x1)
53 fpgamgr_set_cd_ratio(CDRATIO_x4);
54 /* MSEL[1:0] = 2, CD Ratio = 8 */
55 else if ((msel & 0x3) == 0x2)
56 fpgamgr_set_cd_ratio(CDRATIO_x8);
58 } else { /* MSEL[3] = 0 */
59 clrbits_le32(&fpgamgr_regs->ctrl,
60 FPGAMGRREGS_CTRL_CFGWDTH_MASK);
62 /* To determine the CD ratio */
63 /* MSEL[1:0] = 0, CD Ratio = 1 */
64 if ((msel & 0x3) == 0x0)
65 fpgamgr_set_cd_ratio(CDRATIO_x1);
66 /* MSEL[1:0] = 1, CD Ratio = 2 */
67 else if ((msel & 0x3) == 0x1)
68 fpgamgr_set_cd_ratio(CDRATIO_x2);
69 /* MSEL[1:0] = 2, CD Ratio = 4 */
70 else if ((msel & 0x3) == 0x2)
71 fpgamgr_set_cd_ratio(CDRATIO_x4);
74 /* To enable FPGA Manager configuration */
75 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCE_MASK);
77 /* To enable FPGA Manager drive over configuration line */
78 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
80 /* Put FPGA into reset phase */
81 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
83 /* (1) wait until FPGA enter reset phase */
84 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
85 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_RESETPHASE)
89 /* If not in reset state, return error */
90 if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_RESETPHASE) {
91 puts("FPGA: Could not reset\n");
95 /* Release FPGA from reset phase */
96 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
98 /* (2) wait until FPGA enter configuration phase */
99 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
100 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_CFGPHASE)
104 /* If not in configuration state, return error */
105 if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_CFGPHASE) {
106 puts("FPGA: Could not configure\n");
110 /* Clear all interrupts in CB Monitor */
111 writel(0xFFF, &fpgamgr_regs->gpio_porta_eoi);
113 /* Enable AXI configuration */
114 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
119 /* Ensure the FPGA entering config done */
120 static int fpgamgr_program_poll_cd(void)
122 const uint32_t mask = FPGAMGRREGS_MON_GPIO_EXT_PORTA_NS_MASK |
123 FPGAMGRREGS_MON_GPIO_EXT_PORTA_CD_MASK;
124 unsigned long reg, i;
126 /* (3) wait until full config done */
127 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
128 reg = readl(&fpgamgr_regs->gpio_ext_porta);
132 printf("FPGA: Configuration error.\n");
136 /* Config done without error */
141 /* Timeout happened, return error */
142 if (i == FPGA_TIMEOUT_CNT) {
143 printf("FPGA: Timeout waiting for program.\n");
147 /* Disable AXI configuration */
148 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
153 /* Ensure the FPGA entering init phase */
154 static int fpgamgr_program_poll_initphase(void)
158 /* Additional clocks for the CB to enter initialization phase */
159 if (fpgamgr_dclkcnt_set(0x4))
162 /* (4) wait until FPGA enter init phase or user mode */
163 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
164 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_INITPHASE)
166 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
170 /* If not in configuration state, return error */
171 if (i == FPGA_TIMEOUT_CNT)
177 /* Ensure the FPGA entering user mode */
178 static int fpgamgr_program_poll_usermode(void)
182 /* Additional clocks for the CB to exit initialization phase */
183 if (fpgamgr_dclkcnt_set(0x5000))
186 /* (5) wait until FPGA enter user mode */
187 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
188 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
191 /* If not in configuration state, return error */
192 if (i == FPGA_TIMEOUT_CNT)
195 /* To release FPGA Manager drive over configuration line */
196 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
202 * FPGA Manager to program the FPGA. This is the interface used by FPGA driver.
203 * Return 0 for sucess, non-zero for error.
205 int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
207 unsigned long status;
209 if ((uint32_t)rbf_data & 0x3) {
210 puts("FPGA: Unaligned data, realign to 32bit boundary.\n");
214 /* Prior programming the FPGA, all bridges need to be shut off */
216 /* Disable all signals from hps peripheral controller to fpga */
217 writel(0, &sysmgr_regs->fpgaintfgrp_module);
219 /* Disable all signals from FPGA to HPS SDRAM */
220 #define SDR_CTRLGRP_FPGAPORTRST_ADDRESS 0x5080
221 writel(0, SOCFPGA_SDR_ADDRESS + SDR_CTRLGRP_FPGAPORTRST_ADDRESS);
223 /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */
224 socfpga_bridges_reset(1);
226 /* Unmap the bridges from NIC-301 */
227 writel(0x1, SOCFPGA_L3REGS_ADDRESS);
229 /* Initialize the FPGA Manager */
230 status = fpgamgr_program_init();
234 /* Write the RBF data to FPGA Manager */
235 fpgamgr_program_write(rbf_data, rbf_size);
237 /* Ensure the FPGA entering config done */
238 status = fpgamgr_program_poll_cd();
242 /* Ensure the FPGA entering init phase */
243 status = fpgamgr_program_poll_initphase();
247 /* Ensure the FPGA entering user mode */
248 return fpgamgr_program_poll_usermode();