blob: a341c54eb2a1243cbf81f5de3053845a7df8ecfe [file] [log] [blame]
Norman James6a58a272015-10-07 14:34:16 -05001#ifndef __LIBFLASH_H
2#define __LIBFLASH_H
3
4#include <stdint.h>
5#include <stdbool.h>
6
7#ifndef FL_INF
8#define FL_INF(fmt...) do { printf(fmt); } while(0)
9#endif
10
11#ifndef FL_DBG
12#define FL_DBG(fmt...) do { if (libflash_debug) printf(fmt); } while(0)
13#endif
14
15#ifndef FL_ERR
16#define FL_ERR(fmt...) do { printf(fmt); } while(0)
17#endif
18
19extern bool libflash_debug;
20
21/* API status/return:
22 *
23 * <0 = flash controller errors passed through,
24 * 0 = success
25 * >0 = libflash error
26 */
27
28#define FLASH_ERR_MALLOC_FAILED 1
29#define FLASH_ERR_CHIP_UNKNOWN 2
30#define FLASH_ERR_PARM_ERROR 3
31#define FLASH_ERR_ERASE_BOUNDARY 4
32#define FLASH_ERR_WREN_TIMEOUT 5
33#define FLASH_ERR_WIP_TIMEOUT 6
34#define FLASH_ERR_BAD_PAGE_SIZE 7
35#define FLASH_ERR_VERIFY_FAILURE 8
36#define FLASH_ERR_4B_NOT_SUPPORTED 9
37#define FLASH_ERR_CTRL_CONFIG_MISMATCH 10
38#define FLASH_ERR_CHIP_ER_NOT_SUPPORTED 11
39#define FLASH_ERR_CTRL_CMD_UNSUPPORTED 12
40#define FLASH_ERR_CTRL_TIMEOUT 13
41
42/* Flash chip, opaque */
43struct flash_chip;
44struct spi_flash_ctrl;
45
46int flash_init(struct spi_flash_ctrl *ctrl, struct flash_chip **flash);
47void flash_exit(struct flash_chip *chip);
48
49int flash_get_info(struct flash_chip *chip, const char **name,
50 uint32_t *total_size, uint32_t *erase_granule);
51
52/* libflash sets the 4b mode automatically based on the flash
53 * size and controller capabilities but it can be overriden
54 */
55int flash_force_4b_mode(struct flash_chip *chip, bool enable_4b);
56
57int flash_read(struct flash_chip *c, uint32_t pos, void *buf, uint32_t len);
58int flash_erase(struct flash_chip *c, uint32_t dst, uint32_t size);
59int flash_write(struct flash_chip *c, uint32_t dst, const void *src,
60 uint32_t size, bool verify);
61int flash_smart_write(struct flash_chip *c, uint32_t dst, const void *src,
62 uint32_t size);
63
64/* chip erase may not be supported by all chips/controllers, get ready
65 * for FLASH_ERR_CHIP_ER_NOT_SUPPORTED
66 */
67int flash_erase_chip(struct flash_chip *c);
68
69#endif /* __LIBFLASH_H */