Skip to content

Commit 59d6748

Browse files
committed
driver: flash: Add Set/ Get write protect function
Add Set_WP function to set SPI flash WP line to low Add Get_WP function to obtain status of the SPI flash WP line Signed-off-by: Benson Huang <[email protected]>
1 parent 6e34acc commit 59d6748

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

drivers/flash/flash_realtek_rts5912.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,20 +427,44 @@ static int flash_read_sr2(const struct device *dev, uint8_t *val)
427427
}
428428
#endif
429429

430+
#ifdef CONFIG_FLASH_EX_OP_ENABLED
431+
#define SPIC_BASE 0x40010200ul
432+
static int flash_set_wp(const struct device *dev, uint8_t *val, uint8_t cnt)
433+
{
434+
volatile uint32_t *reg = (volatile uint32_t *)(SPIC_BASE + 0x110u);
435+
436+
if (!val) {
437+
return -EINVAL;
438+
}
439+
440+
if (*val) {
441+
*reg |= (1u << 1);
442+
}
443+
444+
return 0;
445+
}
446+
447+
static int flash_get_wp(const struct device *dev, uint8_t *val)
448+
{
449+
volatile uint32_t *reg = (volatile uint32_t *)(SPIC_BASE + 0x110u);
450+
uint32_t tmp = *reg;
451+
452+
*val = (uint8_t)(tmp & 0x02u);
453+
454+
return 0;
455+
}
456+
#endif
457+
430458
static int flash_wait_till_ready(const struct device *dev)
431459
{
432-
int ret;
433460
int timeout = TIMEOUT_SPIBUSY;
434461
uint8_t sr = 0;
435462

436463
/* If it's a sector erase loop, it requires approximately 3000 cycles,
437464
* while a program page requires about 40 cycles.
438465
*/
439466
do {
440-
ret = flash_read_sr(dev, &sr);
441-
if (ret < 0) {
442-
return ret;
443-
}
467+
flash_read_sr(dev, &sr);
444468
if (!(sr & SPI_NOR_WIP_BIT)) {
445469
return 0;
446470
}
@@ -751,6 +775,12 @@ static int flash_rts5912_ex_op(const struct device *dev, uint16_t opcode, const
751775
case FLASH_RTS5912_EX_OP_RD_SR2:
752776
ret = flash_read_sr2(dev, (uint8_t *)in);
753777
break;
778+
case FLASH_RTS5912_EX_OP_SET_WP:
779+
ret = flash_set_wp(dev, (uint8_t *)out, 1);
780+
break;
781+
case FLASH_RTS5912_EX_OP_GET_WP:
782+
ret = flash_get_wp(dev, (uint8_t *)in);
783+
break;
754784
}
755785

756786
k_sem_give(&dev_data->sem);

include/zephyr/drivers/flash/rts5912_flash_api_ex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ enum flash_rts5912_ex_ops {
1313
FLASH_RTS5912_EX_OP_WR_SR2,
1414
FLASH_RTS5912_EX_OP_RD_SR,
1515
FLASH_RTS5912_EX_OP_RD_SR2,
16+
FLASH_RTS5912_EX_OP_SET_WP,
17+
FLASH_RTS5912_EX_OP_GET_WP,
1618
};
1719

1820
#endif /* __ZEPHYR_INCLUDE_DRIVERS_RTS5912_FLASH_API_EX_H__ */

0 commit comments

Comments
 (0)