blob: 5a4f45ad5afe79db0253eb97427eb9f80b8acf54 [file] [log] [blame]
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +01001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
4 *
5 * Driver for Amlogic hardware random number generator
6 */
7
8#include <common.h>
9#include <clk.h>
10#include <dm.h>
11#include <rng.h>
12#include <asm/io.h>
13
Simon Glass8a8d24b2020-12-03 16:55:23 -070014struct meson_rng_plat {
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +010015 fdt_addr_t base;
16 struct clk clk;
17};
18
19/**
20 * meson_rng_read() - fill buffer with random bytes
21 *
22 * @buffer: buffer to receive data
23 * @size: size of buffer
24 *
25 * Return: 0
26 */
27static int meson_rng_read(struct udevice *dev, void *data, size_t len)
28{
Simon Glass8a8d24b2020-12-03 16:55:23 -070029 struct meson_rng_plat *pdata = dev_get_plat(dev);
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +010030 char *buffer = (char *)data;
31
32 while (len) {
33 u32 rand = readl(pdata->base);
34 size_t step;
35
36 if (len >= 4)
37 step = 4;
38 else
39 step = len;
40 memcpy(buffer, &rand, step);
41 buffer += step;
42 len -= step;
43 }
44 return 0;
45}
46
47/**
48 * meson_rng_probe() - probe rng device
49 *
50 * @dev: device
51 * Return: 0 if ok
52 */
53static int meson_rng_probe(struct udevice *dev)
54{
Simon Glass8a8d24b2020-12-03 16:55:23 -070055 struct meson_rng_plat *pdata = dev_get_plat(dev);
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +010056 int err;
57
58 err = clk_enable(&pdata->clk);
59 if (err)
60 return err;
61
62 return 0;
63}
64
65/**
66 * meson_rng_remove() - deinitialize rng device
67 *
68 * @dev: device
69 * Return: 0 if ok
70 */
71static int meson_rng_remove(struct udevice *dev)
72{
Simon Glass8a8d24b2020-12-03 16:55:23 -070073 struct meson_rng_plat *pdata = dev_get_plat(dev);
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +010074
75 return clk_disable(&pdata->clk);
76}
77
78/**
Simon Glassd1998a92020-12-03 16:55:21 -070079 * meson_rng_of_to_plat() - transfer device tree data to plaform data
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +010080 *
81 * @dev: device
82 * Return: 0 if ok
83 */
Simon Glassd1998a92020-12-03 16:55:21 -070084static int meson_rng_of_to_plat(struct udevice *dev)
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +010085{
Simon Glass8a8d24b2020-12-03 16:55:23 -070086 struct meson_rng_plat *pdata = dev_get_plat(dev);
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +010087 int err;
88
89 pdata->base = dev_read_addr(dev);
90 if (!pdata->base)
91 return -ENODEV;
92
Neil Armstrong02d249f2020-09-25 09:19:20 +020093 /* Get optional "core" clock */
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +010094 err = clk_get_by_name(dev, "core", &pdata->clk);
Neil Armstrong02d249f2020-09-25 09:19:20 +020095 if (err && err != -ENODATA)
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +010096 return err;
97
98 return 0;
99}
100
101static const struct dm_rng_ops meson_rng_ops = {
102 .read = meson_rng_read,
103};
104
105static const struct udevice_id meson_rng_match[] = {
106 {
107 .compatible = "amlogic,meson-rng",
108 },
109 {},
110};
111
112U_BOOT_DRIVER(meson_rng) = {
113 .name = "meson-rng",
114 .id = UCLASS_RNG,
115 .of_match = meson_rng_match,
116 .ops = &meson_rng_ops,
117 .probe = meson_rng_probe,
118 .remove = meson_rng_remove,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700119 .plat_auto = sizeof(struct meson_rng_plat),
Simon Glassd1998a92020-12-03 16:55:21 -0700120 .of_to_plat = meson_rng_of_to_plat,
Heinrich Schuchardtbc40eb22020-02-02 19:52:04 +0100121};