Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | /* * Copyright (c) 2020 Nordic Semiconductor ASA * * SPDX-License-Identifier: Apache-2.0 */ #include <drivers/flash.h> #include <drivers/spi.h> #include <sys/byteorder.h> #include <logging/log.h> LOG_MODULE_REGISTER(spi_flash_at45, CONFIG_FLASH_LOG_LEVEL); /* AT45 commands used by this driver: */ /* - Continuous Array Read (Low Power Mode) */ #define CMD_READ 0x01 /* - Main Memory Byte/Page Program through Buffer 1 without Built-In Erase */ #define CMD_WRITE 0x02 /* - Read-Modify-Write */ #define CMD_MODIFY 0x58 /* - Manufacturer and Device ID Read */ #define CMD_READ_ID 0x9F /* - Status Register Read */ #define CMD_READ_STATUS 0xD7 /* - Chip Erase */ #define CMD_CHIP_ERASE { 0xC7, 0x94, 0x80, 0x9A } /* - Sector Erase */ #define CMD_SECTOR_ERASE 0x7C /* - Block Erase */ #define CMD_BLOCK_ERASE 0x50 /* - Page Erase */ #define CMD_PAGE_ERASE 0x81 /* - Deep Power-Down */ #define CMD_ENTER_DPD 0xB9 /* - Resume from Deep Power-Down */ #define CMD_EXIT_DPD 0xAB /* - Ultra-Deep Power-Down */ #define CMD_ENTER_UDPD 0x79 /* - Buffer and Page Size Configuration, "Power of 2" binary page size */ #define CMD_BINARY_PAGE_SIZE { 0x3D, 0x2A, 0x80, 0xA6 } #define STATUS_REG_LSB_RDY_BUSY_BIT 0x80 #define STATUS_REG_LSB_PAGE_SIZE_BIT 0x01 #define DEF_BUF_SET(_name, _buf_array) \ const struct spi_buf_set _name = { \ .buffers = _buf_array, \ .count = ARRAY_SIZE(_buf_array), \ } struct spi_flash_at45_data { const struct device *spi; struct spi_cs_control spi_cs; struct k_sem lock; #if IS_ENABLED(CONFIG_DEVICE_POWER_MANAGEMENT) uint32_t pm_state; #endif }; struct spi_flash_at45_config { const char *spi_bus; struct spi_config spi_cfg; const char *cs_gpio; gpio_pin_t cs_pin; gpio_dt_flags_t cs_dt_flags; #if IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT) struct flash_pages_layout pages_layout; #endif uint32_t chip_size; uint32_t sector_size; uint16_t block_size; uint16_t page_size; uint16_t t_enter_dpd; /* in microseconds */ uint16_t t_exit_dpd; /* in microseconds */ bool use_udpd; uint8_t jedec_id[3]; }; static const struct flash_parameters flash_at45_parameters = { .write_block_size = 1, .erase_value = 0xff, }; static struct spi_flash_at45_data *get_dev_data(const struct device *dev) { return dev->data; } static const struct spi_flash_at45_config *get_dev_config(const struct device *dev) { return dev->config; } static void acquire(const struct device *dev) { k_sem_take(&get_dev_data(dev)->lock, K_FOREVER); } static void release(const struct device *dev) { k_sem_give(&get_dev_data(dev)->lock); } static int check_jedec_id(const struct device *dev) { const struct spi_flash_at45_config *cfg = get_dev_config(dev); int err; uint8_t const *expected_id = cfg->jedec_id; uint8_t read_id[sizeof(cfg->jedec_id)]; const uint8_t opcode = CMD_READ_ID; const struct spi_buf tx_buf[] = { { .buf = (void *)&opcode, .len = sizeof(opcode), } }; const struct spi_buf rx_buf[] = { { .len = sizeof(opcode), }, { .buf = read_id, .len = sizeof(read_id), } }; DEF_BUF_SET(tx_buf_set, tx_buf); DEF_BUF_SET(rx_buf_set, rx_buf); err = spi_transceive(get_dev_data(dev)->spi, &cfg->spi_cfg, &tx_buf_set, &rx_buf_set); if (err != 0) { LOG_ERR("SPI transaction failed with code: %d/%u", err, __LINE__); return -EIO; } if (memcmp(expected_id, read_id, sizeof(read_id)) != 0) { LOG_ERR("Wrong JEDEC ID: %02X %02X %02X, " "expected: %02X %02X %02X", read_id[0], read_id[1], read_id[2], expected_id[0], expected_id[1], expected_id[2]); return -ENODEV; } return 0; } /* * Reads 2-byte Status Register: * - Byte 0 to LSB * - Byte 1 to MSB * of the pointed parameter. */ static int read_status_register(const struct device *dev, uint16_t *status) { int err; const uint8_t opcode = CMD_READ_STATUS; const struct spi_buf tx_buf[] = { { .buf = (void *)&opcode, .len = sizeof(opcode), } }; const struct spi_buf rx_buf[] = { { .len = sizeof(opcode), }, { .buf = status, .len = sizeof(uint16_t), } }; DEF_BUF_SET(tx_buf_set, tx_buf); DEF_BUF_SET(rx_buf_set, rx_buf); err = spi_transceive(get_dev_data(dev)->spi, &get_dev_config(dev)->spi_cfg, &tx_buf_set, &rx_buf_set); if (err != 0) { LOG_ERR("SPI transaction failed with code: %d/%u", err, __LINE__); return -EIO; } *status = sys_le16_to_cpu(*status); return 0; } static int wait_until_ready(const struct device *dev) { int err; uint16_t status; do { err = read_status_register(dev, &status); } while (err == 0 && !(status & STATUS_REG_LSB_RDY_BUSY_BIT)); return err; } static int configure_page_size(const struct device *dev) { int err; uint16_t status; uint8_t const conf_binary_page_size[] = CMD_BINARY_PAGE_SIZE; const struct spi_buf tx_buf[] = { { .buf = (void *)conf_binary_page_size, .len = sizeof(conf_binary_page_size), } }; DEF_BUF_SET(tx_buf_set, tx_buf); err = read_status_register(dev, &status); if (err != 0) { return err; } /* If the device is already configured for "power of 2" binary * page size, there is nothing more to do. */ if (status & STATUS_REG_LSB_PAGE_SIZE_BIT) { return 0; } err = spi_write(get_dev_data(dev)->spi, &get_dev_config(dev)->spi_cfg, &tx_buf_set); if (err != 0) { LOG_ERR("SPI transaction failed with code: %d/%u", err, __LINE__); } else { err = wait_until_ready(dev); } return (err != 0) ? -EIO : 0; } static bool is_valid_request(off_t addr, size_t size, size_t chip_size) { return (addr >= 0 && (addr + size) <= chip_size); } static int spi_flash_at45_read(const struct device *dev, off_t offset, void *data, size_t len) { const struct spi_flash_at45_config *cfg = get_dev_config(dev); int err; if (!is_valid_request(offset, len, cfg->chip_size)) { return -ENODEV; } uint8_t const op_and_addr[] = { CMD_READ, (offset >> 16) & 0xFF, (offset >> 8) & 0xFF, (offset >> 0) & 0xFF, }; const struct spi_buf tx_buf[] = { { .buf = (void *)&op_and_addr, .len = sizeof(op_and_addr), } }; const struct spi_buf rx_buf[] = { { .len = sizeof(op_and_addr), }, { .buf = data, .len = len, } }; DEF_BUF_SET(tx_buf_set, tx_buf); DEF_BUF_SET(rx_buf_set, rx_buf); acquire(dev); err = spi_transceive(get_dev_data(dev)->spi, &cfg->spi_cfg, &tx_buf_set, &rx_buf_set); release(dev); if (err != 0) { LOG_ERR("SPI transaction failed with code: %d/%u", err, __LINE__); } return (err != 0) ? -EIO : 0; } static int perform_write(const struct device *dev, off_t offset, const void *data, size_t len) { int err; uint8_t const op_and_addr[] = { IS_ENABLED(CONFIG_SPI_FLASH_AT45_USE_READ_MODIFY_WRITE) ? CMD_MODIFY : CMD_WRITE, (offset >> 16) & 0xFF, (offset >> 8) & 0xFF, (offset >> 0) & 0xFF, }; const struct spi_buf tx_buf[] = { { .buf = (void *)&op_and_addr, .len = sizeof(op_and_addr), }, { .buf = (void *)data, .len = len, } }; DEF_BUF_SET(tx_buf_set, tx_buf); err = spi_write(get_dev_data(dev)->spi, &get_dev_config(dev)->spi_cfg, &tx_buf_set); if (err != 0) { LOG_ERR("SPI transaction failed with code: %d/%u", err, __LINE__); } else { err = wait_until_ready(dev); } return (err != 0) ? -EIO : 0; } static int spi_flash_at45_write(const struct device *dev, off_t offset, const void *data, size_t len) { const struct spi_flash_at45_config *cfg = get_dev_config(dev); int err = 0; if (!is_valid_request(offset, len, cfg->chip_size)) { return -ENODEV; } acquire(dev); while (len) { size_t chunk_len = len; off_t current_page_start = offset - (offset & (cfg->page_size - 1)); off_t current_page_end = current_page_start + cfg->page_size; if (chunk_len > (current_page_end - offset)) { chunk_len = (current_page_end - offset); } err = perform_write(dev, offset, data, chunk_len); if (err != 0) { break; } data = (uint8_t *)data + chunk_len; offset += chunk_len; len -= chunk_len; } release(dev); return err; } static int perform_chip_erase(const struct device *dev) { int err; uint8_t const chip_erase_cmd[] = CMD_CHIP_ERASE; const struct spi_buf tx_buf[] = { { .buf = (void *)&chip_erase_cmd, .len = sizeof(chip_erase_cmd), } }; DEF_BUF_SET(tx_buf_set, tx_buf); err = spi_write(get_dev_data(dev)->spi, &get_dev_config(dev)->spi_cfg, &tx_buf_set); if (err != 0) { LOG_ERR("SPI transaction failed with code: %d/%u", err, __LINE__); } else { err = wait_until_ready(dev); } return (err != 0) ? -EIO : 0; } static bool is_erase_possible(size_t entity_size, off_t offset, size_t requested_size) { return (requested_size >= entity_size && (offset & (entity_size - 1)) == 0); } static int perform_erase_op(const struct device *dev, uint8_t opcode, off_t offset) { int err; uint8_t const op_and_addr[] = { opcode, (offset >> 16) & 0xFF, (offset >> 8) & 0xFF, (offset >> 0) & 0xFF, }; const struct spi_buf tx_buf[] = { { .buf = (void *)&op_and_addr, .len = sizeof(op_and_addr), } }; DEF_BUF_SET(tx_buf_set, tx_buf); err = spi_write(get_dev_data(dev)->spi, &get_dev_config(dev)->spi_cfg, &tx_buf_set); if (err != 0) { LOG_ERR("SPI transaction failed with code: %d/%u", err, __LINE__); } else { err = wait_until_ready(dev); } return (err != 0) ? -EIO : 0; } static int spi_flash_at45_erase(const struct device *dev, off_t offset, size_t size) { const struct spi_flash_at45_config *cfg = get_dev_config(dev); int err = 0; if (!is_valid_request(offset, size, cfg->chip_size)) { return -ENODEV; } /* Diagnose region errors before starting to erase. */ if (((offset % cfg->page_size) != 0) || ((size % cfg->page_size) != 0)) { return -EINVAL; } acquire(dev); if (size == cfg->chip_size) { err = perform_chip_erase(dev); } else { while (size) { if (is_erase_possible(cfg->sector_size, offset, size)) { err = perform_erase_op(dev, CMD_SECTOR_ERASE, offset); offset += cfg->sector_size; size -= cfg->sector_size; } else if (is_erase_possible(cfg->block_size, offset, size)) { err = perform_erase_op(dev, CMD_BLOCK_ERASE, offset); offset += cfg->block_size; size -= cfg->block_size; } else if (is_erase_possible(cfg->page_size, offset, size)) { err = perform_erase_op(dev, CMD_PAGE_ERASE, offset); offset += cfg->page_size; size -= cfg->page_size; } else { LOG_ERR("Unsupported erase request: " "size %zu at 0x%lx", size, (long)offset); err = -EINVAL; } if (err != 0) { break; } } } release(dev); return err; } static int spi_flash_at45_write_protection(const struct device *dev, bool enable) { ARG_UNUSED(dev); ARG_UNUSED(enable); /* The Sector Protection mechanism that is available in AT45 family * chips is more complex than what is exposed by the the flash API * (particular sectors need to be earlier configured in a write to * the nonvolatile Sector Protection Register), so it is not feasible * to try to use it here. Since the protection is not automatically * enabled after the device is power cycled, there is nothing needed * to be done in this function. */ return 0; } #if IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT) static void spi_flash_at45_pages_layout(const struct device *dev, const struct flash_pages_layout **layout, size_t *layout_size) { *layout = &get_dev_config(dev)->pages_layout; *layout_size = 1; } #endif /* IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT) */ static int power_down_op(const struct device *dev, uint8_t opcode, uint32_t delay) { int err = 0; const struct spi_buf tx_buf[] = { { .buf = (void *)&opcode, .len = sizeof(opcode), } }; DEF_BUF_SET(tx_buf_set, tx_buf); err = spi_write(get_dev_data(dev)->spi, &get_dev_config(dev)->spi_cfg, &tx_buf_set); if (err != 0) { LOG_ERR("SPI transaction failed with code: %d/%u", err, __LINE__); return -EIO; } k_busy_wait(delay); return 0; } static int spi_flash_at45_init(const struct device *dev) { struct spi_flash_at45_data *dev_data = get_dev_data(dev); const struct spi_flash_at45_config *dev_config = get_dev_config(dev); int err; dev_data->spi = device_get_binding(dev_config->spi_bus); if (!dev_data->spi) { LOG_ERR("Cannot find %s", dev_config->spi_bus); return -ENODEV; } if (dev_config->cs_gpio) { dev_data->spi_cs.gpio_dev = device_get_binding(dev_config->cs_gpio); if (!dev_data->spi_cs.gpio_dev) { LOG_ERR("Cannot find %s", dev_config->cs_gpio); return -ENODEV; } dev_data->spi_cs.gpio_pin = dev_config->cs_pin; dev_data->spi_cs.gpio_dt_flags = dev_config->cs_dt_flags; dev_data->spi_cs.delay = 0; } acquire(dev); /* Just in case the chip was in the Deep (or Ultra-Deep) Power-Down * mode, issue the command to bring it back to normal operation. * Exiting from the Ultra-Deep mode requires only that the CS line * is asserted for a certain time, so issuing the Resume from Deep * Power-Down command will work in both cases. */ power_down_op(dev, CMD_EXIT_DPD, dev_config->t_exit_dpd); err = check_jedec_id(dev); if (err == 0) { err = configure_page_size(dev); } release(dev); return err; } #if IS_ENABLED(CONFIG_DEVICE_POWER_MANAGEMENT) static int spi_flash_at45_pm_control(const struct device *dev, uint32_t ctrl_command, void *context, device_pm_cb cb, void *arg) { struct spi_flash_at45_data *dev_data = get_dev_data(dev); const struct spi_flash_at45_config *dev_config = get_dev_config(dev); int err = 0; if (ctrl_command == DEVICE_PM_SET_POWER_STATE) { uint32_t new_state = *((const uint32_t *)context); if (new_state != dev_data->pm_state) { switch (new_state) { case DEVICE_PM_ACTIVE_STATE: acquire(dev); power_down_op(dev, CMD_EXIT_DPD, dev_config->t_exit_dpd); release(dev); break; case DEVICE_PM_LOW_POWER_STATE: case DEVICE_PM_SUSPEND_STATE: case DEVICE_PM_OFF_STATE: acquire(dev); power_down_op(dev, dev_config->use_udpd ? CMD_ENTER_UDPD : CMD_ENTER_DPD, dev_config->t_enter_dpd); release(dev); break; default: return -ENOTSUP; } dev_data->pm_state = new_state; } } else { __ASSERT_NO_MSG(ctrl_command == DEVICE_PM_GET_POWER_STATE); *((uint32_t *)context) = dev_data->pm_state; } if (cb) { cb(dev, err, context, arg); } return err; } #endif /* IS_ENABLED(CONFIG_DEVICE_POWER_MANAGEMENT) */ static const struct flash_parameters * flash_at45_get_parameters(const struct device *dev) { ARG_UNUSED(dev); return &flash_at45_parameters; } static const struct flash_driver_api spi_flash_at45_api = { .read = spi_flash_at45_read, .write = spi_flash_at45_write, .erase = spi_flash_at45_erase, .write_protection = spi_flash_at45_write_protection, .get_parameters = flash_at45_get_parameters, #if IS_ENABLED(CONFIG_FLASH_PAGE_LAYOUT) .page_layout = spi_flash_at45_pages_layout, #endif }; #define DT_DRV_COMPAT atmel_at45 #define SPI_FLASH_AT45_INST(idx) \ enum { \ INST_##idx##_BYTES = (DT_INST_PROP(idx, size) / 8), \ INST_##idx##_PAGES = (INST_##idx##_BYTES / \ DT_INST_PROP(idx, page_size)), \ }; \ static struct spi_flash_at45_data inst_##idx##_data = { \ .lock = Z_SEM_INITIALIZER(inst_##idx##_data.lock, 1, 1), \ IF_ENABLED(CONFIG_DEVICE_POWER_MANAGEMENT, ( \ .pm_state = DEVICE_PM_ACTIVE_STATE)) \ }; \ static const struct spi_flash_at45_config inst_##idx##_config = { \ .spi_bus = DT_INST_BUS_LABEL(idx), \ .spi_cfg = { \ .frequency = DT_INST_PROP(idx, spi_max_frequency), \ .operation = SPI_OP_MODE_MASTER | SPI_TRANSFER_MSB | \ SPI_WORD_SET(8) | SPI_LINES_SINGLE, \ .slave = DT_INST_REG_ADDR(idx), \ .cs = &inst_##idx##_data.spi_cs, \ }, \ IF_ENABLED(DT_INST_SPI_DEV_HAS_CS_GPIOS(idx), ( \ .cs_gpio = DT_INST_SPI_DEV_CS_GPIOS_LABEL(idx), \ .cs_pin = DT_INST_SPI_DEV_CS_GPIOS_PIN(idx), \ .cs_dt_flags = DT_INST_SPI_DEV_CS_GPIOS_FLAGS(idx),)) \ IF_ENABLED(CONFIG_FLASH_PAGE_LAYOUT, ( \ .pages_layout = { \ .pages_count = INST_##idx##_PAGES, \ .pages_size = DT_INST_PROP(idx, page_size), \ },)) \ .chip_size = INST_##idx##_BYTES, \ .sector_size = DT_INST_PROP(idx, sector_size), \ .block_size = DT_INST_PROP(idx, block_size), \ .page_size = DT_INST_PROP(idx, page_size), \ .t_enter_dpd = ceiling_fraction( \ DT_INST_PROP(idx, enter_dpd_delay), \ NSEC_PER_USEC), \ .t_exit_dpd = ceiling_fraction( \ DT_INST_PROP(idx, exit_dpd_delay), \ NSEC_PER_USEC), \ .use_udpd = DT_INST_PROP(idx, use_udpd), \ .jedec_id = DT_INST_PROP(idx, jedec_id), \ }; \ IF_ENABLED(CONFIG_FLASH_PAGE_LAYOUT, ( \ BUILD_ASSERT( \ (INST_##idx##_PAGES * DT_INST_PROP(idx, page_size)) \ == INST_##idx##_BYTES, \ "Page size specified for instance " #idx " of " \ "atmel,at45 is not compatible with its " \ "total size");)) \ DEVICE_DEFINE(inst_##idx, DT_INST_LABEL(idx), \ spi_flash_at45_init, spi_flash_at45_pm_control, \ &inst_##idx##_data, &inst_##idx##_config, \ POST_KERNEL, CONFIG_SPI_FLASH_AT45_INIT_PRIORITY, \ &spi_flash_at45_api); DT_INST_FOREACH_STATUS_OKAY(SPI_FLASH_AT45_INST) |