1
2
3
4
5
6
7#include <common.h>
8#include <errno.h>
9#include <asm/io.h>
10#include <asm/arch/freeze_controller.h>
11#include <asm/arch/scan_manager.h>
12#include <asm/arch/system_manager.h>
13
14
15
16
17
18
19
20#define SCANMGR_MAX_DELAY 100
21
22
23
24
25
26#define TDI_TDO_MAX_PAYLOAD 127
27
28#define SCANMGR_STAT_ACTIVE (1 << 31)
29#define SCANMGR_STAT_WFIFOCNT_MASK 0x70000000
30
31DECLARE_GLOBAL_DATA_PTR;
32
33static const struct socfpga_scan_manager *scan_manager_base =
34 (void *)(SOCFPGA_SCANMGR_ADDRESS);
35static const struct socfpga_freeze_controller *freeze_controller_base =
36 (void *)(SOCFPGA_SYSMGR_ADDRESS + SYSMGR_FRZCTRL_ADDRESS);
37static struct socfpga_system_manager *sys_mgr_base =
38 (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
39
40
41
42
43
44
45
46
47static u32 scan_chain_engine_is_idle(u32 max_iter)
48{
49 const u32 mask = SCANMGR_STAT_ACTIVE | SCANMGR_STAT_WFIFOCNT_MASK;
50 u32 status;
51
52
53 do {
54 status = readl(&scan_manager_base->stat);
55 if (!(status & mask))
56 return 0;
57 } while (max_iter--);
58
59 return -ETIMEDOUT;
60}
61
62#define JTAG_BP_INSN (1 << 0)
63#define JTAG_BP_TMS (1 << 1)
64#define JTAG_BP_PAYLOAD (1 << 2)
65#define JTAG_BP_2BYTE (1 << 3)
66#define JTAG_BP_4BYTE (1 << 4)
67
68
69
70
71
72
73
74
75
76static void scan_mgr_jtag_io(const u32 flags, const u8 iarg, const u32 parg)
77{
78 u32 data = parg;
79
80 if (flags & JTAG_BP_INSN) {
81
82
83
84
85 data <<= 8;
86 if (flags & JTAG_BP_TMS) {
87 data |= (0 << 7);
88 data |= iarg & 0x3f;
89 if (flags & JTAG_BP_PAYLOAD)
90 data |= (1 << 6);
91 } else {
92 data |= (1 << 7);
93 data |= iarg & 0xf;
94 if (flags & JTAG_BP_PAYLOAD)
95 data |= (1 << 4);
96 }
97 }
98
99 if (flags & JTAG_BP_4BYTE)
100 writel(data, &scan_manager_base->fifo_quad_byte);
101 else if (flags & JTAG_BP_2BYTE)
102 writel(data & 0xffff, &scan_manager_base->fifo_double_byte);
103 else
104 writel(data & 0xff, &scan_manager_base->fifo_single_byte);
105}
106
107
108
109
110
111
112
113
114
115
116static int
117scan_mgr_jtag_insn_data(const u8 iarg, const unsigned long *data,
118 const unsigned int dlen)
119{
120 int i, j;
121
122 scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, iarg, dlen - 1);
123
124
125 for (i = 0; i < dlen / 32; i++)
126 scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, data[i]);
127
128 if ((dlen % 32) > 24) {
129 scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, data[i]);
130 } else if (dlen % 32) {
131 for (j = 0; j < dlen % 32; j += 8)
132 scan_mgr_jtag_io(0, 0x0, data[i] >> j);
133 }
134
135 return scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
136}
137
138
139
140
141
142static int scan_mgr_io_scan_chain_prg(const unsigned int io_scan_chain_id)
143{
144 u32 io_scan_chain_len_in_bits;
145 const unsigned long *iocsr_scan_chain;
146 unsigned int rem, idx = 0;
147 int ret;
148
149 ret = iocsr_get_config_table(io_scan_chain_id, &iocsr_scan_chain,
150 &io_scan_chain_len_in_bits);
151 if (ret)
152 return 1;
153
154
155
156
157
158 if (io_scan_chain_id == 3)
159 clrbits_le32(&freeze_controller_base->hioctrl,
160 SYSMGR_FRZCTRL_HIOCTRL_DLLRST_MASK);
161
162
163
164
165
166 ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
167 if (ret)
168 return ret;
169
170
171
172
173
174 setbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
175
176
177 while (io_scan_chain_len_in_bits) {
178 if (io_scan_chain_len_in_bits > 128)
179 rem = 128;
180 else
181 rem = io_scan_chain_len_in_bits;
182
183 ret = scan_mgr_jtag_insn_data(0x0, &iocsr_scan_chain[idx], rem);
184 if (ret)
185 goto error;
186 io_scan_chain_len_in_bits -= rem;
187 idx += 4;
188 }
189
190
191 clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
192 return 0;
193
194error:
195
196 clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
197 return ret;
198}
199
200int scan_mgr_configure_iocsr(void)
201{
202 int status = 0;
203
204
205 status |= scan_mgr_io_scan_chain_prg(0);
206 status |= scan_mgr_io_scan_chain_prg(1);
207 status |= scan_mgr_io_scan_chain_prg(2);
208 status |= scan_mgr_io_scan_chain_prg(3);
209 return status;
210}
211
212
213
214
215
216
217u32 scan_mgr_get_fpga_id(void)
218{
219 const unsigned long data = 0;
220 u32 id = 0xffffffff;
221 int ret;
222
223
224 writel(0x1, &sys_mgr_base->scanmgrgrp_ctrl);
225
226
227 writel(0x80, &scan_manager_base->en);
228
229 writel(0x02, &scan_manager_base->stat);
230
231
232 mdelay(1);
233
234
235 writel(0, &scan_manager_base->stat);
236
237
238
239
240
241 scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_TMS, 0x1f | (1 << 5), 0x0);
242
243
244 scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_TMS, 0x02 | (1 << 4), 0x0);
245
246
247
248
249
250
251
252 ret = scan_mgr_jtag_insn_data(0x4, &data, 32);
253
254 if (!ret)
255 id = readl(&scan_manager_base->fifo_quad_byte);
256
257
258 writel(0, &scan_manager_base->en);
259 writel(0, &sys_mgr_base->scanmgrgrp_ctrl);
260
261 return id;
262}
263