Lines Matching +full:fast +full:- +full:read
1 // SPDX-License-Identifier: GPL-2.0
10 #include <linux/mtd/spi-nor.h>
14 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
16 (((p)->parameter_table_pointer[2] << 16) | \
17 ((p)->parameter_table_pointer[1] << 8) | \
18 ((p)->parameter_table_pointer[0] << 0))
19 #define SFDP_PARAM_HEADER_PARAM_LEN(p) ((p)->length * 4)
23 #define SFDP_4BAIT_ID 0xff84 /* 4-byte Address Instruction Table */
36 u8 nph; /* 0-base number of parameter headers */
43 /* Fast Read settings. */
45 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
50 * whether the Fast Read x-y-z command is supported.
56 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
58 * states to be used by Fast Read x-y-z command.
63 /* The SPI protocol for this Fast Read x-y-z command. */
69 * The half-word at offset <shift> in DWORD <dword> encodes the
130 * the associated 4-byte address op code is supported.
136 * spi_nor_read_raw() - raw read of serial flash memory. read_opcode,
142 * @len: number of bytes to read
143 * @buf: buffer where the data is copied into (dma-safe memory)
145 * Return: 0 on success, -errno otherwise.
156 return -EIO; in spi_nor_read_raw()
160 len -= ret; in spi_nor_read_raw()
166 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
169 * @len: number of bytes to read
170 * @buf: buffer where the SFDP data are copied into (dma-safe memory)
173 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
174 * followed by a 3-byte address and 8 dummy clock cycles.
176 * Return: 0 on success, -errno otherwise.
184 read_opcode = nor->read_opcode; in spi_nor_read_sfdp()
185 addr_nbytes = nor->addr_nbytes; in spi_nor_read_sfdp()
186 read_dummy = nor->read_dummy; in spi_nor_read_sfdp()
188 nor->read_opcode = SPINOR_OP_RDSFDP; in spi_nor_read_sfdp()
189 nor->addr_nbytes = 3; in spi_nor_read_sfdp()
190 nor->read_dummy = 8; in spi_nor_read_sfdp()
194 nor->read_opcode = read_opcode; in spi_nor_read_sfdp()
195 nor->addr_nbytes = addr_nbytes; in spi_nor_read_sfdp()
196 nor->read_dummy = read_dummy; in spi_nor_read_sfdp()
202 * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
205 * @len: number of bytes to read
209 * guaranteed to be dma-safe.
211 * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
222 return -ENOMEM; in spi_nor_read_sfdp_dma_unsafe()
232 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read, in spi_nor_set_read_settings_from_bfpt() argument
236 read->num_mode_clocks = (half >> 5) & 0x07; in spi_nor_set_read_settings_from_bfpt()
237 read->num_wait_states = (half >> 0) & 0x1f; in spi_nor_set_read_settings_from_bfpt()
238 read->opcode = (half >> 8) & 0xff; in spi_nor_set_read_settings_from_bfpt()
239 read->proto = proto; in spi_nor_set_read_settings_from_bfpt()
243 /* Fast Read 1-1-2 */
251 /* Fast Read 1-2-2 */
259 /* Fast Read 2-2-2 */
267 /* Fast Read 1-1-4 */
275 /* Fast Read 1-4-4 */
283 /* Fast Read 4-4-4 */
307 * spi_nor_set_erase_settings_from_bfpt() - set erase type settings from BFPT
323 erase->idx = i; in spi_nor_set_erase_settings_from_bfpt()
328 * spi_nor_map_cmp_erase_type() - compare the map's erase types by size
336 * Return: the result of @l->size - @r->size
342 return left->size - right->size; in spi_nor_map_cmp_erase_type()
346 * spi_nor_sort_erase_mask() - sort erase mask
358 struct spi_nor_erase_type *erase_type = map->erase_type; in spi_nor_sort_erase_mask()
374 * spi_nor_regions_sort_erase_types() - sort erase types in each region
387 struct spi_nor_erase_region *region = map->regions; in spi_nor_regions_sort_erase_types()
391 region_erase_mask = region->offset & SNOR_ERASE_TYPE_MASK; in spi_nor_regions_sort_erase_types()
397 region->offset = (region->offset & ~SNOR_ERASE_TYPE_MASK) | in spi_nor_regions_sort_erase_types()
405 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
413 * the number of address bytes for Fast Read, Page Program and Sector Erase
415 * For Fast READ commands, it also gives the number of mode clock cycles and
430 * Return: 0 on success, -errno otherwise.
435 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_parse_bfpt()
436 struct spi_nor_erase_map *map = ¶ms->erase_map; in spi_nor_parse_bfpt()
437 struct spi_nor_erase_type *erase_type = map->erase_type; in spi_nor_parse_bfpt()
446 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216) in spi_nor_parse_bfpt()
447 return -EINVAL; in spi_nor_parse_bfpt()
449 /* Read the Basic Flash Parameter Table. */ in spi_nor_parse_bfpt()
451 bfpt_header->length * sizeof(u32)); in spi_nor_parse_bfpt()
465 params->addr_nbytes = 3; in spi_nor_parse_bfpt()
466 params->addr_mode_nbytes = 3; in spi_nor_parse_bfpt()
470 params->addr_nbytes = 4; in spi_nor_parse_bfpt()
471 params->addr_mode_nbytes = 4; in spi_nor_parse_bfpt()
484 * Prevent overflows on params->size. Anyway, a NOR of 2^64 in spi_nor_parse_bfpt()
489 return -EINVAL; in spi_nor_parse_bfpt()
491 params->size = 1ULL << val; in spi_nor_parse_bfpt()
493 params->size = val + 1; in spi_nor_parse_bfpt()
495 params->size >>= 3; /* Convert to bytes. */ in spi_nor_parse_bfpt()
497 /* Fast Read settings. */ in spi_nor_parse_bfpt()
500 struct spi_nor_read_command *read; in spi_nor_parse_bfpt() local
502 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) { in spi_nor_parse_bfpt()
503 params->hwcaps.mask &= ~rd->hwcaps; in spi_nor_parse_bfpt()
507 params->hwcaps.mask |= rd->hwcaps; in spi_nor_parse_bfpt()
508 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps); in spi_nor_parse_bfpt()
509 read = ¶ms->reads[cmd]; in spi_nor_parse_bfpt()
510 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift; in spi_nor_parse_bfpt()
511 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto); in spi_nor_parse_bfpt()
519 memset(¶ms->erase_map, 0, sizeof(params->erase_map)); in spi_nor_parse_bfpt()
525 half = bfpt.dwords[er->dword] >> er->shift; in spi_nor_parse_bfpt()
538 spi_nor_init_uniform_erase_map(map, erase_mask, params->size); in spi_nor_parse_bfpt()
551 map->uniform_erase_type = map->uniform_region.offset & in spi_nor_parse_bfpt()
555 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216) in spi_nor_parse_bfpt()
562 params->page_size = 1U << val; in spi_nor_parse_bfpt()
567 params->quad_enable = NULL; in spi_nor_parse_bfpt()
573 * side-effect of clearing Status Register 2. in spi_nor_parse_bfpt()
577 * Read Configuration Register (35h) instruction is not in spi_nor_parse_bfpt()
580 nor->flags |= SNOR_F_HAS_16BIT_SR | SNOR_F_NO_READ_CR; in spi_nor_parse_bfpt()
581 params->quad_enable = spi_nor_sr2_bit1_quad_enable; in spi_nor_parse_bfpt()
585 nor->flags &= ~SNOR_F_HAS_16BIT_SR; in spi_nor_parse_bfpt()
586 params->quad_enable = spi_nor_sr1_bit6_quad_enable; in spi_nor_parse_bfpt()
590 nor->flags &= ~SNOR_F_HAS_16BIT_SR; in spi_nor_parse_bfpt()
591 params->quad_enable = spi_nor_sr2_bit7_quad_enable; in spi_nor_parse_bfpt()
599 * assumption of a 16-bit Write Status (01h) command. in spi_nor_parse_bfpt()
601 nor->flags |= SNOR_F_HAS_16BIT_SR; in spi_nor_parse_bfpt()
603 params->quad_enable = spi_nor_sr2_bit1_quad_enable; in spi_nor_parse_bfpt()
607 dev_dbg(nor->dev, "BFPT QER reserved value used\n"); in spi_nor_parse_bfpt()
613 nor->flags |= SNOR_F_SOFT_RESET; in spi_nor_parse_bfpt()
616 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216B) in spi_nor_parse_bfpt()
619 /* 8D-8D-8D command extension. */ in spi_nor_parse_bfpt()
622 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT; in spi_nor_parse_bfpt()
626 nor->cmd_ext_type = SPI_NOR_EXT_INVERT; in spi_nor_parse_bfpt()
630 dev_dbg(nor->dev, "Reserved command extension used\n"); in spi_nor_parse_bfpt()
634 dev_dbg(nor->dev, "16-bit opcodes not supported\n"); in spi_nor_parse_bfpt()
635 return -EOPNOTSUPP; in spi_nor_parse_bfpt()
642 * spi_nor_smpt_addr_nbytes() - return the number of address bytes used in the
658 return nor->params->addr_mode_nbytes; in spi_nor_smpt_addr_nbytes()
663 * spi_nor_smpt_read_dummy() - return the configuration detection command read
668 * Return: the number of dummy cycles for an SMPT read
675 return nor->read_dummy; in spi_nor_smpt_read_dummy()
680 * spi_nor_get_map_in_use() - get the configuration map in use
685 * Return: pointer to the map in use, ERR_PTR(-errno) otherwise.
698 /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */ in spi_nor_get_map_in_use()
701 return ERR_PTR(-ENOMEM); in spi_nor_get_map_in_use()
703 addr_nbytes = nor->addr_nbytes; in spi_nor_get_map_in_use()
704 read_dummy = nor->read_dummy; in spi_nor_get_map_in_use()
705 read_opcode = nor->read_opcode; in spi_nor_get_map_in_use()
714 nor->addr_nbytes = spi_nor_smpt_addr_nbytes(nor, smpt[i]); in spi_nor_get_map_in_use()
715 nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]); in spi_nor_get_map_in_use()
716 nor->read_opcode = SMPT_CMD_OPCODE(smpt[i]); in spi_nor_get_map_in_use()
739 ret = ERR_PTR(-EINVAL); in spi_nor_get_map_in_use()
761 nor->addr_nbytes = addr_nbytes; in spi_nor_get_map_in_use()
762 nor->read_dummy = read_dummy; in spi_nor_get_map_in_use()
763 nor->read_opcode = read_opcode; in spi_nor_get_map_in_use()
769 region->offset |= SNOR_LAST_REGION; in spi_nor_region_mark_end()
774 region->offset |= SNOR_OVERLAID_REGION; in spi_nor_region_mark_overlay()
778 * spi_nor_region_check_overlay() - set overlay bit when the region is overlaid
793 if (region->size & erase[i].size_mask) { in spi_nor_region_check_overlay()
801 * spi_nor_init_non_uniform_erase_map() - initialize the non-uniform erase map
805 * Return: 0 on success, -errno otherwise.
810 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_init_non_uniform_erase_map()
811 struct spi_nor_erase_type *erase = map->erase_type; in spi_nor_init_non_uniform_erase_map()
824 region = devm_kcalloc(nor->dev, region_count, sizeof(*region), in spi_nor_init_non_uniform_erase_map()
827 return -ENOMEM; in spi_nor_init_non_uniform_erase_map()
828 map->regions = region; in spi_nor_init_non_uniform_erase_map()
857 spi_nor_region_mark_end(®ion[i - 1]); in spi_nor_init_non_uniform_erase_map()
859 save_uniform_erase_type = map->uniform_erase_type; in spi_nor_init_non_uniform_erase_map()
860 map->uniform_erase_type = spi_nor_sort_erase_mask(map, in spi_nor_init_non_uniform_erase_map()
868 map->uniform_erase_type = save_uniform_erase_type; in spi_nor_init_non_uniform_erase_map()
869 return -EINVAL; in spi_nor_init_non_uniform_erase_map()
885 * spi_nor_parse_smpt() - parse Sector Map Parameter Table
893 * Return: 0 on success, -errno otherwise.
904 /* Read the Sector Map Parameter Table. */ in spi_nor_parse_smpt()
905 len = smpt_header->length * sizeof(*smpt); in spi_nor_parse_smpt()
908 return -ENOMEM; in spi_nor_parse_smpt()
916 le32_to_cpu_array(smpt, smpt_header->length); in spi_nor_parse_smpt()
918 sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length); in spi_nor_parse_smpt()
928 spi_nor_regions_sort_erase_types(&nor->params->erase_map); in spi_nor_parse_smpt()
936 * spi_nor_parse_4bait() - parse the 4-Byte Address Instruction Table
939 * the 4-Byte Address Instruction Table length and version.
941 * Return: 0 on success, -errno otherwise.
968 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_parse_4bait()
969 struct spi_nor_pp_command *params_pp = params->page_programs; in spi_nor_parse_4bait()
970 struct spi_nor_erase_map *map = ¶ms->erase_map; in spi_nor_parse_4bait()
971 struct spi_nor_erase_type *erase_type = map->erase_type; in spi_nor_parse_4bait()
977 if (param_header->major != SFDP_JESD216_MAJOR || in spi_nor_parse_4bait()
978 param_header->length < SFDP_4BAIT_DWORD_MAX) in spi_nor_parse_4bait()
979 return -EINVAL; in spi_nor_parse_4bait()
981 /* Read the 4-byte Address Instruction Table. */ in spi_nor_parse_4bait()
984 /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */ in spi_nor_parse_4bait()
987 return -ENOMEM; in spi_nor_parse_4bait()
998 * Compute the subset of (Fast) Read commands for which the 4-byte in spi_nor_parse_4bait()
1004 const struct sfdp_4bait *read = &reads[i]; in spi_nor_parse_4bait() local
1006 discard_hwcaps |= read->hwcaps; in spi_nor_parse_4bait()
1007 if ((params->hwcaps.mask & read->hwcaps) && in spi_nor_parse_4bait()
1008 (dwords[0] & read->supported_bit)) in spi_nor_parse_4bait()
1009 read_hwcaps |= read->hwcaps; in spi_nor_parse_4bait()
1013 * Compute the subset of Page Program commands for which the 4-byte in spi_nor_parse_4bait()
1023 * Bypass the params->hwcaps.mask and consider 4BAIT the biggest in spi_nor_parse_4bait()
1026 discard_hwcaps |= program->hwcaps; in spi_nor_parse_4bait()
1027 if (dwords[0] & program->supported_bit) in spi_nor_parse_4bait()
1028 pp_hwcaps |= program->hwcaps; in spi_nor_parse_4bait()
1032 * Compute the subset of Sector Erase commands for which the 4-byte in spi_nor_parse_4bait()
1039 if (dwords[0] & erase->supported_bit) in spi_nor_parse_4bait()
1047 * We need at least one 4-byte op code per read, program and erase in spi_nor_parse_4bait()
1048 * operation; the .read(), .write() and .erase() hooks share the in spi_nor_parse_4bait()
1049 * nor->addr_nbytes value. in spi_nor_parse_4bait()
1055 * Discard all operations from the 4-byte instruction set which are in spi_nor_parse_4bait()
1058 params->hwcaps.mask &= ~discard_hwcaps; in spi_nor_parse_4bait()
1059 params->hwcaps.mask |= (read_hwcaps | pp_hwcaps); in spi_nor_parse_4bait()
1061 /* Use the 4-byte address instruction set. */ in spi_nor_parse_4bait()
1063 struct spi_nor_read_command *read_cmd = ¶ms->reads[i]; in spi_nor_parse_4bait()
1065 read_cmd->opcode = spi_nor_convert_3to4_read(read_cmd->opcode); in spi_nor_parse_4bait()
1103 params->addr_nbytes = 4; in spi_nor_parse_4bait()
1104 nor->flags |= SNOR_F_4B_OPCODES | SNOR_F_HAS_4BAIT; in spi_nor_parse_4bait()
1121 * spi_nor_parse_profile1() - parse the xSPI Profile 1.0 table
1126 * Return: 0 on success, -errno otherwise.
1136 len = profile1_header->length * sizeof(*dwords); in spi_nor_parse_profile1()
1139 return -ENOMEM; in spi_nor_parse_profile1()
1146 le32_to_cpu_array(dwords, profile1_header->length); in spi_nor_parse_profile1()
1148 /* Get 8D-8D-8D fast read opcode and dummy cycles. */ in spi_nor_parse_profile1()
1151 /* Set the Read Status Register dummy cycles and dummy address bytes. */ in spi_nor_parse_profile1()
1153 nor->params->rdsr_dummy = 8; in spi_nor_parse_profile1()
1155 nor->params->rdsr_dummy = 4; in spi_nor_parse_profile1()
1158 nor->params->rdsr_addr_nbytes = 4; in spi_nor_parse_profile1()
1160 nor->params->rdsr_addr_nbytes = 0; in spi_nor_parse_profile1()
1179 dev_dbg(nor->dev, in spi_nor_parse_profile1()
1185 /* Update the fast read settings. */ in spi_nor_parse_profile1()
1186 spi_nor_set_read_settings(&nor->params->reads[SNOR_CMD_READ_8_8_8_DTR], in spi_nor_parse_profile1()
1198 * spi_nor_parse_sccr() - Parse the Status, Control and Configuration Register
1204 * Return: 0 on success, -errno otherwise.
1213 len = sccr_header->length * sizeof(*dwords); in spi_nor_parse_sccr()
1216 return -ENOMEM; in spi_nor_parse_sccr()
1223 le32_to_cpu_array(dwords, sccr_header->length); in spi_nor_parse_sccr()
1226 nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE; in spi_nor_parse_sccr()
1234 * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
1244 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_post_sfdp_fixups()
1245 nor->manufacturer->fixups->post_sfdp) in spi_nor_post_sfdp_fixups()
1246 nor->manufacturer->fixups->post_sfdp(nor); in spi_nor_post_sfdp_fixups()
1248 if (nor->info->fixups && nor->info->fixups->post_sfdp) in spi_nor_post_sfdp_fixups()
1249 nor->info->fixups->post_sfdp(nor); in spi_nor_post_sfdp_fixups()
1253 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
1258 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
1260 * as Fast Read, Page Program or Sector Erase commands.
1262 * Return: 0 on success, -errno otherwise.
1269 struct device *dev = nor->dev; in spi_nor_parse_sfdp()
1283 return -EINVAL; in spi_nor_parse_sfdp()
1291 bfpt_header->major != SFDP_JESD216_MAJOR) in spi_nor_parse_sfdp()
1292 return -EINVAL; in spi_nor_parse_sfdp()
1298 * Allocate memory then read all parameter headers with a single in spi_nor_parse_sfdp()
1299 * Read SFDP command. These parameter headers will actually be parsed in spi_nor_parse_sfdp()
1303 * Hence we read the parameter headers once for all to reduce the in spi_nor_parse_sfdp()
1313 return -ENOMEM; in spi_nor_parse_sfdp()
1318 dev_dbg(dev, "failed to read SFDP parameter headers\n"); in spi_nor_parse_sfdp()
1346 err = -ENOMEM; in spi_nor_parse_sfdp()
1356 sfdp->num_dwords = DIV_ROUND_UP(sfdp_size, sizeof(*sfdp->dwords)); in spi_nor_parse_sfdp()
1357 sfdp->dwords = devm_kcalloc(dev, sfdp->num_dwords, in spi_nor_parse_sfdp()
1358 sizeof(*sfdp->dwords), GFP_KERNEL); in spi_nor_parse_sfdp()
1359 if (!sfdp->dwords) { in spi_nor_parse_sfdp()
1360 err = -ENOMEM; in spi_nor_parse_sfdp()
1365 err = spi_nor_read_sfdp(nor, 0, sfdp_size, sfdp->dwords); in spi_nor_parse_sfdp()
1367 dev_dbg(dev, "failed to read SFDP data\n"); in spi_nor_parse_sfdp()
1368 devm_kfree(dev, sfdp->dwords); in spi_nor_parse_sfdp()
1373 nor->sfdp = sfdp; in spi_nor_parse_sfdp()
1383 param_header->major == SFDP_JESD216_MAJOR && in spi_nor_parse_sfdp()
1384 (param_header->minor > bfpt_header->minor || in spi_nor_parse_sfdp()
1385 (param_header->minor == bfpt_header->minor && in spi_nor_parse_sfdp()
1386 param_header->length > bfpt_header->length))) in spi_nor_parse_sfdp()