Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Altera Corporation <www.altera.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 8 | #include <errno.h> |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 9 | #include <asm/io.h> |
| 10 | #include <asm/arch/freeze_controller.h> |
| 11 | #include <asm/arch/scan_manager.h> |
| 12 | |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 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 | |
Marek Vasut | 65371c8 | 2015-08-01 03:18:50 +0200 | [diff] [blame^] | 21 | /* |
| 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 Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 27 | #define SCANMGR_STAT_ACTIVE (1 << 31) |
| 28 | #define SCANMGR_STAT_WFIFOCNT_MASK 0x70000000 |
| 29 | |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 30 | DECLARE_GLOBAL_DATA_PTR; |
| 31 | |
| 32 | static const struct socfpga_scan_manager *scan_manager_base = |
| 33 | (void *)(SOCFPGA_SCANMGR_ADDRESS); |
| 34 | static const struct socfpga_freeze_controller *freeze_controller_base = |
| 35 | (void *)(SOCFPGA_SYSMGR_ADDRESS + SYSMGR_FRZCTRL_ADDRESS); |
| 36 | |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 37 | /** |
| 38 | * scan_chain_engine_is_idle() - Check if the JTAG scan chain is idle |
| 39 | * @max_iter: Maximum number of iterations to wait for idle |
| 40 | * |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 41 | * Function to check IO scan chain engine status and wait if the engine is |
| 42 | * is active. Poll the IO scan chain engine till maximum iteration reached. |
| 43 | */ |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 44 | static u32 scan_chain_engine_is_idle(u32 max_iter) |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 45 | { |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 46 | const u32 mask = SCANMGR_STAT_ACTIVE | SCANMGR_STAT_WFIFOCNT_MASK; |
| 47 | u32 status; |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 48 | |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 49 | /* Poll the engine until the scan engine is inactive. */ |
| 50 | do { |
| 51 | status = readl(&scan_manager_base->stat); |
| 52 | if (!(status & mask)) |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 53 | return 0; |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 54 | } while (max_iter--); |
| 55 | |
| 56 | return -ETIMEDOUT; |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Marek Vasut | e1b96fc | 2015-08-01 03:01:25 +0200 | [diff] [blame] | 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 | * scan_mgr_jtag_io() - Access the JTAG chain |
| 67 | * @flags: Control flags, used to configure the action on the JTAG |
| 68 | * @iarg: Instruction argument |
| 69 | * @parg: Payload argument or data |
| 70 | * |
| 71 | * Perform I/O on the JTAG chain |
| 72 | */ |
| 73 | static 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) { /* JTAG instruction */ |
| 78 | /* |
| 79 | * The SCC JTAG register is LSB first, so make |
| 80 | * space for the instruction at the LSB. |
| 81 | */ |
| 82 | data <<= 8; |
| 83 | if (flags & JTAG_BP_TMS) { |
| 84 | data |= (0 << 7); /* TMS instruction. */ |
| 85 | data |= iarg & 0x3f; /* TMS arg is 6 bits. */ |
| 86 | if (flags & JTAG_BP_PAYLOAD) |
| 87 | data |= (1 << 6); |
| 88 | } else { |
| 89 | data |= (1 << 7); /* TDI/TDO instruction. */ |
| 90 | data |= iarg & 0xf; /* TDI/TDO arg is 4 bits. */ |
| 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 | |
Marek Vasut | b4c1a00 | 2015-07-25 09:33:28 +0200 | [diff] [blame] | 104 | /** |
| 105 | * scan_mgr_io_scan_chain_prg() - Program HPS IO Scan Chain |
| 106 | * @io_scan_chain_id: IO scan chain ID |
Marek Vasut | b4c1a00 | 2015-07-25 09:33:28 +0200 | [diff] [blame] | 107 | */ |
Marek Vasut | 92af868 | 2015-07-25 09:36:54 +0200 | [diff] [blame] | 108 | static int scan_mgr_io_scan_chain_prg(const unsigned int io_scan_chain_id) |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 109 | { |
Marek Vasut | 65371c8 | 2015-08-01 03:18:50 +0200 | [diff] [blame^] | 110 | u32 residual; |
| 111 | u32 io_scan_chain_len_in_bits; |
Marek Vasut | 9c3e006 | 2015-07-25 09:53:23 +0200 | [diff] [blame] | 112 | const unsigned long *iocsr_scan_chain; |
Marek Vasut | 65371c8 | 2015-08-01 03:18:50 +0200 | [diff] [blame^] | 113 | int i, ret, index = 0; |
Marek Vasut | 92af868 | 2015-07-25 09:36:54 +0200 | [diff] [blame] | 114 | |
Marek Vasut | 9c3e006 | 2015-07-25 09:53:23 +0200 | [diff] [blame] | 115 | ret = iocsr_get_config_table(io_scan_chain_id, &iocsr_scan_chain, |
| 116 | &io_scan_chain_len_in_bits); |
| 117 | if (ret) |
| 118 | return 1; |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * De-assert reinit if the IO scan chain is intended for HIO. In |
| 122 | * this, its the chain 3. |
| 123 | */ |
| 124 | if (io_scan_chain_id == 3) |
| 125 | clrbits_le32(&freeze_controller_base->hioctrl, |
| 126 | SYSMGR_FRZCTRL_HIOCTRL_DLLRST_MASK); |
| 127 | |
| 128 | /* |
| 129 | * Check if the scan chain engine is inactive and the |
| 130 | * WFIFO is empty before enabling the IO scan chain |
| 131 | */ |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 132 | ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY); |
| 133 | if (ret) |
| 134 | return ret; |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Enable IO Scan chain based on scan chain id |
| 138 | * Note: only one chain can be enabled at a time |
| 139 | */ |
| 140 | setbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id); |
| 141 | |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 142 | /* Program IO scan chain in 128-bit iteration */ |
Marek Vasut | 65371c8 | 2015-08-01 03:18:50 +0200 | [diff] [blame^] | 143 | for (i = 0; i < io_scan_chain_len_in_bits / 128; i++) { |
Marek Vasut | e1b96fc | 2015-08-01 03:01:25 +0200 | [diff] [blame] | 144 | /* Write TDI_TDO packet header for 128-bit IO scan chain */ |
| 145 | scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, 0x0, |
| 146 | TDI_TDO_MAX_PAYLOAD); |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 147 | |
| 148 | /* write 4 successive 32-bit IO scan chain data into WFIFO */ |
Marek Vasut | e1b96fc | 2015-08-01 03:01:25 +0200 | [diff] [blame] | 149 | scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]); |
| 150 | scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]); |
| 151 | scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]); |
| 152 | scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, iocsr_scan_chain[index++]); |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 153 | |
| 154 | /* |
| 155 | * Check if the scan chain engine has completed the |
| 156 | * IO scan chain data shifting |
| 157 | */ |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 158 | ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY); |
| 159 | if (ret) |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 160 | goto error; |
| 161 | } |
| 162 | |
Marek Vasut | 65371c8 | 2015-08-01 03:18:50 +0200 | [diff] [blame^] | 163 | residual = io_scan_chain_len_in_bits % 128; |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 164 | |
Marek Vasut | 65371c8 | 2015-08-01 03:18:50 +0200 | [diff] [blame^] | 165 | /* Final TDI_TDO packet (if chain length is not aligned to 128 bits) */ |
| 166 | if (residual) { |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 167 | /* |
Marek Vasut | e1b96fc | 2015-08-01 03:01:25 +0200 | [diff] [blame] | 168 | * Program the last part of IO scan chain write TDI_TDO |
| 169 | * packet header (2 bytes) to scan manager. |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 170 | */ |
Marek Vasut | e1b96fc | 2015-08-01 03:01:25 +0200 | [diff] [blame] | 171 | scan_mgr_jtag_io(JTAG_BP_INSN | JTAG_BP_2BYTE, 0x0, |
Marek Vasut | 65371c8 | 2015-08-01 03:18:50 +0200 | [diff] [blame^] | 172 | residual - 1); |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 173 | |
Marek Vasut | 65371c8 | 2015-08-01 03:18:50 +0200 | [diff] [blame^] | 174 | for (i = 0; i < residual / 32; i++) { |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 175 | /* |
| 176 | * write remaining scan chain data into scan |
| 177 | * manager WFIFO with 4 bytes write |
Marek Vasut | e1b96fc | 2015-08-01 03:01:25 +0200 | [diff] [blame] | 178 | */ |
| 179 | scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, |
| 180 | iocsr_scan_chain[index++]); |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Marek Vasut | 65371c8 | 2015-08-01 03:18:50 +0200 | [diff] [blame^] | 183 | residual = io_scan_chain_len_in_bits % 32; |
| 184 | if (residual > 24) { |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 185 | /* |
| 186 | * write the last 4B scan chain data |
| 187 | * into scan manager WFIFO |
| 188 | */ |
Marek Vasut | e1b96fc | 2015-08-01 03:01:25 +0200 | [diff] [blame] | 189 | scan_mgr_jtag_io(JTAG_BP_4BYTE, 0x0, |
| 190 | iocsr_scan_chain[index]); |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 191 | } else { |
| 192 | /* |
| 193 | * write the remaining 1 - 3 bytes scan chain |
| 194 | * data into scan manager WFIFO byte by byte |
| 195 | * to prevent JTAG engine shifting unused data |
| 196 | * from the FIFO and mistaken the data as a |
| 197 | * valid command (even though unused bits are |
| 198 | * set to 0, but just to prevent hardware |
| 199 | * glitch) |
| 200 | */ |
| 201 | for (i = 0; i < residual; i += 8) { |
Marek Vasut | e1b96fc | 2015-08-01 03:01:25 +0200 | [diff] [blame] | 202 | scan_mgr_jtag_io(0, 0x0, |
| 203 | iocsr_scan_chain[index] >> i); |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * Check if the scan chain engine has completed the |
| 209 | * IO scan chain data shifting |
| 210 | */ |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 211 | ret = scan_chain_engine_is_idle(SCANMGR_MAX_DELAY); |
| 212 | if (ret) |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 213 | goto error; |
| 214 | } |
| 215 | |
| 216 | /* Disable IO Scan chain when configuration done*/ |
| 217 | clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id); |
| 218 | return 0; |
| 219 | |
| 220 | error: |
| 221 | /* Disable IO Scan chain when error detected */ |
| 222 | clrbits_le32(&scan_manager_base->en, 1 << io_scan_chain_id); |
Marek Vasut | 24d12ec | 2015-08-01 02:48:03 +0200 | [diff] [blame] | 223 | return ret; |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | int scan_mgr_configure_iocsr(void) |
| 227 | { |
| 228 | int status = 0; |
| 229 | |
| 230 | /* configure the IOCSR through scan chain */ |
Marek Vasut | 92af868 | 2015-07-25 09:36:54 +0200 | [diff] [blame] | 231 | status |= scan_mgr_io_scan_chain_prg(0); |
| 232 | status |= scan_mgr_io_scan_chain_prg(1); |
| 233 | status |= scan_mgr_io_scan_chain_prg(2); |
| 234 | status |= scan_mgr_io_scan_chain_prg(3); |
Chin Liang See | 6355024 | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 235 | return status; |
| 236 | } |