drivers/char/hw_random/xiphera-trng.c

Source file repositories/reference/linux-study-clean/drivers/char/hw_random/xiphera-trng.c

File Facts

System
Linux kernel
Corpus path
drivers/char/hw_random/xiphera-trng.c
Extension
.c
Size
3894 bytes
Lines
146
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 xiphera_trng {
	void __iomem *mem;
	struct hwrng rng;
};

static int xiphera_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
{
	struct xiphera_trng *trng = container_of(rng, struct xiphera_trng, rng);
	int ret = 0;

	while (max >= sizeof(u32)) {
		/* check for data */
		if (readl(trng->mem + STATUS_REG) == TRNG_NEW_RAND_AVAILABLE) {
			*(u32 *)buf = readl(trng->mem + RAND_REG);
			/*
			 * Inform the trng of the read
			 * and re-enable it to produce a new random number
			 */
			writel(HOST_TO_TRNG_READ, trng->mem + CONTROL_REG);
			writel(HOST_TO_TRNG_ENABLE, trng->mem + CONTROL_REG);
			ret += sizeof(u32);
			buf += sizeof(u32);
			max -= sizeof(u32);
		} else {
			break;
		}
	}
	return ret;
}

static int xiphera_trng_probe(struct platform_device *pdev)
{
	int ret;
	struct xiphera_trng *trng;
	struct device *dev = &pdev->dev;

	trng = devm_kzalloc(dev, sizeof(*trng), GFP_KERNEL);
	if (!trng)
		return -ENOMEM;

	trng->mem = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(trng->mem))
		return PTR_ERR(trng->mem);

	/*
	 * the trng needs to be reset first which might not happen in time,
	 * hence we incorporate a small delay to ensure proper behaviour
	 */
	writel(HOST_TO_TRNG_RESET, trng->mem + CONTROL_REG);
	usleep_range(100, 200);

	if (readl(trng->mem + STATUS_REG) != TRNG_ACK_RESET) {
		/*
		 * there is a small chance the trng is just not ready yet,
		 * so we try one more time. If the second time fails, we give up
		 */
		usleep_range(100, 200);
		if (readl(trng->mem + STATUS_REG) != TRNG_ACK_RESET) {
			dev_err(dev, "failed to reset the trng ip\n");
			return -ENODEV;
		}
	}

	/*
	 * once again, to ensure proper behaviour we sleep
	 * for a while after zeroizing the trng
	 */
	writel(HOST_TO_TRNG_RELEASE_RESET, trng->mem + CONTROL_REG);
	writel(HOST_TO_TRNG_ENABLE, trng->mem + CONTROL_REG);
	writel(HOST_TO_TRNG_ZEROIZE, trng->mem + CONTROL_REG);
	msleep(20);

	if (readl(trng->mem + STATUS_REG) != TRNG_SUCCESSFUL_STARTUP) {
		/* diagnose the reason for the failure */
		if (readl(trng->mem + STATUS_REG) == TRNG_FAILED_STARTUP) {
			dev_err(dev, "trng ip startup-tests failed\n");
			return -ENODEV;
		}
		dev_err(dev, "startup-tests yielded no response\n");
		return -ENODEV;
	}

	writel(HOST_TO_TRNG_ACK_ZEROIZE, trng->mem + CONTROL_REG);

	trng->rng.name = pdev->name;
	trng->rng.read = xiphera_trng_read;
	trng->rng.quality = 900;

	ret = devm_hwrng_register(dev, &trng->rng);
	if (ret) {

Annotation

Implementation Notes