Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * ST Random Number Generator Driver ST's Platforms
  4 *
  5 * Author: Pankaj Dev: <pankaj.dev@st.com>
  6 *         Lee Jones <lee.jones@linaro.org>
  7 *
  8 * Copyright (C) 2015 STMicroelectronics (R&D) Limited
 
 
 
 
  9 */
 10
 11#include <linux/clk.h>
 12#include <linux/delay.h>
 13#include <linux/hw_random.h>
 14#include <linux/io.h>
 15#include <linux/kernel.h>
 16#include <linux/module.h>
 17#include <linux/of.h>
 18#include <linux/platform_device.h>
 19#include <linux/slab.h>
 20
 21/* Registers */
 22#define ST_RNG_STATUS_REG		0x20
 23#define ST_RNG_DATA_REG			0x24
 24
 25/* Registers fields */
 26#define ST_RNG_STATUS_BAD_SEQUENCE	BIT(0)
 27#define ST_RNG_STATUS_BAD_ALTERNANCE	BIT(1)
 28#define ST_RNG_STATUS_FIFO_FULL		BIT(5)
 29
 30#define ST_RNG_SAMPLE_SIZE		2 /* 2 Byte (16bit) samples */
 31#define ST_RNG_FIFO_DEPTH		4
 32#define ST_RNG_FIFO_SIZE		(ST_RNG_FIFO_DEPTH * ST_RNG_SAMPLE_SIZE)
 33
 34/*
 35 * Samples are documented to be available every 0.667us, so in theory
 36 * the 4 sample deep FIFO should take 2.668us to fill.  However, during
 37 * thorough testing, it became apparent that filling the FIFO actually
 38 * takes closer to 12us.  We then multiply by 2 in order to account for
 39 * the lack of udelay()'s reliability, suggested by Russell King.
 40 */
 41#define ST_RNG_FILL_FIFO_TIMEOUT	(12 * 2)
 42
 43struct st_rng_data {
 44	void __iomem	*base;
 
 45	struct hwrng	ops;
 46};
 47
 48static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
 49{
 50	struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
 51	u32 status;
 52	int i;
 53
 54	/* Wait until FIFO is full - max 4uS*/
 55	for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
 56		status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
 57		if (status & ST_RNG_STATUS_FIFO_FULL)
 58			break;
 59		udelay(1);
 60	}
 61
 62	if (i == ST_RNG_FILL_FIFO_TIMEOUT)
 63		return 0;
 64
 65	for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
 66		*(u16 *)(data + i) =
 67			readl_relaxed(ddata->base + ST_RNG_DATA_REG);
 68
 69	return i;	/* No of bytes read */
 70}
 71
 72static int st_rng_probe(struct platform_device *pdev)
 73{
 74	struct st_rng_data *ddata;
 
 75	struct clk *clk;
 76	void __iomem *base;
 77	int ret;
 78
 79	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
 80	if (!ddata)
 81		return -ENOMEM;
 82
 83	base = devm_platform_ioremap_resource(pdev, 0);
 
 84	if (IS_ERR(base))
 85		return PTR_ERR(base);
 86
 87	clk = devm_clk_get_enabled(&pdev->dev, NULL);
 88	if (IS_ERR(clk))
 89		return PTR_ERR(clk);
 90
 
 
 
 
 91	ddata->ops.priv	= (unsigned long)ddata;
 92	ddata->ops.read	= st_rng_read;
 93	ddata->ops.name	= pdev->name;
 94	ddata->base	= base;
 
 95
 96	ret = devm_hwrng_register(&pdev->dev, &ddata->ops);
 
 
 97	if (ret) {
 98		dev_err(&pdev->dev, "Failed to register HW RNG\n");
 
 99		return ret;
100	}
101
102	dev_info(&pdev->dev, "Successfully registered HW RNG\n");
103
104	return 0;
105}
106
107static const struct of_device_id st_rng_match[] __maybe_unused = {
 
 
 
 
 
 
 
 
 
 
 
108	{ .compatible = "st,rng" },
109	{},
110};
111MODULE_DEVICE_TABLE(of, st_rng_match);
112
113static struct platform_driver st_rng_driver = {
114	.driver = {
115		.name = "st-hwrandom",
116		.of_match_table = of_match_ptr(st_rng_match),
117	},
118	.probe = st_rng_probe,
 
119};
120
121module_platform_driver(st_rng_driver);
122
123MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
124MODULE_DESCRIPTION("ST Microelectronics HW Random Number Generator");
125MODULE_LICENSE("GPL v2");
v4.17
 
  1/*
  2 * ST Random Number Generator Driver ST's Platforms
  3 *
  4 * Author: Pankaj Dev: <pankaj.dev@st.com>
  5 *         Lee Jones <lee.jones@linaro.org>
  6 *
  7 * Copyright (C) 2015 STMicroelectronics (R&D) Limited
  8 *
  9 * This program is free software; you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License version 2 as
 11 * published by the Free Software Foundation.
 12 */
 13
 14#include <linux/clk.h>
 15#include <linux/delay.h>
 16#include <linux/hw_random.h>
 17#include <linux/io.h>
 
 18#include <linux/module.h>
 19#include <linux/of.h>
 20#include <linux/platform_device.h>
 21#include <linux/slab.h>
 22
 23/* Registers */
 24#define ST_RNG_STATUS_REG		0x20
 25#define ST_RNG_DATA_REG			0x24
 26
 27/* Registers fields */
 28#define ST_RNG_STATUS_BAD_SEQUENCE	BIT(0)
 29#define ST_RNG_STATUS_BAD_ALTERNANCE	BIT(1)
 30#define ST_RNG_STATUS_FIFO_FULL		BIT(5)
 31
 32#define ST_RNG_SAMPLE_SIZE		2 /* 2 Byte (16bit) samples */
 33#define ST_RNG_FIFO_DEPTH		4
 34#define ST_RNG_FIFO_SIZE		(ST_RNG_FIFO_DEPTH * ST_RNG_SAMPLE_SIZE)
 35
 36/*
 37 * Samples are documented to be available every 0.667us, so in theory
 38 * the 4 sample deep FIFO should take 2.668us to fill.  However, during
 39 * thorough testing, it became apparent that filling the FIFO actually
 40 * takes closer to 12us.  We then multiply by 2 in order to account for
 41 * the lack of udelay()'s reliability, suggested by Russell King.
 42 */
 43#define ST_RNG_FILL_FIFO_TIMEOUT	(12 * 2)
 44
 45struct st_rng_data {
 46	void __iomem	*base;
 47	struct clk	*clk;
 48	struct hwrng	ops;
 49};
 50
 51static int st_rng_read(struct hwrng *rng, void *data, size_t max, bool wait)
 52{
 53	struct st_rng_data *ddata = (struct st_rng_data *)rng->priv;
 54	u32 status;
 55	int i;
 56
 57	/* Wait until FIFO is full - max 4uS*/
 58	for (i = 0; i < ST_RNG_FILL_FIFO_TIMEOUT; i++) {
 59		status = readl_relaxed(ddata->base + ST_RNG_STATUS_REG);
 60		if (status & ST_RNG_STATUS_FIFO_FULL)
 61			break;
 62		udelay(1);
 63	}
 64
 65	if (i == ST_RNG_FILL_FIFO_TIMEOUT)
 66		return 0;
 67
 68	for (i = 0; i < ST_RNG_FIFO_SIZE && i < max; i += 2)
 69		*(u16 *)(data + i) =
 70			readl_relaxed(ddata->base + ST_RNG_DATA_REG);
 71
 72	return i;	/* No of bytes read */
 73}
 74
 75static int st_rng_probe(struct platform_device *pdev)
 76{
 77	struct st_rng_data *ddata;
 78	struct resource *res;
 79	struct clk *clk;
 80	void __iomem *base;
 81	int ret;
 82
 83	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
 84	if (!ddata)
 85		return -ENOMEM;
 86
 87	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 88	base = devm_ioremap_resource(&pdev->dev, res);
 89	if (IS_ERR(base))
 90		return PTR_ERR(base);
 91
 92	clk = devm_clk_get(&pdev->dev, NULL);
 93	if (IS_ERR(clk))
 94		return PTR_ERR(clk);
 95
 96	ret = clk_prepare_enable(clk);
 97	if (ret)
 98		return ret;
 99
100	ddata->ops.priv	= (unsigned long)ddata;
101	ddata->ops.read	= st_rng_read;
102	ddata->ops.name	= pdev->name;
103	ddata->base	= base;
104	ddata->clk	= clk;
105
106	dev_set_drvdata(&pdev->dev, ddata);
107
108	ret = hwrng_register(&ddata->ops);
109	if (ret) {
110		dev_err(&pdev->dev, "Failed to register HW RNG\n");
111		clk_disable_unprepare(clk);
112		return ret;
113	}
114
115	dev_info(&pdev->dev, "Successfully registered HW RNG\n");
116
117	return 0;
118}
119
120static int st_rng_remove(struct platform_device *pdev)
121{
122	struct st_rng_data *ddata = dev_get_drvdata(&pdev->dev);
123
124	hwrng_unregister(&ddata->ops);
125
126	clk_disable_unprepare(ddata->clk);
127
128	return 0;
129}
130
131static const struct of_device_id st_rng_match[] = {
132	{ .compatible = "st,rng" },
133	{},
134};
135MODULE_DEVICE_TABLE(of, st_rng_match);
136
137static struct platform_driver st_rng_driver = {
138	.driver = {
139		.name = "st-hwrandom",
140		.of_match_table = of_match_ptr(st_rng_match),
141	},
142	.probe = st_rng_probe,
143	.remove = st_rng_remove
144};
145
146module_platform_driver(st_rng_driver);
147
148MODULE_AUTHOR("Pankaj Dev <pankaj.dev@st.com>");
 
149MODULE_LICENSE("GPL v2");