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