Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_BLK |
| 8 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 9 | #include <common.h> |
| 10 | #include <blk.h> |
| 11 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 14 | #include <part.h> |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 15 | #include <dm/device-internal.h> |
| 16 | #include <dm/lists.h> |
Stefan Roese | ef58a90 | 2017-11-29 16:46:42 +0100 | [diff] [blame] | 17 | #include <dm/uclass-internal.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 19 | |
Simon Glass | 95e6e03 | 2022-08-11 19:34:57 -0600 | [diff] [blame] | 20 | static struct { |
| 21 | enum uclass_id id; |
| 22 | const char *name; |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 23 | } uclass_idname_str[] = { |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 24 | { UCLASS_IDE, "ide" }, |
| 25 | { UCLASS_SCSI, "scsi" }, |
| 26 | { UCLASS_USB, "usb" }, |
| 27 | { UCLASS_MMC, "mmc" }, |
| 28 | { UCLASS_AHCI, "sata" }, |
| 29 | { UCLASS_ROOT, "host" }, |
| 30 | { UCLASS_NVME, "nvme" }, |
| 31 | { UCLASS_EFI_MEDIA, "efi" }, |
| 32 | { UCLASS_EFI_LOADER, "efiloader" }, |
| 33 | { UCLASS_VIRTIO, "virtio" }, |
| 34 | { UCLASS_PVBLOCK, "pvblock" }, |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 35 | }; |
| 36 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 37 | static enum uclass_id uclass_name_to_iftype(const char *uclass_idname) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 38 | { |
| 39 | int i; |
| 40 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 41 | for (i = 0; i < ARRAY_SIZE(uclass_idname_str); i++) { |
| 42 | if (!strcmp(uclass_idname, uclass_idname_str[i].name)) |
| 43 | return uclass_idname_str[i].id; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 44 | } |
| 45 | |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 46 | return UCLASS_INVALID; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 47 | } |
| 48 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 49 | static enum uclass_id conv_uclass_id(enum uclass_id uclass_id) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 50 | { |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 51 | /* |
| 52 | * This strange adjustment is used because we use UCLASS_MASS_STORAGE |
| 53 | * for USB storage devices, so need to return this as the uclass to |
| 54 | * use for USB. In fact USB_UCLASS is for USB controllers, not |
| 55 | * peripherals. |
| 56 | * |
| 57 | * The name of the UCLASS_MASS_STORAGE uclass driver is |
| 58 | * "usb_mass_storage", but we want to use "usb" in things like the |
| 59 | * 'part list' command and when showing interfaces. |
| 60 | * |
| 61 | * So for now we have this one-way conversion. |
| 62 | * |
| 63 | * The fix for this is possibly to: |
| 64 | * - rename UCLASS_MASS_STORAGE name to "usb" |
| 65 | * - rename UCLASS_USB name to "usb_ctlr" |
| 66 | * - use UCLASS_MASS_STORAGE instead of UCLASS_USB in if_typename_str |
| 67 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 68 | if (uclass_id == UCLASS_USB) |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 69 | return UCLASS_MASS_STORAGE; |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 70 | return uclass_id; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 71 | } |
| 72 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 73 | const char *blk_get_uclass_name(enum uclass_id uclass_id) |
Simon Glass | 85af5a4 | 2017-07-29 11:34:53 -0600 | [diff] [blame] | 74 | { |
Simon Glass | 95e6e03 | 2022-08-11 19:34:57 -0600 | [diff] [blame] | 75 | int i; |
| 76 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 77 | for (i = 0; i < ARRAY_SIZE(uclass_idname_str); i++) { |
| 78 | if (uclass_idname_str[i].id == uclass_id) |
| 79 | return uclass_idname_str[i].name; |
Simon Glass | 95e6e03 | 2022-08-11 19:34:57 -0600 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return "(none)"; |
Simon Glass | 85af5a4 | 2017-07-29 11:34:53 -0600 | [diff] [blame] | 83 | } |
| 84 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 85 | struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 86 | { |
| 87 | struct blk_desc *desc; |
| 88 | struct udevice *dev; |
| 89 | int ret; |
| 90 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 91 | ret = blk_get_device(uclass_id, devnum, &dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 92 | if (ret) |
| 93 | return NULL; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 94 | desc = dev_get_uclass_plat(dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 95 | |
| 96 | return desc; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * This function is complicated with driver model. We look up the interface |
| 101 | * name in a local table. This gives us an interface type which we can match |
| 102 | * against the uclass of the block device's parent. |
| 103 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 104 | struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 105 | { |
| 106 | enum uclass_id uclass_id; |
Simon Glass | 57b3d2e | 2022-08-11 19:35:01 -0600 | [diff] [blame] | 107 | enum uclass_id type; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 108 | struct udevice *dev; |
| 109 | struct uclass *uc; |
| 110 | int ret; |
| 111 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 112 | type = uclass_name_to_iftype(uclass_idname); |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 113 | if (type == UCLASS_INVALID) { |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 114 | debug("%s: Unknown interface type '%s'\n", __func__, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 115 | uclass_idname); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 116 | return NULL; |
| 117 | } |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 118 | uclass_id = conv_uclass_id(type); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 119 | if (uclass_id == UCLASS_INVALID) { |
| 120 | debug("%s: Unknown uclass for interface type'\n", |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 121 | blk_get_uclass_name(type)); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | ret = uclass_get(UCLASS_BLK, &uc); |
| 126 | if (ret) |
| 127 | return NULL; |
| 128 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 129 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 130 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 131 | debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 132 | type, devnum, dev->name, desc->uclass_id, desc->devnum); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 133 | if (desc->devnum != devnum) |
| 134 | continue; |
| 135 | |
| 136 | /* Find out the parent device uclass */ |
| 137 | if (device_get_uclass_id(dev->parent) != uclass_id) { |
| 138 | debug("%s: parent uclass %d, this dev %d\n", __func__, |
| 139 | device_get_uclass_id(dev->parent), uclass_id); |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | if (device_probe(dev)) |
| 144 | return NULL; |
| 145 | |
| 146 | debug("%s: Device desc %p\n", __func__, desc); |
| 147 | return desc; |
| 148 | } |
| 149 | debug("%s: No device found\n", __func__); |
| 150 | |
| 151 | return NULL; |
| 152 | } |
| 153 | |
| 154 | /** |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 155 | * blk_get_by_device() - Get the block device descriptor for the given device |
| 156 | * @dev: Instance of a storage device |
| 157 | * |
| 158 | * Return: With block device descriptor on success , NULL if there is no such |
| 159 | * block device. |
| 160 | */ |
| 161 | struct blk_desc *blk_get_by_device(struct udevice *dev) |
| 162 | { |
Simon Glass | cfd7293 | 2019-09-25 08:55:56 -0600 | [diff] [blame] | 163 | struct udevice *child_dev; |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 164 | |
Simon Glass | cfd7293 | 2019-09-25 08:55:56 -0600 | [diff] [blame] | 165 | device_foreach_child(child_dev, dev) { |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 166 | if (device_get_uclass_id(child_dev) != UCLASS_BLK) |
| 167 | continue; |
| 168 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 169 | return dev_get_uclass_plat(child_dev); |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | debug("%s: No block device found\n", __func__); |
| 173 | |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | /** |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 178 | * get_desc() - Get the block device descriptor for the given device number |
| 179 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 180 | * @uclass_id: Interface type |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 181 | * @devnum: Device number (0 = first) |
| 182 | * @descp: Returns block device descriptor on success |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 183 | * Return: 0 on success, -ENODEV if there is no such device and no device |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 184 | * with a higher device number, -ENOENT if there is no such device but there |
| 185 | * is one with a higher number, or other -ve on other error. |
| 186 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 187 | static int get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 188 | { |
| 189 | bool found_more = false; |
| 190 | struct udevice *dev; |
| 191 | struct uclass *uc; |
| 192 | int ret; |
| 193 | |
| 194 | *descp = NULL; |
| 195 | ret = uclass_get(UCLASS_BLK, &uc); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 199 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 200 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 201 | debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 202 | uclass_id, devnum, dev->name, desc->uclass_id, desc->devnum); |
| 203 | if (desc->uclass_id == uclass_id) { |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 204 | if (desc->devnum == devnum) { |
| 205 | ret = device_probe(dev); |
| 206 | if (ret) |
| 207 | return ret; |
| 208 | |
Michal Simek | f438b6b | 2016-11-16 17:37:42 +0100 | [diff] [blame] | 209 | *descp = desc; |
| 210 | return 0; |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 211 | } else if (desc->devnum > devnum) { |
| 212 | found_more = true; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | return found_more ? -ENOENT : -ENODEV; |
| 218 | } |
| 219 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 220 | int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart) |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 221 | { |
| 222 | struct udevice *dev; |
| 223 | int ret; |
| 224 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 225 | ret = blk_get_device(uclass_id, devnum, &dev); |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 226 | if (ret) |
| 227 | return ret; |
| 228 | |
developer | f50d8d1 | 2019-08-27 15:32:18 +0800 | [diff] [blame] | 229 | return blk_select_hwpart(dev, hwpart); |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 230 | } |
| 231 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 232 | int blk_list_part(enum uclass_id uclass_id) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 233 | { |
| 234 | struct blk_desc *desc; |
| 235 | int devnum, ok; |
| 236 | int ret; |
| 237 | |
| 238 | for (ok = 0, devnum = 0;; ++devnum) { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 239 | ret = get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 240 | if (ret == -ENODEV) |
| 241 | break; |
| 242 | else if (ret) |
| 243 | continue; |
| 244 | if (desc->part_type != PART_TYPE_UNKNOWN) { |
| 245 | ++ok; |
| 246 | if (devnum) |
| 247 | putc('\n'); |
| 248 | part_print(desc); |
| 249 | } |
| 250 | } |
| 251 | if (!ok) |
| 252 | return -ENODEV; |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 257 | int blk_print_part_devnum(enum uclass_id uclass_id, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 258 | { |
| 259 | struct blk_desc *desc; |
| 260 | int ret; |
| 261 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 262 | ret = get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 263 | if (ret) |
| 264 | return ret; |
| 265 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 266 | return -ENOENT; |
| 267 | part_print(desc); |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 272 | void blk_list_devices(enum uclass_id uclass_id) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 273 | { |
| 274 | struct blk_desc *desc; |
| 275 | int ret; |
| 276 | int i; |
| 277 | |
| 278 | for (i = 0;; ++i) { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 279 | ret = get_desc(uclass_id, i, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 280 | if (ret == -ENODEV) |
| 281 | break; |
| 282 | else if (ret) |
| 283 | continue; |
| 284 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 285 | continue; /* list only known devices */ |
| 286 | printf("Device %d: ", i); |
| 287 | dev_print(desc); |
| 288 | } |
| 289 | } |
| 290 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 291 | int blk_print_device_num(enum uclass_id uclass_id, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 292 | { |
| 293 | struct blk_desc *desc; |
| 294 | int ret; |
| 295 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 296 | ret = get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 297 | if (ret) |
| 298 | return ret; |
| 299 | printf("\nIDE device %d: ", devnum); |
| 300 | dev_print(desc); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 305 | int blk_show_device(enum uclass_id uclass_id, int devnum) |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 306 | { |
| 307 | struct blk_desc *desc; |
| 308 | int ret; |
| 309 | |
| 310 | printf("\nDevice %d: ", devnum); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 311 | ret = get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 312 | if (ret == -ENODEV || ret == -ENOENT) { |
| 313 | printf("unknown device\n"); |
| 314 | return -ENODEV; |
| 315 | } |
| 316 | if (ret) |
| 317 | return ret; |
| 318 | dev_print(desc); |
| 319 | |
| 320 | if (desc->type == DEV_TYPE_UNKNOWN) |
| 321 | return -ENOENT; |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 326 | ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start, |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 327 | lbaint_t blkcnt, void *buffer) |
| 328 | { |
| 329 | struct blk_desc *desc; |
| 330 | ulong n; |
| 331 | int ret; |
| 332 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 333 | ret = get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 334 | if (ret) |
| 335 | return ret; |
| 336 | n = blk_dread(desc, start, blkcnt, buffer); |
| 337 | if (IS_ERR_VALUE(n)) |
| 338 | return n; |
| 339 | |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 340 | return n; |
| 341 | } |
| 342 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 343 | ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start, |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 344 | lbaint_t blkcnt, const void *buffer) |
| 345 | { |
| 346 | struct blk_desc *desc; |
| 347 | int ret; |
| 348 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 349 | ret = get_desc(uclass_id, devnum, &desc); |
Simon Glass | 4131ad5 | 2016-05-01 11:36:08 -0600 | [diff] [blame] | 350 | if (ret) |
| 351 | return ret; |
| 352 | return blk_dwrite(desc, start, blkcnt, buffer); |
| 353 | } |
| 354 | |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 355 | int blk_select_hwpart(struct udevice *dev, int hwpart) |
| 356 | { |
| 357 | const struct blk_ops *ops = blk_get_ops(dev); |
| 358 | |
| 359 | if (!ops) |
| 360 | return -ENOSYS; |
| 361 | if (!ops->select_hwpart) |
| 362 | return 0; |
| 363 | |
| 364 | return ops->select_hwpart(dev, hwpart); |
| 365 | } |
| 366 | |
| 367 | int blk_dselect_hwpart(struct blk_desc *desc, int hwpart) |
| 368 | { |
developer | f50d8d1 | 2019-08-27 15:32:18 +0800 | [diff] [blame] | 369 | return blk_select_hwpart(desc->bdev, hwpart); |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 370 | } |
| 371 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 372 | int blk_first_device(int uclass_id, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 373 | { |
| 374 | struct blk_desc *desc; |
| 375 | int ret; |
| 376 | |
Stefan Roese | ef58a90 | 2017-11-29 16:46:42 +0100 | [diff] [blame] | 377 | ret = uclass_find_first_device(UCLASS_BLK, devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 378 | if (ret) |
| 379 | return ret; |
| 380 | if (!*devp) |
| 381 | return -ENODEV; |
| 382 | do { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 383 | desc = dev_get_uclass_plat(*devp); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 384 | if (desc->uclass_id == uclass_id) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 385 | return 0; |
Stefan Roese | ef58a90 | 2017-11-29 16:46:42 +0100 | [diff] [blame] | 386 | ret = uclass_find_next_device(devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 387 | if (ret) |
| 388 | return ret; |
| 389 | } while (*devp); |
| 390 | |
| 391 | return -ENODEV; |
| 392 | } |
| 393 | |
| 394 | int blk_next_device(struct udevice **devp) |
| 395 | { |
| 396 | struct blk_desc *desc; |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 397 | int ret, uclass_id; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 398 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 399 | desc = dev_get_uclass_plat(*devp); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 400 | uclass_id = desc->uclass_id; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 401 | do { |
Stefan Roese | ef58a90 | 2017-11-29 16:46:42 +0100 | [diff] [blame] | 402 | ret = uclass_find_next_device(devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 403 | if (ret) |
| 404 | return ret; |
| 405 | if (!*devp) |
| 406 | return -ENODEV; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 407 | desc = dev_get_uclass_plat(*devp); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 408 | if (desc->uclass_id == uclass_id) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 409 | return 0; |
| 410 | } while (1); |
| 411 | } |
| 412 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 413 | int blk_find_device(int uclass_id, int devnum, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 414 | { |
| 415 | struct uclass *uc; |
| 416 | struct udevice *dev; |
| 417 | int ret; |
| 418 | |
| 419 | ret = uclass_get(UCLASS_BLK, &uc); |
| 420 | if (ret) |
| 421 | return ret; |
| 422 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 423 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 424 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 425 | debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 426 | uclass_id, devnum, dev->name, desc->uclass_id, desc->devnum); |
| 427 | if (desc->uclass_id == uclass_id && desc->devnum == devnum) { |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 428 | *devp = dev; |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 429 | return 0; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | return -ENODEV; |
| 434 | } |
| 435 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 436 | int blk_get_device(int uclass_id, int devnum, struct udevice **devp) |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 437 | { |
| 438 | int ret; |
| 439 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 440 | ret = blk_find_device(uclass_id, devnum, devp); |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 441 | if (ret) |
| 442 | return ret; |
| 443 | |
| 444 | return device_probe(*devp); |
| 445 | } |
| 446 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 447 | unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start, |
| 448 | lbaint_t blkcnt, void *buffer) |
| 449 | { |
| 450 | struct udevice *dev = block_dev->bdev; |
| 451 | const struct blk_ops *ops = blk_get_ops(dev); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 452 | ulong blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 453 | |
| 454 | if (!ops->read) |
| 455 | return -ENOSYS; |
| 456 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 457 | if (blkcache_read(block_dev->uclass_id, block_dev->devnum, |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 458 | start, blkcnt, block_dev->blksz, buffer)) |
| 459 | return blkcnt; |
| 460 | blks_read = ops->read(dev, start, blkcnt, buffer); |
| 461 | if (blks_read == blkcnt) |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 462 | blkcache_fill(block_dev->uclass_id, block_dev->devnum, |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 463 | start, blkcnt, block_dev->blksz, buffer); |
| 464 | |
| 465 | return blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start, |
| 469 | lbaint_t blkcnt, const void *buffer) |
| 470 | { |
| 471 | struct udevice *dev = block_dev->bdev; |
| 472 | const struct blk_ops *ops = blk_get_ops(dev); |
| 473 | |
| 474 | if (!ops->write) |
| 475 | return -ENOSYS; |
| 476 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 477 | blkcache_invalidate(block_dev->uclass_id, block_dev->devnum); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 478 | return ops->write(dev, start, blkcnt, buffer); |
| 479 | } |
| 480 | |
| 481 | unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, |
| 482 | lbaint_t blkcnt) |
| 483 | { |
| 484 | struct udevice *dev = block_dev->bdev; |
| 485 | const struct blk_ops *ops = blk_get_ops(dev); |
| 486 | |
| 487 | if (!ops->erase) |
| 488 | return -ENOSYS; |
| 489 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 490 | blkcache_invalidate(block_dev->uclass_id, block_dev->devnum); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 491 | return ops->erase(dev, start, blkcnt); |
| 492 | } |
| 493 | |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 494 | int blk_get_from_parent(struct udevice *parent, struct udevice **devp) |
| 495 | { |
| 496 | struct udevice *dev; |
| 497 | enum uclass_id id; |
| 498 | int ret; |
| 499 | |
| 500 | device_find_first_child(parent, &dev); |
| 501 | if (!dev) { |
| 502 | debug("%s: No block device found for parent '%s'\n", __func__, |
| 503 | parent->name); |
| 504 | return -ENODEV; |
| 505 | } |
| 506 | id = device_get_uclass_id(dev); |
| 507 | if (id != UCLASS_BLK) { |
| 508 | debug("%s: Incorrect uclass %s for block device '%s'\n", |
| 509 | __func__, uclass_get_name(id), dev->name); |
| 510 | return -ENOTBLK; |
| 511 | } |
| 512 | ret = device_probe(dev); |
| 513 | if (ret) |
| 514 | return ret; |
| 515 | *devp = dev; |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
Simon Glass | f3086cf | 2022-04-24 23:31:03 -0600 | [diff] [blame] | 520 | const char *blk_get_devtype(struct udevice *dev) |
| 521 | { |
| 522 | struct udevice *parent = dev_get_parent(dev); |
| 523 | |
| 524 | return uclass_get_name(device_get_uclass_id(parent)); |
| 525 | }; |
| 526 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 527 | int blk_find_max_devnum(enum uclass_id uclass_id) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 528 | { |
| 529 | struct udevice *dev; |
| 530 | int max_devnum = -ENODEV; |
| 531 | struct uclass *uc; |
| 532 | int ret; |
| 533 | |
| 534 | ret = uclass_get(UCLASS_BLK, &uc); |
| 535 | if (ret) |
| 536 | return ret; |
| 537 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 538 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 539 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 540 | if (desc->uclass_id == uclass_id && desc->devnum > max_devnum) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 541 | max_devnum = desc->devnum; |
| 542 | } |
| 543 | |
| 544 | return max_devnum; |
| 545 | } |
| 546 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 547 | int blk_next_free_devnum(enum uclass_id uclass_id) |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame] | 548 | { |
| 549 | int ret; |
| 550 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 551 | ret = blk_find_max_devnum(uclass_id); |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame] | 552 | if (ret == -ENODEV) |
| 553 | return 0; |
| 554 | if (ret < 0) |
| 555 | return ret; |
| 556 | |
| 557 | return ret + 1; |
| 558 | } |
| 559 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 560 | static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags) |
| 561 | { |
| 562 | const struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 563 | enum blk_flag_t flags; |
| 564 | |
| 565 | flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED; |
| 566 | |
| 567 | return flags & req_flags ? 0 : 1; |
| 568 | } |
| 569 | |
Simon Glass | 8e61f93 | 2022-02-28 12:08:35 -0700 | [diff] [blame] | 570 | int blk_find_first(enum blk_flag_t flags, struct udevice **devp) |
| 571 | { |
| 572 | int ret; |
| 573 | |
| 574 | for (ret = uclass_find_first_device(UCLASS_BLK, devp); |
| 575 | *devp && !blk_flags_check(*devp, flags); |
| 576 | ret = uclass_find_next_device(devp)) |
| 577 | return 0; |
| 578 | |
| 579 | return -ENODEV; |
| 580 | } |
| 581 | |
| 582 | int blk_find_next(enum blk_flag_t flags, struct udevice **devp) |
| 583 | { |
| 584 | int ret; |
| 585 | |
| 586 | for (ret = uclass_find_next_device(devp); |
| 587 | *devp && !blk_flags_check(*devp, flags); |
| 588 | ret = uclass_find_next_device(devp)) |
| 589 | return 0; |
| 590 | |
| 591 | return -ENODEV; |
| 592 | } |
| 593 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 594 | int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp) |
| 595 | { |
| 596 | int ret; |
| 597 | |
| 598 | for (ret = uclass_first_device_err(UCLASS_BLK, devp); |
| 599 | !ret; |
| 600 | ret = uclass_next_device_err(devp)) { |
| 601 | if (!blk_flags_check(*devp, flags)) |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | return -ENODEV; |
| 606 | } |
| 607 | |
| 608 | int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp) |
| 609 | { |
| 610 | int ret; |
| 611 | |
| 612 | for (ret = uclass_next_device_err(devp); |
| 613 | !ret; |
| 614 | ret = uclass_next_device_err(devp)) { |
| 615 | if (!blk_flags_check(*devp, flags)) |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | return -ENODEV; |
| 620 | } |
| 621 | |
| 622 | int blk_count_devices(enum blk_flag_t flag) |
| 623 | { |
| 624 | struct udevice *dev; |
| 625 | int count = 0; |
| 626 | |
| 627 | blk_foreach_probe(flag, dev) |
| 628 | count++; |
| 629 | |
| 630 | return count; |
| 631 | } |
| 632 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 633 | static int blk_claim_devnum(enum uclass_id uclass_id, int devnum) |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 634 | { |
| 635 | struct udevice *dev; |
| 636 | struct uclass *uc; |
| 637 | int ret; |
| 638 | |
| 639 | ret = uclass_get(UCLASS_BLK, &uc); |
| 640 | if (ret) |
| 641 | return ret; |
| 642 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 643 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 644 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 645 | if (desc->uclass_id == uclass_id && desc->devnum == devnum) { |
| 646 | int next = blk_next_free_devnum(uclass_id); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 647 | |
| 648 | if (next < 0) |
| 649 | return next; |
| 650 | desc->devnum = next; |
| 651 | return 0; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | return -ENOENT; |
| 656 | } |
| 657 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 658 | int blk_create_device(struct udevice *parent, const char *drv_name, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 659 | const char *name, int uclass_id, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 660 | lbaint_t lba, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 661 | { |
| 662 | struct blk_desc *desc; |
| 663 | struct udevice *dev; |
| 664 | int ret; |
| 665 | |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 666 | if (devnum == -1) { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 667 | devnum = blk_next_free_devnum(uclass_id); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 668 | } else { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 669 | ret = blk_claim_devnum(uclass_id, devnum); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 670 | if (ret < 0 && ret != -ENOENT) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 671 | return ret; |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 672 | } |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 673 | if (devnum < 0) |
| 674 | return devnum; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 675 | ret = device_bind_driver(parent, drv_name, name, &dev); |
| 676 | if (ret) |
| 677 | return ret; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 678 | desc = dev_get_uclass_plat(dev); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 679 | desc->uclass_id = uclass_id; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 680 | desc->blksz = blksz; |
Heinrich Schuchardt | 09ba4ea | 2019-10-25 12:15:31 +0200 | [diff] [blame] | 681 | desc->log2blksz = LOG2(desc->blksz); |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 682 | desc->lba = lba; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 683 | desc->part_type = PART_TYPE_UNKNOWN; |
| 684 | desc->bdev = dev; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 685 | desc->devnum = devnum; |
| 686 | *devp = dev; |
| 687 | |
| 688 | return 0; |
| 689 | } |
| 690 | |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 691 | int blk_create_devicef(struct udevice *parent, const char *drv_name, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 692 | const char *name, int uclass_id, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 693 | lbaint_t lba, struct udevice **devp) |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 694 | { |
| 695 | char dev_name[30], *str; |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 696 | int ret; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 697 | |
| 698 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 699 | str = strdup(dev_name); |
| 700 | if (!str) |
| 701 | return -ENOMEM; |
| 702 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 703 | ret = blk_create_device(parent, drv_name, str, uclass_id, devnum, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 704 | blksz, lba, devp); |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 705 | if (ret) { |
| 706 | free(str); |
| 707 | return ret; |
| 708 | } |
| 709 | device_set_name_alloced(*devp); |
| 710 | |
Simon Glass | 4450597 | 2017-07-29 11:34:59 -0600 | [diff] [blame] | 711 | return 0; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 712 | } |
| 713 | |
AKASHI Takahiro | 3e32dbe | 2021-12-10 15:49:29 +0900 | [diff] [blame] | 714 | int blk_probe_or_unbind(struct udevice *dev) |
| 715 | { |
| 716 | int ret; |
| 717 | |
| 718 | ret = device_probe(dev); |
| 719 | if (ret) { |
| 720 | log_debug("probing %s failed\n", dev->name); |
| 721 | device_unbind(dev); |
| 722 | } |
| 723 | |
| 724 | return ret; |
| 725 | } |
| 726 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 727 | int blk_unbind_all(int uclass_id) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 728 | { |
| 729 | struct uclass *uc; |
| 730 | struct udevice *dev, *next; |
| 731 | int ret; |
| 732 | |
| 733 | ret = uclass_get(UCLASS_BLK, &uc); |
| 734 | if (ret) |
| 735 | return ret; |
| 736 | uclass_foreach_dev_safe(dev, next, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 737 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 738 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 739 | if (desc->uclass_id == uclass_id) { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 740 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 741 | if (ret) |
| 742 | return ret; |
| 743 | ret = device_unbind(dev); |
| 744 | if (ret) |
| 745 | return ret; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | return 0; |
| 750 | } |
| 751 | |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 752 | static int blk_post_probe(struct udevice *dev) |
| 753 | { |
Simon Glass | f5ac303 | 2022-08-11 19:34:45 -0600 | [diff] [blame] | 754 | if (CONFIG_IS_ENABLED(PARTITIONS) && blk_enabled()) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 755 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 756 | |
Ovidiu Panait | 76160a0 | 2020-07-24 14:12:21 +0300 | [diff] [blame] | 757 | part_init(desc); |
AKASHI Takahiro | 358e6c7 | 2022-04-19 10:05:10 +0900 | [diff] [blame] | 758 | |
| 759 | if (desc->part_type != PART_TYPE_UNKNOWN && |
| 760 | part_create_block_devices(dev)) |
| 761 | debug("*** creating partitions failed\n"); |
Ovidiu Panait | 76160a0 | 2020-07-24 14:12:21 +0300 | [diff] [blame] | 762 | } |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 767 | UCLASS_DRIVER(blk) = { |
| 768 | .id = UCLASS_BLK, |
| 769 | .name = "blk", |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 770 | .post_probe = blk_post_probe, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 771 | .per_device_plat_auto = sizeof(struct blk_desc), |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 772 | }; |