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