xref: /rk3399_rockchip-uboot/include/blk.h (revision 1f3b6bbf4e675a2f73c7f0aeb8837f1c71b2e2f8)
11a73661bSSimon Glass /*
21a73661bSSimon Glass  * (C) Copyright 2000-2004
31a73661bSSimon Glass  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
41a73661bSSimon Glass  *
51a73661bSSimon Glass  * SPDX-License-Identifier:	GPL-2.0+
61a73661bSSimon Glass  */
71a73661bSSimon Glass 
81a73661bSSimon Glass #ifndef BLK_H
91a73661bSSimon Glass #define BLK_H
101a73661bSSimon Glass 
11e9aba365SPeter Jones #include <efi.h>
12e9aba365SPeter Jones 
131a73661bSSimon Glass #ifdef CONFIG_SYS_64BIT_LBA
141a73661bSSimon Glass typedef uint64_t lbaint_t;
151a73661bSSimon Glass #define LBAFlength "ll"
161a73661bSSimon Glass #else
171a73661bSSimon Glass typedef ulong lbaint_t;
181a73661bSSimon Glass #define LBAFlength "l"
191a73661bSSimon Glass #endif
201a73661bSSimon Glass #define LBAF "%" LBAFlength "x"
211a73661bSSimon Glass #define LBAFU "%" LBAFlength "u"
221a73661bSSimon Glass 
231a73661bSSimon Glass /* Interface types: */
245ec4f1a5SSimon Glass enum if_type {
255ec4f1a5SSimon Glass 	IF_TYPE_UNKNOWN = 0,
265ec4f1a5SSimon Glass 	IF_TYPE_IDE,
275ec4f1a5SSimon Glass 	IF_TYPE_SCSI,
285ec4f1a5SSimon Glass 	IF_TYPE_ATAPI,
295ec4f1a5SSimon Glass 	IF_TYPE_USB,
305ec4f1a5SSimon Glass 	IF_TYPE_DOC,
315ec4f1a5SSimon Glass 	IF_TYPE_MMC,
325ec4f1a5SSimon Glass 	IF_TYPE_SD,
335ec4f1a5SSimon Glass 	IF_TYPE_SATA,
345ec4f1a5SSimon Glass 	IF_TYPE_HOST,
353ef85e37SSimon Glass 	IF_TYPE_SYSTEMACE,
36ffab6945SZhikang Zhang 	IF_TYPE_NVME,
37441217e3SZhaoyifeng 	IF_TYPE_RKNAND,
385bd6dc27SKever Yang 	IF_TYPE_SPINAND,
395bd6dc27SKever Yang 	IF_TYPE_SPINOR,
40a3fec70dSJoseph Chen 	IF_TYPE_RAMDISK,
41054229abSJason Zhu 	IF_TYPE_MTD,
425ec4f1a5SSimon Glass 	IF_TYPE_COUNT,			/* Number of interface types */
435ec4f1a5SSimon Glass };
441a73661bSSimon Glass 
45054229abSJason Zhu /* define mtd device devnum */
46054229abSJason Zhu #define BLK_MTD_NAND		0
47054229abSJason Zhu #define BLK_MTD_SPI_NAND	1
48054229abSJason Zhu #define BLK_MTD_SPI_NOR		2
49054229abSJason Zhu 
50eb81b1a4SBin Meng #define BLK_VEN_SIZE		40
51eb81b1a4SBin Meng #define BLK_PRD_SIZE		20
52eb81b1a4SBin Meng #define BLK_REV_SIZE		8
53eb81b1a4SBin Meng 
54*1f3b6bbfSJason Zhu /* define block device operation flags */
55*1f3b6bbfSJason Zhu #define BLK_PRE_RW		1	/* Block prepare read & write*/
56*1f3b6bbfSJason Zhu #define BLK_MTD_NBA_RW		2	/* MTD block non-block-aligned read & write */
57*1f3b6bbfSJason Zhu 
5809d71aacSSimon Glass /*
59e9aba365SPeter Jones  * Identifies the partition table type (ie. MBR vs GPT GUID) signature
60e9aba365SPeter Jones  */
61e9aba365SPeter Jones enum sig_type {
62e9aba365SPeter Jones 	SIG_TYPE_NONE,
63e9aba365SPeter Jones 	SIG_TYPE_MBR,
64e9aba365SPeter Jones 	SIG_TYPE_GUID,
65e9aba365SPeter Jones 
66e9aba365SPeter Jones 	SIG_TYPE_COUNT			/* Number of signature types */
67e9aba365SPeter Jones };
68e9aba365SPeter Jones 
69e9aba365SPeter Jones /*
7009d71aacSSimon Glass  * With driver model (CONFIG_BLK) this is uclass platform data, accessible
7109d71aacSSimon Glass  * with dev_get_uclass_platdata(dev)
7209d71aacSSimon Glass  */
731a73661bSSimon Glass struct blk_desc {
7409d71aacSSimon Glass 	/*
7509d71aacSSimon Glass 	 * TODO: With driver model we should be able to use the parent
7609d71aacSSimon Glass 	 * device's uclass instead.
7709d71aacSSimon Glass 	 */
785ec4f1a5SSimon Glass 	enum if_type	if_type;	/* type of the interface */
79bcce53d0SSimon Glass 	int		devnum;		/* device number */
801a73661bSSimon Glass 	unsigned char	part_type;	/* partition type */
811a73661bSSimon Glass 	unsigned char	target;		/* target SCSI ID */
821a73661bSSimon Glass 	unsigned char	lun;		/* target LUN */
831a73661bSSimon Glass 	unsigned char	hwpart;		/* HW partition, e.g. for eMMC */
841a73661bSSimon Glass 	unsigned char	type;		/* device type */
851a73661bSSimon Glass 	unsigned char	removable;	/* removable device */
86*1f3b6bbfSJason Zhu 	unsigned char	op_flag;	/* Some special operation flags */
871a73661bSSimon Glass #ifdef CONFIG_LBA48
881a73661bSSimon Glass 	/* device can use 48bit addr (ATA/ATAPI v7) */
891a73661bSSimon Glass 	unsigned char	lba48;
901a73661bSSimon Glass #endif
911a73661bSSimon Glass 	lbaint_t	lba;		/* number of blocks */
921a73661bSSimon Glass 	unsigned long	blksz;		/* block size */
931a73661bSSimon Glass 	int		log2blksz;	/* for convenience: log2(blksz) */
94eb81b1a4SBin Meng 	char		vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
95eb81b1a4SBin Meng 	char		product[BLK_PRD_SIZE + 1]; /* device product number */
96eb81b1a4SBin Meng 	char		revision[BLK_REV_SIZE + 1]; /* firmware revision */
97e9aba365SPeter Jones 	enum sig_type	sig_type;	/* Partition table signature type */
98e9aba365SPeter Jones 	union {
99e9aba365SPeter Jones 		uint32_t mbr_sig;	/* MBR integer signature */
100e9aba365SPeter Jones 		efi_guid_t guid_sig;	/* GPT GUID Signature */
101e9aba365SPeter Jones 	};
102c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(BLK)
103b6694a33SSimon Glass 	/*
104b6694a33SSimon Glass 	 * For now we have a few functions which take struct blk_desc as a
105b6694a33SSimon Glass 	 * parameter. This field allows them to look up the associated
106b6694a33SSimon Glass 	 * device. Once these functions are removed we can drop this field.
107b6694a33SSimon Glass 	 */
10809d71aacSSimon Glass 	struct udevice *bdev;
10909d71aacSSimon Glass #else
1101a73661bSSimon Glass 	unsigned long	(*block_read)(struct blk_desc *block_dev,
1111a73661bSSimon Glass 				      lbaint_t start,
1121a73661bSSimon Glass 				      lbaint_t blkcnt,
1131a73661bSSimon Glass 				      void *buffer);
1141a73661bSSimon Glass 	unsigned long	(*block_write)(struct blk_desc *block_dev,
1151a73661bSSimon Glass 				       lbaint_t start,
1161a73661bSSimon Glass 				       lbaint_t blkcnt,
1171a73661bSSimon Glass 				       const void *buffer);
1181a73661bSSimon Glass 	unsigned long	(*block_erase)(struct blk_desc *block_dev,
1191a73661bSSimon Glass 				       lbaint_t start,
1201a73661bSSimon Glass 				       lbaint_t blkcnt);
1211a73661bSSimon Glass 	void		*priv;		/* driver private struct pointer */
12209d71aacSSimon Glass #endif
1231a73661bSSimon Glass };
1241a73661bSSimon Glass 
1251a73661bSSimon Glass #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
1261a73661bSSimon Glass #define PAD_TO_BLOCKSIZE(size, blk_desc) \
1271a73661bSSimon Glass 	(PAD_SIZE(size, blk_desc->blksz))
1281a73661bSSimon Glass 
129e40cf34aSEric Nelson #ifdef CONFIG_BLOCK_CACHE
130e40cf34aSEric Nelson /**
131e40cf34aSEric Nelson  * blkcache_read() - attempt to read a set of blocks from cache
132e40cf34aSEric Nelson  *
133e40cf34aSEric Nelson  * @param iftype - IF_TYPE_x for type of device
134e40cf34aSEric Nelson  * @param dev - device index of particular type
135e40cf34aSEric Nelson  * @param start - starting block number
136e40cf34aSEric Nelson  * @param blkcnt - number of blocks to read
137e40cf34aSEric Nelson  * @param blksz - size in bytes of each block
138e40cf34aSEric Nelson  * @param buf - buffer to contain cached data
139e40cf34aSEric Nelson  *
140e40cf34aSEric Nelson  * @return - '1' if block returned from cache, '0' otherwise.
141e40cf34aSEric Nelson  */
142c8e4d2a8SEric Nelson int blkcache_read(int iftype, int dev,
143e40cf34aSEric Nelson 		  lbaint_t start, lbaint_t blkcnt,
144e40cf34aSEric Nelson 		  unsigned long blksz, void *buffer);
145e40cf34aSEric Nelson 
146e40cf34aSEric Nelson /**
147e40cf34aSEric Nelson  * blkcache_fill() - make data read from a block device available
148e40cf34aSEric Nelson  * to the block cache
149e40cf34aSEric Nelson  *
150e40cf34aSEric Nelson  * @param iftype - IF_TYPE_x for type of device
151e40cf34aSEric Nelson  * @param dev - device index of particular type
152e40cf34aSEric Nelson  * @param start - starting block number
153e40cf34aSEric Nelson  * @param blkcnt - number of blocks available
154e40cf34aSEric Nelson  * @param blksz - size in bytes of each block
155e40cf34aSEric Nelson  * @param buf - buffer containing data to cache
156e40cf34aSEric Nelson  *
157e40cf34aSEric Nelson  */
158c8e4d2a8SEric Nelson void blkcache_fill(int iftype, int dev,
159e40cf34aSEric Nelson 		   lbaint_t start, lbaint_t blkcnt,
160e40cf34aSEric Nelson 		   unsigned long blksz, void const *buffer);
161e40cf34aSEric Nelson 
162e40cf34aSEric Nelson /**
163e40cf34aSEric Nelson  * blkcache_invalidate() - discard the cache for a set of blocks
164e40cf34aSEric Nelson  * because of a write or device (re)initialization.
165e40cf34aSEric Nelson  *
166e40cf34aSEric Nelson  * @param iftype - IF_TYPE_x for type of device
167e40cf34aSEric Nelson  * @param dev - device index of particular type
168e40cf34aSEric Nelson  */
169c8e4d2a8SEric Nelson void blkcache_invalidate(int iftype, int dev);
170e40cf34aSEric Nelson 
171e40cf34aSEric Nelson /**
172e40cf34aSEric Nelson  * blkcache_configure() - configure block cache
173e40cf34aSEric Nelson  *
174e40cf34aSEric Nelson  * @param blocks - maximum blocks per entry
175e40cf34aSEric Nelson  * @param entries - maximum entries in cache
176e40cf34aSEric Nelson  */
177e40cf34aSEric Nelson void blkcache_configure(unsigned blocks, unsigned entries);
178e40cf34aSEric Nelson 
179e40cf34aSEric Nelson /*
180e40cf34aSEric Nelson  * statistics of the block cache
181e40cf34aSEric Nelson  */
182e40cf34aSEric Nelson struct block_cache_stats {
183e40cf34aSEric Nelson 	unsigned hits;
184e40cf34aSEric Nelson 	unsigned misses;
185e40cf34aSEric Nelson 	unsigned entries; /* current entry count */
186e40cf34aSEric Nelson 	unsigned max_blocks_per_entry;
187e40cf34aSEric Nelson 	unsigned max_entries;
188e40cf34aSEric Nelson };
189e40cf34aSEric Nelson 
190e40cf34aSEric Nelson /**
191e40cf34aSEric Nelson  * get_blkcache_stats() - return statistics and reset
192e40cf34aSEric Nelson  *
193e40cf34aSEric Nelson  * @param stats - statistics are copied here
194e40cf34aSEric Nelson  */
195e40cf34aSEric Nelson void blkcache_stats(struct block_cache_stats *stats);
196e40cf34aSEric Nelson 
197e40cf34aSEric Nelson #else
198e40cf34aSEric Nelson 
199c8e4d2a8SEric Nelson static inline int blkcache_read(int iftype, int dev,
200e40cf34aSEric Nelson 				lbaint_t start, lbaint_t blkcnt,
201e40cf34aSEric Nelson 				unsigned long blksz, void *buffer)
202e40cf34aSEric Nelson {
203e40cf34aSEric Nelson 	return 0;
204e40cf34aSEric Nelson }
205e40cf34aSEric Nelson 
206c8e4d2a8SEric Nelson static inline void blkcache_fill(int iftype, int dev,
207e40cf34aSEric Nelson 				 lbaint_t start, lbaint_t blkcnt,
208e40cf34aSEric Nelson 				 unsigned long blksz, void const *buffer) {}
209e40cf34aSEric Nelson 
210c8e4d2a8SEric Nelson static inline void blkcache_invalidate(int iftype, int dev) {}
211e40cf34aSEric Nelson 
212e40cf34aSEric Nelson #endif
213e40cf34aSEric Nelson 
214c4d660d4SSimon Glass #if CONFIG_IS_ENABLED(BLK)
21509d71aacSSimon Glass struct udevice;
21609d71aacSSimon Glass 
21709d71aacSSimon Glass /* Operations on block devices */
21809d71aacSSimon Glass struct blk_ops {
21909d71aacSSimon Glass 	/**
22009d71aacSSimon Glass 	 * read() - read from a block device
22109d71aacSSimon Glass 	 *
22209d71aacSSimon Glass 	 * @dev:	Device to read from
22309d71aacSSimon Glass 	 * @start:	Start block number to read (0=first)
22409d71aacSSimon Glass 	 * @blkcnt:	Number of blocks to read
22509d71aacSSimon Glass 	 * @buffer:	Destination buffer for data read
22609d71aacSSimon Glass 	 * @return number of blocks read, or -ve error number (see the
22709d71aacSSimon Glass 	 * IS_ERR_VALUE() macro
22809d71aacSSimon Glass 	 */
22909d71aacSSimon Glass 	unsigned long (*read)(struct udevice *dev, lbaint_t start,
23009d71aacSSimon Glass 			      lbaint_t blkcnt, void *buffer);
23109d71aacSSimon Glass 
23209d71aacSSimon Glass 	/**
23347f7fd3aSJason Zhu 	 * read_prepare() - read from a block device
23447f7fd3aSJason Zhu 	 *
23547f7fd3aSJason Zhu 	 * @dev:	Device to read from
23647f7fd3aSJason Zhu 	 * @start:	Start block number to read (0=first)
23747f7fd3aSJason Zhu 	 * @blkcnt:	Number of blocks to read
23847f7fd3aSJason Zhu 	 * @buffer:	Destination buffer for data read
23947f7fd3aSJason Zhu 	 * @return number of blocks read, or -ve error number (see the
24047f7fd3aSJason Zhu 	 * IS_ERR_VALUE() macro
24147f7fd3aSJason Zhu 	 */
24247f7fd3aSJason Zhu #ifdef CONFIG_SPL_BLK_READ_PREPARE
24347f7fd3aSJason Zhu 	unsigned long (*read_prepare)(struct udevice *dev, lbaint_t start,
24447f7fd3aSJason Zhu 				      lbaint_t blkcnt, void *buffer);
24547f7fd3aSJason Zhu #endif
24647f7fd3aSJason Zhu 	/**
24709d71aacSSimon Glass 	 * write() - write to a block device
24809d71aacSSimon Glass 	 *
24909d71aacSSimon Glass 	 * @dev:	Device to write to
25009d71aacSSimon Glass 	 * @start:	Start block number to write (0=first)
25109d71aacSSimon Glass 	 * @blkcnt:	Number of blocks to write
25209d71aacSSimon Glass 	 * @buffer:	Source buffer for data to write
25309d71aacSSimon Glass 	 * @return number of blocks written, or -ve error number (see the
25409d71aacSSimon Glass 	 * IS_ERR_VALUE() macro
25509d71aacSSimon Glass 	 */
25609d71aacSSimon Glass 	unsigned long (*write)(struct udevice *dev, lbaint_t start,
25709d71aacSSimon Glass 			       lbaint_t blkcnt, const void *buffer);
25809d71aacSSimon Glass 
25909d71aacSSimon Glass 	/**
26009d71aacSSimon Glass 	 * erase() - erase a section of a block device
26109d71aacSSimon Glass 	 *
26209d71aacSSimon Glass 	 * @dev:	Device to (partially) erase
26309d71aacSSimon Glass 	 * @start:	Start block number to erase (0=first)
26409d71aacSSimon Glass 	 * @blkcnt:	Number of blocks to erase
26509d71aacSSimon Glass 	 * @return number of blocks erased, or -ve error number (see the
26609d71aacSSimon Glass 	 * IS_ERR_VALUE() macro
26709d71aacSSimon Glass 	 */
26809d71aacSSimon Glass 	unsigned long (*erase)(struct udevice *dev, lbaint_t start,
26909d71aacSSimon Glass 			       lbaint_t blkcnt);
270cd0fb55bSSimon Glass 
271cd0fb55bSSimon Glass 	/**
272cd0fb55bSSimon Glass 	 * select_hwpart() - select a particular hardware partition
273cd0fb55bSSimon Glass 	 *
274cd0fb55bSSimon Glass 	 * Some devices (e.g. MMC) can support partitioning at the hardware
275cd0fb55bSSimon Glass 	 * level. This is quite separate from the normal idea of
276cd0fb55bSSimon Glass 	 * software-based partitions. MMC hardware partitions must be
277cd0fb55bSSimon Glass 	 * explicitly selected. Once selected only the region of the device
278cd0fb55bSSimon Glass 	 * covered by that partition is accessible.
279cd0fb55bSSimon Glass 	 *
280cd0fb55bSSimon Glass 	 * The MMC standard provides for two boot partitions (numbered 1 and 2),
281cd0fb55bSSimon Glass 	 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
282cd0fb55bSSimon Glass 	 *
283cd0fb55bSSimon Glass 	 * @desc:	Block device to update
284cd0fb55bSSimon Glass 	 * @hwpart:	Hardware partition number to select. 0 means the raw
285cd0fb55bSSimon Glass 	 *		device, 1 is the first partition, 2 is the second, etc.
286cd0fb55bSSimon Glass 	 * @return 0 if OK, -ve on error
287cd0fb55bSSimon Glass 	 */
288cd0fb55bSSimon Glass 	int (*select_hwpart)(struct udevice *dev, int hwpart);
28909d71aacSSimon Glass };
29009d71aacSSimon Glass 
29109d71aacSSimon Glass #define blk_get_ops(dev)	((struct blk_ops *)(dev)->driver->ops)
29209d71aacSSimon Glass 
29309d71aacSSimon Glass /*
29409d71aacSSimon Glass  * These functions should take struct udevice instead of struct blk_desc,
29509d71aacSSimon Glass  * but this is convenient for migration to driver model. Add a 'd' prefix
29609d71aacSSimon Glass  * to the function operations, so that blk_read(), etc. can be reserved for
29709d71aacSSimon Glass  * functions with the correct arguments.
29809d71aacSSimon Glass  */
29909d71aacSSimon Glass unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
30009d71aacSSimon Glass 			lbaint_t blkcnt, void *buffer);
30147f7fd3aSJason Zhu #ifdef CONFIG_SPL_BLK_READ_PREPARE
30247f7fd3aSJason Zhu unsigned long blk_dread_prepare(struct blk_desc *block_dev, lbaint_t start,
30347f7fd3aSJason Zhu 				lbaint_t blkcnt, void *buffer);
30447f7fd3aSJason Zhu #endif
30509d71aacSSimon Glass unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
30609d71aacSSimon Glass 			 lbaint_t blkcnt, const void *buffer);
30709d71aacSSimon Glass unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
30809d71aacSSimon Glass 			 lbaint_t blkcnt);
30909d71aacSSimon Glass 
31009d71aacSSimon Glass /**
3116139281aSSimon Glass  * blk_find_device() - Find a block device
3126139281aSSimon Glass  *
3136139281aSSimon Glass  * This function does not activate the device. The device will be returned
3146139281aSSimon Glass  * whether or not it is activated.
3156139281aSSimon Glass  *
3166139281aSSimon Glass  * @if_type:	Interface type (enum if_type_t)
3176139281aSSimon Glass  * @devnum:	Device number (specific to each interface type)
3186139281aSSimon Glass  * @devp:	the device, if found
3196139281aSSimon Glass  * @return 0 if found, -ENODEV if no device found, or other -ve error value
3206139281aSSimon Glass  */
3216139281aSSimon Glass int blk_find_device(int if_type, int devnum, struct udevice **devp);
3226139281aSSimon Glass 
3236139281aSSimon Glass /**
32409d71aacSSimon Glass  * blk_get_device() - Find and probe a block device ready for use
32509d71aacSSimon Glass  *
32609d71aacSSimon Glass  * @if_type:	Interface type (enum if_type_t)
32709d71aacSSimon Glass  * @devnum:	Device number (specific to each interface type)
32809d71aacSSimon Glass  * @devp:	the device, if found
3296139281aSSimon Glass  * @return 0 if found, -ENODEV if no device found, or other -ve error value
33009d71aacSSimon Glass  */
33109d71aacSSimon Glass int blk_get_device(int if_type, int devnum, struct udevice **devp);
33209d71aacSSimon Glass 
33309d71aacSSimon Glass /**
33409d71aacSSimon Glass  * blk_first_device() - Find the first device for a given interface
33509d71aacSSimon Glass  *
33609d71aacSSimon Glass  * The device is probed ready for use
33709d71aacSSimon Glass  *
33809d71aacSSimon Glass  * @devnum:	Device number (specific to each interface type)
33909d71aacSSimon Glass  * @devp:	the device, if found
34009d71aacSSimon Glass  * @return 0 if found, -ENODEV if no device, or other -ve error value
34109d71aacSSimon Glass  */
34209d71aacSSimon Glass int blk_first_device(int if_type, struct udevice **devp);
34309d71aacSSimon Glass 
34409d71aacSSimon Glass /**
34509d71aacSSimon Glass  * blk_next_device() - Find the next device for a given interface
34609d71aacSSimon Glass  *
34709d71aacSSimon Glass  * This can be called repeatedly after blk_first_device() to iterate through
34809d71aacSSimon Glass  * all devices of the given interface type.
34909d71aacSSimon Glass  *
35009d71aacSSimon Glass  * The device is probed ready for use
35109d71aacSSimon Glass  *
35209d71aacSSimon Glass  * @devp:	On entry, the previous device returned. On exit, the next
35309d71aacSSimon Glass  *		device, if found
35409d71aacSSimon Glass  * @return 0 if found, -ENODEV if no device, or other -ve error value
35509d71aacSSimon Glass  */
35609d71aacSSimon Glass int blk_next_device(struct udevice **devp);
35709d71aacSSimon Glass 
35809d71aacSSimon Glass /**
35909d71aacSSimon Glass  * blk_create_device() - Create a new block device
36009d71aacSSimon Glass  *
36109d71aacSSimon Glass  * @parent:	Parent of the new device
36209d71aacSSimon Glass  * @drv_name:	Driver name to use for the block device
36309d71aacSSimon Glass  * @name:	Name for the device
36409d71aacSSimon Glass  * @if_type:	Interface type (enum if_type_t)
36552138fd4SSimon Glass  * @devnum:	Device number, specific to the interface type, or -1 to
36652138fd4SSimon Glass  *		allocate the next available number
36709d71aacSSimon Glass  * @blksz:	Block size of the device in bytes (typically 512)
36809d71aacSSimon Glass  * @size:	Total size of the device in bytes
36909d71aacSSimon Glass  * @devp:	the new device (which has not been probed)
37009d71aacSSimon Glass  */
37109d71aacSSimon Glass int blk_create_device(struct udevice *parent, const char *drv_name,
37209d71aacSSimon Glass 		      const char *name, int if_type, int devnum, int blksz,
37309d71aacSSimon Glass 		      lbaint_t size, struct udevice **devp);
37409d71aacSSimon Glass 
37509d71aacSSimon Glass /**
3769107c973SSimon Glass  * blk_create_devicef() - Create a new named block device
3779107c973SSimon Glass  *
3789107c973SSimon Glass  * @parent:	Parent of the new device
3799107c973SSimon Glass  * @drv_name:	Driver name to use for the block device
3809107c973SSimon Glass  * @name:	Name for the device (parent name is prepended)
3819107c973SSimon Glass  * @if_type:	Interface type (enum if_type_t)
3829107c973SSimon Glass  * @devnum:	Device number, specific to the interface type, or -1 to
3839107c973SSimon Glass  *		allocate the next available number
3849107c973SSimon Glass  * @blksz:	Block size of the device in bytes (typically 512)
3859107c973SSimon Glass  * @size:	Total size of the device in bytes
3869107c973SSimon Glass  * @devp:	the new device (which has not been probed)
3879107c973SSimon Glass  */
3889107c973SSimon Glass int blk_create_devicef(struct udevice *parent, const char *drv_name,
3899107c973SSimon Glass 		       const char *name, int if_type, int devnum, int blksz,
3909107c973SSimon Glass 		       lbaint_t size, struct udevice **devp);
3919107c973SSimon Glass 
3929107c973SSimon Glass /**
39309d71aacSSimon Glass  * blk_prepare_device() - Prepare a block device for use
39409d71aacSSimon Glass  *
39509d71aacSSimon Glass  * This reads partition information from the device if supported.
39609d71aacSSimon Glass  *
39709d71aacSSimon Glass  * @dev:	Device to prepare
39809d71aacSSimon Glass  * @return 0 if ok, -ve on error
39909d71aacSSimon Glass  */
40009d71aacSSimon Glass int blk_prepare_device(struct udevice *dev);
40109d71aacSSimon Glass 
40209d71aacSSimon Glass /**
40309d71aacSSimon Glass  * blk_unbind_all() - Unbind all device of the given interface type
40409d71aacSSimon Glass  *
40509d71aacSSimon Glass  * The devices are removed and then unbound.
40609d71aacSSimon Glass  *
40709d71aacSSimon Glass  * @if_type:	Interface type to unbind
40809d71aacSSimon Glass  * @return 0 if OK, -ve on error
40909d71aacSSimon Glass  */
41009d71aacSSimon Glass int blk_unbind_all(int if_type);
41109d71aacSSimon Glass 
41252138fd4SSimon Glass /**
41352138fd4SSimon Glass  * blk_find_max_devnum() - find the maximum device number for an interface type
41452138fd4SSimon Glass  *
41552138fd4SSimon Glass  * Finds the last allocated device number for an interface type @if_type. The
41652138fd4SSimon Glass  * next number is safe to use for a newly allocated device.
41752138fd4SSimon Glass  *
41852138fd4SSimon Glass  * @if_type:	Interface type to scan
41952138fd4SSimon Glass  * @return maximum device number found, or -ENODEV if none, or other -ve on
42052138fd4SSimon Glass  * error
42152138fd4SSimon Glass  */
42252138fd4SSimon Glass int blk_find_max_devnum(enum if_type if_type);
42352138fd4SSimon Glass 
424cd0fb55bSSimon Glass /**
425cd0fb55bSSimon Glass  * blk_select_hwpart() - select a hardware partition
426cd0fb55bSSimon Glass  *
427cd0fb55bSSimon Glass  * Select a hardware partition if the device supports it (typically MMC does)
428cd0fb55bSSimon Glass  *
429cd0fb55bSSimon Glass  * @dev:	Device to update
430cd0fb55bSSimon Glass  * @hwpart:	Partition number to select
431cd0fb55bSSimon Glass  * @return 0 if OK, -ve on error
432cd0fb55bSSimon Glass  */
433cd0fb55bSSimon Glass int blk_select_hwpart(struct udevice *dev, int hwpart);
434cd0fb55bSSimon Glass 
4354bdb49a7STom Rini /**
4364bdb49a7STom Rini  * blk_get_from_parent() - obtain a block device by looking up its parent
4374bdb49a7STom Rini  *
4384bdb49a7STom Rini  * All devices with
4394bdb49a7STom Rini  */
4404bdb49a7STom Rini int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
4414bdb49a7STom Rini 
44209d71aacSSimon Glass #else
44309d71aacSSimon Glass #include <errno.h>
4442a981dc2SSimon Glass /*
4452a981dc2SSimon Glass  * These functions should take struct udevice instead of struct blk_desc,
4462a981dc2SSimon Glass  * but this is convenient for migration to driver model. Add a 'd' prefix
4472a981dc2SSimon Glass  * to the function operations, so that blk_read(), etc. can be reserved for
4482a981dc2SSimon Glass  * functions with the correct arguments.
4492a981dc2SSimon Glass  */
4502a981dc2SSimon Glass static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
4512a981dc2SSimon Glass 			      lbaint_t blkcnt, void *buffer)
4522a981dc2SSimon Glass {
453e40cf34aSEric Nelson 	ulong blks_read;
454e40cf34aSEric Nelson 	if (blkcache_read(block_dev->if_type, block_dev->devnum,
455e40cf34aSEric Nelson 			  start, blkcnt, block_dev->blksz, buffer))
456e40cf34aSEric Nelson 		return blkcnt;
457e40cf34aSEric Nelson 
4582a981dc2SSimon Glass 	/*
4592a981dc2SSimon Glass 	 * We could check if block_read is NULL and return -ENOSYS. But this
4602a981dc2SSimon Glass 	 * bloats the code slightly (cause some board to fail to build), and
4612a981dc2SSimon Glass 	 * it would be an error to try an operation that does not exist.
4622a981dc2SSimon Glass 	 */
463e40cf34aSEric Nelson 	blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
464e40cf34aSEric Nelson 	if (blks_read == blkcnt)
465e40cf34aSEric Nelson 		blkcache_fill(block_dev->if_type, block_dev->devnum,
466e40cf34aSEric Nelson 			      start, blkcnt, block_dev->blksz, buffer);
467e40cf34aSEric Nelson 
468e40cf34aSEric Nelson 	return blks_read;
4692a981dc2SSimon Glass }
4702a981dc2SSimon Glass 
4712a981dc2SSimon Glass static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
4722a981dc2SSimon Glass 			       lbaint_t blkcnt, const void *buffer)
4732a981dc2SSimon Glass {
474e40cf34aSEric Nelson 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
4752a981dc2SSimon Glass 	return block_dev->block_write(block_dev, start, blkcnt, buffer);
4762a981dc2SSimon Glass }
4772a981dc2SSimon Glass 
4782a981dc2SSimon Glass static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
4792a981dc2SSimon Glass 			       lbaint_t blkcnt)
4802a981dc2SSimon Glass {
481e40cf34aSEric Nelson 	blkcache_invalidate(block_dev->if_type, block_dev->devnum);
4822a981dc2SSimon Glass 	return block_dev->block_erase(block_dev, start, blkcnt);
4832a981dc2SSimon Glass }
4846eef6eacSSimon Glass 
4856eef6eacSSimon Glass /**
4866eef6eacSSimon Glass  * struct blk_driver - Driver for block interface types
4876eef6eacSSimon Glass  *
4886eef6eacSSimon Glass  * This provides access to the block devices for each interface type. One
4896eef6eacSSimon Glass  * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
4906eef6eacSSimon Glass  * type that is to be supported.
4916eef6eacSSimon Glass  *
4926eef6eacSSimon Glass  * @if_typename:	Interface type name
4936eef6eacSSimon Glass  * @if_type:		Interface type
4946eef6eacSSimon Glass  * @max_devs:		Maximum number of devices supported
4956eef6eacSSimon Glass  * @desc:		Pointer to list of devices for this interface type,
4966eef6eacSSimon Glass  *			or NULL to use @get_dev() instead
4976eef6eacSSimon Glass  */
4986eef6eacSSimon Glass struct blk_driver {
4996eef6eacSSimon Glass 	const char *if_typename;
5006eef6eacSSimon Glass 	enum if_type if_type;
5016eef6eacSSimon Glass 	int max_devs;
5026eef6eacSSimon Glass 	struct blk_desc *desc;
5036eef6eacSSimon Glass 	/**
5046eef6eacSSimon Glass 	 * get_dev() - get a pointer to a block device given its number
5056eef6eacSSimon Glass 	 *
5066eef6eacSSimon Glass 	 * Each interface allocates its own devices and typically
5076eef6eacSSimon Glass 	 * struct blk_desc is contained with the interface's data structure.
5086eef6eacSSimon Glass 	 * There is no global numbering for block devices. This method allows
5096eef6eacSSimon Glass 	 * the device for an interface type to be obtained when @desc is NULL.
5106eef6eacSSimon Glass 	 *
5116eef6eacSSimon Glass 	 * @devnum:	Device number (0 for first device on that interface,
5126eef6eacSSimon Glass 	 *		1 for second, etc.
5136eef6eacSSimon Glass 	 * @descp:	Returns pointer to the block device on success
5146eef6eacSSimon Glass 	 * @return 0 if OK, -ve on error
5156eef6eacSSimon Glass 	 */
5166eef6eacSSimon Glass 	int (*get_dev)(int devnum, struct blk_desc **descp);
5176eef6eacSSimon Glass 
5186eef6eacSSimon Glass 	/**
5196eef6eacSSimon Glass 	 * select_hwpart() - Select a hardware partition
5206eef6eacSSimon Glass 	 *
5216eef6eacSSimon Glass 	 * Some devices (e.g. MMC) can support partitioning at the hardware
5226eef6eacSSimon Glass 	 * level. This is quite separate from the normal idea of
5236eef6eacSSimon Glass 	 * software-based partitions. MMC hardware partitions must be
5246eef6eacSSimon Glass 	 * explicitly selected. Once selected only the region of the device
5256eef6eacSSimon Glass 	 * covered by that partition is accessible.
5266eef6eacSSimon Glass 	 *
5276eef6eacSSimon Glass 	 * The MMC standard provides for two boot partitions (numbered 1 and 2),
5286eef6eacSSimon Glass 	 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
5296eef6eacSSimon Glass 	 * Partition 0 is the main user-data partition.
5306eef6eacSSimon Glass 	 *
5316eef6eacSSimon Glass 	 * @desc:	Block device descriptor
5326eef6eacSSimon Glass 	 * @hwpart:	Hardware partition number to select. 0 means the main
5336eef6eacSSimon Glass 	 *		user-data partition, 1 is the first partition, 2 is
5346eef6eacSSimon Glass 	 *		the second, etc.
5356eef6eacSSimon Glass 	 * @return 0 if OK, other value for an error
5366eef6eacSSimon Glass 	 */
5376eef6eacSSimon Glass 	int (*select_hwpart)(struct blk_desc *desc, int hwpart);
5386eef6eacSSimon Glass };
5396eef6eacSSimon Glass 
5406eef6eacSSimon Glass /*
5416eef6eacSSimon Glass  * Declare a new U-Boot legacy block driver. New drivers should use driver
5426eef6eacSSimon Glass  * model (UCLASS_BLK).
5436eef6eacSSimon Glass  */
5446eef6eacSSimon Glass #define U_BOOT_LEGACY_BLK(__name)					\
5456eef6eacSSimon Glass 	ll_entry_declare(struct blk_driver, __name, blk_driver)
5466eef6eacSSimon Glass 
5476eef6eacSSimon Glass struct blk_driver *blk_driver_lookup_type(int if_type);
5486eef6eacSSimon Glass 
54909d71aacSSimon Glass #endif /* !CONFIG_BLK */
5502a981dc2SSimon Glass 
5516eef6eacSSimon Glass /**
5526eef6eacSSimon Glass  * blk_get_devnum_by_typename() - Get a block device by type and number
5536eef6eacSSimon Glass  *
5546eef6eacSSimon Glass  * This looks through the available block devices of the given type, returning
5556eef6eacSSimon Glass  * the one with the given @devnum.
5566eef6eacSSimon Glass  *
5576eef6eacSSimon Glass  * @if_type:	Block device type
5586eef6eacSSimon Glass  * @devnum:	Device number
5596eef6eacSSimon Glass  * @return point to block device descriptor, or NULL if not found
5606eef6eacSSimon Glass  */
5616eef6eacSSimon Glass struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
5626eef6eacSSimon Glass 
5636eef6eacSSimon Glass /**
5646eef6eacSSimon Glass  * blk_get_devnum_by_type() - Get a block device by type name, and number
5656eef6eacSSimon Glass  *
5666eef6eacSSimon Glass  * This looks up the block device type based on @if_typename, then calls
5676eef6eacSSimon Glass  * blk_get_devnum_by_type().
5686eef6eacSSimon Glass  *
5696eef6eacSSimon Glass  * @if_typename:	Block device type name
5706eef6eacSSimon Glass  * @devnum:		Device number
5716eef6eacSSimon Glass  * @return point to block device descriptor, or NULL if not found
5726eef6eacSSimon Glass  */
5736eef6eacSSimon Glass struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
5746eef6eacSSimon Glass 					    int devnum);
5756eef6eacSSimon Glass 
5766eef6eacSSimon Glass /**
5776eef6eacSSimon Glass  * blk_dselect_hwpart() - select a hardware partition
5786eef6eacSSimon Glass  *
5796eef6eacSSimon Glass  * This selects a hardware partition (such as is supported by MMC). The block
5806eef6eacSSimon Glass  * device size may change as this effectively points the block device to a
5816eef6eacSSimon Glass  * partition at the hardware level. See the select_hwpart() method above.
5826eef6eacSSimon Glass  *
5836eef6eacSSimon Glass  * @desc:	Block device descriptor for the device to select
5846eef6eacSSimon Glass  * @hwpart:	Partition number to select
5856eef6eacSSimon Glass  * @return 0 if OK, -ve on error
5866eef6eacSSimon Glass  */
5876eef6eacSSimon Glass int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
5886eef6eacSSimon Glass 
5896eef6eacSSimon Glass /**
5906eef6eacSSimon Glass  * blk_list_part() - list the partitions for block devices of a given type
5916eef6eacSSimon Glass  *
5926eef6eacSSimon Glass  * This looks up the partition type for each block device of type @if_type,
5936eef6eacSSimon Glass  * then displays a list of partitions.
5946eef6eacSSimon Glass  *
5956eef6eacSSimon Glass  * @if_type:	Block device type
5966eef6eacSSimon Glass  * @return 0 if OK, -ENODEV if there is none of that type
5976eef6eacSSimon Glass  */
5986eef6eacSSimon Glass int blk_list_part(enum if_type if_type);
5996eef6eacSSimon Glass 
6006eef6eacSSimon Glass /**
6016eef6eacSSimon Glass  * blk_list_devices() - list the block devices of a given type
6026eef6eacSSimon Glass  *
6036eef6eacSSimon Glass  * This lists each block device of the type @if_type, showing the capacity
6046eef6eacSSimon Glass  * as well as type-specific information.
6056eef6eacSSimon Glass  *
6066eef6eacSSimon Glass  * @if_type:	Block device type
6076eef6eacSSimon Glass  */
6086eef6eacSSimon Glass void blk_list_devices(enum if_type if_type);
6096eef6eacSSimon Glass 
6106eef6eacSSimon Glass /**
6116eef6eacSSimon Glass  * blk_show_device() - show information about a given block device
6126eef6eacSSimon Glass  *
6136eef6eacSSimon Glass  * This shows the block device capacity as well as type-specific information.
6146eef6eacSSimon Glass  *
6156eef6eacSSimon Glass  * @if_type:	Block device type
6166eef6eacSSimon Glass  * @devnum:	Device number
6176eef6eacSSimon Glass  * @return 0 if OK, -ENODEV for invalid device number
6186eef6eacSSimon Glass  */
6196eef6eacSSimon Glass int blk_show_device(enum if_type if_type, int devnum);
6206eef6eacSSimon Glass 
6216eef6eacSSimon Glass /**
6226eef6eacSSimon Glass  * blk_print_device_num() - show information about a given block device
6236eef6eacSSimon Glass  *
6246eef6eacSSimon Glass  * This is similar to blk_show_device() but returns an error if the block
6256eef6eacSSimon Glass  * device type is unknown.
6266eef6eacSSimon Glass  *
6276eef6eacSSimon Glass  * @if_type:	Block device type
6286eef6eacSSimon Glass  * @devnum:	Device number
6296eef6eacSSimon Glass  * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
6306eef6eacSSimon Glass  * device is not connected
6316eef6eacSSimon Glass  */
6326eef6eacSSimon Glass int blk_print_device_num(enum if_type if_type, int devnum);
6336eef6eacSSimon Glass 
6346eef6eacSSimon Glass /**
6356eef6eacSSimon Glass  * blk_print_part_devnum() - print the partition information for a device
6366eef6eacSSimon Glass  *
6376eef6eacSSimon Glass  * @if_type:	Block device type
6386eef6eacSSimon Glass  * @devnum:	Device number
6396eef6eacSSimon Glass  * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
6406eef6eacSSimon Glass  * the interface type is not supported, other -ve on other error
6416eef6eacSSimon Glass  */
6426eef6eacSSimon Glass int blk_print_part_devnum(enum if_type if_type, int devnum);
6436eef6eacSSimon Glass 
6446eef6eacSSimon Glass /**
6456eef6eacSSimon Glass  * blk_read_devnum() - read blocks from a device
6466eef6eacSSimon Glass  *
6476eef6eacSSimon Glass  * @if_type:	Block device type
6486eef6eacSSimon Glass  * @devnum:	Device number
6496eef6eacSSimon Glass  * @blkcnt:	Number of blocks to read
6506eef6eacSSimon Glass  * @buffer:	Address to write data to
6516eef6eacSSimon Glass  * @return number of blocks read, or -ve error number on error
6526eef6eacSSimon Glass  */
6536eef6eacSSimon Glass ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
6546eef6eacSSimon Glass 		      lbaint_t blkcnt, void *buffer);
6556eef6eacSSimon Glass 
6566eef6eacSSimon Glass /**
6576eef6eacSSimon Glass  * blk_write_devnum() - write blocks to a device
6586eef6eacSSimon Glass  *
6596eef6eacSSimon Glass  * @if_type:	Block device type
6606eef6eacSSimon Glass  * @devnum:	Device number
6616eef6eacSSimon Glass  * @blkcnt:	Number of blocks to write
6626eef6eacSSimon Glass  * @buffer:	Address to read data from
6636eef6eacSSimon Glass  * @return number of blocks written, or -ve error number on error
6646eef6eacSSimon Glass  */
6656eef6eacSSimon Glass ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
6666eef6eacSSimon Glass 		       lbaint_t blkcnt, const void *buffer);
6676eef6eacSSimon Glass 
6686eef6eacSSimon Glass /**
6696eef6eacSSimon Glass  * blk_select_hwpart_devnum() - select a hardware partition
6706eef6eacSSimon Glass  *
6716eef6eacSSimon Glass  * This is similar to blk_dselect_hwpart() but it looks up the interface and
6726eef6eacSSimon Glass  * device number.
6736eef6eacSSimon Glass  *
6746eef6eacSSimon Glass  * @if_type:	Block device type
6756eef6eacSSimon Glass  * @devnum:	Device number
6766eef6eacSSimon Glass  * @hwpart:	Partition number to select
6776eef6eacSSimon Glass  * @return 0 if OK, -ve on error
6786eef6eacSSimon Glass  */
6796eef6eacSSimon Glass int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
6806eef6eacSSimon Glass 
6816faa4ed7SSimon Glass /**
6826faa4ed7SSimon Glass  * blk_get_if_type_name() - Get the name of an interface type
6836faa4ed7SSimon Glass  *
6846faa4ed7SSimon Glass  * @if_type: Interface type to check
6856faa4ed7SSimon Glass  * @return name of interface, or NULL if none
6866faa4ed7SSimon Glass  */
6876faa4ed7SSimon Glass const char *blk_get_if_type_name(enum if_type if_type);
6886faa4ed7SSimon Glass 
6894395f667SSimon Glass /**
6904395f667SSimon Glass  * blk_common_cmd() - handle common commands with block devices
6914395f667SSimon Glass  *
6924395f667SSimon Glass  * @args: Number of arguments to the command (argv[0] is the command itself)
6934395f667SSimon Glass  * @argv: Command arguments
6944395f667SSimon Glass  * @if_type: Interface type
6954395f667SSimon Glass  * @cur_devnump: Current device number for this interface type
6964395f667SSimon Glass  * @return 0 if OK, CMD_RET_ERROR on error
6974395f667SSimon Glass  */
6984395f667SSimon Glass int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,
6994395f667SSimon Glass 		   int *cur_devnump);
7004395f667SSimon Glass 
7011712dc5cSJoseph Chen /**
7021712dc5cSJoseph Chen  * if_typename_to_iftype() - get iftype according to iftype name
7031712dc5cSJoseph Chen  *
7041712dc5cSJoseph Chen  * @if_typename: iftype name
7051712dc5cSJoseph Chen  * @return iftype index
7061712dc5cSJoseph Chen  */
7071712dc5cSJoseph Chen enum if_type if_typename_to_iftype(const char *if_typename);
7081712dc5cSJoseph Chen 
7091a73661bSSimon Glass #endif
710