blob: aef796c741caf72a7794d190aa340201d301e5f3 [file] [log] [blame]
Pavel Machekc7213802014-09-08 14:08:45 +02001/*
Tien Fong Cheec604b0a2017-09-25 16:40:01 +08002 * Copyright (C) 2012-2017 Altera Corporation <www.altera.com>
Pavel Machekc7213802014-09-08 14:08:45 +02003 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#include <common.h>
9#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090010#include <linux/errno.h>
Pavel Machekc7213802014-09-08 14:08:45 +020011#include <asm/arch/fpga_manager.h>
12#include <asm/arch/reset_manager.h>
13#include <asm/arch/system_manager.h>
14
Pavel Machekc7213802014-09-08 14:08:45 +020015/* Timeout count */
16#define FPGA_TIMEOUT_CNT 0x1000000
17
18static struct socfpga_fpga_manager *fpgamgr_regs =
19 (struct socfpga_fpga_manager *)SOCFPGA_FPGAMGRREGS_ADDRESS;
Pavel Machekc7213802014-09-08 14:08:45 +020020
Tien Fong Chee31e50f42017-07-26 13:05:38 +080021int fpgamgr_dclkcnt_set(unsigned long cnt)
Pavel Machekc7213802014-09-08 14:08:45 +020022{
23 unsigned long i;
24
25 /* Clear any existing done status */
26 if (readl(&fpgamgr_regs->dclkstat))
27 writel(0x1, &fpgamgr_regs->dclkstat);
28
29 /* Write the dclkcnt */
30 writel(cnt, &fpgamgr_regs->dclkcnt);
31
32 /* Wait till the dclkcnt done */
33 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
34 if (!readl(&fpgamgr_regs->dclkstat))
35 continue;
36
37 writel(0x1, &fpgamgr_regs->dclkstat);
38 return 0;
39 }
40
41 return -ETIMEDOUT;
42}
43
Pavel Machekc7213802014-09-08 14:08:45 +020044/* Write the RBF data to FPGA Manager */
Tien Fong Chee31e50f42017-07-26 13:05:38 +080045void fpgamgr_program_write(const void *rbf_data, size_t rbf_size)
Pavel Machekc7213802014-09-08 14:08:45 +020046{
47 uint32_t src = (uint32_t)rbf_data;
48 uint32_t dst = SOCFPGA_FPGAMGRDATA_ADDRESS;
49
50 /* Number of loops for 32-byte long copying. */
51 uint32_t loops32 = rbf_size / 32;
52 /* Number of loops for 4-byte long copying + trailing bytes */
53 uint32_t loops4 = DIV_ROUND_UP(rbf_size % 32, 4);
54
55 asm volatile(
Tien Fong Cheec604b0a2017-09-25 16:40:01 +080056 " cmp %2, #0\n"
57 " beq 2f\n"
Pavel Machekc7213802014-09-08 14:08:45 +020058 "1: ldmia %0!, {r0-r7}\n"
59 " stmia %1!, {r0-r7}\n"
60 " sub %1, #32\n"
61 " subs %2, #1\n"
62 " bne 1b\n"
Tien Fong Cheec604b0a2017-09-25 16:40:01 +080063 "2: cmp %3, #0\n"
64 " beq 4f\n"
65 "3: ldr %2, [%0], #4\n"
Pavel Machekc7213802014-09-08 14:08:45 +020066 " str %2, [%1]\n"
67 " subs %3, #1\n"
Tien Fong Cheec604b0a2017-09-25 16:40:01 +080068 " bne 3b\n"
69 "4: nop\n"
Pavel Machekc7213802014-09-08 14:08:45 +020070 : "+r"(src), "+r"(dst), "+r"(loops32), "+r"(loops4) :
71 : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "cc");
72}
73