Lines Matching +full:half +full:- +full:length

1 // SPDX-License-Identifier: GPL-2.0
8 #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 */
31 * Register Map Offsets for Multi-Chip
41 u8 nph; /* 0-base number of parameter headers */
50 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
55 * whether the Fast Read x-y-z command is supported.
61 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
63 * states to be used by Fast Read x-y-z command.
68 /* The SPI protocol for this Fast Read x-y-z command. */
74 * The half-word at offset <shift> in DWORD <dword> encodes the
135 * the associated 4-byte address op code is supported.
141 * spi_nor_read_raw() - raw read of serial flash memory. read_opcode,
147 * @buf: buffer where the data is copied into (dma-safe memory)
149 * Return: 0 on success, -errno otherwise.
160 return -EIO; in spi_nor_read_raw()
164 len -= ret; in spi_nor_read_raw()
170 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
174 * @buf: buffer where the SFDP data are copied into (dma-safe memory)
178 * followed by a 3-byte address and 8 dummy clock cycles.
180 * Return: 0 on success, -errno otherwise.
188 read_opcode = nor->read_opcode; in spi_nor_read_sfdp()
189 addr_nbytes = nor->addr_nbytes; in spi_nor_read_sfdp()
190 read_dummy = nor->read_dummy; in spi_nor_read_sfdp()
192 nor->read_opcode = SPINOR_OP_RDSFDP; in spi_nor_read_sfdp()
193 nor->addr_nbytes = 3; in spi_nor_read_sfdp()
194 nor->read_dummy = 8; in spi_nor_read_sfdp()
198 nor->read_opcode = read_opcode; in spi_nor_read_sfdp()
199 nor->addr_nbytes = addr_nbytes; in spi_nor_read_sfdp()
200 nor->read_dummy = read_dummy; in spi_nor_read_sfdp()
206 * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
213 * guaranteed to be dma-safe.
215 * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
226 return -ENOMEM; in spi_nor_read_sfdp_dma_unsafe()
237 u16 half, in spi_nor_set_read_settings_from_bfpt() argument
240 read->num_mode_clocks = (half >> 5) & 0x07; in spi_nor_set_read_settings_from_bfpt()
241 read->num_wait_states = (half >> 0) & 0x1f; in spi_nor_set_read_settings_from_bfpt()
242 read->opcode = (half >> 8) & 0xff; in spi_nor_set_read_settings_from_bfpt()
243 read->proto = proto; in spi_nor_set_read_settings_from_bfpt()
247 /* Fast Read 1-1-2 */
255 /* Fast Read 1-2-2 */
263 /* Fast Read 2-2-2 */
271 /* Fast Read 1-1-4 */
279 /* Fast Read 1-4-4 */
287 /* Fast Read 4-4-4 */
311 * spi_nor_set_erase_settings_from_bfpt() - set erase type settings from BFPT
327 erase->idx = i; in spi_nor_set_erase_settings_from_bfpt()
332 * spi_nor_map_cmp_erase_type() - compare the map's erase types by size
333 * @l: member in the left half of the map's erase_type array
334 * @r: member in the right half of the map's erase_type array
340 * Return: the result of @l->size - @r->size
346 return left->size - right->size; in spi_nor_map_cmp_erase_type()
350 * spi_nor_sort_erase_mask() - sort erase mask
362 struct spi_nor_erase_type *erase_type = map->erase_type; in spi_nor_sort_erase_mask()
378 * spi_nor_regions_sort_erase_types() - sort erase types in each region
391 struct spi_nor_erase_region *region = map->regions; in spi_nor_regions_sort_erase_types()
395 region_erase_mask = region->offset & SNOR_ERASE_TYPE_MASK; in spi_nor_regions_sort_erase_types()
401 region->offset = (region->offset & ~SNOR_ERASE_TYPE_MASK) | in spi_nor_regions_sort_erase_types()
409 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
412 * the Basic Flash Parameter Table length and version
434 * Return: 0 on success, -errno otherwise.
439 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_parse_bfpt()
440 struct spi_nor_erase_map *map = &params->erase_map; in spi_nor_parse_bfpt()
441 struct spi_nor_erase_type *erase_type = map->erase_type; in spi_nor_parse_bfpt()
447 u16 half; in spi_nor_parse_bfpt() local
450 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */ in spi_nor_parse_bfpt()
451 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216) in spi_nor_parse_bfpt()
452 return -EINVAL; in spi_nor_parse_bfpt()
456 bfpt_header->length * sizeof(u32)); in spi_nor_parse_bfpt()
470 params->addr_nbytes = 3; in spi_nor_parse_bfpt()
471 params->addr_mode_nbytes = 3; in spi_nor_parse_bfpt()
475 params->addr_nbytes = 4; in spi_nor_parse_bfpt()
476 params->addr_mode_nbytes = 4; in spi_nor_parse_bfpt()
489 * Prevent overflows on params->size. Anyway, a NOR of 2^64 in spi_nor_parse_bfpt()
494 return -EINVAL; in spi_nor_parse_bfpt()
496 params->size = 1ULL << val; in spi_nor_parse_bfpt()
498 params->size = val + 1; in spi_nor_parse_bfpt()
500 params->size >>= 3; /* Convert to bytes. */ in spi_nor_parse_bfpt()
507 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) { in spi_nor_parse_bfpt()
508 params->hwcaps.mask &= ~rd->hwcaps; in spi_nor_parse_bfpt()
512 params->hwcaps.mask |= rd->hwcaps; in spi_nor_parse_bfpt()
513 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps); in spi_nor_parse_bfpt()
514 read = &params->reads[cmd]; in spi_nor_parse_bfpt()
515 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift; in spi_nor_parse_bfpt()
516 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto); in spi_nor_parse_bfpt()
524 memset(&params->erase_map, 0, sizeof(params->erase_map)); in spi_nor_parse_bfpt()
530 half = bfpt.dwords[er->dword] >> er->shift; in spi_nor_parse_bfpt()
531 erasesize = half & 0xff; in spi_nor_parse_bfpt()
538 opcode = (half >> 8) & 0xff; in spi_nor_parse_bfpt()
543 spi_nor_init_uniform_erase_map(map, erase_mask, params->size); in spi_nor_parse_bfpt()
556 map->uniform_erase_type = map->uniform_region.offset & in spi_nor_parse_bfpt()
560 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216) in spi_nor_parse_bfpt()
567 params->page_size = 1U << val; in spi_nor_parse_bfpt()
572 params->quad_enable = NULL; in spi_nor_parse_bfpt()
578 * side-effect of clearing Status Register 2. in spi_nor_parse_bfpt()
585 nor->flags |= SNOR_F_HAS_16BIT_SR | SNOR_F_NO_READ_CR; in spi_nor_parse_bfpt()
586 params->quad_enable = spi_nor_sr2_bit1_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_sr1_bit6_quad_enable; in spi_nor_parse_bfpt()
595 nor->flags &= ~SNOR_F_HAS_16BIT_SR; in spi_nor_parse_bfpt()
596 params->quad_enable = spi_nor_sr2_bit7_quad_enable; in spi_nor_parse_bfpt()
604 * assumption of a 16-bit Write Status (01h) command. in spi_nor_parse_bfpt()
606 nor->flags |= SNOR_F_HAS_16BIT_SR; in spi_nor_parse_bfpt()
608 params->quad_enable = spi_nor_sr2_bit1_quad_enable; in spi_nor_parse_bfpt()
612 dev_dbg(nor->dev, "BFPT QER reserved value used\n"); in spi_nor_parse_bfpt()
618 params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_brwr; in spi_nor_parse_bfpt()
620 params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_wren_en4b_ex4b; in spi_nor_parse_bfpt()
622 params->set_4byte_addr_mode = spi_nor_set_4byte_addr_mode_en4b_ex4b; in spi_nor_parse_bfpt()
624 dev_dbg(nor->dev, "BFPT: 4-Byte Address Mode method is not recognized or not implemented\n"); in spi_nor_parse_bfpt()
628 nor->flags |= SNOR_F_SOFT_RESET; in spi_nor_parse_bfpt()
631 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216B) in spi_nor_parse_bfpt()
634 /* 8D-8D-8D command extension. */ in spi_nor_parse_bfpt()
637 nor->cmd_ext_type = SPI_NOR_EXT_REPEAT; in spi_nor_parse_bfpt()
641 nor->cmd_ext_type = SPI_NOR_EXT_INVERT; in spi_nor_parse_bfpt()
645 dev_dbg(nor->dev, "Reserved command extension used\n"); in spi_nor_parse_bfpt()
649 dev_dbg(nor->dev, "16-bit opcodes not supported\n"); in spi_nor_parse_bfpt()
650 return -EOPNOTSUPP; in spi_nor_parse_bfpt()
657 * spi_nor_smpt_addr_nbytes() - return the number of address bytes used in the
673 return nor->params->addr_mode_nbytes; in spi_nor_smpt_addr_nbytes()
678 * spi_nor_smpt_read_dummy() - return the configuration detection command read
690 return nor->read_dummy; in spi_nor_smpt_read_dummy()
695 * spi_nor_get_map_in_use() - get the configuration map in use
698 * @smpt_len: sector map parameter table length
700 * Return: pointer to the map in use, ERR_PTR(-errno) otherwise.
713 /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */ in spi_nor_get_map_in_use()
716 return ERR_PTR(-ENOMEM); in spi_nor_get_map_in_use()
718 addr_nbytes = nor->addr_nbytes; in spi_nor_get_map_in_use()
719 read_dummy = nor->read_dummy; in spi_nor_get_map_in_use()
720 read_opcode = nor->read_opcode; in spi_nor_get_map_in_use()
729 nor->addr_nbytes = spi_nor_smpt_addr_nbytes(nor, smpt[i]); in spi_nor_get_map_in_use()
730 nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]); in spi_nor_get_map_in_use()
731 nor->read_opcode = SMPT_CMD_OPCODE(smpt[i]); in spi_nor_get_map_in_use()
754 ret = ERR_PTR(-EINVAL); in spi_nor_get_map_in_use()
776 nor->addr_nbytes = addr_nbytes; in spi_nor_get_map_in_use()
777 nor->read_dummy = read_dummy; in spi_nor_get_map_in_use()
778 nor->read_opcode = read_opcode; in spi_nor_get_map_in_use()
784 region->offset |= SNOR_LAST_REGION; in spi_nor_region_mark_end()
789 region->offset |= SNOR_OVERLAID_REGION; in spi_nor_region_mark_overlay()
793 * spi_nor_region_check_overlay() - set overlay bit when the region is overlaid
808 if (region->size & erase[i].size_mask) { in spi_nor_region_check_overlay()
816 * spi_nor_init_non_uniform_erase_map() - initialize the non-uniform erase map
820 * Return: 0 on success, -errno otherwise.
825 struct spi_nor_erase_map *map = &nor->params->erase_map; in spi_nor_init_non_uniform_erase_map()
826 struct spi_nor_erase_type *erase = map->erase_type; in spi_nor_init_non_uniform_erase_map()
839 region = devm_kcalloc(nor->dev, region_count, sizeof(*region), in spi_nor_init_non_uniform_erase_map()
842 return -ENOMEM; in spi_nor_init_non_uniform_erase_map()
843 map->regions = region; in spi_nor_init_non_uniform_erase_map()
872 spi_nor_region_mark_end(&region[i - 1]); in spi_nor_init_non_uniform_erase_map()
874 save_uniform_erase_type = map->uniform_erase_type; in spi_nor_init_non_uniform_erase_map()
875 map->uniform_erase_type = spi_nor_sort_erase_mask(map, in spi_nor_init_non_uniform_erase_map()
883 map->uniform_erase_type = save_uniform_erase_type; in spi_nor_init_non_uniform_erase_map()
884 return -EINVAL; in spi_nor_init_non_uniform_erase_map()
900 * spi_nor_parse_smpt() - parse Sector Map Parameter Table
908 * Return: 0 on success, -errno otherwise.
920 len = smpt_header->length * sizeof(*smpt); in spi_nor_parse_smpt()
923 return -ENOMEM; in spi_nor_parse_smpt()
931 le32_to_cpu_array(smpt, smpt_header->length); in spi_nor_parse_smpt()
933 sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length); in spi_nor_parse_smpt()
943 spi_nor_regions_sort_erase_types(&nor->params->erase_map); in spi_nor_parse_smpt()
951 * spi_nor_parse_4bait() - parse the 4-Byte Address Instruction Table
954 * the 4-Byte Address Instruction Table length and version.
956 * Return: 0 on success, -errno otherwise.
983 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_parse_4bait()
984 struct spi_nor_pp_command *params_pp = params->page_programs; in spi_nor_parse_4bait()
985 struct spi_nor_erase_map *map = &params->erase_map; in spi_nor_parse_4bait()
986 struct spi_nor_erase_type *erase_type = map->erase_type; in spi_nor_parse_4bait()
992 if (param_header->major != SFDP_JESD216_MAJOR || in spi_nor_parse_4bait()
993 param_header->length < SFDP_4BAIT_DWORD_MAX) in spi_nor_parse_4bait()
994 return -EINVAL; in spi_nor_parse_4bait()
996 /* Read the 4-byte Address Instruction Table. */ in spi_nor_parse_4bait()
999 /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */ in spi_nor_parse_4bait()
1002 return -ENOMEM; in spi_nor_parse_4bait()
1013 * Compute the subset of (Fast) Read commands for which the 4-byte in spi_nor_parse_4bait()
1021 discard_hwcaps |= read->hwcaps; in spi_nor_parse_4bait()
1022 if ((params->hwcaps.mask & read->hwcaps) && in spi_nor_parse_4bait()
1023 (dwords[SFDP_DWORD(1)] & read->supported_bit)) in spi_nor_parse_4bait()
1024 read_hwcaps |= read->hwcaps; in spi_nor_parse_4bait()
1028 * Compute the subset of Page Program commands for which the 4-byte in spi_nor_parse_4bait()
1038 * Bypass the params->hwcaps.mask and consider 4BAIT the biggest in spi_nor_parse_4bait()
1041 discard_hwcaps |= program->hwcaps; in spi_nor_parse_4bait()
1042 if (dwords[SFDP_DWORD(1)] & program->supported_bit) in spi_nor_parse_4bait()
1043 pp_hwcaps |= program->hwcaps; in spi_nor_parse_4bait()
1047 * Compute the subset of Sector Erase commands for which the 4-byte in spi_nor_parse_4bait()
1054 if (dwords[SFDP_DWORD(1)] & erase->supported_bit) in spi_nor_parse_4bait()
1062 * We need at least one 4-byte op code per read, program and erase in spi_nor_parse_4bait()
1064 * nor->addr_nbytes value. in spi_nor_parse_4bait()
1070 * Discard all operations from the 4-byte instruction set which are in spi_nor_parse_4bait()
1073 params->hwcaps.mask &= ~discard_hwcaps; in spi_nor_parse_4bait()
1074 params->hwcaps.mask |= (read_hwcaps | pp_hwcaps); in spi_nor_parse_4bait()
1076 /* Use the 4-byte address instruction set. */ in spi_nor_parse_4bait()
1078 struct spi_nor_read_command *read_cmd = &params->reads[i]; in spi_nor_parse_4bait()
1080 read_cmd->opcode = spi_nor_convert_3to4_read(read_cmd->opcode); in spi_nor_parse_4bait()
1118 params->addr_nbytes = 4; in spi_nor_parse_4bait()
1119 nor->flags |= SNOR_F_4B_OPCODES | SNOR_F_HAS_4BAIT; in spi_nor_parse_4bait()
1136 * spi_nor_parse_profile1() - parse the xSPI Profile 1.0 table
1139 * the Profile 1.0 Table length and version.
1141 * Return: 0 on success, -errno otherwise.
1151 len = profile1_header->length * sizeof(*dwords); in spi_nor_parse_profile1()
1154 return -ENOMEM; in spi_nor_parse_profile1()
1161 le32_to_cpu_array(dwords, profile1_header->length); in spi_nor_parse_profile1()
1163 /* Get 8D-8D-8D fast read opcode and dummy cycles. */ in spi_nor_parse_profile1()
1168 nor->params->rdsr_dummy = 8; in spi_nor_parse_profile1()
1170 nor->params->rdsr_dummy = 4; in spi_nor_parse_profile1()
1173 nor->params->rdsr_addr_nbytes = 4; in spi_nor_parse_profile1()
1175 nor->params->rdsr_addr_nbytes = 0; in spi_nor_parse_profile1()
1197 dev_dbg(nor->dev, in spi_nor_parse_profile1()
1204 nor->params->hwcaps.mask |= SNOR_HWCAPS_READ_8_8_8_DTR; in spi_nor_parse_profile1()
1205 spi_nor_set_read_settings(&nor->params->reads[SNOR_CMD_READ_8_8_8_DTR], in spi_nor_parse_profile1()
1211 * the params->hwcaps.mask here. in spi_nor_parse_profile1()
1213 nor->params->hwcaps.mask |= SNOR_HWCAPS_PP_8_8_8_DTR; in spi_nor_parse_profile1()
1223 * spi_nor_parse_sccr() - Parse the Status, Control and Configuration Register
1227 * the SCCR Map table length and version.
1229 * Return: 0 on success, -errno otherwise.
1234 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_parse_sccr()
1239 len = sccr_header->length * sizeof(*dwords); in spi_nor_parse_sccr()
1242 return -ENOMEM; in spi_nor_parse_sccr()
1249 le32_to_cpu_array(dwords, sccr_header->length); in spi_nor_parse_sccr()
1252 if (!params->vreg_offset) { in spi_nor_parse_sccr()
1253 params->vreg_offset = devm_kmalloc(nor->dev, sizeof(*dwords), in spi_nor_parse_sccr()
1255 if (!params->vreg_offset) { in spi_nor_parse_sccr()
1256 ret = -ENOMEM; in spi_nor_parse_sccr()
1260 params->vreg_offset[0] = dwords[SFDP_DWORD(1)]; in spi_nor_parse_sccr()
1261 params->n_dice = 1; in spi_nor_parse_sccr()
1265 nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE; in spi_nor_parse_sccr()
1273 * spi_nor_parse_sccr_mc() - Parse the Status, Control and Configuration
1274 * Register Map Offsets for Multi-Chip SPI Memory
1278 * the SCCR Map offsets table length and version.
1280 * Return: 0 on success, -errno otherwise.
1285 struct spi_nor_flash_parameter *params = nor->params; in spi_nor_parse_sccr_mc()
1291 len = sccr_mc_header->length * sizeof(*dwords); in spi_nor_parse_sccr_mc()
1294 return -ENOMEM; in spi_nor_parse_sccr_mc()
1301 le32_to_cpu_array(dwords, sccr_mc_header->length); in spi_nor_parse_sccr_mc()
1304 * Pair of DOWRDs (volatile and non-volatile register offsets) per in spi_nor_parse_sccr_mc()
1305 * additional die. Hence, length = 2 * (number of additional dice). in spi_nor_parse_sccr_mc()
1307 n_dice = 1 + sccr_mc_header->length / 2; in spi_nor_parse_sccr_mc()
1310 params->vreg_offset = in spi_nor_parse_sccr_mc()
1311 devm_krealloc(nor->dev, params->vreg_offset, in spi_nor_parse_sccr_mc()
1314 if (!params->vreg_offset) { in spi_nor_parse_sccr_mc()
1315 ret = -ENOMEM; in spi_nor_parse_sccr_mc()
1320 params->vreg_offset[i] = dwords[SFDP_DWORD(i) * 2]; in spi_nor_parse_sccr_mc()
1322 params->n_dice = n_dice; in spi_nor_parse_sccr_mc()
1330 * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
1342 if (nor->manufacturer && nor->manufacturer->fixups && in spi_nor_post_sfdp_fixups()
1343 nor->manufacturer->fixups->post_sfdp) { in spi_nor_post_sfdp_fixups()
1344 ret = nor->manufacturer->fixups->post_sfdp(nor); in spi_nor_post_sfdp_fixups()
1349 if (nor->info->fixups && nor->info->fixups->post_sfdp) in spi_nor_post_sfdp_fixups()
1350 return nor->info->fixups->post_sfdp(nor); in spi_nor_post_sfdp_fixups()
1356 * spi_nor_check_sfdp_signature() - check for a valid SFDP signature
1362 * Return: 0 on success, -errno otherwise.
1377 return -EINVAL; in spi_nor_check_sfdp_signature()
1383 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
1388 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
1392 * Return: 0 on success, -errno otherwise.
1399 struct device *dev = nor->dev; in spi_nor_parse_sfdp()
1413 return -EINVAL; in spi_nor_parse_sfdp()
1421 bfpt_header->major != SFDP_JESD216_MAJOR) in spi_nor_parse_sfdp()
1422 return -EINVAL; in spi_nor_parse_sfdp()
1443 return -ENOMEM; in spi_nor_parse_sfdp()
1476 err = -ENOMEM; in spi_nor_parse_sfdp()
1486 sfdp->num_dwords = DIV_ROUND_UP(sfdp_size, sizeof(*sfdp->dwords)); in spi_nor_parse_sfdp()
1487 sfdp->dwords = devm_kcalloc(dev, sfdp->num_dwords, in spi_nor_parse_sfdp()
1488 sizeof(*sfdp->dwords), GFP_KERNEL); in spi_nor_parse_sfdp()
1489 if (!sfdp->dwords) { in spi_nor_parse_sfdp()
1490 err = -ENOMEM; in spi_nor_parse_sfdp()
1495 err = spi_nor_read_sfdp(nor, 0, sfdp_size, sfdp->dwords); in spi_nor_parse_sfdp()
1498 devm_kfree(dev, sfdp->dwords); in spi_nor_parse_sfdp()
1503 nor->sfdp = sfdp; in spi_nor_parse_sfdp()
1513 param_header->major == SFDP_JESD216_MAJOR && in spi_nor_parse_sfdp()
1514 (param_header->minor > bfpt_header->minor || in spi_nor_parse_sfdp()
1515 (param_header->minor == bfpt_header->minor && in spi_nor_parse_sfdp()
1516 param_header->length > bfpt_header->length))) in spi_nor_parse_sfdp()