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