static int flash_wait_ready(int timeout) { uint8_t mask = SPI_FLASH_SR1_BUSY; if (timeout <= 0) return EC_ERROR_INVAL; /* Chip Select down. */ flash_cs_level(0); /* Command for Read status register */ flash_execute_cmd(CMD_READ_STATUS_REG, MASK_CMD_ONLY); while (timeout > 0) { /* Read status register */ NPCX_UMA_CTS = MASK_RD_1BYTE; while (IS_BIT_SET(NPCX_UMA_CTS, NPCX_UMA_CTS_EXEC_DONE)) ; /* Busy bit is clear */ if ((NPCX_UMA_DB0 & mask) == 0) break; if (--timeout > 0) msleep(1); }; /* Wait for Busy clear */ /* Chip Select high. */ flash_cs_level(1); if (timeout == 0) return EC_ERROR_TIMEOUT; return EC_SUCCESS; }
int flash_physical_is_erased(uint32_t offset, int size) { int dest_addr = offset; uint32_t idx; uint8_t temp; /* Chip Select down. */ flash_cs_level(0); /* Set read address */ flash_set_address(dest_addr); /* Start fast read -1110 1001 - EXEC, WR, CMD, ADDR */ flash_execute_cmd(CMD_FAST_READ, MASK_CMD_ADR_WR); /* Burst read transaction */ for (idx = 0; idx < size; idx++) { /* 1101 0101 - EXEC, RD, NO CMD, NO ADDR, 4 bytes */ NPCX_UMA_CTS = MASK_RD_1BYTE; /* Wait for UMA to complete */ while (IS_BIT_SET(NPCX_UMA_CTS, EXEC_DONE)) ; /* Get read transaction results */ temp = NPCX_UMA_DB0; if (temp != 0xFF) break; } /* Chip Select up */ flash_cs_level(1); if (idx == size) return 1; else return 0; }
int flash_physical_read(int offset, int size, char *data) { int dest_addr = offset; uint32_t idx; /* Disable tri-state */ TRISTATE_FLASH(0); /* Chip Select down. */ flash_cs_level(0); /* Set read address */ flash_set_address(dest_addr); /* Start fast read - 1110 1001 - EXEC, WR, CMD, ADDR */ flash_execute_cmd(CMD_FAST_READ, MASK_CMD_ADR_WR); /* Burst read transaction */ for (idx = 0; idx < size; idx++) { /* 1101 0101 - EXEC, RD, NO CMD, NO ADDR, 4 bytes */ NPCX_UMA_CTS = MASK_RD_1BYTE; /* wait for UMA to complete */ while (IS_BIT_SET(NPCX_UMA_CTS, EXEC_DONE)) ; /* Get read transaction results*/ data[idx] = NPCX_UMA_DB0; } /* Chip Select up */ flash_cs_level(1); /* Enable tri-state */ TRISTATE_FLASH(1); return EC_SUCCESS; }
static int flash_set_status_for_prot(int reg1, int reg2) { /* Lock physical flash operations */ flash_lock_mapped_storage(1); /* Disable tri-state */ TRISTATE_FLASH(0); /* Enable write */ flash_write_enable(); NPCX_UMA_DB0 = reg1; NPCX_UMA_DB1 = reg2; /* Write status register 1/2 */ flash_execute_cmd(CMD_WRITE_STATUS_REG, MASK_CMD_WR_2BYTE); /* Enable tri-state */ TRISTATE_FLASH(1); /* Unlock physical flash operations */ flash_lock_mapped_storage(0); reg_to_protect(reg1, reg2, &addr_prot_start, &addr_prot_length); return EC_SUCCESS; }
void flash_burst_write(unsigned int dest_addr, unsigned int bytes, const char *data) { unsigned int i; /* Chip Select down. */ flash_cs_level(0); /* Set erase address */ flash_set_address(dest_addr); /* Start write */ flash_execute_cmd(CMD_FLASH_PROGRAM, MASK_CMD_WR_ADR); for (i = 0; i < bytes; i++) { flash_execute_cmd(*data, MASK_CMD_WR_ONLY); data++; } /* Chip Select up */ flash_cs_level(1); }
uint8_t flash_get_status2(void) { /* Disable tri-state */ TRISTATE_FLASH(0); /* Read status register2 */ flash_execute_cmd(CMD_READ_STATUS_REG2, MASK_CMD_RD_1BYTE); /* Enable tri-state */ TRISTATE_FLASH(1); return NPCX_UMA_DB0; }
int flash_physical_erase(int offset, int size) { int rv = EC_SUCCESS; /* check protection */ if (all_protected) return EC_ERROR_ACCESS_DENIED; /* Lock physical flash operations */ flash_lock_mapped_storage(1); /* Disable tri-state */ TRISTATE_FLASH(0); /* Alignment has been checked in upper layer */ for (; size > 0; size -= CONFIG_FLASH_ERASE_SIZE, offset += CONFIG_FLASH_ERASE_SIZE) { /* check protection */ if (flash_check_prot_range(offset, CONFIG_FLASH_ERASE_SIZE)) { rv = EC_ERROR_ACCESS_DENIED; break; } /* * Reload the watchdog timer, so that erasing many flash pages * doesn't cause a watchdog reset. May not need this now that * we're using msleep() below. */ watchdog_reload(); /* Enable write */ rv = flash_write_enable(); if (rv) break; /* Set erase address */ flash_set_address(offset); /* Start erase */ flash_execute_cmd(CMD_SECTOR_ERASE, MASK_CMD_ADR); /* Wait erase completed */ rv = flash_wait_ready(FLASH_ABORT_TIMEOUT); if (rv) break; } /* Enable tri-state */ TRISTATE_FLASH(1); /* Unlock physical flash operations */ flash_lock_mapped_storage(0); return rv; }
int flash_write_enable(void) { uint8_t mask = SPI_FLASH_SR1_WEL; /* Write enable command */ flash_execute_cmd(CMD_WRITE_EN, MASK_CMD_ONLY); /* Wait for flash is not busy */ flash_wait_ready(); if (NPCX_UMA_DB0 & mask) return 1; else return 0; }
void flash_get_jedec_id(uint8_t *dest) { /* Disable tri-state */ TRISTATE_FLASH(0); /* Read manufacturer and device ID */ flash_execute_cmd(CMD_READ_ID, MASK_CMD_RD_3BYTE); /* Enable tri-state */ TRISTATE_FLASH(1); dest[0] = NPCX_UMA_DB0; dest[1] = NPCX_UMA_DB1; dest[2] = NPCX_UMA_DB2; }
void flash_get_mfr_dev_id(uint8_t *dest) { /* Disable tri-state */ TRISTATE_FLASH(0); /* Read manufacturer and device ID. Send cmd=0x90 + 24-bit address=0 */ flash_set_address(0); flash_execute_cmd(CMD_READ_MAN_DEV_ID, MASK_CMD_RD_2BYTE | MASK(A_SIZE)); /* Enable tri-state */ TRISTATE_FLASH(1); dest[0] = NPCX_UMA_DB0; dest[1] = NPCX_UMA_DB1; }
int flash_physical_erase(int offset, int size) { /* check protection */ if (all_protected) return EC_ERROR_ACCESS_DENIED; /* Disable tri-state */ TRISTATE_FLASH(0); /* Alignment has been checked in upper layer */ for (; size > 0; size -= CONFIG_FLASH_ERASE_SIZE, offset += CONFIG_FLASH_ERASE_SIZE) { /* Do nothing if already erased */ if (flash_is_erased(offset, CONFIG_FLASH_ERASE_SIZE)) continue; /* check protection */ if (flash_check_prot_range(offset, CONFIG_FLASH_ERASE_SIZE)) return EC_ERROR_ACCESS_DENIED; /* * Reload the watchdog timer, so that erasing many flash pages * doesn't cause a watchdog reset. May not need this now that * we're using msleep() below. */ watchdog_reload(); /* Enable write */ flash_write_enable(); /* Set erase address */ flash_set_address(offset); /* Start erase */ flash_execute_cmd(CMD_SECTOR_ERASE, MASK_CMD_ADR); /* Wait erase completed */ flash_wait_ready(); } /* Enable tri-state */ TRISTATE_FLASH(1); return EC_SUCCESS; }
static int flash_write_enable(void) { uint8_t mask = SPI_FLASH_SR1_WEL; int rv; /* Wait for previous operation to complete */ rv = flash_wait_ready(FLASH_ABORT_TIMEOUT); if (rv) return rv; /* Write enable command */ flash_execute_cmd(CMD_WRITE_EN, MASK_CMD_ONLY); /* Wait for flash is not busy */ rv = flash_wait_ready(FLASH_ABORT_TIMEOUT); if (rv) return rv; if (NPCX_UMA_DB0 & mask) return EC_SUCCESS; else return EC_ERROR_BUSY; }
void flash_wait_ready(void) { uint8_t mask = SPI_FLASH_SR1_BUSY; uint16_t timeout = FLASH_ABORT_TIMEOUT; /* Chip Select down. */ flash_cs_level(0); /* Command for Read status register */ flash_execute_cmd(CMD_READ_STATUS_REG, MASK_CMD_ONLY); while (--timeout) { /* Read status register */ NPCX_UMA_CTS = MASK_RD_1BYTE; while (IS_BIT_SET(NPCX_UMA_CTS, NPCX_UMA_CTS_EXEC_DONE)) ; /* Busy bit is clear */ if ((NPCX_UMA_DB0 & mask) == 0) break; msleep(1); }; /* Wait for Busy clear */ /* Chip Select high. */ flash_cs_level(1); }
int flash_physical_read_image_size(int offset, int size) { int dest_addr = offset; uint8_t temp; uint32_t idx; uint32_t image_size = 0; /* Disable tri-state */ TRISTATE_FLASH(0); /* Chip Select down. */ flash_cs_level(0); /* Set read address */ flash_set_address(dest_addr); /* Start fast read - 1110 1001 - EXEC, WR, CMD, ADDR */ flash_execute_cmd(CMD_FAST_READ, MASK_CMD_ADR_WR); /* Burst read transaction */ for (idx = 0; idx < size; idx++) { /* 1101 0101 - EXEC, RD, NO CMD, NO ADDR, 4 bytes */ NPCX_UMA_CTS = MASK_RD_1BYTE; /* wait for UMA to complete */ while (IS_BIT_SET(NPCX_UMA_CTS, EXEC_DONE)) ; /* Find eof of image */ temp = NPCX_UMA_DB0; if (temp == 0xea) image_size = idx; } /* Chip Select up */ flash_cs_level(1); /* Enable tri-state */ TRISTATE_FLASH(1); return image_size; }