Skip to content

driver: flash: Add Set/ Get write protect function #90054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions drivers/flash/flash_realtek_rts5912.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,20 +427,44 @@ static int flash_read_sr2(const struct device *dev, uint8_t *val)
}
#endif

#ifdef CONFIG_FLASH_EX_OP_ENABLED
#define SPIC_BASE 0x40010200ul
static int flash_set_wp(const struct device *dev, uint8_t *val, uint8_t cnt)
{
volatile uint32_t *reg = (volatile uint32_t *)(SPIC_BASE + 0x110u);

if (!val) {
return -EINVAL;
}

if (*val) {
*reg |= (1u << 1);
}

return 0;
}

static int flash_get_wp(const struct device *dev, uint8_t *val)
{
volatile uint32_t *reg = (volatile uint32_t *)(SPIC_BASE + 0x110u);
uint32_t tmp = *reg;

*val = (uint8_t)(tmp & 0x02u);

return 0;
}
#endif

static int flash_wait_till_ready(const struct device *dev)
{
int ret;
int timeout = TIMEOUT_SPIBUSY;
uint8_t sr = 0;

/* If it's a sector erase loop, it requires approximately 3000 cycles,
* while a program page requires about 40 cycles.
*/
do {
ret = flash_read_sr(dev, &sr);
if (ret < 0) {
return ret;
}
flash_read_sr(dev, &sr);
if (!(sr & SPI_NOR_WIP_BIT)) {
return 0;
}
Expand Down Expand Up @@ -751,6 +775,12 @@ static int flash_rts5912_ex_op(const struct device *dev, uint16_t opcode, const
case FLASH_RTS5912_EX_OP_RD_SR2:
ret = flash_read_sr2(dev, (uint8_t *)in);
break;
case FLASH_RTS5912_EX_OP_SET_WP:
ret = flash_set_wp(dev, (uint8_t *)out, 1);
break;
case FLASH_RTS5912_EX_OP_GET_WP:
ret = flash_get_wp(dev, (uint8_t *)in);
break;
}

k_sem_give(&dev_data->sem);
Expand Down
2 changes: 2 additions & 0 deletions include/zephyr/drivers/flash/rts5912_flash_api_ex.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ enum flash_rts5912_ex_ops {
FLASH_RTS5912_EX_OP_WR_SR2,
FLASH_RTS5912_EX_OP_RD_SR,
FLASH_RTS5912_EX_OP_RD_SR2,
FLASH_RTS5912_EX_OP_SET_WP,
FLASH_RTS5912_EX_OP_GET_WP,
};

#endif /* __ZEPHYR_INCLUDE_DRIVERS_RTS5912_FLASH_API_EX_H__ */