/* SynaEnableFlashing kicks off the reflash process
 */
void SynaEnableFlashing(struct i2c_client *client)
{
	unsigned char uStatus = 0;
	enum FlashCommand cmd;
	TPD_LOG("%s\n", __func__);
	TPD_LOG("Enable Reflash...\n");

	readRMI(client, SynaF01DataBase, &uStatus, 1);

/* APK_TEST */
	TPD_LOG("APK_TEST uStatus= 0x%02x\n", uStatus);

	if ((uStatus & 0x40) == 0 /*|| force */) {
		/* Reflash is enabled by first reading the bootloader ID from the firmware and write it back */
		SynaReadBootloadID(client);
		SynaWriteBootloadID(client);

		/* Write the "Enable Flash Programming command to F34 Control register */
		/* Wait for ATTN and then clear the ATTN. */
		cmd = m_uF34ReflashCmd_Enable;
		writeRMI(client, SynaF34_FlashControl, (unsigned char *)&cmd, 1);
		SynaWaitForATTN(1000, client);

		/* I2C addrss may change */
		/* ConfigCommunication();//APK_TEST */

		/* Scan the PDT again to ensure all register offsets are correct */
		SynaScanPDT(client);

		/* Read the "Program Enabled" bit of the F34 Control register, and proceed only if the */
		/* bit is set. */
		CheckFlashStatus(cmd, client);
	}
}
/* SynaEnableFlashing kicks off the reflash process
 */
static int SynaEnableFlashing(void)
{
    unsigned char uData;
    unsigned char uStatus;
    int retry = 3;

    printk("\nEnable Reflash...\n");

    // Reflash is enabled by first reading the bootloader ID from the firmware and write it back
    SynaReadBootloadID();
    SynaWriteBootloadID();

    // Make sure Reflash is not already enabled
    do {
        readRMI(SynaF34_FlashControl, &uData, 1);
        printk("----Read reflash enable ---uData=0x%x--\n",uData);
    } while (uData  ==  0x0f);//while (((uData & 0x0f) != 0x00));

    // Clear ATTN
    readRMI (SynaF01DataBase, &uStatus, 1);
    printk("----Read status ---uStatus=0x%x--\n",uStatus);
    if ((uStatus &0x40) == 0) {
        // Write the "Enable Flash Programming command to F34 Control register
        // Wait for ATTN and then clear the ATTN.
        //uData = 0x0f;    //lemon
        readRMI(SynaF34_FlashControl, &uData, 1);
        uData = uData | 0x0f;  
        writeRMI(SynaF34_FlashControl, &uData, 1);
        SynaWaitForATTN();
        readRMI((SynaF01DataBase + 1), &uStatus, 1);

        // Scan the PDT again to ensure all register offsets are correct
        SynaSetup();

        // Read the "Program Enabled" bit of the F34 Control register, and proceed only if the
        // bit is set.
        readRMI(SynaF34_FlashControl, &uData, 1);
        printk("----read--enable ---uData=0x%x--\n",uData);
        while (uData != 0x80) {
            // In practice, if uData!=0x80 happens for multiple counts, it indicates reflash
            // is failed to be enabled, and program should quit
            printk("%s Can NOT enable reflash !!!\n",__func__);

            if (!retry--)
                return -1;
            readRMI(SynaF34_FlashControl, &uData, 1);
            printk("----read--enable ---uData=0x%x--\n",uData);
        }
    }
    return 0;
}
/* SynaProgramFirmware prepares the firmware writing process
*/
static void SynaProgramFirmware(void)
{
	u8 uData;

	pr_info("tsp fw. : Program Firmware Section...");

	SynaReadBootloadID();
	SynaWriteBootloadID();

	uData = 3;
	writeRMI(SynaF34_FlashControl, &uData, 1);

	SynaWaitATTN();
	SynaFlashFirmwareWrite();
}
/* eraseConfigBlock erases the config block */
static void eraseConfigBlock(void)
{
	u8 uData;

	pr_info("tsp fw. : eraseConfigBlock\n");
	/* Erase of config block is done by first entering into
	bootloader mode */
	SynaReadBootloadID();
	SynaWriteBootloadID();

	/* Command 7 to erase config block */
	uData = 7;
	writeRMI(SynaF34_FlashControl, &uData, 1);

	SynaWaitATTN();
}
/* EraseConfigBlock erases the config block
*/
void eraseAllBlock(struct i2c_client *client)
{
	enum FlashCommand cmd;

	TPD_LOG("%s\n", __func__);
	/* Erase of config block is done by first entering into bootloader mode */
	SynaReadBootloadID(client);
	SynaWriteBootloadID(client);

	/* Command 7 to erase config block */
	cmd = m_uF34ReflashCmd_EraseAll;
	writeRMI(client, SynaF34_FlashControl, (unsigned char *)&cmd, 1);

	SynaWaitForATTN(6000, client);
	CheckFlashStatus(cmd, client);
}
/* SynaEnableFlashing kicks off the reflash process
 */
static void SynaEnableFlashing(void)
{
	u8 uData;
	u8 uStatus;

	pr_info("\nEnable Reflash...");

	/* Reflash is enabled by first reading the bootloader ID from
	   the firmware and write it back */
	SynaReadBootloadID();
	SynaWriteBootloadID();

	/* Make sure Reflash is not already enabled */
	do {
		readRMI(SynaF34_FlashControl, &uData, 1);
	} while (((uData & 0x0f) != 0x00));

	readRMI(SynaF01DataBase, &uStatus, 1);

	if ((uStatus & 0x40) == 0) {
		/* Write the "Enable Flash Programming command to
		F34 Control register Wait for ATTN and then clear the ATTN. */
		uData = 0x0f;
		writeRMI(SynaF34_FlashControl, &uData, 1);
		mdelay(300);
		readRMI((SynaF01DataBase + 1), &uStatus, 1);

		/* Scan the PDT again to ensure all register offsets are
		correct */
		SynaSetup();

		/* Read the "Program Enabled" bit of the F34 Control register,
		and proceed only if the bit is set.*/
		readRMI(SynaF34_FlashControl, &uData, 1);

		while (uData != 0x80) {
			/* In practice, if uData!=0x80 happens for multiple
			counts, it indicates reflash is failed to be enabled,
			and program should quit */
			;
		}
	}
}
/* SynaProgramFirmware prepares the firmware writing process
 */
static void SynaProgramFirmware(void)
{
    unsigned char uData;

    printk("\nProgram Firmware Section...");

    SynaReadBootloadID();
    printk("\n------------SynaReadBootloadID()--");
    SynaWriteBootloadID();
    printk("\n-------------SynaWriteBootloadID()--");
    uData = 3;
    writeRMI(SynaF34_FlashControl, &uData, 1);
    printk("\n------------writeRMI(SynaF34_FlashControl, &uData, 1)--\n");
    msleep(1000);
    SynaWaitATTN();
    printk("\n-------------SynaWaitATTN()---\n");
    SynaFlashFirmwareWrite();
    printk("\n-------------SynaFlashFirmwareWrite()---\n");
}