2 * (C) Copyright 2000-2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
11 #ifdef CONFIG_SYS_64BIT_LBA
12 typedef uint64_t lbaint_t;
13 #define LBAFlength "ll"
15 typedef ulong lbaint_t;
16 #define LBAFlength "l"
18 #define LBAF "%" LBAFlength "x"
19 #define LBAFU "%" LBAFlength "u"
21 /* Interface types: */
35 IF_TYPE_COUNT, /* Number of interface types */
39 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
40 * with dev_get_uclass_platdata(dev)
44 * TODO: With driver model we should be able to use the parent
45 * device's uclass instead.
47 enum if_type if_type; /* type of the interface */
48 int devnum; /* device number */
49 unsigned char part_type; /* partition type */
50 unsigned char target; /* target SCSI ID */
51 unsigned char lun; /* target LUN */
52 unsigned char hwpart; /* HW partition, e.g. for eMMC */
53 unsigned char type; /* device type */
54 unsigned char removable; /* removable device */
56 /* device can use 48bit addr (ATA/ATAPI v7) */
59 lbaint_t lba; /* number of blocks */
60 unsigned long blksz; /* block size */
61 int log2blksz; /* for convenience: log2(blksz) */
62 char vendor[40+1]; /* IDE model, SCSI Vendor */
63 char product[20+1]; /* IDE Serial no, SCSI product */
64 char revision[8+1]; /* firmware revision */
68 unsigned long (*block_read)(struct blk_desc *block_dev,
72 unsigned long (*block_write)(struct blk_desc *block_dev,
76 unsigned long (*block_erase)(struct blk_desc *block_dev,
79 void *priv; /* driver private struct pointer */
83 #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
84 #define PAD_TO_BLOCKSIZE(size, blk_desc) \
85 (PAD_SIZE(size, blk_desc->blksz))
87 #ifdef CONFIG_BLOCK_CACHE
89 * blkcache_read() - attempt to read a set of blocks from cache
91 * @param iftype - IF_TYPE_x for type of device
92 * @param dev - device index of particular type
93 * @param start - starting block number
94 * @param blkcnt - number of blocks to read
95 * @param blksz - size in bytes of each block
96 * @param buf - buffer to contain cached data
98 * @return - '1' if block returned from cache, '0' otherwise.
100 int blkcache_read(int iftype, int dev,
101 lbaint_t start, lbaint_t blkcnt,
102 unsigned long blksz, void *buffer);
105 * blkcache_fill() - make data read from a block device available
108 * @param iftype - IF_TYPE_x for type of device
109 * @param dev - device index of particular type
110 * @param start - starting block number
111 * @param blkcnt - number of blocks available
112 * @param blksz - size in bytes of each block
113 * @param buf - buffer containing data to cache
116 void blkcache_fill(int iftype, int dev,
117 lbaint_t start, lbaint_t blkcnt,
118 unsigned long blksz, void const *buffer);
121 * blkcache_invalidate() - discard the cache for a set of blocks
122 * because of a write or device (re)initialization.
124 * @param iftype - IF_TYPE_x for type of device
125 * @param dev - device index of particular type
127 void blkcache_invalidate(int iftype, int dev);
130 * blkcache_configure() - configure block cache
132 * @param blocks - maximum blocks per entry
133 * @param entries - maximum entries in cache
135 void blkcache_configure(unsigned blocks, unsigned entries);
138 * statistics of the block cache
140 struct block_cache_stats {
143 unsigned entries; /* current entry count */
144 unsigned max_blocks_per_entry;
145 unsigned max_entries;
149 * get_blkcache_stats() - return statistics and reset
151 * @param stats - statistics are copied here
153 void blkcache_stats(struct block_cache_stats *stats);
157 static inline int blkcache_read(int iftype, int dev,
158 lbaint_t start, lbaint_t blkcnt,
159 unsigned long blksz, void *buffer)
164 static inline void blkcache_fill(int iftype, int dev,
165 lbaint_t start, lbaint_t blkcnt,
166 unsigned long blksz, void const *buffer) {}
168 static inline void blkcache_invalidate(int iftype, int dev) {}
175 /* Operations on block devices */
178 * read() - read from a block device
180 * @dev: Device to read from
181 * @start: Start block number to read (0=first)
182 * @blkcnt: Number of blocks to read
183 * @buffer: Destination buffer for data read
184 * @return number of blocks read, or -ve error number (see the
185 * IS_ERR_VALUE() macro
187 unsigned long (*read)(struct udevice *dev, lbaint_t start,
188 lbaint_t blkcnt, void *buffer);
191 * write() - write to a block device
193 * @dev: Device to write to
194 * @start: Start block number to write (0=first)
195 * @blkcnt: Number of blocks to write
196 * @buffer: Source buffer for data to write
197 * @return number of blocks written, or -ve error number (see the
198 * IS_ERR_VALUE() macro
200 unsigned long (*write)(struct udevice *dev, lbaint_t start,
201 lbaint_t blkcnt, const void *buffer);
204 * erase() - erase a section of a block device
206 * @dev: Device to (partially) erase
207 * @start: Start block number to erase (0=first)
208 * @blkcnt: Number of blocks to erase
209 * @return number of blocks erased, or -ve error number (see the
210 * IS_ERR_VALUE() macro
212 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
216 #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
219 * These functions should take struct udevice instead of struct blk_desc,
220 * but this is convenient for migration to driver model. Add a 'd' prefix
221 * to the function operations, so that blk_read(), etc. can be reserved for
222 * functions with the correct arguments.
224 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
225 lbaint_t blkcnt, void *buffer);
226 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
227 lbaint_t blkcnt, const void *buffer);
228 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
232 * blk_get_device() - Find and probe a block device ready for use
234 * @if_type: Interface type (enum if_type_t)
235 * @devnum: Device number (specific to each interface type)
236 * @devp: the device, if found
237 * @return - if found, -ENODEV if no device found, or other -ve error value
239 int blk_get_device(int if_type, int devnum, struct udevice **devp);
242 * blk_first_device() - Find the first device for a given interface
244 * The device is probed ready for use
246 * @devnum: Device number (specific to each interface type)
247 * @devp: the device, if found
248 * @return 0 if found, -ENODEV if no device, or other -ve error value
250 int blk_first_device(int if_type, struct udevice **devp);
253 * blk_next_device() - Find the next device for a given interface
255 * This can be called repeatedly after blk_first_device() to iterate through
256 * all devices of the given interface type.
258 * The device is probed ready for use
260 * @devp: On entry, the previous device returned. On exit, the next
262 * @return 0 if found, -ENODEV if no device, or other -ve error value
264 int blk_next_device(struct udevice **devp);
267 * blk_create_device() - Create a new block device
269 * @parent: Parent of the new device
270 * @drv_name: Driver name to use for the block device
271 * @name: Name for the device
272 * @if_type: Interface type (enum if_type_t)
273 * @devnum: Device number, specific to the interface type, or -1 to
274 * allocate the next available number
275 * @blksz: Block size of the device in bytes (typically 512)
276 * @size: Total size of the device in bytes
277 * @devp: the new device (which has not been probed)
279 int blk_create_device(struct udevice *parent, const char *drv_name,
280 const char *name, int if_type, int devnum, int blksz,
281 lbaint_t size, struct udevice **devp);
284 * blk_prepare_device() - Prepare a block device for use
286 * This reads partition information from the device if supported.
288 * @dev: Device to prepare
289 * @return 0 if ok, -ve on error
291 int blk_prepare_device(struct udevice *dev);
294 * blk_unbind_all() - Unbind all device of the given interface type
296 * The devices are removed and then unbound.
298 * @if_type: Interface type to unbind
299 * @return 0 if OK, -ve on error
301 int blk_unbind_all(int if_type);
304 * blk_find_max_devnum() - find the maximum device number for an interface type
306 * Finds the last allocated device number for an interface type @if_type. The
307 * next number is safe to use for a newly allocated device.
309 * @if_type: Interface type to scan
310 * @return maximum device number found, or -ENODEV if none, or other -ve on
313 int blk_find_max_devnum(enum if_type if_type);
318 * These functions should take struct udevice instead of struct blk_desc,
319 * but this is convenient for migration to driver model. Add a 'd' prefix
320 * to the function operations, so that blk_read(), etc. can be reserved for
321 * functions with the correct arguments.
323 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
324 lbaint_t blkcnt, void *buffer)
327 if (blkcache_read(block_dev->if_type, block_dev->devnum,
328 start, blkcnt, block_dev->blksz, buffer))
332 * We could check if block_read is NULL and return -ENOSYS. But this
333 * bloats the code slightly (cause some board to fail to build), and
334 * it would be an error to try an operation that does not exist.
336 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
337 if (blks_read == blkcnt)
338 blkcache_fill(block_dev->if_type, block_dev->devnum,
339 start, blkcnt, block_dev->blksz, buffer);
344 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
345 lbaint_t blkcnt, const void *buffer)
347 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
348 return block_dev->block_write(block_dev, start, blkcnt, buffer);
351 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
354 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
355 return block_dev->block_erase(block_dev, start, blkcnt);
359 * struct blk_driver - Driver for block interface types
361 * This provides access to the block devices for each interface type. One
362 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
363 * type that is to be supported.
365 * @if_typename: Interface type name
366 * @if_type: Interface type
367 * @max_devs: Maximum number of devices supported
368 * @desc: Pointer to list of devices for this interface type,
369 * or NULL to use @get_dev() instead
372 const char *if_typename;
373 enum if_type if_type;
375 struct blk_desc *desc;
377 * get_dev() - get a pointer to a block device given its number
379 * Each interface allocates its own devices and typically
380 * struct blk_desc is contained with the interface's data structure.
381 * There is no global numbering for block devices. This method allows
382 * the device for an interface type to be obtained when @desc is NULL.
384 * @devnum: Device number (0 for first device on that interface,
386 * @descp: Returns pointer to the block device on success
387 * @return 0 if OK, -ve on error
389 int (*get_dev)(int devnum, struct blk_desc **descp);
392 * select_hwpart() - Select a hardware partition
394 * Some devices (e.g. MMC) can support partitioning at the hardware
395 * level. This is quite separate from the normal idea of
396 * software-based partitions. MMC hardware partitions must be
397 * explicitly selected. Once selected only the region of the device
398 * covered by that partition is accessible.
400 * The MMC standard provides for two boot partitions (numbered 1 and 2),
401 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
402 * Partition 0 is the main user-data partition.
404 * @desc: Block device descriptor
405 * @hwpart: Hardware partition number to select. 0 means the main
406 * user-data partition, 1 is the first partition, 2 is
408 * @return 0 if OK, other value for an error
410 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
414 * Declare a new U-Boot legacy block driver. New drivers should use driver
415 * model (UCLASS_BLK).
417 #define U_BOOT_LEGACY_BLK(__name) \
418 ll_entry_declare(struct blk_driver, __name, blk_driver)
420 struct blk_driver *blk_driver_lookup_type(int if_type);
422 #endif /* !CONFIG_BLK */
425 * blk_get_devnum_by_typename() - Get a block device by type and number
427 * This looks through the available block devices of the given type, returning
428 * the one with the given @devnum.
430 * @if_type: Block device type
431 * @devnum: Device number
432 * @return point to block device descriptor, or NULL if not found
434 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
437 * blk_get_devnum_by_type() - Get a block device by type name, and number
439 * This looks up the block device type based on @if_typename, then calls
440 * blk_get_devnum_by_type().
442 * @if_typename: Block device type name
443 * @devnum: Device number
444 * @return point to block device descriptor, or NULL if not found
446 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
450 * blk_dselect_hwpart() - select a hardware partition
452 * This selects a hardware partition (such as is supported by MMC). The block
453 * device size may change as this effectively points the block device to a
454 * partition at the hardware level. See the select_hwpart() method above.
456 * @desc: Block device descriptor for the device to select
457 * @hwpart: Partition number to select
458 * @return 0 if OK, -ve on error
460 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
463 * blk_list_part() - list the partitions for block devices of a given type
465 * This looks up the partition type for each block device of type @if_type,
466 * then displays a list of partitions.
468 * @if_type: Block device type
469 * @return 0 if OK, -ENODEV if there is none of that type
471 int blk_list_part(enum if_type if_type);
474 * blk_list_devices() - list the block devices of a given type
476 * This lists each block device of the type @if_type, showing the capacity
477 * as well as type-specific information.
479 * @if_type: Block device type
481 void blk_list_devices(enum if_type if_type);
484 * blk_show_device() - show information about a given block device
486 * This shows the block device capacity as well as type-specific information.
488 * @if_type: Block device type
489 * @devnum: Device number
490 * @return 0 if OK, -ENODEV for invalid device number
492 int blk_show_device(enum if_type if_type, int devnum);
495 * blk_print_device_num() - show information about a given block device
497 * This is similar to blk_show_device() but returns an error if the block
498 * device type is unknown.
500 * @if_type: Block device type
501 * @devnum: Device number
502 * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
503 * device is not connected
505 int blk_print_device_num(enum if_type if_type, int devnum);
508 * blk_print_part_devnum() - print the partition information for a device
510 * @if_type: Block device type
511 * @devnum: Device number
512 * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
513 * the interface type is not supported, other -ve on other error
515 int blk_print_part_devnum(enum if_type if_type, int devnum);
518 * blk_read_devnum() - read blocks from a device
520 * @if_type: Block device type
521 * @devnum: Device number
522 * @blkcnt: Number of blocks to read
523 * @buffer: Address to write data to
524 * @return number of blocks read, or -ve error number on error
526 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
527 lbaint_t blkcnt, void *buffer);
530 * blk_write_devnum() - write blocks to a device
532 * @if_type: Block device type
533 * @devnum: Device number
534 * @blkcnt: Number of blocks to write
535 * @buffer: Address to read data from
536 * @return number of blocks written, or -ve error number on error
538 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
539 lbaint_t blkcnt, const void *buffer);
542 * blk_select_hwpart_devnum() - select a hardware partition
544 * This is similar to blk_dselect_hwpart() but it looks up the interface and
547 * @if_type: Block device type
548 * @devnum: Device number
549 * @hwpart: Partition number to select
550 * @return 0 if OK, -ve on error
552 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);