xref: /rk3399_rockchip-uboot/drivers/rkflash/sfc_nor.c (revision 4ba12f8a933529f74d790939ef287a54475742d2)
1 /*
2  * Copyright (c) 2018 Fuzhou Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:	GPL-2.0
5  */
6 #include <linux/compat.h>
7 #include <linux/delay.h>
8 #include <linux/kernel.h>
9 #include <linux/string.h>
10 
11 #include "rkflash_debug.h"
12 #include "sfc_nor.h"
13 
14 static struct flash_info spi_flash_tbl[] = {
15 	/* GD25Q32B */
16 	{ 0xc84016, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 13, 9, 0 },
17 	/* GD25Q64B/C/E */
18 	{ 0xc84017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 9, 0 },
19 	/* GD25Q127C and GD25Q128C/E */
20 	{ 0xc84018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
21 	/* GD25Q256B/C/D/E */
22 	{ 0xc84019, 128, 8, 0x13, 0x12, 0x6C, 0x3E, 0x21, 0xDC, 0x1C, 16, 6, 0 },
23 	/* GD25Q512MC */
24 	{ 0xc84020, 128, 8, 0x13, 0x12, 0x6C, 0x3E, 0x21, 0xDC, 0x1C, 17, 6, 0 },
25 	/* GD25LQ64C */
26 	{ 0xc86017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 14, 9, 0 },
27 	/* GD25LQ32E */
28 	{ 0xc86016, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 13, 9, 0 },
29 	/* GD25B512MEYIG */
30 	{ 0xc8471A, 128, 8, 0x13, 0x12, 0x6C, 0x34, 0x21, 0xDC, 0x1C, 17, 0, 0 },
31 	/* GD25LQ255E and GD25LQ256C */
32 	{ 0xc86019, 128, 8, 0x13, 0x12, 0x6C, 0x34, 0x21, 0xDC, 0x1D, 16, 9, 0 },
33 	/* GD25LB512MEYIG */
34 	{ 0xc8671A, 128, 8, 0x13, 0x12, 0x6C, 0x34, 0x21, 0xDC, 0x1C, 17, 0, 0 },
35 
36 	/* W25Q32JV */
37 	{ 0xef4016, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 13, 9, 0 },
38 	/* W25Q64JVSSIQ */
39 	{ 0xef4017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 9, 0 },
40 	/* W25Q128FV and W25Q128JV*/
41 	{ 0xef4018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
42 	/* W25Q256F/J */
43 	{ 0xef4019, 128, 8, 0x13, 0x02, 0x6C, 0x32, 0x20, 0xD8, 0x3C, 16, 9, 0 },
44 	/* W25Q32JW */
45 	{ 0xef6016, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 13, 9, 0 },
46 	/* W25Q64FWSSIG */
47 	{ 0xef6017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 9, 0 },
48 	/* W25Q128JWSQ */
49 	{ 0xef6018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
50 	/* W25Q256JWEQ*/
51 	{ 0xef6019, 128, 8, 0x13, 0x02, 0x6C, 0x32, 0x20, 0xD8, 0x3C, 16, 9, 0 },
52 	/* W25Q128JVSIM */
53 	{ 0xef7018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
54 	/* W25Q256JVEM */
55 	{ 0xef7019, 128, 8, 0x13, 0x12, 0x6C, 0x34, 0x21, 0xDC, 0x3C, 16, 9, 0 },
56 
57 	/* MX25L3233FM2I-08G */
58 	{ 0xc22016, 128, 8, 0x03, 0x02, 0x6B, 0x38, 0x20, 0xD8, 0x0E, 13, 6, 0 },
59 	/* MX25L6433F */
60 	{ 0xc22017, 128, 8, 0x03, 0x02, 0x6B, 0x38, 0x20, 0xD8, 0x0E, 14, 6, 0 },
61 	/* MX25L12835E/F MX25L12833FMI-10G */
62 	{ 0xc22018, 128, 8, 0x03, 0x02, 0x6B, 0x38, 0x20, 0xD8, 0x0E, 15, 6, 0 },
63 	/* MX25L25635E/F MX25L25645G MX25L25645GMI-08G */
64 	{ 0xc22019, 128, 8, 0x13, 0x12, 0x6C, 0x3E, 0x21, 0xDC, 0x1E, 16, 6, 0 },
65 	/* MX25L51245GMI */
66 	{ 0xc2201a, 128, 8, 0x13, 0x12, 0x6C, 0x3E, 0x21, 0xDC, 0x1E, 17, 6, 0 },
67 	/* MX25U51245G */
68 	{ 0xc2253a, 128, 8, 0x0C, 0x12, 0x6C, 0x3E, 0x21, 0xDC, 0x1E, 17, 6, 0 },
69 	/* MX25U3232F */
70 	{ 0xc22536, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0E, 13, 6, 0 },
71 	/* MX25U6432F */
72 	{ 0xc22537, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0E, 14, 6, 0 },
73 	/* MX25U12832F */
74 	{ 0xc22538, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0E, 15, 6, 0 },
75 	/* MX25U25645GZ4I-00 */
76 	{ 0xc22539, 128, 8, 0x13, 0x12, 0x6C, 0x3E, 0x21, 0xDC, 0x1E, 16, 6, 0 },
77 
78 	/* XM25QH32C */
79 	{ 0x204016, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 13, 9, 0 },
80 	/* XM25QH64C */
81 	{ 0x204017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 9, 0 },
82 	/* XM25QH128C */
83 	{ 0x204018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x05, 15, 9, 0 },
84 	/* XM25QH256C */
85 	{ 0x204019, 128, 8, 0x13, 0x12, 0x6C, 0x34, 0x21, 0xDC, 0x1C, 16, 9, 0 },
86 	/* XM25QH64B */
87 	{ 0x206017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 14, 6, 0 },
88 	/* XM25QH128B */
89 	{ 0x206018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 15, 6, 0 },
90 	/* XM25QH(QU)256B */
91 	{ 0x206019, 128, 8, 0x13, 0x12, 0x6C, 0x3E, 0x21, 0xDC, 0x1D, 16, 6, 0 },
92 	/* XM25QH64A */
93 	{ 0x207017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 0, 0 },
94 	/* XM25QU128C */
95 	{ 0x204118, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
96 	/* XM25QU64C */
97 	{ 0x204117, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 9, 0 },
98 
99 	/* XT25F128A XM25QH128A */
100 	{ 0x207018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 0, 0 },
101 	/* XT25F64BSSIGU-5 XT25F64F */
102 	{ 0x0b4017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 14, 9, 0 },
103 	/* XT25F128BSSIGU */
104 	{ 0x0b4018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 15, 9, 0 },
105 	/* XT25F256BSFIGU */
106 	{ 0x0b4019, 128, 8, 0x13, 0x12, 0x6C, 0x34, 0x21, 0xDC, 0x1C, 16, 9, 0 },
107 	/* XT25F32BS */
108 	{ 0x0b4016, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 13, 9, 0 },
109 	/* XT25F16BS */
110 	{ 0x0b4015, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 12, 9, 0 },
111 
112 	/* EN25QH64A */
113 	{ 0x1c7017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 0, 0 },
114 	/* EN25QH128A */
115 	{ 0x1c7018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 0, 0 },
116 	/* EN25QH32B */
117 	{ 0x1c7016, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 13, 0, 0 },
118 	/* EN25S32A */
119 	{ 0x1c3816, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 13, 0, 0 },
120 	/* EN25S64A */
121 	{ 0x1c3817, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 0, 0 },
122 	/* EN25QH256A */
123 	{ 0x1c7019, 128, 8, 0x13, 0x12, 0x6C, 0x34, 0x21, 0xDC, 0x3C, 16, 0, 0 },
124 
125 	/* P25Q64H */
126 	{ 0x856017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 9, 0 },
127 	/* P25Q128H */
128 	{ 0x856018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
129 	/* P25Q16H-SUH-IT */
130 	{ 0x856015, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 12, 9, 0 },
131 	/* P25Q32SL P25Q32SH-SSH-IT */
132 	{ 0x856016, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 13, 9, 0 },
133 	/* PY25Q128H */
134 	{ 0x852018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
135 
136 	/* ZB25VQ64 */
137 	{ 0x5e4017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 9, 0 },
138 	/* ZB25VQ128 */
139 	{ 0x5e4018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
140 	/* ZB25LQ128 */
141 	{ 0x5e5018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
142 
143 	/* BH25Q128AS */
144 	{ 0x684018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
145 	/* BH25Q64BS */
146 	{ 0x684017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 9, 0 },
147 
148 	/* FM25Q128A */
149 	{ 0xA14018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
150 	/* FM25Q64-SOB-T-G */
151 	{ 0xA14017, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 14, 9, 0 },
152 
153 	/* FM25Q64A */
154 	{ 0xf83217, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 14, 9, 0 },
155 	/* FM25M4AA */
156 	{ 0xf84218, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 15, 9, 0 },
157 	/* FM25M64C */
158 	{ 0xf84317, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0D, 14, 9, 0 },
159 
160 	/* DS25M4AB-1AIB4 */
161 	{ 0xe54218, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
162 
163 	/* GM25Q128A */
164 	{ 0x1c4018, 128, 8, 0x03, 0x02, 0x6B, 0x32, 0x20, 0xD8, 0x0C, 15, 9, 0 },
165 
166 	/* IS25LP512M */
167 	{ 0x9D601A, 128, 8, 0x13, 0x12, 0x6C, 0x34, 0x21, 0xDC, 0x3C, 17, 6, 0 },
168 	/* IS25WP512M */
169 	{ 0x9D701A, 128, 8, 0x13, 0x12, 0x6C, 0x34, 0x21, 0xDC, 0x3C, 17, 6, 0 },
170 };
171 
172 static int snor_write_en(void)
173 {
174 	int ret;
175 	struct rk_sfc_op op;
176 
177 	op.sfcmd.d32 = 0;
178 	op.sfcmd.b.cmd = CMD_WRITE_EN;
179 
180 	op.sfctrl.d32 = 0;
181 
182 	ret = sfc_request(&op, 0, NULL, 0);
183 
184 	return ret;
185 }
186 
187 int snor_reset_device(void)
188 {
189 	struct rk_sfc_op op;
190 
191 	op.sfcmd.d32 = 0;
192 	op.sfcmd.b.cmd = CMD_ENABLE_RESER;
193 
194 	op.sfctrl.d32 = 0;
195 	sfc_request(&op, 0, NULL, 0);
196 
197 	op.sfcmd.d32 = 0;
198 	op.sfcmd.b.cmd = CMD_RESET_DEVICE;
199 
200 	op.sfctrl.d32 = 0;
201 	sfc_request(&op, 0, NULL, 0);
202 	/* tRST=30us , delay 1ms here */
203 	sfc_delay(1000);
204 
205 	return SFC_OK;
206 }
207 
208 static int snor_enter_4byte_mode(void)
209 {
210 	int ret;
211 	struct rk_sfc_op op;
212 
213 	op.sfcmd.d32 = 0;
214 	op.sfcmd.b.cmd = CMD_ENTER_4BYTE_MODE;
215 
216 	op.sfctrl.d32 = 0;
217 
218 	ret = sfc_request(&op, 0, NULL, 0);
219 	return ret;
220 }
221 
222 static int snor_read_status(u32 reg_index, u8 *status)
223 {
224 	int ret;
225 	struct rk_sfc_op op;
226 	u8 read_stat_cmd[] = {CMD_READ_STATUS,
227 				CMD_READ_STATUS2, CMD_READ_STATUS3};
228 	op.sfcmd.d32 = 0;
229 	op.sfcmd.b.cmd = read_stat_cmd[reg_index];
230 
231 	op.sfctrl.d32 = 0;
232 	ret = sfc_request(&op, 0, status, 1);
233 
234 	return ret;
235 }
236 
237 static int snor_wait_busy(int timeout)
238 {
239 	int ret;
240 	struct rk_sfc_op op;
241 	int i;
242 	u32 status;
243 
244 	op.sfcmd.d32 = 0;
245 	op.sfcmd.b.cmd = CMD_READ_STATUS;
246 
247 	op.sfctrl.d32 = 0;
248 
249 	for (i = 0; i < timeout; i++) {
250 		ret = sfc_request(&op, 0, &status, 1);
251 		if (ret != SFC_OK)
252 			return ret;
253 
254 		if ((status & 0x01) == 0)
255 			return SFC_OK;
256 
257 		sfc_delay(1);
258 	}
259 	rkflash_print_error("%s  error %x\n", __func__, timeout);
260 
261 	return SFC_BUSY_TIMEOUT;
262 }
263 
264 static int snor_write_status2(u32 reg_index, u8 status)
265 {
266 	int ret;
267 	struct rk_sfc_op op;
268 	u8 status2[2];
269 
270 	status2[reg_index] = status;
271 	if (reg_index == 0)
272 		ret = snor_read_status(2, &status2[1]);
273 	else
274 		ret = snor_read_status(0, &status2[0]);
275 	if (ret != SFC_OK)
276 		return ret;
277 
278 	snor_write_en();
279 
280 	op.sfcmd.d32 = 0;
281 	op.sfcmd.b.cmd = CMD_WRITE_STATUS;
282 	op.sfcmd.b.rw = SFC_WRITE;
283 
284 	op.sfctrl.d32 = 0;
285 
286 	ret = sfc_request(&op, 0, &status2[0], 2);
287 	if (ret != SFC_OK)
288 		return ret;
289 
290 	ret = snor_wait_busy(10000);    /* 10ms */
291 
292 	return ret;
293 }
294 
295 static int snor_write_status1(u32 reg_index, u8 status)
296 {
297 	int ret;
298 	struct rk_sfc_op op;
299 	u8 status2[2];
300 	u8 read_index;
301 
302 	status2[reg_index] = status;
303 	read_index = (reg_index == 0) ? 1 : 0;
304 	ret = snor_read_status(read_index, &status2[read_index]);
305 	if (ret != SFC_OK)
306 		return ret;
307 
308 	snor_write_en();
309 
310 	op.sfcmd.d32 = 0;
311 	op.sfcmd.b.cmd = CMD_WRITE_STATUS;
312 	op.sfcmd.b.rw = SFC_WRITE;
313 
314 	op.sfctrl.d32 = 0;
315 
316 	ret = sfc_request(&op, 0, &status2[0], 2);
317 	if (ret != SFC_OK)
318 		return ret;
319 
320 	ret = snor_wait_busy(10000);    /* 10ms */
321 
322 	return ret;
323 }
324 
325 static int snor_write_status(u32 reg_index, u8 status)
326 {
327 	int ret;
328 	struct rk_sfc_op op;
329 	u8 write_stat_cmd[] = {CMD_WRITE_STATUS,
330 			       CMD_WRITE_STATUS2, CMD_WRITE_STATUS3};
331 	snor_write_en();
332 	op.sfcmd.d32 = 0;
333 	op.sfcmd.b.cmd = write_stat_cmd[reg_index];
334 	op.sfcmd.b.rw = SFC_WRITE;
335 
336 	op.sfctrl.d32 = 0;
337 
338 	ret = sfc_request(&op, 0, &status, 1);
339 	if (ret != SFC_OK)
340 		return ret;
341 
342 	ret = snor_wait_busy(10000);    /* 10ms */
343 
344 	return ret;
345 }
346 
347 int snor_erase(struct SFNOR_DEV *p_dev,
348 	       u32 addr,
349 	       enum NOR_ERASE_TYPE erase_type)
350 {
351 	int ret;
352 	struct rk_sfc_op op;
353 	int timeout[] = {400, 2000, 40000};   /* ms */
354 
355 	rkflash_print_dio("%s %x %x\n", __func__, addr, erase_type);
356 
357 	if (erase_type > ERASE_CHIP)
358 		return SFC_PARAM_ERR;
359 
360 	op.sfcmd.d32 = 0;
361 	if (erase_type == ERASE_BLOCK64K)
362 		op.sfcmd.b.cmd = p_dev->blk_erase_cmd;
363 	else if (erase_type == ERASE_SECTOR)
364 		op.sfcmd.b.cmd = p_dev->sec_erase_cmd;
365 	else
366 		op.sfcmd.b.cmd = CMD_CHIP_ERASE;
367 
368 	op.sfcmd.b.addrbits = (erase_type != ERASE_CHIP) ?
369 				SFC_ADDR_24BITS : SFC_ADDR_0BITS;
370 	if (p_dev->addr_mode == ADDR_MODE_4BYTE && erase_type != ERASE_CHIP)
371 		op.sfcmd.b.addrbits = SFC_ADDR_32BITS;
372 	op.sfcmd.b.rw = SFC_WRITE;
373 
374 	op.sfctrl.d32 = 0;
375 
376 	snor_write_en();
377 
378 	ret = sfc_request(&op, addr, NULL, 0);
379 	if (ret != SFC_OK)
380 		return ret;
381 
382 	ret = snor_wait_busy(timeout[erase_type] * 1000);
383 	return ret;
384 }
385 
386 int snor_prog_page(struct SFNOR_DEV *p_dev,
387 		   u32 addr,
388 		   void *p_data,
389 		   u32 size)
390 {
391 	int ret;
392 	struct rk_sfc_op op;
393 
394 	rkflash_print_dio("%s %x %x\n", __func__, addr, *(u32 *)(p_data));
395 
396 	op.sfcmd.d32 = 0;
397 	op.sfcmd.b.cmd = p_dev->prog_cmd;
398 	op.sfcmd.b.addrbits = SFC_ADDR_24BITS;
399 	op.sfcmd.b.rw = SFC_WRITE;
400 
401 	op.sfctrl.d32 = 0;
402 	op.sfctrl.b.datalines = p_dev->prog_lines;
403 	op.sfctrl.b.enbledma = 1;
404 	op.sfctrl.b.addrlines = p_dev->prog_addr_lines;
405 
406 	if (p_dev->addr_mode == ADDR_MODE_4BYTE)
407 		op.sfcmd.b.addrbits = SFC_ADDR_32BITS;
408 
409 	snor_write_en();
410 
411 	ret = sfc_request(&op, addr, p_data, size);
412 	if (ret != SFC_OK)
413 		return ret;
414 
415 	ret = snor_wait_busy(10000);
416 
417 	return ret;
418 }
419 
420 static int snor_prog(struct SFNOR_DEV *p_dev, u32 addr, void *p_data, u32 size)
421 {
422 	int ret = SFC_OK;
423 	u32 page_size, len;
424 	u8 *p_buf =  (u8 *)p_data;
425 
426 	page_size = NOR_PAGE_SIZE;
427 	while (size) {
428 		len = page_size < size ? page_size : size;
429 		ret = snor_prog_page(p_dev, addr, p_buf, len);
430 		if (ret != SFC_OK)
431 			return ret;
432 
433 		size -= len;
434 		addr += len;
435 		p_buf += len;
436 	}
437 
438 	return ret;
439 }
440 
441 static int snor_enable_QE(struct SFNOR_DEV *p_dev)
442 {
443 	int ret = SFC_OK;
444 	int reg_index;
445 	int bit_offset;
446 	u8 status;
447 
448 	reg_index = p_dev->QE_bits >> 3;
449 	bit_offset = p_dev->QE_bits & 0x7;
450 	ret = snor_read_status(reg_index, &status);
451 	if (ret != SFC_OK)
452 		return ret;
453 
454 	if (status & (1 << bit_offset))   /* is QE bit set */
455 		return SFC_OK;
456 
457 	status |= (1 << bit_offset);
458 
459 	return p_dev->write_status(reg_index, status);
460 }
461 
462 int snor_disable_QE(struct SFNOR_DEV *p_dev)
463 {
464 	int ret = SFC_OK;
465 	int reg_index;
466 	int bit_offset;
467 	u8 status;
468 
469 	reg_index = p_dev->QE_bits >> 3;
470 	bit_offset = p_dev->QE_bits & 0x7;
471 	ret = snor_read_status(reg_index, &status);
472 	if (ret != SFC_OK)
473 		return ret;
474 
475 	if (!(status & (1 << bit_offset)))
476 		return SFC_OK;
477 
478 	status &= ~(1 << bit_offset);
479 
480 	return p_dev->write_status(reg_index, status);
481 }
482 
483 int snor_read_data(struct SFNOR_DEV *p_dev,
484 		   u32 addr,
485 		   void *p_data,
486 		   u32 size)
487 {
488 	int ret;
489 	struct rk_sfc_op op;
490 
491 	op.sfcmd.d32 = 0;
492 	op.sfcmd.b.cmd = p_dev->read_cmd;
493 	op.sfcmd.b.addrbits = SFC_ADDR_24BITS;
494 
495 	op.sfctrl.d32 = 0;
496 	op.sfctrl.b.datalines = p_dev->read_lines;
497 	if (!(size & 0x3) && size >= 4)
498 		op.sfctrl.b.enbledma = 1;
499 
500 	if (p_dev->read_cmd == CMD_FAST_READ_X1 ||
501 	    p_dev->read_cmd == CMD_PAGE_FASTREAD4B ||
502 	    p_dev->read_cmd == CMD_FAST_READ_X4 ||
503 	    p_dev->read_cmd == CMD_FAST_READ_X2 ||
504 	    p_dev->read_cmd == CMD_FAST_4READ_X4) {
505 		op.sfcmd.b.dummybits = 8;
506 	} else if (p_dev->read_cmd == CMD_FAST_READ_A4) {
507 		op.sfcmd.b.addrbits = SFC_ADDR_32BITS;
508 		addr = (addr << 8) | 0xFF;	/* Set M[7:0] = 0xFF */
509 		op.sfcmd.b.dummybits = 4;
510 		op.sfctrl.b.addrlines = SFC_4BITS_LINE;
511 	}
512 
513 	if (p_dev->addr_mode == ADDR_MODE_4BYTE)
514 		op.sfcmd.b.addrbits = SFC_ADDR_32BITS;
515 
516 	ret = sfc_request(&op, addr, p_data, size);
517 	rkflash_print_dio("%s %x %x\n", __func__, addr, *(u32 *)(p_data));
518 
519 	return ret;
520 }
521 
522 int snor_read(struct SFNOR_DEV *p_dev, u32 sec, u32 n_sec, void *p_data)
523 {
524 	int ret = SFC_OK;
525 	u32 addr, size, len;
526 	u8 *p_buf =  (u8 *)p_data;
527 
528 	rkflash_print_dio("%s %x %x\n", __func__, sec, n_sec);
529 
530 	if ((sec + n_sec) > p_dev->capacity)
531 		return SFC_PARAM_ERR;
532 
533 	addr = sec << 9;
534 	size = n_sec << 9;
535 	while (size) {
536 		len = size < p_dev->max_iosize ? size : p_dev->max_iosize;
537 		ret = snor_read_data(p_dev, addr, p_buf, len);
538 		if (ret != SFC_OK) {
539 			rkflash_print_error("snor_read_data %x ret= %x\n",
540 					    addr >> 9, ret);
541 			goto out;
542 		}
543 
544 		size -= len;
545 		addr += len;
546 		p_buf += len;
547 	}
548 out:
549 	if (!ret)
550 		ret = n_sec;
551 
552 	return ret;
553 }
554 
555 int snor_write(struct SFNOR_DEV *p_dev, u32 sec, u32 n_sec, void *p_data)
556 {
557 	int ret = SFC_OK;
558 	u32 len, blk_size, offset;
559 	u8 *p_buf =  (u8 *)p_data;
560 	u32 total_sec = n_sec;
561 
562 	rkflash_print_dio("%s %x %x\n", __func__, sec, n_sec);
563 
564 	if ((sec + n_sec) > p_dev->capacity)
565 		return SFC_PARAM_ERR;
566 
567 	while (n_sec) {
568 		if (sec < 512 || sec >= p_dev->capacity  - 512)
569 			blk_size = 8;
570 		else
571 			blk_size = p_dev->blk_size;
572 
573 		offset = (sec & (blk_size - 1));
574 		if (!offset) {
575 			ret = snor_erase(p_dev, sec << 9, (blk_size == 8) ?
576 				ERASE_SECTOR : ERASE_BLOCK64K);
577 			if (ret != SFC_OK) {
578 				rkflash_print_error("snor_erase %x ret= %x\n",
579 						    sec, ret);
580 				goto out;
581 			}
582 		}
583 		len = (blk_size - offset) < n_sec ?
584 		      (blk_size - offset) : n_sec;
585 		ret = snor_prog(p_dev, sec << 9, p_buf, len << 9);
586 		if (ret != SFC_OK) {
587 			rkflash_print_error("snor_prog %x ret= %x\n", sec, ret);
588 			goto out;
589 		}
590 		n_sec -= len;
591 		sec += len;
592 		p_buf += len << 9;
593 	}
594 out:
595 	if (!ret)
596 		ret = total_sec;
597 
598 	return ret;
599 }
600 
601 int snor_read_id(u8 *data)
602 {
603 	int ret;
604 	struct rk_sfc_op op;
605 
606 	op.sfcmd.d32 = 0;
607 	op.sfcmd.b.cmd = CMD_READ_JEDECID;
608 
609 	op.sfctrl.d32 = 0;
610 
611 	ret = sfc_request(&op, 0, data, 3);
612 
613 	return ret;
614 }
615 
616 static int snor_read_parameter(u32 addr, u8 *data)
617 {
618 	int ret;
619 	struct rk_sfc_op op;
620 
621 	op.sfcmd.d32 = 0;
622 	op.sfcmd.b.cmd = CMD_READ_PARAMETER;
623 	op.sfcmd.b.addrbits = SFC_ADDR_24BITS;
624 	op.sfcmd.b.dummybits = 8;
625 
626 	op.sfctrl.d32 = 0;
627 
628 	ret = sfc_request(&op, addr, data, 1);
629 
630 	return ret;
631 }
632 
633 u32 snor_get_capacity(struct SFNOR_DEV *p_dev)
634 {
635 	return p_dev->capacity;
636 }
637 
638 static struct flash_info *snor_get_flash_info(u8 *flash_id)
639 {
640 	u32 i;
641 	u32 id = (flash_id[0] << 16) | (flash_id[1] << 8) | (flash_id[2] << 0);
642 
643 	for (i = 0; i < ARRAY_SIZE(spi_flash_tbl); i++) {
644 		if (spi_flash_tbl[i].id == id)
645 			return &spi_flash_tbl[i];
646 	}
647 	return NULL;
648 }
649 
650 /* Adjust flash info in ram base on parameter */
651 static void *snor_flash_info_adjust(struct flash_info *spi_flash_info)
652 {
653 	u32 addr;
654 	u8 para_version;
655 
656 	if (spi_flash_info->id == 0xc84019) {
657 		addr = 0x09;
658 		snor_read_parameter(addr, &para_version);
659 		if (para_version == 0x06) {
660 			spi_flash_info->QE_bits = 9;
661 			spi_flash_info->prog_cmd_4 = 0x34;
662 		}
663 	}
664 	return 0;
665 }
666 
667 static int snor_parse_flash_table(struct SFNOR_DEV *p_dev,
668 				  struct flash_info *g_spi_flash_info)
669 {
670 	int i, ret;
671 
672 	if (g_spi_flash_info) {
673 		snor_flash_info_adjust(g_spi_flash_info);
674 		p_dev->manufacturer = (g_spi_flash_info->id >> 16) & 0xFF;
675 		p_dev->mem_type = (g_spi_flash_info->id >> 8) & 0xFF;
676 		p_dev->capacity = 1 << g_spi_flash_info->density;
677 		p_dev->blk_size = g_spi_flash_info->block_size;
678 		p_dev->page_size = NOR_SECS_PAGE;
679 		p_dev->read_cmd = g_spi_flash_info->read_cmd;
680 		p_dev->prog_cmd = g_spi_flash_info->prog_cmd;
681 		p_dev->sec_erase_cmd = g_spi_flash_info->sector_erase_cmd;
682 		p_dev->blk_erase_cmd = g_spi_flash_info->block_erase_cmd;
683 		p_dev->prog_lines = DATA_LINES_X1;
684 		p_dev->read_lines = DATA_LINES_X1;
685 		p_dev->QE_bits = g_spi_flash_info->QE_bits;
686 		p_dev->addr_mode = ADDR_MODE_3BYTE;
687 
688 		i = g_spi_flash_info->feature & FEA_READ_STATUE_MASK;
689 		if (i == 0)
690 			p_dev->write_status = snor_write_status;
691 		else if (i == 1)
692 			p_dev->write_status = snor_write_status1;
693 		else if (i == 2)
694 			p_dev->write_status = snor_write_status2;
695 
696 		if (g_spi_flash_info->feature & FEA_4BIT_READ) {
697 			ret = SFC_OK;
698 			if (g_spi_flash_info->QE_bits)
699 				ret = snor_enable_QE(p_dev);
700 			if (ret == SFC_OK) {
701 				p_dev->read_lines = DATA_LINES_X4;
702 				p_dev->read_cmd = g_spi_flash_info->read_cmd_4;
703 			}
704 		}
705 		if (g_spi_flash_info->feature & FEA_4BIT_PROG &&
706 		    p_dev->read_lines == DATA_LINES_X4) {
707 			p_dev->prog_lines = DATA_LINES_X4;
708 			p_dev->prog_cmd = g_spi_flash_info->prog_cmd_4;
709 			if ((p_dev->manufacturer == MID_MACRONIX) &&
710 			    (p_dev->prog_cmd == CMD_PAGE_PROG_A4 ||
711 			     p_dev->prog_cmd == CMD_PAGE_PROG_4PP))
712 				p_dev->prog_addr_lines = DATA_LINES_X4;
713 		}
714 
715 		if (g_spi_flash_info->feature & FEA_4BYTE_ADDR)
716 			p_dev->addr_mode = ADDR_MODE_4BYTE;
717 
718 		if ((g_spi_flash_info->feature & FEA_4BYTE_ADDR_MODE))
719 			snor_enter_4byte_mode();
720 	}
721 
722 	return SFC_OK;
723 }
724 
725 int snor_init(struct SFNOR_DEV *p_dev)
726 {
727 	struct flash_info *g_spi_flash_info;
728 	u8 id_byte[5];
729 
730 	if (!p_dev)
731 		return SFC_PARAM_ERR;
732 
733 	memset((void *)p_dev, 0, sizeof(struct SFNOR_DEV));
734 	p_dev->max_iosize = sfc_get_max_iosize();
735 
736 	snor_read_id(id_byte);
737 	rkflash_print_error("sfc nor id: %x %x %x\n",
738 			    id_byte[0], id_byte[1], id_byte[2]);
739 	if (0xFF == id_byte[0] || 0x00 == id_byte[0] || 0xFF == id_byte[1] || 0x00 == id_byte[1])
740 		return SFC_ERROR;
741 
742 	g_spi_flash_info = snor_get_flash_info(id_byte);
743 	if (g_spi_flash_info) {
744 		snor_parse_flash_table(p_dev, g_spi_flash_info);
745 	} else {
746 		pr_err("The device not support yet!\n");
747 
748 		p_dev->manufacturer = id_byte[0];
749 		p_dev->mem_type = id_byte[1];
750 		p_dev->capacity = 1 << (id_byte[2] - 9);
751 		p_dev->QE_bits = 0;
752 		p_dev->blk_size = NOR_SECS_BLK;
753 		p_dev->page_size = NOR_SECS_PAGE;
754 		p_dev->read_cmd = CMD_READ_DATA;
755 		p_dev->prog_cmd = CMD_PAGE_PROG;
756 		p_dev->sec_erase_cmd = CMD_SECTOR_ERASE;
757 		p_dev->blk_erase_cmd = CMD_BLOCK_ERASE;
758 		p_dev->prog_lines = DATA_LINES_X1;
759 		p_dev->prog_addr_lines = DATA_LINES_X1;
760 		p_dev->read_lines = DATA_LINES_X1;
761 		p_dev->write_status = snor_write_status;
762 		snor_reset_device();
763 	}
764 
765 	rkflash_print_info("addr_mode: %x\n", p_dev->addr_mode);
766 	rkflash_print_info("read_lines: %x\n", p_dev->read_lines);
767 	rkflash_print_info("prog_lines: %x\n", p_dev->prog_lines);
768 	rkflash_print_info("read_cmd: %x\n", p_dev->read_cmd);
769 	rkflash_print_info("prog_cmd: %x\n", p_dev->prog_cmd);
770 	rkflash_print_info("blk_erase_cmd: %x\n", p_dev->blk_erase_cmd);
771 	rkflash_print_info("sec_erase_cmd: %x\n", p_dev->sec_erase_cmd);
772 	rkflash_print_info("capacity: %x\n", p_dev->capacity);
773 
774 	return SFC_OK;
775 }
776 
777 int snor_reinit_from_table_packet(struct SFNOR_DEV *p_dev,
778 				  struct snor_info_packet *packet)
779 {
780 	struct flash_info g_spi_flash_info;
781 	u8 id_byte[5];
782 	int ret;
783 
784 	if (!p_dev || packet->id != SNOR_INFO_PACKET_ID)
785 		return SFC_PARAM_ERR;
786 
787 	snor_read_id(id_byte);
788 	if (0xFF == id_byte[0] || 0x00 == id_byte[0])
789 		return SFC_ERROR;
790 
791 	g_spi_flash_info.id = id_byte[0] << 16 | id_byte[1] << 8 | id_byte[2];
792 	g_spi_flash_info.block_size = NOR_SECS_BLK;
793 	g_spi_flash_info.sector_size = NOR_SECS_PAGE;
794 	g_spi_flash_info.read_cmd = packet->read_cmd;
795 	g_spi_flash_info.prog_cmd = packet->prog_cmd;
796 	g_spi_flash_info.read_cmd_4 = packet->read_cmd_4;
797 	g_spi_flash_info.prog_cmd_4 = packet->prog_cmd_4;
798 	if (id_byte[2] >=  0x19)
799 		g_spi_flash_info.read_cmd_4 = CMD_FAST_4READ_X4;
800 	g_spi_flash_info.sector_erase_cmd = packet->sector_erase_cmd;
801 	g_spi_flash_info.block_erase_cmd = packet->block_erase_cmd;
802 	g_spi_flash_info.feature = packet->feature;
803 	g_spi_flash_info.density = id_byte[2] - 9;
804 	g_spi_flash_info.QE_bits = packet->QE_bits;
805 
806 	ret = snor_parse_flash_table(p_dev, &g_spi_flash_info);
807 
808 	return ret;
809 }
810 
811