Skip to content

Commit 549def6

Browse files
TomChang19kartben
authored andcommitted
drivers: espi: npcx: update the handler for accessing the flash
This commit updates the handler of the eSPI TAF request for accessing two external flashes. Signed-off-by: Tom Chang <[email protected]>
1 parent 5c62097 commit 549def6

File tree

1 file changed

+85
-2
lines changed

1 file changed

+85
-2
lines changed

drivers/espi/espi_taf_npcx.c

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@
1818

1919
LOG_MODULE_REGISTER(espi_taf, CONFIG_ESPI_LOG_LEVEL);
2020

21-
static const struct device *const spi_dev = DEVICE_DT_GET(DT_ALIAS(taf_flash));
21+
#define NPCX_TAF_PRIME_FLASH_NODE DT_ALIAS(taf_flash)
22+
#define NPCX_TAF_SEC_FLASH_NODE DT_ALIAS(taf_flash1)
23+
24+
#define NPCX_TAF_ALLOC_SIZE(node) (MB(1) << DT_ENUM_IDX(node, spi_dev_size))
25+
26+
static const struct device *const spi_dev = DEVICE_DT_GET(NPCX_TAF_PRIME_FLASH_NODE);
27+
#if DT_NODE_HAS_STATUS_OKAY(NPCX_TAF_SEC_FLASH_NODE)
28+
static const struct device *const spi_dev1 = DEVICE_DT_GET(NPCX_TAF_SEC_FLASH_NODE);
29+
#endif
2230

2331
enum ESPI_TAF_ERASE_LEN {
2432
NPCX_ESPI_TAF_ERASE_LEN_4KB,
@@ -52,6 +60,11 @@ struct espi_taf_npcx_data {
5260
uint32_t src[16];
5361
uint8_t read_buf[MAX_TX_PAYLOAD_SIZE];
5462
struct k_work work;
63+
#if DT_NODE_HAS_STATUS_OKAY(NPCX_TAF_SEC_FLASH_NODE)
64+
const struct device *low_dev_ptr;
65+
const struct device *high_dev_ptr;
66+
uint32_t low_dev_size;
67+
#endif
5568
};
5669

5770
static struct espi_taf_npcx_data npcx_espi_taf_data;
@@ -236,6 +249,11 @@ static bool espi_taf_npcx_channel_ready(const struct device *dev)
236249
if (!device_is_ready(spi_dev)) {
237250
return false;
238251
}
252+
#if DT_NODE_HAS_STATUS_OKAY(NPCX_TAF_SEC_FLASH_NODE)
253+
if (!device_is_ready(spi_dev1)) {
254+
return false;
255+
}
256+
#endif
239257

240258
return true;
241259
}
@@ -362,7 +380,34 @@ static int espi_taf_npcx_flash_read(const struct device *dev, struct espi_saf_pa
362380
}
363381

364382
do {
383+
#if DT_NODE_HAS_STATUS_OKAY(NPCX_TAF_SEC_FLASH_NODE)
384+
if ((addr + len) <= npcx_espi_taf_data.low_dev_size) {
385+
rc = flash_read(npcx_espi_taf_data.low_dev_ptr, addr,
386+
npcx_espi_taf_data.read_buf, len);
387+
} else if (addr >= npcx_espi_taf_data.low_dev_size) {
388+
rc = flash_read(npcx_espi_taf_data.high_dev_ptr,
389+
(addr - npcx_espi_taf_data.low_dev_size),
390+
npcx_espi_taf_data.read_buf, len);
391+
} else {
392+
rc = flash_read(npcx_espi_taf_data.low_dev_ptr, addr,
393+
npcx_espi_taf_data.read_buf,
394+
(npcx_espi_taf_data.low_dev_size.low_dev_size - addr));
395+
396+
if (rc) {
397+
LOG_ERR("flash read fail 0x%x", rc);
398+
return -EIO;
399+
}
400+
401+
uint32_t index = low_dev_size - addr;
402+
403+
rc = flash_read(
404+
npcx_espi_taf_data.high_dev_ptr, 0x0,
405+
&npcx_espi_taf_data.read_buf[index],
406+
(addr + len - npcx_espi_taf_data.low_dev_size.low_dev_size));
407+
}
408+
#else
365409
rc = flash_read(spi_dev, addr, npcx_espi_taf_data.read_buf, len);
410+
#endif
366411
if (rc) {
367412
LOG_ERR("flash read fail 0x%x", rc);
368413
return -EIO;
@@ -394,6 +439,8 @@ static int espi_taf_npcx_flash_write(const struct device *dev, struct espi_saf_p
394439
{
395440
struct espi_taf_npcx_pckt *taf_data_ptr = (struct espi_taf_npcx_pckt *)pckt->buf;
396441
uint8_t *data_ptr = (uint8_t *)(taf_data_ptr->data);
442+
uint32_t addr = pckt->flash_addr;
443+
uint32_t len = pckt->len;
397444
int rc;
398445

399446
if (espi_taf_check_write_protect(dev, pckt->flash_addr,
@@ -402,7 +449,19 @@ static int espi_taf_npcx_flash_write(const struct device *dev, struct espi_saf_p
402449
return -EINVAL;
403450
}
404451

405-
rc = flash_write(spi_dev, pckt->flash_addr, data_ptr, pckt->len);
452+
#if DT_NODE_HAS_STATUS_OKAY(NPCX_TAF_SEC_FLASH_NODE)
453+
if ((addr + len) <= npcx_espi_taf_data.low_dev_size) {
454+
rc = flash_write(npcx_espi_taf_data.low_dev_ptr, addr, data_ptr, len);
455+
} else if (addr >= npcx_espi_taf_data.low_dev_size) {
456+
rc = flash_write(npcx_espi_taf_data.high_dev_ptr,
457+
(addr - npcx_espi_taf_data.low_dev_size), data_ptr, len);
458+
} else {
459+
LOG_ERR("Write across two flashes");
460+
return -EINVAL;
461+
}
462+
#else
463+
rc = flash_write(spi_dev, addr, data_ptr, len);
464+
#endif
406465
if (rc) {
407466
LOG_ERR("flash write fail 0x%x", rc);
408467
return -EIO;
@@ -438,7 +497,19 @@ static int espi_taf_npcx_flash_erase(const struct device *dev, struct espi_saf_p
438497
return -EINVAL;
439498
}
440499

500+
#if DT_NODE_HAS_STATUS_OKAY(NPCX_TAF_SEC_FLASH_NODE)
501+
if ((addr + len) <= npcx_espi_taf_data.low_dev_size) {
502+
rc = flash_erase(npcx_espi_taf_data.low_dev_ptr, addr, len);
503+
} else if (addr >= npcx_espi_taf_data.low_dev_size) {
504+
rc = flash_erase(npcx_espi_taf_data.high_dev_ptr,
505+
(addr - npcx_espi_taf_data.low_dev_size), len);
506+
} else {
507+
LOG_ERR("Erase across two flashes");
508+
return -EINVAL;
509+
}
510+
#else
441511
rc = flash_erase(spi_dev, addr, len);
512+
#endif
442513
if (rc) {
443514
LOG_ERR("flash erase fail");
444515
return -EIO;
@@ -620,6 +691,18 @@ static int espi_taf_npcx_init(const struct device *dev)
620691
config->max_rd_sz);
621692
inst->FLASHBASE = config->mapped_addr;
622693

694+
#if DT_NODE_HAS_STATUS_OKAY(NPCX_TAF_SEC_FLASH_NODE)
695+
if (IS_ENABLED(CONFIG_FLASH_NPCX_FIU_SUPP_LOW_DEV_SWAP)) {
696+
npcx_espi_taf_data.low_dev_ptr = spi_dev1;
697+
npcx_espi_taf_data.high_dev_ptr = spi_dev;
698+
npcx_espi_taf_data.low_dev_size = NPCX_TAF_ALLOC_SIZE(NPCX_TAF_SEC_FLASH_NODE);
699+
} else {
700+
npcx_espi_taf_data.low_dev_ptr = spi_dev;
701+
npcx_espi_taf_data.high_dev_ptr = spi_dev1;
702+
npcx_espi_taf_data.low_dev_size = NPCX_TAF_ALLOC_SIZE(NPCX_TAF_PRIME_FLASH_NODE);
703+
}
704+
#endif
705+
623706
#ifdef CONFIG_ESPI_TAF_NPCX_RPMC_SUPPORT
624707
uint8_t count_num = 0;
625708

0 commit comments

Comments
 (0)