Software APIs
retention_sram.c
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 #include "sw/device/silicon_creator/lib/drivers/retention_sram.h"
6 
7 #include <assert.h>
8 
9 #include "dt/dt_sram_ctrl.h"
12 
13 #include "sram_ctrl_regs.h" // Generated.
14 
15 static const dt_sram_ctrl_t kSramCtrlDt = kDtSramCtrlRetAon;
16 
17 void retention_sram_clear(void) {
18  memset(retention_sram_get(), 0, sizeof(retention_sram_t));
19 }
20 
21 void retention_sram_init(void) {
22  uint32_t reg = bitfield_bit32_write(0, SRAM_CTRL_CTRL_INIT_BIT, true);
23  uint32_t base = dt_sram_ctrl_primary_reg_block(kSramCtrlDt);
24  abs_mmio_write32(base + SRAM_CTRL_CTRL_REG_OFFSET, reg);
25 }
26 
27 void retention_sram_readback_enable(uint32_t en) {
28  uint32_t base = dt_sram_ctrl_primary_reg_block(kSramCtrlDt);
29  abs_mmio_write32(base + SRAM_CTRL_READBACK_REG_OFFSET, en);
30 }
31 
32 void retention_sram_scramble(void) {
33  // Request the renewal of the scrambling key and initialization to random
34  // values.
35  uint32_t ctrl = 0;
36  ctrl = bitfield_bit32_write(ctrl, SRAM_CTRL_CTRL_RENEW_SCR_KEY_BIT, true);
37  ctrl = bitfield_bit32_write(ctrl, SRAM_CTRL_CTRL_INIT_BIT, true);
38  uint32_t base = dt_sram_ctrl_primary_reg_block(kSramCtrlDt);
39  abs_mmio_write32(base + SRAM_CTRL_CTRL_REG_OFFSET, ctrl);
40 }
41 
42 rom_error_t retention_sram_check_version(void) {
43  retention_sram_t *rr = retention_sram_get();
44  switch (rr->version) {
45  case kRetentionSramVersion1:
46  // Version 1 can be in-place upgraded to version 4.
47  rr->version = kRetentionSramVersion4;
48  break;
49  case kRetentionSramVersion4:
50  // Nothing to do for version 4.
51  break;
52  case kRetentionSramVersion3:
53  case kRetentionSramVersion2:
54  default:
55  // Versions 2 and 3 never went into a product, so we should never see
56  // them. Other versions are not defined.
57  return kErrorRetRamBadVersion;
58  }
59  return kErrorOk;
60 }
61 
62 // Extern declarations for the inline functions in the header.
63 extern retention_sram_t *retention_sram_get(void);