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>
18 #include "sf_internal.h"
20 static void spi_flash_addr(u32 addr, u8 *cmd)
22 /* cmd[0] is actual command */
28 int spi_flash_cmd_read_status(struct spi_flash *flash, u8 *rs)
33 cmd = CMD_READ_STATUS;
34 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
36 debug("SF: fail to read status register\n");
43 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 ws)
48 cmd = CMD_WRITE_STATUS;
49 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
51 debug("SF: fail to write status register\n");
58 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
59 int spi_flash_cmd_read_config(struct spi_flash *flash, u8 *rc)
64 cmd = CMD_READ_CONFIG;
65 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
67 debug("SF: fail to read config register\n");
74 int spi_flash_cmd_write_config(struct spi_flash *flash, u8 wc)
80 ret = spi_flash_cmd_read_status(flash, &data[0]);
84 cmd = CMD_WRITE_STATUS;
86 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
88 debug("SF: fail to write config register\n");
96 #ifdef CONFIG_SPI_FLASH_BAR
97 static int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
102 if (flash->bank_curr == bank_sel) {
103 debug("SF: not require to enable bank%d\n", bank_sel);
107 cmd = flash->bank_write_cmd;
108 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
110 debug("SF: fail to write bank register\n");
113 flash->bank_curr = bank_sel;
118 static int spi_flash_bank(struct spi_flash *flash, u32 offset)
123 bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
125 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
127 debug("SF: fail to set bank%d\n", bank_sel);
135 #ifdef CONFIG_SF_DUAL_FLASH
136 static void spi_flash_dual_flash(struct spi_flash *flash, u32 *addr)
138 switch (flash->dual_flash) {
139 case SF_DUAL_STACKED_FLASH:
140 if (*addr >= (flash->size >> 1)) {
141 *addr -= flash->size >> 1;
142 flash->spi->flags |= SPI_XFER_U_PAGE;
144 flash->spi->flags &= ~SPI_XFER_U_PAGE;
147 case SF_DUAL_PARALLEL_FLASH:
148 *addr >>= flash->shift;
151 debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
157 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
159 struct spi_slave *spi = flash->spi;
160 unsigned long timebase;
161 unsigned long flags = SPI_XFER_BEGIN;
164 u8 check_status = 0x0;
165 u8 poll_bit = STATUS_WIP;
166 u8 cmd = flash->poll_cmd;
168 if (cmd == CMD_FLAG_STATUS) {
169 poll_bit = STATUS_PEC;
170 check_status = poll_bit;
173 #ifdef CONFIG_SF_DUAL_FLASH
174 if (spi->flags & SPI_XFER_U_PAGE)
175 flags |= SPI_XFER_U_PAGE;
177 ret = spi_xfer(spi, 8, &cmd, NULL, flags);
179 debug("SF: fail to read %s status register\n",
180 cmd == CMD_READ_STATUS ? "read" : "flag");
184 timebase = get_timer(0);
188 ret = spi_xfer(spi, 8, NULL, &status, 0);
192 if ((status & poll_bit) == check_status)
195 } while (get_timer(timebase) < timeout);
197 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
199 if ((status & poll_bit) == check_status)
203 debug("SF: time out!\n");
207 int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
208 size_t cmd_len, const void *buf, size_t buf_len)
210 struct spi_slave *spi = flash->spi;
211 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
215 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
217 ret = spi_claim_bus(flash->spi);
219 debug("SF: unable to claim SPI bus\n");
223 ret = spi_flash_cmd_write_enable(flash);
225 debug("SF: enabling write failed\n");
229 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
231 debug("SF: write cmd failed\n");
235 ret = spi_flash_cmd_wait_ready(flash, timeout);
237 debug("SF: write %s timed out\n",
238 timeout == SPI_FLASH_PROG_TIMEOUT ?
239 "program" : "page erase");
243 spi_release_bus(spi);
248 int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
250 u32 erase_size, erase_addr;
251 u8 cmd[SPI_FLASH_CMD_LEN];
254 erase_size = flash->erase_size;
255 if (offset % erase_size || len % erase_size) {
256 debug("SF: Erase offset/length not multiple of erase size\n");
260 cmd[0] = flash->erase_cmd;
264 #ifdef CONFIG_SF_DUAL_FLASH
265 if (flash->dual_flash > SF_SINGLE_FLASH)
266 spi_flash_dual_flash(flash, &erase_addr);
268 #ifdef CONFIG_SPI_FLASH_BAR
269 ret = spi_flash_bank(flash, erase_addr);
273 spi_flash_addr(erase_addr, cmd);
275 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
276 cmd[2], cmd[3], erase_addr);
278 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
280 debug("SF: erase failed\n");
284 offset += erase_size;
291 int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
292 size_t len, const void *buf)
294 unsigned long byte_addr, page_size;
296 size_t chunk_len, actual;
297 u8 cmd[SPI_FLASH_CMD_LEN];
300 page_size = flash->page_size;
302 cmd[0] = flash->write_cmd;
303 for (actual = 0; actual < len; actual += chunk_len) {
306 #ifdef CONFIG_SF_DUAL_FLASH
307 if (flash->dual_flash > SF_SINGLE_FLASH)
308 spi_flash_dual_flash(flash, &write_addr);
310 #ifdef CONFIG_SPI_FLASH_BAR
311 ret = spi_flash_bank(flash, write_addr);
315 byte_addr = offset % page_size;
316 chunk_len = min(len - actual, page_size - byte_addr);
318 if (flash->spi->max_write_size)
319 chunk_len = min(chunk_len, flash->spi->max_write_size);
321 spi_flash_addr(write_addr, cmd);
323 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
324 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
326 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
327 buf + actual, chunk_len);
329 debug("SF: write failed\n");
339 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
340 size_t cmd_len, void *data, size_t data_len)
342 struct spi_slave *spi = flash->spi;
345 ret = spi_claim_bus(flash->spi);
347 debug("SF: unable to claim SPI bus\n");
351 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
353 debug("SF: read cmd failed\n");
357 spi_release_bus(spi);
362 int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
363 size_t len, void *data)
366 u32 remain_len, read_len, read_addr;
370 /* Handle memory-mapped SPI */
371 if (flash->memory_map) {
372 ret = spi_claim_bus(flash->spi);
374 debug("SF: unable to claim SPI bus\n");
377 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
378 memcpy(data, flash->memory_map + offset, len);
379 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
380 spi_release_bus(flash->spi);
384 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
385 cmd = calloc(1, cmdsz);
387 debug("SF: Failed to allocate cmd\n");
391 cmd[0] = flash->read_cmd;
395 #ifdef CONFIG_SF_DUAL_FLASH
396 if (flash->dual_flash > SF_SINGLE_FLASH)
397 spi_flash_dual_flash(flash, &read_addr);
399 #ifdef CONFIG_SPI_FLASH_BAR
400 bank_sel = spi_flash_bank(flash, read_addr);
404 remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
405 (bank_sel + 1)) - offset;
406 if (len < remain_len)
409 read_len = remain_len;
411 spi_flash_addr(read_addr, cmd);
413 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
415 debug("SF: read failed\n");
427 #ifdef CONFIG_SPI_FLASH_SST
428 static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
438 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
439 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
441 ret = spi_flash_cmd_write_enable(flash);
445 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
449 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
452 int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
455 size_t actual, cmd_len;
459 ret = spi_claim_bus(flash->spi);
461 debug("SF: Unable to claim SPI bus\n");
465 /* If the data is not word aligned, write out leading single byte */
468 ret = sst_byte_write(flash, offset, buf);
474 ret = spi_flash_cmd_write_enable(flash);
479 cmd[0] = CMD_SST_AAI_WP;
480 cmd[1] = offset >> 16;
481 cmd[2] = offset >> 8;
484 for (; actual < len - 1; actual += 2) {
485 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
486 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
489 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
492 debug("SF: sst word program failed\n");
496 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
505 ret = spi_flash_cmd_write_disable(flash);
507 /* If there is a single trailing byte, write it out */
508 if (!ret && actual != len)
509 ret = sst_byte_write(flash, offset, buf + actual);
512 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
513 ret ? "failure" : "success", len, offset - actual);
515 spi_release_bus(flash->spi);