Zephyr API Documentation 4.1.99
A Scalable Open Source RTOS
 4.1.99
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
flash.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017-2024 Nordic Semiconductor ASA
3 * Copyright (c) 2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
13#ifndef ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
14#define ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
15
23#include <errno.h>
24
25#include <zephyr/types.h>
26#include <stddef.h>
27#include <sys/types.h>
28#include <zephyr/device.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34#if defined(CONFIG_FLASH_PAGE_LAYOUT)
36 size_t pages_count; /* count of pages sequence of the same size */
37 size_t pages_size;
38};
39#endif /* CONFIG_FLASH_PAGE_LAYOUT */
40
61 const size_t write_block_size;
62
64 /* User code should call flash_params_get_ functions on flash_parameters
65 * to get capabilities, rather than accessing object contents directly.
66 */
67 struct {
68 /* Device has no explicit erase, so it either erases on
69 * write or does not require it at all.
70 * This also includes devices that support erase but
71 * do not require it.
72 */
73 bool no_explicit_erase: 1;
74 } caps;
78};
79
81#define FLASH_ERASE_C_EXPLICIT 0x01
85#define FLASH_ERASE_CAPS_UNSET (int)-1
86/* The values below are now reserved but not used */
87#define FLASH_ERASE_C_SUPPORTED 0x02
88#define FLASH_ERASE_C_VAL_BIT 0x04
89#define FLASH_ERASE_UNIFORM_PAGE 0x08
90
91
92/* @brief Parser for flash_parameters for retrieving erase capabilities
93 *
94 * The functions parses flash_parameters type object and returns combination
95 * of erase capabilities of 0 if device does not have any.
96 * Not that in some cases availability of erase may be dependent on driver
97 * options, so even if by hardware design a device provides some erase
98 * capabilities, the function may return 0 if these been disabled or not
99 * implemented by driver.
100 *
101 * @param p pointer to flash_parameters type object
102 *
103 * @return 0 or combination of FLASH_ERASE_C_ capabilities.
104 */
105static inline
107{
108#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
109#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
110 return (p->caps.no_explicit_erase) ? 0 : FLASH_ERASE_C_EXPLICIT;
111#else
112 ARG_UNUSED(p);
114#endif
115#endif
116 return 0;
117}
118
140typedef int (*flash_api_read)(const struct device *dev, off_t offset,
141 void *data,
142 size_t len);
151typedef int (*flash_api_write)(const struct device *dev, off_t offset,
152 const void *data, size_t len);
153
167typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
168 size_t size);
169
180typedef int (*flash_api_get_size)(const struct device *dev, uint64_t *size);
181
182typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
183
184#if defined(CONFIG_FLASH_PAGE_LAYOUT)
206typedef void (*flash_api_pages_layout)(const struct device *dev,
207 const struct flash_pages_layout **layout,
208 size_t *layout_size);
209#endif /* CONFIG_FLASH_PAGE_LAYOUT */
210
211typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
212 void *data, size_t len);
213typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
214typedef int (*flash_api_ex_op)(const struct device *dev, uint16_t code,
215 const uintptr_t in, void *out);
216
217__subsystem struct flash_driver_api {
223#if defined(CONFIG_FLASH_PAGE_LAYOUT)
225#endif /* CONFIG_FLASH_PAGE_LAYOUT */
226#if defined(CONFIG_FLASH_JESD216_API)
229#endif /* CONFIG_FLASH_JESD216_API */
230#if defined(CONFIG_FLASH_EX_OP_ENABLED)
231 flash_api_ex_op ex_op;
232#endif /* CONFIG_FLASH_EX_OP_ENABLED */
233};
234
257__syscall int flash_read(const struct device *dev, off_t offset, void *data,
258 size_t len);
259
260static inline int z_impl_flash_read(const struct device *dev, off_t offset,
261 void *data,
262 size_t len)
263{
264 const struct flash_driver_api *api =
265 (const struct flash_driver_api *)dev->api;
266
267 return api->read(dev, offset, data, len);
268}
269
288__syscall int flash_write(const struct device *dev, off_t offset,
289 const void *data,
290 size_t len);
291
292static inline int z_impl_flash_write(const struct device *dev, off_t offset,
293 const void *data, size_t len)
294{
295 const struct flash_driver_api *api =
296 (const struct flash_driver_api *)dev->api;
297 int rc;
298
299 rc = api->write(dev, offset, data, len);
300
301 return rc;
302}
303
332__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
333
334static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
335 size_t size)
336{
337 int rc = -ENOSYS;
338
339 const struct flash_driver_api *api =
340 (const struct flash_driver_api *)dev->api;
341
342 if (api->erase != NULL) {
343 rc = api->erase(dev, offset, size);
344 }
345
346 return rc;
347}
348
362__syscall int flash_get_size(const struct device *dev, uint64_t *size);
363
364static inline int z_impl_flash_get_size(const struct device *dev, uint64_t *size)
365{
366 int rc = -ENOSYS;
367 const struct flash_driver_api *api = (const struct flash_driver_api *)dev->api;
368
369 if (api->get_size != NULL) {
370 rc = api->get_size(dev, size);
371 }
372
373 return rc;
374}
375
391__syscall int flash_fill(const struct device *dev, uint8_t val, off_t offset, size_t size);
392
430__syscall int flash_flatten(const struct device *dev, off_t offset, size_t size);
431
433 off_t start_offset; /* offset from the base of flash address */
434 size_t size;
436};
437
438#if defined(CONFIG_FLASH_PAGE_LAYOUT)
448__syscall int flash_get_page_info_by_offs(const struct device *dev,
449 off_t offset,
450 struct flash_pages_info *info);
451
461__syscall int flash_get_page_info_by_idx(const struct device *dev,
462 uint32_t page_index,
463 struct flash_pages_info *info);
464
472__syscall size_t flash_get_page_count(const struct device *dev);
473
484typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
485
498void flash_page_foreach(const struct device *dev, flash_page_cb cb,
499 void *data);
500#endif /* CONFIG_FLASH_PAGE_LAYOUT */
501
502#if defined(CONFIG_FLASH_JESD216_API)
523__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
524 void *data, size_t len);
525
526static inline int z_impl_flash_sfdp_read(const struct device *dev,
527 off_t offset,
528 void *data, size_t len)
529{
530 int rv = -ENOTSUP;
531 const struct flash_driver_api *api =
532 (const struct flash_driver_api *)dev->api;
533
534 if (api->sfdp_read != NULL) {
535 rv = api->sfdp_read(dev, offset, data, len);
536 }
537 return rv;
538}
539
551__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
552
553static inline int z_impl_flash_read_jedec_id(const struct device *dev,
554 uint8_t *id)
555{
556 int rv = -ENOTSUP;
557 const struct flash_driver_api *api =
558 (const struct flash_driver_api *)dev->api;
559
560 if (api->read_jedec_id != NULL) {
561 rv = api->read_jedec_id(dev, id);
562 }
563 return rv;
564}
565#endif /* CONFIG_FLASH_JESD216_API */
566
578__syscall size_t flash_get_write_block_size(const struct device *dev);
579
580static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
581{
582 const struct flash_driver_api *api =
583 (const struct flash_driver_api *)dev->api;
584
585 return api->get_parameters(dev)->write_block_size;
586}
587
588
600__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
601
602static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
603{
604 const struct flash_driver_api *api =
605 (const struct flash_driver_api *)dev->api;
606
607 return api->get_parameters(dev);
608}
609
635__syscall int flash_ex_op(const struct device *dev, uint16_t code,
636 const uintptr_t in, void *out);
637
666__syscall int flash_copy(const struct device *src_dev, off_t src_offset,
667 const struct device *dst_dev, off_t dst_offset, off_t size, uint8_t *buf,
668 size_t buf_size);
669/*
670 * Extended operation interface provides flexible way for supporting flash
671 * controller features. Code space is divided equally into Zephyr codes
672 * (MSb == 0) and vendor codes (MSb == 1). This way we can easily add extended
673 * operations to the drivers without cluttering the API or problems with API
674 * incompatibility. Extended operation can be promoted from vendor codes to
675 * Zephyr codes if the feature is available in most flash controllers and
676 * can be represented in the same way.
677 *
678 * It's not forbidden to have operation in Zephyr codes and vendor codes for
679 * the same functionality. In this case, vendor operation could provide more
680 * specific access when abstraction in Zephyr counterpart is insufficient.
681 */
682#define FLASH_EX_OP_VENDOR_BASE 0x8000
683#define FLASH_EX_OP_IS_VENDOR(c) ((c) & FLASH_EX_OP_VENDOR_BASE)
684
689 /*
690 * Reset flash device.
691 */
693};
694
695static inline int z_impl_flash_ex_op(const struct device *dev, uint16_t code,
696 const uintptr_t in, void *out)
697{
698#if defined(CONFIG_FLASH_EX_OP_ENABLED)
699 const struct flash_driver_api *api =
700 (const struct flash_driver_api *)dev->api;
701
702 if (api->ex_op == NULL) {
703 return -ENOTSUP;
704 }
705
706 return api->ex_op(dev, code, in, out);
707#else
708 ARG_UNUSED(dev);
709 ARG_UNUSED(code);
710 ARG_UNUSED(in);
711 ARG_UNUSED(out);
712
713 return -ENOSYS;
714#endif /* CONFIG_FLASH_EX_OP_ENABLED */
715}
716
717#ifdef __cplusplus
718}
719#endif
720
725#include <zephyr/syscalls/flash.h>
726
727#endif /* ZEPHYR_INCLUDE_DRIVERS_FLASH_H_ */
Incorrect memory layout
Definition bindesc.h:314
System error numbers.
int flash_fill(const struct device *dev, uint8_t val, off_t offset, size_t size)
Fill selected range of device with specified value.
int flash_erase(const struct device *dev, off_t offset, size_t size)
Erase part or all of a flash memory.
const struct flash_parameters * flash_get_parameters(const struct device *dev)
Get pointer to flash_parameters structure.
int flash_flatten(const struct device *dev, off_t offset, size_t size)
Erase part or all of a flash memory or level it.
void flash_page_foreach(const struct device *dev, flash_page_cb cb, void *data)
Iterate over all flash pages on a device.
bool(* flash_page_cb)(const struct flash_pages_info *info, void *data)
Callback type for iterating over flash pages present on a device.
Definition flash.h:484
int flash_get_size(const struct device *dev, uint64_t *size)
Get device size in bytes.
int flash_ex_op(const struct device *dev, uint16_t code, const uintptr_t in, void *out)
Execute flash extended operation on given device.
#define FLASH_ERASE_C_EXPLICIT
Set for ordinary Flash where erase is needed before write of random data.
Definition flash.h:81
int flash_write(const struct device *dev, off_t offset, const void *data, size_t len)
Write buffer into flash memory.
int flash_copy(const struct device *src_dev, off_t src_offset, const struct device *dst_dev, off_t dst_offset, off_t size, uint8_t *buf, size_t buf_size)
Copy flash memory from one device to another.
int flash_sfdp_read(const struct device *dev, off_t offset, void *data, size_t len)
Read data from Serial Flash Discoverable Parameters.
int flash_read(const struct device *dev, off_t offset, void *data, size_t len)
Read data from flash.
size_t flash_get_write_block_size(const struct device *dev)
Get the minimum write block size supported by the driver.
int flash_get_page_info_by_idx(const struct device *dev, uint32_t page_index, struct flash_pages_info *info)
Get the size and start offset of flash page of certain index.
static int flash_params_get_erase_cap(const struct flash_parameters *p)
Definition flash.h:106
int flash_read_jedec_id(const struct device *dev, uint8_t *id)
Read the JEDEC ID from a compatible flash device.
flash_ex_op_types
Enumeration for extra flash operations.
Definition flash.h:688
size_t flash_get_page_count(const struct device *dev)
Get the total number of flash pages.
int flash_get_page_info_by_offs(const struct device *dev, off_t offset, struct flash_pages_info *info)
Get the size and start offset of flash page at certain flash offset.
@ FLASH_EX_OP_RESET
Definition flash.h:692
int(* flash_api_read_jedec_id)(const struct device *dev, uint8_t *id)
Definition flash.h:213
int(* flash_api_erase)(const struct device *dev, off_t offset, size_t size)
Flash erase implementation handler type.
Definition flash.h:167
const struct flash_parameters *(* flash_api_get_parameters)(const struct device *dev)
Definition flash.h:182
int(* flash_api_read)(const struct device *dev, off_t offset, void *data, size_t len)
Flash read implementation handler type.
Definition flash.h:140
void(* flash_api_pages_layout)(const struct device *dev, const struct flash_pages_layout **layout, size_t *layout_size)
Retrieve a flash device's layout.
Definition flash.h:206
int(* flash_api_get_size)(const struct device *dev, uint64_t *size)
Get device size in bytes.
Definition flash.h:180
int(* flash_api_sfdp_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition flash.h:211
int(* flash_api_ex_op)(const struct device *dev, uint16_t code, const uintptr_t in, void *out)
Definition flash.h:214
int(* flash_api_write)(const struct device *dev, off_t offset, const void *data, size_t len)
Flash write implementation handler type.
Definition flash.h:151
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define ENOTSUP
Unsupported value.
Definition errno.h:114
#define NULL
Definition iar_missing_defs.h:20
__INTPTR_TYPE__ off_t
Definition types.h:36
#define bool
Definition stdbool.h:13
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:510
void * data
Address of the device instance private data.
Definition device.h:520
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
Definition flash.h:217
flash_api_sfdp_read sfdp_read
Definition flash.h:227
flash_api_get_parameters get_parameters
Definition flash.h:221
flash_api_pages_layout page_layout
Definition flash.h:224
flash_api_read read
Definition flash.h:218
flash_api_get_size get_size
Definition flash.h:222
flash_api_write write
Definition flash.h:219
flash_api_erase erase
Definition flash.h:220
flash_api_read_jedec_id read_jedec_id
Definition flash.h:228
Definition flash.h:432
size_t size
Definition flash.h:434
off_t start_offset
Definition flash.h:433
uint32_t index
Definition flash.h:435
Definition flash.h:35
size_t pages_size
Definition flash.h:37
size_t pages_count
Definition flash.h:36
Flash memory parameters.
Definition flash.h:59
uint8_t erase_value
Value the device is filled in erased areas.
Definition flash.h:77
const size_t write_block_size
Minimal write alignment and size.
Definition flash.h:61