]>
Commit | Line | Data |
---|---|---|
1a73661b SG |
1 | /* |
2 | * (C) Copyright 2000-2004 | |
3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
4 | * | |
5 | * SPDX-License-Identifier: GPL-2.0+ | |
6 | */ | |
7 | ||
8 | #ifndef BLK_H | |
9 | #define BLK_H | |
10 | ||
11 | #ifdef CONFIG_SYS_64BIT_LBA | |
12 | typedef uint64_t lbaint_t; | |
13 | #define LBAFlength "ll" | |
14 | #else | |
15 | typedef ulong lbaint_t; | |
16 | #define LBAFlength "l" | |
17 | #endif | |
18 | #define LBAF "%" LBAFlength "x" | |
19 | #define LBAFU "%" LBAFlength "u" | |
20 | ||
21 | /* Interface types: */ | |
5ec4f1a5 SG |
22 | enum if_type { |
23 | IF_TYPE_UNKNOWN = 0, | |
24 | IF_TYPE_IDE, | |
25 | IF_TYPE_SCSI, | |
26 | IF_TYPE_ATAPI, | |
27 | IF_TYPE_USB, | |
28 | IF_TYPE_DOC, | |
29 | IF_TYPE_MMC, | |
30 | IF_TYPE_SD, | |
31 | IF_TYPE_SATA, | |
32 | IF_TYPE_HOST, | |
33 | ||
34 | IF_TYPE_COUNT, /* Number of interface types */ | |
35 | }; | |
1a73661b | 36 | |
09d71aac SG |
37 | /* |
38 | * With driver model (CONFIG_BLK) this is uclass platform data, accessible | |
39 | * with dev_get_uclass_platdata(dev) | |
40 | */ | |
1a73661b | 41 | struct blk_desc { |
09d71aac SG |
42 | /* |
43 | * TODO: With driver model we should be able to use the parent | |
44 | * device's uclass instead. | |
45 | */ | |
5ec4f1a5 | 46 | enum if_type if_type; /* type of the interface */ |
bcce53d0 | 47 | int devnum; /* device number */ |
1a73661b SG |
48 | unsigned char part_type; /* partition type */ |
49 | unsigned char target; /* target SCSI ID */ | |
50 | unsigned char lun; /* target LUN */ | |
51 | unsigned char hwpart; /* HW partition, e.g. for eMMC */ | |
52 | unsigned char type; /* device type */ | |
53 | unsigned char removable; /* removable device */ | |
54 | #ifdef CONFIG_LBA48 | |
55 | /* device can use 48bit addr (ATA/ATAPI v7) */ | |
56 | unsigned char lba48; | |
57 | #endif | |
58 | lbaint_t lba; /* number of blocks */ | |
59 | unsigned long blksz; /* block size */ | |
60 | int log2blksz; /* for convenience: log2(blksz) */ | |
61 | char vendor[40+1]; /* IDE model, SCSI Vendor */ | |
62 | char product[20+1]; /* IDE Serial no, SCSI product */ | |
63 | char revision[8+1]; /* firmware revision */ | |
09d71aac SG |
64 | #ifdef CONFIG_BLK |
65 | struct udevice *bdev; | |
66 | #else | |
1a73661b SG |
67 | unsigned long (*block_read)(struct blk_desc *block_dev, |
68 | lbaint_t start, | |
69 | lbaint_t blkcnt, | |
70 | void *buffer); | |
71 | unsigned long (*block_write)(struct blk_desc *block_dev, | |
72 | lbaint_t start, | |
73 | lbaint_t blkcnt, | |
74 | const void *buffer); | |
75 | unsigned long (*block_erase)(struct blk_desc *block_dev, | |
76 | lbaint_t start, | |
77 | lbaint_t blkcnt); | |
78 | void *priv; /* driver private struct pointer */ | |
09d71aac | 79 | #endif |
1a73661b SG |
80 | }; |
81 | ||
82 | #define BLOCK_CNT(size, blk_desc) (PAD_COUNT(size, blk_desc->blksz)) | |
83 | #define PAD_TO_BLOCKSIZE(size, blk_desc) \ | |
84 | (PAD_SIZE(size, blk_desc->blksz)) | |
85 | ||
e40cf34a EN |
86 | #ifdef CONFIG_BLOCK_CACHE |
87 | /** | |
88 | * blkcache_read() - attempt to read a set of blocks from cache | |
89 | * | |
90 | * @param iftype - IF_TYPE_x for type of device | |
91 | * @param dev - device index of particular type | |
92 | * @param start - starting block number | |
93 | * @param blkcnt - number of blocks to read | |
94 | * @param blksz - size in bytes of each block | |
95 | * @param buf - buffer to contain cached data | |
96 | * | |
97 | * @return - '1' if block returned from cache, '0' otherwise. | |
98 | */ | |
c8e4d2a8 EN |
99 | int blkcache_read(int iftype, int dev, |
100 | lbaint_t start, lbaint_t blkcnt, | |
101 | unsigned long blksz, void *buffer); | |
e40cf34a EN |
102 | |
103 | /** | |
104 | * blkcache_fill() - make data read from a block device available | |
105 | * to the block cache | |
106 | * | |
107 | * @param iftype - IF_TYPE_x for type of device | |
108 | * @param dev - device index of particular type | |
109 | * @param start - starting block number | |
110 | * @param blkcnt - number of blocks available | |
111 | * @param blksz - size in bytes of each block | |
112 | * @param buf - buffer containing data to cache | |
113 | * | |
114 | */ | |
c8e4d2a8 EN |
115 | void blkcache_fill(int iftype, int dev, |
116 | lbaint_t start, lbaint_t blkcnt, | |
117 | unsigned long blksz, void const *buffer); | |
e40cf34a EN |
118 | |
119 | /** | |
120 | * blkcache_invalidate() - discard the cache for a set of blocks | |
121 | * because of a write or device (re)initialization. | |
122 | * | |
123 | * @param iftype - IF_TYPE_x for type of device | |
124 | * @param dev - device index of particular type | |
125 | */ | |
c8e4d2a8 | 126 | void blkcache_invalidate(int iftype, int dev); |
e40cf34a EN |
127 | |
128 | /** | |
129 | * blkcache_configure() - configure block cache | |
130 | * | |
131 | * @param blocks - maximum blocks per entry | |
132 | * @param entries - maximum entries in cache | |
133 | */ | |
134 | void blkcache_configure(unsigned blocks, unsigned entries); | |
135 | ||
136 | /* | |
137 | * statistics of the block cache | |
138 | */ | |
139 | struct block_cache_stats { | |
140 | unsigned hits; | |
141 | unsigned misses; | |
142 | unsigned entries; /* current entry count */ | |
143 | unsigned max_blocks_per_entry; | |
144 | unsigned max_entries; | |
145 | }; | |
146 | ||
147 | /** | |
148 | * get_blkcache_stats() - return statistics and reset | |
149 | * | |
150 | * @param stats - statistics are copied here | |
151 | */ | |
152 | void blkcache_stats(struct block_cache_stats *stats); | |
153 | ||
154 | #else | |
155 | ||
c8e4d2a8 EN |
156 | static inline int blkcache_read(int iftype, int dev, |
157 | lbaint_t start, lbaint_t blkcnt, | |
158 | unsigned long blksz, void *buffer) | |
e40cf34a EN |
159 | { |
160 | return 0; | |
161 | } | |
162 | ||
c8e4d2a8 EN |
163 | static inline void blkcache_fill(int iftype, int dev, |
164 | lbaint_t start, lbaint_t blkcnt, | |
165 | unsigned long blksz, void const *buffer) {} | |
e40cf34a | 166 | |
c8e4d2a8 | 167 | static inline void blkcache_invalidate(int iftype, int dev) {} |
e40cf34a EN |
168 | |
169 | #endif | |
170 | ||
09d71aac SG |
171 | #ifdef CONFIG_BLK |
172 | struct udevice; | |
173 | ||
174 | /* Operations on block devices */ | |
175 | struct blk_ops { | |
176 | /** | |
177 | * read() - read from a block device | |
178 | * | |
179 | * @dev: Device to read from | |
180 | * @start: Start block number to read (0=first) | |
181 | * @blkcnt: Number of blocks to read | |
182 | * @buffer: Destination buffer for data read | |
183 | * @return number of blocks read, or -ve error number (see the | |
184 | * IS_ERR_VALUE() macro | |
185 | */ | |
186 | unsigned long (*read)(struct udevice *dev, lbaint_t start, | |
187 | lbaint_t blkcnt, void *buffer); | |
188 | ||
189 | /** | |
190 | * write() - write to a block device | |
191 | * | |
192 | * @dev: Device to write to | |
193 | * @start: Start block number to write (0=first) | |
194 | * @blkcnt: Number of blocks to write | |
195 | * @buffer: Source buffer for data to write | |
196 | * @return number of blocks written, or -ve error number (see the | |
197 | * IS_ERR_VALUE() macro | |
198 | */ | |
199 | unsigned long (*write)(struct udevice *dev, lbaint_t start, | |
200 | lbaint_t blkcnt, const void *buffer); | |
201 | ||
202 | /** | |
203 | * erase() - erase a section of a block device | |
204 | * | |
205 | * @dev: Device to (partially) erase | |
206 | * @start: Start block number to erase (0=first) | |
207 | * @blkcnt: Number of blocks to erase | |
208 | * @return number of blocks erased, or -ve error number (see the | |
209 | * IS_ERR_VALUE() macro | |
210 | */ | |
211 | unsigned long (*erase)(struct udevice *dev, lbaint_t start, | |
212 | lbaint_t blkcnt); | |
213 | }; | |
214 | ||
215 | #define blk_get_ops(dev) ((struct blk_ops *)(dev)->driver->ops) | |
216 | ||
217 | /* | |
218 | * These functions should take struct udevice instead of struct blk_desc, | |
219 | * but this is convenient for migration to driver model. Add a 'd' prefix | |
220 | * to the function operations, so that blk_read(), etc. can be reserved for | |
221 | * functions with the correct arguments. | |
222 | */ | |
223 | unsigned long blk_dread(struct blk_desc *block_dev, lbaint_t start, | |
224 | lbaint_t blkcnt, void *buffer); | |
225 | unsigned long blk_dwrite(struct blk_desc *block_dev, lbaint_t start, | |
226 | lbaint_t blkcnt, const void *buffer); | |
227 | unsigned long blk_derase(struct blk_desc *block_dev, lbaint_t start, | |
228 | lbaint_t blkcnt); | |
229 | ||
230 | /** | |
231 | * blk_get_device() - Find and probe a block device ready for use | |
232 | * | |
233 | * @if_type: Interface type (enum if_type_t) | |
234 | * @devnum: Device number (specific to each interface type) | |
235 | * @devp: the device, if found | |
236 | * @return - if found, -ENODEV if no device found, or other -ve error value | |
237 | */ | |
238 | int blk_get_device(int if_type, int devnum, struct udevice **devp); | |
239 | ||
240 | /** | |
241 | * blk_first_device() - Find the first device for a given interface | |
242 | * | |
243 | * The device is probed ready for use | |
244 | * | |
245 | * @devnum: Device number (specific to each interface type) | |
246 | * @devp: the device, if found | |
247 | * @return 0 if found, -ENODEV if no device, or other -ve error value | |
248 | */ | |
249 | int blk_first_device(int if_type, struct udevice **devp); | |
250 | ||
251 | /** | |
252 | * blk_next_device() - Find the next device for a given interface | |
253 | * | |
254 | * This can be called repeatedly after blk_first_device() to iterate through | |
255 | * all devices of the given interface type. | |
256 | * | |
257 | * The device is probed ready for use | |
258 | * | |
259 | * @devp: On entry, the previous device returned. On exit, the next | |
260 | * device, if found | |
261 | * @return 0 if found, -ENODEV if no device, or other -ve error value | |
262 | */ | |
263 | int blk_next_device(struct udevice **devp); | |
264 | ||
265 | /** | |
266 | * blk_create_device() - Create a new block device | |
267 | * | |
268 | * @parent: Parent of the new device | |
269 | * @drv_name: Driver name to use for the block device | |
270 | * @name: Name for the device | |
271 | * @if_type: Interface type (enum if_type_t) | |
272 | * @devnum: Device number, specific to the interface type | |
273 | * @blksz: Block size of the device in bytes (typically 512) | |
274 | * @size: Total size of the device in bytes | |
275 | * @devp: the new device (which has not been probed) | |
276 | */ | |
277 | int blk_create_device(struct udevice *parent, const char *drv_name, | |
278 | const char *name, int if_type, int devnum, int blksz, | |
279 | lbaint_t size, struct udevice **devp); | |
280 | ||
281 | /** | |
282 | * blk_prepare_device() - Prepare a block device for use | |
283 | * | |
284 | * This reads partition information from the device if supported. | |
285 | * | |
286 | * @dev: Device to prepare | |
287 | * @return 0 if ok, -ve on error | |
288 | */ | |
289 | int blk_prepare_device(struct udevice *dev); | |
290 | ||
291 | /** | |
292 | * blk_unbind_all() - Unbind all device of the given interface type | |
293 | * | |
294 | * The devices are removed and then unbound. | |
295 | * | |
296 | * @if_type: Interface type to unbind | |
297 | * @return 0 if OK, -ve on error | |
298 | */ | |
299 | int blk_unbind_all(int if_type); | |
300 | ||
301 | #else | |
302 | #include <errno.h> | |
2a981dc2 SG |
303 | /* |
304 | * These functions should take struct udevice instead of struct blk_desc, | |
305 | * but this is convenient for migration to driver model. Add a 'd' prefix | |
306 | * to the function operations, so that blk_read(), etc. can be reserved for | |
307 | * functions with the correct arguments. | |
308 | */ | |
309 | static inline ulong blk_dread(struct blk_desc *block_dev, lbaint_t start, | |
310 | lbaint_t blkcnt, void *buffer) | |
311 | { | |
e40cf34a EN |
312 | ulong blks_read; |
313 | if (blkcache_read(block_dev->if_type, block_dev->devnum, | |
314 | start, blkcnt, block_dev->blksz, buffer)) | |
315 | return blkcnt; | |
316 | ||
2a981dc2 SG |
317 | /* |
318 | * We could check if block_read is NULL and return -ENOSYS. But this | |
319 | * bloats the code slightly (cause some board to fail to build), and | |
320 | * it would be an error to try an operation that does not exist. | |
321 | */ | |
e40cf34a EN |
322 | blks_read = block_dev->block_read(block_dev, start, blkcnt, buffer); |
323 | if (blks_read == blkcnt) | |
324 | blkcache_fill(block_dev->if_type, block_dev->devnum, | |
325 | start, blkcnt, block_dev->blksz, buffer); | |
326 | ||
327 | return blks_read; | |
2a981dc2 SG |
328 | } |
329 | ||
330 | static inline ulong blk_dwrite(struct blk_desc *block_dev, lbaint_t start, | |
331 | lbaint_t blkcnt, const void *buffer) | |
332 | { | |
e40cf34a | 333 | blkcache_invalidate(block_dev->if_type, block_dev->devnum); |
2a981dc2 SG |
334 | return block_dev->block_write(block_dev, start, blkcnt, buffer); |
335 | } | |
336 | ||
337 | static inline ulong blk_derase(struct blk_desc *block_dev, lbaint_t start, | |
338 | lbaint_t blkcnt) | |
339 | { | |
e40cf34a | 340 | blkcache_invalidate(block_dev->if_type, block_dev->devnum); |
2a981dc2 SG |
341 | return block_dev->block_erase(block_dev, start, blkcnt); |
342 | } | |
09d71aac | 343 | #endif /* !CONFIG_BLK */ |
2a981dc2 | 344 | |
1a73661b | 345 | #endif |