// File: /workspace/github_c_makefile_star2_O0/ataradov[P]edbg/target_rpi_rp2040.c
// Function: flash_prepare
static void flash_prepare(void)
{
  uint8_t buf[4];

  memset(buf, 0, sizeof(buf));

  // Exit XIP mode. There is no universal way, so we do the best we can using
  // the same procedure as used by the RP2040 ROM code.

  dap_write_word_req(RESETS_RESET_CLR, RESETS_RESET_PADS_QSPI | RESETS_RESET_IO_QSPI | RESETS_RESET_DMA);
  dap_write_word_req(GPIO_QSPI_SD0_CTRL, GPIO_QSPI_xx_CTRL_OUTOVER_NORMAL);
  dap_write_word_req(GPIO_QSPI_SD1_CTRL, GPIO_QSPI_xx_CTRL_OUTOVER_NORMAL);
  dap_write_word_req(GPIO_QSPI_SD2_CTRL, GPIO_QSPI_xx_CTRL_OUTOVER_NORMAL);
  dap_write_word_req(GPIO_QSPI_SD3_CTRL, GPIO_QSPI_xx_CTRL_OUTOVER_NORMAL);
  dap_write_word_req(GPIO_QSPI_SCLK_CTRL, GPIO_QSPI_xx_CTRL_OUTOVER_NORMAL);
  dap_write_word_req(GPIO_QSPI_SS_CTRL, GPIO_QSPI_xx_CTRL_OUTOVER_NORMAL);
  dap_transfer();

  spi_normal_mode();

  dap_write_word_req(PADS_QSPI_SD0, PADS_QSPI_DEFAULT | PADS_QSPI_xx_OD | PADS_QSPI_xx_PDE);
  dap_write_word_req(PADS_QSPI_SD1, PADS_QSPI_DEFAULT | PADS_QSPI_xx_OD | PADS_QSPI_xx_PDE);
  dap_write_word_req(PADS_QSPI_SD2, PADS_QSPI_DEFAULT | PADS_QSPI_xx_OD | PADS_QSPI_xx_PDE);
  dap_write_word_req(PADS_QSPI_SD3, PADS_QSPI_DEFAULT | PADS_QSPI_xx_OD | PADS_QSPI_xx_PDE);
  dap_transfer();

  spi_select(1);
  spi_transfer(buf, 4, 4);

  dap_write_word_req(PADS_QSPI_SD0, PADS_QSPI_DEFAULT | PADS_QSPI_xx_OD | PADS_QSPI_xx_PUE);
  dap_write_word_req(PADS_QSPI_SD1, PADS_QSPI_DEFAULT | PADS_QSPI_xx_OD | PADS_QSPI_xx_PUE);
  dap_write_word_req(PADS_QSPI_SD2, PADS_QSPI_DEFAULT | PADS_QSPI_xx_OD | PADS_QSPI_xx_PUE);
  dap_write_word_req(PADS_QSPI_SD3, PADS_QSPI_DEFAULT | PADS_QSPI_xx_OD | PADS_QSPI_xx_PUE);
  dap_transfer();

  spi_select(0);
  spi_transfer(buf, 4, 4);
  spi_select(1);

  dap_write_word_req(PADS_QSPI_SD0, PADS_QSPI_DEFAULT | PADS_QSPI_xx_PDE);
  dap_write_word_req(PADS_QSPI_SD1, PADS_QSPI_DEFAULT | PADS_QSPI_xx_PDE);
  dap_write_word_req(PADS_QSPI_SD2, PADS_QSPI_DEFAULT | PADS_QSPI_xx_PUE);
  dap_write_word_req(PADS_QSPI_SD3, PADS_QSPI_DEFAULT | PADS_QSPI_xx_PUE);
  dap_transfer();

  buf[0] = 0xff;
  buf[1] = 0xff;

  spi_select(0);
  spi_transfer(buf, 2, 2);
  spi_select(1);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10, %rsp
leaq -0x4(%rbp), %rdi
xorl %esi, %esi
movl $0x4, %edx
callq 0x60f0
movl $0x4000f000, %edi       # imm = 0x4000F000
movl $0x244, %esi            # imm = 0x244
callq 0x70c0
movl $0x40018014, %edi       # imm = 0x40018014
xorl %esi, %esi
callq 0x70c0
movl $0x4001801c, %edi       # imm = 0x4001801C
xorl %esi, %esi
callq 0x70c0
movl $0x40018024, %edi       # imm = 0x40018024
xorl %esi, %esi
callq 0x70c0
movl $0x4001802c, %edi       # imm = 0x4001802C
xorl %esi, %esi
callq 0x70c0
movl $0x40018004, %edi       # imm = 0x40018004
xorl %esi, %esi
callq 0x70c0
movl $0x4001800c, %edi       # imm = 0x4001800C
xorl %esi, %esi
callq 0x70c0
callq 0x6c70
callq 0x11c50
movl $0x40020008, %edi       # imm = 0x40020008
movl $0xd6, %esi
callq 0x70c0
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0xd6, %esi
callq 0x70c0
movl $0x40020010, %edi       # imm = 0x40020010
movl $0xd6, %esi
callq 0x70c0
movl $0x40020014, %edi       # imm = 0x40020014
movl $0xd6, %esi
callq 0x70c0
callq 0x6c70
movl $0x1, %edi
callq 0x11cf0
leaq -0x4(%rbp), %rdi
movl $0x4, %edx
movl %edx, %esi
callq 0x11d10
movl $0x40020008, %edi       # imm = 0x40020008
movl $0xda, %esi
callq 0x70c0
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0xda, %esi
callq 0x70c0
movl $0x40020010, %edi       # imm = 0x40020010
movl $0xda, %esi
callq 0x70c0
movl $0x40020014, %edi       # imm = 0x40020014
movl $0xda, %esi
callq 0x70c0
callq 0x6c70
xorl %edi, %edi
callq 0x11cf0
leaq -0x4(%rbp), %rdi
movl $0x4, %edx
movl %edx, %esi
callq 0x11d10
movl $0x1, %edi
callq 0x11cf0
movl $0x40020008, %edi       # imm = 0x40020008
movl $0x56, %esi
callq 0x70c0
movl $0x4002000c, %edi       # imm = 0x4002000C
movl $0x56, %esi
callq 0x70c0
movl $0x40020010, %edi       # imm = 0x40020010
movl $0x5a, %esi
callq 0x70c0
movl $0x40020014, %edi       # imm = 0x40020014
movl $0x5a, %esi
callq 0x70c0
callq 0x6c70
movb $-0x1, -0x4(%rbp)
movb $-0x1, -0x3(%rbp)
xorl %edi, %edi
callq 0x11cf0
leaq -0x4(%rbp), %rdi
movl $0x2, %edx
movl %edx, %esi
callq 0x11d10
movl $0x1, %edi
callq 0x11cf0
addq $0x10, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)