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" }, |
Simon Glass | e57f8d4 | 2022-10-29 19:47:17 -0600 | [diff] [blame] | 29 | { UCLASS_HOST, "host" }, |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 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 | |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 372 | static int _blk_next_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; |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 375 | int ret = 0; |
| 376 | |
| 377 | for (; *devp; uclass_find_next_device(devp)) { |
| 378 | desc = dev_get_uclass_plat(*devp); |
| 379 | if (desc->uclass_id == uclass_id) { |
| 380 | ret = device_probe(*devp); |
| 381 | if (!ret) |
| 382 | return 0; |
| 383 | } |
| 384 | } |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 385 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 386 | if (ret) |
| 387 | return ret; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 388 | |
| 389 | return -ENODEV; |
| 390 | } |
| 391 | |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 392 | int blk_first_device(int uclass_id, struct udevice **devp) |
| 393 | { |
| 394 | uclass_find_first_device(UCLASS_BLK, devp); |
| 395 | |
| 396 | return _blk_next_device(uclass_id, devp); |
| 397 | } |
| 398 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 399 | int blk_next_device(struct udevice **devp) |
| 400 | { |
| 401 | struct blk_desc *desc; |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 402 | int uclass_id; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 403 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 404 | desc = dev_get_uclass_plat(*devp); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 405 | uclass_id = desc->uclass_id; |
Michal Suchanek | bad8ab6 | 2022-09-27 23:23:53 +0200 | [diff] [blame] | 406 | uclass_find_next_device(devp); |
| 407 | |
| 408 | return _blk_next_device(uclass_id, devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 411 | int blk_find_device(int uclass_id, int devnum, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 412 | { |
| 413 | struct uclass *uc; |
| 414 | struct udevice *dev; |
| 415 | int ret; |
| 416 | |
| 417 | ret = uclass_get(UCLASS_BLK, &uc); |
| 418 | if (ret) |
| 419 | return ret; |
| 420 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 421 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 422 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 423 | debug("%s: uclass_id=%d, devnum=%d: %s, %d, %d\n", __func__, |
| 424 | uclass_id, devnum, dev->name, desc->uclass_id, desc->devnum); |
| 425 | if (desc->uclass_id == uclass_id && desc->devnum == devnum) { |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 426 | *devp = dev; |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 427 | return 0; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | return -ENODEV; |
| 432 | } |
| 433 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 434 | int blk_get_device(int uclass_id, int devnum, struct udevice **devp) |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 435 | { |
| 436 | int ret; |
| 437 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 438 | ret = blk_find_device(uclass_id, devnum, devp); |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 439 | if (ret) |
| 440 | return ret; |
| 441 | |
| 442 | return device_probe(*devp); |
| 443 | } |
| 444 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 445 | 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] | 446 | { |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 447 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 448 | const struct blk_ops *ops = blk_get_ops(dev); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 449 | ulong blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 450 | |
| 451 | if (!ops->read) |
| 452 | return -ENOSYS; |
| 453 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 454 | if (blkcache_read(desc->uclass_id, desc->devnum, |
| 455 | start, blkcnt, desc->blksz, buf)) |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 456 | return blkcnt; |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 457 | blks_read = ops->read(dev, start, blkcnt, buf); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 458 | if (blks_read == blkcnt) |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 459 | blkcache_fill(desc->uclass_id, desc->devnum, start, blkcnt, |
| 460 | desc->blksz, buf); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 461 | |
| 462 | return blks_read; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 465 | long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, |
| 466 | const void *buf) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 467 | { |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 468 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 469 | const struct blk_ops *ops = blk_get_ops(dev); |
| 470 | |
| 471 | if (!ops->write) |
| 472 | return -ENOSYS; |
| 473 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 474 | blkcache_invalidate(desc->uclass_id, desc->devnum); |
| 475 | |
| 476 | return ops->write(dev, start, blkcnt, buf); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 477 | } |
| 478 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 479 | long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 480 | { |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 481 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 482 | const struct blk_ops *ops = blk_get_ops(dev); |
| 483 | |
| 484 | if (!ops->erase) |
| 485 | return -ENOSYS; |
| 486 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 487 | blkcache_invalidate(desc->uclass_id, desc->devnum); |
| 488 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 489 | return ops->erase(dev, start, blkcnt); |
| 490 | } |
| 491 | |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 492 | ulong blk_dread(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt, |
| 493 | void *buffer) |
| 494 | { |
| 495 | return blk_read(desc->bdev, start, blkcnt, buffer); |
| 496 | } |
| 497 | |
| 498 | ulong blk_dwrite(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt, |
| 499 | const void *buffer) |
| 500 | { |
| 501 | return blk_write(desc->bdev, start, blkcnt, buffer); |
| 502 | } |
| 503 | |
| 504 | ulong blk_derase(struct blk_desc *desc, lbaint_t start, lbaint_t blkcnt) |
| 505 | { |
| 506 | return blk_erase(desc->bdev, start, blkcnt); |
| 507 | } |
| 508 | |
Simon Glass | c0bcaaf | 2022-10-29 19:47:14 -0600 | [diff] [blame] | 509 | int blk_find_from_parent(struct udevice *parent, struct udevice **devp) |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 510 | { |
| 511 | struct udevice *dev; |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 512 | |
Simon Glass | c0bcaaf | 2022-10-29 19:47:14 -0600 | [diff] [blame] | 513 | if (device_find_first_child_by_uclass(parent, UCLASS_BLK, &dev)) { |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 514 | debug("%s: No block device found for parent '%s'\n", __func__, |
| 515 | parent->name); |
| 516 | return -ENODEV; |
| 517 | } |
Simon Glass | c0bcaaf | 2022-10-29 19:47:14 -0600 | [diff] [blame] | 518 | *devp = dev; |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | int blk_get_from_parent(struct udevice *parent, struct udevice **devp) |
| 524 | { |
| 525 | struct udevice *dev; |
| 526 | int ret; |
| 527 | |
| 528 | ret = blk_find_from_parent(parent, &dev); |
| 529 | if (ret) |
| 530 | return ret; |
Simon Glass | 4f26913 | 2017-05-27 11:37:17 -0600 | [diff] [blame] | 531 | ret = device_probe(dev); |
| 532 | if (ret) |
| 533 | return ret; |
| 534 | *devp = dev; |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
Simon Glass | f3086cf | 2022-04-24 23:31:03 -0600 | [diff] [blame] | 539 | const char *blk_get_devtype(struct udevice *dev) |
| 540 | { |
| 541 | struct udevice *parent = dev_get_parent(dev); |
| 542 | |
| 543 | return uclass_get_name(device_get_uclass_id(parent)); |
| 544 | }; |
| 545 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 546 | int blk_find_max_devnum(enum uclass_id uclass_id) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 547 | { |
| 548 | struct udevice *dev; |
| 549 | int max_devnum = -ENODEV; |
| 550 | struct uclass *uc; |
| 551 | int ret; |
| 552 | |
| 553 | ret = uclass_get(UCLASS_BLK, &uc); |
| 554 | if (ret) |
| 555 | return ret; |
| 556 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 557 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 558 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 559 | if (desc->uclass_id == uclass_id && desc->devnum > max_devnum) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 560 | max_devnum = desc->devnum; |
| 561 | } |
| 562 | |
| 563 | return max_devnum; |
| 564 | } |
| 565 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 566 | int blk_next_free_devnum(enum uclass_id uclass_id) |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame] | 567 | { |
| 568 | int ret; |
| 569 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 570 | ret = blk_find_max_devnum(uclass_id); |
Simon Glass | dbc3861 | 2017-04-23 20:02:06 -0600 | [diff] [blame] | 571 | if (ret == -ENODEV) |
| 572 | return 0; |
| 573 | if (ret < 0) |
| 574 | return ret; |
| 575 | |
| 576 | return ret + 1; |
| 577 | } |
| 578 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 579 | static int blk_flags_check(struct udevice *dev, enum blk_flag_t req_flags) |
| 580 | { |
| 581 | const struct blk_desc *desc = dev_get_uclass_plat(dev); |
| 582 | enum blk_flag_t flags; |
| 583 | |
| 584 | flags = desc->removable ? BLKF_REMOVABLE : BLKF_FIXED; |
| 585 | |
| 586 | return flags & req_flags ? 0 : 1; |
| 587 | } |
| 588 | |
Simon Glass | 8e61f93 | 2022-02-28 12:08:35 -0700 | [diff] [blame] | 589 | int blk_find_first(enum blk_flag_t flags, struct udevice **devp) |
| 590 | { |
| 591 | int ret; |
| 592 | |
| 593 | for (ret = uclass_find_first_device(UCLASS_BLK, devp); |
| 594 | *devp && !blk_flags_check(*devp, flags); |
| 595 | ret = uclass_find_next_device(devp)) |
| 596 | return 0; |
| 597 | |
| 598 | return -ENODEV; |
| 599 | } |
| 600 | |
| 601 | int blk_find_next(enum blk_flag_t flags, struct udevice **devp) |
| 602 | { |
| 603 | int ret; |
| 604 | |
| 605 | for (ret = uclass_find_next_device(devp); |
| 606 | *devp && !blk_flags_check(*devp, flags); |
| 607 | ret = uclass_find_next_device(devp)) |
| 608 | return 0; |
| 609 | |
| 610 | return -ENODEV; |
| 611 | } |
| 612 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 613 | int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp) |
| 614 | { |
Michal Suchanek | ac9e9fc | 2022-10-12 21:58:01 +0200 | [diff] [blame] | 615 | for (uclass_first_device(UCLASS_BLK, devp); |
| 616 | *devp; |
| 617 | uclass_next_device(devp)) { |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 618 | if (!blk_flags_check(*devp, flags)) |
| 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | return -ENODEV; |
| 623 | } |
| 624 | |
| 625 | int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp) |
| 626 | { |
Michal Suchanek | ac9e9fc | 2022-10-12 21:58:01 +0200 | [diff] [blame] | 627 | for (uclass_next_device(devp); |
| 628 | *devp; |
| 629 | uclass_next_device(devp)) { |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 630 | if (!blk_flags_check(*devp, flags)) |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | return -ENODEV; |
| 635 | } |
| 636 | |
| 637 | int blk_count_devices(enum blk_flag_t flag) |
| 638 | { |
| 639 | struct udevice *dev; |
| 640 | int count = 0; |
| 641 | |
| 642 | blk_foreach_probe(flag, dev) |
| 643 | count++; |
| 644 | |
| 645 | return count; |
| 646 | } |
| 647 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 648 | static int blk_claim_devnum(enum uclass_id uclass_id, int devnum) |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 649 | { |
| 650 | struct udevice *dev; |
| 651 | struct uclass *uc; |
| 652 | int ret; |
| 653 | |
| 654 | ret = uclass_get(UCLASS_BLK, &uc); |
| 655 | if (ret) |
| 656 | return ret; |
| 657 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 658 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 659 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 660 | if (desc->uclass_id == uclass_id && desc->devnum == devnum) { |
| 661 | int next = blk_next_free_devnum(uclass_id); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 662 | |
| 663 | if (next < 0) |
| 664 | return next; |
| 665 | desc->devnum = next; |
| 666 | return 0; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | return -ENOENT; |
| 671 | } |
| 672 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 673 | int blk_create_device(struct udevice *parent, const char *drv_name, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 674 | const char *name, int uclass_id, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 675 | lbaint_t lba, struct udevice **devp) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 676 | { |
| 677 | struct blk_desc *desc; |
| 678 | struct udevice *dev; |
| 679 | int ret; |
| 680 | |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 681 | if (devnum == -1) { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 682 | devnum = blk_next_free_devnum(uclass_id); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 683 | } else { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 684 | ret = blk_claim_devnum(uclass_id, devnum); |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 685 | if (ret < 0 && ret != -ENOENT) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 686 | return ret; |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 687 | } |
Simon Glass | e4fef74 | 2017-04-23 20:02:07 -0600 | [diff] [blame] | 688 | if (devnum < 0) |
| 689 | return devnum; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 690 | ret = device_bind_driver(parent, drv_name, name, &dev); |
| 691 | if (ret) |
| 692 | return ret; |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 693 | desc = dev_get_uclass_plat(dev); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 694 | desc->uclass_id = uclass_id; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 695 | desc->blksz = blksz; |
Heinrich Schuchardt | 09ba4ea | 2019-10-25 12:15:31 +0200 | [diff] [blame] | 696 | desc->log2blksz = LOG2(desc->blksz); |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 697 | desc->lba = lba; |
Simon Glass | 77f7fb8 | 2016-05-01 13:52:22 -0600 | [diff] [blame] | 698 | desc->part_type = PART_TYPE_UNKNOWN; |
| 699 | desc->bdev = dev; |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 700 | desc->devnum = devnum; |
| 701 | *devp = dev; |
| 702 | |
| 703 | return 0; |
| 704 | } |
| 705 | |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 706 | int blk_create_devicef(struct udevice *parent, const char *drv_name, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 707 | const char *name, int uclass_id, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 708 | lbaint_t lba, struct udevice **devp) |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 709 | { |
| 710 | char dev_name[30], *str; |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 711 | int ret; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 712 | |
| 713 | snprintf(dev_name, sizeof(dev_name), "%s.%s", parent->name, name); |
| 714 | str = strdup(dev_name); |
| 715 | if (!str) |
| 716 | return -ENOMEM; |
| 717 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 718 | ret = blk_create_device(parent, drv_name, str, uclass_id, devnum, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 719 | blksz, lba, devp); |
Simon Glass | 39e5492 | 2016-05-01 13:52:24 -0600 | [diff] [blame] | 720 | if (ret) { |
| 721 | free(str); |
| 722 | return ret; |
| 723 | } |
| 724 | device_set_name_alloced(*devp); |
| 725 | |
Simon Glass | 4450597 | 2017-07-29 11:34:59 -0600 | [diff] [blame] | 726 | return 0; |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 727 | } |
| 728 | |
AKASHI Takahiro | 3e32dbe | 2021-12-10 15:49:29 +0900 | [diff] [blame] | 729 | int blk_probe_or_unbind(struct udevice *dev) |
| 730 | { |
| 731 | int ret; |
| 732 | |
| 733 | ret = device_probe(dev); |
| 734 | if (ret) { |
| 735 | log_debug("probing %s failed\n", dev->name); |
| 736 | device_unbind(dev); |
| 737 | } |
| 738 | |
| 739 | return ret; |
| 740 | } |
| 741 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 742 | int blk_unbind_all(int uclass_id) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 743 | { |
| 744 | struct uclass *uc; |
| 745 | struct udevice *dev, *next; |
| 746 | int ret; |
| 747 | |
| 748 | ret = uclass_get(UCLASS_BLK, &uc); |
| 749 | if (ret) |
| 750 | return ret; |
| 751 | uclass_foreach_dev_safe(dev, next, uc) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 752 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 753 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 754 | if (desc->uclass_id == uclass_id) { |
Stefan Roese | 80b5bc9 | 2017-03-20 12:51:48 +0100 | [diff] [blame] | 755 | ret = device_remove(dev, DM_REMOVE_NORMAL); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 756 | if (ret) |
| 757 | return ret; |
| 758 | ret = device_unbind(dev); |
| 759 | if (ret) |
| 760 | return ret; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | return 0; |
| 765 | } |
| 766 | |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 767 | static int blk_post_probe(struct udevice *dev) |
| 768 | { |
Simon Glass | f5ac303 | 2022-08-11 19:34:45 -0600 | [diff] [blame] | 769 | if (CONFIG_IS_ENABLED(PARTITIONS) && blk_enabled()) { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 770 | struct blk_desc *desc = dev_get_uclass_plat(dev); |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 771 | |
Ovidiu Panait | 76160a0 | 2020-07-24 14:12:21 +0300 | [diff] [blame] | 772 | part_init(desc); |
AKASHI Takahiro | 358e6c7 | 2022-04-19 10:05:10 +0900 | [diff] [blame] | 773 | |
| 774 | if (desc->part_type != PART_TYPE_UNKNOWN && |
| 775 | part_create_block_devices(dev)) |
| 776 | debug("*** creating partitions failed\n"); |
Ovidiu Panait | 76160a0 | 2020-07-24 14:12:21 +0300 | [diff] [blame] | 777 | } |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 782 | UCLASS_DRIVER(blk) = { |
| 783 | .id = UCLASS_BLK, |
| 784 | .name = "blk", |
Bin Meng | cbc3da8 | 2018-10-15 02:21:07 -0700 | [diff] [blame] | 785 | .post_probe = blk_post_probe, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 786 | .per_device_plat_auto = sizeof(struct blk_desc), |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 787 | }; |