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",
27 static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
28 [IF_TYPE_IDE] = UCLASS_INVALID,
29 [IF_TYPE_SCSI] = UCLASS_SCSI,
30 [IF_TYPE_ATAPI] = UCLASS_INVALID,
31 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
32 [IF_TYPE_DOC] = UCLASS_INVALID,
33 [IF_TYPE_MMC] = UCLASS_MMC,
34 [IF_TYPE_SD] = UCLASS_INVALID,
35 [IF_TYPE_SATA] = UCLASS_AHCI,
36 [IF_TYPE_HOST] = UCLASS_ROOT,
37 [IF_TYPE_SYSTEMACE] = UCLASS_INVALID,
40 static enum if_type if_typename_to_iftype(const char *if_typename)
44 for (i = 0; i < IF_TYPE_COUNT; i++) {
45 if (if_typename_str[i] &&
46 !strcmp(if_typename, if_typename_str[i]))
50 return IF_TYPE_UNKNOWN;
53 static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
55 return if_type_uclass_id[if_type];
58 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
60 struct blk_desc *desc;
64 ret = blk_get_device(if_type, devnum, &dev);
67 desc = dev_get_uclass_platdata(dev);
73 * This function is complicated with driver model. We look up the interface
74 * name in a local table. This gives us an interface type which we can match
75 * against the uclass of the block device's parent.
77 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
79 enum uclass_id uclass_id;
85 if_type = if_typename_to_iftype(if_typename);
86 if (if_type == IF_TYPE_UNKNOWN) {
87 debug("%s: Unknown interface type '%s'\n", __func__,
91 uclass_id = if_type_to_uclass_id(if_type);
92 if (uclass_id == UCLASS_INVALID) {
93 debug("%s: Unknown uclass for interface type'\n",
94 if_typename_str[if_type]);
98 ret = uclass_get(UCLASS_BLK, &uc);
101 uclass_foreach_dev(dev, uc) {
102 struct blk_desc *desc = dev_get_uclass_platdata(dev);
104 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
105 if_type, devnum, dev->name, desc->if_type, desc->devnum);
106 if (desc->devnum != devnum)
109 /* Find out the parent device uclass */
110 if (device_get_uclass_id(dev->parent) != uclass_id) {
111 debug("%s: parent uclass %d, this dev %d\n", __func__,
112 device_get_uclass_id(dev->parent), uclass_id);
116 if (device_probe(dev))
119 debug("%s: Device desc %p\n", __func__, desc);
122 debug("%s: No device found\n", __func__);
128 * get_desc() - Get the block device descriptor for the given device number
130 * @if_type: Interface type
131 * @devnum: Device number (0 = first)
132 * @descp: Returns block device descriptor on success
133 * @return 0 on success, -ENODEV if there is no such device and no device
134 * with a higher device number, -ENOENT if there is no such device but there
135 * is one with a higher number, or other -ve on other error.
137 static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
139 bool found_more = false;
145 ret = uclass_get(UCLASS_BLK, &uc);
148 uclass_foreach_dev(dev, uc) {
149 struct blk_desc *desc = dev_get_uclass_platdata(dev);
151 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
152 if_type, devnum, dev->name, desc->if_type, desc->devnum);
153 if (desc->if_type == if_type) {
154 if (desc->devnum == devnum) {
155 ret = device_probe(dev);
161 } else if (desc->devnum > devnum) {
167 return found_more ? -ENOENT : -ENODEV;
170 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
175 ret = blk_get_device(if_type, devnum, &dev);
179 return blk_select_hwpart(dev, hwpart);
182 int blk_list_part(enum if_type if_type)
184 struct blk_desc *desc;
188 for (ok = 0, devnum = 0;; ++devnum) {
189 ret = get_desc(if_type, devnum, &desc);
194 if (desc->part_type != PART_TYPE_UNKNOWN) {
207 int blk_print_part_devnum(enum if_type if_type, int devnum)
209 struct blk_desc *desc;
212 ret = get_desc(if_type, devnum, &desc);
215 if (desc->type == DEV_TYPE_UNKNOWN)
222 void blk_list_devices(enum if_type if_type)
224 struct blk_desc *desc;
229 ret = get_desc(if_type, i, &desc);
234 if (desc->type == DEV_TYPE_UNKNOWN)
235 continue; /* list only known devices */
236 printf("Device %d: ", i);
241 int blk_print_device_num(enum if_type if_type, int devnum)
243 struct blk_desc *desc;
246 ret = get_desc(if_type, devnum, &desc);
249 printf("\nIDE device %d: ", devnum);
255 int blk_show_device(enum if_type if_type, int devnum)
257 struct blk_desc *desc;
260 printf("\nDevice %d: ", devnum);
261 ret = get_desc(if_type, devnum, &desc);
262 if (ret == -ENODEV || ret == -ENOENT) {
263 printf("unknown device\n");
270 if (desc->type == DEV_TYPE_UNKNOWN)
276 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
277 lbaint_t blkcnt, void *buffer)
279 struct blk_desc *desc;
283 ret = get_desc(if_type, devnum, &desc);
286 n = blk_dread(desc, start, blkcnt, buffer);
290 /* flush cache after read */
291 flush_cache((ulong)buffer, blkcnt * desc->blksz);
296 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
297 lbaint_t blkcnt, const void *buffer)
299 struct blk_desc *desc;
302 ret = get_desc(if_type, devnum, &desc);
305 return blk_dwrite(desc, start, blkcnt, buffer);
308 int blk_select_hwpart(struct udevice *dev, int hwpart)
310 const struct blk_ops *ops = blk_get_ops(dev);
314 if (!ops->select_hwpart)
317 return ops->select_hwpart(dev, hwpart);
320 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
322 return blk_select_hwpart(desc->bdev, hwpart);
325 int blk_first_device(int if_type, struct udevice **devp)
327 struct blk_desc *desc;
330 ret = uclass_first_device(UCLASS_BLK, devp);
336 desc = dev_get_uclass_platdata(*devp);
337 if (desc->if_type == if_type)
339 ret = uclass_next_device(devp);
347 int blk_next_device(struct udevice **devp)
349 struct blk_desc *desc;
352 desc = dev_get_uclass_platdata(*devp);
353 if_type = desc->if_type;
355 ret = uclass_next_device(devp);
360 desc = dev_get_uclass_platdata(*devp);
361 if (desc->if_type == if_type)
366 int blk_find_device(int if_type, int devnum, struct udevice **devp)
372 ret = uclass_get(UCLASS_BLK, &uc);
375 uclass_foreach_dev(dev, uc) {
376 struct blk_desc *desc = dev_get_uclass_platdata(dev);
378 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
379 if_type, devnum, dev->name, desc->if_type, desc->devnum);
380 if (desc->if_type == if_type && desc->devnum == devnum) {
389 int blk_get_device(int if_type, int devnum, struct udevice **devp)
393 ret = blk_find_device(if_type, devnum, devp);
397 return device_probe(*devp);
400 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
401 lbaint_t blkcnt, void *buffer)
403 struct udevice *dev = block_dev->bdev;
404 const struct blk_ops *ops = blk_get_ops(dev);
410 if (blkcache_read(block_dev->if_type, block_dev->devnum,
411 start, blkcnt, block_dev->blksz, buffer))
413 blks_read = ops->read(dev, start, blkcnt, buffer);
414 if (blks_read == blkcnt)
415 blkcache_fill(block_dev->if_type, block_dev->devnum,
416 start, blkcnt, block_dev->blksz, buffer);
421 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
422 lbaint_t blkcnt, const void *buffer)
424 struct udevice *dev = block_dev->bdev;
425 const struct blk_ops *ops = blk_get_ops(dev);
430 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
431 return ops->write(dev, start, blkcnt, buffer);
434 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
437 struct udevice *dev = block_dev->bdev;
438 const struct blk_ops *ops = blk_get_ops(dev);
443 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
444 return ops->erase(dev, start, blkcnt);
447 int blk_prepare_device(struct udevice *dev)
449 struct blk_desc *desc = dev_get_uclass_platdata(dev);
456 int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
462 device_find_first_child(parent, &dev);
464 debug("%s: No block device found for parent '%s'\n", __func__,
468 id = device_get_uclass_id(dev);
469 if (id != UCLASS_BLK) {
470 debug("%s: Incorrect uclass %s for block device '%s'\n",
471 __func__, uclass_get_name(id), dev->name);
474 ret = device_probe(dev);
482 int blk_find_max_devnum(enum if_type if_type)
485 int max_devnum = -ENODEV;
489 ret = uclass_get(UCLASS_BLK, &uc);
492 uclass_foreach_dev(dev, uc) {
493 struct blk_desc *desc = dev_get_uclass_platdata(dev);
495 if (desc->if_type == if_type && desc->devnum > max_devnum)
496 max_devnum = desc->devnum;
502 static int blk_next_free_devnum(enum if_type if_type)
506 ret = blk_find_max_devnum(if_type);
515 static int blk_claim_devnum(enum if_type if_type, int devnum)
521 ret = uclass_get(UCLASS_BLK, &uc);
524 uclass_foreach_dev(dev, uc) {
525 struct blk_desc *desc = dev_get_uclass_platdata(dev);
527 if (desc->if_type == if_type && desc->devnum == devnum) {
528 int next = blk_next_free_devnum(if_type);
540 int blk_create_device(struct udevice *parent, const char *drv_name,
541 const char *name, int if_type, int devnum, int blksz,
542 lbaint_t size, struct udevice **devp)
544 struct blk_desc *desc;
549 devnum = blk_next_free_devnum(if_type);
551 ret = blk_claim_devnum(if_type, devnum);
552 if (ret < 0 && ret != -ENOENT)
557 ret = device_bind_driver(parent, drv_name, name, &dev);
560 desc = dev_get_uclass_platdata(dev);
561 desc->if_type = if_type;
563 desc->lba = size / blksz;
564 desc->part_type = PART_TYPE_UNKNOWN;
566 desc->devnum = devnum;
572 int blk_create_devicef(struct udevice *parent, const char *drv_name,
573 const char *name, int if_type, int devnum, int blksz,
574 lbaint_t size, struct udevice **devp)
576 char dev_name[30], *str;
579 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
580 str = strdup(dev_name);
584 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
590 device_set_name_alloced(*devp);
595 int blk_unbind_all(int if_type)
598 struct udevice *dev, *next;
601 ret = uclass_get(UCLASS_BLK, &uc);
604 uclass_foreach_dev_safe(dev, next, uc) {
605 struct blk_desc *desc = dev_get_uclass_platdata(dev);
607 if (desc->if_type == if_type) {
608 ret = device_remove(dev, DM_REMOVE_NORMAL);
611 ret = device_unbind(dev);
620 UCLASS_DRIVER(blk) = {
623 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),