drivers/char/hw_random/stm32-rng.c
Source file repositories/reference/linux-study-clean/drivers/char/hw_random/stm32-rng.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/char/hw_random/stm32-rng.c- Extension
.c- Size
- 16203 bytes
- Lines
- 613
- Domain
- Driver Families
- Bucket
- drivers/char
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/clk.hlinux/clk-provider.hlinux/delay.hlinux/hw_random.hlinux/io.hlinux/iopoll.hlinux/kernel.hlinux/module.hlinux/of.hlinux/of_address.hlinux/platform_device.hlinux/pm_runtime.hlinux/reset.hlinux/slab.h
Detected Declarations
struct stm32_rng_datastruct stm32_rng_configstruct stm32_rng_privatefunction sourcefunction sourcefunction stm32_rng_conceal_seed_errorfunction stm32_rng_readfunction stm32_rng_clock_freq_restrainfunction stm32_rng_initfunction stm32_rng_removefunction stm32_rng_runtime_suspendfunction stm32_rng_suspendfunction stm32_rng_runtime_resumefunction stm32_rng_resumefunction stm32_rng_probe
Annotated Snippet
struct stm32_rng_data {
uint max_clock_rate;
uint nb_clock;
u32 cr;
u32 nscr;
u32 htcr;
bool has_cond_reset;
};
/**
* struct stm32_rng_config - RNG configuration data
*
* @cr: RNG configuration. 0 means default hardware RNG configuration
* @nscr: Noise sources control configuration.
* @htcr: Health tests configuration.
*/
struct stm32_rng_config {
u32 cr;
u32 nscr;
u32 htcr;
};
struct stm32_rng_private {
struct hwrng rng;
struct device *dev;
void __iomem *base;
struct clk_bulk_data *clk_bulk;
struct reset_control *rst;
struct stm32_rng_config pm_conf;
const struct stm32_rng_data *data;
bool ced;
bool lock_conf;
};
/*
* Extracts from the STM32 RNG specification when RNG supports CONDRST.
*
* When a noise source (or seed) error occurs, the RNG stops generating
* random numbers and sets to “1” both SEIS and SECS bits to indicate
* that a seed error occurred. (...)
*
* 1. Software reset by writing CONDRST at 1 and at 0 (see bitfield
* description for details). This step is needed only if SECS is set.
* Indeed, when SEIS is set and SECS is cleared it means RNG performed
* the reset automatically (auto-reset).
* 2. If SECS was set in step 1 (no auto-reset) wait for CONDRST
* to be cleared in the RNG_CR register, then confirm that SEIS is
* cleared in the RNG_SR register. Otherwise just clear SEIS bit in
* the RNG_SR register.
* 3. If SECS was set in step 1 (no auto-reset) wait for SECS to be
* cleared by RNG. The random number generation is now back to normal.
*/
static int stm32_rng_conceal_seed_error_cond_reset(struct stm32_rng_private *priv)
{
struct device *dev = priv->dev;
u32 sr = readl_relaxed(priv->base + RNG_SR);
u32 cr = readl_relaxed(priv->base + RNG_CR);
int err;
if (sr & RNG_SR_SECS) {
/* Conceal by resetting the subsystem (step 1.) */
writel_relaxed(cr | RNG_CR_CONDRST, priv->base + RNG_CR);
writel_relaxed(cr & ~RNG_CR_CONDRST, priv->base + RNG_CR);
} else {
/* RNG auto-reset (step 2.) */
writel_relaxed(sr & ~RNG_SR_SEIS, priv->base + RNG_SR);
goto end;
}
err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_CR, cr, !(cr & RNG_CR_CONDRST), 10,
100000);
if (err) {
dev_err(dev, "%s: timeout %x\n", __func__, sr);
return err;
}
/* Check SEIS is cleared (step 2.) */
if (readl_relaxed(priv->base + RNG_SR) & RNG_SR_SEIS)
return -EINVAL;
err = readl_relaxed_poll_timeout_atomic(priv->base + RNG_SR, sr, !(sr & RNG_SR_SECS), 10,
100000);
if (err) {
dev_err(dev, "%s: timeout %x\n", __func__, sr);
return err;
}
end:
return 0;
}
Annotation
- Immediate include surface: `linux/clk.h`, `linux/clk-provider.h`, `linux/delay.h`, `linux/hw_random.h`, `linux/io.h`, `linux/iopoll.h`, `linux/kernel.h`, `linux/module.h`.
- Detected declarations: `struct stm32_rng_data`, `struct stm32_rng_config`, `struct stm32_rng_private`, `function source`, `function source`, `function stm32_rng_conceal_seed_error`, `function stm32_rng_read`, `function stm32_rng_clock_freq_restrain`, `function stm32_rng_init`, `function stm32_rng_remove`.
- Atlas domain: Driver Families / drivers/char.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.