#include "core_v5.h"

.section .switch, "ax"
.globl quad_switch
.type quad_switch,@function
.balign 4

quad_switch:
.option push
.option norvc
  /*save ra*/
  addi sp,sp,-4
  sw ra,0(sp)

  /*load NORC_CTL*/
  li t1,0x20090000
  lw t2,0(t1)

   /*set NORC_CTL.WAIT*/
  li t0,0x01000000
  or t2,t2,t0
  sw t2,0(t1)

  /*waiting for NORC idle*/
1:
  lw t2,8(t1)
  andi t0,t2,0x20
  bne t0,zero,do_swicth
  j 1b

   /*call NORCTRL_QuadSPIinit()*/
do_swicth:
  beqc a3,1,switch_single
  beqc a3,2,switch_dual
  beqc a3,4,switch_quad

switch_quad:
  mv a3,zero
  li t0,0x81a82
  jalr t0

  /*clear NORC_CTL.WAIT*/
  li t1,0x20090000
  lw t2,0(t1)
  li t0,~0x01000000
  and t2,t2,t0

  /*set NORC_CTL.STOP_CYCLE=3*/
  li t0,0x00300000
  or t2,t2,t0
  sw t2,0(t1)

  j exit

switch_dual:
/*
DEV_NORC_CTL_SPI_WIDTH_DUAL	|
DEV_NORC_CTL_ADR_MODE_ACCORDINGTOSPI_WIDTH	|
DEV_NORC_CTL_FAST_MODE_FASTMODE	|
(3u << 8)	|	//DUMY_CYCLE
(15u << 12)	|	//DELAY_CHAIN
DEV_NORC_CTL_HOLD_MODE_ENABLE	|
DEV_NORC_CTL_HOLD_CYCLE_16		|
DEV_NORC_CTL_STOP_CYCLE_4		|
DEV_NORC_CTL_PREF_EN_ENABLE
*/
  li t2,0x0236f30d
  sw t2,0(t1)
  j exit

switch_single:
/*
DEV_NORC_CTL_SPI_WIDTH_SINGLE	|
DEV_NORC_CTL_ADR_MODE_SINGLE	|
DEV_NORC_CTL_FAST_MODE_FASTMODE	|
(7u << 8)	|	//DUMY_CYCLE
(15u << 12)	|	//DELAY_CHAIN
DEV_NORC_CTL_HOLD_MODE_ENABLE	|
DEV_NORC_CTL_HOLD_CYCLE_16		|
DEV_NORC_CTL_STOP_CYCLE_4		|
DEV_NORC_CTL_PREF_EN_ENABLE
*/
  li t2,0x0236f708
  sw t2,0(t1)

exit:
  lw ra,0(sp)
  addi sp,sp,4
  ret
.option pop


