Zephyr API Documentation 4.5.0-rc1
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
14#ifndef ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
15#define ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
16
23
24#include <errno.h>
25
26#include <zephyr/types.h>
27#include <stddef.h>
28#include <sys/types.h>
29#include <zephyr/device.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
39 size_t pages_count;
40 size_t pages_size;
41};
42
46
61
69 const size_t write_block_size;
70
72 /* User code should call flash_params_get_ functions on flash_parameters
73 * to get capabilities, rather than accessing object contents directly.
74 */
75 struct {
76 /* Device has no explicit erase, so it either erases on
77 * write or does not require it at all.
78 * This also includes devices that support erase but
79 * do not require it.
80 */
81 bool no_explicit_erase: 1;
82
83 /* Erased area does not read back as erase_value, for example
84 * because the read path decrypts what is stored.
85 */
86 bool erase_value_readback_unsupported: 1;
87 } caps;
91};
92
94#define FLASH_ERASE_C_EXPLICIT 0x01
98#define FLASH_ERASE_CAPS_UNSET (int)-1
99/* The values below are now reserved but not used */
101#define FLASH_ERASE_C_SUPPORTED 0x02
103#define FLASH_ERASE_C_VAL_BIT 0x04
105#define FLASH_ERASE_UNIFORM_PAGE 0x08
106
121static inline
123{
124#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
125#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
126 return (p->caps.no_explicit_erase) ? 0 : FLASH_ERASE_C_EXPLICIT;
127#else
128 ARG_UNUSED(p);
130#endif
131#else
132 ARG_UNUSED(p);
133#endif
134 return 0;
135}
136
149static inline bool flash_params_erase_value_readable(const struct flash_parameters *p)
150{
151 return !p->caps.erase_value_readback_unsupported;
152}
153
157
163
176typedef int (*flash_api_read)(const struct device *dev, off_t offset,
177 void *data,
178 size_t len);
187typedef int (*flash_api_write)(const struct device *dev, off_t offset,
188 const void *data, size_t len);
189
203typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
204 size_t size);
205
206#if defined(CONFIG_FLASH_HAS_DRIVER_FILL)
226typedef int (*flash_api_fill)(const struct device *dev, uint8_t val,
227 off_t offset, size_t size);
228#endif /* CONFIG_FLASH_HAS_DRIVER_FILL */
229
240typedef int (*flash_api_get_size)(const struct device *dev, uint64_t *size);
241
248typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
249
271typedef void (*flash_api_pages_layout)(const struct device *dev,
272 const struct flash_pages_layout **layout,
273 size_t *layout_size);
274
279typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
280 void *data, size_t len);
285typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
290typedef int (*flash_api_ex_op)(const struct device *dev, uint16_t code,
291 const uintptr_t in, void *out);
292
296__subsystem struct flash_driver_api {
303#if defined(CONFIG_FLASH_HAS_DRIVER_FILL)
305 flash_api_fill fill;
306#endif /* CONFIG_FLASH_HAS_DRIVER_FILL */
311#if defined(CONFIG_FLASH_PAGE_LAYOUT) || defined(__DOXYGEN__)
317#endif /* CONFIG_FLASH_PAGE_LAYOUT */
318#if defined(CONFIG_FLASH_JESD216_API) || defined(__DOXYGEN__)
329#endif /* CONFIG_FLASH_JESD216_API */
330#if defined(CONFIG_FLASH_EX_OP_ENABLED) || defined(__DOXYGEN__)
336#endif /* CONFIG_FLASH_EX_OP_ENABLED */
337};
338
340
345
359__syscall int flash_read(const struct device *dev, off_t offset, void *data,
360 size_t len);
361
362static inline int z_impl_flash_read(const struct device *dev, off_t offset,
363 void *data,
364 size_t len)
365{
366 return DEVICE_API_GET(flash, dev)->read(dev, offset, data, len);
367}
368
387__syscall int flash_write(const struct device *dev, off_t offset,
388 const void *data,
389 size_t len);
390
391static inline int z_impl_flash_write(const struct device *dev, off_t offset,
392 const void *data, size_t len)
393{
394 return DEVICE_API_GET(flash, dev)->write(dev, offset, data, len);
395}
396
425__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
426
427static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
428 size_t size)
429{
430 int rc = -ENOSYS;
431
432 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
433
434 if (api->erase != NULL) {
435 rc = api->erase(dev, offset, size);
436 }
437
438 return rc;
439}
440
454__syscall int flash_get_size(const struct device *dev, uint64_t *size);
455
456static inline int z_impl_flash_get_size(const struct device *dev, uint64_t *size)
457{
458 int rc = -ENOSYS;
459 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
460
461 if (api->get_size != NULL) {
462 rc = api->get_size(dev, size);
463 }
464
465 return rc;
466}
467
483__syscall int flash_fill(const struct device *dev, uint8_t val, off_t offset, size_t size);
484
522__syscall int flash_flatten(const struct device *dev, off_t offset, size_t size);
523
532
533#if defined(CONFIG_FLASH_PAGE_LAYOUT) || defined(__DOXYGEN__)
545__syscall int flash_get_page_info_by_offs(const struct device *dev,
546 off_t offset,
547 struct flash_pages_info *info);
548
560__syscall int flash_get_page_info_by_idx(const struct device *dev,
561 uint32_t page_index,
562 struct flash_pages_info *info);
563
573__syscall size_t flash_get_page_count(const struct device *dev);
574
587typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
588
603void flash_page_foreach(const struct device *dev, flash_page_cb cb,
604 void *data);
605#endif /* CONFIG_FLASH_PAGE_LAYOUT */
606
607#if defined(CONFIG_FLASH_JESD216_API) || defined(__DOXYGEN__)
625__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
626 void *data, size_t len);
627
628static inline int z_impl_flash_sfdp_read(const struct device *dev,
629 off_t offset,
630 void *data, size_t len)
631{
632 int rv = -ENOTSUP;
633 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
634
635 if (api->sfdp_read != NULL) {
636 rv = api->sfdp_read(dev, offset, data, len);
637 }
638 return rv;
639}
640
653__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
654
655static inline int z_impl_flash_read_jedec_id(const struct device *dev,
656 uint8_t *id)
657{
658 int rv = -ENOTSUP;
659 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
660
661 if (api->read_jedec_id != NULL) {
662 rv = api->read_jedec_id(dev, id);
663 }
664 return rv;
665}
666#endif /* CONFIG_FLASH_JESD216_API */
667
679__syscall size_t flash_get_write_block_size(const struct device *dev);
680
681static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
682{
683 return DEVICE_API_GET(flash, dev)->get_parameters(dev)->write_block_size;
684}
685
686
698__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
699
700static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
701{
702 return DEVICE_API_GET(flash, dev)->get_parameters(dev);
703}
704
731__syscall int flash_ex_op(const struct device *dev, uint16_t code,
732 const uintptr_t in, void *out);
733
762__syscall int flash_copy(const struct device *src_dev, off_t src_offset,
763 const struct device *dst_dev, off_t dst_offset, off_t size, uint8_t *buf,
764 size_t buf_size);
765/*
766 * Extended operation interface provides flexible way for supporting flash
767 * controller features. Code space is divided equally into Zephyr codes
768 * (MSb == 0) and vendor codes (MSb == 1). This way we can easily add extended
769 * operations to the drivers without cluttering the API or problems with API
770 * incompatibility. Extended operation can be promoted from vendor codes to
771 * Zephyr codes if the feature is available in most flash controllers and
772 * can be represented in the same way.
773 *
774 * It's not forbidden to have operation in Zephyr codes and vendor codes for
775 * the same functionality. In this case, vendor operation could provide more
776 * specific access when abstraction in Zephyr counterpart is insufficient.
777 */
779#define FLASH_EX_OP_VENDOR_BASE 0x8000
786#define FLASH_EX_OP_IS_VENDOR(c) ((c) & FLASH_EX_OP_VENDOR_BASE)
787
809
824
825static inline int z_impl_flash_ex_op(const struct device *dev, uint16_t code,
826 const uintptr_t in, void *out)
827{
828#if defined(CONFIG_FLASH_EX_OP_ENABLED)
829 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
830
831 if (api->ex_op == NULL) {
832 return -ENOTSUP;
833 }
834
835 return api->ex_op(dev, code, in, out);
836#else
837 ARG_UNUSED(dev);
838 ARG_UNUSED(code);
839 ARG_UNUSED(in);
840 ARG_UNUSED(out);
841
842 return -ENOSYS;
843#endif /* CONFIG_FLASH_EX_OP_ENABLED */
844}
845
846#ifdef __cplusplus
847}
848#endif
849
853
854#include <zephyr/syscalls/flash.h>
855
856#endif /* ZEPHYR_INCLUDE_DRIVERS_FLASH_H_ */
APIs and macros for the Zephyr device model.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1486
System error numbers.
int(* flash_api_read_jedec_id)(const struct device *dev, uint8_t *id)
Read the JEDEC ID of the device.
Definition flash.h:285
int(* flash_api_erase)(const struct device *dev, off_t offset, size_t size)
Flash erase implementation handler type.
Definition flash.h:203
const struct flash_parameters *(* flash_api_get_parameters)(const struct device *dev)
Get device parameters.
Definition flash.h:248
int(* flash_api_read)(const struct device *dev, off_t offset, void *data, size_t len)
Flash read implementation handler type.
Definition flash.h:176
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:271
int(* flash_api_get_size)(const struct device *dev, uint64_t *size)
Get device size in bytes.
Definition flash.h:240
int(* flash_api_sfdp_read)(const struct device *dev, off_t offset, void *data, size_t len)
Read data from Serial Flash Discoverable Parameters.
Definition flash.h:279
int(* flash_api_ex_op)(const struct device *dev, uint16_t code, const uintptr_t in, void *out)
Perform an extended operation.
Definition flash.h:290
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:187
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.
static bool flash_params_erase_value_readable(const struct flash_parameters *p)
Check if erased area reads back as the erase value.
Definition flash.h:149
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:587
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:94
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.
flash_block_status
Enumeration for flash block status.
Definition flash.h:813
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)
Parser for flash_parameters for retrieving erase capabilities.
Definition flash.h:122
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:791
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_BLOCK_GOOD
Block is functional.
Definition flash.h:817
@ FLASH_BLOCK_BAD
Block is marked as bad.
Definition flash.h:822
@ FLASH_EX_OP_MARK_BAD_BLOCK
Marks a block as bad.
Definition flash.h:807
@ FLASH_EX_OP_RESET
Reset flash device.
Definition flash.h:795
@ FLASH_EX_OP_IS_BAD_BLOCK
Checks whether a block is marked as bad.
Definition flash.h:801
#define ENOSYS
Function not implemented.
Definition errno.h:83
#define ENOTSUP
Unsupported value.
Definition errno.h:115
__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:543
void * data
Address of the device instance private data.
Definition device.h:553
<span class="mlabel">Driver Operations</span> Flash driver operations
Definition flash.h:296
flash_api_sfdp_read sfdp_read
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition flash.h:323
flash_api_get_parameters get_parameters
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:308
flash_api_pages_layout page_layout
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:316
flash_api_ex_op ex_op
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition flash.h:335
flash_api_read read
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:298
flash_api_get_size get_size
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition flash.h:310
flash_api_write write
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:300
flash_api_erase erase
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition flash.h:302
flash_api_read_jedec_id read_jedec_id
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition flash.h:328
Information about a flash page.
Definition flash.h:527
size_t size
Size of the page in bytes.
Definition flash.h:529
off_t start_offset
Offset of the page from the base of the flash address space.
Definition flash.h:528
uint32_t index
Index of the page, counted from 0.
Definition flash.h:530
Describes a sequence of flash pages of the same size.
Definition flash.h:38
size_t pages_size
Size of each page in the sequence, in bytes.
Definition flash.h:40
size_t pages_count
Number of pages of the same size in the sequence.
Definition flash.h:39
Flash memory parameters.
Definition flash.h:67
uint8_t erase_value
Value the device is filled in erased areas.
Definition flash.h:90
const size_t write_block_size
Minimal write alignment and size.
Definition flash.h:69