2 * Copyright (C) 2016 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
11 #include <dm/device-internal.h>
14 static const char *if_typename_str[IF_TYPE_COUNT] = {
15 [IF_TYPE_IDE] = "ide",
16 [IF_TYPE_SCSI] = "scsi",
17 [IF_TYPE_ATAPI] = "atapi",
18 [IF_TYPE_USB] = "usb",
19 [IF_TYPE_DOC] = "doc",
20 [IF_TYPE_MMC] = "mmc",
22 [IF_TYPE_SATA] = "sata",
23 [IF_TYPE_HOST] = "host",
24 [IF_TYPE_SYSTEMACE] = "ace",
25 [IF_TYPE_NVME] = "nvme",
28 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
29 [IF_TYPE_IDE] = UCLASS_INVALID,
30 [IF_TYPE_SCSI] = UCLASS_SCSI,
31 [IF_TYPE_ATAPI] = UCLASS_INVALID,
32 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
33 [IF_TYPE_DOC] = UCLASS_INVALID,
34 [IF_TYPE_MMC] = UCLASS_MMC,
35 [IF_TYPE_SD] = UCLASS_INVALID,
36 [IF_TYPE_SATA] = UCLASS_AHCI,
37 [IF_TYPE_HOST] = UCLASS_ROOT,
38 [IF_TYPE_NVME] = UCLASS_NVME,
39 [IF_TYPE_SYSTEMACE] = UCLASS_INVALID,
42 static enum if_type if_typename_to_iftype(const char *if_typename)
46 for (i = 0; i < IF_TYPE_COUNT; i++) {
47 if (if_typename_str[i] &&
48 !strcmp(if_typename, if_typename_str[i]))
52 return IF_TYPE_UNKNOWN;
55 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
57 return if_type_uclass_id[if_type];
60 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
62 struct blk_desc *desc;
66 ret = blk_get_device(if_type, devnum, &dev);
69 desc = dev_get_uclass_platdata(dev);
75 * This function is complicated with driver model. We look up the interface
76 * name in a local table. This gives us an interface type which we can match
77 * against the uclass of the block device's parent.
79 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
81 enum uclass_id uclass_id;
87 if_type = if_typename_to_iftype(if_typename);
88 if (if_type == IF_TYPE_UNKNOWN) {
89 debug("%s: Unknown interface type '%s'\n", __func__,
93 uclass_id = if_type_to_uclass_id(if_type);
94 if (uclass_id == UCLASS_INVALID) {
95 debug("%s: Unknown uclass for interface type'\n",
96 if_typename_str[if_type]);
100 ret = uclass_get(UCLASS_BLK, &uc);
103 uclass_foreach_dev(dev, uc) {
104 struct blk_desc *desc = dev_get_uclass_platdata(dev);
106 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
107 if_type, devnum, dev->name, desc->if_type, desc->devnum);
108 if (desc->devnum != devnum)
111 /* Find out the parent device uclass */
112 if (device_get_uclass_id(dev->parent) != uclass_id) {
113 debug("%s: parent uclass %d, this dev %d\n", __func__,
114 device_get_uclass_id(dev->parent), uclass_id);
118 if (device_probe(dev))
121 debug("%s: Device desc %p\n", __func__, desc);
124 debug("%s: No device found\n", __func__);
130 * get_desc() - Get the block device descriptor for the given device number
132 * @if_type: Interface type
133 * @devnum: Device number (0 = first)
134 * @descp: Returns block device descriptor on success
135 * @return 0 on success, -ENODEV if there is no such device and no device
136 * with a higher device number, -ENOENT if there is no such device but there
137 * is one with a higher number, or other -ve on other error.
139 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
141 bool found_more = false;
147 ret = uclass_get(UCLASS_BLK, &uc);
150 uclass_foreach_dev(dev, uc) {
151 struct blk_desc *desc = dev_get_uclass_platdata(dev);
153 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
154 if_type, devnum, dev->name, desc->if_type, desc->devnum);
155 if (desc->if_type == if_type) {
156 if (desc->devnum == devnum) {
157 ret = device_probe(dev);
163 } else if (desc->devnum > devnum) {
169 return found_more ? -ENOENT : -ENODEV;
172 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
177 ret = blk_get_device(if_type, devnum, &dev);
181 return blk_select_hwpart(dev, hwpart);
184 int blk_list_part(enum if_type if_type)
186 struct blk_desc *desc;
190 for (ok = 0, devnum = 0;; ++devnum) {
191 ret = get_desc(if_type, devnum, &desc);
196 if (desc->part_type != PART_TYPE_UNKNOWN) {
209 int blk_print_part_devnum(enum if_type if_type, int devnum)
211 struct blk_desc *desc;
214 ret = get_desc(if_type, devnum, &desc);
217 if (desc->type == DEV_TYPE_UNKNOWN)
224 void blk_list_devices(enum if_type if_type)
226 struct blk_desc *desc;
231 ret = get_desc(if_type, i, &desc);
236 if (desc->type == DEV_TYPE_UNKNOWN)
237 continue; /* list only known devices */
238 printf("Device %d: ", i);
243 int blk_print_device_num(enum if_type if_type, int devnum)
245 struct blk_desc *desc;
248 ret = get_desc(if_type, devnum, &desc);
251 printf("\nIDE device %d: ", devnum);
257 int blk_show_device(enum if_type if_type, int devnum)
259 struct blk_desc *desc;
262 printf("\nDevice %d: ", devnum);
263 ret = get_desc(if_type, devnum, &desc);
264 if (ret == -ENODEV || ret == -ENOENT) {
265 printf("unknown device\n");
272 if (desc->type == DEV_TYPE_UNKNOWN)
278 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
279 lbaint_t blkcnt, void *buffer)
281 struct blk_desc *desc;
285 ret = get_desc(if_type, devnum, &desc);
288 n = blk_dread(desc, start, blkcnt, buffer);
292 /* flush cache after read */
293 flush_cache((ulong)buffer, blkcnt * desc->blksz);
298 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
299 lbaint_t blkcnt, const void *buffer)
301 struct blk_desc *desc;
304 ret = get_desc(if_type, devnum, &desc);
307 return blk_dwrite(desc, start, blkcnt, buffer);
310 int blk_select_hwpart(struct udevice *dev, int hwpart)
312 const struct blk_ops *ops = blk_get_ops(dev);
316 if (!ops->select_hwpart)
319 return ops->select_hwpart(dev, hwpart);
322 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
324 return blk_select_hwpart(desc->bdev, hwpart);
327 int blk_first_device(int if_type, struct udevice **devp)
329 struct blk_desc *desc;
332 ret = uclass_first_device(UCLASS_BLK, devp);
338 desc = dev_get_uclass_platdata(*devp);
339 if (desc->if_type == if_type)
341 ret = uclass_next_device(devp);
349 int blk_next_device(struct udevice **devp)
351 struct blk_desc *desc;
354 desc = dev_get_uclass_platdata(*devp);
355 if_type = desc->if_type;
357 ret = uclass_next_device(devp);
362 desc = dev_get_uclass_platdata(*devp);
363 if (desc->if_type == if_type)
368 int blk_find_device(int if_type, int devnum, struct udevice **devp)
374 ret = uclass_get(UCLASS_BLK, &uc);
377 uclass_foreach_dev(dev, uc) {
378 struct blk_desc *desc = dev_get_uclass_platdata(dev);
380 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
381 if_type, devnum, dev->name, desc->if_type, desc->devnum);
382 if (desc->if_type == if_type && desc->devnum == devnum) {
391 int blk_get_device(int if_type, int devnum, struct udevice **devp)
395 ret = blk_find_device(if_type, devnum, devp);
399 return device_probe(*devp);
402 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
403 lbaint_t blkcnt, void *buffer)
405 struct udevice *dev = block_dev->bdev;
406 const struct blk_ops *ops = blk_get_ops(dev);
412 if (blkcache_read(block_dev->if_type, block_dev->devnum,
413 start, blkcnt, block_dev->blksz, buffer))
415 blks_read = ops->read(dev, start, blkcnt, buffer);
416 if (blks_read == blkcnt)
417 blkcache_fill(block_dev->if_type, block_dev->devnum,
418 start, blkcnt, block_dev->blksz, buffer);
423 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
424 lbaint_t blkcnt, const void *buffer)
426 struct udevice *dev = block_dev->bdev;
427 const struct blk_ops *ops = blk_get_ops(dev);
432 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
433 return ops->write(dev, start, blkcnt, buffer);
436 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
439 struct udevice *dev = block_dev->bdev;
440 const struct blk_ops *ops = blk_get_ops(dev);
445 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
446 return ops->erase(dev, start, blkcnt);
449 int blk_prepare_device(struct udevice *dev)
451 struct blk_desc *desc = dev_get_uclass_platdata(dev);
458 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
464 device_find_first_child(parent, &dev);
466 debug("%s: No block device found for parent '%s'\n", __func__,
470 id = device_get_uclass_id(dev);
471 if (id != UCLASS_BLK) {
472 debug("%s: Incorrect uclass %s for block device '%s'\n",
473 __func__, uclass_get_name(id), dev->name);
476 ret = device_probe(dev);
484 int blk_find_max_devnum(enum if_type if_type)
487 int max_devnum = -ENODEV;
491 ret = uclass_get(UCLASS_BLK, &uc);
494 uclass_foreach_dev(dev, uc) {
495 struct blk_desc *desc = dev_get_uclass_platdata(dev);
497 if (desc->if_type == if_type && desc->devnum > max_devnum)
498 max_devnum = desc->devnum;
504 static int blk_next_free_devnum(enum if_type if_type)
508 ret = blk_find_max_devnum(if_type);
517 static int blk_claim_devnum(enum if_type if_type, int devnum)
523 ret = uclass_get(UCLASS_BLK, &uc);
526 uclass_foreach_dev(dev, uc) {
527 struct blk_desc *desc = dev_get_uclass_platdata(dev);
529 if (desc->if_type == if_type && desc->devnum == devnum) {
530 int next = blk_next_free_devnum(if_type);
542 int blk_create_device(struct udevice *parent, const char *drv_name,
543 const char *name, int if_type, int devnum, int blksz,
544 lbaint_t size, struct udevice **devp)
546 struct blk_desc *desc;
551 devnum = blk_next_free_devnum(if_type);
553 ret = blk_claim_devnum(if_type, devnum);
554 if (ret < 0 && ret != -ENOENT)
559 ret = device_bind_driver(parent, drv_name, name, &dev);
562 desc = dev_get_uclass_platdata(dev);
563 desc->if_type = if_type;
565 desc->lba = size / blksz;
566 desc->part_type = PART_TYPE_UNKNOWN;
568 desc->devnum = devnum;
574 int blk_create_devicef(struct udevice *parent, const char *drv_name,
575 const char *name, int if_type, int devnum, int blksz,
576 lbaint_t size, struct udevice **devp)
578 char dev_name[30], *str;
581 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
582 str = strdup(dev_name);
586 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
592 device_set_name_alloced(*devp);
597 int blk_unbind_all(int if_type)
600 struct udevice *dev, *next;
603 ret = uclass_get(UCLASS_BLK, &uc);
606 uclass_foreach_dev_safe(dev, next, uc) {
607 struct blk_desc *desc = dev_get_uclass_platdata(dev);
609 if (desc->if_type == if_type) {
610 ret = device_remove(dev, DM_REMOVE_NORMAL);
613 ret = device_unbind(dev);
622 UCLASS_DRIVER(blk) = {
625 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),