xref: /OK3568_Linux_fs/u-boot/drivers/mtd/nand/spi/winbond.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2017 exceet electronics GmbH
4  *
5  * Authors:
6  *	Frieder Schrempf <frieder.schrempf@exceet.de>
7  *	Boris Brezillon <boris.brezillon@bootlin.com>
8  */
9 
10 #ifndef __UBOOT__
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #endif
14 #include <linux/mtd/spinand.h>
15 
16 #define SPINAND_MFR_WINBOND		0xEF
17 
18 #define WINBOND_CFG_BUF_READ		BIT(3)
19 
20 static SPINAND_OP_VARIANTS(read_cache_variants,
21 		SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
22 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
23 		SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
24 		SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
25 		SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
26 		SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
27 
28 static SPINAND_OP_VARIANTS(write_cache_variants,
29 		SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
30 		SPINAND_PROG_LOAD(true, 0, NULL, 0));
31 
32 static SPINAND_OP_VARIANTS(update_cache_variants,
33 		SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
34 		SPINAND_PROG_LOAD(false, 0, NULL, 0));
35 
w25m02gv_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)36 static int w25m02gv_ooblayout_ecc(struct mtd_info *mtd, int section,
37 				  struct mtd_oob_region *region)
38 {
39 	if (section > 3)
40 		return -ERANGE;
41 
42 	region->offset = (16 * section) + 8;
43 	region->length = 8;
44 
45 	return 0;
46 }
47 
w25m02gv_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)48 static int w25m02gv_ooblayout_free(struct mtd_info *mtd, int section,
49 				   struct mtd_oob_region *region)
50 {
51 	if (section > 3)
52 		return -ERANGE;
53 
54 	region->offset = (16 * section) + 2;
55 	region->length = 6;
56 
57 	return 0;
58 }
59 
60 static const struct mtd_ooblayout_ops w25m02gv_ooblayout = {
61 	.ecc = w25m02gv_ooblayout_ecc,
62 	.rfree = w25m02gv_ooblayout_free,
63 };
64 
w25m02gv_select_target(struct spinand_device * spinand,unsigned int target)65 static int w25m02gv_select_target(struct spinand_device *spinand,
66 				  unsigned int target)
67 {
68 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0xc2, 1),
69 					  SPI_MEM_OP_NO_ADDR,
70 					  SPI_MEM_OP_NO_DUMMY,
71 					  SPI_MEM_OP_DATA_OUT(1,
72 							spinand->scratchbuf,
73 							1));
74 
75 	*spinand->scratchbuf = target;
76 	return spi_mem_exec_op(spinand->slave, &op);
77 }
78 
w25n02kv_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)79 static int w25n02kv_ooblayout_ecc(struct mtd_info *mtd, int section,
80 				  struct mtd_oob_region *region)
81 {
82 	if (section)
83 		return -ERANGE;
84 
85 	region->offset = 64;
86 	region->length = 64;
87 
88 	return 0;
89 }
90 
w25n02kv_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)91 static int w25n02kv_ooblayout_free(struct mtd_info *mtd, int section,
92 				   struct mtd_oob_region *region)
93 {
94 	if (section)
95 		return -ERANGE;
96 
97 	/* Reserve 2 bytes for the BBM. */
98 	region->offset = 2;
99 	region->length = 62;
100 
101 	return 0;
102 }
103 
104 static const struct mtd_ooblayout_ops w25n02kv_ooblayout = {
105 	.ecc = w25n02kv_ooblayout_ecc,
106 	.rfree = w25n02kv_ooblayout_free,
107 };
108 
w25n02kv_ecc_get_status(struct spinand_device * spinand,u8 status)109 static int w25n02kv_ecc_get_status(struct spinand_device *spinand,
110 				   u8 status)
111 {
112 	struct nand_device *nand = spinand_to_nand(spinand);
113 
114 	switch (status & STATUS_ECC_MASK) {
115 	case STATUS_ECC_NO_BITFLIPS:
116 		return 0;
117 
118 	case STATUS_ECC_UNCOR_ERROR:
119 		return -EBADMSG;
120 
121 	case STATUS_ECC_HAS_BITFLIPS:
122 		return 1;
123 
124 	default:
125 		return nand->eccreq.strength;
126 	}
127 
128 	return -EINVAL;
129 }
130 
131 /* Another set for the same id[2] devices in one series */
132 static const struct spinand_info winbond_spinand_table[] = {
133 	SPINAND_INFO("W25M02GV",
134 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xAB),
135 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 2),
136 		     NAND_ECCREQ(1, 512),
137 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
138 					      &write_cache_variants,
139 					      &update_cache_variants),
140 		     0,
141 		     SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
142 		     SPINAND_SELECT_TARGET(w25m02gv_select_target)),
143 	SPINAND_INFO("W25N512GV",
144 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xAA, 0x20),
145 		     NAND_MEMORG(1, 2048, 64, 64, 512, 1, 1, 1),
146 		     NAND_ECCREQ(1, 512),
147 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
148 					      &write_cache_variants,
149 					      &update_cache_variants),
150 		     0,
151 		     SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
152 		     SPINAND_SELECT_TARGET(w25m02gv_select_target)),
153 	SPINAND_INFO("W25N01GV",
154 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xAA, 0x21),
155 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
156 		     NAND_ECCREQ(1, 512),
157 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
158 					      &write_cache_variants,
159 					      &update_cache_variants),
160 		     0,
161 		     SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
162 		     SPINAND_SELECT_TARGET(w25m02gv_select_target)),
163 	SPINAND_INFO("W25N02KV",
164 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xAA, 0x22),
165 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
166 		     NAND_ECCREQ(8, 512),
167 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
168 					      &write_cache_variants,
169 					      &update_cache_variants),
170 		     0,
171 		     SPINAND_ECCINFO(&w25n02kv_ooblayout,
172 				     w25n02kv_ecc_get_status)),
173 	SPINAND_INFO("W25N04KV",
174 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xAA, 0x23),
175 		     NAND_MEMORG(1, 2048, 128, 64, 4096, 1, 1, 1),
176 		     NAND_ECCREQ(8, 512),
177 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
178 					      &write_cache_variants,
179 					      &update_cache_variants),
180 		     0,
181 		     SPINAND_ECCINFO(&w25n02kv_ooblayout,
182 				     w25n02kv_ecc_get_status)),
183 	SPINAND_INFO("W25N01GW",
184 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xBA, 0x21),
185 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1),
186 		     NAND_ECCREQ(1, 512),
187 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
188 					      &write_cache_variants,
189 					      &update_cache_variants),
190 		     0,
191 		     SPINAND_ECCINFO(&w25m02gv_ooblayout, NULL),
192 		     SPINAND_SELECT_TARGET(w25m02gv_select_target)),
193 	SPINAND_INFO("W25N02KW",
194 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xBA, 0x22),
195 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
196 		     NAND_ECCREQ(8, 512),
197 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
198 					      &write_cache_variants,
199 					      &update_cache_variants),
200 		     0,
201 		     SPINAND_ECCINFO(&w25n02kv_ooblayout,
202 				     w25n02kv_ecc_get_status)),
203 	SPINAND_INFO("W25N01KV",
204 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xAE, 0x21),
205 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
206 		     NAND_ECCREQ(4, 512),
207 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
208 					      &write_cache_variants,
209 					      &update_cache_variants),
210 		     0,
211 		     SPINAND_ECCINFO(&w25n02kv_ooblayout,
212 				     w25n02kv_ecc_get_status)),
213 };
214 
winbond_spinand_init(struct spinand_device * spinand)215 static int winbond_spinand_init(struct spinand_device *spinand)
216 {
217 	struct nand_device *nand = spinand_to_nand(spinand);
218 	unsigned int i;
219 
220 	/*
221 	 * Make sure all dies are in buffer read mode and not continuous read
222 	 * mode.
223 	 */
224 	for (i = 0; i < nand->memorg.ntargets; i++) {
225 		spinand_select_target(spinand, i);
226 		spinand_upd_cfg(spinand, WINBOND_CFG_BUF_READ,
227 				WINBOND_CFG_BUF_READ);
228 	}
229 
230 	return 0;
231 }
232 
233 static const struct spinand_manufacturer_ops winbond_spinand_manuf_ops = {
234 	.init = winbond_spinand_init,
235 };
236 
237 const struct spinand_manufacturer winbond_spinand_manufacturer = {
238 	.id = SPINAND_MFR_WINBOND,
239 	.name = "Winbond",
240 	.chips = winbond_spinand_table,
241 	.nchips = ARRAY_SIZE(winbond_spinand_table),
242 	.ops = &winbond_spinand_manuf_ops,
243 };
244