drivers/char/hw_random/pic32-rng.c
Source file repositories/reference/linux-study-clean/drivers/char/hw_random/pic32-rng.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/char/hw_random/pic32-rng.c- Extension
.c- Size
- 2889 bytes
- Lines
- 121
- 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/clkdev.hlinux/err.hlinux/hw_random.hlinux/io.hlinux/kernel.hlinux/mod_devicetable.hlinux/module.hlinux/platform_device.hlinux/slab.h
Detected Declarations
struct pic32_rngfunction pic32_rng_initfunction pic32_rng_readfunction pic32_rng_cleanupfunction pic32_rng_probe
Annotated Snippet
struct pic32_rng {
void __iomem *base;
struct hwrng rng;
};
/*
* The TRNG can generate up to 24Mbps. This is a timeout that should be safe
* enough given the instructions in the loop and that the TRNG may not always
* be at maximum rate.
*/
#define RNG_TIMEOUT 500
static int pic32_rng_init(struct hwrng *rng)
{
struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
/* enable TRNG in enhanced mode */
writel(TRNGEN | TRNGMOD, priv->base + RNGCON);
return 0;
}
static int pic32_rng_read(struct hwrng *rng, void *buf, size_t max,
bool wait)
{
struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
u64 *data = buf;
u32 t;
unsigned int timeout = RNG_TIMEOUT;
do {
t = readl(priv->base + RNGRCNT) & RCNT_MASK;
if (t == 64) {
/* TRNG value comes through the seed registers */
*data = ((u64)readl(priv->base + RNGSEED2) << 32) +
readl(priv->base + RNGSEED1);
return 8;
}
} while (wait && --timeout);
return -EIO;
}
static void pic32_rng_cleanup(struct hwrng *rng)
{
struct pic32_rng *priv = container_of(rng, struct pic32_rng, rng);
writel(0, priv->base + RNGCON);
}
static int pic32_rng_probe(struct platform_device *pdev)
{
struct pic32_rng *priv;
struct clk *clk;
priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
priv->base = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(priv->base))
return PTR_ERR(priv->base);
clk = devm_clk_get_enabled(&pdev->dev, NULL);
if (IS_ERR(clk))
return PTR_ERR(clk);
priv->rng.name = pdev->name;
priv->rng.init = pic32_rng_init;
priv->rng.read = pic32_rng_read;
priv->rng.cleanup = pic32_rng_cleanup;
return devm_hwrng_register(&pdev->dev, &priv->rng);
}
static const struct of_device_id pic32_rng_of_match[] __maybe_unused = {
{ .compatible = "microchip,pic32mzda-rng", },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, pic32_rng_of_match);
static struct platform_driver pic32_rng_driver = {
.probe = pic32_rng_probe,
.driver = {
.name = "pic32-rng",
.of_match_table = pic32_rng_of_match,
},
};
module_platform_driver(pic32_rng_driver);
Annotation
- Immediate include surface: `linux/clk.h`, `linux/clkdev.h`, `linux/err.h`, `linux/hw_random.h`, `linux/io.h`, `linux/kernel.h`, `linux/mod_devicetable.h`, `linux/module.h`.
- Detected declarations: `struct pic32_rng`, `function pic32_rng_init`, `function pic32_rng_read`, `function pic32_rng_cleanup`, `function pic32_rng_probe`.
- 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.