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 
26 #include <rtthread.h>
27 #include "sysctl_boot.h"
28 #include "ioremap.h"
29 #include "board.h"
30 
31 volatile sysctl_boot_t* sysctl_boot = (volatile sysctl_boot_t*)BOOT_BASE_ADDR;
32 
sysctl_boot_get_boot_mode(void)33 sysctl_boot_mode_e sysctl_boot_get_boot_mode(void)
34 {
35     switch(sysctl_boot->soc_boot_ctl & 0x3) /* bit 0~1 */
36     {
37         case 1:
38             return SYSCTL_BOOT_NANDFLASH;
39         case 2:
40             return SYSCTL_BOOT_EMMC;
41         case 3:
42             return SYSCTL_BOOT_SDCARD;
43         case 0:
44         default:
45             return SYSCTL_BOOT_NORFLASH;
46     }
47 }
48 
sysctl_boot_get_otp_bypass(void)49 bool sysctl_boot_get_otp_bypass(void)
50 {
51     if(sysctl_boot->soc_boot_ctl & 0x10)
52         return true;
53     else
54         return false;
55 }
56 
sysctl_boot_set_pll_lock(void)57 void sysctl_boot_set_pll_lock(void)
58 {
59     sysctl_boot->soc_boot_ctl |= 1 << 3;
60 }
61 
sysctl_boot_set_spi2axi(void)62 void sysctl_boot_set_spi2axi(void)
63 {
64     sysctl_boot->soc_boot_ctl |= 1 << 2;
65 }
66 
sysctl_boot_reset_soc(void)67 void sysctl_boot_reset_soc(void)
68 {
69     sysctl_boot->soc_glb_rst |= (1 << 0) | (1 << 16);
70     while(1)
71     {
72     }
73 }
74 
75 
sysctl_boot_soc_sleep_ctl(void)76 void sysctl_boot_soc_sleep_ctl(void)
77 {
78     sysctl_boot->soc_slp_ctl |= (1 << 4) | (1 << 20);
79 }
80 
sysctl_boot_read_is_boot_wakeup(void)81 int sysctl_boot_read_is_boot_wakeup(void)
82 {
83     return sysctl_boot->soc_wakeup_src;
84 }
85 
rt_hw_sysctl_boot_init(void)86 int rt_hw_sysctl_boot_init(void)
87 {
88     sysctl_boot = rt_ioremap((void*)BOOT_BASE_ADDR, BOOT_IO_SIZE);
89     if(!sysctl_boot)
90     {
91         rt_kprintf("sysctl_boot ioremap error\n");
92         return -1;
93     }
94 
95     return 0;
96 }
97 INIT_BOARD_EXPORT(rt_hw_sysctl_boot_init);