1
2
3
4
5
6
7#include <common.h>
8#include <asm/io.h>
9#include <linux/errno.h>
10#include <asm/arch/fpga_manager.h>
11#include <asm/arch/reset_manager.h>
12#include <asm/arch/system_manager.h>
13
14#define FPGA_TIMEOUT_CNT 0x1000000
15
16static struct socfpga_fpga_manager *fpgamgr_regs =
17 (struct socfpga_fpga_manager *)SOCFPGA_FPGAMGRREGS_ADDRESS;
18
19
20static void fpgamgr_set_cd_ratio(unsigned long ratio)
21{
22 clrsetbits_le32(&fpgamgr_regs->ctrl,
23 0x3 << FPGAMGRREGS_CTRL_CDRATIO_LSB,
24 (ratio & 0x3) << FPGAMGRREGS_CTRL_CDRATIO_LSB);
25}
26
27
28static int fpgamgr_program_init(void)
29{
30 unsigned long msel, i;
31
32
33 msel = readl(&fpgamgr_regs->stat);
34 msel &= FPGAMGRREGS_STAT_MSEL_MASK;
35 msel >>= FPGAMGRREGS_STAT_MSEL_LSB;
36
37
38
39
40
41 if (msel & 0x8) {
42 setbits_le32(&fpgamgr_regs->ctrl,
43 FPGAMGRREGS_CTRL_CFGWDTH_MASK);
44
45
46
47 if ((msel & 0x3) == 0x0)
48 fpgamgr_set_cd_ratio(CDRATIO_x1);
49
50 else if ((msel & 0x3) == 0x1)
51 fpgamgr_set_cd_ratio(CDRATIO_x4);
52
53 else if ((msel & 0x3) == 0x2)
54 fpgamgr_set_cd_ratio(CDRATIO_x8);
55
56 } else {
57 clrbits_le32(&fpgamgr_regs->ctrl,
58 FPGAMGRREGS_CTRL_CFGWDTH_MASK);
59
60
61
62 if ((msel & 0x3) == 0x0)
63 fpgamgr_set_cd_ratio(CDRATIO_x1);
64
65 else if ((msel & 0x3) == 0x1)
66 fpgamgr_set_cd_ratio(CDRATIO_x2);
67
68 else if ((msel & 0x3) == 0x2)
69 fpgamgr_set_cd_ratio(CDRATIO_x4);
70 }
71
72
73 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCE_MASK);
74
75
76 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
77
78
79 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
80
81
82 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
83 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_RESETPHASE)
84 break;
85 }
86
87
88 if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_RESETPHASE) {
89 puts("FPGA: Could not reset\n");
90 return -1;
91 }
92
93
94 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_NCONFIGPULL_MASK);
95
96
97 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
98 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_CFGPHASE)
99 break;
100 }
101
102
103 if (fpgamgr_get_mode() != FPGAMGRREGS_MODE_CFGPHASE) {
104 puts("FPGA: Could not configure\n");
105 return -2;
106 }
107
108
109 writel(0xFFF, &fpgamgr_regs->gpio_porta_eoi);
110
111
112 setbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
113
114 return 0;
115}
116
117
118static int fpgamgr_program_poll_cd(void)
119{
120 const uint32_t mask = FPGAMGRREGS_MON_GPIO_EXT_PORTA_NS_MASK |
121 FPGAMGRREGS_MON_GPIO_EXT_PORTA_CD_MASK;
122 unsigned long reg, i;
123
124
125 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
126 reg = readl(&fpgamgr_regs->gpio_ext_porta);
127
128
129 if (!(reg & mask)) {
130 printf("FPGA: Configuration error.\n");
131 return -3;
132 }
133
134
135 if (reg & mask)
136 break;
137 }
138
139
140 if (i == FPGA_TIMEOUT_CNT) {
141 printf("FPGA: Timeout waiting for program.\n");
142 return -4;
143 }
144
145
146 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_AXICFGEN_MASK);
147
148 return 0;
149}
150
151
152static int fpgamgr_program_poll_initphase(void)
153{
154 unsigned long i;
155
156
157 if (fpgamgr_dclkcnt_set(0x4))
158 return -5;
159
160
161 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
162 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_INITPHASE)
163 break;
164 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
165 break;
166 }
167
168
169 if (i == FPGA_TIMEOUT_CNT)
170 return -6;
171
172 return 0;
173}
174
175
176static int fpgamgr_program_poll_usermode(void)
177{
178 unsigned long i;
179
180
181 if (fpgamgr_dclkcnt_set(0x5000))
182 return -7;
183
184
185 for (i = 0; i < FPGA_TIMEOUT_CNT; i++) {
186 if (fpgamgr_get_mode() == FPGAMGRREGS_MODE_USERMODE)
187 break;
188 }
189
190 if (i == FPGA_TIMEOUT_CNT)
191 return -8;
192
193
194 clrbits_le32(&fpgamgr_regs->ctrl, FPGAMGRREGS_CTRL_EN_MASK);
195
196 return 0;
197}
198
199
200
201
202
203int socfpga_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size)
204{
205 int status;
206
207 if ((uint32_t)rbf_data & 0x3) {
208 puts("FPGA: Unaligned data, realign to 32bit boundary.\n");
209 return -EINVAL;
210 }
211
212
213
214
215 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_GEN5_FPGAINFGRP_MODULE);
216
217
218#define SDR_CTRLGRP_FPGAPORTRST_ADDRESS 0x5080
219 writel(0, SOCFPGA_SDR_ADDRESS + SDR_CTRLGRP_FPGAPORTRST_ADDRESS);
220
221
222 socfpga_bridges_reset(1);
223
224
225 writel(0x1, SOCFPGA_L3REGS_ADDRESS);
226
227
228 status = fpgamgr_program_init();
229 if (status)
230 return status;
231
232
233 fpgamgr_program_write(rbf_data, rbf_size);
234
235
236 status = fpgamgr_program_poll_cd();
237 if (status)
238 return status;
239
240
241 status = fpgamgr_program_poll_initphase();
242 if (status)
243 return status;
244
245
246 return fpgamgr_program_poll_usermode();
247}
248