11a73661bSSimon Glass /* 21a73661bSSimon Glass * (C) Copyright 2000-2004 31a73661bSSimon Glass * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 41a73661bSSimon Glass * 51a73661bSSimon Glass * SPDX-License-Identifier: GPL-2.0+ 61a73661bSSimon Glass */ 71a73661bSSimon Glass 81a73661bSSimon Glass #ifndef BLK_H 91a73661bSSimon Glass #define BLK_H 101a73661bSSimon Glass 111a73661bSSimon Glass #ifdef CONFIG_SYS_64BIT_LBA 121a73661bSSimon Glass typedef uint64_t lbaint_t; 131a73661bSSimon Glass #define LBAFlength "ll" 141a73661bSSimon Glass #else 151a73661bSSimon Glass typedef ulong lbaint_t; 161a73661bSSimon Glass #define LBAFlength "l" 171a73661bSSimon Glass #endif 181a73661bSSimon Glass #define LBAF "%" LBAFlength "x" 191a73661bSSimon Glass #define LBAFU "%" LBAFlength "u" 201a73661bSSimon Glass 211a73661bSSimon Glass /* Interface types: */ 225ec4f1a5SSimon Glass enum if_type { 235ec4f1a5SSimon Glass IF_TYPE_UNKNOWN = 0, 245ec4f1a5SSimon Glass IF_TYPE_IDE, 255ec4f1a5SSimon Glass IF_TYPE_SCSI, 265ec4f1a5SSimon Glass IF_TYPE_ATAPI, 275ec4f1a5SSimon Glass IF_TYPE_USB, 285ec4f1a5SSimon Glass IF_TYPE_DOC, 295ec4f1a5SSimon Glass IF_TYPE_MMC, 305ec4f1a5SSimon Glass IF_TYPE_SD, 315ec4f1a5SSimon Glass IF_TYPE_SATA, 325ec4f1a5SSimon Glass IF_TYPE_HOST, 333ef85e37SSimon Glass IF_TYPE_SYSTEMACE, 345ec4f1a5SSimon Glass 355ec4f1a5SSimon Glass IF_TYPE_COUNT, /* Number of interface types */ 365ec4f1a5SSimon Glass }; 371a73661bSSimon Glass 3809d71aacSSimon Glass /* 3909d71aacSSimon Glass * With driver model (CONFIG_BLK) this is uclass platform data, accessible 4009d71aacSSimon Glass * with dev_get_uclass_platdata(dev) 4109d71aacSSimon Glass */ 421a73661bSSimon Glass struct blk_desc { 4309d71aacSSimon Glass /* 4409d71aacSSimon Glass * TODO: With driver model we should be able to use the parent 4509d71aacSSimon Glass * device's uclass instead. 4609d71aacSSimon Glass */ 475ec4f1a5SSimon Glass enum if_type if_type; /* type of the interface */ 48bcce53d0SSimon Glass int devnum; /* device number */ 491a73661bSSimon Glass unsigned char part_type; /* partition type */ 501a73661bSSimon Glass unsigned char target; /* target SCSI ID */ 511a73661bSSimon Glass unsigned char lun; /* target LUN */ 521a73661bSSimon Glass unsigned char hwpart; /* HW partition, e.g. for eMMC */ 531a73661bSSimon Glass unsigned char type; /* device type */ 541a73661bSSimon Glass unsigned char removable; /* removable device */ 551a73661bSSimon Glass #ifdef CONFIG_LBA48 561a73661bSSimon Glass /* device can use 48bit addr (ATA/ATAPI v7) */ 571a73661bSSimon Glass unsigned char lba48; 581a73661bSSimon Glass #endif 591a73661bSSimon Glass lbaint_t lba; /* number of blocks */ 601a73661bSSimon Glass unsigned long blksz; /* block size */ 611a73661bSSimon Glass int log2blksz; /* for convenience: log2(blksz) */ 621a73661bSSimon Glass char vendor[40+1]; /* IDE model, SCSI Vendor */ 631a73661bSSimon Glass char product[20+1]; /* IDE Serial no, SCSI product */ 641a73661bSSimon Glass char revision[8+1]; /* firmware revision */ 6509d71aacSSimon Glass #ifdef CONFIG_BLK 66*b6694a33SSimon Glass /* 67*b6694a33SSimon Glass * For now we have a few functions which take struct blk_desc as a 68*b6694a33SSimon Glass * parameter. This field allows them to look up the associated 69*b6694a33SSimon Glass * device. Once these functions are removed we can drop this field. 70*b6694a33SSimon Glass */ 7109d71aacSSimon Glass struct udevice *bdev; 7209d71aacSSimon Glass #else 731a73661bSSimon Glass unsigned long (*block_read)(struct blk_desc *block_dev, 741a73661bSSimon Glass lbaint_t start, 751a73661bSSimon Glass lbaint_t blkcnt, 761a73661bSSimon Glass void *buffer); 771a73661bSSimon Glass unsigned long (*block_write)(struct blk_desc *block_dev, 781a73661bSSimon Glass lbaint_t start, 791a73661bSSimon Glass lbaint_t blkcnt, 801a73661bSSimon Glass const void *buffer); 811a73661bSSimon Glass unsigned long (*block_erase)(struct blk_desc *block_dev, 821a73661bSSimon Glass lbaint_t start, 831a73661bSSimon Glass lbaint_t blkcnt); 841a73661bSSimon Glass void *priv; /* driver private struct pointer */ 8509d71aacSSimon Glass #endif 861a73661bSSimon Glass }; 871a73661bSSimon Glass 881a73661bSSimon Glass #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz)) 891a73661bSSimon Glass #define PAD_TO_BLOCKSIZE(size, blk_desc) \ 901a73661bSSimon Glass (PAD_SIZE(size, blk_desc->blksz)) 911a73661bSSimon Glass 92e40cf34aSEric Nelson #ifdef CONFIG_BLOCK_CACHE 93e40cf34aSEric Nelson /** 94e40cf34aSEric Nelson * blkcache_read() - attempt to read a set of blocks from cache 95e40cf34aSEric Nelson * 96e40cf34aSEric Nelson * @param iftype - IF_TYPE_x for type of device 97e40cf34aSEric Nelson * @param dev - device index of particular type 98e40cf34aSEric Nelson * @param start - starting block number 99e40cf34aSEric Nelson * @param blkcnt - number of blocks to read 100e40cf34aSEric Nelson * @param blksz - size in bytes of each block 101e40cf34aSEric Nelson * @param buf - buffer to contain cached data 102e40cf34aSEric Nelson * 103e40cf34aSEric Nelson * @return - '1' if block returned from cache, '0' otherwise. 104e40cf34aSEric Nelson */ 105c8e4d2a8SEric Nelson int blkcache_read(int iftype, int dev, 106e40cf34aSEric Nelson lbaint_t start, lbaint_t blkcnt, 107e40cf34aSEric Nelson unsigned long blksz, void *buffer); 108e40cf34aSEric Nelson 109e40cf34aSEric Nelson /** 110e40cf34aSEric Nelson * blkcache_fill() - make data read from a block device available 111e40cf34aSEric Nelson * to the block cache 112e40cf34aSEric Nelson * 113e40cf34aSEric Nelson * @param iftype - IF_TYPE_x for type of device 114e40cf34aSEric Nelson * @param dev - device index of particular type 115e40cf34aSEric Nelson * @param start - starting block number 116e40cf34aSEric Nelson * @param blkcnt - number of blocks available 117e40cf34aSEric Nelson * @param blksz - size in bytes of each block 118e40cf34aSEric Nelson * @param buf - buffer containing data to cache 119e40cf34aSEric Nelson * 120e40cf34aSEric Nelson */ 121c8e4d2a8SEric Nelson void blkcache_fill(int iftype, int dev, 122e40cf34aSEric Nelson lbaint_t start, lbaint_t blkcnt, 123e40cf34aSEric Nelson unsigned long blksz, void const *buffer); 124e40cf34aSEric Nelson 125e40cf34aSEric Nelson /** 126e40cf34aSEric Nelson * blkcache_invalidate() - discard the cache for a set of blocks 127e40cf34aSEric Nelson * because of a write or device (re)initialization. 128e40cf34aSEric Nelson * 129e40cf34aSEric Nelson * @param iftype - IF_TYPE_x for type of device 130e40cf34aSEric Nelson * @param dev - device index of particular type 131e40cf34aSEric Nelson */ 132c8e4d2a8SEric Nelson void blkcache_invalidate(int iftype, int dev); 133e40cf34aSEric Nelson 134e40cf34aSEric Nelson /** 135e40cf34aSEric Nelson * blkcache_configure() - configure block cache 136e40cf34aSEric Nelson * 137e40cf34aSEric Nelson * @param blocks - maximum blocks per entry 138e40cf34aSEric Nelson * @param entries - maximum entries in cache 139e40cf34aSEric Nelson */ 140e40cf34aSEric Nelson void blkcache_configure(unsigned blocks, unsigned entries); 141e40cf34aSEric Nelson 142e40cf34aSEric Nelson /* 143e40cf34aSEric Nelson * statistics of the block cache 144e40cf34aSEric Nelson */ 145e40cf34aSEric Nelson struct block_cache_stats { 146e40cf34aSEric Nelson unsigned hits; 147e40cf34aSEric Nelson unsigned misses; 148e40cf34aSEric Nelson unsigned entries; /* current entry count */ 149e40cf34aSEric Nelson unsigned max_blocks_per_entry; 150e40cf34aSEric Nelson unsigned max_entries; 151e40cf34aSEric Nelson }; 152e40cf34aSEric Nelson 153e40cf34aSEric Nelson /** 154e40cf34aSEric Nelson * get_blkcache_stats() - return statistics and reset 155e40cf34aSEric Nelson * 156e40cf34aSEric Nelson * @param stats - statistics are copied here 157e40cf34aSEric Nelson */ 158e40cf34aSEric Nelson void blkcache_stats(struct block_cache_stats *stats); 159e40cf34aSEric Nelson 160e40cf34aSEric Nelson #else 161e40cf34aSEric Nelson 162c8e4d2a8SEric Nelson static inline int blkcache_read(int iftype, int dev, 163e40cf34aSEric Nelson lbaint_t start, lbaint_t blkcnt, 164e40cf34aSEric Nelson unsigned long blksz, void *buffer) 165e40cf34aSEric Nelson { 166e40cf34aSEric Nelson return 0; 167e40cf34aSEric Nelson } 168e40cf34aSEric Nelson 169c8e4d2a8SEric Nelson static inline void blkcache_fill(int iftype, int dev, 170e40cf34aSEric Nelson lbaint_t start, lbaint_t blkcnt, 171e40cf34aSEric Nelson unsigned long blksz, void const *buffer) {} 172e40cf34aSEric Nelson 173c8e4d2a8SEric Nelson static inline void blkcache_invalidate(int iftype, int dev) {} 174e40cf34aSEric Nelson 175e40cf34aSEric Nelson #endif 176e40cf34aSEric Nelson 17709d71aacSSimon Glass #ifdef CONFIG_BLK 17809d71aacSSimon Glass struct udevice; 17909d71aacSSimon Glass 18009d71aacSSimon Glass /* Operations on block devices */ 18109d71aacSSimon Glass struct blk_ops { 18209d71aacSSimon Glass /** 18309d71aacSSimon Glass * read() - read from a block device 18409d71aacSSimon Glass * 18509d71aacSSimon Glass * @dev: Device to read from 18609d71aacSSimon Glass * @start: Start block number to read (0=first) 18709d71aacSSimon Glass * @blkcnt: Number of blocks to read 18809d71aacSSimon Glass * @buffer: Destination buffer for data read 18909d71aacSSimon Glass * @return number of blocks read, or -ve error number (see the 19009d71aacSSimon Glass * IS_ERR_VALUE() macro 19109d71aacSSimon Glass */ 19209d71aacSSimon Glass unsigned long (*read)(struct udevice *dev, lbaint_t start, 19309d71aacSSimon Glass lbaint_t blkcnt, void *buffer); 19409d71aacSSimon Glass 19509d71aacSSimon Glass /** 19609d71aacSSimon Glass * write() - write to a block device 19709d71aacSSimon Glass * 19809d71aacSSimon Glass * @dev: Device to write to 19909d71aacSSimon Glass * @start: Start block number to write (0=first) 20009d71aacSSimon Glass * @blkcnt: Number of blocks to write 20109d71aacSSimon Glass * @buffer: Source buffer for data to write 20209d71aacSSimon Glass * @return number of blocks written, or -ve error number (see the 20309d71aacSSimon Glass * IS_ERR_VALUE() macro 20409d71aacSSimon Glass */ 20509d71aacSSimon Glass unsigned long (*write)(struct udevice *dev, lbaint_t start, 20609d71aacSSimon Glass lbaint_t blkcnt, const void *buffer); 20709d71aacSSimon Glass 20809d71aacSSimon Glass /** 20909d71aacSSimon Glass * erase() - erase a section of a block device 21009d71aacSSimon Glass * 21109d71aacSSimon Glass * @dev: Device to (partially) erase 21209d71aacSSimon Glass * @start: Start block number to erase (0=first) 21309d71aacSSimon Glass * @blkcnt: Number of blocks to erase 21409d71aacSSimon Glass * @return number of blocks erased, or -ve error number (see the 21509d71aacSSimon Glass * IS_ERR_VALUE() macro 21609d71aacSSimon Glass */ 21709d71aacSSimon Glass unsigned long (*erase)(struct udevice *dev, lbaint_t start, 21809d71aacSSimon Glass lbaint_t blkcnt); 219cd0fb55bSSimon Glass 220cd0fb55bSSimon Glass /** 221cd0fb55bSSimon Glass * select_hwpart() - select a particular hardware partition 222cd0fb55bSSimon Glass * 223cd0fb55bSSimon Glass * Some devices (e.g. MMC) can support partitioning at the hardware 224cd0fb55bSSimon Glass * level. This is quite separate from the normal idea of 225cd0fb55bSSimon Glass * software-based partitions. MMC hardware partitions must be 226cd0fb55bSSimon Glass * explicitly selected. Once selected only the region of the device 227cd0fb55bSSimon Glass * covered by that partition is accessible. 228cd0fb55bSSimon Glass * 229cd0fb55bSSimon Glass * The MMC standard provides for two boot partitions (numbered 1 and 2), 230cd0fb55bSSimon Glass * rpmb (3), and up to 4 addition general-purpose partitions (4-7). 231cd0fb55bSSimon Glass * 232cd0fb55bSSimon Glass * @desc: Block device to update 233cd0fb55bSSimon Glass * @hwpart: Hardware partition number to select. 0 means the raw 234cd0fb55bSSimon Glass * device, 1 is the first partition, 2 is the second, etc. 235cd0fb55bSSimon Glass * @return 0 if OK, -ve on error 236cd0fb55bSSimon Glass */ 237cd0fb55bSSimon Glass int (*select_hwpart)(struct udevice *dev, int hwpart); 23809d71aacSSimon Glass }; 23909d71aacSSimon Glass 24009d71aacSSimon Glass #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops) 24109d71aacSSimon Glass 24209d71aacSSimon Glass /* 24309d71aacSSimon Glass * These functions should take struct udevice instead of struct blk_desc, 24409d71aacSSimon Glass * but this is convenient for migration to driver model. Add a 'd' prefix 24509d71aacSSimon Glass * to the function operations, so that blk_read(), etc. can be reserved for 24609d71aacSSimon Glass * functions with the correct arguments. 24709d71aacSSimon Glass */ 24809d71aacSSimon Glass unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start, 24909d71aacSSimon Glass lbaint_t blkcnt, void *buffer); 25009d71aacSSimon Glass unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start, 25109d71aacSSimon Glass lbaint_t blkcnt, const void *buffer); 25209d71aacSSimon Glass unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, 25309d71aacSSimon Glass lbaint_t blkcnt); 25409d71aacSSimon Glass 25509d71aacSSimon Glass /** 25609d71aacSSimon Glass * blk_get_device() - Find and probe a block device ready for use 25709d71aacSSimon Glass * 25809d71aacSSimon Glass * @if_type: Interface type (enum if_type_t) 25909d71aacSSimon Glass * @devnum: Device number (specific to each interface type) 26009d71aacSSimon Glass * @devp: the device, if found 26109d71aacSSimon Glass * @return - if found, -ENODEV if no device found, or other -ve error value 26209d71aacSSimon Glass */ 26309d71aacSSimon Glass int blk_get_device(int if_type, int devnum, struct udevice **devp); 26409d71aacSSimon Glass 26509d71aacSSimon Glass /** 26609d71aacSSimon Glass * blk_first_device() - Find the first device for a given interface 26709d71aacSSimon Glass * 26809d71aacSSimon Glass * The device is probed ready for use 26909d71aacSSimon Glass * 27009d71aacSSimon Glass * @devnum: Device number (specific to each interface type) 27109d71aacSSimon Glass * @devp: the device, if found 27209d71aacSSimon Glass * @return 0 if found, -ENODEV if no device, or other -ve error value 27309d71aacSSimon Glass */ 27409d71aacSSimon Glass int blk_first_device(int if_type, struct udevice **devp); 27509d71aacSSimon Glass 27609d71aacSSimon Glass /** 27709d71aacSSimon Glass * blk_next_device() - Find the next device for a given interface 27809d71aacSSimon Glass * 27909d71aacSSimon Glass * This can be called repeatedly after blk_first_device() to iterate through 28009d71aacSSimon Glass * all devices of the given interface type. 28109d71aacSSimon Glass * 28209d71aacSSimon Glass * The device is probed ready for use 28309d71aacSSimon Glass * 28409d71aacSSimon Glass * @devp: On entry, the previous device returned. On exit, the next 28509d71aacSSimon Glass * device, if found 28609d71aacSSimon Glass * @return 0 if found, -ENODEV if no device, or other -ve error value 28709d71aacSSimon Glass */ 28809d71aacSSimon Glass int blk_next_device(struct udevice **devp); 28909d71aacSSimon Glass 29009d71aacSSimon Glass /** 29109d71aacSSimon Glass * blk_create_device() - Create a new block device 29209d71aacSSimon Glass * 29309d71aacSSimon Glass * @parent: Parent of the new device 29409d71aacSSimon Glass * @drv_name: Driver name to use for the block device 29509d71aacSSimon Glass * @name: Name for the device 29609d71aacSSimon Glass * @if_type: Interface type (enum if_type_t) 29752138fd4SSimon Glass * @devnum: Device number, specific to the interface type, or -1 to 29852138fd4SSimon Glass * allocate the next available number 29909d71aacSSimon Glass * @blksz: Block size of the device in bytes (typically 512) 30009d71aacSSimon Glass * @size: Total size of the device in bytes 30109d71aacSSimon Glass * @devp: the new device (which has not been probed) 30209d71aacSSimon Glass */ 30309d71aacSSimon Glass int blk_create_device(struct udevice *parent, const char *drv_name, 30409d71aacSSimon Glass const char *name, int if_type, int devnum, int blksz, 30509d71aacSSimon Glass lbaint_t size, struct udevice **devp); 30609d71aacSSimon Glass 30709d71aacSSimon Glass /** 3089107c973SSimon Glass * blk_create_devicef() - Create a new named block device 3099107c973SSimon Glass * 3109107c973SSimon Glass * @parent: Parent of the new device 3119107c973SSimon Glass * @drv_name: Driver name to use for the block device 3129107c973SSimon Glass * @name: Name for the device (parent name is prepended) 3139107c973SSimon Glass * @if_type: Interface type (enum if_type_t) 3149107c973SSimon Glass * @devnum: Device number, specific to the interface type, or -1 to 3159107c973SSimon Glass * allocate the next available number 3169107c973SSimon Glass * @blksz: Block size of the device in bytes (typically 512) 3179107c973SSimon Glass * @size: Total size of the device in bytes 3189107c973SSimon Glass * @devp: the new device (which has not been probed) 3199107c973SSimon Glass */ 3209107c973SSimon Glass int blk_create_devicef(struct udevice *parent, const char *drv_name, 3219107c973SSimon Glass const char *name, int if_type, int devnum, int blksz, 3229107c973SSimon Glass lbaint_t size, struct udevice **devp); 3239107c973SSimon Glass 3249107c973SSimon Glass /** 32509d71aacSSimon Glass * blk_prepare_device() - Prepare a block device for use 32609d71aacSSimon Glass * 32709d71aacSSimon Glass * This reads partition information from the device if supported. 32809d71aacSSimon Glass * 32909d71aacSSimon Glass * @dev: Device to prepare 33009d71aacSSimon Glass * @return 0 if ok, -ve on error 33109d71aacSSimon Glass */ 33209d71aacSSimon Glass int blk_prepare_device(struct udevice *dev); 33309d71aacSSimon Glass 33409d71aacSSimon Glass /** 33509d71aacSSimon Glass * blk_unbind_all() - Unbind all device of the given interface type 33609d71aacSSimon Glass * 33709d71aacSSimon Glass * The devices are removed and then unbound. 33809d71aacSSimon Glass * 33909d71aacSSimon Glass * @if_type: Interface type to unbind 34009d71aacSSimon Glass * @return 0 if OK, -ve on error 34109d71aacSSimon Glass */ 34209d71aacSSimon Glass int blk_unbind_all(int if_type); 34309d71aacSSimon Glass 34452138fd4SSimon Glass /** 34552138fd4SSimon Glass * blk_find_max_devnum() - find the maximum device number for an interface type 34652138fd4SSimon Glass * 34752138fd4SSimon Glass * Finds the last allocated device number for an interface type @if_type. The 34852138fd4SSimon Glass * next number is safe to use for a newly allocated device. 34952138fd4SSimon Glass * 35052138fd4SSimon Glass * @if_type: Interface type to scan 35152138fd4SSimon Glass * @return maximum device number found, or -ENODEV if none, or other -ve on 35252138fd4SSimon Glass * error 35352138fd4SSimon Glass */ 35452138fd4SSimon Glass int blk_find_max_devnum(enum if_type if_type); 35552138fd4SSimon Glass 356cd0fb55bSSimon Glass /** 357cd0fb55bSSimon Glass * blk_select_hwpart() - select a hardware partition 358cd0fb55bSSimon Glass * 359cd0fb55bSSimon Glass * Select a hardware partition if the device supports it (typically MMC does) 360cd0fb55bSSimon Glass * 361cd0fb55bSSimon Glass * @dev: Device to update 362cd0fb55bSSimon Glass * @hwpart: Partition number to select 363cd0fb55bSSimon Glass * @return 0 if OK, -ve on error 364cd0fb55bSSimon Glass */ 365cd0fb55bSSimon Glass int blk_select_hwpart(struct udevice *dev, int hwpart); 366cd0fb55bSSimon Glass 36709d71aacSSimon Glass #else 36809d71aacSSimon Glass #include <errno.h> 3692a981dc2SSimon Glass /* 3702a981dc2SSimon Glass * These functions should take struct udevice instead of struct blk_desc, 3712a981dc2SSimon Glass * but this is convenient for migration to driver model. Add a 'd' prefix 3722a981dc2SSimon Glass * to the function operations, so that blk_read(), etc. can be reserved for 3732a981dc2SSimon Glass * functions with the correct arguments. 3742a981dc2SSimon Glass */ 3752a981dc2SSimon Glass static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start, 3762a981dc2SSimon Glass lbaint_t blkcnt, void *buffer) 3772a981dc2SSimon Glass { 378e40cf34aSEric Nelson ulong blks_read; 379e40cf34aSEric Nelson if (blkcache_read(block_dev->if_type, block_dev->devnum, 380e40cf34aSEric Nelson start, blkcnt, block_dev->blksz, buffer)) 381e40cf34aSEric Nelson return blkcnt; 382e40cf34aSEric Nelson 3832a981dc2SSimon Glass /* 3842a981dc2SSimon Glass * We could check if block_read is NULL and return -ENOSYS. But this 3852a981dc2SSimon Glass * bloats the code slightly (cause some board to fail to build), and 3862a981dc2SSimon Glass * it would be an error to try an operation that does not exist. 3872a981dc2SSimon Glass */ 388e40cf34aSEric Nelson blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer); 389e40cf34aSEric Nelson if (blks_read == blkcnt) 390e40cf34aSEric Nelson blkcache_fill(block_dev->if_type, block_dev->devnum, 391e40cf34aSEric Nelson start, blkcnt, block_dev->blksz, buffer); 392e40cf34aSEric Nelson 393e40cf34aSEric Nelson return blks_read; 3942a981dc2SSimon Glass } 3952a981dc2SSimon Glass 3962a981dc2SSimon Glass static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start, 3972a981dc2SSimon Glass lbaint_t blkcnt, const void *buffer) 3982a981dc2SSimon Glass { 399e40cf34aSEric Nelson blkcache_invalidate(block_dev->if_type, block_dev->devnum); 4002a981dc2SSimon Glass return block_dev->block_write(block_dev, start, blkcnt, buffer); 4012a981dc2SSimon Glass } 4022a981dc2SSimon Glass 4032a981dc2SSimon Glass static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start, 4042a981dc2SSimon Glass lbaint_t blkcnt) 4052a981dc2SSimon Glass { 406e40cf34aSEric Nelson blkcache_invalidate(block_dev->if_type, block_dev->devnum); 4072a981dc2SSimon Glass return block_dev->block_erase(block_dev, start, blkcnt); 4082a981dc2SSimon Glass } 4096eef6eacSSimon Glass 4106eef6eacSSimon Glass /** 4116eef6eacSSimon Glass * struct blk_driver - Driver for block interface types 4126eef6eacSSimon Glass * 4136eef6eacSSimon Glass * This provides access to the block devices for each interface type. One 4146eef6eacSSimon Glass * driver should be provided using U_BOOT_LEGACY_BLK() for each interface 4156eef6eacSSimon Glass * type that is to be supported. 4166eef6eacSSimon Glass * 4176eef6eacSSimon Glass * @if_typename: Interface type name 4186eef6eacSSimon Glass * @if_type: Interface type 4196eef6eacSSimon Glass * @max_devs: Maximum number of devices supported 4206eef6eacSSimon Glass * @desc: Pointer to list of devices for this interface type, 4216eef6eacSSimon Glass * or NULL to use @get_dev() instead 4226eef6eacSSimon Glass */ 4236eef6eacSSimon Glass struct blk_driver { 4246eef6eacSSimon Glass const char *if_typename; 4256eef6eacSSimon Glass enum if_type if_type; 4266eef6eacSSimon Glass int max_devs; 4276eef6eacSSimon Glass struct blk_desc *desc; 4286eef6eacSSimon Glass /** 4296eef6eacSSimon Glass * get_dev() - get a pointer to a block device given its number 4306eef6eacSSimon Glass * 4316eef6eacSSimon Glass * Each interface allocates its own devices and typically 4326eef6eacSSimon Glass * struct blk_desc is contained with the interface's data structure. 4336eef6eacSSimon Glass * There is no global numbering for block devices. This method allows 4346eef6eacSSimon Glass * the device for an interface type to be obtained when @desc is NULL. 4356eef6eacSSimon Glass * 4366eef6eacSSimon Glass * @devnum: Device number (0 for first device on that interface, 4376eef6eacSSimon Glass * 1 for second, etc. 4386eef6eacSSimon Glass * @descp: Returns pointer to the block device on success 4396eef6eacSSimon Glass * @return 0 if OK, -ve on error 4406eef6eacSSimon Glass */ 4416eef6eacSSimon Glass int (*get_dev)(int devnum, struct blk_desc **descp); 4426eef6eacSSimon Glass 4436eef6eacSSimon Glass /** 4446eef6eacSSimon Glass * select_hwpart() - Select a hardware partition 4456eef6eacSSimon Glass * 4466eef6eacSSimon Glass * Some devices (e.g. MMC) can support partitioning at the hardware 4476eef6eacSSimon Glass * level. This is quite separate from the normal idea of 4486eef6eacSSimon Glass * software-based partitions. MMC hardware partitions must be 4496eef6eacSSimon Glass * explicitly selected. Once selected only the region of the device 4506eef6eacSSimon Glass * covered by that partition is accessible. 4516eef6eacSSimon Glass * 4526eef6eacSSimon Glass * The MMC standard provides for two boot partitions (numbered 1 and 2), 4536eef6eacSSimon Glass * rpmb (3), and up to 4 addition general-purpose partitions (4-7). 4546eef6eacSSimon Glass * Partition 0 is the main user-data partition. 4556eef6eacSSimon Glass * 4566eef6eacSSimon Glass * @desc: Block device descriptor 4576eef6eacSSimon Glass * @hwpart: Hardware partition number to select. 0 means the main 4586eef6eacSSimon Glass * user-data partition, 1 is the first partition, 2 is 4596eef6eacSSimon Glass * the second, etc. 4606eef6eacSSimon Glass * @return 0 if OK, other value for an error 4616eef6eacSSimon Glass */ 4626eef6eacSSimon Glass int (*select_hwpart)(struct blk_desc *desc, int hwpart); 4636eef6eacSSimon Glass }; 4646eef6eacSSimon Glass 4656eef6eacSSimon Glass /* 4666eef6eacSSimon Glass * Declare a new U-Boot legacy block driver. New drivers should use driver 4676eef6eacSSimon Glass * model (UCLASS_BLK). 4686eef6eacSSimon Glass */ 4696eef6eacSSimon Glass #define U_BOOT_LEGACY_BLK(__name) \ 4706eef6eacSSimon Glass ll_entry_declare(struct blk_driver, __name, blk_driver) 4716eef6eacSSimon Glass 4726eef6eacSSimon Glass struct blk_driver *blk_driver_lookup_type(int if_type); 4736eef6eacSSimon Glass 47409d71aacSSimon Glass #endif /* !CONFIG_BLK */ 4752a981dc2SSimon Glass 4766eef6eacSSimon Glass /** 4776eef6eacSSimon Glass * blk_get_devnum_by_typename() - Get a block device by type and number 4786eef6eacSSimon Glass * 4796eef6eacSSimon Glass * This looks through the available block devices of the given type, returning 4806eef6eacSSimon Glass * the one with the given @devnum. 4816eef6eacSSimon Glass * 4826eef6eacSSimon Glass * @if_type: Block device type 4836eef6eacSSimon Glass * @devnum: Device number 4846eef6eacSSimon Glass * @return point to block device descriptor, or NULL if not found 4856eef6eacSSimon Glass */ 4866eef6eacSSimon Glass struct blk_desc *blk_get_devnum_by_type(enum if_type if_type, int devnum); 4876eef6eacSSimon Glass 4886eef6eacSSimon Glass /** 4896eef6eacSSimon Glass * blk_get_devnum_by_type() - Get a block device by type name, and number 4906eef6eacSSimon Glass * 4916eef6eacSSimon Glass * This looks up the block device type based on @if_typename, then calls 4926eef6eacSSimon Glass * blk_get_devnum_by_type(). 4936eef6eacSSimon Glass * 4946eef6eacSSimon Glass * @if_typename: Block device type name 4956eef6eacSSimon Glass * @devnum: Device number 4966eef6eacSSimon Glass * @return point to block device descriptor, or NULL if not found 4976eef6eacSSimon Glass */ 4986eef6eacSSimon Glass struct blk_desc *blk_get_devnum_by_typename(const char *if_typename, 4996eef6eacSSimon Glass int devnum); 5006eef6eacSSimon Glass 5016eef6eacSSimon Glass /** 5026eef6eacSSimon Glass * blk_dselect_hwpart() - select a hardware partition 5036eef6eacSSimon Glass * 5046eef6eacSSimon Glass * This selects a hardware partition (such as is supported by MMC). The block 5056eef6eacSSimon Glass * device size may change as this effectively points the block device to a 5066eef6eacSSimon Glass * partition at the hardware level. See the select_hwpart() method above. 5076eef6eacSSimon Glass * 5086eef6eacSSimon Glass * @desc: Block device descriptor for the device to select 5096eef6eacSSimon Glass * @hwpart: Partition number to select 5106eef6eacSSimon Glass * @return 0 if OK, -ve on error 5116eef6eacSSimon Glass */ 5126eef6eacSSimon Glass int blk_dselect_hwpart(struct blk_desc *desc, int hwpart); 5136eef6eacSSimon Glass 5146eef6eacSSimon Glass /** 5156eef6eacSSimon Glass * blk_list_part() - list the partitions for block devices of a given type 5166eef6eacSSimon Glass * 5176eef6eacSSimon Glass * This looks up the partition type for each block device of type @if_type, 5186eef6eacSSimon Glass * then displays a list of partitions. 5196eef6eacSSimon Glass * 5206eef6eacSSimon Glass * @if_type: Block device type 5216eef6eacSSimon Glass * @return 0 if OK, -ENODEV if there is none of that type 5226eef6eacSSimon Glass */ 5236eef6eacSSimon Glass int blk_list_part(enum if_type if_type); 5246eef6eacSSimon Glass 5256eef6eacSSimon Glass /** 5266eef6eacSSimon Glass * blk_list_devices() - list the block devices of a given type 5276eef6eacSSimon Glass * 5286eef6eacSSimon Glass * This lists each block device of the type @if_type, showing the capacity 5296eef6eacSSimon Glass * as well as type-specific information. 5306eef6eacSSimon Glass * 5316eef6eacSSimon Glass * @if_type: Block device type 5326eef6eacSSimon Glass */ 5336eef6eacSSimon Glass void blk_list_devices(enum if_type if_type); 5346eef6eacSSimon Glass 5356eef6eacSSimon Glass /** 5366eef6eacSSimon Glass * blk_show_device() - show information about a given block device 5376eef6eacSSimon Glass * 5386eef6eacSSimon Glass * This shows the block device capacity as well as type-specific information. 5396eef6eacSSimon Glass * 5406eef6eacSSimon Glass * @if_type: Block device type 5416eef6eacSSimon Glass * @devnum: Device number 5426eef6eacSSimon Glass * @return 0 if OK, -ENODEV for invalid device number 5436eef6eacSSimon Glass */ 5446eef6eacSSimon Glass int blk_show_device(enum if_type if_type, int devnum); 5456eef6eacSSimon Glass 5466eef6eacSSimon Glass /** 5476eef6eacSSimon Glass * blk_print_device_num() - show information about a given block device 5486eef6eacSSimon Glass * 5496eef6eacSSimon Glass * This is similar to blk_show_device() but returns an error if the block 5506eef6eacSSimon Glass * device type is unknown. 5516eef6eacSSimon Glass * 5526eef6eacSSimon Glass * @if_type: Block device type 5536eef6eacSSimon Glass * @devnum: Device number 5546eef6eacSSimon Glass * @return 0 if OK, -ENODEV for invalid device number, -ENOENT if the block 5556eef6eacSSimon Glass * device is not connected 5566eef6eacSSimon Glass */ 5576eef6eacSSimon Glass int blk_print_device_num(enum if_type if_type, int devnum); 5586eef6eacSSimon Glass 5596eef6eacSSimon Glass /** 5606eef6eacSSimon Glass * blk_print_part_devnum() - print the partition information for a device 5616eef6eacSSimon Glass * 5626eef6eacSSimon Glass * @if_type: Block device type 5636eef6eacSSimon Glass * @devnum: Device number 5646eef6eacSSimon Glass * @return 0 if OK, -ENOENT if the block device is not connected, -ENOSYS if 5656eef6eacSSimon Glass * the interface type is not supported, other -ve on other error 5666eef6eacSSimon Glass */ 5676eef6eacSSimon Glass int blk_print_part_devnum(enum if_type if_type, int devnum); 5686eef6eacSSimon Glass 5696eef6eacSSimon Glass /** 5706eef6eacSSimon Glass * blk_read_devnum() - read blocks from a device 5716eef6eacSSimon Glass * 5726eef6eacSSimon Glass * @if_type: Block device type 5736eef6eacSSimon Glass * @devnum: Device number 5746eef6eacSSimon Glass * @blkcnt: Number of blocks to read 5756eef6eacSSimon Glass * @buffer: Address to write data to 5766eef6eacSSimon Glass * @return number of blocks read, or -ve error number on error 5776eef6eacSSimon Glass */ 5786eef6eacSSimon Glass ulong blk_read_devnum(enum if_type if_type, int devnum, lbaint_t start, 5796eef6eacSSimon Glass lbaint_t blkcnt, void *buffer); 5806eef6eacSSimon Glass 5816eef6eacSSimon Glass /** 5826eef6eacSSimon Glass * blk_write_devnum() - write blocks to a device 5836eef6eacSSimon Glass * 5846eef6eacSSimon Glass * @if_type: Block device type 5856eef6eacSSimon Glass * @devnum: Device number 5866eef6eacSSimon Glass * @blkcnt: Number of blocks to write 5876eef6eacSSimon Glass * @buffer: Address to read data from 5886eef6eacSSimon Glass * @return number of blocks written, or -ve error number on error 5896eef6eacSSimon Glass */ 5906eef6eacSSimon Glass ulong blk_write_devnum(enum if_type if_type, int devnum, lbaint_t start, 5916eef6eacSSimon Glass lbaint_t blkcnt, const void *buffer); 5926eef6eacSSimon Glass 5936eef6eacSSimon Glass /** 5946eef6eacSSimon Glass * blk_select_hwpart_devnum() - select a hardware partition 5956eef6eacSSimon Glass * 5966eef6eacSSimon Glass * This is similar to blk_dselect_hwpart() but it looks up the interface and 5976eef6eacSSimon Glass * device number. 5986eef6eacSSimon Glass * 5996eef6eacSSimon Glass * @if_type: Block device type 6006eef6eacSSimon Glass * @devnum: Device number 6016eef6eacSSimon Glass * @hwpart: Partition number to select 6026eef6eacSSimon Glass * @return 0 if OK, -ve on error 6036eef6eacSSimon Glass */ 6046eef6eacSSimon Glass int blk_select_hwpart_devnum(enum if_type if_type, int devnum, int hwpart); 6056eef6eacSSimon Glass 6061a73661bSSimon Glass #endif 607