1 /**
2   ******************************************************************************
3   * @file    bl702_xip_sflash_ext.c
4   * @version V1.0
5   * @date
6   * @brief   This file is the standard driver c file
7   ******************************************************************************
8   * @attention
9   *
10   * <h2><center>&copy; COPYRIGHT(c) 2020 Bouffalo Lab</center></h2>
11   *
12   * Redistribution and use in source and binary forms, with or without modification,
13   * are permitted provided that the following conditions are met:
14   *   1. Redistributions of source code must retain the above copyright notice,
15   *      this list of conditions and the following disclaimer.
16   *   2. Redistributions in binary form must reproduce the above copyright notice,
17   *      this list of conditions and the following disclaimer in the documentation
18   *      and/or other materials provided with the distribution.
19   *   3. Neither the name of Bouffalo Lab nor the names of its contributors
20   *      may be used to endorse or promote products derived from this software
21   *      without specific prior written permission.
22   *
23   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
27   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *
34   ******************************************************************************
35   */
36 
37 #include "bl702_xip_sflash_ext.h"
38 
39 /** @addtogroup  BL702_Peripheral_Driver
40  *  @{
41  */
42 
43 /** @addtogroup  XIP_SFLASH
44  *  @{
45  */
46 
47 /** @defgroup  XIP_SFLASH_EXT_Private_Macros
48  *  @{
49  */
50 
51 /*@} end of group XIP_SFLASH_EXT_Private_Macros */
52 
53 /** @defgroup  XIP_SFLASH_EXT_Private_Types
54  *  @{
55  */
56 
57 /*@} end of group XIP_SFLASH_EXT_Private_Types */
58 
59 /** @defgroup  XIP_SFLASH_EXT_Private_Variables
60  *  @{
61  */
62 
63 /*@} end of group XIP_SFLASH_EXT_Private_Variables */
64 
65 /** @defgroup  XIP_SFLASH_EXT_Global_Variables
66  *  @{
67  */
68 
69 /*@} end of group XIP_SFLASH_EXT_Global_Variables */
70 
71 /** @defgroup  XIP_SFLASH_EXT_Private_Fun_Declaration
72  *  @{
73  */
74 
75 /*@} end of group XIP_SFLASH_EXT_Private_Fun_Declaration */
76 
77 /** @defgroup  XIP_SFLASH_EXT_Private_Functions
78  *  @{
79  */
80 
81 /****************************************************************************//**
82  * @brief  XIP SFlash option save
83  *
84  * @param  aes_enable: AES enable status pointer
85  *
86  * @return None
87  *
88 *******************************************************************************/
bflb_xip_sflash_opt_enter(uint8_t * aes_enable)89 void ATTR_TCM_SECTION bflb_xip_sflash_opt_enter(uint8_t *aes_enable)
90 {
91     *aes_enable = bflb_sf_ctrl_is_aes_enable();
92 
93     if (*aes_enable) {
94         bflb_sf_ctrl_aes_disable();
95     }
96 }
97 
98 /****************************************************************************//**
99  * @brief  XIP SFlash option restore
100  *
101  * @param  aes_enable: AES enable status
102  *
103  * @return None
104  *
105 *******************************************************************************/
bflb_xip_sflash_opt_exit(uint8_t aes_enable)106 void ATTR_TCM_SECTION bflb_xip_sflash_opt_exit(uint8_t aes_enable)
107 {
108     if (aes_enable) {
109         bflb_sf_ctrl_aes_enable();
110     }
111 }
112 
113 /****************************************************************************//**
114  * @brief  Read data from flash via XIP
115  *
116  * @param  addr: flash read start address
117  * @param  data: data pointer to store data read from flash
118  * @param  len: data length to read
119  * @param  group: CPU group id 0 or 1
120  * @param  bank: Flash bank select
121  *
122  * @return BFLB_RET:0 means success and other value means error
123  *
124 *******************************************************************************/
125 __WEAK
bflb_xip_sflash_read_via_cache_need_lock(uint32_t addr,uint8_t * data,uint32_t len,uint8_t group,uint8_t bank)126 int ATTR_TCM_SECTION bflb_xip_sflash_read_via_cache_need_lock(uint32_t addr,uint8_t *data, uint32_t len,
127                                                               uint8_t group, uint8_t bank)
128 {
129     uint32_t offset;
130 
131     if(addr>=BL702_FLASH_XIP_BASE && addr<BL702_FLASH_XIP_END){
132         offset=bflb_sf_ctrl_get_flash_image_offset(group, bank);
133         bflb_sf_ctrl_set_flash_image_offset(0, group, bank);
134         /* Flash read */
135         BL702_MemCpy_Fast(data,(void *)(addr-bflb_sf_ctrl_get_flash_image_offset(group, bank)),len);
136         bflb_sf_ctrl_set_flash_image_offset(offset, group, bank);
137     }
138 
139     return 0;
140 }
141 
142 
143 /****************************************************************************//**
144  * @brief  XIP KH25V40 flash write protect set
145  *
146  * @param  p_flash_cfg: Flash config pointer
147  * @param  protect: protect area
148  *
149  * @return BFLB_RET:0 means success and other value means error
150  *
151 *******************************************************************************/
bflb_xip_sflash_kh25v40_write_protect_need_lock(spi_flash_cfg_type * p_flash_cfg,uint8_t protect)152 int ATTR_TCM_SECTION bflb_xip_sflash_kh25v40_write_protect_need_lock(spi_flash_cfg_type *p_flash_cfg, uint8_t protect)
153 {
154     int stat = -1;
155     uint32_t offset;
156 
157     stat = bflb_xip_sflash_state_save(p_flash_cfg, &offset, 0, 0);
158     if (stat != 0) {
159         bflb_sflash_set_xip_cfg(p_flash_cfg, 1, 0, 32, 0, 0);
160     } else {
161         stat = bflb_sflash_kh25v40_write_protect(p_flash_cfg, protect);
162         bflb_xip_sflash_state_restore(p_flash_cfg, offset, 0, 0);
163     }
164 
165     return stat;
166 }
167 
168 /****************************************************************************//**
169  * @brief  Clear flash status register need lock
170  *
171  * @param  p_flash_cfg: Flash config pointer
172  *
173  * @return BFLB_RET:0 means success and other value means error
174  *
175 *******************************************************************************/
bflb_xip_sflash_clear_status_register_need_lock(spi_flash_cfg_type * p_flash_cfg,uint8_t group,uint8_t bank)176 int ATTR_TCM_SECTION bflb_xip_sflash_clear_status_register_need_lock(spi_flash_cfg_type *p_flash_cfg,
177                                                                      uint8_t group, uint8_t bank)
178 {
179     int stat = -1;
180     uint32_t offset;
181 
182     stat=bflb_xip_sflash_state_save(p_flash_cfg, &offset, 0, 0);
183     if (stat != 0) {
184         bflb_sflash_set_xip_cfg(p_flash_cfg, 1, 0, 32, 0, 0);
185     } else {
186         stat=bflb_sflash_clear_status_register(p_flash_cfg);
187         bflb_xip_sflash_state_restore(p_flash_cfg, offset, 0, 0);
188     }
189 
190     return stat;
191 }
192 
193 /*@} end of group XIP_SFLASH_EXT_Public_Functions */
194 
195 /*@} end of group XIP_SFLASH_EXT */
196 
197 /*@} end of group BL702_Peripheral_Driver */
198