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: */
34 IF_TYPE_COUNT, /* Number of interface types */
38 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
39 * with dev_get_uclass_platdata(dev)
43 * TODO: With driver model we should be able to use the parent
44 * device's uclass instead.
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 */
55 /* device can use 48bit addr (ATA/ATAPI v7) */
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 */
67 unsigned long (*block_read)(struct blk_desc *block_dev,
71 unsigned long (*block_write)(struct blk_desc *block_dev,
75 unsigned long (*block_erase)(struct blk_desc *block_dev,
78 void *priv; /* driver private struct pointer */
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))
86 #ifdef CONFIG_BLOCK_CACHE
88 * blkcache_read() - attempt to read a set of blocks from cache
90 * @param iftype - IF_TYPE_x for type of device
91 * @param dev - device index of particular type
92 * @param start - starting block number
93 * @param blkcnt - number of blocks to read
94 * @param blksz - size in bytes of each block
95 * @param buf - buffer to contain cached data
97 * @return - '1' if block returned from cache, '0' otherwise.
100 (int iftype, int dev,
101 lbaint_t start, lbaint_t blkcnt,
102 unsigned long blksz, void *buffer);
105 * blkcache_fill() - make data read from a block device available
108 * @param iftype - IF_TYPE_x for type of device
109 * @param dev - device index of particular type
110 * @param start - starting block number
111 * @param blkcnt - number of blocks available
112 * @param blksz - size in bytes of each block
113 * @param buf - buffer containing data to cache
117 (int iftype, int dev,
118 lbaint_t start, lbaint_t blkcnt,
119 unsigned long blksz, void const *buffer);
122 * blkcache_invalidate() - discard the cache for a set of blocks
123 * because of a write or device (re)initialization.
125 * @param iftype - IF_TYPE_x for type of device
126 * @param dev - device index of particular type
128 void blkcache_invalidate
129 (int iftype, int dev);
132 * blkcache_configure() - configure block cache
134 * @param blocks - maximum blocks per entry
135 * @param entries - maximum entries in cache
137 void blkcache_configure(unsigned blocks, unsigned entries);
140 * statistics of the block cache
142 struct block_cache_stats {
145 unsigned entries; /* current entry count */
146 unsigned max_blocks_per_entry;
147 unsigned max_entries;
151 * get_blkcache_stats() - return statistics and reset
153 * @param stats - statistics are copied here
155 void blkcache_stats(struct block_cache_stats *stats);
159 static inline int blkcache_read
160 (int iftype, int dev,
161 lbaint_t start, lbaint_t blkcnt,
162 unsigned long blksz, void *buffer)
167 static inline void blkcache_fill
168 (int iftype, int dev,
169 lbaint_t start, lbaint_t blkcnt,
170 unsigned long blksz, void const *buffer) {}
172 static inline void blkcache_invalidate
173 (int iftype, int dev) {}
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 #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
224 * These functions should take struct udevice instead of struct blk_desc,
225 * but this is convenient for migration to driver model. Add a 'd' prefix
226 * to the function operations, so that blk_read(), etc. can be reserved for
227 * functions with the correct arguments.
229 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
230 lbaint_t blkcnt, void *buffer);
231 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
232 lbaint_t blkcnt, const void *buffer);
233 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
237 * blk_get_device() - Find and probe a block device ready for use
239 * @if_type: Interface type (enum if_type_t)
240 * @devnum: Device number (specific to each interface type)
241 * @devp: the device, if found
242 * @return - if found, -ENODEV if no device found, or other -ve error value
244 int blk_get_device(int if_type, int devnum, struct udevice **devp);
247 * blk_first_device() - Find the first device for a given interface
249 * The device is probed ready for use
251 * @devnum: Device number (specific to each interface type)
252 * @devp: the device, if found
253 * @return 0 if found, -ENODEV if no device, or other -ve error value
255 int blk_first_device(int if_type, struct udevice **devp);
258 * blk_next_device() - Find the next device for a given interface
260 * This can be called repeatedly after blk_first_device() to iterate through
261 * all devices of the given interface type.
263 * The device is probed ready for use
265 * @devp: On entry, the previous device returned. On exit, the next
267 * @return 0 if found, -ENODEV if no device, or other -ve error value
269 int blk_next_device(struct udevice **devp);
272 * blk_create_device() - Create a new block device
274 * @parent: Parent of the new device
275 * @drv_name: Driver name to use for the block device
276 * @name: Name for the device
277 * @if_type: Interface type (enum if_type_t)
278 * @devnum: Device number, specific to the interface type
279 * @blksz: Block size of the device in bytes (typically 512)
280 * @size: Total size of the device in bytes
281 * @devp: the new device (which has not been probed)
283 int blk_create_device(struct udevice *parent, const char *drv_name,
284 const char *name, int if_type, int devnum, int blksz,
285 lbaint_t size, struct udevice **devp);
288 * blk_prepare_device() - Prepare a block device for use
290 * This reads partition information from the device if supported.
292 * @dev: Device to prepare
293 * @return 0 if ok, -ve on error
295 int blk_prepare_device(struct udevice *dev);
298 * blk_unbind_all() - Unbind all device of the given interface type
300 * The devices are removed and then unbound.
302 * @if_type: Interface type to unbind
303 * @return 0 if OK, -ve on error
305 int blk_unbind_all(int if_type);
310 * These functions should take struct udevice instead of struct blk_desc,
311 * but this is convenient for migration to driver model. Add a 'd' prefix
312 * to the function operations, so that blk_read(), etc. can be reserved for
313 * functions with the correct arguments.
315 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
316 lbaint_t blkcnt, void *buffer)
319 if (blkcache_read(block_dev->if_type, block_dev->devnum,
320 start, blkcnt, block_dev->blksz, buffer))
324 * We could check if block_read is NULL and return -ENOSYS. But this
325 * bloats the code slightly (cause some board to fail to build), and
326 * it would be an error to try an operation that does not exist.
328 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
329 if (blks_read == blkcnt)
330 blkcache_fill(block_dev->if_type, block_dev->devnum,
331 start, blkcnt, block_dev->blksz, buffer);
336 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
337 lbaint_t blkcnt, const void *buffer)
339 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
340 return block_dev->block_write(block_dev, start, blkcnt, buffer);
343 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
346 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
347 return block_dev->block_erase(block_dev, start, blkcnt);
349 #endif /* !CONFIG_BLK */