/*
 * Copyright (c) 2020 Bouffalolab.
 *
 * This file is part of
 *     *** Bouffalolab Software Dev Kit ***
 *      (see www.bouffalolab.com).
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of Bouffalo Lab nor the names of its contributors
 *      may be used to endorse or promote products derived from this software
 *      without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include <bl702_romdriver.h>

#include "bl_flash.h"
#include <blog.h>
#define USER_UNUSED(a) ((void)(a))

static struct {
    uint32_t magic;
    SPI_Flash_Cfg_Type flashCfg;
} boot2_flashCfg; //XXX Dont change the name of varaible, since we refer this boot2_partition_table in linker script

int bl_flash_erase(uint32_t addr, int len)
{
    /*We assume mid zero is illegal*/
    if (0 == boot2_flashCfg.flashCfg.mid) {
        return -1;
    }

    __disable_irq();
    bl_flash_erase_need_lock(addr, len);
    __enable_irq();
    return 0;
}

int bl_flash_write(uint32_t addr, uint8_t *src, int len)
{
    /*We assume mid zero is illegal*/
    if (0 == boot2_flashCfg.flashCfg.mid) {
        return -1;
    }

    __disable_irq();
    bl_flash_write_need_lock(addr, src, len);
    __enable_irq();
    return 0;
}

int bl_flash_read(uint32_t addr, uint8_t *dst, int len)
{
    /*We assume mid zero is illegal*/
    if (0 == boot2_flashCfg.flashCfg.mid) {
        return -1;
    }

    __disable_irq();
    bl_flash_read_need_lock(addr, dst, len);
    __enable_irq();
    return 0;
}

int ATTR_TCM_SECTION bl_flash_erase_need_lock(uint32_t addr, int len)
{
    /*We assume mid zero is illegal*/
    if (0 == boot2_flashCfg.flashCfg.mid) {
        return -1;
    }

    XIP_SFlash_Opt_Enter();
    RomDriver_XIP_SFlash_Erase_Need_Lock(
            &boot2_flashCfg.flashCfg,
            boot2_flashCfg.flashCfg.ioMode&0xf,
            addr,
            addr + len - 1
    );
    XIP_SFlash_Opt_Exit();
    return 0;
}

int ATTR_TCM_SECTION bl_flash_write_need_lock(uint32_t addr, uint8_t *src, int len)
{
    /*We assume mid zero is illegal*/
    if (0 == boot2_flashCfg.flashCfg.mid) {
        return -1;
    }

    XIP_SFlash_Opt_Enter();
    RomDriver_XIP_SFlash_Write_Need_Lock(
            &boot2_flashCfg.flashCfg,
            boot2_flashCfg.flashCfg.ioMode&0xf,
            addr,
            src,
            len
    );
    XIP_SFlash_Opt_Exit();
    return 0;
}

int ATTR_TCM_SECTION bl_flash_read_need_lock(uint32_t addr, uint8_t *dst, int len)
{
    /*We assume mid zero is illegal*/
    if (0 == boot2_flashCfg.flashCfg.mid) {
        return -1;
    }

    XIP_SFlash_Opt_Enter();
    RomDriver_XIP_SFlash_Read_Need_Lock(
            &boot2_flashCfg.flashCfg,
            boot2_flashCfg.flashCfg.ioMode&0xf,
            addr,
            dst,
            len
    );
    XIP_SFlash_Opt_Exit();
    return 0;
}

static void _dump_flash_config()
{
    blog_info("======= FlashCfg magiccode @%p, code 0x%08lX =======\r\n",
            &boot2_flashCfg.magic,
            boot2_flashCfg.magic
    );
    blog_info("mid \t\t0x%X\r\n", boot2_flashCfg.flashCfg.mid);
    blog_info("clkDelay \t0x%X\r\n", boot2_flashCfg.flashCfg.clkDelay);
    blog_info("clkInvert \t0x%X\r\n", boot2_flashCfg.flashCfg.clkInvert);
    blog_info("sector size\t%uKBytes\r\n", boot2_flashCfg.flashCfg.sectorSize);
    blog_info("page size\t%uBytes\r\n", boot2_flashCfg.flashCfg.pageSize);
    blog_info("---------------------------------------------------------------\r\n");
}

int bl_flash_config_update(void)
{
    _dump_flash_config();

    return 0;
}

void* bl_flash_get_flashCfg(void)
{
    return &boot2_flashCfg.flashCfg;
}

int bl_flash_read_byxip(uint32_t addr, uint8_t *dst, int len)
{
    uint32_t offset;
    uint32_t xipaddr;

    offset = RomDriver_SF_Ctrl_Get_Flash_Image_Offset();

    if ((addr < offset) || (addr >= 0x1000000)) {
        // not support or arg err ?
        return -1;
    }

    xipaddr =  0x23000000 - offset + addr;

    memcpy(dst, (void *)xipaddr, len);

    return 0;
}

