|
| 1 | +// Licensed under the Apache License, Version 2.0 or the MIT License. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 OR MIT |
| 3 | +// Copyright OxidOS Automotive 2026. |
| 4 | + |
| 5 | +use core::cell::Cell; |
| 6 | + |
| 7 | +use kernel::deferred_call::{DeferredCall, DeferredCallClient}; |
| 8 | +use kernel::hil::entropy::{self, Client32, Continue, Entropy32}; |
| 9 | +use kernel::utilities::cells::OptionalCell; |
| 10 | +use kernel::utilities::registers::interfaces::{ReadWriteable, Readable}; |
| 11 | +use kernel::utilities::registers::{register_bitfields, register_structs, ReadOnly, ReadWrite}; |
| 12 | +use kernel::utilities::StaticRef; |
| 13 | + |
| 14 | +register_structs! { |
| 15 | + /// Random number generator |
| 16 | + pub RngRegisters { |
| 17 | + /// control register |
| 18 | + (0x000 => cr: ReadWrite<u32, CR::Register>), |
| 19 | + /// status register |
| 20 | + (0x004 => sr: ReadWrite<u32, SR::Register>), |
| 21 | + /// data register |
| 22 | + (0x008 => dr: ReadOnly<u32>), |
| 23 | + (0x00C => nscr: ReadWrite<u32, NSCR::Register>), |
| 24 | + /// health test control register |
| 25 | + (0x010 => htcr: ReadWrite<u32, HTCR::Register>), |
| 26 | + (0x014 => @END), |
| 27 | + } |
| 28 | +} |
| 29 | +register_bitfields![u32, |
| 30 | + pub CR [ |
| 31 | + /// RNG Config Lock |
| 32 | + CONFIGLOCK OFFSET(31) NUMBITS(1) [], |
| 33 | + /// Conditioning soft reset |
| 34 | + CONDRST OFFSET(30) NUMBITS(1) [], |
| 35 | + /// RNG configuration 1 |
| 36 | + RNG_CONFIG1 OFFSET(20) NUMBITS(6) [], |
| 37 | + /// Clock divider factor |
| 38 | + CLKDIV OFFSET(16) NUMBITS(4) [], |
| 39 | + /// RNG configuration 2 |
| 40 | + RNG_CONFIG2 OFFSET(13) NUMBITS(3) [], |
| 41 | + /// Non NIST compliant |
| 42 | + NISTC OFFSET(12) NUMBITS(1) [], |
| 43 | + /// RNG configuration 3 |
| 44 | + RNG_CONFIG3 OFFSET(8) NUMBITS(4) [], |
| 45 | + /// Auto reset disable |
| 46 | + ARDIS OFFSET(7) NUMBITS(1) [], |
| 47 | + /// Clock error detection |
| 48 | + CED OFFSET(5) NUMBITS(1) [], |
| 49 | + /// Interrupt Enable |
| 50 | + IE OFFSET(3) NUMBITS(1) [], |
| 51 | + /// True random number generator enable |
| 52 | + RNGEN OFFSET(2) NUMBITS(1) [] |
| 53 | + ], |
| 54 | + pub SR [ |
| 55 | + /// Seed error interrupt status |
| 56 | + SEIS OFFSET(6) NUMBITS(1) [], |
| 57 | + /// Clock error interrupt status |
| 58 | + CEIS OFFSET(5) NUMBITS(1) [], |
| 59 | + /// Seed error current status |
| 60 | + SECS OFFSET(2) NUMBITS(1) [], |
| 61 | + /// Clock error current status |
| 62 | + CECS OFFSET(1) NUMBITS(1) [], |
| 63 | + /// Data ready |
| 64 | + DRDY OFFSET(0) NUMBITS(1) [] |
| 65 | + ], |
| 66 | + pub DR [ |
| 67 | + /// Random data |
| 68 | + RNDATA OFFSET(0) NUMBITS(32) [] |
| 69 | + ], |
| 70 | + pub NSCR [ |
| 71 | + /// noise source control register |
| 72 | + NSCFG OFFSET(0) NUMBITS(32) [] |
| 73 | + ], |
| 74 | + pub HTCR [ |
| 75 | + /// health test configuration |
| 76 | + HTCFG OFFSET(0) NUMBITS(32) [] |
| 77 | + ] |
| 78 | +]; |
| 79 | +pub const RNG_BASE: StaticRef<RngRegisters> = |
| 80 | + unsafe { StaticRef::new(0x520C0800 as *const RngRegisters) }; |
| 81 | + |
| 82 | +struct TrngIter<'a, 'b: 'a>(&'a Trng<'b>); |
| 83 | +impl Iterator for TrngIter<'_, '_> { |
| 84 | + type Item = u32; |
| 85 | + |
| 86 | + fn next(&mut self) -> Option<u32> { |
| 87 | + if self.0.registers.sr.is_set(SR::DRDY) { |
| 88 | + Some(self.0.registers.dr.get()) |
| 89 | + } else { |
| 90 | + None |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +pub struct Trng<'a> { |
| 96 | + registers: StaticRef<RngRegisters>, |
| 97 | + client: OptionalCell<&'a dyn Client32>, |
| 98 | + entropy_needed: Cell<bool>, |
| 99 | + deferred_call: DeferredCall, |
| 100 | +} |
| 101 | + |
| 102 | +impl<'a> Trng<'a> { |
| 103 | + pub const fn new(base: StaticRef<RngRegisters>, deferred_call: DeferredCall) -> Self { |
| 104 | + Self { |
| 105 | + registers: base, |
| 106 | + client: OptionalCell::empty(), |
| 107 | + entropy_needed: Cell::new(false), |
| 108 | + deferred_call: deferred_call, |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + pub fn init(&self) { |
| 113 | + // specified in the documentation (NIST compliant RNG configuration table in AN4230 available from www.st.com.) |
| 114 | + // that values for the CR, HTCR and NSCR should be 0x00F11F00, 0x76B3 and 0x24C2 respectivly |
| 115 | + self.registers.cr.modify( |
| 116 | + CR::RNG_CONFIG3.val(0b1111) |
| 117 | + + CR::NISTC::SET |
| 118 | + + CR::CLKDIV.val(0x1) |
| 119 | + + CR::RNG_CONFIG1.val(0b1111) |
| 120 | + + CR::CONDRST::SET, |
| 121 | + ); |
| 122 | + self.registers.htcr.modify(HTCR::HTCFG.val(0x76B3)); |
| 123 | + self.registers.nscr.modify(NSCR::NSCFG.val(0x24C2)); |
| 124 | + self.registers.cr.modify(CR::CONFIGLOCK::SET); |
| 125 | + self.registers |
| 126 | + .cr |
| 127 | + .modify(CR::RNGEN::SET + CR::IE::SET + CR::CONDRST::CLEAR); |
| 128 | + } |
| 129 | + fn send_data(&self) { |
| 130 | + let response = self |
| 131 | + .client |
| 132 | + .map(|client| client.entropy_available(&mut TrngIter(self), Ok(()))); |
| 133 | + match response { |
| 134 | + Some(Continue::Done) | None => self.entropy_needed.set(false), |
| 135 | + _ => {} |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + pub fn handle_interrupt(&self) { |
| 140 | + let regs = self.registers; |
| 141 | + if regs.sr.any_matching_bits_set(SR::DRDY::SET) && self.entropy_needed.get() { |
| 142 | + self.send_data(); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +impl<'a> Entropy32<'a> for Trng<'a> { |
| 148 | + fn get(&self) -> Result<(), kernel::ErrorCode> { |
| 149 | + let regs = self.registers; |
| 150 | + if regs.sr.any_matching_bits_set(SR::CECS::SET + SR::SECS::SET) { |
| 151 | + return Err(kernel::ErrorCode::FAIL); |
| 152 | + } |
| 153 | + self.entropy_needed.set(true); |
| 154 | + self.deferred_call.set(); |
| 155 | + |
| 156 | + Ok(()) |
| 157 | + } |
| 158 | + |
| 159 | + fn cancel(&self) -> Result<(), kernel::ErrorCode> { |
| 160 | + if self.entropy_needed.get() { |
| 161 | + self.entropy_needed.set(false); |
| 162 | + } |
| 163 | + Ok(()) |
| 164 | + } |
| 165 | + |
| 166 | + fn set_client(&'a self, client: &'a dyn kernel::hil::entropy::Client32) { |
| 167 | + self.client.set(client); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +impl DeferredCallClient for Trng<'_> { |
| 172 | + fn handle_deferred_call(&self) { |
| 173 | + if !self.entropy_needed.get() { |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + if self.registers.sr.any_matching_bits_set(SR::DRDY::SET) { |
| 178 | + self.send_data(); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + fn register(&'static self) { |
| 183 | + self.deferred_call.register(self); |
| 184 | + } |
| 185 | +} |
0 commit comments