blob: c1591242591da8da4fd33d9f67dbfad6f754760b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05302/*
Jagan Tekifefdfd22015-12-06 23:29:02 +05303 * SPI Flash Core
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05304 *
Jagan Tekifefdfd22015-12-06 23:29:02 +05305 * Copyright (C) 2015 Jagan Teki <jteki@openedev.com>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +05306 * Copyright (C) 2013 Jagannadha Sutradharudu Teki, Xilinx Inc.
Jagan Tekifefdfd22015-12-06 23:29:02 +05307 * Copyright (C) 2010 Reinhard Meyer, EMK Elektronik
8 * Copyright (C) 2008 Atmel Corporation
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>
Jagan Tekie6401d82015-12-11 21:36:34 +053014#include <mapmem.h>
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053015#include <spi.h>
16#include <spi_flash.h>
Fabio Estevamd9709692015-11-05 12:43:41 -020017#include <linux/log2.h>
Eugeniy Paltsevae0c4082018-04-10 14:40:44 +030018#include <linux/sizes.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
23static void spi_flash_addr(u32 addr, u8 *cmd)
24{
25 /* cmd[0] is actual command */
26 cmd[1] = addr >> 16;
27 cmd[2] = addr >> 8;
28 cmd[3] = addr >> 0;
29}
30
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053031static int read_sr(struct spi_flash *flash, u8 *rs)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053032{
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053033 int ret;
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053034 u8 cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053035
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053036 cmd = CMD_READ_STATUS;
37 ret = spi_flash_read_common(flash, &cmd, 1, rs, 1);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053038 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053039 debug("SF: fail to read status register\n");
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +053040 return ret;
41 }
42
43 return 0;
44}
45
Jagan Teki853ef3e2015-09-29 16:54:31 +053046static int read_fsr(struct spi_flash *flash, u8 *fsr)
47{
48 int ret;
49 const u8 cmd = CMD_FLAG_STATUS;
50
51 ret = spi_flash_read_common(flash, &cmd, 1, fsr, 1);
52 if (ret < 0) {
53 debug("SF: fail to read flag status register\n");
54 return ret;
55 }
56
57 return 0;
58}
59
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053060static int write_sr(struct spi_flash *flash, u8 ws)
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053061{
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053062 u8 cmd;
63 int ret;
64
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053065 cmd = CMD_WRITE_STATUS;
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +053066 ret = spi_flash_write_common(flash, &cmd, 1, &ws, 1);
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053067 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053068 debug("SF: fail to write status register\n");
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053069 return ret;
70 }
71
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053072 return 0;
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053073}
Jagannadha Sutradharudu Teki754c73c2013-12-26 14:13:36 +053074
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053075#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053076static int read_cr(struct spi_flash *flash, u8 *rc)
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053077{
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053078 int ret;
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053079 u8 cmd;
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053080
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053081 cmd = CMD_READ_CONFIG;
82 ret = spi_flash_read_common(flash, &cmd, 1, rc, 1);
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053083 if (ret < 0) {
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053084 debug("SF: fail to read config register\n");
Jagannadha Sutradharudu Teki8a9c9682013-12-23 15:47:48 +053085 return ret;
86 }
87
88 return 0;
89}
90
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053091static int write_cr(struct spi_flash *flash, u8 wc)
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053092{
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053093 u8 data[2];
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053094 u8 cmd;
95 int ret;
96
Jagan Tekidc8ce0f2015-09-29 22:29:33 +053097 ret = read_sr(flash, &data[0]);
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +053098 if (ret < 0)
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +053099 return ret;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +0530100
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +0530101 cmd = CMD_WRITE_STATUS;
102 data[1] = wc;
103 ret = spi_flash_write_common(flash, &cmd, 1, &data, 2);
104 if (ret) {
105 debug("SF: fail to write config register\n");
106 return ret;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +0530107 }
108
Jagannadha Sutradharudu Teki564a1262013-12-30 22:16:23 +0530109 return 0;
Jagannadha Sutradharudu Teki725c64e2013-12-26 13:54:57 +0530110}
111#endif
112
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530113#ifdef CONFIG_SPI_FLASH_BAR
Lukasz Majewski896de8a2017-09-25 12:40:08 +0200114/*
115 * This "clean_bar" is necessary in a situation when one was accessing
116 * spi flash memory > 16 MiB by using Bank Address Register's BA24 bit.
117 *
118 * After it the BA24 bit shall be cleared to allow access to correct
119 * memory region after SW reset (by calling "reset" command).
120 *
121 * Otherwise, the BA24 bit may be left set and then after reset, the
122 * ROM would read/write/erase SPL from 16 MiB * bank_sel address.
123 */
124static int clean_bar(struct spi_flash *flash)
125{
126 u8 cmd, bank_sel = 0;
127
128 if (flash->bank_curr == 0)
129 return 0;
130 cmd = flash->bank_write_cmd;
Marek Vasut314c72a2018-05-24 21:58:40 +0200131 flash->bank_curr = 0;
Lukasz Majewski896de8a2017-09-25 12:40:08 +0200132
133 return spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
134}
135
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530136static int write_bar(struct spi_flash *flash, u32 offset)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530137{
Jagan Teki5088b072015-09-02 11:39:48 +0530138 u8 cmd, bank_sel;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530139 int ret;
140
Jagan Teki5088b072015-09-02 11:39:48 +0530141 bank_sel = offset / (SPI_FLASH_16MB_BOUN << flash->shift);
142 if (bank_sel == flash->bank_curr)
143 goto bar_end;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530144
145 cmd = flash->bank_write_cmd;
146 ret = spi_flash_write_common(flash, &cmd, 1, &bank_sel, 1);
147 if (ret < 0) {
148 debug("SF: fail to write bank register\n");
149 return ret;
150 }
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530151
Jagan Teki5088b072015-09-02 11:39:48 +0530152bar_end:
153 flash->bank_curr = bank_sel;
154 return flash->bank_curr;
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530155}
Jagan Tekif9a026d2015-11-04 00:27:35 +0530156
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530157static int read_bar(struct spi_flash *flash, const struct spi_flash_info *info)
Jagan Tekif9a026d2015-11-04 00:27:35 +0530158{
159 u8 curr_bank = 0;
160 int ret;
161
162 if (flash->size <= SPI_FLASH_16MB_BOUN)
Jagan Tekida042a02015-12-13 23:10:33 +0530163 goto bar_end;
Jagan Tekif9a026d2015-11-04 00:27:35 +0530164
Jagan Teki77ae47b2016-10-30 23:16:10 +0530165 switch (JEDEC_MFR(info)) {
Jagan Tekif9a026d2015-11-04 00:27:35 +0530166 case SPI_FLASH_CFI_MFR_SPANSION:
167 flash->bank_read_cmd = CMD_BANKADDR_BRRD;
168 flash->bank_write_cmd = CMD_BANKADDR_BRWR;
Jagan Tekie37a3622015-11-20 13:00:15 +0530169 break;
Jagan Tekif9a026d2015-11-04 00:27:35 +0530170 default:
171 flash->bank_read_cmd = CMD_EXTNADDR_RDEAR;
172 flash->bank_write_cmd = CMD_EXTNADDR_WREAR;
173 }
174
175 ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
176 &curr_bank, 1);
177 if (ret) {
178 debug("SF: fail to read bank addr register\n");
179 return ret;
180 }
181
Jagan Tekida042a02015-12-13 23:10:33 +0530182bar_end:
Jagan Tekif9a026d2015-11-04 00:27:35 +0530183 flash->bank_curr = curr_bank;
184 return 0;
185}
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530186#endif
187
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530188#ifdef CONFIG_SF_DUAL_FLASH
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530189static void spi_flash_dual(struct spi_flash *flash, u32 *addr)
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530190{
191 switch (flash->dual_flash) {
192 case SF_DUAL_STACKED_FLASH:
193 if (*addr >= (flash->size >> 1)) {
194 *addr -= flash->size >> 1;
Jagan Teki24aa01b2016-10-30 23:16:26 +0530195 flash->flags |= SNOR_F_USE_UPAGE;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530196 } else {
Jagan Teki24aa01b2016-10-30 23:16:26 +0530197 flash->flags &= ~SNOR_F_USE_UPAGE;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530198 }
199 break;
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530200 case SF_DUAL_PARALLEL_FLASH:
201 *addr >>= flash->shift;
202 break;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530203 default:
204 debug("SF: Unsupported dual_flash=%d\n", flash->dual_flash);
205 break;
206 }
207}
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530208#endif
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530209
Jagan Teki853ef3e2015-09-29 16:54:31 +0530210static int spi_flash_sr_ready(struct spi_flash *flash)
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530211{
Jagan Teki5181dd92015-09-02 11:39:50 +0530212 u8 sr;
Jagan Teki853ef3e2015-09-29 16:54:31 +0530213 int ret;
214
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530215 ret = read_sr(flash, &sr);
Jagan Teki853ef3e2015-09-29 16:54:31 +0530216 if (ret < 0)
217 return ret;
218
219 return !(sr & STATUS_WIP);
220}
221
222static int spi_flash_fsr_ready(struct spi_flash *flash)
223{
224 u8 fsr;
225 int ret;
226
227 ret = read_fsr(flash, &fsr);
228 if (ret < 0)
229 return ret;
230
231 return fsr & STATUS_PEC;
232}
233
234static int spi_flash_ready(struct spi_flash *flash)
235{
236 int sr, fsr;
237
238 sr = spi_flash_sr_ready(flash);
239 if (sr < 0)
240 return sr;
241
242 fsr = 1;
243 if (flash->flags & SNOR_F_USE_FSR) {
244 fsr = spi_flash_fsr_ready(flash);
245 if (fsr < 0)
246 return fsr;
247 }
248
249 return sr && fsr;
250}
251
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530252static int spi_flash_wait_till_ready(struct spi_flash *flash,
253 unsigned long timeout)
Jagan Teki853ef3e2015-09-29 16:54:31 +0530254{
Stephen Warren07a6e382016-04-04 11:03:52 -0600255 unsigned long timebase;
256 int ret;
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530257
Jagan Teki5181dd92015-09-02 11:39:50 +0530258 timebase = get_timer(0);
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530259
Jagan Teki5181dd92015-09-02 11:39:50 +0530260 while (get_timer(timebase) < timeout) {
Jagan Teki853ef3e2015-09-29 16:54:31 +0530261 ret = spi_flash_ready(flash);
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530262 if (ret < 0)
263 return ret;
Jagan Teki853ef3e2015-09-29 16:54:31 +0530264 if (ret)
Jagan Teki5181dd92015-09-02 11:39:50 +0530265 return 0;
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530266 }
267
Jagan Teki5181dd92015-09-02 11:39:50 +0530268 printf("SF: Timeout!\n");
269
270 return -ETIMEDOUT;
Siva Durga Prasad Paladugubf4e8652015-03-11 14:47:57 +0530271}
272
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530273int spi_flash_write_common(struct spi_flash *flash, const u8 *cmd,
274 size_t cmd_len, const void *buf, size_t buf_len)
275{
276 struct spi_slave *spi = flash->spi;
277 unsigned long timeout = SPI_FLASH_PROG_TIMEOUT;
278 int ret;
279
280 if (buf == NULL)
281 timeout = SPI_FLASH_PAGE_ERASE_TIMEOUT;
282
Jagan Teki7d005992015-12-12 11:51:57 +0530283 ret = spi_claim_bus(spi);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530284 if (ret) {
285 debug("SF: unable to claim SPI bus\n");
286 return ret;
287 }
288
289 ret = spi_flash_cmd_write_enable(flash);
290 if (ret < 0) {
291 debug("SF: enabling write failed\n");
292 return ret;
293 }
294
295 ret = spi_flash_cmd_write(spi, cmd, cmd_len, buf, buf_len);
296 if (ret < 0) {
297 debug("SF: write cmd failed\n");
298 return ret;
299 }
300
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530301 ret = spi_flash_wait_till_ready(flash, timeout);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530302 if (ret < 0) {
303 debug("SF: write %s timed out\n",
304 timeout == SPI_FLASH_PROG_TIMEOUT ?
305 "program" : "page erase");
306 return ret;
307 }
308
309 spi_release_bus(spi);
310
311 return ret;
312}
313
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530314int spi_flash_cmd_erase_ops(struct spi_flash *flash, u32 offset, size_t len)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530315{
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530316 u32 erase_size, erase_addr;
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530317 u8 cmd[SPI_FLASH_CMD_LEN];
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530318 int ret = -1;
319
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530320 erase_size = flash->erase_size;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530321 if (offset % erase_size || len % erase_size) {
Liam Beguin65635272018-03-14 19:15:10 -0400322 printf("SF: Erase offset/length not multiple of erase size\n");
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530323 return -1;
324 }
325
Bin Meng66528f62015-11-13 02:46:26 -0800326 if (flash->flash_is_locked) {
327 if (flash->flash_is_locked(flash, offset, len) > 0) {
328 printf("offset 0x%x is protected and cannot be erased\n",
329 offset);
330 return -EINVAL;
331 }
Fabio Estevam1cd87612015-11-05 12:43:42 -0200332 }
333
Jagannadha Sutradharudu Tekibc0f8792013-10-02 19:36:58 +0530334 cmd[0] = flash->erase_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530335 while (len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530336 erase_addr = offset;
337
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530338#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530339 if (flash->dual_flash > SF_SINGLE_FLASH)
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530340 spi_flash_dual(flash, &erase_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530341#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530342#ifdef CONFIG_SPI_FLASH_BAR
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530343 ret = write_bar(flash, erase_addr);
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530344 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530345 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530346#endif
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530347 spi_flash_addr(erase_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530348
349 debug("SF: erase %2x %2x %2x %2x (%x)\n", cmd[0], cmd[1],
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530350 cmd[2], cmd[3], erase_addr);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530351
352 ret = spi_flash_write_common(flash, cmd, sizeof(cmd), NULL, 0);
353 if (ret < 0) {
354 debug("SF: erase failed\n");
355 break;
356 }
357
358 offset += erase_size;
359 len -= erase_size;
360 }
361
Lukasz Majewski896de8a2017-09-25 12:40:08 +0200362#ifdef CONFIG_SPI_FLASH_BAR
363 ret = clean_bar(flash);
364#endif
365
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530366 return ret;
367}
368
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530369int spi_flash_cmd_write_ops(struct spi_flash *flash, u32 offset,
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530370 size_t len, const void *buf)
371{
Jagan Teki7d005992015-12-12 11:51:57 +0530372 struct spi_slave *spi = flash->spi;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530373 unsigned long byte_addr, page_size;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530374 u32 write_addr;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530375 size_t chunk_len, actual;
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530376 u8 cmd[SPI_FLASH_CMD_LEN];
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530377 int ret = -1;
378
379 page_size = flash->page_size;
380
Bin Meng66528f62015-11-13 02:46:26 -0800381 if (flash->flash_is_locked) {
382 if (flash->flash_is_locked(flash, offset, len) > 0) {
383 printf("offset 0x%x is protected and cannot be written\n",
384 offset);
385 return -EINVAL;
386 }
Fabio Estevam1cd87612015-11-05 12:43:42 -0200387 }
388
Jagannadha Sutradharudu Tekie0ebabc2014-01-11 15:13:11 +0530389 cmd[0] = flash->write_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530390 for (actual = 0; actual < len; actual += chunk_len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530391 write_addr = offset;
392
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530393#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530394 if (flash->dual_flash > SF_SINGLE_FLASH)
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530395 spi_flash_dual(flash, &write_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530396#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530397#ifdef CONFIG_SPI_FLASH_BAR
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530398 ret = write_bar(flash, write_addr);
Jagannadha Sutradharudu Tekie14596c2013-10-08 23:26:47 +0530399 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530400 return ret;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530401#endif
402 byte_addr = offset % page_size;
Masahiro Yamadadb204642014-11-07 03:03:31 +0900403 chunk_len = min(len - actual, (size_t)(page_size - byte_addr));
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530404
Jagan Teki7d005992015-12-12 11:51:57 +0530405 if (spi->max_write_size)
Masahiro Yamadadb204642014-11-07 03:03:31 +0900406 chunk_len = min(chunk_len,
Álvaro Fernández Rojasf2fb3a82018-01-23 17:14:57 +0100407 spi->max_write_size - sizeof(cmd));
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530408
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530409 spi_flash_addr(write_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530410
Jagannadha Sutradharudu Teki243ced02014-01-12 21:38:21 +0530411 debug("SF: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530412 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
413
414 ret = spi_flash_write_common(flash, cmd, sizeof(cmd),
415 buf + actual, chunk_len);
416 if (ret < 0) {
417 debug("SF: write failed\n");
418 break;
419 }
420
421 offset += chunk_len;
422 }
423
Lukasz Majewski896de8a2017-09-25 12:40:08 +0200424#ifdef CONFIG_SPI_FLASH_BAR
425 ret = clean_bar(flash);
426#endif
427
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530428 return ret;
429}
430
431int spi_flash_read_common(struct spi_flash *flash, const u8 *cmd,
432 size_t cmd_len, void *data, size_t data_len)
433{
434 struct spi_slave *spi = flash->spi;
435 int ret;
436
Jagan Teki7d005992015-12-12 11:51:57 +0530437 ret = spi_claim_bus(spi);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530438 if (ret) {
439 debug("SF: unable to claim SPI bus\n");
440 return ret;
441 }
442
443 ret = spi_flash_cmd_read(spi, cmd, cmd_len, data, data_len);
444 if (ret < 0) {
445 debug("SF: read cmd failed\n");
446 return ret;
447 }
448
449 spi_release_bus(spi);
450
451 return ret;
452}
453
Mugunthan V N4e4b58d2016-02-15 15:31:39 +0530454/*
455 * TODO: remove the weak after all the other spi_flash_copy_mmap
456 * implementations removed from drivers
457 */
Tom Rini4fe44702015-08-17 13:29:54 +0530458void __weak spi_flash_copy_mmap(void *data, void *offset, size_t len)
459{
Mugunthan V N4e4b58d2016-02-15 15:31:39 +0530460#ifdef CONFIG_DMA
461 if (!dma_memcpy(data, offset, len))
462 return;
463#endif
Tom Rini4fe44702015-08-17 13:29:54 +0530464 memcpy(data, offset, len);
465}
466
Jagannadha Sutradharudu Teki25dc86a2013-10-02 19:38:49 +0530467int spi_flash_cmd_read_ops(struct spi_flash *flash, u32 offset,
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530468 size_t len, void *data)
469{
Jagan Teki7d005992015-12-12 11:51:57 +0530470 struct spi_slave *spi = flash->spi;
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530471 u8 *cmd, cmdsz;
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530472 u32 remain_len, read_len, read_addr;
Jagannadha Sutradharudu Teki3bd17ab2014-01-11 16:57:07 +0530473 int bank_sel = 0;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530474 int ret = -1;
475
476 /* Handle memory-mapped SPI */
477 if (flash->memory_map) {
Jagan Teki7d005992015-12-12 11:51:57 +0530478 ret = spi_claim_bus(spi);
Poddar, Sourav47e21e02013-11-14 21:01:15 +0530479 if (ret) {
480 debug("SF: unable to claim SPI bus\n");
481 return ret;
482 }
Jagan Teki7d005992015-12-12 11:51:57 +0530483 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP);
Tom Rini4fe44702015-08-17 13:29:54 +0530484 spi_flash_copy_mmap(data, flash->memory_map + offset, len);
Jagan Teki7d005992015-12-12 11:51:57 +0530485 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_MMAP_END);
486 spi_release_bus(spi);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530487 return 0;
488 }
489
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530490 cmdsz = SPI_FLASH_CMD_LEN + flash->dummy_byte;
Jagannadha Sutradharudu Teki9ad01752014-02-04 21:36:13 +0530491 cmd = calloc(1, cmdsz);
492 if (!cmd) {
493 debug("SF: Failed to allocate cmd\n");
494 return -ENOMEM;
495 }
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530496
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530497 cmd[0] = flash->read_cmd;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530498 while (len) {
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530499 read_addr = offset;
500
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530501#ifdef CONFIG_SF_DUAL_FLASH
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530502 if (flash->dual_flash > SF_SINGLE_FLASH)
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530503 spi_flash_dual(flash, &read_addr);
Jagannadha Sutradharudu Tekid7f253b2014-01-11 15:25:04 +0530504#endif
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530505#ifdef CONFIG_SPI_FLASH_BAR
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530506 ret = write_bar(flash, read_addr);
Jagan Teki5088b072015-09-02 11:39:48 +0530507 if (ret < 0)
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530508 return ret;
Jagan Teki5088b072015-09-02 11:39:48 +0530509 bank_sel = flash->bank_curr;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530510#endif
Jagannadha Sutradharudu Tekib47b7412014-01-07 00:11:35 +0530511 remain_len = ((SPI_FLASH_16MB_BOUN << flash->shift) *
512 (bank_sel + 1)) - offset;
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530513 if (len < remain_len)
514 read_len = len;
515 else
516 read_len = remain_len;
517
Álvaro Fernández Rojasafb36cd2018-01-23 17:14:56 +0100518 if (spi->max_read_size)
519 read_len = min(read_len, spi->max_read_size);
520
Jagannadha Sutradharudu Tekie84b4f62014-01-12 21:40:11 +0530521 spi_flash_addr(read_addr, cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530522
Jagannadha Sutradharudu Tekica799862014-01-11 16:50:45 +0530523 ret = spi_flash_read_common(flash, cmd, cmdsz, data, read_len);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530524 if (ret < 0) {
525 debug("SF: read failed\n");
526 break;
527 }
528
529 offset += read_len;
530 len -= read_len;
531 data += read_len;
532 }
533
Lukasz Majewski896de8a2017-09-25 12:40:08 +0200534#ifdef CONFIG_SPI_FLASH_BAR
535 ret = clean_bar(flash);
536#endif
537
Marek Vasutd774c482014-07-12 18:11:31 +0530538 free(cmd);
Jagannadha Sutradharudu Tekie9a8b822013-08-29 19:01:56 +0530539 return ret;
540}
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530541
542#ifdef CONFIG_SPI_FLASH_SST
Eugeniy Paltsevae0c4082018-04-10 14:40:44 +0300543static bool sst26_process_bpr(u32 bpr_size, u8 *cmd, u32 bit, enum lock_ctl ctl)
544{
545 switch (ctl) {
546 case SST26_CTL_LOCK:
547 cmd[bpr_size - (bit / 8) - 1] |= BIT(bit % 8);
548 break;
549 case SST26_CTL_UNLOCK:
550 cmd[bpr_size - (bit / 8) - 1] &= ~BIT(bit % 8);
551 break;
552 case SST26_CTL_CHECK:
553 return !!(cmd[bpr_size - (bit / 8) - 1] & BIT(bit % 8));
554 }
555
556 return false;
557}
558
559/*
560 * sst26wf016/sst26wf032/sst26wf064 have next block protection:
561 * 4x - 8 KByte blocks - read & write protection bits - upper addresses
562 * 1x - 32 KByte blocks - write protection bits
563 * rest - 64 KByte blocks - write protection bits
564 * 1x - 32 KByte blocks - write protection bits
565 * 4x - 8 KByte blocks - read & write protection bits - lower addresses
566 *
567 * We'll support only per 64k lock/unlock so lower and upper 64 KByte region
568 * will be treated as single block.
569 */
570
571/*
572 * Lock, unlock or check lock status of the flash region of the flash (depending
573 * on the lock_ctl value)
574 */
575static int sst26_lock_ctl(struct spi_flash *flash, u32 ofs, size_t len, enum lock_ctl ctl)
576{
577 u32 i, bpr_ptr, rptr_64k, lptr_64k, bpr_size;
578 bool lower_64k = false, upper_64k = false;
579 u8 cmd, bpr_buff[SST26_MAX_BPR_REG_LEN] = {};
580 int ret;
581
582 /* Check length and offset for 64k alignment */
583 if ((ofs & (SZ_64K - 1)) || (len & (SZ_64K - 1)))
584 return -EINVAL;
585
586 if (ofs + len > flash->size)
587 return -EINVAL;
588
589 /* SST26 family has only 16 Mbit, 32 Mbit and 64 Mbit IC */
590 if (flash->size != SZ_2M &&
591 flash->size != SZ_4M &&
592 flash->size != SZ_8M)
593 return -EINVAL;
594
595 bpr_size = 2 + (flash->size / SZ_64K / 8);
596
597 cmd = SST26_CMD_READ_BPR;
598 ret = spi_flash_read_common(flash, &cmd, 1, bpr_buff, bpr_size);
599 if (ret < 0) {
600 printf("SF: fail to read block-protection register\n");
601 return ret;
602 }
603
604 rptr_64k = min_t(u32, ofs + len , flash->size - SST26_BOUND_REG_SIZE);
605 lptr_64k = max_t(u32, ofs, SST26_BOUND_REG_SIZE);
606
607 upper_64k = ((ofs + len) > (flash->size - SST26_BOUND_REG_SIZE));
608 lower_64k = (ofs < SST26_BOUND_REG_SIZE);
609
610 /* Lower bits in block-protection register are about 64k region */
611 bpr_ptr = lptr_64k / SZ_64K - 1;
612
613 /* Process 64K blocks region */
614 while (lptr_64k < rptr_64k) {
615 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
616 return EACCES;
617
618 bpr_ptr++;
619 lptr_64k += SZ_64K;
620 }
621
622 /* 32K and 8K region bits in BPR are after 64k region bits */
623 bpr_ptr = (flash->size - 2 * SST26_BOUND_REG_SIZE) / SZ_64K;
624
625 /* Process lower 32K block region */
626 if (lower_64k)
627 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
628 return EACCES;
629
630 bpr_ptr++;
631
632 /* Process upper 32K block region */
633 if (upper_64k)
634 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
635 return EACCES;
636
637 bpr_ptr++;
638
639 /* Process lower 8K block regions */
640 for (i = 0; i < SST26_BPR_8K_NUM; i++) {
641 if (lower_64k)
642 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
643 return EACCES;
644
645 /* In 8K area BPR has both read and write protection bits */
646 bpr_ptr += 2;
647 }
648
649 /* Process upper 8K block regions */
650 for (i = 0; i < SST26_BPR_8K_NUM; i++) {
651 if (upper_64k)
652 if (sst26_process_bpr(bpr_size, bpr_buff, bpr_ptr, ctl))
653 return EACCES;
654
655 /* In 8K area BPR has both read and write protection bits */
656 bpr_ptr += 2;
657 }
658
659 /* If we check region status we don't need to write BPR back */
660 if (ctl == SST26_CTL_CHECK)
661 return 0;
662
663 cmd = SST26_CMD_WRITE_BPR;
664 ret = spi_flash_write_common(flash, &cmd, 1, bpr_buff, bpr_size);
665 if (ret < 0) {
666 printf("SF: fail to write block-protection register\n");
667 return ret;
668 }
669
670 return 0;
671}
672
673static int sst26_unlock(struct spi_flash *flash, u32 ofs, size_t len)
674{
675 return sst26_lock_ctl(flash, ofs, len, SST26_CTL_UNLOCK);
676}
677
678static int sst26_lock(struct spi_flash *flash, u32 ofs, size_t len)
679{
680 return sst26_lock_ctl(flash, ofs, len, SST26_CTL_LOCK);
681}
682
683/*
684 * Returns EACCES (positive value) if region is locked, 0 if region is unlocked,
685 * and negative on errors.
686 */
687static int sst26_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
688{
689 /*
690 * is_locked function is used for check before reading or erasing flash
691 * region, so offset and length might be not 64k allighned, so adjust
692 * them to be 64k allighned as sst26_lock_ctl works only with 64k
693 * allighned regions.
694 */
695 ofs -= ofs & (SZ_64K - 1);
696 len = len & (SZ_64K - 1) ? (len & ~(SZ_64K - 1)) + SZ_64K : len;
697
698 return sst26_lock_ctl(flash, ofs, len, SST26_CTL_CHECK);
699}
700
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530701static int sst_byte_write(struct spi_flash *flash, u32 offset, const void *buf)
702{
Jagan Teki7d005992015-12-12 11:51:57 +0530703 struct spi_slave *spi = flash->spi;
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530704 int ret;
705 u8 cmd[4] = {
706 CMD_SST_BP,
707 offset >> 16,
708 offset >> 8,
709 offset,
710 };
711
712 debug("BP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
Jagan Teki7d005992015-12-12 11:51:57 +0530713 spi_w8r8(spi, CMD_READ_STATUS), buf, cmd[0], offset);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530714
715 ret = spi_flash_cmd_write_enable(flash);
716 if (ret)
717 return ret;
718
Jagan Teki7d005992015-12-12 11:51:57 +0530719 ret = spi_flash_cmd_write(spi, cmd, sizeof(cmd), buf, 1);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530720 if (ret)
721 return ret;
722
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530723 return spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530724}
725
726int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
727 const void *buf)
728{
Jagan Teki7d005992015-12-12 11:51:57 +0530729 struct spi_slave *spi = flash->spi;
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530730 size_t actual, cmd_len;
731 int ret;
732 u8 cmd[4];
733
Jagan Teki7d005992015-12-12 11:51:57 +0530734 ret = spi_claim_bus(spi);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530735 if (ret) {
736 debug("SF: Unable to claim SPI bus\n");
737 return ret;
738 }
739
740 /* If the data is not word aligned, write out leading single byte */
741 actual = offset % 2;
742 if (actual) {
743 ret = sst_byte_write(flash, offset, buf);
744 if (ret)
745 goto done;
746 }
747 offset += actual;
748
749 ret = spi_flash_cmd_write_enable(flash);
750 if (ret)
751 goto done;
752
753 cmd_len = 4;
754 cmd[0] = CMD_SST_AAI_WP;
755 cmd[1] = offset >> 16;
756 cmd[2] = offset >> 8;
757 cmd[3] = offset;
758
759 for (; actual < len - 1; actual += 2) {
760 debug("WP[%02x]: 0x%p => cmd = { 0x%02x 0x%06x }\n",
Jagan Teki7d005992015-12-12 11:51:57 +0530761 spi_w8r8(spi, CMD_READ_STATUS), buf + actual,
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530762 cmd[0], offset);
763
Jagan Teki7d005992015-12-12 11:51:57 +0530764 ret = spi_flash_cmd_write(spi, cmd, cmd_len,
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530765 buf + actual, 2);
766 if (ret) {
767 debug("SF: sst word program failed\n");
768 break;
769 }
770
Jagan Tekiff0e43e2016-10-30 23:16:25 +0530771 ret = spi_flash_wait_till_ready(flash, SPI_FLASH_PROG_TIMEOUT);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530772 if (ret)
773 break;
774
775 cmd_len = 1;
776 offset += 2;
777 }
778
779 if (!ret)
780 ret = spi_flash_cmd_write_disable(flash);
781
782 /* If there is a single trailing byte, write it out */
783 if (!ret && actual != len)
784 ret = sst_byte_write(flash, offset, buf + actual);
785
786 done:
787 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
788 ret ? "failure" : "success", len, offset - actual);
789
Jagan Teki7d005992015-12-12 11:51:57 +0530790 spi_release_bus(spi);
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530791 return ret;
792}
Bin Mengfcbfc172014-12-12 19:36:13 +0530793
794int sst_write_bp(struct spi_flash *flash, u32 offset, size_t len,
795 const void *buf)
796{
Jagan Teki7d005992015-12-12 11:51:57 +0530797 struct spi_slave *spi = flash->spi;
Bin Mengfcbfc172014-12-12 19:36:13 +0530798 size_t actual;
799 int ret;
800
Jagan Teki7d005992015-12-12 11:51:57 +0530801 ret = spi_claim_bus(spi);
Bin Mengfcbfc172014-12-12 19:36:13 +0530802 if (ret) {
803 debug("SF: Unable to claim SPI bus\n");
804 return ret;
805 }
806
807 for (actual = 0; actual < len; actual++) {
808 ret = sst_byte_write(flash, offset, buf + actual);
809 if (ret) {
810 debug("SF: sst byte program failed\n");
811 break;
812 }
813 offset++;
814 }
815
816 if (!ret)
817 ret = spi_flash_cmd_write_disable(flash);
818
819 debug("SF: sst: program %s %zu bytes @ 0x%zx\n",
820 ret ? "failure" : "success", len, offset - actual);
821
Jagan Teki7d005992015-12-12 11:51:57 +0530822 spi_release_bus(spi);
Bin Mengfcbfc172014-12-12 19:36:13 +0530823 return ret;
824}
Jagannadha Sutradharudu Teki08032422013-10-02 19:34:53 +0530825#endif
Fabio Estevamd9709692015-11-05 12:43:41 -0200826
Fabio Estevam0a2bf5c2015-11-17 16:50:53 -0200827#if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
Fabio Estevamd9709692015-11-05 12:43:41 -0200828static void stm_get_locked_range(struct spi_flash *flash, u8 sr, loff_t *ofs,
Marek Vasut405a3c62016-03-11 03:20:16 +0100829 u64 *len)
Fabio Estevamd9709692015-11-05 12:43:41 -0200830{
831 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
832 int shift = ffs(mask) - 1;
833 int pow;
834
835 if (!(sr & mask)) {
836 /* No protection */
837 *ofs = 0;
838 *len = 0;
839 } else {
840 pow = ((sr & mask) ^ mask) >> shift;
841 *len = flash->size >> pow;
842 *ofs = flash->size - *len;
843 }
844}
845
846/*
847 * Return 1 if the entire region is locked, 0 otherwise
848 */
Marek Vasut405a3c62016-03-11 03:20:16 +0100849static int stm_is_locked_sr(struct spi_flash *flash, loff_t ofs, u64 len,
Fabio Estevamd9709692015-11-05 12:43:41 -0200850 u8 sr)
851{
852 loff_t lock_offs;
Marek Vasut405a3c62016-03-11 03:20:16 +0100853 u64 lock_len;
Fabio Estevamd9709692015-11-05 12:43:41 -0200854
855 stm_get_locked_range(flash, sr, &lock_offs, &lock_len);
856
857 return (ofs + len <= lock_offs + lock_len) && (ofs >= lock_offs);
858}
859
860/*
861 * Check if a region of the flash is (completely) locked. See stm_lock() for
862 * more info.
863 *
864 * Returns 1 if entire region is locked, 0 if any portion is unlocked, and
865 * negative on errors.
866 */
Fabio Estevam1cd87612015-11-05 12:43:42 -0200867int stm_is_locked(struct spi_flash *flash, u32 ofs, size_t len)
Fabio Estevamd9709692015-11-05 12:43:41 -0200868{
869 int status;
870 u8 sr;
871
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530872 status = read_sr(flash, &sr);
Fabio Estevamd9709692015-11-05 12:43:41 -0200873 if (status < 0)
874 return status;
875
876 return stm_is_locked_sr(flash, ofs, len, sr);
877}
878
879/*
880 * Lock a region of the flash. Compatible with ST Micro and similar flash.
881 * Supports only the block protection bits BP{0,1,2} in the status register
882 * (SR). Does not support these features found in newer SR bitfields:
883 * - TB: top/bottom protect - only handle TB=0 (top protect)
884 * - SEC: sector/block protect - only handle SEC=0 (block protect)
885 * - CMP: complement protect - only support CMP=0 (range is not complemented)
886 *
887 * Sample table portion for 8MB flash (Winbond w25q64fw):
888 *
889 * SEC | TB | BP2 | BP1 | BP0 | Prot Length | Protected Portion
890 * --------------------------------------------------------------------------
891 * X | X | 0 | 0 | 0 | NONE | NONE
892 * 0 | 0 | 0 | 0 | 1 | 128 KB | Upper 1/64
893 * 0 | 0 | 0 | 1 | 0 | 256 KB | Upper 1/32
894 * 0 | 0 | 0 | 1 | 1 | 512 KB | Upper 1/16
895 * 0 | 0 | 1 | 0 | 0 | 1 MB | Upper 1/8
896 * 0 | 0 | 1 | 0 | 1 | 2 MB | Upper 1/4
897 * 0 | 0 | 1 | 1 | 0 | 4 MB | Upper 1/2
898 * X | X | 1 | 1 | 1 | 8 MB | ALL
899 *
900 * Returns negative on errors, 0 on success.
901 */
902int stm_lock(struct spi_flash *flash, u32 ofs, size_t len)
903{
904 u8 status_old, status_new;
905 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
906 u8 shift = ffs(mask) - 1, pow, val;
Fabio Estevam38a7e9b2015-11-17 17:13:33 -0200907 int ret;
Fabio Estevamd9709692015-11-05 12:43:41 -0200908
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530909 ret = read_sr(flash, &status_old);
Fabio Estevam38a7e9b2015-11-17 17:13:33 -0200910 if (ret < 0)
911 return ret;
Fabio Estevamd9709692015-11-05 12:43:41 -0200912
913 /* SPI NOR always locks to the end */
914 if (ofs + len != flash->size) {
915 /* Does combined region extend to end? */
916 if (!stm_is_locked_sr(flash, ofs + len, flash->size - ofs - len,
917 status_old))
918 return -EINVAL;
919 len = flash->size - ofs;
920 }
921
922 /*
923 * Need smallest pow such that:
924 *
925 * 1 / (2^pow) <= (len / size)
926 *
927 * so (assuming power-of-2 size) we do:
928 *
929 * pow = ceil(log2(size / len)) = log2(size) - floor(log2(len))
930 */
931 pow = ilog2(flash->size) - ilog2(len);
932 val = mask - (pow << shift);
933 if (val & ~mask)
934 return -EINVAL;
935
936 /* Don't "lock" with no region! */
937 if (!(val & mask))
938 return -EINVAL;
939
940 status_new = (status_old & ~mask) | val;
941
942 /* Only modify protection if it will not unlock other areas */
943 if ((status_new & mask) <= (status_old & mask))
944 return -EINVAL;
945
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530946 write_sr(flash, status_new);
Fabio Estevamd9709692015-11-05 12:43:41 -0200947
948 return 0;
949}
950
951/*
952 * Unlock a region of the flash. See stm_lock() for more info
953 *
954 * Returns negative on errors, 0 on success.
955 */
956int stm_unlock(struct spi_flash *flash, u32 ofs, size_t len)
957{
958 uint8_t status_old, status_new;
959 u8 mask = SR_BP2 | SR_BP1 | SR_BP0;
960 u8 shift = ffs(mask) - 1, pow, val;
Fabio Estevam38a7e9b2015-11-17 17:13:33 -0200961 int ret;
Fabio Estevamd9709692015-11-05 12:43:41 -0200962
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530963 ret = read_sr(flash, &status_old);
Fabio Estevam38a7e9b2015-11-17 17:13:33 -0200964 if (ret < 0)
965 return ret;
Fabio Estevamd9709692015-11-05 12:43:41 -0200966
967 /* Cannot unlock; would unlock larger region than requested */
Fabio Estevam55f380c2016-01-05 22:24:39 -0200968 if (stm_is_locked_sr(flash, ofs - flash->erase_size, flash->erase_size,
969 status_old))
Fabio Estevamd9709692015-11-05 12:43:41 -0200970 return -EINVAL;
971 /*
972 * Need largest pow such that:
973 *
974 * 1 / (2^pow) >= (len / size)
975 *
976 * so (assuming power-of-2 size) we do:
977 *
978 * pow = floor(log2(size / len)) = log2(size) - ceil(log2(len))
979 */
980 pow = ilog2(flash->size) - order_base_2(flash->size - (ofs + len));
981 if (ofs + len == flash->size) {
982 val = 0; /* fully unlocked */
983 } else {
984 val = mask - (pow << shift);
985 /* Some power-of-two sizes are not supported */
986 if (val & ~mask)
987 return -EINVAL;
988 }
989
990 status_new = (status_old & ~mask) | val;
991
992 /* Only modify protection if it will not lock other areas */
993 if ((status_new & mask) >= (status_old & mask))
994 return -EINVAL;
995
Jagan Tekidc8ce0f2015-09-29 22:29:33 +0530996 write_sr(flash, status_new);
Fabio Estevamd9709692015-11-05 12:43:41 -0200997
998 return 0;
999}
Fabio Estevam0a2bf5c2015-11-17 16:50:53 -02001000#endif
Jagan Tekie6401d82015-12-11 21:36:34 +05301001
Jagan Tekie6401d82015-12-11 21:36:34 +05301002
1003#ifdef CONFIG_SPI_FLASH_MACRONIX
Jagan Teki7fd4fd62015-12-13 23:04:46 +05301004static int macronix_quad_enable(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +05301005{
1006 u8 qeb_status;
1007 int ret;
1008
Jagan Tekidc8ce0f2015-09-29 22:29:33 +05301009 ret = read_sr(flash, &qeb_status);
Jagan Tekie6401d82015-12-11 21:36:34 +05301010 if (ret < 0)
1011 return ret;
1012
Jagan Tekif2db1bf2015-12-15 12:42:02 +05301013 if (qeb_status & STATUS_QEB_MXIC)
1014 return 0;
1015
Jagan Tekid723f342015-12-16 13:48:08 +05301016 ret = write_sr(flash, qeb_status | STATUS_QEB_MXIC);
Jagan Tekif2db1bf2015-12-15 12:42:02 +05301017 if (ret < 0)
1018 return ret;
1019
1020 /* read SR and check it */
1021 ret = read_sr(flash, &qeb_status);
1022 if (!(ret >= 0 && (qeb_status & STATUS_QEB_MXIC))) {
1023 printf("SF: Macronix SR Quad bit not clear\n");
1024 return -EINVAL;
Jagan Tekie6401d82015-12-11 21:36:34 +05301025 }
1026
1027 return ret;
1028}
1029#endif
1030
1031#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
Jagan Teki7fd4fd62015-12-13 23:04:46 +05301032static int spansion_quad_enable(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +05301033{
1034 u8 qeb_status;
1035 int ret;
1036
Jagan Tekidc8ce0f2015-09-29 22:29:33 +05301037 ret = read_cr(flash, &qeb_status);
Jagan Tekie6401d82015-12-11 21:36:34 +05301038 if (ret < 0)
1039 return ret;
1040
Jagan Teki294472b2015-12-15 12:28:39 +05301041 if (qeb_status & STATUS_QEB_WINSPAN)
1042 return 0;
1043
Jagan Tekid723f342015-12-16 13:48:08 +05301044 ret = write_cr(flash, qeb_status | STATUS_QEB_WINSPAN);
Jagan Teki294472b2015-12-15 12:28:39 +05301045 if (ret < 0)
1046 return ret;
1047
1048 /* read CR and check it */
1049 ret = read_cr(flash, &qeb_status);
1050 if (!(ret >= 0 && (qeb_status & STATUS_QEB_WINSPAN))) {
1051 printf("SF: Spansion CR Quad bit not clear\n");
1052 return -EINVAL;
Jagan Tekie6401d82015-12-11 21:36:34 +05301053 }
1054
1055 return ret;
1056}
1057#endif
1058
Jagan Teki77ae47b2016-10-30 23:16:10 +05301059static const struct spi_flash_info *spi_flash_read_id(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +05301060{
Jagan Teki77ae47b2016-10-30 23:16:10 +05301061 int tmp;
Jagan Tekib1c755c2016-10-30 23:16:16 +05301062 u8 id[SPI_FLASH_MAX_ID_LEN];
Jagan Teki77ae47b2016-10-30 23:16:10 +05301063 const struct spi_flash_info *info;
1064
Jagan Tekib1c755c2016-10-30 23:16:16 +05301065 tmp = spi_flash_cmd(flash->spi, CMD_READ_ID, id, SPI_FLASH_MAX_ID_LEN);
Jagan Teki77ae47b2016-10-30 23:16:10 +05301066 if (tmp < 0) {
1067 printf("SF: error %d reading JEDEC ID\n", tmp);
1068 return ERR_PTR(tmp);
1069 }
1070
1071 info = spi_flash_ids;
1072 for (; info->name != NULL; info++) {
1073 if (info->id_len) {
1074 if (!memcmp(info->id, id, info->id_len))
1075 return info;
1076 }
1077 }
1078
1079 printf("SF: unrecognized JEDEC id bytes: %02x, %02x, %02x\n",
1080 id[0], id[1], id[2]);
1081 return ERR_PTR(-ENODEV);
1082}
1083
1084static int set_quad_mode(struct spi_flash *flash,
1085 const struct spi_flash_info *info)
1086{
1087 switch (JEDEC_MFR(info)) {
Jagan Tekie6401d82015-12-11 21:36:34 +05301088#ifdef CONFIG_SPI_FLASH_MACRONIX
1089 case SPI_FLASH_CFI_MFR_MACRONIX:
Jagan Teki7fd4fd62015-12-13 23:04:46 +05301090 return macronix_quad_enable(flash);
Jagan Tekie6401d82015-12-11 21:36:34 +05301091#endif
1092#if defined(CONFIG_SPI_FLASH_SPANSION) || defined(CONFIG_SPI_FLASH_WINBOND)
1093 case SPI_FLASH_CFI_MFR_SPANSION:
1094 case SPI_FLASH_CFI_MFR_WINBOND:
Jagan Teki7fd4fd62015-12-13 23:04:46 +05301095 return spansion_quad_enable(flash);
Jagan Tekie6401d82015-12-11 21:36:34 +05301096#endif
1097#ifdef CONFIG_SPI_FLASH_STMICRO
1098 case SPI_FLASH_CFI_MFR_STMICRO:
Cyrille Pitchen6ef52f12016-12-15 17:45:39 +01001099 debug("SF: QEB is volatile for %02x flash\n", JEDEC_MFR(info));
1100 return 0;
Jagan Tekie6401d82015-12-11 21:36:34 +05301101#endif
1102 default:
Jagan Teki77ae47b2016-10-30 23:16:10 +05301103 printf("SF: Need set QEB func for %02x flash\n",
1104 JEDEC_MFR(info));
Jagan Tekie6401d82015-12-11 21:36:34 +05301105 return -1;
1106 }
Jagan Tekie6401d82015-12-11 21:36:34 +05301107}
Jagan Tekie6401d82015-12-11 21:36:34 +05301108
1109#if CONFIG_IS_ENABLED(OF_CONTROL)
Simon Glass9b203bb2017-05-18 20:09:57 -06001110int spi_flash_decode_fdt(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +05301111{
Simon Glass0f5c2692016-01-21 19:43:54 -07001112#ifdef CONFIG_DM_SPI_FLASH
Jagan Tekie6401d82015-12-11 21:36:34 +05301113 fdt_addr_t addr;
1114 fdt_size_t size;
Jagan Tekie6401d82015-12-11 21:36:34 +05301115
Simon Glass9b203bb2017-05-18 20:09:57 -06001116 addr = dev_read_addr_size(flash->dev, "memory-map", &size);
Jagan Tekie6401d82015-12-11 21:36:34 +05301117 if (addr == FDT_ADDR_T_NONE) {
1118 debug("%s: Cannot decode address\n", __func__);
1119 return 0;
1120 }
1121
Phil Edworthy8d7899f2016-12-09 15:03:39 +00001122 if (flash->size > size) {
Jagan Tekie6401d82015-12-11 21:36:34 +05301123 debug("%s: Memory map must cover entire device\n", __func__);
1124 return -1;
1125 }
1126 flash->memory_map = map_sysmem(addr, size);
Simon Glass0f5c2692016-01-21 19:43:54 -07001127#endif
Jagan Tekie6401d82015-12-11 21:36:34 +05301128
1129 return 0;
1130}
1131#endif /* CONFIG_IS_ENABLED(OF_CONTROL) */
1132
Jagan Teki4abfb982015-12-06 21:33:32 +05301133int spi_flash_scan(struct spi_flash *flash)
Jagan Tekie6401d82015-12-11 21:36:34 +05301134{
Jagan Teki4abfb982015-12-06 21:33:32 +05301135 struct spi_slave *spi = flash->spi;
Jagan Teki77ae47b2016-10-30 23:16:10 +05301136 const struct spi_flash_info *info = NULL;
Fabien Parent4d035ac2016-12-05 19:09:10 +01001137 int ret;
Jagan Teki0cd37262015-09-29 18:06:04 +05301138
Jagan Teki77ae47b2016-10-30 23:16:10 +05301139 info = spi_flash_read_id(flash);
1140 if (IS_ERR_OR_NULL(info))
1141 return -ENOENT;
Jagan Tekie6401d82015-12-11 21:36:34 +05301142
Bin Meng7b55a802017-07-23 07:44:37 -07001143 /*
1144 * Flash powers up read-only, so clear BP# bits.
1145 *
1146 * Note on some flash (like Macronix), QE (quad enable) bit is in the
1147 * same status register as BP# bits, and we need preserve its original
1148 * value during a reboot cycle as this is required by some platforms
1149 * (like Intel ICH SPI controller working under descriptor mode).
1150 */
Jagan Teki77ae47b2016-10-30 23:16:10 +05301151 if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_ATMEL ||
Bin Meng7b55a802017-07-23 07:44:37 -07001152 (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) ||
1153 (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX)) {
1154 u8 sr = 0;
1155
1156 if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_MACRONIX) {
1157 read_sr(flash, &sr);
1158 sr &= STATUS_QEB_MXIC;
1159 }
1160 write_sr(flash, sr);
1161 }
Jagan Tekie6401d82015-12-11 21:36:34 +05301162
Jagan Teki77ae47b2016-10-30 23:16:10 +05301163 flash->name = info->name;
Jagan Tekie6401d82015-12-11 21:36:34 +05301164 flash->memory_map = spi->memory_map;
Jagan Tekie6401d82015-12-11 21:36:34 +05301165
Jagan Teki77ae47b2016-10-30 23:16:10 +05301166 if (info->flags & SST_WR)
Jagan Tekie6401d82015-12-11 21:36:34 +05301167 flash->flags |= SNOR_F_SST_WR;
1168
Jagan Tekie6401d82015-12-11 21:36:34 +05301169#ifndef CONFIG_DM_SPI_FLASH
1170 flash->write = spi_flash_cmd_write_ops;
1171#if defined(CONFIG_SPI_FLASH_SST)
1172 if (flash->flags & SNOR_F_SST_WR) {
Jagan Teki71331b32015-12-13 20:12:45 +05301173 if (spi->mode & SPI_TX_BYTE)
Jagan Tekie6401d82015-12-11 21:36:34 +05301174 flash->write = sst_write_bp;
1175 else
1176 flash->write = sst_write_wp;
1177 }
1178#endif
1179 flash->erase = spi_flash_cmd_erase_ops;
1180 flash->read = spi_flash_cmd_read_ops;
1181#endif
1182
Jagan Tekie6401d82015-12-11 21:36:34 +05301183#if defined(CONFIG_SPI_FLASH_STMICRO) || defined(CONFIG_SPI_FLASH_SST)
Jagan Tekiaf5d8192016-10-30 23:16:11 +05301184 /* NOR protection support for STmicro/Micron chips and similar */
1185 if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_STMICRO ||
1186 JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST) {
Jagan Tekie6401d82015-12-11 21:36:34 +05301187 flash->flash_lock = stm_lock;
1188 flash->flash_unlock = stm_unlock;
1189 flash->flash_is_locked = stm_is_locked;
Jagan Tekie6401d82015-12-11 21:36:34 +05301190 }
Jagan Tekiaf5d8192016-10-30 23:16:11 +05301191#endif
Jagan Tekie6401d82015-12-11 21:36:34 +05301192
Eugeniy Paltsevae0c4082018-04-10 14:40:44 +03001193/* sst26wf series block protection implementation differs from other series */
1194#if defined(CONFIG_SPI_FLASH_SST)
1195 if (JEDEC_MFR(info) == SPI_FLASH_CFI_MFR_SST && info->id[1] == 0x26) {
1196 flash->flash_lock = sst26_lock;
1197 flash->flash_unlock = sst26_unlock;
1198 flash->flash_is_locked = sst26_is_locked;
1199 }
1200#endif
1201
Jagan Tekie6401d82015-12-11 21:36:34 +05301202 /* Compute the flash size */
1203 flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
Jagan Teki77ae47b2016-10-30 23:16:10 +05301204 flash->page_size = info->page_size;
Jagan Tekie6401d82015-12-11 21:36:34 +05301205 /*
Ashish Kumar32ff4e12018-05-07 16:01:47 +05301206 * The Spansion S25FS512S, S25FL032P and S25FL064P have 256b pages,
1207 * yet use the 0x4d00 Extended JEDEC code. The rest of the Spansion
1208 * flashes with the 0x4d00 Extended JEDEC code have 512b pages.
1209 * All of the others have 256b pages.
Jagan Tekie6401d82015-12-11 21:36:34 +05301210 */
Jagan Teki77ae47b2016-10-30 23:16:10 +05301211 if (JEDEC_EXT(info) == 0x4d00) {
1212 if ((JEDEC_ID(info) != 0x0215) &&
Ashish Kumar32ff4e12018-05-07 16:01:47 +05301213 (JEDEC_ID(info) != 0x0216) &&
1214 (JEDEC_ID(info) != 0x0220))
Jagan Tekie6401d82015-12-11 21:36:34 +05301215 flash->page_size = 512;
Jagan Tekie6401d82015-12-11 21:36:34 +05301216 }
1217 flash->page_size <<= flash->shift;
Jagan Teki77ae47b2016-10-30 23:16:10 +05301218 flash->sector_size = info->sector_size << flash->shift;
Jagan Teki49e65792016-10-30 23:16:15 +05301219 flash->size = flash->sector_size * info->n_sectors << flash->shift;
Jagan Tekie6401d82015-12-11 21:36:34 +05301220#ifdef CONFIG_SF_DUAL_FLASH
1221 if (flash->dual_flash & SF_DUAL_STACKED_FLASH)
1222 flash->size <<= 1;
1223#endif
1224
Jagan Tekica40ea22016-08-08 17:23:56 +05301225#ifdef CONFIG_SPI_FLASH_USE_4K_SECTORS
Jagan Tekie6401d82015-12-11 21:36:34 +05301226 /* Compute erase sector and command */
Jagan Teki77ae47b2016-10-30 23:16:10 +05301227 if (info->flags & SECT_4K) {
Jagan Tekie6401d82015-12-11 21:36:34 +05301228 flash->erase_cmd = CMD_ERASE_4K;
1229 flash->erase_size = 4096 << flash->shift;
Jagan Tekica40ea22016-08-08 17:23:56 +05301230 } else
1231#endif
1232 {
Jagan Tekie6401d82015-12-11 21:36:34 +05301233 flash->erase_cmd = CMD_ERASE_64K;
1234 flash->erase_size = flash->sector_size;
1235 }
1236
1237 /* Now erase size becomes valid sector size */
1238 flash->sector_size = flash->erase_size;
1239
Jagan Tekib6785392016-08-08 16:50:45 +05301240 /* Look for read commands */
1241 flash->read_cmd = CMD_READ_ARRAY_FAST;
Jagan Teki96536b12016-08-08 17:12:12 +05301242 if (spi->mode & SPI_RX_SLOW)
Jagan Tekib6785392016-08-08 16:50:45 +05301243 flash->read_cmd = CMD_READ_ARRAY_SLOW;
Jagan Teki77ae47b2016-10-30 23:16:10 +05301244 else if (spi->mode & SPI_RX_QUAD && info->flags & RD_QUAD)
Jagan Tekib6785392016-08-08 16:50:45 +05301245 flash->read_cmd = CMD_READ_QUAD_OUTPUT_FAST;
Jagan Teki77ae47b2016-10-30 23:16:10 +05301246 else if (spi->mode & SPI_RX_DUAL && info->flags & RD_DUAL)
Jagan Tekib6785392016-08-08 16:50:45 +05301247 flash->read_cmd = CMD_READ_DUAL_OUTPUT_FAST;
Jagan Tekie6401d82015-12-11 21:36:34 +05301248
Jagan Tekib6785392016-08-08 16:50:45 +05301249 /* Look for write commands */
Jagan Teki77ae47b2016-10-30 23:16:10 +05301250 if (info->flags & WR_QPP && spi->mode & SPI_TX_QUAD)
Jagan Tekie6401d82015-12-11 21:36:34 +05301251 flash->write_cmd = CMD_QUAD_PAGE_PROGRAM;
1252 else
1253 /* Go for default supported write cmd */
1254 flash->write_cmd = CMD_PAGE_PROGRAM;
1255
1256 /* Set the quad enable bit - only for quad commands */
1257 if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
1258 (flash->read_cmd == CMD_READ_QUAD_IO_FAST) ||
1259 (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
Jagan Teki77ae47b2016-10-30 23:16:10 +05301260 ret = set_quad_mode(flash, info);
Jagan Tekie6401d82015-12-11 21:36:34 +05301261 if (ret) {
Jagan Teki77ae47b2016-10-30 23:16:10 +05301262 debug("SF: Fail to set QEB for %02x\n",
1263 JEDEC_MFR(info));
Jagan Tekie6401d82015-12-11 21:36:34 +05301264 return -EINVAL;
1265 }
1266 }
1267
1268 /* Read dummy_byte: dummy byte is determined based on the
1269 * dummy cycles of a particular command.
1270 * Fast commands - dummy_byte = dummy_cycles/8
1271 * I/O commands- dummy_byte = (dummy_cycles * no.of lines)/8
1272 * For I/O commands except cmd[0] everything goes on no.of lines
1273 * based on particular command but incase of fast commands except
1274 * data all go on single line irrespective of command.
1275 */
1276 switch (flash->read_cmd) {
1277 case CMD_READ_QUAD_IO_FAST:
1278 flash->dummy_byte = 2;
1279 break;
1280 case CMD_READ_ARRAY_SLOW:
1281 flash->dummy_byte = 0;
1282 break;
1283 default:
1284 flash->dummy_byte = 1;
1285 }
1286
1287#ifdef CONFIG_SPI_FLASH_STMICRO
Jagan Teki77ae47b2016-10-30 23:16:10 +05301288 if (info->flags & E_FSR)
Jagan Tekie6401d82015-12-11 21:36:34 +05301289 flash->flags |= SNOR_F_USE_FSR;
1290#endif
1291
1292 /* Configure the BAR - discover bank cmds and read current bank */
1293#ifdef CONFIG_SPI_FLASH_BAR
Jagan Tekiff0e43e2016-10-30 23:16:25 +05301294 ret = read_bar(flash, info);
Jagan Tekie6401d82015-12-11 21:36:34 +05301295 if (ret < 0)
1296 return ret;
1297#endif
1298
Simon Glass3fb33392016-11-13 14:22:01 -07001299#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass9b203bb2017-05-18 20:09:57 -06001300 ret = spi_flash_decode_fdt(flash);
Jagan Tekie6401d82015-12-11 21:36:34 +05301301 if (ret) {
1302 debug("SF: FDT decode error\n");
1303 return -EINVAL;
1304 }
1305#endif
1306
1307#ifndef CONFIG_SPL_BUILD
1308 printf("SF: Detected %s with page size ", flash->name);
1309 print_size(flash->page_size, ", erase size ");
1310 print_size(flash->erase_size, ", total ");
1311 print_size(flash->size, "");
1312 if (flash->memory_map)
1313 printf(", mapped at %p", flash->memory_map);
1314 puts("\n");
1315#endif
1316
1317#ifndef CONFIG_SPI_FLASH_BAR
1318 if (((flash->dual_flash == SF_SINGLE_FLASH) &&
1319 (flash->size > SPI_FLASH_16MB_BOUN)) ||
1320 ((flash->dual_flash > SF_SINGLE_FLASH) &&
1321 (flash->size > SPI_FLASH_16MB_BOUN << 1))) {
1322 puts("SF: Warning - Only lower 16MiB accessible,");
1323 puts(" Full access #define CONFIG_SPI_FLASH_BAR\n");
1324 }
1325#endif
1326
Fabien Parent4d035ac2016-12-05 19:09:10 +01001327 return 0;
Jagan Tekie6401d82015-12-11 21:36:34 +05301328}