blob: 12cd9ebe3835422768ce33d8d7b945a088f3e68e [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
2 * Copyright (c) 2017 Nordic Semiconductor ASA
3 * Copyright (c) 2016 Intel Corporation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8/**
9 * @file
10 * @brief Public API for FLASH drivers
11 */
12
13#ifndef __FLASH_H__
14#define __FLASH_H__
15
16/**
17 * @brief FLASH Interface
18 * @defgroup flash_interface FLASH Interface
19 * @ingroup io_interfaces
20 * @{
21 */
22
Tamas Banf70ef8c2017-12-19 15:35:09 +000023#ifdef __cplusplus
24extern "C" {
25#endif
26
Tamas Ban581034a2017-12-19 19:54:37 +000027#include <stdbool.h>
28#include <stdint.h>
29#include <stdlib.h>
30#include "bl2_util.h" /* struct device */
Tamas Banf70ef8c2017-12-19 15:35:09 +000031
Tamas Ban581034a2017-12-19 19:54:37 +000032#define off_t int32_t
Tamas Banf70ef8c2017-12-19 15:35:09 +000033
34/**
35 * @brief Read data from flash
Tamas Ban581034a2017-12-19 19:54:37 +000036 * @param dev : flash device
Tamas Banf70ef8c2017-12-19 15:35:09 +000037 * @param offset : Offset (byte aligned) to read
38 * @param data : Buffer to store read data
39 * @param len : Number of bytes to read.
40 *
41 * @return 0 on success, negative errno code on fail.
42 */
Tamas Ban581034a2017-12-19 19:54:37 +000043int
44flash_read(struct device *dev, off_t offset, void *data, size_t len);
Tamas Banf70ef8c2017-12-19 15:35:09 +000045
46/**
47 * @brief Write buffer into flash memory.
48 *
49 * Prior to the invocation of this API, the flash_write_protection_set needs
50 * to be called first to disable the write protection.
51 *
52 * @param dev : flash device
53 * @param offset : starting offset for the write
54 * @param data : data to write
55 * @param len : Number of bytes to write
56 *
57 * @return 0 on success, negative errno code on fail.
58 */
Tamas Ban581034a2017-12-19 19:54:37 +000059int
60flash_write(struct device *dev, off_t offset, const void *data, size_t len);
Tamas Banf70ef8c2017-12-19 15:35:09 +000061
62/**
63 * @brief Erase part or all of a flash memory
64 *
65 * Acceptable values of erase size and offset are subject to
Tamas Ban581034a2017-12-19 19:54:37 +000066 * hardware-specific multiples of sector size and offset. Please check the
67 * API implemented by the underlying sub driver.
Tamas Banf70ef8c2017-12-19 15:35:09 +000068 *
69 * Prior to the invocation of this API, the flash_write_protection_set needs
70 * to be called first to disable the write protection.
71 *
72 * @param dev : flash device
73 * @param offset : erase area starting offset
74 * @param size : size of area to be erased
75 *
76 * @return 0 on success, negative errno code on fail.
Tamas Banf70ef8c2017-12-19 15:35:09 +000077 */
Tamas Ban581034a2017-12-19 19:54:37 +000078int
79flash_erase(struct device *dev, off_t offset, size_t size);
Tamas Banf70ef8c2017-12-19 15:35:09 +000080
81/**
82 * @brief Enable or disable write protection for a flash memory
83 *
84 * This API is required to be called before the invocation of write or erase
85 * API. Please note that on some flash components, the write protection is
86 * automatically turned on again by the device after the completion of each
Tamas Ban581034a2017-12-19 19:54:37 +000087 * write or erase calls. Therefore, on those flash parts, write protection
88 * needs to be disabled before each invocation of the write or erase API.
89 * Please refer to the sub-driver API or the data sheet of the flash component
90 * to get details on the write protection behavior.
Tamas Banf70ef8c2017-12-19 15:35:09 +000091 *
92 * @param dev : flash device
93 * @param enable : enable or disable flash write protection
94 *
95 * @return 0 on success, negative errno code on fail.
96 */
Tamas Ban581034a2017-12-19 19:54:37 +000097int
98flash_write_protection_set(struct device *dev, bool enable);
Tamas Banf70ef8c2017-12-19 15:35:09 +000099
100#ifdef __cplusplus
101}
102#endif
103
104/**
105 * @}
106 */
107
Tamas Ban581034a2017-12-19 19:54:37 +0000108#endif /* __FLASH_H__ */