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