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+
14 #include <spi_flash.h>
17 #include "sf_internal.h"
19 static void spi_flash_addr(u32 addr, u8 *cmd)
21 /* cmd[0] is actual command */
27 int spi_flash_cmd_read_status(struct spi_flash *flash, u8 *rs)
32 cmd = CMD_READ_STATUS;
33 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
35 debug("SF: fail to read status register\n");
42 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 ws)
47 cmd = CMD_WRITE_STATUS;
48 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
50 debug("SF: fail to write status register\n");
57 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
58 int spi_flash_cmd_read_config(struct spi_flash *flash, u8 *rc)
63 cmd = CMD_READ_CONFIG;
64 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
66 debug("SF: fail to read config register\n");
73 int spi_flash_cmd_write_config(struct spi_flash *flash, u8 wc)
79 ret = spi_flash_cmd_read_status(flash, &data[0]);
83 cmd = CMD_WRITE_STATUS;
85 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
87 debug("SF: fail to write config register\n");
95 #ifdef CONFIG_SPI_FLASH_BAR
96 static int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
101 if (flash->bank_curr == bank_sel) {
102 debug("SF: not require to enable bank%d\n", bank_sel);
106 cmd = flash->bank_write_cmd;
107 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
109 debug("SF: fail to write bank register\n");
112 flash->bank_curr = bank_sel;
117 static int spi_flash_bank(struct spi_flash *flash, u32 offset)
122 bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
124 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
126 debug("SF: fail to set bank%d\n", bank_sel);
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 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
158 struct spi_slave *spi = flash->spi;
159 unsigned long timebase;
160 unsigned long flags = SPI_XFER_BEGIN;
163 u8 check_status = 0x0;
164 u8 poll_bit = STATUS_WIP;
165 u8 cmd = flash->poll_cmd;
167 if (cmd == CMD_FLAG_STATUS) {
168 poll_bit = STATUS_PEC;
169 check_status = poll_bit;
172 #ifdef CONFIG_SF_DUAL_FLASH
173 if (spi->flags & SPI_XFER_U_PAGE)
174 flags |= SPI_XFER_U_PAGE;
176 ret = spi_xfer(spi, 8, &cmd, NULL, flags);
178 debug("SF: fail to read %s status register\n",
179 cmd == CMD_READ_STATUS ? "read" : "flag");
183 timebase = get_timer(0);
187 ret = spi_xfer(spi, 8, NULL, &status, 0);
191 if ((status & poll_bit) == check_status)
194 } while (get_timer(timebase) < timeout);
196 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
198 if ((status & poll_bit) == check_status)
202 debug("SF: time out!\n");
206 int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
207 size_t cmd_len, const void *buf, size_t buf_len)
209 struct spi_slave *spi = flash->spi;
210 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
214 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
216 ret = spi_claim_bus(flash->spi);
218 debug("SF: unable to claim SPI bus\n");
222 ret = spi_flash_cmd_write_enable(flash);
224 debug("SF: enabling write failed\n");
228 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
230 debug("SF: write cmd failed\n");
234 ret = spi_flash_cmd_wait_ready(flash, timeout);
236 debug("SF: write %s timed out\n",
237 timeout == SPI_FLASH_PROG_TIMEOUT ?
238 "program" : "page erase");
242 spi_release_bus(spi);
247 int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
249 u32 erase_size, erase_addr;
250 u8 cmd[SPI_FLASH_CMD_LEN];
253 erase_size = flash->erase_size;
254 if (offset % erase_size || len % erase_size) {
255 debug("SF: Erase offset/length not multiple of erase size\n");
259 cmd[0] = flash->erase_cmd;
263 #ifdef CONFIG_SF_DUAL_FLASH
264 if (flash->dual_flash > SF_SINGLE_FLASH)
265 spi_flash_dual_flash(flash, &erase_addr);
267 #ifdef CONFIG_SPI_FLASH_BAR
268 ret = spi_flash_bank(flash, erase_addr);
272 spi_flash_addr(erase_addr, cmd);
274 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
275 cmd[2], cmd[3], erase_addr);
277 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
279 debug("SF: erase failed\n");
283 offset += erase_size;
290 int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
291 size_t len, const void *buf)
293 unsigned long byte_addr, page_size;
295 size_t chunk_len, actual;
296 u8 cmd[SPI_FLASH_CMD_LEN];
299 page_size = flash->page_size;
301 cmd[0] = flash->write_cmd;
302 for (actual = 0; actual < len; actual += chunk_len) {
305 #ifdef CONFIG_SF_DUAL_FLASH
306 if (flash->dual_flash > SF_SINGLE_FLASH)
307 spi_flash_dual_flash(flash, &write_addr);
309 #ifdef CONFIG_SPI_FLASH_BAR
310 ret = spi_flash_bank(flash, write_addr);
314 byte_addr = offset % page_size;
315 chunk_len = min(len - actual, page_size - byte_addr);
317 if (flash->spi->max_write_size)
318 chunk_len = min(chunk_len, flash->spi->max_write_size);
320 spi_flash_addr(write_addr, cmd);
322 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
323 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
325 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
326 buf + actual, chunk_len);
328 debug("SF: write failed\n");
338 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
339 size_t cmd_len, void *data, size_t data_len)
341 struct spi_slave *spi = flash->spi;
344 ret = spi_claim_bus(flash->spi);
346 debug("SF: unable to claim SPI bus\n");
350 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
352 debug("SF: read cmd failed\n");
356 spi_release_bus(spi);
361 int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
362 size_t len, void *data)
365 u32 remain_len, read_len, read_addr;
369 /* Handle memory-mapped SPI */
370 if (flash->memory_map) {
371 ret = spi_claim_bus(flash->spi);
373 debug("SF: unable to claim SPI bus\n");
376 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
377 memcpy(data, flash->memory_map + offset, len);
378 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
379 spi_release_bus(flash->spi);
383 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
385 memset(cmd, 0, cmdsz);
387 cmd[0] = flash->read_cmd;
391 #ifdef CONFIG_SF_DUAL_FLASH
392 if (flash->dual_flash > SF_SINGLE_FLASH)
393 spi_flash_dual_flash(flash, &read_addr);
395 #ifdef CONFIG_SPI_FLASH_BAR
396 bank_sel = spi_flash_bank(flash, read_addr);
400 remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
401 (bank_sel + 1)) - offset;
402 if (len < remain_len)
405 read_len = remain_len;
407 spi_flash_addr(read_addr, cmd);
409 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
411 debug("SF: read failed\n");
423 #ifdef CONFIG_SPI_FLASH_SST
424 static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
434 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
435 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
437 ret = spi_flash_cmd_write_enable(flash);
441 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
445 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
448 int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
451 size_t actual, cmd_len;
455 ret = spi_claim_bus(flash->spi);
457 debug("SF: Unable to claim SPI bus\n");
461 /* If the data is not word aligned, write out leading single byte */
464 ret = sst_byte_write(flash, offset, buf);
470 ret = spi_flash_cmd_write_enable(flash);
475 cmd[0] = CMD_SST_AAI_WP;
476 cmd[1] = offset >> 16;
477 cmd[2] = offset >> 8;
480 for (; actual < len - 1; actual += 2) {
481 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
482 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
485 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
488 debug("SF: sst word program failed\n");
492 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
501 ret = spi_flash_cmd_write_disable(flash);
503 /* If there is a single trailing byte, write it out */
504 if (!ret && actual != len)
505 ret = sst_byte_write(flash, offset, buf + actual);
508 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
509 ret ? "failure" : "success", len, offset - actual);
511 spi_release_bus(flash->spi);