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>
17 #include <dm/ofnode.h>
19 #include <fs_loader.h>
22 #include <linux/bitops.h>
23 #include <linux/delay.h>
26 #define MIN_BITSTREAM_SIZECHECK 230
27 #define ENCRYPTION_OFFSET 69
28 #define COMPRESSION_OFFSET 229
29 #define FPGA_TIMEOUT_MSEC 1000 /* timeout in ms */
30 #define FPGA_TIMEOUT_CNT 0x1000000
31 #define DEFAULT_DDR_LOAD_ADDRESS 0x400
33 DECLARE_GLOBAL_DATA_PTR;
35 static const struct socfpga_fpga_manager *fpga_manager_base =
36 (void *)SOCFPGA_FPGAMGRREGS_ADDRESS;
38 static void fpgamgr_set_cd_ratio(unsigned long ratio);
40 static uint32_t fpgamgr_get_msel(void)
44 reg = readl(&fpga_manager_base->imgcfg_stat);
45 reg = (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL_SET_MSD) >>
46 ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL0_LSB;
51 static void fpgamgr_set_cfgwdth(int width)
54 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
55 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
57 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
58 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
61 int is_fpgamgr_user_mode(void)
63 return (readl(&fpga_manager_base->imgcfg_stat) &
64 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) != 0;
67 static int wait_for_user_mode(void)
69 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
70 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK,
71 1, FPGA_TIMEOUT_MSEC, false);
74 int is_fpgamgr_early_user_mode(void)
76 return (readl(&fpga_manager_base->imgcfg_stat) &
77 ALT_FPGAMGR_IMGCFG_STAT_F2S_EARLY_USERMODE_SET_MSK) != 0;
80 int fpgamgr_wait_early_user_mode(void)
82 u32 sync_data = 0xffffffff;
84 unsigned start = get_timer(0);
85 unsigned long cd_ratio;
87 /* Getting existing CDRATIO */
88 cd_ratio = (readl(&fpga_manager_base->imgcfg_ctrl_02) &
89 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK) >>
90 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB;
92 /* Using CDRATIO_X1 for better compatibility */
93 fpgamgr_set_cd_ratio(CDRATIO_x1);
95 while (!is_fpgamgr_early_user_mode()) {
96 if (get_timer(start) > FPGA_TIMEOUT_MSEC)
98 fpgamgr_program_write((const long unsigned int *)&sync_data,
100 udelay(FPGA_TIMEOUT_MSEC);
104 debug("FPGA: Additional %i sync word needed\n", i);
106 /* restoring original CDRATIO */
107 fpgamgr_set_cd_ratio(cd_ratio);
112 /* Read f2s_nconfig_pin and f2s_nstatus_pin; loop until de-asserted */
113 static int wait_for_nconfig_pin_and_nstatus_pin(void)
115 unsigned long mask = ALT_FPGAMGR_IMGCFG_STAT_F2S_NCONFIG_PIN_SET_MSK |
116 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK;
119 * Poll until f2s_nconfig_pin and f2s_nstatus_pin; loop until
120 * de-asserted, timeout at 1000ms
122 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat, mask,
123 true, FPGA_TIMEOUT_MSEC, false);
126 static int wait_for_f2s_nstatus_pin(unsigned long value)
128 /* Poll until f2s to specific value, timeout at 1000ms */
129 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
130 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK,
131 value, FPGA_TIMEOUT_MSEC, false);
135 static void fpgamgr_set_cd_ratio(unsigned long ratio)
137 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
138 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
140 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
141 (ratio << ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB) &
142 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
145 /* get the MSEL value, verify we are set for FPP configuration mode */
146 static int fpgamgr_verify_msel(void)
148 u32 msel = fpgamgr_get_msel();
150 if (msel & ~BIT(0)) {
151 printf("Fail: read msel=%d\n", msel);
159 * Write cdratio and cdwidth based on whether the bitstream is compressed
162 static int fpgamgr_set_cdratio_cdwidth(unsigned int cfg_width, u32 *rbf_data,
165 unsigned int cd_ratio;
166 bool encrypt, compress;
169 * According to the bitstream specification,
170 * both encryption and compression status are
171 * in location before offset 230 of the buffer.
173 if (rbf_size < MIN_BITSTREAM_SIZECHECK)
176 encrypt = (rbf_data[ENCRYPTION_OFFSET] >> 2) & 3;
177 encrypt = encrypt != 0;
179 compress = (rbf_data[COMPRESSION_OFFSET] >> 1) & 1;
180 compress = !compress;
182 debug("FPGA: Header word %d = %08x.\n", 69, rbf_data[69]);
183 debug("FPGA: Header word %d = %08x.\n", 229, rbf_data[229]);
184 debug("FPGA: Read from rbf header: encrypt=%d compress=%d.\n", encrypt,
188 * from the register map description of cdratio in imgcfg_ctrl_02:
189 * Normal Configuration : 32bit Passive Parallel
190 * Partial Reconfiguration : 16bit Passive Parallel
194 * cd ratio is dependent on cfg width and whether the bitstream
195 * is encrypted and/or compressed.
197 * | width | encr. | compr. | cd ratio |
207 if (!compress && !encrypt) {
208 cd_ratio = CDRATIO_x1;
211 cd_ratio = CDRATIO_x4;
213 cd_ratio = CDRATIO_x2;
215 /* if 32 bit, double the cd ratio (so register
216 field setting is incremented) */
217 if (cfg_width == CFGWDTH_32)
221 fpgamgr_set_cfgwdth(cfg_width);
222 fpgamgr_set_cd_ratio(cd_ratio);
227 static int fpgamgr_reset(void)
231 /* S2F_NCONFIG = 0 */
232 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
233 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
235 /* Wait for f2s_nstatus == 0 */
236 if (wait_for_f2s_nstatus_pin(0))
239 /* S2F_NCONFIG = 1 */
240 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
241 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
243 /* Wait for f2s_nstatus == 1 */
244 if (wait_for_f2s_nstatus_pin(1))
247 /* read and confirm f2s_condone_pin = 0 and f2s_condone_oe = 1 */
248 reg = readl(&fpga_manager_base->imgcfg_stat);
249 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) != 0)
252 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_OE_SET_MSK) == 0)
258 /* Start the FPGA programming by initialize the FPGA Manager */
259 int fpgamgr_program_init(u32 * rbf_data, size_t rbf_size)
264 if (fpgamgr_verify_msel())
268 if (fpgamgr_set_cdratio_cdwidth(CFGWDTH_32, rbf_data, rbf_size))
273 * Make sure no other external devices are trying to interfere with
276 if (wait_for_nconfig_pin_and_nstatus_pin())
281 * Deassert the signal drives from HPS
291 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
292 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
294 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
295 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_PR_REQUEST_SET_MSK);
297 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
298 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
299 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
301 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
302 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
304 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
305 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NSTATUS_OE_SET_MSK |
306 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_CONDONE_OE_SET_MSK);
311 * S2F_NENABLE_CONFIG = 0
312 * S2F_NENABLE_NCONFIG = 0
314 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
315 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
316 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
317 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
320 * Disable driving signals that HPS doesn't need to drive.
321 * S2F_NENABLE_NSTATUS = 1
322 * S2F_NENABLE_CONDONE = 1
324 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
325 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NSTATUS_SET_MSK |
326 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_CONDONE_SET_MSK);
330 * Drive chip select S2F_NCE = 0
332 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
333 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
336 if (wait_for_nconfig_pin_and_nstatus_pin())
340 ret = fpgamgr_reset();
347 * EN_CFG_CTRL and EN_CFG_DATA = 1
349 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
350 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
351 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
356 /* Ensure the FPGA entering config done */
357 static int fpgamgr_program_poll_cd(void)
359 unsigned long reg, i;
361 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
362 reg = readl(&fpga_manager_base->imgcfg_stat);
363 if (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK)
366 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) == 0) {
367 printf("nstatus == 0 while waiting for condone\n");
373 if (i == FPGA_TIMEOUT_CNT)
379 /* Ensure the FPGA entering user mode */
380 static int fpgamgr_program_poll_usermode(void)
385 if (fpgamgr_dclkcnt_set(0xf))
388 ret = wait_for_user_mode();
390 printf("%s: Failed to enter user mode with ", __func__);
391 printf("error code %d\n", ret);
397 * Stop DATA path and Dclk
398 * EN_CFG_CTRL and EN_CFG_DATA = 0
400 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
401 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
402 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
407 * S2F_NENABLE_CONFIG = 1
408 * S2F_NENABLE_NCONFIG = 1
410 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
411 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
412 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
413 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
415 /* Disable chip select S2F_NCE = 1 */
416 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
417 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
423 reg = readl(&fpga_manager_base->imgcfg_stat);
424 if (((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) !=
425 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) ||
426 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) !=
427 ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) ||
428 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) !=
429 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK))
435 int fpgamgr_program_finish(void)
437 /* Ensure the FPGA entering config done */
438 int status = fpgamgr_program_poll_cd();
441 printf("FPGA: Poll CD failed with error code %d\n", status);
445 /* Ensure the FPGA entering user mode */
446 status = fpgamgr_program_poll_usermode();
448 printf("FPGA: Poll usermode failed with error code %d\n",
453 printf("Full Configuration Succeeded.\n");
458 ofnode get_fpga_mgr_ofnode(ofnode from)
460 return ofnode_by_compatible(from, "altr,socfpga-a10-fpga-mgr");
463 const char *get_fpga_filename(void)
465 const char *fpga_filename = NULL;
467 ofnode fpgamgr_node = get_fpga_mgr_ofnode(ofnode_null());
469 if (ofnode_valid(fpgamgr_node))
470 fpga_filename = ofnode_read_string(fpgamgr_node,
473 return fpga_filename;
476 static void get_rbf_image_info(struct rbf_info *rbf, u16 *buffer)
479 * Magic ID starting at:
480 * -> 1st dword[15:0] in periph.rbf
481 * -> 2nd dword[15:0] in core.rbf
482 * Note: dword == 32 bits
484 u32 word_reading_max = 2;
487 for (i = 0; i < word_reading_max; i++) {
488 if (*(buffer + i) == FPGA_SOCFPGA_A10_RBF_UNENCRYPTED) {
489 rbf->security = unencrypted;
490 } else if (*(buffer + i) == FPGA_SOCFPGA_A10_RBF_ENCRYPTED) {
491 rbf->security = encrypted;
492 } else if (*(buffer + i + 1) ==
493 FPGA_SOCFPGA_A10_RBF_UNENCRYPTED) {
494 rbf->security = unencrypted;
495 } else if (*(buffer + i + 1) ==
496 FPGA_SOCFPGA_A10_RBF_ENCRYPTED) {
497 rbf->security = encrypted;
499 rbf->security = invalid;
503 /* PERIPH RBF(buffer + i + 1), CORE RBF(buffer + i + 2) */
504 if (*(buffer + i + 1) == FPGA_SOCFPGA_A10_RBF_PERIPH) {
505 rbf->section = periph_section;
507 } else if (*(buffer + i + 1) == FPGA_SOCFPGA_A10_RBF_CORE) {
508 rbf->section = core_section;
510 } else if (*(buffer + i + 2) == FPGA_SOCFPGA_A10_RBF_PERIPH) {
511 rbf->section = periph_section;
513 } else if (*(buffer + i + 2) == FPGA_SOCFPGA_A10_RBF_CORE) {
514 rbf->section = core_section;
518 rbf->section = unknown;
525 #ifdef CONFIG_FS_LOADER
526 static int first_loading_rbf_to_buffer(struct udevice *dev,
527 struct fpga_loadfs_info *fpga_loadfs,
528 u32 *buffer, size_t *buffer_bsize)
530 u32 *buffer_p = (u32 *)*buffer;
531 u32 *loadable = buffer_p;
532 size_t buffer_size = *buffer_bsize;
534 int ret, i, count, confs_noffset, images_noffset, rbf_offset, rbf_size;
535 const char *fpga_node_name = NULL;
536 const char *uname = NULL;
538 /* Load image header into buffer */
539 ret = request_firmware_into_buf(dev,
540 fpga_loadfs->fpga_fsinfo->filename,
541 buffer_p, sizeof(struct image_header),
544 debug("FPGA: Failed to read image header from flash.\n");
548 if (image_get_magic((struct image_header *)buffer_p) != FDT_MAGIC) {
549 debug("FPGA: No FDT magic was found.\n");
553 fit_size = fdt_totalsize(buffer_p);
555 if (fit_size > buffer_size) {
556 debug("FPGA: FIT image is larger than available buffer.\n");
557 debug("Please use FIT external data or increasing buffer.\n");
561 /* Load entire FIT into buffer */
562 ret = request_firmware_into_buf(dev,
563 fpga_loadfs->fpga_fsinfo->filename,
564 buffer_p, fit_size, 0);
568 ret = fit_check_format(buffer_p);
570 debug("FPGA: No valid FIT image was found.\n");
574 confs_noffset = fdt_path_offset(buffer_p, FIT_CONFS_PATH);
575 images_noffset = fdt_path_offset(buffer_p, FIT_IMAGES_PATH);
576 if (confs_noffset < 0 || images_noffset < 0) {
577 debug("FPGA: No Configurations or images nodes were found.\n");
581 /* Get default configuration unit name from default property */
582 confs_noffset = fit_conf_get_node(buffer_p, NULL);
583 if (confs_noffset < 0) {
584 debug("FPGA: No default configuration was found in config.\n");
588 count = fit_conf_get_prop_node_count(buffer_p, confs_noffset,
591 debug("FPGA: Invalid configuration format for FPGA node.\n");
594 debug("FPGA: FPGA node count: %d\n", count);
596 for (i = 0; i < count; i++) {
597 images_noffset = fit_conf_get_prop_node_index(buffer_p,
600 uname = fit_get_name(buffer_p, images_noffset, NULL);
602 debug("FPGA: %s\n", uname);
604 if (strstr(uname, "fpga-periph") &&
605 (!is_fpgamgr_early_user_mode() ||
606 is_fpgamgr_user_mode())) {
607 fpga_node_name = uname;
608 printf("FPGA: Start to program ");
609 printf("peripheral/full bitstream ...\n");
611 } else if (strstr(uname, "fpga-core") &&
612 (is_fpgamgr_early_user_mode() &&
613 !is_fpgamgr_user_mode())) {
614 fpga_node_name = uname;
615 printf("FPGA: Start to program core ");
616 printf("bitstream ...\n");
623 if (!fpga_node_name) {
624 debug("FPGA: No suitable bitstream was found, count: %d.\n", i);
628 images_noffset = fit_image_get_node(buffer_p, fpga_node_name);
629 if (images_noffset < 0) {
630 debug("FPGA: No node '%s' was found in FIT.\n",
635 if (!fit_image_get_data_position(buffer_p, images_noffset,
637 debug("FPGA: Data position was found.\n");
638 } else if (!fit_image_get_data_offset(buffer_p, images_noffset,
641 * For FIT with external data, figure out where
642 * the external images start. This is the base
643 * for the data-offset properties in each image.
645 rbf_offset += ((fdt_totalsize(buffer_p) + 3) & ~3);
646 debug("FPGA: Data offset was found.\n");
648 debug("FPGA: No data position/offset was found.\n");
652 ret = fit_image_get_data_size(buffer_p, images_noffset, &rbf_size);
654 debug("FPGA: No data size was found (err=%d).\n", ret);
658 if (gd->ram_size < rbf_size) {
659 debug("FPGA: Using default OCRAM buffer and size.\n");
661 ret = fit_image_get_load(buffer_p, images_noffset,
664 buffer_p = (u32 *)DEFAULT_DDR_LOAD_ADDRESS;
665 debug("FPGA: No loadable was found.\n");
666 debug("FPGA: Using default DDR load address: 0x%x .\n",
667 DEFAULT_DDR_LOAD_ADDRESS);
669 buffer_p = (u32 *)*loadable;
670 debug("FPGA: Found loadable address = 0x%x.\n",
674 buffer_size = rbf_size;
677 debug("FPGA: External data: offset = 0x%x, size = 0x%x.\n",
678 rbf_offset, rbf_size);
680 fpga_loadfs->remaining = rbf_size;
683 * Determine buffer size vs bitstream size, and calculating number of
684 * chunk by chunk transfer is required due to smaller buffer size
685 * compare to bitstream
687 if (rbf_size <= buffer_size) {
688 /* Loading whole bitstream into buffer */
689 buffer_size = rbf_size;
690 fpga_loadfs->remaining = 0;
692 fpga_loadfs->remaining -= buffer_size;
695 fpga_loadfs->offset = rbf_offset;
696 /* Loading bitstream into buffer */
697 ret = request_firmware_into_buf(dev,
698 fpga_loadfs->fpga_fsinfo->filename,
699 buffer_p, buffer_size,
700 fpga_loadfs->offset);
702 debug("FPGA: Failed to read bitstream from flash.\n");
706 /* Getting info about bitstream types */
707 get_rbf_image_info(&fpga_loadfs->rbfinfo, (u16 *)buffer_p);
709 /* Update next reading bitstream offset */
710 fpga_loadfs->offset += buffer_size;
712 /* Update the final addr for bitstream */
713 *buffer = (u32)buffer_p;
715 /* Update the size of bitstream to be programmed into FPGA */
716 *buffer_bsize = buffer_size;
721 static int subsequent_loading_rbf_to_buffer(struct udevice *dev,
722 struct fpga_loadfs_info *fpga_loadfs,
723 u32 *buffer, size_t *buffer_bsize)
726 u32 *buffer_p = (u32 *)*buffer;
728 /* Read the bitstream chunk by chunk. */
729 if (fpga_loadfs->remaining > *buffer_bsize) {
730 fpga_loadfs->remaining -= *buffer_bsize;
732 *buffer_bsize = fpga_loadfs->remaining;
733 fpga_loadfs->remaining = 0;
736 ret = request_firmware_into_buf(dev,
737 fpga_loadfs->fpga_fsinfo->filename,
738 buffer_p, *buffer_bsize,
739 fpga_loadfs->offset);
741 debug("FPGA: Failed to read bitstream from flash.\n");
745 /* Update next reading bitstream offset */
746 fpga_loadfs->offset += *buffer_bsize;
751 int socfpga_loadfs(fpga_fs_info *fpga_fsinfo, const void *buf, size_t bsize,
754 struct fpga_loadfs_info fpga_loadfs;
756 int status, ret, size;
757 u32 buffer = (uintptr_t)buf;
758 size_t buffer_sizebytes = bsize;
759 size_t buffer_sizebytes_ori = bsize;
760 size_t total_sizeof_image = 0;
762 const fdt32_t *phandle_p;
765 node = get_fpga_mgr_ofnode(ofnode_null());
767 if (ofnode_valid(node)) {
768 phandle_p = ofnode_get_property(node, "firmware-loader", &size);
770 node = ofnode_path("/chosen");
771 if (!ofnode_valid(node)) {
772 debug("FPGA: /chosen node was not found.\n");
776 phandle_p = ofnode_get_property(node, "firmware-loader",
779 debug("FPGA: firmware-loader property was not");
785 debug("FPGA: FPGA manager node was not found.\n");
789 phandle = fdt32_to_cpu(*phandle_p);
790 ret = uclass_get_device_by_phandle_id(UCLASS_FS_FIRMWARE_LOADER,
795 memset(&fpga_loadfs, 0, sizeof(fpga_loadfs));
797 fpga_loadfs.fpga_fsinfo = fpga_fsinfo;
798 fpga_loadfs.offset = offset;
800 printf("FPGA: Checking FPGA configuration setting ...\n");
803 * Note: Both buffer and buffer_sizebytes values can be altered by
806 ret = first_loading_rbf_to_buffer(dev, &fpga_loadfs, &buffer,
809 printf("FPGA: Skipping configuration ...\n");
815 if (fpga_loadfs.rbfinfo.section == core_section &&
816 !(is_fpgamgr_early_user_mode() && !is_fpgamgr_user_mode())) {
817 debug("FPGA : Must be in Early Release mode to program ");
818 debug("core bitstream.\n");
822 /* Disable all signals from HPS peripheral controller to FPGA */
823 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_A10_FPGAINTF_EN_GLOBAL);
825 /* Disable all axi bridges (hps2fpga, lwhps2fpga & fpga2hps) */
826 socfpga_bridges_reset();
828 if (fpga_loadfs.rbfinfo.section == periph_section) {
829 /* Initialize the FPGA Manager */
830 status = fpgamgr_program_init((u32 *)buffer, buffer_sizebytes);
832 debug("FPGA: Init with peripheral bitstream failed.\n");
837 /* Transfer bitstream to FPGA Manager */
838 fpgamgr_program_write((void *)buffer, buffer_sizebytes);
840 total_sizeof_image += buffer_sizebytes;
842 while (fpga_loadfs.remaining) {
843 ret = subsequent_loading_rbf_to_buffer(dev,
846 &buffer_sizebytes_ori);
851 /* Transfer data to FPGA Manager */
852 fpgamgr_program_write((void *)buffer,
853 buffer_sizebytes_ori);
855 total_sizeof_image += buffer_sizebytes_ori;
860 if (fpga_loadfs.rbfinfo.section == periph_section) {
861 if (fpgamgr_wait_early_user_mode() != -ETIMEDOUT) {
862 config_pins(gd->fdt_blob, "shared");
863 puts("FPGA: Early Release Succeeded.\n");
865 debug("FPGA: Failed to see Early Release.\n");
869 /* For monolithic bitstream */
870 if (is_fpgamgr_user_mode()) {
871 /* Ensure the FPGA entering config done */
872 status = fpgamgr_program_finish();
876 config_pins(gd->fdt_blob, "fpga");
877 puts("FPGA: Enter user mode.\n");
879 } else if (fpga_loadfs.rbfinfo.section == core_section) {
880 /* Ensure the FPGA entering config done */
881 status = fpgamgr_program_finish();
885 config_pins(gd->fdt_blob, "fpga");
886 puts("FPGA: Enter user mode.\n");
888 debug("FPGA: Config Error: Unsupported bitstream type.\n");
892 return (int)total_sizeof_image;
895 void fpgamgr_program(const void *buf, size_t bsize, u32 offset)
897 fpga_fs_info fpga_fsinfo;
899 fpga_fsinfo.filename = get_fpga_filename();
901 if (fpga_fsinfo.filename)
902 socfpga_loadfs(&fpga_fsinfo, buf, bsize, offset);
906 /* This function is used to load the core bitstream from the OCRAM. */
907 int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
909 unsigned long status;
910 struct rbf_info rbfinfo;
912 memset(&rbfinfo, 0, sizeof(rbfinfo));
914 /* Disable all signals from hps peripheral controller to fpga */
915 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_A10_FPGAINTF_EN_GLOBAL);
917 /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */
918 socfpga_bridges_reset();
920 /* Getting info about bitstream types */
921 get_rbf_image_info(&rbfinfo, (u16 *)rbf_data);
923 if (rbfinfo.section == periph_section) {
924 /* Initialize the FPGA Manager */
925 status = fpgamgr_program_init((u32 *)rbf_data, rbf_size);
930 if (rbfinfo.section == core_section &&
931 !(is_fpgamgr_early_user_mode() && !is_fpgamgr_user_mode())) {
932 debug("FPGA : Must be in early release mode to program ");
933 debug("core bitstream.\n");
937 /* Write the bitstream to FPGA Manager */
938 fpgamgr_program_write(rbf_data, rbf_size);
940 status = fpgamgr_program_finish();
944 config_pins(gd->fdt_blob, "fpga");
945 puts("FPGA: Enter user mode.\n");