1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2005, Intec Automation Inc.
4 * Copyright (C) 2014, Freescale Semiconductor, Inc.
7 #include <linux/slab.h>
8 #include <linux/sort.h>
9 #include <linux/mtd/spi-nor.h>
13 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
14 #define SFDP_PARAM_HEADER_PTP(p) \
15 (((p)->parameter_table_pointer[2] << 16) | \
16 ((p)->parameter_table_pointer[1] << 8) | \
17 ((p)->parameter_table_pointer[0] << 0))
19 #define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
20 #define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
21 #define SFDP_4BAIT_ID 0xff84 /* 4-byte Address Instruction Table */
23 #define SFDP_SIGNATURE 0x50444653U
26 u32 signature; /* Ox50444653U <=> "SFDP" */
29 u8 nph; /* 0-base number of parameter headers */
32 /* Basic Flash Parameter Table. */
33 struct sfdp_parameter_header bfpt_header;
36 /* Fast Read settings. */
37 struct sfdp_bfpt_read {
38 /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
42 * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
43 * whether the Fast Read x-y-z command is supported.
49 * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
50 * encodes the op code, the number of mode clocks and the number of wait
51 * states to be used by Fast Read x-y-z command.
56 /* The SPI protocol for this Fast Read x-y-z command. */
57 enum spi_nor_protocol proto;
60 struct sfdp_bfpt_erase {
62 * The half-word at offset <shift> in DWORD <dwoard> encodes the
63 * op code and erase sector size to be used by Sector Erase commands.
69 #define SMPT_CMD_ADDRESS_LEN_MASK GENMASK(23, 22)
70 #define SMPT_CMD_ADDRESS_LEN_0 (0x0UL << 22)
71 #define SMPT_CMD_ADDRESS_LEN_3 (0x1UL << 22)
72 #define SMPT_CMD_ADDRESS_LEN_4 (0x2UL << 22)
73 #define SMPT_CMD_ADDRESS_LEN_USE_CURRENT (0x3UL << 22)
75 #define SMPT_CMD_READ_DUMMY_MASK GENMASK(19, 16)
76 #define SMPT_CMD_READ_DUMMY_SHIFT 16
77 #define SMPT_CMD_READ_DUMMY(_cmd) \
78 (((_cmd) & SMPT_CMD_READ_DUMMY_MASK) >> SMPT_CMD_READ_DUMMY_SHIFT)
79 #define SMPT_CMD_READ_DUMMY_IS_VARIABLE 0xfUL
81 #define SMPT_CMD_READ_DATA_MASK GENMASK(31, 24)
82 #define SMPT_CMD_READ_DATA_SHIFT 24
83 #define SMPT_CMD_READ_DATA(_cmd) \
84 (((_cmd) & SMPT_CMD_READ_DATA_MASK) >> SMPT_CMD_READ_DATA_SHIFT)
86 #define SMPT_CMD_OPCODE_MASK GENMASK(15, 8)
87 #define SMPT_CMD_OPCODE_SHIFT 8
88 #define SMPT_CMD_OPCODE(_cmd) \
89 (((_cmd) & SMPT_CMD_OPCODE_MASK) >> SMPT_CMD_OPCODE_SHIFT)
91 #define SMPT_MAP_REGION_COUNT_MASK GENMASK(23, 16)
92 #define SMPT_MAP_REGION_COUNT_SHIFT 16
93 #define SMPT_MAP_REGION_COUNT(_header) \
94 ((((_header) & SMPT_MAP_REGION_COUNT_MASK) >> \
95 SMPT_MAP_REGION_COUNT_SHIFT) + 1)
97 #define SMPT_MAP_ID_MASK GENMASK(15, 8)
98 #define SMPT_MAP_ID_SHIFT 8
99 #define SMPT_MAP_ID(_header) \
100 (((_header) & SMPT_MAP_ID_MASK) >> SMPT_MAP_ID_SHIFT)
102 #define SMPT_MAP_REGION_SIZE_MASK GENMASK(31, 8)
103 #define SMPT_MAP_REGION_SIZE_SHIFT 8
104 #define SMPT_MAP_REGION_SIZE(_region) \
105 (((((_region) & SMPT_MAP_REGION_SIZE_MASK) >> \
106 SMPT_MAP_REGION_SIZE_SHIFT) + 1) * 256)
108 #define SMPT_MAP_REGION_ERASE_TYPE_MASK GENMASK(3, 0)
109 #define SMPT_MAP_REGION_ERASE_TYPE(_region) \
110 ((_region) & SMPT_MAP_REGION_ERASE_TYPE_MASK)
112 #define SMPT_DESC_TYPE_MAP BIT(1)
113 #define SMPT_DESC_END BIT(0)
115 #define SFDP_4BAIT_DWORD_MAX 2
118 /* The hardware capability. */
122 * The <supported_bit> bit in DWORD1 of the 4BAIT tells us whether
123 * the associated 4-byte address op code is supported.
129 * spi_nor_read_raw() - raw read of serial flash memory. read_opcode,
130 * addr_width and read_dummy members of the struct spi_nor
131 * should be previously
133 * @nor: pointer to a 'struct spi_nor'
134 * @addr: offset in the serial flash memory
135 * @len: number of bytes to read
136 * @buf: buffer where the data is copied into (dma-safe memory)
138 * Return: 0 on success, -errno otherwise.
140 static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
145 ret = spi_nor_read_data(nor, addr, len, buf);
148 if (!ret || ret > len)
159 * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
160 * @nor: pointer to a 'struct spi_nor'
161 * @addr: offset in the SFDP area to start reading data from
162 * @len: number of bytes to read
163 * @buf: buffer where the SFDP data are copied into (dma-safe memory)
165 * Whatever the actual numbers of bytes for address and dummy cycles are
166 * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
167 * followed by a 3-byte address and 8 dummy clock cycles.
169 * Return: 0 on success, -errno otherwise.
171 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
172 size_t len, void *buf)
174 u8 addr_width, read_opcode, read_dummy;
177 read_opcode = nor->read_opcode;
178 addr_width = nor->addr_width;
179 read_dummy = nor->read_dummy;
181 nor->read_opcode = SPINOR_OP_RDSFDP;
185 ret = spi_nor_read_raw(nor, addr, len, buf);
187 nor->read_opcode = read_opcode;
188 nor->addr_width = addr_width;
189 nor->read_dummy = read_dummy;
195 * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
196 * @nor: pointer to a 'struct spi_nor'
197 * @addr: offset in the SFDP area to start reading data from
198 * @len: number of bytes to read
199 * @buf: buffer where the SFDP data are copied into
201 * Wrap spi_nor_read_sfdp() using a kmalloc'ed bounce buffer as @buf is now not
202 * guaranteed to be dma-safe.
204 * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
207 static int spi_nor_read_sfdp_dma_unsafe(struct spi_nor *nor, u32 addr,
208 size_t len, void *buf)
213 dma_safe_buf = kmalloc(len, GFP_KERNEL);
217 ret = spi_nor_read_sfdp(nor, addr, len, dma_safe_buf);
218 memcpy(buf, dma_safe_buf, len);
225 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
227 enum spi_nor_protocol proto)
229 read->num_mode_clocks = (half >> 5) & 0x07;
230 read->num_wait_states = (half >> 0) & 0x1f;
231 read->opcode = (half >> 8) & 0xff;
235 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
236 /* Fast Read 1-1-2 */
238 SNOR_HWCAPS_READ_1_1_2,
239 BFPT_DWORD(1), BIT(16), /* Supported bit */
240 BFPT_DWORD(4), 0, /* Settings */
244 /* Fast Read 1-2-2 */
246 SNOR_HWCAPS_READ_1_2_2,
247 BFPT_DWORD(1), BIT(20), /* Supported bit */
248 BFPT_DWORD(4), 16, /* Settings */
252 /* Fast Read 2-2-2 */
254 SNOR_HWCAPS_READ_2_2_2,
255 BFPT_DWORD(5), BIT(0), /* Supported bit */
256 BFPT_DWORD(6), 16, /* Settings */
260 /* Fast Read 1-1-4 */
262 SNOR_HWCAPS_READ_1_1_4,
263 BFPT_DWORD(1), BIT(22), /* Supported bit */
264 BFPT_DWORD(3), 16, /* Settings */
268 /* Fast Read 1-4-4 */
270 SNOR_HWCAPS_READ_1_4_4,
271 BFPT_DWORD(1), BIT(21), /* Supported bit */
272 BFPT_DWORD(3), 0, /* Settings */
276 /* Fast Read 4-4-4 */
278 SNOR_HWCAPS_READ_4_4_4,
279 BFPT_DWORD(5), BIT(4), /* Supported bit */
280 BFPT_DWORD(7), 16, /* Settings */
285 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
286 /* Erase Type 1 in DWORD8 bits[15:0] */
289 /* Erase Type 2 in DWORD8 bits[31:16] */
292 /* Erase Type 3 in DWORD9 bits[15:0] */
295 /* Erase Type 4 in DWORD9 bits[31:16] */
300 * spi_nor_set_erase_settings_from_bfpt() - set erase type settings from BFPT
301 * @erase: pointer to a structure that describes a SPI NOR erase type
302 * @size: the size of the sector/block erased by the erase type
303 * @opcode: the SPI command op code to erase the sector/block
304 * @i: erase type index as sorted in the Basic Flash Parameter Table
306 * The supported Erase Types will be sorted at init in ascending order, with
307 * the smallest Erase Type size being the first member in the erase_type array
308 * of the spi_nor_erase_map structure. Save the Erase Type index as sorted in
309 * the Basic Flash Parameter Table since it will be used later on to
310 * synchronize with the supported Erase Types defined in SFDP optional tables.
313 spi_nor_set_erase_settings_from_bfpt(struct spi_nor_erase_type *erase,
314 u32 size, u8 opcode, u8 i)
317 spi_nor_set_erase_type(erase, size, opcode);
321 * spi_nor_map_cmp_erase_type() - compare the map's erase types by size
322 * @l: member in the left half of the map's erase_type array
323 * @r: member in the right half of the map's erase_type array
325 * Comparison function used in the sort() call to sort in ascending order the
326 * map's erase types, the smallest erase type size being the first member in the
327 * sorted erase_type array.
329 * Return: the result of @l->size - @r->size
331 static int spi_nor_map_cmp_erase_type(const void *l, const void *r)
333 const struct spi_nor_erase_type *left = l, *right = r;
335 return left->size - right->size;
339 * spi_nor_sort_erase_mask() - sort erase mask
340 * @map: the erase map of the SPI NOR
341 * @erase_mask: the erase type mask to be sorted
343 * Replicate the sort done for the map's erase types in BFPT: sort the erase
344 * mask in ascending order with the smallest erase type size starting from
345 * BIT(0) in the sorted erase mask.
347 * Return: sorted erase mask.
349 static u8 spi_nor_sort_erase_mask(struct spi_nor_erase_map *map, u8 erase_mask)
351 struct spi_nor_erase_type *erase_type = map->erase_type;
353 u8 sorted_erase_mask = 0;
358 /* Replicate the sort done for the map's erase types. */
359 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
360 if (erase_type[i].size && erase_mask & BIT(erase_type[i].idx))
361 sorted_erase_mask |= BIT(i);
363 return sorted_erase_mask;
367 * spi_nor_regions_sort_erase_types() - sort erase types in each region
368 * @map: the erase map of the SPI NOR
370 * Function assumes that the erase types defined in the erase map are already
371 * sorted in ascending order, with the smallest erase type size being the first
372 * member in the erase_type array. It replicates the sort done for the map's
373 * erase types. Each region's erase bitmask will indicate which erase types are
374 * supported from the sorted erase types defined in the erase map.
375 * Sort the all region's erase type at init in order to speed up the process of
376 * finding the best erase command at runtime.
378 static void spi_nor_regions_sort_erase_types(struct spi_nor_erase_map *map)
380 struct spi_nor_erase_region *region = map->regions;
381 u8 region_erase_mask, sorted_erase_mask;
384 region_erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
386 sorted_erase_mask = spi_nor_sort_erase_mask(map,
389 /* Overwrite erase mask. */
390 region->offset = (region->offset & ~SNOR_ERASE_TYPE_MASK) |
393 region = spi_nor_region_next(region);
398 * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
399 * @nor: pointer to a 'struct spi_nor'
400 * @bfpt_header: pointer to the 'struct sfdp_parameter_header' describing
401 * the Basic Flash Parameter Table length and version
402 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
405 * The Basic Flash Parameter Table is the main and only mandatory table as
406 * defined by the SFDP (JESD216) specification.
407 * It provides us with the total size (memory density) of the data array and
408 * the number of address bytes for Fast Read, Page Program and Sector Erase
410 * For Fast READ commands, it also gives the number of mode clock cycles and
411 * wait states (regrouped in the number of dummy clock cycles) for each
412 * supported instruction op code.
413 * For Page Program, the page size is now available since JESD216 rev A, however
414 * the supported instruction op codes are still not provided.
415 * For Sector Erase commands, this table stores the supported instruction op
416 * codes and the associated sector sizes.
417 * Finally, the Quad Enable Requirements (QER) are also available since JESD216
418 * rev A. The QER bits encode the manufacturer dependent procedure to be
419 * executed to set the Quad Enable (QE) bit in some internal register of the
420 * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
421 * sending any Quad SPI command to the memory. Actually, setting the QE bit
422 * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
423 * and IO3 hence enabling 4 (Quad) I/O lines.
425 * Return: 0 on success, -errno otherwise.
427 static int spi_nor_parse_bfpt(struct spi_nor *nor,
428 const struct sfdp_parameter_header *bfpt_header,
429 struct spi_nor_flash_parameter *params)
431 struct spi_nor_erase_map *map = ¶ms->erase_map;
432 struct spi_nor_erase_type *erase_type = map->erase_type;
433 struct sfdp_bfpt bfpt;
440 /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
441 if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
444 /* Read the Basic Flash Parameter Table. */
445 len = min_t(size_t, sizeof(bfpt),
446 bfpt_header->length * sizeof(u32));
447 addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
448 memset(&bfpt, 0, sizeof(bfpt));
449 err = spi_nor_read_sfdp_dma_unsafe(nor, addr, len, &bfpt);
453 /* Fix endianness of the BFPT DWORDs. */
454 le32_to_cpu_array(bfpt.dwords, BFPT_DWORD_MAX);
456 /* Number of address bytes. */
457 switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
458 case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
459 case BFPT_DWORD1_ADDRESS_BYTES_3_OR_4:
463 case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
471 /* Flash Memory Density (in bits). */
472 val = bfpt.dwords[BFPT_DWORD(2)];
477 * Prevent overflows on params->size. Anyway, a NOR of 2^64
478 * bits is unlikely to exist so this error probably means
479 * the BFPT we are reading is corrupted/wrong.
484 params->size = 1ULL << val;
486 params->size = val + 1;
488 params->size >>= 3; /* Convert to bytes. */
490 /* Fast Read settings. */
491 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
492 const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
493 struct spi_nor_read_command *read;
495 if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
496 params->hwcaps.mask &= ~rd->hwcaps;
500 params->hwcaps.mask |= rd->hwcaps;
501 cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
502 read = ¶ms->reads[cmd];
503 half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
504 spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
508 * Sector Erase settings. Reinitialize the uniform erase map using the
509 * Erase Types defined in the bfpt table.
512 memset(¶ms->erase_map, 0, sizeof(params->erase_map));
513 for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
514 const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
518 half = bfpt.dwords[er->dword] >> er->shift;
519 erasesize = half & 0xff;
521 /* erasesize == 0 means this Erase Type is not supported. */
525 erasesize = 1U << erasesize;
526 opcode = (half >> 8) & 0xff;
527 erase_mask |= BIT(i);
528 spi_nor_set_erase_settings_from_bfpt(&erase_type[i], erasesize,
531 spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
533 * Sort all the map's Erase Types in ascending order with the smallest
534 * erase size being the first member in the erase_type array.
536 sort(erase_type, SNOR_ERASE_TYPE_MAX, sizeof(erase_type[0]),
537 spi_nor_map_cmp_erase_type, NULL);
539 * Sort the erase types in the uniform region in order to update the
540 * uniform_erase_type bitmask. The bitmask will be used later on when
541 * selecting the uniform erase.
543 spi_nor_regions_sort_erase_types(map);
544 map->uniform_erase_type = map->uniform_region.offset &
545 SNOR_ERASE_TYPE_MASK;
547 /* Stop here if not JESD216 rev A or later. */
548 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216)
549 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt,
552 /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
553 val = bfpt.dwords[BFPT_DWORD(11)];
554 val &= BFPT_DWORD11_PAGE_SIZE_MASK;
555 val >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
556 params->page_size = 1U << val;
558 /* Quad Enable Requirements. */
559 switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
560 case BFPT_DWORD15_QER_NONE:
561 params->quad_enable = NULL;
564 case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
566 * Writing only one byte to the Status Register has the
567 * side-effect of clearing Status Register 2.
569 case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
571 * Read Configuration Register (35h) instruction is not
574 nor->flags |= SNOR_F_HAS_16BIT_SR | SNOR_F_NO_READ_CR;
575 params->quad_enable = spi_nor_sr2_bit1_quad_enable;
578 case BFPT_DWORD15_QER_SR1_BIT6:
579 nor->flags &= ~SNOR_F_HAS_16BIT_SR;
580 params->quad_enable = spi_nor_sr1_bit6_quad_enable;
583 case BFPT_DWORD15_QER_SR2_BIT7:
584 nor->flags &= ~SNOR_F_HAS_16BIT_SR;
585 params->quad_enable = spi_nor_sr2_bit7_quad_enable;
588 case BFPT_DWORD15_QER_SR2_BIT1:
590 * JESD216 rev B or later does not specify if writing only one
591 * byte to the Status Register clears or not the Status
592 * Register 2, so let's be cautious and keep the default
593 * assumption of a 16-bit Write Status (01h) command.
595 nor->flags |= SNOR_F_HAS_16BIT_SR;
597 params->quad_enable = spi_nor_sr2_bit1_quad_enable;
601 dev_dbg(nor->dev, "BFPT QER reserved value used\n");
605 /* Stop here if not JESD216 rev C or later. */
606 if (bfpt_header->length == BFPT_DWORD_MAX_JESD216B)
607 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt,
610 return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt, params);
614 * spi_nor_smpt_addr_width() - return the address width used in the
615 * configuration detection command.
616 * @nor: pointer to a 'struct spi_nor'
617 * @settings: configuration detection command descriptor, dword1
619 static u8 spi_nor_smpt_addr_width(const struct spi_nor *nor, const u32 settings)
621 switch (settings & SMPT_CMD_ADDRESS_LEN_MASK) {
622 case SMPT_CMD_ADDRESS_LEN_0:
624 case SMPT_CMD_ADDRESS_LEN_3:
626 case SMPT_CMD_ADDRESS_LEN_4:
628 case SMPT_CMD_ADDRESS_LEN_USE_CURRENT:
630 return nor->addr_width;
635 * spi_nor_smpt_read_dummy() - return the configuration detection command read
636 * latency, in clock cycles.
637 * @nor: pointer to a 'struct spi_nor'
638 * @settings: configuration detection command descriptor, dword1
640 * Return: the number of dummy cycles for an SMPT read
642 static u8 spi_nor_smpt_read_dummy(const struct spi_nor *nor, const u32 settings)
644 u8 read_dummy = SMPT_CMD_READ_DUMMY(settings);
646 if (read_dummy == SMPT_CMD_READ_DUMMY_IS_VARIABLE)
647 return nor->read_dummy;
652 * spi_nor_get_map_in_use() - get the configuration map in use
653 * @nor: pointer to a 'struct spi_nor'
654 * @smpt: pointer to the sector map parameter table
655 * @smpt_len: sector map parameter table length
657 * Return: pointer to the map in use, ERR_PTR(-errno) otherwise.
659 static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
667 u8 addr_width, read_opcode, read_dummy;
668 u8 read_data_mask, map_id;
670 /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
671 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
673 return ERR_PTR(-ENOMEM);
675 addr_width = nor->addr_width;
676 read_dummy = nor->read_dummy;
677 read_opcode = nor->read_opcode;
680 /* Determine if there are any optional Detection Command Descriptors */
681 for (i = 0; i < smpt_len; i += 2) {
682 if (smpt[i] & SMPT_DESC_TYPE_MAP)
685 read_data_mask = SMPT_CMD_READ_DATA(smpt[i]);
686 nor->addr_width = spi_nor_smpt_addr_width(nor, smpt[i]);
687 nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]);
688 nor->read_opcode = SMPT_CMD_OPCODE(smpt[i]);
691 err = spi_nor_read_raw(nor, addr, 1, buf);
698 * Build an index value that is used to select the Sector Map
699 * Configuration that is currently in use.
701 map_id = map_id << 1 | !!(*buf & read_data_mask);
705 * If command descriptors are provided, they always precede map
706 * descriptors in the table. There is no need to start the iteration
707 * over smpt array all over again.
709 * Find the matching configuration map.
711 ret = ERR_PTR(-EINVAL);
712 while (i < smpt_len) {
713 if (SMPT_MAP_ID(smpt[i]) == map_id) {
719 * If there are no more configuration map descriptors and no
720 * configuration ID matched the configuration identifier, the
721 * sector address map is unknown.
723 if (smpt[i] & SMPT_DESC_END)
726 /* increment the table index to the next map */
727 i += SMPT_MAP_REGION_COUNT(smpt[i]) + 1;
733 nor->addr_width = addr_width;
734 nor->read_dummy = read_dummy;
735 nor->read_opcode = read_opcode;
739 static void spi_nor_region_mark_end(struct spi_nor_erase_region *region)
741 region->offset |= SNOR_LAST_REGION;
744 static void spi_nor_region_mark_overlay(struct spi_nor_erase_region *region)
746 region->offset |= SNOR_OVERLAID_REGION;
750 * spi_nor_region_check_overlay() - set overlay bit when the region is overlaid
751 * @region: pointer to a structure that describes a SPI NOR erase region
752 * @erase: pointer to a structure that describes a SPI NOR erase type
753 * @erase_type: erase type bitmask
756 spi_nor_region_check_overlay(struct spi_nor_erase_region *region,
757 const struct spi_nor_erase_type *erase,
762 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
763 if (!(erase[i].size && erase_type & BIT(erase[i].idx)))
765 if (region->size & erase[i].size_mask) {
766 spi_nor_region_mark_overlay(region);
773 * spi_nor_init_non_uniform_erase_map() - initialize the non-uniform erase map
774 * @nor: pointer to a 'struct spi_nor'
775 * @params: pointer to a duplicate 'struct spi_nor_flash_parameter' that is
776 * used for storing SFDP parsed data
777 * @smpt: pointer to the sector map parameter table
779 * Return: 0 on success, -errno otherwise.
782 spi_nor_init_non_uniform_erase_map(struct spi_nor *nor,
783 struct spi_nor_flash_parameter *params,
786 struct spi_nor_erase_map *map = ¶ms->erase_map;
787 struct spi_nor_erase_type *erase = map->erase_type;
788 struct spi_nor_erase_region *region;
792 u8 uniform_erase_type, save_uniform_erase_type;
793 u8 erase_type, regions_erase_type;
795 region_count = SMPT_MAP_REGION_COUNT(*smpt);
797 * The regions will be freed when the driver detaches from the
800 region = devm_kcalloc(nor->dev, region_count, sizeof(*region),
804 map->regions = region;
806 uniform_erase_type = 0xff;
807 regions_erase_type = 0;
809 /* Populate regions. */
810 for (i = 0; i < region_count; i++) {
811 j = i + 1; /* index for the region dword */
812 region[i].size = SMPT_MAP_REGION_SIZE(smpt[j]);
813 erase_type = SMPT_MAP_REGION_ERASE_TYPE(smpt[j]);
814 region[i].offset = offset | erase_type;
816 spi_nor_region_check_overlay(®ion[i], erase, erase_type);
819 * Save the erase types that are supported in all regions and
820 * can erase the entire flash memory.
822 uniform_erase_type &= erase_type;
825 * regions_erase_type mask will indicate all the erase types
826 * supported in this configuration map.
828 regions_erase_type |= erase_type;
830 offset = (region[i].offset & ~SNOR_ERASE_FLAGS_MASK) +
833 spi_nor_region_mark_end(®ion[i - 1]);
835 save_uniform_erase_type = map->uniform_erase_type;
836 map->uniform_erase_type = spi_nor_sort_erase_mask(map,
839 if (!regions_erase_type) {
841 * Roll back to the previous uniform_erase_type mask, SMPT is
844 map->uniform_erase_type = save_uniform_erase_type;
849 * BFPT advertises all the erase types supported by all the possible
850 * map configurations. Mask out the erase types that are not supported
851 * by the current map configuration.
853 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
854 if (!(regions_erase_type & BIT(erase[i].idx)))
855 spi_nor_set_erase_type(&erase[i], 0, 0xFF);
861 * spi_nor_parse_smpt() - parse Sector Map Parameter Table
862 * @nor: pointer to a 'struct spi_nor'
863 * @smpt_header: sector map parameter table header
864 * @params: pointer to a duplicate 'struct spi_nor_flash_parameter'
865 * that is used for storing SFDP parsed data
867 * This table is optional, but when available, we parse it to identify the
868 * location and size of sectors within the main data array of the flash memory
869 * device and to identify which Erase Types are supported by each sector.
871 * Return: 0 on success, -errno otherwise.
873 static int spi_nor_parse_smpt(struct spi_nor *nor,
874 const struct sfdp_parameter_header *smpt_header,
875 struct spi_nor_flash_parameter *params)
877 const u32 *sector_map;
883 /* Read the Sector Map Parameter Table. */
884 len = smpt_header->length * sizeof(*smpt);
885 smpt = kmalloc(len, GFP_KERNEL);
889 addr = SFDP_PARAM_HEADER_PTP(smpt_header);
890 ret = spi_nor_read_sfdp(nor, addr, len, smpt);
894 /* Fix endianness of the SMPT DWORDs. */
895 le32_to_cpu_array(smpt, smpt_header->length);
897 sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length);
898 if (IS_ERR(sector_map)) {
899 ret = PTR_ERR(sector_map);
903 ret = spi_nor_init_non_uniform_erase_map(nor, params, sector_map);
907 spi_nor_regions_sort_erase_types(¶ms->erase_map);
915 * spi_nor_parse_4bait() - parse the 4-Byte Address Instruction Table
916 * @nor: pointer to a 'struct spi_nor'.
917 * @param_header: pointer to the 'struct sfdp_parameter_header' describing
918 * the 4-Byte Address Instruction Table length and version.
919 * @params: pointer to the 'struct spi_nor_flash_parameter' to be.
921 * Return: 0 on success, -errno otherwise.
923 static int spi_nor_parse_4bait(struct spi_nor *nor,
924 const struct sfdp_parameter_header *param_header,
925 struct spi_nor_flash_parameter *params)
927 static const struct sfdp_4bait reads[] = {
928 { SNOR_HWCAPS_READ, BIT(0) },
929 { SNOR_HWCAPS_READ_FAST, BIT(1) },
930 { SNOR_HWCAPS_READ_1_1_2, BIT(2) },
931 { SNOR_HWCAPS_READ_1_2_2, BIT(3) },
932 { SNOR_HWCAPS_READ_1_1_4, BIT(4) },
933 { SNOR_HWCAPS_READ_1_4_4, BIT(5) },
934 { SNOR_HWCAPS_READ_1_1_1_DTR, BIT(13) },
935 { SNOR_HWCAPS_READ_1_2_2_DTR, BIT(14) },
936 { SNOR_HWCAPS_READ_1_4_4_DTR, BIT(15) },
938 static const struct sfdp_4bait programs[] = {
939 { SNOR_HWCAPS_PP, BIT(6) },
940 { SNOR_HWCAPS_PP_1_1_4, BIT(7) },
941 { SNOR_HWCAPS_PP_1_4_4, BIT(8) },
943 static const struct sfdp_4bait erases[SNOR_ERASE_TYPE_MAX] = {
944 { 0u /* not used */, BIT(9) },
945 { 0u /* not used */, BIT(10) },
946 { 0u /* not used */, BIT(11) },
947 { 0u /* not used */, BIT(12) },
949 struct spi_nor_pp_command *params_pp = params->page_programs;
950 struct spi_nor_erase_map *map = ¶ms->erase_map;
951 struct spi_nor_erase_type *erase_type = map->erase_type;
954 u32 addr, discard_hwcaps, read_hwcaps, pp_hwcaps, erase_mask;
957 if (param_header->major != SFDP_JESD216_MAJOR ||
958 param_header->length < SFDP_4BAIT_DWORD_MAX)
961 /* Read the 4-byte Address Instruction Table. */
962 len = sizeof(*dwords) * SFDP_4BAIT_DWORD_MAX;
964 /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
965 dwords = kmalloc(len, GFP_KERNEL);
969 addr = SFDP_PARAM_HEADER_PTP(param_header);
970 ret = spi_nor_read_sfdp(nor, addr, len, dwords);
974 /* Fix endianness of the 4BAIT DWORDs. */
975 le32_to_cpu_array(dwords, SFDP_4BAIT_DWORD_MAX);
978 * Compute the subset of (Fast) Read commands for which the 4-byte
979 * version is supported.
983 for (i = 0; i < ARRAY_SIZE(reads); i++) {
984 const struct sfdp_4bait *read = &reads[i];
986 discard_hwcaps |= read->hwcaps;
987 if ((params->hwcaps.mask & read->hwcaps) &&
988 (dwords[0] & read->supported_bit))
989 read_hwcaps |= read->hwcaps;
993 * Compute the subset of Page Program commands for which the 4-byte
994 * version is supported.
997 for (i = 0; i < ARRAY_SIZE(programs); i++) {
998 const struct sfdp_4bait *program = &programs[i];
1001 * The 4 Byte Address Instruction (Optional) Table is the only
1002 * SFDP table that indicates support for Page Program Commands.
1003 * Bypass the params->hwcaps.mask and consider 4BAIT the biggest
1004 * authority for specifying Page Program support.
1006 discard_hwcaps |= program->hwcaps;
1007 if (dwords[0] & program->supported_bit)
1008 pp_hwcaps |= program->hwcaps;
1012 * Compute the subset of Sector Erase commands for which the 4-byte
1013 * version is supported.
1016 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1017 const struct sfdp_4bait *erase = &erases[i];
1019 if (dwords[0] & erase->supported_bit)
1020 erase_mask |= BIT(i);
1023 /* Replicate the sort done for the map's erase types in BFPT. */
1024 erase_mask = spi_nor_sort_erase_mask(map, erase_mask);
1027 * We need at least one 4-byte op code per read, program and erase
1028 * operation; the .read(), .write() and .erase() hooks share the
1029 * nor->addr_width value.
1031 if (!read_hwcaps || !pp_hwcaps || !erase_mask)
1035 * Discard all operations from the 4-byte instruction set which are
1036 * not supported by this memory.
1038 params->hwcaps.mask &= ~discard_hwcaps;
1039 params->hwcaps.mask |= (read_hwcaps | pp_hwcaps);
1041 /* Use the 4-byte address instruction set. */
1042 for (i = 0; i < SNOR_CMD_READ_MAX; i++) {
1043 struct spi_nor_read_command *read_cmd = ¶ms->reads[i];
1045 read_cmd->opcode = spi_nor_convert_3to4_read(read_cmd->opcode);
1048 /* 4BAIT is the only SFDP table that indicates page program support. */
1049 if (pp_hwcaps & SNOR_HWCAPS_PP)
1050 spi_nor_set_pp_settings(¶ms_pp[SNOR_CMD_PP],
1051 SPINOR_OP_PP_4B, SNOR_PROTO_1_1_1);
1052 if (pp_hwcaps & SNOR_HWCAPS_PP_1_1_4)
1053 spi_nor_set_pp_settings(¶ms_pp[SNOR_CMD_PP_1_1_4],
1054 SPINOR_OP_PP_1_1_4_4B,
1056 if (pp_hwcaps & SNOR_HWCAPS_PP_1_4_4)
1057 spi_nor_set_pp_settings(¶ms_pp[SNOR_CMD_PP_1_4_4],
1058 SPINOR_OP_PP_1_4_4_4B,
1061 for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1062 if (erase_mask & BIT(i))
1063 erase_type[i].opcode = (dwords[1] >>
1064 erase_type[i].idx * 8) & 0xFF;
1066 spi_nor_set_erase_type(&erase_type[i], 0u, 0xFF);
1070 * We set SNOR_F_HAS_4BAIT in order to skip spi_nor_set_4byte_opcodes()
1071 * later because we already did the conversion to 4byte opcodes. Also,
1072 * this latest function implements a legacy quirk for the erase size of
1073 * Spansion memory. However this quirk is no longer needed with new
1074 * SFDP compliant memories.
1076 nor->addr_width = 4;
1077 nor->flags |= SNOR_F_4B_OPCODES | SNOR_F_HAS_4BAIT;
1086 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
1087 * @nor: pointer to a 'struct spi_nor'
1088 * @params: pointer to the 'struct spi_nor_flash_parameter' to be
1091 * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
1092 * specification. This is a standard which tends to supported by almost all
1093 * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
1094 * runtime the main parameters needed to perform basic SPI flash operations such
1095 * as Fast Read, Page Program or Sector Erase commands.
1097 * Return: 0 on success, -errno otherwise.
1099 int spi_nor_parse_sfdp(struct spi_nor *nor,
1100 struct spi_nor_flash_parameter *params)
1102 const struct sfdp_parameter_header *param_header, *bfpt_header;
1103 struct sfdp_parameter_header *param_headers = NULL;
1104 struct sfdp_header header;
1105 struct device *dev = nor->dev;
1109 /* Get the SFDP header. */
1110 err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(header), &header);
1114 /* Check the SFDP header version. */
1115 if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
1116 header.major != SFDP_JESD216_MAJOR)
1120 * Verify that the first and only mandatory parameter header is a
1121 * Basic Flash Parameter Table header as specified in JESD216.
1123 bfpt_header = &header.bfpt_header;
1124 if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
1125 bfpt_header->major != SFDP_JESD216_MAJOR)
1129 * Allocate memory then read all parameter headers with a single
1130 * Read SFDP command. These parameter headers will actually be parsed
1131 * twice: a first time to get the latest revision of the basic flash
1132 * parameter table, then a second time to handle the supported optional
1134 * Hence we read the parameter headers once for all to reduce the
1135 * processing time. Also we use kmalloc() instead of devm_kmalloc()
1136 * because we don't need to keep these parameter headers: the allocated
1137 * memory is always released with kfree() before exiting this function.
1140 psize = header.nph * sizeof(*param_headers);
1142 param_headers = kmalloc(psize, GFP_KERNEL);
1146 err = spi_nor_read_sfdp(nor, sizeof(header),
1147 psize, param_headers);
1149 dev_dbg(dev, "failed to read SFDP parameter headers\n");
1155 * Check other parameter headers to get the latest revision of
1156 * the basic flash parameter table.
1158 for (i = 0; i < header.nph; i++) {
1159 param_header = ¶m_headers[i];
1161 if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
1162 param_header->major == SFDP_JESD216_MAJOR &&
1163 (param_header->minor > bfpt_header->minor ||
1164 (param_header->minor == bfpt_header->minor &&
1165 param_header->length > bfpt_header->length)))
1166 bfpt_header = param_header;
1169 err = spi_nor_parse_bfpt(nor, bfpt_header, params);
1173 /* Parse optional parameter tables. */
1174 for (i = 0; i < header.nph; i++) {
1175 param_header = ¶m_headers[i];
1177 switch (SFDP_PARAM_HEADER_ID(param_header)) {
1178 case SFDP_SECTOR_MAP_ID:
1179 err = spi_nor_parse_smpt(nor, param_header, params);
1183 err = spi_nor_parse_4bait(nor, param_header, params);
1191 dev_warn(dev, "Failed to parse optional parameter table: %04x\n",
1192 SFDP_PARAM_HEADER_ID(param_header));
1194 * Let's not drop all information we extracted so far
1195 * if optional table parsers fail. In case of failing,
1196 * each optional parser is responsible to roll back to
1197 * the previously known spi_nor data.
1204 kfree(param_headers);