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