Zephyr API Documentation 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
36 size_t pages_count; /* count of pages sequence of the same size */
37 size_t pages_size;
38};
39
43
58
66 const size_t write_block_size;
67
69 /* User code should call flash_params_get_ functions on flash_parameters
70 * to get capabilities, rather than accessing object contents directly.
71 */
72 struct {
73 /* Device has no explicit erase, so it either erases on
74 * write or does not require it at all.
75 * This also includes devices that support erase but
76 * do not require it.
77 */
78 bool no_explicit_erase: 1;
79 } caps;
83};
84
86#define FLASH_ERASE_C_EXPLICIT 0x01
90#define FLASH_ERASE_CAPS_UNSET (int)-1
91/* The values below are now reserved but not used */
92#define FLASH_ERASE_C_SUPPORTED 0x02
93#define FLASH_ERASE_C_VAL_BIT 0x04
94#define FLASH_ERASE_UNIFORM_PAGE 0x08
95
110static inline
112{
113#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
114#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
115 return (p->caps.no_explicit_erase) ? 0 : FLASH_ERASE_C_EXPLICIT;
116#else
117 ARG_UNUSED(p);
119#endif
120#endif
121 return 0;
122}
123
127
133
146typedef int (*flash_api_read)(const struct device *dev, off_t offset,
147 void *data,
148 size_t len);
157typedef int (*flash_api_write)(const struct device *dev, off_t offset,
158 const void *data, size_t len);
159
173typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
174 size_t size);
175
186typedef int (*flash_api_get_size)(const struct device *dev, uint64_t *size);
187
194typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
195
217typedef void (*flash_api_pages_layout)(const struct device *dev,
218 const struct flash_pages_layout **layout,
219 size_t *layout_size);
220
221typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
222 void *data, size_t len);
223typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
224typedef int (*flash_api_ex_op)(const struct device *dev, uint16_t code,
225 const uintptr_t in, void *out);
226
230__subsystem struct flash_driver_api {
241#if defined(CONFIG_FLASH_PAGE_LAYOUT) || defined(__DOXYGEN__)
247#endif /* CONFIG_FLASH_PAGE_LAYOUT */
248#if defined(CONFIG_FLASH_JESD216_API) || defined(__DOXYGEN__)
259#endif /* CONFIG_FLASH_JESD216_API */
260#if defined(CONFIG_FLASH_EX_OP_ENABLED) || defined(__DOXYGEN__)
266#endif /* CONFIG_FLASH_EX_OP_ENABLED */
267};
268
270
275
289__syscall int flash_read(const struct device *dev, off_t offset, void *data,
290 size_t len);
291
292static inline int z_impl_flash_read(const struct device *dev, off_t offset,
293 void *data,
294 size_t len)
295{
296 return DEVICE_API_GET(flash, dev)->read(dev, offset, data, len);
297}
298
317__syscall int flash_write(const struct device *dev, off_t offset,
318 const void *data,
319 size_t len);
320
321static inline int z_impl_flash_write(const struct device *dev, off_t offset,
322 const void *data, size_t len)
323{
324 return DEVICE_API_GET(flash, dev)->write(dev, offset, data, len);
325}
326
355__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
356
357static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
358 size_t size)
359{
360 int rc = -ENOSYS;
361
362 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
363
364 if (api->erase != NULL) {
365 rc = api->erase(dev, offset, size);
366 }
367
368 return rc;
369}
370
384__syscall int flash_get_size(const struct device *dev, uint64_t *size);
385
386static inline int z_impl_flash_get_size(const struct device *dev, uint64_t *size)
387{
388 int rc = -ENOSYS;
389 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
390
391 if (api->get_size != NULL) {
392 rc = api->get_size(dev, size);
393 }
394
395 return rc;
396}
397
413__syscall int flash_fill(const struct device *dev, uint8_t val, off_t offset, size_t size);
414
452__syscall int flash_flatten(const struct device *dev, off_t offset, size_t size);
453
455 off_t start_offset; /* offset from the base of flash address */
456 size_t size;
458};
459
460#if defined(CONFIG_FLASH_PAGE_LAYOUT) || defined(__DOXYGEN__)
472__syscall int flash_get_page_info_by_offs(const struct device *dev,
473 off_t offset,
474 struct flash_pages_info *info);
475
487__syscall int flash_get_page_info_by_idx(const struct device *dev,
488 uint32_t page_index,
489 struct flash_pages_info *info);
490
500__syscall size_t flash_get_page_count(const struct device *dev);
501
514typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
515
530void flash_page_foreach(const struct device *dev, flash_page_cb cb,
531 void *data);
532#endif /* CONFIG_FLASH_PAGE_LAYOUT */
533
534#if defined(CONFIG_FLASH_JESD216_API) || defined(__DOXYGEN__)
553__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
554 void *data, size_t len);
555
556static inline int z_impl_flash_sfdp_read(const struct device *dev,
557 off_t offset,
558 void *data, size_t len)
559{
560 int rv = -ENOTSUP;
561 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
562
563 if (api->sfdp_read != NULL) {
564 rv = api->sfdp_read(dev, offset, data, len);
565 }
566 return rv;
567}
568
582__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
583
584static inline int z_impl_flash_read_jedec_id(const struct device *dev,
585 uint8_t *id)
586{
587 int rv = -ENOTSUP;
588 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
589
590 if (api->read_jedec_id != NULL) {
591 rv = api->read_jedec_id(dev, id);
592 }
593 return rv;
594}
595#endif /* CONFIG_FLASH_JESD216_API */
596
608__syscall size_t flash_get_write_block_size(const struct device *dev);
609
610static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
611{
612 return DEVICE_API_GET(flash, dev)->get_parameters(dev)->write_block_size;
613}
614
615
627__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
628
629static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
630{
631 return DEVICE_API_GET(flash, dev)->get_parameters(dev);
632}
633
661__syscall int flash_ex_op(const struct device *dev, uint16_t code,
662 const uintptr_t in, void *out);
663
692__syscall int flash_copy(const struct device *src_dev, off_t src_offset,
693 const struct device *dst_dev, off_t dst_offset, off_t size, uint8_t *buf,
694 size_t buf_size);
695/*
696 * Extended operation interface provides flexible way for supporting flash
697 * controller features. Code space is divided equally into Zephyr codes
698 * (MSb == 0) and vendor codes (MSb == 1). This way we can easily add extended
699 * operations to the drivers without cluttering the API or problems with API
700 * incompatibility. Extended operation can be promoted from vendor codes to
701 * Zephyr codes if the feature is available in most flash controllers and
702 * can be represented in the same way.
703 *
704 * It's not forbidden to have operation in Zephyr codes and vendor codes for
705 * the same functionality. In this case, vendor operation could provide more
706 * specific access when abstraction in Zephyr counterpart is insufficient.
707 */
708#define FLASH_EX_OP_VENDOR_BASE 0x8000
709#define FLASH_EX_OP_IS_VENDOR(c) ((c) & FLASH_EX_OP_VENDOR_BASE)
710
715 /*
716 * Reset flash device.
717 */
719
725
731};
732
747
748static inline int z_impl_flash_ex_op(const struct device *dev, uint16_t code,
749 const uintptr_t in, void *out)
750{
751#if defined(CONFIG_FLASH_EX_OP_ENABLED)
752 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
753
754 if (api->ex_op == NULL) {
755 return -ENOTSUP;
756 }
757
758 return api->ex_op(dev, code, in, out);
759#else
760 ARG_UNUSED(dev);
761 ARG_UNUSED(code);
762 ARG_UNUSED(in);
763 ARG_UNUSED(out);
764
765 return -ENOSYS;
766#endif /* CONFIG_FLASH_EX_OP_ENABLED */
767}
768
769#ifdef __cplusplus
770}
771#endif
772
776
777#include <zephyr/syscalls/flash.h>
778
779#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:1425
System error numbers.
int(* flash_api_read_jedec_id)(const struct device *dev, uint8_t *id)
Definition flash.h:223
int(* flash_api_erase)(const struct device *dev, off_t offset, size_t size)
Flash erase implementation handler type.
Definition flash.h:173
const struct flash_parameters *(* flash_api_get_parameters)(const struct device *dev)
Get device parameters.
Definition flash.h:194
int(* flash_api_read)(const struct device *dev, off_t offset, void *data, size_t len)
Flash read implementation handler type.
Definition flash.h:146
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:217
int(* flash_api_get_size)(const struct device *dev, uint64_t *size)
Get device size in bytes.
Definition flash.h:186
int(* flash_api_sfdp_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition flash.h:221
int(* flash_api_ex_op)(const struct device *dev, uint16_t code, const uintptr_t in, void *out)
Definition flash.h:224
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:157
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:514
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:86
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:736
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:111
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:714
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:740
@ FLASH_BLOCK_BAD
Block is marked as bad.
Definition flash.h:745
@ FLASH_EX_OP_MARK_BAD_BLOCK
Marks a block as bad.
Definition flash.h:730
@ FLASH_EX_OP_RESET
Definition flash.h:718
@ FLASH_EX_OP_IS_BAD_BLOCK
Checks whether a block is marked as bad.
Definition flash.h:724
#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:513
void * data
Address of the device instance private data.
Definition device.h:523
<span class="mlabel">Driver Operations</span> Flash driver operations
Definition flash.h:230
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:253
flash_api_get_parameters get_parameters
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:238
flash_api_pages_layout page_layout
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:246
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:265
flash_api_read read
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:232
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:240
flash_api_write write
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:234
flash_api_erase erase
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition flash.h:236
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:258
Definition flash.h:454
size_t size
Definition flash.h:456
off_t start_offset
Definition flash.h:455
uint32_t index
Definition flash.h:457
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:64
uint8_t erase_value
Value the device is filled in erased areas.
Definition flash.h:82
const size_t write_block_size
Minimal write alignment and size.
Definition flash.h:66