]> git.sur5r.net Git - u-boot/blob - arch/arm/mach-socfpga/scan_manager.c
arm: socfpga: scan: Introduce generic JTAG accessor
[u-boot] / arch / arm / mach-socfpga / scan_manager.c
1 /*
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 <errno.h>
9 #include <asm/io.h>
10 #include <asm/arch/freeze_controller.h>
11 #include <asm/arch/scan_manager.h>
12
13 /*
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
21 #define SCANMGR_STAT_ACTIVE             (1 << 31)
22 #define SCANMGR_STAT_WFIFOCNT_MASK      0x70000000
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 static const struct socfpga_scan_manager *scan_manager_base =
27                 (void *)(SOCFPGA_SCANMGR_ADDRESS);
28 static const struct socfpga_freeze_controller *freeze_controller_base =
29                 (void *)(SOCFPGA_SYSMGR_ADDRESS + SYSMGR_FRZCTRL_ADDRESS);
30
31 /**
32  * scan_chain_engine_is_idle() - Check if the JTAG scan chain is idle
33  * @max_iter:   Maximum number of iterations to wait for idle
34  *
35  * Function to check IO scan chain engine status and wait if the engine is
36  * is active. Poll the IO scan chain engine till maximum iteration reached.
37  */
38 static u32 scan_chain_engine_is_idle(u32 max_iter)
39 {
40         const u32 mask = SCANMGR_STAT_ACTIVE | SCANMGR_STAT_WFIFOCNT_MASK;
41         u32 status;
42
43         /* Poll the engine until the scan engine is inactive. */
44         do {
45                 status = readl(&scan_manager_base->stat);
46                 if (!(status & mask))
47                         return 0;
48         } while (max_iter--);
49
50         return -ETIMEDOUT;
51 }
52
53 #define JTAG_BP_INSN            (1 << 0)
54 #define JTAG_BP_TMS             (1 << 1)
55 #define JTAG_BP_PAYLOAD         (1 << 2)
56 #define JTAG_BP_2BYTE           (1 << 3)
57 #define JTAG_BP_4BYTE           (1 << 4)
58
59 /**
60  * scan_mgr_jtag_io() - Access the JTAG chain
61  * @flags:      Control flags, used to configure the action on the JTAG
62  * @iarg:       Instruction argument
63  * @parg:       Payload argument or data
64  *
65  * Perform I/O on the JTAG chain
66  */
67 static void scan_mgr_jtag_io(const u32 flags, const u8 iarg, const u32 parg)
68 {
69         u32 data = parg;
70
71         if (flags & JTAG_BP_INSN) {     /* JTAG instruction */
72                 /*
73                  * The SCC JTAG register is LSB first, so make
74                  * space for the instruction at the LSB.
75                  */
76                 data <<= 8;
77                 if (flags & JTAG_BP_TMS) {
78                         data |= (0 << 7);       /* TMS instruction. */
79                         data |= iarg & 0x3f;    /* TMS arg is 6 bits. */
80                         if (flags & JTAG_BP_PAYLOAD)
81                                 data |= (1 << 6);
82                 } else {
83                         data |= (1 << 7);       /* TDI/TDO instruction. */
84                         data |= iarg & 0xf;     /* TDI/TDO arg is 4 bits. */
85                         if (flags & JTAG_BP_PAYLOAD)
86                                 data |= (1 << 4);
87                 }
88         }
89
90         if (flags & JTAG_BP_4BYTE)
91                 writel(data, &scan_manager_base->fifo_quad_byte);
92         else if (flags & JTAG_BP_2BYTE)
93                 writel(data & 0xffff, &scan_manager_base->fifo_double_byte);
94         else
95                 writel(data & 0xff, &scan_manager_base->fifo_single_byte);
96 }
97
98 /**
99  * scan_mgr_io_scan_chain_prg() - Program HPS IO Scan Chain
100  * @io_scan_chain_id:           IO scan chain ID
101  */
102 static int scan_mgr_io_scan_chain_prg(const unsigned int io_scan_chain_id)
103 {
104         uint32_t io_program_iter;
105         uint32_t io_scan_chain_data_residual;
106         uint32_t residual;
107         uint32_t i, ret;
108         uint32_t index = 0;
109         uint32_t io_scan_chain_len_in_bits;
110         const unsigned long *iocsr_scan_chain;
111
112         ret = iocsr_get_config_table(io_scan_chain_id, &iocsr_scan_chain,
113                                      &io_scan_chain_len_in_bits);
114         if (ret)
115                 return 1;
116
117         /*
118          * De-assert reinit if the IO scan chain is intended for HIO. In
119          * this, its the chain 3.
120          */
121         if (io_scan_chain_id == 3)
122                 clrbits_le32(&freeze_controller_base->hioctrl,
123                              SYSMGR_FRZCTRL_HIOCTRL_DLLRST_MASK);
124
125         /*
126          * Check if the scan chain engine is inactive and the
127          * WFIFO is empty before enabling the IO scan chain
128          */
129         ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
130         if (ret)
131                 return ret;
132
133         /*
134          * Enable IO Scan chain based on scan chain id
135          * Note: only one chain can be enabled at a time
136          */
137         setbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
138
139         /*
140          * Calculate number of iteration needed for full 128-bit (4 x32-bits)
141          * bits shifting. Each TDI_TDO packet can shift in maximum 128-bits
142          */
143         io_program_iter = io_scan_chain_len_in_bits >>
144                 IO_SCAN_CHAIN_128BIT_SHIFT;
145         io_scan_chain_data_residual = io_scan_chain_len_in_bits &
146                 IO_SCAN_CHAIN_128BIT_MASK;
147
148         /* Program IO scan chain in 128-bit iteration */
149         for (i = 0; i < io_program_iter; i++) {
150                 /* Write TDI_TDO packet header for 128-bit IO scan chain */
151                 scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, 0x0,
152                                  TDI_TDO_MAX_PAYLOAD);
153
154                 /* write 4 successive 32-bit IO scan chain data into WFIFO */
155                 scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
156                 scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
157                 scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
158                 scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]);
159
160                 /*
161                  * Check if the scan chain engine has completed the
162                  * IO scan chain data shifting
163                  */
164                 ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
165                 if (ret)
166                         goto error;
167         }
168
169         /* Final TDI_TDO packet if any */
170         if (io_scan_chain_data_residual) {
171                 /*
172                  * Calculate number of quad bytes FIFO write
173                  * needed for the final TDI_TDO packet
174                  */
175                 io_program_iter = io_scan_chain_data_residual >>
176                         IO_SCAN_CHAIN_32BIT_SHIFT;
177
178                 /*
179                  * Program the last part of IO scan chain write TDI_TDO
180                  * packet header (2 bytes) to scan manager.
181                  */
182                 scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, 0x0,
183                                  io_scan_chain_data_residual - 1);
184
185                 for (i = 0; i < io_program_iter; i++) {
186                         /*
187                          * write remaining scan chain data into scan
188                          * manager WFIFO with 4 bytes write
189                          */
190                         scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0,
191                                          iocsr_scan_chain[index++]);
192                 }
193
194                 residual = io_scan_chain_data_residual &
195                         IO_SCAN_CHAIN_32BIT_MASK;
196
197                 if (IO_SCAN_CHAIN_PAYLOAD_24BIT < residual) {
198                         /*
199                          * write the last 4B scan chain data
200                          * into scan manager WFIFO
201                          */
202                         scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0,
203                                          iocsr_scan_chain[index]);
204                 } else {
205                         /*
206                          * write the remaining 1 - 3 bytes scan chain
207                          * data into scan manager WFIFO byte by byte
208                          * to prevent JTAG engine shifting unused data
209                          * from the FIFO and mistaken the data as a
210                          * valid command (even though unused bits are
211                          * set to 0, but just to prevent hardware
212                          * glitch)
213                          */
214                         for (i = 0; i < residual; i += 8) {
215                                 scan_mgr_jtag_io(0, 0x0,
216                                          iocsr_scan_chain[index] >> i);
217                         }
218                 }
219
220                 /*
221                  * Check if the scan chain engine has completed the
222                  * IO scan chain data shifting
223                  */
224                 ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY);
225                 if (ret)
226                         goto error;
227         }
228
229         /* Disable IO Scan chain when configuration done*/
230         clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
231         return 0;
232
233 error:
234         /* Disable IO Scan chain when error detected */
235         clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id);
236         return ret;
237 }
238
239 int scan_mgr_configure_iocsr(void)
240 {
241         int status = 0;
242
243         /* configure the IOCSR through scan chain */
244         status |= scan_mgr_io_scan_chain_prg(0);
245         status |= scan_mgr_io_scan_chain_prg(1);
246         status |= scan_mgr_io_scan_chain_prg(2);
247         status |= scan_mgr_io_scan_chain_prg(3);
248         return status;
249 }