1 /* 2 * Copyright (C) 2012 Altera Corporation <www.altera.com> 3 * All rights reserved. 4 * 5 * SPDX-License-Identifier: BSD-3-Clause 6 */ 7 8 #include <common.h> 9 #include <asm/io.h> 10 #include <asm/errno.h> 11 #include <asm/arch/fpga_manager.h> 12 #include <asm/arch/reset_manager.h> 13 #include <asm/arch/system_manager.h> 14 15 DECLARE_GLOBAL_DATA_PTR; 16 17 /* Timeout count */ 18 #define FPGA_TIMEOUT_CNT 0x1000000 19 20 static struct socfpga_fpga_manager *fpgamgr_regs = 21 (struct socfpga_fpga_manager *)SOCFPGA_FPGAMGRREGS_ADDRESS; 22 static struct socfpga_system_manager *sysmgr_regs = 23 (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS; 24 25 /* Set CD ratio */ 26 static void fpgamgr_set_cd_ratio(unsigned long ratio) 27 { 28 clrsetbits_le32(&fpgamgr_regs->ctrl, 29 0x3 << FPGAMGRREGS_CTRL_CDRATIO_LSB, 30 (ratio & 0x3) << FPGAMGRREGS_CTRL_CDRATIO_LSB); 31 } 32 33 static int fpgamgr_dclkcnt_set(unsigned long cnt) 34 { 35 unsigned long i; 36 37 /* Clear any existing done status */ 38 if (readl(&fpgamgr_regs->dclkstat)) 39 writel(0x1, &fpgamgr_regs->dclkstat); 40 41 /* Write the dclkcnt */ 42 writel(cnt, &fpgamgr_regs->dclkcnt); 43 44 /* Wait till the dclkcnt done */ 45 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) { 46 if (!readl(&fpgamgr_regs->dclkstat)) 47 continue; 48 49 writel(0x1, &fpgamgr_regs->dclkstat); 50 return 0; 51 } 52 53 return -ETIMEDOUT; 54 } 55 56 /* Start the FPGA programming by initialize the FPGA Manager */ 57 static int fpgamgr_program_init(void) 58 { 59 unsigned long msel, i; 60 61 /* Get the MSEL value */ 62 msel = readl(&fpgamgr_regs->stat); 63 msel &= FPGAMGRREGS_STAT_MSEL_MASK; 64 msel >>= FPGAMGRREGS_STAT_MSEL_LSB; 65 66 /* 67 * Set the cfg width 68 * If MSEL[3] = 1, cfg width = 32 bit 69 */ 70 if (msel & 0x8) { 71 setbits_le32(&fpgamgr_regs->ctrl, 72 FPGAMGRREGS_CTRL_CFGWDTH_MASK); 73 74 /* To determine the CD ratio */ 75 /* MSEL[1:0] = 0, CD Ratio = 1 */ 76 if ((msel & 0x3) == 0x0) 77 fpgamgr_set_cd_ratio(CDRATIO_x1); 78 /* MSEL[1:0] = 1, CD Ratio = 4 */ 79 else if ((msel & 0x3) == 0x1) 80 fpgamgr_set_cd_ratio(CDRATIO_x4); 81 /* MSEL[1:0] = 2, CD Ratio = 8 */ 82 else if ((msel & 0x3) == 0x2) 83 fpgamgr_set_cd_ratio(CDRATIO_x8); 84 85 } else { /* MSEL[3] = 0 */ 86 clrbits_le32(&fpgamgr_regs->ctrl, 87 FPGAMGRREGS_CTRL_CFGWDTH_MASK); 88 89 /* To determine the CD ratio */ 90 /* MSEL[1:0] = 0, CD Ratio = 1 */ 91 if ((msel & 0x3) == 0x0) 92 fpgamgr_set_cd_ratio(CDRATIO_x1); 93 /* MSEL[1:0] = 1, CD Ratio = 2 */ 94 else if ((msel & 0x3) == 0x1) 95 fpgamgr_set_cd_ratio(CDRATIO_x2); 96 /* MSEL[1:0] = 2, CD Ratio = 4 */ 97 else if ((msel & 0x3) == 0x2) 98 fpgamgr_set_cd_ratio(CDRATIO_x4); 99 } 100 101 /* To enable FPGA Manager configuration */ 102 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCE_MASK); 103 104 /* To enable FPGA Manager drive over configuration line */ 105 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK); 106 107 /* Put FPGA into reset phase */ 108 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK); 109 110 /* (1) wait until FPGA enter reset phase */ 111 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) { 112 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_RESETPHASE) 113 break; 114 } 115 116 /* If not in reset state, return error */ 117 if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_RESETPHASE) { 118 puts("FPGA: Could not reset\n"); 119 return -1; 120 } 121 122 /* Release FPGA from reset phase */ 123 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK); 124 125 /* (2) wait until FPGA enter configuration phase */ 126 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) { 127 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_CFGPHASE) 128 break; 129 } 130 131 /* If not in configuration state, return error */ 132 if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_CFGPHASE) { 133 puts("FPGA: Could not configure\n"); 134 return -2; 135 } 136 137 /* Clear all interrupts in CB Monitor */ 138 writel(0xFFF, &fpgamgr_regs->gpio_porta_eoi); 139 140 /* Enable AXI configuration */ 141 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK); 142 143 return 0; 144 } 145 146 /* Write the RBF data to FPGA Manager */ 147 static void fpgamgr_program_write(const void *rbf_data, unsigned long rbf_size) 148 { 149 uint32_t src = (uint32_t)rbf_data; 150 uint32_t dst = SOCFPGA_FPGAMGRDATA_ADDRESS; 151 152 /* Number of loops for 32-byte long copying. */ 153 uint32_t loops32 = rbf_size / 32; 154 /* Number of loops for 4-byte long copying + trailing bytes */ 155 uint32_t loops4 = DIV_ROUND_UP(rbf_size % 32, 4); 156 157 asm volatile( 158 "1: ldmia %0!, {r0-r7}\n" 159 " stmia %1!, {r0-r7}\n" 160 " sub %1, #32\n" 161 " subs %2, #1\n" 162 " bne 1b\n" 163 "2: ldr %2, [%0], #4\n" 164 " str %2, [%1]\n" 165 " subs %3, #1\n" 166 " bne 2b\n" 167 : "+r"(src), "+r"(dst), "+r"(loops32), "+r"(loops4) : 168 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "cc"); 169 } 170 171 /* Ensure the FPGA entering config done */ 172 static int fpgamgr_program_poll_cd(void) 173 { 174 const uint32_t mask = FPGAMGRREGS_MON_GPIO_EXT_PORTA_NS_MASK | 175 FPGAMGRREGS_MON_GPIO_EXT_PORTA_CD_MASK; 176 unsigned long reg, i; 177 178 /* (3) wait until full config done */ 179 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) { 180 reg = readl(&fpgamgr_regs->gpio_ext_porta); 181 182 /* Config error */ 183 if (!(reg & mask)) { 184 printf("FPGA: Configuration error.\n"); 185 return -3; 186 } 187 188 /* Config done without error */ 189 if (reg & mask) 190 break; 191 } 192 193 /* Timeout happened, return error */ 194 if (i == FPGA_TIMEOUT_CNT) { 195 printf("FPGA: Timeout waiting for program.\n"); 196 return -4; 197 } 198 199 /* Disable AXI configuration */ 200 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK); 201 202 return 0; 203 } 204 205 /* Ensure the FPGA entering init phase */ 206 static int fpgamgr_program_poll_initphase(void) 207 { 208 unsigned long i; 209 210 /* Additional clocks for the CB to enter initialization phase */ 211 if (fpgamgr_dclkcnt_set(0x4)) 212 return -5; 213 214 /* (4) wait until FPGA enter init phase or user mode */ 215 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) { 216 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_INITPHASE) 217 break; 218 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE) 219 break; 220 } 221 222 /* If not in configuration state, return error */ 223 if (i == FPGA_TIMEOUT_CNT) 224 return -6; 225 226 return 0; 227 } 228 229 /* Ensure the FPGA entering user mode */ 230 static int fpgamgr_program_poll_usermode(void) 231 { 232 unsigned long i; 233 234 /* Additional clocks for the CB to exit initialization phase */ 235 if (fpgamgr_dclkcnt_set(0x5000)) 236 return -7; 237 238 /* (5) wait until FPGA enter user mode */ 239 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) { 240 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE) 241 break; 242 } 243 /* If not in configuration state, return error */ 244 if (i == FPGA_TIMEOUT_CNT) 245 return -8; 246 247 /* To release FPGA Manager drive over configuration line */ 248 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK); 249 250 return 0; 251 } 252 253 /* 254 * FPGA Manager to program the FPGA. This is the interface used by FPGA driver. 255 * Return 0 for sucess, non-zero for error. 256 */ 257 int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size) 258 { 259 unsigned long status; 260 261 if ((uint32_t)rbf_data & 0x3) { 262 puts("FPGA: Unaligned data, realign to 32bit boundary.\n"); 263 return -EINVAL; 264 } 265 266 /* Prior programming the FPGA, all bridges need to be shut off */ 267 268 /* Disable all signals from hps peripheral controller to fpga */ 269 writel(0, &sysmgr_regs->fpgaintfgrp_module); 270 271 /* Disable all signals from FPGA to HPS SDRAM */ 272 #define SDR_CTRLGRP_FPGAPORTRST_ADDRESS 0x5080 273 writel(0, SOCFPGA_SDR_ADDRESS + SDR_CTRLGRP_FPGAPORTRST_ADDRESS); 274 275 /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */ 276 socfpga_bridges_reset(1); 277 278 /* Unmap the bridges from NIC-301 */ 279 writel(0x1, SOCFPGA_L3REGS_ADDRESS); 280 281 /* Initialize the FPGA Manager */ 282 status = fpgamgr_program_init(); 283 if (status) 284 return status; 285 286 /* Write the RBF data to FPGA Manager */ 287 fpgamgr_program_write(rbf_data, rbf_size); 288 289 /* Ensure the FPGA entering config done */ 290 status = fpgamgr_program_poll_cd(); 291 if (status) 292 return status; 293 294 /* Ensure the FPGA entering init phase */ 295 status = fpgamgr_program_poll_initphase(); 296 if (status) 297 return status; 298 299 /* Ensure the FPGA entering user mode */ 300 return fpgamgr_program_poll_usermode(); 301 } 302