Software APIs
dif_flash_ctrl.h
Go to the documentation of this file.
1 // Copyright lowRISC contributors (OpenTitan project).
2 // Licensed under the Apache License, Version 2.0, see LICENSE for details.
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef OPENTITAN_SW_DEVICE_LIB_DIF_DIF_FLASH_CTRL_H_
6 #define OPENTITAN_SW_DEVICE_LIB_DIF_DIF_FLASH_CTRL_H_
7 
8 /**
9  * @file
10  * @brief <a href="/hw/ip/flash_ctrl/doc/">Flash Controller</a> Device
11  * Interface Functions
12  */
13 
15 #include "sw/device/lib/base/multibits.h"
16 
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif // __cplusplus
22 
23 /**
24  * Carries state for a flash controller device.
25  *
26  * All members except the `dev` should be considered private and should not be
27  * accessed directly outside of the DIF implementation.
28  */
29 typedef struct dif_flash_ctrl_state {
30  /**
31  * Contains autogenerated device information, like the base address for
32  * registers in the flash controller device.
33  */
35 
36  /* Private members below. */
37 
38  /** Number of words remaining for the current transaction. */
39  uint32_t words_remaining;
40  /** Whether a transaction is pending completion. */
43 
44 /**
45  * Initialize the DIF state for the flash controller.
46  *
47  * @param handle The flash controller DIF state to initialize.
48  * @param base_addr The base address for the flash controller.
49  * @return `kDifBadArg` if `handle` is null. `kDifOk` otherwise.
50  */
53  mmio_region_t base_addr);
54 
56  /** Number of banks under this flash controller. */
57  uint32_t num_banks;
58  /** Number of bytes per flash word. */
59  uint32_t bytes_per_word;
60  /** Number of bytes per flash page. */
61  uint32_t bytes_per_page;
62  /** Number of pages per bank in the data partition. */
63  uint32_t data_pages;
64  /** Number of pages per bank in the info partition, type 0. */
65  uint32_t info0_pages;
66  /** Number of pages per bank in the info partition, type 1. */
67  uint32_t info1_pages;
68  /** Number of pages per bank in the info partition, type 2. */
69  uint32_t info2_pages;
71 
72 // TODO: Associate the data with a base address or acknowledge that there
73 // should only ever be one flash controller in the system.
74 /**
75  * Get information on the flash controller sizes.
76  *
77  * @return A `dif_flash_ctrl_device_info_t` representing the feature sizes for
78  * various components of the flash.
79  */
82 
83 /**
84  * Set whether the flash is enabled.
85  *
86  * Generally, this is used to disable flash functionality in case of an
87  * emergency. Flash functionality is ordinarily enabled.
88  *
89  * @param handle The flash controller to operate on.
90  * @param enable Enable/disable flash functionality.
91  * @return `kDifBadArg` if `handle` is null. `kDifOk` otherwise.
92  */
95  dif_toggle_t enable);
96 
97 /**
98  * Get whether flash functionality is enabled.
99  *
100  * @param handle The flash controller to operate on.
101  * @param enabled_out Out parameter, where to store whether flash is enabled.
102  * @return `kDifBadArg` if `handle` or `enabled_out` are null and `kDifOk`
103  * otherwise.
104  */
107  const dif_flash_ctrl_state_t *handle, dif_toggle_t *enabled_out);
108 
109 /**
110  * Set whether execution / fetch access is enabled.
111  *
112  * @param handle The flash controller to operate on.
113  * @param enable Enable/disable execution / fetch access.
114  * @return `kDifBadArg` if `handle` or `enabled_out` are null and `kDifOk`
115  * otherwise.
116  */
119  dif_toggle_t enable);
120 
121 /**
122  * Get whether execution / fetch access is enabled.
123  *
124  * @param handle The flash controller to operate on.
125  * @param enabled_out Out parameter, whether execution / fetch access is
126  * allowed.
127  * @return `kDifBadArg` if `handle` or `enabled_out` are null and `kDifOk`
128  * otherwise.
129  */
132  const dif_flash_ctrl_state_t *handle, dif_toggle_t *enabled_out);
133 
134 /**
135  * Begins the flash controller's initialization sequence. The flash controller
136  * will read out the root seeds before allowing other usage of the controller.
137  * This operation will only be performed once, and controller initialization
138  * cannot be requested again until after a reset.
139  *
140  * @param handle The flash controller to operate on.
141  * @return `kDifBadArg` if `handle` is null, `kDifError` if initialization has
142  * already been started, and `kDifOk` otherwise.
143  */
146  dif_flash_ctrl_state_t *handle);
147 
148 /**
149  * Status bits that can be queried.
150  */
151 typedef struct dif_flash_ctrl_status {
152  /**
153  * Flash read FIFO full, software must consume data.
154  */
155  bool read_fifo_full : 1;
156  /**
157  * Flash read FIFO empty.
158  */
159  bool read_fifo_empty : 1;
160  /**
161  * Flash program FIFO full.
162  */
163  bool prog_fifo_full : 1;
164  /**
165  * Flash program FIFO empty, software must provide data.
166  */
167  bool prog_fifo_empty : 1;
168  /**
169  * Flash controller undergoing init.
170  */
172  /**
173  * Flash controller initialized.
174  */
177 
178 /**
179  * Query the status registers on the flash controller.
180  *
181  * This function checks the various status bits as described in
182  * `dif_flash_ctrl_status_t`.
183  *
184  * @param handle flash controller device to check the status bits for.
185  * @param status_out Out parameter. The current status of the flash controller.
186  * @return `kDifBadArg` if `handle` or `status_out` is null, `kDifOk` otherwise.
187  */
190  dif_flash_ctrl_status_t *status_out);
191 
192 /**
193  * Represents programming capabilities of the flash controller. The meaning of
194  * the boolean values is context-sensitive. For the result of a query, they
195  * represent which types are allowed. As an argument to a function that disables
196  * capabilities, a true value indicates that the function should disable that
197  * type.
198  */
200  /** Normal programming type. */
202  /** Repair programming type. */
205 
206 /**
207  * Query the allowed programming types.
208  *
209  * Note that this function is a check of a permission only. For checking whether
210  * the flash supports a given type, use `dif_flash_ctrl_get_phy_status()`.
211  * @sa dif_flash_ctrl_get_phy_status
212  *
213  * @param handle The flash controller to query.
214  * @param allowed_types_out Out parameter. Points to location where the allowed
215  * types should be written.
216  * @return `kDifBadArg` if `handle` or `allowed_types_out` are null. `kDifOk`
217  * otherwise.
218  */
221  const dif_flash_ctrl_state_t *handle,
222  dif_flash_ctrl_prog_capabilities_t *allowed_types_out);
223 
224 /**
225  * Disallow the indicated programming operations.
226  *
227  * Note that this is a one-way operation. Programming types that are disabled
228  * cannot be enabled again without a reset.
229  *
230  * @param handle The flash controller to operate on.
231  * @param types_to_disallow A programming type marked true indicates that the
232  * given type should be *disabled*.
233  * @return `kDifBadArg` if handle is null. `kDifOk` otherwise.
234  */
237  dif_flash_ctrl_state_t *handle,
238  dif_flash_ctrl_prog_capabilities_t types_to_disable);
239 
240 /**
241  * Enum to represent the flash partition type (data or info).
242  */
244  kDifFlashCtrlPartitionTypeData = 0,
245  kDifFlashCtrlPartitionTypeInfo = 1
247 
249  /** Read the specified number of words. */
251  /** Program the specified number of words (normal mode). */
253  /**
254  * Program the specified number of words in repair mode. Support for this
255  * mode depends on the underlying flash technology. See
256  * `dif_flash_ctrl_supports_repair_operation`.
257  */
259  /** Erase the specified page. */
261  /** Erase the specified bank. */
263 } dif_flash_ctrl_operation_t;
264 
266  dif_flash_ctrl_operation_t op;
267  /**
268  * The partition type for the transaction.
269  *
270  * Note: For bank erase operations, the data partition is *always* erased,
271  * even when `partition_type` is `kFlashCtrlPartitionTypeInfo`.
272  *
273  */
275  /**
276  * The index / ID of the info partition. Unused for data partitions, since
277  * there is only one in a given bank.
278  */
279  uint32_t partition_id;
280  /**
281  * Byte address (in the flash address space) to start the operation.
282  *
283  * For operations at a granularity of words, the flash controller will
284  * truncate to the closest, lower word aligned address. For example, if 0x13
285  * is supplied for a read, the controller will perform a read at address 0x10.
286  *
287  * Similarly, for page erase and bank erase operations, the flash controller
288  * will truncate to the closest lower page-aligned or bank-aligned address,
289  * respectively.
290  */
291  uint32_t byte_address;
292  /**
293  * Number of 32-bit words in the operation. Must be in the range [1,4096]
294  * (inclusive) for program or read operations. Unused for erase operations.
295  */
296  uint32_t word_count;
298 
299 /**
300  * Start a flash controller operation.
301  *
302  * Note that not all underlying flash memory supports the
303  * `kDifFlashCtrlOpProgramRepair` operation. When specifying this in the `op`,
304  * `dif_flash_ctrl_get_phy_status()` can first be used to ensure the operation
305  * is supported.
306  *
307  * @param handle The flash controller device to execute the transaction on.
308  * @param transaction The parameters that constitute the operation to start.
309  * @return `kDifBadArg` if `handle` is null or `transaction` contains an invalid
310  * or unsupported operation, `kDifUnavailable` if a flash transaction is in
311  * progress at the controller, `kDifIpFifoFull` if the FIFOs are not empty, and
312  * `kDifOk` otherwise.
313  */
316  dif_flash_ctrl_transaction_t transaction);
317 
318 /**
319  * Start a flash controller operation, with fewer safeguards. (unsafe version)
320  *
321  * Unlike `dif_flash_ctrl_start()`, this variant does not check whether the info
322  * partition id is valid, whether the word count is valid, or whether the FIFOs
323  * have been emptied. This function is intended to be used to trigger errors on
324  * purpose, for testing the hardware responses.
325  *
326  * Note that not all underlying flash memory supports the
327  * `kDifFlashCtrlOpProgramRepair` operation. When specifying this in the `op`,
328  * `dif_flash_ctrl_get_phy_status()` can first be used to ensure the operation
329  * is supported.
330  *
331  * @param handle The flash controller device to execute the transaction on.
332  * @param transaction The parameters that constitute the operation to start.
333  * @return `kDifBadArg` if `handle` is null, `kDifUnavailable` if a flash
334  * transaction is in progress, and `kDifOk` otherwise.
335  */
339 
340 /**
341  * Suspend an ongoing erase transaction.
342  *
343  * If no erase transaction is ongoing, returns with no effect.
344  *
345  * @param handle flash controller device to operate on.
346  * @return `kDifBadArg` if `handle` is null. Otherwise, `kDifOk`.
347  */
350 
351 /**
352  * Query the controller for any erase suspension requests pending.
353  *
354  * @param handle The flash controller to query.
355  * @param request_pending_out Out parameter. The location to write whether an
356  * erase suspension request is pending.
357  * @return `kDifBadArg` if `handle` or `request_pending_out` are null. `kDifOk`
358  * otherwise.
359  */
362  dif_flash_ctrl_state_t *handle, bool *request_pending_out);
363 
364 /**
365  * Push data to the program FIFO. (unsafe version)
366  *
367  * This function is primarily for testing hardware error responses. If you are
368  * not specifically trying to force an error, consider
369  * `dif_flash_ctrl_prog_fifo_push()` instead. This function does not check if a
370  * program transaction has begun, nor if the number of words would exceed the
371  * size of the current transaction.
372  *
373  * Attempts to write the contents of `data` to the program FIFO without
374  * consideration for the current controller state.
375  *
376  * It is up to the caller to call `dif_flash_ctrl_end()` to ensure the
377  * flash controller completed this transaction successfully.
378  *
379  * @param handle flash controller device to push data to.
380  * @param word_count The number of words to write.
381  * @param data The data to write.
382  * @return `kDifBadArg` if `handle` or `data` are null. `kDifOk` otherwise.
383  */
386  dif_flash_ctrl_state_t *handle, uint32_t word_count, const uint32_t *data);
387 
388 /**
389  * Push data to the program FIFO.
390  *
391  * Attempts to write the contents of `data` to the program FIFO. It is required
392  * that a program transaction be started prior to calling this function, else
393  * the call will fail with `kDifError`.
394  *
395  * The following conditions are also required:
396  * - `data` must reference a contiguous, allocated, readable region of at
397  * least `word_count` words, violation of this will produce undefined
398  * behavior.
399  * - The first call to this function after starting the program transaction
400  * the same `word_count` must not exceed what was supplied at the start of
401  * the program transaction.
402  * - Each subsequent call the new `word_count` must not exceed `word_count -
403  * words_sent_out` from the previous call.
404  * All deviations on the above will produce a `kDifBadArg` error
405  * unless otherwise specified.
406  *
407  * If the FIFO fills up, this function will cause the CPU to block until the
408  * flash controller frees up more space.
409  *
410  * It is up to the caller to call `dif_flash_ctrl_end()` to ensure the
411  * flash controller completed this transaction successfully.
412  *
413  * @param handle flash controller device to push data to.
414  * @param word_count The number of words to write.
415  * @param data The data to write.
416  * program FIFO, will contain the number of words pushed.
417  * @return `kDifBadArg` if `handle` or `data` are null or if the value of
418  * `word_count` is illegal. `kDifError` if a program transaction was not
419  * started. `kDifOk` otherwise.
420  */
423  uint32_t word_count,
424  const uint32_t *data);
425 
426 /**
427  * Read data from the read FIFO. (unsafe version)
428  *
429  * This function is primarily for testing hardware error responses. If you are
430  * not specifically trying to force an error, consider
431  * `dif_flash_ctrl_read_fifo_pop()` instead. This function does not check if a
432  * transaction is currently in progress, nor if this would cause a read beyond
433  * the number of words for the current operation.
434  *
435  * Attempts to read `word_count` words from the read FIFO without consideration
436  * for the current controller state.
437  *
438  * It is up to the caller to call `dif_flash_ctrl_end()` to ensure the
439  * flash controller completed this transaction successfully.
440  *
441  * @param handle flash controller device to push data to.
442  * @param word_count The number of words to read.
443  * @param data_out The region in memory to store the data read off the FIFO.
444  * @return `kDifBadArg` if `handle` or `data` are null. `kDifOk` otherwise.
445  */
448  uint32_t word_count,
449  uint32_t *data_out);
450 
451 /**
452  * Read data from the read FIFO.
453  *
454  * Attempts to read `word_count` words from the read FIFO.
455  *
456  * The following conditions are required:
457  * - `data_out` must reference a contiguous, allocated, writable region of at
458  * least `word_count` words, violation of this will produce undefined
459  * behavior.
460  * - The first call to this function after starting the program transaction
461  * the same `word_count` must not exceed what was supplied at the start of
462  * the read transaction.
463  * - Each subsequent call the new `word_count` must not exceed `word_count -
464  * words_received` from the previous call.
465  * All deviations on the above will produce a `kDifBadArg` error unless
466  * otherwise specified.
467  *
468  * If the FIFO empties this function will cause the CPU to block until the flash
469  * controller fills the FIFO with more data.
470  *
471  * It is up to the caller to call `dif_flash_ctrl_end()` to ensure the
472  * flash controller completed this transaction successfully.
473  *
474  * @param handle flash controller device to pull data from.
475  * @param word_count The number of words to read.
476  * @param data_out The region in memory to store the data read off the FIFO.
477  * @param words_read_out Out-parameter, the number of words read.
478  * @return `kDifBadArg` if `handle` or `data_out` are null, or if the value of
479  * `word_count` is illegal. `kDifOk` otherwise.
480  */
483  uint32_t word_count,
484  uint32_t *data_out);
485 
487  /**
488  * Access permission error.
489  */
491  /**
492  * Uncorrectable flash read data error.
493  */
494  bool read_error : 1;
495  /**
496  * Flash program has a window resolution error. In other words, the start of
497  * program and end of program are in different windows.
498  */
500  /**
501  * Flash program operation selected an unavailable type. For example, the
502  * repair type of programming was selected, but the flash did not support it.
503  */
504  bool prog_type_error : 1;
505  /**
506  * A shadow register encountered an update error.
507  */
510 
511 /**
512  * Represents recoverable errors and synchronous errors caused by software.
513  */
514 typedef struct dif_flash_ctrl_error {
515  /**
516  * For errors associated with an address, the address where the error
517  * occurred.
518  */
519  uint32_t address;
520 
521  /**
522  * A set of error codes.
523  */
526 
527 /**
528  * Get the error codes and address of the last associated error.
529  *
530  * @param handle flash controller device to query.
531  * @param error_code_out Output parameter. The location where the current status
532  * is to be stored.
533  * @return `kDifBadArg` if `handle` or `error_code_out` are null. `kDifOk`
534  * otherwise.
535  */
538  const dif_flash_ctrl_state_t *handle,
539  dif_flash_ctrl_error_t *error_code_out);
540 
541 /**
542  * Clear the error codes.
543  *
544  * @param handle flash controller device to set.
545  * @return `kDifBadArg` if `handle` is null. `kDifOk` otherwise.
546  */
550 
551 typedef struct dif_flash_ctrl_output {
552  /**
553  * The error code associated with an `operation_error`. This field is only
554  * valid if `operation_error` is true.
555  */
557  /**
558  * Flash operation done.
559  */
560  bool operation_done : 1;
561  /**
562  * Flash operation error.
563  */
564  bool operation_error : 1;
565 
567 
568 /**
569  * Check that the transaction has completed and return any resulting error
570  * codes. Does not clear the error codes, but does clear the operation status if
571  * the operation has terminated.
572  *
573  * @param handle flash controller device for completing the transaction.
574  * @param out Out parameter. Contains the transaction status and error codes.
575  * @return `kDifBadArg` if `handle` or `out` are null, `kDifError` if no
576  * transaction was pending, `kDifUnavailable` if the controller is still
577  * processing the transaction, `kDifIpFifoFull` if there are still words
578  * remaining for the FIFOs, and `kDifOk` otherwise.
579  */
583 
584 /**
585  * Memory protection configuration options.
586  */
588  /** Read enable flag. */
589  multi_bit_bool_t rd_en;
590  /** Program enable flag. */
591  multi_bit_bool_t prog_en;
592  /** Erase enable flag. */
593  multi_bit_bool_t erase_en;
594  /** Scramble enable flag*/
595  multi_bit_bool_t scramble_en;
596  /** ECC enable flag. */
597  multi_bit_bool_t ecc_en;
598  /** High-endurance enable flag. */
599  multi_bit_bool_t high_endurance_en;
601 
602 /**
603  * A composite of a data region and its memory properties.
604  */
606  /** Region base page index. */
607  uint32_t base;
608  /** Region config size (in number of pages). */
609  uint32_t size;
610  /** Memory properties of the region. */
613 
614 /**
615  * Representation of the memory protection regions for the info pages.
616  */
618  /** Region bank ID. */
619  uint32_t bank;
620  /** Region info partition ID. */
621  uint32_t partition_id;
622  /** Region page index. */
623  uint32_t page;
625 
626 /**
627  * Enable/disable the data region of flash indexed by `region`.
628  *
629  * This may only be done if region configuration has not been locked.
630  *
631  * @param handle flash controller device to configure.
632  * @param region The region to operate on.
633  * @param enable Enable or disable this `region`.
634  * @return `kDifBadArg` if `handle` is null or `region` is invalid, `kDifLocked`
635  * if region configuration has been locked, and `kDifOk` otherwise.
636  */
639  dif_flash_ctrl_state_t *handle, uint32_t region, dif_toggle_t enable);
640 
641 /**
642  * Get the enabled/disabled state for the indicated data `region`.
643  *
644  * @param handle flash controller device to query.
645  * @param region The region in question.
646  * @param enabled_out Out-parameter, the enabled/disabled state of this region.
647  * @return `kDifBadArg ` if `handle` or `enabled_out` is null, or if `region` is
648  * invalid, `kDifOk` otherwise.
649  */
652  const dif_flash_ctrl_state_t *handle, uint32_t region,
653  dif_toggle_t *enabled_out);
654 
655 /**
656  * Enable/disable the info region of flash identified by `region`.
657  *
658  * This may only be done if region configuration has not been locked.
659  *
660  * @param handle flash controller device to configure.
661  * @param region The region to operate on.
662  * @param enable Enable or disable this `region`.
663  * @return `kDifBadArg` if `handle` is null or `region` is invalid, `kDifLocked`
664  * if region configuration has been locked, and `kDifOk` otherwise.
665  */
669  dif_toggle_t enable);
670 
671 /**
672  * Get the enabled/disabled state for the indicated info `region`.
673  *
674  * @param handle flash controller device to query.
675  * @param region The region in question.
676  * @param enabled_out Out-parameter, the enabled/disabled state of this region.
677  * @return `kDifBadArg ` if `handle` or `enabled_out` is null, or if `region` is
678  * invalid, `kDifOk` otherwise.
679  */
683  dif_toggle_t *enabled_out);
684 
685 /**
686  * Set the default memory properties for data regions, when not overridden by a
687  * specific entry in the data region memory properties table.
688  *
689  * @param handle flash controller device to configure.
690  * @param properties The memory properties to control.
691  * @return `kDifBadArg` if `handle` is null, `kDifOk` otherwise.
692  */
695  dif_flash_ctrl_state_t *handle,
697 
698 /**
699  * Get the default memory properties for data regions, when they are not
700  * overridden by a specific entry in the data region memory properties table.
701  *
702  * @param handle flash controller device to query.
703  * @param properties_out Out-parameter, a pointer to a
704  * `dif_flash_ctrl_region_properties_t` struct for writing the memory
705  * properties.
706  * @return `kDifBadArg` if `handle` or `properties_out` are null. `kDifOk`
707  * otherwise.
708  */
711  const dif_flash_ctrl_state_t *handle,
712  dif_flash_ctrl_region_properties_t *properties_out);
713 
714 /**
715  * Set memory properties for the data region of flash indexed by `region`.
716  *
717  * This may only be done if region configuration has not been locked.
718  *
719  * @param handle flash controller device to configure.
720  * @param region The region to operate on.
721  * @param config The memory properties to control for this `region`.
722  * @return `kDifBadArg` if `handle` is null or `region` is invalid, `kDifLocked`
723  * if region configuration has been locked, and `kDifOk` otherwise.
724  */
727  dif_flash_ctrl_state_t *handle, uint32_t region,
729 
730 /**
731  * Get the memory properties for the data region of flash indexed by `region`.
732  *
733  * @param handle flash controller device to query.
734  * @param region The index of the region entry in question.
735  * @param config_out Out-parameter, a pointer to a
736  * `dif_flash_ctrl_data_region_properties_t` struct for writing the full entry
737  * showing the region configuration and its memory properties.
738  * @return `kDifBadArg` if `handle` or `properties_out` are null or if `region`
739  * is invalid. `kDifOk` otherwise.
740  */
743  const dif_flash_ctrl_state_t *handle, uint32_t region,
745 
746 /**
747  * Set memory properties for the info region of flash identified by `region`.
748  *
749  * This may only be done if region configuration has not been locked.
750  *
751  * @param handle flash controller device to configure.
752  * @param region The region to operate on.
753  * @param properties The memory properties to control for this `region`.
754  * @return `kDifBadArg` if `handle` is null or `region` is invalid, `kDifLocked`
755  * if region configuration has been locked, and `kDifOk` otherwise.
756  */
761 
762 /**
763  * Get the memory properties for the info region of flash identified by
764  * `region`.
765  *
766  * @param handle flash controller device to query.
767  * @param region The index of the region entry in question.
768  * @param properties_out Out-parameter, a pointer to a
769  * `dif_flash_ctrl_region_properties_t` struct for writing memory properties
770  * associated with the indicated `region`.
771  * @return `kDifBadArg` if `handle` or `properties_out` are null or if `region`
772  * is invalid. `kDifOk` otherwise.
773  */
777  dif_flash_ctrl_region_properties_t *properties_out);
778 
779 /**
780  * Lock data region memory properties configuration until the device is reset.
781  *
782  * This will prevent any further configuration of region properties until device
783  * reset. Future calls to functions that set data region memory properties will
784  * return `kDifLocked`.
785  *
786  * @param handle flash controller device to lock region configuration on.
787  * @param region The region to lock.
788  * @return `kDifBadArg` if `handle` is null, or if `region` is invalid,
789  * `kDifLocked` if configuration is already locked, `kDifOk` otherwise.
790  */
793  dif_flash_ctrl_state_t *handle, uint32_t region);
794 
795 /**
796  * Lock info region memory properties configuration until the device is reset.
797  *
798  * This will prevent any further configuration of region properties until device
799  * reset. Future calls to functions that set info region memory properties will
800  * return `kDifLocked`.
801  *
802  * @param handle flash controller device to lock region configuration on.
803  * @param region The region to lock.
804  * @return `kDifBadArg` if `handle` is null, or if `region` is invalid,
805  * `kDifLocked` if configuration is already locked, `kDifOk` otherwise.
806  */
810 
811 /**
812  * Query the state of the region configuration lock for the given data `region`
813  * entry's index.
814  *
815  * This function checks if memory region configuration is still enabled or if it
816  * has been locked. Once locked, region configuration cannot be enabled again,
817  * and all calls to region configuration functions will return `kDifLocked`
818  * until the device is restarted.
819  *
820  * @param handle flash controller device to check the lock state for.
821  * @param region The region in question.
822  * @param locked_out Out-parameter, the current state of the region's
823  * configuration lock.
824  * @return `kDifBadArg` if `handle` or `locked_out` is null, or if `region` is
825  * invalid, `kDifOk` otherwise.
826  */
829  const dif_flash_ctrl_state_t *handle, uint32_t region, bool *locked_out);
830 
831 /**
832  * Query the state of the region configuration lock for the given info `region`.
833  *
834  * This function checks if memory region configuration is still enabled or if it
835  * has been locked. Once locked, region configuration cannot be enabled again,
836  * and all calls to region configuration functions will return `kDifLocked`
837  * until the device is restarted.
838  *
839  * @param handle flash controller device to check the lock state for.
840  * @param region The region in question.
841  * @param locked_out Out-parameter, the current state of the region's
842  * configuration lock.
843  * @return `kDifBadArg` if `handle` or `locked_out` is null, or if `region` is
844  * invalid, `kDifOk` otherwise.
845  */
849  bool *locked_out);
850 
851 /**
852  * Enable/disable erase operations for a particular flash bank.
853  *
854  * This may only be done if bank configuration is still enabled.
855  *
856  * @param handle flash controller device to operate on.
857  * @param bank The bank to configure.
858  * @param enable Enable/disable erase access for this bank.
859  * @return `kDifBadArg` if `handle` is null or `bank` is invalid, `kDifLocked`
860  * if bank configuration is locked, and `kDifOk` otherwise.
861  */
864  dif_flash_ctrl_state_t *handle, uint32_t bank, dif_toggle_t enable);
865 
866 /**
867  * Query the erase permissions for a particular flash bank.
868  *
869  * @param handle flash controller device to query.
870  * @param bank The bank to query.
871  * @param enabled_out Out-parameter, the erase permissions for this bank.
872  * @return `kDifBadArg` if `handle` or `enabled_out` is null and `kDifOk`
873  * otherwise.
874  */
877  const dif_flash_ctrl_state_t *handle, uint32_t bank,
878  dif_toggle_t *enabled_out);
879 
880 /**
881  * Lock bank configuration until the device is reset.
882  *
883  * This will prevent any further configuration of memory banks until device
884  * reset. Future calls to bank configuration functions will return `kDifLocked`.
885  *
886  * @param handle flash controller device to lock region configuration on.
887  * @return `kDifBadArg` if `handle` is null, `kDifLocked` if already locked,
888  * and `kDifOk` otherwise.
889  */
892  dif_flash_ctrl_state_t *handle);
893 
894 /**
895  * Query the state of the bank configuration lock.
896  *
897  * This function checks if memory bank configuration is still enabled or if it
898  * has been locked. Once locked, bank configuration cannot be enabled again, and
899  * all calls to bank configuration functions will return `kDifLocked` until the
900  * device is restarted.
901  *
902  * @param handle flash controller device to check the lock state for.
903  * @param locked_out Out-parameter, the current state of the bank configuration
904  * lock.
905  * @return `kDifBadArg` if `handle` or `locked_out` is null, `kDifOk` otherwise.
906  */
909  const dif_flash_ctrl_state_t *handle, bool *locked_out);
910 
911 /**
912  * Set the interrupt watermarks for the program FIFO.
913  *
914  * The value of `level` defines the level the program FIFO must drain to before
915  * triggering a `prog_lvl` interrupt.
916  *
917  * This interrupts will only trigger if enabled through the interrupt API.
918  *
919  * @param handle flash controller to set FIFO level watermarks for.
920  * @param prog Trigger an interrupt when the program FIFO drains to this level.
921  * @return `kDifBadArg` if `handle` is null or if the value `level` is out of
922  * range, `kDifOk` otherwise.
923  */
926  dif_flash_ctrl_state_t *handle, uint32_t level);
927 
928 /**
929  * Set the interrupt watermarks for the read FIFO.
930  *
931  * The value of `level` defines the level the read FIFO must fill to before
932  * triggering a `rd_lvl` interrupt.
933  *
934  * This interrupt will only trigger if enabled through the interrupt API.
935  *
936  * @param handle flash controller to set FIFO level watermarks for.
937  * @param level Trigger an interrupt when the read FIFO fills to this level.
938  * @return `kDifBadArg` if `flash_ctrl` is null or if the value of `prog` is out
939  * of range, `kDifOk` otherwise.
940  */
943  dif_flash_ctrl_state_t *handle, uint32_t level);
944 
945 /**
946  * Get the interrupt watermarks for the program and read FIFOs.
947  *
948  * @param handle flash controller to get FIFO level watermarks from.
949  * @param prog_out Out-parameter, the prog FIFO watermark level. If the prog
950  * FIFO empties to this level, a corresponding status bit and possibly an
951  * interrupt may be generated. May be null.
952  * @param read_out Out-parameter, the read FIFO watermark level. If the read
953  * FIFO fills to this level, a corresponding status bit and possibly an
954  * interrupt may be generated. May be null.
955  * @return `kDifBadArg` if `handle` is null, `kDifOk` otherwise.
956  */
959  const dif_flash_ctrl_state_t *handle, uint32_t *prog_out,
960  uint32_t *read_out);
961 
962 /**
963  * Resets both the program and read FIFOs.
964  *
965  * This is useful in the event of an unexpected error as a means of reseting
966  * state.
967  *
968  * @param handle flash controller device to clear FIFOs on.
969  * @return `kDifBadArg` if `handle` is null, `kDifOk` otherwise.
970  */
973 
974 typedef struct dif_flash_ctrl_faults {
975  /** The flash hardware interface encountered a memory permission error. */
977  /** The flash hardware interface encountered a read data error. */
978  bool read_error : 1;
979  /** The flash hardware interface encountered a program resolution error. */
981  /** The flash hardware interface encountered a program type error. */
982  bool prog_type_error : 1;
983  /** A host transaction was granted with illegal properties. */
984  bool host_gnt_error : 1;
985  /** The flash controller encountered a register integrity error. */
987  /** The flash memory encountered a register integrity error. */
989  /** The lifecycle management interface encountered a fatal error. */
991  /** A shadow register encountered a storage fault. */
994 
995 /**
996  * Gets the unrecoverable or hardware faults.
997  *
998  * @param handle flash controller device to query
999  * @param faults_out Out parameter, a pointer to the location to write the fault
1000  * values.
1001  * @return `kDifBadArg` if `handle` or `fault_out` are null. `kDifOk` otherwise.
1002  */
1005  dif_flash_ctrl_faults_t *faults_out);
1006 
1008  /** Count of the number of single-bit ECC errors. */
1010  /** The last address that produced a single-bit ECC error. */
1013 
1014 /**
1015  * Get the ECC error information for the specified flash `bank`.
1016  *
1017  * @param handle The flash controller device to query.
1018  * @param bank The desired bank to look up errors for.
1019  * @return `kDifBadArg` if `handle` or `errors_out` are null or if `bank` is
1020  * invalid. `kDifOk` otherwise.
1021  */
1024  const dif_flash_ctrl_state_t *handle, uint32_t bank,
1025  dif_flash_ctrl_ecc_errors_t *errors_out);
1026 
1028  /** Flash phy controller is initializing. */
1029  bool phy_init_wip : 1;
1030  /** Normal programming is supported. */
1032  /** Repair programming is supported. */
1035 
1036 /**
1037  * Query the status registers on the flash controller.
1038  *
1039  * This function checks the various status bits as described in
1040  * `dif_flash_ctrl_phy_status_t`.
1041  *
1042  * @param handle The flash controller device with the desired phy's status bit
1043  * to check.
1044  * @param status_out Out-parameter, the current status of the flash phy.
1045  * @return `kDifBadArg` if `handle` or `status_out` is null, `kDifOk`
1046  * otherwise.
1047  */
1050  const dif_flash_ctrl_state_t *handle,
1051  dif_flash_ctrl_phy_status_t *status_out);
1052 
1053 /**
1054  * Set the value of the scratch register.
1055  *
1056  * @param handle The flash controller device to operate on.
1057  * @param value The value to set the scratch register to.
1058  * @return `kDifBadArg` if `handle` is null, `kDifOk` otherwise.
1059  */
1062  uint32_t value);
1063 
1064 /**
1065  * Get the value of the scratch register.
1066  *
1067  * @param handle The flash controller device to query.
1068  * @param value_out Out parameter. The location to write the scratch register's
1069  * value to.
1070  * @return `kDifBadArg` if `handle` or `value_out` are null. `kDifOk` otherwise.
1071  */
1074  uint32_t *value_out);
1075 
1076 // TODO: PHY alerts, which are separate from the autogenerated alerts.
1077 
1078 #ifdef __cplusplus
1079 } // extern "C"
1080 #endif // __cplusplus
1081 
1082 #endif // OPENTITAN_SW_DEVICE_LIB_DIF_DIF_FLASH_CTRL_H_