summaryrefslogtreecommitdiff
path: root/bsp/xuantie/virt64/c906/board/drv_virtio.c
blob: 7dbba11984646df459357eda0d3bbe2e06f733d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-11     GuEe-GUI     the first version
 */

#include <rtthread.h>
#include <virt.h>

#ifdef BSP_USING_VIRTIO

#include <virtio.h>
#ifdef BSP_USING_VIRTIO_BLK
#include <virtio_blk.h>
#endif


#include <board.h>

static virtio_device_init_handler virtio_device_init_handlers[] =
{
#ifdef BSP_USING_VIRTIO_BLK
    [VIRTIO_DEVICE_ID_BLOCK]    = rt_virtio_blk_init,
#endif
    [VIRTIO_DEVICE_TYPE_SIZE]   = RT_NULL
};

int rt_virtio_devices_init(void)
{
    int i;
    rt_uint32_t irq = VIRTIO_IRQ_BASE;
    rt_ubase_t mmio_base = VIRTIO_MMIO_BASE;
    struct virtio_mmio_config *mmio_config;
    virtio_device_init_handler init_handler;

    if (sizeof(virtio_device_init_handlers) == 0)
    {
        /* The compiler will optimize the codes after here. */
        return 0;
    }

#ifdef RT_USING_SMART
    mmio_base = (rt_ubase_t)rt_ioremap((void *)mmio_base, VIRTIO_MMIO_SIZE * VIRTIO_MAX_NR);

    if (mmio_base == RT_NULL)
    {
        return -RT_ERROR;
    }
#endif

    for (i = 0; i < VIRTIO_MAX_NR; ++i, ++irq, mmio_base += VIRTIO_MMIO_SIZE)
    {
        mmio_config = (struct virtio_mmio_config *)mmio_base;

        if (mmio_config->magic != VIRTIO_MAGIC_VALUE ||
            mmio_config->version != RT_USING_VIRTIO_VERSION ||
            mmio_config->vendor_id != VIRTIO_VENDOR_ID)
        {
            continue;
        }

        init_handler = virtio_device_init_handlers[mmio_config->device_id];

        if (init_handler != RT_NULL)
        {
            init_handler((rt_ubase_t *)mmio_base, irq);
        }
    }

    return 0;
}
INIT_DEVICE_EXPORT(rt_virtio_devices_init);
#endif  /* BSP_USING_VIRTIO */