Ang, Chee Hong | dcc3bb6 | 2018-12-19 18:35:14 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Intel Corporation <www.intel.com> |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <altera.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Ang, Chee Hong | dcc3bb6 | 2018-12-19 18:35:14 -0800 | [diff] [blame] | 9 | #include <asm/arch/mailbox_s10.h> |
| 10 | |
| 11 | #define RECONFIG_STATUS_POLL_RESP_TIMEOUT_MS 60000 |
| 12 | #define RECONFIG_STATUS_INTERVAL_DELAY_US 1000000 |
| 13 | |
| 14 | static const struct mbox_cfgstat_state { |
| 15 | int err_no; |
| 16 | const char *error_name; |
| 17 | } mbox_cfgstat_state[] = { |
| 18 | {MBOX_CFGSTAT_STATE_IDLE, "FPGA in idle mode."}, |
| 19 | {MBOX_CFGSTAT_STATE_CONFIG, "FPGA in config mode."}, |
| 20 | {MBOX_CFGSTAT_STATE_FAILACK, "Acknowledgment failed!"}, |
| 21 | {MBOX_CFGSTAT_STATE_ERROR_INVALID, "Invalid bitstream!"}, |
| 22 | {MBOX_CFGSTAT_STATE_ERROR_CORRUPT, "Corrupted bitstream!"}, |
| 23 | {MBOX_CFGSTAT_STATE_ERROR_AUTH, "Authentication failed!"}, |
| 24 | {MBOX_CFGSTAT_STATE_ERROR_CORE_IO, "I/O error!"}, |
| 25 | {MBOX_CFGSTAT_STATE_ERROR_HARDWARE, "Hardware error!"}, |
| 26 | {MBOX_CFGSTAT_STATE_ERROR_FAKE, "Fake error!"}, |
| 27 | {MBOX_CFGSTAT_STATE_ERROR_BOOT_INFO, "Error in boot info!"}, |
| 28 | {MBOX_CFGSTAT_STATE_ERROR_QSPI_ERROR, "Error in QSPI!"}, |
| 29 | {MBOX_RESP_ERROR, "Mailbox general error!"}, |
| 30 | {-ETIMEDOUT, "I/O timeout error"}, |
| 31 | {-1, "Unknown error!"} |
| 32 | }; |
| 33 | |
| 34 | #define MBOX_CFGSTAT_MAX ARRAY_SIZE(mbox_cfgstat_state) |
| 35 | |
| 36 | static const char *mbox_cfgstat_to_str(int err) |
| 37 | { |
| 38 | int i; |
| 39 | |
| 40 | for (i = 0; i < MBOX_CFGSTAT_MAX - 1; i++) { |
| 41 | if (mbox_cfgstat_state[i].err_no == err) |
| 42 | return mbox_cfgstat_state[i].error_name; |
| 43 | } |
| 44 | |
| 45 | return mbox_cfgstat_state[MBOX_CFGSTAT_MAX - 1].error_name; |
| 46 | } |
| 47 | |
| 48 | /* |
| 49 | * Add the ongoing transaction's command ID into pending list and return |
| 50 | * the command ID for next transfer. |
| 51 | */ |
| 52 | static u8 add_transfer(u32 *xfer_pending_list, size_t list_size, u8 id) |
| 53 | { |
| 54 | int i; |
| 55 | |
| 56 | for (i = 0; i < list_size; i++) { |
| 57 | if (xfer_pending_list[i]) |
| 58 | continue; |
| 59 | xfer_pending_list[i] = id; |
| 60 | debug("ID(%d) added to transaction pending list\n", id); |
| 61 | /* |
| 62 | * Increment command ID for next transaction. |
| 63 | * Valid command ID (4 bits) is from 1 to 15. |
| 64 | */ |
| 65 | id = (id % 15) + 1; |
| 66 | break; |
| 67 | } |
| 68 | |
| 69 | return id; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Check whether response ID match the command ID in the transfer |
| 74 | * pending list. If a match is found in the transfer pending list, |
| 75 | * it clears the transfer pending list and return the matched |
| 76 | * command ID. |
| 77 | */ |
| 78 | static int get_and_clr_transfer(u32 *xfer_pending_list, size_t list_size, |
| 79 | u8 id) |
| 80 | { |
| 81 | int i; |
| 82 | |
| 83 | for (i = 0; i < list_size; i++) { |
| 84 | if (id != xfer_pending_list[i]) |
| 85 | continue; |
| 86 | xfer_pending_list[i] = 0; |
| 87 | return id; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Polling the FPGA configuration status. |
| 95 | * Return 0 for success, non-zero for error. |
| 96 | */ |
| 97 | static int reconfig_status_polling_resp(void) |
| 98 | { |
| 99 | int ret; |
| 100 | unsigned long start = get_timer(0); |
| 101 | |
| 102 | while (1) { |
| 103 | ret = mbox_get_fpga_config_status(MBOX_RECONFIG_STATUS); |
| 104 | if (!ret) |
| 105 | return 0; /* configuration success */ |
| 106 | |
| 107 | if (ret != MBOX_CFGSTAT_STATE_CONFIG) |
| 108 | return ret; |
| 109 | |
| 110 | if (get_timer(start) > RECONFIG_STATUS_POLL_RESP_TIMEOUT_MS) |
| 111 | break; /* time out */ |
| 112 | |
| 113 | puts("."); |
| 114 | udelay(RECONFIG_STATUS_INTERVAL_DELAY_US); |
| 115 | } |
| 116 | |
| 117 | return -ETIMEDOUT; |
| 118 | } |
| 119 | |
| 120 | static u32 get_resp_hdr(u32 *r_index, u32 *w_index, u32 *resp_count, |
| 121 | u32 *resp_buf, u32 buf_size, u32 client_id) |
| 122 | { |
| 123 | u32 buf[MBOX_RESP_BUFFER_SIZE]; |
| 124 | u32 mbox_hdr; |
| 125 | u32 resp_len; |
| 126 | u32 hdr_len; |
| 127 | u32 i; |
| 128 | |
| 129 | if (*resp_count < buf_size) { |
| 130 | u32 rcv_len_max = buf_size - *resp_count; |
| 131 | |
| 132 | if (rcv_len_max > MBOX_RESP_BUFFER_SIZE) |
| 133 | rcv_len_max = MBOX_RESP_BUFFER_SIZE; |
| 134 | resp_len = mbox_rcv_resp(buf, rcv_len_max); |
| 135 | |
| 136 | for (i = 0; i < resp_len; i++) { |
| 137 | resp_buf[(*w_index)++] = buf[i]; |
| 138 | *w_index %= buf_size; |
| 139 | (*resp_count)++; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* No response in buffer */ |
| 144 | if (*resp_count == 0) |
| 145 | return 0; |
| 146 | |
| 147 | mbox_hdr = resp_buf[*r_index]; |
| 148 | |
| 149 | hdr_len = MBOX_RESP_LEN_GET(mbox_hdr); |
| 150 | |
| 151 | /* Insufficient header length to return a mailbox header */ |
| 152 | if ((*resp_count - 1) < hdr_len) |
| 153 | return 0; |
| 154 | |
| 155 | *r_index += (hdr_len + 1); |
| 156 | *r_index %= buf_size; |
| 157 | *resp_count -= (hdr_len + 1); |
| 158 | |
| 159 | /* Make sure response belongs to us */ |
| 160 | if (MBOX_RESP_CLIENT_GET(mbox_hdr) != client_id) |
| 161 | return 0; |
| 162 | |
| 163 | return mbox_hdr; |
| 164 | } |
| 165 | |
| 166 | /* Send bit stream data to SDM via RECONFIG_DATA mailbox command */ |
| 167 | static int send_reconfig_data(const void *rbf_data, size_t rbf_size, |
| 168 | u32 xfer_max, u32 buf_size_max) |
| 169 | { |
| 170 | u32 response_buffer[MBOX_RESP_BUFFER_SIZE]; |
| 171 | u32 xfer_pending[MBOX_RESP_BUFFER_SIZE]; |
| 172 | u32 resp_rindex = 0; |
| 173 | u32 resp_windex = 0; |
| 174 | u32 resp_count = 0; |
| 175 | u32 xfer_count = 0; |
Ang, Chee Hong | c4192f7 | 2019-02-17 20:07:50 -0800 | [diff] [blame] | 176 | int resp_err = 0; |
Ang, Chee Hong | dcc3bb6 | 2018-12-19 18:35:14 -0800 | [diff] [blame] | 177 | u8 cmd_id = 1; |
| 178 | u32 args[3]; |
| 179 | int ret; |
| 180 | |
| 181 | debug("SDM xfer_max = %d\n", xfer_max); |
| 182 | debug("SDM buf_size_max = %x\n\n", buf_size_max); |
| 183 | |
| 184 | memset(xfer_pending, 0, sizeof(xfer_pending)); |
| 185 | |
| 186 | while (rbf_size || xfer_count) { |
| 187 | if (!resp_err && rbf_size && xfer_count < xfer_max) { |
| 188 | args[0] = MBOX_ARG_DESC_COUNT(1); |
| 189 | args[1] = (u64)rbf_data; |
| 190 | if (rbf_size >= buf_size_max) { |
| 191 | args[2] = buf_size_max; |
| 192 | rbf_size -= buf_size_max; |
| 193 | rbf_data += buf_size_max; |
| 194 | } else { |
| 195 | args[2] = (u64)rbf_size; |
| 196 | rbf_size = 0; |
| 197 | } |
| 198 | |
Ang, Chee Hong | c4192f7 | 2019-02-17 20:07:50 -0800 | [diff] [blame] | 199 | resp_err = mbox_send_cmd_only(cmd_id, MBOX_RECONFIG_DATA, |
Ang, Chee Hong | dcc3bb6 | 2018-12-19 18:35:14 -0800 | [diff] [blame] | 200 | MBOX_CMD_INDIRECT, 3, args); |
Ang, Chee Hong | c4192f7 | 2019-02-17 20:07:50 -0800 | [diff] [blame] | 201 | if (!resp_err) { |
Ang, Chee Hong | dcc3bb6 | 2018-12-19 18:35:14 -0800 | [diff] [blame] | 202 | xfer_count++; |
| 203 | cmd_id = add_transfer(xfer_pending, |
| 204 | MBOX_RESP_BUFFER_SIZE, |
| 205 | cmd_id); |
| 206 | } |
| 207 | puts("."); |
| 208 | } else { |
| 209 | u32 resp_hdr = get_resp_hdr(&resp_rindex, &resp_windex, |
| 210 | &resp_count, |
| 211 | response_buffer, |
| 212 | MBOX_RESP_BUFFER_SIZE, |
| 213 | MBOX_CLIENT_ID_UBOOT); |
| 214 | |
| 215 | /* |
| 216 | * If no valid response header found or |
| 217 | * non-zero length from RECONFIG_DATA |
| 218 | */ |
| 219 | if (!resp_hdr || MBOX_RESP_LEN_GET(resp_hdr)) |
| 220 | continue; |
| 221 | |
| 222 | /* Check for response's status */ |
| 223 | if (!resp_err) { |
Ang, Chee Hong | c4192f7 | 2019-02-17 20:07:50 -0800 | [diff] [blame] | 224 | resp_err = MBOX_RESP_ERR_GET(resp_hdr); |
| 225 | debug("Response error code: %08x\n", resp_err); |
Ang, Chee Hong | dcc3bb6 | 2018-12-19 18:35:14 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | ret = get_and_clr_transfer(xfer_pending, |
| 229 | MBOX_RESP_BUFFER_SIZE, |
| 230 | MBOX_RESP_ID_GET(resp_hdr)); |
| 231 | if (ret) { |
| 232 | /* Claim and reuse the ID */ |
| 233 | cmd_id = (u8)ret; |
| 234 | xfer_count--; |
| 235 | } |
| 236 | |
| 237 | if (resp_err && !xfer_count) |
Ang, Chee Hong | c4192f7 | 2019-02-17 20:07:50 -0800 | [diff] [blame] | 238 | return resp_err; |
Ang, Chee Hong | dcc3bb6 | 2018-12-19 18:35:14 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * This is the interface used by FPGA driver. |
| 247 | * Return 0 for success, non-zero for error. |
| 248 | */ |
| 249 | int stratix10_load(Altera_desc *desc, const void *rbf_data, size_t rbf_size) |
| 250 | { |
| 251 | int ret; |
| 252 | u32 resp_len = 2; |
| 253 | u32 resp_buf[2]; |
| 254 | |
| 255 | debug("Sending MBOX_RECONFIG...\n"); |
| 256 | ret = mbox_send_cmd(MBOX_ID_UBOOT, MBOX_RECONFIG, MBOX_CMD_DIRECT, 0, |
| 257 | NULL, 0, &resp_len, resp_buf); |
| 258 | if (ret) { |
| 259 | puts("Failure in RECONFIG mailbox command!\n"); |
| 260 | return ret; |
| 261 | } |
| 262 | |
| 263 | ret = send_reconfig_data(rbf_data, rbf_size, resp_buf[0], resp_buf[1]); |
| 264 | if (ret) { |
| 265 | printf("RECONFIG_DATA error: %08x, %s\n", ret, |
| 266 | mbox_cfgstat_to_str(ret)); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | /* Make sure we don't send MBOX_RECONFIG_STATUS too fast */ |
| 271 | udelay(RECONFIG_STATUS_INTERVAL_DELAY_US); |
| 272 | |
| 273 | debug("Polling with MBOX_RECONFIG_STATUS...\n"); |
| 274 | ret = reconfig_status_polling_resp(); |
| 275 | if (ret) { |
| 276 | printf("RECONFIG_STATUS Error: %08x, %s\n", ret, |
| 277 | mbox_cfgstat_to_str(ret)); |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | puts("FPGA reconfiguration OK!\n"); |
| 282 | |
| 283 | return ret; |
| 284 | } |