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