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