1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017-2019 Intel Corporation <www.intel.com>
7 #include <asm/global_data.h>
9 #include <asm/arch/fpga_manager.h>
10 #include <asm/arch/reset_manager.h>
11 #include <asm/arch/system_manager.h>
12 #include <asm/arch/sdram.h>
13 #include <asm/arch/misc.h>
15 #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
32 #define DDR_BUFFER_SIZE 0x100000
34 /* When reading bitstream from a filesystem, the size of the first read is
35 * changed so that the subsequent reads are aligned to this value. This value
36 * was chosen so that in subsequent reads the fat fs driver doesn't have to
37 * allocate a temporary buffer in get_contents (assuming 8KiB clusters).
39 #define MAX_FIRST_LOAD_SIZE 0x2000
41 DECLARE_GLOBAL_DATA_PTR;
43 static const struct socfpga_fpga_manager *fpga_manager_base =
44 (void *)SOCFPGA_FPGAMGRREGS_ADDRESS;
46 static void fpgamgr_set_cd_ratio(unsigned long ratio);
48 static uint32_t fpgamgr_get_msel(void)
52 reg = readl(&fpga_manager_base->imgcfg_stat);
53 reg = (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL_SET_MSD) >>
54 ALT_FPGAMGR_IMGCFG_STAT_F2S_MSEL0_LSB;
59 static void fpgamgr_set_cfgwdth(int width)
62 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
63 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
65 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
66 ALT_FPGAMGR_IMGCFG_CTL_02_CFGWIDTH_SET_MSK);
69 int is_fpgamgr_user_mode(void)
71 return (readl(&fpga_manager_base->imgcfg_stat) &
72 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) != 0;
75 static int wait_for_user_mode(void)
77 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
78 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK,
79 1, FPGA_TIMEOUT_MSEC, false);
82 static int wait_for_fifo_empty(void)
84 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
85 ALT_FPGAMGR_IMGCFG_STAT_F2S_IMGCFG_FIFOEMPTY_SET_MSK,
86 1, FPGA_TIMEOUT_MSEC, false);
89 int is_fpgamgr_early_user_mode(void)
91 return (readl(&fpga_manager_base->imgcfg_stat) &
92 ALT_FPGAMGR_IMGCFG_STAT_F2S_EARLY_USERMODE_SET_MSK) != 0;
95 int fpgamgr_wait_early_user_mode(void)
97 u32 sync_data = 0xffffffff;
99 unsigned start = get_timer(0);
100 unsigned long cd_ratio;
102 /* Getting existing CDRATIO */
103 cd_ratio = (readl(&fpga_manager_base->imgcfg_ctrl_02) &
104 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK) >>
105 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB;
107 /* Using CDRATIO_X1 for better compatibility */
108 fpgamgr_set_cd_ratio(CDRATIO_x1);
110 while (!is_fpgamgr_early_user_mode()) {
111 if (get_timer(start) > FPGA_TIMEOUT_MSEC)
113 fpgamgr_program_write((const long unsigned int *)&sync_data,
115 udelay(FPGA_TIMEOUT_MSEC);
119 debug("FPGA: Additional %i sync word needed\n", i);
121 /* restoring original CDRATIO */
122 fpgamgr_set_cd_ratio(cd_ratio);
127 /* Read f2s_nconfig_pin and f2s_nstatus_pin; loop until de-asserted */
128 static int wait_for_nconfig_pin_and_nstatus_pin(void)
130 unsigned long mask = ALT_FPGAMGR_IMGCFG_STAT_F2S_NCONFIG_PIN_SET_MSK |
131 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK;
134 * Poll until f2s_nconfig_pin and f2s_nstatus_pin; loop until
135 * de-asserted, timeout at 1000ms
137 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat, mask,
138 true, FPGA_TIMEOUT_MSEC, false);
141 static int wait_for_f2s_nstatus_pin(unsigned long value)
143 /* Poll until f2s to specific value, timeout at 1000ms */
144 return wait_for_bit_le32(&fpga_manager_base->imgcfg_stat,
145 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK,
146 value, FPGA_TIMEOUT_MSEC, false);
150 static void fpgamgr_set_cd_ratio(unsigned long ratio)
152 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
153 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
155 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
156 (ratio << ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_LSB) &
157 ALT_FPGAMGR_IMGCFG_CTL_02_CDRATIO_SET_MSK);
160 /* get the MSEL value, verify we are set for FPP configuration mode */
161 static int fpgamgr_verify_msel(void)
163 u32 msel = fpgamgr_get_msel();
165 if (msel & ~BIT(0)) {
166 printf("Fail: read msel=%d\n", msel);
174 * Write cdratio and cdwidth based on whether the bitstream is compressed
177 static int fpgamgr_set_cdratio_cdwidth(unsigned int cfg_width, u32 *rbf_data,
180 unsigned int cd_ratio;
181 bool encrypt, compress;
184 * According to the bitstream specification,
185 * both encryption and compression status are
186 * in location before offset 230 of the buffer.
188 if (rbf_size < MIN_BITSTREAM_SIZECHECK)
191 encrypt = (rbf_data[ENCRYPTION_OFFSET] >> 2) & 3;
192 encrypt = encrypt != 0;
194 compress = (rbf_data[COMPRESSION_OFFSET] >> 1) & 1;
195 compress = !compress;
197 debug("FPGA: Header word %d = %08x.\n", 69, rbf_data[69]);
198 debug("FPGA: Header word %d = %08x.\n", 229, rbf_data[229]);
199 debug("FPGA: Read from rbf header: encrypt=%d compress=%d.\n", encrypt,
203 * from the register map description of cdratio in imgcfg_ctrl_02:
204 * Normal Configuration : 32bit Passive Parallel
205 * Partial Reconfiguration : 16bit Passive Parallel
209 * cd ratio is dependent on cfg width and whether the bitstream
210 * is encrypted and/or compressed.
212 * | width | encr. | compr. | cd ratio |
222 if (!compress && !encrypt) {
223 cd_ratio = CDRATIO_x1;
226 cd_ratio = CDRATIO_x4;
228 cd_ratio = CDRATIO_x2;
230 /* if 32 bit, double the cd ratio (so register
231 field setting is incremented) */
232 if (cfg_width == CFGWDTH_32)
236 fpgamgr_set_cfgwdth(cfg_width);
237 fpgamgr_set_cd_ratio(cd_ratio);
242 static int fpgamgr_reset(void)
246 /* S2F_NCONFIG = 0 */
247 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
248 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
250 /* Wait for f2s_nstatus == 0 */
251 if (wait_for_f2s_nstatus_pin(0))
254 /* S2F_NCONFIG = 1 */
255 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
256 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
258 /* Wait for f2s_nstatus == 1 */
259 if (wait_for_f2s_nstatus_pin(1))
262 /* read and confirm f2s_condone_pin = 0 and f2s_condone_oe = 1 */
263 reg = readl(&fpga_manager_base->imgcfg_stat);
264 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) != 0)
267 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_OE_SET_MSK) == 0)
273 /* Start the FPGA programming by initialize the FPGA Manager */
274 int fpgamgr_program_init(u32 * rbf_data, size_t rbf_size)
279 if (fpgamgr_verify_msel())
283 if (fpgamgr_set_cdratio_cdwidth(CFGWDTH_32, rbf_data, rbf_size))
288 * Make sure no other external devices are trying to interfere with
291 if (wait_for_nconfig_pin_and_nstatus_pin())
296 * Deassert the signal drives from HPS
306 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
307 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
309 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
310 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_PR_REQUEST_SET_MSK);
312 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
313 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
314 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
316 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
317 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NCONFIG_SET_MSK);
319 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
320 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NSTATUS_OE_SET_MSK |
321 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_CONDONE_OE_SET_MSK);
326 * S2F_NENABLE_CONFIG = 0
327 * S2F_NENABLE_NCONFIG = 0
329 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
330 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
331 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
332 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
335 * Disable driving signals that HPS doesn't need to drive.
336 * S2F_NENABLE_NSTATUS = 1
337 * S2F_NENABLE_CONDONE = 1
339 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
340 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NSTATUS_SET_MSK |
341 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_CONDONE_SET_MSK);
345 * Drive chip select S2F_NCE = 0
347 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
348 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
351 if (wait_for_nconfig_pin_and_nstatus_pin())
355 ret = fpgamgr_reset();
362 * EN_CFG_CTRL and EN_CFG_DATA = 1
364 setbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
365 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
366 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
371 /* Ensure the FPGA entering config done */
372 static int fpgamgr_program_poll_cd(void)
374 unsigned long reg, i;
376 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
377 reg = readl(&fpga_manager_base->imgcfg_stat);
378 if (reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK)
381 if ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) == 0) {
382 printf("nstatus == 0 while waiting for condone\n");
388 if (i == FPGA_TIMEOUT_CNT)
394 /* Ensure the FPGA entering user mode */
395 static int fpgamgr_program_poll_usermode(void)
400 if (fpgamgr_dclkcnt_set(0xf))
403 ret = wait_for_user_mode();
405 printf("%s: Failed to enter user mode with ", __func__);
406 printf("error code %d\n", ret);
412 * Stop DATA path and Dclk
413 * EN_CFG_CTRL and EN_CFG_DATA = 0
415 clrbits_le32(&fpga_manager_base->imgcfg_ctrl_02,
416 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_DATA_SET_MSK |
417 ALT_FPGAMGR_IMGCFG_CTL_02_EN_CFG_CTRL_SET_MSK);
422 * S2F_NENABLE_CONFIG = 1
423 * S2F_NENABLE_NCONFIG = 1
425 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
426 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NENABLE_CONFIG_SET_MSK);
427 setbits_le32(&fpga_manager_base->imgcfg_ctrl_00,
428 ALT_FPGAMGR_IMGCFG_CTL_00_S2F_NENABLE_NCONFIG_SET_MSK);
430 /* Disable chip select S2F_NCE = 1 */
431 setbits_le32(&fpga_manager_base->imgcfg_ctrl_01,
432 ALT_FPGAMGR_IMGCFG_CTL_01_S2F_NCE_SET_MSK);
438 reg = readl(&fpga_manager_base->imgcfg_stat);
439 if (((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) !=
440 ALT_FPGAMGR_IMGCFG_STAT_F2S_USERMODE_SET_MSK) ||
441 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) !=
442 ALT_FPGAMGR_IMGCFG_STAT_F2S_CONDONE_PIN_SET_MSK) ||
443 ((reg & ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK) !=
444 ALT_FPGAMGR_IMGCFG_STAT_F2S_NSTATUS_PIN_SET_MSK))
450 int fpgamgr_program_finish(void)
452 /* Ensure the FPGA entering config done */
453 int status = fpgamgr_program_poll_cd();
456 printf("FPGA: Poll CD failed with error code %d\n", status);
460 /* Ensure the FPGA entering user mode */
461 status = fpgamgr_program_poll_usermode();
463 printf("FPGA: Poll usermode failed with error code %d\n",
468 printf("Full Configuration Succeeded.\n");
473 ofnode get_fpga_mgr_ofnode(ofnode from)
475 return ofnode_by_compatible(from, "altr,socfpga-a10-fpga-mgr");
478 const char *get_fpga_filename(void)
480 const char *fpga_filename = NULL;
482 ofnode fpgamgr_node = get_fpga_mgr_ofnode(ofnode_null());
484 if (ofnode_valid(fpgamgr_node))
485 fpga_filename = ofnode_read_string(fpgamgr_node,
488 return fpga_filename;
491 static void get_rbf_image_info(struct rbf_info *rbf, u16 *buffer)
494 * Magic ID starting at:
495 * -> 1st dword[15:0] in periph.rbf
496 * -> 2nd dword[15:0] in core.rbf
497 * Note: dword == 32 bits
499 u32 word_reading_max = 2;
502 for (i = 0; i < word_reading_max; i++) {
503 if (*(buffer + i) == FPGA_SOCFPGA_A10_RBF_UNENCRYPTED) {
504 rbf->security = unencrypted;
505 } else if (*(buffer + i) == FPGA_SOCFPGA_A10_RBF_ENCRYPTED) {
506 rbf->security = encrypted;
507 } else if (*(buffer + i + 1) ==
508 FPGA_SOCFPGA_A10_RBF_UNENCRYPTED) {
509 rbf->security = unencrypted;
510 } else if (*(buffer + i + 1) ==
511 FPGA_SOCFPGA_A10_RBF_ENCRYPTED) {
512 rbf->security = encrypted;
514 rbf->security = invalid;
518 /* PERIPH RBF(buffer + i + 1), CORE RBF(buffer + i + 2) */
519 if (*(buffer + i + 1) == FPGA_SOCFPGA_A10_RBF_PERIPH) {
520 rbf->section = periph_section;
522 } else if (*(buffer + i + 1) == FPGA_SOCFPGA_A10_RBF_CORE) {
523 rbf->section = core_section;
525 } else if (*(buffer + i + 2) == FPGA_SOCFPGA_A10_RBF_PERIPH) {
526 rbf->section = periph_section;
528 } else if (*(buffer + i + 2) == FPGA_SOCFPGA_A10_RBF_CORE) {
529 rbf->section = core_section;
533 rbf->section = unknown;
540 #ifdef CONFIG_FS_LOADER
541 static int first_loading_rbf_to_buffer(struct udevice *dev,
542 struct fpga_loadfs_info *fpga_loadfs,
543 u32 *buffer, size_t *buffer_bsize,
544 size_t *buffer_bsize_ori)
546 u32 *buffer_p = (u32 *)*buffer;
547 u32 *loadable = buffer_p;
548 size_t buffer_size = *buffer_bsize;
550 int ret, i, count, confs_noffset, images_noffset, rbf_offset, rbf_size;
551 const char *fpga_node_name = NULL;
552 const char *uname = NULL;
554 /* Load image header into buffer */
555 ret = request_firmware_into_buf(dev,
556 fpga_loadfs->fpga_fsinfo->filename,
557 buffer_p, sizeof(struct legacy_img_hdr),
560 debug("FPGA: Failed to read image header from flash.\n");
564 if (image_get_magic((struct legacy_img_hdr *)buffer_p) != FDT_MAGIC) {
565 debug("FPGA: No FDT magic was found.\n");
569 fit_size = fdt_totalsize(buffer_p);
571 if (fit_size > buffer_size) {
572 debug("FPGA: FIT image is larger than available buffer.\n");
573 debug("Please use FIT external data or increasing buffer.\n");
577 /* Load entire FIT into buffer */
578 ret = request_firmware_into_buf(dev,
579 fpga_loadfs->fpga_fsinfo->filename,
580 buffer_p, fit_size, 0);
584 ret = fit_check_format(buffer_p, IMAGE_SIZE_INVAL);
586 debug("FPGA: No valid FIT image was found.\n");
590 confs_noffset = fdt_path_offset(buffer_p, FIT_CONFS_PATH);
591 images_noffset = fdt_path_offset(buffer_p, FIT_IMAGES_PATH);
592 if (confs_noffset < 0 || images_noffset < 0) {
593 debug("FPGA: No Configurations or images nodes were found.\n");
597 /* Get default configuration unit name from default property */
598 confs_noffset = fit_conf_get_node(buffer_p, NULL);
599 if (confs_noffset < 0) {
600 debug("FPGA: No default configuration was found in config.\n");
604 count = fit_conf_get_prop_node_count(buffer_p, confs_noffset,
607 debug("FPGA: Invalid configuration format for FPGA node.\n");
610 debug("FPGA: FPGA node count: %d\n", count);
612 for (i = 0; i < count; i++) {
613 images_noffset = fit_conf_get_prop_node_index(buffer_p,
616 uname = fit_get_name(buffer_p, images_noffset, NULL);
618 debug("FPGA: %s\n", uname);
620 if (strstr(uname, "fpga-periph") &&
621 (!is_fpgamgr_early_user_mode() ||
622 is_fpgamgr_user_mode() ||
623 is_periph_program_force())) {
624 fpga_node_name = uname;
625 printf("FPGA: Start to program ");
626 printf("peripheral/full bitstream ...\n");
628 } else if (strstr(uname, "fpga-core") &&
629 (is_fpgamgr_early_user_mode() &&
630 !is_fpgamgr_user_mode())) {
631 fpga_node_name = uname;
632 printf("FPGA: Start to program core ");
633 printf("bitstream ...\n");
640 if (!fpga_node_name) {
641 debug("FPGA: No suitable bitstream was found, count: %d.\n", i);
645 images_noffset = fit_image_get_node(buffer_p, fpga_node_name);
646 if (images_noffset < 0) {
647 debug("FPGA: No node '%s' was found in FIT.\n",
652 if (!fit_image_get_data_position(buffer_p, images_noffset,
654 debug("FPGA: Data position was found.\n");
655 } else if (!fit_image_get_data_offset(buffer_p, images_noffset,
658 * For FIT with external data, figure out where
659 * the external images start. This is the base
660 * for the data-offset properties in each image.
662 rbf_offset += ((fdt_totalsize(buffer_p) + 3) & ~3);
663 debug("FPGA: Data offset was found.\n");
665 debug("FPGA: No data position/offset was found.\n");
669 ret = fit_image_get_data_size(buffer_p, images_noffset, &rbf_size);
671 debug("FPGA: No data size was found (err=%d).\n", ret);
675 if (gd->ram_size < rbf_size) {
676 debug("FPGA: Using default OCRAM buffer and size.\n");
678 ret = fit_image_get_load(buffer_p, images_noffset,
681 buffer_p = (u32 *)DEFAULT_DDR_LOAD_ADDRESS;
682 debug("FPGA: No loadable was found.\n");
683 debug("FPGA: Using default DDR load address: 0x%x .\n",
684 DEFAULT_DDR_LOAD_ADDRESS);
686 buffer_p = (u32 *)*loadable;
687 debug("FPGA: Found loadable address = 0x%x.\n",
691 buffer_size = rbf_size;
692 *buffer_bsize_ori = DDR_BUFFER_SIZE;
695 debug("FPGA: External data: offset = 0x%x, size = 0x%x.\n",
696 rbf_offset, rbf_size);
698 fpga_loadfs->remaining = rbf_size;
701 * Determine buffer size vs bitstream size, and calculating number of
702 * chunk by chunk transfer is required due to smaller buffer size
703 * compare to bitstream
706 if (buffer_size > MAX_FIRST_LOAD_SIZE)
707 buffer_size = MAX_FIRST_LOAD_SIZE;
709 if (rbf_size <= buffer_size) {
710 /* Loading whole bitstream into buffer */
711 buffer_size = rbf_size;
712 fpga_loadfs->remaining = 0;
714 buffer_size -= rbf_offset % buffer_size;
715 fpga_loadfs->remaining -= buffer_size;
718 fpga_loadfs->offset = rbf_offset;
719 /* Loading bitstream into buffer */
720 ret = request_firmware_into_buf(dev,
721 fpga_loadfs->fpga_fsinfo->filename,
722 buffer_p, buffer_size,
723 fpga_loadfs->offset);
725 debug("FPGA: Failed to read bitstream from flash.\n");
729 /* Getting info about bitstream types */
730 get_rbf_image_info(&fpga_loadfs->rbfinfo, (u16 *)buffer_p);
732 /* Update next reading bitstream offset */
733 fpga_loadfs->offset += buffer_size;
735 /* Update the final addr for bitstream */
736 *buffer = (u32)buffer_p;
738 /* Update the size of bitstream to be programmed into FPGA */
739 *buffer_bsize = buffer_size;
744 static int subsequent_loading_rbf_to_buffer(struct udevice *dev,
745 struct fpga_loadfs_info *fpga_loadfs,
746 u32 *buffer, size_t *buffer_bsize)
749 u32 *buffer_p = (u32 *)*buffer;
751 /* Read the bitstream chunk by chunk. */
752 if (fpga_loadfs->remaining > *buffer_bsize) {
753 fpga_loadfs->remaining -= *buffer_bsize;
755 *buffer_bsize = fpga_loadfs->remaining;
756 fpga_loadfs->remaining = 0;
759 ret = request_firmware_into_buf(dev,
760 fpga_loadfs->fpga_fsinfo->filename,
761 buffer_p, *buffer_bsize,
762 fpga_loadfs->offset);
764 debug("FPGA: Failed to read bitstream from flash.\n");
768 /* Update next reading bitstream offset */
769 fpga_loadfs->offset += *buffer_bsize;
774 int socfpga_loadfs(fpga_fs_info *fpga_fsinfo, const void *buf, size_t bsize,
777 struct fpga_loadfs_info fpga_loadfs;
780 u32 buffer = (uintptr_t)buf;
781 size_t buffer_sizebytes = bsize;
782 size_t buffer_sizebytes_ori = bsize;
783 size_t total_sizeof_image = 0;
786 node = get_fpga_mgr_ofnode(ofnode_null());
787 if (!ofnode_valid(node)) {
788 debug("FPGA: FPGA manager node was not found.\n");
792 ret = get_fs_loader(&dev);
796 memset(&fpga_loadfs, 0, sizeof(fpga_loadfs));
798 fpga_loadfs.fpga_fsinfo = fpga_fsinfo;
799 fpga_loadfs.offset = offset;
801 printf("FPGA: Checking FPGA configuration setting ...\n");
804 * Note: Both buffer and buffer_sizebytes values can be altered by
807 ret = first_loading_rbf_to_buffer(dev, &fpga_loadfs, &buffer,
809 &buffer_sizebytes_ori);
811 printf("FPGA: Skipping configuration ...\n");
817 if (fpga_loadfs.rbfinfo.section == core_section &&
818 !(is_fpgamgr_early_user_mode() && !is_fpgamgr_user_mode())) {
819 debug("FPGA : Must be in Early Release mode to program ");
820 debug("core bitstream.\n");
824 /* Disable all signals from HPS peripheral controller to FPGA */
825 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_A10_FPGAINTF_EN_GLOBAL);
827 /* Disable all axi bridges (hps2fpga, lwhps2fpga & fpga2hps) */
828 socfpga_bridges_reset();
830 if (fpga_loadfs.rbfinfo.section == periph_section) {
831 /* Initialize the FPGA Manager */
832 status = fpgamgr_program_init((u32 *)buffer, buffer_sizebytes);
834 debug("FPGA: Init with peripheral bitstream failed.\n");
839 /* Transfer bitstream to FPGA Manager */
840 fpgamgr_program_write((void *)buffer, buffer_sizebytes);
842 total_sizeof_image += buffer_sizebytes;
844 while (fpga_loadfs.remaining) {
845 ret = subsequent_loading_rbf_to_buffer(dev,
848 &buffer_sizebytes_ori);
853 /* Transfer data to FPGA Manager */
854 fpgamgr_program_write((void *)buffer,
855 buffer_sizebytes_ori);
857 total_sizeof_image += buffer_sizebytes_ori;
861 wait_for_fifo_empty();
863 if (fpga_loadfs.rbfinfo.section == periph_section) {
864 if (fpgamgr_wait_early_user_mode() != -ETIMEDOUT) {
865 config_pins(gd->fdt_blob, "shared");
866 puts("FPGA: Early Release Succeeded.\n");
868 debug("FPGA: Failed to see Early Release.\n");
872 /* For monolithic bitstream */
873 if (is_fpgamgr_user_mode()) {
874 /* Ensure the FPGA entering config done */
875 status = fpgamgr_program_finish();
879 config_pins(gd->fdt_blob, "fpga");
880 puts("FPGA: Enter user mode.\n");
882 } else if (fpga_loadfs.rbfinfo.section == core_section) {
883 /* Ensure the FPGA entering config done */
884 status = fpgamgr_program_finish();
888 config_pins(gd->fdt_blob, "fpga");
889 puts("FPGA: Enter user mode.\n");
891 debug("FPGA: Config Error: Unsupported bitstream type.\n");
895 return (int)total_sizeof_image;
898 void fpgamgr_program(const void *buf, size_t bsize, u32 offset)
900 fpga_fs_info fpga_fsinfo;
902 fpga_fsinfo.filename = get_fpga_filename();
904 if (fpga_fsinfo.filename)
905 socfpga_loadfs(&fpga_fsinfo, buf, bsize, offset);
909 /* This function is used to load the core bitstream from the OCRAM. */
910 int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
912 unsigned long status;
913 struct rbf_info rbfinfo;
915 memset(&rbfinfo, 0, sizeof(rbfinfo));
917 /* Disable all signals from hps peripheral controller to fpga */
918 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_A10_FPGAINTF_EN_GLOBAL);
920 /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */
921 socfpga_bridges_reset();
923 /* Getting info about bitstream types */
924 get_rbf_image_info(&rbfinfo, (u16 *)rbf_data);
926 if (rbfinfo.section == periph_section) {
927 /* Initialize the FPGA Manager */
928 status = fpgamgr_program_init((u32 *)rbf_data, rbf_size);
933 if (rbfinfo.section == core_section &&
934 !(is_fpgamgr_early_user_mode() && !is_fpgamgr_user_mode())) {
935 debug("FPGA : Must be in early release mode to program ");
936 debug("core bitstream.\n");
940 /* Write the bitstream to FPGA Manager */
941 fpgamgr_program_write(rbf_data, rbf_size);
943 status = fpgamgr_program_finish();
947 config_pins(gd->fdt_blob, "fpga");
948 puts("FPGA: Enter user mode.\n");