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+
13 #include <spi_flash.h>
16 #include "sf_internal.h"
18 static void spi_flash_addr(u32 addr, u8 *cmd)
20 /* cmd[0] is actual command */
26 int spi_flash_cmd_write_status(struct spi_flash *flash, u8 sr)
31 cmd = CMD_WRITE_STATUS;
32 ret = spi_flash_write_common(flash, &cmd, 1, &sr, 1);
34 debug("SF: fail to write status register\n");
41 #ifdef CONFIG_SPI_FLASH_BAR
42 static int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
47 if (flash->bank_curr == bank_sel) {
48 debug("SF: not require to enable bank%d\n", bank_sel);
52 cmd = flash->bank_write_cmd;
53 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
55 debug("SF: fail to write bank register\n");
58 flash->bank_curr = bank_sel;
63 static int spi_flash_bank(struct spi_flash *flash, u32 offset)
68 bank_sel = offset / SPI_FLASH_16MB_BOUN;
70 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
72 debug("SF: fail to set bank%d\n", bank_sel);
80 int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
82 struct spi_slave *spi = flash->spi;
83 unsigned long timebase;
86 u8 check_status = 0x0;
87 u8 poll_bit = STATUS_WIP;
88 u8 cmd = flash->poll_cmd;
90 if (cmd == CMD_FLAG_STATUS) {
91 poll_bit = STATUS_PEC;
92 check_status = poll_bit;
95 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
97 debug("SF: fail to read %s status register\n",
98 cmd == CMD_READ_STATUS ? "read" : "flag");
102 timebase = get_timer(0);
106 ret = spi_xfer(spi, 8, NULL, &status, 0);
110 if ((status & poll_bit) == check_status)
113 } while (get_timer(timebase) < timeout);
115 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
117 if ((status & poll_bit) == check_status)
121 debug("SF: time out!\n");
125 int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
126 size_t cmd_len, const void *buf, size_t buf_len)
128 struct spi_slave *spi = flash->spi;
129 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
133 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
135 ret = spi_claim_bus(flash->spi);
137 debug("SF: unable to claim SPI bus\n");
141 ret = spi_flash_cmd_write_enable(flash);
143 debug("SF: enabling write failed\n");
147 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
149 debug("SF: write cmd failed\n");
153 ret = spi_flash_cmd_wait_ready(flash, timeout);
155 debug("SF: write %s timed out\n",
156 timeout == SPI_FLASH_PROG_TIMEOUT ?
157 "program" : "page erase");
161 spi_release_bus(spi);
166 int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
172 erase_size = flash->erase_size;
173 if (offset % erase_size || len % erase_size) {
174 debug("SF: Erase offset/length not multiple of erase size\n");
178 cmd[0] = flash->erase_cmd;
180 #ifdef CONFIG_SPI_FLASH_BAR
181 ret = spi_flash_bank(flash, offset);
185 spi_flash_addr(offset, cmd);
187 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
188 cmd[2], cmd[3], offset);
190 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
192 debug("SF: erase failed\n");
196 offset += erase_size;
203 int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
204 size_t len, const void *buf)
206 unsigned long byte_addr, page_size;
207 size_t chunk_len, actual;
211 page_size = flash->page_size;
213 cmd[0] = CMD_PAGE_PROGRAM;
214 for (actual = 0; actual < len; actual += chunk_len) {
215 #ifdef CONFIG_SPI_FLASH_BAR
216 ret = spi_flash_bank(flash, offset);
220 byte_addr = offset % page_size;
221 chunk_len = min(len - actual, page_size - byte_addr);
223 if (flash->spi->max_write_size)
224 chunk_len = min(chunk_len, flash->spi->max_write_size);
226 spi_flash_addr(offset, cmd);
228 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
229 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
231 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
232 buf + actual, chunk_len);
234 debug("SF: write failed\n");
244 int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
245 size_t cmd_len, void *data, size_t data_len)
247 struct spi_slave *spi = flash->spi;
250 ret = spi_claim_bus(flash->spi);
252 debug("SF: unable to claim SPI bus\n");
256 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
258 debug("SF: read cmd failed\n");
262 spi_release_bus(spi);
267 int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
268 size_t len, void *data)
270 u8 cmd[5], bank_sel = 0;
271 u32 remain_len, read_len;
274 /* Handle memory-mapped SPI */
275 if (flash->memory_map) {
276 ret = spi_claim_bus(flash->spi);
278 debug("SF: unable to claim SPI bus\n");
281 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
282 memcpy(data, flash->memory_map + offset, len);
283 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
284 spi_release_bus(flash->spi);
288 cmd[0] = CMD_READ_ARRAY_FAST;
292 #ifdef CONFIG_SPI_FLASH_BAR
293 bank_sel = offset / SPI_FLASH_16MB_BOUN;
295 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
297 debug("SF: fail to set bank%d\n", bank_sel);
301 remain_len = (SPI_FLASH_16MB_BOUN * (bank_sel + 1)) - offset;
302 if (len < remain_len)
305 read_len = remain_len;
307 spi_flash_addr(offset, cmd);
309 ret = spi_flash_read_common(flash, cmd, sizeof(cmd),
312 debug("SF: read failed\n");
324 #ifdef CONFIG_SPI_FLASH_SST
325 static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
335 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
336 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
338 ret = spi_flash_cmd_write_enable(flash);
342 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
346 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
349 int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
352 size_t actual, cmd_len;
356 ret = spi_claim_bus(flash->spi);
358 debug("SF: Unable to claim SPI bus\n");
362 /* If the data is not word aligned, write out leading single byte */
365 ret = sst_byte_write(flash, offset, buf);
371 ret = spi_flash_cmd_write_enable(flash);
376 cmd[0] = CMD_SST_AAI_WP;
377 cmd[1] = offset >> 16;
378 cmd[2] = offset >> 8;
381 for (; actual < len - 1; actual += 2) {
382 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
383 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
386 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
389 debug("SF: sst word program failed\n");
393 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
402 ret = spi_flash_cmd_write_disable(flash);
404 /* If there is a single trailing byte, write it out */
405 if (!ret && actual != len)
406 ret = sst_byte_write(flash, offset, buf + actual);
409 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
410 ret ? "failure" : "success", len, offset - actual);
412 spi_release_bus(flash->spi);