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>
23 #define MIN_BITSTREAM_SIZECHECK 230
24 #define ENCRYPTION_OFFSET 69
25 #define COMPRESSION_OFFSET 229
26 #define FPGA_TIMEOUT_MSEC 1000 /* timeout in ms */
27 #define FPGA_TIMEOUT_CNT 0x1000000
28 #define DEFAULT_DDR_LOAD_ADDRESS 0x400
30 DECLARE_GLOBAL_DATA_PTR;
32 static const struct socfpga_fpga_manager *fpga_manager_base =
33 (void *)SOCFPGA_FPGAMGRREGS_ADDRESS;
35 static void fpgamgr_set_cd_ratio(unsigned long ratio);
37 static uint32_t fpgamgr_get_msel(void)
41 reg = readl(&fpga_manager_base->imgcfg_stat);
42 reg = (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL_SET_MSD) >>
43 ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL0_LSB;
48 static void fpgamgr_set_cfgwdth(int width)
51 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
52 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
54 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
55 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
58 int is_fpgamgr_user_mode(void)
60 return (readl(&fpga_manager_base->imgcfg_stat) &
61 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) != 0;
64 static int wait_for_user_mode(void)
66 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
67 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK,
68 1, FPGA_TIMEOUT_MSEC, false);
71 int is_fpgamgr_early_user_mode(void)
73 return (readl(&fpga_manager_base->imgcfg_stat) &
74 ALT_FPGAMGR_IMGCFG_STAT_F2S_EARLY_USERMODE_SET_MSK) != 0;
77 int fpgamgr_wait_early_user_mode(void)
79 u32 sync_data = 0xffffffff;
81 unsigned start = get_timer(0);
82 unsigned long cd_ratio;
84 /* Getting existing CDRATIO */
85 cd_ratio = (readl(&fpga_manager_base->imgcfg_ctrl_02) &
86 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK) >>
87 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB;
89 /* Using CDRATIO_X1 for better compatibility */
90 fpgamgr_set_cd_ratio(CDRATIO_x1);
92 while (!is_fpgamgr_early_user_mode()) {
93 if (get_timer(start) > FPGA_TIMEOUT_MSEC)
95 fpgamgr_program_write((const long unsigned int *)&sync_data,
97 udelay(FPGA_TIMEOUT_MSEC);
101 debug("FPGA: Additional %i sync word needed\n", i);
103 /* restoring original CDRATIO */
104 fpgamgr_set_cd_ratio(cd_ratio);
109 /* Read f2s_nconfig_pin and f2s_nstatus_pin; loop until de-asserted */
110 static int wait_for_nconfig_pin_and_nstatus_pin(void)
112 unsigned long mask = ALT_FPGAMGR_IMGCFG_STAT_F2S_NCONFIG_PIN_SET_MSK |
113 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK;
116 * Poll until f2s_nconfig_pin and f2s_nstatus_pin; loop until
117 * de-asserted, timeout at 1000ms
119 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat, mask,
120 true, FPGA_TIMEOUT_MSEC, false);
123 static int wait_for_f2s_nstatus_pin(unsigned long value)
125 /* Poll until f2s to specific value, timeout at 1000ms */
126 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
127 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK,
128 value, FPGA_TIMEOUT_MSEC, false);
132 static void fpgamgr_set_cd_ratio(unsigned long ratio)
134 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
135 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
137 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
138 (ratio << ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB) &
139 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
142 /* get the MSEL value, verify we are set for FPP configuration mode */
143 static int fpgamgr_verify_msel(void)
145 u32 msel = fpgamgr_get_msel();
147 if (msel & ~BIT(0)) {
148 printf("Fail: read msel=%d\n", msel);
156 * Write cdratio and cdwidth based on whether the bitstream is compressed
159 static int fpgamgr_set_cdratio_cdwidth(unsigned int cfg_width, u32 *rbf_data,
162 unsigned int cd_ratio;
163 bool encrypt, compress;
166 * According to the bitstream specification,
167 * both encryption and compression status are
168 * in location before offset 230 of the buffer.
170 if (rbf_size < MIN_BITSTREAM_SIZECHECK)
173 encrypt = (rbf_data[ENCRYPTION_OFFSET] >> 2) & 3;
174 encrypt = encrypt != 0;
176 compress = (rbf_data[COMPRESSION_OFFSET] >> 1) & 1;
177 compress = !compress;
179 debug("FPGA: Header word %d = %08x.\n", 69, rbf_data[69]);
180 debug("FPGA: Header word %d = %08x.\n", 229, rbf_data[229]);
181 debug("FPGA: Read from rbf header: encrypt=%d compress=%d.\n", encrypt,
185 * from the register map description of cdratio in imgcfg_ctrl_02:
186 * Normal Configuration : 32bit Passive Parallel
187 * Partial Reconfiguration : 16bit Passive Parallel
191 * cd ratio is dependent on cfg width and whether the bitstream
192 * is encrypted and/or compressed.
194 * | width | encr. | compr. | cd ratio |
204 if (!compress && !encrypt) {
205 cd_ratio = CDRATIO_x1;
208 cd_ratio = CDRATIO_x4;
210 cd_ratio = CDRATIO_x2;
212 /* if 32 bit, double the cd ratio (so register
213 field setting is incremented) */
214 if (cfg_width == CFGWDTH_32)
218 fpgamgr_set_cfgwdth(cfg_width);
219 fpgamgr_set_cd_ratio(cd_ratio);
224 static int fpgamgr_reset(void)
228 /* S2F_NCONFIG = 0 */
229 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
230 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
232 /* Wait for f2s_nstatus == 0 */
233 if (wait_for_f2s_nstatus_pin(0))
236 /* S2F_NCONFIG = 1 */
237 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
238 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
240 /* Wait for f2s_nstatus == 1 */
241 if (wait_for_f2s_nstatus_pin(1))
244 /* read and confirm f2s_condone_pin = 0 and f2s_condone_oe = 1 */
245 reg = readl(&fpga_manager_base->imgcfg_stat);
246 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) != 0)
249 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_OE_SET_MSK) == 0)
255 /* Start the FPGA programming by initialize the FPGA Manager */
256 int fpgamgr_program_init(u32 * rbf_data, size_t rbf_size)
261 if (fpgamgr_verify_msel())
265 if (fpgamgr_set_cdratio_cdwidth(CFGWDTH_32, rbf_data, rbf_size))
270 * Make sure no other external devices are trying to interfere with
273 if (wait_for_nconfig_pin_and_nstatus_pin())
278 * Deassert the signal drives from HPS
288 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
289 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
291 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
292 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_PR_REQUEST_SET_MSK);
294 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
295 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
296 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
298 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
299 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
301 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
302 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NSTATUS_OE_SET_MSK |
303 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_CONDONE_OE_SET_MSK);
308 * S2F_NENABLE_CONFIG = 0
309 * S2F_NENABLE_NCONFIG = 0
311 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
312 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
313 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
314 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
317 * Disable driving signals that HPS doesn't need to drive.
318 * S2F_NENABLE_NSTATUS = 1
319 * S2F_NENABLE_CONDONE = 1
321 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
322 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NSTATUS_SET_MSK |
323 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_CONDONE_SET_MSK);
327 * Drive chip select S2F_NCE = 0
329 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
330 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
333 if (wait_for_nconfig_pin_and_nstatus_pin())
337 ret = fpgamgr_reset();
344 * EN_CFG_CTRL and EN_CFG_DATA = 1
346 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
347 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
348 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
353 /* Ensure the FPGA entering config done */
354 static int fpgamgr_program_poll_cd(void)
356 unsigned long reg, i;
358 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
359 reg = readl(&fpga_manager_base->imgcfg_stat);
360 if (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK)
363 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) == 0) {
364 printf("nstatus == 0 while waiting for condone\n");
370 if (i == FPGA_TIMEOUT_CNT)
376 /* Ensure the FPGA entering user mode */
377 static int fpgamgr_program_poll_usermode(void)
382 if (fpgamgr_dclkcnt_set(0xf))
385 ret = wait_for_user_mode();
387 printf("%s: Failed to enter user mode with ", __func__);
388 printf("error code %d\n", ret);
394 * Stop DATA path and Dclk
395 * EN_CFG_CTRL and EN_CFG_DATA = 0
397 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
398 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
399 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
404 * S2F_NENABLE_CONFIG = 1
405 * S2F_NENABLE_NCONFIG = 1
407 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
408 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
409 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
410 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
412 /* Disable chip select S2F_NCE = 1 */
413 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
414 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
420 reg = readl(&fpga_manager_base->imgcfg_stat);
421 if (((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) !=
422 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) ||
423 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) !=
424 ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) ||
425 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) !=
426 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK))
432 int fpgamgr_program_finish(void)
434 /* Ensure the FPGA entering config done */
435 int status = fpgamgr_program_poll_cd();
438 printf("FPGA: Poll CD failed with error code %d\n", status);
442 /* Ensure the FPGA entering user mode */
443 status = fpgamgr_program_poll_usermode();
445 printf("FPGA: Poll usermode failed with error code %d\n",
450 printf("Full Configuration Succeeded.\n");
455 ofnode get_fpga_mgr_ofnode(ofnode from)
457 return ofnode_by_compatible(from, "altr,socfpga-a10-fpga-mgr");
460 const char *get_fpga_filename(void)
462 const char *fpga_filename = NULL;
464 ofnode fpgamgr_node = get_fpga_mgr_ofnode(ofnode_null());
466 if (ofnode_valid(fpgamgr_node))
467 fpga_filename = ofnode_read_string(fpgamgr_node,
470 return fpga_filename;
473 static void get_rbf_image_info(struct rbf_info *rbf, u16 *buffer)
476 * Magic ID starting at:
477 * -> 1st dword[15:0] in periph.rbf
478 * -> 2nd dword[15:0] in core.rbf
479 * Note: dword == 32 bits
481 u32 word_reading_max = 2;
484 for (i = 0; i < word_reading_max; i++) {
485 if (*(buffer + i) == FPGA_SOCFPGA_A10_RBF_UNENCRYPTED) {
486 rbf->security = unencrypted;
487 } else if (*(buffer + i) == FPGA_SOCFPGA_A10_RBF_ENCRYPTED) {
488 rbf->security = encrypted;
489 } else if (*(buffer + i + 1) ==
490 FPGA_SOCFPGA_A10_RBF_UNENCRYPTED) {
491 rbf->security = unencrypted;
492 } else if (*(buffer + i + 1) ==
493 FPGA_SOCFPGA_A10_RBF_ENCRYPTED) {
494 rbf->security = encrypted;
496 rbf->security = invalid;
500 /* PERIPH RBF(buffer + i + 1), CORE RBF(buffer + i + 2) */
501 if (*(buffer + i + 1) == FPGA_SOCFPGA_A10_RBF_PERIPH) {
502 rbf->section = periph_section;
504 } else if (*(buffer + i + 1) == FPGA_SOCFPGA_A10_RBF_CORE) {
505 rbf->section = core_section;
507 } else if (*(buffer + i + 2) == FPGA_SOCFPGA_A10_RBF_PERIPH) {
508 rbf->section = periph_section;
510 } else if (*(buffer + i + 2) == FPGA_SOCFPGA_A10_RBF_CORE) {
511 rbf->section = core_section;
515 rbf->section = unknown;
522 #ifdef CONFIG_FS_LOADER
523 static int first_loading_rbf_to_buffer(struct udevice *dev,
524 struct fpga_loadfs_info *fpga_loadfs,
525 u32 *buffer, size_t *buffer_bsize)
527 u32 *buffer_p = (u32 *)*buffer;
528 u32 *loadable = buffer_p;
529 size_t buffer_size = *buffer_bsize;
531 int ret, i, count, confs_noffset, images_noffset, rbf_offset, rbf_size;
532 const char *fpga_node_name = NULL;
533 const char *uname = NULL;
535 /* Load image header into buffer */
536 ret = request_firmware_into_buf(dev,
537 fpga_loadfs->fpga_fsinfo->filename,
538 buffer_p, sizeof(struct image_header),
541 debug("FPGA: Failed to read image header from flash.\n");
545 if (image_get_magic((struct image_header *)buffer_p) != FDT_MAGIC) {
546 debug("FPGA: No FDT magic was found.\n");
550 fit_size = fdt_totalsize(buffer_p);
552 if (fit_size > buffer_size) {
553 debug("FPGA: FIT image is larger than available buffer.\n");
554 debug("Please use FIT external data or increasing buffer.\n");
558 /* Load entire FIT into buffer */
559 ret = request_firmware_into_buf(dev,
560 fpga_loadfs->fpga_fsinfo->filename,
561 buffer_p, fit_size, 0);
565 ret = fit_check_format(buffer_p);
567 debug("FPGA: No valid FIT image was found.\n");
571 confs_noffset = fdt_path_offset(buffer_p, FIT_CONFS_PATH);
572 images_noffset = fdt_path_offset(buffer_p, FIT_IMAGES_PATH);
573 if (confs_noffset < 0 || images_noffset < 0) {
574 debug("FPGA: No Configurations or images nodes were found.\n");
578 /* Get default configuration unit name from default property */
579 confs_noffset = fit_conf_get_node(buffer_p, NULL);
580 if (confs_noffset < 0) {
581 debug("FPGA: No default configuration was found in config.\n");
585 count = fit_conf_get_prop_node_count(buffer_p, confs_noffset,
588 debug("FPGA: Invalid configuration format for FPGA node.\n");
591 debug("FPGA: FPGA node count: %d\n", count);
593 for (i = 0; i < count; i++) {
594 images_noffset = fit_conf_get_prop_node_index(buffer_p,
597 uname = fit_get_name(buffer_p, images_noffset, NULL);
599 debug("FPGA: %s\n", uname);
601 if (strstr(uname, "fpga-periph") &&
602 (!is_fpgamgr_early_user_mode() ||
603 is_fpgamgr_user_mode())) {
604 fpga_node_name = uname;
605 printf("FPGA: Start to program ");
606 printf("peripheral/full bitstream ...\n");
608 } else if (strstr(uname, "fpga-core") &&
609 (is_fpgamgr_early_user_mode() &&
610 !is_fpgamgr_user_mode())) {
611 fpga_node_name = uname;
612 printf("FPGA: Start to program core ");
613 printf("bitstream ...\n");
620 if (!fpga_node_name) {
621 debug("FPGA: No suitable bitstream was found, count: %d.\n", i);
625 images_noffset = fit_image_get_node(buffer_p, fpga_node_name);
626 if (images_noffset < 0) {
627 debug("FPGA: No node '%s' was found in FIT.\n",
632 if (!fit_image_get_data_position(buffer_p, images_noffset,
634 debug("FPGA: Data position was found.\n");
635 } else if (!fit_image_get_data_offset(buffer_p, images_noffset,
638 * For FIT with external data, figure out where
639 * the external images start. This is the base
640 * for the data-offset properties in each image.
642 rbf_offset += ((fdt_totalsize(buffer_p) + 3) & ~3);
643 debug("FPGA: Data offset was found.\n");
645 debug("FPGA: No data position/offset was found.\n");
649 ret = fit_image_get_data_size(buffer_p, images_noffset, &rbf_size);
651 debug("FPGA: No data size was found (err=%d).\n", ret);
655 if (gd->ram_size < rbf_size) {
656 debug("FPGA: Using default OCRAM buffer and size.\n");
658 ret = fit_image_get_load(buffer_p, images_noffset,
661 buffer_p = (u32 *)DEFAULT_DDR_LOAD_ADDRESS;
662 debug("FPGA: No loadable was found.\n");
663 debug("FPGA: Using default DDR load address: 0x%x .\n",
664 DEFAULT_DDR_LOAD_ADDRESS);
666 buffer_p = (u32 *)*loadable;
667 debug("FPGA: Found loadable address = 0x%x.\n",
671 buffer_size = rbf_size;
674 debug("FPGA: External data: offset = 0x%x, size = 0x%x.\n",
675 rbf_offset, rbf_size);
677 fpga_loadfs->remaining = rbf_size;
680 * Determine buffer size vs bitstream size, and calculating number of
681 * chunk by chunk transfer is required due to smaller buffer size
682 * compare to bitstream
684 if (rbf_size <= buffer_size) {
685 /* Loading whole bitstream into buffer */
686 buffer_size = rbf_size;
687 fpga_loadfs->remaining = 0;
689 fpga_loadfs->remaining -= buffer_size;
692 fpga_loadfs->offset = rbf_offset;
693 /* Loading bitstream into buffer */
694 ret = request_firmware_into_buf(dev,
695 fpga_loadfs->fpga_fsinfo->filename,
696 buffer_p, buffer_size,
697 fpga_loadfs->offset);
699 debug("FPGA: Failed to read bitstream from flash.\n");
703 /* Getting info about bitstream types */
704 get_rbf_image_info(&fpga_loadfs->rbfinfo, (u16 *)buffer_p);
706 /* Update next reading bitstream offset */
707 fpga_loadfs->offset += buffer_size;
709 /* Update the final addr for bitstream */
710 *buffer = (u32)buffer_p;
712 /* Update the size of bitstream to be programmed into FPGA */
713 *buffer_bsize = buffer_size;
718 static int subsequent_loading_rbf_to_buffer(struct udevice *dev,
719 struct fpga_loadfs_info *fpga_loadfs,
720 u32 *buffer, size_t *buffer_bsize)
723 u32 *buffer_p = (u32 *)*buffer;
725 /* Read the bitstream chunk by chunk. */
726 if (fpga_loadfs->remaining > *buffer_bsize) {
727 fpga_loadfs->remaining -= *buffer_bsize;
729 *buffer_bsize = fpga_loadfs->remaining;
730 fpga_loadfs->remaining = 0;
733 ret = request_firmware_into_buf(dev,
734 fpga_loadfs->fpga_fsinfo->filename,
735 buffer_p, *buffer_bsize,
736 fpga_loadfs->offset);
738 debug("FPGA: Failed to read bitstream from flash.\n");
742 /* Update next reading bitstream offset */
743 fpga_loadfs->offset += *buffer_bsize;
748 int socfpga_loadfs(fpga_fs_info *fpga_fsinfo, const void *buf, size_t bsize,
751 struct fpga_loadfs_info fpga_loadfs;
753 int status, ret, size;
754 u32 buffer = (uintptr_t)buf;
755 size_t buffer_sizebytes = bsize;
756 size_t buffer_sizebytes_ori = bsize;
757 size_t total_sizeof_image = 0;
759 const fdt32_t *phandle_p;
762 node = get_fpga_mgr_ofnode(ofnode_null());
764 if (ofnode_valid(node)) {
765 phandle_p = ofnode_get_property(node, "firmware-loader", &size);
767 node = ofnode_path("/chosen");
768 if (!ofnode_valid(node)) {
769 debug("FPGA: /chosen node was not found.\n");
773 phandle_p = ofnode_get_property(node, "firmware-loader",
776 debug("FPGA: firmware-loader property was not");
782 debug("FPGA: FPGA manager node was not found.\n");
786 phandle = fdt32_to_cpu(*phandle_p);
787 ret = uclass_get_device_by_phandle_id(UCLASS_FS_FIRMWARE_LOADER,
792 memset(&fpga_loadfs, 0, sizeof(fpga_loadfs));
794 fpga_loadfs.fpga_fsinfo = fpga_fsinfo;
795 fpga_loadfs.offset = offset;
797 printf("FPGA: Checking FPGA configuration setting ...\n");
800 * Note: Both buffer and buffer_sizebytes values can be altered by
803 ret = first_loading_rbf_to_buffer(dev, &fpga_loadfs, &buffer,
806 printf("FPGA: Skipping configuration ...\n");
812 if (fpga_loadfs.rbfinfo.section == core_section &&
813 !(is_fpgamgr_early_user_mode() && !is_fpgamgr_user_mode())) {
814 debug("FPGA : Must be in Early Release mode to program ");
815 debug("core bitstream.\n");
819 /* Disable all signals from HPS peripheral controller to FPGA */
820 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_A10_FPGAINTF_EN_GLOBAL);
822 /* Disable all axi bridges (hps2fpga, lwhps2fpga & fpga2hps) */
823 socfpga_bridges_reset();
825 if (fpga_loadfs.rbfinfo.section == periph_section) {
826 /* Initialize the FPGA Manager */
827 status = fpgamgr_program_init((u32 *)buffer, buffer_sizebytes);
829 debug("FPGA: Init with peripheral bitstream failed.\n");
834 /* Transfer bitstream to FPGA Manager */
835 fpgamgr_program_write((void *)buffer, buffer_sizebytes);
837 total_sizeof_image += buffer_sizebytes;
839 while (fpga_loadfs.remaining) {
840 ret = subsequent_loading_rbf_to_buffer(dev,
843 &buffer_sizebytes_ori);
848 /* Transfer data to FPGA Manager */
849 fpgamgr_program_write((void *)buffer,
850 buffer_sizebytes_ori);
852 total_sizeof_image += buffer_sizebytes_ori;
857 if (fpga_loadfs.rbfinfo.section == periph_section) {
858 if (fpgamgr_wait_early_user_mode() != -ETIMEDOUT) {
859 config_pins(gd->fdt_blob, "shared");
860 puts("FPGA: Early Release Succeeded.\n");
862 debug("FPGA: Failed to see Early Release.\n");
866 /* For monolithic bitstream */
867 if (is_fpgamgr_user_mode()) {
868 /* Ensure the FPGA entering config done */
869 status = fpgamgr_program_finish();
873 config_pins(gd->fdt_blob, "fpga");
874 puts("FPGA: Enter user mode.\n");
876 } else if (fpga_loadfs.rbfinfo.section == core_section) {
877 /* Ensure the FPGA entering config done */
878 status = fpgamgr_program_finish();
882 config_pins(gd->fdt_blob, "fpga");
883 puts("FPGA: Enter user mode.\n");
885 debug("FPGA: Config Error: Unsupported bitstream type.\n");
889 return (int)total_sizeof_image;
892 void fpgamgr_program(const void *buf, size_t bsize, u32 offset)
894 fpga_fs_info fpga_fsinfo;
896 fpga_fsinfo.filename = get_fpga_filename();
898 if (fpga_fsinfo.filename)
899 socfpga_loadfs(&fpga_fsinfo, buf, bsize, offset);
903 /* This function is used to load the core bitstream from the OCRAM. */
904 int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
906 unsigned long status;
907 struct rbf_info rbfinfo;
909 memset(&rbfinfo, 0, sizeof(rbfinfo));
911 /* Disable all signals from hps peripheral controller to fpga */
912 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_A10_FPGAINTF_EN_GLOBAL);
914 /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */
915 socfpga_bridges_reset();
917 /* Getting info about bitstream types */
918 get_rbf_image_info(&rbfinfo, (u16 *)rbf_data);
920 if (rbfinfo.section == periph_section) {
921 /* Initialize the FPGA Manager */
922 status = fpgamgr_program_init((u32 *)rbf_data, rbf_size);
927 if (rbfinfo.section == core_section &&
928 !(is_fpgamgr_early_user_mode() && !is_fpgamgr_user_mode())) {
929 debug("FPGA : Must be in early release mode to program ");
930 debug("core bitstream.\n");
934 /* Write the bitstream to FPGA Manager */
935 fpgamgr_program_write(rbf_data, rbf_size);
937 status = fpgamgr_program_finish();
941 config_pins(gd->fdt_blob, "fpga");
942 puts("FPGA: Enter user mode.\n");