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