1 // SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
2 /* Copyright 2013-2017 IBM Corp. */
3 
4 #ifndef __LIBFLASH_H
5 #define __LIBFLASH_H
6 
7 #include <stdint.h>
8 #include <stdbool.h>
9 #include <libflash/blocklevel.h>
10 
11 /* API status/return:
12  *
13  *  <0 = flash controller errors passed through,
14  *  0  = success
15  *  >0 = libflash error
16  */
17 #include <libflash/errors.h>
18 
19 #ifndef MIN
20 #define MIN(a, b)	((a) < (b) ? (a) : (b))
21 #endif
22 
23 /* Flash chip, opaque */
24 struct flash_chip;
25 struct spi_flash_ctrl;
26 
27 int flash_init(struct spi_flash_ctrl *ctrl, struct blocklevel_device **bl,
28 		struct flash_chip **flash_chip);
29 void flash_exit(struct blocklevel_device *bl);
30 
31 /*
32  * Function which till call close on the underlying struct spi_flash_ctrl
33  */
34 void flash_exit_close(struct blocklevel_device *bl, void (*close)(struct spi_flash_ctrl *ctrl));
35 
36 /* libflash sets the 4b mode automatically based on the flash
37  * size and controller capabilities but it can be overriden
38  */
39 int flash_force_4b_mode(struct flash_chip *c, bool enable_4b);
40 
41 /*
42  * This provides a wapper around flash_read() on ECCed data. All params are
43  * the same as to flash_read(). Not passing true in ecc is akin to calling
44  * flash_read() directly.
45  *
46  * len is length of data without ecc attached therefore this will read beyond
47  * pos + len.
48  */
49 int flash_read_corrected(struct blocklevel_device *bl, uint32_t pos, void *buf,
50 		uint32_t len, bool ecc);
51 
52 /*
53  * This provides a wrapper around flash_write() on ECCed data. All params are
54  * the same as to flash_write(). Not passing true in ecc is akin to calling
55  * flash_write() directly.
56  *
57  * size is length of data without ECC attached therefore this will write beyond
58  * dst + size.
59  */
60 int flash_write_corrected(struct blocklevel_device *bl, uint32_t dst, const void *src,
61 		uint32_t size, bool verify, bool ecc);
62 
63 /*
64  * This provides a wrapper around flash_smart_write() on ECCed data. All
65  * params are the same as to flash_smart_write(). Not passing true in ecc is
66  * akin to calling flash_smart_write() directly.
67  *
68  * size is length of data without ECC attached therefore this will write beyond
69  * dst + size.
70  */
71 int flash_smart_write_corrected(struct blocklevel_device *bl, uint32_t dst, const void *src,
72 		      uint32_t size, bool ecc);
73 
74 /* chip erase may not be supported by all chips/controllers, get ready
75  * for FLASH_ERR_CHIP_ER_NOT_SUPPORTED
76  */
77 int flash_erase_chip(struct flash_chip *c);
78 
79 #endif /* __LIBFLASH_H */
80