xref: /rk3399_rockchip-uboot/include/blk.h (revision 9ff3d0147e970e15128c2e0436f2f4069b687ad4)
1 /*
2  * (C) Copyright 2000-2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #ifndef BLK_H
9 #define BLK_H
10 
11 #ifdef CONFIG_SYS_64BIT_LBA
12 typedef uint64_t lbaint_t;
13 #define LBAFlength "ll"
14 #else
15 typedef ulong lbaint_t;
16 #define LBAFlength "l"
17 #endif
18 #define LBAF "%" LBAFlength "x"
19 #define LBAFU "%" LBAFlength "u"
20 
21 /* Interface types: */
22 enum if_type {
23 	IF_TYPE_UNKNOWN = 0,
24 	IF_TYPE_IDE,
25 	IF_TYPE_SCSI,
26 	IF_TYPE_ATAPI,
27 	IF_TYPE_USB,
28 	IF_TYPE_DOC,
29 	IF_TYPE_MMC,
30 	IF_TYPE_SD,
31 	IF_TYPE_SATA,
32 	IF_TYPE_HOST,
33 	IF_TYPE_SYSTEMACE,
34 	IF_TYPE_NVME,
35 	IF_TYPE_RKNAND,
36 	IF_TYPE_SPINAND,
37 	IF_TYPE_SPINOR,
38 
39 	IF_TYPE_COUNT,			/* Number of interface types */
40 };
41 
42 #define BLK_VEN_SIZE		40
43 #define BLK_PRD_SIZE		20
44 #define BLK_REV_SIZE		8
45 
46 /*
47  * With driver model (CONFIG_BLK) this is uclass platform data, accessible
48  * with dev_get_uclass_platdata(dev)
49  */
50 struct blk_desc {
51 	/*
52 	 * TODO: With driver model we should be able to use the parent
53 	 * device's uclass instead.
54 	 */
55 	enum if_type	if_type;	/* type of the interface */
56 	int		devnum;		/* device number */
57 	unsigned char	part_type;	/* partition type */
58 	unsigned char	target;		/* target SCSI ID */
59 	unsigned char	lun;		/* target LUN */
60 	unsigned char	hwpart;		/* HW partition, e.g. for eMMC */
61 	unsigned char	type;		/* device type */
62 	unsigned char	removable;	/* removable device */
63 #ifdef CONFIG_LBA48
64 	/* device can use 48bit addr (ATA/ATAPI v7) */
65 	unsigned char	lba48;
66 #endif
67 	lbaint_t	lba;		/* number of blocks */
68 	unsigned long	blksz;		/* block size */
69 	int		log2blksz;	/* for convenience: log2(blksz) */
70 	char		vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
71 	char		product[BLK_PRD_SIZE + 1]; /* device product number */
72 	char		revision[BLK_REV_SIZE + 1]; /* firmware revision */
73 #if CONFIG_IS_ENABLED(BLK)
74 	/*
75 	 * For now we have a few functions which take struct blk_desc as a
76 	 * parameter. This field allows them to look up the associated
77 	 * device. Once these functions are removed we can drop this field.
78 	 */
79 	struct udevice *bdev;
80 #else
81 	unsigned long	(*block_read)(struct blk_desc *block_dev,
82 				      lbaint_t start,
83 				      lbaint_t blkcnt,
84 				      void *buffer);
85 	unsigned long	(*block_write)(struct blk_desc *block_dev,
86 				       lbaint_t start,
87 				       lbaint_t blkcnt,
88 				       const void *buffer);
89 	unsigned long	(*block_erase)(struct blk_desc *block_dev,
90 				       lbaint_t start,
91 				       lbaint_t blkcnt);
92 	void		*priv;		/* driver private struct pointer */
93 #endif
94 };
95 
96 #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
97 #define PAD_TO_BLOCKSIZE(size, blk_desc) \
98 	(PAD_SIZE(size, blk_desc->blksz))
99 
100 #ifdef CONFIG_BLOCK_CACHE
101 /**
102  * blkcache_read() - attempt to read a set of blocks from cache
103  *
104  * @param iftype - IF_TYPE_x for type of device
105  * @param dev - device index of particular type
106  * @param start - starting block number
107  * @param blkcnt - number of blocks to read
108  * @param blksz - size in bytes of each block
109  * @param buf - buffer to contain cached data
110  *
111  * @return - '1' if block returned from cache, '0' otherwise.
112  */
113 int blkcache_read(int iftype, int dev,
114 		  lbaint_t start, lbaint_t blkcnt,
115 		  unsigned long blksz, void *buffer);
116 
117 /**
118  * blkcache_fill() - make data read from a block device available
119  * to the block cache
120  *
121  * @param iftype - IF_TYPE_x for type of device
122  * @param dev - device index of particular type
123  * @param start - starting block number
124  * @param blkcnt - number of blocks available
125  * @param blksz - size in bytes of each block
126  * @param buf - buffer containing data to cache
127  *
128  */
129 void blkcache_fill(int iftype, int dev,
130 		   lbaint_t start, lbaint_t blkcnt,
131 		   unsigned long blksz, void const *buffer);
132 
133 /**
134  * blkcache_invalidate() - discard the cache for a set of blocks
135  * because of a write or device (re)initialization.
136  *
137  * @param iftype - IF_TYPE_x for type of device
138  * @param dev - device index of particular type
139  */
140 void blkcache_invalidate(int iftype, int dev);
141 
142 /**
143  * blkcache_configure() - configure block cache
144  *
145  * @param blocks - maximum blocks per entry
146  * @param entries - maximum entries in cache
147  */
148 void blkcache_configure(unsigned blocks, unsigned entries);
149 
150 /*
151  * statistics of the block cache
152  */
153 struct block_cache_stats {
154 	unsigned hits;
155 	unsigned misses;
156 	unsigned entries; /* current entry count */
157 	unsigned max_blocks_per_entry;
158 	unsigned max_entries;
159 };
160 
161 /**
162  * get_blkcache_stats() - return statistics and reset
163  *
164  * @param stats - statistics are copied here
165  */
166 void blkcache_stats(struct block_cache_stats *stats);
167 
168 #else
169 
170 static inline int blkcache_read(int iftype, int dev,
171 				lbaint_t start, lbaint_t blkcnt,
172 				unsigned long blksz, void *buffer)
173 {
174 	return 0;
175 }
176 
177 static inline void blkcache_fill(int iftype, int dev,
178 				 lbaint_t start, lbaint_t blkcnt,
179 				 unsigned long blksz, void const *buffer) {}
180 
181 static inline void blkcache_invalidate(int iftype, int dev) {}
182 
183 #endif
184 
185 #if CONFIG_IS_ENABLED(BLK)
186 struct udevice;
187 
188 /* Operations on block devices */
189 struct blk_ops {
190 	/**
191 	 * read() - read from a block device
192 	 *
193 	 * @dev:	Device to read from
194 	 * @start:	Start block number to read (0=first)
195 	 * @blkcnt:	Number of blocks to read
196 	 * @buffer:	Destination buffer for data read
197 	 * @return number of blocks read, or -ve error number (see the
198 	 * IS_ERR_VALUE() macro
199 	 */
200 	unsigned long (*read)(struct udevice *dev, lbaint_t start,
201 			      lbaint_t blkcnt, void *buffer);
202 
203 	/**
204 	 * write() - write to a block device
205 	 *
206 	 * @dev:	Device to write to
207 	 * @start:	Start block number to write (0=first)
208 	 * @blkcnt:	Number of blocks to write
209 	 * @buffer:	Source buffer for data to write
210 	 * @return number of blocks written, or -ve error number (see the
211 	 * IS_ERR_VALUE() macro
212 	 */
213 	unsigned long (*write)(struct udevice *dev, lbaint_t start,
214 			       lbaint_t blkcnt, const void *buffer);
215 
216 	/**
217 	 * erase() - erase a section of a block device
218 	 *
219 	 * @dev:	Device to (partially) erase
220 	 * @start:	Start block number to erase (0=first)
221 	 * @blkcnt:	Number of blocks to erase
222 	 * @return number of blocks erased, or -ve error number (see the
223 	 * IS_ERR_VALUE() macro
224 	 */
225 	unsigned long (*erase)(struct udevice *dev, lbaint_t start,
226 			       lbaint_t blkcnt);
227 
228 	/**
229 	 * select_hwpart() - select a particular hardware partition
230 	 *
231 	 * Some devices (e.g. MMC) can support partitioning at the hardware
232 	 * level. This is quite separate from the normal idea of
233 	 * software-based partitions. MMC hardware partitions must be
234 	 * explicitly selected. Once selected only the region of the device
235 	 * covered by that partition is accessible.
236 	 *
237 	 * The MMC standard provides for two boot partitions (numbered 1 and 2),
238 	 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
239 	 *
240 	 * @desc:	Block device to update
241 	 * @hwpart:	Hardware partition number to select. 0 means the raw
242 	 *		device, 1 is the first partition, 2 is the second, etc.
243 	 * @return 0 if OK, -ve on error
244 	 */
245 	int (*select_hwpart)(struct udevice *dev, int hwpart);
246 };
247 
248 #define blk_get_ops(dev)	((struct blk_ops *)(dev)->driver->ops)
249 
250 /*
251  * These functions should take struct udevice instead of struct blk_desc,
252  * but this is convenient for migration to driver model. Add a 'd' prefix
253  * to the function operations, so that blk_read(), etc. can be reserved for
254  * functions with the correct arguments.
255  */
256 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
257 			lbaint_t blkcnt, void *buffer);
258 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
259 			 lbaint_t blkcnt, const void *buffer);
260 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
261 			 lbaint_t blkcnt);
262 
263 /**
264  * blk_find_device() - Find a block device
265  *
266  * This function does not activate the device. The device will be returned
267  * whether or not it is activated.
268  *
269  * @if_type:	Interface type (enum if_type_t)
270  * @devnum:	Device number (specific to each interface type)
271  * @devp:	the device, if found
272  * @return 0 if found, -ENODEV if no device found, or other -ve error value
273  */
274 int blk_find_device(int if_type, int devnum, struct udevice **devp);
275 
276 /**
277  * blk_get_device() - Find and probe a block device ready for use
278  *
279  * @if_type:	Interface type (enum if_type_t)
280  * @devnum:	Device number (specific to each interface type)
281  * @devp:	the device, if found
282  * @return 0 if found, -ENODEV if no device found, or other -ve error value
283  */
284 int blk_get_device(int if_type, int devnum, struct udevice **devp);
285 
286 /**
287  * blk_first_device() - Find the first device for a given interface
288  *
289  * The device is probed ready for use
290  *
291  * @devnum:	Device number (specific to each interface type)
292  * @devp:	the device, if found
293  * @return 0 if found, -ENODEV if no device, or other -ve error value
294  */
295 int blk_first_device(int if_type, struct udevice **devp);
296 
297 /**
298  * blk_next_device() - Find the next device for a given interface
299  *
300  * This can be called repeatedly after blk_first_device() to iterate through
301  * all devices of the given interface type.
302  *
303  * The device is probed ready for use
304  *
305  * @devp:	On entry, the previous device returned. On exit, the next
306  *		device, if found
307  * @return 0 if found, -ENODEV if no device, or other -ve error value
308  */
309 int blk_next_device(struct udevice **devp);
310 
311 /**
312  * blk_create_device() - Create a new block device
313  *
314  * @parent:	Parent of the new device
315  * @drv_name:	Driver name to use for the block device
316  * @name:	Name for the device
317  * @if_type:	Interface type (enum if_type_t)
318  * @devnum:	Device number, specific to the interface type, or -1 to
319  *		allocate the next available number
320  * @blksz:	Block size of the device in bytes (typically 512)
321  * @size:	Total size of the device in bytes
322  * @devp:	the new device (which has not been probed)
323  */
324 int blk_create_device(struct udevice *parent, const char *drv_name,
325 		      const char *name, int if_type, int devnum, int blksz,
326 		      lbaint_t size, struct udevice **devp);
327 
328 /**
329  * blk_create_devicef() - Create a new named block device
330  *
331  * @parent:	Parent of the new device
332  * @drv_name:	Driver name to use for the block device
333  * @name:	Name for the device (parent name is prepended)
334  * @if_type:	Interface type (enum if_type_t)
335  * @devnum:	Device number, specific to the interface type, or -1 to
336  *		allocate the next available number
337  * @blksz:	Block size of the device in bytes (typically 512)
338  * @size:	Total size of the device in bytes
339  * @devp:	the new device (which has not been probed)
340  */
341 int blk_create_devicef(struct udevice *parent, const char *drv_name,
342 		       const char *name, int if_type, int devnum, int blksz,
343 		       lbaint_t size, struct udevice **devp);
344 
345 /**
346  * blk_prepare_device() - Prepare a block device for use
347  *
348  * This reads partition information from the device if supported.
349  *
350  * @dev:	Device to prepare
351  * @return 0 if ok, -ve on error
352  */
353 int blk_prepare_device(struct udevice *dev);
354 
355 /**
356  * blk_unbind_all() - Unbind all device of the given interface type
357  *
358  * The devices are removed and then unbound.
359  *
360  * @if_type:	Interface type to unbind
361  * @return 0 if OK, -ve on error
362  */
363 int blk_unbind_all(int if_type);
364 
365 /**
366  * blk_find_max_devnum() - find the maximum device number for an interface type
367  *
368  * Finds the last allocated device number for an interface type @if_type. The
369  * next number is safe to use for a newly allocated device.
370  *
371  * @if_type:	Interface type to scan
372  * @return maximum device number found, or -ENODEV if none, or other -ve on
373  * error
374  */
375 int blk_find_max_devnum(enum if_type if_type);
376 
377 /**
378  * blk_select_hwpart() - select a hardware partition
379  *
380  * Select a hardware partition if the device supports it (typically MMC does)
381  *
382  * @dev:	Device to update
383  * @hwpart:	Partition number to select
384  * @return 0 if OK, -ve on error
385  */
386 int blk_select_hwpart(struct udevice *dev, int hwpart);
387 
388 /**
389  * blk_get_from_parent() - obtain a block device by looking up its parent
390  *
391  * All devices with
392  */
393 int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
394 
395 #else
396 #include <errno.h>
397 /*
398  * These functions should take struct udevice instead of struct blk_desc,
399  * but this is convenient for migration to driver model. Add a 'd' prefix
400  * to the function operations, so that blk_read(), etc. can be reserved for
401  * functions with the correct arguments.
402  */
403 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
404 			      lbaint_t blkcnt, void *buffer)
405 {
406 	ulong blks_read;
407 	if (blkcache_read(block_dev->if_type, block_dev->devnum,
408 			  start, blkcnt, block_dev->blksz, buffer))
409 		return blkcnt;
410 
411 	/*
412 	 * We could check if block_read is NULL and return -ENOSYS. But this
413 	 * bloats the code slightly (cause some board to fail to build), and
414 	 * it would be an error to try an operation that does not exist.
415 	 */
416 	blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
417 	if (blks_read == blkcnt)
418 		blkcache_fill(block_dev->if_type, block_dev->devnum,
419 			      start, blkcnt, block_dev->blksz, buffer);
420 
421 	return blks_read;
422 }
423 
424 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
425 			       lbaint_t blkcnt, const void *buffer)
426 {
427 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
428 	return block_dev->block_write(block_dev, start, blkcnt, buffer);
429 }
430 
431 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
432 			       lbaint_t blkcnt)
433 {
434 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
435 	return block_dev->block_erase(block_dev, start, blkcnt);
436 }
437 
438 /**
439  * struct blk_driver - Driver for block interface types
440  *
441  * This provides access to the block devices for each interface type. One
442  * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
443  * type that is to be supported.
444  *
445  * @if_typename:	Interface type name
446  * @if_type:		Interface type
447  * @max_devs:		Maximum number of devices supported
448  * @desc:		Pointer to list of devices for this interface type,
449  *			or NULL to use @get_dev() instead
450  */
451 struct blk_driver {
452 	const char *if_typename;
453 	enum if_type if_type;
454 	int max_devs;
455 	struct blk_desc *desc;
456 	/**
457 	 * get_dev() - get a pointer to a block device given its number
458 	 *
459 	 * Each interface allocates its own devices and typically
460 	 * struct blk_desc is contained with the interface's data structure.
461 	 * There is no global numbering for block devices. This method allows
462 	 * the device for an interface type to be obtained when @desc is NULL.
463 	 *
464 	 * @devnum:	Device number (0 for first device on that interface,
465 	 *		1 for second, etc.
466 	 * @descp:	Returns pointer to the block device on success
467 	 * @return 0 if OK, -ve on error
468 	 */
469 	int (*get_dev)(int devnum, struct blk_desc **descp);
470 
471 	/**
472 	 * select_hwpart() - Select a hardware partition
473 	 *
474 	 * Some devices (e.g. MMC) can support partitioning at the hardware
475 	 * level. This is quite separate from the normal idea of
476 	 * software-based partitions. MMC hardware partitions must be
477 	 * explicitly selected. Once selected only the region of the device
478 	 * covered by that partition is accessible.
479 	 *
480 	 * The MMC standard provides for two boot partitions (numbered 1 and 2),
481 	 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
482 	 * Partition 0 is the main user-data partition.
483 	 *
484 	 * @desc:	Block device descriptor
485 	 * @hwpart:	Hardware partition number to select. 0 means the main
486 	 *		user-data partition, 1 is the first partition, 2 is
487 	 *		the second, etc.
488 	 * @return 0 if OK, other value for an error
489 	 */
490 	int (*select_hwpart)(struct blk_desc *desc, int hwpart);
491 };
492 
493 /*
494  * Declare a new U-Boot legacy block driver. New drivers should use driver
495  * model (UCLASS_BLK).
496  */
497 #define U_BOOT_LEGACY_BLK(__name)					\
498 	ll_entry_declare(struct blk_driver, __name, blk_driver)
499 
500 struct blk_driver *blk_driver_lookup_type(int if_type);
501 
502 #endif /* !CONFIG_BLK */
503 
504 /**
505  * blk_get_devnum_by_typename() - Get a block device by type and number
506  *
507  * This looks through the available block devices of the given type, returning
508  * the one with the given @devnum.
509  *
510  * @if_type:	Block device type
511  * @devnum:	Device number
512  * @return point to block device descriptor, or NULL if not found
513  */
514 struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
515 
516 /**
517  * blk_get_devnum_by_type() - Get a block device by type name, and number
518  *
519  * This looks up the block device type based on @if_typename, then calls
520  * blk_get_devnum_by_type().
521  *
522  * @if_typename:	Block device type name
523  * @devnum:		Device number
524  * @return point to block device descriptor, or NULL if not found
525  */
526 struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
527 					    int devnum);
528 
529 /**
530  * blk_dselect_hwpart() - select a hardware partition
531  *
532  * This selects a hardware partition (such as is supported by MMC). The block
533  * device size may change as this effectively points the block device to a
534  * partition at the hardware level. See the select_hwpart() method above.
535  *
536  * @desc:	Block device descriptor for the device to select
537  * @hwpart:	Partition number to select
538  * @return 0 if OK, -ve on error
539  */
540 int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
541 
542 /**
543  * blk_list_part() - list the partitions for block devices of a given type
544  *
545  * This looks up the partition type for each block device of type @if_type,
546  * then displays a list of partitions.
547  *
548  * @if_type:	Block device type
549  * @return 0 if OK, -ENODEV if there is none of that type
550  */
551 int blk_list_part(enum if_type if_type);
552 
553 /**
554  * blk_list_devices() - list the block devices of a given type
555  *
556  * This lists each block device of the type @if_type, showing the capacity
557  * as well as type-specific information.
558  *
559  * @if_type:	Block device type
560  */
561 void blk_list_devices(enum if_type if_type);
562 
563 /**
564  * blk_show_device() - show information about a given block device
565  *
566  * This shows the block device capacity as well as type-specific information.
567  *
568  * @if_type:	Block device type
569  * @devnum:	Device number
570  * @return 0 if OK, -ENODEV for invalid device number
571  */
572 int blk_show_device(enum if_type if_type, int devnum);
573 
574 /**
575  * blk_print_device_num() - show information about a given block device
576  *
577  * This is similar to blk_show_device() but returns an error if the block
578  * device type is unknown.
579  *
580  * @if_type:	Block device type
581  * @devnum:	Device number
582  * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
583  * device is not connected
584  */
585 int blk_print_device_num(enum if_type if_type, int devnum);
586 
587 /**
588  * blk_print_part_devnum() - print the partition information for a device
589  *
590  * @if_type:	Block device type
591  * @devnum:	Device number
592  * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
593  * the interface type is not supported, other -ve on other error
594  */
595 int blk_print_part_devnum(enum if_type if_type, int devnum);
596 
597 /**
598  * blk_read_devnum() - read blocks from a device
599  *
600  * @if_type:	Block device type
601  * @devnum:	Device number
602  * @blkcnt:	Number of blocks to read
603  * @buffer:	Address to write data to
604  * @return number of blocks read, or -ve error number on error
605  */
606 ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
607 		      lbaint_t blkcnt, void *buffer);
608 
609 /**
610  * blk_write_devnum() - write blocks to a device
611  *
612  * @if_type:	Block device type
613  * @devnum:	Device number
614  * @blkcnt:	Number of blocks to write
615  * @buffer:	Address to read data from
616  * @return number of blocks written, or -ve error number on error
617  */
618 ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
619 		       lbaint_t blkcnt, const void *buffer);
620 
621 /**
622  * blk_select_hwpart_devnum() - select a hardware partition
623  *
624  * This is similar to blk_dselect_hwpart() but it looks up the interface and
625  * device number.
626  *
627  * @if_type:	Block device type
628  * @devnum:	Device number
629  * @hwpart:	Partition number to select
630  * @return 0 if OK, -ve on error
631  */
632 int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
633 
634 /**
635  * blk_get_if_type_name() - Get the name of an interface type
636  *
637  * @if_type: Interface type to check
638  * @return name of interface, or NULL if none
639  */
640 const char *blk_get_if_type_name(enum if_type if_type);
641 
642 /**
643  * blk_common_cmd() - handle common commands with block devices
644  *
645  * @args: Number of arguments to the command (argv[0] is the command itself)
646  * @argv: Command arguments
647  * @if_type: Interface type
648  * @cur_devnump: Current device number for this interface type
649  * @return 0 if OK, CMD_RET_ERROR on error
650  */
651 int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,
652 		   int *cur_devnump);
653 
654 #endif
655