mtd: spi-nor: factor out replace-able flash_{lock,unlock}
Flash lock/unlock is a flash-specific operations. Factor out a callback for it to more readily support other vendors. Signed-off-by: Brian Norris <computersforpeace@gmail.com> Tested-by: VIET NGA DAO <vndao@altera.com>
This commit is contained in:
@@ -369,17 +369,13 @@ erase_err:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
static int stm_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
|
||||||
{
|
{
|
||||||
struct spi_nor *nor = mtd_to_spi_nor(mtd);
|
struct mtd_info *mtd = nor->mtd;
|
||||||
uint32_t offset = ofs;
|
uint32_t offset = ofs;
|
||||||
uint8_t status_old, status_new;
|
uint8_t status_old, status_new;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
|
|
||||||
if (ret)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
status_old = read_sr(nor);
|
status_old = read_sr(nor);
|
||||||
|
|
||||||
if (offset < mtd->size - (mtd->size / 2))
|
if (offset < mtd->size - (mtd->size / 2))
|
||||||
@@ -402,26 +398,18 @@ static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
|||||||
(status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
|
(status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
|
||||||
write_enable(nor);
|
write_enable(nor);
|
||||||
ret = write_sr(nor, status_new);
|
ret = write_sr(nor, status_new);
|
||||||
if (ret)
|
|
||||||
goto err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err:
|
|
||||||
spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
static int stm_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
|
||||||
{
|
{
|
||||||
struct spi_nor *nor = mtd_to_spi_nor(mtd);
|
struct mtd_info *mtd = nor->mtd;
|
||||||
uint32_t offset = ofs;
|
uint32_t offset = ofs;
|
||||||
uint8_t status_old, status_new;
|
uint8_t status_old, status_new;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
|
|
||||||
if (ret)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
status_old = read_sr(nor);
|
status_old = read_sr(nor);
|
||||||
|
|
||||||
if (offset+len > mtd->size - (mtd->size / 64))
|
if (offset+len > mtd->size - (mtd->size / 64))
|
||||||
@@ -444,15 +432,41 @@ static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
|||||||
(status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
|
(status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
|
||||||
write_enable(nor);
|
write_enable(nor);
|
||||||
ret = write_sr(nor, status_new);
|
ret = write_sr(nor, status_new);
|
||||||
if (ret)
|
|
||||||
goto err;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err:
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
||||||
|
{
|
||||||
|
struct spi_nor *nor = mtd_to_spi_nor(mtd);
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
ret = nor->flash_lock(nor, ofs, len);
|
||||||
|
|
||||||
spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
|
spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
||||||
|
{
|
||||||
|
struct spi_nor *nor = mtd_to_spi_nor(mtd);
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
|
ret = nor->flash_unlock(nor, ofs, len);
|
||||||
|
|
||||||
|
spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
/* Used when the "_ext_id" is two bytes at most */
|
/* Used when the "_ext_id" is two bytes at most */
|
||||||
#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
|
#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
|
||||||
((kernel_ulong_t)&(struct flash_info) { \
|
((kernel_ulong_t)&(struct flash_info) { \
|
||||||
@@ -1045,6 +1059,11 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
|
|||||||
|
|
||||||
/* nor protection support for STmicro chips */
|
/* nor protection support for STmicro chips */
|
||||||
if (JEDEC_MFR(info) == CFI_MFR_ST) {
|
if (JEDEC_MFR(info) == CFI_MFR_ST) {
|
||||||
|
nor->flash_lock = stm_lock;
|
||||||
|
nor->flash_unlock = stm_unlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nor->flash_lock && nor->flash_unlock) {
|
||||||
mtd->_lock = spi_nor_lock;
|
mtd->_lock = spi_nor_lock;
|
||||||
mtd->_unlock = spi_nor_unlock;
|
mtd->_unlock = spi_nor_unlock;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,6 +155,8 @@ enum spi_nor_option_flags {
|
|||||||
* @write: [DRIVER-SPECIFIC] write data to the SPI NOR
|
* @write: [DRIVER-SPECIFIC] write data to the SPI NOR
|
||||||
* @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
|
* @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
|
||||||
* at the offset @offs
|
* at the offset @offs
|
||||||
|
* @lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
|
||||||
|
* @unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
|
||||||
* @priv: the private data
|
* @priv: the private data
|
||||||
*/
|
*/
|
||||||
struct spi_nor {
|
struct spi_nor {
|
||||||
@@ -189,6 +191,9 @@ struct spi_nor {
|
|||||||
size_t len, size_t *retlen, const u_char *write_buf);
|
size_t len, size_t *retlen, const u_char *write_buf);
|
||||||
int (*erase)(struct spi_nor *nor, loff_t offs);
|
int (*erase)(struct spi_nor *nor, loff_t offs);
|
||||||
|
|
||||||
|
int (*flash_lock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
|
||||||
|
int (*flash_unlock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
|
||||||
|
|
||||||
void *priv;
|
void *priv;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user