Skip to content

Commit 3e8ec3a

Browse files
benson0715kartben
authored andcommitted
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 42aef1b commit 3e8ec3a

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
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+
static int flash_set_wp(const struct device *dev, uint8_t *val)
432+
{
433+
const struct flash_rts5912_dev_config *config = dev->config;
434+
volatile struct reg_spic_reg *spic_reg = config->regs;
435+
436+
if (!val) {
437+
return -EINVAL;
438+
}
439+
440+
if (*val) {
441+
spic_reg->CTRLR2 |= SPIC_CTRLR2_WPN_SET;
442+
}
443+
444+
return 0;
445+
}
446+
447+
static int flash_get_wp(const struct device *dev, uint8_t *val)
448+
{
449+
const struct flash_rts5912_dev_config *config = dev->config;
450+
volatile struct reg_spic_reg *spic_reg = config->regs;
451+
452+
*val = (uint8_t)(spic_reg->CTRLR2 & SPIC_CTRLR2_WPN_SET);
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);
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__ */

soc/realtek/ec/rts5912/reg/reg_spic.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ struct reg_spic_reg {
3737
uint16_t HALF;
3838
uint32_t WORD;
3939
} DR;
40-
const uint32_t RESERVED2[44];
40+
const uint32_t RESERVED2[43];
41+
uint32_t CTRLR2;
4142
uint32_t FBAUD;
4243
uint32_t USERLENGTH;
4344
const uint32_t RESERVED3[3];
@@ -107,6 +108,8 @@ struct reg_spic_reg {
107108
#define SPIC_RISR_FSEIR BIT(5UL)
108109
#define SPIC_RISR_USEIR BIT(9UL)
109110
#define SPIC_RISR_TFSIR BIT(10UL)
111+
/* CTRLR2 */
112+
#define SPIC_CTRLR2_WPN_SET BIT(1UL)
110113
/* USERLENGTH */
111114
#define SPIC_USERLENGTH_RDDUMMYLEN_Pos (0UL)
112115
#define SPIC_USERLENGTH_RDDUMMYLEN_Msk GENMASK(11, 0)

0 commit comments

Comments
 (0)