• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <soc/soc.h>
18 #include "hal_port.h"
19 #include "flash_hw.h"
20 #include <driver/hal/hal_flash_types.h>
21 #include "icu_hw.h"
22 #include "icu_ll.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define FLASH_LL_REG_BASE(_flash_unit_id)    (SOC_FLASH_REG_BASE)
29 
flash_ll_init(flash_hw_t * hw)30 static inline void flash_ll_init(flash_hw_t *hw)
31 {
32 
33 }
34 
flash_ll_is_busy(flash_hw_t * hw)35 static inline bool flash_ll_is_busy(flash_hw_t *hw)
36 {
37 	return hw->op_sw.busy_sw;
38 }
39 
flash_ll_read_flash_id(flash_hw_t * hw)40 static inline uint32_t flash_ll_read_flash_id(flash_hw_t *hw)
41 {
42 	return hw->rd_flash_id;
43 }
44 
flash_ll_set_op_cmd(flash_hw_t * hw,flash_op_cmd_t cmd)45 static inline void flash_ll_set_op_cmd(flash_hw_t *hw, flash_op_cmd_t cmd)
46 {
47 	hw->op_sw.op_type_sw = cmd;
48 	hw->op_sw.op_sw = 1;
49 	hw->op_sw.wp_value = 1;
50 }
51 
flash_ll_get_id(flash_hw_t * hw)52 static inline uint32_t flash_ll_get_id(flash_hw_t *hw)
53 {
54 	flash_ll_set_op_cmd(hw, FLASH_OP_CMD_RDID);
55 	while (flash_ll_is_busy(hw));
56 	return flash_ll_read_flash_id(hw);
57 }
58 
flash_ll_get_mid(flash_hw_t * hw)59 static inline uint32_t flash_ll_get_mid(flash_hw_t *hw)
60 {
61 	while (flash_ll_is_busy(hw));
62 	hw->op_sw.op_type_sw = FLASH_OP_CMD_RDID;
63 	hw->op_sw.op_sw = 1;
64 	while (flash_ll_is_busy(hw));
65 	return flash_ll_read_flash_id(hw);
66 }
67 
flash_ll_init_wrsr_cmd(flash_hw_t * hw,uint8_t wrsr_cmd)68 static inline void flash_ll_init_wrsr_cmd(flash_hw_t *hw, uint8_t wrsr_cmd)
69 {
70 	hw->sr_cmd.wrsr_cmd_reg = wrsr_cmd;
71 	hw->sr_cmd.wrsr_cmd_sel = 1;
72 	while (flash_ll_is_busy(hw));
73 }
74 
flash_ll_init_rdsr_cmd(flash_hw_t * hw,uint8_t rdsr_cmd)75 static inline void flash_ll_init_rdsr_cmd(flash_hw_t *hw, uint8_t rdsr_cmd)
76 {
77 	hw->sr_cmd.rdsr_cmd_reg = rdsr_cmd;
78 	hw->sr_cmd.rdsr_cmd_sel = 1;
79 	while (flash_ll_is_busy(hw));
80 }
81 
flash_ll_deinit_wrsr_cmd(flash_hw_t * hw)82 static inline void flash_ll_deinit_wrsr_cmd(flash_hw_t *hw)
83 {
84 	hw->sr_cmd.wrsr_cmd_reg = 0x1;
85 	hw->sr_cmd.wrsr_cmd_sel = 0;
86 	while (flash_ll_is_busy(hw));
87 }
88 
flash_ll_deinit_rdsr_cmd(flash_hw_t * hw)89 static inline void flash_ll_deinit_rdsr_cmd(flash_hw_t *hw)
90 {
91 	hw->sr_cmd.rdsr_cmd_reg = 0x5;
92 	hw->sr_cmd.rdsr_cmd_sel = 0;
93 	while (flash_ll_is_busy(hw));
94 }
95 
flash_ll_write_status_reg(flash_hw_t * hw,uint8_t sr_width,uint16_t sr_data)96 static inline void flash_ll_write_status_reg(flash_hw_t *hw, uint8_t sr_width, uint16_t sr_data)
97 {
98 	while (flash_ll_is_busy(hw));
99 	hw->config.wrsr_data = sr_data;
100 	while (flash_ll_is_busy(hw));
101 
102 	if (sr_width == 1) {
103 		flash_ll_set_op_cmd(hw, FLASH_OP_CMD_WRSR);
104 	} else if (sr_width == 2) {
105 		if(FLASH_ID_GD25Q32C == flash_ll_get_id(hw)) {
106 			flash_ll_set_op_cmd(hw, FLASH_OP_CMD_WRSR);
107 			while (flash_ll_is_busy(hw));
108 
109 			hw->config.wrsr_data = (sr_data >> LEN_WRSR_S0_S7);
110 			while (flash_ll_is_busy(hw));
111 			flash_ll_init_wrsr_cmd(hw, CMD_WRSR_S8_S15);
112 			while (flash_ll_is_busy(hw));
113 			flash_ll_set_op_cmd(hw, FLASH_OP_CMD_WRSR);
114 			while (flash_ll_is_busy(hw));
115 			flash_ll_deinit_wrsr_cmd(hw);
116 		} else {
117 			flash_ll_set_op_cmd(hw, FLASH_OP_CMD_WRSR2);
118 		}
119 	}
120 	while (flash_ll_is_busy(hw));
121 }
122 
flash_ll_set_qe(flash_hw_t * hw,uint8_t qe_bit,uint8_t qe_bit_post)123 static inline void flash_ll_set_qe(flash_hw_t *hw, uint8_t qe_bit, uint8_t qe_bit_post)
124 {
125 	hw->config.v |= qe_bit << qe_bit_post;
126 }
127 
flash_ll_read_status_reg(flash_hw_t * hw,uint8_t sr_width)128 static inline uint16_t flash_ll_read_status_reg(flash_hw_t *hw, uint8_t sr_width)
129 {
130 	uint16_t state_reg_data = 0;
131 
132 	while (flash_ll_is_busy(hw));
133 	flash_ll_set_op_cmd(hw, FLASH_OP_CMD_RDSR);
134 	while (flash_ll_is_busy(hw));
135 
136 	state_reg_data = hw->state.status_reg;
137 
138 	if (sr_width == 2) {
139 		flash_ll_set_op_cmd(hw, FLASH_OP_CMD_RDSR2);
140 		while (flash_ll_is_busy(hw));
141 		state_reg_data |= hw->state.status_reg << 8;
142 	}
143 	return state_reg_data;
144 }
145 
flash_ll_enable_cpu_data_wr(flash_hw_t * hw)146 static inline void flash_ll_enable_cpu_data_wr(flash_hw_t *hw)
147 {
148 	hw->config.cpu_data_wr_en = 1;
149 }
150 
flash_ll_disable_cpu_data_wr(flash_hw_t * hw)151 static inline void flash_ll_disable_cpu_data_wr(flash_hw_t *hw)
152 {
153 	hw->config.cpu_data_wr_en = 0;
154 }
155 
flash_ll_clear_qwfr(flash_hw_t * hw)156 static inline void flash_ll_clear_qwfr(flash_hw_t *hw)
157 {
158 	hw->config.mode_sel = 0;
159 	hw->op_sw.addr_sw_reg = 0;
160 	flash_ll_set_op_cmd(hw, FLASH_OP_CMD_CRMR);
161 	while (flash_ll_is_busy(hw));
162 }
163 
flash_ll_set_mode(flash_hw_t * hw,uint8_t mode_sel)164 static inline void flash_ll_set_mode(flash_hw_t *hw, uint8_t mode_sel)
165 {
166 	hw->config.mode_sel = mode_sel;
167 }
168 
flash_ll_set_dual_mode(flash_hw_t * hw)169 static inline void flash_ll_set_dual_mode(flash_hw_t *hw)
170 {
171 	hw->config.mode_sel = FLASH_MODE_DUAL;
172 }
173 
flash_ll_set_quad_m_value(flash_hw_t * hw,uint32_t m_value)174 static inline void flash_ll_set_quad_m_value(flash_hw_t *hw, uint32_t m_value)
175 {
176 
177 }
178 
flash_ll_erase_sector(flash_hw_t * hw,uint32_t erase_addr)179 static inline void flash_ll_erase_sector(flash_hw_t *hw, uint32_t erase_addr)
180 {
181 	while (flash_ll_is_busy(hw));
182 	hw->op_sw.addr_sw_reg = erase_addr;
183 	hw->op_sw.op_type_sw = FLASH_OP_CMD_SE;
184 	hw->op_sw.op_sw = 1;
185 	while (flash_ll_is_busy(hw));
186 }
187 
flash_ll_set_op_cmd_read(flash_hw_t * hw,uint32_t read_addr)188 static inline void flash_ll_set_op_cmd_read(flash_hw_t *hw, uint32_t read_addr)
189 {
190 	hw->op_sw.addr_sw_reg = read_addr;
191 	hw->op_sw.op_type_sw = FLASH_OP_CMD_READ;
192 	hw->op_sw.op_sw = 1;
193 	while (flash_ll_is_busy(hw));
194 }
195 
flash_ll_read_data(flash_hw_t * hw)196 static inline uint32_t flash_ll_read_data(flash_hw_t *hw)
197 {
198 	return hw->data_flash_sw;
199 }
200 
flash_ll_set_op_cmd_write(flash_hw_t * hw,uint32_t write_addr)201 static inline void flash_ll_set_op_cmd_write(flash_hw_t *hw, uint32_t write_addr)
202 {
203 	hw->op_sw.addr_sw_reg = write_addr;
204 	hw->op_sw.op_type_sw = FLASH_OP_CMD_PP;
205 	hw->op_sw.op_sw = 1;
206 	while (flash_ll_is_busy(hw));
207 }
208 
flash_ll_write_data(flash_hw_t * hw,uint32_t data)209 static inline void flash_ll_write_data(flash_hw_t *hw, uint32_t data)
210 {
211 	hw->data_sw_flash = data;
212 }
213 
flash_ll_set_clk(flash_hw_t * hw,uint8_t clk_cfg)214 static inline void flash_ll_set_clk(flash_hw_t *hw, uint8_t clk_cfg)
215 {
216 	hw->config.clk_cfg = clk_cfg;
217 
218 #if CONFIG_JTAG
219 	hw->config.crc_en = 0;
220 #endif
221 }
222 
flash_ll_set_default_clk(flash_hw_t * hw)223 static inline void flash_ll_set_default_clk(flash_hw_t *hw)
224 {
225 	flash_ll_set_clk(hw, 0x5);
226 }
227 
flash_ll_set_clk_dpll(flash_hw_t * hw)228 static inline void flash_ll_set_clk_dpll(flash_hw_t *hw)
229 {
230 	hw->config.clk_cfg = 5;
231 }
232 
flash_ll_set_clk_dco(flash_hw_t * hw,bool ate_enabled)233 static inline void flash_ll_set_clk_dco(flash_hw_t *hw, bool ate_enabled)
234 {
235 	if (ate_enabled) {
236 		hw->config.clk_cfg = 0xB;
237 	} else {
238 		hw->config.clk_cfg = 0x9;
239 	}
240 }
241 
flash_ll_write_enable(flash_hw_t * hw)242 static inline void flash_ll_write_enable(flash_hw_t *hw)
243 {
244 	flash_ll_set_op_cmd(hw, FLASH_OP_CMD_WREN);
245 	while (flash_ll_is_busy(hw));
246 }
247 
flash_ll_write_disable(flash_hw_t * hw)248 static inline void flash_ll_write_disable(flash_hw_t *hw)
249 {
250 	flash_ll_set_op_cmd(hw, FLASH_OP_CMD_WRDI);
251 	while (flash_ll_is_busy(hw));
252 }
253 
254 #ifdef __cplusplus
255 }
256 #endif
257 
258 
259