Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | /* |
| 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 Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 27 | #include <stdbool.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdlib.h> |
| 30 | #include "bl2_util.h" /* struct device */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 31 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 32 | #define off_t int32_t |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * @brief Read data from flash |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 36 | * @param dev : flash device |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 37 | * @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 Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 43 | int |
| 44 | flash_read(struct device *dev, off_t offset, void *data, size_t len); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 45 | |
| 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 Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 59 | int |
| 60 | flash_write(struct device *dev, off_t offset, const void *data, size_t len); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * @brief Erase part or all of a flash memory |
| 64 | * |
| 65 | * Acceptable values of erase size and offset are subject to |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 66 | * hardware-specific multiples of sector size and offset. Please check the |
| 67 | * API implemented by the underlying sub driver. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 68 | * |
| 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 Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 77 | */ |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 78 | int |
| 79 | flash_erase(struct device *dev, off_t offset, size_t size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 80 | |
| 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 Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 87 | * 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 Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 91 | * |
| 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 Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 97 | int |
| 98 | flash_write_protection_set(struct device *dev, bool enable); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 99 | |
| 100 | #ifdef __cplusplus |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | /** |
| 105 | * @} |
| 106 | */ |
| 107 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 108 | #endif /* __FLASH_H__ */ |