Loading...
1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2/*
3 * Copyright (c) 2016 BayLibre, SAS.
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 * Copyright (C) 2014 Amlogic, Inc.
6 */
7#include <linux/err.h>
8#include <linux/module.h>
9#include <linux/io.h>
10#include <linux/platform_device.h>
11#include <linux/hw_random.h>
12#include <linux/slab.h>
13#include <linux/types.h>
14#include <linux/of.h>
15#include <linux/clk.h>
16#include <linux/iopoll.h>
17
18#define RNG_DATA 0x00
19#define RNG_S4_DATA 0x08
20#define RNG_S4_CFG 0x00
21
22#define RUN_BIT BIT(0)
23#define SEED_READY_STS_BIT BIT(31)
24
25struct meson_rng_priv {
26 int (*read)(struct hwrng *rng, void *buf, size_t max, bool wait);
27};
28
29struct meson_rng_data {
30 void __iomem *base;
31 struct hwrng rng;
32 struct device *dev;
33};
34
35static int meson_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
36{
37 struct meson_rng_data *data =
38 container_of(rng, struct meson_rng_data, rng);
39
40 *(u32 *)buf = readl_relaxed(data->base + RNG_DATA);
41
42 return sizeof(u32);
43}
44
45static int meson_rng_wait_status(void __iomem *cfg_addr, int bit)
46{
47 u32 status = 0;
48 int ret;
49
50 ret = readl_relaxed_poll_timeout_atomic(cfg_addr,
51 status, !(status & bit),
52 10, 10000);
53 if (ret)
54 return -EBUSY;
55
56 return 0;
57}
58
59static int meson_s4_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
60{
61 struct meson_rng_data *data =
62 container_of(rng, struct meson_rng_data, rng);
63
64 void __iomem *cfg_addr = data->base + RNG_S4_CFG;
65 int err;
66
67 writel_relaxed(readl_relaxed(cfg_addr) | SEED_READY_STS_BIT, cfg_addr);
68
69 err = meson_rng_wait_status(cfg_addr, SEED_READY_STS_BIT);
70 if (err) {
71 dev_err(data->dev, "Seed isn't ready, try again\n");
72 return err;
73 }
74
75 err = meson_rng_wait_status(cfg_addr, RUN_BIT);
76 if (err) {
77 dev_err(data->dev, "Can't get random number, try again\n");
78 return err;
79 }
80
81 *(u32 *)buf = readl_relaxed(data->base + RNG_S4_DATA);
82
83 return sizeof(u32);
84}
85
86static int meson_rng_probe(struct platform_device *pdev)
87{
88 struct device *dev = &pdev->dev;
89 struct meson_rng_data *data;
90 struct clk *core_clk;
91 const struct meson_rng_priv *priv;
92
93 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
94 if (!data)
95 return -ENOMEM;
96
97 priv = device_get_match_data(&pdev->dev);
98 if (!priv)
99 return -ENODEV;
100
101 data->base = devm_platform_ioremap_resource(pdev, 0);
102 if (IS_ERR(data->base))
103 return PTR_ERR(data->base);
104
105 core_clk = devm_clk_get_optional_enabled(dev, "core");
106 if (IS_ERR(core_clk))
107 return dev_err_probe(dev, PTR_ERR(core_clk),
108 "Failed to get core clock\n");
109
110 data->rng.name = pdev->name;
111 data->rng.read = priv->read;
112
113 data->dev = &pdev->dev;
114
115 return devm_hwrng_register(dev, &data->rng);
116}
117
118static const struct meson_rng_priv meson_rng_priv = {
119 .read = meson_rng_read,
120};
121
122static const struct meson_rng_priv meson_rng_priv_s4 = {
123 .read = meson_s4_rng_read,
124};
125
126static const struct of_device_id meson_rng_of_match[] = {
127 {
128 .compatible = "amlogic,meson-rng",
129 .data = (void *)&meson_rng_priv,
130 },
131 {
132 .compatible = "amlogic,meson-s4-rng",
133 .data = (void *)&meson_rng_priv_s4,
134 },
135 {},
136};
137MODULE_DEVICE_TABLE(of, meson_rng_of_match);
138
139static struct platform_driver meson_rng_driver = {
140 .probe = meson_rng_probe,
141 .driver = {
142 .name = "meson-rng",
143 .of_match_table = meson_rng_of_match,
144 },
145};
146
147module_platform_driver(meson_rng_driver);
148
149MODULE_DESCRIPTION("Meson H/W Random Number Generator driver");
150MODULE_AUTHOR("Lawrence Mok <lawrence.mok@amlogic.com>");
151MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
152MODULE_LICENSE("Dual BSD/GPL");
1/*
2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
4 *
5 * GPL LICENSE SUMMARY
6 *
7 * Copyright (c) 2016 BayLibre, SAS.
8 * Author: Neil Armstrong <narmstrong@baylibre.com>
9 * Copyright (C) 2014 Amlogic, Inc.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 * The full GNU General Public License is included in this distribution
23 * in the file called COPYING.
24 *
25 * BSD LICENSE
26 *
27 * Copyright (c) 2016 BayLibre, SAS.
28 * Author: Neil Armstrong <narmstrong@baylibre.com>
29 * Copyright (C) 2014 Amlogic, Inc.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 *
35 * * Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * * Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in
39 * the documentation and/or other materials provided with the
40 * distribution.
41 * * Neither the name of Intel Corporation nor the names of its
42 * contributors may be used to endorse or promote products derived
43 * from this software without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
46 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
47 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
48 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
49 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
52 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
53 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
54 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
55 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56 */
57#include <linux/err.h>
58#include <linux/module.h>
59#include <linux/io.h>
60#include <linux/platform_device.h>
61#include <linux/hw_random.h>
62#include <linux/slab.h>
63#include <linux/types.h>
64#include <linux/of.h>
65#include <linux/clk.h>
66
67#define RNG_DATA 0x00
68
69struct meson_rng_data {
70 void __iomem *base;
71 struct platform_device *pdev;
72 struct hwrng rng;
73 struct clk *core_clk;
74};
75
76static int meson_rng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
77{
78 struct meson_rng_data *data =
79 container_of(rng, struct meson_rng_data, rng);
80
81 *(u32 *)buf = readl_relaxed(data->base + RNG_DATA);
82
83 return sizeof(u32);
84}
85
86static void meson_rng_clk_disable(void *data)
87{
88 clk_disable_unprepare(data);
89}
90
91static int meson_rng_probe(struct platform_device *pdev)
92{
93 struct device *dev = &pdev->dev;
94 struct meson_rng_data *data;
95 struct resource *res;
96 int ret;
97
98 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
99 if (!data)
100 return -ENOMEM;
101
102 data->pdev = pdev;
103
104 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105 data->base = devm_ioremap_resource(dev, res);
106 if (IS_ERR(data->base))
107 return PTR_ERR(data->base);
108
109 data->core_clk = devm_clk_get(dev, "core");
110 if (IS_ERR(data->core_clk))
111 data->core_clk = NULL;
112
113 if (data->core_clk) {
114 ret = clk_prepare_enable(data->core_clk);
115 if (ret)
116 return ret;
117 ret = devm_add_action_or_reset(dev, meson_rng_clk_disable,
118 data->core_clk);
119 if (ret)
120 return ret;
121 }
122
123 data->rng.name = pdev->name;
124 data->rng.read = meson_rng_read;
125
126 platform_set_drvdata(pdev, data);
127
128 return devm_hwrng_register(dev, &data->rng);
129}
130
131static const struct of_device_id meson_rng_of_match[] = {
132 { .compatible = "amlogic,meson-rng", },
133 {},
134};
135MODULE_DEVICE_TABLE(of, meson_rng_of_match);
136
137static struct platform_driver meson_rng_driver = {
138 .probe = meson_rng_probe,
139 .driver = {
140 .name = "meson-rng",
141 .of_match_table = meson_rng_of_match,
142 },
143};
144
145module_platform_driver(meson_rng_driver);
146
147MODULE_DESCRIPTION("Meson H/W Random Number Generator driver");
148MODULE_AUTHOR("Lawrence Mok <lawrence.mok@amlogic.com>");
149MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");
150MODULE_LICENSE("Dual BSD/GPL");