Skip to content

Commit 2be9507

Browse files
CyrillePitchenambarus
authored andcommitted
mtd: spi-nor: parse SFDP 4-byte Address Instruction Table
Backport mainline patch: 816873e. Add support for SFDP (JESD216B) 4-byte Address Instruction Table. This table is optional but when available, we parse it to get the 4-byte address op codes supported by the memory. Using these op codes is stateless as opposed to entering the 4-byte address mode or setting the Base Address Register (BAR). Flashes that have the 4BAIT table declared can now support SPINOR_OP_PP_1_1_4_4B and SPINOR_OP_PP_1_4_4_4B opcodes. Tested on MX25L25673G. Signed-off-by: Cyrille Pitchen <cyrille.pitchen@microchip.com> [tudor.ambarus@microchip.com: - rework erase and page program logic, - pass DMA-able buffer to spi_nor_read_sfdp(), - introduce SPI_NOR_HAS_4BAIT - various minor updates.] Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com> Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com> [tudor.ambarus@microchip.com: backport 816873e] Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
1 parent c27bb13 commit 2be9507

2 files changed

Lines changed: 194 additions & 2 deletions

File tree

drivers/mtd/spi-nor/spi-nor.c

Lines changed: 193 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2209,6 +2209,7 @@ struct sfdp_parameter_header {
22092209

22102210
#define SFDP_BFPT_ID 0xff00 /* Basic Flash Parameter Table */
22112211
#define SFDP_SECTOR_MAP_ID 0xff81 /* Sector Map Table */
2212+
#define SFDP_4BAIT_ID 0xff84 /* 4-byte Address Instruction Table */
22122213

22132214
#define SFDP_SIGNATURE 0x50444653U
22142215
#define SFDP_JESD216_MAJOR 1
@@ -3069,6 +3070,191 @@ static int spi_nor_parse_smpt(struct spi_nor *nor,
30693070
return ret;
30703071
}
30713072

3073+
#define SFDP_4BAIT_DWORD_MAX 2
3074+
3075+
struct sfdp_4bait {
3076+
/* The hardware capability. */
3077+
u32 hwcaps;
3078+
3079+
/*
3080+
* The <supported_bit> bit in DWORD1 of the 4BAIT tells us whether
3081+
* the associated 4-byte address op code is supported.
3082+
*/
3083+
u32 supported_bit;
3084+
};
3085+
3086+
/**
3087+
* spi_nor_parse_4bait() - parse the 4-Byte Address Instruction Table
3088+
* @nor: pointer to a 'struct spi_nor'.
3089+
* @param_header: pointer to the 'struct sfdp_parameter_header' describing
3090+
* the 4-Byte Address Instruction Table length and version.
3091+
* @params: pointer to the 'struct spi_nor_flash_parameter' to be.
3092+
*
3093+
* Return: 0 on success, -errno otherwise.
3094+
*/
3095+
static int spi_nor_parse_4bait(struct spi_nor *nor,
3096+
const struct sfdp_parameter_header *param_header,
3097+
struct spi_nor_flash_parameter *params)
3098+
{
3099+
static const struct sfdp_4bait reads[] = {
3100+
{ SNOR_HWCAPS_READ, BIT(0) },
3101+
{ SNOR_HWCAPS_READ_FAST, BIT(1) },
3102+
{ SNOR_HWCAPS_READ_1_1_2, BIT(2) },
3103+
{ SNOR_HWCAPS_READ_1_2_2, BIT(3) },
3104+
{ SNOR_HWCAPS_READ_1_1_4, BIT(4) },
3105+
{ SNOR_HWCAPS_READ_1_4_4, BIT(5) },
3106+
{ SNOR_HWCAPS_READ_1_1_1_DTR, BIT(13) },
3107+
{ SNOR_HWCAPS_READ_1_2_2_DTR, BIT(14) },
3108+
{ SNOR_HWCAPS_READ_1_4_4_DTR, BIT(15) },
3109+
};
3110+
static const struct sfdp_4bait programs[] = {
3111+
{ SNOR_HWCAPS_PP, BIT(6) },
3112+
{ SNOR_HWCAPS_PP_1_1_4, BIT(7) },
3113+
{ SNOR_HWCAPS_PP_1_4_4, BIT(8) },
3114+
};
3115+
static const struct sfdp_4bait erases[SNOR_ERASE_TYPE_MAX] = {
3116+
{ 0u /* not used */, BIT(9) },
3117+
{ 0u /* not used */, BIT(10) },
3118+
{ 0u /* not used */, BIT(11) },
3119+
{ 0u /* not used */, BIT(12) },
3120+
};
3121+
struct spi_nor_pp_command *params_pp = params->page_programs;
3122+
struct spi_nor_erase_map *map = &nor->erase_map;
3123+
struct spi_nor_erase_type *erase_type = map->erase_type;
3124+
u32 *dwords;
3125+
size_t len;
3126+
u32 addr, discard_hwcaps, read_hwcaps, pp_hwcaps, erase_mask;
3127+
int i, ret;
3128+
3129+
if (param_header->major != SFDP_JESD216_MAJOR ||
3130+
param_header->length < SFDP_4BAIT_DWORD_MAX)
3131+
return -EINVAL;
3132+
3133+
/* Read the 4-byte Address Instruction Table. */
3134+
len = sizeof(*dwords) * SFDP_4BAIT_DWORD_MAX;
3135+
3136+
/* Use a kmalloc'ed bounce buffer to guarantee it is DMA-able. */
3137+
dwords = kmalloc(len, GFP_KERNEL);
3138+
if (!dwords)
3139+
return -ENOMEM;
3140+
3141+
addr = SFDP_PARAM_HEADER_PTP(param_header);
3142+
ret = spi_nor_read_sfdp(nor, addr, len, dwords);
3143+
if (ret)
3144+
return ret;
3145+
3146+
/* Fix endianness of the 4BAIT DWORDs. */
3147+
for (i = 0; i < SFDP_4BAIT_DWORD_MAX; i++)
3148+
dwords[i] = le32_to_cpu(dwords[i]);
3149+
3150+
/*
3151+
* Compute the subset of (Fast) Read commands for which the 4-byte
3152+
* version is supported.
3153+
*/
3154+
discard_hwcaps = 0;
3155+
read_hwcaps = 0;
3156+
for (i = 0; i < ARRAY_SIZE(reads); i++) {
3157+
const struct sfdp_4bait *read = &reads[i];
3158+
3159+
discard_hwcaps |= read->hwcaps;
3160+
if ((params->hwcaps.mask & read->hwcaps) &&
3161+
(dwords[0] & read->supported_bit))
3162+
read_hwcaps |= read->hwcaps;
3163+
}
3164+
3165+
/*
3166+
* Compute the subset of Page Program commands for which the 4-byte
3167+
* version is supported.
3168+
*/
3169+
pp_hwcaps = 0;
3170+
for (i = 0; i < ARRAY_SIZE(programs); i++) {
3171+
const struct sfdp_4bait *program = &programs[i];
3172+
3173+
/*
3174+
* The 4 Byte Address Instruction (Optional) Table is the only
3175+
* SFDP table that indicates support for Page Program Commands.
3176+
* Bypass the params->hwcaps.mask and consider 4BAIT the biggest
3177+
* authority for specifying Page Program support.
3178+
*/
3179+
discard_hwcaps |= program->hwcaps;
3180+
if (dwords[0] & program->supported_bit)
3181+
pp_hwcaps |= program->hwcaps;
3182+
}
3183+
3184+
/*
3185+
* Compute the subset of Sector Erase commands for which the 4-byte
3186+
* version is supported.
3187+
*/
3188+
erase_mask = 0;
3189+
for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
3190+
const struct sfdp_4bait *erase = &erases[i];
3191+
3192+
if (dwords[0] & erase->supported_bit)
3193+
erase_mask |= BIT(i);
3194+
}
3195+
3196+
/* Replicate the sort done for the map's erase types in BFPT. */
3197+
erase_mask = spi_nor_sort_erase_mask(map, erase_mask);
3198+
3199+
/*
3200+
* We need at least one 4-byte op code per read, program and erase
3201+
* operation; the .read(), .write() and .erase() hooks share the
3202+
* nor->addr_width value.
3203+
*/
3204+
if (!read_hwcaps || !pp_hwcaps || !erase_mask)
3205+
goto out;
3206+
3207+
/*
3208+
* Discard all operations from the 4-byte instruction set which are
3209+
* not supported by this memory.
3210+
*/
3211+
params->hwcaps.mask &= ~discard_hwcaps;
3212+
params->hwcaps.mask |= (read_hwcaps | pp_hwcaps);
3213+
3214+
/* Use the 4-byte address instruction set. */
3215+
for (i = 0; i < SNOR_CMD_READ_MAX; i++) {
3216+
struct spi_nor_read_command *read_cmd = &params->reads[i];
3217+
3218+
read_cmd->opcode = spi_nor_convert_3to4_read(read_cmd->opcode);
3219+
}
3220+
3221+
/* 4BAIT is the only SFDP table that indicates page program support. */
3222+
if (pp_hwcaps & SNOR_HWCAPS_PP)
3223+
spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP],
3224+
SPINOR_OP_PP_4B, SNOR_PROTO_1_1_1);
3225+
if (pp_hwcaps & SNOR_HWCAPS_PP_1_1_4)
3226+
spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_1_1_4],
3227+
SPINOR_OP_PP_1_1_4_4B,
3228+
SNOR_PROTO_1_1_4);
3229+
if (pp_hwcaps & SNOR_HWCAPS_PP_1_4_4)
3230+
spi_nor_set_pp_settings(&params_pp[SNOR_CMD_PP_1_4_4],
3231+
SPINOR_OP_PP_1_4_4_4B,
3232+
SNOR_PROTO_1_4_4);
3233+
3234+
for (i = 0; i < SNOR_ERASE_TYPE_MAX; i++) {
3235+
if (erase_mask & BIT(i))
3236+
erase_type[i].opcode = (dwords[1] >>
3237+
erase_type[i].idx * 8) & 0xFF;
3238+
else
3239+
spi_nor_set_erase_type(&erase_type[i], 0u, 0xFF);
3240+
}
3241+
3242+
/*
3243+
* We set SNOR_F_HAS_4BAIT in order to skip spi_nor_set_4byte_opcodes()
3244+
* later because we already did the conversion to 4byte opcodes. Also,
3245+
* this latest function implements a legacy quirk for the erase size of
3246+
* Spansion memory. However this quirk is no longer needed with new
3247+
* SFDP compliant memories.
3248+
*/
3249+
nor->addr_width = 4;
3250+
nor->flags |= SNOR_F_HAS_4BAIT;
3251+
3252+
/* fall through */
3253+
out:
3254+
kfree(dwords);
3255+
return ret;
3256+
}
3257+
30723258
/**
30733259
* spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
30743260
* @nor: pointer to a 'struct spi_nor'
@@ -3167,6 +3353,10 @@ static int spi_nor_parse_sfdp(struct spi_nor *nor,
31673353
err = spi_nor_parse_smpt(nor, param_header);
31683354
break;
31693355

3356+
case SFDP_4BAIT_ID:
3357+
err = spi_nor_parse_4bait(nor, param_header, params);
3358+
break;
3359+
31703360
default:
31713361
break;
31723362
}
@@ -3759,8 +3949,9 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
37593949
} else if (mtd->size > 0x1000000) {
37603950
/* enable 4-byte addressing if the device exceeds 16MiB */
37613951
nor->addr_width = 4;
3762-
if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
3763-
info->flags & SPI_NOR_4B_OPCODES)
3952+
if ((JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
3953+
info->flags & SPI_NOR_4B_OPCODES) &&
3954+
!(nor->flags & SNOR_F_HAS_4BAIT))
37643955
spi_nor_set_4byte_opcodes(nor, info);
37653956
} else {
37663957
nor->addr_width = 3;

include/linux/mtd/spi-nor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ enum spi_nor_option_flags {
230230
SNOR_F_S3AN_ADDR_DEFAULT = BIT(3),
231231
SNOR_F_READY_XSR_RDY = BIT(4),
232232
SNOR_F_USE_CLSR = BIT(5),
233+
SNOR_F_HAS_4BAIT = BIT(6),
233234
};
234235

235236
/**

0 commit comments

Comments
 (0)