4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
8 * SPDX-License-Identifier: GPL-2.0+
15 #include <spi_flash.h>
17 #include <linux/compiler.h>
19 #include "sf_internal.h"
21 static void spi_flash_addr(u32 addr
, u8
*cmd
)
23 /* cmd[0] is actual command */
29 int spi_flash_cmd_read_status(struct spi_flash
*flash
, u8
*rs
)
34 cmd
= CMD_READ_STATUS
;
35 ret
= spi_flash_read_common(flash
, &cmd
, 1, rs
, 1);
37 debug("SF: fail to read status register\n");
44 static int read_fsr(struct spi_flash
*flash
, u8
*fsr
)
47 const u8 cmd
= CMD_FLAG_STATUS
;
49 ret
= spi_flash_read_common(flash
, &cmd
, 1, fsr
, 1);
51 debug("SF: fail to read flag status register\n");
58 int spi_flash_cmd_write_status(struct spi_flash
*flash
, u8 ws
)
63 cmd
= CMD_WRITE_STATUS
;
64 ret
= spi_flash_write_common(flash
, &cmd
, 1, &ws
, 1);
66 debug("SF: fail to write status register\n");
73 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
74 int spi_flash_cmd_read_config(struct spi_flash
*flash
, u8
*rc
)
79 cmd
= CMD_READ_CONFIG
;
80 ret
= spi_flash_read_common(flash
, &cmd
, 1, rc
, 1);
82 debug("SF: fail to read config register\n");
89 int spi_flash_cmd_write_config(struct spi_flash
*flash
, u8 wc
)
95 ret
= spi_flash_cmd_read_status(flash
, &data
[0]);
99 cmd
= CMD_WRITE_STATUS
;
101 ret
= spi_flash_write_common(flash
, &cmd
, 1, &data
, 2);
103 debug("SF: fail to write config register\n");
111 #ifdef CONFIG_SPI_FLASH_BAR
112 static int spi_flash_write_bank(struct spi_flash
*flash
, u32 offset
)
117 bank_sel
= offset
/ (SPI_FLASH_16MB_BOUN
<< flash
->shift
);
118 if (bank_sel
== flash
->bank_curr
)
121 cmd
= flash
->bank_write_cmd
;
122 ret
= spi_flash_write_common(flash
, &cmd
, 1, &bank_sel
, 1);
124 debug("SF: fail to write bank register\n");
129 flash
->bank_curr
= bank_sel
;
130 return flash
->bank_curr
;
134 #ifdef CONFIG_SF_DUAL_FLASH
135 static void spi_flash_dual_flash(struct spi_flash
*flash
, u32
*addr
)
137 switch (flash
->dual_flash
) {
138 case SF_DUAL_STACKED_FLASH
:
139 if (*addr
>= (flash
->size
>> 1)) {
140 *addr
-= flash
->size
>> 1;
141 flash
->spi
->flags
|= SPI_XFER_U_PAGE
;
143 flash
->spi
->flags
&= ~SPI_XFER_U_PAGE
;
146 case SF_DUAL_PARALLEL_FLASH
:
147 *addr
>>= flash
->shift
;
150 debug("SF: Unsupported dual_flash=%d\n", flash
->dual_flash
);
156 static int spi_flash_sr_ready(struct spi_flash
*flash
)
161 ret
= spi_flash_cmd_read_status(flash
, &sr
);
165 return !(sr
& STATUS_WIP
);
168 static int spi_flash_fsr_ready(struct spi_flash
*flash
)
173 ret
= read_fsr(flash
, &fsr
);
177 return fsr
& STATUS_PEC
;
180 static int spi_flash_ready(struct spi_flash
*flash
)
184 sr
= spi_flash_sr_ready(flash
);
189 if (flash
->flags
& SNOR_F_USE_FSR
) {
190 fsr
= spi_flash_fsr_ready(flash
);
198 int spi_flash_cmd_wait_ready(struct spi_flash
*flash
, unsigned long timeout
)
202 timebase
= get_timer(0);
204 while (get_timer(timebase
) < timeout
) {
205 ret
= spi_flash_ready(flash
);
212 printf("SF: Timeout!\n");
217 int spi_flash_write_common(struct spi_flash
*flash
, const u8
*cmd
,
218 size_t cmd_len
, const void *buf
, size_t buf_len
)
220 struct spi_slave
*spi
= flash
->spi
;
221 unsigned long timeout
= SPI_FLASH_PROG_TIMEOUT
;
225 timeout
= SPI_FLASH_PAGE_ERASE_TIMEOUT
;
227 ret
= spi_claim_bus(flash
->spi
);
229 debug("SF: unable to claim SPI bus\n");
233 ret
= spi_flash_cmd_write_enable(flash
);
235 debug("SF: enabling write failed\n");
239 ret
= spi_flash_cmd_write(spi
, cmd
, cmd_len
, buf
, buf_len
);
241 debug("SF: write cmd failed\n");
245 ret
= spi_flash_cmd_wait_ready(flash
, timeout
);
247 debug("SF: write %s timed out\n",
248 timeout
== SPI_FLASH_PROG_TIMEOUT
?
249 "program" : "page erase");
253 spi_release_bus(spi
);
258 int spi_flash_cmd_erase_ops(struct spi_flash
*flash
, u32 offset
, size_t len
)
260 u32 erase_size
, erase_addr
;
261 u8 cmd
[SPI_FLASH_CMD_LEN
];
264 erase_size
= flash
->erase_size
;
265 if (offset
% erase_size
|| len
% erase_size
) {
266 debug("SF: Erase offset/length not multiple of erase size\n");
270 cmd
[0] = flash
->erase_cmd
;
274 #ifdef CONFIG_SF_DUAL_FLASH
275 if (flash
->dual_flash
> SF_SINGLE_FLASH
)
276 spi_flash_dual_flash(flash
, &erase_addr
);
278 #ifdef CONFIG_SPI_FLASH_BAR
279 ret
= spi_flash_write_bank(flash
, erase_addr
);
283 spi_flash_addr(erase_addr
, cmd
);
285 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd
[0], cmd
[1],
286 cmd
[2], cmd
[3], erase_addr
);
288 ret
= spi_flash_write_common(flash
, cmd
, sizeof(cmd
), NULL
, 0);
290 debug("SF: erase failed\n");
294 offset
+= erase_size
;
301 int spi_flash_cmd_write_ops(struct spi_flash
*flash
, u32 offset
,
302 size_t len
, const void *buf
)
304 unsigned long byte_addr
, page_size
;
306 size_t chunk_len
, actual
;
307 u8 cmd
[SPI_FLASH_CMD_LEN
];
310 page_size
= flash
->page_size
;
312 cmd
[0] = flash
->write_cmd
;
313 for (actual
= 0; actual
< len
; actual
+= chunk_len
) {
316 #ifdef CONFIG_SF_DUAL_FLASH
317 if (flash
->dual_flash
> SF_SINGLE_FLASH
)
318 spi_flash_dual_flash(flash
, &write_addr
);
320 #ifdef CONFIG_SPI_FLASH_BAR
321 ret
= spi_flash_write_bank(flash
, write_addr
);
325 byte_addr
= offset
% page_size
;
326 chunk_len
= min(len
- actual
, (size_t)(page_size
- byte_addr
));
328 if (flash
->spi
->max_write_size
)
329 chunk_len
= min(chunk_len
,
330 (size_t)flash
->spi
->max_write_size
);
332 spi_flash_addr(write_addr
, cmd
);
334 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
335 buf
+ actual
, cmd
[0], cmd
[1], cmd
[2], cmd
[3], chunk_len
);
337 ret
= spi_flash_write_common(flash
, cmd
, sizeof(cmd
),
338 buf
+ actual
, chunk_len
);
340 debug("SF: write failed\n");
350 int spi_flash_read_common(struct spi_flash
*flash
, const u8
*cmd
,
351 size_t cmd_len
, void *data
, size_t data_len
)
353 struct spi_slave
*spi
= flash
->spi
;
356 ret
= spi_claim_bus(flash
->spi
);
358 debug("SF: unable to claim SPI bus\n");
362 ret
= spi_flash_cmd_read(spi
, cmd
, cmd_len
, data
, data_len
);
364 debug("SF: read cmd failed\n");
368 spi_release_bus(spi
);
373 void __weak
spi_flash_copy_mmap(void *data
, void *offset
, size_t len
)
375 memcpy(data
, offset
, len
);
378 int spi_flash_cmd_read_ops(struct spi_flash
*flash
, u32 offset
,
379 size_t len
, void *data
)
382 u32 remain_len
, read_len
, read_addr
;
386 /* Handle memory-mapped SPI */
387 if (flash
->memory_map
) {
388 ret
= spi_claim_bus(flash
->spi
);
390 debug("SF: unable to claim SPI bus\n");
393 spi_xfer(flash
->spi
, 0, NULL
, NULL
, SPI_XFER_MMAP
);
394 spi_flash_copy_mmap(data
, flash
->memory_map
+ offset
, len
);
395 spi_xfer(flash
->spi
, 0, NULL
, NULL
, SPI_XFER_MMAP_END
);
396 spi_release_bus(flash
->spi
);
400 cmdsz
= SPI_FLASH_CMD_LEN
+ flash
->dummy_byte
;
401 cmd
= calloc(1, cmdsz
);
403 debug("SF: Failed to allocate cmd\n");
407 cmd
[0] = flash
->read_cmd
;
411 #ifdef CONFIG_SF_DUAL_FLASH
412 if (flash
->dual_flash
> SF_SINGLE_FLASH
)
413 spi_flash_dual_flash(flash
, &read_addr
);
415 #ifdef CONFIG_SPI_FLASH_BAR
416 ret
= spi_flash_write_bank(flash
, read_addr
);
419 bank_sel
= flash
->bank_curr
;
421 remain_len
= ((SPI_FLASH_16MB_BOUN
<< flash
->shift
) *
422 (bank_sel
+ 1)) - offset
;
423 if (len
< remain_len
)
426 read_len
= remain_len
;
428 spi_flash_addr(read_addr
, cmd
);
430 ret
= spi_flash_read_common(flash
, cmd
, cmdsz
, data
, read_len
);
432 debug("SF: read failed\n");
445 #ifdef CONFIG_SPI_FLASH_SST
446 static int sst_byte_write(struct spi_flash
*flash
, u32 offset
, const void *buf
)
456 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
457 spi_w8r8(flash
->spi
, CMD_READ_STATUS
), buf
, cmd
[0], offset
);
459 ret
= spi_flash_cmd_write_enable(flash
);
463 ret
= spi_flash_cmd_write(flash
->spi
, cmd
, sizeof(cmd
), buf
, 1);
467 return spi_flash_cmd_wait_ready(flash
, SPI_FLASH_PROG_TIMEOUT
);
470 int sst_write_wp(struct spi_flash
*flash
, u32 offset
, size_t len
,
473 size_t actual
, cmd_len
;
477 ret
= spi_claim_bus(flash
->spi
);
479 debug("SF: Unable to claim SPI bus\n");
483 /* If the data is not word aligned, write out leading single byte */
486 ret
= sst_byte_write(flash
, offset
, buf
);
492 ret
= spi_flash_cmd_write_enable(flash
);
497 cmd
[0] = CMD_SST_AAI_WP
;
498 cmd
[1] = offset
>> 16;
499 cmd
[2] = offset
>> 8;
502 for (; actual
< len
- 1; actual
+= 2) {
503 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
504 spi_w8r8(flash
->spi
, CMD_READ_STATUS
), buf
+ actual
,
507 ret
= spi_flash_cmd_write(flash
->spi
, cmd
, cmd_len
,
510 debug("SF: sst word program failed\n");
514 ret
= spi_flash_cmd_wait_ready(flash
, SPI_FLASH_PROG_TIMEOUT
);
523 ret
= spi_flash_cmd_write_disable(flash
);
525 /* If there is a single trailing byte, write it out */
526 if (!ret
&& actual
!= len
)
527 ret
= sst_byte_write(flash
, offset
, buf
+ actual
);
530 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
531 ret
? "failure" : "success", len
, offset
- actual
);
533 spi_release_bus(flash
->spi
);
537 int sst_write_bp(struct spi_flash
*flash
, u32 offset
, size_t len
,
543 ret
= spi_claim_bus(flash
->spi
);
545 debug("SF: Unable to claim SPI bus\n");
549 for (actual
= 0; actual
< len
; actual
++) {
550 ret
= sst_byte_write(flash
, offset
, buf
+ actual
);
552 debug("SF: sst byte program failed\n");
559 ret
= spi_flash_cmd_write_disable(flash
);
561 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
562 ret
? "failure" : "success", len
, offset
- actual
);
564 spi_release_bus(flash
->spi
);