Debian Codegraph @

src/driver/spi/spi_flash.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /**
3 ******************************************************************************
4 * @file spi_flash.c
5 * @author StarFive Technology
6 * @version V1.0
7 * @date 07/24/2020
8 * @brief
9 ******************************************************************************
10 * @copy
11 *
12 * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
14 * TIME. AS A RESULT, STARFIVE SHALL NOT BE HELD LIABLE FOR ANY
15 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
16 * FROM THE CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
17 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 *
19 * COPYRIGHT 2020 Shanghai StarFive Technology Co., Ltd.
20 */
21
22
23 #include <comdef.h>
24 #include "spi.h"
25 #include "spi_flash.h"
26 #include "spi_flash_internal.h"
27 #include <cadence_qspi.h>
28
29 static int qe_enable = 0;
30
31 static void spi_flash_addr(u32 addr, u8 *cmd)
32 {
33 /* cmd[0] is actual command */
34 cmd[1] = (addr & 0x00FF0000) >> 16;
35 cmd[2] = (addr & 0x0000FF00) >> 8;
36 cmd[3] = (addr & 0x000000FF) >> 0;
37 }
38
39 static int spi_flash_read_write(struct spi_slave *spi,
40 u8 *cmd, u32 cmd_len,
41 u8 *data_out, u8 *data_in,
42 u32 data_len)
43 {
44 unsigned long flags = SPI_XFER_BEGIN;
45 int ret;
46
47 if (data_len == 0)
48 flags |= SPI_XFER_END;
49
50 ret = spi_xfer(spi, cmd_len * 8, cmd, NULL, flags, SPI_DATAMODE_8);
51 if (ret)
52 {
53 //uart_printf("SF: Failed to send command (%d bytes): %d\n",
54 //cmd_len, ret);
55 }
56 else if (data_len != 0)
57 {
58 ret = spi_xfer(spi, data_len * 8, data_out, data_in, SPI_XFER_END, SPI_DATAMODE_8);
59 }
60
61 return ret;
62 }
63
64 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, u32 len)
65 {
66 return spi_flash_cmd_read(spi, &cmd, 1, response, len);
67 }
68
69 int spi_flash_cmd_read(struct spi_slave *spi, u8 *cmd,
70 u32 cmd_len, void *data, u32 data_len)
71 {
72 return spi_flash_read_write(spi, cmd, cmd_len, NULL, data, data_len);
73 }
74
75 int spi_flash_cmd_write(struct spi_slave *spi, u8 *cmd, u32 cmd_len,
76 void *data, u32 data_len)
77 {
78 return spi_flash_read_write(spi, cmd, cmd_len, data, NULL, data_len);
79 }
80
81 int spi_flash_cmd_write_enable(struct spi_flash *flash)
82 {
83 return spi_flash_cmd(flash->spi, CMD_WRITE_ENABLE, (void*)NULL, 0);
84 }
85
86 int spi_flash_cmd_write_status_enable(struct spi_flash *flash)
87 {
88 return spi_flash_cmd(flash->spi, CMD_STATUS_ENABLE, (void*)NULL, 0);
89 }
90
91 int spi_flash_cmd_write_disable(struct spi_slave *spi)
92 {
93 return spi_flash_cmd(spi, CMD_WRITE_DISABLE, (void*)NULL, 0);
94 }
95 int spi_flash_cmd_read_status(struct spi_flash *flash, u8 *cmd, u32 cmd_len, u8 *status)
96 {
97 struct spi_slave *spi = flash->spi;
98 int ret;
99
100 ret = spi_xfer(spi, 8*cmd_len, cmd, NULL, SPI_XFER_BEGIN, SPI_DATAMODE_8);
101 if (ret) {
102 //uart_printf("SF: Failed to send command %x: %d\n", (unsigned int)cmd, ret);
103 return ret;
104 }
105
106 ret = spi_xfer(spi, 8*1, NULL, status, SPI_XFER_END, SPI_DATAMODE_8);
107 //uart_printf("status = 0x%x\r\n", status[0]);
108 if (ret)
109 return ret;
110
111 return 0;
112 }
113
114 int spi_flash_cmd_poll_bit(struct spi_flash *flash, unsigned long timeout,
115 u8 cmd, u8 poll_bit)
116 {
117 int ret;
118 u8 status;
119 u32 status_tmp = 0;
120 u32 timebase_1 = 0;
121 do {
122 ret = spi_flash_cmd_read_status(flash, &cmd, 1, &status);
123 //uart_printf("cmd = 0x%x, status = 0x%x\r\n", cmd, status);
124 if (ret)
125 return ret;
126 if ((status & poll_bit) == 0)
127 break;
128 timebase_1++;//libo
129 } while (timebase_1 < timeout);
130
131 if ((status & poll_bit) == 0)
132 return 0;
133
134 /* Timed out */
135 //uart_printf("SF: time out!\r\n");
136 return -1;
137 }
138
139 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
140 {
141 return spi_flash_cmd_poll_bit(flash, timeout,
142 CMD_READ_STATUS, STATUS_WIP);
143 }
144
145 int spi_flash_cmd_poll_enable(struct spi_flash *flash, unsigned long timeout,
146 u8 cmd, u32 poll_bit)
147 {
148 int ret;
149 u8 status;
150 u32 status_tmp = 0;
151
152 u32 timebase_1 = 0;
153 do {
154
155 ret = spi_flash_cmd_read_status(flash, &cmd, 1, &status);
156 if (ret)
157 return ret;
158 //uart_printf("read status = 0x%x\r\n", status);
159 if ((status & poll_bit) == 1)
160 break;
161 timebase_1++;
162 } while (timebase_1 < timeout);
163
164 /* Timed out */
165 //uart_printf("SF: time out!\r\n");
166 return 0;
167 }
168
169 int spi_flash_cmd_status_poll_enable(struct spi_flash *flash, unsigned long timeout,
170 u8 cmd, u32 poll_bit)
171 {
172 int ret;
173 u8 status;
174 u32 status_tmp = 0;
175
176 u32 timebase_1 = 0;
177 do {
178
179 ret = spi_flash_cmd_read_status(flash, &cmd, 1, &status);
180 if (ret)
181 return ret;
182 //uart_printf("read status = 0x%x\r\n", status);
183 if ((status & poll_bit) == 0x2)
184 break;
185 timebase_1++;
186 } while (timebase_1 < timeout);
187
188 /* Timed out */
189 //uart_printf("SF: time out!\r\n");
190 return 0;
191 }
192
193 int spi_flash_cmd_wait_enable(struct spi_flash *flash, unsigned long timeout)
194 {
195 return spi_flash_cmd_status_poll_enable(flash, timeout,
196 CMD_READ_STATUS, FLASH_ENABLE);
197 }
198 int spi_flash_write_status(struct spi_flash *flash, u8 *cmd, unsigned int cmd_len,void *data, unsigned int data_len)
199 {
200 int ret;
201
202 ret = spi_flash_cmd_write_enable(flash);
203 if (ret) {
204 // uart_printf("SF: Unable to claim SPI bus\n");
205 return ret;
206 }
207
208 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len, data, data_len);
209 if (ret < 0) {
210 //uart_printf("SF: write failed\n");
211 return ret;
212 }
213 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
214 if (ret < 0) {
215 //uart_printf("SF: wait ready failed\n");
216 return ret;
217 }
218 ret = spi_flash_cmd_write_disable(flash->spi);
219 if (ret < 0) {
220 //uart_printf("SF: disable write failed\n");
221 return ret;
222 }
223 return 0;
224 }
225 #ifdef ISP_BOARD
226 int spi_flash_write_status_bit(struct spi_flash *flash, u8 status0, u8 bit0)
227 {
228 u8 status[2];
229 int ret = 0;
230
231 status[0] = CMD_WRITE_STATUS;
232 status[1] = status0|bit0;
233 spi_flash_write_status(flash, &status[0], 1, &status[1], 1);
234
235 if (bit0)
236 {
237 ret &= spi_flash_cmd_poll_bit(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT, CMD_READ_STATUS, ~bit0);
238 }
239
240 return ret;
241 delay(1000);
242
243
244 return ret;
245 }
246 int spi_flash_protect(struct spi_flash *flash)
247 {
248 /* set PB=0 all can write */
249 return spi_flash_write_status_bit(flash, 0x00, 0);
250 }
251 #else
252 int spi_flash_write_status_bit(struct spi_flash *flash, u8 status1, u8 status2, u8 bit1, u8 bit2)
253 {
254 u8 status[3];
255 int ret = 0;
256
257 status[0] = CMD_WRITE_STATUS;
258 status[1] = status1|bit1;
259 status[2] = status2|bit2;
260 spi_flash_write_status(flash, &status[0], 1, &status[1], 2);
261
262 if (bit1)
263 {
264 ret &= spi_flash_cmd_poll_bit(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT, CMD_READ_STATUS, ~bit1);
265 }
266 if (bit2)
267 {
268 ret &= spi_flash_cmd_poll_bit(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT, CMD_READ_STATUS1, ~bit2);
269 }
270
271 return ret;
272 delay(1000);
273
274
275 return ret;
276 }
277
278 int spi_flash_protect(struct spi_flash *flash)
279 {
280 /* set PB=0 all can write */
281 return spi_flash_write_status_bit(flash, 0x00, 0x00, 0, 0);
282 }
283 #endif
284 int spi_flash_cmd_erase(struct spi_flash *flash, u8 erase_cmd,
285 u32 offset, u32 len)
286 {
287 u32 start, end, erase_size;
288 int ret;
289 u8 cmd[4];
290
291 //uart_printf("spi_flash_cmd_erase \r\n");
292
293 switch(erase_cmd){
294 case CMD_W25_SE:
295 erase_size = flash->sector_size;
296 break;
297 case CMD_W25_BE_32:
298 erase_size = flash->sector_size * 8;
299 break;
300 case CMD_W25_BE:
301 erase_size = flash->block_size;
302 break;
303 default:
304 erase_size = flash->sector_size;
305 break;
306 }
307
308 if (offset % erase_size || len % erase_size) {
309 //uart_printf("SF: Erase offset/length not multiple of erase size\n");
310 return -1;
311 }
312
313 // spi_flash_cmd_write_status_enable(flash);
314 spi_flash_protect(flash);
315
316 cmd[0] = erase_cmd;
317 start = offset;
318 end = start + len;
319 while (offset < end)
320 {
321 spi_flash_addr(offset, cmd);
322 offset += erase_size;
323
324 //uart_printf("SF: erase %x %x %x %x (%x)\n", cmd[0], cmd[1],
325 // cmd[2], cmd[3], offset);
326
327 ret = spi_flash_cmd_write_enable(flash);
328 if (ret)
329 goto out;
330
331 //spi_flash_cmd_wait_enable(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
332
333 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), NULL, 0);
334 if (ret)
335 goto out;
336
337 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
338 if (ret)
339 goto out;
340
341 ret = spi_flash_cmd_write_disable(flash->spi);
342 if (ret)
343 goto out;
344
345 }
346
347 //uart_printf("SF: Successfully erased %d bytes @ %x\n", len , start);
348
349 out:
350 return ret;
351 }
352
353 /* mode is 4, 32, 64*/
354 int spi_flash_erase_mode(struct spi_flash *flash, u32 offset, u32 len, u32 mode)
355 {
356 int ret = 0;
357 switch (mode)
358 {
359 case 4:
360 ret = spi_flash_cmd_erase(flash, CMD_W25_SE, offset, len);
361 break;
362 case 32:
363 ret = spi_flash_cmd_erase(flash, CMD_W25_BE_32, offset, len);
364 break;
365 case 64:
366 ret = spi_flash_cmd_erase(flash, CMD_W25_BE, offset, len);
367 break;
368 default:
369 ret = spi_flash_cmd_erase(flash, CMD_W25_BE, offset, len);
370 break;
371 }
372 return ret;
373 }
374
375 int spi_flash_cmd_write_mode(struct spi_flash *flash, u32 offset,u32 len, void *buf, u32 mode)
376 {
377 struct spi_slave *spi = flash->spi;
378 unsigned long byte_addr, page_size;
379 u32 write_addr;
380 u32 chunk_len, actual;
381 int ret;
382 u8 cmd[4];
383 int write_data = 1;
384 unsigned long flags = SPI_XFER_BEGIN;
385
386 page_size = flash->page_size;
387
388 switch (mode){
389 case 1:
390 cmd[0] = CMD_PAGE_PROGRAM;
391 qe_enable = 0;
392 break;
393 case 4:
394 cmd[0] = CMD_PAGE_PROGRAM_QUAD;
395 #ifdef ISP_BOARD
396 spi_flash_write_status_bit(flash, 0x00, STATUS_QE);
397 #else
398 spi_flash_write_status_bit(flash, 0x00, 0x00, 0, STATUS_QE);
399 #endif
400 break;
401 default:
402 cmd[0] = CMD_PAGE_PROGRAM;
403 break;
404 }
405
406
407 for (actual = 0; actual < len; actual += chunk_len)
408 {
409 write_addr = offset;
410 byte_addr = offset % page_size;
411 chunk_len = min(len - actual, page_size - byte_addr);
412
413 spi_flash_addr(write_addr, cmd);
414
415 ret = spi_flash_cmd_write_enable(flash);
416 if (ret < 0) {
417 //uart_printf("SF: enabling write failed\n");
418 break;
419 }
420 spi_flash_cmd_wait_enable(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
421 #if 1
422 if (mode == 1)
423 {
424
425
426 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
427 (unsigned char*)buf + actual, chunk_len);
428 if (ret < 0) {
429 //uart_printf("SF: write failed\n");
430 break;
431 }
432
433 }
434 #endif
435 if (mode == 4)
436 {
437 flags = SPI_XFER_BEGIN;
438 if (chunk_len == 0)
439 flags |= SPI_XFER_END;
440
441 ret = spi_xfer(spi, 4 * 8, cmd, NULL, flags, SPI_DATAMODE_8);
442 if (ret < 0)
443 {
444 //uart_printf("xfer failed\n");
445 return ret;
446 }
447 else if (chunk_len != 0)
448 {
449 //qspi_mode_ctl(SPI4_DATEMODE_4);
450 ret = spi_xfer(spi, chunk_len * 8, (unsigned char*)buf + actual, NULL, SPI_XFER_END, SPI_DATAMODE_8);
451
452 }
453 }
454 //qspi_mode_ctl(SPI4_DATEMODE_0);
455 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
456 if (ret < 0)
457 {
458 //uart_printf("SF: spi_flash_cmd_wait_ready failed\n");
459 break;
460 }
461 ret = spi_flash_cmd_write_disable(flash->spi);
462 if (ret < 0)
463 {
464 //uart_printf("SF: disable write failed\n");
465 break;
466 }
467
468 offset += chunk_len;
469 //uart_printf("SF: program %s %d bytes @ %d\n", ret ? "failure" : "success", len, offset);
470 }
471 return ret;
472 }
473
474 int spi_flash_read_common(struct spi_flash *flash, u8 *cmd,
475 u32 cmd_len, void *data, u32 data_len)
476 {
477 struct spi_slave *spi = flash->spi;
478 int ret;
479
480 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
481
482 return ret;
483 }
484
485 int spi_flash_cmd_read_fast(struct spi_flash *flash, u32 offset,
486 u32 len, void *data, u32 mode)
487 {
488 u8 cmd[5];
489
490 cmd[0] = CMD_READ_ARRAY_FAST;
491 spi_flash_addr(offset, cmd);
492 cmd[4] = 0x00;
493
494 return spi_flash_read_common(flash, cmd, sizeof(cmd), data, len);
495 }
496
497 int spi_flash_read_mode(struct spi_flash *flash, u32 offset,
498 u32 len, void *data, u32 mode)
499 {
500 struct spi_slave *spi = flash->spi;
501 u8 cmd[5];
502 int ret;
503 int write_data = 0;
504 u8 status[2] = {2};
505 int i = 0;
506
507 switch (mode)
508 {
509 case 1:
510 cmd[0] = CMD_READ_ARRAY_FAST;
511 qe_enable = 0;
512 break;
513 case 2:
514 cmd[0] = CMD_READ_ARRAY_DUAL;
515 break;
516 case 4:
517 #ifdef ISP_BOARD
518 cmd[0] = CMD_READ_ARRAY_LEGACY;
519 //spi_flash_cmd_write_reg_mode(flash, flash_reg_offset, 1, flash_reg);
520 //flash_reg[0] = 0;
521 //spi_flash_cmd_write_reg_mode(flash, flash_reg_offset, 1, flash_reg);
522 //spi_flash_read_reg_mode(flash, flash_reg_offset, 1, flash_reg);
523 #else
524 cmd[0] = CMD_READ_ARRAY_QUAD;
525 if(qe_enable == 0)
526 {
527 spi_flash_write_status_bit(flash, 0x00, 0x00, 0, STATUS_QE);;
528 qe_enable = 1;
529 }
530 #endif
531 break;
532 default:
533 cmd[0] = CMD_READ_ARRAY_FAST;
534 break;
535 }
536
537 //spi_flash_cmd_poll_bit(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT, CMD_READ_STATUS1, ~STATUS_QE);
538
539
540 spi_flash_addr(offset, cmd);
541 cmd[4] = 0x00;
542
543
544 ret = spi_xfer(spi, 5*8, cmd, NULL, SPI_XFER_BEGIN, SPI_DATAMODE_8);
545 if (ret < 0)
546 {
547 //uart_printf("xfer failed\n");
548 return ret;
549 }
550 ret = spi_xfer(spi, len*8, NULL, data, SPI_XFER_END, SPI_DATAMODE_8);
551 if (ret < 0)
552 {
553 //uart_printf("xfer failed\n");
554 return ret;
555 }
556
557 return 0;
558 }