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;
20 static void fpgamgr_set_cd_ratio(unsigned long ratio)
22 clrsetbits_le32(&fpgamgr_regs->ctrl,
23 0x3 << FPGAMGRREGS_CTRL_CDRATIO_LSB,
24 (ratio & 0x3) << FPGAMGRREGS_CTRL_CDRATIO_LSB);
27 /* Start the FPGA programming by initialize the FPGA Manager */
28 static int fpgamgr_program_init(void)
30 unsigned long msel, i;
32 /* Get the MSEL value */
33 msel = readl(&fpgamgr_regs->stat);
34 msel &= FPGAMGRREGS_STAT_MSEL_MASK;
35 msel >>= FPGAMGRREGS_STAT_MSEL_LSB;
39 * If MSEL[3] = 1, cfg width = 32 bit
42 setbits_le32(&fpgamgr_regs->ctrl,
43 FPGAMGRREGS_CTRL_CFGWDTH_MASK);
45 /* To determine the CD ratio */
46 /* MSEL[1:0] = 0, CD Ratio = 1 */
47 if ((msel & 0x3) == 0x0)
48 fpgamgr_set_cd_ratio(CDRATIO_x1);
49 /* MSEL[1:0] = 1, CD Ratio = 4 */
50 else if ((msel & 0x3) == 0x1)
51 fpgamgr_set_cd_ratio(CDRATIO_x4);
52 /* MSEL[1:0] = 2, CD Ratio = 8 */
53 else if ((msel & 0x3) == 0x2)
54 fpgamgr_set_cd_ratio(CDRATIO_x8);
56 } else { /* MSEL[3] = 0 */
57 clrbits_le32(&fpgamgr_regs->ctrl,
58 FPGAMGRREGS_CTRL_CFGWDTH_MASK);
60 /* To determine the CD ratio */
61 /* MSEL[1:0] = 0, CD Ratio = 1 */
62 if ((msel & 0x3) == 0x0)
63 fpgamgr_set_cd_ratio(CDRATIO_x1);
64 /* MSEL[1:0] = 1, CD Ratio = 2 */
65 else if ((msel & 0x3) == 0x1)
66 fpgamgr_set_cd_ratio(CDRATIO_x2);
67 /* MSEL[1:0] = 2, CD Ratio = 4 */
68 else if ((msel & 0x3) == 0x2)
69 fpgamgr_set_cd_ratio(CDRATIO_x4);
72 /* To enable FPGA Manager configuration */
73 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCE_MASK);
75 /* To enable FPGA Manager drive over configuration line */
76 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
78 /* Put FPGA into reset phase */
79 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
81 /* (1) wait until FPGA enter reset phase */
82 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
83 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_RESETPHASE)
87 /* If not in reset state, return error */
88 if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_RESETPHASE) {
89 puts("FPGA: Could not reset\n");
93 /* Release FPGA from reset phase */
94 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
96 /* (2) wait until FPGA enter configuration phase */
97 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
98 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_CFGPHASE)
102 /* If not in configuration state, return error */
103 if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_CFGPHASE) {
104 puts("FPGA: Could not configure\n");
108 /* Clear all interrupts in CB Monitor */
109 writel(0xFFF, &fpgamgr_regs->gpio_porta_eoi);
111 /* Enable AXI configuration */
112 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
117 /* Ensure the FPGA entering config done */
118 static int fpgamgr_program_poll_cd(void)
120 const uint32_t mask = FPGAMGRREGS_MON_GPIO_EXT_PORTA_NS_MASK |
121 FPGAMGRREGS_MON_GPIO_EXT_PORTA_CD_MASK;
122 unsigned long reg, i;
124 /* (3) wait until full config done */
125 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
126 reg = readl(&fpgamgr_regs->gpio_ext_porta);
130 printf("FPGA: Configuration error.\n");
134 /* Config done without error */
139 /* Timeout happened, return error */
140 if (i == FPGA_TIMEOUT_CNT) {
141 printf("FPGA: Timeout waiting for program.\n");
145 /* Disable AXI configuration */
146 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
151 /* Ensure the FPGA entering init phase */
152 static int fpgamgr_program_poll_initphase(void)
156 /* Additional clocks for the CB to enter initialization phase */
157 if (fpgamgr_dclkcnt_set(0x4))
160 /* (4) wait until FPGA enter init phase or user mode */
161 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
162 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_INITPHASE)
164 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
168 /* If not in configuration state, return error */
169 if (i == FPGA_TIMEOUT_CNT)
175 /* Ensure the FPGA entering user mode */
176 static int fpgamgr_program_poll_usermode(void)
180 /* Additional clocks for the CB to exit initialization phase */
181 if (fpgamgr_dclkcnt_set(0x5000))
184 /* (5) wait until FPGA enter user mode */
185 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
186 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
189 /* If not in configuration state, return error */
190 if (i == FPGA_TIMEOUT_CNT)
193 /* To release FPGA Manager drive over configuration line */
194 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
200 * FPGA Manager to program the FPGA. This is the interface used by FPGA driver.
201 * Return 0 for sucess, non-zero for error.
203 int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
207 if ((uint32_t)rbf_data & 0x3) {
208 puts("FPGA: Unaligned data, realign to 32bit boundary.\n");
212 /* Prior programming the FPGA, all bridges need to be shut off */
214 /* Disable all signals from hps peripheral controller to fpga */
215 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_GEN5_FPGAINFGRP_MODULE);
217 /* Disable all signals from FPGA to HPS SDRAM */
218 #define SDR_CTRLGRP_FPGAPORTRST_ADDRESS 0x5080
219 writel(0, SOCFPGA_SDR_ADDRESS + SDR_CTRLGRP_FPGAPORTRST_ADDRESS);
221 /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */
222 socfpga_bridges_reset(1);
224 /* Unmap the bridges from NIC-301 */
225 writel(0x1, SOCFPGA_L3REGS_ADDRESS);
227 /* Initialize the FPGA Manager */
228 status = fpgamgr_program_init();
232 /* Write the RBF data to FPGA Manager */
233 fpgamgr_program_write(rbf_data, rbf_size);
235 /* Ensure the FPGA entering config done */
236 status = fpgamgr_program_poll_cd();
240 /* Ensure the FPGA entering init phase */
241 status = fpgamgr_program_poll_initphase();
245 /* Ensure the FPGA entering user mode */
246 return fpgamgr_program_poll_usermode();