2019-07-31 09:19:38

by Tudor Ambarus

[permalink] [raw]
Subject: [PATCH 0/3] mtd: spi-nor: move manuf out of the core - batch 3

From: Tudor Ambarus <[email protected]>

Trim spi_nor_scan() huge function.

Tudor Ambarus (3):
mtd: spi-nor: Bring flash params init together
mtd: spi_nor: Introduce spi_nor_set_addr_width()
mtd: spi-nor: Introduce spi_nor_get_flash_info()

drivers/mtd/spi-nor/spi-nor.c | 129 +++++++++++++++++++++++-------------------
1 file changed, 72 insertions(+), 57 deletions(-)

--
2.9.5


2019-07-31 09:21:42

by Tudor Ambarus

[permalink] [raw]
Subject: [PATCH 3/3] mtd: spi-nor: Introduce spi_nor_get_flash_info()

From: Tudor Ambarus <[email protected]>

Dedicate a function for getting the pointer to the flash_info
const struct. Trim a bit the spi_nor_scan() huge function.

Signed-off-by: Tudor Ambarus <[email protected]>
---
drivers/mtd/spi-nor/spi-nor.c | 52 ++++++++++++++++++++++++++-----------------
1 file changed, 32 insertions(+), 20 deletions(-)

diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
index c322d7cd8216..636f065cc869 100644
--- a/drivers/mtd/spi-nor/spi-nor.c
+++ b/drivers/mtd/spi-nor/spi-nor.c
@@ -4800,25 +4800,10 @@ static int spi_nor_set_addr_width(struct spi_nor *nor)
return 0;
}

-int spi_nor_scan(struct spi_nor *nor, const char *name,
- const struct spi_nor_hwcaps *hwcaps)
+static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
+ const char *name)
{
- struct spi_nor_flash_parameter params;
const struct flash_info *info = NULL;
- struct device *dev = nor->dev;
- struct mtd_info *mtd = &nor->mtd;
- struct device_node *np = spi_nor_get_flash_node(nor);
- int ret;
- int i;
-
- ret = spi_nor_check(nor);
- if (ret)
- return ret;
-
- /* Reset SPI protocol for all commands. */
- nor->reg_proto = SNOR_PROTO_1_1_1;
- nor->read_proto = SNOR_PROTO_1_1_1;
- nor->write_proto = SNOR_PROTO_1_1_1;

if (name)
info = spi_nor_match_id(name);
@@ -4826,7 +4811,7 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
if (!info)
info = spi_nor_read_id(nor);
if (IS_ERR_OR_NULL(info))
- return -ENOENT;
+ return ERR_PTR(-ENOENT);

/*
* If caller has specified name of flash model that can normally be
@@ -4837,7 +4822,7 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,

jinfo = spi_nor_read_id(nor);
if (IS_ERR(jinfo)) {
- return PTR_ERR(jinfo);
+ return jinfo;
} else if (jinfo != info) {
/*
* JEDEC knows better, so overwrite platform ID. We
@@ -4846,12 +4831,39 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
* marked read-only, and we don't want to lose that
* information, even if it's not 100% accurate.
*/
- dev_warn(dev, "found %s, expected %s\n",
+ dev_warn(nor->dev, "found %s, expected %s\n",
jinfo->name, info->name);
info = jinfo;
}
}

+ return info;
+}
+
+int spi_nor_scan(struct spi_nor *nor, const char *name,
+ const struct spi_nor_hwcaps *hwcaps)
+{
+ struct spi_nor_flash_parameter params;
+ const struct flash_info *info;
+ struct device *dev = nor->dev;
+ struct mtd_info *mtd = &nor->mtd;
+ struct device_node *np = spi_nor_get_flash_node(nor);
+ int ret;
+ int i;
+
+ ret = spi_nor_check(nor);
+ if (ret)
+ return ret;
+
+ /* Reset SPI protocol for all commands. */
+ nor->reg_proto = SNOR_PROTO_1_1_1;
+ nor->read_proto = SNOR_PROTO_1_1_1;
+ nor->write_proto = SNOR_PROTO_1_1_1;
+
+ info = spi_nor_get_flash_info(nor, name);
+ if (IS_ERR(info))
+ return PTR_ERR(info);
+
nor->info = info;

mutex_init(&nor->lock);
--
2.9.5

2019-07-31 09:21:42

by Tudor Ambarus

[permalink] [raw]
Subject: [PATCH 2/3] mtd: spi_nor: Introduce spi_nor_set_addr_width()

From: Tudor Ambarus <[email protected]>

Parsing of flash parameters were interleaved with setting of the
nor addr width. Dedicate a function for setting nor addr width.

Signed-off-by: Tudor Ambarus <[email protected]>
---
drivers/mtd/spi-nor/spi-nor.c | 50 ++++++++++++++++++++++++++-----------------
1 file changed, 30 insertions(+), 20 deletions(-)

diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
index fba941a932cc..c322d7cd8216 100644
--- a/drivers/mtd/spi-nor/spi-nor.c
+++ b/drivers/mtd/spi-nor/spi-nor.c
@@ -4773,6 +4773,33 @@ static const struct flash_info *spi_nor_match_id(const char *name)
return NULL;
}

+static int spi_nor_set_addr_width(struct spi_nor *nor)
+{
+ if (nor->addr_width) {
+ /* already configured from SFDP */
+ } else if (nor->info->addr_width) {
+ nor->addr_width = nor->info->addr_width;
+ } else if (nor->mtd.size > 0x1000000) {
+ /* enable 4-byte addressing if the device exceeds 16MiB */
+ nor->addr_width = 4;
+ } else {
+ nor->addr_width = 3;
+ }
+
+ if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
+ dev_err(nor->dev, "address width is too large: %u\n",
+ nor->addr_width);
+ return -EINVAL;
+ }
+
+ /* Set 4byte opcodes when possible. */
+ if (nor->addr_width == 4 && nor->flags & SNOR_F_4B_OPCODES &&
+ !(nor->flags & SNOR_F_HAS_4BAIT))
+ spi_nor_set_4byte_opcodes(nor);
+
+ return 0;
+}
+
int spi_nor_scan(struct spi_nor *nor, const char *name,
const struct spi_nor_hwcaps *hwcaps)
{
@@ -4903,29 +4930,12 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
if (ret)
return ret;

- if (nor->addr_width) {
- /* already configured from SFDP */
- } else if (info->addr_width) {
- nor->addr_width = info->addr_width;
- } else if (mtd->size > 0x1000000) {
- /* enable 4-byte addressing if the device exceeds 16MiB */
- nor->addr_width = 4;
- } else {
- nor->addr_width = 3;
- }
-
if (info->flags & SPI_NOR_4B_OPCODES)
nor->flags |= SNOR_F_4B_OPCODES;

- if (nor->addr_width == 4 && nor->flags & SNOR_F_4B_OPCODES &&
- !(nor->flags & SNOR_F_HAS_4BAIT))
- spi_nor_set_4byte_opcodes(nor);
-
- if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
- dev_err(dev, "address width is too large: %u\n",
- nor->addr_width);
- return -EINVAL;
- }
+ ret = spi_nor_set_addr_width(nor);
+ if (ret)
+ return ret;

/* Send all the required SPI flash commands to initialize device */
ret = spi_nor_init(nor);
--
2.9.5

2019-07-31 10:21:13

by Tudor Ambarus

[permalink] [raw]
Subject: [PATCH 1/3] mtd: spi-nor: Bring flash params init together

From: Tudor Ambarus <[email protected]>

Bring all flash parameters default initialization in
spi_nor_default_params_init().

Signed-off-by: Tudor Ambarus <[email protected]>
---
drivers/mtd/spi-nor/spi-nor.c | 29 +++++++++++------------------
1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
index 01be6d49ce3b..fba941a932cc 100644
--- a/drivers/mtd/spi-nor/spi-nor.c
+++ b/drivers/mtd/spi-nor/spi-nor.c
@@ -4319,6 +4319,7 @@ static void spi_nor_default_init_params(struct spi_nor *nor,
{
struct spi_nor_erase_map *map = &nor->erase_map;
const struct flash_info *info = nor->info;
+ struct device_node *np = spi_nor_get_flash_node(nor);
u8 i, erase_mask;

/* Set legacy flash parameters as default. */
@@ -4328,18 +4329,25 @@ static void spi_nor_default_init_params(struct spi_nor *nor,
params->size = (u64)info->sector_size * info->n_sectors;
params->page_size = info->page_size;

+ if (!(info->flags & SPI_NOR_NO_FR)) {
+ /* Default to Fast Read for DT and non-DT platform devices. */
+ params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
+
+ /* Mask out Fast Read if not requested at DT instantiation. */
+ if (np && !of_property_read_bool(np, "m25p,fast-read"))
+ params->hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
+ }
+
/* (Fast) Read settings. */
params->hwcaps.mask |= SNOR_HWCAPS_READ;
spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
0, 0, SPINOR_OP_READ,
SNOR_PROTO_1_1_1);

- if (!(info->flags & SPI_NOR_NO_FR)) {
- params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
+ if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
0, 8, SPINOR_OP_READ_FAST,
SNOR_PROTO_1_1_1);
- }

if (info->flags & SPI_NOR_DUAL_READ) {
params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
@@ -4876,24 +4884,9 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
nor->page_size = params.page_size;
mtd->writebufsize = nor->page_size;

- if (np) {
- /* If we were instantiated by DT, use it */
- if (of_property_read_bool(np, "m25p,fast-read"))
- params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
- else
- params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
- } else {
- /* If we weren't instantiated by DT, default to fast-read */
- params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
- }
-
if (of_property_read_bool(np, "broken-flash-reset"))
nor->flags |= SNOR_F_BROKEN_RESET;

- /* Some devices cannot do fast-read, no matter what DT tells us */
- if (info->flags & SPI_NOR_NO_FR)
- params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
-
if (nor->locking_ops) {
mtd->_lock = spi_nor_lock;
mtd->_unlock = spi_nor_unlock;
--
2.9.5

2019-08-01 07:30:53

by Boris Brezillon

[permalink] [raw]
Subject: Re: [PATCH 1/3] mtd: spi-nor: Bring flash params init together

On Wed, 31 Jul 2019 09:18:45 +0000
<[email protected]> wrote:

> From: Tudor Ambarus <[email protected]>
>
> Bring all flash parameters default initialization in
> spi_nor_default_params_init().
>
> Signed-off-by: Tudor Ambarus <[email protected]>

Reviewed-by: Boris Brezillon <[email protected]>

> ---
> drivers/mtd/spi-nor/spi-nor.c | 29 +++++++++++------------------
> 1 file changed, 11 insertions(+), 18 deletions(-)
>
> diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
> index 01be6d49ce3b..fba941a932cc 100644
> --- a/drivers/mtd/spi-nor/spi-nor.c
> +++ b/drivers/mtd/spi-nor/spi-nor.c
> @@ -4319,6 +4319,7 @@ static void spi_nor_default_init_params(struct spi_nor *nor,
> {
> struct spi_nor_erase_map *map = &nor->erase_map;
> const struct flash_info *info = nor->info;
> + struct device_node *np = spi_nor_get_flash_node(nor);
> u8 i, erase_mask;
>
> /* Set legacy flash parameters as default. */
> @@ -4328,18 +4329,25 @@ static void spi_nor_default_init_params(struct spi_nor *nor,
> params->size = (u64)info->sector_size * info->n_sectors;
> params->page_size = info->page_size;
>
> + if (!(info->flags & SPI_NOR_NO_FR)) {
> + /* Default to Fast Read for DT and non-DT platform devices. */
> + params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
> +
> + /* Mask out Fast Read if not requested at DT instantiation. */
> + if (np && !of_property_read_bool(np, "m25p,fast-read"))
> + params->hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
> + }
> +
> /* (Fast) Read settings. */
> params->hwcaps.mask |= SNOR_HWCAPS_READ;
> spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ],
> 0, 0, SPINOR_OP_READ,
> SNOR_PROTO_1_1_1);
>
> - if (!(info->flags & SPI_NOR_NO_FR)) {
> - params->hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
> + if (params->hwcaps.mask & SNOR_HWCAPS_READ_FAST)
> spi_nor_set_read_settings(&params->reads[SNOR_CMD_READ_FAST],
> 0, 8, SPINOR_OP_READ_FAST,
> SNOR_PROTO_1_1_1);
> - }
>
> if (info->flags & SPI_NOR_DUAL_READ) {
> params->hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
> @@ -4876,24 +4884,9 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
> nor->page_size = params.page_size;
> mtd->writebufsize = nor->page_size;
>
> - if (np) {
> - /* If we were instantiated by DT, use it */
> - if (of_property_read_bool(np, "m25p,fast-read"))
> - params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
> - else
> - params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
> - } else {
> - /* If we weren't instantiated by DT, default to fast-read */
> - params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
> - }
> -
> if (of_property_read_bool(np, "broken-flash-reset"))
> nor->flags |= SNOR_F_BROKEN_RESET;
>
> - /* Some devices cannot do fast-read, no matter what DT tells us */
> - if (info->flags & SPI_NOR_NO_FR)
> - params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;
> -
> if (nor->locking_ops) {
> mtd->_lock = spi_nor_lock;
> mtd->_unlock = spi_nor_unlock;

2019-08-01 07:31:34

by Boris Brezillon

[permalink] [raw]
Subject: Re: [PATCH 2/3] mtd: spi_nor: Introduce spi_nor_set_addr_width()

On Wed, 31 Jul 2019 09:18:47 +0000
<[email protected]> wrote:

> From: Tudor Ambarus <[email protected]>
>
> Parsing of flash parameters were interleaved with setting of the
> nor addr width. Dedicate a function for setting nor addr width.
>
> Signed-off-by: Tudor Ambarus <[email protected]>

Reviewed-by: Boris Brezillon <[email protected]>

> ---
> drivers/mtd/spi-nor/spi-nor.c | 50 ++++++++++++++++++++++++++-----------------
> 1 file changed, 30 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
> index fba941a932cc..c322d7cd8216 100644
> --- a/drivers/mtd/spi-nor/spi-nor.c
> +++ b/drivers/mtd/spi-nor/spi-nor.c
> @@ -4773,6 +4773,33 @@ static const struct flash_info *spi_nor_match_id(const char *name)
> return NULL;
> }
>
> +static int spi_nor_set_addr_width(struct spi_nor *nor)
> +{
> + if (nor->addr_width) {
> + /* already configured from SFDP */
> + } else if (nor->info->addr_width) {
> + nor->addr_width = nor->info->addr_width;
> + } else if (nor->mtd.size > 0x1000000) {
> + /* enable 4-byte addressing if the device exceeds 16MiB */
> + nor->addr_width = 4;
> + } else {
> + nor->addr_width = 3;
> + }
> +
> + if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
> + dev_err(nor->dev, "address width is too large: %u\n",
> + nor->addr_width);
> + return -EINVAL;
> + }
> +
> + /* Set 4byte opcodes when possible. */
> + if (nor->addr_width == 4 && nor->flags & SNOR_F_4B_OPCODES &&
> + !(nor->flags & SNOR_F_HAS_4BAIT))
> + spi_nor_set_4byte_opcodes(nor);
> +
> + return 0;
> +}
> +
> int spi_nor_scan(struct spi_nor *nor, const char *name,
> const struct spi_nor_hwcaps *hwcaps)
> {
> @@ -4903,29 +4930,12 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
> if (ret)
> return ret;
>
> - if (nor->addr_width) {
> - /* already configured from SFDP */
> - } else if (info->addr_width) {
> - nor->addr_width = info->addr_width;
> - } else if (mtd->size > 0x1000000) {
> - /* enable 4-byte addressing if the device exceeds 16MiB */
> - nor->addr_width = 4;
> - } else {
> - nor->addr_width = 3;
> - }
> -
> if (info->flags & SPI_NOR_4B_OPCODES)
> nor->flags |= SNOR_F_4B_OPCODES;
>
> - if (nor->addr_width == 4 && nor->flags & SNOR_F_4B_OPCODES &&
> - !(nor->flags & SNOR_F_HAS_4BAIT))
> - spi_nor_set_4byte_opcodes(nor);
> -
> - if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
> - dev_err(dev, "address width is too large: %u\n",
> - nor->addr_width);
> - return -EINVAL;
> - }
> + ret = spi_nor_set_addr_width(nor);
> + if (ret)
> + return ret;
>
> /* Send all the required SPI flash commands to initialize device */
> ret = spi_nor_init(nor);

2019-08-01 07:34:51

by Boris Brezillon

[permalink] [raw]
Subject: Re: [PATCH 3/3] mtd: spi-nor: Introduce spi_nor_get_flash_info()

On Wed, 31 Jul 2019 09:18:49 +0000
<[email protected]> wrote:

> From: Tudor Ambarus <[email protected]>
>
> Dedicate a function for getting the pointer to the flash_info
> const struct. Trim a bit the spi_nor_scan() huge function.
>
> Signed-off-by: Tudor Ambarus <[email protected]>

Reviewed-by: Boris Brezillon <[email protected]>

> ---
> drivers/mtd/spi-nor/spi-nor.c | 52 ++++++++++++++++++++++++++-----------------
> 1 file changed, 32 insertions(+), 20 deletions(-)
>
> diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
> index c322d7cd8216..636f065cc869 100644
> --- a/drivers/mtd/spi-nor/spi-nor.c
> +++ b/drivers/mtd/spi-nor/spi-nor.c
> @@ -4800,25 +4800,10 @@ static int spi_nor_set_addr_width(struct spi_nor *nor)
> return 0;
> }
>
> -int spi_nor_scan(struct spi_nor *nor, const char *name,
> - const struct spi_nor_hwcaps *hwcaps)
> +static const struct flash_info *spi_nor_get_flash_info(struct spi_nor *nor,
> + const char *name)
> {
> - struct spi_nor_flash_parameter params;
> const struct flash_info *info = NULL;
> - struct device *dev = nor->dev;
> - struct mtd_info *mtd = &nor->mtd;
> - struct device_node *np = spi_nor_get_flash_node(nor);
> - int ret;
> - int i;
> -
> - ret = spi_nor_check(nor);
> - if (ret)
> - return ret;
> -
> - /* Reset SPI protocol for all commands. */
> - nor->reg_proto = SNOR_PROTO_1_1_1;
> - nor->read_proto = SNOR_PROTO_1_1_1;
> - nor->write_proto = SNOR_PROTO_1_1_1;
>
> if (name)
> info = spi_nor_match_id(name);
> @@ -4826,7 +4811,7 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
> if (!info)
> info = spi_nor_read_id(nor);
> if (IS_ERR_OR_NULL(info))
> - return -ENOENT;
> + return ERR_PTR(-ENOENT);
>
> /*
> * If caller has specified name of flash model that can normally be
> @@ -4837,7 +4822,7 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
>
> jinfo = spi_nor_read_id(nor);
> if (IS_ERR(jinfo)) {
> - return PTR_ERR(jinfo);
> + return jinfo;
> } else if (jinfo != info) {
> /*
> * JEDEC knows better, so overwrite platform ID. We
> @@ -4846,12 +4831,39 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
> * marked read-only, and we don't want to lose that
> * information, even if it's not 100% accurate.
> */
> - dev_warn(dev, "found %s, expected %s\n",
> + dev_warn(nor->dev, "found %s, expected %s\n",
> jinfo->name, info->name);
> info = jinfo;
> }
> }
>
> + return info;
> +}
> +
> +int spi_nor_scan(struct spi_nor *nor, const char *name,
> + const struct spi_nor_hwcaps *hwcaps)
> +{
> + struct spi_nor_flash_parameter params;
> + const struct flash_info *info;
> + struct device *dev = nor->dev;
> + struct mtd_info *mtd = &nor->mtd;
> + struct device_node *np = spi_nor_get_flash_node(nor);
> + int ret;
> + int i;
> +
> + ret = spi_nor_check(nor);
> + if (ret)
> + return ret;
> +
> + /* Reset SPI protocol for all commands. */
> + nor->reg_proto = SNOR_PROTO_1_1_1;
> + nor->read_proto = SNOR_PROTO_1_1_1;
> + nor->write_proto = SNOR_PROTO_1_1_1;
> +
> + info = spi_nor_get_flash_info(nor, name);
> + if (IS_ERR(info))
> + return PTR_ERR(info);
> +
> nor->info = info;
>
> mutex_init(&nor->lock);