xref: /rk3399_rockchip-uboot/include/linux/mtd/spinand.h (revision fc656fc366cf5a7efc56712ccc46a5aaccfea62b)
1749af7cdSPeter Pan /* SPDX-License-Identifier: GPL-2.0 */
2749af7cdSPeter Pan /*
3749af7cdSPeter Pan  * Copyright (c) 2016-2017 Micron Technology, Inc.
4749af7cdSPeter Pan  *
5749af7cdSPeter Pan  *  Authors:
6749af7cdSPeter Pan  *	Peter Pan <peterpandong@micron.com>
7749af7cdSPeter Pan  */
8749af7cdSPeter Pan #ifndef __LINUX_MTD_SPINAND_H
9749af7cdSPeter Pan #define __LINUX_MTD_SPINAND_H
10749af7cdSPeter Pan 
11749af7cdSPeter Pan #ifndef __UBOOT__
12749af7cdSPeter Pan #include <linux/mutex.h>
13749af7cdSPeter Pan #include <linux/bitops.h>
14749af7cdSPeter Pan #include <linux/device.h>
15749af7cdSPeter Pan #include <linux/mtd/mtd.h>
16749af7cdSPeter Pan #include <linux/mtd/nand.h>
17749af7cdSPeter Pan #include <linux/spi/spi.h>
18749af7cdSPeter Pan #include <linux/spi/spi-mem.h>
19749af7cdSPeter Pan #else
20749af7cdSPeter Pan #include <common.h>
21749af7cdSPeter Pan #include <spi.h>
22749af7cdSPeter Pan #include <spi-mem.h>
23749af7cdSPeter Pan #include <linux/mtd/nand.h>
24749af7cdSPeter Pan #endif
25749af7cdSPeter Pan 
26749af7cdSPeter Pan /**
27749af7cdSPeter Pan  * Standard SPI NAND flash operations
28749af7cdSPeter Pan  */
29749af7cdSPeter Pan 
30749af7cdSPeter Pan #define SPINAND_RESET_OP						\
31749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0xff, 1),				\
32749af7cdSPeter Pan 		   SPI_MEM_OP_NO_ADDR,					\
33749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DUMMY,					\
34749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DATA)
35749af7cdSPeter Pan 
36749af7cdSPeter Pan #define SPINAND_WR_EN_DIS_OP(enable)					\
37749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD((enable) ? 0x06 : 0x04, 1),		\
38749af7cdSPeter Pan 		   SPI_MEM_OP_NO_ADDR,					\
39749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DUMMY,					\
40749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DATA)
41749af7cdSPeter Pan 
42749af7cdSPeter Pan #define SPINAND_READID_OP(ndummy, buf, len)				\
43749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0x9f, 1),				\
44749af7cdSPeter Pan 		   SPI_MEM_OP_NO_ADDR,					\
45749af7cdSPeter Pan 		   SPI_MEM_OP_DUMMY(ndummy, 1),				\
46749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_IN(len, buf, 1))
47749af7cdSPeter Pan 
48749af7cdSPeter Pan #define SPINAND_SET_FEATURE_OP(reg, valptr)				\
49749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0x1f, 1),				\
50749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(1, reg, 1),				\
51749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DUMMY,					\
52749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_OUT(1, valptr, 1))
53749af7cdSPeter Pan 
54749af7cdSPeter Pan #define SPINAND_GET_FEATURE_OP(reg, valptr)				\
55749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0x0f, 1),				\
56749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(1, reg, 1),				\
57749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DUMMY,					\
58749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_IN(1, valptr, 1))
59749af7cdSPeter Pan 
60749af7cdSPeter Pan #define SPINAND_BLK_ERASE_OP(addr)					\
61749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0xd8, 1),				\
62749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(3, addr, 1),				\
63749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DUMMY,					\
64749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DATA)
65749af7cdSPeter Pan 
66749af7cdSPeter Pan #define SPINAND_PAGE_READ_OP(addr)					\
67749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0x13, 1),				\
68749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(3, addr, 1),				\
69749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DUMMY,					\
70749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DATA)
71749af7cdSPeter Pan 
72749af7cdSPeter Pan #define SPINAND_PAGE_READ_FROM_CACHE_OP(fast, addr, ndummy, buf, len)	\
73749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(fast ? 0x0b : 0x03, 1),		\
74749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(2, addr, 1),				\
75749af7cdSPeter Pan 		   SPI_MEM_OP_DUMMY(ndummy, 1),				\
76749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_IN(len, buf, 1))
77749af7cdSPeter Pan 
78749af7cdSPeter Pan #define SPINAND_PAGE_READ_FROM_CACHE_X2_OP(addr, ndummy, buf, len)	\
79749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0x3b, 1),				\
80749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(2, addr, 1),				\
81749af7cdSPeter Pan 		   SPI_MEM_OP_DUMMY(ndummy, 1),				\
82749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_IN(len, buf, 2))
83749af7cdSPeter Pan 
84749af7cdSPeter Pan #define SPINAND_PAGE_READ_FROM_CACHE_X4_OP(addr, ndummy, buf, len)	\
85749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0x6b, 1),				\
86749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(2, addr, 1),				\
87749af7cdSPeter Pan 		   SPI_MEM_OP_DUMMY(ndummy, 1),				\
88749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_IN(len, buf, 4))
89749af7cdSPeter Pan 
90749af7cdSPeter Pan #define SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(addr, ndummy, buf, len)	\
91749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0xbb, 1),				\
92749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(2, addr, 2),				\
93749af7cdSPeter Pan 		   SPI_MEM_OP_DUMMY(ndummy, 2),				\
94749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_IN(len, buf, 2))
95749af7cdSPeter Pan 
96749af7cdSPeter Pan #define SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(addr, ndummy, buf, len)	\
97749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0xeb, 1),				\
98749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(2, addr, 4),				\
99749af7cdSPeter Pan 		   SPI_MEM_OP_DUMMY(ndummy, 4),				\
100749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_IN(len, buf, 4))
101749af7cdSPeter Pan 
102749af7cdSPeter Pan #define SPINAND_PROG_EXEC_OP(addr)					\
103749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(0x10, 1),				\
104749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(3, addr, 1),				\
105749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DUMMY,					\
106749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DATA)
107749af7cdSPeter Pan 
108749af7cdSPeter Pan #define SPINAND_PROG_LOAD(reset, addr, buf, len)			\
109749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(reset ? 0x02 : 0x84, 1),		\
110749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(2, addr, 1),				\
111749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DUMMY,					\
112749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_OUT(len, buf, 1))
113749af7cdSPeter Pan 
114749af7cdSPeter Pan #define SPINAND_PROG_LOAD_X4(reset, addr, buf, len)			\
115749af7cdSPeter Pan 	SPI_MEM_OP(SPI_MEM_OP_CMD(reset ? 0x32 : 0x34, 1),		\
116749af7cdSPeter Pan 		   SPI_MEM_OP_ADDR(2, addr, 1),				\
117749af7cdSPeter Pan 		   SPI_MEM_OP_NO_DUMMY,					\
118749af7cdSPeter Pan 		   SPI_MEM_OP_DATA_OUT(len, buf, 4))
119749af7cdSPeter Pan 
120749af7cdSPeter Pan /**
121749af7cdSPeter Pan  * Standard SPI NAND flash commands
122749af7cdSPeter Pan  */
123749af7cdSPeter Pan #define SPINAND_CMD_PROG_LOAD_X4		0x32
124749af7cdSPeter Pan #define SPINAND_CMD_PROG_LOAD_RDM_DATA_X4	0x34
125749af7cdSPeter Pan 
126749af7cdSPeter Pan /* feature register */
127749af7cdSPeter Pan #define REG_BLOCK_LOCK		0xa0
128749af7cdSPeter Pan #define BL_ALL_UNLOCKED		0x00
129749af7cdSPeter Pan 
130749af7cdSPeter Pan /* configuration register */
131749af7cdSPeter Pan #define REG_CFG			0xb0
132749af7cdSPeter Pan #define CFG_OTP_ENABLE		BIT(6)
133749af7cdSPeter Pan #define CFG_ECC_ENABLE		BIT(4)
134749af7cdSPeter Pan #define CFG_QUAD_ENABLE		BIT(0)
135749af7cdSPeter Pan 
136749af7cdSPeter Pan /* status register */
137749af7cdSPeter Pan #define REG_STATUS		0xc0
138749af7cdSPeter Pan #define STATUS_BUSY		BIT(0)
139749af7cdSPeter Pan #define STATUS_ERASE_FAILED	BIT(2)
140749af7cdSPeter Pan #define STATUS_PROG_FAILED	BIT(3)
141749af7cdSPeter Pan #define STATUS_ECC_MASK		GENMASK(5, 4)
142749af7cdSPeter Pan #define STATUS_ECC_NO_BITFLIPS	(0 << 4)
143749af7cdSPeter Pan #define STATUS_ECC_HAS_BITFLIPS	(1 << 4)
144749af7cdSPeter Pan #define STATUS_ECC_UNCOR_ERROR	(2 << 4)
145749af7cdSPeter Pan 
146749af7cdSPeter Pan struct spinand_op;
147749af7cdSPeter Pan struct spinand_device;
148749af7cdSPeter Pan 
149749af7cdSPeter Pan #define SPINAND_MAX_ID_LEN	4
150749af7cdSPeter Pan 
151749af7cdSPeter Pan /**
152749af7cdSPeter Pan  * struct spinand_id - SPI NAND id structure
153749af7cdSPeter Pan  * @data: buffer containing the id bytes. Currently 4 bytes large, but can
154749af7cdSPeter Pan  *	  be extended if required
155749af7cdSPeter Pan  * @len: ID length
156749af7cdSPeter Pan  *
157749af7cdSPeter Pan  * struct_spinand_id->data contains all bytes returned after a READ_ID command,
158749af7cdSPeter Pan  * including dummy bytes if the chip does not emit ID bytes right after the
159749af7cdSPeter Pan  * READ_ID command. The responsibility to extract real ID bytes is left to
160749af7cdSPeter Pan  * struct_manufacurer_ops->detect().
161749af7cdSPeter Pan  */
162749af7cdSPeter Pan struct spinand_id {
163749af7cdSPeter Pan 	u8 data[SPINAND_MAX_ID_LEN];
164749af7cdSPeter Pan 	int len;
165749af7cdSPeter Pan };
166749af7cdSPeter Pan 
167749af7cdSPeter Pan /**
168749af7cdSPeter Pan  * struct manufacurer_ops - SPI NAND manufacturer specific operations
169749af7cdSPeter Pan  * @detect: detect a SPI NAND device. Every time a SPI NAND device is probed
170749af7cdSPeter Pan  *	    the core calls the struct_manufacurer_ops->detect() hook of each
171749af7cdSPeter Pan  *	    registered manufacturer until one of them return 1. Note that
172749af7cdSPeter Pan  *	    the first thing to check in this hook is that the manufacturer ID
173749af7cdSPeter Pan  *	    in struct_spinand_device->id matches the manufacturer whose
174749af7cdSPeter Pan  *	    ->detect() hook has been called. Should return 1 if there's a
175749af7cdSPeter Pan  *	    match, 0 if the manufacturer ID does not match and a negative
176749af7cdSPeter Pan  *	    error code otherwise. When true is returned, the core assumes
177749af7cdSPeter Pan  *	    that properties of the NAND chip (spinand->base.memorg and
178749af7cdSPeter Pan  *	    spinand->base.eccreq) have been filled
179749af7cdSPeter Pan  * @init: initialize a SPI NAND device
180749af7cdSPeter Pan  * @cleanup: cleanup a SPI NAND device
181749af7cdSPeter Pan  *
182749af7cdSPeter Pan  * Each SPI NAND manufacturer driver should implement this interface so that
183749af7cdSPeter Pan  * NAND chips coming from this vendor can be detected and initialized properly.
184749af7cdSPeter Pan  */
185749af7cdSPeter Pan struct spinand_manufacturer_ops {
186749af7cdSPeter Pan 	int (*detect)(struct spinand_device *spinand);
187749af7cdSPeter Pan 	int (*init)(struct spinand_device *spinand);
188749af7cdSPeter Pan 	void (*cleanup)(struct spinand_device *spinand);
189749af7cdSPeter Pan };
190749af7cdSPeter Pan 
191749af7cdSPeter Pan /**
192749af7cdSPeter Pan  * struct spinand_manufacturer - SPI NAND manufacturer instance
193749af7cdSPeter Pan  * @id: manufacturer ID
194749af7cdSPeter Pan  * @name: manufacturer name
195749af7cdSPeter Pan  * @ops: manufacturer operations
196749af7cdSPeter Pan  */
197749af7cdSPeter Pan struct spinand_manufacturer {
198749af7cdSPeter Pan 	u8 id;
199749af7cdSPeter Pan 	char *name;
200749af7cdSPeter Pan 	const struct spinand_manufacturer_ops *ops;
201749af7cdSPeter Pan };
202749af7cdSPeter Pan 
203ed13557fSPeter Pan /* SPI NAND manufacturers */
2046eb4b036SStefan Roese extern const struct spinand_manufacturer gigadevice_spinand_manufacturer;
20580c0c832SBoris Brezillon extern const struct spinand_manufacturer macronix_spinand_manufacturer;
206ed13557fSPeter Pan extern const struct spinand_manufacturer micron_spinand_manufacturer;
207e0242cafSRobert Marko extern const struct spinand_manufacturer toshiba_spinand_manufacturer;
208b98ac5e2SFrieder Schrempf extern const struct spinand_manufacturer winbond_spinand_manufacturer;
209c219aedbSJon Lin extern const struct spinand_manufacturer dosilicon_spinand_manufacturer;
21052b00601SJon Lin extern const struct spinand_manufacturer esmt_spinand_manufacturer;
211*fc656fc3SJon Lin extern const struct spinand_manufacturer xtx_spinand_manufacturer;
212ed13557fSPeter Pan 
213749af7cdSPeter Pan /**
214749af7cdSPeter Pan  * struct spinand_op_variants - SPI NAND operation variants
215749af7cdSPeter Pan  * @ops: the list of variants for a given operation
216749af7cdSPeter Pan  * @nops: the number of variants
217749af7cdSPeter Pan  *
218749af7cdSPeter Pan  * Some operations like read-from-cache/write-to-cache have several variants
219749af7cdSPeter Pan  * depending on the number of IO lines you use to transfer data or address
220749af7cdSPeter Pan  * cycles. This structure is a way to describe the different variants supported
221749af7cdSPeter Pan  * by a chip and let the core pick the best one based on the SPI mem controller
222749af7cdSPeter Pan  * capabilities.
223749af7cdSPeter Pan  */
224749af7cdSPeter Pan struct spinand_op_variants {
225749af7cdSPeter Pan 	const struct spi_mem_op *ops;
226749af7cdSPeter Pan 	unsigned int nops;
227749af7cdSPeter Pan };
228749af7cdSPeter Pan 
229749af7cdSPeter Pan #define SPINAND_OP_VARIANTS(name, ...)					\
230749af7cdSPeter Pan 	const struct spinand_op_variants name = {			\
231749af7cdSPeter Pan 		.ops = (struct spi_mem_op[]) { __VA_ARGS__ },		\
232749af7cdSPeter Pan 		.nops = sizeof((struct spi_mem_op[]){ __VA_ARGS__ }) /	\
233749af7cdSPeter Pan 			sizeof(struct spi_mem_op),			\
234749af7cdSPeter Pan 	}
235749af7cdSPeter Pan 
236749af7cdSPeter Pan /**
237749af7cdSPeter Pan  * spinand_ecc_info - description of the on-die ECC implemented by a SPI NAND
238749af7cdSPeter Pan  *		      chip
239749af7cdSPeter Pan  * @get_status: get the ECC status. Should return a positive number encoding
240749af7cdSPeter Pan  *		the number of corrected bitflips if correction was possible or
241749af7cdSPeter Pan  *		-EBADMSG if there are uncorrectable errors. I can also return
242749af7cdSPeter Pan  *		other negative error codes if the error is not caused by
243749af7cdSPeter Pan  *		uncorrectable bitflips
244749af7cdSPeter Pan  * @ooblayout: the OOB layout used by the on-die ECC implementation
245749af7cdSPeter Pan  */
246749af7cdSPeter Pan struct spinand_ecc_info {
247749af7cdSPeter Pan 	int (*get_status)(struct spinand_device *spinand, u8 status);
248749af7cdSPeter Pan 	const struct mtd_ooblayout_ops *ooblayout;
249749af7cdSPeter Pan };
250749af7cdSPeter Pan 
251749af7cdSPeter Pan #define SPINAND_HAS_QE_BIT		BIT(0)
252749af7cdSPeter Pan 
253749af7cdSPeter Pan /**
254749af7cdSPeter Pan  * struct spinand_info - Structure used to describe SPI NAND chips
255749af7cdSPeter Pan  * @model: model name
256749af7cdSPeter Pan  * @devid: device ID
257749af7cdSPeter Pan  * @flags: OR-ing of the SPINAND_XXX flags
258749af7cdSPeter Pan  * @memorg: memory organization
259749af7cdSPeter Pan  * @eccreq: ECC requirements
260749af7cdSPeter Pan  * @eccinfo: on-die ECC info
261749af7cdSPeter Pan  * @op_variants: operations variants
262749af7cdSPeter Pan  * @op_variants.read_cache: variants of the read-cache operation
263749af7cdSPeter Pan  * @op_variants.write_cache: variants of the write-cache operation
264749af7cdSPeter Pan  * @op_variants.update_cache: variants of the update-cache operation
265749af7cdSPeter Pan  * @select_target: function used to select a target/die. Required only for
266749af7cdSPeter Pan  *		   multi-die chips
267749af7cdSPeter Pan  *
268749af7cdSPeter Pan  * Each SPI NAND manufacturer driver should have a spinand_info table
269749af7cdSPeter Pan  * describing all the chips supported by the driver.
270749af7cdSPeter Pan  */
271749af7cdSPeter Pan struct spinand_info {
272749af7cdSPeter Pan 	const char *model;
273749af7cdSPeter Pan 	u8 devid;
274749af7cdSPeter Pan 	u32 flags;
275749af7cdSPeter Pan 	struct nand_memory_organization memorg;
276749af7cdSPeter Pan 	struct nand_ecc_req eccreq;
277749af7cdSPeter Pan 	struct spinand_ecc_info eccinfo;
278749af7cdSPeter Pan 	struct {
279749af7cdSPeter Pan 		const struct spinand_op_variants *read_cache;
280749af7cdSPeter Pan 		const struct spinand_op_variants *write_cache;
281749af7cdSPeter Pan 		const struct spinand_op_variants *update_cache;
282749af7cdSPeter Pan 	} op_variants;
283749af7cdSPeter Pan 	int (*select_target)(struct spinand_device *spinand,
284749af7cdSPeter Pan 			     unsigned int target);
285749af7cdSPeter Pan };
286749af7cdSPeter Pan 
287749af7cdSPeter Pan #define SPINAND_INFO_OP_VARIANTS(__read, __write, __update)		\
288749af7cdSPeter Pan 	{								\
289749af7cdSPeter Pan 		.read_cache = __read,					\
290749af7cdSPeter Pan 		.write_cache = __write,					\
291749af7cdSPeter Pan 		.update_cache = __update,				\
292749af7cdSPeter Pan 	}
293749af7cdSPeter Pan 
294749af7cdSPeter Pan #define SPINAND_ECCINFO(__ooblayout, __get_status)			\
295749af7cdSPeter Pan 	.eccinfo = {							\
296749af7cdSPeter Pan 		.ooblayout = __ooblayout,				\
297749af7cdSPeter Pan 		.get_status = __get_status,				\
298749af7cdSPeter Pan 	}
299749af7cdSPeter Pan 
300749af7cdSPeter Pan #define SPINAND_SELECT_TARGET(__func)					\
301749af7cdSPeter Pan 	.select_target = __func,
302749af7cdSPeter Pan 
303749af7cdSPeter Pan #define SPINAND_INFO(__model, __id, __memorg, __eccreq, __op_variants,	\
304749af7cdSPeter Pan 		     __flags, ...)					\
305749af7cdSPeter Pan 	{								\
306749af7cdSPeter Pan 		.model = __model,					\
307749af7cdSPeter Pan 		.devid = __id,						\
308749af7cdSPeter Pan 		.memorg = __memorg,					\
309749af7cdSPeter Pan 		.eccreq = __eccreq,					\
310749af7cdSPeter Pan 		.op_variants = __op_variants,				\
311749af7cdSPeter Pan 		.flags = __flags,					\
312749af7cdSPeter Pan 		__VA_ARGS__						\
313749af7cdSPeter Pan 	}
314749af7cdSPeter Pan 
315749af7cdSPeter Pan /**
316749af7cdSPeter Pan  * struct spinand_device - SPI NAND device instance
317749af7cdSPeter Pan  * @base: NAND device instance
318749af7cdSPeter Pan  * @slave: pointer to the SPI slave object
319749af7cdSPeter Pan  * @lock: lock used to serialize accesses to the NAND
320749af7cdSPeter Pan  * @id: NAND ID as returned by READ_ID
321749af7cdSPeter Pan  * @flags: NAND flags
322749af7cdSPeter Pan  * @op_templates: various SPI mem op templates
323749af7cdSPeter Pan  * @op_templates.read_cache: read cache op template
324749af7cdSPeter Pan  * @op_templates.write_cache: write cache op template
325749af7cdSPeter Pan  * @op_templates.update_cache: update cache op template
326749af7cdSPeter Pan  * @select_target: select a specific target/die. Usually called before sending
327749af7cdSPeter Pan  *		   a command addressing a page or an eraseblock embedded in
328749af7cdSPeter Pan  *		   this die. Only required if your chip exposes several dies
329749af7cdSPeter Pan  * @cur_target: currently selected target/die
330749af7cdSPeter Pan  * @eccinfo: on-die ECC information
331749af7cdSPeter Pan  * @cfg_cache: config register cache. One entry per die
332749af7cdSPeter Pan  * @databuf: bounce buffer for data
333749af7cdSPeter Pan  * @oobbuf: bounce buffer for OOB data
334749af7cdSPeter Pan  * @scratchbuf: buffer used for everything but page accesses. This is needed
335749af7cdSPeter Pan  *		because the spi-mem interface explicitly requests that buffers
336749af7cdSPeter Pan  *		passed in spi_mem_op be DMA-able, so we can't based the bufs on
337749af7cdSPeter Pan  *		the stack
338749af7cdSPeter Pan  * @manufacturer: SPI NAND manufacturer information
339749af7cdSPeter Pan  * @priv: manufacturer private data
340749af7cdSPeter Pan  */
341749af7cdSPeter Pan struct spinand_device {
342749af7cdSPeter Pan 	struct nand_device base;
343749af7cdSPeter Pan #ifndef __UBOOT__
344749af7cdSPeter Pan 	struct spi_mem *spimem;
345749af7cdSPeter Pan 	struct mutex lock;
346749af7cdSPeter Pan #else
347749af7cdSPeter Pan 	struct spi_slave *slave;
348749af7cdSPeter Pan #endif
349749af7cdSPeter Pan 	struct spinand_id id;
350749af7cdSPeter Pan 	u32 flags;
351749af7cdSPeter Pan 
352749af7cdSPeter Pan 	struct {
353749af7cdSPeter Pan 		const struct spi_mem_op *read_cache;
354749af7cdSPeter Pan 		const struct spi_mem_op *write_cache;
355749af7cdSPeter Pan 		const struct spi_mem_op *update_cache;
356749af7cdSPeter Pan 	} op_templates;
357749af7cdSPeter Pan 
358749af7cdSPeter Pan 	int (*select_target)(struct spinand_device *spinand,
359749af7cdSPeter Pan 			     unsigned int target);
360749af7cdSPeter Pan 	unsigned int cur_target;
361749af7cdSPeter Pan 
362749af7cdSPeter Pan 	struct spinand_ecc_info eccinfo;
363749af7cdSPeter Pan 
364749af7cdSPeter Pan 	u8 *cfg_cache;
365749af7cdSPeter Pan 	u8 *databuf;
366749af7cdSPeter Pan 	u8 *oobbuf;
367749af7cdSPeter Pan 	u8 *scratchbuf;
368749af7cdSPeter Pan 	const struct spinand_manufacturer *manufacturer;
369749af7cdSPeter Pan 	void *priv;
370749af7cdSPeter Pan };
371749af7cdSPeter Pan 
372749af7cdSPeter Pan /**
373749af7cdSPeter Pan  * mtd_to_spinand() - Get the SPI NAND device attached to an MTD instance
374749af7cdSPeter Pan  * @mtd: MTD instance
375749af7cdSPeter Pan  *
376749af7cdSPeter Pan  * Return: the SPI NAND device attached to @mtd.
377749af7cdSPeter Pan  */
378749af7cdSPeter Pan static inline struct spinand_device *mtd_to_spinand(struct mtd_info *mtd)
379749af7cdSPeter Pan {
380749af7cdSPeter Pan 	return container_of(mtd_to_nanddev(mtd), struct spinand_device, base);
381749af7cdSPeter Pan }
382749af7cdSPeter Pan 
383749af7cdSPeter Pan /**
384749af7cdSPeter Pan  * spinand_to_mtd() - Get the MTD device embedded in a SPI NAND device
385749af7cdSPeter Pan  * @spinand: SPI NAND device
386749af7cdSPeter Pan  *
387749af7cdSPeter Pan  * Return: the MTD device embedded in @spinand.
388749af7cdSPeter Pan  */
389749af7cdSPeter Pan static inline struct mtd_info *spinand_to_mtd(struct spinand_device *spinand)
390749af7cdSPeter Pan {
391749af7cdSPeter Pan 	return nanddev_to_mtd(&spinand->base);
392749af7cdSPeter Pan }
393749af7cdSPeter Pan 
394749af7cdSPeter Pan /**
395749af7cdSPeter Pan  * nand_to_spinand() - Get the SPI NAND device embedding an NAND object
396749af7cdSPeter Pan  * @nand: NAND object
397749af7cdSPeter Pan  *
398749af7cdSPeter Pan  * Return: the SPI NAND device embedding @nand.
399749af7cdSPeter Pan  */
400749af7cdSPeter Pan static inline struct spinand_device *nand_to_spinand(struct nand_device *nand)
401749af7cdSPeter Pan {
402749af7cdSPeter Pan 	return container_of(nand, struct spinand_device, base);
403749af7cdSPeter Pan }
404749af7cdSPeter Pan 
405749af7cdSPeter Pan /**
406749af7cdSPeter Pan  * spinand_to_nand() - Get the NAND device embedded in a SPI NAND object
407749af7cdSPeter Pan  * @spinand: SPI NAND device
408749af7cdSPeter Pan  *
409749af7cdSPeter Pan  * Return: the NAND device embedded in @spinand.
410749af7cdSPeter Pan  */
411749af7cdSPeter Pan static inline struct nand_device *
412749af7cdSPeter Pan spinand_to_nand(struct spinand_device *spinand)
413749af7cdSPeter Pan {
414749af7cdSPeter Pan 	return &spinand->base;
415749af7cdSPeter Pan }
416749af7cdSPeter Pan 
417749af7cdSPeter Pan /**
418749af7cdSPeter Pan  * spinand_set_of_node - Attach a DT node to a SPI NAND device
419749af7cdSPeter Pan  * @spinand: SPI NAND device
420749af7cdSPeter Pan  * @np: DT node
421749af7cdSPeter Pan  *
422749af7cdSPeter Pan  * Attach a DT node to a SPI NAND device.
423749af7cdSPeter Pan  */
424749af7cdSPeter Pan static inline void spinand_set_of_node(struct spinand_device *spinand,
425749af7cdSPeter Pan 				       const struct device_node *np)
426749af7cdSPeter Pan {
427749af7cdSPeter Pan 	nanddev_set_of_node(&spinand->base, np);
428749af7cdSPeter Pan }
429749af7cdSPeter Pan 
430749af7cdSPeter Pan int spinand_match_and_init(struct spinand_device *dev,
431749af7cdSPeter Pan 			   const struct spinand_info *table,
432749af7cdSPeter Pan 			   unsigned int table_size, u8 devid);
433749af7cdSPeter Pan 
434749af7cdSPeter Pan int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val);
435749af7cdSPeter Pan int spinand_select_target(struct spinand_device *spinand, unsigned int target);
436749af7cdSPeter Pan 
437749af7cdSPeter Pan #endif /* __LINUX_MTD_SPINAND_H */
438