Zephyr API 3.6.99
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
14#ifndef ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
15#define ZEPHYR_INCLUDE_DRIVERS_FLASH_H_
16
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
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
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
176#if defined(CONFIG_FLASH_HAS_DRIVER_FILL)
196typedef int (*flash_api_fill)(const struct device *dev, uint8_t val,
197 off_t offset, size_t size);
198#endif /* CONFIG_FLASH_HAS_DRIVER_FILL */
199
210typedef int (*flash_api_get_size)(const struct device *dev, uint64_t *size);
211
218typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
219
241typedef void (*flash_api_pages_layout)(const struct device *dev,
242 const struct flash_pages_layout **layout,
243 size_t *layout_size);
244
245typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
246 void *data, size_t len);
247typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
248typedef int (*flash_api_ex_op)(const struct device *dev, uint16_t code,
249 const uintptr_t in, void *out);
250
254__subsystem struct flash_driver_api {
261#if defined(CONFIG_FLASH_HAS_DRIVER_FILL)
263 flash_api_fill fill;
264#endif /* CONFIG_FLASH_HAS_DRIVER_FILL */
269#if defined(CONFIG_FLASH_PAGE_LAYOUT) || defined(__DOXYGEN__)
275#endif /* CONFIG_FLASH_PAGE_LAYOUT */
276#if defined(CONFIG_FLASH_JESD216_API) || defined(__DOXYGEN__)
287#endif /* CONFIG_FLASH_JESD216_API */
288#if defined(CONFIG_FLASH_EX_OP_ENABLED) || defined(__DOXYGEN__)
294#endif /* CONFIG_FLASH_EX_OP_ENABLED */
295};
296
317__syscall int flash_read(const struct device *dev, off_t offset, void *data,
318 size_t len);
319
320static inline int z_impl_flash_read(const struct device *dev, off_t offset,
321 void *data,
322 size_t len)
323{
324 return DEVICE_API_GET(flash, dev)->read(dev, offset, data, len);
325}
326
345__syscall int flash_write(const struct device *dev, off_t offset,
346 const void *data,
347 size_t len);
348
349static inline int z_impl_flash_write(const struct device *dev, off_t offset,
350 const void *data, size_t len)
351{
352 return DEVICE_API_GET(flash, dev)->write(dev, offset, data, len);
353}
354
383__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
384
385static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
386 size_t size)
387{
388 int rc = -ENOSYS;
389
390 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
391
392 if (api->erase != NULL) {
393 rc = api->erase(dev, offset, size);
394 }
395
396 return rc;
397}
398
412__syscall int flash_get_size(const struct device *dev, uint64_t *size);
413
414static inline int z_impl_flash_get_size(const struct device *dev, uint64_t *size)
415{
416 int rc = -ENOSYS;
417 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
418
419 if (api->get_size != NULL) {
420 rc = api->get_size(dev, size);
421 }
422
423 return rc;
424}
425
441__syscall int flash_fill(const struct device *dev, uint8_t val, off_t offset, size_t size);
442
480__syscall int flash_flatten(const struct device *dev, off_t offset, size_t size);
481
483 off_t start_offset; /* offset from the base of flash address */
484 size_t size;
486};
487
488#if defined(CONFIG_FLASH_PAGE_LAYOUT) || defined(__DOXYGEN__)
500__syscall int flash_get_page_info_by_offs(const struct device *dev,
501 off_t offset,
502 struct flash_pages_info *info);
503
515__syscall int flash_get_page_info_by_idx(const struct device *dev,
516 uint32_t page_index,
517 struct flash_pages_info *info);
518
528__syscall size_t flash_get_page_count(const struct device *dev);
529
542typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
543
558void flash_page_foreach(const struct device *dev, flash_page_cb cb,
559 void *data);
560#endif /* CONFIG_FLASH_PAGE_LAYOUT */
561
562#if defined(CONFIG_FLASH_JESD216_API) || defined(__DOXYGEN__)
581__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
582 void *data, size_t len);
583
584static inline int z_impl_flash_sfdp_read(const struct device *dev,
585 off_t offset,
586 void *data, size_t len)
587{
588 int rv = -ENOTSUP;
589 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
590
591 if (api->sfdp_read != NULL) {
592 rv = api->sfdp_read(dev, offset, data, len);
593 }
594 return rv;
595}
596
610__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
611
612static inline int z_impl_flash_read_jedec_id(const struct device *dev,
613 uint8_t *id)
614{
615 int rv = -ENOTSUP;
616 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
617
618 if (api->read_jedec_id != NULL) {
619 rv = api->read_jedec_id(dev, id);
620 }
621 return rv;
622}
623#endif /* CONFIG_FLASH_JESD216_API */
624
636__syscall size_t flash_get_write_block_size(const struct device *dev);
637
638static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
639{
640 return DEVICE_API_GET(flash, dev)->get_parameters(dev)->write_block_size;
641}
642
643
655__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
656
657static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
658{
659 return DEVICE_API_GET(flash, dev)->get_parameters(dev);
660}
661
689__syscall int flash_ex_op(const struct device *dev, uint16_t code,
690 const uintptr_t in, void *out);
691
720__syscall int flash_copy(const struct device *src_dev, off_t src_offset,
721 const struct device *dst_dev, off_t dst_offset, off_t size, uint8_t *buf,
722 size_t buf_size);
723/*
724 * Extended operation interface provides flexible way for supporting flash
725 * controller features. Code space is divided equally into Zephyr codes
726 * (MSb == 0) and vendor codes (MSb == 1). This way we can easily add extended
727 * operations to the drivers without cluttering the API or problems with API
728 * incompatibility. Extended operation can be promoted from vendor codes to
729 * Zephyr codes if the feature is available in most flash controllers and
730 * can be represented in the same way.
731 *
732 * It's not forbidden to have operation in Zephyr codes and vendor codes for
733 * the same functionality. In this case, vendor operation could provide more
734 * specific access when abstraction in Zephyr counterpart is insufficient.
735 */
736#define FLASH_EX_OP_VENDOR_BASE 0x8000
737#define FLASH_EX_OP_IS_VENDOR(c) ((c) & FLASH_EX_OP_VENDOR_BASE)
738
743 /*
744 * Reset flash device.
745 */
747
753
759};
760
775
776static inline int z_impl_flash_ex_op(const struct device *dev, uint16_t code,
777 const uintptr_t in, void *out)
778{
779#if defined(CONFIG_FLASH_EX_OP_ENABLED)
780 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
781
782 if (api->ex_op == NULL) {
783 return -ENOTSUP;
784 }
785
786 return api->ex_op(dev, code, in, out);
787#else
788 ARG_UNUSED(dev);
789 ARG_UNUSED(code);
790 ARG_UNUSED(in);
791 ARG_UNUSED(out);
792
793 return -ENOSYS;
794#endif /* CONFIG_FLASH_EX_OP_ENABLED */
795}
796
797#ifdef __cplusplus
798}
799#endif
800
805#include <zephyr/syscalls/flash.h>
806
807#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:1375
System error numbers.
int(* flash_api_read_jedec_id)(const struct device *dev, uint8_t *id)
Definition flash.h:247
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:218
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:241
int(* flash_api_get_size)(const struct device *dev, uint64_t *size)
Get device size in bytes.
Definition flash.h:210
int(* flash_api_sfdp_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition flash.h:245
int(* flash_api_ex_op)(const struct device *dev, uint16_t code, const uintptr_t in, void *out)
Definition flash.h:248
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:542
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:764
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:742
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:768
@ FLASH_BLOCK_BAD
Block is marked as bad.
Definition flash.h:773
@ FLASH_EX_OP_MARK_BAD_BLOCK
Marks a block as bad.
Definition flash.h:758
@ FLASH_EX_OP_RESET
Definition flash.h:746
@ FLASH_EX_OP_IS_BAD_BLOCK
Checks whether a block is marked as bad.
Definition flash.h:752
#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:254
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:281
flash_api_get_parameters get_parameters
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:266
flash_api_pages_layout page_layout
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:274
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:293
flash_api_read read
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:256
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:268
flash_api_write write
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition flash.h:258
flash_api_erase erase
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition flash.h:260
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:286
Definition flash.h:482
size_t size
Definition flash.h:484
off_t start_offset
Definition flash.h:483
uint32_t index
Definition flash.h:485
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