summaryrefslogtreecommitdiff
path: root/common/cbi.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/cbi.c')
-rw-r--r--common/cbi.c31
1 files changed, 19 insertions, 12 deletions
diff --git a/common/cbi.c b/common/cbi.c
index 41e8606487..bc21d901fa 100644
--- a/common/cbi.c
+++ b/common/cbi.c
@@ -17,12 +17,9 @@
#define CPRINTS(format, args...) cprints(CC_SYSTEM, "CBI " format, ## args)
#define EEPROM_PAGE_WRITE_SIZE 16
+#define EC_ERROR_CBI_CACHE_INVALID EC_ERROR_INTERNAL_FIRST
static struct board_info bi;
-/* TODO: Init it to -1. On error (I2C or bad contents), retry a read and set it
- * to enum ec_error_list if it still fails. The successive calls can be
- * immediately returned with the cached error code. This will avoid attempting
- * reads doomed to fail. */
-static int initialized;
+static int cached_read_result = EC_ERROR_CBI_CACHE_INVALID;
static uint8_t cbi_crc8(const struct board_info *bi)
{
@@ -32,14 +29,11 @@ static uint8_t cbi_crc8(const struct board_info *bi)
/*
* Get board information from EEPROM
*/
-static int read_board_info(void)
+static int do_read_board_info(void)
{
uint8_t buf[256];
uint8_t offset;
- if (initialized)
- return EC_SUCCESS;
-
CPRINTS("Reading board info");
/* Read header */
@@ -92,11 +86,24 @@ static int read_board_info(void)
sizeof(bi) - sizeof(bi.head));
/* If we're handling previous version, clear all new fields */
- initialized = 1;
-
return EC_SUCCESS;
}
+static int read_board_info(void)
+{
+ if (cached_read_result != EC_ERROR_CBI_CACHE_INVALID)
+ /* We already tried and know the result. Return the cached
+ * error code immediately to avoid wasteful reads. */
+ return cached_read_result;
+
+ cached_read_result = do_read_board_info();
+ if (cached_read_result)
+ /* On error (I2C or bad contents), retry a read */
+ cached_read_result = do_read_board_info();
+
+ return cached_read_result;
+}
+
static int eeprom_is_write_protected(void)
{
return !gpio_get_level(GPIO_WP_L);
@@ -196,7 +203,7 @@ static int hc_cbi_set(struct host_cmd_handler_args *args)
if (p->flag & CBI_SET_INIT) {
memset(&bi, 0, sizeof(bi));
memcpy(&bi.head.magic, cbi_magic, sizeof(cbi_magic));
- initialized = 1;
+ cached_read_result = EC_SUCCESS;
} else {
if (read_board_info())
return EC_RES_ERROR;