blob: 2fb9f6b765ee61010c3f8cfb69dc8cd26b32dcc9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasscceee552016-02-29 15:25:55 -07002/*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glasscceee552016-02-29 15:25:55 -07005 */
6
7#include <common.h>
8#include <blk.h>
9#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Simon Glass655306c2020-05-10 11:39:58 -060012#include <part.h>
Simon Glasscceee552016-02-29 15:25:55 -070013#include <dm/device-internal.h>
14#include <dm/lists.h>
Stefan Roeseef58a902017-11-29 16:46:42 +010015#include <dm/uclass-internal.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Simon Glasscceee552016-02-29 15:25:55 -070017
Simon Glass4131ad52016-05-01 11:36:08 -060018static const char *if_typename_str[IF_TYPE_COUNT] = {
19 [IF_TYPE_IDE] = "ide",
20 [IF_TYPE_SCSI] = "scsi",
21 [IF_TYPE_ATAPI] = "atapi",
22 [IF_TYPE_USB] = "usb",
23 [IF_TYPE_DOC] = "doc",
24 [IF_TYPE_MMC] = "mmc",
25 [IF_TYPE_SD] = "sd",
26 [IF_TYPE_SATA] = "sata",
27 [IF_TYPE_HOST] = "host",
Zhikang Zhang182fccd2017-08-03 02:30:56 -070028 [IF_TYPE_NVME] = "nvme",
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010029 [IF_TYPE_EFI] = "efi",
Tuomas Tynkkynend4580062018-10-15 02:21:10 -070030 [IF_TYPE_VIRTIO] = "virtio",
Anastasiia Lukianenko4fec7f82020-08-06 12:42:55 +030031 [IF_TYPE_PVBLOCK] = "pvblock",
Simon Glass4131ad52016-05-01 11:36:08 -060032};
33
34static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
Bin Mengb650afa2017-09-10 05:12:51 -070035 [IF_TYPE_IDE] = UCLASS_IDE,
Michal Simekc886f352016-09-08 15:06:45 +020036 [IF_TYPE_SCSI] = UCLASS_SCSI,
Simon Glass4131ad52016-05-01 11:36:08 -060037 [IF_TYPE_ATAPI] = UCLASS_INVALID,
38 [IF_TYPE_USB] = UCLASS_MASS_STORAGE,
39 [IF_TYPE_DOC] = UCLASS_INVALID,
40 [IF_TYPE_MMC] = UCLASS_MMC,
41 [IF_TYPE_SD] = UCLASS_INVALID,
42 [IF_TYPE_SATA] = UCLASS_AHCI,
43 [IF_TYPE_HOST] = UCLASS_ROOT,
Heinrich Schuchardt11206f42018-01-21 19:29:30 +010044 [IF_TYPE_NVME] = UCLASS_NVME,
45 [IF_TYPE_EFI] = UCLASS_EFI,
Tuomas Tynkkynend4580062018-10-15 02:21:10 -070046 [IF_TYPE_VIRTIO] = UCLASS_VIRTIO,
Anastasiia Lukianenko4fec7f82020-08-06 12:42:55 +030047 [IF_TYPE_PVBLOCK] = UCLASS_PVBLOCK,
Simon Glass4131ad52016-05-01 11:36:08 -060048};
49
50static enum if_type if_typename_to_iftype(const char *if_typename)
51{
52 int i;
53
54 for (i = 0; i < IF_TYPE_COUNT; i++) {
55 if (if_typename_str[i] &&
56 !strcmp(if_typename, if_typename_str[i]))
57 return i;
58 }
59
60 return IF_TYPE_UNKNOWN;
61}
62
63static enum uclass_id if_type_to_uclass_id(enum if_type if_type)
64{
65 return if_type_uclass_id[if_type];
66}
67
Simon Glass85af5a42017-07-29 11:34:53 -060068const char *blk_get_if_type_name(enum if_type if_type)
69{
70 return if_typename_str[if_type];
71}
72
Simon Glass4131ad52016-05-01 11:36:08 -060073struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum)
74{
75 struct blk_desc *desc;
76 struct udevice *dev;
77 int ret;
78
79 ret = blk_get_device(if_type, devnum, &dev);
80 if (ret)
81 return NULL;
82 desc = dev_get_uclass_platdata(dev);
83
84 return desc;
85}
86
87/*
88 * This function is complicated with driver model. We look up the interface
89 * name in a local table. This gives us an interface type which we can match
90 * against the uclass of the block device's parent.
91 */
92struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, int devnum)
93{
94 enum uclass_id uclass_id;
95 enum if_type if_type;
96 struct udevice *dev;
97 struct uclass *uc;
98 int ret;
99
100 if_type = if_typename_to_iftype(if_typename);
101 if (if_type == IF_TYPE_UNKNOWN) {
102 debug("%s: Unknown interface type '%s'\n", __func__,
103 if_typename);
104 return NULL;
105 }
106 uclass_id = if_type_to_uclass_id(if_type);
107 if (uclass_id == UCLASS_INVALID) {
108 debug("%s: Unknown uclass for interface type'\n",
109 if_typename_str[if_type]);
110 return NULL;
111 }
112
113 ret = uclass_get(UCLASS_BLK, &uc);
114 if (ret)
115 return NULL;
116 uclass_foreach_dev(dev, uc) {
117 struct blk_desc *desc = dev_get_uclass_platdata(dev);
118
119 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
120 if_type, devnum, dev->name, desc->if_type, desc->devnum);
121 if (desc->devnum != devnum)
122 continue;
123
124 /* Find out the parent device uclass */
125 if (device_get_uclass_id(dev->parent) != uclass_id) {
126 debug("%s: parent uclass %d, this dev %d\n", __func__,
127 device_get_uclass_id(dev->parent), uclass_id);
128 continue;
129 }
130
131 if (device_probe(dev))
132 return NULL;
133
134 debug("%s: Device desc %p\n", __func__, desc);
135 return desc;
136 }
137 debug("%s: No device found\n", __func__);
138
139 return NULL;
140}
141
142/**
Tien Fong Chee10378522018-07-06 16:26:36 +0800143 * blk_get_by_device() - Get the block device descriptor for the given device
144 * @dev: Instance of a storage device
145 *
146 * Return: With block device descriptor on success , NULL if there is no such
147 * block device.
148 */
149struct blk_desc *blk_get_by_device(struct udevice *dev)
150{
Simon Glasscfd72932019-09-25 08:55:56 -0600151 struct udevice *child_dev;
Tien Fong Chee10378522018-07-06 16:26:36 +0800152
Simon Glasscfd72932019-09-25 08:55:56 -0600153 device_foreach_child(child_dev, dev) {
Tien Fong Chee10378522018-07-06 16:26:36 +0800154 if (device_get_uclass_id(child_dev) != UCLASS_BLK)
155 continue;
156
157 return dev_get_uclass_platdata(child_dev);
158 }
159
160 debug("%s: No block device found\n", __func__);
161
162 return NULL;
163}
164
165/**
Simon Glass4131ad52016-05-01 11:36:08 -0600166 * get_desc() - Get the block device descriptor for the given device number
167 *
168 * @if_type: Interface type
169 * @devnum: Device number (0 = first)
170 * @descp: Returns block device descriptor on success
171 * @return 0 on success, -ENODEV if there is no such device and no device
172 * with a higher device number, -ENOENT if there is no such device but there
173 * is one with a higher number, or other -ve on other error.
174 */
175static int get_desc(enum if_type if_type, int devnum, struct blk_desc **descp)
176{
177 bool found_more = false;
178 struct udevice *dev;
179 struct uclass *uc;
180 int ret;
181
182 *descp = NULL;
183 ret = uclass_get(UCLASS_BLK, &uc);
184 if (ret)
185 return ret;
186 uclass_foreach_dev(dev, uc) {
187 struct blk_desc *desc = dev_get_uclass_platdata(dev);
188
189 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
190 if_type, devnum, dev->name, desc->if_type, desc->devnum);
191 if (desc->if_type == if_type) {
192 if (desc->devnum == devnum) {
193 ret = device_probe(dev);
194 if (ret)
195 return ret;
196
Michal Simekf438b6b2016-11-16 17:37:42 +0100197 *descp = desc;
198 return 0;
Simon Glass4131ad52016-05-01 11:36:08 -0600199 } else if (desc->devnum > devnum) {
200 found_more = true;
201 }
202 }
203 }
204
205 return found_more ? -ENOENT : -ENODEV;
206}
207
Simon Glass13c2c292016-05-01 13:52:30 -0600208int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart)
209{
210 struct udevice *dev;
211 int ret;
212
213 ret = blk_get_device(if_type, devnum, &dev);
214 if (ret)
215 return ret;
216
developerf50d8d12019-08-27 15:32:18 +0800217 return blk_select_hwpart(dev, hwpart);
Simon Glass13c2c292016-05-01 13:52:30 -0600218}
219
Simon Glass4131ad52016-05-01 11:36:08 -0600220int blk_list_part(enum if_type if_type)
221{
222 struct blk_desc *desc;
223 int devnum, ok;
224 int ret;
225
226 for (ok = 0, devnum = 0;; ++devnum) {
227 ret = get_desc(if_type, devnum, &desc);
228 if (ret == -ENODEV)
229 break;
230 else if (ret)
231 continue;
232 if (desc->part_type != PART_TYPE_UNKNOWN) {
233 ++ok;
234 if (devnum)
235 putc('\n');
236 part_print(desc);
237 }
238 }
239 if (!ok)
240 return -ENODEV;
241
242 return 0;
243}
244
245int blk_print_part_devnum(enum if_type if_type, int devnum)
246{
247 struct blk_desc *desc;
248 int ret;
249
250 ret = get_desc(if_type, devnum, &desc);
251 if (ret)
252 return ret;
253 if (desc->type == DEV_TYPE_UNKNOWN)
254 return -ENOENT;
255 part_print(desc);
256
257 return 0;
258}
259
260void blk_list_devices(enum if_type if_type)
261{
262 struct blk_desc *desc;
263 int ret;
264 int i;
265
266 for (i = 0;; ++i) {
267 ret = get_desc(if_type, i, &desc);
268 if (ret == -ENODEV)
269 break;
270 else if (ret)
271 continue;
272 if (desc->type == DEV_TYPE_UNKNOWN)
273 continue; /* list only known devices */
274 printf("Device %d: ", i);
275 dev_print(desc);
276 }
277}
278
279int blk_print_device_num(enum if_type if_type, int devnum)
280{
281 struct blk_desc *desc;
282 int ret;
283
284 ret = get_desc(if_type, devnum, &desc);
285 if (ret)
286 return ret;
287 printf("\nIDE device %d: ", devnum);
288 dev_print(desc);
289
290 return 0;
291}
292
293int blk_show_device(enum if_type if_type, int devnum)
294{
295 struct blk_desc *desc;
296 int ret;
297
298 printf("\nDevice %d: ", devnum);
299 ret = get_desc(if_type, devnum, &desc);
300 if (ret == -ENODEV || ret == -ENOENT) {
301 printf("unknown device\n");
302 return -ENODEV;
303 }
304 if (ret)
305 return ret;
306 dev_print(desc);
307
308 if (desc->type == DEV_TYPE_UNKNOWN)
309 return -ENOENT;
310
311 return 0;
312}
313
314ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
315 lbaint_t blkcnt, void *buffer)
316{
317 struct blk_desc *desc;
318 ulong n;
319 int ret;
320
321 ret = get_desc(if_type, devnum, &desc);
322 if (ret)
323 return ret;
324 n = blk_dread(desc, start, blkcnt, buffer);
325 if (IS_ERR_VALUE(n))
326 return n;
327
Simon Glass4131ad52016-05-01 11:36:08 -0600328 return n;
329}
330
331ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
332 lbaint_t blkcnt, const void *buffer)
333{
334 struct blk_desc *desc;
335 int ret;
336
337 ret = get_desc(if_type, devnum, &desc);
338 if (ret)
339 return ret;
340 return blk_dwrite(desc, start, blkcnt, buffer);
341}
342
Simon Glass13c2c292016-05-01 13:52:30 -0600343int blk_select_hwpart(struct udevice *dev, int hwpart)
344{
345 const struct blk_ops *ops = blk_get_ops(dev);
346
347 if (!ops)
348 return -ENOSYS;
349 if (!ops->select_hwpart)
350 return 0;
351
352 return ops->select_hwpart(dev, hwpart);
353}
354
355int blk_dselect_hwpart(struct blk_desc *desc, int hwpart)
356{
developerf50d8d12019-08-27 15:32:18 +0800357 return blk_select_hwpart(desc->bdev, hwpart);
Simon Glass13c2c292016-05-01 13:52:30 -0600358}
359
Simon Glasscceee552016-02-29 15:25:55 -0700360int blk_first_device(int if_type, struct udevice **devp)
361{
362 struct blk_desc *desc;
363 int ret;
364
Stefan Roeseef58a902017-11-29 16:46:42 +0100365 ret = uclass_find_first_device(UCLASS_BLK, devp);
Simon Glasscceee552016-02-29 15:25:55 -0700366 if (ret)
367 return ret;
368 if (!*devp)
369 return -ENODEV;
370 do {
371 desc = dev_get_uclass_platdata(*devp);
372 if (desc->if_type == if_type)
373 return 0;
Stefan Roeseef58a902017-11-29 16:46:42 +0100374 ret = uclass_find_next_device(devp);
Simon Glasscceee552016-02-29 15:25:55 -0700375 if (ret)
376 return ret;
377 } while (*devp);
378
379 return -ENODEV;
380}
381
382int blk_next_device(struct udevice **devp)
383{
384 struct blk_desc *desc;
385 int ret, if_type;
386
387 desc = dev_get_uclass_platdata(*devp);
388 if_type = desc->if_type;
389 do {
Stefan Roeseef58a902017-11-29 16:46:42 +0100390 ret = uclass_find_next_device(devp);
Simon Glasscceee552016-02-29 15:25:55 -0700391 if (ret)
392 return ret;
393 if (!*devp)
394 return -ENODEV;
395 desc = dev_get_uclass_platdata(*devp);
396 if (desc->if_type == if_type)
397 return 0;
398 } while (1);
399}
400
Simon Glassd5d4c102017-04-23 20:02:05 -0600401int blk_find_device(int if_type, int devnum, struct udevice **devp)
Simon Glasscceee552016-02-29 15:25:55 -0700402{
403 struct uclass *uc;
404 struct udevice *dev;
405 int ret;
406
407 ret = uclass_get(UCLASS_BLK, &uc);
408 if (ret)
409 return ret;
410 uclass_foreach_dev(dev, uc) {
411 struct blk_desc *desc = dev_get_uclass_platdata(dev);
412
413 debug("%s: if_type=%d, devnum=%d: %s, %d, %d\n", __func__,
414 if_type, devnum, dev->name, desc->if_type, desc->devnum);
415 if (desc->if_type == if_type && desc->devnum == devnum) {
416 *devp = dev;
Simon Glassd5d4c102017-04-23 20:02:05 -0600417 return 0;
Simon Glasscceee552016-02-29 15:25:55 -0700418 }
419 }
420
421 return -ENODEV;
422}
423
Simon Glassd5d4c102017-04-23 20:02:05 -0600424int blk_get_device(int if_type, int devnum, struct udevice **devp)
425{
426 int ret;
427
428 ret = blk_find_device(if_type, devnum, devp);
429 if (ret)
430 return ret;
431
432 return device_probe(*devp);
433}
434
Simon Glasscceee552016-02-29 15:25:55 -0700435unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
436 lbaint_t blkcnt, void *buffer)
437{
438 struct udevice *dev = block_dev->bdev;
439 const struct blk_ops *ops = blk_get_ops(dev);
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700440 ulong blks_read;
Simon Glasscceee552016-02-29 15:25:55 -0700441
442 if (!ops->read)
443 return -ENOSYS;
444
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700445 if (blkcache_read(block_dev->if_type, block_dev->devnum,
446 start, blkcnt, block_dev->blksz, buffer))
447 return blkcnt;
448 blks_read = ops->read(dev, start, blkcnt, buffer);
449 if (blks_read == blkcnt)
450 blkcache_fill(block_dev->if_type, block_dev->devnum,
451 start, blkcnt, block_dev->blksz, buffer);
452
453 return blks_read;
Simon Glasscceee552016-02-29 15:25:55 -0700454}
455
456unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
457 lbaint_t blkcnt, const void *buffer)
458{
459 struct udevice *dev = block_dev->bdev;
460 const struct blk_ops *ops = blk_get_ops(dev);
461
462 if (!ops->write)
463 return -ENOSYS;
464
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700465 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glasscceee552016-02-29 15:25:55 -0700466 return ops->write(dev, start, blkcnt, buffer);
467}
468
469unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
470 lbaint_t blkcnt)
471{
472 struct udevice *dev = block_dev->bdev;
473 const struct blk_ops *ops = blk_get_ops(dev);
474
475 if (!ops->erase)
476 return -ENOSYS;
477
Eric Nelsonfaf4f052016-03-28 10:05:44 -0700478 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
Simon Glasscceee552016-02-29 15:25:55 -0700479 return ops->erase(dev, start, blkcnt);
480}
481
Simon Glass4f269132017-05-27 11:37:17 -0600482int blk_get_from_parent(struct udevice *parent, struct udevice **devp)
483{
484 struct udevice *dev;
485 enum uclass_id id;
486 int ret;
487
488 device_find_first_child(parent, &dev);
489 if (!dev) {
490 debug("%s: No block device found for parent '%s'\n", __func__,
491 parent->name);
492 return -ENODEV;
493 }
494 id = device_get_uclass_id(dev);
495 if (id != UCLASS_BLK) {
496 debug("%s: Incorrect uclass %s for block device '%s'\n",
497 __func__, uclass_get_name(id), dev->name);
498 return -ENOTBLK;
499 }
500 ret = device_probe(dev);
501 if (ret)
502 return ret;
503 *devp = dev;
504
505 return 0;
506}
507
Simon Glassd089ba32016-05-01 11:36:28 -0600508int blk_find_max_devnum(enum if_type if_type)
509{
510 struct udevice *dev;
511 int max_devnum = -ENODEV;
512 struct uclass *uc;
513 int ret;
514
515 ret = uclass_get(UCLASS_BLK, &uc);
516 if (ret)
517 return ret;
518 uclass_foreach_dev(dev, uc) {
519 struct blk_desc *desc = dev_get_uclass_platdata(dev);
520
521 if (desc->if_type == if_type && desc->devnum > max_devnum)
522 max_devnum = desc->devnum;
523 }
524
525 return max_devnum;
526}
527
Bin Mengfd5eda72018-10-15 02:21:09 -0700528int blk_next_free_devnum(enum if_type if_type)
Simon Glassdbc38612017-04-23 20:02:06 -0600529{
530 int ret;
531
532 ret = blk_find_max_devnum(if_type);
533 if (ret == -ENODEV)
534 return 0;
535 if (ret < 0)
536 return ret;
537
538 return ret + 1;
539}
540
Simon Glasse4fef742017-04-23 20:02:07 -0600541static int blk_claim_devnum(enum if_type if_type, int devnum)
542{
543 struct udevice *dev;
544 struct uclass *uc;
545 int ret;
546
547 ret = uclass_get(UCLASS_BLK, &uc);
548 if (ret)
549 return ret;
550 uclass_foreach_dev(dev, uc) {
551 struct blk_desc *desc = dev_get_uclass_platdata(dev);
552
553 if (desc->if_type == if_type && desc->devnum == devnum) {
554 int next = blk_next_free_devnum(if_type);
555
556 if (next < 0)
557 return next;
558 desc->devnum = next;
559 return 0;
560 }
561 }
562
563 return -ENOENT;
564}
565
Simon Glasscceee552016-02-29 15:25:55 -0700566int blk_create_device(struct udevice *parent, const char *drv_name,
567 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200568 lbaint_t lba, struct udevice **devp)
Simon Glasscceee552016-02-29 15:25:55 -0700569{
570 struct blk_desc *desc;
571 struct udevice *dev;
572 int ret;
573
Simon Glassd089ba32016-05-01 11:36:28 -0600574 if (devnum == -1) {
Simon Glasse4fef742017-04-23 20:02:07 -0600575 devnum = blk_next_free_devnum(if_type);
576 } else {
577 ret = blk_claim_devnum(if_type, devnum);
578 if (ret < 0 && ret != -ENOENT)
Simon Glassd089ba32016-05-01 11:36:28 -0600579 return ret;
Simon Glassd089ba32016-05-01 11:36:28 -0600580 }
Simon Glasse4fef742017-04-23 20:02:07 -0600581 if (devnum < 0)
582 return devnum;
Simon Glass77f7fb82016-05-01 13:52:22 -0600583 ret = device_bind_driver(parent, drv_name, name, &dev);
584 if (ret)
585 return ret;
586 desc = dev_get_uclass_platdata(dev);
587 desc->if_type = if_type;
588 desc->blksz = blksz;
Heinrich Schuchardt09ba4ea2019-10-25 12:15:31 +0200589 desc->log2blksz = LOG2(desc->blksz);
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200590 desc->lba = lba;
Simon Glass77f7fb82016-05-01 13:52:22 -0600591 desc->part_type = PART_TYPE_UNKNOWN;
592 desc->bdev = dev;
Simon Glasscceee552016-02-29 15:25:55 -0700593 desc->devnum = devnum;
594 *devp = dev;
595
596 return 0;
597}
598
Simon Glass966b6952016-05-01 11:36:29 -0600599int blk_create_devicef(struct udevice *parent, const char *drv_name,
600 const char *name, int if_type, int devnum, int blksz,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200601 lbaint_t lba, struct udevice **devp)
Simon Glass966b6952016-05-01 11:36:29 -0600602{
603 char dev_name[30], *str;
Simon Glass39e54922016-05-01 13:52:24 -0600604 int ret;
Simon Glass966b6952016-05-01 11:36:29 -0600605
606 snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name);
607 str = strdup(dev_name);
608 if (!str)
609 return -ENOMEM;
610
Simon Glass39e54922016-05-01 13:52:24 -0600611 ret = blk_create_device(parent, drv_name, str, if_type, devnum,
Jean-Jacques Hiblot99b324a2017-06-09 16:45:18 +0200612 blksz, lba, devp);
Simon Glass39e54922016-05-01 13:52:24 -0600613 if (ret) {
614 free(str);
615 return ret;
616 }
617 device_set_name_alloced(*devp);
618
Simon Glass44505972017-07-29 11:34:59 -0600619 return 0;
Simon Glass966b6952016-05-01 11:36:29 -0600620}
621
Simon Glasscceee552016-02-29 15:25:55 -0700622int blk_unbind_all(int if_type)
623{
624 struct uclass *uc;
625 struct udevice *dev, *next;
626 int ret;
627
628 ret = uclass_get(UCLASS_BLK, &uc);
629 if (ret)
630 return ret;
631 uclass_foreach_dev_safe(dev, next, uc) {
632 struct blk_desc *desc = dev_get_uclass_platdata(dev);
633
634 if (desc->if_type == if_type) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100635 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glasscceee552016-02-29 15:25:55 -0700636 if (ret)
637 return ret;
638 ret = device_unbind(dev);
639 if (ret)
640 return ret;
641 }
642 }
643
644 return 0;
645}
646
Bin Mengcbc3da82018-10-15 02:21:07 -0700647static int blk_post_probe(struct udevice *dev)
648{
Ovidiu Panait76160a02020-07-24 14:12:21 +0300649 if (IS_ENABLED(CONFIG_PARTITIONS) &&
650 IS_ENABLED(CONFIG_HAVE_BLOCK_DEVICE)) {
651 struct blk_desc *desc = dev_get_uclass_platdata(dev);
Bin Mengcbc3da82018-10-15 02:21:07 -0700652
Ovidiu Panait76160a02020-07-24 14:12:21 +0300653 part_init(desc);
654 }
Bin Mengcbc3da82018-10-15 02:21:07 -0700655
656 return 0;
657}
658
Simon Glasscceee552016-02-29 15:25:55 -0700659UCLASS_DRIVER(blk) = {
660 .id = UCLASS_BLK,
661 .name = "blk",
Bin Mengcbc3da82018-10-15 02:21:07 -0700662 .post_probe = blk_post_probe,
Simon Glasscceee552016-02-29 15:25:55 -0700663 .per_device_platdata_auto_alloc_size = sizeof(struct blk_desc),
664};