2016-02-19 02:52:19 +08:00
|
|
|
/*
|
2017-07-17 18:03:10 +08:00
|
|
|
* Copyright (c) 2017 Nordic Semiconductor ASA
|
2016-02-19 02:52:19 +08:00
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-02-19 02:52:19 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Public API for FLASH drivers
|
|
|
|
*/
|
|
|
|
|
2018-09-15 01:43:44 +08:00
|
|
|
#ifndef ZEPHYR_INCLUDE_FLASH_H_
|
|
|
|
#define ZEPHYR_INCLUDE_FLASH_H_
|
2016-02-19 02:52:19 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FLASH Interface
|
|
|
|
* @defgroup flash_interface FLASH Interface
|
|
|
|
* @ingroup io_interfaces
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
Introduce new sized integer typedefs
This is a start to move away from the C99 {u}int{8,16,32,64}_t types to
Zephyr defined u{8,16,32,64}_t and s{8,16,32,64}_t. This allows Zephyr
to define the sized types in a consistent manor across all the
architectures we support and not conflict with what various compilers
and libc might do with regards to the C99 types.
We introduce <zephyr/types.h> as part of this and have it include
<stdint.h> for now until we transition all the code away from the C99
types.
We go with u{8,16,32,64}_t and s{8,16,32,64}_t as there are some
existing variables defined u8 & u16 as well as to be consistent with
Zephyr naming conventions.
Jira: ZEP-2051
Change-Id: I451fed0623b029d65866622e478225dfab2c0ca8
Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2017-04-19 23:32:08 +08:00
|
|
|
#include <zephyr/types.h>
|
2016-02-19 02:52:19 +08:00
|
|
|
#include <stddef.h>
|
2016-03-10 02:40:32 +08:00
|
|
|
#include <sys/types.h>
|
2016-02-19 02:52:19 +08:00
|
|
|
#include <device.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2017-07-17 18:03:10 +08:00
|
|
|
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
|
|
|
|
struct flash_pages_layout {
|
|
|
|
size_t pages_count; /* count of pages sequence of the same size */
|
|
|
|
size_t pages_size;
|
|
|
|
};
|
|
|
|
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
|
|
|
|
|
2016-03-10 02:40:32 +08:00
|
|
|
typedef int (*flash_api_read)(struct device *dev, off_t offset, void *data,
|
|
|
|
size_t len);
|
|
|
|
typedef int (*flash_api_write)(struct device *dev, off_t offset,
|
2016-03-10 02:11:26 +08:00
|
|
|
const void *data, size_t len);
|
2016-03-10 02:40:32 +08:00
|
|
|
typedef int (*flash_api_erase)(struct device *dev, off_t offset, size_t size);
|
2016-02-19 02:52:19 +08:00
|
|
|
typedef int (*flash_api_write_protection)(struct device *dev, bool enable);
|
|
|
|
|
2017-07-17 18:03:10 +08:00
|
|
|
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
|
|
|
|
/**
|
|
|
|
* @brief Retrieve a flash device's layout.
|
|
|
|
*
|
|
|
|
* A flash device layout is a run-length encoded description of the
|
2017-11-23 04:25:49 +08:00
|
|
|
* pages on the device. (Here, "page" means the smallest erasable
|
|
|
|
* area on the flash device.)
|
2017-07-17 18:03:10 +08:00
|
|
|
*
|
2017-11-16 02:18:51 +08:00
|
|
|
* For flash memories which have uniform page sizes, this routine
|
2017-07-17 18:03:10 +08:00
|
|
|
* returns an array of length 1, which specifies the page size and
|
|
|
|
* number of pages in the memory.
|
|
|
|
*
|
2017-11-16 02:18:51 +08:00
|
|
|
* Layouts for flash memories with nonuniform page sizes will be
|
2017-07-17 18:03:10 +08:00
|
|
|
* returned as an array with multiple elements, each of which
|
2017-11-16 02:18:51 +08:00
|
|
|
* describes a group of pages that all have the same size. In this
|
2017-07-17 18:03:10 +08:00
|
|
|
* case, the sequence of array elements specifies the order in which
|
|
|
|
* these groups occur on the device.
|
|
|
|
*
|
|
|
|
* @param dev Flash device whose layout to retrieve.
|
|
|
|
* @param layout The flash layout will be returned in this argument.
|
|
|
|
* @param layout_size The number of elements in the returned layout.
|
|
|
|
*/
|
|
|
|
typedef void (*flash_api_pages_layout)(struct device *dev,
|
|
|
|
const struct flash_pages_layout **layout,
|
|
|
|
size_t *layout_size);
|
|
|
|
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
|
|
|
|
|
2016-02-19 02:52:19 +08:00
|
|
|
struct flash_driver_api {
|
|
|
|
flash_api_read read;
|
|
|
|
flash_api_write write;
|
|
|
|
flash_api_erase erase;
|
|
|
|
flash_api_write_protection write_protection;
|
2017-07-17 18:03:10 +08:00
|
|
|
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
|
|
|
|
flash_api_pages_layout page_layout;
|
|
|
|
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
|
2017-09-22 15:55:43 +08:00
|
|
|
const size_t write_block_size;
|
2016-02-19 02:52:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read data from flash
|
|
|
|
*
|
|
|
|
* @param dev : flash dev
|
|
|
|
* @param offset : Offset (byte aligned) to read
|
|
|
|
* @param data : Buffer to store read data
|
2016-03-10 02:11:26 +08:00
|
|
|
* @param len : Number of bytes to read.
|
2016-02-19 02:52:19 +08:00
|
|
|
*
|
2016-03-10 22:47:06 +08:00
|
|
|
* @return 0 on success, negative errno code on fail.
|
2016-02-19 02:52:19 +08:00
|
|
|
*/
|
2017-10-26 03:36:51 +08:00
|
|
|
__syscall int flash_read(struct device *dev, off_t offset, void *data,
|
|
|
|
size_t len);
|
|
|
|
|
|
|
|
static inline int _impl_flash_read(struct device *dev, off_t offset, void *data,
|
2016-03-10 02:11:26 +08:00
|
|
|
size_t len)
|
2016-02-19 02:52:19 +08:00
|
|
|
{
|
2016-10-22 17:03:28 +08:00
|
|
|
const struct flash_driver_api *api = dev->driver_api;
|
2016-02-19 02:52:19 +08:00
|
|
|
|
2016-03-10 02:11:26 +08:00
|
|
|
return api->read(dev, offset, data, len);
|
2016-02-19 02:52:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Write buffer into flash memory.
|
|
|
|
*
|
2016-05-24 07:54:11 +08:00
|
|
|
* Prior to the invocation of this API, the flash_write_protection_set needs
|
|
|
|
* to be called first to disable the write protection.
|
|
|
|
*
|
2016-02-19 02:52:19 +08:00
|
|
|
* @param dev : flash device
|
|
|
|
* @param offset : starting offset for the write
|
|
|
|
* @param data : data to write
|
2016-03-10 02:11:26 +08:00
|
|
|
* @param len : Number of bytes to write
|
2016-02-19 02:52:19 +08:00
|
|
|
*
|
2016-03-10 22:47:06 +08:00
|
|
|
* @return 0 on success, negative errno code on fail.
|
2016-02-19 02:52:19 +08:00
|
|
|
*/
|
2017-10-26 03:36:51 +08:00
|
|
|
__syscall int flash_write(struct device *dev, off_t offset, const void *data,
|
|
|
|
size_t len);
|
|
|
|
|
|
|
|
static inline int _impl_flash_write(struct device *dev, off_t offset,
|
|
|
|
const void *data, size_t len)
|
2016-02-19 02:52:19 +08:00
|
|
|
{
|
2016-10-22 17:03:28 +08:00
|
|
|
const struct flash_driver_api *api = dev->driver_api;
|
2016-02-19 02:52:19 +08:00
|
|
|
|
2016-03-10 02:11:26 +08:00
|
|
|
return api->write(dev, offset, data, len);
|
2016-02-19 02:52:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Erase part or all of a flash memory
|
|
|
|
*
|
2016-05-24 07:54:11 +08:00
|
|
|
* Acceptable values of erase size and offset are subject to
|
2017-11-16 02:18:51 +08:00
|
|
|
* hardware-specific multiples of page size and offset. Please check
|
|
|
|
* the API implemented by the underlying sub driver, for example by
|
|
|
|
* using flash_get_page_info_by_offs() if that is supported by your
|
|
|
|
* flash driver.
|
2016-05-24 07:54:11 +08:00
|
|
|
*
|
|
|
|
* Prior to the invocation of this API, the flash_write_protection_set needs
|
|
|
|
* to be called first to disable the write protection.
|
2016-02-19 02:52:19 +08:00
|
|
|
*
|
|
|
|
* @param dev : flash device
|
|
|
|
* @param offset : erase area starting offset
|
|
|
|
* @param size : size of area to be erased
|
|
|
|
*
|
2016-03-10 22:47:06 +08:00
|
|
|
* @return 0 on success, negative errno code on fail.
|
2017-11-16 02:18:51 +08:00
|
|
|
*
|
|
|
|
* @see flash_get_page_info_by_offs()
|
|
|
|
* @see flash_get_page_info_by_idx()
|
2016-02-19 02:52:19 +08:00
|
|
|
*/
|
2017-10-26 03:36:51 +08:00
|
|
|
__syscall int flash_erase(struct device *dev, off_t offset, size_t size);
|
|
|
|
|
|
|
|
static inline int _impl_flash_erase(struct device *dev, off_t offset,
|
|
|
|
size_t size)
|
2016-02-19 02:52:19 +08:00
|
|
|
{
|
2016-10-22 17:03:28 +08:00
|
|
|
const struct flash_driver_api *api = dev->driver_api;
|
2016-02-19 02:52:19 +08:00
|
|
|
|
|
|
|
return api->erase(dev, offset, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enable or disable write protection for a flash memory
|
|
|
|
*
|
2016-05-24 07:54:11 +08:00
|
|
|
* This API is required to be called before the invocation of write or erase
|
|
|
|
* API. Please note that on some flash components, the write protection is
|
|
|
|
* automatically turned on again by the device after the completion of each
|
|
|
|
* write or erase calls. Therefore, on those flash parts, write protection needs
|
|
|
|
* to be disabled before each invocation of the write or erase API. Please refer
|
|
|
|
* to the sub-driver API or the data sheet of the flash component to get details
|
|
|
|
* on the write protection behavior.
|
|
|
|
*
|
2016-02-19 02:52:19 +08:00
|
|
|
* @param dev : flash device
|
|
|
|
* @param enable : enable or disable flash write protection
|
|
|
|
*
|
2016-03-10 22:47:06 +08:00
|
|
|
* @return 0 on success, negative errno code on fail.
|
2016-02-19 02:52:19 +08:00
|
|
|
*/
|
2017-10-26 03:36:51 +08:00
|
|
|
__syscall int flash_write_protection_set(struct device *dev, bool enable);
|
|
|
|
|
|
|
|
static inline int _impl_flash_write_protection_set(struct device *dev,
|
|
|
|
bool enable)
|
2016-02-19 02:52:19 +08:00
|
|
|
{
|
2016-10-22 17:03:28 +08:00
|
|
|
const struct flash_driver_api *api = dev->driver_api;
|
2016-02-19 02:52:19 +08:00
|
|
|
|
|
|
|
return api->write_protection(dev, enable);
|
|
|
|
}
|
|
|
|
|
2017-07-17 18:03:10 +08:00
|
|
|
struct flash_pages_info {
|
|
|
|
off_t start_offset; /* offset from the base of flash address */
|
|
|
|
size_t size;
|
|
|
|
u32_t index;
|
|
|
|
};
|
|
|
|
|
2017-10-26 03:36:51 +08:00
|
|
|
#if defined(CONFIG_FLASH_PAGE_LAYOUT)
|
2017-07-17 18:03:10 +08:00
|
|
|
/**
|
2017-11-23 04:25:49 +08:00
|
|
|
* @brief Get the size and start offset of flash page at certain flash offset.
|
2017-07-17 18:03:10 +08:00
|
|
|
*
|
|
|
|
* @param dev flash device
|
|
|
|
* @param offset Offset within the page
|
|
|
|
* @param info Page Info structure to be filled
|
|
|
|
*
|
|
|
|
* @return 0 on success, -EINVAL if page of the offset doesn't exist.
|
|
|
|
*/
|
2017-10-26 03:36:51 +08:00
|
|
|
__syscall int flash_get_page_info_by_offs(struct device *dev, off_t offset,
|
|
|
|
struct flash_pages_info *info);
|
2017-07-17 18:03:10 +08:00
|
|
|
|
|
|
|
/**
|
2017-11-23 04:25:49 +08:00
|
|
|
* @brief Get the size and start offset of flash page of certain index.
|
2017-07-17 18:03:10 +08:00
|
|
|
*
|
|
|
|
* @param dev flash device
|
|
|
|
* @param page_index Index of the page. Index are counted from 0.
|
|
|
|
* @param info Page Info structure to be filled
|
|
|
|
*
|
|
|
|
* @return 0 on success, -EINVAL if page of the index doesn't exist.
|
|
|
|
*/
|
2017-10-26 03:36:51 +08:00
|
|
|
__syscall int flash_get_page_info_by_idx(struct device *dev, u32_t page_index,
|
|
|
|
struct flash_pages_info *info);
|
2017-07-17 18:03:10 +08:00
|
|
|
|
|
|
|
/**
|
2017-11-23 04:25:49 +08:00
|
|
|
* @brief Get the total number of flash pages.
|
2017-07-17 18:03:10 +08:00
|
|
|
*
|
|
|
|
* @param dev flash device
|
|
|
|
*
|
|
|
|
* @return Number of flash pages.
|
|
|
|
*/
|
2017-10-26 03:36:51 +08:00
|
|
|
__syscall size_t flash_get_page_count(struct device *dev);
|
2017-09-22 00:47:09 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback type for iterating over flash pages present on a device.
|
|
|
|
*
|
|
|
|
* The callback should return true to continue iterating, and false to halt.
|
|
|
|
*
|
|
|
|
* @param info Information for current page
|
|
|
|
* @param data Private data for callback
|
|
|
|
* @return True to continue iteration, false to halt iteration.
|
|
|
|
* @see flash_page_foreach()
|
|
|
|
*/
|
|
|
|
typedef bool (*flash_page_cb)(const struct flash_pages_info *info, void *data);
|
|
|
|
|
|
|
|
/**
|
2017-11-23 04:25:49 +08:00
|
|
|
* @brief Iterate over all flash pages on a device
|
2017-09-22 00:47:09 +08:00
|
|
|
*
|
|
|
|
* This routine iterates over all flash pages on the given device,
|
|
|
|
* ordered by increasing start offset. For each page, it invokes the
|
|
|
|
* given callback, passing it the page's information and a private
|
|
|
|
* data object.
|
|
|
|
*
|
|
|
|
* @param dev Device whose pages to iterate over
|
|
|
|
* @param cb Callback to invoke for each flash page
|
|
|
|
* @param data Private data for callback function
|
|
|
|
*/
|
|
|
|
void flash_page_foreach(struct device *dev, flash_page_cb cb, void *data);
|
2017-07-17 18:03:10 +08:00
|
|
|
#endif /* CONFIG_FLASH_PAGE_LAYOUT */
|
|
|
|
|
2017-09-22 15:55:43 +08:00
|
|
|
/**
|
|
|
|
* @brief Get the minimum write block size supported by the driver
|
|
|
|
*
|
2017-11-23 04:25:49 +08:00
|
|
|
* The write block size supported by the driver might differ from the write
|
2017-09-22 15:55:43 +08:00
|
|
|
* block size of memory used because the driver might implements write-modify
|
|
|
|
* algorithm.
|
|
|
|
*
|
|
|
|
* @param dev flash device
|
|
|
|
*
|
2017-11-23 04:25:49 +08:00
|
|
|
* @return write block size in bytes.
|
2017-09-22 15:55:43 +08:00
|
|
|
*/
|
2017-10-26 03:36:51 +08:00
|
|
|
__syscall size_t flash_get_write_block_size(struct device *dev);
|
|
|
|
|
|
|
|
static inline size_t _impl_flash_get_write_block_size(struct device *dev)
|
2017-09-22 15:55:43 +08:00
|
|
|
{
|
|
|
|
const struct flash_driver_api *api = dev->driver_api;
|
|
|
|
|
|
|
|
return api->write_block_size;
|
|
|
|
}
|
|
|
|
|
2016-02-19 02:52:19 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2017-10-26 03:36:51 +08:00
|
|
|
#include <syscalls/flash.h>
|
|
|
|
|
2018-09-15 01:43:44 +08:00
|
|
|
#endif /* ZEPHYR_INCLUDE_FLASH_H_ */
|