Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * drivers/char/hw_random/timeriomem-rng.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2009 Alexander Clouter <alex@digriz.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Derived from drivers/char/hw_random/omap-rng.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *   Copyright 2005 (c) MontaVista Software, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *   Author: Deepak Saxena <dsaxena@plexity.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * Overview:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *   This driver is useful for platforms that have an IO range that provides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *   periodic random data from a single IO memory address.  All the platform
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *   has to do is provide the address and 'wait time' that new data becomes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *   available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * TODO: add support for reading sizes other than 32bits and masking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/ktime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/timeriomem-rng.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) struct timeriomem_rng_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	void __iomem		*io_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	ktime_t			period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	unsigned int		present:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	struct hrtimer		timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct completion	completion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct hwrng		rng_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static int timeriomem_rng_read(struct hwrng *hwrng, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 				size_t max, bool wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct timeriomem_rng_private *priv =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		container_of(hwrng, struct timeriomem_rng_private, rng_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int period_us = ktime_to_us(priv->period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	 * There may not have been enough time for new data to be generated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	 * since the last request.  If the caller doesn't want to wait, let them
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	 * bail out.  Otherwise, wait for the completion.  If the new data has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	 * already been generated, the completion should already be available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	if (!wait && !priv->present)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	wait_for_completion(&priv->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		 * After the first read, all additional reads will need to wait
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		 * for the RNG to generate new data.  Since the period can have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		 * a wide range of values (1us to 1s have been observed), allow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		 * for 1% tolerance in the sleep time rather than a fixed value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		if (retval > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 			usleep_range(period_us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 					period_us + max(1, period_us / 100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		*(u32 *)data = readl(priv->io_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		retval += sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		data += sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		max -= sizeof(u32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	} while (wait && max > sizeof(u32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	 * Block any new callers until the RNG has had time to generate new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 * data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	priv->present = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	reinit_completion(&priv->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	hrtimer_forward_now(&priv->timer, priv->period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	hrtimer_restart(&priv->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) static enum hrtimer_restart timeriomem_rng_trigger(struct hrtimer *timer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct timeriomem_rng_private *priv
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		= container_of(timer, struct timeriomem_rng_private, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	priv->present = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	complete(&priv->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	return HRTIMER_NORESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int timeriomem_rng_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	struct timeriomem_rng_data *pdata = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	struct timeriomem_rng_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (!pdev->dev.of_node && !pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		dev_err(&pdev->dev, "timeriomem_rng_data is missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (res->start % 4 != 0 || resource_size(res) < 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			"address must be at least four bytes wide and 32-bit aligned\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	/* Allocate memory for the device structure (and zero it) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	priv = devm_kzalloc(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 			sizeof(struct timeriomem_rng_private), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	if (!priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	platform_set_drvdata(pdev, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	if (pdev->dev.of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		if (!of_property_read_u32(pdev->dev.of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 						"period", &i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			period = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			dev_err(&pdev->dev, "missing period\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (!of_property_read_u32(pdev->dev.of_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 						"quality", &i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			priv->rng_ops.quality = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			priv->rng_ops.quality = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		period = pdata->period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		priv->rng_ops.quality = pdata->quality;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	priv->period = ns_to_ktime(period * NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	init_completion(&priv->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	hrtimer_init(&priv->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	priv->timer.function = timeriomem_rng_trigger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	priv->rng_ops.name = dev_name(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	priv->rng_ops.read = timeriomem_rng_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	priv->io_base = devm_ioremap_resource(&pdev->dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (IS_ERR(priv->io_base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		return PTR_ERR(priv->io_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	/* Assume random data is already available. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	priv->present = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	complete(&priv->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	err = hwrng_register(&priv->rng_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		dev_err(&pdev->dev, "problem registering\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	dev_info(&pdev->dev, "32bits from 0x%p @ %dus\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			priv->io_base, period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static int timeriomem_rng_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	struct timeriomem_rng_private *priv = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	hwrng_unregister(&priv->rng_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	hrtimer_cancel(&priv->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) static const struct of_device_id timeriomem_rng_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	{ .compatible = "timeriomem_rng" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) MODULE_DEVICE_TABLE(of, timeriomem_rng_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static struct platform_driver timeriomem_rng_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		.name		= "timeriomem_rng",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		.of_match_table	= timeriomem_rng_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	.probe		= timeriomem_rng_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	.remove		= timeriomem_rng_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) module_platform_driver(timeriomem_rng_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) MODULE_AUTHOR("Alexander Clouter <alex@digriz.org.uk>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) MODULE_DESCRIPTION("Timer IOMEM H/W RNG driver");