blob: df8f9830d3a635df231eaaa0a157febc506b7cf7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glass1a736612016-02-29 15:25:39 -07002/*
3 * (C) Copyright 2000-2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glass1a736612016-02-29 15:25:39 -07005 */
6
7#ifndef BLK_H
8#define BLK_H
9
Simon Glasse33a5c62022-08-11 19:34:59 -060010#include <dm/uclass-id.h>
Peter Jonesff98cb92017-09-13 18:05:25 -040011#include <efi.h>
12
Simon Glass1a736612016-02-29 15:25:39 -070013#ifdef CONFIG_SYS_64BIT_LBA
14typedef uint64_t lbaint_t;
15#define LBAFlength "ll"
16#else
17typedef ulong lbaint_t;
18#define LBAFlength "l"
19#endif
20#define LBAF "%" LBAFlength "x"
21#define LBAFU "%" LBAFlength "u"
22
Bo Lv72d0e902023-01-02 14:27:34 +000023#ifdef CONFIG_AMLOGIC_MODIFY
24/* Interface types: */
25enum if_type {
26 IF_TYPE_UNKNOWN = 0,
27 IF_TYPE_IDE,
28 IF_TYPE_SCSI,
29 IF_TYPE_ATAPI,
30 IF_TYPE_USB,
31 IF_TYPE_DOC,
32 IF_TYPE_MMC,
33 IF_TYPE_SD,
34 IF_TYPE_SATA,
35 IF_TYPE_HOST,
36 IF_TYPE_NVME,
37 IF_TYPE_EFI,
38 IF_TYPE_VIRTIO,
39
40 IF_TYPE_COUNT, /* Number of interface types */
41};
42#endif
43
Simon Glass96f37b02021-07-05 16:32:59 -060044struct udevice;
45
Simon Glassa51eb8d2022-08-11 19:34:45 -060046static inline bool blk_enabled(void)
47{
Simon Glass7f8967c2022-08-11 19:34:48 -060048 return CONFIG_IS_ENABLED(BLK) || IS_ENABLED(CONFIG_SPL_LEGACY_BLOCK);
Simon Glassa51eb8d2022-08-11 19:34:45 -060049}
50
Bin Mengeb81b1a2017-09-10 05:12:50 -070051#define BLK_VEN_SIZE 40
52#define BLK_PRD_SIZE 20
53#define BLK_REV_SIZE 8
54
Masahisa Kojimace3dbc52021-10-26 17:27:25 +090055#define PART_FORMAT_PCAT 0x1
56#define PART_FORMAT_GPT 0x2
57
Simon Glass09d71aa2016-02-29 15:25:55 -070058/*
Peter Jonesff98cb92017-09-13 18:05:25 -040059 * Identifies the partition table type (ie. MBR vs GPT GUID) signature
60 */
61enum sig_type {
62 SIG_TYPE_NONE,
63 SIG_TYPE_MBR,
64 SIG_TYPE_GUID,
65
66 SIG_TYPE_COUNT /* Number of signature types */
67};
68
69/*
Simon Glass09d71aa2016-02-29 15:25:55 -070070 * With driver model (CONFIG_BLK) this is uclass platform data, accessible
Simon Glasscaa4daa2020-12-03 16:55:18 -070071 * with dev_get_uclass_plat(dev)
Simon Glass09d71aa2016-02-29 15:25:55 -070072 */
Simon Glass1a736612016-02-29 15:25:39 -070073struct blk_desc {
Simon Glass09d71aa2016-02-29 15:25:55 -070074 /*
75 * TODO: With driver model we should be able to use the parent
76 * device's uclass instead.
77 */
Simon Glass8149b152022-09-17 09:00:09 -060078 enum uclass_id uclass_id; /* type of the interface */
Simon Glassbcce53d2016-02-29 15:25:51 -070079 int devnum; /* device number */
Simon Glass1a736612016-02-29 15:25:39 -070080 unsigned char part_type; /* partition type */
81 unsigned char target; /* target SCSI ID */
82 unsigned char lun; /* target LUN */
83 unsigned char hwpart; /* HW partition, e.g. for eMMC */
84 unsigned char type; /* device type */
85 unsigned char removable; /* removable device */
86#ifdef CONFIG_LBA48
87 /* device can use 48bit addr (ATA/ATAPI v7) */
88 unsigned char lba48;
89#endif
90 lbaint_t lba; /* number of blocks */
91 unsigned long blksz; /* block size */
92 int log2blksz; /* for convenience: log2(blksz) */
Bin Mengeb81b1a2017-09-10 05:12:50 -070093 char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
94 char product[BLK_PRD_SIZE + 1]; /* device product number */
95 char revision[BLK_REV_SIZE + 1]; /* firmware revision */
Peter Jonesff98cb92017-09-13 18:05:25 -040096 enum sig_type sig_type; /* Partition table signature type */
97 union {
98 uint32_t mbr_sig; /* MBR integer signature */
99 efi_guid_t guid_sig; /* GPT GUID Signature */
100 };
Simon Glassc4d660d2017-07-04 13:31:19 -0600101#if CONFIG_IS_ENABLED(BLK)
Simon Glassb6694a32016-05-01 13:52:33 -0600102 /*
103 * For now we have a few functions which take struct blk_desc as a
104 * parameter. This field allows them to look up the associated
105 * device. Once these functions are removed we can drop this field.
106 */
Simon Glass09d71aa2016-02-29 15:25:55 -0700107 struct udevice *bdev;
108#else
Simon Glass1a736612016-02-29 15:25:39 -0700109 unsigned long (*block_read)(struct blk_desc *block_dev,
110 lbaint_t start,
111 lbaint_t blkcnt,
112 void *buffer);
113 unsigned long (*block_write)(struct blk_desc *block_dev,
114 lbaint_t start,
115 lbaint_t blkcnt,
116 const void *buffer);
117 unsigned long (*block_erase)(struct blk_desc *block_dev,
118 lbaint_t start,
119 lbaint_t blkcnt);
120 void *priv; /* driver private struct pointer */
Simon Glass09d71aa2016-02-29 15:25:55 -0700121#endif
Simon Glass1a736612016-02-29 15:25:39 -0700122};
123
124#define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
125#define PAD_TO_BLOCKSIZE(size, blk_desc) \
126 (PAD_SIZE(size, blk_desc->blksz))
127
Adam Ford6fef62c2018-06-11 17:17:48 -0500128#if CONFIG_IS_ENABLED(BLOCK_CACHE)
Angelo Durgehello1526bcc2020-01-21 10:37:27 +0100129
130/**
131 * blkcache_init() - initialize the block cache list pointers
132 */
133int blkcache_init(void);
134
Eric Nelsone40cf342016-03-28 10:05:44 -0700135/**
136 * blkcache_read() - attempt to read a set of blocks from cache
137 *
Simon Glass8149b152022-09-17 09:00:09 -0600138 * @param iftype - uclass_id_x for type of device
Eric Nelsone40cf342016-03-28 10:05:44 -0700139 * @param dev - device index of particular type
140 * @param start - starting block number
141 * @param blkcnt - number of blocks to read
142 * @param blksz - size in bytes of each block
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200143 * @param buffer - buffer to contain cached data
Eric Nelsone40cf342016-03-28 10:05:44 -0700144 *
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100145 * Return: - 1 if block returned from cache, 0 otherwise.
Eric Nelsone40cf342016-03-28 10:05:44 -0700146 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700147int blkcache_read(int iftype, int dev,
148 lbaint_t start, lbaint_t blkcnt,
149 unsigned long blksz, void *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700150
151/**
152 * blkcache_fill() - make data read from a block device available
153 * to the block cache
154 *
Simon Glass8149b152022-09-17 09:00:09 -0600155 * @param iftype - uclass_id_x for type of device
Eric Nelsone40cf342016-03-28 10:05:44 -0700156 * @param dev - device index of particular type
157 * @param start - starting block number
158 * @param blkcnt - number of blocks available
159 * @param blksz - size in bytes of each block
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200160 * @param buffer - buffer containing data to cache
Eric Nelsone40cf342016-03-28 10:05:44 -0700161 *
162 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700163void blkcache_fill(int iftype, int dev,
164 lbaint_t start, lbaint_t blkcnt,
165 unsigned long blksz, void const *buffer);
Eric Nelsone40cf342016-03-28 10:05:44 -0700166
167/**
168 * blkcache_invalidate() - discard the cache for a set of blocks
169 * because of a write or device (re)initialization.
170 *
Simon Glass5ea894a2022-10-29 19:47:08 -0600171 * @iftype - UCLASS_ID_ for type of device, or -1 for any
172 * @dev - device index of particular type, if @iftype is not -1
Eric Nelsone40cf342016-03-28 10:05:44 -0700173 */
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700174void blkcache_invalidate(int iftype, int dev);
Eric Nelsone40cf342016-03-28 10:05:44 -0700175
176/**
177 * blkcache_configure() - configure block cache
178 *
179 * @param blocks - maximum blocks per entry
180 * @param entries - maximum entries in cache
181 */
182void blkcache_configure(unsigned blocks, unsigned entries);
183
184/*
185 * statistics of the block cache
186 */
187struct block_cache_stats {
188 unsigned hits;
189 unsigned misses;
190 unsigned entries; /* current entry count */
191 unsigned max_blocks_per_entry;
192 unsigned max_entries;
193};
194
195/**
196 * get_blkcache_stats() - return statistics and reset
197 *
198 * @param stats - statistics are copied here
199 */
200void blkcache_stats(struct block_cache_stats *stats);
201
Simon Glass5ea894a2022-10-29 19:47:08 -0600202/** blkcache_free() - free all memory allocated to the block cache */
203void blkcache_free(void);
204
Eric Nelsone40cf342016-03-28 10:05:44 -0700205#else
206
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700207static inline int blkcache_read(int iftype, int dev,
208 lbaint_t start, lbaint_t blkcnt,
209 unsigned long blksz, void *buffer)
Eric Nelsone40cf342016-03-28 10:05:44 -0700210{
211 return 0;
212}
213
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700214static inline void blkcache_fill(int iftype, int dev,
215 lbaint_t start, lbaint_t blkcnt,
216 unsigned long blksz, void const *buffer) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700217
Eric Nelsonc8e4d2a2016-04-02 07:37:14 -0700218static inline void blkcache_invalidate(int iftype, int dev) {}
Eric Nelsone40cf342016-03-28 10:05:44 -0700219
Simon Glass5ea894a2022-10-29 19:47:08 -0600220static inline void blkcache_free(void) {}
221
Eric Nelsone40cf342016-03-28 10:05:44 -0700222#endif
223
Simon Glassc4d660d2017-07-04 13:31:19 -0600224#if CONFIG_IS_ENABLED(BLK)
Simon Glass09d71aa2016-02-29 15:25:55 -0700225struct udevice;
226
227/* Operations on block devices */
228struct blk_ops {
229 /**
230 * read() - read from a block device
231 *
232 * @dev: Device to read from
233 * @start: Start block number to read (0=first)
234 * @blkcnt: Number of blocks to read
235 * @buffer: Destination buffer for data read
236 * @return number of blocks read, or -ve error number (see the
237 * IS_ERR_VALUE() macro
238 */
239 unsigned long (*read)(struct udevice *dev, lbaint_t start,
240 lbaint_t blkcnt, void *buffer);
241
242 /**
243 * write() - write to a block device
244 *
245 * @dev: Device to write to
246 * @start: Start block number to write (0=first)
247 * @blkcnt: Number of blocks to write
248 * @buffer: Source buffer for data to write
249 * @return number of blocks written, or -ve error number (see the
250 * IS_ERR_VALUE() macro
251 */
252 unsigned long (*write)(struct udevice *dev, lbaint_t start,
253 lbaint_t blkcnt, const void *buffer);
254
255 /**
256 * erase() - erase a section of a block device
257 *
258 * @dev: Device to (partially) erase
259 * @start: Start block number to erase (0=first)
260 * @blkcnt: Number of blocks to erase
261 * @return number of blocks erased, or -ve error number (see the
262 * IS_ERR_VALUE() macro
263 */
264 unsigned long (*erase)(struct udevice *dev, lbaint_t start,
265 lbaint_t blkcnt);
Simon Glasscd0fb552016-05-01 13:52:30 -0600266
267 /**
268 * select_hwpart() - select a particular hardware partition
269 *
270 * Some devices (e.g. MMC) can support partitioning at the hardware
271 * level. This is quite separate from the normal idea of
272 * software-based partitions. MMC hardware partitions must be
273 * explicitly selected. Once selected only the region of the device
274 * covered by that partition is accessible.
275 *
276 * The MMC standard provides for two boot partitions (numbered 1 and 2),
277 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
278 *
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200279 * @dev: Block device to update
Simon Glasscd0fb552016-05-01 13:52:30 -0600280 * @hwpart: Hardware partition number to select. 0 means the raw
281 * device, 1 is the first partition, 2 is the second, etc.
282 * @return 0 if OK, -ve on error
283 */
284 int (*select_hwpart)(struct udevice *dev, int hwpart);
Simon Glass09d71aa2016-02-29 15:25:55 -0700285};
286
287#define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
288
289/*
290 * These functions should take struct udevice instead of struct blk_desc,
291 * but this is convenient for migration to driver model. Add a 'd' prefix
292 * to the function operations, so that blk_read(), etc. can be reserved for
293 * functions with the correct arguments.
294 */
295unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
296 lbaint_t blkcnt, void *buffer);
297unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
298 lbaint_t blkcnt, const void *buffer);
299unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
300 lbaint_t blkcnt);
301
302/**
Simon Glass606b9262022-10-20 18:22:54 -0600303 * blk_read() - Read from a block device
304 *
305 * @dev: Device to read from
306 * @start: Start block for the read
307 * @blkcnt: Number of blocks to read
308 * @buf: Place to put the data
309 * @return number of blocks read (which may be less than @blkcnt),
310 * or -ve on error. This never returns 0 unless @blkcnt is 0
311 */
312long blk_read(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
313 void *buffer);
314
315/**
316 * blk_write() - Write to a block device
317 *
318 * @dev: Device to write to
319 * @start: Start block for the write
320 * @blkcnt: Number of blocks to write
321 * @buf: Data to write
322 * @return number of blocks written (which may be less than @blkcnt),
323 * or -ve on error. This never returns 0 unless @blkcnt is 0
324 */
325long blk_write(struct udevice *dev, lbaint_t start, lbaint_t blkcnt,
326 const void *buffer);
327
328/**
329 * blk_erase() - Erase part of a block device
330 *
331 * @dev: Device to erase
332 * @start: Start block for the erase
333 * @blkcnt: Number of blocks to erase
334 * @return number of blocks erased (which may be less than @blkcnt),
335 * or -ve on error. This never returns 0 unless @blkcnt is 0
336 */
337long blk_erase(struct udevice *dev, lbaint_t start, lbaint_t blkcnt);
338
339/**
Simon Glass61392812017-04-23 20:02:05 -0600340 * blk_find_device() - Find a block device
341 *
342 * This function does not activate the device. The device will be returned
343 * whether or not it is activated.
344 *
Simon Glass8149b152022-09-17 09:00:09 -0600345 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass61392812017-04-23 20:02:05 -0600346 * @devnum: Device number (specific to each interface type)
347 * @devp: the device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100348 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glass61392812017-04-23 20:02:05 -0600349 */
Simon Glass8149b152022-09-17 09:00:09 -0600350int blk_find_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glass61392812017-04-23 20:02:05 -0600351
352/**
Simon Glass09d71aa2016-02-29 15:25:55 -0700353 * blk_get_device() - Find and probe a block device ready for use
354 *
Simon Glass8149b152022-09-17 09:00:09 -0600355 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass09d71aa2016-02-29 15:25:55 -0700356 * @devnum: Device number (specific to each interface type)
357 * @devp: the device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100358 * Return: 0 if found, -ENODEV if no device found, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700359 */
Simon Glass8149b152022-09-17 09:00:09 -0600360int blk_get_device(int uclass_id, int devnum, struct udevice **devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700361
362/**
363 * blk_first_device() - Find the first device for a given interface
364 *
365 * The device is probed ready for use
366 *
367 * @devnum: Device number (specific to each interface type)
368 * @devp: the device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100369 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700370 */
Simon Glass8149b152022-09-17 09:00:09 -0600371int blk_first_device(int uclass_id, struct udevice **devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700372
373/**
374 * blk_next_device() - Find the next device for a given interface
375 *
376 * This can be called repeatedly after blk_first_device() to iterate through
377 * all devices of the given interface type.
378 *
379 * The device is probed ready for use
380 *
381 * @devp: On entry, the previous device returned. On exit, the next
382 * device, if found
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100383 * Return: 0 if found, -ENODEV if no device, or other -ve error value
Simon Glass09d71aa2016-02-29 15:25:55 -0700384 */
385int blk_next_device(struct udevice **devp);
386
387/**
388 * blk_create_device() - Create a new block device
389 *
390 * @parent: Parent of the new device
391 * @drv_name: Driver name to use for the block device
392 * @name: Name for the device
Simon Glass8149b152022-09-17 09:00:09 -0600393 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass52138fd2016-05-01 11:36:28 -0600394 * @devnum: Device number, specific to the interface type, or -1 to
395 * allocate the next available number
Simon Glass09d71aa2016-02-29 15:25:55 -0700396 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200397 * @lba: Total number of blocks of the device
Simon Glass09d71aa2016-02-29 15:25:55 -0700398 * @devp: the new device (which has not been probed)
399 */
400int blk_create_device(struct udevice *parent, const char *drv_name,
Simon Glass8149b152022-09-17 09:00:09 -0600401 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200402 lbaint_t lba, struct udevice **devp);
Simon Glass09d71aa2016-02-29 15:25:55 -0700403
404/**
Simon Glass9107c972016-05-01 11:36:29 -0600405 * blk_create_devicef() - Create a new named block device
406 *
407 * @parent: Parent of the new device
408 * @drv_name: Driver name to use for the block device
409 * @name: Name for the device (parent name is prepended)
Simon Glass8149b152022-09-17 09:00:09 -0600410 * @uclass_id: Interface type (enum uclass_id_t)
Simon Glass9107c972016-05-01 11:36:29 -0600411 * @devnum: Device number, specific to the interface type, or -1 to
412 * allocate the next available number
413 * @blksz: Block size of the device in bytes (typically 512)
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200414 * @lba: Total number of blocks of the device
Simon Glass9107c972016-05-01 11:36:29 -0600415 * @devp: the new device (which has not been probed)
416 */
417int blk_create_devicef(struct udevice *parent, const char *drv_name,
Simon Glass8149b152022-09-17 09:00:09 -0600418 const char *name, int uclass_id, int devnum, int blksz,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200419 lbaint_t lba, struct udevice **devp);
Simon Glass9107c972016-05-01 11:36:29 -0600420
421/**
AKASHI Takahiro19b241c2021-12-10 15:49:29 +0900422 * blk_probe_or_unbind() - Try to probe
423 *
424 * Try to probe the device, primarily for enumerating partitions.
425 * If it fails, the device itself is unbound since it means that it won't
426 * work any more.
427 *
428 * @dev: The device to probe
429 * Return: 0 if OK, -ve on error
430 */
431int blk_probe_or_unbind(struct udevice *dev);
432
433/**
Simon Glass09d71aa2016-02-29 15:25:55 -0700434 * blk_unbind_all() - Unbind all device of the given interface type
435 *
436 * The devices are removed and then unbound.
437 *
Simon Glass8149b152022-09-17 09:00:09 -0600438 * @uclass_id: Interface type to unbind
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100439 * Return: 0 if OK, -ve on error
Simon Glass09d71aa2016-02-29 15:25:55 -0700440 */
Simon Glass8149b152022-09-17 09:00:09 -0600441int blk_unbind_all(int uclass_id);
Simon Glass09d71aa2016-02-29 15:25:55 -0700442
Simon Glass52138fd2016-05-01 11:36:28 -0600443/**
444 * blk_find_max_devnum() - find the maximum device number for an interface type
445 *
Simon Glass8149b152022-09-17 09:00:09 -0600446 * Finds the last allocated device number for an interface type @uclass_id. The
Simon Glass52138fd2016-05-01 11:36:28 -0600447 * next number is safe to use for a newly allocated device.
448 *
Simon Glass8149b152022-09-17 09:00:09 -0600449 * @uclass_id: Interface type to scan
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100450 * Return: maximum device number found, or -ENODEV if none, or other -ve on
Simon Glass52138fd2016-05-01 11:36:28 -0600451 * error
452 */
Simon Glass8149b152022-09-17 09:00:09 -0600453int blk_find_max_devnum(enum uclass_id uclass_id);
Simon Glass52138fd2016-05-01 11:36:28 -0600454
Simon Glasscd0fb552016-05-01 13:52:30 -0600455/**
Bin Mengc879eeb2018-10-15 02:21:09 -0700456 * blk_next_free_devnum() - get the next device number for an interface type
457 *
458 * Finds the next number that is safe to use for a newly allocated device for
Simon Glass8149b152022-09-17 09:00:09 -0600459 * an interface type @uclass_id.
Bin Mengc879eeb2018-10-15 02:21:09 -0700460 *
Simon Glass8149b152022-09-17 09:00:09 -0600461 * @uclass_id: Interface type to scan
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100462 * Return: next device number safe to use, or -ve on error
Bin Mengc879eeb2018-10-15 02:21:09 -0700463 */
Simon Glass8149b152022-09-17 09:00:09 -0600464int blk_next_free_devnum(enum uclass_id uclass_id);
Bin Mengc879eeb2018-10-15 02:21:09 -0700465
466/**
Simon Glasscd0fb552016-05-01 13:52:30 -0600467 * blk_select_hwpart() - select a hardware partition
468 *
469 * Select a hardware partition if the device supports it (typically MMC does)
470 *
471 * @dev: Device to update
472 * @hwpart: Partition number to select
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100473 * Return: 0 if OK, -ve on error
Simon Glasscd0fb552016-05-01 13:52:30 -0600474 */
475int blk_select_hwpart(struct udevice *dev, int hwpart);
476
Tom Rini4bdb49a2017-06-10 10:01:05 -0400477/**
Simon Glass41e75102022-10-29 19:47:14 -0600478 * blk_find_from_parent() - find a block device by looking up its parent
479 *
480 * All block devices have a parent 'media' device which provides the block
481 * driver for the block device, ensuring that access to the underlying medium
482 * is available.
483 *
484 * The block device is not activated by this function. See
485 * blk_get_from_parent() for that.
486 *
487 * @parent: Media device
488 * @devp: Returns the associated block device, if any
489 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
490 * UCLASS_BLK child
491 */
492int blk_find_from_parent(struct udevice *parent, struct udevice **devp);
493
494/**
Tom Rini4bdb49a2017-06-10 10:01:05 -0400495 * blk_get_from_parent() - obtain a block device by looking up its parent
496 *
Simon Glass41e75102022-10-29 19:47:14 -0600497 * All block devices have a parent 'media' device which provides the block
498 * driver for the block device, ensuring that access to the underlying medium
499 * is available.
500 *
501 * The block device is probed and ready for use.
502 *
503 * @parent: Media device
504 * @devp: Returns the associated block device, if any
505 * Returns: 0 if OK, -ENODEV if @parent is not a media device and has no
506 * UCLASS_BLK child
Tom Rini4bdb49a2017-06-10 10:01:05 -0400507 */
508int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
509
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800510/**
Simon Glass87571b72022-04-24 23:31:03 -0600511 * blk_get_devtype() - Get the device type of a block device
512 *
513 * @dev: Block device to check
514 * Return: device tree, i.e. the uclass name of its parent, e.g. "mmc"
515 */
516const char *blk_get_devtype(struct udevice *dev);
517
518/**
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800519 * blk_get_by_device() - Get the block device descriptor for the given device
Simon Glass606b9262022-10-20 18:22:54 -0600520 * @dev: Instance of a storage device (the parent of the block device)
Tien Fong Cheebc53d262018-07-06 16:26:36 +0800521 *
522 * Return: With block device descriptor on success , NULL if there is no such
523 * block device.
524 */
525struct blk_desc *blk_get_by_device(struct udevice *dev);
526
Simon Glass09d71aa2016-02-29 15:25:55 -0700527#else
528#include <errno.h>
Simon Glass2a981dc2016-02-29 15:25:52 -0700529/*
530 * These functions should take struct udevice instead of struct blk_desc,
531 * but this is convenient for migration to driver model. Add a 'd' prefix
532 * to the function operations, so that blk_read(), etc. can be reserved for
533 * functions with the correct arguments.
534 */
535static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
536 lbaint_t blkcnt, void *buffer)
537{
Eric Nelsone40cf342016-03-28 10:05:44 -0700538 ulong blks_read;
Simon Glass8149b152022-09-17 09:00:09 -0600539 if (blkcache_read(block_dev->uclass_id, block_dev->devnum,
Eric Nelsone40cf342016-03-28 10:05:44 -0700540 start, blkcnt, block_dev->blksz, buffer))
541 return blkcnt;
542
Simon Glass2a981dc2016-02-29 15:25:52 -0700543 /*
544 * We could check if block_read is NULL and return -ENOSYS. But this
545 * bloats the code slightly (cause some board to fail to build), and
546 * it would be an error to try an operation that does not exist.
547 */
Eric Nelsone40cf342016-03-28 10:05:44 -0700548 blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
549 if (blks_read == blkcnt)
Simon Glass8149b152022-09-17 09:00:09 -0600550 blkcache_fill(block_dev->uclass_id, block_dev->devnum,
Eric Nelsone40cf342016-03-28 10:05:44 -0700551 start, blkcnt, block_dev->blksz, buffer);
552
553 return blks_read;
Simon Glass2a981dc2016-02-29 15:25:52 -0700554}
555
556static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
557 lbaint_t blkcnt, const void *buffer)
558{
Simon Glass8149b152022-09-17 09:00:09 -0600559 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700560 return block_dev->block_write(block_dev, start, blkcnt, buffer);
561}
562
563static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
564 lbaint_t blkcnt)
565{
Simon Glass8149b152022-09-17 09:00:09 -0600566 blkcache_invalidate(block_dev->uclass_id, block_dev->devnum);
Simon Glass2a981dc2016-02-29 15:25:52 -0700567 return block_dev->block_erase(block_dev, start, blkcnt);
568}
Simon Glass6eef6ea2016-05-01 11:36:03 -0600569
570/**
571 * struct blk_driver - Driver for block interface types
572 *
573 * This provides access to the block devices for each interface type. One
574 * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
575 * type that is to be supported.
576 *
Simon Glass8149b152022-09-17 09:00:09 -0600577 * @uclass_idname: Interface type name
578 * @uclass_id: Interface type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600579 * @max_devs: Maximum number of devices supported
580 * @desc: Pointer to list of devices for this interface type,
581 * or NULL to use @get_dev() instead
582 */
583struct blk_driver {
Simon Glass8149b152022-09-17 09:00:09 -0600584 const char *uclass_idname;
585 enum uclass_id uclass_id;
Simon Glass6eef6ea2016-05-01 11:36:03 -0600586 int max_devs;
587 struct blk_desc *desc;
588 /**
589 * get_dev() - get a pointer to a block device given its number
590 *
591 * Each interface allocates its own devices and typically
592 * struct blk_desc is contained with the interface's data structure.
593 * There is no global numbering for block devices. This method allows
594 * the device for an interface type to be obtained when @desc is NULL.
595 *
596 * @devnum: Device number (0 for first device on that interface,
597 * 1 for second, etc.
598 * @descp: Returns pointer to the block device on success
599 * @return 0 if OK, -ve on error
600 */
601 int (*get_dev)(int devnum, struct blk_desc **descp);
602
603 /**
604 * select_hwpart() - Select a hardware partition
605 *
606 * Some devices (e.g. MMC) can support partitioning at the hardware
607 * level. This is quite separate from the normal idea of
608 * software-based partitions. MMC hardware partitions must be
609 * explicitly selected. Once selected only the region of the device
610 * covered by that partition is accessible.
611 *
612 * The MMC standard provides for two boot partitions (numbered 1 and 2),
613 * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
614 * Partition 0 is the main user-data partition.
615 *
616 * @desc: Block device descriptor
617 * @hwpart: Hardware partition number to select. 0 means the main
618 * user-data partition, 1 is the first partition, 2 is
619 * the second, etc.
620 * @return 0 if OK, other value for an error
621 */
622 int (*select_hwpart)(struct blk_desc *desc, int hwpart);
623};
624
625/*
626 * Declare a new U-Boot legacy block driver. New drivers should use driver
627 * model (UCLASS_BLK).
628 */
629#define U_BOOT_LEGACY_BLK(__name) \
630 ll_entry_declare(struct blk_driver, __name, blk_driver)
631
Simon Glass8149b152022-09-17 09:00:09 -0600632struct blk_driver *blk_driver_lookup_type(int uclass_id);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600633
Simon Glass09d71aa2016-02-29 15:25:55 -0700634#endif /* !CONFIG_BLK */
Simon Glass2a981dc2016-02-29 15:25:52 -0700635
Simon Glass6eef6ea2016-05-01 11:36:03 -0600636/**
Simon Glass8149b152022-09-17 09:00:09 -0600637 * blk_get_devnum_by_uclass_idname() - Get a block device by type and number
Simon Glass6eef6ea2016-05-01 11:36:03 -0600638 *
639 * This looks through the available block devices of the given type, returning
640 * the one with the given @devnum.
641 *
Simon Glass8149b152022-09-17 09:00:09 -0600642 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600643 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100644 * Return: point to block device descriptor, or NULL if not found
Simon Glass6eef6ea2016-05-01 11:36:03 -0600645 */
Simon Glass8149b152022-09-17 09:00:09 -0600646struct blk_desc *blk_get_devnum_by_uclass_id(enum uclass_id uclass_id, int devnum);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600647
648/**
Simon Glass8149b152022-09-17 09:00:09 -0600649 * blk_get_devnum_by_uclass_id() - Get a block device by type name, and number
Simon Glass6eef6ea2016-05-01 11:36:03 -0600650 *
Simon Glass8149b152022-09-17 09:00:09 -0600651 * This looks up the block device type based on @uclass_idname, then calls
652 * blk_get_devnum_by_uclass_id().
Simon Glass6eef6ea2016-05-01 11:36:03 -0600653 *
Simon Glass8149b152022-09-17 09:00:09 -0600654 * @uclass_idname: Block device type name
Simon Glass6eef6ea2016-05-01 11:36:03 -0600655 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100656 * Return: point to block device descriptor, or NULL if not found
Simon Glass6eef6ea2016-05-01 11:36:03 -0600657 */
Simon Glass8149b152022-09-17 09:00:09 -0600658struct blk_desc *blk_get_devnum_by_uclass_idname(const char *uclass_idname,
Simon Glass6eef6ea2016-05-01 11:36:03 -0600659 int devnum);
660
661/**
662 * blk_dselect_hwpart() - select a hardware partition
663 *
664 * This selects a hardware partition (such as is supported by MMC). The block
665 * device size may change as this effectively points the block device to a
666 * partition at the hardware level. See the select_hwpart() method above.
667 *
668 * @desc: Block device descriptor for the device to select
669 * @hwpart: Partition number to select
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100670 * Return: 0 if OK, -ve on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600671 */
672int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
673
674/**
675 * blk_list_part() - list the partitions for block devices of a given type
676 *
Simon Glass8149b152022-09-17 09:00:09 -0600677 * This looks up the partition type for each block device of type @uclass_id,
Simon Glass6eef6ea2016-05-01 11:36:03 -0600678 * then displays a list of partitions.
679 *
Simon Glass8149b152022-09-17 09:00:09 -0600680 * @uclass_id: Block device type
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100681 * Return: 0 if OK, -ENODEV if there is none of that type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600682 */
Simon Glass8149b152022-09-17 09:00:09 -0600683int blk_list_part(enum uclass_id uclass_id);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600684
685/**
686 * blk_list_devices() - list the block devices of a given type
687 *
Simon Glass8149b152022-09-17 09:00:09 -0600688 * This lists each block device of the type @uclass_id, showing the capacity
Simon Glass6eef6ea2016-05-01 11:36:03 -0600689 * as well as type-specific information.
690 *
Simon Glass8149b152022-09-17 09:00:09 -0600691 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600692 */
Simon Glass8149b152022-09-17 09:00:09 -0600693void blk_list_devices(enum uclass_id uclass_id);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600694
695/**
696 * blk_show_device() - show information about a given block device
697 *
698 * This shows the block device capacity as well as type-specific information.
699 *
Simon Glass8149b152022-09-17 09:00:09 -0600700 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600701 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100702 * Return: 0 if OK, -ENODEV for invalid device number
Simon Glass6eef6ea2016-05-01 11:36:03 -0600703 */
Simon Glass8149b152022-09-17 09:00:09 -0600704int blk_show_device(enum uclass_id uclass_id, int devnum);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600705
706/**
707 * blk_print_device_num() - show information about a given block device
708 *
709 * This is similar to blk_show_device() but returns an error if the block
710 * device type is unknown.
711 *
Simon Glass8149b152022-09-17 09:00:09 -0600712 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600713 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100714 * Return: 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
Simon Glass6eef6ea2016-05-01 11:36:03 -0600715 * device is not connected
716 */
Simon Glass8149b152022-09-17 09:00:09 -0600717int blk_print_device_num(enum uclass_id uclass_id, int devnum);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600718
719/**
720 * blk_print_part_devnum() - print the partition information for a device
721 *
Simon Glass8149b152022-09-17 09:00:09 -0600722 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600723 * @devnum: Device number
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100724 * Return: 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
Simon Glass6eef6ea2016-05-01 11:36:03 -0600725 * the interface type is not supported, other -ve on other error
726 */
Simon Glass8149b152022-09-17 09:00:09 -0600727int blk_print_part_devnum(enum uclass_id uclass_id, int devnum);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600728
729/**
730 * blk_read_devnum() - read blocks from a device
731 *
Simon Glass8149b152022-09-17 09:00:09 -0600732 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600733 * @devnum: Device number
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200734 * @start: Start block number to read (0=first)
Simon Glass6eef6ea2016-05-01 11:36:03 -0600735 * @blkcnt: Number of blocks to read
736 * @buffer: Address to write data to
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100737 * Return: number of blocks read, or -ve error number on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600738 */
Simon Glass8149b152022-09-17 09:00:09 -0600739ulong blk_read_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass6eef6ea2016-05-01 11:36:03 -0600740 lbaint_t blkcnt, void *buffer);
741
742/**
743 * blk_write_devnum() - write blocks to a device
744 *
Simon Glass8149b152022-09-17 09:00:09 -0600745 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600746 * @devnum: Device number
Mattijs Korpershoek601d4e12022-10-17 09:35:04 +0200747 * @start: Start block number to write (0=first)
Simon Glass6eef6ea2016-05-01 11:36:03 -0600748 * @blkcnt: Number of blocks to write
749 * @buffer: Address to read data from
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100750 * Return: number of blocks written, or -ve error number on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600751 */
Simon Glass8149b152022-09-17 09:00:09 -0600752ulong blk_write_devnum(enum uclass_id uclass_id, int devnum, lbaint_t start,
Simon Glass6eef6ea2016-05-01 11:36:03 -0600753 lbaint_t blkcnt, const void *buffer);
754
755/**
756 * blk_select_hwpart_devnum() - select a hardware partition
757 *
758 * This is similar to blk_dselect_hwpart() but it looks up the interface and
759 * device number.
760 *
Simon Glass8149b152022-09-17 09:00:09 -0600761 * @uclass_id: Block device type
Simon Glass6eef6ea2016-05-01 11:36:03 -0600762 * @devnum: Device number
763 * @hwpart: Partition number to select
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100764 * Return: 0 if OK, -ve on error
Simon Glass6eef6ea2016-05-01 11:36:03 -0600765 */
Simon Glass8149b152022-09-17 09:00:09 -0600766int blk_select_hwpart_devnum(enum uclass_id uclass_id, int devnum, int hwpart);
Simon Glass6eef6ea2016-05-01 11:36:03 -0600767
Simon Glass6faa4ed2017-07-29 11:34:53 -0600768/**
Simon Glass8149b152022-09-17 09:00:09 -0600769 * blk_get_uclass_name() - Get the name of an interface type
Simon Glass6faa4ed2017-07-29 11:34:53 -0600770 *
Simon Glass8149b152022-09-17 09:00:09 -0600771 * @uclass_id: Interface type to check
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100772 * Return: name of interface, or NULL if none
Simon Glass6faa4ed2017-07-29 11:34:53 -0600773 */
Simon Glass8149b152022-09-17 09:00:09 -0600774const char *blk_get_uclass_name(enum uclass_id uclass_id);
Simon Glass6faa4ed2017-07-29 11:34:53 -0600775
Simon Glass4395f662017-07-29 11:34:54 -0600776/**
777 * blk_common_cmd() - handle common commands with block devices
778 *
779 * @args: Number of arguments to the command (argv[0] is the command itself)
780 * @argv: Command arguments
Simon Glass8149b152022-09-17 09:00:09 -0600781 * @uclass_id: Interface type
Simon Glass4395f662017-07-29 11:34:54 -0600782 * @cur_devnump: Current device number for this interface type
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100783 * Return: 0 if OK, CMD_RET_ERROR on error
Simon Glass4395f662017-07-29 11:34:54 -0600784 */
Simon Glass8149b152022-09-17 09:00:09 -0600785int blk_common_cmd(int argc, char *const argv[], enum uclass_id uclass_id,
Simon Glass4395f662017-07-29 11:34:54 -0600786 int *cur_devnump);
787
Simon Glass96f37b02021-07-05 16:32:59 -0600788enum blk_flag_t {
789 BLKF_FIXED = 1 << 0,
790 BLKF_REMOVABLE = 1 << 1,
791 BLKF_BOTH = BLKF_FIXED | BLKF_REMOVABLE,
792};
793
794/**
795 * blk_first_device_err() - Get the first block device
796 *
797 * The device returned is probed if necessary, and ready for use
798 *
799 * @flags: Indicates type of device to return
800 * @devp: Returns pointer to the first device in that uclass, or NULL if none
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100801 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glass96f37b02021-07-05 16:32:59 -0600802 */
803int blk_first_device_err(enum blk_flag_t flags, struct udevice **devp);
804
805/**
806 * blk_next_device_err() - Get the next block device
807 *
808 * The device returned is probed if necessary, and ready for use
809 *
810 * @flags: Indicates type of device to return
811 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
812 * to the next device in the uclass if no error occurred, or -ENODEV if
813 * there is no next device.
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100814 * Return: 0 if found, -ENODEV if not found, other -ve on error
Simon Glass96f37b02021-07-05 16:32:59 -0600815 */
816int blk_next_device_err(enum blk_flag_t flags, struct udevice **devp);
817
818/**
Simon Glass49e86682022-02-28 12:08:35 -0700819 * blk_find_first() - Return the first matching block device
820 * @flags: Indicates type of device to return
821 * @devp: Returns pointer to device, or NULL on error
822 *
823 * The device is not prepared for use - this is an internal function.
824 * The function uclass_get_device_tail() can be used to probe the device.
825 *
826 * Note that some devices are considered removable until they have been probed
827 *
828 * @return 0 if found, -ENODEV if not found
829 */
830int blk_find_first(enum blk_flag_t flags, struct udevice **devp);
831
832/**
833 * blk_find_next() - Return the next matching block device
834 * @flags: Indicates type of device to return
835 * @devp: On entry, pointer to device to lookup. On exit, returns pointer
836 * to the next device in the same uclass, or NULL if none
837 *
838 * The device is not prepared for use - this is an internal function.
839 * The function uclass_get_device_tail() can be used to probe the device.
840 *
841 * Note that some devices are considered removable until they have been probed
842 *
843 * @return 0 if found, -ENODEV if not found
844 */
845int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
846
847/**
848 * blk_foreach() - iterate through block devices
849 *
850 * This creates a for() loop which works through the available block devices in
851 * order from start to end.
852 *
853 * If for some reason the uclass cannot be found, this does nothing.
854 *
855 * @_flags: Indicates type of device to return
856 * @_pos: struct udevice * to hold the current device. Set to NULL when there
857 * are no more devices.
858 */
859#define blk_foreach(_flags, _pos) \
860 for (int _ret = blk_find_first(_flags, &_pos); !_ret && _pos; \
861 _ret = blk_find_next(_flags, &_pos))
862
863/**
Simon Glass96f37b02021-07-05 16:32:59 -0600864 * blk_foreach_probe() - Helper function to iteration through block devices
865 *
866 * This creates a for() loop which works through the available devices in
867 * a uclass in order from start to end. Devices are probed if necessary,
868 * and ready for use.
869 *
870 * @flags: Indicates type of device to return
871 * @dev: struct udevice * to hold the current device. Set to NULL when there
872 * are no more devices.
873 */
874#define blk_foreach_probe(flags, pos) \
875 for (int _ret = blk_first_device_err(flags, &(pos)); \
876 !_ret && pos; \
877 _ret = blk_next_device_err(flags, &(pos)))
878
879/**
880 * blk_count_devices() - count the number of devices of a particular type
881 *
882 * @flags: Indicates type of device to find
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100883 * Return: number of devices matching those flags
Simon Glass96f37b02021-07-05 16:32:59 -0600884 */
885int blk_count_devices(enum blk_flag_t flag);
886
Simon Glass1a736612016-02-29 15:25:39 -0700887#endif