sysctl_boot.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <rtthread.h>
  26. #include "sysctl_boot.h"
  27. #include "ioremap.h"
  28. #include "board.h"
  29. volatile sysctl_boot_t* sysctl_boot = (volatile sysctl_boot_t*)BOOT_BASE_ADDR;
  30. sysctl_boot_mode_e sysctl_boot_get_boot_mode(void)
  31. {
  32. switch(sysctl_boot->soc_boot_ctl & 0x3) /* bit 0~1 */
  33. {
  34. case 1:
  35. return SYSCTL_BOOT_NANDFLASH;
  36. case 2:
  37. return SYSCTL_BOOT_EMMC;
  38. case 3:
  39. return SYSCTL_BOOT_SDCARD;
  40. case 0:
  41. default:
  42. return SYSCTL_BOOT_NORFLASH;
  43. }
  44. }
  45. bool sysctl_boot_get_otp_bypass(void)
  46. {
  47. if(sysctl_boot->soc_boot_ctl & 0x10)
  48. return true;
  49. else
  50. return false;
  51. }
  52. void sysctl_boot_set_pll_lock(void)
  53. {
  54. sysctl_boot->soc_boot_ctl |= 1 << 3;
  55. }
  56. void sysctl_boot_set_spi2axi(void)
  57. {
  58. sysctl_boot->soc_boot_ctl |= 1 << 2;
  59. }
  60. void sysctl_boot_reset_soc(void)
  61. {
  62. sysctl_boot->soc_glb_rst |= (1 << 0) | (1 << 16);
  63. while(1)
  64. {
  65. }
  66. }
  67. void sysctl_boot_soc_sleep_ctl(void)
  68. {
  69. sysctl_boot->soc_slp_ctl |= (1 << 4) | (1 << 20);
  70. }
  71. int sysctl_boot_read_is_boot_wakeup(void)
  72. {
  73. return sysctl_boot->soc_wakeup_src;
  74. }
  75. int rt_hw_sysctl_boot_init(void)
  76. {
  77. sysctl_boot = rt_ioremap((void*)BOOT_BASE_ADDR, BOOT_IO_SIZE);
  78. if(!sysctl_boot)
  79. {
  80. rt_kprintf("sysctl_boot ioremap error\n");
  81. return -1;
  82. }
  83. return 0;
  84. }
  85. INIT_BOARD_EXPORT(rt_hw_sysctl_boot_init);