summaryrefslogtreecommitdiff
path: root/chip
diff options
context:
space:
mode:
authorVic Yang <victoryang@chromium.org>2013-05-26 18:46:55 +0800
committerChromeBot <chrome-bot@google.com>2013-06-03 14:34:10 -0700
commit80105a95569c36b7a4d372c7e74f086ad27f5492 (patch)
tree368706c3d44566cb696ebf98018d1dfb7bcd7f80 /chip
parent1806f521955623770405778a742d0398cae028a2 (diff)
downloadchrome-ec-80105a95569c36b7a4d372c7e74f086ad27f5492.tar.gz
Enable flash unit test on emulator
BUG=chrome-os-partner:19236 TEST=Pass all tests BRANCH=None Change-Id: I09276292499b94b2d4810830de51e4c63a9b7342 Signed-off-by: Vic Yang <victoryang@chromium.org> Reviewed-on: https://gerrit.chromium.org/gerrit/56704 Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
Diffstat (limited to 'chip')
-rw-r--r--chip/host/build.mk2
-rw-r--r--chip/host/config.h11
-rw-r--r--chip/host/flash.c135
-rw-r--r--chip/host/registers.h11
-rw-r--r--chip/lm4/config.h1
-rw-r--r--chip/stm32/config.h1
6 files changed, 157 insertions, 4 deletions
diff --git a/chip/host/build.mk b/chip/host/build.mk
index 110daefb8b..866c84da20 100644
--- a/chip/host/build.mk
+++ b/chip/host/build.mk
@@ -8,5 +8,5 @@
CORE:=host
-chip-y=system.o gpio.o uart.o persistence.o
+chip-y=system.o gpio.o uart.o persistence.o flash.o
chip-$(HAS_TASK_KEYSCAN)+=keyboard_raw.o
diff --git a/chip/host/config.h b/chip/host/config.h
index c296e1b7cc..9825f086e9 100644
--- a/chip/host/config.h
+++ b/chip/host/config.h
@@ -9,14 +9,16 @@
#define __CONFIG_H
/* Memory mapping */
-#define CONFIG_FLASH_BASE 0x08000000
#define CONFIG_FLASH_PHYSICAL_SIZE 0x00020000
#define CONFIG_FLASH_SIZE CONFIG_FLASH_PHYSICAL_SIZE
+extern char __host_flash[CONFIG_FLASH_PHYSICAL_SIZE];
+
+#define CONFIG_FLASH_BASE ((uintptr_t)__host_flash)
#define CONFIG_FLASH_BANK_SIZE 0x1000
#define CONFIG_FLASH_ERASE_SIZE 0x0400 /* erase bank size */
#define CONFIG_FLASH_WRITE_SIZE 0x0002 /* minimum write size */
-#define CONFIG_RAM_BASE 0x20000000
-#define CONFIG_RAM_SIZE 0x00002000
+#define CONFIG_RAM_BASE 0x0 /* Not supported */
+#define CONFIG_RAM_SIZE 0x0 /* Not supported */
/* Size of one firmware image in flash */
#define CONFIG_FW_IMAGE_SIZE (64 * 1024)
@@ -28,6 +30,9 @@
#define CONFIG_FW_WP_RO_OFF CONFIG_FW_RO_OFF
#define CONFIG_FW_WP_RO_SIZE CONFIG_FW_IMAGE_SIZE
+/* Features */
+#define CONFIG_FLASH
+
/*
* Put this after RO to give RW more space and make RO write protect region
* contiguous.
diff --git a/chip/host/flash.c b/chip/host/flash.c
new file mode 100644
index 0000000000..24023c5155
--- /dev/null
+++ b/chip/host/flash.c
@@ -0,0 +1,135 @@
+/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* Flash module for emulator */
+
+#include <stdio.h>
+
+#include "config.h"
+#include "flash.h"
+#include "persistence.h"
+#include "util.h"
+
+char __host_flash[CONFIG_FLASH_PHYSICAL_SIZE];
+uint8_t __host_flash_protect[PHYSICAL_BANKS];
+
+static int flash_check_protect(int offset, int size)
+{
+ int first_bank = offset / CONFIG_FLASH_BANK_SIZE;
+ int last_bank = DIV_ROUND_UP(offset + size + 1,
+ CONFIG_FLASH_BANK_SIZE);
+ int bank;
+
+ for (bank = first_bank; bank <= last_bank; ++bank)
+ if (__host_flash_protect[bank])
+ return 1;
+ return 0;
+}
+
+static void flash_set_persistent(void)
+{
+ FILE *f = get_persistent_storage("flash", "wb");
+ int sz;
+
+ ASSERT(f != NULL);
+
+ sz = fwrite(__host_flash, sizeof(__host_flash), 1, f);
+ ASSERT(sz == 1);
+
+ release_persistent_storage(f);
+}
+
+static void flash_get_persistent(void)
+{
+ FILE *f = get_persistent_storage("flash", "rb");
+
+ if (f == NULL) {
+ fprintf(stderr,
+ "No flash storage found. Initializing to 0xff.\n");
+ memset(__host_flash, 0xff, sizeof(__host_flash));
+ return;
+ }
+
+ fread(__host_flash, sizeof(__host_flash), 1, f);
+
+ release_persistent_storage(f);
+}
+
+int flash_physical_write(int offset, int size, const char *data)
+{
+ ASSERT((size & (CONFIG_FLASH_WRITE_SIZE - 1)) == 0);
+
+ if (flash_check_protect(offset, size))
+ return EC_ERROR_ACCESS_DENIED;
+
+ memcpy(__host_flash + offset, data, size);
+ flash_set_persistent();
+
+ return EC_SUCCESS;
+}
+
+int flash_physical_erase(int offset, int size)
+{
+ ASSERT((size & (CONFIG_FLASH_ERASE_SIZE - 1)) == 0);
+
+ if (flash_check_protect(offset, size))
+ return EC_ERROR_ACCESS_DENIED;
+
+ memset(__host_flash + offset, 0xff, size);
+ flash_set_persistent();
+
+ return EC_SUCCESS;
+}
+
+int flash_physical_get_protect(int bank)
+{
+ return __host_flash_protect[bank];
+}
+
+uint32_t flash_physical_get_protect_flags(void)
+{
+ int i;
+ uint32_t flags = EC_FLASH_PROTECT_ALL_NOW;
+
+ for (i = 0; i < PHYSICAL_BANKS; ++i)
+ if (__host_flash_protect[i] == 0)
+ flags = 0;
+
+ return flags;
+}
+
+int flash_physical_protect_now(int all)
+{
+ memset(__host_flash_protect, 1, all ? PHYSICAL_BANKS : RO_BANK_COUNT);
+ return EC_SUCCESS;
+}
+
+int flash_pre_init(void)
+{
+ uint32_t prot_flags;
+
+ flash_get_persistent();
+
+ prot_flags = flash_get_protect();
+
+ if (prot_flags & EC_FLASH_PROTECT_GPIO_ASSERTED) {
+ /*
+ * Write protect is asserted. If we want RO flash protected,
+ * protect it now.
+ */
+ if ((prot_flags & EC_FLASH_PROTECT_RO_AT_BOOT) &&
+ !(prot_flags & EC_FLASH_PROTECT_RO_NOW)) {
+ int rv = flash_set_protect(EC_FLASH_PROTECT_RO_NOW,
+ EC_FLASH_PROTECT_RO_NOW);
+ if (rv)
+ return rv;
+
+ /* Re-read flags */
+ prot_flags = flash_get_protect();
+ }
+ }
+
+ return EC_SUCCESS;
+}
diff --git a/chip/host/registers.h b/chip/host/registers.h
new file mode 100644
index 0000000000..0ee68b8e23
--- /dev/null
+++ b/chip/host/registers.h
@@ -0,0 +1,11 @@
+/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/* Empty registers header for emulator */
+
+/*
+ * There is no register for emulator, but this file exists to prevent
+ * compilation failure if any file includes registers.h
+ */
diff --git a/chip/lm4/config.h b/chip/lm4/config.h
index 609a88ae90..6be1085c02 100644
--- a/chip/lm4/config.h
+++ b/chip/lm4/config.h
@@ -101,6 +101,7 @@
/* Optional features present on this chip */
#define CONFIG_ADC
#define CONFIG_FLASH
+#define CONFIG_FMAP
#define CONFIG_FPU
#define CONFIG_I2C
#define CONFIG_WATCHDOG
diff --git a/chip/stm32/config.h b/chip/stm32/config.h
index d4107b1d36..6eb8a48b51 100644
--- a/chip/stm32/config.h
+++ b/chip/stm32/config.h
@@ -39,6 +39,7 @@
/* support programming on-chip flash */
#define CONFIG_FLASH
+#define CONFIG_FMAP
/* build with assertions and debug messages */
#define CONFIG_DEBUG