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
35#if defined(CONFIG_FLASH_PAGE_LAYOUT)
37 size_t pages_count; /* count of pages sequence of the same size */
38 size_t pages_size;
39};
40#endif /* CONFIG_FLASH_PAGE_LAYOUT */
41
45
60
68 const size_t write_block_size;
69
71 /* User code should call flash_params_get_ functions on flash_parameters
72 * to get capabilities, rather than accessing object contents directly.
73 */
74 struct {
75 /* Device has no explicit erase, so it either erases on
76 * write or does not require it at all.
77 * This also includes devices that support erase but
78 * do not require it.
79 */
80 bool no_explicit_erase: 1;
81 } caps;
85};
86
88#define FLASH_ERASE_C_EXPLICIT 0x01
92#define FLASH_ERASE_CAPS_UNSET (int)-1
93/* The values below are now reserved but not used */
94#define FLASH_ERASE_C_SUPPORTED 0x02
95#define FLASH_ERASE_C_VAL_BIT 0x04
96#define FLASH_ERASE_UNIFORM_PAGE 0x08
97
112static inline
114{
115#if defined(CONFIG_FLASH_HAS_EXPLICIT_ERASE)
116#if defined(CONFIG_FLASH_HAS_NO_EXPLICIT_ERASE)
117 return (p->caps.no_explicit_erase) ? 0 : FLASH_ERASE_C_EXPLICIT;
118#else
119 ARG_UNUSED(p);
121#endif
122#endif
123 return 0;
124}
125
129
134
147typedef int (*flash_api_read)(const struct device *dev, off_t offset,
148 void *data,
149 size_t len);
158typedef int (*flash_api_write)(const struct device *dev, off_t offset,
159 const void *data, size_t len);
160
174typedef int (*flash_api_erase)(const struct device *dev, off_t offset,
175 size_t size);
176
187typedef int (*flash_api_get_size)(const struct device *dev, uint64_t *size);
188
189typedef const struct flash_parameters* (*flash_api_get_parameters)(const struct device *dev);
190
191#if defined(CONFIG_FLASH_PAGE_LAYOUT)
213typedef void (*flash_api_pages_layout)(const struct device *dev,
214 const struct flash_pages_layout **layout,
215 size_t *layout_size);
216#endif /* CONFIG_FLASH_PAGE_LAYOUT */
217
218typedef int (*flash_api_sfdp_read)(const struct device *dev, off_t offset,
219 void *data, size_t len);
220typedef int (*flash_api_read_jedec_id)(const struct device *dev, uint8_t *id);
221typedef int (*flash_api_ex_op)(const struct device *dev, uint16_t code,
222 const uintptr_t in, void *out);
223
224__subsystem struct flash_driver_api {
230#if defined(CONFIG_FLASH_PAGE_LAYOUT)
232#endif /* CONFIG_FLASH_PAGE_LAYOUT */
233#if defined(CONFIG_FLASH_JESD216_API)
236#endif /* CONFIG_FLASH_JESD216_API */
237#if defined(CONFIG_FLASH_EX_OP_ENABLED)
238 flash_api_ex_op ex_op;
239#endif /* CONFIG_FLASH_EX_OP_ENABLED */
240};
241
245
250
264__syscall int flash_read(const struct device *dev, off_t offset, void *data,
265 size_t len);
266
267static inline int z_impl_flash_read(const struct device *dev, off_t offset,
268 void *data,
269 size_t len)
270{
271 return DEVICE_API_GET(flash, dev)->read(dev, offset, data, len);
272}
273
292__syscall int flash_write(const struct device *dev, off_t offset,
293 const void *data,
294 size_t len);
295
296static inline int z_impl_flash_write(const struct device *dev, off_t offset,
297 const void *data, size_t len)
298{
299 return DEVICE_API_GET(flash, dev)->write(dev, offset, data, len);
300}
301
330__syscall int flash_erase(const struct device *dev, off_t offset, size_t size);
331
332static inline int z_impl_flash_erase(const struct device *dev, off_t offset,
333 size_t size)
334{
335 int rc = -ENOSYS;
336
337 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
338
339 if (api->erase != NULL) {
340 rc = api->erase(dev, offset, size);
341 }
342
343 return rc;
344}
345
359__syscall int flash_get_size(const struct device *dev, uint64_t *size);
360
361static inline int z_impl_flash_get_size(const struct device *dev, uint64_t *size)
362{
363 int rc = -ENOSYS;
364 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
365
366 if (api->get_size != NULL) {
367 rc = api->get_size(dev, size);
368 }
369
370 return rc;
371}
372
388__syscall int flash_fill(const struct device *dev, uint8_t val, off_t offset, size_t size);
389
427__syscall int flash_flatten(const struct device *dev, off_t offset, size_t size);
428
430 off_t start_offset; /* offset from the base of flash address */
431 size_t size;
433};
434
435#if defined(CONFIG_FLASH_PAGE_LAYOUT)
445__syscall int flash_get_page_info_by_offs(const struct device *dev,
446 off_t offset,
447 struct flash_pages_info *info);
448
458__syscall int flash_get_page_info_by_idx(const struct device *dev,
459 uint32_t page_index,
460 struct flash_pages_info *info);
461
469__syscall size_t flash_get_page_count(const struct device *dev);
470
481typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
482
495void flash_page_foreach(const struct device *dev, flash_page_cb cb,
496 void *data);
497#endif /* CONFIG_FLASH_PAGE_LAYOUT */
498
499#if defined(CONFIG_FLASH_JESD216_API)
520__syscall int flash_sfdp_read(const struct device *dev, off_t offset,
521 void *data, size_t len);
522
523static inline int z_impl_flash_sfdp_read(const struct device *dev,
524 off_t offset,
525 void *data, size_t len)
526{
527 int rv = -ENOTSUP;
528 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
529
530 if (api->sfdp_read != NULL) {
531 rv = api->sfdp_read(dev, offset, data, len);
532 }
533 return rv;
534}
535
547__syscall int flash_read_jedec_id(const struct device *dev, uint8_t *id);
548
549static inline int z_impl_flash_read_jedec_id(const struct device *dev,
550 uint8_t *id)
551{
552 int rv = -ENOTSUP;
553 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
554
555 if (api->read_jedec_id != NULL) {
556 rv = api->read_jedec_id(dev, id);
557 }
558 return rv;
559}
560#endif /* CONFIG_FLASH_JESD216_API */
561
573__syscall size_t flash_get_write_block_size(const struct device *dev);
574
575static inline size_t z_impl_flash_get_write_block_size(const struct device *dev)
576{
577 return DEVICE_API_GET(flash, dev)->get_parameters(dev)->write_block_size;
578}
579
580
592__syscall const struct flash_parameters *flash_get_parameters(const struct device *dev);
593
594static inline const struct flash_parameters *z_impl_flash_get_parameters(const struct device *dev)
595{
596 return DEVICE_API_GET(flash, dev)->get_parameters(dev);
597}
598
624__syscall int flash_ex_op(const struct device *dev, uint16_t code,
625 const uintptr_t in, void *out);
626
655__syscall int flash_copy(const struct device *src_dev, off_t src_offset,
656 const struct device *dst_dev, off_t dst_offset, off_t size, uint8_t *buf,
657 size_t buf_size);
658/*
659 * Extended operation interface provides flexible way for supporting flash
660 * controller features. Code space is divided equally into Zephyr codes
661 * (MSb == 0) and vendor codes (MSb == 1). This way we can easily add extended
662 * operations to the drivers without cluttering the API or problems with API
663 * incompatibility. Extended operation can be promoted from vendor codes to
664 * Zephyr codes if the feature is available in most flash controllers and
665 * can be represented in the same way.
666 *
667 * It's not forbidden to have operation in Zephyr codes and vendor codes for
668 * the same functionality. In this case, vendor operation could provide more
669 * specific access when abstraction in Zephyr counterpart is insufficient.
670 */
671#define FLASH_EX_OP_VENDOR_BASE 0x8000
672#define FLASH_EX_OP_IS_VENDOR(c) ((c) & FLASH_EX_OP_VENDOR_BASE)
673
678 /*
679 * Reset flash device.
680 */
682
688
694};
695
710
711static inline int z_impl_flash_ex_op(const struct device *dev, uint16_t code,
712 const uintptr_t in, void *out)
713{
714#if defined(CONFIG_FLASH_EX_OP_ENABLED)
715 const struct flash_driver_api *api = DEVICE_API_GET(flash, dev);
716
717 if (api->ex_op == NULL) {
718 return -ENOTSUP;
719 }
720
721 return api->ex_op(dev, code, in, out);
722#else
723 ARG_UNUSED(dev);
724 ARG_UNUSED(code);
725 ARG_UNUSED(in);
726 ARG_UNUSED(out);
727
728 return -ENOSYS;
729#endif /* CONFIG_FLASH_EX_OP_ENABLED */
730}
731
732#ifdef __cplusplus
733}
734#endif
735
739
740#include <zephyr/syscalls/flash.h>
741
742#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_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:481
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:88
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:699
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:113
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:677
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:703
@ FLASH_BLOCK_BAD
Block is marked as bad.
Definition flash.h:708
@ FLASH_EX_OP_MARK_BAD_BLOCK
Marks a block as bad.
Definition flash.h:693
@ FLASH_EX_OP_RESET
Definition flash.h:681
@ FLASH_EX_OP_IS_BAD_BLOCK
Checks whether a block is marked as bad.
Definition flash.h:687
int(* flash_api_read_jedec_id)(const struct device *dev, uint8_t *id)
Definition flash.h:220
int(* flash_api_erase)(const struct device *dev, off_t offset, size_t size)
Flash erase implementation handler type.
Definition flash.h:174
const struct flash_parameters *(* flash_api_get_parameters)(const struct device *dev)
Definition flash.h:189
int(* flash_api_read)(const struct device *dev, off_t offset, void *data, size_t len)
Flash read implementation handler type.
Definition flash.h:147
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:213
int(* flash_api_get_size)(const struct device *dev, uint64_t *size)
Get device size in bytes.
Definition flash.h:187
int(* flash_api_sfdp_read)(const struct device *dev, off_t offset, void *data, size_t len)
Definition flash.h:218
int(* flash_api_ex_op)(const struct device *dev, uint16_t code, const uintptr_t in, void *out)
Definition flash.h:221
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:158
#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
Definition flash.h:224
flash_api_sfdp_read sfdp_read
Definition flash.h:234
flash_api_get_parameters get_parameters
Definition flash.h:228
flash_api_pages_layout page_layout
Definition flash.h:231
flash_api_read read
Definition flash.h:225
flash_api_get_size get_size
Definition flash.h:229
flash_api_write write
Definition flash.h:226
flash_api_erase erase
Definition flash.h:227
flash_api_read_jedec_id read_jedec_id
Definition flash.h:235
Definition flash.h:429
size_t size
Definition flash.h:431
off_t start_offset
Definition flash.h:430
uint32_t index
Definition flash.h:432
Definition flash.h:36
size_t pages_size
Definition flash.h:38
size_t pages_count
Definition flash.h:37
Flash memory parameters.
Definition flash.h:66
uint8_t erase_value
Value the device is filled in erased areas.
Definition flash.h:84
const size_t write_block_size
Minimal write alignment and size.
Definition flash.h:68