Chin Liang See | dc4d4aa | 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> |
| 8 | #include <asm/io.h> |
| 9 | #include <asm/arch/freeze_controller.h> |
| 10 | #include <asm/arch/scan_manager.h> |
| 11 | |
| 12 | DECLARE_GLOBAL_DATA_PTR; |
| 13 | |
| 14 | static const struct socfpga_scan_manager *scan_manager_base = |
| 15 | (void *)(SOCFPGA_SCANMGR_ADDRESS); |
| 16 | static 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 | */ |
| 23 | static 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 Vasut | 2df7b2a | 2015-07-25 09:33:28 +0200 | [diff] [blame] | 41 | /** |
| 42 | * scan_mgr_io_scan_chain_prg() - Program HPS IO Scan Chain |
| 43 | * @io_scan_chain_id: IO scan chain ID |
Marek Vasut | 2df7b2a | 2015-07-25 09:33:28 +0200 | [diff] [blame] | 44 | */ |
Marek Vasut | 03a92b2 | 2015-07-25 09:36:54 +0200 | [diff] [blame] | 45 | static int scan_mgr_io_scan_chain_prg(const unsigned int io_scan_chain_id) |
Chin Liang See | dc4d4aa | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 46 | { |
| 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 Vasut | 575d741 | 2015-07-25 09:53:23 +0200 | [diff] [blame] | 51 | uint32_t i, ret; |
Chin Liang See | dc4d4aa | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 52 | uint32_t index = 0; |
Marek Vasut | 575d741 | 2015-07-25 09:53:23 +0200 | [diff] [blame] | 53 | uint32_t io_scan_chain_len_in_bits; |
| 54 | const unsigned long *iocsr_scan_chain; |
Marek Vasut | 03a92b2 | 2015-07-25 09:36:54 +0200 | [diff] [blame] | 55 | |
Marek Vasut | 575d741 | 2015-07-25 09:53:23 +0200 | [diff] [blame] | 56 | 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 See | dc4d4aa | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 60 | |
| 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 | |
| 196 | error: |
| 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 | |
| 202 | int scan_mgr_configure_iocsr(void) |
| 203 | { |
| 204 | int status = 0; |
| 205 | |
| 206 | /* configure the IOCSR through scan chain */ |
Marek Vasut | 03a92b2 | 2015-07-25 09:36:54 +0200 | [diff] [blame] | 207 | 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 See | dc4d4aa | 2014-06-10 01:17:42 -0500 | [diff] [blame] | 211 | return status; |
| 212 | } |