// File: /workspace/github_cpp_makefile_star2_O2/chenhn123[P]wdt_util_src/wdt_ct/w8760_funcs.cpp
// Function: wh_w8760_dev_send_commands(WdtDevice*, int, unsigned int)
int wh_w8760_dev_send_commands(WDT_DEV* pdev, int cmd, UINT32 value)
{
	switch(cmd)
	{
		case 	WH_CMD_ALGO_STOP: {
			return wh_w8760_dev_set_n_check_device_mode(pdev, W8760_MODE_COMMAND, 0, 0);
		}
		break;
		case 	WH_CMD_ALGO_START: 
		case 	WH_CMD_ALGO_RESTART: {	
			return wh_w8760_dev_set_n_check_device_mode(pdev, W8760_MODE_SENSING, 0, 0);
		}
		break;
		case 	WH_CMD_SET_DEV_MODE: {
			return wh_w8760_dev_set_n_check_device_mode(pdev, value, 0, 0);
		}
		break;
		case 	WH_CMD_RESET: {
			/**
 		 	 *  Hard reset doesn't make OSC back to default. So we need this workaround to set OSC to default.
                 	 * If not, the OSC clock for ROM code is too high and might cause booting fails.
                 	 */
                        if (pdev->board_info.dev_type & FW_WDT8762) {
                                wh_w8760_dev_write_register(pdev, 0x90801408, 0x1B);
                        }else if (pdev->board_info.dev_type & FW_WDT8760) {
                                wh_w8760_dev_write_register(pdev, 0x00801408, 0x28);
                        }
	
			return wh_w8760_dev_reboot(pdev);
		}
		break;
		case	WH_CMD_FLASH_ERASE4K:	{
			return wh_w8760_dev_flash_erase(pdev, value, 0x1000);
		}
		break;
		case	WH_CMD_FLASH_ERASE32K:	{
			return wh_w8760_dev_flash_erase(pdev, value, 0x8000);
		}
		break;
		case	WH_CMD_FLASH_ERASE64K:	{
			return wh_w8760_dev_flash_erase(pdev, value, 0x10000);
		}
		break;
		case	WH_CMD_FLASH_LOCK:	
		case	WH_CMD_FLASH_PROTECTION_ON: 
		{
			return wh_w8760_dev_protect_flash(pdev, W8760_ProtectAll512k);
		}
		break;
		case	WH_CMD_FLASH_UNLOCK: 
		case	WH_CMD_FLASH_PROTECTION_OFF: 
		{
			/* address align to 0x100 */
			UINT32 addr = (value & 0xFFFF0000) >> 8;
			/*  size align to 0x100 */
			UINT32 size = (value & 0xFFFF) << 8; 	
			int ret = 0;
			
			if (addr < 128 * 1024 && addr + size <= 128 * 1024)
				ret = wh_w8760_dev_protect_flash(pdev, W8760_UnprotectLower128k);
					else if (addr < 256 * 1024 && addr + size <= 256 * 1024)
				ret = wh_w8760_dev_protect_flash(pdev, W8760_UnprotectLower256k);
			else if (addr < 384 * 1024 && addr + size <= 384 * 1024)
				ret = wh_w8760_dev_protect_flash(pdev, W8760_UnprotectLower384k);
	  		else if (addr < 508 * 1024 && addr + size <= 508 * 1024)
				ret = wh_w8760_dev_protect_flash(pdev, W8760_UnprotectLower508k);
			return ret;
		}
		break;		
 	
		default: {
			return 0;
		}
	}

	return 0;
}

// Assembly:
pushq %rbx
decl %esi
cmpl $0x16, %esi
ja 0x45f2
movq %rdi, %rbx
leaq 0x637e(%rip), %rax      # 0xa840
movslq (%rax,%rsi,4), %rcx
addq %rax, %rcx
jmpq *%rcx
pushq $0x1
popq %rsi
jmp 0x458a
movl %edx, %ecx
shrl $0x8, %ecx
andl $0xffff00, %ecx         # imm = 0xFFFF00
movzwl %dx, %eax
shll $0x8, %eax
cmpl $0x2000000, %edx        # imm = 0x2000000
setb %sil
addl %ecx, %eax
cmpl $0x20001, %eax          # imm = 0x20001
setb %cl
andb %sil, %cl
cmpb $0x1, %cl
jne 0x452c
movq %rbx, %rdi
movl $0x4028, %esi           # imm = 0x4028
jmp 0x45ec
pushq $0x7c
jmp 0x45e8
movq %rbx, %rdi
movl %edx, %esi
movl $0x10000, %edx          # imm = 0x10000
jmp 0x4581
movq %rbx, %rdi
movl %edx, %esi
movl $0x8000, %edx           # imm = 0x8000
jmp 0x4581
cmpl $0x4000000, %edx        # imm = 0x4000000
setb %cl
cmpl $0x40001, %eax          # imm = 0x40001
setb %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0x4597
pushq $0xc
jmp 0x45e8
movl 0x188(%rbx), %eax
btl $0xd, %eax
jb 0x45b6
btl $0xb, %eax
jae 0x45c6
pushq $0x28
popq %rdx
movq %rbx, %rdi
movl $0x801408, %esi         # imm = 0x801408
jmp 0x45c1
movq %rbx, %rdi
movl $0x90, %esi
jmp 0x458d
movq %rbx, %rdi
movl %edx, %esi
movl $0x1000, %edx           # imm = 0x1000
popq %rbx
jmp 0x45f6
movzbl %dl, %esi
movq %rbx, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
popq %rbx
jmp 0x3f5f
cmpl $0x6000000, %edx        # imm = 0x6000000
setb %cl
cmpl $0x60001, %eax          # imm = 0x60001
setb %sil
andb %cl, %sil
cmpb $0x1, %sil
jne 0x45cf
pushq $0x8
jmp 0x45e8
pushq $0x1b
popq %rdx
movq %rbx, %rdi
movl $0x90801408, %esi       # imm = 0x90801408
callq 0x4467
movq %rbx, %rdi
popq %rbx
jmp 0x3e6c
cmpl $0x7f00000, %edx        # imm = 0x7F00000
setb %cl
cmpl $0x7f001, %eax          # imm = 0x7F001
setb %al
andb %cl, %al
cmpb $0x1, %al
jne 0x45f2
pushq $0x44
popq %rsi
movq %rbx, %rdi
popq %rbx
jmp 0x437d
xorl %eax, %eax
popq %rbx
retq