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 */
65 #if CONFIG_IS_ENABLED(BLK)
67 * For now we have a few functions which take struct blk_desc as a
68 * parameter. This field allows them to look up the associated
69 * device. Once these functions are removed we can drop this field.
73 unsigned long (*block_read)(struct blk_desc *block_dev,
77 unsigned long (*block_write)(struct blk_desc *block_dev,
81 unsigned long (*block_erase)(struct blk_desc *block_dev,
84 void *priv; /* driver private struct pointer */
88 #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
89 #define PAD_TO_BLOCKSIZE(size, blk_desc) \
90 (PAD_SIZE(size, blk_desc->blksz))
92 #ifdef CONFIG_BLOCK_CACHE
94 * blkcache_read() - attempt to read a set of blocks from cache
96 * @param iftype - IF_TYPE_x for type of device
97 * @param dev - device index of particular type
98 * @param start - starting block number
99 * @param blkcnt - number of blocks to read
100 * @param blksz - size in bytes of each block
101 * @param buf - buffer to contain cached data
103 * @return - '1' if block returned from cache, '0' otherwise.
105 int blkcache_read(int iftype, int dev,
106 lbaint_t start, lbaint_t blkcnt,
107 unsigned long blksz, void *buffer);
110 * blkcache_fill() - make data read from a block device available
113 * @param iftype - IF_TYPE_x for type of device
114 * @param dev - device index of particular type
115 * @param start - starting block number
116 * @param blkcnt - number of blocks available
117 * @param blksz - size in bytes of each block
118 * @param buf - buffer containing data to cache
121 void blkcache_fill(int iftype, int dev,
122 lbaint_t start, lbaint_t blkcnt,
123 unsigned long blksz, void const *buffer);
126 * blkcache_invalidate() - discard the cache for a set of blocks
127 * because of a write or device (re)initialization.
129 * @param iftype - IF_TYPE_x for type of device
130 * @param dev - device index of particular type
132 void blkcache_invalidate(int iftype, int dev);
135 * blkcache_configure() - configure block cache
137 * @param blocks - maximum blocks per entry
138 * @param entries - maximum entries in cache
140 void blkcache_configure(unsigned blocks, unsigned entries);
143 * statistics of the block cache
145 struct block_cache_stats {
148 unsigned entries; /* current entry count */
149 unsigned max_blocks_per_entry;
150 unsigned max_entries;
154 * get_blkcache_stats() - return statistics and reset
156 * @param stats - statistics are copied here
158 void blkcache_stats(struct block_cache_stats *stats);
162 static inline int blkcache_read(int iftype, int dev,
163 lbaint_t start, lbaint_t blkcnt,
164 unsigned long blksz, void *buffer)
169 static inline void blkcache_fill(int iftype, int dev,
170 lbaint_t start, lbaint_t blkcnt,
171 unsigned long blksz, void const *buffer) {}
173 static inline void blkcache_invalidate(int iftype, int dev) {}
177 #if CONFIG_IS_ENABLED(BLK)
180 /* Operations on block devices */
183 * read() - read from a block device
185 * @dev: Device to read from
186 * @start: Start block number to read (0=first)
187 * @blkcnt: Number of blocks to read
188 * @buffer: Destination buffer for data read
189 * @return number of blocks read, or -ve error number (see the
190 * IS_ERR_VALUE() macro
192 unsigned long (*read)(struct udevice *dev, lbaint_t start,
193 lbaint_t blkcnt, void *buffer);
196 * write() - write to a block device
198 * @dev: Device to write to
199 * @start: Start block number to write (0=first)
200 * @blkcnt: Number of blocks to write
201 * @buffer: Source buffer for data to write
202 * @return number of blocks written, or -ve error number (see the
203 * IS_ERR_VALUE() macro
205 unsigned long (*write)(struct udevice *dev, lbaint_t start,
206 lbaint_t blkcnt, const void *buffer);
209 * erase() - erase a section of a block device
211 * @dev: Device to (partially) erase
212 * @start: Start block number to erase (0=first)
213 * @blkcnt: Number of blocks to erase
214 * @return number of blocks erased, or -ve error number (see the
215 * IS_ERR_VALUE() macro
217 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
221 * select_hwpart() - select a particular hardware partition
223 * Some devices (e.g. MMC) can support partitioning at the hardware
224 * level. This is quite separate from the normal idea of
225 * software-based partitions. MMC hardware partitions must be
226 * explicitly selected. Once selected only the region of the device
227 * covered by that partition is accessible.
229 * The MMC standard provides for two boot partitions (numbered 1 and 2),
230 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
232 * @desc: Block device to update
233 * @hwpart: Hardware partition number to select. 0 means the raw
234 * device, 1 is the first partition, 2 is the second, etc.
235 * @return 0 if OK, -ve on error
237 int (*select_hwpart)(struct udevice *dev, int hwpart);
240 #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
243 * These functions should take struct udevice instead of struct blk_desc,
244 * but this is convenient for migration to driver model. Add a 'd' prefix
245 * to the function operations, so that blk_read(), etc. can be reserved for
246 * functions with the correct arguments.
248 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
249 lbaint_t blkcnt, void *buffer);
250 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
251 lbaint_t blkcnt, const void *buffer);
252 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
256 * blk_find_device() - Find a block device
258 * This function does not activate the device. The device will be returned
259 * whether or not it is activated.
261 * @if_type: Interface type (enum if_type_t)
262 * @devnum: Device number (specific to each interface type)
263 * @devp: the device, if found
264 * @return 0 if found, -ENODEV if no device found, or other -ve error value
266 int blk_find_device(int if_type, int devnum, struct udevice **devp);
269 * blk_get_device() - Find and probe a block device ready for use
271 * @if_type: Interface type (enum if_type_t)
272 * @devnum: Device number (specific to each interface type)
273 * @devp: the device, if found
274 * @return 0 if found, -ENODEV if no device found, or other -ve error value
276 int blk_get_device(int if_type, int devnum, struct udevice **devp);
279 * blk_first_device() - Find the first device for a given interface
281 * The device is probed ready for use
283 * @devnum: Device number (specific to each interface type)
284 * @devp: the device, if found
285 * @return 0 if found, -ENODEV if no device, or other -ve error value
287 int blk_first_device(int if_type, struct udevice **devp);
290 * blk_next_device() - Find the next device for a given interface
292 * This can be called repeatedly after blk_first_device() to iterate through
293 * all devices of the given interface type.
295 * The device is probed ready for use
297 * @devp: On entry, the previous device returned. On exit, the next
299 * @return 0 if found, -ENODEV if no device, or other -ve error value
301 int blk_next_device(struct udevice **devp);
304 * blk_create_device() - Create a new block device
306 * @parent: Parent of the new device
307 * @drv_name: Driver name to use for the block device
308 * @name: Name for the device
309 * @if_type: Interface type (enum if_type_t)
310 * @devnum: Device number, specific to the interface type, or -1 to
311 * allocate the next available number
312 * @blksz: Block size of the device in bytes (typically 512)
313 * @size: Total size of the device in bytes
314 * @devp: the new device (which has not been probed)
316 int blk_create_device(struct udevice *parent, const char *drv_name,
317 const char *name, int if_type, int devnum, int blksz,
318 lbaint_t size, struct udevice **devp);
321 * blk_create_devicef() - Create a new named block device
323 * @parent: Parent of the new device
324 * @drv_name: Driver name to use for the block device
325 * @name: Name for the device (parent name is prepended)
326 * @if_type: Interface type (enum if_type_t)
327 * @devnum: Device number, specific to the interface type, or -1 to
328 * allocate the next available number
329 * @blksz: Block size of the device in bytes (typically 512)
330 * @size: Total size of the device in bytes
331 * @devp: the new device (which has not been probed)
333 int blk_create_devicef(struct udevice *parent, const char *drv_name,
334 const char *name, int if_type, int devnum, int blksz,
335 lbaint_t size, struct udevice **devp);
338 * blk_prepare_device() - Prepare a block device for use
340 * This reads partition information from the device if supported.
342 * @dev: Device to prepare
343 * @return 0 if ok, -ve on error
345 int blk_prepare_device(struct udevice *dev);
348 * blk_unbind_all() - Unbind all device of the given interface type
350 * The devices are removed and then unbound.
352 * @if_type: Interface type to unbind
353 * @return 0 if OK, -ve on error
355 int blk_unbind_all(int if_type);
358 * blk_find_max_devnum() - find the maximum device number for an interface type
360 * Finds the last allocated device number for an interface type @if_type. The
361 * next number is safe to use for a newly allocated device.
363 * @if_type: Interface type to scan
364 * @return maximum device number found, or -ENODEV if none, or other -ve on
367 int blk_find_max_devnum(enum if_type if_type);
370 * blk_select_hwpart() - select a hardware partition
372 * Select a hardware partition if the device supports it (typically MMC does)
374 * @dev: Device to update
375 * @hwpart: Partition number to select
376 * @return 0 if OK, -ve on error
378 int blk_select_hwpart(struct udevice *dev, int hwpart);
381 * blk_get_from_parent() - obtain a block device by looking up its parent
385 int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
390 * These functions should take struct udevice instead of struct blk_desc,
391 * but this is convenient for migration to driver model. Add a 'd' prefix
392 * to the function operations, so that blk_read(), etc. can be reserved for
393 * functions with the correct arguments.
395 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
396 lbaint_t blkcnt, void *buffer)
399 if (blkcache_read(block_dev->if_type, block_dev->devnum,
400 start, blkcnt, block_dev->blksz, buffer))
404 * We could check if block_read is NULL and return -ENOSYS. But this
405 * bloats the code slightly (cause some board to fail to build), and
406 * it would be an error to try an operation that does not exist.
408 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
409 if (blks_read == blkcnt)
410 blkcache_fill(block_dev->if_type, block_dev->devnum,
411 start, blkcnt, block_dev->blksz, buffer);
416 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
417 lbaint_t blkcnt, const void *buffer)
419 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
420 return block_dev->block_write(block_dev, start, blkcnt, buffer);
423 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
426 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
427 return block_dev->block_erase(block_dev, start, blkcnt);
431 * struct blk_driver - Driver for block interface types
433 * This provides access to the block devices for each interface type. One
434 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
435 * type that is to be supported.
437 * @if_typename: Interface type name
438 * @if_type: Interface type
439 * @max_devs: Maximum number of devices supported
440 * @desc: Pointer to list of devices for this interface type,
441 * or NULL to use @get_dev() instead
444 const char *if_typename;
445 enum if_type if_type;
447 struct blk_desc *desc;
449 * get_dev() - get a pointer to a block device given its number
451 * Each interface allocates its own devices and typically
452 * struct blk_desc is contained with the interface's data structure.
453 * There is no global numbering for block devices. This method allows
454 * the device for an interface type to be obtained when @desc is NULL.
456 * @devnum: Device number (0 for first device on that interface,
458 * @descp: Returns pointer to the block device on success
459 * @return 0 if OK, -ve on error
461 int (*get_dev)(int devnum, struct blk_desc **descp);
464 * select_hwpart() - Select a hardware partition
466 * Some devices (e.g. MMC) can support partitioning at the hardware
467 * level. This is quite separate from the normal idea of
468 * software-based partitions. MMC hardware partitions must be
469 * explicitly selected. Once selected only the region of the device
470 * covered by that partition is accessible.
472 * The MMC standard provides for two boot partitions (numbered 1 and 2),
473 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
474 * Partition 0 is the main user-data partition.
476 * @desc: Block device descriptor
477 * @hwpart: Hardware partition number to select. 0 means the main
478 * user-data partition, 1 is the first partition, 2 is
480 * @return 0 if OK, other value for an error
482 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
486 * Declare a new U-Boot legacy block driver. New drivers should use driver
487 * model (UCLASS_BLK).
489 #define U_BOOT_LEGACY_BLK(__name) \
490 ll_entry_declare(struct blk_driver, __name, blk_driver)
492 struct blk_driver *blk_driver_lookup_type(int if_type);
494 #endif /* !CONFIG_BLK */
497 * blk_get_devnum_by_typename() - Get a block device by type and number
499 * This looks through the available block devices of the given type, returning
500 * the one with the given @devnum.
502 * @if_type: Block device type
503 * @devnum: Device number
504 * @return point to block device descriptor, or NULL if not found
506 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
509 * blk_get_devnum_by_type() - Get a block device by type name, and number
511 * This looks up the block device type based on @if_typename, then calls
512 * blk_get_devnum_by_type().
514 * @if_typename: Block device type name
515 * @devnum: Device number
516 * @return point to block device descriptor, or NULL if not found
518 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
522 * blk_dselect_hwpart() - select a hardware partition
524 * This selects a hardware partition (such as is supported by MMC). The block
525 * device size may change as this effectively points the block device to a
526 * partition at the hardware level. See the select_hwpart() method above.
528 * @desc: Block device descriptor for the device to select
529 * @hwpart: Partition number to select
530 * @return 0 if OK, -ve on error
532 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
535 * blk_list_part() - list the partitions for block devices of a given type
537 * This looks up the partition type for each block device of type @if_type,
538 * then displays a list of partitions.
540 * @if_type: Block device type
541 * @return 0 if OK, -ENODEV if there is none of that type
543 int blk_list_part(enum if_type if_type);
546 * blk_list_devices() - list the block devices of a given type
548 * This lists each block device of the type @if_type, showing the capacity
549 * as well as type-specific information.
551 * @if_type: Block device type
553 void blk_list_devices(enum if_type if_type);
556 * blk_show_device() - show information about a given block device
558 * This shows the block device capacity as well as type-specific information.
560 * @if_type: Block device type
561 * @devnum: Device number
562 * @return 0 if OK, -ENODEV for invalid device number
564 int blk_show_device(enum if_type if_type, int devnum);
567 * blk_print_device_num() - show information about a given block device
569 * This is similar to blk_show_device() but returns an error if the block
570 * device type is unknown.
572 * @if_type: Block device type
573 * @devnum: Device number
574 * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
575 * device is not connected
577 int blk_print_device_num(enum if_type if_type, int devnum);
580 * blk_print_part_devnum() - print the partition information for a device
582 * @if_type: Block device type
583 * @devnum: Device number
584 * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
585 * the interface type is not supported, other -ve on other error
587 int blk_print_part_devnum(enum if_type if_type, int devnum);
590 * blk_read_devnum() - read blocks from a device
592 * @if_type: Block device type
593 * @devnum: Device number
594 * @blkcnt: Number of blocks to read
595 * @buffer: Address to write data to
596 * @return number of blocks read, or -ve error number on error
598 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
599 lbaint_t blkcnt, void *buffer);
602 * blk_write_devnum() - write blocks to a device
604 * @if_type: Block device type
605 * @devnum: Device number
606 * @blkcnt: Number of blocks to write
607 * @buffer: Address to read data from
608 * @return number of blocks written, or -ve error number on error
610 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
611 lbaint_t blkcnt, const void *buffer);
614 * blk_select_hwpart_devnum() - select a hardware partition
616 * This is similar to blk_dselect_hwpart() but it looks up the interface and
619 * @if_type: Block device type
620 * @devnum: Device number
621 * @hwpart: Partition number to select
622 * @return 0 if OK, -ve on error
624 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);