+static flash_addr_t flash_region_start;
+static flash_addr_t flash_region_end;
+static int flash_enable;
+
+/* This assumes reading the flash with monotonically increasing flash addresses */
+static flash_addr_t nfc_l_to_p(flash_addr_t addr)
+{
+ if (g_block_offset == 0) {
+ return addr;
+ } else {
+ flash_addr_t ra;
+ u32 block = (addr & MXC_NAND_ADDR_MASK) / NF_BLK_SZ;
+ u32 offset = addr % NF_BLK_SZ;
+
+ ra = (block + g_block_offset) * NF_BLK_SZ + offset;
+ if (offset == 0) {
+ nfc_printf(NFC_DEBUG_MIN,
+ "Remapping block %u at addr 0x%08llx to block %u at addr 0x%08llx\n",
+ block, (u64)addr, block + g_block_offset, (u64)ra);
+ }
+ return ra;
+ }
+}
+
+static int flash_addr_valid(flash_addr_t addr)
+{
+ if (addr < flash_region_start || addr >= flash_region_end) {
+ diag_printf("Flash address 0x%08llx is outside valid region 0x%08llx..0x%08llx\n",
+ (u64)addr, (u64)flash_region_start, (u64)flash_region_end);
+ }
+ return addr >= flash_region_start && addr < flash_region_end;
+}
+
+/* FIXME: we should pass flash_addr_t as arguments */
+void mxc_flash_enable(void *start, void *end)
+{
+ flash_addr_t s = (unsigned long)start & MXC_NAND_ADDR_MASK;
+ flash_addr_t e = (unsigned long)end & MXC_NAND_ADDR_MASK;
+
+ if (flash_enable++ == 0) {
+ flash_region_start = s;
+ flash_region_end = e;
+ diag_printf1("Enabling flash region 0x%08llx..0x%08llx\n",
+ (u64)s, (u64)e);
+ g_block_offset = 0;
+ } else {
+ if (s < flash_region_start ||
+ e > flash_region_end) {
+ diag_printf("** WARNING: Enable 0x%08llx..0x%08llx outside enabled flash region 0x%08llx..0x%08llx\n",
+ (u64)s, (u64)e, (u64)flash_region_start, (u64)flash_region_end);
+ }
+ }
+}
+
+void mxc_flash_disable(void *start, void *end)
+{
+ flash_addr_t s = (unsigned long)start & MXC_NAND_ADDR_MASK;
+ flash_addr_t e = (unsigned long)end & MXC_NAND_ADDR_MASK;
+
+ if (flash_enable) {
+ if (--flash_enable == 0) {
+ diag_printf1("Disabling flash region 0x%08llx..0x%08llx\n",
+ (u64)s, (u64)e);
+ if (s != flash_region_start ||
+ e != flash_region_end) {
+ diag_printf("** Error: Disable 0x%08llx..0x%08llx not equal to enabled flash region 0x%08llx..0x%08llx\n",
+ (u64)s, (u64)e, (u64)flash_region_start, (u64)flash_region_end);
+ }
+ }
+ } else {
+ diag_printf("** Error: unbalanced call to flash_disable()\n");
+ }
+}
+