1 /*
2 * Copyright (c) 2016 Intel Corporation.
3 * Copyright (c) 2021,2023 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <string.h>
9 #include <zephyr/types.h>
10 #include <zephyr/drivers/disk.h>
11 #include <errno.h>
12 #include <zephyr/init.h>
13 #include <zephyr/device.h>
14 #include <zephyr/logging/log.h>
15
16 LOG_MODULE_REGISTER(ramdisk, CONFIG_RAMDISK_LOG_LEVEL);
17
18 struct ram_disk_data {
19 struct disk_info info;
20 const size_t sector_size;
21 const size_t sector_count;
22 uint8_t *const buf;
23 };
24
25 struct ram_disk_config {
26 const size_t sector_size;
27 const size_t sector_count;
28 const size_t size;
29 uint8_t *const buf;
30 };
31
lba_to_address(const struct device * dev,uint32_t lba)32 static void *lba_to_address(const struct device *dev, uint32_t lba)
33 {
34 const struct ram_disk_config *config = dev->config;
35
36 return &config->buf[lba * config->sector_size];
37 }
38
disk_ram_access_status(struct disk_info * disk)39 static int disk_ram_access_status(struct disk_info *disk)
40 {
41 return DISK_STATUS_OK;
42 }
43
disk_ram_access_read(struct disk_info * disk,uint8_t * buff,uint32_t sector,uint32_t count)44 static int disk_ram_access_read(struct disk_info *disk, uint8_t *buff,
45 uint32_t sector, uint32_t count)
46 {
47 const struct device *dev = disk->dev;
48 const struct ram_disk_config *config = dev->config;
49 uint32_t last_sector = sector + count;
50
51 if (last_sector < sector || last_sector > config->sector_count) {
52 LOG_ERR("Sector %" PRIu32 " is outside the range %zu",
53 last_sector, config->sector_count);
54 return -EIO;
55 }
56
57 memcpy(buff, lba_to_address(dev, sector), count * config->sector_size);
58
59 return 0;
60 }
61
disk_ram_access_write(struct disk_info * disk,const uint8_t * buff,uint32_t sector,uint32_t count)62 static int disk_ram_access_write(struct disk_info *disk, const uint8_t *buff,
63 uint32_t sector, uint32_t count)
64 {
65 const struct device *dev = disk->dev;
66 const struct ram_disk_config *config = dev->config;
67 uint32_t last_sector = sector + count;
68
69 if (last_sector < sector || last_sector > config->sector_count) {
70 LOG_ERR("Sector %" PRIu32 " is outside the range %zu",
71 last_sector, config->sector_count);
72 return -EIO;
73 }
74
75 memcpy(lba_to_address(dev, sector), buff, count * config->sector_size);
76
77 return 0;
78 }
79
disk_ram_access_ioctl(struct disk_info * disk,uint8_t cmd,void * buff)80 static int disk_ram_access_ioctl(struct disk_info *disk, uint8_t cmd, void *buff)
81 {
82 const struct ram_disk_config *config = disk->dev->config;
83
84 switch (cmd) {
85 case DISK_IOCTL_CTRL_SYNC:
86 break;
87 case DISK_IOCTL_GET_SECTOR_COUNT:
88 *(uint32_t *)buff = config->sector_count;
89 break;
90 case DISK_IOCTL_GET_SECTOR_SIZE:
91 *(uint32_t *)buff = config->sector_size;
92 break;
93 case DISK_IOCTL_GET_ERASE_BLOCK_SZ:
94 *(uint32_t *)buff = 1U;
95 break;
96 case DISK_IOCTL_CTRL_INIT:
97 case DISK_IOCTL_CTRL_DEINIT:
98 break;
99 default:
100 return -EINVAL;
101 }
102
103 return 0;
104 }
105
disk_ram_access_init(struct disk_info * disk)106 static int disk_ram_access_init(struct disk_info *disk)
107 {
108 return disk_ram_access_ioctl(disk, DISK_IOCTL_CTRL_INIT, NULL);
109 }
110
disk_ram_init(const struct device * dev)111 static int disk_ram_init(const struct device *dev)
112 {
113 struct disk_info *info = dev->data;
114
115 info->dev = dev;
116
117 return disk_access_register(info);
118 }
119
120 static const struct disk_operations ram_disk_ops = {
121 .init = disk_ram_access_init,
122 .status = disk_ram_access_status,
123 .read = disk_ram_access_read,
124 .write = disk_ram_access_write,
125 .ioctl = disk_ram_access_ioctl,
126 };
127
128 #define DT_DRV_COMPAT zephyr_ram_disk
129
130 #define RAMDISK_DEVICE_SIZE(n) \
131 (DT_INST_PROP(n, sector_size) * DT_INST_PROP(n, sector_count))
132
133 #define RAMDISK_DEVICE_CONFIG_DEFINE_MEMREG(n) \
134 BUILD_ASSERT(RAMDISK_DEVICE_SIZE(n) <= \
135 DT_REG_SIZE(DT_INST_PHANDLE(n, ram_region)), \
136 "Disk size is smaller than memory region"); \
137 \
138 static struct ram_disk_config disk_config_##n = { \
139 .sector_size = DT_INST_PROP(n, sector_size), \
140 .sector_count = DT_INST_PROP(n, sector_count), \
141 .size = RAMDISK_DEVICE_SIZE(n), \
142 .buf = UINT_TO_POINTER(DT_REG_ADDR(DT_INST_PHANDLE(n, ram_region))), \
143 }
144
145 #define RAMDISK_DEVICE_CONFIG_DEFINE_LOCAL(n) \
146 static uint8_t disk_buf_##n[DT_INST_PROP(n, sector_size) * \
147 DT_INST_PROP(n, sector_count)]; \
148 \
149 static struct ram_disk_config disk_config_##n = { \
150 .sector_size = DT_INST_PROP(n, sector_size), \
151 .sector_count = DT_INST_PROP(n, sector_count), \
152 .size = RAMDISK_DEVICE_SIZE(n), \
153 .buf = disk_buf_##n, \
154 }
155
156 #define RAMDISK_DEVICE_CONFIG_DEFINE(n) \
157 COND_CODE_1(DT_INST_NODE_HAS_PROP(n, ram_region), \
158 (RAMDISK_DEVICE_CONFIG_DEFINE_MEMREG(n)), \
159 (RAMDISK_DEVICE_CONFIG_DEFINE_LOCAL(n)))
160
161 #define RAMDISK_DEVICE_DEFINE(n) \
162 \
163 static struct disk_info disk_info_##n = { \
164 .name = DT_INST_PROP(n, disk_name), \
165 .ops = &ram_disk_ops, \
166 }; \
167 \
168 RAMDISK_DEVICE_CONFIG_DEFINE(n); \
169 \
170 DEVICE_DT_INST_DEFINE(n, disk_ram_init, NULL, \
171 &disk_info_##n, &disk_config_##n, \
172 POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
173 &ram_disk_ops);
174
175 DT_INST_FOREACH_STATUS_OKAY(RAMDISK_DEVICE_DEFINE)
176