1 /*
2  * Copyright (c) 2024 Renesas Electronics Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_FLASH_RA_HP_H_
8 #define ZEPHYR_DRIVERS_FLASH_RA_HP_H_
9 
10 #include <zephyr/drivers/flash.h>
11 #include <instances/r_flash_hp.h>
12 #include <api/r_flash_api.h>
13 #include <zephyr/drivers/flash/ra_flash_api_extensions.h>
14 
15 #define CHECK_EQ(val1, val2)                 ((val1) == (val2) ? 1 : 0)
16 #define GET_SIZE(COND, value, default_value) ((COND) ? (value) : (default_value))
17 
18 #define FLASH_HP_BANK2_OFFSET                                                                      \
19 	(BSP_FEATURE_FLASH_HP_CF_DUAL_BANK_START - BSP_FEATURE_FLASH_CODE_FLASH_START)
20 
21 #define FLASH_HP_CF_BLOCK_8KB_SIZE  BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE
22 #define FLASH_HP_CF_BLOCK_32KB_SIZE BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE
23 #define FLASH_HP_DF_BLOCK_SIZE      BSP_FEATURE_FLASH_HP_DF_BLOCK_SIZE
24 #define FLASH_HP_DF_START           BSP_FEATURE_FLASH_DATA_FLASH_START
25 
26 #define FLASH_HP_CF_BLOCK_8KB_LOW_START  (0)
27 #define FLASH_HP_CF_BLOCK_8KB_LOW_END    (7)
28 #define FLASH_HP_CF_BLOCK_8KB_HIGH_START (70)
29 #define FLASH_HP_CF_BLOCK_8KB_HIGH_END   (77)
30 
31 #define FLASH_HP_CF_BLOCK_32KB_LINEAR_START (8)
32 
33 #define FLASH_HP_CF_BLOCK_32KB_DUAL_LOW_START  (8)
34 #define FLASH_HP_CF_BLOCK_32KB_DUAL_HIGH_START (78)
35 
36 #if defined(CONFIG_SOC_R7FA8M1AHECBD) || defined(CONFIG_SOC_R7FA8D1BHECBD) ||                      \
37 	defined(CONFIG_SOC_R7FA8T1AHECBD)
38 #define FLASH_RESERVED_AREA_NUM              (33)
39 #define FLASH_HP_CF_BLOCK_32KB_LINEAR_END    (68)
40 #define FLASH_HP_CF_BLOCK_32KB_DUAL_LOW_END  (36)
41 #define FLASH_HP_CF_BLOCK_32KB_DUAL_HIGH_END (106)
42 #endif
43 
44 #if defined(CONFIG_FLASH_EX_OP_ENABLED)
45 #define FLASH_HP_FCU_CONFIG_SET_BPS     (0x1300A1C0U)
46 #define FLASH_HP_FCU_CONFIG_SET_BPS_SEC (0x0300A240U)
47 #define FLASH_HP_FCU_CONFIG_SET_BPS_SEL (0x0300A2C0U)
48 
49 #define FLASH_HP_FCU_CONFIG_SET_PBPS     (0x1300A1E0U)
50 #define FLASH_HP_FCU_CONFIG_SET_PBPS_SEC (0x0300A260U)
51 #endif /* CONFIG_FLASH_EX_OP_ENABLED */
52 
53 /* Zero based offset into g_configuration_area_data[] for BPS */
54 #define FLASH_HP_FCU_CONFIG_SET_BPS_OFFSET (0U)
55 
56 enum flash_region {
57 	CODE_FLASH,
58 	DATA_FLASH,
59 };
60 
61 typedef void (*irq_config_func_t)(const struct device *dev);
62 
63 struct flash_hp_ra_controller {
64 	struct st_flash_hp_instance_ctrl flash_ctrl;
65 	struct k_sem ctrl_sem;
66 	struct st_flash_cfg fsp_config;
67 };
68 
69 struct flash_hp_ra_controller_config {
70 	irq_config_func_t irq_config;
71 };
72 
73 struct flash_hp_ra_data {
74 	struct flash_hp_ra_controller *controller;
75 	enum flash_region FlashRegion;
76 	uint32_t area_address;
77 	uint32_t area_size;
78 };
79 
80 struct flash_hp_ra_config {
81 	struct flash_parameters flash_ra_parameters;
82 };
83 
84 struct event_flash {
85 	volatile bool erase_complete;
86 	volatile bool write_complete;
87 };
88 
89 #if defined(CONFIG_FLASH_RA_WRITE_PROTECT)
90 int flash_ra_ex_op_write_protect(const struct device *dev, const uintptr_t in, void *out);
91 #endif /*CONFIG_FLASH_RA_WRITE_PROTECT*/
92 
93 #endif /* ZEPHYR_DRIVERS_FLASH_RA_HP_H_ */
94