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: */
36 IF_TYPE_COUNT, /* Number of interface types */
39 #define BLK_VEN_SIZE 40
40 #define BLK_PRD_SIZE 20
41 #define BLK_REV_SIZE 8
44 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
45 * with dev_get_uclass_platdata(dev)
49 * TODO: With driver model we should be able to use the parent
50 * device's uclass instead.
52 enum if_type if_type; /* type of the interface */
53 int devnum; /* device number */
54 unsigned char part_type; /* partition type */
55 unsigned char target; /* target SCSI ID */
56 unsigned char lun; /* target LUN */
57 unsigned char hwpart; /* HW partition, e.g. for eMMC */
58 unsigned char type; /* device type */
59 unsigned char removable; /* removable device */
61 /* device can use 48bit addr (ATA/ATAPI v7) */
64 lbaint_t lba; /* number of blocks */
65 unsigned long blksz; /* block size */
66 int log2blksz; /* for convenience: log2(blksz) */
67 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
68 char product[BLK_PRD_SIZE + 1]; /* device product number */
69 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
70 #if CONFIG_IS_ENABLED(BLK)
72 * For now we have a few functions which take struct blk_desc as a
73 * parameter. This field allows them to look up the associated
74 * device. Once these functions are removed we can drop this field.
78 unsigned long (*block_read)(struct blk_desc *block_dev,
82 unsigned long (*block_write)(struct blk_desc *block_dev,
86 unsigned long (*block_erase)(struct blk_desc *block_dev,
89 void *priv; /* driver private struct pointer */
93 #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
94 #define PAD_TO_BLOCKSIZE(size, blk_desc) \
95 (PAD_SIZE(size, blk_desc->blksz))
97 #ifdef CONFIG_BLOCK_CACHE
99 * blkcache_read() - attempt to read a set of blocks from cache
101 * @param iftype - IF_TYPE_x for type of device
102 * @param dev - device index of particular type
103 * @param start - starting block number
104 * @param blkcnt - number of blocks to read
105 * @param blksz - size in bytes of each block
106 * @param buf - buffer to contain cached data
108 * @return - '1' if block returned from cache, '0' otherwise.
110 int blkcache_read(int iftype, int dev,
111 lbaint_t start, lbaint_t blkcnt,
112 unsigned long blksz, void *buffer);
115 * blkcache_fill() - make data read from a block device available
118 * @param iftype - IF_TYPE_x for type of device
119 * @param dev - device index of particular type
120 * @param start - starting block number
121 * @param blkcnt - number of blocks available
122 * @param blksz - size in bytes of each block
123 * @param buf - buffer containing data to cache
126 void blkcache_fill(int iftype, int dev,
127 lbaint_t start, lbaint_t blkcnt,
128 unsigned long blksz, void const *buffer);
131 * blkcache_invalidate() - discard the cache for a set of blocks
132 * because of a write or device (re)initialization.
134 * @param iftype - IF_TYPE_x for type of device
135 * @param dev - device index of particular type
137 void blkcache_invalidate(int iftype, int dev);
140 * blkcache_configure() - configure block cache
142 * @param blocks - maximum blocks per entry
143 * @param entries - maximum entries in cache
145 void blkcache_configure(unsigned blocks, unsigned entries);
148 * statistics of the block cache
150 struct block_cache_stats {
153 unsigned entries; /* current entry count */
154 unsigned max_blocks_per_entry;
155 unsigned max_entries;
159 * get_blkcache_stats() - return statistics and reset
161 * @param stats - statistics are copied here
163 void blkcache_stats(struct block_cache_stats *stats);
167 static inline int blkcache_read(int iftype, int dev,
168 lbaint_t start, lbaint_t blkcnt,
169 unsigned long blksz, void *buffer)
174 static inline void blkcache_fill(int iftype, int dev,
175 lbaint_t start, lbaint_t blkcnt,
176 unsigned long blksz, void const *buffer) {}
178 static inline void blkcache_invalidate(int iftype, int dev) {}
182 #if CONFIG_IS_ENABLED(BLK)
185 /* Operations on block devices */
188 * read() - read from a block device
190 * @dev: Device to read from
191 * @start: Start block number to read (0=first)
192 * @blkcnt: Number of blocks to read
193 * @buffer: Destination buffer for data read
194 * @return number of blocks read, or -ve error number (see the
195 * IS_ERR_VALUE() macro
197 unsigned long (*read)(struct udevice *dev, lbaint_t start,
198 lbaint_t blkcnt, void *buffer);
201 * write() - write to a block device
203 * @dev: Device to write to
204 * @start: Start block number to write (0=first)
205 * @blkcnt: Number of blocks to write
206 * @buffer: Source buffer for data to write
207 * @return number of blocks written, or -ve error number (see the
208 * IS_ERR_VALUE() macro
210 unsigned long (*write)(struct udevice *dev, lbaint_t start,
211 lbaint_t blkcnt, const void *buffer);
214 * erase() - erase a section of a block device
216 * @dev: Device to (partially) erase
217 * @start: Start block number to erase (0=first)
218 * @blkcnt: Number of blocks to erase
219 * @return number of blocks erased, or -ve error number (see the
220 * IS_ERR_VALUE() macro
222 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
226 * select_hwpart() - select a particular hardware partition
228 * Some devices (e.g. MMC) can support partitioning at the hardware
229 * level. This is quite separate from the normal idea of
230 * software-based partitions. MMC hardware partitions must be
231 * explicitly selected. Once selected only the region of the device
232 * covered by that partition is accessible.
234 * The MMC standard provides for two boot partitions (numbered 1 and 2),
235 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
237 * @desc: Block device to update
238 * @hwpart: Hardware partition number to select. 0 means the raw
239 * device, 1 is the first partition, 2 is the second, etc.
240 * @return 0 if OK, -ve on error
242 int (*select_hwpart)(struct udevice *dev, int hwpart);
245 #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
248 * These functions should take struct udevice instead of struct blk_desc,
249 * but this is convenient for migration to driver model. Add a 'd' prefix
250 * to the function operations, so that blk_read(), etc. can be reserved for
251 * functions with the correct arguments.
253 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
254 lbaint_t blkcnt, void *buffer);
255 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
256 lbaint_t blkcnt, const void *buffer);
257 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
261 * blk_find_device() - Find a block device
263 * This function does not activate the device. The device will be returned
264 * whether or not it is activated.
266 * @if_type: Interface type (enum if_type_t)
267 * @devnum: Device number (specific to each interface type)
268 * @devp: the device, if found
269 * @return 0 if found, -ENODEV if no device found, or other -ve error value
271 int blk_find_device(int if_type, int devnum, struct udevice **devp);
274 * blk_get_device() - Find and probe a block device ready for use
276 * @if_type: Interface type (enum if_type_t)
277 * @devnum: Device number (specific to each interface type)
278 * @devp: the device, if found
279 * @return 0 if found, -ENODEV if no device found, or other -ve error value
281 int blk_get_device(int if_type, int devnum, struct udevice **devp);
284 * blk_first_device() - Find the first device for a given interface
286 * The device is probed ready for use
288 * @devnum: Device number (specific to each interface type)
289 * @devp: the device, if found
290 * @return 0 if found, -ENODEV if no device, or other -ve error value
292 int blk_first_device(int if_type, struct udevice **devp);
295 * blk_next_device() - Find the next device for a given interface
297 * This can be called repeatedly after blk_first_device() to iterate through
298 * all devices of the given interface type.
300 * The device is probed ready for use
302 * @devp: On entry, the previous device returned. On exit, the next
304 * @return 0 if found, -ENODEV if no device, or other -ve error value
306 int blk_next_device(struct udevice **devp);
309 * blk_create_device() - Create a new block device
311 * @parent: Parent of the new device
312 * @drv_name: Driver name to use for the block device
313 * @name: Name for the device
314 * @if_type: Interface type (enum if_type_t)
315 * @devnum: Device number, specific to the interface type, or -1 to
316 * allocate the next available number
317 * @blksz: Block size of the device in bytes (typically 512)
318 * @lba: Total number of blocks of the device
319 * @devp: the new device (which has not been probed)
321 int blk_create_device(struct udevice *parent, const char *drv_name,
322 const char *name, int if_type, int devnum, int blksz,
323 lbaint_t lba, struct udevice **devp);
326 * blk_create_devicef() - Create a new named block device
328 * @parent: Parent of the new device
329 * @drv_name: Driver name to use for the block device
330 * @name: Name for the device (parent name is prepended)
331 * @if_type: Interface type (enum if_type_t)
332 * @devnum: Device number, specific to the interface type, or -1 to
333 * allocate the next available number
334 * @blksz: Block size of the device in bytes (typically 512)
335 * @lba: Total number of blocks of the device
336 * @devp: the new device (which has not been probed)
338 int blk_create_devicef(struct udevice *parent, const char *drv_name,
339 const char *name, int if_type, int devnum, int blksz,
340 lbaint_t lba, struct udevice **devp);
343 * blk_prepare_device() - Prepare a block device for use
345 * This reads partition information from the device if supported.
347 * @dev: Device to prepare
348 * @return 0 if ok, -ve on error
350 int blk_prepare_device(struct udevice *dev);
353 * blk_unbind_all() - Unbind all device of the given interface type
355 * The devices are removed and then unbound.
357 * @if_type: Interface type to unbind
358 * @return 0 if OK, -ve on error
360 int blk_unbind_all(int if_type);
363 * blk_find_max_devnum() - find the maximum device number for an interface type
365 * Finds the last allocated device number for an interface type @if_type. The
366 * next number is safe to use for a newly allocated device.
368 * @if_type: Interface type to scan
369 * @return maximum device number found, or -ENODEV if none, or other -ve on
372 int blk_find_max_devnum(enum if_type if_type);
375 * blk_select_hwpart() - select a hardware partition
377 * Select a hardware partition if the device supports it (typically MMC does)
379 * @dev: Device to update
380 * @hwpart: Partition number to select
381 * @return 0 if OK, -ve on error
383 int blk_select_hwpart(struct udevice *dev, int hwpart);
386 * blk_get_from_parent() - obtain a block device by looking up its parent
390 int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
395 * These functions should take struct udevice instead of struct blk_desc,
396 * but this is convenient for migration to driver model. Add a 'd' prefix
397 * to the function operations, so that blk_read(), etc. can be reserved for
398 * functions with the correct arguments.
400 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
401 lbaint_t blkcnt, void *buffer)
404 if (blkcache_read(block_dev->if_type, block_dev->devnum,
405 start, blkcnt, block_dev->blksz, buffer))
409 * We could check if block_read is NULL and return -ENOSYS. But this
410 * bloats the code slightly (cause some board to fail to build), and
411 * it would be an error to try an operation that does not exist.
413 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
414 if (blks_read == blkcnt)
415 blkcache_fill(block_dev->if_type, block_dev->devnum,
416 start, blkcnt, block_dev->blksz, buffer);
421 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
422 lbaint_t blkcnt, const void *buffer)
424 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
425 return block_dev->block_write(block_dev, start, blkcnt, buffer);
428 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
431 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
432 return block_dev->block_erase(block_dev, start, blkcnt);
436 * struct blk_driver - Driver for block interface types
438 * This provides access to the block devices for each interface type. One
439 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
440 * type that is to be supported.
442 * @if_typename: Interface type name
443 * @if_type: Interface type
444 * @max_devs: Maximum number of devices supported
445 * @desc: Pointer to list of devices for this interface type,
446 * or NULL to use @get_dev() instead
449 const char *if_typename;
450 enum if_type if_type;
452 struct blk_desc *desc;
454 * get_dev() - get a pointer to a block device given its number
456 * Each interface allocates its own devices and typically
457 * struct blk_desc is contained with the interface's data structure.
458 * There is no global numbering for block devices. This method allows
459 * the device for an interface type to be obtained when @desc is NULL.
461 * @devnum: Device number (0 for first device on that interface,
463 * @descp: Returns pointer to the block device on success
464 * @return 0 if OK, -ve on error
466 int (*get_dev)(int devnum, struct blk_desc **descp);
469 * select_hwpart() - Select a hardware partition
471 * Some devices (e.g. MMC) can support partitioning at the hardware
472 * level. This is quite separate from the normal idea of
473 * software-based partitions. MMC hardware partitions must be
474 * explicitly selected. Once selected only the region of the device
475 * covered by that partition is accessible.
477 * The MMC standard provides for two boot partitions (numbered 1 and 2),
478 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
479 * Partition 0 is the main user-data partition.
481 * @desc: Block device descriptor
482 * @hwpart: Hardware partition number to select. 0 means the main
483 * user-data partition, 1 is the first partition, 2 is
485 * @return 0 if OK, other value for an error
487 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
491 * Declare a new U-Boot legacy block driver. New drivers should use driver
492 * model (UCLASS_BLK).
494 #define U_BOOT_LEGACY_BLK(__name) \
495 ll_entry_declare(struct blk_driver, __name, blk_driver)
497 struct blk_driver *blk_driver_lookup_type(int if_type);
499 #endif /* !CONFIG_BLK */
502 * blk_get_devnum_by_typename() - Get a block device by type and number
504 * This looks through the available block devices of the given type, returning
505 * the one with the given @devnum.
507 * @if_type: Block device type
508 * @devnum: Device number
509 * @return point to block device descriptor, or NULL if not found
511 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
514 * blk_get_devnum_by_type() - Get a block device by type name, and number
516 * This looks up the block device type based on @if_typename, then calls
517 * blk_get_devnum_by_type().
519 * @if_typename: Block device type name
520 * @devnum: Device number
521 * @return point to block device descriptor, or NULL if not found
523 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
527 * blk_dselect_hwpart() - select a hardware partition
529 * This selects a hardware partition (such as is supported by MMC). The block
530 * device size may change as this effectively points the block device to a
531 * partition at the hardware level. See the select_hwpart() method above.
533 * @desc: Block device descriptor for the device to select
534 * @hwpart: Partition number to select
535 * @return 0 if OK, -ve on error
537 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
540 * blk_list_part() - list the partitions for block devices of a given type
542 * This looks up the partition type for each block device of type @if_type,
543 * then displays a list of partitions.
545 * @if_type: Block device type
546 * @return 0 if OK, -ENODEV if there is none of that type
548 int blk_list_part(enum if_type if_type);
551 * blk_list_devices() - list the block devices of a given type
553 * This lists each block device of the type @if_type, showing the capacity
554 * as well as type-specific information.
556 * @if_type: Block device type
558 void blk_list_devices(enum if_type if_type);
561 * blk_show_device() - show information about a given block device
563 * This shows the block device capacity as well as type-specific information.
565 * @if_type: Block device type
566 * @devnum: Device number
567 * @return 0 if OK, -ENODEV for invalid device number
569 int blk_show_device(enum if_type if_type, int devnum);
572 * blk_print_device_num() - show information about a given block device
574 * This is similar to blk_show_device() but returns an error if the block
575 * device type is unknown.
577 * @if_type: Block device type
578 * @devnum: Device number
579 * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
580 * device is not connected
582 int blk_print_device_num(enum if_type if_type, int devnum);
585 * blk_print_part_devnum() - print the partition information for a device
587 * @if_type: Block device type
588 * @devnum: Device number
589 * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
590 * the interface type is not supported, other -ve on other error
592 int blk_print_part_devnum(enum if_type if_type, int devnum);
595 * blk_read_devnum() - read blocks from a device
597 * @if_type: Block device type
598 * @devnum: Device number
599 * @blkcnt: Number of blocks to read
600 * @buffer: Address to write data to
601 * @return number of blocks read, or -ve error number on error
603 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
604 lbaint_t blkcnt, void *buffer);
607 * blk_write_devnum() - write blocks to a device
609 * @if_type: Block device type
610 * @devnum: Device number
611 * @blkcnt: Number of blocks to write
612 * @buffer: Address to read data from
613 * @return number of blocks written, or -ve error number on error
615 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
616 lbaint_t blkcnt, const void *buffer);
619 * blk_select_hwpart_devnum() - select a hardware partition
621 * This is similar to blk_dselect_hwpart() but it looks up the interface and
624 * @if_type: Block device type
625 * @devnum: Device number
626 * @hwpart: Partition number to select
627 * @return 0 if OK, -ve on error
629 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
632 * blk_get_if_type_name() - Get the name of an interface type
634 * @if_type: Interface type to check
635 * @return name of interface, or NULL if none
637 const char *blk_get_if_type_name(enum if_type if_type);
640 * blk_common_cmd() - handle common commands with block devices
642 * @args: Number of arguments to the command (argv[0] is the command itself)
643 * @argv: Command arguments
644 * @if_type: Interface type
645 * @cur_devnump: Current device number for this interface type
646 * @return 0 if OK, CMD_RET_ERROR on error
648 int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,