drivers/char/hw_random/npcm-rng.c

Source file repositories/reference/linux-study-clean/drivers/char/hw_random/npcm-rng.c

File Facts

System
Linux kernel
Corpus path
drivers/char/hw_random/npcm-rng.c
Extension
.c
Size
4499 bytes
Lines
187
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.

Dependency Surface

Detected Declarations

Annotated Snippet

struct npcm_rng {
	void __iomem *base;
	struct hwrng rng;
	struct device *dev;
	u32 clkp;
};

static int npcm_rng_init(struct hwrng *rng)
{
	struct npcm_rng *priv = to_npcm_rng(rng);

	writel(priv->clkp | NPCM_RNG_ENABLE, priv->base + NPCM_RNGCS_REG);

	return 0;
}

static void npcm_rng_cleanup(struct hwrng *rng)
{
	struct npcm_rng *priv = to_npcm_rng(rng);

	writel(priv->clkp, priv->base + NPCM_RNGCS_REG);
}

static int npcm_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
{
	struct npcm_rng *priv = to_npcm_rng(rng);
	int retval = 0;
	int ready;

	pm_runtime_get_sync(priv->dev);

	while (max) {
		if (wait) {
			if (readb_poll_timeout(priv->base + NPCM_RNGCS_REG,
					       ready,
					       ready & NPCM_RNG_DATA_VALID,
					       NPCM_RNG_POLL_USEC,
					       NPCM_RNG_TIMEOUT_USEC))
				break;
		} else {
			if ((readb(priv->base + NPCM_RNGCS_REG) &
			    NPCM_RNG_DATA_VALID) == 0)
				break;
		}

		*(u8 *)buf = readb(priv->base + NPCM_RNGD_REG);
		retval++;
		buf++;
		max--;
	}

	pm_runtime_put_sync_autosuspend(priv->dev);

	return retval || !wait ? retval : -EIO;
}

static int npcm_rng_probe(struct platform_device *pdev)
{
	struct npcm_rng *priv;
	int ret;

	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);

	dev_set_drvdata(&pdev->dev, priv);
	pm_runtime_set_autosuspend_delay(&pdev->dev, 100);
	pm_runtime_use_autosuspend(&pdev->dev);
	pm_runtime_enable(&pdev->dev);

#ifndef CONFIG_PM
	priv->rng.init = npcm_rng_init;
	priv->rng.cleanup = npcm_rng_cleanup;
#endif
	priv->rng.name = pdev->name;
	priv->rng.read = npcm_rng_read;
	priv->dev = &pdev->dev;
	priv->clkp = (u32)(uintptr_t)of_device_get_match_data(&pdev->dev);

	writel(NPCM_RNG_M1ROSEL, priv->base + NPCM_RNGMODE_REG);

	ret = devm_hwrng_register(&pdev->dev, &priv->rng);
	if (ret) {
		dev_err(&pdev->dev, "Failed to register rng device: %d\n",
			ret);
		pm_runtime_disable(&pdev->dev);

Annotation

Implementation Notes