Driver_Storage.h 40 KB

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