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_write_status(struct spi_flash *flash, u8 sr)
32 cmd = CMD_WRITE_STATUS;
33 ret = spi_flash_write_common(flash, &cmd, 1, &sr, 1);
35 debug("SF: fail to write status register\n");
42 #ifdef CONFIG_SPI_FLASH_MACRONIX
43 int spi_flash_set_qeb_mxic(struct spi_flash *flash)
49 cmd = CMD_READ_STATUS;
50 ret = spi_flash_read_common(flash, &cmd, 1, &qeb_status, 1);
52 debug("SF: fail to read status register\n");
56 if (qeb_status & STATUS_QEB_MXIC) {
57 debug("SF: Quad enable bit is already set\n");
59 ret = spi_flash_cmd_write_status(flash, STATUS_QEB_MXIC);
68 #if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
69 static int spi_flash_cmd_write_config(struct spi_flash *flash, u8 cr)
75 cmd = CMD_READ_STATUS;
76 ret = spi_flash_read_common(flash, &cmd, 1, &data[0], 1);
78 debug("SF: fail to read status register\n");
82 cmd = CMD_WRITE_STATUS;
84 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
86 debug("SF: fail to write config register\n");
93 int spi_flash_set_qeb_winspan(struct spi_flash *flash)
99 cmd = CMD_READ_CONFIG;
100 ret = spi_flash_read_common(flash, &cmd, 1, &qeb_status, 1);
102 debug("SF: fail to read config register\n");
106 if (qeb_status & STATUS_QEB_WINSPAN) {
107 debug("SF: Quad enable bit is already set\n");
109 ret = spi_flash_cmd_write_config(flash, STATUS_QEB_WINSPAN);
118 #ifdef CONFIG_SPI_FLASH_BAR
119 static int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
124 if (flash->bank_curr == bank_sel) {
125 debug("SF: not require to enable bank%d\n", bank_sel);
129 cmd = flash->bank_write_cmd;
130 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
132 debug("SF: fail to write bank register\n");
135 flash->bank_curr = bank_sel;
140 static int spi_flash_bank(struct spi_flash *flash, u32 offset)
145 bank_sel = offset / SPI_FLASH_16MB_BOUN;
147 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
149 debug("SF: fail to set bank%d\n", bank_sel);
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;
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 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
174 debug("SF: fail to read %s status register\n",
175 cmd == CMD_READ_STATUS ? "read" : "flag");
179 timebase = get_timer(0);
183 ret = spi_xfer(spi, 8, NULL, &status, 0);
187 if ((status & poll_bit) == check_status)
190 } while (get_timer(timebase) < timeout);
192 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
194 if ((status & poll_bit) == check_status)
198 debug("SF: time out!\n");
202 int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
203 size_t cmd_len, const void *buf, size_t buf_len)
205 struct spi_slave *spi = flash->spi;
206 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
210 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
212 ret = spi_claim_bus(flash->spi);
214 debug("SF: unable to claim SPI bus\n");
218 ret = spi_flash_cmd_write_enable(flash);
220 debug("SF: enabling write failed\n");
224 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
226 debug("SF: write cmd failed\n");
230 ret = spi_flash_cmd_wait_ready(flash, timeout);
232 debug("SF: write %s timed out\n",
233 timeout == SPI_FLASH_PROG_TIMEOUT ?
234 "program" : "page erase");
238 spi_release_bus(spi);
243 int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
246 u8 cmd[SPI_FLASH_CMD_LEN];
249 erase_size = flash->erase_size;
250 if (offset % erase_size || len % erase_size) {
251 debug("SF: Erase offset/length not multiple of erase size\n");
255 cmd[0] = flash->erase_cmd;
257 #ifdef CONFIG_SPI_FLASH_BAR
258 ret = spi_flash_bank(flash, offset);
262 spi_flash_addr(offset, cmd);
264 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
265 cmd[2], cmd[3], offset);
267 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
269 debug("SF: erase failed\n");
273 offset += erase_size;
280 int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
281 size_t len, const void *buf)
283 unsigned long byte_addr, page_size;
284 size_t chunk_len, actual;
285 u8 cmd[SPI_FLASH_CMD_LEN];
288 page_size = flash->page_size;
290 cmd[0] = flash->write_cmd;
291 for (actual = 0; actual < len; actual += chunk_len) {
292 #ifdef CONFIG_SPI_FLASH_BAR
293 ret = spi_flash_bank(flash, offset);
297 byte_addr = offset % page_size;
298 chunk_len = min(len - actual, page_size - byte_addr);
300 if (flash->spi->max_write_size)
301 chunk_len = min(chunk_len, flash->spi->max_write_size);
303 spi_flash_addr(offset, cmd);
305 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
306 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
308 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
309 buf + actual, chunk_len);
311 debug("SF: write failed\n");
321 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
322 size_t cmd_len, void *data, size_t data_len)
324 struct spi_slave *spi = flash->spi;
327 ret = spi_claim_bus(flash->spi);
329 debug("SF: unable to claim SPI bus\n");
333 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
335 debug("SF: read cmd failed\n");
339 spi_release_bus(spi);
344 int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
345 size_t len, void *data)
347 u8 *cmd, cmdsz, bank_sel = 0;
348 u32 remain_len, read_len;
351 /* Handle memory-mapped SPI */
352 if (flash->memory_map) {
353 ret = spi_claim_bus(flash->spi);
355 debug("SF: unable to claim SPI bus\n");
358 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
359 memcpy(data, flash->memory_map + offset, len);
360 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
361 spi_release_bus(flash->spi);
365 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
367 memset(cmd, 0, cmdsz);
369 cmd[0] = flash->read_cmd;
371 #ifdef CONFIG_SPI_FLASH_BAR
372 bank_sel = offset / SPI_FLASH_16MB_BOUN;
374 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
376 debug("SF: fail to set bank%d\n", bank_sel);
380 remain_len = (SPI_FLASH_16MB_BOUN * (bank_sel + 1)) - offset;
381 if (len < remain_len)
384 read_len = remain_len;
386 spi_flash_addr(offset, cmd);
388 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
390 debug("SF: read failed\n");
402 #ifdef CONFIG_SPI_FLASH_SST
403 static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
413 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
414 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
416 ret = spi_flash_cmd_write_enable(flash);
420 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
424 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
427 int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
430 size_t actual, cmd_len;
434 ret = spi_claim_bus(flash->spi);
436 debug("SF: Unable to claim SPI bus\n");
440 /* If the data is not word aligned, write out leading single byte */
443 ret = sst_byte_write(flash, offset, buf);
449 ret = spi_flash_cmd_write_enable(flash);
454 cmd[0] = CMD_SST_AAI_WP;
455 cmd[1] = offset >> 16;
456 cmd[2] = offset >> 8;
459 for (; actual < len - 1; actual += 2) {
460 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
461 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
464 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
467 debug("SF: sst word program failed\n");
471 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
480 ret = spi_flash_cmd_write_disable(flash);
482 /* If there is a single trailing byte, write it out */
483 if (!ret && actual != len)
484 ret = sst_byte_write(flash, offset, buf + actual);
487 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
488 ret ? "failure" : "success", len, offset - actual);
490 spi_release_bus(flash->spi);