blob: 900ec1f2a9ce8cb95ae4f2d087db3bc440c72b69 [file] [log] [blame]
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05301/*
2 * SPI flash operations
3 *
4 * Copyright (C) 2008 Atmel Corporation
5 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
6 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
7 *
Jagannadha Sutradharudu Tekid1452702013-10-10 22:32:55 +05308 * SPDX-License-Identifier: GPL-2.0+
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05309 */
10
11#include <common.h>
Jagannadha Sutradharudu Teki9ad01752014-02-04 21:36:13 +053012#include <errno.h>
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +053013#include <malloc.h>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053014#include <spi.h>
15#include <spi_flash.h>
16#include <watchdog.h>
Tom Rini4fe44702015-08-17 13:29:54 +053017#include <linux/compiler.h>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053018
Jagannadha Sutradharudu Tekiac8242d2013-09-26 16:00:15 +053019#include "sf_internal.h"
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053020
21static void spi_flash_addr(u32 addr, u8 *cmd)
22{
23 /* cmd[0] is actual command */
24 cmd[1] = addr >> 16;
25 cmd[2] = addr >> 8;
26 cmd[3] = addr >> 0;
27}
28
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053029int spi_flash_cmd_read_status(struct spi_flash *flash, u8 *rs)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053030{
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053031 int ret;
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053032 u8 cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053033
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053034 cmd = CMD_READ_STATUS;
35 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053036 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053037 debug("SF: fail to read status register\n");
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053038 return ret;
39 }
40
41 return 0;
42}
43
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +053044int spi_flash_cmd_write_status(struct spi_flash *flash, u8 ws)
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053045{
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053046 u8 cmd;
47 int ret;
48
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053049 cmd = CMD_WRITE_STATUS;
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +053050 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053051 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053052 debug("SF: fail to write status register\n");
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053053 return ret;
54 }
55
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053056 return 0;
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053057}
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053058
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053059#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053060int spi_flash_cmd_read_config(struct spi_flash *flash, u8 *rc)
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053061{
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053062 int ret;
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053063 u8 cmd;
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053064
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053065 cmd = CMD_READ_CONFIG;
66 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053067 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053068 debug("SF: fail to read config register\n");
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053069 return ret;
70 }
71
72 return 0;
73}
74
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053075int spi_flash_cmd_write_config(struct spi_flash *flash, u8 wc)
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053076{
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053077 u8 data[2];
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053078 u8 cmd;
79 int ret;
80
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053081 ret = spi_flash_cmd_read_status(flash, &data[0]);
82 if (ret < 0)
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053083 return ret;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053084
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053085 cmd = CMD_WRITE_STATUS;
86 data[1] = wc;
87 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
88 if (ret) {
89 debug("SF: fail to write config register\n");
90 return ret;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053091 }
92
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053093 return 0;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053094}
95#endif
96
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053097#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Teki86a380e2013-08-28 14:57:03 +053098static int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053099{
100 u8 cmd;
101 int ret;
102
103 if (flash->bank_curr == bank_sel) {
104 debug("SF: not require to enable bank%d\n", bank_sel);
105 return 0;
106 }
107
108 cmd = flash->bank_write_cmd;
109 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
110 if (ret < 0) {
111 debug("SF: fail to write bank register\n");
112 return ret;
113 }
114 flash->bank_curr = bank_sel;
115
116 return 0;
117}
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530118
119static int spi_flash_bank(struct spi_flash *flash, u32 offset)
120{
121 u8 bank_sel;
122 int ret;
123
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530124 bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530125
126 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
127 if (ret) {
128 debug("SF: fail to set bank%d\n", bank_sel);
129 return ret;
130 }
131
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530132 return bank_sel;
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530133}
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530134#endif
135
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530136#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530137static void spi_flash_dual_flash(struct spi_flash *flash, u32 *addr)
138{
139 switch (flash->dual_flash) {
140 case SF_DUAL_STACKED_FLASH:
141 if (*addr >= (flash->size >> 1)) {
142 *addr -= flash->size >> 1;
143 flash->spi->flags |= SPI_XFER_U_PAGE;
144 } else {
145 flash->spi->flags &= ~SPI_XFER_U_PAGE;
146 }
147 break;
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530148 case SF_DUAL_PARALLEL_FLASH:
149 *addr >>= flash->shift;
150 break;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530151 default:
152 debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
153 break;
154 }
155}
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530156#endif
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530157
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530158static int spi_flash_poll_status(struct spi_slave *spi, unsigned long timeout,
159 u8 cmd, u8 poll_bit)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530160{
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530161 unsigned long timebase;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530162 unsigned long flags = SPI_XFER_BEGIN;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530163 int ret;
164 u8 status;
165 u8 check_status = 0x0;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530166
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530167 if (cmd == CMD_FLAG_STATUS)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530168 check_status = poll_bit;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530169
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530170#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530171 if (spi->flags & SPI_XFER_U_PAGE)
172 flags |= SPI_XFER_U_PAGE;
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530173#endif
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530174 ret = spi_xfer(spi, 8, &cmd, NULL, flags);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530175 if (ret) {
176 debug("SF: fail to read %s status register\n",
177 cmd == CMD_READ_STATUS ? "read" : "flag");
178 return ret;
179 }
180
181 timebase = get_timer(0);
182 do {
183 WATCHDOG_RESET();
184
185 ret = spi_xfer(spi, 8, NULL, &status, 0);
186 if (ret)
187 return -1;
188
189 if ((status & poll_bit) == check_status)
190 break;
191
192 } while (get_timer(timebase) < timeout);
193
194 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
195
196 if ((status & poll_bit) == check_status)
197 return 0;
198
199 /* Timed out */
200 debug("SF: time out!\n");
201 return -1;
202}
203
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530204int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
205{
206 struct spi_slave *spi = flash->spi;
207 int ret;
208 u8 poll_bit = STATUS_WIP;
209 u8 cmd = CMD_READ_STATUS;
210
211 ret = spi_flash_poll_status(spi, timeout, cmd, poll_bit);
212 if (ret < 0)
213 return ret;
214
215 if (flash->poll_cmd == CMD_FLAG_STATUS) {
216 poll_bit = STATUS_PEC;
217 cmd = CMD_FLAG_STATUS;
218 ret = spi_flash_poll_status(spi, timeout, cmd, poll_bit);
219 if (ret < 0)
220 return ret;
221 }
222
223 return 0;
224}
225
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530226int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
227 size_t cmd_len, const void *buf, size_t buf_len)
228{
229 struct spi_slave *spi = flash->spi;
230 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
231 int ret;
232
233 if (buf == NULL)
234 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
235
236 ret = spi_claim_bus(flash->spi);
237 if (ret) {
238 debug("SF: unable to claim SPI bus\n");
239 return ret;
240 }
241
242 ret = spi_flash_cmd_write_enable(flash);
243 if (ret < 0) {
244 debug("SF: enabling write failed\n");
245 return ret;
246 }
247
248 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
249 if (ret < 0) {
250 debug("SF: write cmd failed\n");
251 return ret;
252 }
253
254 ret = spi_flash_cmd_wait_ready(flash, timeout);
255 if (ret < 0) {
256 debug("SF: write %s timed out\n",
257 timeout == SPI_FLASH_PROG_TIMEOUT ?
258 "program" : "page erase");
259 return ret;
260 }
261
262 spi_release_bus(spi);
263
264 return ret;
265}
266
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530267int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530268{
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530269 u32 erase_size, erase_addr;
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530270 u8 cmd[SPI_FLASH_CMD_LEN];
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530271 int ret = -1;
272
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530273 erase_size = flash->erase_size;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530274 if (offset % erase_size || len % erase_size) {
275 debug("SF: Erase offset/length not multiple of erase size\n");
276 return -1;
277 }
278
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530279 cmd[0] = flash->erase_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530280 while (len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530281 erase_addr = offset;
282
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530283#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530284 if (flash->dual_flash > SF_SINGLE_FLASH)
285 spi_flash_dual_flash(flash, &erase_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530286#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530287#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530288 ret = spi_flash_bank(flash, erase_addr);
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530289 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530290 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530291#endif
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530292 spi_flash_addr(erase_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530293
294 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530295 cmd[2], cmd[3], erase_addr);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530296
297 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
298 if (ret < 0) {
299 debug("SF: erase failed\n");
300 break;
301 }
302
303 offset += erase_size;
304 len -= erase_size;
305 }
306
307 return ret;
308}
309
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530310int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530311 size_t len, const void *buf)
312{
313 unsigned long byte_addr, page_size;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530314 u32 write_addr;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530315 size_t chunk_len, actual;
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530316 u8 cmd[SPI_FLASH_CMD_LEN];
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530317 int ret = -1;
318
319 page_size = flash->page_size;
320
Jagannadha Sutradharudu Tekie0ebabc2014-01-11 15:13:11 +0530321 cmd[0] = flash->write_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530322 for (actual = 0; actual < len; actual += chunk_len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530323 write_addr = offset;
324
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530325#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530326 if (flash->dual_flash > SF_SINGLE_FLASH)
327 spi_flash_dual_flash(flash, &write_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530328#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530329#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530330 ret = spi_flash_bank(flash, write_addr);
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530331 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530332 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530333#endif
334 byte_addr = offset % page_size;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900335 chunk_len = min(len - actual, (size_t)(page_size - byte_addr));
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530336
337 if (flash->spi->max_write_size)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900338 chunk_len = min(chunk_len,
339 (size_t)flash->spi->max_write_size);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530340
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530341 spi_flash_addr(write_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530342
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +0530343 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530344 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
345
346 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
347 buf + actual, chunk_len);
348 if (ret < 0) {
349 debug("SF: write failed\n");
350 break;
351 }
352
353 offset += chunk_len;
354 }
355
356 return ret;
357}
358
359int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
360 size_t cmd_len, void *data, size_t data_len)
361{
362 struct spi_slave *spi = flash->spi;
363 int ret;
364
365 ret = spi_claim_bus(flash->spi);
366 if (ret) {
367 debug("SF: unable to claim SPI bus\n");
368 return ret;
369 }
370
371 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
372 if (ret < 0) {
373 debug("SF: read cmd failed\n");
374 return ret;
375 }
376
377 spi_release_bus(spi);
378
379 return ret;
380}
381
Tom Rini4fe44702015-08-17 13:29:54 +0530382void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len)
383{
384 memcpy(data, offset, len);
385}
386
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530387int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530388 size_t len, void *data)
389{
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530390 u8 *cmd, cmdsz;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530391 u32 remain_len, read_len, read_addr;
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530392 int bank_sel = 0;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530393 int ret = -1;
394
395 /* Handle memory-mapped SPI */
396 if (flash->memory_map) {
Poddar, Sourav47e21e02013-11-14 21:01:15 +0530397 ret = spi_claim_bus(flash->spi);
398 if (ret) {
399 debug("SF: unable to claim SPI bus\n");
400 return ret;
401 }
Poddar, Souravdbbe68d2013-10-07 15:53:01 +0530402 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
Tom Rini4fe44702015-08-17 13:29:54 +0530403 spi_flash_copy_mmap(data, flash->memory_map + offset, len);
Poddar, Souravdbbe68d2013-10-07 15:53:01 +0530404 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
Poddar, Sourav47e21e02013-11-14 21:01:15 +0530405 spi_release_bus(flash->spi);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530406 return 0;
407 }
408
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530409 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
Jagannadha Sutradharudu Teki9ad01752014-02-04 21:36:13 +0530410 cmd = calloc(1, cmdsz);
411 if (!cmd) {
412 debug("SF: Failed to allocate cmd\n");
413 return -ENOMEM;
414 }
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530415
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530416 cmd[0] = flash->read_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530417 while (len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530418 read_addr = offset;
419
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530420#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530421 if (flash->dual_flash > SF_SINGLE_FLASH)
422 spi_flash_dual_flash(flash, &read_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530423#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530424#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530425 bank_sel = spi_flash_bank(flash, read_addr);
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530426 if (bank_sel < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530427 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530428#endif
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530429 remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
430 (bank_sel + 1)) - offset;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530431 if (len < remain_len)
432 read_len = len;
433 else
434 read_len = remain_len;
435
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530436 spi_flash_addr(read_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530437
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530438 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530439 if (ret < 0) {
440 debug("SF: read failed\n");
441 break;
442 }
443
444 offset += read_len;
445 len -= read_len;
446 data += read_len;
447 }
448
Marek Vasutd774c482014-07-12 18:11:31 +0530449 free(cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530450 return ret;
451}
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530452
453#ifdef CONFIG_SPI_FLASH_SST
454static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
455{
456 int ret;
457 u8 cmd[4] = {
458 CMD_SST_BP,
459 offset >> 16,
460 offset >> 8,
461 offset,
462 };
463
464 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
465 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
466
467 ret = spi_flash_cmd_write_enable(flash);
468 if (ret)
469 return ret;
470
471 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
472 if (ret)
473 return ret;
474
475 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
476}
477
478int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
479 const void *buf)
480{
481 size_t actual, cmd_len;
482 int ret;
483 u8 cmd[4];
484
485 ret = spi_claim_bus(flash->spi);
486 if (ret) {
487 debug("SF: Unable to claim SPI bus\n");
488 return ret;
489 }
490
491 /* If the data is not word aligned, write out leading single byte */
492 actual = offset % 2;
493 if (actual) {
494 ret = sst_byte_write(flash, offset, buf);
495 if (ret)
496 goto done;
497 }
498 offset += actual;
499
500 ret = spi_flash_cmd_write_enable(flash);
501 if (ret)
502 goto done;
503
504 cmd_len = 4;
505 cmd[0] = CMD_SST_AAI_WP;
506 cmd[1] = offset >> 16;
507 cmd[2] = offset >> 8;
508 cmd[3] = offset;
509
510 for (; actual < len - 1; actual += 2) {
511 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
512 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
513 cmd[0], offset);
514
515 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
516 buf + actual, 2);
517 if (ret) {
518 debug("SF: sst word program failed\n");
519 break;
520 }
521
522 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
523 if (ret)
524 break;
525
526 cmd_len = 1;
527 offset += 2;
528 }
529
530 if (!ret)
531 ret = spi_flash_cmd_write_disable(flash);
532
533 /* If there is a single trailing byte, write it out */
534 if (!ret && actual != len)
535 ret = sst_byte_write(flash, offset, buf + actual);
536
537 done:
538 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
539 ret ? "failure" : "success", len, offset - actual);
540
541 spi_release_bus(flash->spi);
542 return ret;
543}
Bin Mengfcbfc172014-12-12 19:36:13 +0530544
545int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
546 const void *buf)
547{
548 size_t actual;
549 int ret;
550
551 ret = spi_claim_bus(flash->spi);
552 if (ret) {
553 debug("SF: Unable to claim SPI bus\n");
554 return ret;
555 }
556
557 for (actual = 0; actual < len; actual++) {
558 ret = sst_byte_write(flash, offset, buf + actual);
559 if (ret) {
560 debug("SF: sst byte program failed\n");
561 break;
562 }
563 offset++;
564 }
565
566 if (!ret)
567 ret = spi_flash_cmd_write_disable(flash);
568
569 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
570 ret ? "failure" : "success", len, offset - actual);
571
572 spi_release_bus(flash->spi);
573 return ret;
574}
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530575#endif