1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * (C) Copyright 2000-2004
3*4882a593Smuzhiyun * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #ifndef BLK_H
9*4882a593Smuzhiyun #define BLK_H
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <efi.h>
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #ifdef CONFIG_SYS_64BIT_LBA
14*4882a593Smuzhiyun typedef uint64_t lbaint_t;
15*4882a593Smuzhiyun #define LBAFlength "ll"
16*4882a593Smuzhiyun #else
17*4882a593Smuzhiyun typedef ulong lbaint_t;
18*4882a593Smuzhiyun #define LBAFlength "l"
19*4882a593Smuzhiyun #endif
20*4882a593Smuzhiyun #define LBAF "%" LBAFlength "x"
21*4882a593Smuzhiyun #define LBAFU "%" LBAFlength "u"
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /* Interface types: */
24*4882a593Smuzhiyun enum if_type {
25*4882a593Smuzhiyun IF_TYPE_UNKNOWN = 0,
26*4882a593Smuzhiyun IF_TYPE_IDE,
27*4882a593Smuzhiyun IF_TYPE_SCSI,
28*4882a593Smuzhiyun IF_TYPE_ATAPI,
29*4882a593Smuzhiyun IF_TYPE_USB,
30*4882a593Smuzhiyun IF_TYPE_DOC,
31*4882a593Smuzhiyun IF_TYPE_MMC,
32*4882a593Smuzhiyun IF_TYPE_SD,
33*4882a593Smuzhiyun IF_TYPE_SATA,
34*4882a593Smuzhiyun IF_TYPE_HOST,
35*4882a593Smuzhiyun IF_TYPE_SYSTEMACE,
36*4882a593Smuzhiyun IF_TYPE_NVME,
37*4882a593Smuzhiyun IF_TYPE_RKNAND,
38*4882a593Smuzhiyun IF_TYPE_SPINAND,
39*4882a593Smuzhiyun IF_TYPE_SPINOR,
40*4882a593Smuzhiyun IF_TYPE_RAMDISK,
41*4882a593Smuzhiyun IF_TYPE_MTD,
42*4882a593Smuzhiyun IF_TYPE_COUNT, /* Number of interface types */
43*4882a593Smuzhiyun };
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /* define mtd device devnum */
46*4882a593Smuzhiyun #define BLK_MTD_NAND 0
47*4882a593Smuzhiyun #define BLK_MTD_SPI_NAND 1
48*4882a593Smuzhiyun #define BLK_MTD_SPI_NOR 2
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #define BLK_VEN_SIZE 40
51*4882a593Smuzhiyun #define BLK_PRD_SIZE 20
52*4882a593Smuzhiyun #define BLK_REV_SIZE 8
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun /* define block device operation flags */
55*4882a593Smuzhiyun #define BLK_PRE_RW BIT(0) /* Block prepare read & write*/
56*4882a593Smuzhiyun #define BLK_MTD_CONT_WRITE BIT(1) /* Special for Nand device P/E */
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun /*
59*4882a593Smuzhiyun * Identifies the partition table type (ie. MBR vs GPT GUID) signature
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun enum sig_type {
62*4882a593Smuzhiyun SIG_TYPE_NONE,
63*4882a593Smuzhiyun SIG_TYPE_MBR,
64*4882a593Smuzhiyun SIG_TYPE_GUID,
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun SIG_TYPE_COUNT /* Number of signature types */
67*4882a593Smuzhiyun };
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun * With driver model (CONFIG_BLK) this is uclass platform data, accessible
71*4882a593Smuzhiyun * with dev_get_uclass_platdata(dev)
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun struct blk_desc {
74*4882a593Smuzhiyun /*
75*4882a593Smuzhiyun * TODO: With driver model we should be able to use the parent
76*4882a593Smuzhiyun * device's uclass instead.
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun enum if_type if_type; /* type of the interface */
79*4882a593Smuzhiyun int devnum; /* device number */
80*4882a593Smuzhiyun unsigned char part_type; /* partition type */
81*4882a593Smuzhiyun unsigned char target; /* target SCSI ID */
82*4882a593Smuzhiyun unsigned char lun; /* target LUN */
83*4882a593Smuzhiyun unsigned char hwpart; /* HW partition, e.g. for eMMC */
84*4882a593Smuzhiyun unsigned char type; /* device type */
85*4882a593Smuzhiyun unsigned char removable; /* removable device */
86*4882a593Smuzhiyun unsigned char op_flag; /* Some special operation flags */
87*4882a593Smuzhiyun #ifdef CONFIG_LBA48
88*4882a593Smuzhiyun /* device can use 48bit addr (ATA/ATAPI v7) */
89*4882a593Smuzhiyun unsigned char lba48;
90*4882a593Smuzhiyun #endif
91*4882a593Smuzhiyun lbaint_t lba; /* number of blocks */
92*4882a593Smuzhiyun unsigned long blksz; /* block size */
93*4882a593Smuzhiyun int log2blksz; /* for convenience: log2(blksz) */
94*4882a593Smuzhiyun char vendor[BLK_VEN_SIZE + 1]; /* device vendor string */
95*4882a593Smuzhiyun char product[BLK_PRD_SIZE + 1]; /* device product number */
96*4882a593Smuzhiyun char revision[BLK_REV_SIZE + 1]; /* firmware revision */
97*4882a593Smuzhiyun enum sig_type sig_type; /* Partition table signature type */
98*4882a593Smuzhiyun union {
99*4882a593Smuzhiyun uint32_t mbr_sig; /* MBR integer signature */
100*4882a593Smuzhiyun efi_guid_t guid_sig; /* GPT GUID Signature */
101*4882a593Smuzhiyun };
102*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(BLK)
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun * For now we have a few functions which take struct blk_desc as a
105*4882a593Smuzhiyun * parameter. This field allows them to look up the associated
106*4882a593Smuzhiyun * device. Once these functions are removed we can drop this field.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun struct udevice *bdev;
109*4882a593Smuzhiyun #else
110*4882a593Smuzhiyun unsigned long (*block_read)(struct blk_desc *block_dev,
111*4882a593Smuzhiyun lbaint_t start,
112*4882a593Smuzhiyun lbaint_t blkcnt,
113*4882a593Smuzhiyun void *buffer);
114*4882a593Smuzhiyun unsigned long (*block_write)(struct blk_desc *block_dev,
115*4882a593Smuzhiyun lbaint_t start,
116*4882a593Smuzhiyun lbaint_t blkcnt,
117*4882a593Smuzhiyun const void *buffer);
118*4882a593Smuzhiyun unsigned long (*block_erase)(struct blk_desc *block_dev,
119*4882a593Smuzhiyun lbaint_t start,
120*4882a593Smuzhiyun lbaint_t blkcnt);
121*4882a593Smuzhiyun void *priv; /* driver private struct pointer */
122*4882a593Smuzhiyun #endif
123*4882a593Smuzhiyun };
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz))
126*4882a593Smuzhiyun #define PAD_TO_BLOCKSIZE(size, blk_desc) \
127*4882a593Smuzhiyun (PAD_SIZE(size, blk_desc->blksz))
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun #ifdef CONFIG_BLOCK_CACHE
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun * blkcache_read() - attempt to read a set of blocks from cache
132*4882a593Smuzhiyun *
133*4882a593Smuzhiyun * @param iftype - IF_TYPE_x for type of device
134*4882a593Smuzhiyun * @param dev - device index of particular type
135*4882a593Smuzhiyun * @param start - starting block number
136*4882a593Smuzhiyun * @param blkcnt - number of blocks to read
137*4882a593Smuzhiyun * @param blksz - size in bytes of each block
138*4882a593Smuzhiyun * @param buf - buffer to contain cached data
139*4882a593Smuzhiyun *
140*4882a593Smuzhiyun * @return - '1' if block returned from cache, '0' otherwise.
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun int blkcache_read(int iftype, int dev,
143*4882a593Smuzhiyun lbaint_t start, lbaint_t blkcnt,
144*4882a593Smuzhiyun unsigned long blksz, void *buffer);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /**
147*4882a593Smuzhiyun * blkcache_fill() - make data read from a block device available
148*4882a593Smuzhiyun * to the block cache
149*4882a593Smuzhiyun *
150*4882a593Smuzhiyun * @param iftype - IF_TYPE_x for type of device
151*4882a593Smuzhiyun * @param dev - device index of particular type
152*4882a593Smuzhiyun * @param start - starting block number
153*4882a593Smuzhiyun * @param blkcnt - number of blocks available
154*4882a593Smuzhiyun * @param blksz - size in bytes of each block
155*4882a593Smuzhiyun * @param buf - buffer containing data to cache
156*4882a593Smuzhiyun *
157*4882a593Smuzhiyun */
158*4882a593Smuzhiyun void blkcache_fill(int iftype, int dev,
159*4882a593Smuzhiyun lbaint_t start, lbaint_t blkcnt,
160*4882a593Smuzhiyun unsigned long blksz, void const *buffer);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /**
163*4882a593Smuzhiyun * blkcache_invalidate() - discard the cache for a set of blocks
164*4882a593Smuzhiyun * because of a write or device (re)initialization.
165*4882a593Smuzhiyun *
166*4882a593Smuzhiyun * @param iftype - IF_TYPE_x for type of device
167*4882a593Smuzhiyun * @param dev - device index of particular type
168*4882a593Smuzhiyun */
169*4882a593Smuzhiyun void blkcache_invalidate(int iftype, int dev);
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun /**
172*4882a593Smuzhiyun * blkcache_configure() - configure block cache
173*4882a593Smuzhiyun *
174*4882a593Smuzhiyun * @param blocks - maximum blocks per entry
175*4882a593Smuzhiyun * @param entries - maximum entries in cache
176*4882a593Smuzhiyun */
177*4882a593Smuzhiyun void blkcache_configure(unsigned blocks, unsigned entries);
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun * statistics of the block cache
181*4882a593Smuzhiyun */
182*4882a593Smuzhiyun struct block_cache_stats {
183*4882a593Smuzhiyun unsigned hits;
184*4882a593Smuzhiyun unsigned misses;
185*4882a593Smuzhiyun unsigned entries; /* current entry count */
186*4882a593Smuzhiyun unsigned max_blocks_per_entry;
187*4882a593Smuzhiyun unsigned max_entries;
188*4882a593Smuzhiyun };
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun * get_blkcache_stats() - return statistics and reset
192*4882a593Smuzhiyun *
193*4882a593Smuzhiyun * @param stats - statistics are copied here
194*4882a593Smuzhiyun */
195*4882a593Smuzhiyun void blkcache_stats(struct block_cache_stats *stats);
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun #else
198*4882a593Smuzhiyun
blkcache_read(int iftype,int dev,lbaint_t start,lbaint_t blkcnt,unsigned long blksz,void * buffer)199*4882a593Smuzhiyun static inline int blkcache_read(int iftype, int dev,
200*4882a593Smuzhiyun lbaint_t start, lbaint_t blkcnt,
201*4882a593Smuzhiyun unsigned long blksz, void *buffer)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun return 0;
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
blkcache_fill(int iftype,int dev,lbaint_t start,lbaint_t blkcnt,unsigned long blksz,void const * buffer)206*4882a593Smuzhiyun static inline void blkcache_fill(int iftype, int dev,
207*4882a593Smuzhiyun lbaint_t start, lbaint_t blkcnt,
208*4882a593Smuzhiyun unsigned long blksz, void const *buffer) {}
209*4882a593Smuzhiyun
blkcache_invalidate(int iftype,int dev)210*4882a593Smuzhiyun static inline void blkcache_invalidate(int iftype, int dev) {}
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun #endif
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun #if CONFIG_IS_ENABLED(BLK)
215*4882a593Smuzhiyun struct udevice;
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* Operations on block devices */
218*4882a593Smuzhiyun struct blk_ops {
219*4882a593Smuzhiyun /**
220*4882a593Smuzhiyun * read() - read from a block device
221*4882a593Smuzhiyun *
222*4882a593Smuzhiyun * @dev: Device to read from
223*4882a593Smuzhiyun * @start: Start block number to read (0=first)
224*4882a593Smuzhiyun * @blkcnt: Number of blocks to read
225*4882a593Smuzhiyun * @buffer: Destination buffer for data read
226*4882a593Smuzhiyun * @return number of blocks read, or -ve error number (see the
227*4882a593Smuzhiyun * IS_ERR_VALUE() macro
228*4882a593Smuzhiyun */
229*4882a593Smuzhiyun unsigned long (*read)(struct udevice *dev, lbaint_t start,
230*4882a593Smuzhiyun lbaint_t blkcnt, void *buffer);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /**
233*4882a593Smuzhiyun * write() - write to a block device
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * @dev: Device to write to
236*4882a593Smuzhiyun * @start: Start block number to write (0=first)
237*4882a593Smuzhiyun * @blkcnt: Number of blocks to write
238*4882a593Smuzhiyun * @buffer: Source buffer for data to write
239*4882a593Smuzhiyun * @return number of blocks written, or -ve error number (see the
240*4882a593Smuzhiyun * IS_ERR_VALUE() macro
241*4882a593Smuzhiyun */
242*4882a593Smuzhiyun unsigned long (*write)(struct udevice *dev, lbaint_t start,
243*4882a593Smuzhiyun lbaint_t blkcnt, const void *buffer);
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun /**
246*4882a593Smuzhiyun * erase() - erase a section of a block device
247*4882a593Smuzhiyun *
248*4882a593Smuzhiyun * @dev: Device to (partially) erase
249*4882a593Smuzhiyun * @start: Start block number to erase (0=first)
250*4882a593Smuzhiyun * @blkcnt: Number of blocks to erase
251*4882a593Smuzhiyun * @return number of blocks erased, or -ve error number (see the
252*4882a593Smuzhiyun * IS_ERR_VALUE() macro
253*4882a593Smuzhiyun */
254*4882a593Smuzhiyun unsigned long (*erase)(struct udevice *dev, lbaint_t start,
255*4882a593Smuzhiyun lbaint_t blkcnt);
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /**
258*4882a593Smuzhiyun * select_hwpart() - select a particular hardware partition
259*4882a593Smuzhiyun *
260*4882a593Smuzhiyun * Some devices (e.g. MMC) can support partitioning at the hardware
261*4882a593Smuzhiyun * level. This is quite separate from the normal idea of
262*4882a593Smuzhiyun * software-based partitions. MMC hardware partitions must be
263*4882a593Smuzhiyun * explicitly selected. Once selected only the region of the device
264*4882a593Smuzhiyun * covered by that partition is accessible.
265*4882a593Smuzhiyun *
266*4882a593Smuzhiyun * The MMC standard provides for two boot partitions (numbered 1 and 2),
267*4882a593Smuzhiyun * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * @desc: Block device to update
270*4882a593Smuzhiyun * @hwpart: Hardware partition number to select. 0 means the raw
271*4882a593Smuzhiyun * device, 1 is the first partition, 2 is the second, etc.
272*4882a593Smuzhiyun * @return 0 if OK, -ve on error
273*4882a593Smuzhiyun */
274*4882a593Smuzhiyun int (*select_hwpart)(struct udevice *dev, int hwpart);
275*4882a593Smuzhiyun };
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops)
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun /*
280*4882a593Smuzhiyun * These functions should take struct udevice instead of struct blk_desc,
281*4882a593Smuzhiyun * but this is convenient for migration to driver model. Add a 'd' prefix
282*4882a593Smuzhiyun * to the function operations, so that blk_read(), etc. can be reserved for
283*4882a593Smuzhiyun * functions with the correct arguments.
284*4882a593Smuzhiyun */
285*4882a593Smuzhiyun unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start,
286*4882a593Smuzhiyun lbaint_t blkcnt, void *buffer);
287*4882a593Smuzhiyun unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
288*4882a593Smuzhiyun lbaint_t blkcnt, const void *buffer);
289*4882a593Smuzhiyun unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start,
290*4882a593Smuzhiyun lbaint_t blkcnt);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun /**
293*4882a593Smuzhiyun * blk_find_device() - Find a block device
294*4882a593Smuzhiyun *
295*4882a593Smuzhiyun * This function does not activate the device. The device will be returned
296*4882a593Smuzhiyun * whether or not it is activated.
297*4882a593Smuzhiyun *
298*4882a593Smuzhiyun * @if_type: Interface type (enum if_type_t)
299*4882a593Smuzhiyun * @devnum: Device number (specific to each interface type)
300*4882a593Smuzhiyun * @devp: the device, if found
301*4882a593Smuzhiyun * @return 0 if found, -ENODEV if no device found, or other -ve error value
302*4882a593Smuzhiyun */
303*4882a593Smuzhiyun int blk_find_device(int if_type, int devnum, struct udevice **devp);
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun /**
306*4882a593Smuzhiyun * blk_get_device() - Find and probe a block device ready for use
307*4882a593Smuzhiyun *
308*4882a593Smuzhiyun * @if_type: Interface type (enum if_type_t)
309*4882a593Smuzhiyun * @devnum: Device number (specific to each interface type)
310*4882a593Smuzhiyun * @devp: the device, if found
311*4882a593Smuzhiyun * @return 0 if found, -ENODEV if no device found, or other -ve error value
312*4882a593Smuzhiyun */
313*4882a593Smuzhiyun int blk_get_device(int if_type, int devnum, struct udevice **devp);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /**
316*4882a593Smuzhiyun * blk_first_device() - Find the first device for a given interface
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * The device is probed ready for use
319*4882a593Smuzhiyun *
320*4882a593Smuzhiyun * @devnum: Device number (specific to each interface type)
321*4882a593Smuzhiyun * @devp: the device, if found
322*4882a593Smuzhiyun * @return 0 if found, -ENODEV if no device, or other -ve error value
323*4882a593Smuzhiyun */
324*4882a593Smuzhiyun int blk_first_device(int if_type, struct udevice **devp);
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun /**
327*4882a593Smuzhiyun * blk_next_device() - Find the next device for a given interface
328*4882a593Smuzhiyun *
329*4882a593Smuzhiyun * This can be called repeatedly after blk_first_device() to iterate through
330*4882a593Smuzhiyun * all devices of the given interface type.
331*4882a593Smuzhiyun *
332*4882a593Smuzhiyun * The device is probed ready for use
333*4882a593Smuzhiyun *
334*4882a593Smuzhiyun * @devp: On entry, the previous device returned. On exit, the next
335*4882a593Smuzhiyun * device, if found
336*4882a593Smuzhiyun * @return 0 if found, -ENODEV if no device, or other -ve error value
337*4882a593Smuzhiyun */
338*4882a593Smuzhiyun int blk_next_device(struct udevice **devp);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /**
341*4882a593Smuzhiyun * blk_create_device() - Create a new block device
342*4882a593Smuzhiyun *
343*4882a593Smuzhiyun * @parent: Parent of the new device
344*4882a593Smuzhiyun * @drv_name: Driver name to use for the block device
345*4882a593Smuzhiyun * @name: Name for the device
346*4882a593Smuzhiyun * @if_type: Interface type (enum if_type_t)
347*4882a593Smuzhiyun * @devnum: Device number, specific to the interface type, or -1 to
348*4882a593Smuzhiyun * allocate the next available number
349*4882a593Smuzhiyun * @blksz: Block size of the device in bytes (typically 512)
350*4882a593Smuzhiyun * @size: Total size of the device in bytes
351*4882a593Smuzhiyun * @devp: the new device (which has not been probed)
352*4882a593Smuzhiyun */
353*4882a593Smuzhiyun int blk_create_device(struct udevice *parent, const char *drv_name,
354*4882a593Smuzhiyun const char *name, int if_type, int devnum, int blksz,
355*4882a593Smuzhiyun lbaint_t size, struct udevice **devp);
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun /**
358*4882a593Smuzhiyun * blk_create_devicef() - Create a new named block device
359*4882a593Smuzhiyun *
360*4882a593Smuzhiyun * @parent: Parent of the new device
361*4882a593Smuzhiyun * @drv_name: Driver name to use for the block device
362*4882a593Smuzhiyun * @name: Name for the device (parent name is prepended)
363*4882a593Smuzhiyun * @if_type: Interface type (enum if_type_t)
364*4882a593Smuzhiyun * @devnum: Device number, specific to the interface type, or -1 to
365*4882a593Smuzhiyun * allocate the next available number
366*4882a593Smuzhiyun * @blksz: Block size of the device in bytes (typically 512)
367*4882a593Smuzhiyun * @size: Total size of the device in bytes
368*4882a593Smuzhiyun * @devp: the new device (which has not been probed)
369*4882a593Smuzhiyun */
370*4882a593Smuzhiyun int blk_create_devicef(struct udevice *parent, const char *drv_name,
371*4882a593Smuzhiyun const char *name, int if_type, int devnum, int blksz,
372*4882a593Smuzhiyun lbaint_t size, struct udevice **devp);
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /**
375*4882a593Smuzhiyun * blk_prepare_device() - Prepare a block device for use
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * This reads partition information from the device if supported.
378*4882a593Smuzhiyun *
379*4882a593Smuzhiyun * @dev: Device to prepare
380*4882a593Smuzhiyun * @return 0 if ok, -ve on error
381*4882a593Smuzhiyun */
382*4882a593Smuzhiyun int blk_prepare_device(struct udevice *dev);
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun /**
385*4882a593Smuzhiyun * blk_unbind_all() - Unbind all device of the given interface type
386*4882a593Smuzhiyun *
387*4882a593Smuzhiyun * The devices are removed and then unbound.
388*4882a593Smuzhiyun *
389*4882a593Smuzhiyun * @if_type: Interface type to unbind
390*4882a593Smuzhiyun * @return 0 if OK, -ve on error
391*4882a593Smuzhiyun */
392*4882a593Smuzhiyun int blk_unbind_all(int if_type);
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun /**
395*4882a593Smuzhiyun * blk_find_max_devnum() - find the maximum device number for an interface type
396*4882a593Smuzhiyun *
397*4882a593Smuzhiyun * Finds the last allocated device number for an interface type @if_type. The
398*4882a593Smuzhiyun * next number is safe to use for a newly allocated device.
399*4882a593Smuzhiyun *
400*4882a593Smuzhiyun * @if_type: Interface type to scan
401*4882a593Smuzhiyun * @return maximum device number found, or -ENODEV if none, or other -ve on
402*4882a593Smuzhiyun * error
403*4882a593Smuzhiyun */
404*4882a593Smuzhiyun int blk_find_max_devnum(enum if_type if_type);
405*4882a593Smuzhiyun
406*4882a593Smuzhiyun /**
407*4882a593Smuzhiyun * blk_select_hwpart() - select a hardware partition
408*4882a593Smuzhiyun *
409*4882a593Smuzhiyun * Select a hardware partition if the device supports it (typically MMC does)
410*4882a593Smuzhiyun *
411*4882a593Smuzhiyun * @dev: Device to update
412*4882a593Smuzhiyun * @hwpart: Partition number to select
413*4882a593Smuzhiyun * @return 0 if OK, -ve on error
414*4882a593Smuzhiyun */
415*4882a593Smuzhiyun int blk_select_hwpart(struct udevice *dev, int hwpart);
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun /**
418*4882a593Smuzhiyun * blk_get_from_parent() - obtain a block device by looking up its parent
419*4882a593Smuzhiyun *
420*4882a593Smuzhiyun * All devices with
421*4882a593Smuzhiyun */
422*4882a593Smuzhiyun int blk_get_from_parent(struct udevice *parent, struct udevice **devp);
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun #else
425*4882a593Smuzhiyun #include <errno.h>
426*4882a593Smuzhiyun /*
427*4882a593Smuzhiyun * These functions should take struct udevice instead of struct blk_desc,
428*4882a593Smuzhiyun * but this is convenient for migration to driver model. Add a 'd' prefix
429*4882a593Smuzhiyun * to the function operations, so that blk_read(), etc. can be reserved for
430*4882a593Smuzhiyun * functions with the correct arguments.
431*4882a593Smuzhiyun */
blk_dread(struct blk_desc * block_dev,lbaint_t start,lbaint_t blkcnt,void * buffer)432*4882a593Smuzhiyun static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start,
433*4882a593Smuzhiyun lbaint_t blkcnt, void *buffer)
434*4882a593Smuzhiyun {
435*4882a593Smuzhiyun ulong blks_read;
436*4882a593Smuzhiyun if (blkcache_read(block_dev->if_type, block_dev->devnum,
437*4882a593Smuzhiyun start, blkcnt, block_dev->blksz, buffer))
438*4882a593Smuzhiyun return blkcnt;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun /*
441*4882a593Smuzhiyun * We could check if block_read is NULL and return -ENOSYS. But this
442*4882a593Smuzhiyun * bloats the code slightly (cause some board to fail to build), and
443*4882a593Smuzhiyun * it would be an error to try an operation that does not exist.
444*4882a593Smuzhiyun */
445*4882a593Smuzhiyun blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer);
446*4882a593Smuzhiyun if (blks_read == blkcnt)
447*4882a593Smuzhiyun blkcache_fill(block_dev->if_type, block_dev->devnum,
448*4882a593Smuzhiyun start, blkcnt, block_dev->blksz, buffer);
449*4882a593Smuzhiyun
450*4882a593Smuzhiyun return blks_read;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
blk_dwrite(struct blk_desc * block_dev,lbaint_t start,lbaint_t blkcnt,const void * buffer)453*4882a593Smuzhiyun static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start,
454*4882a593Smuzhiyun lbaint_t blkcnt, const void *buffer)
455*4882a593Smuzhiyun {
456*4882a593Smuzhiyun blkcache_invalidate(block_dev->if_type, block_dev->devnum);
457*4882a593Smuzhiyun return block_dev->block_write(block_dev, start, blkcnt, buffer);
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun
blk_derase(struct blk_desc * block_dev,lbaint_t start,lbaint_t blkcnt)460*4882a593Smuzhiyun static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start,
461*4882a593Smuzhiyun lbaint_t blkcnt)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun blkcache_invalidate(block_dev->if_type, block_dev->devnum);
464*4882a593Smuzhiyun return block_dev->block_erase(block_dev, start, blkcnt);
465*4882a593Smuzhiyun }
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun /**
468*4882a593Smuzhiyun * struct blk_driver - Driver for block interface types
469*4882a593Smuzhiyun *
470*4882a593Smuzhiyun * This provides access to the block devices for each interface type. One
471*4882a593Smuzhiyun * driver should be provided using U_BOOT_LEGACY_BLK() for each interface
472*4882a593Smuzhiyun * type that is to be supported.
473*4882a593Smuzhiyun *
474*4882a593Smuzhiyun * @if_typename: Interface type name
475*4882a593Smuzhiyun * @if_type: Interface type
476*4882a593Smuzhiyun * @max_devs: Maximum number of devices supported
477*4882a593Smuzhiyun * @desc: Pointer to list of devices for this interface type,
478*4882a593Smuzhiyun * or NULL to use @get_dev() instead
479*4882a593Smuzhiyun */
480*4882a593Smuzhiyun struct blk_driver {
481*4882a593Smuzhiyun const char *if_typename;
482*4882a593Smuzhiyun enum if_type if_type;
483*4882a593Smuzhiyun int max_devs;
484*4882a593Smuzhiyun struct blk_desc *desc;
485*4882a593Smuzhiyun /**
486*4882a593Smuzhiyun * get_dev() - get a pointer to a block device given its number
487*4882a593Smuzhiyun *
488*4882a593Smuzhiyun * Each interface allocates its own devices and typically
489*4882a593Smuzhiyun * struct blk_desc is contained with the interface's data structure.
490*4882a593Smuzhiyun * There is no global numbering for block devices. This method allows
491*4882a593Smuzhiyun * the device for an interface type to be obtained when @desc is NULL.
492*4882a593Smuzhiyun *
493*4882a593Smuzhiyun * @devnum: Device number (0 for first device on that interface,
494*4882a593Smuzhiyun * 1 for second, etc.
495*4882a593Smuzhiyun * @descp: Returns pointer to the block device on success
496*4882a593Smuzhiyun * @return 0 if OK, -ve on error
497*4882a593Smuzhiyun */
498*4882a593Smuzhiyun int (*get_dev)(int devnum, struct blk_desc **descp);
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun /**
501*4882a593Smuzhiyun * select_hwpart() - Select a hardware partition
502*4882a593Smuzhiyun *
503*4882a593Smuzhiyun * Some devices (e.g. MMC) can support partitioning at the hardware
504*4882a593Smuzhiyun * level. This is quite separate from the normal idea of
505*4882a593Smuzhiyun * software-based partitions. MMC hardware partitions must be
506*4882a593Smuzhiyun * explicitly selected. Once selected only the region of the device
507*4882a593Smuzhiyun * covered by that partition is accessible.
508*4882a593Smuzhiyun *
509*4882a593Smuzhiyun * The MMC standard provides for two boot partitions (numbered 1 and 2),
510*4882a593Smuzhiyun * rpmb (3), and up to 4 addition general-purpose partitions (4-7).
511*4882a593Smuzhiyun * Partition 0 is the main user-data partition.
512*4882a593Smuzhiyun *
513*4882a593Smuzhiyun * @desc: Block device descriptor
514*4882a593Smuzhiyun * @hwpart: Hardware partition number to select. 0 means the main
515*4882a593Smuzhiyun * user-data partition, 1 is the first partition, 2 is
516*4882a593Smuzhiyun * the second, etc.
517*4882a593Smuzhiyun * @return 0 if OK, other value for an error
518*4882a593Smuzhiyun */
519*4882a593Smuzhiyun int (*select_hwpart)(struct blk_desc *desc, int hwpart);
520*4882a593Smuzhiyun };
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun /*
523*4882a593Smuzhiyun * Declare a new U-Boot legacy block driver. New drivers should use driver
524*4882a593Smuzhiyun * model (UCLASS_BLK).
525*4882a593Smuzhiyun */
526*4882a593Smuzhiyun #define U_BOOT_LEGACY_BLK(__name) \
527*4882a593Smuzhiyun ll_entry_declare(struct blk_driver, __name, blk_driver)
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun struct blk_driver *blk_driver_lookup_type(int if_type);
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun #endif /* !CONFIG_BLK */
532*4882a593Smuzhiyun
533*4882a593Smuzhiyun /**
534*4882a593Smuzhiyun * blk_get_devnum_by_typename() - Get a block device by type and number
535*4882a593Smuzhiyun *
536*4882a593Smuzhiyun * This looks through the available block devices of the given type, returning
537*4882a593Smuzhiyun * the one with the given @devnum.
538*4882a593Smuzhiyun *
539*4882a593Smuzhiyun * @if_type: Block device type
540*4882a593Smuzhiyun * @devnum: Device number
541*4882a593Smuzhiyun * @return point to block device descriptor, or NULL if not found
542*4882a593Smuzhiyun */
543*4882a593Smuzhiyun struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum);
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /**
546*4882a593Smuzhiyun * blk_get_devnum_by_type() - Get a block device by type name, and number
547*4882a593Smuzhiyun *
548*4882a593Smuzhiyun * This looks up the block device type based on @if_typename, then calls
549*4882a593Smuzhiyun * blk_get_devnum_by_type().
550*4882a593Smuzhiyun *
551*4882a593Smuzhiyun * @if_typename: Block device type name
552*4882a593Smuzhiyun * @devnum: Device number
553*4882a593Smuzhiyun * @return point to block device descriptor, or NULL if not found
554*4882a593Smuzhiyun */
555*4882a593Smuzhiyun struct blk_desc *blk_get_devnum_by_typename(const char *if_typename,
556*4882a593Smuzhiyun int devnum);
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun /**
559*4882a593Smuzhiyun * blk_dselect_hwpart() - select a hardware partition
560*4882a593Smuzhiyun *
561*4882a593Smuzhiyun * This selects a hardware partition (such as is supported by MMC). The block
562*4882a593Smuzhiyun * device size may change as this effectively points the block device to a
563*4882a593Smuzhiyun * partition at the hardware level. See the select_hwpart() method above.
564*4882a593Smuzhiyun *
565*4882a593Smuzhiyun * @desc: Block device descriptor for the device to select
566*4882a593Smuzhiyun * @hwpart: Partition number to select
567*4882a593Smuzhiyun * @return 0 if OK, -ve on error
568*4882a593Smuzhiyun */
569*4882a593Smuzhiyun int blk_dselect_hwpart(struct blk_desc *desc, int hwpart);
570*4882a593Smuzhiyun
571*4882a593Smuzhiyun /**
572*4882a593Smuzhiyun * blk_list_part() - list the partitions for block devices of a given type
573*4882a593Smuzhiyun *
574*4882a593Smuzhiyun * This looks up the partition type for each block device of type @if_type,
575*4882a593Smuzhiyun * then displays a list of partitions.
576*4882a593Smuzhiyun *
577*4882a593Smuzhiyun * @if_type: Block device type
578*4882a593Smuzhiyun * @return 0 if OK, -ENODEV if there is none of that type
579*4882a593Smuzhiyun */
580*4882a593Smuzhiyun int blk_list_part(enum if_type if_type);
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun /**
583*4882a593Smuzhiyun * blk_list_devices() - list the block devices of a given type
584*4882a593Smuzhiyun *
585*4882a593Smuzhiyun * This lists each block device of the type @if_type, showing the capacity
586*4882a593Smuzhiyun * as well as type-specific information.
587*4882a593Smuzhiyun *
588*4882a593Smuzhiyun * @if_type: Block device type
589*4882a593Smuzhiyun */
590*4882a593Smuzhiyun void blk_list_devices(enum if_type if_type);
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /**
593*4882a593Smuzhiyun * blk_show_device() - show information about a given block device
594*4882a593Smuzhiyun *
595*4882a593Smuzhiyun * This shows the block device capacity as well as type-specific information.
596*4882a593Smuzhiyun *
597*4882a593Smuzhiyun * @if_type: Block device type
598*4882a593Smuzhiyun * @devnum: Device number
599*4882a593Smuzhiyun * @return 0 if OK, -ENODEV for invalid device number
600*4882a593Smuzhiyun */
601*4882a593Smuzhiyun int blk_show_device(enum if_type if_type, int devnum);
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun /**
604*4882a593Smuzhiyun * blk_print_device_num() - show information about a given block device
605*4882a593Smuzhiyun *
606*4882a593Smuzhiyun * This is similar to blk_show_device() but returns an error if the block
607*4882a593Smuzhiyun * device type is unknown.
608*4882a593Smuzhiyun *
609*4882a593Smuzhiyun * @if_type: Block device type
610*4882a593Smuzhiyun * @devnum: Device number
611*4882a593Smuzhiyun * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block
612*4882a593Smuzhiyun * device is not connected
613*4882a593Smuzhiyun */
614*4882a593Smuzhiyun int blk_print_device_num(enum if_type if_type, int devnum);
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun /**
617*4882a593Smuzhiyun * blk_print_part_devnum() - print the partition information for a device
618*4882a593Smuzhiyun *
619*4882a593Smuzhiyun * @if_type: Block device type
620*4882a593Smuzhiyun * @devnum: Device number
621*4882a593Smuzhiyun * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if
622*4882a593Smuzhiyun * the interface type is not supported, other -ve on other error
623*4882a593Smuzhiyun */
624*4882a593Smuzhiyun int blk_print_part_devnum(enum if_type if_type, int devnum);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun /**
627*4882a593Smuzhiyun * blk_read_devnum() - read blocks from a device
628*4882a593Smuzhiyun *
629*4882a593Smuzhiyun * @if_type: Block device type
630*4882a593Smuzhiyun * @devnum: Device number
631*4882a593Smuzhiyun * @blkcnt: Number of blocks to read
632*4882a593Smuzhiyun * @buffer: Address to write data to
633*4882a593Smuzhiyun * @return number of blocks read, or -ve error number on error
634*4882a593Smuzhiyun */
635*4882a593Smuzhiyun ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start,
636*4882a593Smuzhiyun lbaint_t blkcnt, void *buffer);
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun /**
639*4882a593Smuzhiyun * blk_write_devnum() - write blocks to a device
640*4882a593Smuzhiyun *
641*4882a593Smuzhiyun * @if_type: Block device type
642*4882a593Smuzhiyun * @devnum: Device number
643*4882a593Smuzhiyun * @blkcnt: Number of blocks to write
644*4882a593Smuzhiyun * @buffer: Address to read data from
645*4882a593Smuzhiyun * @return number of blocks written, or -ve error number on error
646*4882a593Smuzhiyun */
647*4882a593Smuzhiyun ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start,
648*4882a593Smuzhiyun lbaint_t blkcnt, const void *buffer);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun /**
651*4882a593Smuzhiyun * blk_erase_devnum() - erase blocks to a device
652*4882a593Smuzhiyun *
653*4882a593Smuzhiyun * @if_type: Block device type
654*4882a593Smuzhiyun * @devnum: Device number
655*4882a593Smuzhiyun * @blkcnt: Number of blocks to erase
656*4882a593Smuzhiyun * @return number of blocks erased, or -ve error number on error
657*4882a593Smuzhiyun */
658*4882a593Smuzhiyun ulong blk_erase_devnum(enum if_type if_type, int devnum, lbaint_t start,
659*4882a593Smuzhiyun lbaint_t blkcnt);
660*4882a593Smuzhiyun /**
661*4882a593Smuzhiyun * blk_select_hwpart_devnum() - select a hardware partition
662*4882a593Smuzhiyun *
663*4882a593Smuzhiyun * This is similar to blk_dselect_hwpart() but it looks up the interface and
664*4882a593Smuzhiyun * device number.
665*4882a593Smuzhiyun *
666*4882a593Smuzhiyun * @if_type: Block device type
667*4882a593Smuzhiyun * @devnum: Device number
668*4882a593Smuzhiyun * @hwpart: Partition number to select
669*4882a593Smuzhiyun * @return 0 if OK, -ve on error
670*4882a593Smuzhiyun */
671*4882a593Smuzhiyun int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart);
672*4882a593Smuzhiyun
673*4882a593Smuzhiyun /**
674*4882a593Smuzhiyun * blk_get_if_type_name() - Get the name of an interface type
675*4882a593Smuzhiyun *
676*4882a593Smuzhiyun * @if_type: Interface type to check
677*4882a593Smuzhiyun * @return name of interface, or NULL if none
678*4882a593Smuzhiyun */
679*4882a593Smuzhiyun const char *blk_get_if_type_name(enum if_type if_type);
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun /**
682*4882a593Smuzhiyun * blk_common_cmd() - handle common commands with block devices
683*4882a593Smuzhiyun *
684*4882a593Smuzhiyun * @args: Number of arguments to the command (argv[0] is the command itself)
685*4882a593Smuzhiyun * @argv: Command arguments
686*4882a593Smuzhiyun * @if_type: Interface type
687*4882a593Smuzhiyun * @cur_devnump: Current device number for this interface type
688*4882a593Smuzhiyun * @return 0 if OK, CMD_RET_ERROR on error
689*4882a593Smuzhiyun */
690*4882a593Smuzhiyun int blk_common_cmd(int argc, char * const argv[], enum if_type if_type,
691*4882a593Smuzhiyun int *cur_devnump);
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun /**
694*4882a593Smuzhiyun * if_typename_to_iftype() - get iftype according to iftype name
695*4882a593Smuzhiyun *
696*4882a593Smuzhiyun * @if_typename: iftype name
697*4882a593Smuzhiyun * @return iftype index
698*4882a593Smuzhiyun */
699*4882a593Smuzhiyun enum if_type if_typename_to_iftype(const char *if_typename);
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun #endif
702