u-boot/drivers/rng/meson-rng.c
Neil Armstrong 02d249f99e rng: meson: make core clock optional
This fixes HWRNG support on Amlogic GXL, GXM, G12A, G12B & SM1
based boards dues to the lack of the core clock in the device tree.

It was reported breaking EFI boot in the Linux EFI stub, because the
EFI_RNG_PROTOCOL didn't check for the RNG device presence before
installing itself.

The Linux amlogic,meson-rng.yaml doesn't mandate the core clock,
this the clock should be ignores if not present.

Nevertheless, the clock should be present and this should be fixed
on the Linux meson-gxl.dtsi & meson-g12-common.dtsi then synced
with U-Boot.

The change has been tested on a Khadas VIM3, which uses the common
meson-g12-common.dtsi like the Odroid-C4 & Odroid-N2 in Scott's
report, along with the RNG cmd.

Cc: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reported-by: Scott K Logan <logans@cottsay.net>
Fixes: bc40eb278b ("drivers/rng: add Amlogic hardware RNG driver")
Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
Tested-by: Scott K Logan <logans@cottsay.net>
Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
2020-09-28 09:38:11 +02:00

121 lines
2.2 KiB
C

// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
*
* Driver for Amlogic hardware random number generator
*/
#include <common.h>
#include <clk.h>
#include <dm.h>
#include <rng.h>
#include <asm/io.h>
struct meson_rng_platdata {
fdt_addr_t base;
struct clk clk;
};
/**
* meson_rng_read() - fill buffer with random bytes
*
* @buffer: buffer to receive data
* @size: size of buffer
*
* Return: 0
*/
static int meson_rng_read(struct udevice *dev, void *data, size_t len)
{
struct meson_rng_platdata *pdata = dev_get_platdata(dev);
char *buffer = (char *)data;
while (len) {
u32 rand = readl(pdata->base);
size_t step;
if (len >= 4)
step = 4;
else
step = len;
memcpy(buffer, &rand, step);
buffer += step;
len -= step;
}
return 0;
}
/**
* meson_rng_probe() - probe rng device
*
* @dev: device
* Return: 0 if ok
*/
static int meson_rng_probe(struct udevice *dev)
{
struct meson_rng_platdata *pdata = dev_get_platdata(dev);
int err;
err = clk_enable(&pdata->clk);
if (err)
return err;
return 0;
}
/**
* meson_rng_remove() - deinitialize rng device
*
* @dev: device
* Return: 0 if ok
*/
static int meson_rng_remove(struct udevice *dev)
{
struct meson_rng_platdata *pdata = dev_get_platdata(dev);
return clk_disable(&pdata->clk);
}
/**
* meson_rng_ofdata_to_platdata() - transfer device tree data to plaform data
*
* @dev: device
* Return: 0 if ok
*/
static int meson_rng_ofdata_to_platdata(struct udevice *dev)
{
struct meson_rng_platdata *pdata = dev_get_platdata(dev);
int err;
pdata->base = dev_read_addr(dev);
if (!pdata->base)
return -ENODEV;
/* Get optional "core" clock */
err = clk_get_by_name(dev, "core", &pdata->clk);
if (err && err != -ENODATA)
return err;
return 0;
}
static const struct dm_rng_ops meson_rng_ops = {
.read = meson_rng_read,
};
static const struct udevice_id meson_rng_match[] = {
{
.compatible = "amlogic,meson-rng",
},
{},
};
U_BOOT_DRIVER(meson_rng) = {
.name = "meson-rng",
.id = UCLASS_RNG,
.of_match = meson_rng_match,
.ops = &meson_rng_ops,
.probe = meson_rng_probe,
.remove = meson_rng_remove,
.platdata_auto_alloc_size = sizeof(struct meson_rng_platdata),
.ofdata_to_platdata = meson_rng_ofdata_to_platdata,
};