drivers/char/hw_random/atmel-rng.c
Source file repositories/reference/linux-study-clean/drivers/char/hw_random/atmel-rng.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/char/hw_random/atmel-rng.c- Extension
.c- Size
- 5248 bytes
- Lines
- 232
- 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/kernel.hlinux/module.hlinux/mod_devicetable.hlinux/slab.hlinux/err.hlinux/clk.hlinux/io.hlinux/iopoll.hlinux/hw_random.hlinux/of.hlinux/platform_device.hlinux/pm_runtime.h
Detected Declarations
struct atmel_trng_datastruct atmel_trngfunction atmel_trng_wait_readyfunction atmel_trng_readfunction atmel_trng_initfunction atmel_trng_cleanupfunction atmel_trng_probefunction atmel_trng_removefunction atmel_trng_runtime_suspendfunction atmel_trng_runtime_resume
Annotated Snippet
struct atmel_trng_data {
bool has_half_rate;
};
struct atmel_trng {
struct clk *clk;
void __iomem *base;
struct hwrng rng;
struct device *dev;
bool has_half_rate;
};
static bool atmel_trng_wait_ready(struct atmel_trng *trng, bool wait)
{
int ready;
ready = readl(trng->base + TRNG_ISR) & TRNG_ISR_DATRDY;
if (!ready && wait)
readl_poll_timeout(trng->base + TRNG_ISR, ready,
ready & TRNG_ISR_DATRDY, 1000, 20000);
return !!ready;
}
static int atmel_trng_read(struct hwrng *rng, void *buf, size_t max,
bool wait)
{
struct atmel_trng *trng = container_of(rng, struct atmel_trng, rng);
u32 *data = buf;
int ret;
ret = pm_runtime_get_sync(trng->dev);
if (ret < 0) {
pm_runtime_put_sync(trng->dev);
return ret;
}
ret = atmel_trng_wait_ready(trng, wait);
if (!ret)
goto out;
*data = readl(trng->base + TRNG_ODATA);
/*
* ensure data ready is only set again AFTER the next data word is ready
* in case it got set between checking ISR and reading ODATA, so we
* don't risk re-reading the same word
*/
readl(trng->base + TRNG_ISR);
ret = 4;
out:
pm_runtime_put_sync_autosuspend(trng->dev);
return ret;
}
static int atmel_trng_init(struct atmel_trng *trng)
{
unsigned long rate;
int ret;
ret = clk_prepare_enable(trng->clk);
if (ret)
return ret;
if (trng->has_half_rate) {
rate = clk_get_rate(trng->clk);
/* if peripheral clk is above 100MHz, set HALFR */
if (rate > 100000000)
writel(TRNG_HALFR, trng->base + TRNG_MR);
}
writel(TRNG_KEY | 1, trng->base + TRNG_CR);
return 0;
}
static void atmel_trng_cleanup(struct atmel_trng *trng)
{
writel(TRNG_KEY, trng->base + TRNG_CR);
clk_disable_unprepare(trng->clk);
}
static int atmel_trng_probe(struct platform_device *pdev)
{
struct atmel_trng *trng;
const struct atmel_trng_data *data;
int ret;
trng = devm_kzalloc(&pdev->dev, sizeof(*trng), GFP_KERNEL);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/module.h`, `linux/mod_devicetable.h`, `linux/slab.h`, `linux/err.h`, `linux/clk.h`, `linux/io.h`, `linux/iopoll.h`.
- Detected declarations: `struct atmel_trng_data`, `struct atmel_trng`, `function atmel_trng_wait_ready`, `function atmel_trng_read`, `function atmel_trng_init`, `function atmel_trng_cleanup`, `function atmel_trng_probe`, `function atmel_trng_remove`, `function atmel_trng_runtime_suspend`, `function atmel_trng_runtime_resume`.
- 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.