blob: 85cf22d42ecd08761a149639ddbc2dda47ea590a [file] [log] [blame]
Jagannadha Sutradharudu Teki4d5e29a2013-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 Teki0c88a842013-10-10 22:32:55 +05308 * SPDX-License-Identifier: GPL-2.0+
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +05309 */
10
11#include <common.h>
Jagannadha Sutradharudu Tekic6136aa2014-02-04 21:36:13 +053012#include <errno.h>
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +053013#include <malloc.h>
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053014#include <spi.h>
15#include <spi_flash.h>
16#include <watchdog.h>
17
Jagannadha Sutradharudu Teki898e76c2013-09-26 16:00:15 +053018#include "sf_internal.h"
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053019
20static void spi_flash_addr(u32 addr, u8 *cmd)
21{
22 /* cmd[0] is actual command */
23 cmd[1] = addr >> 16;
24 cmd[2] = addr >> 8;
25 cmd[3] = addr >> 0;
26}
27
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053028int spi_flash_cmd_read_status(struct spi_flash *flash, u8 *rs)
29{
30 int ret;
31 u8 cmd;
32
33 cmd = CMD_READ_STATUS;
34 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
35 if (ret < 0) {
36 debug("SF: fail to read status register\n");
37 return ret;
38 }
39
40 return 0;
41}
42
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +053043int spi_flash_cmd_write_status(struct spi_flash *flash, u8 ws)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053044{
45 u8 cmd;
46 int ret;
47
48 cmd = CMD_WRITE_STATUS;
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +053049 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053050 if (ret < 0) {
51 debug("SF: fail to write status register\n");
52 return ret;
53 }
54
55 return 0;
56}
57
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053058#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
59int spi_flash_cmd_read_config(struct spi_flash *flash, u8 *rc)
Jagannadha Sutradharudu Teki06795122013-12-26 14:13:36 +053060{
Jagannadha Sutradharudu Teki06795122013-12-26 14:13:36 +053061 int ret;
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053062 u8 cmd;
Jagannadha Sutradharudu Teki06795122013-12-26 14:13:36 +053063
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053064 cmd = CMD_READ_CONFIG;
65 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
Jagannadha Sutradharudu Teki06795122013-12-26 14:13:36 +053066 if (ret < 0) {
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053067 debug("SF: fail to read config register\n");
Jagannadha Sutradharudu Teki06795122013-12-26 14:13:36 +053068 return ret;
69 }
70
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053071 return 0;
Jagannadha Sutradharudu Teki06795122013-12-26 14:13:36 +053072}
Jagannadha Sutradharudu Teki06795122013-12-26 14:13:36 +053073
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053074int spi_flash_cmd_write_config(struct spi_flash *flash, u8 wc)
Jagannadha Sutradharudu Teki6cba6fd2013-12-23 15:47:48 +053075{
76 u8 data[2];
77 u8 cmd;
78 int ret;
79
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053080 ret = spi_flash_cmd_read_status(flash, &data[0]);
81 if (ret < 0)
Jagannadha Sutradharudu Teki6cba6fd2013-12-23 15:47:48 +053082 return ret;
Jagannadha Sutradharudu Teki6cba6fd2013-12-23 15:47:48 +053083
84 cmd = CMD_WRITE_STATUS;
Jagannadha Sutradharudu Teki9f4322f2013-12-30 22:16:23 +053085 data[1] = wc;
Jagannadha Sutradharudu Teki6cba6fd2013-12-23 15:47:48 +053086 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
87 if (ret) {
88 debug("SF: fail to write config register\n");
89 return ret;
90 }
91
92 return 0;
93}
Jagannadha Sutradharudu Tekid08a1ba2013-12-26 13:54:57 +053094#endif
95
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053096#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Teki532f2f12013-08-28 14:57:03 +053097static int spi_flash_cmd_bankaddr_write(struct spi_flash *flash, u8 bank_sel)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +053098{
99 u8 cmd;
100 int ret;
101
102 if (flash->bank_curr == bank_sel) {
103 debug("SF: not require to enable bank%d\n", bank_sel);
104 return 0;
105 }
106
107 cmd = flash->bank_write_cmd;
108 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
109 if (ret < 0) {
110 debug("SF: fail to write bank register\n");
111 return ret;
112 }
113 flash->bank_curr = bank_sel;
114
115 return 0;
116}
Jagannadha Sutradharudu Teki6152dd12013-10-08 23:26:47 +0530117
118static int spi_flash_bank(struct spi_flash *flash, u32 offset)
119{
120 u8 bank_sel;
121 int ret;
122
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530123 bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
Jagannadha Sutradharudu Teki6152dd12013-10-08 23:26:47 +0530124
125 ret = spi_flash_cmd_bankaddr_write(flash, bank_sel);
126 if (ret) {
127 debug("SF: fail to set bank%d\n", bank_sel);
128 return ret;
129 }
130
Jagannadha Sutradharudu Tekiab922242014-01-11 16:57:07 +0530131 return bank_sel;
Jagannadha Sutradharudu Teki6152dd12013-10-08 23:26:47 +0530132}
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530133#endif
134
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530135#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530136static void spi_flash_dual_flash(struct spi_flash *flash, u32 *addr)
137{
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;
143 } else {
144 flash->spi->flags &= ~SPI_XFER_U_PAGE;
145 }
146 break;
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530147 case SF_DUAL_PARALLEL_FLASH:
148 *addr >>= flash->shift;
149 break;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530150 default:
151 debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
152 break;
153 }
154}
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530155#endif
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530156
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530157int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
158{
159 struct spi_slave *spi = flash->spi;
160 unsigned long timebase;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530161 unsigned long flags = SPI_XFER_BEGIN;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530162 int ret;
163 u8 status;
164 u8 check_status = 0x0;
165 u8 poll_bit = STATUS_WIP;
166 u8 cmd = flash->poll_cmd;
167
168 if (cmd == CMD_FLAG_STATUS) {
169 poll_bit = STATUS_PEC;
170 check_status = poll_bit;
171 }
172
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530173#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530174 if (spi->flags & SPI_XFER_U_PAGE)
175 flags |= SPI_XFER_U_PAGE;
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530176#endif
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530177 ret = spi_xfer(spi, 8, &cmd, NULL, flags);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530178 if (ret) {
179 debug("SF: fail to read %s status register\n",
180 cmd == CMD_READ_STATUS ? "read" : "flag");
181 return ret;
182 }
183
184 timebase = get_timer(0);
185 do {
186 WATCHDOG_RESET();
187
188 ret = spi_xfer(spi, 8, NULL, &status, 0);
189 if (ret)
190 return -1;
191
192 if ((status & poll_bit) == check_status)
193 break;
194
195 } while (get_timer(timebase) < timeout);
196
197 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
198
199 if ((status & poll_bit) == check_status)
200 return 0;
201
202 /* Timed out */
203 debug("SF: time out!\n");
204 return -1;
205}
206
207int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
208 size_t cmd_len, const void *buf, size_t buf_len)
209{
210 struct spi_slave *spi = flash->spi;
211 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
212 int ret;
213
214 if (buf == NULL)
215 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
216
217 ret = spi_claim_bus(flash->spi);
218 if (ret) {
219 debug("SF: unable to claim SPI bus\n");
220 return ret;
221 }
222
223 ret = spi_flash_cmd_write_enable(flash);
224 if (ret < 0) {
225 debug("SF: enabling write failed\n");
226 return ret;
227 }
228
229 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
230 if (ret < 0) {
231 debug("SF: write cmd failed\n");
232 return ret;
233 }
234
235 ret = spi_flash_cmd_wait_ready(flash, timeout);
236 if (ret < 0) {
237 debug("SF: write %s timed out\n",
238 timeout == SPI_FLASH_PROG_TIMEOUT ?
239 "program" : "page erase");
240 return ret;
241 }
242
243 spi_release_bus(spi);
244
245 return ret;
246}
247
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530248int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530249{
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530250 u32 erase_size, erase_addr;
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530251 u8 cmd[SPI_FLASH_CMD_LEN];
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530252 int ret = -1;
253
Jagannadha Sutradharudu Tekif4f51a82013-10-02 19:36:58 +0530254 erase_size = flash->erase_size;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530255 if (offset % erase_size || len % erase_size) {
256 debug("SF: Erase offset/length not multiple of erase size\n");
257 return -1;
258 }
259
Jagannadha Sutradharudu Tekif4f51a82013-10-02 19:36:58 +0530260 cmd[0] = flash->erase_cmd;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530261 while (len) {
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530262 erase_addr = offset;
263
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530264#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530265 if (flash->dual_flash > SF_SINGLE_FLASH)
266 spi_flash_dual_flash(flash, &erase_addr);
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530267#endif
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530268#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530269 ret = spi_flash_bank(flash, erase_addr);
Jagannadha Sutradharudu Teki6152dd12013-10-08 23:26:47 +0530270 if (ret < 0)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530271 return ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530272#endif
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530273 spi_flash_addr(erase_addr, cmd);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530274
275 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530276 cmd[2], cmd[3], erase_addr);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530277
278 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
279 if (ret < 0) {
280 debug("SF: erase failed\n");
281 break;
282 }
283
284 offset += erase_size;
285 len -= erase_size;
286 }
287
288 return ret;
289}
290
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530291int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530292 size_t len, const void *buf)
293{
294 unsigned long byte_addr, page_size;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530295 u32 write_addr;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530296 size_t chunk_len, actual;
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530297 u8 cmd[SPI_FLASH_CMD_LEN];
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530298 int ret = -1;
299
300 page_size = flash->page_size;
301
Jagannadha Sutradharudu Teki3163aaa2014-01-11 15:13:11 +0530302 cmd[0] = flash->write_cmd;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530303 for (actual = 0; actual < len; actual += chunk_len) {
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530304 write_addr = offset;
305
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530306#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530307 if (flash->dual_flash > SF_SINGLE_FLASH)
308 spi_flash_dual_flash(flash, &write_addr);
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530309#endif
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530310#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530311 ret = spi_flash_bank(flash, write_addr);
Jagannadha Sutradharudu Teki6152dd12013-10-08 23:26:47 +0530312 if (ret < 0)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530313 return ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530314#endif
315 byte_addr = offset % page_size;
316 chunk_len = min(len - actual, page_size - byte_addr);
317
318 if (flash->spi->max_write_size)
319 chunk_len = min(chunk_len, flash->spi->max_write_size);
320
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530321 spi_flash_addr(write_addr, cmd);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530322
Jagannadha Sutradharudu Teki2ba863f2014-01-12 21:38:21 +0530323 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530324 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
325
326 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
327 buf + actual, chunk_len);
328 if (ret < 0) {
329 debug("SF: write failed\n");
330 break;
331 }
332
333 offset += chunk_len;
334 }
335
336 return ret;
337}
338
339int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
340 size_t cmd_len, void *data, size_t data_len)
341{
342 struct spi_slave *spi = flash->spi;
343 int ret;
344
345 ret = spi_claim_bus(flash->spi);
346 if (ret) {
347 debug("SF: unable to claim SPI bus\n");
348 return ret;
349 }
350
351 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
352 if (ret < 0) {
353 debug("SF: read cmd failed\n");
354 return ret;
355 }
356
357 spi_release_bus(spi);
358
359 return ret;
360}
361
Jagannadha Sutradharudu Tekia5e81992013-10-02 19:38:49 +0530362int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530363 size_t len, void *data)
364{
Jagannadha Sutradharudu Tekiab922242014-01-11 16:57:07 +0530365 u8 *cmd, cmdsz;
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530366 u32 remain_len, read_len, read_addr;
Jagannadha Sutradharudu Tekiab922242014-01-11 16:57:07 +0530367 int bank_sel = 0;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530368 int ret = -1;
369
370 /* Handle memory-mapped SPI */
371 if (flash->memory_map) {
Poddar, Souravac5cce32013-11-14 21:01:15 +0530372 ret = spi_claim_bus(flash->spi);
373 if (ret) {
374 debug("SF: unable to claim SPI bus\n");
375 return ret;
376 }
Poddar, Sourav004f15b2013-10-07 15:53:01 +0530377 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530378 memcpy(data, flash->memory_map + offset, len);
Poddar, Sourav004f15b2013-10-07 15:53:01 +0530379 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
Poddar, Souravac5cce32013-11-14 21:01:15 +0530380 spi_release_bus(flash->spi);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530381 return 0;
382 }
383
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530384 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
Jagannadha Sutradharudu Tekic6136aa2014-02-04 21:36:13 +0530385 cmd = calloc(1, cmdsz);
386 if (!cmd) {
387 debug("SF: Failed to allocate cmd\n");
388 return -ENOMEM;
389 }
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530390
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530391 cmd[0] = flash->read_cmd;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530392 while (len) {
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530393 read_addr = offset;
394
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530395#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530396 if (flash->dual_flash > SF_SINGLE_FLASH)
397 spi_flash_dual_flash(flash, &read_addr);
Jagannadha Sutradharudu Tekib902e072014-01-11 15:25:04 +0530398#endif
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530399#ifdef CONFIG_SPI_FLASH_BAR
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530400 bank_sel = spi_flash_bank(flash, read_addr);
Jagannadha Sutradharudu Tekiab922242014-01-11 16:57:07 +0530401 if (bank_sel < 0)
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530402 return ret;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530403#endif
Jagannadha Sutradharudu Teki056fbc72014-01-07 00:11:35 +0530404 remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
405 (bank_sel + 1)) - offset;
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530406 if (len < remain_len)
407 read_len = len;
408 else
409 read_len = remain_len;
410
Jagannadha Sutradharudu Tekif77f4692014-01-12 21:40:11 +0530411 spi_flash_addr(read_addr, cmd);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530412
Jagannadha Sutradharudu Tekiff063ed2014-01-11 16:50:45 +0530413 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530414 if (ret < 0) {
415 debug("SF: read failed\n");
416 break;
417 }
418
419 offset += read_len;
420 len -= read_len;
421 data += read_len;
422 }
423
Marek Vasuta52a1782014-07-12 18:11:31 +0530424 free(cmd);
Jagannadha Sutradharudu Teki4d5e29a2013-08-29 19:01:56 +0530425 return ret;
426}
Jagannadha Sutradharudu Teki10ca45d2013-10-02 19:34:53 +0530427
428#ifdef CONFIG_SPI_FLASH_SST
429static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
430{
431 int ret;
432 u8 cmd[4] = {
433 CMD_SST_BP,
434 offset >> 16,
435 offset >> 8,
436 offset,
437 };
438
439 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
440 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
441
442 ret = spi_flash_cmd_write_enable(flash);
443 if (ret)
444 return ret;
445
446 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
447 if (ret)
448 return ret;
449
450 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
451}
452
453int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
454 const void *buf)
455{
456 size_t actual, cmd_len;
457 int ret;
458 u8 cmd[4];
459
460 ret = spi_claim_bus(flash->spi);
461 if (ret) {
462 debug("SF: Unable to claim SPI bus\n");
463 return ret;
464 }
465
466 /* If the data is not word aligned, write out leading single byte */
467 actual = offset % 2;
468 if (actual) {
469 ret = sst_byte_write(flash, offset, buf);
470 if (ret)
471 goto done;
472 }
473 offset += actual;
474
475 ret = spi_flash_cmd_write_enable(flash);
476 if (ret)
477 goto done;
478
479 cmd_len = 4;
480 cmd[0] = CMD_SST_AAI_WP;
481 cmd[1] = offset >> 16;
482 cmd[2] = offset >> 8;
483 cmd[3] = offset;
484
485 for (; actual < len - 1; actual += 2) {
486 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
487 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
488 cmd[0], offset);
489
490 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
491 buf + actual, 2);
492 if (ret) {
493 debug("SF: sst word program failed\n");
494 break;
495 }
496
497 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
498 if (ret)
499 break;
500
501 cmd_len = 1;
502 offset += 2;
503 }
504
505 if (!ret)
506 ret = spi_flash_cmd_write_disable(flash);
507
508 /* If there is a single trailing byte, write it out */
509 if (!ret && actual != len)
510 ret = sst_byte_write(flash, offset, buf + actual);
511
512 done:
513 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
514 ret ? "failure" : "success", len, offset - actual);
515
516 spi_release_bus(flash->spi);
517 return ret;
518}
519#endif