1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017-2019 Intel Corporation <www.intel.com>
8 #include <asm/arch/fpga_manager.h>
9 #include <asm/arch/reset_manager.h>
10 #include <asm/arch/system_manager.h>
11 #include <asm/arch/sdram.h>
12 #include <asm/arch/misc.h>
14 #include <asm/arch/pinmux.h>
16 #include <dm/ofnode.h>
18 #include <fs_loader.h>
21 #include <linux/bitops.h>
22 #include <linux/delay.h>
25 #define MIN_BITSTREAM_SIZECHECK 230
26 #define ENCRYPTION_OFFSET 69
27 #define COMPRESSION_OFFSET 229
28 #define FPGA_TIMEOUT_MSEC 1000 /* timeout in ms */
29 #define FPGA_TIMEOUT_CNT 0x1000000
30 #define DEFAULT_DDR_LOAD_ADDRESS 0x400
32 DECLARE_GLOBAL_DATA_PTR;
34 static const struct socfpga_fpga_manager *fpga_manager_base =
35 (void *)SOCFPGA_FPGAMGRREGS_ADDRESS;
37 static void fpgamgr_set_cd_ratio(unsigned long ratio);
39 static uint32_t fpgamgr_get_msel(void)
43 reg = readl(&fpga_manager_base->imgcfg_stat);
44 reg = (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL_SET_MSD) >>
45 ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL0_LSB;
50 static void fpgamgr_set_cfgwdth(int width)
53 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
54 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
56 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
57 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
60 int is_fpgamgr_user_mode(void)
62 return (readl(&fpga_manager_base->imgcfg_stat) &
63 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) != 0;
66 static int wait_for_user_mode(void)
68 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
69 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK,
70 1, FPGA_TIMEOUT_MSEC, false);
73 int is_fpgamgr_early_user_mode(void)
75 return (readl(&fpga_manager_base->imgcfg_stat) &
76 ALT_FPGAMGR_IMGCFG_STAT_F2S_EARLY_USERMODE_SET_MSK) != 0;
79 int fpgamgr_wait_early_user_mode(void)
81 u32 sync_data = 0xffffffff;
83 unsigned start = get_timer(0);
84 unsigned long cd_ratio;
86 /* Getting existing CDRATIO */
87 cd_ratio = (readl(&fpga_manager_base->imgcfg_ctrl_02) &
88 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK) >>
89 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB;
91 /* Using CDRATIO_X1 for better compatibility */
92 fpgamgr_set_cd_ratio(CDRATIO_x1);
94 while (!is_fpgamgr_early_user_mode()) {
95 if (get_timer(start) > FPGA_TIMEOUT_MSEC)
97 fpgamgr_program_write((const long unsigned int *)&sync_data,
99 udelay(FPGA_TIMEOUT_MSEC);
103 debug("FPGA: Additional %i sync word needed\n", i);
105 /* restoring original CDRATIO */
106 fpgamgr_set_cd_ratio(cd_ratio);
111 /* Read f2s_nconfig_pin and f2s_nstatus_pin; loop until de-asserted */
112 static int wait_for_nconfig_pin_and_nstatus_pin(void)
114 unsigned long mask = ALT_FPGAMGR_IMGCFG_STAT_F2S_NCONFIG_PIN_SET_MSK |
115 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK;
118 * Poll until f2s_nconfig_pin and f2s_nstatus_pin; loop until
119 * de-asserted, timeout at 1000ms
121 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat, mask,
122 true, FPGA_TIMEOUT_MSEC, false);
125 static int wait_for_f2s_nstatus_pin(unsigned long value)
127 /* Poll until f2s to specific value, timeout at 1000ms */
128 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
129 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK,
130 value, FPGA_TIMEOUT_MSEC, false);
134 static void fpgamgr_set_cd_ratio(unsigned long ratio)
136 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
137 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
139 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
140 (ratio << ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB) &
141 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
144 /* get the MSEL value, verify we are set for FPP configuration mode */
145 static int fpgamgr_verify_msel(void)
147 u32 msel = fpgamgr_get_msel();
149 if (msel & ~BIT(0)) {
150 printf("Fail: read msel=%d\n", msel);
158 * Write cdratio and cdwidth based on whether the bitstream is compressed
161 static int fpgamgr_set_cdratio_cdwidth(unsigned int cfg_width, u32 *rbf_data,
164 unsigned int cd_ratio;
165 bool encrypt, compress;
168 * According to the bitstream specification,
169 * both encryption and compression status are
170 * in location before offset 230 of the buffer.
172 if (rbf_size < MIN_BITSTREAM_SIZECHECK)
175 encrypt = (rbf_data[ENCRYPTION_OFFSET] >> 2) & 3;
176 encrypt = encrypt != 0;
178 compress = (rbf_data[COMPRESSION_OFFSET] >> 1) & 1;
179 compress = !compress;
181 debug("FPGA: Header word %d = %08x.\n", 69, rbf_data[69]);
182 debug("FPGA: Header word %d = %08x.\n", 229, rbf_data[229]);
183 debug("FPGA: Read from rbf header: encrypt=%d compress=%d.\n", encrypt,
187 * from the register map description of cdratio in imgcfg_ctrl_02:
188 * Normal Configuration : 32bit Passive Parallel
189 * Partial Reconfiguration : 16bit Passive Parallel
193 * cd ratio is dependent on cfg width and whether the bitstream
194 * is encrypted and/or compressed.
196 * | width | encr. | compr. | cd ratio |
206 if (!compress && !encrypt) {
207 cd_ratio = CDRATIO_x1;
210 cd_ratio = CDRATIO_x4;
212 cd_ratio = CDRATIO_x2;
214 /* if 32 bit, double the cd ratio (so register
215 field setting is incremented) */
216 if (cfg_width == CFGWDTH_32)
220 fpgamgr_set_cfgwdth(cfg_width);
221 fpgamgr_set_cd_ratio(cd_ratio);
226 static int fpgamgr_reset(void)
230 /* S2F_NCONFIG = 0 */
231 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
232 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
234 /* Wait for f2s_nstatus == 0 */
235 if (wait_for_f2s_nstatus_pin(0))
238 /* S2F_NCONFIG = 1 */
239 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
240 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
242 /* Wait for f2s_nstatus == 1 */
243 if (wait_for_f2s_nstatus_pin(1))
246 /* read and confirm f2s_condone_pin = 0 and f2s_condone_oe = 1 */
247 reg = readl(&fpga_manager_base->imgcfg_stat);
248 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) != 0)
251 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_OE_SET_MSK) == 0)
257 /* Start the FPGA programming by initialize the FPGA Manager */
258 int fpgamgr_program_init(u32 * rbf_data, size_t rbf_size)
263 if (fpgamgr_verify_msel())
267 if (fpgamgr_set_cdratio_cdwidth(CFGWDTH_32, rbf_data, rbf_size))
272 * Make sure no other external devices are trying to interfere with
275 if (wait_for_nconfig_pin_and_nstatus_pin())
280 * Deassert the signal drives from HPS
290 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
291 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
293 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
294 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_PR_REQUEST_SET_MSK);
296 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
297 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
298 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
300 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
301 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
303 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
304 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NSTATUS_OE_SET_MSK |
305 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_CONDONE_OE_SET_MSK);
310 * S2F_NENABLE_CONFIG = 0
311 * S2F_NENABLE_NCONFIG = 0
313 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
314 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
315 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
316 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
319 * Disable driving signals that HPS doesn't need to drive.
320 * S2F_NENABLE_NSTATUS = 1
321 * S2F_NENABLE_CONDONE = 1
323 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
324 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NSTATUS_SET_MSK |
325 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_CONDONE_SET_MSK);
329 * Drive chip select S2F_NCE = 0
331 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
332 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
335 if (wait_for_nconfig_pin_and_nstatus_pin())
339 ret = fpgamgr_reset();
346 * EN_CFG_CTRL and EN_CFG_DATA = 1
348 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
349 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
350 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
355 /* Ensure the FPGA entering config done */
356 static int fpgamgr_program_poll_cd(void)
358 unsigned long reg, i;
360 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
361 reg = readl(&fpga_manager_base->imgcfg_stat);
362 if (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK)
365 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) == 0) {
366 printf("nstatus == 0 while waiting for condone\n");
372 if (i == FPGA_TIMEOUT_CNT)
378 /* Ensure the FPGA entering user mode */
379 static int fpgamgr_program_poll_usermode(void)
384 if (fpgamgr_dclkcnt_set(0xf))
387 ret = wait_for_user_mode();
389 printf("%s: Failed to enter user mode with ", __func__);
390 printf("error code %d\n", ret);
396 * Stop DATA path and Dclk
397 * EN_CFG_CTRL and EN_CFG_DATA = 0
399 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
400 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
401 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
406 * S2F_NENABLE_CONFIG = 1
407 * S2F_NENABLE_NCONFIG = 1
409 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
410 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
411 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
412 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
414 /* Disable chip select S2F_NCE = 1 */
415 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
416 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
422 reg = readl(&fpga_manager_base->imgcfg_stat);
423 if (((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) !=
424 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) ||
425 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) !=
426 ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) ||
427 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) !=
428 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK))
434 int fpgamgr_program_finish(void)
436 /* Ensure the FPGA entering config done */
437 int status = fpgamgr_program_poll_cd();
440 printf("FPGA: Poll CD failed with error code %d\n", status);
444 /* Ensure the FPGA entering user mode */
445 status = fpgamgr_program_poll_usermode();
447 printf("FPGA: Poll usermode failed with error code %d\n",
452 printf("Full Configuration Succeeded.\n");
457 ofnode get_fpga_mgr_ofnode(ofnode from)
459 return ofnode_by_compatible(from, "altr,socfpga-a10-fpga-mgr");
462 const char *get_fpga_filename(void)
464 const char *fpga_filename = NULL;
466 ofnode fpgamgr_node = get_fpga_mgr_ofnode(ofnode_null());
468 if (ofnode_valid(fpgamgr_node))
469 fpga_filename = ofnode_read_string(fpgamgr_node,
472 return fpga_filename;
475 static void get_rbf_image_info(struct rbf_info *rbf, u16 *buffer)
478 * Magic ID starting at:
479 * -> 1st dword[15:0] in periph.rbf
480 * -> 2nd dword[15:0] in core.rbf
481 * Note: dword == 32 bits
483 u32 word_reading_max = 2;
486 for (i = 0; i < word_reading_max; i++) {
487 if (*(buffer + i) == FPGA_SOCFPGA_A10_RBF_UNENCRYPTED) {
488 rbf->security = unencrypted;
489 } else if (*(buffer + i) == FPGA_SOCFPGA_A10_RBF_ENCRYPTED) {
490 rbf->security = encrypted;
491 } else if (*(buffer + i + 1) ==
492 FPGA_SOCFPGA_A10_RBF_UNENCRYPTED) {
493 rbf->security = unencrypted;
494 } else if (*(buffer + i + 1) ==
495 FPGA_SOCFPGA_A10_RBF_ENCRYPTED) {
496 rbf->security = encrypted;
498 rbf->security = invalid;
502 /* PERIPH RBF(buffer + i + 1), CORE RBF(buffer + i + 2) */
503 if (*(buffer + i + 1) == FPGA_SOCFPGA_A10_RBF_PERIPH) {
504 rbf->section = periph_section;
506 } else if (*(buffer + i + 1) == FPGA_SOCFPGA_A10_RBF_CORE) {
507 rbf->section = core_section;
509 } else if (*(buffer + i + 2) == FPGA_SOCFPGA_A10_RBF_PERIPH) {
510 rbf->section = periph_section;
512 } else if (*(buffer + i + 2) == FPGA_SOCFPGA_A10_RBF_CORE) {
513 rbf->section = core_section;
517 rbf->section = unknown;
524 #ifdef CONFIG_FS_LOADER
525 static int first_loading_rbf_to_buffer(struct udevice *dev,
526 struct fpga_loadfs_info *fpga_loadfs,
527 u32 *buffer, size_t *buffer_bsize)
529 u32 *buffer_p = (u32 *)*buffer;
530 u32 *loadable = buffer_p;
531 size_t buffer_size = *buffer_bsize;
533 int ret, i, count, confs_noffset, images_noffset, rbf_offset, rbf_size;
534 const char *fpga_node_name = NULL;
535 const char *uname = NULL;
537 /* Load image header into buffer */
538 ret = request_firmware_into_buf(dev,
539 fpga_loadfs->fpga_fsinfo->filename,
540 buffer_p, sizeof(struct image_header),
543 debug("FPGA: Failed to read image header from flash.\n");
547 if (image_get_magic((struct image_header *)buffer_p) != FDT_MAGIC) {
548 debug("FPGA: No FDT magic was found.\n");
552 fit_size = fdt_totalsize(buffer_p);
554 if (fit_size > buffer_size) {
555 debug("FPGA: FIT image is larger than available buffer.\n");
556 debug("Please use FIT external data or increasing buffer.\n");
560 /* Load entire FIT into buffer */
561 ret = request_firmware_into_buf(dev,
562 fpga_loadfs->fpga_fsinfo->filename,
563 buffer_p, fit_size, 0);
567 ret = fit_check_format(buffer_p);
569 debug("FPGA: No valid FIT image was found.\n");
573 confs_noffset = fdt_path_offset(buffer_p, FIT_CONFS_PATH);
574 images_noffset = fdt_path_offset(buffer_p, FIT_IMAGES_PATH);
575 if (confs_noffset < 0 || images_noffset < 0) {
576 debug("FPGA: No Configurations or images nodes were found.\n");
580 /* Get default configuration unit name from default property */
581 confs_noffset = fit_conf_get_node(buffer_p, NULL);
582 if (confs_noffset < 0) {
583 debug("FPGA: No default configuration was found in config.\n");
587 count = fit_conf_get_prop_node_count(buffer_p, confs_noffset,
590 debug("FPGA: Invalid configuration format for FPGA node.\n");
593 debug("FPGA: FPGA node count: %d\n", count);
595 for (i = 0; i < count; i++) {
596 images_noffset = fit_conf_get_prop_node_index(buffer_p,
599 uname = fit_get_name(buffer_p, images_noffset, NULL);
601 debug("FPGA: %s\n", uname);
603 if (strstr(uname, "fpga-periph") &&
604 (!is_fpgamgr_early_user_mode() ||
605 is_fpgamgr_user_mode())) {
606 fpga_node_name = uname;
607 printf("FPGA: Start to program ");
608 printf("peripheral/full bitstream ...\n");
610 } else if (strstr(uname, "fpga-core") &&
611 (is_fpgamgr_early_user_mode() &&
612 !is_fpgamgr_user_mode())) {
613 fpga_node_name = uname;
614 printf("FPGA: Start to program core ");
615 printf("bitstream ...\n");
622 if (!fpga_node_name) {
623 debug("FPGA: No suitable bitstream was found, count: %d.\n", i);
627 images_noffset = fit_image_get_node(buffer_p, fpga_node_name);
628 if (images_noffset < 0) {
629 debug("FPGA: No node '%s' was found in FIT.\n",
634 if (!fit_image_get_data_position(buffer_p, images_noffset,
636 debug("FPGA: Data position was found.\n");
637 } else if (!fit_image_get_data_offset(buffer_p, images_noffset,
640 * For FIT with external data, figure out where
641 * the external images start. This is the base
642 * for the data-offset properties in each image.
644 rbf_offset += ((fdt_totalsize(buffer_p) + 3) & ~3);
645 debug("FPGA: Data offset was found.\n");
647 debug("FPGA: No data position/offset was found.\n");
651 ret = fit_image_get_data_size(buffer_p, images_noffset, &rbf_size);
653 debug("FPGA: No data size was found (err=%d).\n", ret);
657 if (gd->ram_size < rbf_size) {
658 debug("FPGA: Using default OCRAM buffer and size.\n");
660 ret = fit_image_get_load(buffer_p, images_noffset,
663 buffer_p = (u32 *)DEFAULT_DDR_LOAD_ADDRESS;
664 debug("FPGA: No loadable was found.\n");
665 debug("FPGA: Using default DDR load address: 0x%x .\n",
666 DEFAULT_DDR_LOAD_ADDRESS);
668 buffer_p = (u32 *)*loadable;
669 debug("FPGA: Found loadable address = 0x%x.\n",
673 buffer_size = rbf_size;
676 debug("FPGA: External data: offset = 0x%x, size = 0x%x.\n",
677 rbf_offset, rbf_size);
679 fpga_loadfs->remaining = rbf_size;
682 * Determine buffer size vs bitstream size, and calculating number of
683 * chunk by chunk transfer is required due to smaller buffer size
684 * compare to bitstream
686 if (rbf_size <= buffer_size) {
687 /* Loading whole bitstream into buffer */
688 buffer_size = rbf_size;
689 fpga_loadfs->remaining = 0;
691 fpga_loadfs->remaining -= buffer_size;
694 fpga_loadfs->offset = rbf_offset;
695 /* Loading bitstream into buffer */
696 ret = request_firmware_into_buf(dev,
697 fpga_loadfs->fpga_fsinfo->filename,
698 buffer_p, buffer_size,
699 fpga_loadfs->offset);
701 debug("FPGA: Failed to read bitstream from flash.\n");
705 /* Getting info about bitstream types */
706 get_rbf_image_info(&fpga_loadfs->rbfinfo, (u16 *)buffer_p);
708 /* Update next reading bitstream offset */
709 fpga_loadfs->offset += buffer_size;
711 /* Update the final addr for bitstream */
712 *buffer = (u32)buffer_p;
714 /* Update the size of bitstream to be programmed into FPGA */
715 *buffer_bsize = buffer_size;
720 static int subsequent_loading_rbf_to_buffer(struct udevice *dev,
721 struct fpga_loadfs_info *fpga_loadfs,
722 u32 *buffer, size_t *buffer_bsize)
725 u32 *buffer_p = (u32 *)*buffer;
727 /* Read the bitstream chunk by chunk. */
728 if (fpga_loadfs->remaining > *buffer_bsize) {
729 fpga_loadfs->remaining -= *buffer_bsize;
731 *buffer_bsize = fpga_loadfs->remaining;
732 fpga_loadfs->remaining = 0;
735 ret = request_firmware_into_buf(dev,
736 fpga_loadfs->fpga_fsinfo->filename,
737 buffer_p, *buffer_bsize,
738 fpga_loadfs->offset);
740 debug("FPGA: Failed to read bitstream from flash.\n");
744 /* Update next reading bitstream offset */
745 fpga_loadfs->offset += *buffer_bsize;
750 int socfpga_loadfs(fpga_fs_info *fpga_fsinfo, const void *buf, size_t bsize,
753 struct fpga_loadfs_info fpga_loadfs;
755 int status, ret, size;
756 u32 buffer = (uintptr_t)buf;
757 size_t buffer_sizebytes = bsize;
758 size_t buffer_sizebytes_ori = bsize;
759 size_t total_sizeof_image = 0;
761 const fdt32_t *phandle_p;
764 node = get_fpga_mgr_ofnode(ofnode_null());
766 if (ofnode_valid(node)) {
767 phandle_p = ofnode_get_property(node, "firmware-loader", &size);
769 node = ofnode_path("/chosen");
770 if (!ofnode_valid(node)) {
771 debug("FPGA: /chosen node was not found.\n");
775 phandle_p = ofnode_get_property(node, "firmware-loader",
778 debug("FPGA: firmware-loader property was not");
784 debug("FPGA: FPGA manager node was not found.\n");
788 phandle = fdt32_to_cpu(*phandle_p);
789 ret = uclass_get_device_by_phandle_id(UCLASS_FS_FIRMWARE_LOADER,
794 memset(&fpga_loadfs, 0, sizeof(fpga_loadfs));
796 fpga_loadfs.fpga_fsinfo = fpga_fsinfo;
797 fpga_loadfs.offset = offset;
799 printf("FPGA: Checking FPGA configuration setting ...\n");
802 * Note: Both buffer and buffer_sizebytes values can be altered by
805 ret = first_loading_rbf_to_buffer(dev, &fpga_loadfs, &buffer,
808 printf("FPGA: Skipping configuration ...\n");
814 if (fpga_loadfs.rbfinfo.section == core_section &&
815 !(is_fpgamgr_early_user_mode() && !is_fpgamgr_user_mode())) {
816 debug("FPGA : Must be in Early Release mode to program ");
817 debug("core bitstream.\n");
821 /* Disable all signals from HPS peripheral controller to FPGA */
822 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_A10_FPGAINTF_EN_GLOBAL);
824 /* Disable all axi bridges (hps2fpga, lwhps2fpga & fpga2hps) */
825 socfpga_bridges_reset();
827 if (fpga_loadfs.rbfinfo.section == periph_section) {
828 /* Initialize the FPGA Manager */
829 status = fpgamgr_program_init((u32 *)buffer, buffer_sizebytes);
831 debug("FPGA: Init with peripheral bitstream failed.\n");
836 /* Transfer bitstream to FPGA Manager */
837 fpgamgr_program_write((void *)buffer, buffer_sizebytes);
839 total_sizeof_image += buffer_sizebytes;
841 while (fpga_loadfs.remaining) {
842 ret = subsequent_loading_rbf_to_buffer(dev,
845 &buffer_sizebytes_ori);
850 /* Transfer data to FPGA Manager */
851 fpgamgr_program_write((void *)buffer,
852 buffer_sizebytes_ori);
854 total_sizeof_image += buffer_sizebytes_ori;
859 if (fpga_loadfs.rbfinfo.section == periph_section) {
860 if (fpgamgr_wait_early_user_mode() != -ETIMEDOUT) {
861 config_pins(gd->fdt_blob, "shared");
862 puts("FPGA: Early Release Succeeded.\n");
864 debug("FPGA: Failed to see Early Release.\n");
868 /* For monolithic bitstream */
869 if (is_fpgamgr_user_mode()) {
870 /* Ensure the FPGA entering config done */
871 status = fpgamgr_program_finish();
875 config_pins(gd->fdt_blob, "fpga");
876 puts("FPGA: Enter user mode.\n");
878 } else if (fpga_loadfs.rbfinfo.section == core_section) {
879 /* Ensure the FPGA entering config done */
880 status = fpgamgr_program_finish();
884 config_pins(gd->fdt_blob, "fpga");
885 puts("FPGA: Enter user mode.\n");
887 debug("FPGA: Config Error: Unsupported bitstream type.\n");
891 return (int)total_sizeof_image;
894 void fpgamgr_program(const void *buf, size_t bsize, u32 offset)
896 fpga_fs_info fpga_fsinfo;
898 fpga_fsinfo.filename = get_fpga_filename();
900 if (fpga_fsinfo.filename)
901 socfpga_loadfs(&fpga_fsinfo, buf, bsize, offset);
905 /* This function is used to load the core bitstream from the OCRAM. */
906 int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
908 unsigned long status;
909 struct rbf_info rbfinfo;
911 memset(&rbfinfo, 0, sizeof(rbfinfo));
913 /* Disable all signals from hps peripheral controller to fpga */
914 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_A10_FPGAINTF_EN_GLOBAL);
916 /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */
917 socfpga_bridges_reset();
919 /* Getting info about bitstream types */
920 get_rbf_image_info(&rbfinfo, (u16 *)rbf_data);
922 if (rbfinfo.section == periph_section) {
923 /* Initialize the FPGA Manager */
924 status = fpgamgr_program_init((u32 *)rbf_data, rbf_size);
929 if (rbfinfo.section == core_section &&
930 !(is_fpgamgr_early_user_mode() && !is_fpgamgr_user_mode())) {
931 debug("FPGA : Must be in early release mode to program ");
932 debug("core bitstream.\n");
936 /* Write the bitstream to FPGA Manager */
937 fpgamgr_program_write(rbf_data, rbf_size);
939 status = fpgamgr_program_finish();
943 config_pins(gd->fdt_blob, "fpga");
944 puts("FPGA: Enter user mode.\n");