blob: 384224d75a74b6dc4b51ce3de32c86043fe9cb85 [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>
Fabio Estevamd9709692015-11-05 12:43:41 -020018#include <linux/log2.h>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053019
Jagannadha Sutradharudu Tekiac8242d2013-09-26 16:00:15 +053020#include "sf_internal.h"
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053021
22static void spi_flash_addr(u32 addr, u8 *cmd)
23{
24 /* cmd[0] is actual command */
25 cmd[1] = addr >> 16;
26 cmd[2] = addr >> 8;
27 cmd[3] = addr >> 0;
28}
29
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053030int spi_flash_cmd_read_status(struct spi_flash *flash, u8 *rs)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053031{
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053032 int ret;
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053033 u8 cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053034
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053035 cmd = CMD_READ_STATUS;
36 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053037 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053038 debug("SF: fail to read status register\n");
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053039 return ret;
40 }
41
42 return 0;
43}
44
Jagan Teki853ef3e2015-09-29 16:54:31 +053045static int read_fsr(struct spi_flash *flash, u8 *fsr)
46{
47 int ret;
48 const u8 cmd = CMD_FLAG_STATUS;
49
50 ret = spi_flash_read_common(flash, &cmd, 1, fsr, 1);
51 if (ret < 0) {
52 debug("SF: fail to read flag status register\n");
53 return ret;
54 }
55
56 return 0;
57}
58
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +053059int spi_flash_cmd_write_status(struct spi_flash *flash, u8 ws)
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053060{
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053061 u8 cmd;
62 int ret;
63
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053064 cmd = CMD_WRITE_STATUS;
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +053065 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053066 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053067 debug("SF: fail to write status register\n");
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053068 return ret;
69 }
70
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053071 return 0;
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053072}
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053073
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053074#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053075int spi_flash_cmd_read_config(struct spi_flash *flash, u8 *rc)
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053076{
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053077 int ret;
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053078 u8 cmd;
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053079
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053080 cmd = CMD_READ_CONFIG;
81 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053082 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053083 debug("SF: fail to read config register\n");
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053084 return ret;
85 }
86
87 return 0;
88}
89
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053090int spi_flash_cmd_write_config(struct spi_flash *flash, u8 wc)
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053091{
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053092 u8 data[2];
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053093 u8 cmd;
94 int ret;
95
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053096 ret = spi_flash_cmd_read_status(flash, &data[0]);
97 if (ret < 0)
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053098 return ret;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053099
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +0530100 cmd = CMD_WRITE_STATUS;
101 data[1] = wc;
102 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
103 if (ret) {
104 debug("SF: fail to write config register\n");
105 return ret;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +0530106 }
107
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +0530108 return 0;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +0530109}
110#endif
111
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530112#ifdef CONFIG_SPI_FLASH_BAR
Jagan Teki5088b072015-09-02 11:39:48 +0530113static int spi_flash_write_bank(struct spi_flash *flash, u32 offset)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530114{
Jagan Teki5088b072015-09-02 11:39:48 +0530115 u8 cmd, bank_sel;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530116 int ret;
117
Jagan Teki5088b072015-09-02 11:39:48 +0530118 bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
119 if (bank_sel == flash->bank_curr)
120 goto bar_end;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530121
122 cmd = flash->bank_write_cmd;
123 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
124 if (ret < 0) {
125 debug("SF: fail to write bank register\n");
126 return ret;
127 }
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530128
Jagan Teki5088b072015-09-02 11:39:48 +0530129bar_end:
130 flash->bank_curr = bank_sel;
131 return flash->bank_curr;
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530132}
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530133#endif
134
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530135#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-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 Tekib47b7412014-01-07 00:11:35 +0530147 case SF_DUAL_PARALLEL_FLASH:
148 *addr >>= flash->shift;
149 break;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530150 default:
151 debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
152 break;
153 }
154}
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530155#endif
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530156
Jagan Teki853ef3e2015-09-29 16:54:31 +0530157static int spi_flash_sr_ready(struct spi_flash *flash)
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530158{
Jagan Teki5181dd92015-09-02 11:39:50 +0530159 u8 sr;
Jagan Teki853ef3e2015-09-29 16:54:31 +0530160 int ret;
161
162 ret = spi_flash_cmd_read_status(flash, &sr);
163 if (ret < 0)
164 return ret;
165
166 return !(sr & STATUS_WIP);
167}
168
169static int spi_flash_fsr_ready(struct spi_flash *flash)
170{
171 u8 fsr;
172 int ret;
173
174 ret = read_fsr(flash, &fsr);
175 if (ret < 0)
176 return ret;
177
178 return fsr & STATUS_PEC;
179}
180
181static int spi_flash_ready(struct spi_flash *flash)
182{
183 int sr, fsr;
184
185 sr = spi_flash_sr_ready(flash);
186 if (sr < 0)
187 return sr;
188
189 fsr = 1;
190 if (flash->flags & SNOR_F_USE_FSR) {
191 fsr = spi_flash_fsr_ready(flash);
192 if (fsr < 0)
193 return fsr;
194 }
195
196 return sr && fsr;
197}
198
199int spi_flash_cmd_wait_ready(struct spi_flash *flash, unsigned long timeout)
200{
Jagan Teki5181dd92015-09-02 11:39:50 +0530201 int timebase, ret;
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530202
Jagan Teki5181dd92015-09-02 11:39:50 +0530203 timebase = get_timer(0);
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530204
Jagan Teki5181dd92015-09-02 11:39:50 +0530205 while (get_timer(timebase) < timeout) {
Jagan Teki853ef3e2015-09-29 16:54:31 +0530206 ret = spi_flash_ready(flash);
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530207 if (ret < 0)
208 return ret;
Jagan Teki853ef3e2015-09-29 16:54:31 +0530209 if (ret)
Jagan Teki5181dd92015-09-02 11:39:50 +0530210 return 0;
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530211 }
212
Jagan Teki5181dd92015-09-02 11:39:50 +0530213 printf("SF: Timeout!\n");
214
215 return -ETIMEDOUT;
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530216}
217
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530218int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
219 size_t cmd_len, const void *buf, size_t buf_len)
220{
221 struct spi_slave *spi = flash->spi;
222 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
223 int ret;
224
225 if (buf == NULL)
226 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
227
228 ret = spi_claim_bus(flash->spi);
229 if (ret) {
230 debug("SF: unable to claim SPI bus\n");
231 return ret;
232 }
233
234 ret = spi_flash_cmd_write_enable(flash);
235 if (ret < 0) {
236 debug("SF: enabling write failed\n");
237 return ret;
238 }
239
240 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
241 if (ret < 0) {
242 debug("SF: write cmd failed\n");
243 return ret;
244 }
245
246 ret = spi_flash_cmd_wait_ready(flash, timeout);
247 if (ret < 0) {
248 debug("SF: write %s timed out\n",
249 timeout == SPI_FLASH_PROG_TIMEOUT ?
250 "program" : "page erase");
251 return ret;
252 }
253
254 spi_release_bus(spi);
255
256 return ret;
257}
258
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530259int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530260{
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530261 u32 erase_size, erase_addr;
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530262 u8 cmd[SPI_FLASH_CMD_LEN];
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530263 int ret = -1;
264
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530265 erase_size = flash->erase_size;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530266 if (offset % erase_size || len % erase_size) {
267 debug("SF: Erase offset/length not multiple of erase size\n");
268 return -1;
269 }
270
Bin Meng66528f62015-11-13 02:46:26 -0800271 if (flash->flash_is_locked) {
272 if (flash->flash_is_locked(flash, offset, len) > 0) {
273 printf("offset 0x%x is protected and cannot be erased\n",
274 offset);
275 return -EINVAL;
276 }
Fabio Estevam1cd87612015-11-05 12:43:42 -0200277 }
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
Jagan Teki5088b072015-09-02 11:39:48 +0530288 ret = spi_flash_write_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
Bin Meng66528f62015-11-13 02:46:26 -0800321 if (flash->flash_is_locked) {
322 if (flash->flash_is_locked(flash, offset, len) > 0) {
323 printf("offset 0x%x is protected and cannot be written\n",
324 offset);
325 return -EINVAL;
326 }
Fabio Estevam1cd87612015-11-05 12:43:42 -0200327 }
328
Jagannadha Sutradharudu Tekie0ebabc2014-01-11 15:13:11 +0530329 cmd[0] = flash->write_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530330 for (actual = 0; actual < len; actual += chunk_len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530331 write_addr = offset;
332
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530333#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530334 if (flash->dual_flash > SF_SINGLE_FLASH)
335 spi_flash_dual_flash(flash, &write_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530336#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530337#ifdef CONFIG_SPI_FLASH_BAR
Jagan Teki5088b072015-09-02 11:39:48 +0530338 ret = spi_flash_write_bank(flash, write_addr);
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530339 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530340 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530341#endif
342 byte_addr = offset % page_size;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900343 chunk_len = min(len - actual, (size_t)(page_size - byte_addr));
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530344
345 if (flash->spi->max_write_size)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900346 chunk_len = min(chunk_len,
347 (size_t)flash->spi->max_write_size);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530348
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530349 spi_flash_addr(write_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530350
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +0530351 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530352 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
353
354 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
355 buf + actual, chunk_len);
356 if (ret < 0) {
357 debug("SF: write failed\n");
358 break;
359 }
360
361 offset += chunk_len;
362 }
363
364 return ret;
365}
366
367int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
368 size_t cmd_len, void *data, size_t data_len)
369{
370 struct spi_slave *spi = flash->spi;
371 int ret;
372
373 ret = spi_claim_bus(flash->spi);
374 if (ret) {
375 debug("SF: unable to claim SPI bus\n");
376 return ret;
377 }
378
379 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
380 if (ret < 0) {
381 debug("SF: read cmd failed\n");
382 return ret;
383 }
384
385 spi_release_bus(spi);
386
387 return ret;
388}
389
Tom Rini4fe44702015-08-17 13:29:54 +0530390void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len)
391{
392 memcpy(data, offset, len);
393}
394
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530395int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530396 size_t len, void *data)
397{
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530398 u8 *cmd, cmdsz;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530399 u32 remain_len, read_len, read_addr;
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530400 int bank_sel = 0;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530401 int ret = -1;
402
403 /* Handle memory-mapped SPI */
404 if (flash->memory_map) {
Poddar, Sourav47e21e02013-11-14 21:01:15 +0530405 ret = spi_claim_bus(flash->spi);
406 if (ret) {
407 debug("SF: unable to claim SPI bus\n");
408 return ret;
409 }
Poddar, Souravdbbe68d2013-10-07 15:53:01 +0530410 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP);
Tom Rini4fe44702015-08-17 13:29:54 +0530411 spi_flash_copy_mmap(data, flash->memory_map + offset, len);
Poddar, Souravdbbe68d2013-10-07 15:53:01 +0530412 spi_xfer(flash->spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
Poddar, Sourav47e21e02013-11-14 21:01:15 +0530413 spi_release_bus(flash->spi);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530414 return 0;
415 }
416
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530417 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
Jagannadha Sutradharudu Teki9ad01752014-02-04 21:36:13 +0530418 cmd = calloc(1, cmdsz);
419 if (!cmd) {
420 debug("SF: Failed to allocate cmd\n");
421 return -ENOMEM;
422 }
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530423
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530424 cmd[0] = flash->read_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530425 while (len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530426 read_addr = offset;
427
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530428#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530429 if (flash->dual_flash > SF_SINGLE_FLASH)
430 spi_flash_dual_flash(flash, &read_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530431#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530432#ifdef CONFIG_SPI_FLASH_BAR
Jagan Teki5088b072015-09-02 11:39:48 +0530433 ret = spi_flash_write_bank(flash, read_addr);
434 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530435 return ret;
Jagan Teki5088b072015-09-02 11:39:48 +0530436 bank_sel = flash->bank_curr;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530437#endif
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530438 remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
439 (bank_sel + 1)) - offset;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530440 if (len < remain_len)
441 read_len = len;
442 else
443 read_len = remain_len;
444
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530445 spi_flash_addr(read_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530446
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530447 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530448 if (ret < 0) {
449 debug("SF: read failed\n");
450 break;
451 }
452
453 offset += read_len;
454 len -= read_len;
455 data += read_len;
456 }
457
Marek Vasutd774c482014-07-12 18:11:31 +0530458 free(cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530459 return ret;
460}
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530461
462#ifdef CONFIG_SPI_FLASH_SST
463static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
464{
465 int ret;
466 u8 cmd[4] = {
467 CMD_SST_BP,
468 offset >> 16,
469 offset >> 8,
470 offset,
471 };
472
473 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
474 spi_w8r8(flash->spi, CMD_READ_STATUS), buf, cmd[0], offset);
475
476 ret = spi_flash_cmd_write_enable(flash);
477 if (ret)
478 return ret;
479
480 ret = spi_flash_cmd_write(flash->spi, cmd, sizeof(cmd), buf, 1);
481 if (ret)
482 return ret;
483
484 return spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
485}
486
487int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
488 const void *buf)
489{
490 size_t actual, cmd_len;
491 int ret;
492 u8 cmd[4];
493
494 ret = spi_claim_bus(flash->spi);
495 if (ret) {
496 debug("SF: Unable to claim SPI bus\n");
497 return ret;
498 }
499
500 /* If the data is not word aligned, write out leading single byte */
501 actual = offset % 2;
502 if (actual) {
503 ret = sst_byte_write(flash, offset, buf);
504 if (ret)
505 goto done;
506 }
507 offset += actual;
508
509 ret = spi_flash_cmd_write_enable(flash);
510 if (ret)
511 goto done;
512
513 cmd_len = 4;
514 cmd[0] = CMD_SST_AAI_WP;
515 cmd[1] = offset >> 16;
516 cmd[2] = offset >> 8;
517 cmd[3] = offset;
518
519 for (; actual < len - 1; actual += 2) {
520 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
521 spi_w8r8(flash->spi, CMD_READ_STATUS), buf + actual,
522 cmd[0], offset);
523
524 ret = spi_flash_cmd_write(flash->spi, cmd, cmd_len,
525 buf + actual, 2);
526 if (ret) {
527 debug("SF: sst word program failed\n");
528 break;
529 }
530
531 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
532 if (ret)
533 break;
534
535 cmd_len = 1;
536 offset += 2;
537 }
538
539 if (!ret)
540 ret = spi_flash_cmd_write_disable(flash);
541
542 /* If there is a single trailing byte, write it out */
543 if (!ret && actual != len)
544 ret = sst_byte_write(flash, offset, buf + actual);
545
546 done:
547 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
548 ret ? "failure" : "success", len, offset - actual);
549
550 spi_release_bus(flash->spi);
551 return ret;
552}
Bin Mengfcbfc172014-12-12 19:36:13 +0530553
554int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
555 const void *buf)
556{
557 size_t actual;
558 int ret;
559
560 ret = spi_claim_bus(flash->spi);
561 if (ret) {
562 debug("SF: Unable to claim SPI bus\n");
563 return ret;
564 }
565
566 for (actual = 0; actual < len; actual++) {
567 ret = sst_byte_write(flash, offset, buf + actual);
568 if (ret) {
569 debug("SF: sst byte program failed\n");
570 break;
571 }
572 offset++;
573 }
574
575 if (!ret)
576 ret = spi_flash_cmd_write_disable(flash);
577
578 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
579 ret ? "failure" : "success", len, offset - actual);
580
581 spi_release_bus(flash->spi);
582 return ret;
583}
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530584#endif
Fabio Estevamd9709692015-11-05 12:43:41 -0200585
586#ifdef CONFIG_SPI_FLASH_STMICRO
587static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
588 u32 *len)
589{
590 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
591 int shift = ffs(mask) - 1;
592 int pow;
593
594 if (!(sr & mask)) {
595 /* No protection */
596 *ofs = 0;
597 *len = 0;
598 } else {
599 pow = ((sr & mask) ^ mask) >> shift;
600 *len = flash->size >> pow;
601 *ofs = flash->size - *len;
602 }
603}
604
605/*
606 * Return 1 if the entire region is locked, 0 otherwise
607 */
Fabio Estevam1cd87612015-11-05 12:43:42 -0200608static int stm_is_locked_sr(struct spi_flash *flash, u32 ofs, u32 len,
Fabio Estevamd9709692015-11-05 12:43:41 -0200609 u8 sr)
610{
611 loff_t lock_offs;
612 u32 lock_len;
613
614 stm_get_locked_range(flash, sr, &lock_offs, &lock_len);
615
616 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
617}
618
619/*
620 * Check if a region of the flash is (completely) locked. See stm_lock() for
621 * more info.
622 *
623 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
624 * negative on errors.
625 */
Fabio Estevam1cd87612015-11-05 12:43:42 -0200626int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
Fabio Estevamd9709692015-11-05 12:43:41 -0200627{
628 int status;
629 u8 sr;
630
631 status = spi_flash_cmd_read_status(flash, &sr);
632 if (status < 0)
633 return status;
634
635 return stm_is_locked_sr(flash, ofs, len, sr);
636}
637
638/*
639 * Lock a region of the flash. Compatible with ST Micro and similar flash.
640 * Supports only the block protection bits BP{0,1,2} in the status register
641 * (SR). Does not support these features found in newer SR bitfields:
642 * - TB: top/bottom protect - only handle TB=0 (top protect)
643 * - SEC: sector/block protect - only handle SEC=0 (block protect)
644 * - CMP: complement protect - only support CMP=0 (range is not complemented)
645 *
646 * Sample table portion for 8MB flash (Winbond w25q64fw):
647 *
648 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
649 * --------------------------------------------------------------------------
650 * X | X | 0 | 0 | 0 | NONE | NONE
651 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
652 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
653 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
654 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
655 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
656 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
657 * X | X | 1 | 1 | 1 | 8 MB | ALL
658 *
659 * Returns negative on errors, 0 on success.
660 */
661int stm_lock(struct spi_flash *flash, u32 ofs, size_t len)
662{
663 u8 status_old, status_new;
664 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
665 u8 shift = ffs(mask) - 1, pow, val;
666
667 spi_flash_cmd_read_status(flash, &status_old);
668
669 /* SPI NOR always locks to the end */
670 if (ofs + len != flash->size) {
671 /* Does combined region extend to end? */
672 if (!stm_is_locked_sr(flash, ofs + len, flash->size - ofs - len,
673 status_old))
674 return -EINVAL;
675 len = flash->size - ofs;
676 }
677
678 /*
679 * Need smallest pow such that:
680 *
681 * 1 / (2^pow) <= (len / size)
682 *
683 * so (assuming power-of-2 size) we do:
684 *
685 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
686 */
687 pow = ilog2(flash->size) - ilog2(len);
688 val = mask - (pow << shift);
689 if (val & ~mask)
690 return -EINVAL;
691
692 /* Don't "lock" with no region! */
693 if (!(val & mask))
694 return -EINVAL;
695
696 status_new = (status_old & ~mask) | val;
697
698 /* Only modify protection if it will not unlock other areas */
699 if ((status_new & mask) <= (status_old & mask))
700 return -EINVAL;
701
702 spi_flash_cmd_write_status(flash, status_new);
703
704 return 0;
705}
706
707/*
708 * Unlock a region of the flash. See stm_lock() for more info
709 *
710 * Returns negative on errors, 0 on success.
711 */
712int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
713{
714 uint8_t status_old, status_new;
715 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
716 u8 shift = ffs(mask) - 1, pow, val;
717
718 spi_flash_cmd_read_status(flash, &status_old);
719
720 /* Cannot unlock; would unlock larger region than requested */
721 if (stm_is_locked_sr(flash, status_old, ofs - flash->erase_size,
722 flash->erase_size))
723 return -EINVAL;
724 /*
725 * Need largest pow such that:
726 *
727 * 1 / (2^pow) >= (len / size)
728 *
729 * so (assuming power-of-2 size) we do:
730 *
731 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
732 */
733 pow = ilog2(flash->size) - order_base_2(flash->size - (ofs + len));
734 if (ofs + len == flash->size) {
735 val = 0; /* fully unlocked */
736 } else {
737 val = mask - (pow << shift);
738 /* Some power-of-two sizes are not supported */
739 if (val & ~mask)
740 return -EINVAL;
741 }
742
743 status_new = (status_old & ~mask) | val;
744
745 /* Only modify protection if it will not lock other areas */
746 if ((status_new & mask) >= (status_old & mask))
747 return -EINVAL;
748
749 spi_flash_cmd_write_status(flash, status_new);
750
751 return 0;
752}
753#endif /* CONFIG_SPI_FLASH_STMICRO */