@@ -33,39 +33,11 @@ static volatile struct event_flash g_event_flash = {
3333 .write_complete = false,
3434};
3535
36+ static struct flash_pages_layout flash_ra_layout [5 ];
37+
3638void fcu_frdyi_isr (void );
3739void fcu_fiferr_isr (void );
3840
39- static int flash_controller_ra_init (const struct device * dev );
40- static int flash_ra_init (const struct device * dev );
41- static int flash_ra_erase (const struct device * dev , off_t offset , size_t len );
42- static int flash_ra_read (const struct device * dev , off_t offset , void * data , size_t len );
43- static int flash_ra_write (const struct device * dev , off_t offset , const void * data , size_t len );
44- static const struct flash_parameters * flash_ra_get_parameters (const struct device * dev );
45- #ifdef CONFIG_FLASH_PAGE_LAYOUT
46- void flash_ra_page_layout (const struct device * dev , const struct flash_pages_layout * * layout ,
47- size_t * layout_size );
48- #endif
49-
50- #ifdef CONFIG_FLASH_EX_OP_ENABLED
51- static int flash_ra_ex_op (const struct device * dev , uint16_t code , const uintptr_t in , void * out );
52- #endif
53-
54- static DEVICE_API (flash , flash_ra_api ) = {
55- .erase = flash_ra_erase ,
56- .write = flash_ra_write ,
57- .read = flash_ra_read ,
58- .get_parameters = flash_ra_get_parameters ,
59- #ifdef CONFIG_FLASH_PAGE_LAYOUT
60- .page_layout = flash_ra_page_layout ,
61- #endif
62- #ifdef CONFIG_FLASH_EX_OP_ENABLED
63- .ex_op = flash_ra_ex_op ,
64- #endif
65- };
66-
67- static struct flash_pages_layout flash_ra_layout [5 ];
68-
6941void bgo_callback (flash_callback_args_t * p_args )
7042{
7143 if (FLASH_EVENT_ERASE_COMPLETE == p_args -> event ) {
@@ -75,20 +47,39 @@ void bgo_callback(flash_callback_args_t *p_args)
7547 }
7648}
7749
78- static bool flash_ra_valid_range (off_t area_size , off_t offset , size_t len )
50+ static bool flash_ra_valid_range (struct flash_hp_ra_data * flash_data , off_t offset , size_t len )
7951{
80- if ((offset < 0 ) || (offset >= area_size ) || ((area_size - offset ) < len )) {
52+ #if defined(CONFIG_DUAL_BANK_MODE )
53+ if (flash_data -> FlashRegion == DATA_FLASH ) {
54+ if ((offset < 0 ) || (offset >= flash_data -> area_size ) ||
55+ (flash_data -> area_size - offset < len ) || (len > UINT32_MAX - offset )) {
56+ return false;
57+ }
58+ } else {
59+ if ((offset < 0 ) || (offset >= FLASH_HP_CF_DUAL_HIGH_END_ADDRESS ) ||
60+ (offset >= FLASH_HP_CF_DUAL_LOW_END_ADDRESS &&
61+ offset < FLASH_HP_BANK2_OFFSET ) ||
62+ ((len + offset ) > FLASH_HP_CF_DUAL_HIGH_END_ADDRESS ) ||
63+ ((len + offset ) > FLASH_HP_CF_DUAL_LOW_END_ADDRESS &&
64+ (len + offset ) < FLASH_HP_BANK2_OFFSET ) ||
65+ (len > UINT32_MAX - offset )) {
66+ return false;
67+ }
68+ }
69+ #else
70+ if ((offset < 0 ) || (offset >= flash_data -> area_size ) ||
71+ (flash_data -> area_size - offset < len ) || (len > UINT32_MAX - offset )) {
8172 return false;
8273 }
83-
74+ #endif
8475 return true;
8576}
8677
8778static int flash_ra_read (const struct device * dev , off_t offset , void * data , size_t len )
8879{
8980 struct flash_hp_ra_data * flash_data = dev -> data ;
9081
91- if (!flash_ra_valid_range (flash_data -> area_size , offset , len )) {
82+ if (!flash_ra_valid_range (flash_data , offset , len )) {
9283 return - EINVAL ;
9384 }
9485
@@ -108,11 +99,13 @@ static int flash_ra_erase(const struct device *dev, off_t offset, size_t len)
10899 struct flash_hp_ra_data * flash_data = dev -> data ;
109100 struct flash_hp_ra_controller * dev_ctrl = flash_data -> controller ;
110101 static struct flash_pages_info page_info_off , page_info_len ;
111- fsp_err_t err = FSP_ERR_ASSERTION ;
102+ fsp_err_t err ;
112103 uint32_t block_num ;
113104 int rc , rc2 ;
105+ int key = 0 ;
106+ bool is_contain_end_block = false;
114107
115- if (!flash_ra_valid_range (flash_data -> area_size , offset , len )) {
108+ if (!flash_ra_valid_range (flash_data , offset , len )) {
116109 return - EINVAL ;
117110 }
118111
@@ -132,35 +125,54 @@ static int flash_ra_erase(const struct device *dev, off_t offset, size_t len)
132125 return - EINVAL ;
133126 }
134127
135- rc2 = flash_get_page_info_by_offs (dev , (offset + len ), & page_info_len );
136- if (rc2 != 0 ) {
137- return - EINVAL ;
138- }
139-
128+ if (flash_data -> FlashRegion == CODE_FLASH ) {
140129#if defined(CONFIG_DUAL_BANK_MODE )
141- /* Invalid offset in dual bank mode, this is reversed area. */
142- if ((page_info_off .index > FLASH_HP_CF_BLOCK_32KB_DUAL_LOW_END &&
143- page_info_off .index < FLASH_HP_CF_BLOCK_8KB_HIGH_START ) ||
144- (page_info_len .index > FLASH_HP_CF_BLOCK_32KB_DUAL_LOW_END &&
145- page_info_len .index < FLASH_HP_CF_BLOCK_8KB_HIGH_START )) {
146- return - EIO ;
147- }
130+ if ((offset + len ) == (uint32_t )FLASH_HP_CF_DUAL_HIGH_END_ADDRESS ) {
131+ page_info_len .index = FLASH_HP_CF_BLOCK_32KB_DUAL_HIGH_END + 1 ;
132+ is_contain_end_block = true;
133+ }
134+ #else
135+ if ((offset + len ) == (uint32_t )DT_REG_SIZE (DT_NODELABEL (flash0 ))) {
136+ page_info_len .index = FLASH_HP_CF_BLOCK_32KB_LINEAR_END + 1 ;
137+ is_contain_end_block = true;
138+ }
148139#endif
140+ } else {
141+ if ((offset + len ) == (uint32_t )DT_REG_SIZE (DT_NODELABEL (flash1 ))) {
142+ page_info_len .index = FLASH_HP_DF_BLOCK_END ;
143+ is_contain_end_block = true;
144+ }
145+ }
149146
150- if ((offset + len ) != (page_info_len .start_offset )) {
151- return - EIO ;
147+ if (!is_contain_end_block ) {
148+ rc2 = flash_get_page_info_by_offs (dev , (offset + len ), & page_info_len );
149+ if (rc2 != 0 ) {
150+ return - EINVAL ;
151+ }
152+ if ((offset + len ) != (page_info_len .start_offset )) {
153+ return - EIO ;
154+ }
152155 }
153156
154157 block_num = (uint32_t )((page_info_len .index ) - page_info_off .index );
155158
156159 if (block_num > 0 ) {
157- k_sem_take (& dev_ctrl -> ctrl_sem , K_FOREVER );
160+ if (flash_data -> FlashRegion == CODE_FLASH ) {
161+ /* Disable interrupts during code flash operations */
162+ key = irq_lock ();
163+ } else {
164+ k_sem_take (& dev_ctrl -> ctrl_sem , K_FOREVER );
165+ }
158166
159167 err = R_FLASH_HP_Erase (& dev_ctrl -> flash_ctrl ,
160168 (long )(flash_data -> area_address + offset ), block_num );
161169
162170 if (err != FSP_SUCCESS ) {
163- k_sem_give (& dev_ctrl -> ctrl_sem );
171+ if (flash_data -> FlashRegion == CODE_FLASH ) {
172+ irq_unlock (key );
173+ } else {
174+ k_sem_give (& dev_ctrl -> ctrl_sem );
175+ }
164176 return - EIO ;
165177 }
166178
@@ -174,19 +186,24 @@ static int flash_ra_erase(const struct device *dev, off_t offset, size_t len)
174186 }
175187 }
176188
177- k_sem_give (& dev_ctrl -> ctrl_sem );
189+ if (flash_data -> FlashRegion == CODE_FLASH ) {
190+ irq_unlock (key );
191+ } else {
192+ k_sem_give (& dev_ctrl -> ctrl_sem );
193+ }
178194 }
179195
180196 return 0 ;
181197}
182198
183199static int flash_ra_write (const struct device * dev , off_t offset , const void * data , size_t len )
184200{
185- fsp_err_t err = FSP_ERR_ASSERTION ;
201+ fsp_err_t err ;
186202 struct flash_hp_ra_data * flash_data = dev -> data ;
187203 struct flash_hp_ra_controller * dev_ctrl = flash_data -> controller ;
204+ int key = 0 ;
188205
189- if (!flash_ra_valid_range (flash_data -> area_size , offset , len )) {
206+ if (!flash_ra_valid_range (flash_data , offset , len )) {
190207 return - EINVAL ;
191208 }
192209
@@ -196,13 +213,22 @@ static int flash_ra_write(const struct device *dev, off_t offset, const void *da
196213
197214 LOG_DBG ("flash: write 0x%lx, len: %u" , (long )(offset + flash_data -> area_address ), len );
198215
199- k_sem_take (& dev_ctrl -> ctrl_sem , K_FOREVER );
216+ if (flash_data -> FlashRegion == CODE_FLASH ) {
217+ /* Disable interrupts during code flash operations */
218+ key = irq_lock ();
219+ } else {
220+ k_sem_take (& dev_ctrl -> ctrl_sem , K_FOREVER );
221+ }
200222
201223 err = R_FLASH_HP_Write (& dev_ctrl -> flash_ctrl , (uint32_t )data ,
202224 (long )(offset + flash_data -> area_address ), len );
203225
204226 if (err != FSP_SUCCESS ) {
205- k_sem_give (& dev_ctrl -> ctrl_sem );
227+ if (flash_data -> FlashRegion == CODE_FLASH ) {
228+ irq_unlock (key );
229+ } else {
230+ k_sem_give (& dev_ctrl -> ctrl_sem );
231+ }
206232 return - EIO ;
207233 }
208234
@@ -216,11 +242,24 @@ static int flash_ra_write(const struct device *dev, off_t offset, const void *da
216242 }
217243 }
218244
219- k_sem_give (& dev_ctrl -> ctrl_sem );
245+ if (flash_data -> FlashRegion == CODE_FLASH ) {
246+ irq_unlock (key );
247+ } else {
248+ k_sem_give (& dev_ctrl -> ctrl_sem );
249+ }
250+
251+ return 0 ;
252+ }
253+
254+ static int flash_ra_get_size (const struct device * dev , uint64_t * size )
255+ {
256+ struct flash_hp_ra_data * flash_data = dev -> data ;
257+ * size = (uint64_t )flash_data -> area_size ;
220258
221259 return 0 ;
222260}
223261
262+ #ifdef CONFIG_FLASH_PAGE_LAYOUT
224263void flash_ra_page_layout (const struct device * dev , const struct flash_pages_layout * * layout ,
225264 size_t * layout_size )
226265{
@@ -242,12 +281,12 @@ void flash_ra_page_layout(const struct device *dev, const struct flash_pages_lay
242281 1 ;
243282 flash_ra_layout [1 ].pages_size = FLASH_HP_CF_BLOCK_32KB_SIZE ;
244283
245- flash_ra_layout [2 ].pages_count = FLASH_RESERVED_AREA_NUM ;
284+ flash_ra_layout [2 ].pages_count = FLASH_HP_CF_NUM_BLOCK_RESERVED ;
246285 flash_ra_layout [2 ].pages_size =
247286 (FLASH_HP_BANK2_OFFSET -
248287 (flash_ra_layout [0 ].pages_count * flash_ra_layout [0 ].pages_size ) -
249288 (flash_ra_layout [1 ].pages_count * flash_ra_layout [1 ].pages_size )) /
250- FLASH_RESERVED_AREA_NUM ;
289+ FLASH_HP_CF_NUM_BLOCK_RESERVED ;
251290
252291 flash_ra_layout [3 ].pages_count =
253292 (FLASH_HP_CF_BLOCK_8KB_HIGH_END - FLASH_HP_CF_BLOCK_8KB_HIGH_START ) + 1 ;
@@ -275,6 +314,7 @@ void flash_ra_page_layout(const struct device *dev, const struct flash_pages_lay
275314
276315 * layout = flash_ra_layout ;
277316}
317+ #endif
278318
279319static const struct flash_parameters * flash_ra_get_parameters (const struct device * dev )
280320{
@@ -354,7 +394,7 @@ static void flash_controller_ra_irq_config_func(const struct device *dev)
354394
355395static int flash_controller_ra_init (const struct device * dev )
356396{
357- fsp_err_t err = FSP_SUCCESS ;
397+ fsp_err_t err ;
358398 const struct flash_hp_ra_controller_config * cfg = dev -> config ;
359399 struct flash_hp_ra_controller * data = dev -> data ;
360400
@@ -376,6 +416,20 @@ static struct flash_hp_ra_controller_config flash_hp_ra_controller_config = {
376416 .irq_config = flash_controller_ra_irq_config_func ,
377417};
378418
419+ static DEVICE_API (flash , flash_ra_api ) = {
420+ .erase = flash_ra_erase ,
421+ .write = flash_ra_write ,
422+ .read = flash_ra_read ,
423+ .get_parameters = flash_ra_get_parameters ,
424+ .get_size = flash_ra_get_size ,
425+ #ifdef CONFIG_FLASH_PAGE_LAYOUT
426+ .page_layout = flash_ra_page_layout ,
427+ #endif
428+ #ifdef CONFIG_FLASH_EX_OP_ENABLED
429+ .ex_op = flash_ra_ex_op ,
430+ #endif
431+ };
432+
379433#define RA_FLASH_INIT (index ) \
380434 struct flash_hp_ra_data flash_hp_ra_data_##index = {.area_address = DT_REG_ADDR(index), \
381435 .area_size = DT_REG_SIZE(index)}; \
0 commit comments