blob: 51e28bf07b87cd02800aa826a76b91b6dc8a9b65 [file] [log] [blame]
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05301/*
Jagan Tekifefdfd22015-12-06 23:29:02 +05302 * SPI Flash Core
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05303 *
Jagan Tekifefdfd22015-12-06 23:29:02 +05304 * Copyright (C) 2015 Jagan Teki <jteki@openedev.com>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05305 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
Jagan Tekifefdfd22015-12-06 23:29:02 +05306 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
7 * Copyright (C) 2008 Atmel Corporation
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05308 *
Jagannadha Sutradharudu Tekid1452702013-10-10 22:32:55 +05309 * SPDX-License-Identifier: GPL-2.0+
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053010 */
11
12#include <common.h>
Jagannadha Sutradharudu Teki9ad01752014-02-04 21:36:13 +053013#include <errno.h>
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +053014#include <malloc.h>
Jagan Tekie6401d82015-12-11 21:36:34 +053015#include <mapmem.h>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053016#include <spi.h>
17#include <spi_flash.h>
Fabio Estevamd9709692015-11-05 12:43:41 -020018#include <linux/log2.h>
Mugunthan V N4e4b58d2016-02-15 15:31:39 +053019#include <dma.h>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053020
Jagannadha Sutradharudu Tekiac8242d2013-09-26 16:00:15 +053021#include "sf_internal.h"
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053022
Jagan Tekie6401d82015-12-11 21:36:34 +053023DECLARE_GLOBAL_DATA_PTR;
24
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053025static void spi_flash_addr(u32 addr, u8 *cmd)
26{
27 /* cmd[0] is actual command */
28 cmd[1] = addr >> 16;
29 cmd[2] = addr >> 8;
30 cmd[3] = addr >> 0;
31}
32
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053033static int read_sr(struct spi_flash *flash, u8 *rs)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053034{
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053035 int ret;
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053036 u8 cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053037
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053038 cmd = CMD_READ_STATUS;
39 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053040 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053041 debug("SF: fail to read status register\n");
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053042 return ret;
43 }
44
45 return 0;
46}
47
Jagan Teki853ef3e2015-09-29 16:54:31 +053048static int read_fsr(struct spi_flash *flash, u8 *fsr)
49{
50 int ret;
51 const u8 cmd = CMD_FLAG_STATUS;
52
53 ret = spi_flash_read_common(flash, &cmd, 1, fsr, 1);
54 if (ret < 0) {
55 debug("SF: fail to read flag status register\n");
56 return ret;
57 }
58
59 return 0;
60}
61
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053062static int write_sr(struct spi_flash *flash, u8 ws)
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053063{
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053064 u8 cmd;
65 int ret;
66
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053067 cmd = CMD_WRITE_STATUS;
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +053068 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053069 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053070 debug("SF: fail to write status register\n");
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053071 return ret;
72 }
73
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053074 return 0;
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053075}
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053076
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053077#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053078static int read_cr(struct spi_flash *flash, u8 *rc)
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053079{
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053080 int ret;
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053081 u8 cmd;
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053082
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053083 cmd = CMD_READ_CONFIG;
84 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053085 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053086 debug("SF: fail to read config register\n");
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053087 return ret;
88 }
89
90 return 0;
91}
92
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053093static int write_cr(struct spi_flash *flash, u8 wc)
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053094{
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053095 u8 data[2];
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053096 u8 cmd;
97 int ret;
98
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053099 ret = read_sr(flash, &data[0]);
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +0530100 if (ret < 0)
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +0530101 return ret;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +0530102
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +0530103 cmd = CMD_WRITE_STATUS;
104 data[1] = wc;
105 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
106 if (ret) {
107 debug("SF: fail to write config register\n");
108 return ret;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +0530109 }
110
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +0530111 return 0;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +0530112}
113#endif
114
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530115#ifdef CONFIG_SPI_FLASH_BAR
Lukasz Majewski896de8a2017-09-25 12:40:08 +0200116/*
117 * This "clean_bar" is necessary in a situation when one was accessing
118 * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
119 *
120 * After it the BA24 bit shall be cleared to allow access to correct
121 * memory region after SW reset (by calling "reset" command).
122 *
123 * Otherwise, the BA24 bit may be left set and then after reset, the
124 * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
125 */
126static int clean_bar(struct spi_flash *flash)
127{
128 u8 cmd, bank_sel = 0;
129
130 if (flash->bank_curr == 0)
131 return 0;
132 cmd = flash->bank_write_cmd;
133
134 return spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
135}
136
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530137static int write_bar(struct spi_flash *flash, u32 offset)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530138{
Jagan Teki5088b072015-09-02 11:39:48 +0530139 u8 cmd, bank_sel;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530140 int ret;
141
Jagan Teki5088b072015-09-02 11:39:48 +0530142 bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
143 if (bank_sel == flash->bank_curr)
144 goto bar_end;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530145
146 cmd = flash->bank_write_cmd;
147 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
148 if (ret < 0) {
149 debug("SF: fail to write bank register\n");
150 return ret;
151 }
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530152
Jagan Teki5088b072015-09-02 11:39:48 +0530153bar_end:
154 flash->bank_curr = bank_sel;
155 return flash->bank_curr;
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530156}
Jagan Tekif9a026d2015-11-04 00:27:35 +0530157
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530158static int read_bar(struct spi_flash *flash, const struct spi_flash_info *info)
Jagan Tekif9a026d2015-11-04 00:27:35 +0530159{
160 u8 curr_bank = 0;
161 int ret;
162
163 if (flash->size <= SPI_FLASH_16MB_BOUN)
Jagan Tekida042a02015-12-13 23:10:33 +0530164 goto bar_end;
Jagan Tekif9a026d2015-11-04 00:27:35 +0530165
Jagan Teki77ae47b2016-10-30 23:16:10 +0530166 switch (JEDEC_MFR(info)) {
Jagan Tekif9a026d2015-11-04 00:27:35 +0530167 case SPI_FLASH_CFI_MFR_SPANSION:
168 flash->bank_read_cmd = CMD_BANKADDR_BRRD;
169 flash->bank_write_cmd = CMD_BANKADDR_BRWR;
Jagan Tekie37a3622015-11-20 13:00:15 +0530170 break;
Jagan Tekif9a026d2015-11-04 00:27:35 +0530171 default:
172 flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
173 flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
174 }
175
176 ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
177 &curr_bank, 1);
178 if (ret) {
179 debug("SF: fail to read bank addr register\n");
180 return ret;
181 }
182
Jagan Tekida042a02015-12-13 23:10:33 +0530183bar_end:
Jagan Tekif9a026d2015-11-04 00:27:35 +0530184 flash->bank_curr = curr_bank;
185 return 0;
186}
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530187#endif
188
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530189#ifdef CONFIG_SF_DUAL_FLASH
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530190static void spi_flash_dual(struct spi_flash *flash, u32 *addr)
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530191{
192 switch (flash->dual_flash) {
193 case SF_DUAL_STACKED_FLASH:
194 if (*addr >= (flash->size >> 1)) {
195 *addr -= flash->size >> 1;
Jagan Teki24aa01b2016-10-30 23:16:26 +0530196 flash->flags |= SNOR_F_USE_UPAGE;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530197 } else {
Jagan Teki24aa01b2016-10-30 23:16:26 +0530198 flash->flags &= ~SNOR_F_USE_UPAGE;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530199 }
200 break;
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530201 case SF_DUAL_PARALLEL_FLASH:
202 *addr >>= flash->shift;
203 break;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530204 default:
205 debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
206 break;
207 }
208}
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530209#endif
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530210
Jagan Teki853ef3e2015-09-29 16:54:31 +0530211static int spi_flash_sr_ready(struct spi_flash *flash)
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530212{
Jagan Teki5181dd92015-09-02 11:39:50 +0530213 u8 sr;
Jagan Teki853ef3e2015-09-29 16:54:31 +0530214 int ret;
215
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530216 ret = read_sr(flash, &sr);
Jagan Teki853ef3e2015-09-29 16:54:31 +0530217 if (ret < 0)
218 return ret;
219
220 return !(sr & STATUS_WIP);
221}
222
223static int spi_flash_fsr_ready(struct spi_flash *flash)
224{
225 u8 fsr;
226 int ret;
227
228 ret = read_fsr(flash, &fsr);
229 if (ret < 0)
230 return ret;
231
232 return fsr & STATUS_PEC;
233}
234
235static int spi_flash_ready(struct spi_flash *flash)
236{
237 int sr, fsr;
238
239 sr = spi_flash_sr_ready(flash);
240 if (sr < 0)
241 return sr;
242
243 fsr = 1;
244 if (flash->flags & SNOR_F_USE_FSR) {
245 fsr = spi_flash_fsr_ready(flash);
246 if (fsr < 0)
247 return fsr;
248 }
249
250 return sr && fsr;
251}
252
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530253static int spi_flash_wait_till_ready(struct spi_flash *flash,
254 unsigned long timeout)
Jagan Teki853ef3e2015-09-29 16:54:31 +0530255{
Stephen Warren07a6e382016-04-04 11:03:52 -0600256 unsigned long timebase;
257 int ret;
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530258
Jagan Teki5181dd92015-09-02 11:39:50 +0530259 timebase = get_timer(0);
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530260
Jagan Teki5181dd92015-09-02 11:39:50 +0530261 while (get_timer(timebase) < timeout) {
Jagan Teki853ef3e2015-09-29 16:54:31 +0530262 ret = spi_flash_ready(flash);
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530263 if (ret < 0)
264 return ret;
Jagan Teki853ef3e2015-09-29 16:54:31 +0530265 if (ret)
Jagan Teki5181dd92015-09-02 11:39:50 +0530266 return 0;
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530267 }
268
Jagan Teki5181dd92015-09-02 11:39:50 +0530269 printf("SF: Timeout!\n");
270
271 return -ETIMEDOUT;
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530272}
273
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530274int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
275 size_t cmd_len, const void *buf, size_t buf_len)
276{
277 struct spi_slave *spi = flash->spi;
278 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
279 int ret;
280
281 if (buf == NULL)
282 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
283
Jagan Teki7d005992015-12-12 11:51:57 +0530284 ret = spi_claim_bus(spi);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530285 if (ret) {
286 debug("SF: unable to claim SPI bus\n");
287 return ret;
288 }
289
290 ret = spi_flash_cmd_write_enable(flash);
291 if (ret < 0) {
292 debug("SF: enabling write failed\n");
293 return ret;
294 }
295
296 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
297 if (ret < 0) {
298 debug("SF: write cmd failed\n");
299 return ret;
300 }
301
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530302 ret = spi_flash_wait_till_ready(flash, timeout);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530303 if (ret < 0) {
304 debug("SF: write %s timed out\n",
305 timeout == SPI_FLASH_PROG_TIMEOUT ?
306 "program" : "page erase");
307 return ret;
308 }
309
310 spi_release_bus(spi);
311
312 return ret;
313}
314
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530315int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530316{
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530317 u32 erase_size, erase_addr;
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530318 u8 cmd[SPI_FLASH_CMD_LEN];
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530319 int ret = -1;
320
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530321 erase_size = flash->erase_size;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530322 if (offset % erase_size || len % erase_size) {
323 debug("SF: Erase offset/length not multiple of erase size\n");
324 return -1;
325 }
326
Bin Meng66528f62015-11-13 02:46:26 -0800327 if (flash->flash_is_locked) {
328 if (flash->flash_is_locked(flash, offset, len) > 0) {
329 printf("offset 0x%x is protected and cannot be erased\n",
330 offset);
331 return -EINVAL;
332 }
Fabio Estevam1cd87612015-11-05 12:43:42 -0200333 }
334
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530335 cmd[0] = flash->erase_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530336 while (len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530337 erase_addr = offset;
338
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530339#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530340 if (flash->dual_flash > SF_SINGLE_FLASH)
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530341 spi_flash_dual(flash, &erase_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530342#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530343#ifdef CONFIG_SPI_FLASH_BAR
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530344 ret = write_bar(flash, erase_addr);
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530345 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530346 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530347#endif
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530348 spi_flash_addr(erase_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530349
350 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530351 cmd[2], cmd[3], erase_addr);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530352
353 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
354 if (ret < 0) {
355 debug("SF: erase failed\n");
356 break;
357 }
358
359 offset += erase_size;
360 len -= erase_size;
361 }
362
Lukasz Majewski896de8a2017-09-25 12:40:08 +0200363#ifdef CONFIG_SPI_FLASH_BAR
364 ret = clean_bar(flash);
365#endif
366
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530367 return ret;
368}
369
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530370int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530371 size_t len, const void *buf)
372{
Jagan Teki7d005992015-12-12 11:51:57 +0530373 struct spi_slave *spi = flash->spi;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530374 unsigned long byte_addr, page_size;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530375 u32 write_addr;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530376 size_t chunk_len, actual;
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530377 u8 cmd[SPI_FLASH_CMD_LEN];
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530378 int ret = -1;
379
380 page_size = flash->page_size;
381
Bin Meng66528f62015-11-13 02:46:26 -0800382 if (flash->flash_is_locked) {
383 if (flash->flash_is_locked(flash, offset, len) > 0) {
384 printf("offset 0x%x is protected and cannot be written\n",
385 offset);
386 return -EINVAL;
387 }
Fabio Estevam1cd87612015-11-05 12:43:42 -0200388 }
389
Jagannadha Sutradharudu Tekie0ebabc2014-01-11 15:13:11 +0530390 cmd[0] = flash->write_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530391 for (actual = 0; actual < len; actual += chunk_len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530392 write_addr = offset;
393
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530394#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530395 if (flash->dual_flash > SF_SINGLE_FLASH)
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530396 spi_flash_dual(flash, &write_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530397#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530398#ifdef CONFIG_SPI_FLASH_BAR
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530399 ret = write_bar(flash, write_addr);
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530400 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530401 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530402#endif
403 byte_addr = offset % page_size;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900404 chunk_len = min(len - actual, (size_t)(page_size - byte_addr));
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530405
Jagan Teki7d005992015-12-12 11:51:57 +0530406 if (spi->max_write_size)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900407 chunk_len = min(chunk_len,
Jagan Teki7d005992015-12-12 11:51:57 +0530408 (size_t)spi->max_write_size);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530409
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530410 spi_flash_addr(write_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530411
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +0530412 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530413 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
414
415 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
416 buf + actual, chunk_len);
417 if (ret < 0) {
418 debug("SF: write failed\n");
419 break;
420 }
421
422 offset += chunk_len;
423 }
424
Lukasz Majewski896de8a2017-09-25 12:40:08 +0200425#ifdef CONFIG_SPI_FLASH_BAR
426 ret = clean_bar(flash);
427#endif
428
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530429 return ret;
430}
431
432int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
433 size_t cmd_len, void *data, size_t data_len)
434{
435 struct spi_slave *spi = flash->spi;
436 int ret;
437
Jagan Teki7d005992015-12-12 11:51:57 +0530438 ret = spi_claim_bus(spi);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530439 if (ret) {
440 debug("SF: unable to claim SPI bus\n");
441 return ret;
442 }
443
444 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
445 if (ret < 0) {
446 debug("SF: read cmd failed\n");
447 return ret;
448 }
449
450 spi_release_bus(spi);
451
452 return ret;
453}
454
Mugunthan V N4e4b58d2016-02-15 15:31:39 +0530455/*
456 * TODO: remove the weak after all the other spi_flash_copy_mmap
457 * implementations removed from drivers
458 */
Tom Rini4fe44702015-08-17 13:29:54 +0530459void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len)
460{
Mugunthan V N4e4b58d2016-02-15 15:31:39 +0530461#ifdef CONFIG_DMA
462 if (!dma_memcpy(data, offset, len))
463 return;
464#endif
Tom Rini4fe44702015-08-17 13:29:54 +0530465 memcpy(data, offset, len);
466}
467
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530468int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530469 size_t len, void *data)
470{
Jagan Teki7d005992015-12-12 11:51:57 +0530471 struct spi_slave *spi = flash->spi;
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530472 u8 *cmd, cmdsz;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530473 u32 remain_len, read_len, read_addr;
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530474 int bank_sel = 0;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530475 int ret = -1;
476
477 /* Handle memory-mapped SPI */
478 if (flash->memory_map) {
Jagan Teki7d005992015-12-12 11:51:57 +0530479 ret = spi_claim_bus(spi);
Poddar, Sourav47e21e02013-11-14 21:01:15 +0530480 if (ret) {
481 debug("SF: unable to claim SPI bus\n");
482 return ret;
483 }
Jagan Teki7d005992015-12-12 11:51:57 +0530484 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP);
Tom Rini4fe44702015-08-17 13:29:54 +0530485 spi_flash_copy_mmap(data, flash->memory_map + offset, len);
Jagan Teki7d005992015-12-12 11:51:57 +0530486 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
487 spi_release_bus(spi);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530488 return 0;
489 }
490
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530491 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
Jagannadha Sutradharudu Teki9ad01752014-02-04 21:36:13 +0530492 cmd = calloc(1, cmdsz);
493 if (!cmd) {
494 debug("SF: Failed to allocate cmd\n");
495 return -ENOMEM;
496 }
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530497
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530498 cmd[0] = flash->read_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530499 while (len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530500 read_addr = offset;
501
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530502#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530503 if (flash->dual_flash > SF_SINGLE_FLASH)
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530504 spi_flash_dual(flash, &read_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530505#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530506#ifdef CONFIG_SPI_FLASH_BAR
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530507 ret = write_bar(flash, read_addr);
Jagan Teki5088b072015-09-02 11:39:48 +0530508 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530509 return ret;
Jagan Teki5088b072015-09-02 11:39:48 +0530510 bank_sel = flash->bank_curr;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530511#endif
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530512 remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
513 (bank_sel + 1)) - offset;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530514 if (len < remain_len)
515 read_len = len;
516 else
517 read_len = remain_len;
518
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530519 spi_flash_addr(read_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530520
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530521 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530522 if (ret < 0) {
523 debug("SF: read failed\n");
524 break;
525 }
526
527 offset += read_len;
528 len -= read_len;
529 data += read_len;
530 }
531
Lukasz Majewski896de8a2017-09-25 12:40:08 +0200532#ifdef CONFIG_SPI_FLASH_BAR
533 ret = clean_bar(flash);
534#endif
535
Marek Vasutd774c482014-07-12 18:11:31 +0530536 free(cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530537 return ret;
538}
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530539
540#ifdef CONFIG_SPI_FLASH_SST
541static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
542{
Jagan Teki7d005992015-12-12 11:51:57 +0530543 struct spi_slave *spi = flash->spi;
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530544 int ret;
545 u8 cmd[4] = {
546 CMD_SST_BP,
547 offset >> 16,
548 offset >> 8,
549 offset,
550 };
551
552 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
Jagan Teki7d005992015-12-12 11:51:57 +0530553 spi_w8r8(spi, CMD_READ_STATUS), buf, cmd[0], offset);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530554
555 ret = spi_flash_cmd_write_enable(flash);
556 if (ret)
557 return ret;
558
Jagan Teki7d005992015-12-12 11:51:57 +0530559 ret = spi_flash_cmd_write(spi, cmd, sizeof(cmd), buf, 1);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530560 if (ret)
561 return ret;
562
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530563 return spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530564}
565
566int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
567 const void *buf)
568{
Jagan Teki7d005992015-12-12 11:51:57 +0530569 struct spi_slave *spi = flash->spi;
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530570 size_t actual, cmd_len;
571 int ret;
572 u8 cmd[4];
573
Jagan Teki7d005992015-12-12 11:51:57 +0530574 ret = spi_claim_bus(spi);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530575 if (ret) {
576 debug("SF: Unable to claim SPI bus\n");
577 return ret;
578 }
579
580 /* If the data is not word aligned, write out leading single byte */
581 actual = offset % 2;
582 if (actual) {
583 ret = sst_byte_write(flash, offset, buf);
584 if (ret)
585 goto done;
586 }
587 offset += actual;
588
589 ret = spi_flash_cmd_write_enable(flash);
590 if (ret)
591 goto done;
592
593 cmd_len = 4;
594 cmd[0] = CMD_SST_AAI_WP;
595 cmd[1] = offset >> 16;
596 cmd[2] = offset >> 8;
597 cmd[3] = offset;
598
599 for (; actual < len - 1; actual += 2) {
600 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
Jagan Teki7d005992015-12-12 11:51:57 +0530601 spi_w8r8(spi, CMD_READ_STATUS), buf + actual,
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530602 cmd[0], offset);
603
Jagan Teki7d005992015-12-12 11:51:57 +0530604 ret = spi_flash_cmd_write(spi, cmd, cmd_len,
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530605 buf + actual, 2);
606 if (ret) {
607 debug("SF: sst word program failed\n");
608 break;
609 }
610
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530611 ret = spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530612 if (ret)
613 break;
614
615 cmd_len = 1;
616 offset += 2;
617 }
618
619 if (!ret)
620 ret = spi_flash_cmd_write_disable(flash);
621
622 /* If there is a single trailing byte, write it out */
623 if (!ret && actual != len)
624 ret = sst_byte_write(flash, offset, buf + actual);
625
626 done:
627 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
628 ret ? "failure" : "success", len, offset - actual);
629
Jagan Teki7d005992015-12-12 11:51:57 +0530630 spi_release_bus(spi);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530631 return ret;
632}
Bin Mengfcbfc172014-12-12 19:36:13 +0530633
634int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
635 const void *buf)
636{
Jagan Teki7d005992015-12-12 11:51:57 +0530637 struct spi_slave *spi = flash->spi;
Bin Mengfcbfc172014-12-12 19:36:13 +0530638 size_t actual;
639 int ret;
640
Jagan Teki7d005992015-12-12 11:51:57 +0530641 ret = spi_claim_bus(spi);
Bin Mengfcbfc172014-12-12 19:36:13 +0530642 if (ret) {
643 debug("SF: Unable to claim SPI bus\n");
644 return ret;
645 }
646
647 for (actual = 0; actual < len; actual++) {
648 ret = sst_byte_write(flash, offset, buf + actual);
649 if (ret) {
650 debug("SF: sst byte program failed\n");
651 break;
652 }
653 offset++;
654 }
655
656 if (!ret)
657 ret = spi_flash_cmd_write_disable(flash);
658
659 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
660 ret ? "failure" : "success", len, offset - actual);
661
Jagan Teki7d005992015-12-12 11:51:57 +0530662 spi_release_bus(spi);
Bin Mengfcbfc172014-12-12 19:36:13 +0530663 return ret;
664}
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530665#endif
Fabio Estevamd9709692015-11-05 12:43:41 -0200666
Fabio Estevam0a2bf5c2015-11-17 16:50:53 -0200667#if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
Fabio Estevamd9709692015-11-05 12:43:41 -0200668static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
Marek Vasut405a3c62016-03-11 03:20:16 +0100669 u64 *len)
Fabio Estevamd9709692015-11-05 12:43:41 -0200670{
671 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
672 int shift = ffs(mask) - 1;
673 int pow;
674
675 if (!(sr & mask)) {
676 /* No protection */
677 *ofs = 0;
678 *len = 0;
679 } else {
680 pow = ((sr & mask) ^ mask) >> shift;
681 *len = flash->size >> pow;
682 *ofs = flash->size - *len;
683 }
684}
685
686/*
687 * Return 1 if the entire region is locked, 0 otherwise
688 */
Marek Vasut405a3c62016-03-11 03:20:16 +0100689static int stm_is_locked_sr(struct spi_flash *flash, loff_t ofs, u64 len,
Fabio Estevamd9709692015-11-05 12:43:41 -0200690 u8 sr)
691{
692 loff_t lock_offs;
Marek Vasut405a3c62016-03-11 03:20:16 +0100693 u64 lock_len;
Fabio Estevamd9709692015-11-05 12:43:41 -0200694
695 stm_get_locked_range(flash, sr, &lock_offs, &lock_len);
696
697 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
698}
699
700/*
701 * Check if a region of the flash is (completely) locked. See stm_lock() for
702 * more info.
703 *
704 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
705 * negative on errors.
706 */
Fabio Estevam1cd87612015-11-05 12:43:42 -0200707int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
Fabio Estevamd9709692015-11-05 12:43:41 -0200708{
709 int status;
710 u8 sr;
711
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530712 status = read_sr(flash, &sr);
Fabio Estevamd9709692015-11-05 12:43:41 -0200713 if (status < 0)
714 return status;
715
716 return stm_is_locked_sr(flash, ofs, len, sr);
717}
718
719/*
720 * Lock a region of the flash. Compatible with ST Micro and similar flash.
721 * Supports only the block protection bits BP{0,1,2} in the status register
722 * (SR). Does not support these features found in newer SR bitfields:
723 * - TB: top/bottom protect - only handle TB=0 (top protect)
724 * - SEC: sector/block protect - only handle SEC=0 (block protect)
725 * - CMP: complement protect - only support CMP=0 (range is not complemented)
726 *
727 * Sample table portion for 8MB flash (Winbond w25q64fw):
728 *
729 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
730 * --------------------------------------------------------------------------
731 * X | X | 0 | 0 | 0 | NONE | NONE
732 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
733 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
734 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
735 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
736 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
737 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
738 * X | X | 1 | 1 | 1 | 8 MB | ALL
739 *
740 * Returns negative on errors, 0 on success.
741 */
742int stm_lock(struct spi_flash *flash, u32 ofs, size_t len)
743{
744 u8 status_old, status_new;
745 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
746 u8 shift = ffs(mask) - 1, pow, val;
Fabio Estevam38a7e9b2015-11-17 17:13:33 -0200747 int ret;
Fabio Estevamd9709692015-11-05 12:43:41 -0200748
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530749 ret = read_sr(flash, &status_old);
Fabio Estevam38a7e9b2015-11-17 17:13:33 -0200750 if (ret < 0)
751 return ret;
Fabio Estevamd9709692015-11-05 12:43:41 -0200752
753 /* SPI NOR always locks to the end */
754 if (ofs + len != flash->size) {
755 /* Does combined region extend to end? */
756 if (!stm_is_locked_sr(flash, ofs + len, flash->size - ofs - len,
757 status_old))
758 return -EINVAL;
759 len = flash->size - ofs;
760 }
761
762 /*
763 * Need smallest pow such that:
764 *
765 * 1 / (2^pow) <= (len / size)
766 *
767 * so (assuming power-of-2 size) we do:
768 *
769 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
770 */
771 pow = ilog2(flash->size) - ilog2(len);
772 val = mask - (pow << shift);
773 if (val & ~mask)
774 return -EINVAL;
775
776 /* Don't "lock" with no region! */
777 if (!(val & mask))
778 return -EINVAL;
779
780 status_new = (status_old & ~mask) | val;
781
782 /* Only modify protection if it will not unlock other areas */
783 if ((status_new & mask) <= (status_old & mask))
784 return -EINVAL;
785
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530786 write_sr(flash, status_new);
Fabio Estevamd9709692015-11-05 12:43:41 -0200787
788 return 0;
789}
790
791/*
792 * Unlock a region of the flash. See stm_lock() for more info
793 *
794 * Returns negative on errors, 0 on success.
795 */
796int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
797{
798 uint8_t status_old, status_new;
799 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
800 u8 shift = ffs(mask) - 1, pow, val;
Fabio Estevam38a7e9b2015-11-17 17:13:33 -0200801 int ret;
Fabio Estevamd9709692015-11-05 12:43:41 -0200802
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530803 ret = read_sr(flash, &status_old);
Fabio Estevam38a7e9b2015-11-17 17:13:33 -0200804 if (ret < 0)
805 return ret;
Fabio Estevamd9709692015-11-05 12:43:41 -0200806
807 /* Cannot unlock; would unlock larger region than requested */
Fabio Estevam55f380c2016-01-05 22:24:39 -0200808 if (stm_is_locked_sr(flash, ofs - flash->erase_size, flash->erase_size,
809 status_old))
Fabio Estevamd9709692015-11-05 12:43:41 -0200810 return -EINVAL;
811 /*
812 * Need largest pow such that:
813 *
814 * 1 / (2^pow) >= (len / size)
815 *
816 * so (assuming power-of-2 size) we do:
817 *
818 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
819 */
820 pow = ilog2(flash->size) - order_base_2(flash->size - (ofs + len));
821 if (ofs + len == flash->size) {
822 val = 0; /* fully unlocked */
823 } else {
824 val = mask - (pow << shift);
825 /* Some power-of-two sizes are not supported */
826 if (val & ~mask)
827 return -EINVAL;
828 }
829
830 status_new = (status_old & ~mask) | val;
831
832 /* Only modify protection if it will not lock other areas */
833 if ((status_new & mask) >= (status_old & mask))
834 return -EINVAL;
835
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530836 write_sr(flash, status_new);
Fabio Estevamd9709692015-11-05 12:43:41 -0200837
838 return 0;
839}
Fabio Estevam0a2bf5c2015-11-17 16:50:53 -0200840#endif
Jagan Tekie6401d82015-12-11 21:36:34 +0530841
Jagan Tekie6401d82015-12-11 21:36:34 +0530842
843#ifdef CONFIG_SPI_FLASH_MACRONIX
Jagan Teki7fd4fd62015-12-13 23:04:46 +0530844static int macronix_quad_enable(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +0530845{
846 u8 qeb_status;
847 int ret;
848
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530849 ret = read_sr(flash, &qeb_status);
Jagan Tekie6401d82015-12-11 21:36:34 +0530850 if (ret < 0)
851 return ret;
852
Jagan Tekif2db1bf2015-12-15 12:42:02 +0530853 if (qeb_status & STATUS_QEB_MXIC)
854 return 0;
855
Jagan Tekid723f342015-12-16 13:48:08 +0530856 ret = write_sr(flash, qeb_status | STATUS_QEB_MXIC);
Jagan Tekif2db1bf2015-12-15 12:42:02 +0530857 if (ret < 0)
858 return ret;
859
860 /* read SR and check it */
861 ret = read_sr(flash, &qeb_status);
862 if (!(ret >= 0 && (qeb_status & STATUS_QEB_MXIC))) {
863 printf("SF: Macronix SR Quad bit not clear\n");
864 return -EINVAL;
Jagan Tekie6401d82015-12-11 21:36:34 +0530865 }
866
867 return ret;
868}
869#endif
870
871#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
Jagan Teki7fd4fd62015-12-13 23:04:46 +0530872static int spansion_quad_enable(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +0530873{
874 u8 qeb_status;
875 int ret;
876
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530877 ret = read_cr(flash, &qeb_status);
Jagan Tekie6401d82015-12-11 21:36:34 +0530878 if (ret < 0)
879 return ret;
880
Jagan Teki294472b2015-12-15 12:28:39 +0530881 if (qeb_status & STATUS_QEB_WINSPAN)
882 return 0;
883
Jagan Tekid723f342015-12-16 13:48:08 +0530884 ret = write_cr(flash, qeb_status | STATUS_QEB_WINSPAN);
Jagan Teki294472b2015-12-15 12:28:39 +0530885 if (ret < 0)
886 return ret;
887
888 /* read CR and check it */
889 ret = read_cr(flash, &qeb_status);
890 if (!(ret >= 0 && (qeb_status & STATUS_QEB_WINSPAN))) {
891 printf("SF: Spansion CR Quad bit not clear\n");
892 return -EINVAL;
Jagan Tekie6401d82015-12-11 21:36:34 +0530893 }
894
895 return ret;
896}
897#endif
898
Jagan Teki77ae47b2016-10-30 23:16:10 +0530899static const struct spi_flash_info *spi_flash_read_id(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +0530900{
Jagan Teki77ae47b2016-10-30 23:16:10 +0530901 int tmp;
Jagan Tekib1c755c2016-10-30 23:16:16 +0530902 u8 id[SPI_FLASH_MAX_ID_LEN];
Jagan Teki77ae47b2016-10-30 23:16:10 +0530903 const struct spi_flash_info *info;
904
Jagan Tekib1c755c2016-10-30 23:16:16 +0530905 tmp = spi_flash_cmd(flash->spi, CMD_READ_ID, id, SPI_FLASH_MAX_ID_LEN);
Jagan Teki77ae47b2016-10-30 23:16:10 +0530906 if (tmp < 0) {
907 printf("SF: error %d reading JEDEC ID\n", tmp);
908 return ERR_PTR(tmp);
909 }
910
911 info = spi_flash_ids;
912 for (; info->name != NULL; info++) {
913 if (info->id_len) {
914 if (!memcmp(info->id, id, info->id_len))
915 return info;
916 }
917 }
918
919 printf("SF: unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
920 id[0], id[1], id[2]);
921 return ERR_PTR(-ENODEV);
922}
923
924static int set_quad_mode(struct spi_flash *flash,
925 const struct spi_flash_info *info)
926{
927 switch (JEDEC_MFR(info)) {
Jagan Tekie6401d82015-12-11 21:36:34 +0530928#ifdef CONFIG_SPI_FLASH_MACRONIX
929 case SPI_FLASH_CFI_MFR_MACRONIX:
Jagan Teki7fd4fd62015-12-13 23:04:46 +0530930 return macronix_quad_enable(flash);
Jagan Tekie6401d82015-12-11 21:36:34 +0530931#endif
932#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
933 case SPI_FLASH_CFI_MFR_SPANSION:
934 case SPI_FLASH_CFI_MFR_WINBOND:
Jagan Teki7fd4fd62015-12-13 23:04:46 +0530935 return spansion_quad_enable(flash);
Jagan Tekie6401d82015-12-11 21:36:34 +0530936#endif
937#ifdef CONFIG_SPI_FLASH_STMICRO
938 case SPI_FLASH_CFI_MFR_STMICRO:
Cyrille Pitchen6ef52f12016-12-15 17:45:39 +0100939 debug("SF: QEB is volatile for %02x flash\n", JEDEC_MFR(info));
940 return 0;
Jagan Tekie6401d82015-12-11 21:36:34 +0530941#endif
942 default:
Jagan Teki77ae47b2016-10-30 23:16:10 +0530943 printf("SF: Need set QEB func for %02x flash\n",
944 JEDEC_MFR(info));
Jagan Tekie6401d82015-12-11 21:36:34 +0530945 return -1;
946 }
Jagan Tekie6401d82015-12-11 21:36:34 +0530947}
Jagan Tekie6401d82015-12-11 21:36:34 +0530948
949#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass9b203bb2017-05-18 20:09:57 -0600950int spi_flash_decode_fdt(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +0530951{
Simon Glass0f5c2692016-01-21 19:43:54 -0700952#ifdef CONFIG_DM_SPI_FLASH
Jagan Tekie6401d82015-12-11 21:36:34 +0530953 fdt_addr_t addr;
954 fdt_size_t size;
Jagan Tekie6401d82015-12-11 21:36:34 +0530955
Simon Glass9b203bb2017-05-18 20:09:57 -0600956 addr = dev_read_addr_size(flash->dev, "memory-map", &size);
Jagan Tekie6401d82015-12-11 21:36:34 +0530957 if (addr == FDT_ADDR_T_NONE) {
958 debug("%s: Cannot decode address\n", __func__);
959 return 0;
960 }
961
Phil Edworthy8d7899f2016-12-09 15:03:39 +0000962 if (flash->size > size) {
Jagan Tekie6401d82015-12-11 21:36:34 +0530963 debug("%s: Memory map must cover entire device\n", __func__);
964 return -1;
965 }
966 flash->memory_map = map_sysmem(addr, size);
Simon Glass0f5c2692016-01-21 19:43:54 -0700967#endif
Jagan Tekie6401d82015-12-11 21:36:34 +0530968
969 return 0;
970}
971#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
972
Jagan Teki4abfb982015-12-06 21:33:32 +0530973int spi_flash_scan(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +0530974{
Jagan Teki4abfb982015-12-06 21:33:32 +0530975 struct spi_slave *spi = flash->spi;
Jagan Teki77ae47b2016-10-30 23:16:10 +0530976 const struct spi_flash_info *info = NULL;
Fabien Parent4d035ac2016-12-05 19:09:10 +0100977 int ret;
Jagan Teki0cd37262015-09-29 18:06:04 +0530978
Jagan Teki77ae47b2016-10-30 23:16:10 +0530979 info = spi_flash_read_id(flash);
980 if (IS_ERR_OR_NULL(info))
981 return -ENOENT;
Jagan Tekie6401d82015-12-11 21:36:34 +0530982
Bin Meng7b55a802017-07-23 07:44:37 -0700983 /*
984 * Flash powers up read-only, so clear BP# bits.
985 *
986 * Note on some flash (like Macronix), QE (quad enable) bit is in the
987 * same status register as BP# bits, and we need preserve its original
988 * value during a reboot cycle as this is required by some platforms
989 * (like Intel ICH SPI controller working under descriptor mode).
990 */
Jagan Teki77ae47b2016-10-30 23:16:10 +0530991 if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_ATMEL ||
Bin Meng7b55a802017-07-23 07:44:37 -0700992 (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) ||
993 (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX)) {
994 u8 sr = 0;
995
996 if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX) {
997 read_sr(flash, &sr);
998 sr &= STATUS_QEB_MXIC;
999 }
1000 write_sr(flash, sr);
1001 }
Jagan Tekie6401d82015-12-11 21:36:34 +05301002
Jagan Teki77ae47b2016-10-30 23:16:10 +05301003 flash->name = info->name;
Jagan Tekie6401d82015-12-11 21:36:34 +05301004 flash->memory_map = spi->memory_map;
Jagan Tekie6401d82015-12-11 21:36:34 +05301005
Jagan Teki77ae47b2016-10-30 23:16:10 +05301006 if (info->flags & SST_WR)
Jagan Tekie6401d82015-12-11 21:36:34 +05301007 flash->flags |= SNOR_F_SST_WR;
1008
Jagan Tekie6401d82015-12-11 21:36:34 +05301009#ifndef CONFIG_DM_SPI_FLASH
1010 flash->write = spi_flash_cmd_write_ops;
1011#if defined(CONFIG_SPI_FLASH_SST)
1012 if (flash->flags & SNOR_F_SST_WR) {
Jagan Teki71331b32015-12-13 20:12:45 +05301013 if (spi->mode & SPI_TX_BYTE)
Jagan Tekie6401d82015-12-11 21:36:34 +05301014 flash->write = sst_write_bp;
1015 else
1016 flash->write = sst_write_wp;
1017 }
1018#endif
1019 flash->erase = spi_flash_cmd_erase_ops;
1020 flash->read = spi_flash_cmd_read_ops;
1021#endif
1022
Jagan Tekie6401d82015-12-11 21:36:34 +05301023#if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
Jagan Tekiaf5d8192016-10-30 23:16:11 +05301024 /* NOR protection support for STmicro/Micron chips and similar */
1025 if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_STMICRO ||
1026 JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) {
Jagan Tekie6401d82015-12-11 21:36:34 +05301027 flash->flash_lock = stm_lock;
1028 flash->flash_unlock = stm_unlock;
1029 flash->flash_is_locked = stm_is_locked;
Jagan Tekie6401d82015-12-11 21:36:34 +05301030 }
Jagan Tekiaf5d8192016-10-30 23:16:11 +05301031#endif
Jagan Tekie6401d82015-12-11 21:36:34 +05301032
1033 /* Compute the flash size */
1034 flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
Jagan Teki77ae47b2016-10-30 23:16:10 +05301035 flash->page_size = info->page_size;
Jagan Tekie6401d82015-12-11 21:36:34 +05301036 /*
1037 * The Spansion S25FL032P and S25FL064P have 256b pages, yet use the
1038 * 0x4d00 Extended JEDEC code. The rest of the Spansion flashes with
1039 * the 0x4d00 Extended JEDEC code have 512b pages. All of the others
1040 * have 256b pages.
1041 */
Jagan Teki77ae47b2016-10-30 23:16:10 +05301042 if (JEDEC_EXT(info) == 0x4d00) {
1043 if ((JEDEC_ID(info) != 0x0215) &&
1044 (JEDEC_ID(info) != 0x0216))
Jagan Tekie6401d82015-12-11 21:36:34 +05301045 flash->page_size = 512;
Jagan Tekie6401d82015-12-11 21:36:34 +05301046 }
1047 flash->page_size <<= flash->shift;
Jagan Teki77ae47b2016-10-30 23:16:10 +05301048 flash->sector_size = info->sector_size << flash->shift;
Jagan Teki49e65792016-10-30 23:16:15 +05301049 flash->size = flash->sector_size * info->n_sectors << flash->shift;
Jagan Tekie6401d82015-12-11 21:36:34 +05301050#ifdef CONFIG_SF_DUAL_FLASH
1051 if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
1052 flash->size <<= 1;
1053#endif
1054
Jagan Tekica40ea22016-08-08 17:23:56 +05301055#ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
Jagan Tekie6401d82015-12-11 21:36:34 +05301056 /* Compute erase sector and command */
Jagan Teki77ae47b2016-10-30 23:16:10 +05301057 if (info->flags & SECT_4K) {
Jagan Tekie6401d82015-12-11 21:36:34 +05301058 flash->erase_cmd = CMD_ERASE_4K;
1059 flash->erase_size = 4096 << flash->shift;
Jagan Tekica40ea22016-08-08 17:23:56 +05301060 } else
1061#endif
1062 {
Jagan Tekie6401d82015-12-11 21:36:34 +05301063 flash->erase_cmd = CMD_ERASE_64K;
1064 flash->erase_size = flash->sector_size;
1065 }
1066
1067 /* Now erase size becomes valid sector size */
1068 flash->sector_size = flash->erase_size;
1069
Jagan Tekib6785392016-08-08 16:50:45 +05301070 /* Look for read commands */
1071 flash->read_cmd = CMD_READ_ARRAY_FAST;
Jagan Teki96536b12016-08-08 17:12:12 +05301072 if (spi->mode & SPI_RX_SLOW)
Jagan Tekib6785392016-08-08 16:50:45 +05301073 flash->read_cmd = CMD_READ_ARRAY_SLOW;
Jagan Teki77ae47b2016-10-30 23:16:10 +05301074 else if (spi->mode & SPI_RX_QUAD && info->flags & RD_QUAD)
Jagan Tekib6785392016-08-08 16:50:45 +05301075 flash->read_cmd = CMD_READ_QUAD_OUTPUT_FAST;
Jagan Teki77ae47b2016-10-30 23:16:10 +05301076 else if (spi->mode & SPI_RX_DUAL && info->flags & RD_DUAL)
Jagan Tekib6785392016-08-08 16:50:45 +05301077 flash->read_cmd = CMD_READ_DUAL_OUTPUT_FAST;
Jagan Tekie6401d82015-12-11 21:36:34 +05301078
Jagan Tekib6785392016-08-08 16:50:45 +05301079 /* Look for write commands */
Jagan Teki77ae47b2016-10-30 23:16:10 +05301080 if (info->flags & WR_QPP && spi->mode & SPI_TX_QUAD)
Jagan Tekie6401d82015-12-11 21:36:34 +05301081 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
1082 else
1083 /* Go for default supported write cmd */
1084 flash->write_cmd = CMD_PAGE_PROGRAM;
1085
1086 /* Set the quad enable bit - only for quad commands */
1087 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
1088 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
1089 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
Jagan Teki77ae47b2016-10-30 23:16:10 +05301090 ret = set_quad_mode(flash, info);
Jagan Tekie6401d82015-12-11 21:36:34 +05301091 if (ret) {
Jagan Teki77ae47b2016-10-30 23:16:10 +05301092 debug("SF: Fail to set QEB for %02x\n",
1093 JEDEC_MFR(info));
Jagan Tekie6401d82015-12-11 21:36:34 +05301094 return -EINVAL;
1095 }
1096 }
1097
1098 /* Read dummy_byte: dummy byte is determined based on the
1099 * dummy cycles of a particular command.
1100 * Fast commands - dummy_byte = dummy_cycles/8
1101 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
1102 * For I/O commands except cmd[0] everything goes on no.of lines
1103 * based on particular command but incase of fast commands except
1104 * data all go on single line irrespective of command.
1105 */
1106 switch (flash->read_cmd) {
1107 case CMD_READ_QUAD_IO_FAST:
1108 flash->dummy_byte = 2;
1109 break;
1110 case CMD_READ_ARRAY_SLOW:
1111 flash->dummy_byte = 0;
1112 break;
1113 default:
1114 flash->dummy_byte = 1;
1115 }
1116
1117#ifdef CONFIG_SPI_FLASH_STMICRO
Jagan Teki77ae47b2016-10-30 23:16:10 +05301118 if (info->flags & E_FSR)
Jagan Tekie6401d82015-12-11 21:36:34 +05301119 flash->flags |= SNOR_F_USE_FSR;
1120#endif
1121
1122 /* Configure the BAR - discover bank cmds and read current bank */
1123#ifdef CONFIG_SPI_FLASH_BAR
Jagan Tekiff0e43e2016-10-30 23:16:25 +05301124 ret = read_bar(flash, info);
Jagan Tekie6401d82015-12-11 21:36:34 +05301125 if (ret < 0)
1126 return ret;
1127#endif
1128
Simon Glass3fb33392016-11-13 14:22:01 -07001129#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass9b203bb2017-05-18 20:09:57 -06001130 ret = spi_flash_decode_fdt(flash);
Jagan Tekie6401d82015-12-11 21:36:34 +05301131 if (ret) {
1132 debug("SF: FDT decode error\n");
1133 return -EINVAL;
1134 }
1135#endif
1136
1137#ifndef CONFIG_SPL_BUILD
1138 printf("SF: Detected %s with page size ", flash->name);
1139 print_size(flash->page_size, ", erase size ");
1140 print_size(flash->erase_size, ", total ");
1141 print_size(flash->size, "");
1142 if (flash->memory_map)
1143 printf(", mapped at %p", flash->memory_map);
1144 puts("\n");
1145#endif
1146
1147#ifndef CONFIG_SPI_FLASH_BAR
1148 if (((flash->dual_flash == SF_SINGLE_FLASH) &&
1149 (flash->size > SPI_FLASH_16MB_BOUN)) ||
1150 ((flash->dual_flash > SF_SINGLE_FLASH) &&
1151 (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
1152 puts("SF: Warning - Only lower 16MiB accessible,");
1153 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
1154 }
1155#endif
1156
Fabien Parent4d035ac2016-12-05 19:09:10 +01001157 return 0;
Jagan Tekie6401d82015-12-11 21:36:34 +05301158}