| @@ -18,6 +18,120 @@ · 17 unmodified lines |
| 18 | 18 | #include <linux/wait.h> |
| 19 | 19 | #include "internal.h" |
| 20 | 20 | |
| 21 | +static int mhi_fw_load_amss(struct mhi_controller *mhi_cntrl, |
| 22 | + const struct mhi_buf *mhi_buf) |
| 23 | +{ |
| 24 | + void __iomem *base = mhi_cntrl->bhie; |
| 25 | + rwlock_t *pm_lock = &mhi_cntrl->pm_lock; |
| 26 | + u32 tx_status, sequence_id; |
| 27 | + |
| 28 | + read_lock_bh(pm_lock); |
| 29 | + if (!MHI_REG_ACCESS_VALID(mhi_cntrl->pm_state)) { |
| 30 | + read_unlock_bh(pm_lock); |
| 31 | + return -EIO; |
| 32 | + } |
| 33 | + |
| 34 | + mhi_write_reg(mhi_cntrl, base, BHIE_TXVECADDR_HIGH_OFFS, |
| 35 | + upper_32_bits(mhi_buf->dma_addr)); |
| 36 | + |
| 37 | + mhi_write_reg(mhi_cntrl, base, BHIE_TXVECADDR_LOW_OFFS, |
| 38 | + lower_32_bits(mhi_buf->dma_addr)); |
| 39 | + |
| 40 | + mhi_write_reg(mhi_cntrl, base, BHIE_TXVECSIZE_OFFS, mhi_buf->len); |
| 41 | + |
| 42 | + sequence_id = prandom_u32() & BHIE_TXVECSTATUS_SEQNUM_BMSK; |
| 43 | + mhi_write_reg_field(mhi_cntrl, base, BHIE_TXVECDB_OFFS, |
| 44 | + BHIE_TXVECDB_SEQNUM_BMSK, BHIE_TXVECDB_SEQNUM_SHFT, |
| 45 | + sequence_id); |
| 46 | + read_unlock_bh(pm_lock); |
| 47 | + |
| 48 | + /* Wait for the image download to complete */ |
| 49 | + wait_event_timeout(mhi_cntrl->state_event, |
| 50 | + MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state) || |
| 51 | + mhi_read_reg_field(mhi_cntrl, base, |
| 52 | + BHIE_TXVECSTATUS_OFFS, |
| 53 | + BHIE_TXVECSTATUS_STATUS_BMSK, |
| 54 | + BHIE_TXVECSTATUS_STATUS_SHFT, |
| 55 | + &tx_status) || tx_status, |
| 56 | + msecs_to_jiffies(mhi_cntrl->timeout_ms)); |
| 57 | + |
| 58 | + if (MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) |
| 59 | + return -EIO; |
| 60 | + |
| 61 | + return (tx_status == BHIE_TXVECSTATUS_STATUS_XFER_COMPL) ? 0 : -EIO; |
| 62 | +} |
| 63 | + |
| 64 | +static int mhi_fw_load_sbl(struct mhi_controller *mhi_cntrl, |
| 65 | + dma_addr_t dma_addr, |
| 66 | + size_t size) |
| 67 | +{ |
| 68 | + u32 tx_status, val, session_id; |
| 69 | + int i, ret; |
| 70 | + void __iomem *base = mhi_cntrl->bhi; |
| 71 | + rwlock_t *pm_lock = &mhi_cntrl->pm_lock; |
| 72 | + struct device *dev = &mhi_cntrl->mhi_dev->dev; |
| 73 | + struct { |
| 74 | + char *name; |
| 75 | + u32 offset; |
| 76 | + } error_reg[] = { |
| 77 | + { "ERROR_CODE", BHI_ERRCODE }, |
| 78 | + { "ERROR_DBG1", BHI_ERRDBG1 }, |
| 79 | + { "ERROR_DBG2", BHI_ERRDBG2 }, |
| 80 | + { "ERROR_DBG3", BHI_ERRDBG3 }, |
| 81 | + { NULL }, |
| 82 | + }; |
| 83 | + |
| 84 | + read_lock_bh(pm_lock); |
| 85 | + if (!MHI_REG_ACCESS_VALID(mhi_cntrl->pm_state)) { |
| 86 | + read_unlock_bh(pm_lock); |
| 87 | + goto invalid_pm_state; |
| 88 | + } |
| 89 | + |
| 90 | + dev_dbg(dev, "Starting SBL download via BHI\n"); |
| 91 | + mhi_write_reg(mhi_cntrl, base, BHI_STATUS, 0); |
| 92 | + mhi_write_reg(mhi_cntrl, base, BHI_IMGADDR_HIGH, |
| 93 | + upper_32_bits(dma_addr)); |
| 94 | + mhi_write_reg(mhi_cntrl, base, BHI_IMGADDR_LOW, |
| 95 | + lower_32_bits(dma_addr)); |
| 96 | + mhi_write_reg(mhi_cntrl, base, BHI_IMGSIZE, size); |
| 97 | + session_id = prandom_u32() & BHI_TXDB_SEQNUM_BMSK; |
| 98 | + mhi_write_reg(mhi_cntrl, base, BHI_IMGTXDB, session_id); |
| 99 | + read_unlock_bh(pm_lock); |
| 100 | + |
| 101 | + /* Wait for the image download to complete */ |
| 102 | + ret = wait_event_timeout(mhi_cntrl->state_event, |
| 103 | + MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state) || |
| 104 | + mhi_read_reg_field(mhi_cntrl, base, BHI_STATUS, |
| 105 | + BHI_STATUS_MASK, BHI_STATUS_SHIFT, |
| 106 | + &tx_status) || tx_status, |
| 107 | + msecs_to_jiffies(mhi_cntrl->timeout_ms)); |
| 108 | + if (MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) |
| 109 | + goto invalid_pm_state; |
| 110 | + |
| 111 | + if (tx_status == BHI_STATUS_ERROR) { |
| 112 | + dev_err(dev, "Image transfer failed\n"); |
| 113 | + read_lock_bh(pm_lock); |
| 114 | + if (MHI_REG_ACCESS_VALID(mhi_cntrl->pm_state)) { |
| 115 | + for (i = 0; error_reg[i].name; i++) { |
| 116 | + ret = mhi_read_reg(mhi_cntrl, base, |
| 117 | + error_reg[i].offset, &val); |
| 118 | + if (ret) |
| 119 | + break; |
| 120 | + dev_err(dev, "Reg: %s value: 0x%x\n", |
| 121 | + error_reg[i].name, val); |
| 122 | + } |
| 123 | + } |
| 124 | + read_unlock_bh(pm_lock); |
| 125 | + goto invalid_pm_state; |
| 126 | + } |
| 127 | + |
| 128 | + return (!ret) ? -ETIMEDOUT : 0; |
| 129 | + |
| 130 | +invalid_pm_state: |
| 131 | + |
| 132 | + return -EIO; |
| 133 | +} |
| 134 | + |
| 21 | 135 | void mhi_free_bhie_table(struct mhi_controller *mhi_cntrl, |
| 22 | 136 | struct image_info *image_info) |
| 23 | 137 | { |
| @@ -85,3 +199,157 @@ error_alloc_mhi_buf: · 61 unmodified lines |
| 85 | 199 | |
| 86 | 200 | return -ENOMEM; |
| 87 | 201 | } |
| 202 | + |
| 203 | +static void mhi_firmware_copy(struct mhi_controller *mhi_cntrl, |
| 204 | + const struct firmware *firmware, |
| 205 | + struct image_info *img_info) |
| 206 | +{ |
| 207 | + size_t remainder = firmware->size; |
| 208 | + size_t to_cpy; |
| 209 | + const u8 *buf = firmware->data; |
| 210 | + int i = 0; |
| 211 | + struct mhi_buf *mhi_buf = img_info->mhi_buf; |
| 212 | + struct bhi_vec_entry *bhi_vec = img_info->bhi_vec; |
| 213 | + |
| 214 | + while (remainder) { |
| 215 | + to_cpy = min(remainder, mhi_buf->len); |
| 216 | + memcpy(mhi_buf->buf, buf, to_cpy); |
| 217 | + bhi_vec->dma_addr = mhi_buf->dma_addr; |
| 218 | + bhi_vec->size = to_cpy; |
| 219 | + |
| 220 | + buf += to_cpy; |
| 221 | + remainder -= to_cpy; |
| 222 | + i++; |
| 223 | + bhi_vec++; |
| 224 | + mhi_buf++; |
| 225 | + } |
| 226 | +} |
| 227 | + |
| 228 | +void mhi_fw_load_worker(struct work_struct *work) |
| 229 | +{ |
| 230 | + struct mhi_controller *mhi_cntrl; |
| 231 | + const struct firmware *firmware = NULL; |
| 232 | + struct image_info *image_info; |
| 233 | + struct device *dev; |
| 234 | + const char *fw_name; |
| 235 | + void *buf; |
| 236 | + dma_addr_t dma_addr; |
| 237 | + size_t size; |
| 238 | + int ret; |
| 239 | + |
| 240 | + mhi_cntrl = container_of(work, struct mhi_controller, fw_worker); |
| 241 | + dev = &mhi_cntrl->mhi_dev->dev; |
| 242 | + |
| 243 | + dev_dbg(dev, "Waiting for device to enter PBL from: %s\n", |
| 244 | + TO_MHI_EXEC_STR(mhi_cntrl->ee)); |
| 245 | + |
| 246 | + ret = wait_event_timeout(mhi_cntrl->state_event, |
| 247 | + MHI_IN_PBL(mhi_cntrl->ee) || |
| 248 | + MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state), |
| 249 | + msecs_to_jiffies(mhi_cntrl->timeout_ms)); |
| 250 | + |
| 251 | + if (!ret || MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { |
| 252 | + dev_err(dev, "Device MHI is not in valid state\n"); |
| 253 | + return; |
| 254 | + } |
| 255 | + |
| 256 | + /* If device is in pass through, do reset to ready state transition */ |
| 257 | + if (mhi_cntrl->ee == MHI_EE_PTHRU) |
| 258 | + goto fw_load_ee_pthru; |
| 259 | + |
| 260 | + fw_name = (mhi_cntrl->ee == MHI_EE_EDL) ? |
| 261 | + mhi_cntrl->edl_image : mhi_cntrl->fw_image; |
| 262 | + |
| 263 | + if (!fw_name || (mhi_cntrl->fbc_download && (!mhi_cntrl->sbl_size || |
| 264 | + !mhi_cntrl->seg_len))) { |
| 265 | + dev_err(dev, |
| 266 | + "No firmware image defined or !sbl_size || !seg_len\n"); |
| 267 | + return; |
| 268 | + } |
| 269 | + |
| 270 | + ret = request_firmware(&firmware, fw_name, dev); |
| 271 | + if (ret) { |
| 272 | + dev_err(dev, "Error loading firmware: %d\n", ret); |
| 273 | + return; |
| 274 | + } |
| 275 | + |
| 276 | + size = (mhi_cntrl->fbc_download) ? mhi_cntrl->sbl_size : firmware->size; |
| 277 | + |
| 278 | + /* SBL size provided is maximum size, not necessarily the image size */ |
| 279 | + if (size > firmware->size) |
| 280 | + size = firmware->size; |
| 281 | + |
| 282 | + buf = mhi_alloc_coherent(mhi_cntrl, size, &dma_addr, GFP_KERNEL); |
| 283 | + if (!buf) { |
| 284 | + release_firmware(firmware); |
| 285 | + return; |
| 286 | + } |
| 287 | + |
| 288 | + /* Download SBL image */ |
| 289 | + memcpy(buf, firmware->data, size); |
| 290 | + ret = mhi_fw_load_sbl(mhi_cntrl, dma_addr, size); |
| 291 | + mhi_free_coherent(mhi_cntrl, size, buf, dma_addr); |
| 292 | + |
| 293 | + if (!mhi_cntrl->fbc_download || ret || mhi_cntrl->ee == MHI_EE_EDL) |
| 294 | + release_firmware(firmware); |
| 295 | + |
| 296 | + /* Error or in EDL mode, we're done */ |
| 297 | + if (ret || mhi_cntrl->ee == MHI_EE_EDL) |
| 298 | + return; |
| 299 | + |
| 300 | + write_lock_irq(&mhi_cntrl->pm_lock); |
| 301 | + mhi_cntrl->dev_state = MHI_STATE_RESET; |
| 302 | + write_unlock_irq(&mhi_cntrl->pm_lock); |
| 303 | + |
| 304 | + /* |
| 305 | + * If we're doing fbc, populate vector tables while |
| 306 | + * device transitioning into MHI READY state |
| 307 | + */ |
| 308 | + if (mhi_cntrl->fbc_download) { |
| 309 | + ret = mhi_alloc_bhie_table(mhi_cntrl, &mhi_cntrl->fbc_image, |
| 310 | + firmware->size); |
| 311 | + if (ret) |
| 312 | + goto error_alloc_fw_table; |
| 313 | + |
| 314 | + /* Load the firmware into BHIE vec table */ |
| 315 | + mhi_firmware_copy(mhi_cntrl, firmware, mhi_cntrl->fbc_image); |
| 316 | + } |
| 317 | + |
| 318 | +fw_load_ee_pthru: |
| 319 | + /* Transitioning into MHI RESET->READY state */ |
| 320 | + ret = mhi_ready_state_transition(mhi_cntrl); |
| 321 | + |
| 322 | + if (!mhi_cntrl->fbc_download) |
| 323 | + return; |
| 324 | + |
| 325 | + if (ret) |
| 326 | + goto error_read; |
| 327 | + |
| 328 | + /* Wait for the SBL event */ |
| 329 | + ret = wait_event_timeout(mhi_cntrl->state_event, |
| 330 | + mhi_cntrl->ee == MHI_EE_SBL || |
| 331 | + MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state), |
| 332 | + msecs_to_jiffies(mhi_cntrl->timeout_ms)); |
| 333 | + |
| 334 | + if (!ret || MHI_PM_IN_ERROR_STATE(mhi_cntrl->pm_state)) { |
| 335 | + dev_err(dev, "MHI did not enter SBL\n"); |
| 336 | + goto error_read; |
| 337 | + } |
| 338 | + |
| 339 | + /* Start full firmware image download */ |
| 340 | + image_info = mhi_cntrl->fbc_image; |
| 341 | + ret = mhi_fw_load_amss(mhi_cntrl, |
| 342 | + /* Vector table is the last entry */ |
| 343 | + &image_info->mhi_buf[image_info->entries - 1]); |
| 344 | + |
| 345 | + release_firmware(firmware); |
| 346 | + |
| 347 | + return; |
| 348 | + |
| 349 | +error_read: |
| 350 | + mhi_free_bhie_table(mhi_cntrl, mhi_cntrl->fbc_image); |
| 351 | + mhi_cntrl->fbc_image = NULL; |
| 352 | + |
| 353 | +error_alloc_fw_table: |
| 354 | + release_firmware(firmware); |
| 355 | +} |