1 /*
2  * Copyright (c) 2019-2020, Broadcom
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef SF_H
8 #define SF_H
9 
10 #include <stdint.h>
11 #include <stddef.h>
12 
13 #ifdef SPI_DEBUG
14 #define SPI_DEBUG(fmt, ...)	INFO(fmt, ##__VA_ARGS__)
15 #else
16 #define SPI_DEBUG(fmt, ...)
17 #endif
18 
19 #define SPI_FLASH_MAX_ID_LEN	6
20 
21 #define CMD_WRSR		0x01 /* Write status register */
22 #define CMD_PAGE_PROGRAM	0x02
23 #define CMD_READ_NORMAL		0x03
24 #define CMD_RDSR		0x05
25 #define CMD_WRITE_ENABLE	0x06
26 #define CMD_RDFSR		0x70
27 #define CMD_READ_ID		0x9f
28 #define CMD_ERASE_4K		0x20
29 #define CMD_ERASE_64K		0xd8
30 #define ERASE_SIZE_64K		(64 * 1024)
31 
32 /* Common status */
33 #define STATUS_WIP		BIT(0)
34 
35 struct spi_flash {
36 	struct spi_slave *spi;
37 	uint32_t size;
38 	uint32_t page_size;
39 	uint32_t sector_size;
40 	uint32_t erase_size;
41 	uint8_t erase_cmd;
42 	uint8_t read_cmd;
43 	uint8_t write_cmd;
44 	uint8_t flags;
45 };
46 
47 struct spi_flash_info {
48 	const char *name;
49 
50 	/*
51 	 * This array stores the ID bytes.
52 	 * The first three bytes are the JEDIC ID.
53 	 * JEDEC ID zero means "no ID" (mostly older chips).
54 	 */
55 	uint8_t id[SPI_FLASH_MAX_ID_LEN];
56 	uint8_t id_len;
57 
58 	uint32_t sector_size;
59 	uint32_t n_sectors;
60 	uint16_t page_size;
61 
62 	uint8_t flags;
63 };
64 
65 /* Enum list - Full read commands */
66 enum spi_read_cmds {
67 	ARRAY_SLOW              = BIT(0),
68 	ARRAY_FAST              = BIT(1),
69 	DUAL_OUTPUT_FAST        = BIT(2),
70 	DUAL_IO_FAST            = BIT(3),
71 	QUAD_OUTPUT_FAST        = BIT(4),
72 	QUAD_IO_FAST            = BIT(5),
73 };
74 
75 /* sf param flags */
76 enum spi_param_flag {
77 	SECT_4K         = BIT(0),
78 	SECT_32K        = BIT(1),
79 	E_FSR           = BIT(2),
80 	SST_BP          = BIT(3),
81 	SST_WP          = BIT(4),
82 	WR_QPP          = BIT(5),
83 };
84 
85 int spi_flash_cmd_read(const uint8_t *cmd, size_t cmd_len,
86 		       void *data, size_t data_len);
87 int spi_flash_cmd(uint8_t cmd, void *response, size_t len);
88 int spi_flash_cmd_write(const uint8_t *cmd, size_t cmd_len,
89 			const void *data, size_t data_len);
90 #endif
91