xref: /rk3399_rockchip-uboot/include/spi_flash.h (revision 33adfb5f9b06ac1a386dddc222cc50e24a9909e2)
1 /*
2  * Common SPI flash Interface
3  *
4  * Copyright (C) 2008 Atmel Corporation
5  * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * version 2 as published by the Free Software Foundation.
13  */
14 
15 #ifndef _SPI_FLASH_H_
16 #define _SPI_FLASH_H_
17 
18 #include <spi.h>
19 #include <linux/types.h>
20 #include <linux/compiler.h>
21 
22 /* No enum list for write commands only QPP */
23 #define WR_QPP		1 << 4
24 
25 /* Enum list - Full read commands */
26 enum spi_read_cmds {
27 	ARRAY_SLOW = 1 << 0,
28 	DUAL_OUTPUT_FAST = 1 << 1,
29 	DUAL_IO_FAST = 1 << 2,
30 	QUAD_OUTPUT_FAST = 1 << 3,
31 };
32 #define RD_EXTN		ARRAY_SLOW | DUAL_OUTPUT_FAST | DUAL_IO_FAST
33 #define RD_FULL		RD_EXTN | QUAD_OUTPUT_FAST
34 
35 /**
36  * struct spi_flash_params - SPI/QSPI flash device params structure
37  *
38  * @name:		Device name ([MANUFLETTER][DEVTYPE][DENSITY][EXTRAINFO])
39  * @jedec:		Device jedec ID (0x[1byte_manuf_id][2byte_dev_id])
40  * @ext_jedec:		Device ext_jedec ID
41  * @sector_size:	Sector size of this device
42  * @nr_sectors:		No.of sectors on this device
43  * @e_rd_cmd:		Enum list for read commands
44  * @flags:		Importent param, for flash specific behaviour
45  */
46 struct spi_flash_params {
47 	const char *name;
48 	u32 jedec;
49 	u16 ext_jedec;
50 	u32 sector_size;
51 	u32 nr_sectors;
52 	u8 e_rd_cmd;
53 	u16 flags;
54 };
55 
56 extern const struct spi_flash_params spi_flash_params_table[];
57 
58 /**
59  * struct spi_flash - SPI flash structure
60  *
61  * @spi:		SPI slave
62  * @name:		Name of SPI flash
63  * @size:		Total flash size
64  * @page_size:		Write (page) size
65  * @sector_size:	Sector size
66  * @erase_size:		Erase size
67  * @bank_read_cmd:	Bank read cmd
68  * @bank_write_cmd:	Bank write cmd
69  * @bank_curr:		Current flash bank
70  * @poll_cmd:		Poll cmd - for flash erase/program
71  * @erase_cmd:		Erase cmd 4K, 32K, 64K
72  * @read_cmd:		Read cmd - Array Fast, Extn read and quad read.
73  * @write_cmd:		Write cmd - page and quad program.
74  * @memory_map:		Address of read-only SPI flash access
75  * @read:		Flash read ops: Read len bytes at offset into buf
76  *			Supported cmds: Fast Array Read
77  * @write:		Flash write ops: Write len bytes from buf into offeset
78  *			Supported cmds: Page Program
79  * @erase:		Flash erase ops: Erase len bytes from offset
80  *			Supported cmds: Sector erase 4K, 32K, 64K
81  * return 0 - Sucess, 1 - Failure
82  */
83 struct spi_flash {
84 	struct spi_slave *spi;
85 	const char *name;
86 
87 	u32 size;
88 	u32 page_size;
89 	u32 sector_size;
90 	u32 erase_size;
91 #ifdef CONFIG_SPI_FLASH_BAR
92 	u8 bank_read_cmd;
93 	u8 bank_write_cmd;
94 	u8 bank_curr;
95 #endif
96 	u8 poll_cmd;
97 	u8 erase_cmd;
98 	u8 read_cmd;
99 	u8 write_cmd;
100 
101 	void *memory_map;
102 	int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
103 	int (*write)(struct spi_flash *flash, u32 offset, size_t len,
104 			const void *buf);
105 	int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
106 };
107 
108 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
109 		unsigned int max_hz, unsigned int spi_mode);
110 
111 /**
112  * Set up a new SPI flash from an fdt node
113  *
114  * @param blob		Device tree blob
115  * @param slave_node	Pointer to this SPI slave node in the device tree
116  * @param spi_node	Cached pointer to the SPI interface this node belongs
117  *			to
118  * @return 0 if ok, -1 on error
119  */
120 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
121 				      int spi_node);
122 
123 void spi_flash_free(struct spi_flash *flash);
124 
125 static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
126 		size_t len, void *buf)
127 {
128 	return flash->read(flash, offset, len, buf);
129 }
130 
131 static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
132 		size_t len, const void *buf)
133 {
134 	return flash->write(flash, offset, len, buf);
135 }
136 
137 static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
138 		size_t len)
139 {
140 	return flash->erase(flash, offset, len);
141 }
142 
143 void spi_boot(void) __noreturn;
144 
145 #endif /* _SPI_FLASH_H_ */
146