blob: f7ee28915ecd0e97e1351d158b98002c70f42701 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Chin Liang See63550242014-06-10 01:17:42 -05002/*
3 * Copyright (C) 2013 Altera Corporation <www.altera.com>
Chin Liang See63550242014-06-10 01:17:42 -05004 */
5
6#include <common.h>
Marek Vasut24d12ec2015-08-01 02:48:03 +02007#include <errno.h>
Chin Liang See63550242014-06-10 01:17:42 -05008#include <asm/io.h>
9#include <asm/arch/freeze_controller.h>
10#include <asm/arch/scan_manager.h>
Dinh Nguyenb640cae2015-07-31 11:06:50 -050011#include <asm/arch/system_manager.h>
Chin Liang See63550242014-06-10 01:17:42 -050012
Marek Vasut24d12ec2015-08-01 02:48:03 +020013/*
14 * Maximum polling loop to wait for IO scan chain engine becomes idle
15 * to prevent infinite loop. It is important that this is NOT changed
16 * to delay using timer functions, since at the time this function is
17 * called, timer might not yet be inited.
18 */
19#define SCANMGR_MAX_DELAY 100
20
Marek Vasut65371c82015-08-01 03:18:50 +020021/*
22 * Maximum length of TDI_TDO packet payload is 128 bits,
23 * represented by (length - 1) in TDI_TDO header.
24 */
25#define TDI_TDO_MAX_PAYLOAD 127
26
Marek Vasut24d12ec2015-08-01 02:48:03 +020027#define SCANMGR_STAT_ACTIVE (1 << 31)
28#define SCANMGR_STAT_WFIFOCNT_MASK 0x70000000
29
Chin Liang See63550242014-06-10 01:17:42 -050030static 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);
34
Marek Vasut24d12ec2015-08-01 02:48:03 +020035/**
36 * scan_chain_engine_is_idle() - Check if the JTAG scan chain is idle
37 * @max_iter: Maximum number of iterations to wait for idle
38 *
Chin Liang See63550242014-06-10 01:17:42 -050039 * Function to check IO scan chain engine status and wait if the engine is
40 * is active. Poll the IO scan chain engine till maximum iteration reached.
41 */
Marek Vasut24d12ec2015-08-01 02:48:03 +020042static u32 scan_chain_engine_is_idle(u32 max_iter)
Chin Liang See63550242014-06-10 01:17:42 -050043{
Marek Vasut24d12ec2015-08-01 02:48:03 +020044 const u32 mask = SCANMGR_STAT_ACTIVE | SCANMGR_STAT_WFIFOCNT_MASK;
45 u32 status;
Chin Liang See63550242014-06-10 01:17:42 -050046
Marek Vasut24d12ec2015-08-01 02:48:03 +020047 /* Poll the engine until the scan engine is inactive. */
48 do {
49 status = readl(&scan_manager_base->stat);
50 if (!(status & mask))
Chin Liang See63550242014-06-10 01:17:42 -050051 return 0;
Marek Vasut24d12ec2015-08-01 02:48:03 +020052 } while (max_iter--);
53
54 return -ETIMEDOUT;
Chin Liang See63550242014-06-10 01:17:42 -050055}
56
Marek Vasute1b96fc2015-08-01 03:01:25 +020057#define JTAG_BP_INSN (1 << 0)
58#define JTAG_BP_TMS (1 << 1)
59#define JTAG_BP_PAYLOAD (1 << 2)
60#define JTAG_BP_2BYTE (1 << 3)
61#define JTAG_BP_4BYTE (1 << 4)
62
63/**
64 * scan_mgr_jtag_io() - Access the JTAG chain
65 * @flags: Control flags, used to configure the action on the JTAG
66 * @iarg: Instruction argument
67 * @parg: Payload argument or data
68 *
69 * Perform I/O on the JTAG chain
70 */
71static void scan_mgr_jtag_io(const u32 flags, const u8 iarg, const u32 parg)
72{
73 u32 data = parg;
74
75 if (flags & JTAG_BP_INSN) { /* JTAG instruction */
76 /*
77 * The SCC JTAG register is LSB first, so make
78 * space for the instruction at the LSB.
79 */
80 data <<= 8;
81 if (flags & JTAG_BP_TMS) {
82 data |= (0 << 7); /* TMS instruction. */
83 data |= iarg & 0x3f; /* TMS arg is 6 bits. */
84 if (flags & JTAG_BP_PAYLOAD)
85 data |= (1 << 6);
86 } else {
87 data |= (1 << 7); /* TDI/TDO instruction. */
88 data |= iarg & 0xf; /* TDI/TDO arg is 4 bits. */
89 if (flags & JTAG_BP_PAYLOAD)
90 data |= (1 << 4);
91 }
92 }
93
94 if (flags & JTAG_BP_4BYTE)
95 writel(data, &scan_manager_base->fifo_quad_byte);
96 else if (flags & JTAG_BP_2BYTE)
97 writel(data & 0xffff, &scan_manager_base->fifo_double_byte);
98 else
99 writel(data & 0xff, &scan_manager_base->fifo_single_byte);
100}
101
Marek Vasutb4c1a002015-07-25 09:33:28 +0200102/**
Marek Vasutefc6ed52015-08-01 14:28:48 +0200103 * scan_mgr_jtag_insn_data() - Send JTAG instruction and data
104 * @iarg: Instruction argument
105 * @data: Associated data
106 * @dlen: Length of data in bits
107 *
108 * This function is used when programming the IO chains to submit the
109 * instruction followed by variable length payload.
110 */
111static int
112scan_mgr_jtag_insn_data(const u8 iarg, const unsigned long *data,
113 const unsigned int dlen)
114{
115 int i, j;
116
117 scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, iarg, dlen - 1);
118
119 /* 32 bits or more remain */
120 for (i = 0; i < dlen / 32; i++)
121 scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, data[i]);
122
123 if ((dlen % 32) > 24) { /* 31...24 bits remain */
124 scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, data[i]);
125 } else if (dlen % 32) { /* 24...1 bit remain */
126 for (j = 0; j < dlen % 32; j += 8)
127 scan_mgr_jtag_io(0, 0x0, data[i] >> j);
128 }
129
130 return scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
131}
132
133/**
Marek Vasutb4c1a002015-07-25 09:33:28 +0200134 * scan_mgr_io_scan_chain_prg() - Program HPS IO Scan Chain
135 * @io_scan_chain_id: IO scan chain ID
Marek Vasutb4c1a002015-07-25 09:33:28 +0200136 */
Marek Vasut92af8682015-07-25 09:36:54 +0200137static int scan_mgr_io_scan_chain_prg(const unsigned int io_scan_chain_id)
Chin Liang See63550242014-06-10 01:17:42 -0500138{
Marek Vasut65371c82015-08-01 03:18:50 +0200139 u32 io_scan_chain_len_in_bits;
Marek Vasut9c3e0062015-07-25 09:53:23 +0200140 const unsigned long *iocsr_scan_chain;
Marek Vasutefc6ed52015-08-01 14:28:48 +0200141 unsigned int rem, idx = 0;
142 int ret;
Marek Vasut92af8682015-07-25 09:36:54 +0200143
Marek Vasut9c3e0062015-07-25 09:53:23 +0200144 ret = iocsr_get_config_table(io_scan_chain_id, &iocsr_scan_chain,
145 &io_scan_chain_len_in_bits);
146 if (ret)
147 return 1;
Chin Liang See63550242014-06-10 01:17:42 -0500148
149 /*
150 * De-assert reinit if the IO scan chain is intended for HIO. In
151 * this, its the chain 3.
152 */
153 if (io_scan_chain_id == 3)
154 clrbits_le32(&freeze_controller_base->hioctrl,
155 SYSMGR_FRZCTRL_HIOCTRL_DLLRST_MASK);
156
157 /*
158 * Check if the scan chain engine is inactive and the
159 * WFIFO is empty before enabling the IO scan chain
160 */
Marek Vasut24d12ec2015-08-01 02:48:03 +0200161 ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
162 if (ret)
163 return ret;
Chin Liang See63550242014-06-10 01:17:42 -0500164
165 /*
166 * Enable IO Scan chain based on scan chain id
167 * Note: only one chain can be enabled at a time
168 */
169 setbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
170
Marek Vasutefc6ed52015-08-01 14:28:48 +0200171 /* Program IO scan chain. */
172 while (io_scan_chain_len_in_bits) {
173 if (io_scan_chain_len_in_bits > 128)
174 rem = 128;
175 else
176 rem = io_scan_chain_len_in_bits;
Chin Liang See63550242014-06-10 01:17:42 -0500177
Marek Vasutefc6ed52015-08-01 14:28:48 +0200178 ret = scan_mgr_jtag_insn_data(0x0, &iocsr_scan_chain[idx], rem);
Marek Vasut24d12ec2015-08-01 02:48:03 +0200179 if (ret)
Chin Liang See63550242014-06-10 01:17:42 -0500180 goto error;
Marek Vasutefc6ed52015-08-01 14:28:48 +0200181 io_scan_chain_len_in_bits -= rem;
182 idx += 4;
Chin Liang See63550242014-06-10 01:17:42 -0500183 }
184
185 /* Disable IO Scan chain when configuration done*/
186 clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
187 return 0;
188
189error:
190 /* Disable IO Scan chain when error detected */
191 clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
Marek Vasut24d12ec2015-08-01 02:48:03 +0200192 return ret;
Chin Liang See63550242014-06-10 01:17:42 -0500193}
194
195int scan_mgr_configure_iocsr(void)
196{
197 int status = 0;
198
199 /* configure the IOCSR through scan chain */
Marek Vasut92af8682015-07-25 09:36:54 +0200200 status |= scan_mgr_io_scan_chain_prg(0);
201 status |= scan_mgr_io_scan_chain_prg(1);
202 status |= scan_mgr_io_scan_chain_prg(2);
203 status |= scan_mgr_io_scan_chain_prg(3);
Chin Liang See63550242014-06-10 01:17:42 -0500204 return status;
205}
Dinh Nguyenb640cae2015-07-31 11:06:50 -0500206
207/**
208 * scan_mgr_get_fpga_id() - Obtain FPGA JTAG ID
209 *
210 * This function obtains JTAG ID from the FPGA TAP controller.
211 */
212u32 scan_mgr_get_fpga_id(void)
213{
214 const unsigned long data = 0;
215 u32 id = 0xffffffff;
216 int ret;
217
218 /* Enable HPS to talk to JTAG in the FPGA through the System Manager */
Ley Foon Tan3d3a8602019-11-08 10:38:20 +0800219 writel(0x1, socfpga_get_sysmgr_addr() + SYSMGR_GEN5_SCANMGRGRP_CTRL);
Dinh Nguyenb640cae2015-07-31 11:06:50 -0500220
221 /* Enable port 7 */
222 writel(0x80, &scan_manager_base->en);
223 /* write to CSW to make s2f_ntrst reset */
224 writel(0x02, &scan_manager_base->stat);
225
226 /* Add a pause */
227 mdelay(1);
228
229 /* write 0x00 to CSW to clear the s2f_ntrst */
230 writel(0, &scan_manager_base->stat);
231
232 /*
233 * Go to Test-Logic-Reset state.
234 * This sets TAP controller into IDCODE mode.
235 */
236 scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_TMS, 0x1f | (1 << 5), 0x0);
237
238 /* Go to Run-Test/Idle -> DR-Scan -> Capture-DR -> Shift-DR state. */
239 scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_TMS, 0x02 | (1 << 4), 0x0);
240
241 /*
242 * Push 4 bytes of data through TDI->DR->TDO.
243 *
244 * Length of TDI data is 32bits (length - 1) and they are only
245 * zeroes as we care only for TDO data.
246 */
247 ret = scan_mgr_jtag_insn_data(0x4, &data, 32);
248 /* Read 32 bit from captured JTAG data. */
249 if (!ret)
250 id = readl(&scan_manager_base->fifo_quad_byte);
251
252 /* Disable all port */
253 writel(0, &scan_manager_base->en);
Ley Foon Tan3d3a8602019-11-08 10:38:20 +0800254 writel(0, socfpga_get_sysmgr_addr() + SYSMGR_GEN5_SCANMGRGRP_CTRL);
Dinh Nguyenb640cae2015-07-31 11:06:50 -0500255
256 return id;
257}