drivers/char/hw_random/mtk-rng.c
Source file repositories/reference/linux-study-clean/drivers/char/hw_random/mtk-rng.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/char/hw_random/mtk-rng.c- Extension
.c- Size
- 6611 bytes
- Lines
- 289
- 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/arm-smccc.hlinux/clk.hlinux/delay.hlinux/err.hlinux/hw_random.hlinux/io.hlinux/iopoll.hlinux/kernel.hlinux/module.hlinux/of.hlinux/platform_device.hlinux/pm_runtime.hlinux/soc/mediatek/mtk_sip_svc.h
Detected Declarations
struct mtk_rngfunction mtk_rng_initfunction mtk_rng_cleanupfunction mtk_rng_wait_readyfunction mtk_rng_readfunction mtk_rng_read_smcfunction mtk_rng_hw_accessiblefunction mtk_rng_probefunction mtk_rng_runtime_suspendfunction mtk_rng_runtime_resume
Annotated Snippet
struct mtk_rng {
void __iomem *base;
struct clk *clk;
struct hwrng rng;
struct device *dev;
unsigned long flags;
};
static int mtk_rng_init(struct hwrng *rng)
{
struct mtk_rng *priv = to_mtk_rng(rng);
u32 val;
int err;
err = clk_prepare_enable(priv->clk);
if (err)
return err;
val = readl(priv->base + RNG_CTRL);
val |= RNG_EN;
writel(val, priv->base + RNG_CTRL);
return 0;
}
static void mtk_rng_cleanup(struct hwrng *rng)
{
struct mtk_rng *priv = to_mtk_rng(rng);
u32 val;
val = readl(priv->base + RNG_CTRL);
val &= ~RNG_EN;
writel(val, priv->base + RNG_CTRL);
clk_disable_unprepare(priv->clk);
}
static bool mtk_rng_wait_ready(struct hwrng *rng, bool wait)
{
struct mtk_rng *priv = to_mtk_rng(rng);
int ready;
ready = readl(priv->base + RNG_CTRL) & RNG_READY;
if (!ready && wait)
readl_poll_timeout_atomic(priv->base + RNG_CTRL, ready,
ready & RNG_READY, USEC_POLL,
TIMEOUT_POLL);
return !!(ready & RNG_READY);
}
static int mtk_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
{
struct mtk_rng *priv = to_mtk_rng(rng);
int retval = 0;
pm_runtime_get_sync(priv->dev);
while (max >= sizeof(u32)) {
if (!mtk_rng_wait_ready(rng, wait))
break;
*(u32 *)buf = readl(priv->base + RNG_DATA);
retval += sizeof(u32);
buf += sizeof(u32);
max -= sizeof(u32);
}
pm_runtime_put_sync_autosuspend(priv->dev);
return retval || !wait ? retval : -EIO;
}
static int mtk_rng_read_smc(struct hwrng *rng, void *buf, size_t max,
bool wait)
{
struct arm_smccc_res res;
int retval = 0;
while (max >= sizeof(u32)) {
arm_smccc_smc(MTK_SIP_KERNEL_GET_RND, 0, 0, 0, 0, 0, 0, 0,
&res);
if (res.a0)
break;
*(u32 *)buf = res.a1;
retval += sizeof(u32);
buf += sizeof(u32);
max -= sizeof(u32);
}
Annotation
- Immediate include surface: `linux/arm-smccc.h`, `linux/clk.h`, `linux/delay.h`, `linux/err.h`, `linux/hw_random.h`, `linux/io.h`, `linux/iopoll.h`, `linux/kernel.h`.
- Detected declarations: `struct mtk_rng`, `function mtk_rng_init`, `function mtk_rng_cleanup`, `function mtk_rng_wait_ready`, `function mtk_rng_read`, `function mtk_rng_read_smc`, `function mtk_rng_hw_accessible`, `function mtk_rng_probe`, `function mtk_rng_runtime_suspend`, `function mtk_rng_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.