]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
arm: socfpga: fpga: Add SoCFPGA FPGA programming interface
authorPavel Machek <pavel@denx.de>
Mon, 8 Sep 2014 12:08:45 +0000 (14:08 +0200)
committerMarek Vasut <marex@denx.de>
Mon, 6 Oct 2014 15:46:50 +0000 (17:46 +0200)
Add code necessary to program the FPGA part of SoCFPGA from U-Boot
with an RBF blob. This patch also integrates the code into the
FPGA driver framework in U-Boot so it can be used via the 'fpga'
command.

Signed-off-by: Pavel Machek <pavel@denx.de>
Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Chin Liang See <clsee@altera.com>
Cc: Dinh Nguyen <dinguyen@altera.com>
Cc: Albert Aribaud <albert.u.boot@aribaud.net>
Cc: Tom Rini <trini@ti.com>
Cc: Wolfgang Denk <wd@denx.de>
Cc: Pavel Machek <pavel@denx.de>
V2: Move the not-CPU specific stuff into drivers/fpga/ and base
    this on the cleaned up altera FPGA support.

arch/arm/cpu/armv7/socfpga/Makefile
arch/arm/cpu/armv7/socfpga/fpga_manager.c [new file with mode: 0644]
arch/arm/cpu/armv7/socfpga/misc.c
arch/arm/include/asm/arch-socfpga/fpga_manager.h [new file with mode: 0644]
drivers/fpga/Makefile
drivers/fpga/altera.c
drivers/fpga/socfpga.c [new file with mode: 0644]
include/altera.h

index eb33f2c5fb1d9720c57cab6a3585cd94018ceef4..8b6e108c429cce0c67c2ea8d957433783f15486c 100644 (file)
@@ -8,5 +8,6 @@
 #
 
 obj-y  := lowlevel_init.o
-obj-y  += misc.o timer.o reset_manager.o system_manager.o clock_manager.o
+obj-y  += misc.o timer.o reset_manager.o system_manager.o clock_manager.o \
+          fpga_manager.o
 obj-$(CONFIG_SPL_BUILD) += spl.o freeze_controller.o scan_manager.o
diff --git a/arch/arm/cpu/armv7/socfpga/fpga_manager.c b/arch/arm/cpu/armv7/socfpga/fpga_manager.c
new file mode 100644 (file)
index 0000000..43fd2fe
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2012 Altera Corporation <www.altera.com>
+ * All rights reserved.
+ *
+ * This file contains only support functions used also by the SoCFPGA
+ * platform code, the real meat is located in drivers/fpga/socfpga.c .
+ *
+ * SPDX-License-Identifier:    BSD-3-Clause
+ */
+
+#include <common.h>
+#include <asm/io.h>
+#include <asm/errno.h>
+#include <asm/arch/fpga_manager.h>
+#include <asm/arch/reset_manager.h>
+#include <asm/arch/system_manager.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Timeout count */
+#define FPGA_TIMEOUT_CNT               0x1000000
+
+static struct socfpga_fpga_manager *fpgamgr_regs =
+       (struct socfpga_fpga_manager *)SOCFPGA_FPGAMGRREGS_ADDRESS;
+
+/* Check whether FPGA Init_Done signal is high */
+static int is_fpgamgr_initdone_high(void)
+{
+       unsigned long val;
+
+       val = readl(&fpgamgr_regs->gpio_ext_porta);
+       return val & FPGAMGRREGS_MON_GPIO_EXT_PORTA_ID_MASK;
+}
+
+/* Get the FPGA mode */
+int fpgamgr_get_mode(void)
+{
+       unsigned long val;
+
+       val = readl(&fpgamgr_regs->stat);
+       return val & FPGAMGRREGS_STAT_MODE_MASK;
+}
+
+/* Check whether FPGA is ready to be accessed */
+int fpgamgr_test_fpga_ready(void)
+{
+       /* Check for init done signal */
+       if (!is_fpgamgr_initdone_high())
+               return 0;
+
+       /* Check again to avoid false glitches */
+       if (!is_fpgamgr_initdone_high())
+               return 0;
+
+       if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_USERMODE)
+               return 0;
+
+       return 1;
+}
+
+/* Poll until FPGA is ready to be accessed or timeout occurred */
+int fpgamgr_poll_fpga_ready(void)
+{
+       unsigned long i;
+
+       /* If FPGA is blank, wait till WD invoke warm reset */
+       for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
+               /* check for init done signal */
+               if (!is_fpgamgr_initdone_high())
+                       continue;
+               /* check again to avoid false glitches */
+               if (!is_fpgamgr_initdone_high())
+                       continue;
+               return 1;
+       }
+
+       return 0;
+}
index f41c329a439e545b8a4d0a03eea3bf5f37853992..b07d97ee05cbcdf93a69860f9b2529ddc1020ab4 100644 (file)
@@ -6,6 +6,7 @@
 
 #include <common.h>
 #include <asm/io.h>
+#include <altera.h>
 #include <miiphy.h>
 #include <netdev.h>
 #include <asm/arch/reset_manager.h>
@@ -93,6 +94,39 @@ int overwrite_console(void)
 }
 #endif
 
+#ifdef CONFIG_FPGA
+/*
+ * FPGA programming support for SoC FPGA Cyclone V
+ */
+static Altera_desc altera_fpga[] = {
+       {
+               /* Family */
+               Altera_SoCFPGA,
+               /* Interface type */
+               fast_passive_parallel,
+               /* No limitation as additional data will be ignored */
+               -1,
+               /* No device function table */
+               NULL,
+               /* Base interface address specified in driver */
+               NULL,
+               /* No cookie implementation */
+               0
+       },
+};
+
+/* add device descriptor to FPGA device table */
+static void socfpga_fpga_add(void)
+{
+       int i;
+       fpga_init();
+       for (i = 0; i < ARRAY_SIZE(altera_fpga); i++)
+               fpga_add(fpga_altera, &altera_fpga[i]);
+}
+#else
+static inline void socfpga_fpga_add(void) {}
+#endif
+
 int arch_cpu_init(void)
 {
        /*
@@ -108,5 +142,7 @@ int arch_cpu_init(void)
 
 int misc_init_r(void)
 {
+       /* Add device descriptor to FPGA device table */
+       socfpga_fpga_add();
        return 0;
 }
diff --git a/arch/arm/include/asm/arch-socfpga/fpga_manager.h b/arch/arm/include/asm/arch-socfpga/fpga_manager.h
new file mode 100644 (file)
index 0000000..a077e22
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2012 Altera Corporation <www.altera.com>
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:    BSD-3-Clause
+ */
+
+#ifndef        _FPGA_MANAGER_H_
+#define        _FPGA_MANAGER_H_
+
+#include <altera.h>
+
+struct socfpga_fpga_manager {
+       /* FPGA Manager Module */
+       u32     stat;                   /* 0x00 */
+       u32     ctrl;
+       u32     dclkcnt;
+       u32     dclkstat;
+       u32     gpo;                    /* 0x10 */
+       u32     gpi;
+       u32     misci;                  /* 0x18 */
+       u32     _pad_0x1c_0x82c[517];
+
+       /* Configuration Monitor (MON) Registers */
+       u32     gpio_inten;             /* 0x830 */
+       u32     gpio_intmask;
+       u32     gpio_inttype_level;
+       u32     gpio_int_polarity;
+       u32     gpio_intstatus;         /* 0x840 */
+       u32     gpio_raw_intstatus;
+       u32     _pad_0x848;
+       u32     gpio_porta_eoi;
+       u32     gpio_ext_porta;         /* 0x850 */
+       u32     _pad_0x854_0x85c[3];
+       u32     gpio_1s_sync;           /* 0x860 */
+       u32     _pad_0x864_0x868[2];
+       u32     gpio_ver_id_code;
+       u32     gpio_config_reg2;       /* 0x870 */
+       u32     gpio_config_reg1;
+};
+
+#define FPGAMGRREGS_STAT_MODE_MASK             0x7
+#define FPGAMGRREGS_STAT_MSEL_MASK             0xf8
+#define FPGAMGRREGS_STAT_MSEL_LSB              3
+
+#define FPGAMGRREGS_CTRL_CFGWDTH_MASK          0x200
+#define FPGAMGRREGS_CTRL_AXICFGEN_MASK         0x100
+#define FPGAMGRREGS_CTRL_NCONFIGPULL_MASK      0x4
+#define FPGAMGRREGS_CTRL_NCE_MASK              0x2
+#define FPGAMGRREGS_CTRL_EN_MASK               0x1
+#define FPGAMGRREGS_CTRL_CDRATIO_LSB           6
+
+#define FPGAMGRREGS_MON_GPIO_EXT_PORTA_CRC_MASK        0x8
+#define FPGAMGRREGS_MON_GPIO_EXT_PORTA_ID_MASK 0x4
+#define FPGAMGRREGS_MON_GPIO_EXT_PORTA_CD_MASK 0x2
+#define FPGAMGRREGS_MON_GPIO_EXT_PORTA_NS_MASK 0x1
+
+/* FPGA Mode */
+#define FPGAMGRREGS_MODE_FPGAOFF               0x0
+#define FPGAMGRREGS_MODE_RESETPHASE            0x1
+#define FPGAMGRREGS_MODE_CFGPHASE              0x2
+#define FPGAMGRREGS_MODE_INITPHASE             0x3
+#define FPGAMGRREGS_MODE_USERMODE              0x4
+#define FPGAMGRREGS_MODE_UNKNOWN               0x5
+
+/* FPGA CD Ratio Value */
+#define CDRATIO_x1                             0x0
+#define CDRATIO_x2                             0x1
+#define CDRATIO_x4                             0x2
+#define CDRATIO_x8                             0x3
+
+/* SoCFPGA support functions */
+int fpgamgr_test_fpga_ready(void);
+int fpgamgr_poll_fpga_ready(void);
+int fpgamgr_get_mode(void);
+
+#endif /* _FPGA_MANAGER_H_ */
index dfb2e7fc760f564564b62840db878fd3c3b9083a..6aa24d43590b68e24851cdc9919dab887e2a16de 100644 (file)
@@ -17,4 +17,5 @@ obj-y += altera.o
 obj-$(CONFIG_FPGA_ACEX1K) += ACEX1K.o
 obj-$(CONFIG_FPGA_CYCLON2) += cyclon2.o
 obj-$(CONFIG_FPGA_STRATIX_II) += stratixII.o
+obj-$(CONFIG_FPGA_SOCFPGA) += socfpga.o
 endif
index fd2b4f0103d2e0061cc4f2b975afa18508817305..a5bfe5dce19a3dce36d35053031bfe4bf24c775e 100644 (file)
@@ -37,6 +37,9 @@ static const struct altera_fpga {
        { Altera_StratixII, "StratixII", StratixII_load,
          StratixII_dump, StratixII_info },
 #endif
+#if defined(CONFIG_FPGA_SOCFPGA)
+       { Altera_SoCFPGA, "SoC FPGA", socfpga_load, NULL, NULL },
+#endif
 };
 
 static int altera_validate(Altera_desc *desc, const char *fn)
diff --git a/drivers/fpga/socfpga.c b/drivers/fpga/socfpga.c
new file mode 100644 (file)
index 0000000..63b3566
--- /dev/null
@@ -0,0 +1,301 @@
+/*
+ * Copyright (C) 2012 Altera Corporation <www.altera.com>
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:    BSD-3-Clause
+ */
+
+#include <common.h>
+#include <asm/io.h>
+#include <asm/errno.h>
+#include <asm/arch/fpga_manager.h>
+#include <asm/arch/reset_manager.h>
+#include <asm/arch/system_manager.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Timeout count */
+#define FPGA_TIMEOUT_CNT               0x1000000
+
+static struct socfpga_fpga_manager *fpgamgr_regs =
+       (struct socfpga_fpga_manager *)SOCFPGA_FPGAMGRREGS_ADDRESS;
+static struct socfpga_system_manager *sysmgr_regs =
+       (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
+
+/* Set CD ratio */
+static void fpgamgr_set_cd_ratio(unsigned long ratio)
+{
+       clrsetbits_le32(&fpgamgr_regs->ctrl,
+                       0x3 << FPGAMGRREGS_CTRL_CDRATIO_LSB,
+                       (ratio & 0x3) << FPGAMGRREGS_CTRL_CDRATIO_LSB);
+}
+
+static int fpgamgr_dclkcnt_set(unsigned long cnt)
+{
+       unsigned long i;
+
+       /* Clear any existing done status */
+       if (readl(&fpgamgr_regs->dclkstat))
+               writel(0x1, &fpgamgr_regs->dclkstat);
+
+       /* Write the dclkcnt */
+       writel(cnt, &fpgamgr_regs->dclkcnt);
+
+       /* Wait till the dclkcnt done */
+       for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
+               if (!readl(&fpgamgr_regs->dclkstat))
+                       continue;
+
+               writel(0x1, &fpgamgr_regs->dclkstat);
+               return 0;
+       }
+
+       return -ETIMEDOUT;
+}
+
+/* Start the FPGA programming by initialize the FPGA Manager */
+static int fpgamgr_program_init(void)
+{
+       unsigned long msel, i;
+
+       /* Get the MSEL value */
+       msel = readl(&fpgamgr_regs->stat);
+       msel &= FPGAMGRREGS_STAT_MSEL_MASK;
+       msel >>= FPGAMGRREGS_STAT_MSEL_LSB;
+
+       /*
+        * Set the cfg width
+        * If MSEL[3] = 1, cfg width = 32 bit
+        */
+       if (msel & 0x8) {
+               setbits_le32(&fpgamgr_regs->ctrl,
+                            FPGAMGRREGS_CTRL_CFGWDTH_MASK);
+
+               /* To determine the CD ratio */
+               /* MSEL[1:0] = 0, CD Ratio = 1 */
+               if ((msel & 0x3) == 0x0)
+                       fpgamgr_set_cd_ratio(CDRATIO_x1);
+               /* MSEL[1:0] = 1, CD Ratio = 4 */
+               else if ((msel & 0x3) == 0x1)
+                       fpgamgr_set_cd_ratio(CDRATIO_x4);
+               /* MSEL[1:0] = 2, CD Ratio = 8 */
+               else if ((msel & 0x3) == 0x2)
+                       fpgamgr_set_cd_ratio(CDRATIO_x8);
+
+       } else {        /* MSEL[3] = 0 */
+               clrbits_le32(&fpgamgr_regs->ctrl,
+                            FPGAMGRREGS_CTRL_CFGWDTH_MASK);
+
+               /* To determine the CD ratio */
+               /* MSEL[1:0] = 0, CD Ratio = 1 */
+               if ((msel & 0x3) == 0x0)
+                       fpgamgr_set_cd_ratio(CDRATIO_x1);
+               /* MSEL[1:0] = 1, CD Ratio = 2 */
+               else if ((msel & 0x3) == 0x1)
+                       fpgamgr_set_cd_ratio(CDRATIO_x2);
+               /* MSEL[1:0] = 2, CD Ratio = 4 */
+               else if ((msel & 0x3) == 0x2)
+                       fpgamgr_set_cd_ratio(CDRATIO_x4);
+       }
+
+       /* To enable FPGA Manager configuration */
+       clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCE_MASK);
+
+       /* To enable FPGA Manager drive over configuration line */
+       setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
+
+       /* Put FPGA into reset phase */
+       setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
+
+       /* (1) wait until FPGA enter reset phase */
+       for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
+               if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_RESETPHASE)
+                       break;
+       }
+
+       /* If not in reset state, return error */
+       if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_RESETPHASE) {
+               puts("FPGA: Could not reset\n");
+               return -1;
+       }
+
+       /* Release FPGA from reset phase */
+       clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
+
+       /* (2) wait until FPGA enter configuration phase */
+       for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
+               if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_CFGPHASE)
+                       break;
+       }
+
+       /* If not in configuration state, return error */
+       if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_CFGPHASE) {
+               puts("FPGA: Could not configure\n");
+               return -2;
+       }
+
+       /* Clear all interrupts in CB Monitor */
+       writel(0xFFF, &fpgamgr_regs->gpio_porta_eoi);
+
+       /* Enable AXI configuration */
+       setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
+
+       return 0;
+}
+
+/* Write the RBF data to FPGA Manager */
+static void fpgamgr_program_write(const void *rbf_data, unsigned long rbf_size)
+{
+       uint32_t src = (uint32_t)rbf_data;
+       uint32_t dst = SOCFPGA_FPGAMGRDATA_ADDRESS;
+
+       /* Number of loops for 32-byte long copying. */
+       uint32_t loops32 = rbf_size / 32;
+       /* Number of loops for 4-byte long copying + trailing bytes */
+       uint32_t loops4 = DIV_ROUND_UP(rbf_size % 32, 4);
+
+       asm volatile(
+               "1:     ldmia   %0!,    {r0-r7}\n"
+               "       stmia   %1!,    {r0-r7}\n"
+               "       sub     %1,     #32\n"
+               "       subs    %2,     #1\n"
+               "       bne     1b\n"
+               "2:     ldr     %2,     [%0],   #4\n"
+               "       str     %2,     [%1]\n"
+               "       subs    %3,     #1\n"
+               "       bne     2b\n"
+               : "+r"(src), "+r"(dst), "+r"(loops32), "+r"(loops4) :
+               : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "cc");
+}
+
+/* Ensure the FPGA entering config done */
+static int fpgamgr_program_poll_cd(void)
+{
+       const uint32_t mask = FPGAMGRREGS_MON_GPIO_EXT_PORTA_NS_MASK |
+                             FPGAMGRREGS_MON_GPIO_EXT_PORTA_CD_MASK;
+       unsigned long reg, i;
+
+       /* (3) wait until full config done */
+       for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
+               reg = readl(&fpgamgr_regs->gpio_ext_porta);
+
+               /* Config error */
+               if (!(reg & mask)) {
+                       printf("FPGA: Configuration error.\n");
+                       return -3;
+               }
+
+               /* Config done without error */
+               if (reg & mask)
+                       break;
+       }
+
+       /* Timeout happened, return error */
+       if (i == FPGA_TIMEOUT_CNT) {
+               printf("FPGA: Timeout waiting for program.\n");
+               return -4;
+       }
+
+       /* Disable AXI configuration */
+       clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
+
+       return 0;
+}
+
+/* Ensure the FPGA entering init phase */
+static int fpgamgr_program_poll_initphase(void)
+{
+       unsigned long i;
+
+       /* Additional clocks for the CB to enter initialization phase */
+       if (fpgamgr_dclkcnt_set(0x4))
+               return -5;
+
+       /* (4) wait until FPGA enter init phase or user mode */
+       for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
+               if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_INITPHASE)
+                       break;
+               if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
+                       break;
+       }
+
+       /* If not in configuration state, return error */
+       if (i == FPGA_TIMEOUT_CNT)
+               return -6;
+
+       return 0;
+}
+
+/* Ensure the FPGA entering user mode */
+static int fpgamgr_program_poll_usermode(void)
+{
+       unsigned long i;
+
+       /* Additional clocks for the CB to exit initialization phase */
+       if (fpgamgr_dclkcnt_set(0x5000))
+               return -7;
+
+       /* (5) wait until FPGA enter user mode */
+       for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
+               if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
+                       break;
+       }
+       /* If not in configuration state, return error */
+       if (i == FPGA_TIMEOUT_CNT)
+               return -8;
+
+       /* To release FPGA Manager drive over configuration line */
+       clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
+
+       return 0;
+}
+
+/*
+ * FPGA Manager to program the FPGA. This is the interface used by FPGA driver.
+ * Return 0 for sucess, non-zero for error.
+ */
+int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
+{
+       unsigned long status;
+
+       if ((uint32_t)rbf_data & 0x3) {
+               puts("FPGA: Unaligned data, realign to 32bit boundary.\n");
+               return -EINVAL;
+       }
+
+       /* Prior programming the FPGA, all bridges need to be shut off */
+
+       /* Disable all signals from hps peripheral controller to fpga */
+       writel(0, &sysmgr_regs->fpgaintfgrp_module);
+
+       /* Disable all signals from FPGA to HPS SDRAM */
+#define SDR_CTRLGRP_FPGAPORTRST_ADDRESS        0x5080
+       writel(0, SOCFPGA_SDR_ADDRESS + SDR_CTRLGRP_FPGAPORTRST_ADDRESS);
+
+       /* Disable all axi bridge (hps2fpga, lwhps2fpga & fpga2hps) */
+       socfpga_bridges_reset(1);
+
+       /* Unmap the bridges from NIC-301 */
+       writel(0x1, SOCFPGA_L3REGS_ADDRESS);
+
+       /* Initialize the FPGA Manager */
+       status = fpgamgr_program_init();
+       if (status)
+               return status;
+
+       /* Write the RBF data to FPGA Manager */
+       fpgamgr_program_write(rbf_data, rbf_size);
+
+       /* Ensure the FPGA entering config done */
+       status = fpgamgr_program_poll_cd();
+       if (status)
+               return status;
+
+       /* Ensure the FPGA entering init phase */
+       status = fpgamgr_program_poll_initphase();
+       if (status)
+               return status;
+
+       /* Ensure the FPGA entering user mode */
+       return fpgamgr_program_poll_usermode();
+}
index e266a6449c52103d79d657923594a307f1bb068c..c2991ad800073b0de56c844456eade3d8e823023 100644 (file)
@@ -40,6 +40,8 @@ enum altera_family {
        Altera_CYC2,
        /* StratixII Family */
        Altera_StratixII,
+       /* SoCFPGA Family */
+       Altera_SoCFPGA,
 
        /* Add new models here */
 
@@ -91,4 +93,8 @@ typedef struct {
        Altera_post_fn post;
 } altera_board_specific_func;
 
+#ifdef CONFIG_FPGA_SOCFPGA
+int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size);
+#endif
+
 #endif /* _ALTERA_H_ */