]> begriffs open source - cmsis/blob - CMSIS/Driver/Include/Driver_Storage.h
populate initial versions of Driver_Storage.[ch]
[cmsis] / CMSIS / Driver / Include / Driver_Storage.h
1 /*
2  * Copyright (c) 2006-2016, ARM Limited, All Rights Reserved
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #ifndef __DRIVER_STORAGE_H
19 #define __DRIVER_STORAGE_H
20
21 #ifdef __cplusplus
22 extern "C" {
23 #endif // __cplusplus
24
25 #include "Driver_Common.h"
26
27 #define ARM_STORAGE_API_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(1,00)  /* API version */
28
29
30 #define _ARM_Driver_Storage_(n)      Driver_Storage##n
31 #define  ARM_Driver_Storage_(n) _ARM_Driver_Storage_(n)
32
33 #define ARM_STORAGE_INVALID_OFFSET  (0xFFFFFFFFFFFFFFFFULL) ///< Invalid address (relative to a storage controller's
34                                                             ///< address space). A storage block may never start at this address.
35
36 #define ARM_STORAGE_INVALID_ADDRESS (0xFFFFFFFFUL)          ///< Invalid address within the processor's memory address space.
37                                                             ///< Refer to memory-mapped storage, i.e. < \ref ARM_DRIVER_STORAGE::ResolveAddress().
38
39 /****** Storage specific error codes *****/
40 #define ARM_STORAGE_ERROR_NOT_ERASABLE      (ARM_DRIVER_ERROR_SPECIFIC - 1) ///< Part (or all) of the range provided to Erase() isn't erasable.
41 #define ARM_STORAGE_ERROR_NOT_PROGRAMMABLE  (ARM_DRIVER_ERROR_SPECIFIC - 2) ///< Part (or all) of the range provided to ProgramData() isn't programmable.
42 #define ARM_STORAGE_ERROR_PROTECTED         (ARM_DRIVER_ERROR_SPECIFIC - 3) ///< Part (or all) of the range to Erase() or ProgramData() is protected.
43
44 /**
45  * Attributes of the storage range within a storage block.
46  */
47 typedef struct _ARM_STORAGE_BLOCK_ATTRIBUTES {
48   uint32_t erasable      :  1;   ///< Erasing blocks is permitted with a minimum granularity of 'erase_unit'.
49                                  ///< @note: if 'erasable' is 0--i.e. the 'erase' operation isn't available--then
50                                  ///< 'erase_unit' (see below) is immaterial and should be 0.
51
52   uint32_t programmable  :  1;   ///< Writing to ranges is permitted with a minimum granularity of 'program_unit'.
53                                  ///< Writes are typically achieved through the ProgramData operation (following an erase);
54                                  ///< if storage isn't erasable (see 'erasable' above) but is memory-mapped
55                                  ///< (i.e. 'memory_mapped'), it can be written directly using memory-store operations.
56
57   uint32_t executable    :  1;   ///< This storage block can hold program data; the processor can fetch and execute code
58                                  ///< sourced from it. Often this is accompanied with the device being 'memory_mapped' (see \ref ARM_STORAGE_INFO).
59
60   uint32_t protectable   :  1;   ///< The entire block can be protected from program and erase operations. Once protection
61                                  ///< is enabled for a block, its 'erasable' and 'programmable' bits are turned off.
62
63   uint32_t reserved      : 28;
64
65   uint32_t erase_unit;           ///< Minimum erase size in bytes.
66                                  ///< The offset of the start of the erase-range should also be aligned with this value.
67                                  ///< Applicable if the 'erasable' attribute is set for the block.
68                                  ///< @note: if 'erasable' (see above) is 0--i.e. the 'erase' operation isn't available--then
69                                  ///< 'erase_unit' is immaterial and should be 0.
70
71   uint32_t protection_unit;      ///< Minimum protectable size in bytes. Applicable if the 'protectable'
72                                  ///< attribute is set for the block. This should be a divisor of the block's size. A
73                                  ///< block can be considered to be made up of consecutive, individually-protectable fragments.
74 } ARM_STORAGE_BLOCK_ATTRIBUTES;
75
76 /**
77  * \brief A storage block is a range of memory with uniform attributes. Storage blocks
78  * combine to make up the address map of a storage controller.
79  */
80 typedef struct _ARM_STORAGE_BLOCK {
81   uint64_t                     addr;       ///< This is the start address of the storage block. It is
82                                            ///< expressed as an offset from the start of the storage map
83                                            ///< maintained by the owning storage controller.
84
85   uint64_t                     size;       ///< This is the size of the storage block, in units of bytes.
86                                            ///< Together with addr, it describes a range [addr, addr+size).
87
88   ARM_STORAGE_BLOCK_ATTRIBUTES attributes; ///< Attributes for this block.
89 } ARM_STORAGE_BLOCK;
90
91 /**
92  * The check for a valid ARM_STORAGE_BLOCK.
93  */
94 #define ARM_STORAGE_VALID_BLOCK(BLK) (((BLK)->addr != ARM_STORAGE_INVALID_OFFSET) && ((BLK)->size != 0))
95
96 /**
97  * \brief Values for encoding storage memory-types with respect to programmability.
98  *
99  * Please ensure that the maximum of the following memory types doesn't exceed 16; we
100  * encode this in a 4-bit field within ARM_STORAGE_INFO::programmability.
101  */
102 #define ARM_STORAGE_PROGRAMMABILITY_RAM       (0x0)
103 #define ARM_STORAGE_PROGRAMMABILITY_ROM       (0x1) ///< Read-only memory.
104 #define ARM_STORAGE_PROGRAMMABILITY_WORM      (0x2) ///< write-once-read-only-memory (WORM).
105 #define ARM_STORAGE_PROGRAMMABILITY_ERASABLE  (0x3) ///< re-programmable based on erase. Supports multiple writes.
106
107 /**
108  * Values for encoding data-retention levels for storage blocks.
109  *
110  * Please ensure that the maximum of the following retention types doesn't exceed 16; we
111  * encode this in a 4-bit field within ARM_STORAGE_INFO::retention_level.
112  */
113 #define ARM_RETENTION_WHILE_DEVICE_ACTIVE     (0x0) ///< Data is retained only during device activity.
114 #define ARM_RETENTION_ACROSS_SLEEP            (0x1) ///< Data is retained across processor sleep.
115 #define ARM_RETENTION_ACROSS_DEEP_SLEEP       (0x2) ///< Data is retained across processor deep-sleep.
116 #define ARM_RETENTION_BATTERY_BACKED          (0x3) ///< Data is battery-backed. Device can be powered off.
117 #define ARM_RETENTION_NVM                     (0x4) ///< Data is retained in non-volatile memory.
118
119 /**
120  * Device Data Security Protection Features. Applicable mostly to EXTERNAL_NVM.
121  */
122 typedef struct _ARM_STORAGE_SECURITY_FEATURES {
123   uint32_t acls                :  1; ///< Protection against internal software attacks using ACLs.
124   uint32_t rollback_protection :  1; ///< Roll-back protection. Set to true if the creator of the storage
125                                      ///<   can ensure that an external attacker can't force an
126                                      ///<   older firmware to run or to revert back to a previous state.
127   uint32_t tamper_proof        :  1; ///< Tamper-proof memory (will be deleted on tamper-attempts using board level or chip level sensors).
128   uint32_t internal_flash      :  1; ///< Internal flash.
129   uint32_t reserved1           : 12;
130
131   /**
132    * Encode support for hardening against various classes of attacks.
133    */
134   uint32_t software_attacks     :  1; ///< device software (malware running on the device).
135   uint32_t board_level_attacks  :  1; ///< board level attacks (debug probes, copy protection fuses.)
136   uint32_t chip_level_attacks   :  1; ///< chip level attacks (tamper-protection).
137   uint32_t side_channel_attacks :  1; ///< side channel attacks.
138   uint32_t reserved2            : 12;
139 } ARM_STORAGE_SECURITY_FEATURES;
140
141 #define ARM_STORAGE_PROGRAM_CYCLES_INFINITE (0UL) /**< Infinite or unknown endurance for reprogramming. */
142
143 /**
144  * \brief Storage information. This contains device-metadata, and is the return
145  *     value from calling GetInfo() on the storage driver.
146  *
147  * \details These fields serve a different purpose than the ones contained in
148  *     ARM_STORAGE_CAPABILITIES (\ref ARM_STORAGE_CAPABILITIES), which is
149  *     another structure containing device-level metadata.
150  *     ARM_STORAGE_CAPABILITIES describes the API capabilities, whereas ARM_STORAGE_INFO
151  *     describes the device. Furthermore ARM_STORAGE_CAPABILITIES fits within a
152  *     single word, and is designed to be passed around by value;
153  *     ARM_STORAGE_INFO, on the other hand contains metadata which doesn't fit
154  *     into a single word and requires the use of pointers to be moved around.
155  */
156 typedef struct _ARM_STORAGE_INFO {
157   uint64_t                      total_storage;        ///< Total available storage, in bytes.
158
159   uint32_t                      program_unit;         ///< Minimum programming size in bytes.
160                                                       ///< The offset of the start of the program-range should also be aligned with this value.
161                                                       ///< Applicable only if the 'programmable' attribute is set for a block.
162                                                       ///< @note: setting program_unit to 0 has the effect of disabling the size and alignment
163                                                       ///< restrictions (setting it to 1 also has the same effect).
164
165   uint32_t                      optimal_program_unit; ///< Optimal programming page-size in bytes. Some storage controllers
166                                                       ///< have internal buffers into which to receive data. Writing in chunks of
167                                                       ///< 'optimal_program_unit' would make the best use of such capabilities.
168                                                       ///< Applicable only if the 'programmable' attribute is set a the block.
169
170   uint32_t                      program_cycles;       ///< A measure of endurance for reprogramming.
171                                                       ///< Use ARM_STORAGE_PROGRAM_CYCLES_INFINITE for infinite or unknown endurance.
172
173   uint32_t                      erased_value    :  1; ///< Contents of erased memory (usually 1 to indicate erased bytes with state 0xFF).
174
175   uint32_t                      memory_mapped   :  1; ///< This storage device has a mapping onto the processor's memory address space.
176                                                       ///< @note: For a memory-mapped block which isn't erasable but is programmable (i.e. if
177                                                       ///< 'erasable' is set to 0, but 'programmable' is 1), writes should be possible directly to
178                                                       ///< the memory-mapped storage without going through the ProgramData operation.
179
180   uint32_t                      programmability :  4; ///< A value to indicate storage programmability.
181
182   uint32_t                      retention_level :  4;
183
184   uint32_t                      reserved        : 22;
185
186   ARM_STORAGE_SECURITY_FEATURES security;             ///< \ref ARM_STORAGE_SECURITY_FEATURES
187 } ARM_STORAGE_INFO;
188
189 /**
190 \brief Operating status of the storage controller.
191 */
192 typedef struct _ARM_STORAGE_STATUS {
193   uint32_t busy  : 1;                   ///< Controller busy flag
194   uint32_t error : 1;                   ///< Read/Program/Erase error flag (cleared on start of next operation)
195 } ARM_STORAGE_STATUS;
196
197 /**
198  * \brief Storage Driver API Capabilities.
199  *
200  * This data structure is designed to fit within a single word so that it can be
201  * fetched cheaply using a call to driver->GetCapabilities().
202  */
203 typedef struct _ARM_STORAGE_CAPABILITIES {
204   uint32_t asynchronous_ops :  1; ///< Used to indicate if APIs like initialize,
205                                   ///<   read, erase, program, etc. can operate in asynchronous mode.
206                                   ///<   Setting this bit to 1 means that the driver is capable
207                                   ///<   of launching asynchronous operations; command completion is
208                                   ///<   signaled by the invocation of a completion callback. If
209                                   ///<   set to 1, drivers may still complete asynchronous
210                                   ///<   operations synchronously as necessary--in which case they
211                                   ///<   return a positive error code to indicate synchronous completion.
212
213   uint32_t erase_all        :  1; ///< Supports EraseAll operation.
214
215   uint32_t reserved         : 30;
216 } ARM_STORAGE_CAPABILITIES;
217
218 /**
219  * Command opcodes for Storage. Completion callbacks use these codes to refer to
220  * completing commands. Refer to \ref ARM_Storage_Callback_t.
221  */
222 typedef enum _ARM_STORAGE_OPERATION {
223   ARM_STORAGE_OPERATION_GET_VERSION,
224   ARM_STORAGE_OPERATION_GET_CAPABILITIES,
225   ARM_STORAGE_OPERATION_INITIALIZE,
226   ARM_STORAGE_OPERATION_UNINITIALIZE,
227   ARM_STORAGE_OPERATION_POWER_CONTROL,
228   ARM_STORAGE_OPERATION_READ_DATA,
229   ARM_STORAGE_OPERATION_PROGRAM_DATA,
230   ARM_STORAGE_OPERATION_ERASE,
231   ARM_STORAGE_OPERATION_ERASE_ALL,
232   ARM_STORAGE_OPERATION_GET_STATUS,
233   ARM_STORAGE_OPERATION_GET_INFO,
234   ARM_STORAGE_OPERATION_RESOLVE_ADDRESS,
235   ARM_STORAGE_OPERATION_GET_FIRST_BLOCK,
236   ARM_STORAGE_OPERATION_GET_NEXT_BLOCK,
237   ARM_STORAGE_OPERATION_GET_BLOCK
238 } ARM_STORAGE_OPERATION;
239
240 /**
241  * Declaration of the callback-type for command completion.
242  *
243  * @param [in] status
244  *               A code to indicate the status of the completed operation. For data
245  *               transfer operations, the status field is overloaded in case of
246  *               success to return the count of items successfully transferred; this
247  *               can be done safely because error codes are negative values.
248  *
249  * @param [in] operation
250  *               The command op-code. This value isn't essential for the callback in
251  *               the presence of the command instance-id, but it is expected that
252  *               this information could be a quick and useful filter.
253  */
254 typedef void (*ARM_Storage_Callback_t)(int32_t status, ARM_STORAGE_OPERATION operation);
255
256 /**
257  * This is the set of operations constituting the Storage driver. Their
258  * implementation is platform-specific, and needs to be supplied by the
259  * porting effort.
260  *
261  * Some APIs within `ARM_DRIVER_STORAGE` will always operate synchronously:
262  * GetVersion, GetCapabilities, GetStatus, GetInfo, ResolveAddress,
263  * GetNextBlock, and GetBlock. This means that control returns to the caller
264  * with a relevant status code only after the completion of the operation (or
265  * the discovery of a failure condition).
266  *
267  * The remainder of the APIs: Initialize, Uninitialize, PowerControl, ReadData,
268  * ProgramData, Erase, EraseAll, can function asynchronously if the underlying
269  * controller supports it--i.e. if ARM_STORAGE_CAPABILITIES::asynchronous_ops is
270  * set. In the case of asynchronous operation, the invocation returns early
271  * (with ARM_DRIVER_OK) and results in a completion callback later. If
272  * ARM_STORAGE_CAPABILITIES::asynchronous_ops is not set, then all such APIs
273  * execute synchronously, and control returns to the caller with a status code
274  * only after the completion of the operation (or the discovery of a failure
275  * condition).
276  *
277  * If ARM_STORAGE_CAPABILITIES::asynchronous_ops is set, a storage driver may
278  * still choose to execute asynchronous operations in a synchronous manner. If
279  * so, the driver returns a positive value to indicate successful synchronous
280  * completion (or an error code in case of failure) and no further invocation of
281  * completion callback should be expected. The expected return value for
282  * synchronous completion of such asynchronous operations varies depending on
283  * the operation. For operations involving data access, it often equals the
284  * amount of data transferred or affected. For non data-transfer operations,
285  * such as EraseAll or Initialize, it is usually 1.
286  *
287  * Here's a code snippet to suggest how asynchronous APIs might be used by
288  * callers to handle both synchronous and asynchronous execution by the
289  * underlying storage driver:
290  * \code
291  *     ASSERT(ARM_DRIVER_OK == 0); // this is a precondition; it doesn't need to be put in code
292  *     int32_t returnValue = drv->asynchronousAPI(...);
293  *     if (returnValue < ARM_DRIVER_OK) {
294  *         // handle error.
295  *     } else if (returnValue == ARM_DRIVER_OK) {
296  *         ASSERT(drv->GetCapabilities().asynchronous_ops == 1);
297  *         // handle early return from asynchronous execution; remainder of the work is done in the callback handler.
298  *     } else {
299  *         ASSERT(returnValue == EXPECTED_RETURN_VALUE_FOR_SYNCHRONOUS_COMPLETION);
300  *         // handle synchronous completion.
301  *     }
302  * \endcode
303  */
304 typedef struct _ARM_DRIVER_STORAGE {
305   /**
306    * \brief Get driver version.
307    *
308    * The function GetVersion() returns version information of the driver implementation in ARM_DRIVER_VERSION.
309    *
310    *    - API version is the version of the CMSIS-Driver specification used to implement this driver.
311    *    - Driver version is source code version of the actual driver implementation.
312    *
313    * Example:
314    * \code
315    *     extern ARM_DRIVER_STORAGE *drv_info;
316    *
317    *     void read_version (void)  {
318    *       ARM_DRIVER_VERSION  version;
319    *
320    *       version = drv_info->GetVersion ();
321    *       if (version.api < 0x10A)   {      // requires at minimum API version 1.10 or higher
322    *         // error handling
323    *         return;
324    *       }
325    *     }
326    * \endcode
327    *
328    * @return \ref ARM_DRIVER_VERSION.
329    *
330    * @note This API returns synchronously--it does not result in an invocation
331    *     of a completion callback.
332    *
333    * @note The functions GetVersion() can be called any time to obtain the
334    *     required information from the driver (even before initialization). It
335    *     always returns the same information.
336    */
337   ARM_DRIVER_VERSION (*GetVersion)(void);
338
339   /**
340    * \brief Get driver capabilities.
341    *
342    * \details The function GetCapabilities() returns information about
343    * capabilities in this driver implementation. The data fields of the struct
344    * ARM_STORAGE_CAPABILITIES encode various capabilities, for example if the device
345    * is able to execute operations asynchronously.
346    *
347    * Example:
348    * \code
349    *     extern ARM_DRIVER_STORAGE *drv_info;
350    *
351    *     void read_capabilities (void)  {
352    *       ARM_STORAGE_CAPABILITIES drv_capabilities;
353    *
354    *       drv_capabilities = drv_info->GetCapabilities ();
355    *       // interrogate capabilities
356    *
357    *     }
358    * \endcode
359    *
360    * @return \ref ARM_STORAGE_CAPABILITIES.
361    *
362    * @note This API returns synchronously--it does not result in an invocation
363    *     of a completion callback.
364    *
365    * @note The functions GetVersion() can be called any time to obtain the
366    *     required information from the driver (even before initialization). It
367    *     always returns the same information.
368    */
369   ARM_STORAGE_CAPABILITIES (*GetCapabilities)(void);
370
371   /**
372    * \brief Initialize the Storage Interface.
373    *
374    * The function Initialize is called when the middleware component starts
375    * operation. In addition to bringing the controller to a ready state,
376    * Initialize() receives a callback handler to be invoked upon completion of
377    * asynchronous operations.
378    *
379    * Initialize() needs to be called explicitly before
380    * powering the peripheral using PowerControl(), and before initiating other
381    * accesses to the storage controller.
382    *
383    * The function performs the following operations:
384    *   - Initializes the resources needed for the Storage interface.
385    *   - Registers the ARM_STOR_SignalEvent callback function.
386    *
387    * To start working with a peripheral the functions Initialize and PowerControl need to be called in this order:
388    *     drv->Initialize (...); // Allocate I/O pins
389    *     drv->PowerControl (ARM_POWER_FULL);        // Power up peripheral, setup IRQ/DMA
390    *
391    * - Initialize() typically allocates the I/O resources (pins) for the
392    *   peripheral. The function can be called multiple times; if the I/O resources
393    *   are already initialized it performs no operation and just returns with
394    *   ARM_DRIVER_OK.
395    *
396    * - PowerControl (ARM_POWER_FULL) sets the peripheral registers including
397    *   interrupt (NVIC) and optionally DMA. The function can be called multiple
398    *   times; if the registers are already set it performs no operation and just
399    *   returns with ARM_DRIVER_OK.
400    *
401    * To stop working with a peripheral the functions PowerControl and Uninitialize need to be called in this order:
402    *     drv->PowerControl (ARM_POWER_OFF);     // Terminate any pending transfers, reset IRQ/DMA, power off peripheral
403    *     drv->Uninitialize (...);               // Release I/O pins
404    *
405    * The functions PowerControl and Uninitialize always execute and can be used
406    * to put the peripheral into a Safe State, for example after any data
407    * transmission errors. To restart the peripheral in an error condition,
408    * you should first execute the Stop Sequence and then the Start Sequence.
409    *
410    * @param [in] callback
411    *               Caller-defined callback to be invoked upon command completion
412    *               for asynchronous APIs (including the completion of
413    *               initialization). Use a NULL pointer when no callback
414    *               signals are required.
415    *
416    * @note This API may execute asynchronously if
417    *     ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
418    *     execution is optional even if 'asynchronous_ops' is set.
419    *
420    * @return If asynchronous activity is launched, an invocation returns
421    *     ARM_DRIVER_OK, and the caller can expect to receive a callback in the
422    *     future with a status value of ARM_DRIVER_OK or an error-code. In the
423    *     case of synchronous execution, control returns after completion with a
424    *     value of 1. Return values less than ARM_DRIVER_OK (0) signify errors.
425    */
426   int32_t (*Initialize)(ARM_Storage_Callback_t callback);
427
428   /**
429    * \brief De-initialize the Storage Interface.
430    *
431    * The function Uninitialize() de-initializes the resources of Storage interface.
432    *
433    * It is called when the middleware component stops operation, and wishes to
434    * release the software resources used by the interface.
435    *
436    * @note This API may execute asynchronously if
437    *     ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
438    *     execution is optional even if 'asynchronous_ops' is set.
439    *
440    * @return If asynchronous activity is launched, an invocation returns
441    *     ARM_DRIVER_OK, and the caller can expect to receive a callback in the
442    *     future with a status value of ARM_DRIVER_OK or an error-code. In the
443    *     case of synchronous execution, control returns after completion with a
444    *     value of 1. Return values less than ARM_DRIVER_OK (0) signify errors.
445    */
446   int32_t (*Uninitialize)(void);
447
448   /**
449    * \brief Control the Storage interface power.
450    *
451    * The function ARM_STOR_PowerControl operates the power modes of the Storage interface.
452    *
453    * To start working with a peripheral the functions Initialize and PowerControl need to be called in this order:
454    *     drv->Initialize (...);                 // Allocate I/O pins
455    *     drv->PowerControl (ARM_POWER_FULL);    // Power up peripheral, setup IRQ/DMA
456    *
457    * - Initialize() typically allocates the I/O resources (pins) for the
458    *   peripheral. The function can be called multiple times; if the I/O resources
459    *   are already initialized it performs no operation and just returns with
460    *   ARM_DRIVER_OK.
461    *
462    * - PowerControl (ARM_POWER_FULL) sets the peripheral registers including
463    *   interrupt (NVIC) and optionally DMA. The function can be called multiple
464    *   times; if the registers are already set it performs no operation and just
465    *   returns with ARM_DRIVER_OK.
466    *
467    * To stop working with a peripheral the functions PowerControl and Uninitialize need to be called in this order:
468    *
469    *     drv->PowerControl (ARM_POWER_OFF);     // Terminate any pending transfers, reset IRQ/DMA, power off peripheral
470    *     drv->Uninitialize (...);               // Release I/O pins
471    *
472    * The functions PowerControl and Uninitialize always execute and can be used
473    * to put the peripheral into a Safe State, for example after any data
474    * transmission errors. To restart the peripheral in an error condition,
475    * you should first execute the Stop Sequence and then the Start Sequence.
476    *
477    * @param state
478    *          \ref ARM_POWER_STATE. The target power-state for the storage controller.
479    *          The parameter state can have the following values:
480    *              - ARM_POWER_FULL : set-up peripheral for data transfers, enable interrupts
481    *                                 (NVIC) and optionally DMA. Can be called multiple times. If the peripheral
482    *                                 is already in this mode, then the function performs no operation and returns
483    *                                 with ARM_DRIVER_OK.
484    *              - ARM_POWER_LOW : may use power saving. Returns ARM_DRIVER_ERROR_UNSUPPORTED when not implemented.
485    *              - ARM_POWER_OFF : terminates any pending data transfers, disables peripheral, disables related interrupts and DMA.
486    *
487    * @note This API may execute asynchronously if
488    *     ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
489    *     execution is optional even if 'asynchronous_ops' is set.
490    *
491    * @return If asynchronous activity is launched, an invocation returns
492    *     ARM_DRIVER_OK, and the caller can expect to receive a callback in the
493    *     future with a status value of ARM_DRIVER_OK or an error-code. In the
494    *     case of synchronous execution, control returns after completion with a
495    *     value of 1. Return values less than ARM_DRIVER_OK (0) signify errors.
496    */
497   int32_t (*PowerControl)(ARM_POWER_STATE state);
498
499   /**
500    * \brief read the contents of a given address range from the storage device.
501    *
502    * \details Read the contents of a range of storage memory into a buffer
503    *   supplied by the caller. The buffer is owned by the caller and should
504    *   remain accessible for the lifetime of this command.
505    *
506    * @param  [in] addr
507    *                This specifies the address from where to read data.
508    *
509    * @param [out] data
510    *                The destination of the read operation. The buffer
511    *                is owned by the caller and should remain accessible for the
512    *                lifetime of this command.
513    *
514    * @param  [in] size
515    *                The number of bytes requested to read. The data buffer
516    *                should be at least as large as this size.
517    *
518    * @note This API may execute asynchronously if
519    *     ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
520    *     execution is optional even if 'asynchronous_ops' is set.
521    *
522    * @return If asynchronous activity is launched, an invocation returns
523    *     ARM_DRIVER_OK, and the caller can expect to receive a callback in the
524    *     future with the number of successfully transferred bytes passed in as
525    *     the 'status' parameter. In the case of synchronous execution, control
526    *     returns after completion with a positive transfer-count. Return values
527    *     less than ARM_DRIVER_OK (0) signify errors.
528    */
529   int32_t (*ReadData)(uint64_t addr, void *data, uint32_t size);
530
531   /**
532    * \brief program (write into) the contents of a given address range of the storage device.
533    *
534    * \details Write the contents of a given memory buffer into a range of
535    *   storage memory. In the case of flash memory, the destination range in
536    *   storage memory typically has its contents in an erased state from a
537    *   preceding erase operation. The source memory buffer is owned by the
538    *   caller and should remain accessible for the lifetime of this command.
539    *
540    * @param [in] addr
541    *               This is the start address of the range to be written into. It
542    *               needs to be aligned to the device's \em program_unit
543    *               specified in \ref ARM_STORAGE_INFO.
544    *
545    * @param [in] data
546    *               The source of the write operation. The buffer is owned by the
547    *               caller and should remain accessible for the lifetime of this
548    *               command.
549    *
550    * @param [in] size
551    *               The number of bytes requested to be written. The buffer
552    *               should be at least as large as this size. \note 'size' should
553    *               be a multiple of the device's 'program_unit' (see \ref
554    *               ARM_STORAGE_INFO).
555    *
556    * @note It is best for the middleware to write in units of
557    *     'optimal_program_unit' (\ref ARM_STORAGE_INFO) of the device.
558    *
559    * @note This API may execute asynchronously if
560    *     ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
561    *     execution is optional even if 'asynchronous_ops' is set.
562    *
563    * @return If asynchronous activity is launched, an invocation returns
564    *     ARM_DRIVER_OK, and the caller can expect to receive a callback in the
565    *     future with the number of successfully transferred bytes passed in as
566    *     the 'status' parameter. In the case of synchronous execution, control
567    *     returns after completion with a positive transfer-count. Return values
568    *     less than ARM_DRIVER_OK (0) signify errors.
569    */
570   int32_t (*ProgramData)(uint64_t addr, const void *data, uint32_t size);
571
572   /**
573    * @brief Erase Storage range.
574    *
575    * @details This function erases a range of storage specified by [addr, addr +
576    *     size). Both 'addr' and 'addr + size' should align with the
577    *     'erase_unit'(s) of the respective owning storage block(s) (see \ref
578    *     ARM_STORAGE_BLOCK and \ref ARM_STORAGE_BLOCK_ATTRIBUTES). The range to
579    *     be erased will have its contents returned to the un-programmed state--
580    *     i.e. to 'erased_value' (see \ref ARM_STORAGE_BLOCK_ATTRIBUTES), which
581    *     is usually 1 to indicate the pattern of all ones: 0xFF.
582    *
583    * @param [in] addr
584    *               This is the start-address of the range to be erased. It must
585    *               start at an 'erase_unit' boundary of the underlying block.
586    *
587    * @param [in] size
588    *               Size (in bytes) of the range to be erased. 'addr + size'
589    *               must be aligned with the 'erase_unit' of the underlying
590    *               block.
591    *
592    * @note This API may execute asynchronously if
593    *     ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
594    *     execution is optional even if 'asynchronous_ops' is set.
595    *
596    * @return
597    *   If the range to be erased doesn't align with the erase_units of the
598    *   respective start and end blocks, ARM_DRIVER_ERROR_PARAMETER is returned.
599    *   If any part of the range is protected, ARM_STORAGE_ERROR_PROTECTED is
600    *   returned. If any part of the range is not erasable,
601    *   ARM_STORAGE_ERROR_NOT_ERASABLE is returned. All such sanity-check
602    *   failures result in the error code being returned synchronously and the
603    *   storage bytes within the range remain unaffected.
604    *   Otherwise the function executes in the following ways:
605    *     If asynchronous activity is launched, an invocation returns
606    *     ARM_DRIVER_OK, and the caller can expect to receive a callback in the
607    *     future with the number of successfully erased bytes passed in as
608    *     the 'status' parameter. In the case of synchronous execution, control
609    *     returns after completion with a positive erase-count. Return values
610    *     less than ARM_DRIVER_OK (0) signify errors.
611    *
612    * @note Erase() may return a smaller (positive) value than the size of the
613    *     requested range. The returned value indicates the actual number of bytes
614    *     erased. It is the caller's responsibility to follow up with an appropriate
615    *     request to complete the operation.
616    *
617    * @note in the case of a failed erase (except when
618    *     ARM_DRIVER_ERROR_PARAMETER, ARM_STORAGE_ERROR_PROTECTED, or
619    *     ARM_STORAGE_ERROR_NOT_ERASABLE is returned synchronously), the
620    *     requested range should be assumed to be in an unknown state. The
621    *     previous contents may not be retained.
622    */
623   int32_t (*Erase)(uint64_t addr, uint32_t size);
624
625   /**
626    * @brief Erase complete storage. Optional function for faster erase of the complete device.
627    *
628    * This optional function erases the complete device. If the device does not
629    *    support global erase then the function returns the error value \ref
630    *    ARM_DRIVER_ERROR_UNSUPPORTED. The data field \em 'erase_all' =
631    *    \token{1} of the structure \ref ARM_STORAGE_CAPABILITIES encodes that
632    *    \ref ARM_STORAGE_EraseAll is supported.
633    *
634    * @note This API may execute asynchronously if
635    *     ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
636    *     execution is optional even if 'asynchronous_ops' is set.
637    *
638    * @return
639    *   If any part of the storage range is protected,
640    *   ARM_STORAGE_ERROR_PROTECTED is returned. If any part of the storage
641    *   range is not erasable, ARM_STORAGE_ERROR_NOT_ERASABLE is returned. All
642    *   such sanity-check failures result in the error code being returned
643    *   synchronously and the storage bytes within the range remain unaffected.
644    *   Otherwise the function executes in the following ways:
645    *     If asynchronous activity is launched, an invocation returns
646    *     ARM_DRIVER_OK, and the caller can expect to receive a callback in the
647    *     future with ARM_DRIVER_OK passed in as the 'status' parameter. In the
648    *     case of synchronous execution, control returns after completion with a
649    *     value of 1. Return values less than ARM_DRIVER_OK (0) signify errors.
650    */
651   int32_t (*EraseAll)(void);
652
653   /**
654    * @brief Get the status of the current (or previous) command executed by the
655    *     storage controller; stored in the structure \ref ARM_STORAGE_STATUS.
656    *
657    * @return
658    *          The status of the underlying controller.
659    *
660    * @note This API returns synchronously--it does not result in an invocation
661    *     of a completion callback.
662    */
663   ARM_STORAGE_STATUS (*GetStatus)(void);
664
665   /**
666    * @brief Get information about the Storage device; stored in the structure \ref ARM_STORAGE_INFO.
667    *
668    * @param [out] info
669    *                A caller-supplied buffer capable of being filled in with an
670    *                \ref ARM_STORAGE_INFO.
671    *
672    * @return ARM_DRIVER_OK if a ARM_STORAGE_INFO structure containing top level
673    *         metadata about the storage controller is filled into the supplied
674    *         buffer, else an appropriate error value.
675    *
676    * @note It is the caller's responsibility to ensure that the buffer passed in
677    *         is able to be initialized with a \ref ARM_STORAGE_INFO.
678    *
679    * @note This API returns synchronously--it does not result in an invocation
680    *     of a completion callback.
681    */
682   int32_t (*GetInfo)(ARM_STORAGE_INFO *info);
683
684   /**
685    * \brief For memory-mapped storage, resolve an address relative to
686    *     the storage controller into a memory address.
687    *
688    * @param addr
689    *          This is the address for which we want a resolution to the
690    *          processor's physical address space. It is an offset from the
691    *          start of the storage map maintained by the owning storage
692    *          controller.
693    *
694    * @return
695    *          The resolved address in the processor's address space; else
696    *          ARM_STORAGE_INVALID_ADDRESS, if no resolution is possible.
697    *
698    * @note This API returns synchronously. The invocation should return quickly,
699    *     and result in a resolved address.
700    */
701   uint32_t (*ResolveAddress)(uint64_t addr);
702
703   /**
704    * @brief Advance to the successor of the current block (iterator), or fetch
705    *     the first block (if 'prev_block' is passed in as NULL).
706    *
707    * @details This helper function fetches (an iterator to) the next block (or
708    *     the first block if 'prev_block' is passed in as NULL). In the failure
709    *     case, a terminating, invalid block iterator is filled into the out
710    *     parameter: 'next_block'. In combination with \ref
711    *     ARM_STORAGE_VALID_BLOCK(), it can be used to iterate over the sequence
712    *     of blocks within the storage map:
713    *
714    * \code
715    *   ARM_STORAGE_BLOCK block;
716    *   for (drv->GetNextBlock(NULL, &block); ARM_STORAGE_VALID_BLOCK(&block); drv->GetNextBlock(&block, &block)) {
717    *       // make use of block
718    *   }
719    * \endcode
720    *
721    * @param[in]  prev_block
722    *               An existing block (iterator) within the same storage
723    *               controller. The memory buffer holding this block is owned
724    *               by the caller. This pointer may be NULL; if so, the
725    *               invocation fills in the first block into the out parameter:
726    *               'next_block'.
727    *
728    * @param[out] next_block
729    *               A caller-owned buffer large enough to be filled in with
730    *               the following ARM_STORAGE_BLOCK. It is legal to provide the
731    *               same buffer using 'next_block' as was passed in with 'prev_block'. It
732    *               is also legal to pass a NULL into this parameter if the
733    *               caller isn't interested in populating a buffer with the next
734    *               block--i.e. if the caller only wishes to establish the
735    *               presence of a next block.
736    *
737    * @return ARM_DRIVER_OK if a valid next block is found (or first block, if
738    *     prev_block is passed as NULL); upon successful operation, the contents
739    *     of the next (or first) block are filled into the buffer pointed to by
740    *     the parameter 'next_block' and ARM_STORAGE_VALID_BLOCK(next_block) is
741    *     guaranteed to be true. Upon reaching the end of the sequence of blocks
742    *     (iterators), or in case the driver is unable to fetch information about
743    *     the next (or first) block, an error (negative) value is returned and an
744    *     invalid StorageBlock is populated into the supplied buffer. If
745    *     prev_block is NULL, the first block is returned.
746    *
747    * @note This API returns synchronously--it does not result in an invocation
748    *     of a completion callback.
749    */
750    int32_t (*GetNextBlock)(const ARM_STORAGE_BLOCK* prev_block, ARM_STORAGE_BLOCK *next_block);
751
752   /**
753    * @brief Find the storage block (iterator) encompassing a given storage address.
754    *
755    * @param[in]  addr
756    *               Storage address in bytes.
757    *
758    * @param[out] block
759    *               A caller-owned buffer large enough to be filled in with the
760    *               ARM_STORAGE_BLOCK encapsulating the given address. This value
761    *               can also be passed in as NULL if the caller isn't interested
762    *               in populating a buffer with the block--if the caller only
763    *               wishes to establish the presence of a containing storage
764    *               block.
765    *
766    * @return ARM_DRIVER_OK if a containing storage-block is found. In this case,
767    *     if block is non-NULL, the buffer pointed to by it is populated with
768    *     the contents of the storage block--i.e. if block is valid and a block is
769    *     found, ARM_STORAGE_VALID_BLOCK(block) would return true following this
770    *     call. If there is no storage block containing the given offset, or in
771    *     case the driver is unable to resolve an address to a storage-block, an
772    *     error (negative) value is returned and an invalid StorageBlock is
773    *     populated into the supplied buffer.
774    *
775    * @note This API returns synchronously--it does not result in an invocation
776    *     of a completion callback.
777    */
778   int32_t (*GetBlock)(uint64_t addr, ARM_STORAGE_BLOCK *block);
779 } const ARM_DRIVER_STORAGE;
780
781 #ifdef __cplusplus
782 }
783 #endif // __cplusplus
784
785 #endif /* __DRIVER_STORAGE_H */