1 // SPDX-License-Identifier: GPL-2.0+
3 * Software partition device (UCLASS_PARTITION)
5 * Copyright (c) 2021 Linaro Limited
6 * Author: AKASHI Takahiro
9 #define LOG_CATEGORY UCLASS_PARTITION
17 #include <dm/device-internal.h>
20 int part_create_block_devices(struct udevice *blk_dev)
23 struct blk_desc *desc = dev_get_uclass_plat(blk_dev);
24 struct disk_partition info;
25 struct disk_part *part_data;
30 if (!CONFIG_IS_ENABLED(PARTITIONS) ||
31 !CONFIG_IS_ENABLED(HAVE_BLOCK_DEVICE))
34 if (device_get_uclass_id(blk_dev) != UCLASS_BLK)
37 /* Add devices for each partition */
38 for (count = 0, part = 1; part <= MAX_SEARCH_PARTITIONS; part++) {
39 if (part_get_info(desc, part, &info))
41 snprintf(devname, sizeof(devname), "%s:%d", blk_dev->name,
44 ret = device_bind_driver(blk_dev, "blk_partition",
45 strdup(devname), &dev);
49 part_data = dev_get_uclass_plat(dev);
50 part_data->partnum = part;
51 part_data->gpt_part_info = info;
54 ret = device_probe(dev);
56 debug("Can't probe\n");
63 debug("%s: %d partitions found in %s\n", __func__, count,
69 static ulong blk_part_read(struct udevice *dev, lbaint_t start,
70 lbaint_t blkcnt, void *buffer)
72 struct udevice *parent;
73 struct disk_part *part;
74 const struct blk_ops *ops;
76 parent = dev_get_parent(dev);
77 ops = blk_get_ops(parent);
81 part = dev_get_uclass_plat(dev);
82 if (start >= part->gpt_part_info.size)
85 if ((start + blkcnt) > part->gpt_part_info.size)
86 blkcnt = part->gpt_part_info.size - start;
87 start += part->gpt_part_info.start;
89 return ops->read(parent, start, blkcnt, buffer);
92 static ulong blk_part_write(struct udevice *dev, lbaint_t start,
93 lbaint_t blkcnt, const void *buffer)
95 struct udevice *parent;
96 struct disk_part *part;
97 const struct blk_ops *ops;
99 parent = dev_get_parent(dev);
100 ops = blk_get_ops(parent);
104 part = dev_get_uclass_plat(dev);
105 if (start >= part->gpt_part_info.size)
108 if ((start + blkcnt) > part->gpt_part_info.size)
109 blkcnt = part->gpt_part_info.size - start;
110 start += part->gpt_part_info.start;
112 return ops->write(parent, start, blkcnt, buffer);
115 static ulong blk_part_erase(struct udevice *dev, lbaint_t start,
118 struct udevice *parent;
119 struct disk_part *part;
120 const struct blk_ops *ops;
122 parent = dev_get_parent(dev);
123 ops = blk_get_ops(parent);
127 part = dev_get_uclass_plat(dev);
128 if (start >= part->gpt_part_info.size)
131 if ((start + blkcnt) > part->gpt_part_info.size)
132 blkcnt = part->gpt_part_info.size - start;
133 start += part->gpt_part_info.start;
135 return ops->erase(parent, start, blkcnt);
138 static const struct blk_ops blk_part_ops = {
139 .read = blk_part_read,
140 .write = blk_part_write,
141 .erase = blk_part_erase,
144 U_BOOT_DRIVER(blk_partition) = {
145 .name = "blk_partition",
146 .id = UCLASS_PARTITION,
147 .ops = &blk_part_ops,
153 static struct blk_desc *dev_get_blk(struct udevice *dev)
155 struct blk_desc *block_dev;
157 switch (device_get_uclass_id(dev)) {
159 * We won't support UCLASS_BLK with dev_* interfaces.
161 case UCLASS_PARTITION:
162 block_dev = dev_get_uclass_plat(dev_get_parent(dev));
172 unsigned long dev_read(struct udevice *dev, lbaint_t start,
173 lbaint_t blkcnt, void *buffer)
175 struct blk_desc *block_dev;
176 const struct blk_ops *ops;
177 struct disk_part *part;
178 lbaint_t start_in_disk;
181 block_dev = dev_get_blk(dev);
185 ops = blk_get_ops(dev);
189 start_in_disk = start;
190 if (device_get_uclass_id(dev) == UCLASS_PARTITION) {
191 part = dev_get_uclass_plat(dev);
192 start_in_disk += part->gpt_part_info.start;
195 if (blkcache_read(block_dev->if_type, block_dev->devnum,
196 start_in_disk, blkcnt, block_dev->blksz, buffer))
198 blks_read = ops->read(dev, start, blkcnt, buffer);
199 if (blks_read == blkcnt)
200 blkcache_fill(block_dev->if_type, block_dev->devnum,
201 start_in_disk, blkcnt, block_dev->blksz, buffer);
206 unsigned long dev_write(struct udevice *dev, lbaint_t start,
207 lbaint_t blkcnt, const void *buffer)
209 struct blk_desc *block_dev;
210 const struct blk_ops *ops;
212 block_dev = dev_get_blk(dev);
216 ops = blk_get_ops(dev);
220 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
222 return ops->write(dev, start, blkcnt, buffer);
225 unsigned long dev_erase(struct udevice *dev, lbaint_t start,
228 struct blk_desc *block_dev;
229 const struct blk_ops *ops;
231 block_dev = dev_get_blk(dev);
235 ops = blk_get_ops(dev);
239 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
241 return ops->erase(dev, start, blkcnt);
244 UCLASS_DRIVER(partition) = {
245 .id = UCLASS_PARTITION,
246 .per_device_plat_auto = sizeof(struct disk_part),