2016-02-19 02:52:19 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Public API for FLASH drivers
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __FLASH_H__
|
|
|
|
#define __FLASH_H__
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief FLASH Interface
|
|
|
|
* @defgroup flash_interface FLASH Interface
|
|
|
|
* @ingroup io_interfaces
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#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
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
struct flash_driver_api {
|
|
|
|
flash_api_read read;
|
|
|
|
flash_api_write write;
|
|
|
|
flash_api_erase erase;
|
|
|
|
flash_api_write_protection write_protection;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
*/
|
2016-03-10 02:40:32 +08:00
|
|
|
static inline int 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
|
|
|
*/
|
2016-03-10 02:40:32 +08:00
|
|
|
static inline int flash_write(struct device *dev, off_t offset,
|
2016-03-10 02:11:26 +08:00
|
|
|
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
|
|
|
|
* hardware-specific multiples of sector size and offset. Please check the
|
|
|
|
* API implemented by the underlying sub driver.
|
|
|
|
*
|
|
|
|
* 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.
|
2016-02-19 02:52:19 +08:00
|
|
|
*/
|
2016-03-10 02:40:32 +08:00
|
|
|
static inline int 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
|
|
|
*/
|
|
|
|
static inline int flash_write_protection_set(struct device *dev, bool enable)
|
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endif /* _FLASH_H_ */
|