1 // SPDX-License-Identifier: GPL-2.0-or-later 2 3 #include <linux/array_size.h> 4 #include <linux/bitfield.h> 5 #include <linux/bits.h> 6 #include <linux/delay.h> 7 #include <linux/dev_printk.h> 8 #include <linux/errno.h> 9 #include <linux/jiffies.h> 10 #include <linux/minmax.h> 11 #include <linux/netlink.h> 12 #include <linux/sched/signal.h> 13 #include <linux/sizes.h> 14 #include <linux/sprintf.h> 15 #include <linux/string.h> 16 #include <linux/types.h> 17 #include <linux/unaligned.h> 18 #include <net/devlink.h> 19 20 #include "core.h" 21 #include "devlink.h" 22 #include "flash.h" 23 24 #define ZL_FLASH_ERR_PFX "FW update failed: " 25 #define ZL_FLASH_ERR_MSG(_extack, _msg, ...) \ 26 NL_SET_ERR_MSG_FMT_MOD((_extack), ZL_FLASH_ERR_PFX _msg, \ 27 ## __VA_ARGS__) 28 29 /** 30 * zl3073x_flash_download - Download image block to device memory 31 * @zldev: zl3073x device structure 32 * @component: name of the component to be downloaded 33 * @addr: device memory target address 34 * @data: pointer to data to download 35 * @size: size of data to download 36 * @extack: netlink extack pointer to report errors 37 * 38 * Return: 0 on success, <0 on error 39 */ 40 static int 41 zl3073x_flash_download(struct zl3073x_dev *zldev, const char *component, 42 u32 addr, const void *data, size_t size, 43 struct netlink_ext_ack *extack) 44 { 45 #define ZL_CHECK_DELAY 5000 /* Check for interrupt each 5 seconds */ 46 unsigned long check_time; 47 const void *ptr, *end; 48 int rc = 0; 49 50 dev_dbg(zldev->dev, "Downloading %zu bytes to device memory at 0x%0x\n", 51 size, addr); 52 53 check_time = jiffies + msecs_to_jiffies(ZL_CHECK_DELAY); 54 55 for (ptr = data, end = data + size; ptr < end; ptr += 4, addr += 4) { 56 /* Write current word to HW memory */ 57 rc = zl3073x_write_hwreg(zldev, addr, 58 get_unaligned((u32 *)ptr)); 59 if (rc) { 60 ZL_FLASH_ERR_MSG(extack, 61 "failed to write to memory at 0x%0x", 62 addr); 63 return rc; 64 } 65 66 if (time_is_before_jiffies(check_time)) { 67 if (signal_pending(current)) { 68 ZL_FLASH_ERR_MSG(extack, 69 "Flashing interrupted"); 70 return -EINTR; 71 } 72 73 check_time = jiffies + msecs_to_jiffies(ZL_CHECK_DELAY); 74 } 75 76 /* Report status each 1 kB block */ 77 if ((ptr - data) % 1024 == 0) 78 zl3073x_devlink_flash_notify(zldev, "Downloading image", 79 component, ptr - data, 80 size); 81 } 82 83 zl3073x_devlink_flash_notify(zldev, "Downloading image", component, 84 ptr - data, size); 85 86 dev_dbg(zldev->dev, "%zu bytes downloaded to device memory\n", size); 87 88 return rc; 89 } 90 91 /** 92 * zl3073x_flash_error_check - Check for flash utility errors 93 * @zldev: zl3073x device structure 94 * @extack: netlink extack pointer to report errors 95 * 96 * The function checks for errors detected by the flash utility and 97 * reports them if any were found. 98 * 99 * Return: 0 on success, -EIO when errors are detected 100 */ 101 static int 102 zl3073x_flash_error_check(struct zl3073x_dev *zldev, 103 struct netlink_ext_ack *extack) 104 { 105 u32 count, cause; 106 int rc; 107 108 rc = zl3073x_read_u32(zldev, ZL_REG_ERROR_COUNT, &count); 109 if (rc) 110 return rc; 111 else if (!count) 112 return 0; /* No error */ 113 114 rc = zl3073x_read_u32(zldev, ZL_REG_ERROR_CAUSE, &cause); 115 if (rc) 116 return rc; 117 118 /* Report errors */ 119 ZL_FLASH_ERR_MSG(extack, 120 "utility error occurred: count=%u cause=0x%x", count, 121 cause); 122 123 return -EIO; 124 } 125 126 /** 127 * zl3073x_flash_wait_ready - Check or wait for utility to be ready to flash 128 * @zldev: zl3073x device structure 129 * @timeout_ms: timeout for the waiting 130 * 131 * Return: 0 on success, <0 on error 132 */ 133 static int 134 zl3073x_flash_wait_ready(struct zl3073x_dev *zldev, unsigned int timeout_ms) 135 { 136 #define ZL_FLASH_POLL_DELAY_MS 100 137 unsigned long timeout; 138 int rc, i; 139 140 dev_dbg(zldev->dev, "Waiting for flashing to be ready\n"); 141 142 timeout = jiffies + msecs_to_jiffies(timeout_ms); 143 144 for (i = 0; time_is_after_jiffies(timeout); i++) { 145 u8 value; 146 147 /* Check for interrupt each 1s */ 148 if (i > 9) { 149 if (signal_pending(current)) 150 return -EINTR; 151 i = 0; 152 } 153 154 rc = zl3073x_read_u8(zldev, ZL_REG_WRITE_FLASH, &value); 155 if (rc) 156 return rc; 157 158 value = FIELD_GET(ZL_WRITE_FLASH_OP, value); 159 160 if (value == ZL_WRITE_FLASH_OP_DONE) 161 return 0; /* Successfully done */ 162 163 msleep(ZL_FLASH_POLL_DELAY_MS); 164 } 165 166 return -ETIMEDOUT; 167 } 168 169 /** 170 * zl3073x_flash_cmd_wait - Perform flash operation and wait for finish 171 * @zldev: zl3073x device structure 172 * @operation: operation to perform 173 * @extack: netlink extack pointer to report errors 174 * 175 * Return: 0 on success, <0 on error 176 */ 177 static int 178 zl3073x_flash_cmd_wait(struct zl3073x_dev *zldev, u32 operation, 179 struct netlink_ext_ack *extack) 180 { 181 #define ZL_FLASH_PHASE1_TIMEOUT_MS 60000 /* up to 1 minute */ 182 #define ZL_FLASH_PHASE2_TIMEOUT_MS 120000 /* up to 2 minutes */ 183 u8 value; 184 int rc; 185 186 dev_dbg(zldev->dev, "Sending flash command: 0x%x\n", operation); 187 188 rc = zl3073x_flash_wait_ready(zldev, ZL_FLASH_PHASE1_TIMEOUT_MS); 189 if (rc) 190 return rc; 191 192 /* Issue the requested operation */ 193 rc = zl3073x_read_u8(zldev, ZL_REG_WRITE_FLASH, &value); 194 if (rc) 195 return rc; 196 197 FIELD_MODIFY(ZL_WRITE_FLASH_OP, &value, operation); 198 199 rc = zl3073x_write_u8(zldev, ZL_REG_WRITE_FLASH, value); 200 if (rc) 201 return rc; 202 203 /* Wait for command completion */ 204 rc = zl3073x_flash_wait_ready(zldev, ZL_FLASH_PHASE2_TIMEOUT_MS); 205 if (rc) 206 return rc; 207 208 return zl3073x_flash_error_check(zldev, extack); 209 } 210 211 /** 212 * zl3073x_flash_get_sector_size - Get flash sector size 213 * @zldev: zl3073x device structure 214 * @sector_size: sector size returned by the function 215 * 216 * The function reads the flash sector size detected by flash utility and 217 * stores it into @sector_size. 218 * 219 * Return: 0 on success, <0 on error 220 */ 221 static int 222 zl3073x_flash_get_sector_size(struct zl3073x_dev *zldev, size_t *sector_size) 223 { 224 u8 flash_info; 225 int rc; 226 227 rc = zl3073x_read_u8(zldev, ZL_REG_FLASH_INFO, &flash_info); 228 if (rc) 229 return rc; 230 231 switch (FIELD_GET(ZL_FLASH_INFO_SECTOR_SIZE, flash_info)) { 232 case ZL_FLASH_INFO_SECTOR_4K: 233 *sector_size = SZ_4K; 234 break; 235 case ZL_FLASH_INFO_SECTOR_64K: 236 *sector_size = SZ_64K; 237 break; 238 default: 239 rc = -EINVAL; 240 break; 241 } 242 243 return rc; 244 } 245 246 /** 247 * zl3073x_flash_block - Download and flash memory block 248 * @zldev: zl3073x device structure 249 * @component: component name 250 * @operation: flash operation to perform 251 * @page: destination flash page 252 * @addr: device memory address to load data 253 * @data: pointer to data to be flashed 254 * @size: size of data 255 * @extack: netlink extack pointer to report errors 256 * 257 * The function downloads the memory block given by the @data pointer and 258 * the size @size and flashes it into internal memory on flash page @page. 259 * The internal flash operation performed by the firmware is specified by 260 * the @operation parameter. 261 * 262 * Return: 0 on success, <0 on error 263 */ 264 static int 265 zl3073x_flash_block(struct zl3073x_dev *zldev, const char *component, 266 u32 operation, u32 page, u32 addr, const void *data, 267 size_t size, struct netlink_ext_ack *extack) 268 { 269 int rc; 270 271 /* Download block to device memory */ 272 rc = zl3073x_flash_download(zldev, component, addr, data, size, extack); 273 if (rc) 274 return rc; 275 276 /* Set address to flash from */ 277 rc = zl3073x_write_u32(zldev, ZL_REG_IMAGE_START_ADDR, addr); 278 if (rc) 279 return rc; 280 281 /* Set size of block to flash */ 282 rc = zl3073x_write_u32(zldev, ZL_REG_IMAGE_SIZE, size); 283 if (rc) 284 return rc; 285 286 /* Set destination page to flash */ 287 rc = zl3073x_write_u32(zldev, ZL_REG_FLASH_INDEX_WRITE, page); 288 if (rc) 289 return rc; 290 291 /* Set filling pattern */ 292 rc = zl3073x_write_u32(zldev, ZL_REG_FILL_PATTERN, U32_MAX); 293 if (rc) 294 return rc; 295 296 zl3073x_devlink_flash_notify(zldev, "Flashing image", component, 0, 297 size); 298 299 dev_dbg(zldev->dev, "Flashing %zu bytes to page %u\n", size, page); 300 301 /* Execute sectors flash operation */ 302 rc = zl3073x_flash_cmd_wait(zldev, operation, extack); 303 if (rc) 304 return rc; 305 306 zl3073x_devlink_flash_notify(zldev, "Flashing image", component, size, 307 size); 308 309 return 0; 310 } 311 312 /** 313 * zl3073x_flash_sectors - Flash sectors 314 * @zldev: zl3073x device structure 315 * @component: component name 316 * @page: destination flash page 317 * @addr: device memory address to load data 318 * @data: pointer to data to be flashed 319 * @size: size of data 320 * @extack: netlink extack pointer to report errors 321 * 322 * The function flashes given @data with size of @size to the internal flash 323 * memory block starting from page @page. The function uses sector flash 324 * method and has to take into account the flash sector size reported by 325 * flashing utility. Input data are spliced into blocks according this 326 * sector size and each block is flashed separately. 327 * 328 * Return: 0 on success, <0 on error 329 */ 330 int zl3073x_flash_sectors(struct zl3073x_dev *zldev, const char *component, 331 u32 page, u32 addr, const void *data, size_t size, 332 struct netlink_ext_ack *extack) 333 { 334 #define ZL_FLASH_MAX_BLOCK_SIZE 0x0001E000 335 #define ZL_FLASH_PAGE_SIZE 256 336 size_t max_block_size, block_size, sector_size; 337 const void *ptr, *end; 338 int rc; 339 340 /* Get flash sector size */ 341 rc = zl3073x_flash_get_sector_size(zldev, §or_size); 342 if (rc) { 343 ZL_FLASH_ERR_MSG(extack, "Failed to get flash sector size"); 344 return rc; 345 } 346 347 /* Determine max block size depending on sector size */ 348 max_block_size = ALIGN_DOWN(ZL_FLASH_MAX_BLOCK_SIZE, sector_size); 349 350 for (ptr = data, end = data + size; ptr < end; ptr += block_size) { 351 char comp_str[32]; 352 353 block_size = min_t(size_t, max_block_size, end - ptr); 354 355 /* Add suffix '-partN' if the requested component size is 356 * greater than max_block_size. 357 */ 358 if (max_block_size < size) 359 snprintf(comp_str, sizeof(comp_str), "%s-part%zu", 360 component, (ptr - data) / max_block_size + 1); 361 else 362 strscpy(comp_str, component); 363 364 /* Flash the memory block */ 365 rc = zl3073x_flash_block(zldev, comp_str, 366 ZL_WRITE_FLASH_OP_SECTORS, page, addr, 367 ptr, block_size, extack); 368 if (rc) 369 goto finish; 370 371 /* Move to next page */ 372 page += block_size / ZL_FLASH_PAGE_SIZE; 373 } 374 375 finish: 376 zl3073x_devlink_flash_notify(zldev, 377 rc ? "Flashing failed" : "Flashing done", 378 component, 0, 0); 379 380 return rc; 381 } 382 383 /** 384 * zl3073x_flash_page - Flash page 385 * @zldev: zl3073x device structure 386 * @component: component name 387 * @page: destination flash page 388 * @addr: device memory address to load data 389 * @data: pointer to data to be flashed 390 * @size: size of data 391 * @extack: netlink extack pointer to report errors 392 * 393 * The function flashes given @data with size of @size to the internal flash 394 * memory block starting with page @page. 395 * 396 * Return: 0 on success, <0 on error 397 */ 398 int zl3073x_flash_page(struct zl3073x_dev *zldev, const char *component, 399 u32 page, u32 addr, const void *data, size_t size, 400 struct netlink_ext_ack *extack) 401 { 402 int rc; 403 404 /* Flash the memory block */ 405 rc = zl3073x_flash_block(zldev, component, ZL_WRITE_FLASH_OP_PAGE, page, 406 addr, data, size, extack); 407 408 zl3073x_devlink_flash_notify(zldev, 409 rc ? "Flashing failed" : "Flashing done", 410 component, 0, 0); 411 412 return rc; 413 } 414 415 /** 416 * zl3073x_flash_page_copy - Copy flash page 417 * @zldev: zl3073x device structure 418 * @component: component name 419 * @src_page: source page to copy 420 * @dst_page: destination page 421 * @extack: netlink extack pointer to report errors 422 * 423 * The function copies one flash page specified by @src_page into the flash 424 * page specified by @dst_page. 425 * 426 * Return: 0 on success, <0 on error 427 */ 428 int zl3073x_flash_page_copy(struct zl3073x_dev *zldev, const char *component, 429 u32 src_page, u32 dst_page, 430 struct netlink_ext_ack *extack) 431 { 432 int rc; 433 434 /* Set source page to be copied */ 435 rc = zl3073x_write_u32(zldev, ZL_REG_FLASH_INDEX_READ, src_page); 436 if (rc) 437 return rc; 438 439 /* Set destination page for the copy */ 440 rc = zl3073x_write_u32(zldev, ZL_REG_FLASH_INDEX_WRITE, dst_page); 441 if (rc) 442 return rc; 443 444 /* Perform copy operation */ 445 rc = zl3073x_flash_cmd_wait(zldev, ZL_WRITE_FLASH_OP_COPY_PAGE, extack); 446 if (rc) 447 ZL_FLASH_ERR_MSG(extack, "Failed to copy page %u to page %u", 448 src_page, dst_page); 449 450 return rc; 451 } 452 453 /** 454 * zl3073x_flash_mode_verify - Check flash utility 455 * @zldev: zl3073x device structure 456 * 457 * Return: 0 if the flash utility is ready, <0 on error 458 */ 459 static int 460 zl3073x_flash_mode_verify(struct zl3073x_dev *zldev) 461 { 462 u8 family, release; 463 u32 hash; 464 int rc; 465 466 rc = zl3073x_read_u32(zldev, ZL_REG_FLASH_HASH, &hash); 467 if (rc) 468 return rc; 469 470 rc = zl3073x_read_u8(zldev, ZL_REG_FLASH_FAMILY, &family); 471 if (rc) 472 return rc; 473 474 rc = zl3073x_read_u8(zldev, ZL_REG_FLASH_RELEASE, &release); 475 if (rc) 476 return rc; 477 478 dev_dbg(zldev->dev, 479 "Flash utility check: hash 0x%08x, fam 0x%02x, rel 0x%02x\n", 480 hash, family, release); 481 482 /* Return success for correct family */ 483 return (family == 0x21) ? 0 : -ENODEV; 484 } 485 486 static int 487 zl3073x_flash_host_ctrl_enable(struct zl3073x_dev *zldev) 488 { 489 u8 host_ctrl; 490 int rc; 491 492 /* Enable host control */ 493 rc = zl3073x_read_u8(zldev, ZL_REG_HOST_CONTROL, &host_ctrl); 494 if (rc) 495 return rc; 496 497 host_ctrl |= ZL_HOST_CONTROL_ENABLE; 498 499 return zl3073x_write_u8(zldev, ZL_REG_HOST_CONTROL, host_ctrl); 500 } 501 502 /** 503 * zl3073x_flash_mode_enter - Switch the device to flash mode 504 * @zldev: zl3073x device structure 505 * @util_ptr: buffer with flash utility 506 * @util_size: size of buffer with flash utility 507 * @extack: netlink extack pointer to report errors 508 * 509 * The function prepares and switches the device into flash mode. 510 * 511 * The procedure: 512 * 1) Stop device CPU by specific HW register sequence 513 * 2) Download flash utility to device memory 514 * 3) Resume device CPU by specific HW register sequence 515 * 4) Check communication with flash utility 516 * 5) Enable host control necessary to access flash API 517 * 6) Check for potential error detected by the utility 518 * 519 * The API provided by normal firmware is not available in flash mode 520 * so the caller has to ensure that this API is not used in this mode. 521 * 522 * After performing flash operation the caller should call 523 * @zl3073x_flash_mode_leave to return back to normal operation. 524 * 525 * Return: 0 on success, <0 on error. 526 */ 527 int zl3073x_flash_mode_enter(struct zl3073x_dev *zldev, const void *util_ptr, 528 size_t util_size, struct netlink_ext_ack *extack) 529 { 530 /* Sequence to be written prior utility download */ 531 static const struct zl3073x_hwreg_seq_item pre_seq[] = { 532 HWREG_SEQ_ITEM(0x80000400, 1, BIT(0), 0), 533 HWREG_SEQ_ITEM(0x80206340, 1, BIT(4), 0), 534 HWREG_SEQ_ITEM(0x10000000, 1, BIT(2), 0), 535 HWREG_SEQ_ITEM(0x10000024, 0x00000001, U32_MAX, 0), 536 HWREG_SEQ_ITEM(0x10000020, 0x00000001, U32_MAX, 0), 537 HWREG_SEQ_ITEM(0x10000000, 1, BIT(10), 1000), 538 }; 539 /* Sequence to be written after utility download */ 540 static const struct zl3073x_hwreg_seq_item post_seq[] = { 541 HWREG_SEQ_ITEM(0x10400004, 0x000000C0, U32_MAX, 0), 542 HWREG_SEQ_ITEM(0x10400008, 0x00000000, U32_MAX, 0), 543 HWREG_SEQ_ITEM(0x10400010, 0x20000000, U32_MAX, 0), 544 HWREG_SEQ_ITEM(0x10400014, 0x20000004, U32_MAX, 0), 545 HWREG_SEQ_ITEM(0x10000000, 1, GENMASK(10, 9), 0), 546 HWREG_SEQ_ITEM(0x10000020, 0x00000000, U32_MAX, 0), 547 HWREG_SEQ_ITEM(0x10000000, 0, BIT(0), 1000), 548 }; 549 int rc; 550 551 zl3073x_devlink_flash_notify(zldev, "Prepare flash mode", "utility", 552 0, 0); 553 554 /* Execure pre-load sequence */ 555 rc = zl3073x_write_hwreg_seq(zldev, pre_seq, ARRAY_SIZE(pre_seq)); 556 if (rc) { 557 ZL_FLASH_ERR_MSG(extack, "cannot execute pre-load sequence"); 558 goto error; 559 } 560 561 /* Download utility image to device memory */ 562 rc = zl3073x_flash_download(zldev, "utility", 0x20000000, util_ptr, 563 util_size, extack); 564 if (rc) { 565 ZL_FLASH_ERR_MSG(extack, "cannot download flash utility"); 566 goto error; 567 } 568 569 /* Execute post-load sequence */ 570 rc = zl3073x_write_hwreg_seq(zldev, post_seq, ARRAY_SIZE(post_seq)); 571 if (rc) { 572 ZL_FLASH_ERR_MSG(extack, "cannot execute post-load sequence"); 573 goto error; 574 } 575 576 /* Check that utility identifies itself correctly */ 577 rc = zl3073x_flash_mode_verify(zldev); 578 if (rc) { 579 ZL_FLASH_ERR_MSG(extack, "flash utility check failed"); 580 goto error; 581 } 582 583 /* Enable host control */ 584 rc = zl3073x_flash_host_ctrl_enable(zldev); 585 if (rc) { 586 ZL_FLASH_ERR_MSG(extack, "cannot enable host control"); 587 goto error; 588 } 589 590 zl3073x_devlink_flash_notify(zldev, "Flash mode enabled", "utility", 591 0, 0); 592 593 return 0; 594 595 error: 596 zl3073x_flash_mode_leave(zldev, extack); 597 598 return rc; 599 } 600 601 /** 602 * zl3073x_flash_mode_leave - Leave flash mode 603 * @zldev: zl3073x device structure 604 * @extack: netlink extack pointer to report errors 605 * 606 * The function instructs the device to leave the flash mode and 607 * to return back to normal operation. 608 * 609 * The procedure: 610 * 1) Set reset flag 611 * 2) Reset the device CPU by specific HW register sequence 612 * 3) Wait for the device to be ready 613 * 4) Check the reset flag was cleared 614 * 615 * Return: 0 on success, <0 on error 616 */ 617 int zl3073x_flash_mode_leave(struct zl3073x_dev *zldev, 618 struct netlink_ext_ack *extack) 619 { 620 /* Sequence to be written after flash */ 621 static const struct zl3073x_hwreg_seq_item fw_reset_seq[] = { 622 HWREG_SEQ_ITEM(0x80000404, 1, BIT(0), 0), 623 HWREG_SEQ_ITEM(0x80000410, 1, BIT(0), 0), 624 }; 625 u8 reset_status; 626 int rc; 627 628 zl3073x_devlink_flash_notify(zldev, "Leaving flash mode", "utility", 629 0, 0); 630 631 /* Read reset status register */ 632 rc = zl3073x_read_u8(zldev, ZL_REG_RESET_STATUS, &reset_status); 633 if (rc) 634 return rc; 635 636 /* Set reset bit */ 637 reset_status |= ZL_REG_RESET_STATUS_RESET; 638 639 /* Update reset status register */ 640 rc = zl3073x_write_u8(zldev, ZL_REG_RESET_STATUS, reset_status); 641 if (rc) 642 return rc; 643 644 /* We do not check the return value here as the sequence resets 645 * the device CPU and the last write always return an error. 646 */ 647 zl3073x_write_hwreg_seq(zldev, fw_reset_seq, ARRAY_SIZE(fw_reset_seq)); 648 649 /* Wait for the device to be ready */ 650 msleep(500); 651 652 /* Read again the reset status register */ 653 rc = zl3073x_read_u8(zldev, ZL_REG_RESET_STATUS, &reset_status); 654 if (rc) 655 return rc; 656 657 /* Check the reset bit was cleared */ 658 if (reset_status & ZL_REG_RESET_STATUS_RESET) { 659 dev_err(zldev->dev, 660 "Reset not confirmed after switch to normal mode\n"); 661 return -EINVAL; 662 } 663 664 return 0; 665 } 666