xref: /openbmc/u-boot/include/blk.h (revision 2f3f477b)
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 
34 	IF_TYPE_COUNT,			/* Number of interface types */
35 };
36 
37 /*
38  * With driver model (CONFIG_BLK) this is uclass platform data, accessible
39  * with dev_get_uclass_platdata(dev)
40  */
41 struct blk_desc {
42 	/*
43 	 * TODO: With driver model we should be able to use the parent
44 	 * device's uclass instead.
45 	 */
46 	enum if_type	if_type;	/* type of the interface */
47 	int		devnum;		/* device number */
48 	unsigned char	part_type;	/* partition type */
49 	unsigned char	target;		/* target SCSI ID */
50 	unsigned char	lun;		/* target LUN */
51 	unsigned char	hwpart;		/* HW partition, e.g. for eMMC */
52 	unsigned char	type;		/* device type */
53 	unsigned char	removable;	/* removable device */
54 #ifdef CONFIG_LBA48
55 	/* device can use 48bit addr (ATA/ATAPI v7) */
56 	unsigned char	lba48;
57 #endif
58 	lbaint_t	lba;		/* number of blocks */
59 	unsigned long	blksz;		/* block size */
60 	int		log2blksz;	/* for convenience: log2(blksz) */
61 	char		vendor[40+1];	/* IDE model, SCSI Vendor */
62 	char		product[20+1];	/* IDE Serial no, SCSI product */
63 	char		revision[8+1];	/* firmware revision */
64 #ifdef CONFIG_BLK
65 	struct udevice *bdev;
66 #else
67 	unsigned long	(*block_read)(struct blk_desc *block_dev,
68 				      lbaint_t start,
69 				      lbaint_t blkcnt,
70 				      void *buffer);
71 	unsigned long	(*block_write)(struct blk_desc *block_dev,
72 				       lbaint_t start,
73 				       lbaint_t blkcnt,
74 				       const void *buffer);
75 	unsigned long	(*block_erase)(struct blk_desc *block_dev,
76 				       lbaint_t start,
77 				       lbaint_t blkcnt);
78 	void		*priv;		/* driver private struct pointer */
79 #endif
80 };
81 
82 #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
83 #define PAD_TO_BLOCKSIZE(size, blk_desc) \
84 	(PAD_SIZE(size, blk_desc->blksz))
85 
86 #ifdef CONFIG_BLK
87 struct udevice;
88 
89 /* Operations on block devices */
90 struct blk_ops {
91 	/**
92 	 * read() - read from a block device
93 	 *
94 	 * @dev:	Device to read from
95 	 * @start:	Start block number to read (0=first)
96 	 * @blkcnt:	Number of blocks to read
97 	 * @buffer:	Destination buffer for data read
98 	 * @return number of blocks read, or -ve error number (see the
99 	 * IS_ERR_VALUE() macro
100 	 */
101 	unsigned long (*read)(struct udevice *dev, lbaint_t start,
102 			      lbaint_t blkcnt, void *buffer);
103 
104 	/**
105 	 * write() - write to a block device
106 	 *
107 	 * @dev:	Device to write to
108 	 * @start:	Start block number to write (0=first)
109 	 * @blkcnt:	Number of blocks to write
110 	 * @buffer:	Source buffer for data to write
111 	 * @return number of blocks written, or -ve error number (see the
112 	 * IS_ERR_VALUE() macro
113 	 */
114 	unsigned long (*write)(struct udevice *dev, lbaint_t start,
115 			       lbaint_t blkcnt, const void *buffer);
116 
117 	/**
118 	 * erase() - erase a section of a block device
119 	 *
120 	 * @dev:	Device to (partially) erase
121 	 * @start:	Start block number to erase (0=first)
122 	 * @blkcnt:	Number of blocks to erase
123 	 * @return number of blocks erased, or -ve error number (see the
124 	 * IS_ERR_VALUE() macro
125 	 */
126 	unsigned long (*erase)(struct udevice *dev, lbaint_t start,
127 			       lbaint_t blkcnt);
128 };
129 
130 #define blk_get_ops(dev)	((struct blk_ops *)(dev)->driver->ops)
131 
132 /*
133  * These functions should take struct udevice instead of struct blk_desc,
134  * but this is convenient for migration to driver model. Add a 'd' prefix
135  * to the function operations, so that blk_read(), etc. can be reserved for
136  * functions with the correct arguments.
137  */
138 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
139 			lbaint_t blkcnt, void *buffer);
140 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
141 			 lbaint_t blkcnt, const void *buffer);
142 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
143 			 lbaint_t blkcnt);
144 
145 /**
146  * blk_get_device() - Find and probe a block device ready for use
147  *
148  * @if_type:	Interface type (enum if_type_t)
149  * @devnum:	Device number (specific to each interface type)
150  * @devp:	the device, if found
151  * @return - if found, -ENODEV if no device found, or other -ve error value
152  */
153 int blk_get_device(int if_type, int devnum, struct udevice **devp);
154 
155 /**
156  * blk_first_device() - Find the first device for a given interface
157  *
158  * The device is probed ready for use
159  *
160  * @devnum:	Device number (specific to each interface type)
161  * @devp:	the device, if found
162  * @return 0 if found, -ENODEV if no device, or other -ve error value
163  */
164 int blk_first_device(int if_type, struct udevice **devp);
165 
166 /**
167  * blk_next_device() - Find the next device for a given interface
168  *
169  * This can be called repeatedly after blk_first_device() to iterate through
170  * all devices of the given interface type.
171  *
172  * The device is probed ready for use
173  *
174  * @devp:	On entry, the previous device returned. On exit, the next
175  *		device, if found
176  * @return 0 if found, -ENODEV if no device, or other -ve error value
177  */
178 int blk_next_device(struct udevice **devp);
179 
180 /**
181  * blk_create_device() - Create a new block device
182  *
183  * @parent:	Parent of the new device
184  * @drv_name:	Driver name to use for the block device
185  * @name:	Name for the device
186  * @if_type:	Interface type (enum if_type_t)
187  * @devnum:	Device number, specific to the interface type
188  * @blksz:	Block size of the device in bytes (typically 512)
189  * @size:	Total size of the device in bytes
190  * @devp:	the new device (which has not been probed)
191  */
192 int blk_create_device(struct udevice *parent, const char *drv_name,
193 		      const char *name, int if_type, int devnum, int blksz,
194 		      lbaint_t size, struct udevice **devp);
195 
196 /**
197  * blk_prepare_device() - Prepare a block device for use
198  *
199  * This reads partition information from the device if supported.
200  *
201  * @dev:	Device to prepare
202  * @return 0 if ok, -ve on error
203  */
204 int blk_prepare_device(struct udevice *dev);
205 
206 /**
207  * blk_unbind_all() - Unbind all device of the given interface type
208  *
209  * The devices are removed and then unbound.
210  *
211  * @if_type:	Interface type to unbind
212  * @return 0 if OK, -ve on error
213  */
214 int blk_unbind_all(int if_type);
215 
216 #else
217 #include <errno.h>
218 /*
219  * These functions should take struct udevice instead of struct blk_desc,
220  * but this is convenient for migration to driver model. Add a 'd' prefix
221  * to the function operations, so that blk_read(), etc. can be reserved for
222  * functions with the correct arguments.
223  */
224 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
225 			      lbaint_t blkcnt, void *buffer)
226 {
227 	/*
228 	 * We could check if block_read is NULL and return -ENOSYS. But this
229 	 * bloats the code slightly (cause some board to fail to build), and
230 	 * it would be an error to try an operation that does not exist.
231 	 */
232 	return block_dev->block_read(block_dev, start, blkcnt, buffer);
233 }
234 
235 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
236 			       lbaint_t blkcnt, const void *buffer)
237 {
238 	return block_dev->block_write(block_dev, start, blkcnt, buffer);
239 }
240 
241 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
242 			       lbaint_t blkcnt)
243 {
244 	return block_dev->block_erase(block_dev, start, blkcnt);
245 }
246 #endif /* !CONFIG_BLK */
247 
248 #endif
249