Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 1 | // 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 14 | struct meson_rng_plat { |
Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 15 | 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 | */ |
| 27 | static int meson_rng_read(struct udevice *dev, void *data, size_t len) |
| 28 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 29 | struct meson_rng_plat *pdata = dev_get_plat(dev); |
Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 30 | 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 | */ |
| 53 | static int meson_rng_probe(struct udevice *dev) |
| 54 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 55 | struct meson_rng_plat *pdata = dev_get_plat(dev); |
Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 56 | 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 | */ |
| 71 | static int meson_rng_remove(struct udevice *dev) |
| 72 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 73 | struct meson_rng_plat *pdata = dev_get_plat(dev); |
Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 74 | |
| 75 | return clk_disable(&pdata->clk); |
| 76 | } |
| 77 | |
| 78 | /** |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 79 | * meson_rng_of_to_plat() - transfer device tree data to plaform data |
Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 80 | * |
| 81 | * @dev: device |
| 82 | * Return: 0 if ok |
| 83 | */ |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 84 | static int meson_rng_of_to_plat(struct udevice *dev) |
Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 85 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 86 | struct meson_rng_plat *pdata = dev_get_plat(dev); |
Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 87 | int err; |
| 88 | |
| 89 | pdata->base = dev_read_addr(dev); |
| 90 | if (!pdata->base) |
| 91 | return -ENODEV; |
| 92 | |
Neil Armstrong | e733052 | 2020-09-25 09:19:20 +0200 | [diff] [blame] | 93 | /* Get optional "core" clock */ |
Sean Anderson | e1c56f3 | 2022-01-15 15:52:47 -0500 | [diff] [blame] | 94 | err = clk_get_by_name_optional(dev, "core", &pdata->clk); |
| 95 | if (err) |
Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 96 | return err; |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static const struct dm_rng_ops meson_rng_ops = { |
| 102 | .read = meson_rng_read, |
| 103 | }; |
| 104 | |
| 105 | static const struct udevice_id meson_rng_match[] = { |
| 106 | { |
| 107 | .compatible = "amlogic,meson-rng", |
| 108 | }, |
| 109 | {}, |
| 110 | }; |
| 111 | |
| 112 | U_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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 119 | .plat_auto = sizeof(struct meson_rng_plat), |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 120 | .of_to_plat = meson_rng_of_to_plat, |
Heinrich Schuchardt | 009d4a7 | 2020-02-02 19:52:04 +0100 | [diff] [blame] | 121 | }; |