1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019, Linaro Limited
10 #include <virtio_types.h>
12 #include <virtio_ring.h>
14 #define BUFFER_SIZE 16UL
16 struct virtio_rng_priv {
17 struct virtqueue *rng_vq;
20 static int virtio_rng_read(struct udevice *dev, void *data, size_t len)
24 unsigned char buf[BUFFER_SIZE] __aligned(4);
25 unsigned char *ptr = data;
27 struct virtio_sg *sgs[1];
28 struct virtio_rng_priv *priv = dev_get_priv(dev);
32 sg.length = min(len, sizeof(buf));
35 ret = virtqueue_add(priv->rng_vq, sgs, 0, 1);
39 virtqueue_kick(priv->rng_vq);
41 while (!virtqueue_get_buf(priv->rng_vq, &rsize))
44 if (rsize > sg.length)
47 memcpy(ptr, buf, rsize);
55 static int virtio_rng_bind(struct udevice *dev)
57 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
59 /* Indicate what driver features we support */
60 virtio_driver_features_init(uc_priv, NULL, 0, NULL, 0);
65 static int virtio_rng_probe(struct udevice *dev)
67 struct virtio_rng_priv *priv = dev_get_priv(dev);
70 ret = virtio_find_vqs(dev, 1, &priv->rng_vq);
72 debug("%s: virtio_find_vqs failed\n", __func__);
79 static const struct dm_rng_ops virtio_rng_ops = {
80 .read = virtio_rng_read,
83 U_BOOT_DRIVER(virtio_rng) = {
84 .name = VIRTIO_RNG_DRV_NAME,
86 .bind = virtio_rng_bind,
87 .probe = virtio_rng_probe,
88 .remove = virtio_reset,
89 .ops = &virtio_rng_ops,
90 .priv_auto = sizeof(struct virtio_rng_priv),
91 .flags = DM_FLAG_ACTIVE_DMA,