1 /*
2  * Copyright (c) 2022 BrainCo Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_FLASH_FLASH_GD32_H_
8 #define ZEPHYR_DRIVERS_FLASH_FLASH_GD32_H_
9 
10 #include <stdint.h>
11 #include <zephyr/devicetree.h>
12 #include <zephyr/drivers/flash.h>
13 
14 #define SOC_NV_FLASH_NODE	DT_INST(0, soc_nv_flash)
15 #define SOC_NV_FLASH_SIZE	DT_REG_SIZE(SOC_NV_FLASH_NODE)
16 #define SOC_NV_FLASH_ADDR	DT_REG_ADDR(SOC_NV_FLASH_NODE)
17 #define SOC_NV_FLASH_PRG_SIZE	DT_PROP(SOC_NV_FLASH_NODE, write_block_size)
18 
19 #if (4 == SOC_NV_FLASH_PRG_SIZE)
20 typedef uint32_t flash_prg_t;
21 #elif (2 == SOC_NV_FLASH_PRG_SIZE)
22 typedef uint16_t flash_prg_t;
23 #elif (1 == SOC_NV_FLASH_PRG_SIZE)
24 typedef uint8_t flash_prg_t;
25 #else
26 #error "Invalid write-block-size value in FMC DTS"
27 #endif
28 
29 /* Helper for conditional compilation directives, KB cannot be used because it has type casting. */
30 #define PRE_KB(x) ((x) << 10)
31 
32 bool flash_gd32_valid_range(off_t offset, uint32_t len, bool write);
33 
34 int flash_gd32_write_range(off_t offset, const void *data, size_t len);
35 
36 int flash_gd32_erase_block(off_t offset, size_t size);
37 
38 #ifdef CONFIG_FLASH_PAGE_LAYOUT
39 void flash_gd32_pages_layout(const struct device *dev,
40 			     const struct flash_pages_layout **layout,
41 			     size_t *layout_size);
42 #endif
43 #endif /* ZEPHYR_DRIVERS_FLASH_FLASH_GD32_H_ */
44