Zephyr Project API 4.4.99
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 } caps;
86};
87
89#define FLASH_ERASE_C_EXPLICIT 0x01
93#define FLASH_ERASE_CAPS_UNSET (int)-1
94/* The values below are now reserved but not used */
96#define FLASH_ERASE_C_SUPPORTED 0x02
98#define FLASH_ERASE_C_VAL_BIT 0x04
100#define FLASH_ERASE_UNIFORM_PAGE 0x08
101
116static inline
118{
119#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
120#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
121 return (p->caps.no_explicit_erase) ? 0 : FLASH_ERASE_C_EXPLICIT;
122#else
123 ARG_UNUSED(p);
125#endif
126#else
127 ARG_UNUSED(p);
128#endif
129 return 0;
130}
131
135
141
154typedef int (*flash_api_read)(const struct device *dev, off_t offset,
155 void *data,
156 size_t len);
165typedef int (*flash_api_write)(const struct device *dev, off_t offset,
166 const void *data, size_t len);
167
181typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
182 size_t size);
183
184#if defined(CONFIG_FLASH_HAS_DRIVER_FILL)
204typedef int (*flash_api_fill)(const struct device *dev, uint8_t val,
205 off_t offset, size_t size);
206#endif /* CONFIG_FLASH_HAS_DRIVER_FILL */
207
218typedef int (*flash_api_get_size)(const struct device *dev, uint64_t *size);
219
226typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
227
249typedef void (*flash_api_pages_layout)(const struct device *dev,
250 const struct flash_pages_layout **layout,
251 size_t *layout_size);
252
257typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
258 void *data, size_t len);
263typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
268typedef int (*flash_api_ex_op)(const struct device *dev, uint16_t code,
269 const uintptr_t in, void *out);
270
274__subsystem struct flash_driver_api {
281#if defined(CONFIG_FLASH_HAS_DRIVER_FILL)
283 flash_api_fill fill;
284#endif /* CONFIG_FLASH_HAS_DRIVER_FILL */
289#if defined(CONFIG_FLASH_PAGE_LAYOUT) || defined(__DOXYGEN__)
295#endif /* CONFIG_FLASH_PAGE_LAYOUT */
296#if defined(CONFIG_FLASH_JESD216_API) || defined(__DOXYGEN__)
307#endif /* CONFIG_FLASH_JESD216_API */
308#if defined(CONFIG_FLASH_EX_OP_ENABLED) || defined(__DOXYGEN__)
314#endif /* CONFIG_FLASH_EX_OP_ENABLED */
315};
316
318
323
337__syscall int flash_read(const struct device *dev, off_t offset, void *data,
338 size_t len);
339
340static inline int z_impl_flash_read(const struct device *dev, off_t offset,
341 void *data,
342 size_t len)
343{
344 return DEVICE_API_GET(flash, dev)->read(dev, offset, data, len);
345}
346
365__syscall int flash_write(const struct device *dev, off_t offset,
366 const void *data,
367 size_t len);
368
369static inline int z_impl_flash_write(const struct device *dev, off_t offset,
370 const void *data, size_t len)
371{
372 return DEVICE_API_GET(flash, dev)->write(dev, offset, data, len);
373}
374
403__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
404
405static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
406 size_t size)
407{
408 int rc = -ENOSYS;
409
410 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
411
412 if (api->erase != NULL) {
413 rc = api->erase(dev, offset, size);
414 }
415
416 return rc;
417}
418
432__syscall int flash_get_size(const struct device *dev, uint64_t *size);
433
434static inline int z_impl_flash_get_size(const struct device *dev, uint64_t *size)
435{
436 int rc = -ENOSYS;
437 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
438
439 if (api->get_size != NULL) {
440 rc = api->get_size(dev, size);
441 }
442
443 return rc;
444}
445
461__syscall int flash_fill(const struct device *dev, uint8_t val, off_t offset, size_t size);
462
500__syscall int flash_flatten(const struct device *dev, off_t offset, size_t size);
501
510
511#if defined(CONFIG_FLASH_PAGE_LAYOUT) || defined(__DOXYGEN__)
523__syscall int flash_get_page_info_by_offs(const struct device *dev,
524 off_t offset,
525 struct flash_pages_info *info);
526
538__syscall int flash_get_page_info_by_idx(const struct device *dev,
539 uint32_t page_index,
540 struct flash_pages_info *info);
541
551__syscall size_t flash_get_page_count(const struct device *dev);
552
565typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
566
581void flash_page_foreach(const struct device *dev, flash_page_cb cb,
582 void *data);
583#endif /* CONFIG_FLASH_PAGE_LAYOUT */
584
585#if defined(CONFIG_FLASH_JESD216_API) || defined(__DOXYGEN__)
603__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
604 void *data, size_t len);
605
606static inline int z_impl_flash_sfdp_read(const struct device *dev,
607 off_t offset,
608 void *data, size_t len)
609{
610 int rv = -ENOTSUP;
611 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
612
613 if (api->sfdp_read != NULL) {
614 rv = api->sfdp_read(dev, offset, data, len);
615 }
616 return rv;
617}
618
631__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
632
633static inline int z_impl_flash_read_jedec_id(const struct device *dev,
634 uint8_t *id)
635{
636 int rv = -ENOTSUP;
637 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
638
639 if (api->read_jedec_id != NULL) {
640 rv = api->read_jedec_id(dev, id);
641 }
642 return rv;
643}
644#endif /* CONFIG_FLASH_JESD216_API */
645
657__syscall size_t flash_get_write_block_size(const struct device *dev);
658
659static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
660{
661 return DEVICE_API_GET(flash, dev)->get_parameters(dev)->write_block_size;
662}
663
664
676__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
677
678static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
679{
680 return DEVICE_API_GET(flash, dev)->get_parameters(dev);
681}
682
709__syscall int flash_ex_op(const struct device *dev, uint16_t code,
710 const uintptr_t in, void *out);
711
740__syscall int flash_copy(const struct device *src_dev, off_t src_offset,
741 const struct device *dst_dev, off_t dst_offset, off_t size, uint8_t *buf,
742 size_t buf_size);
743/*
744 * Extended operation interface provides flexible way for supporting flash
745 * controller features. Code space is divided equally into Zephyr codes
746 * (MSb == 0) and vendor codes (MSb == 1). This way we can easily add extended
747 * operations to the drivers without cluttering the API or problems with API
748 * incompatibility. Extended operation can be promoted from vendor codes to
749 * Zephyr codes if the feature is available in most flash controllers and
750 * can be represented in the same way.
751 *
752 * It's not forbidden to have operation in Zephyr codes and vendor codes for
753 * the same functionality. In this case, vendor operation could provide more
754 * specific access when abstraction in Zephyr counterpart is insufficient.
755 */
757#define FLASH_EX_OP_VENDOR_BASE 0x8000
764#define FLASH_EX_OP_IS_VENDOR(c) ((c) & FLASH_EX_OP_VENDOR_BASE)
765
787
802
803static inline int z_impl_flash_ex_op(const struct device *dev, uint16_t code,
804 const uintptr_t in, void *out)
805{
806#if defined(CONFIG_FLASH_EX_OP_ENABLED)
807 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
808
809 if (api->ex_op == NULL) {
810 return -ENOTSUP;
811 }
812
813 return api->ex_op(dev, code, in, out);
814#else
815 ARG_UNUSED(dev);
816 ARG_UNUSED(code);
817 ARG_UNUSED(in);
818 ARG_UNUSED(out);
819
820 return -ENOSYS;
821#endif /* CONFIG_FLASH_EX_OP_ENABLED */
822}
823
824#ifdef __cplusplus
825}
826#endif
827
831
832#include <zephyr/syscalls/flash.h>
833
834#endif /* ZEPHYR_INCLUDE_DRIVERS_FLASH_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1449
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_api_read_jedec_id)(const struct device *dev, uint8_t *id)
Read the JEDEC ID of the device.
Definition flash.h:263
int flash_flatten(const struct device *dev, off_t offset, size_t size)
Erase part or all of a flash memory or level it.
int(* flash_api_erase)(const struct device *dev, off_t offset, size_t size)
Flash erase implementation handler type.
Definition flash.h:181
void flash_page_foreach(const struct device *dev, flash_page_cb cb, void *data)
Iterate over all flash pages on a device.
const struct flash_parameters *(* flash_api_get_parameters)(const struct device *dev)
Get device parameters.
Definition flash.h:226
int(* flash_api_read)(const struct device *dev, off_t offset, void *data, size_t len)
@def_driverbackendgroup{Flash,flash_interface}
Definition flash.h:154
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:565
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.
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:249
#define FLASH_ERASE_C_EXPLICIT
Set for ordinary Flash where erase is needed before write of random data.
Definition flash.h:89
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:791
int(* flash_api_get_size)(const struct device *dev, uint64_t *size)
Get device size in bytes.
Definition flash.h:218
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:117
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:257
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:268
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:769
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:165
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:795
@ FLASH_BLOCK_BAD
Block is marked as bad.
Definition flash.h:800
@ FLASH_EX_OP_MARK_BAD_BLOCK
Marks a block as bad.
Definition flash.h:785
@ FLASH_EX_OP_RESET
Reset flash device.
Definition flash.h:773
@ FLASH_EX_OP_IS_BAD_BLOCK
Checks whether a block is marked as bad.
Definition flash.h:779
#define ENOSYS
Function not implemented.
Definition errno.h:83
#define ENOTSUP
Unsupported value.
Definition errno.h:115
#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:513
void * data
Address of the device instance private data.
Definition device.h:523
@driver_ops{Flash}
Definition flash.h:274
flash_api_sfdp_read sfdp_read
@driver_ops_optional Read data from Serial Flash Discoverable Parameters.
Definition flash.h:301
flash_api_get_parameters get_parameters
@driver_ops_mandatory Get pointer to flash_parameters structure.
Definition flash.h:286
flash_api_pages_layout page_layout
@driver_ops_mandatory Retrieve a flash device's layout.
Definition flash.h:294
flash_api_ex_op ex_op
@driver_ops_optional Vendor-specific extended operations for flash drivers.
Definition flash.h:313
flash_api_read read
@driver_ops_mandatory Read data from flash.
Definition flash.h:276
flash_api_get_size get_size
@driver_ops_optional Get device size in bytes.
Definition flash.h:288
flash_api_write write
@driver_ops_mandatory Write buffer into flash memory.
Definition flash.h:278
flash_api_erase erase
@driver_ops_optional Erase part or all of a flash memory.
Definition flash.h:280
flash_api_read_jedec_id read_jedec_id
@driver_ops_optional Read the JEDEC ID from a compatible flash device.
Definition flash.h:306
Information about a flash page.
Definition flash.h:505
size_t size
Size of the page in bytes.
Definition flash.h:507
off_t start_offset
Offset of the page from the base of the flash address space.
Definition flash.h:506
uint32_t index
Index of the page, counted from 0.
Definition flash.h:508
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:85
const size_t write_block_size
Minimal write alignment and size.
Definition flash.h:69