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; }
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; }
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_write(int offset, int size, const char *data) { int dest_addr = offset; const int sz_page = CONFIG_FLASH_WRITE_IDEAL_SIZE; /* Fail if offset, size, and data aren't at least word-aligned */ if ((offset | size | (uint32_t)(uintptr_t)data) & (CONFIG_FLASH_WRITE_SIZE - 1)) return EC_ERROR_INVAL; /* check protection */ if (all_protected) return EC_ERROR_ACCESS_DENIED; /* Disable tri-state */ TRISTATE_FLASH(0); /* Write the data per CONFIG_FLASH_WRITE_IDEAL_SIZE bytes */ for (; size >= sz_page; size -= sz_page) { /* check protection */ if (flash_check_prot_range(dest_addr, sz_page)) return EC_ERROR_ACCESS_DENIED; /* Enable write */ flash_write_enable(); /* Burst UMA transaction */ flash_burst_write(dest_addr, sz_page, data); /* Wait write completed */ flash_wait_ready(); data += sz_page; dest_addr += sz_page; } /* Handle final partial page, if any */ if (size != 0) { /* check protection */ if (flash_check_prot_range(dest_addr, size)) return EC_ERROR_ACCESS_DENIED; /* Enable write */ flash_write_enable(); /* Burst UMA transaction */ flash_burst_write(dest_addr, size, data); /* Wait write completed */ flash_wait_ready(); } /* Enable tri-state */ TRISTATE_FLASH(1); return EC_SUCCESS; }
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; }
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_write(int offset, int size, const char *data) { int dest_addr = offset; int write_len; int rv; /* Fail if offset, size, and data aren't at least word-aligned */ if ((offset | size | (uint32_t)(uintptr_t)data) & (CONFIG_FLASH_WRITE_SIZE - 1)) return EC_ERROR_INVAL; /* 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); while (size > 0) { /* First write multiples of 256, then (size % 256) last */ write_len = ((size % CONFIG_FLASH_WRITE_IDEAL_SIZE) == size) ? size : CONFIG_FLASH_WRITE_IDEAL_SIZE; /* check protection */ if (flash_check_prot_range(dest_addr, write_len)) { rv = EC_ERROR_ACCESS_DENIED; break; } rv = flash_program_bytes(dest_addr, write_len, data); if (rv) break; data += write_len; dest_addr += write_len; size -= write_len; } /* Enable tri-state */ TRISTATE_FLASH(1); /* Unlock physical flash operations */ flash_lock_mapped_storage(0); return rv; }
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; }
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; }