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