Add spi-nor flash device pm support
Subsystems (3)
v6 cover Add spi-nor flash device pm support
Changes since v5 based on review comments:
spi-nor.h
- Added flash state field
- Added spin_lock for flash state change
- Added wait queue for synchronization of spi-nor ops between mtd and spi-nor driver
spi-nor.c
- Added spi_nor_get/release_device() flash state syncronization functions
- Added mtd suspend and resume handlers
rerverted m25p80.c changes
The V5 changes below implements power management support using the mtd handlers for resume and suspend in the spi-nor driver. Implemented flash state synchronization methods based on other mtd device drivers. spi-nor mtd pm resume() calls newly implemented spi_nor_init() function that sets up the spi-nor flash to its pre-suspend state. This is needed on platfroms that turn off power to the spi-nor flash on pm suspend. The code has been rebased to git://github.com/spi-nor/linux.git next branch.
Kamal Dasu (3):
mtd: spi-nor: Add spi-nor init function
mtd: spi-nor: Add spi-nor flash device synchronization between flash
states
mtd: spi-nor: Add spi-nor mtd suspend and resume handlers
drivers/mtd/spi-nor/spi-nor.c | 170 +++++++++++++++++++++++++++++++++++------- include/linux/mtd/spi-nor.h | 22 +++++- 2 files changed, 164 insertions(+), 28 deletions(-)
Signature · 6 lines
1.9.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
PATCH v6 1/3 mtd: spi-nor: Add spi-nor init function
Refactored spi_nor_scan() code to add spi_nor_init() function that programs the spi-nor flash to:
1) remove flash protection if applicable for certain vendors
2) set read mode to quad mode if configured such
3) set the address width based on the flash size
spi_nor_scan() now calls spi_nor_init() to setup nor flash. The init function can also be called by flash device driver to reinitialize spi-nor flash.
--- drivers/mtd/spi-nor/spi-nor.c | 77 ++++++++++++++++++++++++++++--------------- include/linux/mtd/spi-nor.h | 18 +++++++++- 2 files changed, 67 insertions(+), 28 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +50 -27
@@ -1526,6 +1526,44 @@ static int s3an_nor_scan(const struct flash_info *info, struct spi_nor *nor)return 0 ; } +int spi_nor_init (struct spi_nor *nor) +{ +int ret =0 ; +const struct flash_info *info = nor->info; +struct device *dev = nor->dev; + +/* +* Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up +* with the software protection bits set +*/ + +if (JEDEC_MFR (info) ==SNOR_MFR_ATMEL || +JEDEC_MFR (info) ==SNOR_MFR_INTEL || +JEDEC_MFR (info) ==SNOR_MFR_SST || + info->flags &SPI_NOR_HAS_LOCK ) { +write_enable (nor); +write_sr (nor,0 ); +spi_nor_wait_till_ready (nor); + } + +if (nor->flash_read ==SPI_NOR_QUAD ) { + ret =set_quad_mode (nor, info); +if (ret) { +dev_err (dev,"quad mode not supported\n" ); +return ret; + } + } + +if (JEDEC_MFR (info) ==SNOR_MFR_SPANSION || + info->flags &SPI_NOR_4B_OPCODES ) +spi_nor_set_4byte_opcodes (nor, info); +else +set_4byte (nor, info,1 ); + +return ret; +} +EXPORT_SYMBOL_GPL (spi_nor_init); +int spi_nor_scan (struct spi_nor *nor,const char *name,enum read_mode mode) {const struct flash_info *info =NULL ;
@@ -1571,6 +1609,8 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode) } } +/* update pointer to flash_info in the nor structure */ + nor->info = info;mutex_init (&nor->lock);/*
@@ -1581,20 +1621,6 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)if (info->flags &SPI_S3AN ) nor->flags |=SNOR_F_READY_XSR_RDY ; -/* -* Atmel, SST, Intel/Numonyx, and others serial NOR tend to power up -* with the software protection bits set -*/ - -if (JEDEC_MFR (info) ==SNOR_MFR_ATMEL || -JEDEC_MFR (info) ==SNOR_MFR_INTEL || -JEDEC_MFR (info) ==SNOR_MFR_SST || - info->flags &SPI_NOR_HAS_LOCK ) { -write_enable (nor); -write_sr (nor,0 ); -spi_nor_wait_till_ready (nor); - } -if (!mtd->name) mtd->name =dev_name (dev); mtd->priv = nor;
@@ -1669,11 +1695,6 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)/* Quad/Dual-read mode takes precedence over fast/normal */ if (mode ==SPI_NOR_QUAD && info->flags &SPI_NOR_QUAD_READ ) { - ret =set_quad_mode (nor, info); -if (ret) { -dev_err (dev,"quad mode not supported\n" ); -return ret; - } nor->flash_read =SPI_NOR_QUAD ; }else if (mode ==SPI_NOR_DUAL && info->flags &SPI_NOR_DUAL_READ ) { nor->flash_read =SPI_NOR_DUAL ;
@@ -1702,17 +1723,11 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)if (info->addr_width) nor->addr_width = info->addr_width; -else if (mtd->size >0x1000000 ) { +else if (mtd->size >0x1000000 )/* enable 4-byte addressing if the device exceeds 16MiB */ nor->addr_width =4 ; -if (JEDEC_MFR (info) ==SNOR_MFR_SPANSION || - info->flags &SPI_NOR_4B_OPCODES ) -spi_nor_set_4byte_opcodes (nor, info); -else -set_4byte (nor, info,1 ); - }else { +else nor->addr_width =3 ; - }if (nor->addr_width >SPI_NOR_MAX_ADDR_WIDTH ) { dev_err(dev, "address width is too large: %u\n",
@@ -1728,6 +1743,14 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)return ret; } +/* +* call init function to send necessary spi-nor read/write config +* commands to nor flash based on above nor settings +*/ + ret =spi_nor_init (nor); +if (ret) +return ret; +dev_info (dev,"%s (%lld Kbytes)\n" , info->name, (long long )mtd->size >>10 );
include/linux/mtd/spi-nor.h +17 -1
@@ -143,6 +143,8 @@ enum spi_nor_option_flags { SNOR_F_READY_XSR_RDY = BIT(4), }; +struct flash_info ; +/** * struct spi_nor - Structure for defining a the SPI NOR layer * @mtd: point to a mtd_info structure
@@ -174,6 +176,7 @@ enum spi_nor_option_flags {* @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is * completely locked * @priv: the private data +* @info: points to the flash_info structure */ struct spi_nor {struct mtd_info mtd;
@@ -206,6 +209,7 @@ struct spi_nor {int (*flash_is_locked)(struct spi_nor *nor,loff_t ofs,uint64_t len);void *priv; +const struct flash_info *info; }; static inline void spi_nor_set_flash_node(struct spi_nor *nor,
@@ -220,12 +224,24 @@ static inline struct device_node *spi_nor_get_flash_node(struct spi_nor *nor) }/** +* spi_nor_init() - initialize SPI NOR +* @nor: the spi_nor structure +* +* The drivers uses this function to initialize the SPI NOR flash device to +* settings in spi_nor structure. The functions sets read mode, address width +* and removes protection on the flash device based on those settings. +* +* Return: 0 for success, others for failure. +*/ +int spi_nor_init (struct spi_nor *nor); + +/** * spi_nor_scan() - scan the SPI NOR * @nor: the spi_nor structure * @name: the chip type name * @mode: the read mode supported by the driver * -* The drivers can use this fuction to scan the SPI NOR. +* The drivers can use this function to scan the SPI NOR. * In the scanning, it will try to get all the necessary information to * fill the mtd_info{} and the spi_nor{}. *
Signature · 6 lines
1.9.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
PATCH v6 2/3 mtd: spi-nor: Add spi-nor flash device synchronization between flash states
In reply to Kamal DasuAdded flash access synchronization methods spi_nor_get/release_device(). This change allows spi-nor driver to maintain flash states in spi-nor stucture for read, write, erase, lock, unlock nor ops. Only when the flash state is FL_READY a new state is set and spi-nor flash op is initiated. The state change is done with a spin_lock held and released as soon as the state is changed. Else the current process for spi-nor transfer is queued till the flash is in FL_READY state. This change allows us to add mtd layer synchronization when the mtd resume suspend handlers are added.
--- drivers/mtd/spi-nor/spi-nor.c | 69 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 4 +++ 2 files changed, 73 insertions(+)
drivers/mtd/spi-nor/spi-nor.c +69 -0
@@ -89,6 +89,15 @@ struct flash_info {#define JEDEC_MFR (info) ((info)->id[0]) +/* map table for spi_nor op to flashchip state */ +static int spi_nor_state[] = { + [SPI_NOR_OPS_READ ] =FL_READING , + [SPI_NOR_OPS_WRITE ] =FL_WRITING , + [SPI_NOR_OPS_ERASE ] =FL_ERASING , + [SPI_NOR_OPS_LOCK ] =FL_LOCKING , + [SPI_NOR_OPS_UNLOCK ] =FL_UNLOCKING , +}; +static const struct flash_info *spi_nor_match_id (const char *name);/*
@@ -396,10 +405,64 @@ static int erase_chip(struct spi_nor *nor)return nor->write_reg (nor,SPINOR_OP_CHIP_ERASE ,NULL ,0 ); } +/** +* spi_nor_get_device - [GENERIC] Get chip for selected access +* @param mtd MTD device structure +* @param new_state the state which is requested +* +* Get the nor flash device and lock it for exclusive access +*/ +static int spi_nor_get_device (struct mtd_info *mtd,int new_state) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); +DECLARE_WAITQUEUE (wait, current); + +/* +* Grab the lock and see if the device is available +*/ +while (1 ) { +spin_lock (&nor->chip_lock); +if (nor->state ==FL_READY ) { + nor->state = new_state; +spin_unlock (&nor->chip_lock); +break ; + } +if (new_state ==FL_PM_SUSPENDED ) { +spin_unlock (&nor->chip_lock); +return (nor->state ==FL_PM_SUSPENDED ) ?0 : -EAGAIN ; + } +set_current_state (TASK_UNINTERRUPTIBLE ); +add_wait_queue (&nor->wq, &wait); +spin_unlock (&nor->chip_lock); +schedule (); +remove_wait_queue (&nor->wq, &wait); + } + +return 0 ; +} + +/** +* spi_nor_release_device - [GENERIC] release chip +* @mtd: MTD device structure +* +* Release nor flash chip lock and wake up anyone waiting on the device. +*/ +static void spi_nor_release_device (struct mtd_info *mtd) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); + +/* Release the controller and the chip */ +spin_lock (&nor->chip_lock); + nor->state =FL_READY ; +wake_up (&nor->wq); +spin_unlock (&nor->chip_lock); +} +static int spi_nor_lock_and_prep (struct spi_nor *nor,enum spi_nor_ops ops) {int ret =0 ; +spi_nor_get_device (&nor->mtd, spi_nor_state[ops]);mutex_lock (&nor->lock);if (nor->prepare) {
@@ -407,6 +470,7 @@ static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)if (ret) {dev_err (nor->dev,"failed in the preparation.\n" );mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd);return ret; } }
@@ -418,6 +482,7 @@ static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)if (nor->unprepare) nor->unprepare (nor, ops);mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd); }/*
@@ -1743,6 +1808,10 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)return ret; } + nor->state =FL_READY ; +init_waitqueue_head (&nor->wq); +spin_lock_init (&nor->chip_lock); +/* * call init function to send necessary spi-nor read/write config * commands to nor flash based on above nor settings
include/linux/mtd/spi-nor.h +4 -0
@@ -13,6 +13,7 @@#include <linux/bitops.h> #include <linux/mtd/cfi.h> #include <linux/mtd/mtd.h> +#include <linux/mtd/flashchip.h> /* * Manufacturer IDs
@@ -210,6 +211,9 @@ struct spi_nor {void *priv;const struct flash_info *info; +spinlock_t chip_lock; +wait_queue_head_t wq; +flstate_t state; }; static inline void spi_nor_set_flash_node(struct spi_nor *nor,
Signature · 6 lines
1.9.1 ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
PATCH v6 3/3 mtd: spi-nor: Add spi-nor mtd suspend and resume handlers
In reply to Kamal DasuImplemented and populated spi-nor mtd PM handlers for suspend and resume ops. spi-nor suspend op set's the flash state to FL_PM_SUSPENDED state and resume op re-initializes spi-nor flash and set's flash state to FL_READY. The handlers ensures synchronization between the spi-nor and the mtd layer by calling spi_nor_get/release_device() and setting respective flash states.
--- drivers/mtd/spi-nor/spi-nor.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
drivers/mtd/spi-nor/spi-nor.c +24 -0
@@ -1629,6 +1629,28 @@ int spi_nor_init(struct spi_nor *nor) }EXPORT_SYMBOL_GPL (spi_nor_init); +/* mtd suspend handler */ +static int spi_nor_suspend (struct mtd_info *mtd) +{ +return spi_nor_get_device (mtd,FL_PM_SUSPENDED ); +} + +/* mtd resume handler */ +static void spi_nor_resume (struct mtd_info *mtd) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); +struct device *dev = nor->dev; + +if (nor->state ==FL_PM_SUSPENDED ) { +/* re-initialize the nor chip */ +spi_nor_init (nor); +/* mtd can resume transfers */ +spi_nor_release_device (mtd); + }else { +dev_err (dev,"resume() called, chip not in suspended state\n" ); + } +} +int spi_nor_scan (struct spi_nor *nor,const char *name,enum read_mode mode) {const struct flash_info *info =NULL ;
@@ -1695,6 +1717,8 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode) mtd->size = info->sector_size * info->n_sectors; mtd->_erase = spi_nor_erase; mtd->_read = spi_nor_read; + mtd->_suspend = spi_nor_suspend; + mtd->_resume = spi_nor_resume;/* NOR protection support for STmicro/Micron chips and similar */ if (JEDEC_MFR(info) == SNOR_MFR_MICRON ||
Signature · 6 lines
1.9.1 -- To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v6 2/3] mtd: spi-nor: Add spi-nor flash device synchronization between flash states
In reply to Kamal DasuQuoted 1 line · Kamal Dasu
Added flash access synchronization methods spi_nor_get/release_device(). This
Should be get/put to be consistent with the rest of the kernel ...
Quoted 20 lines
change allows spi-nor driver to maintain flash states in spi-nor stucture for read, write, erase, lock, unlock nor ops. Only when the flash state is
FL_READYa new state is set and spi-nor flash op is initiated. The state change is done with aspin_lockheld and released as soon as the state is changed. Else the current process for spi-nor transfer is queued till the flash is inFL_READYstate. This change allows us to add mtd layer synchronization when the mtd resume suspend handlers are added.Signed-off-by Kamal Dasu <[email protected]>--- drivers/mtd/spi-nor/spi-nor.c | 69 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 4 +++ 2 files changed, 73 insertions(+)drivers/mtd/spi-nor/spi-nor.c +0
-0@@ -89,6 +89,15 @@ struct flash_info {
drivers/mtd/spi-nor/spi-nor.c struct flash_info { · line 99
Quoted 10 lines
drivers/mtd/spi-nor/spi-nor.c +8
-0#define JEDEC_MFR (info) ((info)->id[0]) +/* map table for spi_nor op to flashchip state */ +static int spi_nor_state[] = { + [SPI_NOR_OPS_READ ] =FL_READING , + [SPI_NOR_OPS_WRITE ] =FL_WRITING , + [SPI_NOR_OPS_ERASE ] =FL_ERASING , + [SPI_NOR_OPS_LOCK ] =FL_LOCKING , + [SPI_NOR_OPS_UNLOCK ] =FL_UNLOCKING , +};
Can't you just retain which instruction is in progress instead of adding yet another orthogonal FL_FOO ?
Quoted 28 lines
drivers/mtd/spi-nor/spi-nor.c +21
-0static const struct flash_info *spi_nor_match_id (const char *name);/* @@ -396,10 +405,64 @@ static int erase_chip(struct spi_nor *nor)return nor->write_reg (nor,SPINOR_OP_CHIP_ERASE ,NULL ,0 ); } +/** +* spi_nor_get_device - [GENERIC] Get chip for selected access +* @param mtd MTD device structure +* @param new_state the state which is requested +* +* Get the nor flash device and lock it for exclusive access +*/ +static int spi_nor_get_device (struct mtd_info *mtd,int new_state) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); +DECLARE_WAITQUEUE (wait, current); + +/* +* Grab the lock and see if the device is available +*/ +while (1 ) { +spin_lock (&nor->chip_lock); +if (nor->state ==FL_READY ) { + nor->state = new_state; +spin_unlock (&nor->chip_lock); +break ;
drivers/mtd/spi-nor/spi-nor.c static int erase_chip(struct spi_nor *nor) · line 438
Quoted 10 lines
drivers/mtd/spi-nor/spi-nor.c +10
-0+ } +if (new_state ==FL_PM_SUSPENDED ) { +spin_unlock (&nor->chip_lock); +return (nor->state ==FL_PM_SUSPENDED ) ?0 : -EAGAIN ; + } +set_current_state (TASK_UNINTERRUPTIBLE ); +add_wait_queue (&nor->wq, &wait); +spin_unlock (&nor->chip_lock); +schedule (); +remove_wait_queue (&nor->wq, &wait);
Somehow, I have to wonder, doesn't runtime PM implement this sort of functionality already ?
Quoted 80 lines
drivers/mtd/spi-nor/spi-nor.c +29
-0+ } + +return 0 ; +} + +/** +* spi_nor_release_device - [GENERIC] release chip +* @mtd: MTD device structure +* +* Release nor flash chip lock and wake up anyone waiting on the device. +*/ +static void spi_nor_release_device (struct mtd_info *mtd) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); + +/* Release the controller and the chip */ +spin_lock (&nor->chip_lock); + nor->state =FL_READY ; +wake_up (&nor->wq); +spin_unlock (&nor->chip_lock); +} +static int spi_nor_lock_and_prep (struct spi_nor *nor,enum spi_nor_ops ops) {int ret =0 ; +spi_nor_get_device (&nor->mtd, spi_nor_state[ops]);mutex_lock (&nor->lock);if (nor->prepare) {@@ -407,6 +470,7 @@ static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)if (ret) {dev_err (nor->dev,"failed in the preparation.\n" );mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd);return ret; } }@@ -418,6 +482,7 @@ static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)if (nor->unprepare) nor->unprepare (nor, ops);mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd); }/* @@ -1743,6 +1808,10 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)return ret; } + nor->state =FL_READY ; +init_waitqueue_head (&nor->wq); +spin_lock_init (&nor->chip_lock); +/* * call init function to send necessary spi-nor read/write config * commands to nor flash based on above nor settings include/linux/mtd/spi-nor.h +4
-0@@ -13,6 +13,7 @@#include <linux/bitops.h> #include <linux/mtd/cfi.h> #include <linux/mtd/mtd.h> +#include <linux/mtd/flashchip.h> /* * Manufacturer IDs @@ -210,6 +211,9 @@ struct spi_nor {void *priv;const struct flash_info *info; +spinlock_t chip_lock; +wait_queue_head_t wq; +flstate_t state; }; static inline void spi_nor_set_flash_node(struct spi_nor *nor,
Signature · 6 lines
Best regards, Marek Vasut ______________________________________________________ Linux MTD discussion mailing list http://lists.infradead.org/mailman/listinfo/linux-mtd/
Re: [PATCH v6 2/3] mtd: spi-nor: Add spi-nor flash device synchronization between flash states
Marek,
Quoted 5 lines · Marek Vasut
Quoted 1 line · Kamal Dasu
Added flash access synchronization methods spi_nor_get/release_device(). This
Should be get/put to be consistent with the rest of the kernel ...
Can change that.
Quoted 22 lines
Quoted 22 lines
change allows spi-nor driver to maintain flash states in spi-nor stucture for read, write, erase, lock, unlock nor ops. Only when the flash state is
FL_READYa new state is set and spi-nor flash op is initiated. The state change is done with aspin_lockheld and released as soon as the state is changed. Else the current process for spi-nor transfer is queued till the flash is inFL_READYstate. This change allows us to add mtd layer synchronization when the mtd resume suspend handlers are added.Signed-off-by Kamal Dasu <[email protected]>--- drivers/mtd/spi-nor/spi-nor.c | 69 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 4 +++ 2 files changed, 73 insertions(+)drivers/mtd/spi-nor/spi-nor.c +0
-0@@ -89,6 +89,15 @@ struct flash_info {#define JEDEC_MFR (info) ((info)->id[0])
Quoted 12 lines
drivers/mtd/spi-nor/spi-nor.c struct flash_info { · line 99
Quoted 8 lines
drivers/mtd/spi-nor/spi-nor.c +8
-0+/* map table for spi_nor op to flashchip state */ +static int spi_nor_state[] = { + [SPI_NOR_OPS_READ ] =FL_READING , + [SPI_NOR_OPS_WRITE ] =FL_WRITING , + [SPI_NOR_OPS_ERASE ] =FL_ERASING , + [SPI_NOR_OPS_LOCK ] =FL_LOCKING , + [SPI_NOR_OPS_UNLOCK ] =FL_UNLOCKING , +};Can't you just retain which instruction is in progress instead of adding yet another orthogonal
FL_FOO?
I just used what other mtd flash drivers use. I could use the ops, but will have to add the missing ops/states.
Quoted 33 lines
Quoted 33 lines
drivers/mtd/spi-nor/spi-nor.c +26
-0static const struct flash_info *spi_nor_match_id (const char *name);/* @@ -396,10 +405,64 @@ static int erase_chip(struct spi_nor *nor)return nor->write_reg (nor,SPINOR_OP_CHIP_ERASE ,NULL ,0 ); } +/** +* spi_nor_get_device - [GENERIC] Get chip for selected access +* @param mtd MTD device structure +* @param new_state the state which is requested +* +* Get the nor flash device and lock it for exclusive access +*/ +static int spi_nor_get_device (struct mtd_info *mtd,int new_state) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); +DECLARE_WAITQUEUE (wait, current); + +/* +* Grab the lock and see if the device is available +*/ +while (1 ) { +spin_lock (&nor->chip_lock); +if (nor->state ==FL_READY ) { + nor->state = new_state; +spin_unlock (&nor->chip_lock); +break ; + } +if (new_state ==FL_PM_SUSPENDED ) { +spin_unlock (&nor->chip_lock); +return (nor->state ==FL_PM_SUSPENDED ) ?0 : -EAGAIN ; + }
Quoted 9 lines
drivers/mtd/spi-nor/spi-nor.c static int erase_chip(struct spi_nor *nor) · line 438
Quoted 5 lines
drivers/mtd/spi-nor/spi-nor.c +5
-0+set_current_state (TASK_UNINTERRUPTIBLE ); +add_wait_queue (&nor->wq, &wait); +spin_unlock (&nor->chip_lock); +schedule (); +remove_wait_queue (&nor->wq, &wait);Somehow, I have to wonder, doesn't runtime PM implement this sort of functionality already ?
I did not see any API I could apply here.
Quoted 78 lines
Quoted 78 lines
drivers/mtd/spi-nor/spi-nor.c +29
-0+ } + +return 0 ; +} + +/** +* spi_nor_release_device - [GENERIC] release chip +* @mtd: MTD device structure +* +* Release nor flash chip lock and wake up anyone waiting on the device. +*/ +static void spi_nor_release_device (struct mtd_info *mtd) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); + +/* Release the controller and the chip */ +spin_lock (&nor->chip_lock); + nor->state =FL_READY ; +wake_up (&nor->wq); +spin_unlock (&nor->chip_lock); +} +static int spi_nor_lock_and_prep (struct spi_nor *nor,enum spi_nor_ops ops) {int ret =0 ; +spi_nor_get_device (&nor->mtd, spi_nor_state[ops]);mutex_lock (&nor->lock);if (nor->prepare) {@@ -407,6 +470,7 @@ static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)if (ret) {dev_err (nor->dev,"failed in the preparation.\n" );mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd);return ret; } }@@ -418,6 +482,7 @@ static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)if (nor->unprepare) nor->unprepare (nor, ops);mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd); }/* @@ -1743,6 +1808,10 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)return ret; } + nor->state =FL_READY ; +init_waitqueue_head (&nor->wq); +spin_lock_init (&nor->chip_lock); +/* * call init function to send necessary spi-nor read/write config * commands to nor flash based on above nor settings include/linux/mtd/spi-nor.h +4
-0@@ -13,6 +13,7 @@#include <linux/bitops.h> #include <linux/mtd/cfi.h> #include <linux/mtd/mtd.h> +#include <linux/mtd/flashchip.h> /* * Manufacturer IDs @@ -210,6 +211,9 @@ struct spi_nor {void *priv;const struct flash_info *info; +spinlock_t chip_lock; +wait_queue_head_t wq; +flstate_t state; };
Quoted 7 lines
include/linux/mtd/spi-nor.h struct spi_nor { · line 219
Quoted 2 lines
include/linux/mtd/spi-nor.h +0
-0static inline void spi_nor_set_flash_node(struct spi_nor *nor,--
Best regards,
Marek Vasut
Thanks
Kamal
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v6 2/3] mtd: spi-nor: Add spi-nor flash device synchronization between flash states
Quoted 46 lines · Kamal Dasu
Marek,
Quoted 5 lines · Marek Vasut
Quoted 1 line · Kamal Dasu
Added flash access synchronization methods spi_nor_get/release_device(). This
Should be get/put to be consistent with the rest of the kernel ...
Can change that.
Quoted 22 lines
Quoted 22 lines
change allows spi-nor driver to maintain flash states in spi-nor stucture for read, write, erase, lock, unlock nor ops. Only when the flash state is
FL_READYa new state is set and spi-nor flash op is initiated. The state change is done with aspin_lockheld and released as soon as the state is changed. Else the current process for spi-nor transfer is queued till the flash is inFL_READYstate. This change allows us to add mtd layer synchronization when the mtd resume suspend handlers are added.Signed-off-by Kamal Dasu <[email protected]>--- drivers/mtd/spi-nor/spi-nor.c | 69 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 4 +++ 2 files changed, 73 insertions(+)drivers/mtd/spi-nor/spi-nor.c +0
-0@@ -89,6 +89,15 @@ struct flash_info {#define JEDEC_MFR (info) ((info)->id[0])Quoted 12 lines
drivers/mtd/spi-nor/spi-nor.c struct flash_info { · line 99
Quoted 8 lines
drivers/mtd/spi-nor/spi-nor.c +8
-0+/* map table for spi_nor op to flashchip state */ +static int spi_nor_state[] = { + [SPI_NOR_OPS_READ ] =FL_READING , + [SPI_NOR_OPS_WRITE ] =FL_WRITING , + [SPI_NOR_OPS_ERASE ] =FL_ERASING , + [SPI_NOR_OPS_LOCK ] =FL_LOCKING , + [SPI_NOR_OPS_UNLOCK ] =FL_UNLOCKING , +};Can't you just retain which instruction is in progress instead of adding yet another orthogonal
FL_FOO?
Quoted 2 lines
I just used what other mtd flash drivers use. I could use the ops, but will have to add the missing ops/states.
What is missing ?
Quoted 43 lines
Quoted 33 lines
Quoted 33 lines
drivers/mtd/spi-nor/spi-nor.c +26
-0static const struct flash_info *spi_nor_match_id (const char *name);/* @@ -396,10 +405,64 @@ static int erase_chip(struct spi_nor *nor)return nor->write_reg (nor,SPINOR_OP_CHIP_ERASE ,NULL ,0 ); } +/** +* spi_nor_get_device - [GENERIC] Get chip for selected access +* @param mtd MTD device structure +* @param new_state the state which is requested +* +* Get the nor flash device and lock it for exclusive access +*/ +static int spi_nor_get_device (struct mtd_info *mtd,int new_state) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); +DECLARE_WAITQUEUE (wait, current); + +/* +* Grab the lock and see if the device is available +*/ +while (1 ) { +spin_lock (&nor->chip_lock); +if (nor->state ==FL_READY ) { + nor->state = new_state; +spin_unlock (&nor->chip_lock); +break ; + } +if (new_state ==FL_PM_SUSPENDED ) { +spin_unlock (&nor->chip_lock); +return (nor->state ==FL_PM_SUSPENDED ) ?0 : -EAGAIN ; + }Quoted 9 lines
drivers/mtd/spi-nor/spi-nor.c static int erase_chip(struct spi_nor *nor) · line 438
Quoted 5 lines
drivers/mtd/spi-nor/spi-nor.c +5
-0+set_current_state (TASK_UNINTERRUPTIBLE ); +add_wait_queue (&nor->wq, &wait); +spin_unlock (&nor->chip_lock); +schedule (); +remove_wait_queue (&nor->wq, &wait);Somehow, I have to wonder, doesn't runtime PM implement this sort of functionality already ?
Quoted 1 line
I did not see any API I could apply here.
Does runtime_pm_get()/runtime_pm_put() help here ?
[...]
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v6 2/3] mtd: spi-nor: Add spi-nor flash device synchronization between flash states
In reply to Kamal DasuHi Kamal,
+Boris for his knowledge on power management.
Quoted 9 lines · Kamal Dasu
Added flash access synchronization methods spi_nor_get/release_device(). This change allows spi-nor driver to maintain flash states in spi-nor stucture for read, write, erase, lock, unlock nor ops. Only when the flash state is
FL_READYa new state is set and spi-nor flash op is initiated. The state change is done with aspin_lockheld and released as soon as the state is changed. Else the current process for spi-nor transfer is queued till the flash is inFL_READYstate. This change allows us to add mtd layer synchronization when the mtd resume suspend handlers are added.
This version goes the wrong way, IMHO. This is not what I meant when I was talking about synchronizing MTD and SPI devices: when accessing the MTD device we should wake the SPI device up instead of blocking the MTD handlers when the SPI device enters its suspended mode.
However, the good news is that looking closer at how the runtime pm works, this synchronization should already be managed:
in drivers/mtd/devices/m25p80.c:
nor->dev = &spi->dev;
and in drivers/mtd/spi-nor/spi-nor.c:
struct device *dev = nor->dev; [...] mtd->dev.parent = dev;
Hence the SPI device is the parent of the MTD device.
Then by default, when resuming a device, the runtime pm framework wakes the device parent up too: see rmp_resume() from drivers/base/power/runtime.c
About the SPI controller, by default SPI masters ignore their children, so the SPI controller won't be resumed by the m25p80 SPI device. However spi_sync() can resume the controller when master->auto_runtime_pm is true.
Then back to the MTD subsystem, maybe you should patch the mtdcore.c file rather than m25p80.c so the whole MTD subsystem could take advantage of your work on power management:
you could update the .pm member of 'struct class' mtd_class to add runtime pm support to the already existing system-wide power management support. I guess you will have to add new hooks such mtd->_pm_suspend and mtd->_pm_resume. Finally spi-nor.c will implement those 2 hooks (and the NAND subsystem may also implement them as needed).
Boris, does it make sense?
Also, one last comment: I guess a call to pm_runtime_enable() is missing somewhere is this series, otherwise I don't think this could work. Besides, depending on where it will be added, we should not call pm_runtime_enable() unconditionally thinking of users who don't want to use this feature sending unneeded SPI commands to the memory.
Best regards,
Cyrille
Quoted 132 lines
Signed-off-by Kamal Dasu <[email protected]>--- drivers/mtd/spi-nor/spi-nor.c | 69 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 4 +++ 2 files changed, 73 insertions(+)drivers/mtd/spi-nor/spi-nor.c +69
-0@@ -89,6 +89,15 @@ struct flash_info {#define JEDEC_MFR (info) ((info)->id[0]) +/* map table for spi_nor op to flashchip state */ +static int spi_nor_state[] = { + [SPI_NOR_OPS_READ ] =FL_READING , + [SPI_NOR_OPS_WRITE ] =FL_WRITING , + [SPI_NOR_OPS_ERASE ] =FL_ERASING , + [SPI_NOR_OPS_LOCK ] =FL_LOCKING , + [SPI_NOR_OPS_UNLOCK ] =FL_UNLOCKING , +}; +static const struct flash_info *spi_nor_match_id (const char *name);/* @@ -396,10 +405,64 @@ static int erase_chip(struct spi_nor *nor)return nor->write_reg (nor,SPINOR_OP_CHIP_ERASE ,NULL ,0 ); } +/** +* spi_nor_get_device - [GENERIC] Get chip for selected access +* @param mtd MTD device structure +* @param new_state the state which is requested +* +* Get the nor flash device and lock it for exclusive access +*/ +static int spi_nor_get_device (struct mtd_info *mtd,int new_state) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); +DECLARE_WAITQUEUE (wait, current); + +/* +* Grab the lock and see if the device is available +*/ +while (1 ) { +spin_lock (&nor->chip_lock); +if (nor->state ==FL_READY ) { + nor->state = new_state; +spin_unlock (&nor->chip_lock); +break ; + } +if (new_state ==FL_PM_SUSPENDED ) { +spin_unlock (&nor->chip_lock); +return (nor->state ==FL_PM_SUSPENDED ) ?0 : -EAGAIN ; + } +set_current_state (TASK_UNINTERRUPTIBLE ); +add_wait_queue (&nor->wq, &wait); +spin_unlock (&nor->chip_lock); +schedule (); +remove_wait_queue (&nor->wq, &wait); + } + +return 0 ; +} + +/** +* spi_nor_release_device - [GENERIC] release chip +* @mtd: MTD device structure +* +* Release nor flash chip lock and wake up anyone waiting on the device. +*/ +static void spi_nor_release_device (struct mtd_info *mtd) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); + +/* Release the controller and the chip */ +spin_lock (&nor->chip_lock); + nor->state =FL_READY ; +wake_up (&nor->wq); +spin_unlock (&nor->chip_lock); +} +static int spi_nor_lock_and_prep (struct spi_nor *nor,enum spi_nor_ops ops) {int ret =0 ; +spi_nor_get_device (&nor->mtd, spi_nor_state[ops]);mutex_lock (&nor->lock);if (nor->prepare) {@@ -407,6 +470,7 @@ static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)if (ret) {dev_err (nor->dev,"failed in the preparation.\n" );mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd);return ret; } }@@ -418,6 +482,7 @@ static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)if (nor->unprepare) nor->unprepare (nor, ops);mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd); }/* @@ -1743,6 +1808,10 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)return ret; } + nor->state =FL_READY ; +init_waitqueue_head (&nor->wq); +spin_lock_init (&nor->chip_lock); +/* * call init function to send necessary spi-nor read/write config * commands to nor flash based on above nor settings include/linux/mtd/spi-nor.h +1
-0@@ -13,6 +13,7 @@#include <linux/bitops.h> #include <linux/mtd/cfi.h> #include <linux/mtd/mtd.h> +#include <linux/mtd/flashchip.h> /* * Manufacturer IDs
include/linux/mtd/spi-nor.h struct spi_nor { · line 219
Quoted 9 lines
include/linux/mtd/spi-nor.h +3
-0@@ -210,6 +211,9 @@ struct spi_nor {void *priv;const struct flash_info *info; +spinlock_t chip_lock; +wait_queue_head_t wq; +flstate_t state; }; static inline void spi_nor_set_flash_node(struct spi_nor *nor,
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
Re: [PATCH v6 2/3] mtd: spi-nor: Add spi-nor flash device synchronization between flash states
Quoted 50 lines · Cyrille Pitchen
Hi Kamal,
+Boris for his knowledge on power management.
Quoted 9 lines · Kamal Dasu
Added flash access synchronization methods spi_nor_get/release_device(). This change allows spi-nor driver to maintain flash states in spi-nor stucture for read, write, erase, lock, unlock nor ops. Only when the flash state is
FL_READYa new state is set and spi-nor flash op is initiated. The state change is done with aspin_lockheld and released as soon as the state is changed. Else the current process for spi-nor transfer is queued till the flash is inFL_READYstate. This change allows us to add mtd layer synchronization when the mtd resume suspend handlers are added.This version goes the wrong way, IMHO. This is not what I meant when I was talking about synchronizing MTD and SPI devices: when accessing the MTD device we should wake the SPI device up instead of blocking the MTD handlers when the SPI device enters its suspended mode.
However, the good news is that looking closer at how the runtime pm works, this synchronization should already be managed:
in
drivers/mtd/devices/m25p80.c:nor->dev = &spi->dev;and in
drivers/mtd/spi-nor/spi-nor.c:struct device *dev = nor->dev; [...] mtd->dev.parent = dev;Hence the SPI device is the parent of the MTD device.
Then by default, when resuming a device, the runtime pm framework wakes the device parent up too: seermp_resume()fromdrivers/base/power/runtime.cAbout the SPI controller, by default SPI masters ignore their children, so the SPI controller won't be resumed by the m25p80 SPI device. However
spi_sync()can resume the controller whenmaster->auto_runtime_pmis true.Then back to the MTD subsystem, maybe you should patch the
mtdcore.cfile rather thanm25p80.cso the whole MTD subsystem could take advantage of your work on power management:
you could update the .pm member of 'struct class'mtd_classto add runtime pm support to the already existing system-wide power management support. I guess you will have to add new hooks suchmtd->_pm_suspendandmtd->_pm_resume. Finallyspi-nor.cwill implement those 2 hooks (and the NAND subsystem may also implement them as needed).
Quoted 8 lines
Boris, does it make sense?
Also, one last comment: I guess a call to
pm_runtime_enable()is missing somewhere is this series, otherwise I don't think this could work. Besides, depending on where it will be added, we should not callpm_runtime_enable()unconditionally thinking of users who don't want to use this feature sending unneeded SPI commands to the memory.
To be clear I was implementing for system sleep and S2/S3 pm modes, so mtd_resume() and mtd_suspend() or the m25p80 suspend/resume is what I need. I am not not having a run_time_pm requirement.
Quoted 4 lines
Best regards,
Cyrille
Kamal
Quoted 140 lines
Quoted 140 lines
Signed-off-by Kamal Dasu <[email protected]>--- drivers/mtd/spi-nor/spi-nor.c | 69 +++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/spi-nor.h | 4 +++ 2 files changed, 73 insertions(+)drivers/mtd/spi-nor/spi-nor.c +69
-0@@ -89,6 +89,15 @@ struct flash_info {#define JEDEC_MFR (info) ((info)->id[0]) +/* map table for spi_nor op to flashchip state */ +static int spi_nor_state[] = { + [SPI_NOR_OPS_READ ] =FL_READING , + [SPI_NOR_OPS_WRITE ] =FL_WRITING , + [SPI_NOR_OPS_ERASE ] =FL_ERASING , + [SPI_NOR_OPS_LOCK ] =FL_LOCKING , + [SPI_NOR_OPS_UNLOCK ] =FL_UNLOCKING , +}; +static const struct flash_info *spi_nor_match_id (const char *name);/* @@ -396,10 +405,64 @@ static int erase_chip(struct spi_nor *nor)return nor->write_reg (nor,SPINOR_OP_CHIP_ERASE ,NULL ,0 ); } +/** +* spi_nor_get_device - [GENERIC] Get chip for selected access +* @param mtd MTD device structure +* @param new_state the state which is requested +* +* Get the nor flash device and lock it for exclusive access +*/ +static int spi_nor_get_device (struct mtd_info *mtd,int new_state) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); +DECLARE_WAITQUEUE (wait, current); + +/* +* Grab the lock and see if the device is available +*/ +while (1 ) { +spin_lock (&nor->chip_lock); +if (nor->state ==FL_READY ) { + nor->state = new_state; +spin_unlock (&nor->chip_lock); +break ; + } +if (new_state ==FL_PM_SUSPENDED ) { +spin_unlock (&nor->chip_lock); +return (nor->state ==FL_PM_SUSPENDED ) ?0 : -EAGAIN ; + } +set_current_state (TASK_UNINTERRUPTIBLE ); +add_wait_queue (&nor->wq, &wait); +spin_unlock (&nor->chip_lock); +schedule (); +remove_wait_queue (&nor->wq, &wait); + } + +return 0 ; +} + +/** +* spi_nor_release_device - [GENERIC] release chip +* @mtd: MTD device structure +* +* Release nor flash chip lock and wake up anyone waiting on the device. +*/ +static void spi_nor_release_device (struct mtd_info *mtd) +{ +struct spi_nor *nor =mtd_to_spi_nor (mtd); + +/* Release the controller and the chip */ +spin_lock (&nor->chip_lock); + nor->state =FL_READY ; +wake_up (&nor->wq); +spin_unlock (&nor->chip_lock); +} +static int spi_nor_lock_and_prep (struct spi_nor *nor,enum spi_nor_ops ops) {int ret =0 ; +spi_nor_get_device (&nor->mtd, spi_nor_state[ops]);mutex_lock (&nor->lock);if (nor->prepare) {@@ -407,6 +470,7 @@ static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)if (ret) {dev_err (nor->dev,"failed in the preparation.\n" );mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd);return ret; } }@@ -418,6 +482,7 @@ static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)if (nor->unprepare) nor->unprepare (nor, ops);mutex_unlock (&nor->lock); +spi_nor_release_device (&nor->mtd); }/* @@ -1743,6 +1808,10 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)return ret; } + nor->state =FL_READY ; +init_waitqueue_head (&nor->wq); +spin_lock_init (&nor->chip_lock); +/* * call init function to send necessary spi-nor read/write config * commands to nor flash based on above nor settings include/linux/mtd/spi-nor.h +4
-0@@ -13,6 +13,7 @@#include <linux/bitops.h> #include <linux/mtd/cfi.h> #include <linux/mtd/mtd.h> +#include <linux/mtd/flashchip.h> /* * Manufacturer IDs @@ -210,6 +211,9 @@ struct spi_nor {void *priv;const struct flash_info *info; +spinlock_t chip_lock; +wait_queue_head_t wq; +flstate_t state; };
Quoted 3 lines
include/linux/mtd/spi-nor.h struct spi_nor { · line 219
Quoted 2 lines
include/linux/mtd/spi-nor.h +0
-0static inline void spi_nor_set_flash_node(struct spi_nor *nor,
--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html