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 memcpy(ptr, buf, rsize);
52 static int virtio_rng_bind(struct udevice *dev)
54 struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent);
56 /* Indicate what driver features we support */
57 virtio_driver_features_init(uc_priv, NULL, 0, NULL, 0);
62 static int virtio_rng_probe(struct udevice *dev)
64 struct virtio_rng_priv *priv = dev_get_priv(dev);
67 ret = virtio_find_vqs(dev, 1, &priv->rng_vq);
69 debug("%s: virtio_find_vqs failed\n", __func__);
76 static const struct dm_rng_ops virtio_rng_ops = {
77 .read = virtio_rng_read,
80 U_BOOT_DRIVER(virtio_rng) = {
81 .name = VIRTIO_RNG_DRV_NAME,
83 .bind = virtio_rng_bind,
84 .probe = virtio_rng_probe,
85 .remove = virtio_reset,
86 .ops = &virtio_rng_ops,
87 .priv_auto_alloc_size = sizeof(struct virtio_rng_priv),
88 .flags = DM_FLAG_ACTIVE_DMA,