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.
99 int blkcache_read(int iftype, int dev,
100 lbaint_t start, lbaint_t blkcnt,
101 unsigned long blksz, void *buffer);
104 * blkcache_fill() - make data read from a block device available
107 * @param iftype - IF_TYPE_x for type of device
108 * @param dev - device index of particular type
109 * @param start - starting block number
110 * @param blkcnt - number of blocks available
111 * @param blksz - size in bytes of each block
112 * @param buf - buffer containing data to cache
115 void blkcache_fill(int iftype, int dev,
116 lbaint_t start, lbaint_t blkcnt,
117 unsigned long blksz, void const *buffer);
120 * blkcache_invalidate() - discard the cache for a set of blocks
121 * because of a write or device (re)initialization.
123 * @param iftype - IF_TYPE_x for type of device
124 * @param dev - device index of particular type
126 void blkcache_invalidate(int iftype, int dev);
129 * blkcache_configure() - configure block cache
131 * @param blocks - maximum blocks per entry
132 * @param entries - maximum entries in cache
134 void blkcache_configure(unsigned blocks, unsigned entries);
137 * statistics of the block cache
139 struct block_cache_stats {
142 unsigned entries; /* current entry count */
143 unsigned max_blocks_per_entry;
144 unsigned max_entries;
148 * get_blkcache_stats() - return statistics and reset
150 * @param stats - statistics are copied here
152 void blkcache_stats(struct block_cache_stats *stats);
156 static inline int blkcache_read(int iftype, int dev,
157 lbaint_t start, lbaint_t blkcnt,
158 unsigned long blksz, void *buffer)
163 static inline void blkcache_fill(int iftype, int dev,
164 lbaint_t start, lbaint_t blkcnt,
165 unsigned long blksz, void const *buffer) {}
167 static inline void blkcache_invalidate(int iftype, int dev) {}
174 /* Operations on block devices */
177 * read() - read from a block device
179 * @dev: Device to read from
180 * @start: Start block number to read (0=first)
181 * @blkcnt: Number of blocks to read
182 * @buffer: Destination buffer for data read
183 * @return number of blocks read, or -ve error number (see the
184 * IS_ERR_VALUE() macro
186 unsigned long (*read)(struct udevice *dev, lbaint_t start,
187 lbaint_t blkcnt, void *buffer);
190 * write() - write to a block device
192 * @dev: Device to write to
193 * @start: Start block number to write (0=first)
194 * @blkcnt: Number of blocks to write
195 * @buffer: Source buffer for data to write
196 * @return number of blocks written, or -ve error number (see the
197 * IS_ERR_VALUE() macro
199 unsigned long (*write)(struct udevice *dev, lbaint_t start,
200 lbaint_t blkcnt, const void *buffer);
203 * erase() - erase a section of a block device
205 * @dev: Device to (partially) erase
206 * @start: Start block number to erase (0=first)
207 * @blkcnt: Number of blocks to erase
208 * @return number of blocks erased, or -ve error number (see the
209 * IS_ERR_VALUE() macro
211 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
215 #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
218 * These functions should take struct udevice instead of struct blk_desc,
219 * but this is convenient for migration to driver model. Add a 'd' prefix
220 * to the function operations, so that blk_read(), etc. can be reserved for
221 * functions with the correct arguments.
223 unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
224 lbaint_t blkcnt, void *buffer);
225 unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
226 lbaint_t blkcnt, const void *buffer);
227 unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
231 * blk_get_device() - Find and probe a block device ready for use
233 * @if_type: Interface type (enum if_type_t)
234 * @devnum: Device number (specific to each interface type)
235 * @devp: the device, if found
236 * @return - if found, -ENODEV if no device found, or other -ve error value
238 int blk_get_device(int if_type, int devnum, struct udevice **devp);
241 * blk_first_device() - Find the first device for a given interface
243 * The device is probed ready for use
245 * @devnum: Device number (specific to each interface type)
246 * @devp: the device, if found
247 * @return 0 if found, -ENODEV if no device, or other -ve error value
249 int blk_first_device(int if_type, struct udevice **devp);
252 * blk_next_device() - Find the next device for a given interface
254 * This can be called repeatedly after blk_first_device() to iterate through
255 * all devices of the given interface type.
257 * The device is probed ready for use
259 * @devp: On entry, the previous device returned. On exit, the next
261 * @return 0 if found, -ENODEV if no device, or other -ve error value
263 int blk_next_device(struct udevice **devp);
266 * blk_create_device() - Create a new block device
268 * @parent: Parent of the new device
269 * @drv_name: Driver name to use for the block device
270 * @name: Name for the device
271 * @if_type: Interface type (enum if_type_t)
272 * @devnum: Device number, specific to the interface type
273 * @blksz: Block size of the device in bytes (typically 512)
274 * @size: Total size of the device in bytes
275 * @devp: the new device (which has not been probed)
277 int blk_create_device(struct udevice *parent, const char *drv_name,
278 const char *name, int if_type, int devnum, int blksz,
279 lbaint_t size, struct udevice **devp);
282 * blk_prepare_device() - Prepare a block device for use
284 * This reads partition information from the device if supported.
286 * @dev: Device to prepare
287 * @return 0 if ok, -ve on error
289 int blk_prepare_device(struct udevice *dev);
292 * blk_unbind_all() - Unbind all device of the given interface type
294 * The devices are removed and then unbound.
296 * @if_type: Interface type to unbind
297 * @return 0 if OK, -ve on error
299 int blk_unbind_all(int if_type);
304 * These functions should take struct udevice instead of struct blk_desc,
305 * but this is convenient for migration to driver model. Add a 'd' prefix
306 * to the function operations, so that blk_read(), etc. can be reserved for
307 * functions with the correct arguments.
309 static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
310 lbaint_t blkcnt, void *buffer)
313 if (blkcache_read(block_dev->if_type, block_dev->devnum,
314 start, blkcnt, block_dev->blksz, buffer))
318 * We could check if block_read is NULL and return -ENOSYS. But this
319 * bloats the code slightly (cause some board to fail to build), and
320 * it would be an error to try an operation that does not exist.
322 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
323 if (blks_read == blkcnt)
324 blkcache_fill(block_dev->if_type, block_dev->devnum,
325 start, blkcnt, block_dev->blksz, buffer);
330 static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
331 lbaint_t blkcnt, const void *buffer)
333 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
334 return block_dev->block_write(block_dev, start, blkcnt, buffer);
337 static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
340 blkcache_invalidate(block_dev->if_type, block_dev->devnum);
341 return block_dev->block_erase(block_dev, start, blkcnt);
343 #endif /* !CONFIG_BLK */