1 /* Copyright 2013-2014 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef __LIBFLASH_H
17 #define __LIBFLASH_H
18 
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include <libflash/blocklevel.h>
22 
23 /* API status/return:
24  *
25  *  <0 = flash controller errors passed through,
26  *  0  = success
27  *  >0 = libflash error
28  */
29 #include <libflash/errors.h>
30 
31 #ifndef MIN
32 #define MIN(a, b)	((a) < (b) ? (a) : (b))
33 #endif
34 
35 /* Flash chip, opaque */
36 struct flash_chip;
37 struct spi_flash_ctrl;
38 
39 int flash_init(struct spi_flash_ctrl *ctrl, struct blocklevel_device **bl,
40 		struct flash_chip **flash_chip);
41 void flash_exit(struct blocklevel_device *bl);
42 
43 /*
44  * Function which till call close on the underlying struct spi_flash_ctrl
45  */
46 void flash_exit_close(struct blocklevel_device *bl, void (*close)(struct spi_flash_ctrl *ctrl));
47 
48 /* libflash sets the 4b mode automatically based on the flash
49  * size and controller capabilities but it can be overriden
50  */
51 int flash_force_4b_mode(struct flash_chip *c, bool enable_4b);
52 
53 /*
54  * This provides a wapper around flash_read() on ECCed data. All params are
55  * the same as to flash_read(). Not passing true in ecc is akin to calling
56  * flash_read() directly.
57  *
58  * len is length of data without ecc attached therefore this will read beyond
59  * pos + len.
60  */
61 int flash_read_corrected(struct blocklevel_device *bl, uint32_t pos, void *buf,
62 		uint32_t len, bool ecc);
63 
64 /*
65  * This provides a wrapper around flash_write() on ECCed data. All params are
66  * the same as to flash_write(). Not passing true in ecc is akin to calling
67  * flash_write() directly.
68  *
69  * size is length of data without ECC attached therefore this will write beyond
70  * dst + size.
71  */
72 int flash_write_corrected(struct blocklevel_device *bl, uint32_t dst, const void *src,
73 		uint32_t size, bool verify, bool ecc);
74 
75 /*
76  * This provides a wrapper around flash_smart_write() on ECCed data. All
77  * params are the same as to flash_smart_write(). Not passing true in ecc is
78  * akin to calling flash_smart_write() directly.
79  *
80  * size is length of data without ECC attached therefore this will write beyond
81  * dst + size.
82  */
83 int flash_smart_write_corrected(struct blocklevel_device *bl, uint32_t dst, const void *src,
84 		      uint32_t size, bool ecc);
85 
86 /* chip erase may not be supported by all chips/controllers, get ready
87  * for FLASH_ERR_CHIP_ER_NOT_SUPPORTED
88  */
89 int flash_erase_chip(struct flash_chip *c);
90 
91 #endif /* __LIBFLASH_H */
92