Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2004 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef BLK_H |
| 8 | #define BLK_H |
| 9 | |
Marek Vasut | 847e24f | 2023-08-14 01:49:59 +0200 | [diff] [blame] | 10 | #include <bouncebuf.h> |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 11 | #include <dm/uclass-id.h> |
Peter Jones | c27e1c1 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 12 | #include <efi.h> |
| 13 | |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 14 | #ifdef CONFIG_SYS_64BIT_LBA |
| 15 | typedef uint64_t lbaint_t; |
| 16 | #define LBAFlength "ll" |
| 17 | #else |
| 18 | typedef ulong lbaint_t; |
| 19 | #define LBAFlength "l" |
| 20 | #endif |
| 21 | #define LBAF "%" LBAFlength "x" |
| 22 | #define LBAFU "%" LBAFlength "u" |
| 23 | |
Bin Meng | 2294ecb | 2023-09-26 16:43:31 +0800 | [diff] [blame] | 24 | #define DEFAULT_BLKSZ 512 |
| 25 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 26 | struct udevice; |
| 27 | |
Simon Glass | f5ac303 | 2022-08-11 19:34:45 -0600 | [diff] [blame] | 28 | static inline bool blk_enabled(void) |
| 29 | { |
Simon Glass | 3bf7d7a | 2022-08-11 19:34:48 -0600 | [diff] [blame] | 30 | return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK); |
Simon Glass | f5ac303 | 2022-08-11 19:34:45 -0600 | [diff] [blame] | 31 | } |
| 32 | |
Bin Meng | cf406d2 | 2017-09-10 05:12:50 -0700 | [diff] [blame] | 33 | #define BLK_VEN_SIZE 40 |
| 34 | #define BLK_PRD_SIZE 20 |
| 35 | #define BLK_REV_SIZE 8 |
| 36 | |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 37 | #define PART_FORMAT_PCAT 0x1 |
| 38 | #define PART_FORMAT_GPT 0x2 |
| 39 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 40 | /* |
Peter Jones | c27e1c1 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 41 | * Identifies the partition table type (ie. MBR vs GPT GUID) signature |
| 42 | */ |
| 43 | enum sig_type { |
| 44 | SIG_TYPE_NONE, |
| 45 | SIG_TYPE_MBR, |
| 46 | SIG_TYPE_GUID, |
| 47 | |
| 48 | SIG_TYPE_COUNT /* Number of signature types */ |
| 49 | }; |
| 50 | |
| 51 | /* |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 52 | * With driver model (CONFIG_BLK) this is uclass platform data, accessible |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 53 | * with dev_get_uclass_plat(dev) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 54 | */ |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 55 | struct blk_desc { |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 56 | /* |
| 57 | * TODO: With driver model we should be able to use the parent |
| 58 | * device's uclass instead. |
| 59 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 60 | enum uclass_id uclass_id; /* type of the interface */ |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 61 | int devnum; /* device number */ |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 62 | unsigned char part_type; /* partition type */ |
| 63 | unsigned char target; /* target SCSI ID */ |
| 64 | unsigned char lun; /* target LUN */ |
| 65 | unsigned char hwpart; /* HW partition, e.g. for eMMC */ |
| 66 | unsigned char type; /* device type */ |
| 67 | unsigned char removable; /* removable device */ |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 68 | /* device can use 48bit addr (ATA/ATAPI v7) */ |
Simon Glass | 6131728 | 2023-04-25 10:54:41 -0600 | [diff] [blame] | 69 | bool lba48; |
Simon Glass | 631db66 | 2023-04-25 10:54:35 -0600 | [diff] [blame] | 70 | unsigned char atapi; /* Use ATAPI protocol */ |
Johan Jonker | 10a986c | 2023-10-18 16:01:10 +0200 | [diff] [blame] | 71 | unsigned char bb; /* Use bounce buffer */ |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 72 | lbaint_t lba; /* number of blocks */ |
| 73 | unsigned long blksz; /* block size */ |
| 74 | int log2blksz; /* for convenience: log2(blksz) */ |
Bin Meng | cf406d2 | 2017-09-10 05:12:50 -0700 | [diff] [blame] | 75 | char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */ |
| 76 | char product[BLK_PRD_SIZE + 1]; /* device product number */ |
| 77 | char revision[BLK_REV_SIZE + 1]; /* firmware revision */ |
Peter Jones | c27e1c1 | 2017-09-13 18:05:25 -0400 | [diff] [blame] | 78 | enum sig_type sig_type; /* Partition table signature type */ |
| 79 | union { |
| 80 | uint32_t mbr_sig; /* MBR integer signature */ |
| 81 | efi_guid_t guid_sig; /* GPT GUID Signature */ |
| 82 | }; |
Simon Glass | 5f4bd8c | 2017-07-04 13:31:19 -0600 | [diff] [blame] | 83 | #if CONFIG_IS_ENABLED(BLK) |
Simon Glass | 8f5f722 | 2016-05-01 13:52:33 -0600 | [diff] [blame] | 84 | /* |
| 85 | * For now we have a few functions which take struct blk_desc as a |
| 86 | * parameter. This field allows them to look up the associated |
| 87 | * device. Once these functions are removed we can drop this field. |
| 88 | */ |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 89 | struct udevice *bdev; |
| 90 | #else |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 91 | unsigned long (*block_read)(struct blk_desc *block_dev, |
| 92 | lbaint_t start, |
| 93 | lbaint_t blkcnt, |
| 94 | void *buffer); |
| 95 | unsigned long (*block_write)(struct blk_desc *block_dev, |
| 96 | lbaint_t start, |
| 97 | lbaint_t blkcnt, |
| 98 | const void *buffer); |
| 99 | unsigned long (*block_erase)(struct blk_desc *block_dev, |
| 100 | lbaint_t start, |
| 101 | lbaint_t blkcnt); |
| 102 | void *priv; /* driver private struct pointer */ |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 103 | #endif |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz)) |
| 107 | #define PAD_TO_BLOCKSIZE(size, blk_desc) \ |
| 108 | (PAD_SIZE(size, blk_desc->blksz)) |
| 109 | |
Adam Ford | d693fb9 | 2018-06-11 17:17:48 -0500 | [diff] [blame] | 110 | #if CONFIG_IS_ENABLED(BLOCK_CACHE) |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 111 | /** |
| 112 | * blkcache_read() - attempt to read a set of blocks from cache |
| 113 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 114 | * @param iftype - uclass_id_x for type of device |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 115 | * @param dev - device index of particular type |
| 116 | * @param start - starting block number |
| 117 | * @param blkcnt - number of blocks to read |
| 118 | * @param blksz - size in bytes of each block |
Mattijs Korpershoek | 3cc71a0 | 2022-10-17 09:35:04 +0200 | [diff] [blame] | 119 | * @param buffer - buffer to contain cached data |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 120 | * |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 121 | * Return: - 1 if block returned from cache, 0 otherwise. |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 122 | */ |
Eric Nelson | f201f23 | 2016-04-02 07:37:14 -0700 | [diff] [blame] | 123 | int blkcache_read(int iftype, int dev, |
| 124 | lbaint_t start, lbaint_t blkcnt, |
| 125 | unsigned long blksz, void *buffer); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * blkcache_fill() - make data read from a block device available |
| 129 | * to the block cache |
| 130 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 131 | * @param iftype - uclass_id_x for type of device |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 132 | * @param dev - device index of particular type |
| 133 | * @param start - starting block number |
| 134 | * @param blkcnt - number of blocks available |
| 135 | * @param blksz - size in bytes of each block |
Mattijs Korpershoek | 3cc71a0 | 2022-10-17 09:35:04 +0200 | [diff] [blame] | 136 | * @param buffer - buffer containing data to cache |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 137 | * |
| 138 | */ |
Eric Nelson | f201f23 | 2016-04-02 07:37:14 -0700 | [diff] [blame] | 139 | void blkcache_fill(int iftype, int dev, |
| 140 | lbaint_t start, lbaint_t blkcnt, |
| 141 | unsigned long blksz, void const *buffer); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * blkcache_invalidate() - discard the cache for a set of blocks |
| 145 | * because of a write or device (re)initialization. |
| 146 | * |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 147 | * @iftype - UCLASS_ID_ for type of device, or -1 for any |
| 148 | * @dev - device index of particular type, if @iftype is not -1 |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 149 | */ |
Eric Nelson | f201f23 | 2016-04-02 07:37:14 -0700 | [diff] [blame] | 150 | void blkcache_invalidate(int iftype, int dev); |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * blkcache_configure() - configure block cache |
| 154 | * |
| 155 | * @param blocks - maximum blocks per entry |
| 156 | * @param entries - maximum entries in cache |
| 157 | */ |
| 158 | void blkcache_configure(unsigned blocks, unsigned entries); |
| 159 | |
| 160 | /* |
| 161 | * statistics of the block cache |
| 162 | */ |
| 163 | struct block_cache_stats { |
| 164 | unsigned hits; |
| 165 | unsigned misses; |
| 166 | unsigned entries; /* current entry count */ |
| 167 | unsigned max_blocks_per_entry; |
| 168 | unsigned max_entries; |
| 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * get_blkcache_stats() - return statistics and reset |
| 173 | * |
| 174 | * @param stats - statistics are copied here |
| 175 | */ |
| 176 | void blkcache_stats(struct block_cache_stats *stats); |
| 177 | |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 178 | /** blkcache_free() - free all memory allocated to the block cache */ |
| 179 | void blkcache_free(void); |
| 180 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 181 | #else |
| 182 | |
Eric Nelson | f201f23 | 2016-04-02 07:37:14 -0700 | [diff] [blame] | 183 | static inline int blkcache_read(int iftype, int dev, |
| 184 | lbaint_t start, lbaint_t blkcnt, |
| 185 | unsigned long blksz, void *buffer) |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 186 | { |
| 187 | return 0; |
| 188 | } |
| 189 | |
Eric Nelson | f201f23 | 2016-04-02 07:37:14 -0700 | [diff] [blame] | 190 | static inline void blkcache_fill(int iftype, int dev, |
| 191 | lbaint_t start, lbaint_t blkcnt, |
| 192 | unsigned long blksz, void const *buffer) {} |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 193 | |
Eric Nelson | f201f23 | 2016-04-02 07:37:14 -0700 | [diff] [blame] | 194 | static inline void blkcache_invalidate(int iftype, int dev) {} |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 195 | |
Simon Glass | 76c6269 | 2022-10-29 19:47:08 -0600 | [diff] [blame] | 196 | static inline void blkcache_free(void) {} |
| 197 | |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 198 | #endif |
| 199 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 200 | struct udevice; |
| 201 | |
| 202 | /* Operations on block devices */ |
| 203 | struct blk_ops { |
| 204 | /** |
| 205 | * read() - read from a block device |
| 206 | * |
| 207 | * @dev: Device to read from |
| 208 | * @start: Start block number to read (0=first) |
| 209 | * @blkcnt: Number of blocks to read |
| 210 | * @buffer: Destination buffer for data read |
| 211 | * @return number of blocks read, or -ve error number (see the |
| 212 | * IS_ERR_VALUE() macro |
| 213 | */ |
| 214 | unsigned long (*read)(struct udevice *dev, lbaint_t start, |
| 215 | lbaint_t blkcnt, void *buffer); |
| 216 | |
| 217 | /** |
| 218 | * write() - write to a block device |
| 219 | * |
| 220 | * @dev: Device to write to |
| 221 | * @start: Start block number to write (0=first) |
| 222 | * @blkcnt: Number of blocks to write |
| 223 | * @buffer: Source buffer for data to write |
| 224 | * @return number of blocks written, or -ve error number (see the |
| 225 | * IS_ERR_VALUE() macro |
| 226 | */ |
| 227 | unsigned long (*write)(struct udevice *dev, lbaint_t start, |
| 228 | lbaint_t blkcnt, const void *buffer); |
| 229 | |
| 230 | /** |
| 231 | * erase() - erase a section of a block device |
| 232 | * |
| 233 | * @dev: Device to (partially) erase |
| 234 | * @start: Start block number to erase (0=first) |
| 235 | * @blkcnt: Number of blocks to erase |
| 236 | * @return number of blocks erased, or -ve error number (see the |
| 237 | * IS_ERR_VALUE() macro |
| 238 | */ |
| 239 | unsigned long (*erase)(struct udevice *dev, lbaint_t start, |
| 240 | lbaint_t blkcnt); |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * select_hwpart() - select a particular hardware partition |
| 244 | * |
| 245 | * Some devices (e.g. MMC) can support partitioning at the hardware |
| 246 | * level. This is quite separate from the normal idea of |
| 247 | * software-based partitions. MMC hardware partitions must be |
| 248 | * explicitly selected. Once selected only the region of the device |
| 249 | * covered by that partition is accessible. |
| 250 | * |
| 251 | * The MMC standard provides for two boot partitions (numbered 1 and 2), |
| 252 | * rpmb (3), and up to 4 addition general-purpose partitions (4-7). |
| 253 | * |
Mattijs Korpershoek | 3cc71a0 | 2022-10-17 09:35:04 +0200 | [diff] [blame] | 254 | * @dev: Block device to update |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 255 | * @hwpart: Hardware partition number to select. 0 means the raw |
| 256 | * device, 1 is the first partition, 2 is the second, etc. |
| 257 | * @return 0 if OK, -ve on error |
| 258 | */ |
| 259 | int (*select_hwpart)(struct udevice *dev, int hwpart); |
Marek Vasut | 847e24f | 2023-08-14 01:49:59 +0200 | [diff] [blame] | 260 | |
| 261 | #if IS_ENABLED(CONFIG_BOUNCE_BUFFER) |
| 262 | /** |
| 263 | * buffer_aligned() - test memory alignment of block operation buffer |
| 264 | * |
| 265 | * Some devices have limited DMA capabilities and require that the |
| 266 | * buffers passed to them fit specific properties. This optional |
| 267 | * callback can be used to indicate whether a buffer alignment is |
| 268 | * suitable for the device DMA or not, and trigger use of generic |
| 269 | * bounce buffer implementation to help use of unsuitable buffers |
| 270 | * at the expense of performance degradation. |
| 271 | * |
| 272 | * @dev: Block device associated with the request |
| 273 | * @state: Bounce buffer state |
| 274 | * @return 1 if OK, 0 if unaligned |
| 275 | */ |
| 276 | int (*buffer_aligned)(struct udevice *dev, struct bounce_buffer *state); |
| 277 | #endif /* CONFIG_BOUNCE_BUFFER */ |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 278 | }; |
| 279 | |
Simon Glass | b9f3406 | 2024-09-01 16:27:27 -0600 | [diff] [blame] | 280 | #if CONFIG_IS_ENABLED(BLK) |
| 281 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 282 | /* |
| 283 | * These functions should take struct udevice instead of struct blk_desc, |
| 284 | * but this is convenient for migration to driver model. Add a 'd' prefix |
| 285 | * to the function operations, so that blk_read(), etc. can be reserved for |
| 286 | * functions with the correct arguments. |
| 287 | */ |
| 288 | unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start, |
| 289 | lbaint_t blkcnt, void *buffer); |
| 290 | unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start, |
| 291 | lbaint_t blkcnt, const void *buffer); |
| 292 | unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, |
| 293 | lbaint_t blkcnt); |
| 294 | |
Simon Glass | b9f3406 | 2024-09-01 16:27:27 -0600 | [diff] [blame] | 295 | #endif /* BLK */ |
| 296 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 297 | /** |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 298 | * blk_read() - Read from a block device |
| 299 | * |
| 300 | * @dev: Device to read from |
| 301 | * @start: Start block for the read |
| 302 | * @blkcnt: Number of blocks to read |
| 303 | * @buf: Place to put the data |
| 304 | * @return number of blocks read (which may be less than @blkcnt), |
| 305 | * or -ve on error. This never returns 0 unless @blkcnt is 0 |
| 306 | */ |
| 307 | long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, |
| 308 | void *buffer); |
| 309 | |
| 310 | /** |
| 311 | * blk_write() - Write to a block device |
| 312 | * |
| 313 | * @dev: Device to write to |
| 314 | * @start: Start block for the write |
| 315 | * @blkcnt: Number of blocks to write |
| 316 | * @buf: Data to write |
| 317 | * @return number of blocks written (which may be less than @blkcnt), |
| 318 | * or -ve on error. This never returns 0 unless @blkcnt is 0 |
| 319 | */ |
| 320 | long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, |
| 321 | const void *buffer); |
| 322 | |
| 323 | /** |
| 324 | * blk_erase() - Erase part of a block device |
| 325 | * |
| 326 | * @dev: Device to erase |
| 327 | * @start: Start block for the erase |
| 328 | * @blkcnt: Number of blocks to erase |
| 329 | * @return number of blocks erased (which may be less than @blkcnt), |
| 330 | * or -ve on error. This never returns 0 unless @blkcnt is 0 |
| 331 | */ |
| 332 | long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt); |
| 333 | |
| 334 | /** |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 335 | * blk_find_device() - Find a block device |
| 336 | * |
| 337 | * This function does not activate the device. The device will be returned |
| 338 | * whether or not it is activated. |
| 339 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 340 | * @uclass_id: Interface type (enum uclass_id_t) |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 341 | * @devnum: Device number (specific to each interface type) |
| 342 | * @devp: the device, if found |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 343 | * Return: 0 if found, -ENODEV if no device found, or other -ve error value |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 344 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 345 | int blk_find_device(int uclass_id, int devnum, struct udevice **devp); |
Simon Glass | d5d4c10 | 2017-04-23 20:02:05 -0600 | [diff] [blame] | 346 | |
| 347 | /** |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 348 | * blk_get_device() - Find and probe a block device ready for use |
| 349 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 350 | * @uclass_id: Interface type (enum uclass_id_t) |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 351 | * @devnum: Device number (specific to each interface type) |
| 352 | * @devp: the device, if found |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 353 | * Return: 0 if found, -ENODEV if no device found, or other -ve error value |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 354 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 355 | int blk_get_device(int uclass_id, int devnum, struct udevice **devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 356 | |
| 357 | /** |
| 358 | * blk_first_device() - Find the first device for a given interface |
| 359 | * |
| 360 | * The device is probed ready for use |
| 361 | * |
| 362 | * @devnum: Device number (specific to each interface type) |
| 363 | * @devp: the device, if found |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 364 | * Return: 0 if found, -ENODEV if no device, or other -ve error value |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 365 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 366 | int blk_first_device(int uclass_id, struct udevice **devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 367 | |
| 368 | /** |
| 369 | * blk_next_device() - Find the next device for a given interface |
| 370 | * |
| 371 | * This can be called repeatedly after blk_first_device() to iterate through |
| 372 | * all devices of the given interface type. |
| 373 | * |
| 374 | * The device is probed ready for use |
| 375 | * |
| 376 | * @devp: On entry, the previous device returned. On exit, the next |
| 377 | * device, if found |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 378 | * Return: 0 if found, -ENODEV if no device, or other -ve error value |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 379 | */ |
| 380 | int blk_next_device(struct udevice **devp); |
| 381 | |
| 382 | /** |
| 383 | * blk_create_device() - Create a new block device |
| 384 | * |
| 385 | * @parent: Parent of the new device |
| 386 | * @drv_name: Driver name to use for the block device |
| 387 | * @name: Name for the device |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 388 | * @uclass_id: Interface type (enum uclass_id_t) |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 389 | * @devnum: Device number, specific to the interface type, or -1 to |
| 390 | * allocate the next available number |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 391 | * @blksz: Block size of the device in bytes (typically 512) |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 392 | * @lba: Total number of blocks of the device |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 393 | * @devp: the new device (which has not been probed) |
| 394 | */ |
| 395 | int blk_create_device(struct udevice *parent, const char *drv_name, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 396 | const char *name, int uclass_id, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 397 | lbaint_t lba, struct udevice **devp); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 398 | |
| 399 | /** |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 400 | * blk_create_devicef() - Create a new named block device |
| 401 | * |
| 402 | * @parent: Parent of the new device |
| 403 | * @drv_name: Driver name to use for the block device |
| 404 | * @name: Name for the device (parent name is prepended) |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 405 | * @uclass_id: Interface type (enum uclass_id_t) |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 406 | * @devnum: Device number, specific to the interface type, or -1 to |
| 407 | * allocate the next available number |
| 408 | * @blksz: Block size of the device in bytes (typically 512) |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 409 | * @lba: Total number of blocks of the device |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 410 | * @devp: the new device (which has not been probed) |
| 411 | */ |
| 412 | int blk_create_devicef(struct udevice *parent, const char *drv_name, |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 413 | const char *name, int uclass_id, int devnum, int blksz, |
Jean-Jacques Hiblot | 99b324a | 2017-06-09 16:45:18 +0200 | [diff] [blame] | 414 | lbaint_t lba, struct udevice **devp); |
Simon Glass | 966b695 | 2016-05-01 11:36:29 -0600 | [diff] [blame] | 415 | |
| 416 | /** |
AKASHI Takahiro | 3e32dbe | 2021-12-10 15:49:29 +0900 | [diff] [blame] | 417 | * blk_probe_or_unbind() - Try to probe |
| 418 | * |
| 419 | * Try to probe the device, primarily for enumerating partitions. |
| 420 | * If it fails, the device itself is unbound since it means that it won't |
| 421 | * work any more. |
| 422 | * |
| 423 | * @dev: The device to probe |
| 424 | * Return: 0 if OK, -ve on error |
| 425 | */ |
| 426 | int blk_probe_or_unbind(struct udevice *dev); |
| 427 | |
| 428 | /** |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 429 | * blk_unbind_all() - Unbind all device of the given interface type |
| 430 | * |
| 431 | * The devices are removed and then unbound. |
| 432 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 433 | * @uclass_id: Interface type to unbind |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 434 | * Return: 0 if OK, -ve on error |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 435 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 436 | int blk_unbind_all(int uclass_id); |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 437 | |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 438 | /** |
| 439 | * blk_find_max_devnum() - find the maximum device number for an interface type |
| 440 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 441 | * Finds the last allocated device number for an interface type @uclass_id. The |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 442 | * next number is safe to use for a newly allocated device. |
| 443 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 444 | * @uclass_id: Interface type to scan |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 445 | * Return: maximum device number found, or -ENODEV if none, or other -ve on |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 446 | * error |
| 447 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 448 | int blk_find_max_devnum(enum uclass_id uclass_id); |
Simon Glass | d089ba3 | 2016-05-01 11:36:28 -0600 | [diff] [blame] | 449 | |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 450 | /** |
Bin Meng | fd5eda7 | 2018-10-15 02:21:09 -0700 | [diff] [blame] | 451 | * blk_next_free_devnum() - get the next device number for an interface type |
| 452 | * |
| 453 | * Finds the next number that is safe to use for a newly allocated device for |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 454 | * an interface type @uclass_id. |
Bin Meng | fd5eda7 | 2018-10-15 02:21:09 -0700 | [diff] [blame] | 455 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 456 | * @uclass_id: Interface type to scan |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 457 | * Return: next device number safe to use, or -ve on error |
Bin Meng | fd5eda7 | 2018-10-15 02:21:09 -0700 | [diff] [blame] | 458 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 459 | int blk_next_free_devnum(enum uclass_id uclass_id); |
Bin Meng | fd5eda7 | 2018-10-15 02:21:09 -0700 | [diff] [blame] | 460 | |
| 461 | /** |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 462 | * blk_select_hwpart() - select a hardware partition |
| 463 | * |
| 464 | * Select a hardware partition if the device supports it (typically MMC does) |
| 465 | * |
| 466 | * @dev: Device to update |
| 467 | * @hwpart: Partition number to select |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 468 | * Return: 0 if OK, -ve on error |
Simon Glass | 13c2c29 | 2016-05-01 13:52:30 -0600 | [diff] [blame] | 469 | */ |
| 470 | int blk_select_hwpart(struct udevice *dev, int hwpart); |
| 471 | |
Tom Rini | 7c41d14 | 2017-06-10 10:01:05 -0400 | [diff] [blame] | 472 | /** |
Simon Glass | c0bcaaf | 2022-10-29 19:47:14 -0600 | [diff] [blame] | 473 | * blk_find_from_parent() - find a block device by looking up its parent |
| 474 | * |
| 475 | * All block devices have a parent 'media' device which provides the block |
| 476 | * driver for the block device, ensuring that access to the underlying medium |
| 477 | * is available. |
| 478 | * |
| 479 | * The block device is not activated by this function. See |
| 480 | * blk_get_from_parent() for that. |
| 481 | * |
| 482 | * @parent: Media device |
| 483 | * @devp: Returns the associated block device, if any |
| 484 | * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no |
| 485 | * UCLASS_BLK child |
| 486 | */ |
| 487 | int blk_find_from_parent(struct udevice *parent, struct udevice **devp); |
| 488 | |
| 489 | /** |
Tom Rini | 7c41d14 | 2017-06-10 10:01:05 -0400 | [diff] [blame] | 490 | * blk_get_from_parent() - obtain a block device by looking up its parent |
| 491 | * |
Simon Glass | c0bcaaf | 2022-10-29 19:47:14 -0600 | [diff] [blame] | 492 | * All block devices have a parent 'media' device which provides the block |
| 493 | * driver for the block device, ensuring that access to the underlying medium |
| 494 | * is available. |
| 495 | * |
| 496 | * The block device is probed and ready for use. |
| 497 | * |
| 498 | * @parent: Media device |
| 499 | * @devp: Returns the associated block device, if any |
| 500 | * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no |
| 501 | * UCLASS_BLK child |
Tom Rini | 7c41d14 | 2017-06-10 10:01:05 -0400 | [diff] [blame] | 502 | */ |
| 503 | int blk_get_from_parent(struct udevice *parent, struct udevice **devp); |
| 504 | |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 505 | /** |
Simon Glass | f3086cf | 2022-04-24 23:31:03 -0600 | [diff] [blame] | 506 | * blk_get_devtype() - Get the device type of a block device |
| 507 | * |
| 508 | * @dev: Block device to check |
| 509 | * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc" |
| 510 | */ |
| 511 | const char *blk_get_devtype(struct udevice *dev); |
| 512 | |
| 513 | /** |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 514 | * blk_get_by_device() - Get the block device descriptor for the given device |
Simon Glass | 1886100 | 2022-10-20 18:22:54 -0600 | [diff] [blame] | 515 | * @dev: Instance of a storage device (the parent of the block device) |
Tien Fong Chee | 1037852 | 2018-07-06 16:26:36 +0800 | [diff] [blame] | 516 | * |
| 517 | * Return: With block device descriptor on success , NULL if there is no such |
| 518 | * block device. |
| 519 | */ |
| 520 | struct blk_desc *blk_get_by_device(struct udevice *dev); |
| 521 | |
Bin Meng | 9efc245 | 2023-09-26 16:43:41 +0800 | [diff] [blame] | 522 | /** |
| 523 | * blk_get_desc() - Get the block device descriptor for the given device number |
| 524 | * |
| 525 | * @uclass_id: Interface type |
| 526 | * @devnum: Device number (0 = first) |
| 527 | * @descp: Returns block device descriptor on success |
| 528 | * Return: 0 on success, -ENODEV if there is no such device and no device |
| 529 | * with a higher device number, -ENOENT if there is no such device but there |
| 530 | * is one with a higher number, or other -ve on other error. |
| 531 | */ |
| 532 | int blk_get_desc(enum uclass_id uclass_id, int devnum, struct blk_desc **descp); |
| 533 | |
Simon Glass | b9f3406 | 2024-09-01 16:27:27 -0600 | [diff] [blame] | 534 | #if !CONFIG_IS_ENABLED(BLK) |
| 535 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 536 | #include <errno.h> |
Simon Glass | b9f3406 | 2024-09-01 16:27:27 -0600 | [diff] [blame] | 537 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 538 | /* |
| 539 | * These functions should take struct udevice instead of struct blk_desc, |
| 540 | * but this is convenient for migration to driver model. Add a 'd' prefix |
| 541 | * to the function operations, so that blk_read(), etc. can be reserved for |
| 542 | * functions with the correct arguments. |
| 543 | */ |
| 544 | static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start, |
| 545 | lbaint_t blkcnt, void *buffer) |
| 546 | { |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 547 | ulong blks_read; |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 548 | if (blkcache_read(block_dev->uclass_id, block_dev->devnum, |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 549 | start, blkcnt, block_dev->blksz, buffer)) |
| 550 | return blkcnt; |
| 551 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 552 | /* |
| 553 | * We could check if block_read is NULL and return -ENOSYS. But this |
| 554 | * bloats the code slightly (cause some board to fail to build), and |
| 555 | * it would be an error to try an operation that does not exist. |
| 556 | */ |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 557 | blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer); |
| 558 | if (blks_read == blkcnt) |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 559 | blkcache_fill(block_dev->uclass_id, block_dev->devnum, |
Eric Nelson | faf4f05 | 2016-03-28 10:05:44 -0700 | [diff] [blame] | 560 | start, blkcnt, block_dev->blksz, buffer); |
| 561 | |
| 562 | return blks_read; |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start, |
| 566 | lbaint_t blkcnt, const void *buffer) |
| 567 | { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 568 | blkcache_invalidate(block_dev->uclass_id, block_dev->devnum); |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 569 | return block_dev->block_write(block_dev, start, blkcnt, buffer); |
| 570 | } |
| 571 | |
| 572 | static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start, |
| 573 | lbaint_t blkcnt) |
| 574 | { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 575 | blkcache_invalidate(block_dev->uclass_id, block_dev->devnum); |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 576 | return block_dev->block_erase(block_dev, start, blkcnt); |
| 577 | } |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 578 | |
| 579 | /** |
| 580 | * struct blk_driver - Driver for block interface types |
| 581 | * |
| 582 | * This provides access to the block devices for each interface type. One |
| 583 | * driver should be provided using U_BOOT_LEGACY_BLK() for each interface |
| 584 | * type that is to be supported. |
| 585 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 586 | * @uclass_idname: Interface type name |
| 587 | * @uclass_id: Interface type |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 588 | * @max_devs: Maximum number of devices supported |
| 589 | * @desc: Pointer to list of devices for this interface type, |
| 590 | * or NULL to use @get_dev() instead |
| 591 | */ |
| 592 | struct blk_driver { |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 593 | const char *uclass_idname; |
| 594 | enum uclass_id uclass_id; |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 595 | int max_devs; |
| 596 | struct blk_desc *desc; |
| 597 | /** |
| 598 | * get_dev() - get a pointer to a block device given its number |
| 599 | * |
| 600 | * Each interface allocates its own devices and typically |
| 601 | * struct blk_desc is contained with the interface's data structure. |
| 602 | * There is no global numbering for block devices. This method allows |
| 603 | * the device for an interface type to be obtained when @desc is NULL. |
| 604 | * |
| 605 | * @devnum: Device number (0 for first device on that interface, |
| 606 | * 1 for second, etc. |
| 607 | * @descp: Returns pointer to the block device on success |
| 608 | * @return 0 if OK, -ve on error |
| 609 | */ |
| 610 | int (*get_dev)(int devnum, struct blk_desc **descp); |
| 611 | |
| 612 | /** |
| 613 | * select_hwpart() - Select a hardware partition |
| 614 | * |
| 615 | * Some devices (e.g. MMC) can support partitioning at the hardware |
| 616 | * level. This is quite separate from the normal idea of |
| 617 | * software-based partitions. MMC hardware partitions must be |
| 618 | * explicitly selected. Once selected only the region of the device |
| 619 | * covered by that partition is accessible. |
| 620 | * |
| 621 | * The MMC standard provides for two boot partitions (numbered 1 and 2), |
| 622 | * rpmb (3), and up to 4 addition general-purpose partitions (4-7). |
| 623 | * Partition 0 is the main user-data partition. |
| 624 | * |
| 625 | * @desc: Block device descriptor |
| 626 | * @hwpart: Hardware partition number to select. 0 means the main |
| 627 | * user-data partition, 1 is the first partition, 2 is |
| 628 | * the second, etc. |
| 629 | * @return 0 if OK, other value for an error |
| 630 | */ |
| 631 | int (*select_hwpart)(struct blk_desc *desc, int hwpart); |
| 632 | }; |
| 633 | |
| 634 | /* |
| 635 | * Declare a new U-Boot legacy block driver. New drivers should use driver |
| 636 | * model (UCLASS_BLK). |
| 637 | */ |
| 638 | #define U_BOOT_LEGACY_BLK(__name) \ |
| 639 | ll_entry_declare(struct blk_driver, __name, blk_driver) |
| 640 | |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 641 | struct blk_driver *blk_driver_lookup_type(int uclass_id); |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 642 | |
Simon Glass | cceee55 | 2016-02-29 15:25:55 -0700 | [diff] [blame] | 643 | #endif /* !CONFIG_BLK */ |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 644 | |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 645 | /** |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 646 | * blk_get_devnum_by_uclass_idname() - Get a block device by type and number |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 647 | * |
| 648 | * This looks through the available block devices of the given type, returning |
| 649 | * the one with the given @devnum. |
| 650 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 651 | * @uclass_id: Block device type |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 652 | * @devnum: Device number |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 653 | * Return: point to block device descriptor, or NULL if not found |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 654 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 655 | struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum); |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 656 | |
| 657 | /** |
Simon Glass | 18efdaf | 2024-08-22 07:55:03 -0600 | [diff] [blame] | 658 | * blk_get_devnum_by_uclass_idname() - Get block device by type name and number |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 659 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 660 | * This looks up the block device type based on @uclass_idname, then calls |
| 661 | * blk_get_devnum_by_uclass_id(). |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 662 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 663 | * @uclass_idname: Block device type name |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 664 | * @devnum: Device number |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 665 | * Return: point to block device descriptor, or NULL if not found |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 666 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 667 | struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname, |
Simon Glass | 18efdaf | 2024-08-22 07:55:03 -0600 | [diff] [blame] | 668 | int devnum); |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 669 | |
| 670 | /** |
| 671 | * blk_dselect_hwpart() - select a hardware partition |
| 672 | * |
| 673 | * This selects a hardware partition (such as is supported by MMC). The block |
| 674 | * device size may change as this effectively points the block device to a |
| 675 | * partition at the hardware level. See the select_hwpart() method above. |
| 676 | * |
| 677 | * @desc: Block device descriptor for the device to select |
| 678 | * @hwpart: Partition number to select |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 679 | * Return: 0 if OK, -ve on error |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 680 | */ |
| 681 | int blk_dselect_hwpart(struct blk_desc *desc, int hwpart); |
| 682 | |
| 683 | /** |
| 684 | * blk_list_part() - list the partitions for block devices of a given type |
| 685 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 686 | * This looks up the partition type for each block device of type @uclass_id, |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 687 | * then displays a list of partitions. |
| 688 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 689 | * @uclass_id: Block device type |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 690 | * Return: 0 if OK, -ENODEV if there is none of that type |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 691 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 692 | int blk_list_part(enum uclass_id uclass_id); |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 693 | |
| 694 | /** |
| 695 | * blk_list_devices() - list the block devices of a given type |
| 696 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 697 | * This lists each block device of the type @uclass_id, showing the capacity |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 698 | * as well as type-specific information. |
| 699 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 700 | * @uclass_id: Block device type |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 701 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 702 | void blk_list_devices(enum uclass_id uclass_id); |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 703 | |
| 704 | /** |
| 705 | * blk_show_device() - show information about a given block device |
| 706 | * |
| 707 | * This shows the block device capacity as well as type-specific information. |
| 708 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 709 | * @uclass_id: Block device type |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 710 | * @devnum: Device number |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 711 | * Return: 0 if OK, -ENODEV for invalid device number |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 712 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 713 | int blk_show_device(enum uclass_id uclass_id, int devnum); |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 714 | |
| 715 | /** |
| 716 | * blk_print_device_num() - show information about a given block device |
| 717 | * |
| 718 | * This is similar to blk_show_device() but returns an error if the block |
| 719 | * device type is unknown. |
| 720 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 721 | * @uclass_id: Block device type |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 722 | * @devnum: Device number |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 723 | * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 724 | * device is not connected |
| 725 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 726 | int blk_print_device_num(enum uclass_id uclass_id, int devnum); |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 727 | |
| 728 | /** |
| 729 | * blk_print_part_devnum() - print the partition information for a device |
| 730 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 731 | * @uclass_id: Block device type |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 732 | * @devnum: Device number |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 733 | * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 734 | * the interface type is not supported, other -ve on other error |
| 735 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 736 | int blk_print_part_devnum(enum uclass_id uclass_id, int devnum); |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 737 | |
| 738 | /** |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 739 | * blk_select_hwpart_devnum() - select a hardware partition |
| 740 | * |
| 741 | * This is similar to blk_dselect_hwpart() but it looks up the interface and |
| 742 | * device number. |
| 743 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 744 | * @uclass_id: Block device type |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 745 | * @devnum: Device number |
| 746 | * @hwpart: Partition number to select |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 747 | * Return: 0 if OK, -ve on error |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 748 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 749 | int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart); |
Simon Glass | 3bf2ab9 | 2016-05-01 11:36:03 -0600 | [diff] [blame] | 750 | |
Simon Glass | 85af5a4 | 2017-07-29 11:34:53 -0600 | [diff] [blame] | 751 | /** |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 752 | * blk_get_uclass_name() - Get the name of an interface type |
Simon Glass | 85af5a4 | 2017-07-29 11:34:53 -0600 | [diff] [blame] | 753 | * |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 754 | * @uclass_id: Interface type to check |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 755 | * Return: name of interface, or NULL if none |
Simon Glass | 85af5a4 | 2017-07-29 11:34:53 -0600 | [diff] [blame] | 756 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 757 | const char *blk_get_uclass_name(enum uclass_id uclass_id); |
Simon Glass | 85af5a4 | 2017-07-29 11:34:53 -0600 | [diff] [blame] | 758 | |
Simon Glass | bf2e795 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 759 | /** |
| 760 | * blk_common_cmd() - handle common commands with block devices |
| 761 | * |
| 762 | * @args: Number of arguments to the command (argv[0] is the command itself) |
| 763 | * @argv: Command arguments |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 764 | * @uclass_id: Interface type |
Simon Glass | bf2e795 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 765 | * @cur_devnump: Current device number for this interface type |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 766 | * Return: 0 if OK, CMD_RET_ERROR on error |
Simon Glass | bf2e795 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 767 | */ |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 768 | int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id, |
Simon Glass | bf2e795 | 2017-07-29 11:34:54 -0600 | [diff] [blame] | 769 | int *cur_devnump); |
| 770 | |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 771 | enum blk_flag_t { |
| 772 | BLKF_FIXED = 1 << 0, |
| 773 | BLKF_REMOVABLE = 1 << 1, |
| 774 | BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE, |
| 775 | }; |
| 776 | |
| 777 | /** |
| 778 | * blk_first_device_err() - Get the first block device |
| 779 | * |
| 780 | * The device returned is probed if necessary, and ready for use |
| 781 | * |
| 782 | * @flags: Indicates type of device to return |
| 783 | * @devp: Returns pointer to the first device in that uclass, or NULL if none |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 784 | * Return: 0 if found, -ENODEV if not found, other -ve on error |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 785 | */ |
| 786 | int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp); |
| 787 | |
| 788 | /** |
| 789 | * blk_next_device_err() - Get the next block device |
| 790 | * |
| 791 | * The device returned is probed if necessary, and ready for use |
| 792 | * |
| 793 | * @flags: Indicates type of device to return |
| 794 | * @devp: On entry, pointer to device to lookup. On exit, returns pointer |
| 795 | * to the next device in the uclass if no error occurred, or -ENODEV if |
| 796 | * there is no next device. |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 797 | * Return: 0 if found, -ENODEV if not found, other -ve on error |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 798 | */ |
| 799 | int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp); |
| 800 | |
| 801 | /** |
Simon Glass | 8e61f93 | 2022-02-28 12:08:35 -0700 | [diff] [blame] | 802 | * blk_find_first() - Return the first matching block device |
| 803 | * @flags: Indicates type of device to return |
| 804 | * @devp: Returns pointer to device, or NULL on error |
| 805 | * |
| 806 | * The device is not prepared for use - this is an internal function. |
| 807 | * The function uclass_get_device_tail() can be used to probe the device. |
| 808 | * |
| 809 | * Note that some devices are considered removable until they have been probed |
| 810 | * |
| 811 | * @return 0 if found, -ENODEV if not found |
| 812 | */ |
| 813 | int blk_find_first(enum blk_flag_t flags, struct udevice **devp); |
| 814 | |
| 815 | /** |
| 816 | * blk_find_next() - Return the next matching block device |
| 817 | * @flags: Indicates type of device to return |
| 818 | * @devp: On entry, pointer to device to lookup. On exit, returns pointer |
| 819 | * to the next device in the same uclass, or NULL if none |
| 820 | * |
| 821 | * The device is not prepared for use - this is an internal function. |
| 822 | * The function uclass_get_device_tail() can be used to probe the device. |
| 823 | * |
| 824 | * Note that some devices are considered removable until they have been probed |
| 825 | * |
| 826 | * @return 0 if found, -ENODEV if not found |
| 827 | */ |
| 828 | int blk_find_next(enum blk_flag_t flags, struct udevice **devp); |
| 829 | |
| 830 | /** |
| 831 | * blk_foreach() - iterate through block devices |
| 832 | * |
| 833 | * This creates a for() loop which works through the available block devices in |
| 834 | * order from start to end. |
| 835 | * |
| 836 | * If for some reason the uclass cannot be found, this does nothing. |
| 837 | * |
| 838 | * @_flags: Indicates type of device to return |
| 839 | * @_pos: struct udevice * to hold the current device. Set to NULL when there |
| 840 | * are no more devices. |
| 841 | */ |
| 842 | #define blk_foreach(_flags, _pos) \ |
| 843 | for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \ |
| 844 | _ret = blk_find_next(_flags, &_pos)) |
| 845 | |
| 846 | /** |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 847 | * blk_foreach_probe() - Helper function to iteration through block devices |
| 848 | * |
| 849 | * This creates a for() loop which works through the available devices in |
| 850 | * a uclass in order from start to end. Devices are probed if necessary, |
| 851 | * and ready for use. |
| 852 | * |
| 853 | * @flags: Indicates type of device to return |
| 854 | * @dev: struct udevice * to hold the current device. Set to NULL when there |
| 855 | * are no more devices. |
| 856 | */ |
| 857 | #define blk_foreach_probe(flags, pos) \ |
| 858 | for (int _ret = blk_first_device_err(flags, &(pos)); \ |
| 859 | !_ret && pos; \ |
| 860 | _ret = blk_next_device_err(flags, &(pos))) |
| 861 | |
| 862 | /** |
| 863 | * blk_count_devices() - count the number of devices of a particular type |
| 864 | * |
| 865 | * @flags: Indicates type of device to find |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 866 | * Return: number of devices matching those flags |
Simon Glass | fc7a744 | 2021-07-05 16:32:59 -0600 | [diff] [blame] | 867 | */ |
| 868 | int blk_count_devices(enum blk_flag_t flag); |
| 869 | |
Simon Glass | ac8162f | 2016-02-29 15:25:39 -0700 | [diff] [blame] | 870 | #endif |