blob: ec0c630323c4b860c3fed28638f2fba706a48e5d [file] [log] [blame]
Chin Liang See63550242014-06-10 01:17:42 -05001/*
2 * Copyright (C) 2013 Altera Corporation <www.altera.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <asm/io.h>
9#include <asm/arch/freeze_controller.h>
10#include <asm/arch/scan_manager.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
14static const struct socfpga_scan_manager *scan_manager_base =
15 (void *)(SOCFPGA_SCANMGR_ADDRESS);
16static const struct socfpga_freeze_controller *freeze_controller_base =
17 (void *)(SOCFPGA_SYSMGR_ADDRESS + SYSMGR_FRZCTRL_ADDRESS);
18
19/*
20 * Function to check IO scan chain engine status and wait if the engine is
21 * is active. Poll the IO scan chain engine till maximum iteration reached.
22 */
23static inline uint32_t scan_chain_engine_is_idle(uint32_t max_iter)
24{
25 uint32_t scanmgr_status;
26
27 scanmgr_status = readl(&scan_manager_base->stat);
28
29 /* Poll the engine until the scan engine is inactive */
30 while (SCANMGR_STAT_ACTIVE_GET(scanmgr_status) ||
31 (SCANMGR_STAT_WFIFOCNT_GET(scanmgr_status) > 0)) {
32 max_iter--;
33 if (max_iter > 0)
34 scanmgr_status = readl(&scan_manager_base->stat);
35 else
36 return 0;
37 }
38 return 1;
39}
40
Marek Vasutb4c1a002015-07-25 09:33:28 +020041/**
42 * scan_mgr_io_scan_chain_prg() - Program HPS IO Scan Chain
43 * @io_scan_chain_id: IO scan chain ID
Marek Vasutb4c1a002015-07-25 09:33:28 +020044 */
Marek Vasut92af8682015-07-25 09:36:54 +020045static int scan_mgr_io_scan_chain_prg(const unsigned int io_scan_chain_id)
Chin Liang See63550242014-06-10 01:17:42 -050046{
47 uint16_t tdi_tdo_header;
48 uint32_t io_program_iter;
49 uint32_t io_scan_chain_data_residual;
50 uint32_t residual;
Marek Vasut9c3e0062015-07-25 09:53:23 +020051 uint32_t i, ret;
Chin Liang See63550242014-06-10 01:17:42 -050052 uint32_t index = 0;
Marek Vasut9c3e0062015-07-25 09:53:23 +020053 uint32_t io_scan_chain_len_in_bits;
54 const unsigned long *iocsr_scan_chain;
Marek Vasut92af8682015-07-25 09:36:54 +020055
Marek Vasut9c3e0062015-07-25 09:53:23 +020056 ret = iocsr_get_config_table(io_scan_chain_id, &iocsr_scan_chain,
57 &io_scan_chain_len_in_bits);
58 if (ret)
59 return 1;
Chin Liang See63550242014-06-10 01:17:42 -050060
61 /*
62 * De-assert reinit if the IO scan chain is intended for HIO. In
63 * this, its the chain 3.
64 */
65 if (io_scan_chain_id == 3)
66 clrbits_le32(&freeze_controller_base->hioctrl,
67 SYSMGR_FRZCTRL_HIOCTRL_DLLRST_MASK);
68
69 /*
70 * Check if the scan chain engine is inactive and the
71 * WFIFO is empty before enabling the IO scan chain
72 */
73 if (!scan_chain_engine_is_idle(SCAN_MAX_DELAY))
74 return 1;
75
76 /*
77 * Enable IO Scan chain based on scan chain id
78 * Note: only one chain can be enabled at a time
79 */
80 setbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
81
82 /*
83 * Calculate number of iteration needed for full 128-bit (4 x32-bits)
84 * bits shifting. Each TDI_TDO packet can shift in maximum 128-bits
85 */
86 io_program_iter = io_scan_chain_len_in_bits >>
87 IO_SCAN_CHAIN_128BIT_SHIFT;
88 io_scan_chain_data_residual = io_scan_chain_len_in_bits &
89 IO_SCAN_CHAIN_128BIT_MASK;
90
91 /* Construct TDI_TDO packet for 128-bit IO scan chain (2 bytes) */
92 tdi_tdo_header = TDI_TDO_HEADER_FIRST_BYTE |
93 (TDI_TDO_MAX_PAYLOAD << TDI_TDO_HEADER_SECOND_BYTE_SHIFT);
94
95 /* Program IO scan chain in 128-bit iteration */
96 for (i = 0; i < io_program_iter; i++) {
97 /* write TDI_TDO packet header to scan manager */
98 writel(tdi_tdo_header, &scan_manager_base->fifo_double_byte);
99
100 /* calculate array index. Multiply by 4 as write 4 x 32bits */
101 index = i * 4;
102
103 /* write 4 successive 32-bit IO scan chain data into WFIFO */
104 writel(iocsr_scan_chain[index],
105 &scan_manager_base->fifo_quad_byte);
106 writel(iocsr_scan_chain[index + 1],
107 &scan_manager_base->fifo_quad_byte);
108 writel(iocsr_scan_chain[index + 2],
109 &scan_manager_base->fifo_quad_byte);
110 writel(iocsr_scan_chain[index + 3],
111 &scan_manager_base->fifo_quad_byte);
112
113 /*
114 * Check if the scan chain engine has completed the
115 * IO scan chain data shifting
116 */
117 if (!scan_chain_engine_is_idle(SCAN_MAX_DELAY))
118 goto error;
119 }
120
121 /* Calculate array index for final TDI_TDO packet */
122 index = io_program_iter * 4;
123
124 /* Final TDI_TDO packet if any */
125 if (io_scan_chain_data_residual) {
126 /*
127 * Calculate number of quad bytes FIFO write
128 * needed for the final TDI_TDO packet
129 */
130 io_program_iter = io_scan_chain_data_residual >>
131 IO_SCAN_CHAIN_32BIT_SHIFT;
132
133 /*
134 * Construct TDI_TDO packet for remaining IO
135 * scan chain (2 bytes)
136 */
137 tdi_tdo_header = TDI_TDO_HEADER_FIRST_BYTE |
138 ((io_scan_chain_data_residual - 1) <<
139 TDI_TDO_HEADER_SECOND_BYTE_SHIFT);
140
141 /*
142 * Program the last part of IO scan chain write TDI_TDO packet
143 * header (2 bytes) to scan manager
144 */
145 writel(tdi_tdo_header, &scan_manager_base->fifo_double_byte);
146
147 for (i = 0; i < io_program_iter; i++) {
148 /*
149 * write remaining scan chain data into scan
150 * manager WFIFO with 4 bytes write
151 */
152 writel(iocsr_scan_chain[index + i],
153 &scan_manager_base->fifo_quad_byte);
154 }
155
156 index += io_program_iter;
157 residual = io_scan_chain_data_residual &
158 IO_SCAN_CHAIN_32BIT_MASK;
159
160 if (IO_SCAN_CHAIN_PAYLOAD_24BIT < residual) {
161 /*
162 * write the last 4B scan chain data
163 * into scan manager WFIFO
164 */
165 writel(iocsr_scan_chain[index],
166 &scan_manager_base->fifo_quad_byte);
167 } else {
168 /*
169 * write the remaining 1 - 3 bytes scan chain
170 * data into scan manager WFIFO byte by byte
171 * to prevent JTAG engine shifting unused data
172 * from the FIFO and mistaken the data as a
173 * valid command (even though unused bits are
174 * set to 0, but just to prevent hardware
175 * glitch)
176 */
177 for (i = 0; i < residual; i += 8) {
178 writel(((iocsr_scan_chain[index] >> i)
179 & IO_SCAN_CHAIN_BYTE_MASK),
180 &scan_manager_base->fifo_single_byte);
181 }
182 }
183
184 /*
185 * Check if the scan chain engine has completed the
186 * IO scan chain data shifting
187 */
188 if (!scan_chain_engine_is_idle(SCAN_MAX_DELAY))
189 goto error;
190 }
191
192 /* Disable IO Scan chain when configuration done*/
193 clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
194 return 0;
195
196error:
197 /* Disable IO Scan chain when error detected */
198 clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
199 return 1;
200}
201
202int scan_mgr_configure_iocsr(void)
203{
204 int status = 0;
205
206 /* configure the IOCSR through scan chain */
Marek Vasut92af8682015-07-25 09:36:54 +0200207 status |= scan_mgr_io_scan_chain_prg(0);
208 status |= scan_mgr_io_scan_chain_prg(1);
209 status |= scan_mgr_io_scan_chain_prg(2);
210 status |= scan_mgr_io_scan_chain_prg(3);
Chin Liang See63550242014-06-10 01:17:42 -0500211 return status;
212}