Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright (C) 2005, Intec Automation Inc.
0004  * Copyright (C) 2014, Freescale Semiconductor, Inc.
0005  */
0006 
0007 #include <linux/bitfield.h>
0008 #include <linux/slab.h>
0009 #include <linux/sort.h>
0010 #include <linux/mtd/spi-nor.h>
0011 
0012 #include "core.h"
0013 
0014 #define SFDP_PARAM_HEADER_ID(p) (((p)->id_msb << 8) | (p)->id_lsb)
0015 #define SFDP_PARAM_HEADER_PTP(p) \
0016     (((p)->parameter_table_pointer[2] << 16) | \
0017      ((p)->parameter_table_pointer[1] <<  8) | \
0018      ((p)->parameter_table_pointer[0] <<  0))
0019 #define SFDP_PARAM_HEADER_PARAM_LEN(p) ((p)->length * 4)
0020 
0021 #define SFDP_BFPT_ID        0xff00  /* Basic Flash Parameter Table */
0022 #define SFDP_SECTOR_MAP_ID  0xff81  /* Sector Map Table */
0023 #define SFDP_4BAIT_ID       0xff84  /* 4-byte Address Instruction Table */
0024 #define SFDP_PROFILE1_ID    0xff05  /* xSPI Profile 1.0 table. */
0025 #define SFDP_SCCR_MAP_ID    0xff87  /*
0026                      * Status, Control and Configuration
0027                      * Register Map.
0028                      */
0029 
0030 #define SFDP_SIGNATURE      0x50444653U
0031 
0032 struct sfdp_header {
0033     u32     signature; /* Ox50444653U <=> "SFDP" */
0034     u8      minor;
0035     u8      major;
0036     u8      nph; /* 0-base number of parameter headers */
0037     u8      unused;
0038 
0039     /* Basic Flash Parameter Table. */
0040     struct sfdp_parameter_header    bfpt_header;
0041 };
0042 
0043 /* Fast Read settings. */
0044 struct sfdp_bfpt_read {
0045     /* The Fast Read x-y-z hardware capability in params->hwcaps.mask. */
0046     u32         hwcaps;
0047 
0048     /*
0049      * The <supported_bit> bit in <supported_dword> BFPT DWORD tells us
0050      * whether the Fast Read x-y-z command is supported.
0051      */
0052     u32         supported_dword;
0053     u32         supported_bit;
0054 
0055     /*
0056      * The half-word at offset <setting_shift> in <setting_dword> BFPT DWORD
0057      * encodes the op code, the number of mode clocks and the number of wait
0058      * states to be used by Fast Read x-y-z command.
0059      */
0060     u32         settings_dword;
0061     u32         settings_shift;
0062 
0063     /* The SPI protocol for this Fast Read x-y-z command. */
0064     enum spi_nor_protocol   proto;
0065 };
0066 
0067 struct sfdp_bfpt_erase {
0068     /*
0069      * The half-word at offset <shift> in DWORD <dword> encodes the
0070      * op code and erase sector size to be used by Sector Erase commands.
0071      */
0072     u32         dword;
0073     u32         shift;
0074 };
0075 
0076 #define SMPT_CMD_ADDRESS_LEN_MASK       GENMASK(23, 22)
0077 #define SMPT_CMD_ADDRESS_LEN_0          (0x0UL << 22)
0078 #define SMPT_CMD_ADDRESS_LEN_3          (0x1UL << 22)
0079 #define SMPT_CMD_ADDRESS_LEN_4          (0x2UL << 22)
0080 #define SMPT_CMD_ADDRESS_LEN_USE_CURRENT    (0x3UL << 22)
0081 
0082 #define SMPT_CMD_READ_DUMMY_MASK        GENMASK(19, 16)
0083 #define SMPT_CMD_READ_DUMMY_SHIFT       16
0084 #define SMPT_CMD_READ_DUMMY(_cmd) \
0085     (((_cmd) & SMPT_CMD_READ_DUMMY_MASK) >> SMPT_CMD_READ_DUMMY_SHIFT)
0086 #define SMPT_CMD_READ_DUMMY_IS_VARIABLE     0xfUL
0087 
0088 #define SMPT_CMD_READ_DATA_MASK         GENMASK(31, 24)
0089 #define SMPT_CMD_READ_DATA_SHIFT        24
0090 #define SMPT_CMD_READ_DATA(_cmd) \
0091     (((_cmd) & SMPT_CMD_READ_DATA_MASK) >> SMPT_CMD_READ_DATA_SHIFT)
0092 
0093 #define SMPT_CMD_OPCODE_MASK            GENMASK(15, 8)
0094 #define SMPT_CMD_OPCODE_SHIFT           8
0095 #define SMPT_CMD_OPCODE(_cmd) \
0096     (((_cmd) & SMPT_CMD_OPCODE_MASK) >> SMPT_CMD_OPCODE_SHIFT)
0097 
0098 #define SMPT_MAP_REGION_COUNT_MASK      GENMASK(23, 16)
0099 #define SMPT_MAP_REGION_COUNT_SHIFT     16
0100 #define SMPT_MAP_REGION_COUNT(_header) \
0101     ((((_header) & SMPT_MAP_REGION_COUNT_MASK) >> \
0102       SMPT_MAP_REGION_COUNT_SHIFT) + 1)
0103 
0104 #define SMPT_MAP_ID_MASK            GENMASK(15, 8)
0105 #define SMPT_MAP_ID_SHIFT           8
0106 #define SMPT_MAP_ID(_header) \
0107     (((_header) & SMPT_MAP_ID_MASK) >> SMPT_MAP_ID_SHIFT)
0108 
0109 #define SMPT_MAP_REGION_SIZE_MASK       GENMASK(31, 8)
0110 #define SMPT_MAP_REGION_SIZE_SHIFT      8
0111 #define SMPT_MAP_REGION_SIZE(_region) \
0112     (((((_region) & SMPT_MAP_REGION_SIZE_MASK) >> \
0113        SMPT_MAP_REGION_SIZE_SHIFT) + 1) * 256)
0114 
0115 #define SMPT_MAP_REGION_ERASE_TYPE_MASK     GENMASK(3, 0)
0116 #define SMPT_MAP_REGION_ERASE_TYPE(_region) \
0117     ((_region) & SMPT_MAP_REGION_ERASE_TYPE_MASK)
0118 
0119 #define SMPT_DESC_TYPE_MAP          BIT(1)
0120 #define SMPT_DESC_END               BIT(0)
0121 
0122 #define SFDP_4BAIT_DWORD_MAX    2
0123 
0124 struct sfdp_4bait {
0125     /* The hardware capability. */
0126     u32     hwcaps;
0127 
0128     /*
0129      * The <supported_bit> bit in DWORD1 of the 4BAIT tells us whether
0130      * the associated 4-byte address op code is supported.
0131      */
0132     u32     supported_bit;
0133 };
0134 
0135 /**
0136  * spi_nor_read_raw() - raw read of serial flash memory. read_opcode,
0137  *          addr_nbytes and read_dummy members of the struct spi_nor
0138  *          should be previously
0139  * set.
0140  * @nor:    pointer to a 'struct spi_nor'
0141  * @addr:   offset in the serial flash memory
0142  * @len:    number of bytes to read
0143  * @buf:    buffer where the data is copied into (dma-safe memory)
0144  *
0145  * Return: 0 on success, -errno otherwise.
0146  */
0147 static int spi_nor_read_raw(struct spi_nor *nor, u32 addr, size_t len, u8 *buf)
0148 {
0149     ssize_t ret;
0150 
0151     while (len) {
0152         ret = spi_nor_read_data(nor, addr, len, buf);
0153         if (ret < 0)
0154             return ret;
0155         if (!ret || ret > len)
0156             return -EIO;
0157 
0158         buf += ret;
0159         addr += ret;
0160         len -= ret;
0161     }
0162     return 0;
0163 }
0164 
0165 /**
0166  * spi_nor_read_sfdp() - read Serial Flash Discoverable Parameters.
0167  * @nor:    pointer to a 'struct spi_nor'
0168  * @addr:   offset in the SFDP area to start reading data from
0169  * @len:    number of bytes to read
0170  * @buf:    buffer where the SFDP data are copied into (dma-safe memory)
0171  *
0172  * Whatever the actual numbers of bytes for address and dummy cycles are
0173  * for (Fast) Read commands, the Read SFDP (5Ah) instruction is always
0174  * followed by a 3-byte address and 8 dummy clock cycles.
0175  *
0176  * Return: 0 on success, -errno otherwise.
0177  */
0178 static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
0179                  size_t len, void *buf)
0180 {
0181     u8 addr_nbytes, read_opcode, read_dummy;
0182     int ret;
0183 
0184     read_opcode = nor->read_opcode;
0185     addr_nbytes = nor->addr_nbytes;
0186     read_dummy = nor->read_dummy;
0187 
0188     nor->read_opcode = SPINOR_OP_RDSFDP;
0189     nor->addr_nbytes = 3;
0190     nor->read_dummy = 8;
0191 
0192     ret = spi_nor_read_raw(nor, addr, len, buf);
0193 
0194     nor->read_opcode = read_opcode;
0195     nor->addr_nbytes = addr_nbytes;
0196     nor->read_dummy = read_dummy;
0197 
0198     return ret;
0199 }
0200 
0201 /**
0202  * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
0203  * @nor:    pointer to a 'struct spi_nor'
0204  * @addr:   offset in the SFDP area to start reading data from
0205  * @len:    number of bytes to read
0206  * @buf:    buffer where the SFDP data are copied into
0207  *
0208  * Wrap spi_nor_read_sfdp() using a kmalloc'ed bounce buffer as @buf is now not
0209  * guaranteed to be dma-safe.
0210  *
0211  * Return: -ENOMEM if kmalloc() fails, the return code of spi_nor_read_sfdp()
0212  *          otherwise.
0213  */
0214 static int spi_nor_read_sfdp_dma_unsafe(struct spi_nor *nor, u32 addr,
0215                     size_t len, void *buf)
0216 {
0217     void *dma_safe_buf;
0218     int ret;
0219 
0220     dma_safe_buf = kmalloc(len, GFP_KERNEL);
0221     if (!dma_safe_buf)
0222         return -ENOMEM;
0223 
0224     ret = spi_nor_read_sfdp(nor, addr, len, dma_safe_buf);
0225     memcpy(buf, dma_safe_buf, len);
0226     kfree(dma_safe_buf);
0227 
0228     return ret;
0229 }
0230 
0231 static void
0232 spi_nor_set_read_settings_from_bfpt(struct spi_nor_read_command *read,
0233                     u16 half,
0234                     enum spi_nor_protocol proto)
0235 {
0236     read->num_mode_clocks = (half >> 5) & 0x07;
0237     read->num_wait_states = (half >> 0) & 0x1f;
0238     read->opcode = (half >> 8) & 0xff;
0239     read->proto = proto;
0240 }
0241 
0242 static const struct sfdp_bfpt_read sfdp_bfpt_reads[] = {
0243     /* Fast Read 1-1-2 */
0244     {
0245         SNOR_HWCAPS_READ_1_1_2,
0246         BFPT_DWORD(1), BIT(16), /* Supported bit */
0247         BFPT_DWORD(4), 0,   /* Settings */
0248         SNOR_PROTO_1_1_2,
0249     },
0250 
0251     /* Fast Read 1-2-2 */
0252     {
0253         SNOR_HWCAPS_READ_1_2_2,
0254         BFPT_DWORD(1), BIT(20), /* Supported bit */
0255         BFPT_DWORD(4), 16,  /* Settings */
0256         SNOR_PROTO_1_2_2,
0257     },
0258 
0259     /* Fast Read 2-2-2 */
0260     {
0261         SNOR_HWCAPS_READ_2_2_2,
0262         BFPT_DWORD(5),  BIT(0), /* Supported bit */
0263         BFPT_DWORD(6), 16,  /* Settings */
0264         SNOR_PROTO_2_2_2,
0265     },
0266 
0267     /* Fast Read 1-1-4 */
0268     {
0269         SNOR_HWCAPS_READ_1_1_4,
0270         BFPT_DWORD(1), BIT(22), /* Supported bit */
0271         BFPT_DWORD(3), 16,  /* Settings */
0272         SNOR_PROTO_1_1_4,
0273     },
0274 
0275     /* Fast Read 1-4-4 */
0276     {
0277         SNOR_HWCAPS_READ_1_4_4,
0278         BFPT_DWORD(1), BIT(21), /* Supported bit */
0279         BFPT_DWORD(3), 0,   /* Settings */
0280         SNOR_PROTO_1_4_4,
0281     },
0282 
0283     /* Fast Read 4-4-4 */
0284     {
0285         SNOR_HWCAPS_READ_4_4_4,
0286         BFPT_DWORD(5), BIT(4),  /* Supported bit */
0287         BFPT_DWORD(7), 16,  /* Settings */
0288         SNOR_PROTO_4_4_4,
0289     },
0290 };
0291 
0292 static const struct sfdp_bfpt_erase sfdp_bfpt_erases[] = {
0293     /* Erase Type 1 in DWORD8 bits[15:0] */
0294     {BFPT_DWORD(8), 0},
0295 
0296     /* Erase Type 2 in DWORD8 bits[31:16] */
0297     {BFPT_DWORD(8), 16},
0298 
0299     /* Erase Type 3 in DWORD9 bits[15:0] */
0300     {BFPT_DWORD(9), 0},
0301 
0302     /* Erase Type 4 in DWORD9 bits[31:16] */
0303     {BFPT_DWORD(9), 16},
0304 };
0305 
0306 /**
0307  * spi_nor_set_erase_settings_from_bfpt() - set erase type settings from BFPT
0308  * @erase:  pointer to a structure that describes a SPI NOR erase type
0309  * @size:   the size of the sector/block erased by the erase type
0310  * @opcode: the SPI command op code to erase the sector/block
0311  * @i:      erase type index as sorted in the Basic Flash Parameter Table
0312  *
0313  * The supported Erase Types will be sorted at init in ascending order, with
0314  * the smallest Erase Type size being the first member in the erase_type array
0315  * of the spi_nor_erase_map structure. Save the Erase Type index as sorted in
0316  * the Basic Flash Parameter Table since it will be used later on to
0317  * synchronize with the supported Erase Types defined in SFDP optional tables.
0318  */
0319 static void
0320 spi_nor_set_erase_settings_from_bfpt(struct spi_nor_erase_type *erase,
0321                      u32 size, u8 opcode, u8 i)
0322 {
0323     erase->idx = i;
0324     spi_nor_set_erase_type(erase, size, opcode);
0325 }
0326 
0327 /**
0328  * spi_nor_map_cmp_erase_type() - compare the map's erase types by size
0329  * @l:  member in the left half of the map's erase_type array
0330  * @r:  member in the right half of the map's erase_type array
0331  *
0332  * Comparison function used in the sort() call to sort in ascending order the
0333  * map's erase types, the smallest erase type size being the first member in the
0334  * sorted erase_type array.
0335  *
0336  * Return: the result of @l->size - @r->size
0337  */
0338 static int spi_nor_map_cmp_erase_type(const void *l, const void *r)
0339 {
0340     const struct spi_nor_erase_type *left = l, *right = r;
0341 
0342     return left->size - right->size;
0343 }
0344 
0345 /**
0346  * spi_nor_sort_erase_mask() - sort erase mask
0347  * @map:    the erase map of the SPI NOR
0348  * @erase_mask: the erase type mask to be sorted
0349  *
0350  * Replicate the sort done for the map's erase types in BFPT: sort the erase
0351  * mask in ascending order with the smallest erase type size starting from
0352  * BIT(0) in the sorted erase mask.
0353  *
0354  * Return: sorted erase mask.
0355  */
0356 static u8 spi_nor_sort_erase_mask(struct spi_nor_erase_map *map, u8 erase_mask)
0357 {
0358     struct spi_nor_erase_type *erase_type = map->erase_type;
0359     int i;
0360     u8 sorted_erase_mask = 0;
0361 
0362     if (!erase_mask)
0363         return 0;
0364 
0365     /* Replicate the sort done for the map's erase types. */
0366     for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
0367         if (erase_type[i].size && erase_mask & BIT(erase_type[i].idx))
0368             sorted_erase_mask |= BIT(i);
0369 
0370     return sorted_erase_mask;
0371 }
0372 
0373 /**
0374  * spi_nor_regions_sort_erase_types() - sort erase types in each region
0375  * @map:    the erase map of the SPI NOR
0376  *
0377  * Function assumes that the erase types defined in the erase map are already
0378  * sorted in ascending order, with the smallest erase type size being the first
0379  * member in the erase_type array. It replicates the sort done for the map's
0380  * erase types. Each region's erase bitmask will indicate which erase types are
0381  * supported from the sorted erase types defined in the erase map.
0382  * Sort the all region's erase type at init in order to speed up the process of
0383  * finding the best erase command at runtime.
0384  */
0385 static void spi_nor_regions_sort_erase_types(struct spi_nor_erase_map *map)
0386 {
0387     struct spi_nor_erase_region *region = map->regions;
0388     u8 region_erase_mask, sorted_erase_mask;
0389 
0390     while (region) {
0391         region_erase_mask = region->offset & SNOR_ERASE_TYPE_MASK;
0392 
0393         sorted_erase_mask = spi_nor_sort_erase_mask(map,
0394                                 region_erase_mask);
0395 
0396         /* Overwrite erase mask. */
0397         region->offset = (region->offset & ~SNOR_ERASE_TYPE_MASK) |
0398                  sorted_erase_mask;
0399 
0400         region = spi_nor_region_next(region);
0401     }
0402 }
0403 
0404 /**
0405  * spi_nor_parse_bfpt() - read and parse the Basic Flash Parameter Table.
0406  * @nor:        pointer to a 'struct spi_nor'
0407  * @bfpt_header:    pointer to the 'struct sfdp_parameter_header' describing
0408  *          the Basic Flash Parameter Table length and version
0409  *
0410  * The Basic Flash Parameter Table is the main and only mandatory table as
0411  * defined by the SFDP (JESD216) specification.
0412  * It provides us with the total size (memory density) of the data array and
0413  * the number of address bytes for Fast Read, Page Program and Sector Erase
0414  * commands.
0415  * For Fast READ commands, it also gives the number of mode clock cycles and
0416  * wait states (regrouped in the number of dummy clock cycles) for each
0417  * supported instruction op code.
0418  * For Page Program, the page size is now available since JESD216 rev A, however
0419  * the supported instruction op codes are still not provided.
0420  * For Sector Erase commands, this table stores the supported instruction op
0421  * codes and the associated sector sizes.
0422  * Finally, the Quad Enable Requirements (QER) are also available since JESD216
0423  * rev A. The QER bits encode the manufacturer dependent procedure to be
0424  * executed to set the Quad Enable (QE) bit in some internal register of the
0425  * Quad SPI memory. Indeed the QE bit, when it exists, must be set before
0426  * sending any Quad SPI command to the memory. Actually, setting the QE bit
0427  * tells the memory to reassign its WP# and HOLD#/RESET# pins to functions IO2
0428  * and IO3 hence enabling 4 (Quad) I/O lines.
0429  *
0430  * Return: 0 on success, -errno otherwise.
0431  */
0432 static int spi_nor_parse_bfpt(struct spi_nor *nor,
0433                   const struct sfdp_parameter_header *bfpt_header)
0434 {
0435     struct spi_nor_flash_parameter *params = nor->params;
0436     struct spi_nor_erase_map *map = &params->erase_map;
0437     struct spi_nor_erase_type *erase_type = map->erase_type;
0438     struct sfdp_bfpt bfpt;
0439     size_t len;
0440     int i, cmd, err;
0441     u32 addr, val;
0442     u16 half;
0443     u8 erase_mask;
0444 
0445     /* JESD216 Basic Flash Parameter Table length is at least 9 DWORDs. */
0446     if (bfpt_header->length < BFPT_DWORD_MAX_JESD216)
0447         return -EINVAL;
0448 
0449     /* Read the Basic Flash Parameter Table. */
0450     len = min_t(size_t, sizeof(bfpt),
0451             bfpt_header->length * sizeof(u32));
0452     addr = SFDP_PARAM_HEADER_PTP(bfpt_header);
0453     memset(&bfpt, 0, sizeof(bfpt));
0454     err = spi_nor_read_sfdp_dma_unsafe(nor,  addr, len, &bfpt);
0455     if (err < 0)
0456         return err;
0457 
0458     /* Fix endianness of the BFPT DWORDs. */
0459     le32_to_cpu_array(bfpt.dwords, BFPT_DWORD_MAX);
0460 
0461     /* Number of address bytes. */
0462     switch (bfpt.dwords[BFPT_DWORD(1)] & BFPT_DWORD1_ADDRESS_BYTES_MASK) {
0463     case BFPT_DWORD1_ADDRESS_BYTES_3_ONLY:
0464     case BFPT_DWORD1_ADDRESS_BYTES_3_OR_4:
0465         params->addr_nbytes = 3;
0466         params->addr_mode_nbytes = 3;
0467         break;
0468 
0469     case BFPT_DWORD1_ADDRESS_BYTES_4_ONLY:
0470         params->addr_nbytes = 4;
0471         params->addr_mode_nbytes = 4;
0472         break;
0473 
0474     default:
0475         break;
0476     }
0477 
0478     /* Flash Memory Density (in bits). */
0479     val = bfpt.dwords[BFPT_DWORD(2)];
0480     if (val & BIT(31)) {
0481         val &= ~BIT(31);
0482 
0483         /*
0484          * Prevent overflows on params->size. Anyway, a NOR of 2^64
0485          * bits is unlikely to exist so this error probably means
0486          * the BFPT we are reading is corrupted/wrong.
0487          */
0488         if (val > 63)
0489             return -EINVAL;
0490 
0491         params->size = 1ULL << val;
0492     } else {
0493         params->size = val + 1;
0494     }
0495     params->size >>= 3; /* Convert to bytes. */
0496 
0497     /* Fast Read settings. */
0498     for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_reads); i++) {
0499         const struct sfdp_bfpt_read *rd = &sfdp_bfpt_reads[i];
0500         struct spi_nor_read_command *read;
0501 
0502         if (!(bfpt.dwords[rd->supported_dword] & rd->supported_bit)) {
0503             params->hwcaps.mask &= ~rd->hwcaps;
0504             continue;
0505         }
0506 
0507         params->hwcaps.mask |= rd->hwcaps;
0508         cmd = spi_nor_hwcaps_read2cmd(rd->hwcaps);
0509         read = &params->reads[cmd];
0510         half = bfpt.dwords[rd->settings_dword] >> rd->settings_shift;
0511         spi_nor_set_read_settings_from_bfpt(read, half, rd->proto);
0512     }
0513 
0514     /*
0515      * Sector Erase settings. Reinitialize the uniform erase map using the
0516      * Erase Types defined in the bfpt table.
0517      */
0518     erase_mask = 0;
0519     memset(&params->erase_map, 0, sizeof(params->erase_map));
0520     for (i = 0; i < ARRAY_SIZE(sfdp_bfpt_erases); i++) {
0521         const struct sfdp_bfpt_erase *er = &sfdp_bfpt_erases[i];
0522         u32 erasesize;
0523         u8 opcode;
0524 
0525         half = bfpt.dwords[er->dword] >> er->shift;
0526         erasesize = half & 0xff;
0527 
0528         /* erasesize == 0 means this Erase Type is not supported. */
0529         if (!erasesize)
0530             continue;
0531 
0532         erasesize = 1U << erasesize;
0533         opcode = (half >> 8) & 0xff;
0534         erase_mask |= BIT(i);
0535         spi_nor_set_erase_settings_from_bfpt(&erase_type[i], erasesize,
0536                              opcode, i);
0537     }
0538     spi_nor_init_uniform_erase_map(map, erase_mask, params->size);
0539     /*
0540      * Sort all the map's Erase Types in ascending order with the smallest
0541      * erase size being the first member in the erase_type array.
0542      */
0543     sort(erase_type, SNOR_ERASE_TYPE_MAX, sizeof(erase_type[0]),
0544          spi_nor_map_cmp_erase_type, NULL);
0545     /*
0546      * Sort the erase types in the uniform region in order to update the
0547      * uniform_erase_type bitmask. The bitmask will be used later on when
0548      * selecting the uniform erase.
0549      */
0550     spi_nor_regions_sort_erase_types(map);
0551     map->uniform_erase_type = map->uniform_region.offset &
0552                   SNOR_ERASE_TYPE_MASK;
0553 
0554     /* Stop here if not JESD216 rev A or later. */
0555     if (bfpt_header->length == BFPT_DWORD_MAX_JESD216)
0556         return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt);
0557 
0558     /* Page size: this field specifies 'N' so the page size = 2^N bytes. */
0559     val = bfpt.dwords[BFPT_DWORD(11)];
0560     val &= BFPT_DWORD11_PAGE_SIZE_MASK;
0561     val >>= BFPT_DWORD11_PAGE_SIZE_SHIFT;
0562     params->page_size = 1U << val;
0563 
0564     /* Quad Enable Requirements. */
0565     switch (bfpt.dwords[BFPT_DWORD(15)] & BFPT_DWORD15_QER_MASK) {
0566     case BFPT_DWORD15_QER_NONE:
0567         params->quad_enable = NULL;
0568         break;
0569 
0570     case BFPT_DWORD15_QER_SR2_BIT1_BUGGY:
0571         /*
0572          * Writing only one byte to the Status Register has the
0573          * side-effect of clearing Status Register 2.
0574          */
0575     case BFPT_DWORD15_QER_SR2_BIT1_NO_RD:
0576         /*
0577          * Read Configuration Register (35h) instruction is not
0578          * supported.
0579          */
0580         nor->flags |= SNOR_F_HAS_16BIT_SR | SNOR_F_NO_READ_CR;
0581         params->quad_enable = spi_nor_sr2_bit1_quad_enable;
0582         break;
0583 
0584     case BFPT_DWORD15_QER_SR1_BIT6:
0585         nor->flags &= ~SNOR_F_HAS_16BIT_SR;
0586         params->quad_enable = spi_nor_sr1_bit6_quad_enable;
0587         break;
0588 
0589     case BFPT_DWORD15_QER_SR2_BIT7:
0590         nor->flags &= ~SNOR_F_HAS_16BIT_SR;
0591         params->quad_enable = spi_nor_sr2_bit7_quad_enable;
0592         break;
0593 
0594     case BFPT_DWORD15_QER_SR2_BIT1:
0595         /*
0596          * JESD216 rev B or later does not specify if writing only one
0597          * byte to the Status Register clears or not the Status
0598          * Register 2, so let's be cautious and keep the default
0599          * assumption of a 16-bit Write Status (01h) command.
0600          */
0601         nor->flags |= SNOR_F_HAS_16BIT_SR;
0602 
0603         params->quad_enable = spi_nor_sr2_bit1_quad_enable;
0604         break;
0605 
0606     default:
0607         dev_dbg(nor->dev, "BFPT QER reserved value used\n");
0608         break;
0609     }
0610 
0611     /* Soft Reset support. */
0612     if (bfpt.dwords[BFPT_DWORD(16)] & BFPT_DWORD16_SWRST_EN_RST)
0613         nor->flags |= SNOR_F_SOFT_RESET;
0614 
0615     /* Stop here if not JESD216 rev C or later. */
0616     if (bfpt_header->length == BFPT_DWORD_MAX_JESD216B)
0617         return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt);
0618 
0619     /* 8D-8D-8D command extension. */
0620     switch (bfpt.dwords[BFPT_DWORD(18)] & BFPT_DWORD18_CMD_EXT_MASK) {
0621     case BFPT_DWORD18_CMD_EXT_REP:
0622         nor->cmd_ext_type = SPI_NOR_EXT_REPEAT;
0623         break;
0624 
0625     case BFPT_DWORD18_CMD_EXT_INV:
0626         nor->cmd_ext_type = SPI_NOR_EXT_INVERT;
0627         break;
0628 
0629     case BFPT_DWORD18_CMD_EXT_RES:
0630         dev_dbg(nor->dev, "Reserved command extension used\n");
0631         break;
0632 
0633     case BFPT_DWORD18_CMD_EXT_16B:
0634         dev_dbg(nor->dev, "16-bit opcodes not supported\n");
0635         return -EOPNOTSUPP;
0636     }
0637 
0638     return spi_nor_post_bfpt_fixups(nor, bfpt_header, &bfpt);
0639 }
0640 
0641 /**
0642  * spi_nor_smpt_addr_nbytes() - return the number of address bytes used in the
0643  *                 configuration detection command.
0644  * @nor:    pointer to a 'struct spi_nor'
0645  * @settings:   configuration detection command descriptor, dword1
0646  */
0647 static u8 spi_nor_smpt_addr_nbytes(const struct spi_nor *nor, const u32 settings)
0648 {
0649     switch (settings & SMPT_CMD_ADDRESS_LEN_MASK) {
0650     case SMPT_CMD_ADDRESS_LEN_0:
0651         return 0;
0652     case SMPT_CMD_ADDRESS_LEN_3:
0653         return 3;
0654     case SMPT_CMD_ADDRESS_LEN_4:
0655         return 4;
0656     case SMPT_CMD_ADDRESS_LEN_USE_CURRENT:
0657     default:
0658         return nor->params->addr_mode_nbytes;
0659     }
0660 }
0661 
0662 /**
0663  * spi_nor_smpt_read_dummy() - return the configuration detection command read
0664  *                 latency, in clock cycles.
0665  * @nor:    pointer to a 'struct spi_nor'
0666  * @settings:   configuration detection command descriptor, dword1
0667  *
0668  * Return: the number of dummy cycles for an SMPT read
0669  */
0670 static u8 spi_nor_smpt_read_dummy(const struct spi_nor *nor, const u32 settings)
0671 {
0672     u8 read_dummy = SMPT_CMD_READ_DUMMY(settings);
0673 
0674     if (read_dummy == SMPT_CMD_READ_DUMMY_IS_VARIABLE)
0675         return nor->read_dummy;
0676     return read_dummy;
0677 }
0678 
0679 /**
0680  * spi_nor_get_map_in_use() - get the configuration map in use
0681  * @nor:    pointer to a 'struct spi_nor'
0682  * @smpt:   pointer to the sector map parameter table
0683  * @smpt_len:   sector map parameter table length
0684  *
0685  * Return: pointer to the map in use, ERR_PTR(-errno) otherwise.
0686  */
0687 static const u32 *spi_nor_get_map_in_use(struct spi_nor *nor, const u32 *smpt,
0688                      u8 smpt_len)
0689 {
0690     const u32 *ret;
0691     u8 *buf;
0692     u32 addr;
0693     int err;
0694     u8 i;
0695     u8 addr_nbytes, read_opcode, read_dummy;
0696     u8 read_data_mask, map_id;
0697 
0698     /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
0699     buf = kmalloc(sizeof(*buf), GFP_KERNEL);
0700     if (!buf)
0701         return ERR_PTR(-ENOMEM);
0702 
0703     addr_nbytes = nor->addr_nbytes;
0704     read_dummy = nor->read_dummy;
0705     read_opcode = nor->read_opcode;
0706 
0707     map_id = 0;
0708     /* Determine if there are any optional Detection Command Descriptors */
0709     for (i = 0; i < smpt_len; i += 2) {
0710         if (smpt[i] & SMPT_DESC_TYPE_MAP)
0711             break;
0712 
0713         read_data_mask = SMPT_CMD_READ_DATA(smpt[i]);
0714         nor->addr_nbytes = spi_nor_smpt_addr_nbytes(nor, smpt[i]);
0715         nor->read_dummy = spi_nor_smpt_read_dummy(nor, smpt[i]);
0716         nor->read_opcode = SMPT_CMD_OPCODE(smpt[i]);
0717         addr = smpt[i + 1];
0718 
0719         err = spi_nor_read_raw(nor, addr, 1, buf);
0720         if (err) {
0721             ret = ERR_PTR(err);
0722             goto out;
0723         }
0724 
0725         /*
0726          * Build an index value that is used to select the Sector Map
0727          * Configuration that is currently in use.
0728          */
0729         map_id = map_id << 1 | !!(*buf & read_data_mask);
0730     }
0731 
0732     /*
0733      * If command descriptors are provided, they always precede map
0734      * descriptors in the table. There is no need to start the iteration
0735      * over smpt array all over again.
0736      *
0737      * Find the matching configuration map.
0738      */
0739     ret = ERR_PTR(-EINVAL);
0740     while (i < smpt_len) {
0741         if (SMPT_MAP_ID(smpt[i]) == map_id) {
0742             ret = smpt + i;
0743             break;
0744         }
0745 
0746         /*
0747          * If there are no more configuration map descriptors and no
0748          * configuration ID matched the configuration identifier, the
0749          * sector address map is unknown.
0750          */
0751         if (smpt[i] & SMPT_DESC_END)
0752             break;
0753 
0754         /* increment the table index to the next map */
0755         i += SMPT_MAP_REGION_COUNT(smpt[i]) + 1;
0756     }
0757 
0758     /* fall through */
0759 out:
0760     kfree(buf);
0761     nor->addr_nbytes = addr_nbytes;
0762     nor->read_dummy = read_dummy;
0763     nor->read_opcode = read_opcode;
0764     return ret;
0765 }
0766 
0767 static void spi_nor_region_mark_end(struct spi_nor_erase_region *region)
0768 {
0769     region->offset |= SNOR_LAST_REGION;
0770 }
0771 
0772 static void spi_nor_region_mark_overlay(struct spi_nor_erase_region *region)
0773 {
0774     region->offset |= SNOR_OVERLAID_REGION;
0775 }
0776 
0777 /**
0778  * spi_nor_region_check_overlay() - set overlay bit when the region is overlaid
0779  * @region: pointer to a structure that describes a SPI NOR erase region
0780  * @erase:  pointer to a structure that describes a SPI NOR erase type
0781  * @erase_type: erase type bitmask
0782  */
0783 static void
0784 spi_nor_region_check_overlay(struct spi_nor_erase_region *region,
0785                  const struct spi_nor_erase_type *erase,
0786                  const u8 erase_type)
0787 {
0788     int i;
0789 
0790     for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
0791         if (!(erase[i].size && erase_type & BIT(erase[i].idx)))
0792             continue;
0793         if (region->size & erase[i].size_mask) {
0794             spi_nor_region_mark_overlay(region);
0795             return;
0796         }
0797     }
0798 }
0799 
0800 /**
0801  * spi_nor_init_non_uniform_erase_map() - initialize the non-uniform erase map
0802  * @nor:    pointer to a 'struct spi_nor'
0803  * @smpt:   pointer to the sector map parameter table
0804  *
0805  * Return: 0 on success, -errno otherwise.
0806  */
0807 static int spi_nor_init_non_uniform_erase_map(struct spi_nor *nor,
0808                           const u32 *smpt)
0809 {
0810     struct spi_nor_erase_map *map = &nor->params->erase_map;
0811     struct spi_nor_erase_type *erase = map->erase_type;
0812     struct spi_nor_erase_region *region;
0813     u64 offset;
0814     u32 region_count;
0815     int i, j;
0816     u8 uniform_erase_type, save_uniform_erase_type;
0817     u8 erase_type, regions_erase_type;
0818 
0819     region_count = SMPT_MAP_REGION_COUNT(*smpt);
0820     /*
0821      * The regions will be freed when the driver detaches from the
0822      * device.
0823      */
0824     region = devm_kcalloc(nor->dev, region_count, sizeof(*region),
0825                   GFP_KERNEL);
0826     if (!region)
0827         return -ENOMEM;
0828     map->regions = region;
0829 
0830     uniform_erase_type = 0xff;
0831     regions_erase_type = 0;
0832     offset = 0;
0833     /* Populate regions. */
0834     for (i = 0; i < region_count; i++) {
0835         j = i + 1; /* index for the region dword */
0836         region[i].size = SMPT_MAP_REGION_SIZE(smpt[j]);
0837         erase_type = SMPT_MAP_REGION_ERASE_TYPE(smpt[j]);
0838         region[i].offset = offset | erase_type;
0839 
0840         spi_nor_region_check_overlay(&region[i], erase, erase_type);
0841 
0842         /*
0843          * Save the erase types that are supported in all regions and
0844          * can erase the entire flash memory.
0845          */
0846         uniform_erase_type &= erase_type;
0847 
0848         /*
0849          * regions_erase_type mask will indicate all the erase types
0850          * supported in this configuration map.
0851          */
0852         regions_erase_type |= erase_type;
0853 
0854         offset = (region[i].offset & ~SNOR_ERASE_FLAGS_MASK) +
0855              region[i].size;
0856     }
0857     spi_nor_region_mark_end(&region[i - 1]);
0858 
0859     save_uniform_erase_type = map->uniform_erase_type;
0860     map->uniform_erase_type = spi_nor_sort_erase_mask(map,
0861                               uniform_erase_type);
0862 
0863     if (!regions_erase_type) {
0864         /*
0865          * Roll back to the previous uniform_erase_type mask, SMPT is
0866          * broken.
0867          */
0868         map->uniform_erase_type = save_uniform_erase_type;
0869         return -EINVAL;
0870     }
0871 
0872     /*
0873      * BFPT advertises all the erase types supported by all the possible
0874      * map configurations. Mask out the erase types that are not supported
0875      * by the current map configuration.
0876      */
0877     for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++)
0878         if (!(regions_erase_type & BIT(erase[i].idx)))
0879             spi_nor_set_erase_type(&erase[i], 0, 0xFF);
0880 
0881     return 0;
0882 }
0883 
0884 /**
0885  * spi_nor_parse_smpt() - parse Sector Map Parameter Table
0886  * @nor:        pointer to a 'struct spi_nor'
0887  * @smpt_header:    sector map parameter table header
0888  *
0889  * This table is optional, but when available, we parse it to identify the
0890  * location and size of sectors within the main data array of the flash memory
0891  * device and to identify which Erase Types are supported by each sector.
0892  *
0893  * Return: 0 on success, -errno otherwise.
0894  */
0895 static int spi_nor_parse_smpt(struct spi_nor *nor,
0896                   const struct sfdp_parameter_header *smpt_header)
0897 {
0898     const u32 *sector_map;
0899     u32 *smpt;
0900     size_t len;
0901     u32 addr;
0902     int ret;
0903 
0904     /* Read the Sector Map Parameter Table. */
0905     len = smpt_header->length * sizeof(*smpt);
0906     smpt = kmalloc(len, GFP_KERNEL);
0907     if (!smpt)
0908         return -ENOMEM;
0909 
0910     addr = SFDP_PARAM_HEADER_PTP(smpt_header);
0911     ret = spi_nor_read_sfdp(nor, addr, len, smpt);
0912     if (ret)
0913         goto out;
0914 
0915     /* Fix endianness of the SMPT DWORDs. */
0916     le32_to_cpu_array(smpt, smpt_header->length);
0917 
0918     sector_map = spi_nor_get_map_in_use(nor, smpt, smpt_header->length);
0919     if (IS_ERR(sector_map)) {
0920         ret = PTR_ERR(sector_map);
0921         goto out;
0922     }
0923 
0924     ret = spi_nor_init_non_uniform_erase_map(nor, sector_map);
0925     if (ret)
0926         goto out;
0927 
0928     spi_nor_regions_sort_erase_types(&nor->params->erase_map);
0929     /* fall through */
0930 out:
0931     kfree(smpt);
0932     return ret;
0933 }
0934 
0935 /**
0936  * spi_nor_parse_4bait() - parse the 4-Byte Address Instruction Table
0937  * @nor:        pointer to a 'struct spi_nor'.
0938  * @param_header:   pointer to the 'struct sfdp_parameter_header' describing
0939  *          the 4-Byte Address Instruction Table length and version.
0940  *
0941  * Return: 0 on success, -errno otherwise.
0942  */
0943 static int spi_nor_parse_4bait(struct spi_nor *nor,
0944                    const struct sfdp_parameter_header *param_header)
0945 {
0946     static const struct sfdp_4bait reads[] = {
0947         { SNOR_HWCAPS_READ,     BIT(0) },
0948         { SNOR_HWCAPS_READ_FAST,    BIT(1) },
0949         { SNOR_HWCAPS_READ_1_1_2,   BIT(2) },
0950         { SNOR_HWCAPS_READ_1_2_2,   BIT(3) },
0951         { SNOR_HWCAPS_READ_1_1_4,   BIT(4) },
0952         { SNOR_HWCAPS_READ_1_4_4,   BIT(5) },
0953         { SNOR_HWCAPS_READ_1_1_1_DTR,   BIT(13) },
0954         { SNOR_HWCAPS_READ_1_2_2_DTR,   BIT(14) },
0955         { SNOR_HWCAPS_READ_1_4_4_DTR,   BIT(15) },
0956     };
0957     static const struct sfdp_4bait programs[] = {
0958         { SNOR_HWCAPS_PP,       BIT(6) },
0959         { SNOR_HWCAPS_PP_1_1_4,     BIT(7) },
0960         { SNOR_HWCAPS_PP_1_4_4,     BIT(8) },
0961     };
0962     static const struct sfdp_4bait erases[SNOR_ERASE_TYPE_MAX] = {
0963         { 0u /* not used */,        BIT(9) },
0964         { 0u /* not used */,        BIT(10) },
0965         { 0u /* not used */,        BIT(11) },
0966         { 0u /* not used */,        BIT(12) },
0967     };
0968     struct spi_nor_flash_parameter *params = nor->params;
0969     struct spi_nor_pp_command *params_pp = params->page_programs;
0970     struct spi_nor_erase_map *map = &params->erase_map;
0971     struct spi_nor_erase_type *erase_type = map->erase_type;
0972     u32 *dwords;
0973     size_t len;
0974     u32 addr, discard_hwcaps, read_hwcaps, pp_hwcaps, erase_mask;
0975     int i, ret;
0976 
0977     if (param_header->major != SFDP_JESD216_MAJOR ||
0978         param_header->length < SFDP_4BAIT_DWORD_MAX)
0979         return -EINVAL;
0980 
0981     /* Read the 4-byte Address Instruction Table. */
0982     len = sizeof(*dwords) * SFDP_4BAIT_DWORD_MAX;
0983 
0984     /* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
0985     dwords = kmalloc(len, GFP_KERNEL);
0986     if (!dwords)
0987         return -ENOMEM;
0988 
0989     addr = SFDP_PARAM_HEADER_PTP(param_header);
0990     ret = spi_nor_read_sfdp(nor, addr, len, dwords);
0991     if (ret)
0992         goto out;
0993 
0994     /* Fix endianness of the 4BAIT DWORDs. */
0995     le32_to_cpu_array(dwords, SFDP_4BAIT_DWORD_MAX);
0996 
0997     /*
0998      * Compute the subset of (Fast) Read commands for which the 4-byte
0999      * version is supported.
1000      */
1001     discard_hwcaps = 0;
1002     read_hwcaps = 0;
1003     for (i = 0; i < ARRAY_SIZE(reads); i++) {
1004         const struct sfdp_4bait *read = &reads[i];
1005 
1006         discard_hwcaps |= read->hwcaps;
1007         if ((params->hwcaps.mask & read->hwcaps) &&
1008             (dwords[0] & read->supported_bit))
1009             read_hwcaps |= read->hwcaps;
1010     }
1011 
1012     /*
1013      * Compute the subset of Page Program commands for which the 4-byte
1014      * version is supported.
1015      */
1016     pp_hwcaps = 0;
1017     for (i = 0; i < ARRAY_SIZE(programs); i++) {
1018         const struct sfdp_4bait *program = &programs[i];
1019 
1020         /*
1021          * The 4 Byte Address Instruction (Optional) Table is the only
1022          * SFDP table that indicates support for Page Program Commands.
1023          * Bypass the params->hwcaps.mask and consider 4BAIT the biggest
1024          * authority for specifying Page Program support.
1025          */
1026         discard_hwcaps |= program->hwcaps;
1027         if (dwords[0] & program->supported_bit)
1028             pp_hwcaps |= program->hwcaps;
1029     }
1030 
1031     /*
1032      * Compute the subset of Sector Erase commands for which the 4-byte
1033      * version is supported.
1034      */
1035     erase_mask = 0;
1036     for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1037         const struct sfdp_4bait *erase = &erases[i];
1038 
1039         if (dwords[0] & erase->supported_bit)
1040             erase_mask |= BIT(i);
1041     }
1042 
1043     /* Replicate the sort done for the map's erase types in BFPT. */
1044     erase_mask = spi_nor_sort_erase_mask(map, erase_mask);
1045 
1046     /*
1047      * We need at least one 4-byte op code per read, program and erase
1048      * operation; the .read(), .write() and .erase() hooks share the
1049      * nor->addr_nbytes value.
1050      */
1051     if (!read_hwcaps || !pp_hwcaps || !erase_mask)
1052         goto out;
1053 
1054     /*
1055      * Discard all operations from the 4-byte instruction set which are
1056      * not supported by this memory.
1057      */
1058     params->hwcaps.mask &= ~discard_hwcaps;
1059     params->hwcaps.mask |= (read_hwcaps | pp_hwcaps);
1060 
1061     /* Use the 4-byte address instruction set. */
1062     for (i = 0; i < SNOR_CMD_READ_MAX; i++) {
1063         struct spi_nor_read_command *read_cmd = &params->reads[i];
1064 
1065         read_cmd->opcode = spi_nor_convert_3to4_read(read_cmd->opcode);
1066     }
1067 
1068     /* 4BAIT is the only SFDP table that indicates page program support. */
1069     if (pp_hwcaps & SNOR_HWCAPS_PP) {
1070         spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP],
1071                     SPINOR_OP_PP_4B, SNOR_PROTO_1_1_1);
1072         /*
1073          * Since xSPI Page Program opcode is backward compatible with
1074          * Legacy SPI, use Legacy SPI opcode there as well.
1075          */
1076         spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_8_8_8_DTR],
1077                     SPINOR_OP_PP_4B, SNOR_PROTO_8_8_8_DTR);
1078     }
1079     if (pp_hwcaps & SNOR_HWCAPS_PP_1_1_4)
1080         spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_1_1_4],
1081                     SPINOR_OP_PP_1_1_4_4B,
1082                     SNOR_PROTO_1_1_4);
1083     if (pp_hwcaps & SNOR_HWCAPS_PP_1_4_4)
1084         spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_1_4_4],
1085                     SPINOR_OP_PP_1_4_4_4B,
1086                     SNOR_PROTO_1_4_4);
1087 
1088     for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
1089         if (erase_mask & BIT(i))
1090             erase_type[i].opcode = (dwords[1] >>
1091                         erase_type[i].idx * 8) & 0xFF;
1092         else
1093             spi_nor_set_erase_type(&erase_type[i], 0u, 0xFF);
1094     }
1095 
1096     /*
1097      * We set SNOR_F_HAS_4BAIT in order to skip spi_nor_set_4byte_opcodes()
1098      * later because we already did the conversion to 4byte opcodes. Also,
1099      * this latest function implements a legacy quirk for the erase size of
1100      * Spansion memory. However this quirk is no longer needed with new
1101      * SFDP compliant memories.
1102      */
1103     params->addr_nbytes = 4;
1104     nor->flags |= SNOR_F_4B_OPCODES | SNOR_F_HAS_4BAIT;
1105 
1106     /* fall through */
1107 out:
1108     kfree(dwords);
1109     return ret;
1110 }
1111 
1112 #define PROFILE1_DWORD1_RDSR_ADDR_BYTES     BIT(29)
1113 #define PROFILE1_DWORD1_RDSR_DUMMY      BIT(28)
1114 #define PROFILE1_DWORD1_RD_FAST_CMD     GENMASK(15, 8)
1115 #define PROFILE1_DWORD4_DUMMY_200MHZ        GENMASK(11, 7)
1116 #define PROFILE1_DWORD5_DUMMY_166MHZ        GENMASK(31, 27)
1117 #define PROFILE1_DWORD5_DUMMY_133MHZ        GENMASK(21, 17)
1118 #define PROFILE1_DWORD5_DUMMY_100MHZ        GENMASK(11, 7)
1119 
1120 /**
1121  * spi_nor_parse_profile1() - parse the xSPI Profile 1.0 table
1122  * @nor:        pointer to a 'struct spi_nor'
1123  * @profile1_header:    pointer to the 'struct sfdp_parameter_header' describing
1124  *          the Profile 1.0 Table length and version.
1125  *
1126  * Return: 0 on success, -errno otherwise.
1127  */
1128 static int spi_nor_parse_profile1(struct spi_nor *nor,
1129                   const struct sfdp_parameter_header *profile1_header)
1130 {
1131     u32 *dwords, addr;
1132     size_t len;
1133     int ret;
1134     u8 dummy, opcode;
1135 
1136     len = profile1_header->length * sizeof(*dwords);
1137     dwords = kmalloc(len, GFP_KERNEL);
1138     if (!dwords)
1139         return -ENOMEM;
1140 
1141     addr = SFDP_PARAM_HEADER_PTP(profile1_header);
1142     ret = spi_nor_read_sfdp(nor, addr, len, dwords);
1143     if (ret)
1144         goto out;
1145 
1146     le32_to_cpu_array(dwords, profile1_header->length);
1147 
1148     /* Get 8D-8D-8D fast read opcode and dummy cycles. */
1149     opcode = FIELD_GET(PROFILE1_DWORD1_RD_FAST_CMD, dwords[0]);
1150 
1151      /* Set the Read Status Register dummy cycles and dummy address bytes. */
1152     if (dwords[0] & PROFILE1_DWORD1_RDSR_DUMMY)
1153         nor->params->rdsr_dummy = 8;
1154     else
1155         nor->params->rdsr_dummy = 4;
1156 
1157     if (dwords[0] & PROFILE1_DWORD1_RDSR_ADDR_BYTES)
1158         nor->params->rdsr_addr_nbytes = 4;
1159     else
1160         nor->params->rdsr_addr_nbytes = 0;
1161 
1162     /*
1163      * We don't know what speed the controller is running at. Find the
1164      * dummy cycles for the fastest frequency the flash can run at to be
1165      * sure we are never short of dummy cycles. A value of 0 means the
1166      * frequency is not supported.
1167      *
1168      * Default to PROFILE1_DUMMY_DEFAULT if we don't find anything, and let
1169      * flashes set the correct value if needed in their fixup hooks.
1170      */
1171     dummy = FIELD_GET(PROFILE1_DWORD4_DUMMY_200MHZ, dwords[3]);
1172     if (!dummy)
1173         dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_166MHZ, dwords[4]);
1174     if (!dummy)
1175         dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_133MHZ, dwords[4]);
1176     if (!dummy)
1177         dummy = FIELD_GET(PROFILE1_DWORD5_DUMMY_100MHZ, dwords[4]);
1178     if (!dummy)
1179         dev_dbg(nor->dev,
1180             "Can't find dummy cycles from Profile 1.0 table\n");
1181 
1182     /* Round up to an even value to avoid tripping controllers up. */
1183     dummy = round_up(dummy, 2);
1184 
1185     /* Update the fast read settings. */
1186     spi_nor_set_read_settings(&nor->params->reads[SNOR_CMD_READ_8_8_8_DTR],
1187                   0, dummy, opcode,
1188                   SNOR_PROTO_8_8_8_DTR);
1189 
1190 out:
1191     kfree(dwords);
1192     return ret;
1193 }
1194 
1195 #define SCCR_DWORD22_OCTAL_DTR_EN_VOLATILE      BIT(31)
1196 
1197 /**
1198  * spi_nor_parse_sccr() - Parse the Status, Control and Configuration Register
1199  *                        Map.
1200  * @nor:        pointer to a 'struct spi_nor'
1201  * @sccr_header:    pointer to the 'struct sfdp_parameter_header' describing
1202  *          the SCCR Map table length and version.
1203  *
1204  * Return: 0 on success, -errno otherwise.
1205  */
1206 static int spi_nor_parse_sccr(struct spi_nor *nor,
1207                   const struct sfdp_parameter_header *sccr_header)
1208 {
1209     u32 *dwords, addr;
1210     size_t len;
1211     int ret;
1212 
1213     len = sccr_header->length * sizeof(*dwords);
1214     dwords = kmalloc(len, GFP_KERNEL);
1215     if (!dwords)
1216         return -ENOMEM;
1217 
1218     addr = SFDP_PARAM_HEADER_PTP(sccr_header);
1219     ret = spi_nor_read_sfdp(nor, addr, len, dwords);
1220     if (ret)
1221         goto out;
1222 
1223     le32_to_cpu_array(dwords, sccr_header->length);
1224 
1225     if (FIELD_GET(SCCR_DWORD22_OCTAL_DTR_EN_VOLATILE, dwords[22]))
1226         nor->flags |= SNOR_F_IO_MODE_EN_VOLATILE;
1227 
1228 out:
1229     kfree(dwords);
1230     return ret;
1231 }
1232 
1233 /**
1234  * spi_nor_post_sfdp_fixups() - Updates the flash's parameters and settings
1235  * after SFDP has been parsed. Called only for flashes that define JESD216 SFDP
1236  * tables.
1237  * @nor:    pointer to a 'struct spi_nor'
1238  *
1239  * Used to tweak various flash parameters when information provided by the SFDP
1240  * tables are wrong.
1241  */
1242 static void spi_nor_post_sfdp_fixups(struct spi_nor *nor)
1243 {
1244     if (nor->manufacturer && nor->manufacturer->fixups &&
1245         nor->manufacturer->fixups->post_sfdp)
1246         nor->manufacturer->fixups->post_sfdp(nor);
1247 
1248     if (nor->info->fixups && nor->info->fixups->post_sfdp)
1249         nor->info->fixups->post_sfdp(nor);
1250 }
1251 
1252 /**
1253  * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
1254  * @nor:        pointer to a 'struct spi_nor'
1255  *
1256  * The Serial Flash Discoverable Parameters are described by the JEDEC JESD216
1257  * specification. This is a standard which tends to supported by almost all
1258  * (Q)SPI memory manufacturers. Those hard-coded tables allow us to learn at
1259  * runtime the main parameters needed to perform basic SPI flash operations such
1260  * as Fast Read, Page Program or Sector Erase commands.
1261  *
1262  * Return: 0 on success, -errno otherwise.
1263  */
1264 int spi_nor_parse_sfdp(struct spi_nor *nor)
1265 {
1266     const struct sfdp_parameter_header *param_header, *bfpt_header;
1267     struct sfdp_parameter_header *param_headers = NULL;
1268     struct sfdp_header header;
1269     struct device *dev = nor->dev;
1270     struct sfdp *sfdp;
1271     size_t sfdp_size;
1272     size_t psize;
1273     int i, err;
1274 
1275     /* Get the SFDP header. */
1276     err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(header), &header);
1277     if (err < 0)
1278         return err;
1279 
1280     /* Check the SFDP header version. */
1281     if (le32_to_cpu(header.signature) != SFDP_SIGNATURE ||
1282         header.major != SFDP_JESD216_MAJOR)
1283         return -EINVAL;
1284 
1285     /*
1286      * Verify that the first and only mandatory parameter header is a
1287      * Basic Flash Parameter Table header as specified in JESD216.
1288      */
1289     bfpt_header = &header.bfpt_header;
1290     if (SFDP_PARAM_HEADER_ID(bfpt_header) != SFDP_BFPT_ID ||
1291         bfpt_header->major != SFDP_JESD216_MAJOR)
1292         return -EINVAL;
1293 
1294     sfdp_size = SFDP_PARAM_HEADER_PTP(bfpt_header) +
1295             SFDP_PARAM_HEADER_PARAM_LEN(bfpt_header);
1296 
1297     /*
1298      * Allocate memory then read all parameter headers with a single
1299      * Read SFDP command. These parameter headers will actually be parsed
1300      * twice: a first time to get the latest revision of the basic flash
1301      * parameter table, then a second time to handle the supported optional
1302      * tables.
1303      * Hence we read the parameter headers once for all to reduce the
1304      * processing time. Also we use kmalloc() instead of devm_kmalloc()
1305      * because we don't need to keep these parameter headers: the allocated
1306      * memory is always released with kfree() before exiting this function.
1307      */
1308     if (header.nph) {
1309         psize = header.nph * sizeof(*param_headers);
1310 
1311         param_headers = kmalloc(psize, GFP_KERNEL);
1312         if (!param_headers)
1313             return -ENOMEM;
1314 
1315         err = spi_nor_read_sfdp(nor, sizeof(header),
1316                     psize, param_headers);
1317         if (err < 0) {
1318             dev_dbg(dev, "failed to read SFDP parameter headers\n");
1319             goto exit;
1320         }
1321     }
1322 
1323     /*
1324      * Cache the complete SFDP data. It is not (easily) possible to fetch
1325      * SFDP after probe time and we need it for the sysfs access.
1326      */
1327     for (i = 0; i < header.nph; i++) {
1328         param_header = &param_headers[i];
1329         sfdp_size = max_t(size_t, sfdp_size,
1330                   SFDP_PARAM_HEADER_PTP(param_header) +
1331                   SFDP_PARAM_HEADER_PARAM_LEN(param_header));
1332     }
1333 
1334     /*
1335      * Limit the total size to a reasonable value to avoid allocating too
1336      * much memory just of because the flash returned some insane values.
1337      */
1338     if (sfdp_size > PAGE_SIZE) {
1339         dev_dbg(dev, "SFDP data (%zu) too big, truncating\n",
1340             sfdp_size);
1341         sfdp_size = PAGE_SIZE;
1342     }
1343 
1344     sfdp = devm_kzalloc(dev, sizeof(*sfdp), GFP_KERNEL);
1345     if (!sfdp) {
1346         err = -ENOMEM;
1347         goto exit;
1348     }
1349 
1350     /*
1351      * The SFDP is organized in chunks of DWORDs. Thus, in theory, the
1352      * sfdp_size should be a multiple of DWORDs. But in case a flash
1353      * is not spec compliant, make sure that we have enough space to store
1354      * the complete SFDP data.
1355      */
1356     sfdp->num_dwords = DIV_ROUND_UP(sfdp_size, sizeof(*sfdp->dwords));
1357     sfdp->dwords = devm_kcalloc(dev, sfdp->num_dwords,
1358                     sizeof(*sfdp->dwords), GFP_KERNEL);
1359     if (!sfdp->dwords) {
1360         err = -ENOMEM;
1361         devm_kfree(dev, sfdp);
1362         goto exit;
1363     }
1364 
1365     err = spi_nor_read_sfdp(nor, 0, sfdp_size, sfdp->dwords);
1366     if (err < 0) {
1367         dev_dbg(dev, "failed to read SFDP data\n");
1368         devm_kfree(dev, sfdp->dwords);
1369         devm_kfree(dev, sfdp);
1370         goto exit;
1371     }
1372 
1373     nor->sfdp = sfdp;
1374 
1375     /*
1376      * Check other parameter headers to get the latest revision of
1377      * the basic flash parameter table.
1378      */
1379     for (i = 0; i < header.nph; i++) {
1380         param_header = &param_headers[i];
1381 
1382         if (SFDP_PARAM_HEADER_ID(param_header) == SFDP_BFPT_ID &&
1383             param_header->major == SFDP_JESD216_MAJOR &&
1384             (param_header->minor > bfpt_header->minor ||
1385              (param_header->minor == bfpt_header->minor &&
1386               param_header->length > bfpt_header->length)))
1387             bfpt_header = param_header;
1388     }
1389 
1390     err = spi_nor_parse_bfpt(nor, bfpt_header);
1391     if (err)
1392         goto exit;
1393 
1394     /* Parse optional parameter tables. */
1395     for (i = 0; i < header.nph; i++) {
1396         param_header = &param_headers[i];
1397 
1398         switch (SFDP_PARAM_HEADER_ID(param_header)) {
1399         case SFDP_SECTOR_MAP_ID:
1400             err = spi_nor_parse_smpt(nor, param_header);
1401             break;
1402 
1403         case SFDP_4BAIT_ID:
1404             err = spi_nor_parse_4bait(nor, param_header);
1405             break;
1406 
1407         case SFDP_PROFILE1_ID:
1408             err = spi_nor_parse_profile1(nor, param_header);
1409             break;
1410 
1411         case SFDP_SCCR_MAP_ID:
1412             err = spi_nor_parse_sccr(nor, param_header);
1413             break;
1414 
1415         default:
1416             break;
1417         }
1418 
1419         if (err) {
1420             dev_warn(dev, "Failed to parse optional parameter table: %04x\n",
1421                  SFDP_PARAM_HEADER_ID(param_header));
1422             /*
1423              * Let's not drop all information we extracted so far
1424              * if optional table parsers fail. In case of failing,
1425              * each optional parser is responsible to roll back to
1426              * the previously known spi_nor data.
1427              */
1428             err = 0;
1429         }
1430     }
1431 
1432     spi_nor_post_sfdp_fixups(nor);
1433 exit:
1434     kfree(param_headers);
1435     return err;
1436 }