blob: e5394d6e0e79cb6ad1c679a41004871579db4b04 [file] [log] [blame]
David Hendricksd1c55d72010-08-24 15:14:19 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
David Hendricksf7924d12010-06-10 21:26:44 -070021#include <stdlib.h>
22#include <string.h>
23
24#include "flash.h"
25#include "flashchips.h"
26#include "chipdrivers.h"
Louis Yung-Chieh Lo52aa9302010-09-06 10:45:02 +080027#include "spi.h"
David Hendricks23cd7782010-08-25 12:42:38 -070028#include "writeprotect.h"
David Hendricksf7924d12010-06-10 21:26:44 -070029
David Hendricks1c09f802012-10-03 11:03:48 -070030/*
David Hendricksf7924d12010-06-10 21:26:44 -070031 * The following procedures rely on look-up tables to match the user-specified
32 * range with the chip's supported ranges. This turned out to be the most
33 * elegant approach since diferent flash chips use different levels of
34 * granularity and methods to determine protected ranges. In other words,
David Hendrickse0512a72014-07-15 20:30:47 -070035 * be stupid and simple since clever arithmetic will not work for many chips.
David Hendricksf7924d12010-06-10 21:26:44 -070036 */
37
38struct wp_range {
39 unsigned int start; /* starting address */
40 unsigned int len; /* len */
41};
42
43enum bit_state {
44 OFF = 0,
45 ON = 1,
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080046 X = -1 /* don't care. Must be bigger than max # of bp. */
David Hendricksf7924d12010-06-10 21:26:44 -070047};
48
David Hendrickse0512a72014-07-15 20:30:47 -070049/*
50 * Generic write-protection schema for 25-series SPI flash chips. This assumes
51 * there is a status register that contains one or more consecutive bits which
52 * determine which address range is protected.
53 */
54
55struct status_register_layout {
56 int bp0_pos; /* position of BP0 */
57 int bp_bits; /* number of block protect bits */
58 int srp_pos; /* position of status register protect enable bit */
59};
60
61struct generic_range {
David Hendricks148a4bf2015-03-13 21:02:42 -070062 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -070063 unsigned int bp; /* block protect bitfield */
64 struct wp_range range;
65};
66
67struct generic_wp {
68 struct status_register_layout sr1; /* status register 1 */
69 struct generic_range *ranges;
David Hendricks148a4bf2015-03-13 21:02:42 -070070
71 /*
72 * Some chips store modifier bits in one or more special control
73 * registers instead of the status register like many older SPI NOR
74 * flash chips did. get_modifier_bits() and set_modifier_bits() will do
75 * any chip-specific operations necessary to get/set these bit values.
76 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -070077 int (*get_modifier_bits)(const struct flashctx *flash,
David Hendricks148a4bf2015-03-13 21:02:42 -070078 struct generic_modifier_bits *m);
Souvik Ghoshd75cd672016-06-17 14:21:39 -070079 int (*set_modifier_bits)(const struct flashctx *flash,
David Hendricks148a4bf2015-03-13 21:02:42 -070080 struct generic_modifier_bits *m);
David Hendrickse0512a72014-07-15 20:30:47 -070081};
82
83/*
84 * The following ranges and functions are useful for representing Winbond-
85 * style writeprotect schema in which there are typically 5 bits of
86 * relevant information stored in status register 1:
87 * sec: This bit indicates the units (sectors vs. blocks)
88 * tb: The top-bottom bit indicates if the affected range is at the top of
89 * the flash memory's address space or at the bottom.
90 * bp[2:0]: The number of affected sectors/blocks.
91 */
David Hendricksf7924d12010-06-10 21:26:44 -070092struct w25q_range {
93 enum bit_state sec; /* if 1, bp[2:0] describe sectors */
94 enum bit_state tb; /* top/bottom select */
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +080095 int bp; /* block protect bitfield */
David Hendricksf7924d12010-06-10 21:26:44 -070096 struct wp_range range;
97};
98
David Hendrickse0512a72014-07-15 20:30:47 -070099/*
100 * Mask to extract write-protect enable and range bits
101 * Status register 1:
102 * SRP0: bit 7
103 * range(BP2-BP0): bit 4-2
104 * Status register 2:
105 * SRP1: bit 1
106 */
107#define MASK_WP_AREA (0x9C)
108#define MASK_WP2_AREA (0x01)
109
David Hendricks57566ed2010-08-16 18:24:45 -0700110struct w25q_range en25f40_ranges[] = {
111 { X, X, 0, {0, 0} }, /* none */
112 { 0, 0, 0x1, {0x000000, 504 * 1024} },
113 { 0, 0, 0x2, {0x000000, 496 * 1024} },
114 { 0, 0, 0x3, {0x000000, 480 * 1024} },
115 { 0, 0, 0x4, {0x000000, 448 * 1024} },
116 { 0, 0, 0x5, {0x000000, 384 * 1024} },
117 { 0, 0, 0x6, {0x000000, 256 * 1024} },
118 { 0, 0, 0x7, {0x000000, 512 * 1024} },
119};
120
David Hendrickse185bf22011-05-24 15:34:18 -0700121struct w25q_range en25q40_ranges[] = {
122 { 0, 0, 0, {0, 0} }, /* none */
123 { 0, 0, 0x1, {0x000000, 504 * 1024} },
124 { 0, 0, 0x2, {0x000000, 496 * 1024} },
125 { 0, 0, 0x3, {0x000000, 480 * 1024} },
126
127 { 0, 1, 0x0, {0x000000, 448 * 1024} },
128 { 0, 1, 0x1, {0x000000, 384 * 1024} },
129 { 0, 1, 0x2, {0x000000, 256 * 1024} },
130 { 0, 1, 0x3, {0x000000, 512 * 1024} },
131};
132
133struct w25q_range en25q80_ranges[] = {
134 { 0, 0, 0, {0, 0} }, /* none */
135 { 0, 0, 0x1, {0x000000, 1016 * 1024} },
136 { 0, 0, 0x2, {0x000000, 1008 * 1024} },
137 { 0, 0, 0x3, {0x000000, 992 * 1024} },
138 { 0, 0, 0x4, {0x000000, 960 * 1024} },
139 { 0, 0, 0x5, {0x000000, 896 * 1024} },
140 { 0, 0, 0x6, {0x000000, 768 * 1024} },
141 { 0, 0, 0x7, {0x000000, 1024 * 1024} },
142};
143
144struct w25q_range en25q32_ranges[] = {
145 { 0, 0, 0, {0, 0} }, /* none */
146 { 0, 0, 0x1, {0x000000, 4032 * 1024} },
147 { 0, 0, 0x2, {0x000000, 3968 * 1024} },
148 { 0, 0, 0x3, {0x000000, 3840 * 1024} },
149 { 0, 0, 0x4, {0x000000, 3584 * 1024} },
150 { 0, 0, 0x5, {0x000000, 3072 * 1024} },
151 { 0, 0, 0x6, {0x000000, 2048 * 1024} },
152 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
153
154 { 0, 1, 0, {0, 0} }, /* none */
155 { 0, 1, 0x1, {0x010000, 4032 * 1024} },
156 { 0, 1, 0x2, {0x020000, 3968 * 1024} },
157 { 0, 1, 0x3, {0x040000, 3840 * 1024} },
158 { 0, 1, 0x4, {0x080000, 3584 * 1024} },
159 { 0, 1, 0x5, {0x100000, 3072 * 1024} },
160 { 0, 1, 0x6, {0x200000, 2048 * 1024} },
161 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
162};
163
164struct w25q_range en25q64_ranges[] = {
165 { 0, 0, 0, {0, 0} }, /* none */
166 { 0, 0, 0x1, {0x000000, 8128 * 1024} },
167 { 0, 0, 0x2, {0x000000, 8064 * 1024} },
168 { 0, 0, 0x3, {0x000000, 7936 * 1024} },
169 { 0, 0, 0x4, {0x000000, 7680 * 1024} },
170 { 0, 0, 0x5, {0x000000, 7168 * 1024} },
171 { 0, 0, 0x6, {0x000000, 6144 * 1024} },
172 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
173
174 { 0, 1, 0, {0, 0} }, /* none */
175 { 0, 1, 0x1, {0x010000, 8128 * 1024} },
176 { 0, 1, 0x2, {0x020000, 8064 * 1024} },
177 { 0, 1, 0x3, {0x040000, 7936 * 1024} },
178 { 0, 1, 0x4, {0x080000, 7680 * 1024} },
179 { 0, 1, 0x5, {0x100000, 7168 * 1024} },
180 { 0, 1, 0x6, {0x200000, 6144 * 1024} },
181 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
182};
183
184struct w25q_range en25q128_ranges[] = {
185 { 0, 0, 0, {0, 0} }, /* none */
186 { 0, 0, 0x1, {0x000000, 16320 * 1024} },
187 { 0, 0, 0x2, {0x000000, 16256 * 1024} },
188 { 0, 0, 0x3, {0x000000, 16128 * 1024} },
189 { 0, 0, 0x4, {0x000000, 15872 * 1024} },
190 { 0, 0, 0x5, {0x000000, 15360 * 1024} },
191 { 0, 0, 0x6, {0x000000, 14336 * 1024} },
192 { 0, 0, 0x7, {0x000000, 16384 * 1024} },
193
194 { 0, 1, 0, {0, 0} }, /* none */
195 { 0, 1, 0x1, {0x010000, 16320 * 1024} },
196 { 0, 1, 0x2, {0x020000, 16256 * 1024} },
197 { 0, 1, 0x3, {0x040000, 16128 * 1024} },
198 { 0, 1, 0x4, {0x080000, 15872 * 1024} },
199 { 0, 1, 0x5, {0x100000, 15360 * 1024} },
200 { 0, 1, 0x6, {0x200000, 14336 * 1024} },
201 { 0, 1, 0x7, {0x000000, 16384 * 1024} },
202};
203
Marc Jonesb2f90022014-04-29 17:37:23 -0600204struct w25q_range en25s64_ranges[] = {
205 { 0, 0, 0, {0, 0} }, /* none */
206 { 0, 0, 0x1, {0x000000, 8064 * 1024} },
207 { 0, 0, 0x2, {0x000000, 7936 * 1024} },
208 { 0, 0, 0x3, {0x000000, 7680 * 1024} },
209 { 0, 0, 0x4, {0x000000, 7168 * 1024} },
210 { 0, 0, 0x5, {0x000000, 6144 * 1024} },
211 { 0, 0, 0x6, {0x000000, 4096 * 1024} },
212 { 0, 0, 0x7, {0x000000, 8192 * 1024} },
213
214 { 0, 1, 0, {0, 0} }, /* none */
215 { 0, 1, 0x1, {0x7e0000, 128 * 1024} },
216 { 0, 1, 0x2, {0x7c0000, 256 * 1024} },
217 { 0, 1, 0x3, {0x780000, 512 * 1024} },
218 { 0, 1, 0x4, {0x700000, 1024 * 1024} },
219 { 0, 1, 0x5, {0x600000, 2048 * 1024} },
220 { 0, 1, 0x6, {0x400000, 4096 * 1024} },
221 { 0, 1, 0x7, {0x000000, 8192 * 1024} },
222};
223
David Hendricksf8f00c72011-02-01 12:39:46 -0800224/* mx25l1005 ranges also work for the mx25l1005c */
225static struct w25q_range mx25l1005_ranges[] = {
226 { X, X, 0, {0, 0} }, /* none */
227 { X, X, 0x1, {0x010000, 64 * 1024} },
228 { X, X, 0x2, {0x000000, 128 * 1024} },
229 { X, X, 0x3, {0x000000, 128 * 1024} },
230};
231
232static struct w25q_range mx25l2005_ranges[] = {
233 { X, X, 0, {0, 0} }, /* none */
234 { X, X, 0x1, {0x030000, 64 * 1024} },
235 { X, X, 0x2, {0x020000, 128 * 1024} },
236 { X, X, 0x3, {0x000000, 256 * 1024} },
237};
238
239static struct w25q_range mx25l4005_ranges[] = {
240 { X, X, 0, {0, 0} }, /* none */
241 { X, X, 0x1, {0x070000, 64 * 1 * 1024} }, /* block 7 */
242 { X, X, 0x2, {0x060000, 64 * 2 * 1024} }, /* blocks 6-7 */
243 { X, X, 0x3, {0x040000, 64 * 4 * 1024} }, /* blocks 4-7 */
244 { X, X, 0x4, {0x000000, 512 * 1024} },
245 { X, X, 0x5, {0x000000, 512 * 1024} },
246 { X, X, 0x6, {0x000000, 512 * 1024} },
247 { X, X, 0x7, {0x000000, 512 * 1024} },
248};
249
250static struct w25q_range mx25l8005_ranges[] = {
251 { X, X, 0, {0, 0} }, /* none */
252 { X, X, 0x1, {0x0f0000, 64 * 1 * 1024} }, /* block 15 */
253 { X, X, 0x2, {0x0e0000, 64 * 2 * 1024} }, /* blocks 14-15 */
254 { X, X, 0x3, {0x0c0000, 64 * 4 * 1024} }, /* blocks 12-15 */
255 { X, X, 0x4, {0x080000, 64 * 8 * 1024} }, /* blocks 8-15 */
256 { X, X, 0x5, {0x000000, 1024 * 1024} },
257 { X, X, 0x6, {0x000000, 1024 * 1024} },
258 { X, X, 0x7, {0x000000, 1024 * 1024} },
259};
260
261#if 0
262/* FIXME: mx25l1605 has the same IDs as the mx25l1605d */
263static struct w25q_range mx25l1605_ranges[] = {
264 { X, X, 0, {0, 0} }, /* none */
265 { X, X, 0x1, {0x1f0000, 64 * 1024} }, /* block 31 */
266 { X, X, 0x2, {0x1e0000, 128 * 1024} }, /* blocks 30-31 */
267 { X, X, 0x3, {0x1c0000, 256 * 1024} }, /* blocks 28-31 */
268 { X, X, 0x4, {0x180000, 512 * 1024} }, /* blocks 24-31 */
269 { X, X, 0x4, {0x100000, 1024 * 1024} }, /* blocks 16-31 */
270 { X, X, 0x6, {0x000000, 2048 * 1024} },
271 { X, X, 0x7, {0x000000, 2048 * 1024} },
272};
273#endif
274
275#if 0
276/* FIXME: mx25l6405 has the same IDs as the mx25l6405d */
277static struct w25q_range mx25l6405_ranges[] = {
278 { X, 0, 0, {0, 0} }, /* none */
279 { X, 0, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
280 { X, 0, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
281 { X, 0, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
282 { X, 0, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
283 { X, 0, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
284 { X, 0, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
285 { X, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
286
287 { X, 1, 0x0, {0x000000, 8192 * 1024} },
288 { X, 1, 0x1, {0x000000, 8192 * 1024} },
289 { X, 1, 0x2, {0x000000, 8192 * 1024} },
290 { X, 1, 0x3, {0x000000, 8192 * 1024} },
291 { X, 1, 0x4, {0x000000, 8192 * 1024} },
292 { X, 1, 0x5, {0x000000, 8192 * 1024} },
293 { X, 1, 0x6, {0x000000, 8192 * 1024} },
294 { X, 1, 0x7, {0x000000, 8192 * 1024} },
295};
296#endif
297
298static struct w25q_range mx25l1605d_ranges[] = {
299 { X, 0, 0, {0, 0} }, /* none */
300 { X, 0, 0x1, {0x1f0000, 64 * 1 * 1024} }, /* block 31 */
301 { X, 0, 0x2, {0x1e0000, 64 * 2 * 1024} }, /* blocks 30-31 */
302 { X, 0, 0x3, {0x1c0000, 64 * 4 * 1024} }, /* blocks 28-31 */
303 { X, 0, 0x4, {0x180000, 64 * 8 * 1024} }, /* blocks 24-31 */
304 { X, 0, 0x5, {0x100000, 64 * 16 * 1024} }, /* blocks 16-31 */
305 { X, 0, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
306 { X, 0, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
307
308 { X, 1, 0x0, {0x000000, 2048 * 1024} },
309 { X, 1, 0x1, {0x000000, 2048 * 1024} },
310 { X, 1, 0x2, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
311 { X, 1, 0x3, {0x000000, 64 * 24 * 1024} }, /* blocks 0-23 */
312 { X, 1, 0x4, {0x000000, 64 * 28 * 1024} }, /* blocks 0-27 */
313 { X, 1, 0x5, {0x000000, 64 * 30 * 1024} }, /* blocks 0-29 */
314 { X, 1, 0x6, {0x000000, 64 * 31 * 1024} }, /* blocks 0-30 */
315 { X, 1, 0x7, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
316};
317
318/* FIXME: Is there an mx25l3205 (without a trailing letter)? */
David Hendricksac72e362010-08-16 18:20:03 -0700319static struct w25q_range mx25l3205d_ranges[] = {
320 { X, 0, 0, {0, 0} }, /* none */
321 { X, 0, 0x1, {0x3f0000, 64 * 1024} },
322 { X, 0, 0x2, {0x3e0000, 128 * 1024} },
323 { X, 0, 0x3, {0x3c0000, 256 * 1024} },
324 { X, 0, 0x4, {0x380000, 512 * 1024} },
325 { X, 0, 0x5, {0x300000, 1024 * 1024} },
326 { X, 0, 0x6, {0x200000, 2048 * 1024} },
327 { X, 0, 0x7, {0x000000, 4096 * 1024} },
328
329 { X, 1, 0x0, {0x000000, 4096 * 1024} },
330 { X, 1, 0x1, {0x000000, 2048 * 1024} },
331 { X, 1, 0x2, {0x000000, 3072 * 1024} },
332 { X, 1, 0x3, {0x000000, 3584 * 1024} },
333 { X, 1, 0x4, {0x000000, 3840 * 1024} },
334 { X, 1, 0x5, {0x000000, 3968 * 1024} },
335 { X, 1, 0x6, {0x000000, 4032 * 1024} },
336 { X, 1, 0x7, {0x000000, 4096 * 1024} },
337};
338
Vincent Palatin87e092a2013-02-28 15:46:14 -0800339static struct w25q_range mx25u3235e_ranges[] = {
340 { X, 0, 0, {0, 0} }, /* none */
341 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
342 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
343 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
344 { 0, 0, 0x4, {0x380000, 512 * 1024} },
345 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
346 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
347 { 0, 0, 0x7, {0x000000, 4096 * 1024} },
348
349 { 0, 1, 0x0, {0x000000, 4096 * 1024} },
350 { 0, 1, 0x1, {0x000000, 2048 * 1024} },
351 { 0, 1, 0x2, {0x000000, 3072 * 1024} },
352 { 0, 1, 0x3, {0x000000, 3584 * 1024} },
353 { 0, 1, 0x4, {0x000000, 3840 * 1024} },
354 { 0, 1, 0x5, {0x000000, 3968 * 1024} },
355 { 0, 1, 0x6, {0x000000, 4032 * 1024} },
356 { 0, 1, 0x7, {0x000000, 4096 * 1024} },
357};
358
Jongpil66a96492014-08-14 17:59:06 +0900359static struct w25q_range mx25u6435e_ranges[] = {
360 { X, 0, 0, {0, 0} }, /* none */
361 { 0, 0, 0x1, {0x7f0000, 1 * 64 * 1024} }, /* block 127 */
362 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
363 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
364 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
365 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
366 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
367 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
368
369 { 0, 1, 0x0, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
370 { 0, 1, 0x1, {0x000000, 96 * 64 * 1024} }, /* blocks 0-95 */
371 { 0, 1, 0x2, {0x000000, 112 * 64 * 1024} }, /* blocks 0-111 */
372 { 0, 1, 0x3, {0x000000, 120 * 64 * 1024} }, /* blocks 0-119 */
373 { 0, 1, 0x4, {0x000000, 124 * 64 * 1024} }, /* blocks 0-123 */
374 { 0, 1, 0x5, {0x000000, 126 * 64 * 1024} }, /* blocks 0-125 */
375 { 0, 1, 0x6, {0x000000, 127 * 64 * 1024} }, /* blocks 0-126 */
376 { 0, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* blocks 0-127 */
377};
378
David Hendricksbfa624b2012-07-24 12:47:59 -0700379static struct w25q_range n25q064_ranges[] = {
David Hendricksfe9123b2015-04-21 13:18:31 -0700380 /*
381 * Note: For N25Q064, sec (usually in bit position 6) is called BP3
382 * (block protect bit 3). It is only useful when all blocks are to
383 * be write-protected.
384 */
David Hendricks42a549a2015-04-22 11:25:07 -0700385 { 0, 0, 0, {0, 0} }, /* none */
David Hendricksbfa624b2012-07-24 12:47:59 -0700386
387 { 0, 0, 0x1, {0x7f0000, 64 * 1024} }, /* block 127 */
388 { 0, 0, 0x2, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
389 { 0, 0, 0x3, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
390 { 0, 0, 0x4, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
391 { 0, 0, 0x5, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
392 { 0, 0, 0x6, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
393 { 0, 0, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
394
David Hendricksfe9123b2015-04-21 13:18:31 -0700395 { 0, 1, 0x1, {0x000000, 64 * 1024} }, /* block 0 */
396 { 0, 1, 0x2, {0x000000, 2 * 64 * 1024} }, /* blocks 0-1 */
397 { 0, 1, 0x3, {0x000000, 4 * 64 * 1024} }, /* blocks 0-3 */
398 { 0, 1, 0x4, {0x000000, 8 * 64 * 1024} }, /* blocks 0-7 */
399 { 0, 1, 0x5, {0x000000, 16 * 64 * 1024} }, /* blocks 0-15 */
400 { 0, 1, 0x6, {0x000000, 32 * 64 * 1024} }, /* blocks 0-31 */
401 { 0, 1, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
David Hendricksbfa624b2012-07-24 12:47:59 -0700402
403 { X, 1, 0x0, {0x000000, 128 * 64 * 1024} }, /* all */
404 { X, 1, 0x1, {0x000000, 128 * 64 * 1024} }, /* all */
405 { X, 1, 0x2, {0x000000, 128 * 64 * 1024} }, /* all */
406 { X, 1, 0x3, {0x000000, 128 * 64 * 1024} }, /* all */
407 { X, 1, 0x4, {0x000000, 128 * 64 * 1024} }, /* all */
408 { X, 1, 0x5, {0x000000, 128 * 64 * 1024} }, /* all */
409 { X, 1, 0x6, {0x000000, 128 * 64 * 1024} }, /* all */
410 { X, 1, 0x7, {0x000000, 128 * 64 * 1024} }, /* all */
411};
412
David Hendricksf7924d12010-06-10 21:26:44 -0700413static struct w25q_range w25q16_ranges[] = {
414 { X, X, 0, {0, 0} }, /* none */
415 { 0, 0, 0x1, {0x1f0000, 64 * 1024} },
416 { 0, 0, 0x2, {0x1e0000, 128 * 1024} },
417 { 0, 0, 0x3, {0x1c0000, 256 * 1024} },
418 { 0, 0, 0x4, {0x180000, 512 * 1024} },
419 { 0, 0, 0x5, {0x100000, 1024 * 1024} },
420
421 { 0, 1, 0x1, {0x000000, 64 * 1024} },
422 { 0, 1, 0x2, {0x000000, 128 * 1024} },
423 { 0, 1, 0x3, {0x000000, 256 * 1024} },
424 { 0, 1, 0x4, {0x000000, 512 * 1024} },
425 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
426 { X, X, 0x6, {0x000000, 2048 * 1024} },
427 { X, X, 0x7, {0x000000, 2048 * 1024} },
428
429 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
430 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
431 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
432 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
433 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
434
435 { 1, 1, 0x1, {0x000000, 4 * 1024} },
436 { 1, 1, 0x2, {0x000000, 8 * 1024} },
437 { 1, 1, 0x3, {0x000000, 16 * 1024} },
438 { 1, 1, 0x4, {0x000000, 32 * 1024} },
439 { 1, 1, 0x5, {0x000000, 32 * 1024} },
440};
441
442static struct w25q_range w25q32_ranges[] = {
443 { X, X, 0, {0, 0} }, /* none */
444 { 0, 0, 0x1, {0x3f0000, 64 * 1024} },
445 { 0, 0, 0x2, {0x3e0000, 128 * 1024} },
446 { 0, 0, 0x3, {0x3c0000, 256 * 1024} },
447 { 0, 0, 0x4, {0x380000, 512 * 1024} },
448 { 0, 0, 0x5, {0x300000, 1024 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700449 { 0, 0, 0x6, {0x200000, 2048 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700450
451 { 0, 1, 0x1, {0x000000, 64 * 1024} },
452 { 0, 1, 0x2, {0x000000, 128 * 1024} },
453 { 0, 1, 0x3, {0x000000, 256 * 1024} },
454 { 0, 1, 0x4, {0x000000, 512 * 1024} },
455 { 0, 1, 0x5, {0x000000, 1024 * 1024} },
456 { 0, 1, 0x6, {0x000000, 2048 * 1024} },
457 { X, X, 0x7, {0x000000, 4096 * 1024} },
458
459 { 1, 0, 0x1, {0x3ff000, 4 * 1024} },
460 { 1, 0, 0x2, {0x3fe000, 8 * 1024} },
461 { 1, 0, 0x3, {0x3fc000, 16 * 1024} },
462 { 1, 0, 0x4, {0x3f8000, 32 * 1024} },
463 { 1, 0, 0x5, {0x3f8000, 32 * 1024} },
464
465 { 1, 1, 0x1, {0x000000, 4 * 1024} },
466 { 1, 1, 0x2, {0x000000, 8 * 1024} },
467 { 1, 1, 0x3, {0x000000, 16 * 1024} },
468 { 1, 1, 0x4, {0x000000, 32 * 1024} },
469 { 1, 1, 0x5, {0x000000, 32 * 1024} },
470};
471
472static struct w25q_range w25q80_ranges[] = {
473 { X, X, 0, {0, 0} }, /* none */
474 { 0, 0, 0x1, {0x0f0000, 64 * 1024} },
475 { 0, 0, 0x2, {0x0e0000, 128 * 1024} },
476 { 0, 0, 0x3, {0x0c0000, 256 * 1024} },
477 { 0, 0, 0x4, {0x080000, 512 * 1024} },
478
479 { 0, 1, 0x1, {0x000000, 64 * 1024} },
480 { 0, 1, 0x2, {0x000000, 128 * 1024} },
481 { 0, 1, 0x3, {0x000000, 256 * 1024} },
482 { 0, 1, 0x4, {0x000000, 512 * 1024} },
David Hendricks05653ff2010-06-15 16:05:12 -0700483 { X, X, 0x6, {0x000000, 1024 * 1024} },
484 { X, X, 0x7, {0x000000, 1024 * 1024} },
David Hendricksf7924d12010-06-10 21:26:44 -0700485
486 { 1, 0, 0x1, {0x1ff000, 4 * 1024} },
487 { 1, 0, 0x2, {0x1fe000, 8 * 1024} },
488 { 1, 0, 0x3, {0x1fc000, 16 * 1024} },
489 { 1, 0, 0x4, {0x1f8000, 32 * 1024} },
490 { 1, 0, 0x5, {0x1f8000, 32 * 1024} },
491
492 { 1, 1, 0x1, {0x000000, 4 * 1024} },
493 { 1, 1, 0x2, {0x000000, 8 * 1024} },
494 { 1, 1, 0x3, {0x000000, 16 * 1024} },
495 { 1, 1, 0x4, {0x000000, 32 * 1024} },
496 { 1, 1, 0x5, {0x000000, 32 * 1024} },
497};
498
David Hendricks2c4a76c2010-06-28 14:00:43 -0700499static struct w25q_range w25q64_ranges[] = {
500 { X, X, 0, {0, 0} }, /* none */
501
502 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
503 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
504 { 0, 0, 0x3, {0x780000, 512 * 1024} },
505 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
506 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
507 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
508
509 { 0, 1, 0x1, {0x000000, 128 * 1024} },
510 { 0, 1, 0x2, {0x000000, 256 * 1024} },
511 { 0, 1, 0x3, {0x000000, 512 * 1024} },
512 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
513 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
514 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
515 { X, X, 0x7, {0x000000, 8192 * 1024} },
516
517 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
518 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
519 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
520 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
521 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
522
523 { 1, 1, 0x1, {0x000000, 4 * 1024} },
524 { 1, 1, 0x2, {0x000000, 8 * 1024} },
525 { 1, 1, 0x3, {0x000000, 16 * 1024} },
526 { 1, 1, 0x4, {0x000000, 32 * 1024} },
527 { 1, 1, 0x5, {0x000000, 32 * 1024} },
528};
529
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700530static struct w25q_range w25rq128_cmp0_ranges[] = {
531 { X, X, 0, {0, 0} }, /* NONE */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530532
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700533 { 0, 0, 0x1, {0xfc0000, 256 * 1024} }, /* Upper 1/64 */
534 { 0, 0, 0x2, {0xf80000, 512 * 1024} }, /* Upper 1/32 */
535 { 0, 0, 0x3, {0xf00000, 1024 * 1024} }, /* Upper 1/16 */
536 { 0, 0, 0x4, {0xe00000, 2048 * 1024} }, /* Upper 1/8 */
537 { 0, 0, 0x5, {0xc00000, 4096 * 1024} }, /* Upper 1/4 */
538 { 0, 0, 0x6, {0x800000, 8192 * 1024} }, /* Upper 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530539
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700540 { 0, 1, 0x1, {0x000000, 256 * 1024} }, /* Lower 1/64 */
541 { 0, 1, 0x2, {0x000000, 512 * 1024} }, /* Lower 1/32 */
542 { 0, 1, 0x3, {0x000000, 1024 * 1024} }, /* Lower 1/16 */
543 { 0, 1, 0x4, {0x000000, 2048 * 1024} }, /* Lower 1/8 */
544 { 0, 1, 0x5, {0x000000, 4096 * 1024} }, /* Lower 1/4 */
545 { 0, 1, 0x6, {0x000000, 8192 * 1024} }, /* Lower 1/2 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530546
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700547 { X, X, 0x7, {0x000000, 16384 * 1024} }, /* ALL */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530548
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700549 { 1, 0, 0x1, {0xfff000, 4 * 1024} }, /* Upper 1/4096 */
550 { 1, 0, 0x2, {0xffe000, 8 * 1024} }, /* Upper 1/2048 */
551 { 1, 0, 0x3, {0xffc000, 16 * 1024} }, /* Upper 1/1024 */
552 { 1, 0, 0x4, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
553 { 1, 0, 0x5, {0xff8000, 32 * 1024} }, /* Upper 1/512 */
554
555 { 1, 1, 0x1, {0x000000, 4 * 1024} }, /* Lower 1/4096 */
556 { 1, 1, 0x2, {0x000000, 8 * 1024} }, /* Lower 1/2048 */
557 { 1, 1, 0x3, {0x000000, 16 * 1024} }, /* Lower 1/1024 */
558 { 1, 1, 0x4, {0x000000, 32 * 1024} }, /* Lower 1/512 */
559 { 1, 1, 0x5, {0x000000, 32 * 1024} }, /* Lower 1/512 */
560};
561
562static struct w25q_range w25rq128_cmp1_ranges[] = {
563 { X, X, 0x0, {0x000000, 16 * 1024 * 1024} }, /* ALL */
564
565 { 0, 0, 0x1, {0x000000, 16128 * 1024} }, /* Lower 63/64 */
566 { 0, 0, 0x2, {0x000000, 15872 * 1024} }, /* Lower 31/32 */
567 { 0, 0, 0x3, {0x000000, 15 * 1024 * 1024} }, /* Lower 15/16 */
568 { 0, 0, 0x4, {0x000000, 14 * 1024 * 1024} }, /* Lower 7/8 */
569 { 0, 0, 0x5, {0x000000, 12 * 1024 * 1024} }, /* Lower 3/4 */
570 { 0, 0, 0x6, {0x000000, 8 * 1024 * 1024} }, /* Lower 1/2 */
571
572 { 0, 1, 0x1, {0x040000, 16128 * 1024} }, /* Upper 63/64 */
573 { 0, 1, 0x2, {0x080000, 15872 * 1024} }, /* Upper 31/32 */
574 { 0, 1, 0x3, {0x100000, 15 * 1024 * 1024} }, /* Upper 15/16 */
575 { 0, 1, 0x4, {0x200000, 14 * 1024 * 1024} }, /* Upper 7/8 */
576 { 0, 1, 0x5, {0x400000, 12 * 1024 * 1024} }, /* Upper 3/4 */
577 { 0, 1, 0x6, {0x800000, 8 * 1024 * 1024} }, /* Upper 1/2 */
578
579 { X, X, 0x7, {0x000000, 0} }, /* NONE */
580
581 { 1, 0, 0x1, {0x000000, 16380 * 1024} }, /* Lower 4095/4096 */
582 { 1, 0, 0x2, {0x000000, 16376 * 1024} }, /* Lower 2048/2048 */
583 { 1, 0, 0x3, {0x000000, 16368 * 1024} }, /* Lower 1023/1024 */
584 { 1, 0, 0x4, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
585 { 1, 0, 0x5, {0x000000, 16352 * 1024} }, /* Lower 511/512 */
586
587 { 1, 1, 0x1, {0x001000, 16380 * 1024} }, /* Upper 4095/4096 */
588 { 1, 1, 0x2, {0x002000, 16376 * 1024} }, /* Upper 2047/2048 */
589 { 1, 1, 0x3, {0x004000, 16368 * 1024} }, /* Upper 1023/1024 */
590 { 1, 1, 0x4, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
591 { 1, 1, 0x5, {0x008000, 16352 * 1024} }, /* Upper 511/512 */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530592};
593
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800594struct w25q_range w25x10_ranges[] = {
595 { X, X, 0, {0, 0} }, /* none */
596 { 0, 0, 0x1, {0x010000, 64 * 1024} },
597 { 0, 1, 0x1, {0x000000, 64 * 1024} },
598 { X, X, 0x2, {0x000000, 128 * 1024} },
599 { X, X, 0x3, {0x000000, 128 * 1024} },
600};
601
602struct w25q_range w25x20_ranges[] = {
603 { X, X, 0, {0, 0} }, /* none */
604 { 0, 0, 0x1, {0x030000, 64 * 1024} },
605 { 0, 0, 0x2, {0x020000, 128 * 1024} },
606 { 0, 1, 0x1, {0x000000, 64 * 1024} },
607 { 0, 1, 0x2, {0x000000, 128 * 1024} },
608 { 0, X, 0x3, {0x000000, 256 * 1024} },
609};
610
David Hendricks470ca952010-08-13 14:01:53 -0700611struct w25q_range w25x40_ranges[] = {
612 { X, X, 0, {0, 0} }, /* none */
613 { 0, 0, 0x1, {0x070000, 64 * 1024} },
614 { 0, 0, 0x2, {0x060000, 128 * 1024} },
615 { 0, 0, 0x3, {0x040000, 256 * 1024} },
616 { 0, 1, 0x1, {0x000000, 64 * 1024} },
617 { 0, 1, 0x2, {0x000000, 128 * 1024} },
618 { 0, 1, 0x3, {0x000000, 256 * 1024} },
619 { 0, X, 0x4, {0x000000, 512 * 1024} },
David Hendricksb389abb2016-06-17 16:47:00 -0700620 { 0, X, 0x5, {0x000000, 512 * 1024} },
621 { 0, X, 0x6, {0x000000, 512 * 1024} },
622 { 0, X, 0x7, {0x000000, 512 * 1024} },
David Hendricks470ca952010-08-13 14:01:53 -0700623};
624
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800625struct w25q_range w25x80_ranges[] = {
626 { X, X, 0, {0, 0} }, /* none */
627 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
628 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
629 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
630 { 0, 0, 0x4, {0x080000, 512 * 1024} },
631 { 0, 1, 0x1, {0x000000, 64 * 1024} },
632 { 0, 1, 0x2, {0x000000, 128 * 1024} },
633 { 0, 1, 0x3, {0x000000, 256 * 1024} },
634 { 0, 1, 0x4, {0x000000, 512 * 1024} },
635 { 0, X, 0x5, {0x000000, 1024 * 1024} },
636 { 0, X, 0x6, {0x000000, 1024 * 1024} },
637 { 0, X, 0x7, {0x000000, 1024 * 1024} },
638};
639
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600640static struct w25q_range gd25q40_cmp0_ranges[] = {
641 { X, X, 0, {0, 0} }, /* None */
642 { 0, 0, 0x1, {0x070000, 64 * 1024} },
643 { 0, 0, 0x2, {0x060000, 128 * 1024} },
644 { 0, 0, 0x3, {0x040000, 256 * 1024} },
645 { 0, 1, 0x1, {0x000000, 64 * 1024} },
646 { 0, 1, 0x2, {0x000000, 128 * 1024} },
647 { 0, 1, 0x3, {0x000000, 256 * 1024} },
648 { 0, X, 0x4, {0x000000, 512 * 1024} }, /* All */
649 { 0, X, 0x5, {0x000000, 512 * 1024} }, /* All */
650 { 0, X, 0x6, {0x000000, 512 * 1024} }, /* All */
651 { 0, X, 0x7, {0x000000, 512 * 1024} }, /* All */
652 { 1, 0, 0x1, {0x07F000, 4 * 1024} },
653 { 1, 0, 0x2, {0x07E000, 8 * 1024} },
654 { 1, 0, 0x3, {0x07C000, 16 * 1024} },
655 { 1, 0, 0x4, {0x078000, 32 * 1024} },
656 { 1, 0, 0x5, {0x078000, 32 * 1024} },
657 { 1, 0, 0x6, {0x078000, 32 * 1024} },
658 { 1, 1, 0x1, {0x000000, 4 * 1024} },
659 { 1, 1, 0x2, {0x000000, 8 * 1024} },
660 { 1, 1, 0x3, {0x000000, 16 * 1024} },
661 { 1, 1, 0x4, {0x000000, 32 * 1024} },
662 { 1, 1, 0x5, {0x000000, 32 * 1024} },
663 { 1, 1, 0x6, {0x000000, 32 * 1024} },
664 { 1, X, 0x7, {0x000000, 512 * 1024} }, /* All */
665};
666
667static struct w25q_range gd25q40_cmp1_ranges[] = {
668 { X, X, 0x0, {0x000000, 512 * 1024} }, /* ALL */
669 { 0, 0, 0x1, {0x000000, 448 * 1024} },
670 { 0, 0, 0x2, {0x000000, 384 * 1024} },
671 { 0, 0, 0x3, {0x000000, 256 * 1024} },
672
673 { 0, 1, 0x1, {0x010000, 448 * 1024} },
674 { 0, 1, 0x2, {0x020000, 384 * 1024} },
675 { 0, 1, 0x3, {0x040000, 256 * 1024} },
676
677 { 0, X, 0x4, {0x000000, 0} }, /* None */
678 { 0, X, 0x5, {0x000000, 0} }, /* None */
679 { 0, X, 0x6, {0x000000, 0} }, /* None */
680 { 0, X, 0x7, {0x000000, 0} }, /* None */
681
682 { 1, 0, 0x1, {0x000000, 508 * 1024} },
683 { 1, 0, 0x2, {0x000000, 504 * 1024} },
684 { 1, 0, 0x3, {0x000000, 496 * 1024} },
685 { 1, 0, 0x4, {0x000000, 480 * 1024} },
686 { 1, 0, 0x5, {0x000000, 480 * 1024} },
687 { 1, 0, 0x6, {0x000000, 480 * 1024} },
688
689 { 1, 1, 0x1, {0x001000, 508 * 1024} },
690 { 1, 1, 0x2, {0x002000, 504 * 1024} },
691 { 1, 1, 0x3, {0x004000, 496 * 1024} },
692 { 1, 1, 0x4, {0x008000, 480 * 1024} },
693 { 1, 1, 0x5, {0x008000, 480 * 1024} },
694 { 1, 1, 0x6, {0x008000, 480 * 1024} },
695
696 { 1, X, 0x7, {0x000000, 0} }, /* None */
697};
698
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700699static struct w25q_range gd25q64_ranges[] = {
700 { X, X, 0, {0, 0} }, /* none */
701 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
702 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
703 { 0, 0, 0x3, {0x780000, 512 * 1024} },
704 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
705 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
706 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
707
708 { 0, 1, 0x1, {0x000000, 128 * 1024} },
709 { 0, 1, 0x2, {0x000000, 256 * 1024} },
710 { 0, 1, 0x3, {0x000000, 512 * 1024} },
711 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
712 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
713 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
714 { X, X, 0x7, {0x000000, 8192 * 1024} },
715
716 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
717 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
718 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
719 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
720 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
721 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
722
723 { 1, 1, 0x1, {0x000000, 4 * 1024} },
724 { 1, 1, 0x2, {0x000000, 8 * 1024} },
725 { 1, 1, 0x3, {0x000000, 16 * 1024} },
726 { 1, 1, 0x4, {0x000000, 32 * 1024} },
727 { 1, 1, 0x5, {0x000000, 32 * 1024} },
728 { 1, 1, 0x6, {0x000000, 32 * 1024} },
729};
730
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800731static struct w25q_range a25l040_ranges[] = {
732 { X, X, 0x0, {0, 0} }, /* none */
733 { X, X, 0x1, {0x70000, 64 * 1024} },
734 { X, X, 0x2, {0x60000, 128 * 1024} },
735 { X, X, 0x3, {0x40000, 256 * 1024} },
736 { X, X, 0x4, {0x00000, 512 * 1024} },
737 { X, X, 0x5, {0x00000, 512 * 1024} },
738 { X, X, 0x6, {0x00000, 512 * 1024} },
739 { X, X, 0x7, {0x00000, 512 * 1024} },
740};
741
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700742static uint8_t do_read_status(const struct flashctx *flash)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530743{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100744 if (flash->chip->read_status)
745 return flash->chip->read_status(flash);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530746 else
747 return spi_read_status_register(flash);
748}
749
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700750static int do_write_status(const struct flashctx *flash, int status)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530751{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100752 if (flash->chip->write_status)
753 return flash->chip->write_status(flash, status);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530754 else
755 return spi_write_status_register(flash, status);
756}
757
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700758/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700759static uint8_t w25q_read_status_register_2(const struct flashctx *flash)
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700760{
761 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
762 unsigned char readarr[2];
763 int ret;
764
765 /* Read Status Register */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700766 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700767 if (ret) {
768 /*
769 * FIXME: make this a benign failure for now in case we are
770 * unable to execute the opcode
771 */
772 msg_cdbg("RDSR2 failed!\n");
773 readarr[0] = 0x00;
774 }
775
776 return readarr[0];
777}
778
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800779/* Given a flash chip, this function returns its range table. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700780static int w25_range_table(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800781 struct w25q_range **w25q_ranges,
782 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700783{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800784 *w25q_ranges = 0;
785 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700786
Patrick Georgif3fa2992017-02-02 16:24:44 +0100787 switch (flash->chip->manufacture_id) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700788 case WINBOND_NEX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100789 switch(flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800790 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800791 *w25q_ranges = w25x10_ranges;
792 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800793 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800794 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800795 *w25q_ranges = w25x20_ranges;
796 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800797 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800798 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800799 *w25q_ranges = w25x40_ranges;
800 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700801 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800802 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800803 *w25q_ranges = w25x80_ranges;
804 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800805 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100806 case WINBOND_NEX_W25Q80_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800807 *w25q_ranges = w25q80_ranges;
808 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700809 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100810 case WINBOND_NEX_W25Q16_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800811 *w25q_ranges = w25q16_ranges;
812 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700813 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100814 case WINBOND_NEX_W25Q32_V:
815 case WINBOND_NEX_W25Q32_W:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800816 *w25q_ranges = w25q32_ranges;
817 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700818 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100819 case WINBOND_NEX_W25Q64_V:
820 case WINBOND_NEX_W25Q64_W:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800821 *w25q_ranges = w25q64_ranges;
822 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700823 break;
Martin Rothee8dcf92017-05-10 19:16:19 -0600824 case WINBOND_NEX_W25Q128J:
Patrick Georgicc04a452017-02-06 12:14:43 +0100825 case WINBOND_NEX_W25Q128_V:
826 case WINBOND_NEX_W25Q128_W:
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700827 if (w25q_read_status_register_2(flash) & (1 << 6)) {
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700828 /* CMP == 1 */
829 *w25q_ranges = w25rq128_cmp1_ranges;
830 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
831 } else {
832 /* CMP == 0 */
833 *w25q_ranges = w25rq128_cmp0_ranges;
834 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
835 }
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530836 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700837 default:
838 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
839 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100840 flash->chip->model_id);
David Hendricksd494b0a2010-08-16 16:28:50 -0700841 return -1;
842 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700843 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700844 case EON_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100845 switch (flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800846 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800847 *w25q_ranges = en25f40_ranges;
848 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700849 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700850 case EON_EN25Q40:
851 *w25q_ranges = en25q40_ranges;
852 *num_entries = ARRAY_SIZE(en25q40_ranges);
853 break;
854 case EON_EN25Q80:
855 *w25q_ranges = en25q80_ranges;
856 *num_entries = ARRAY_SIZE(en25q80_ranges);
857 break;
858 case EON_EN25Q32:
859 *w25q_ranges = en25q32_ranges;
860 *num_entries = ARRAY_SIZE(en25q32_ranges);
861 break;
862 case EON_EN25Q64:
863 *w25q_ranges = en25q64_ranges;
864 *num_entries = ARRAY_SIZE(en25q64_ranges);
865 break;
866 case EON_EN25Q128:
867 *w25q_ranges = en25q128_ranges;
868 *num_entries = ARRAY_SIZE(en25q128_ranges);
869 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600870 case EON_EN25S64:
871 *w25q_ranges = en25s64_ranges;
872 *num_entries = ARRAY_SIZE(en25s64_ranges);
873 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700874 default:
875 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
876 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100877 flash->chip->model_id);
David Hendricks57566ed2010-08-16 18:24:45 -0700878 return -1;
879 }
880 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800881 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100882 switch (flash->chip->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800883 case MACRONIX_MX25L1005:
884 *w25q_ranges = mx25l1005_ranges;
885 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
886 break;
887 case MACRONIX_MX25L2005:
888 *w25q_ranges = mx25l2005_ranges;
889 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
890 break;
891 case MACRONIX_MX25L4005:
892 *w25q_ranges = mx25l4005_ranges;
893 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
894 break;
895 case MACRONIX_MX25L8005:
896 *w25q_ranges = mx25l8005_ranges;
897 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
898 break;
899 case MACRONIX_MX25L1605:
900 /* FIXME: MX25L1605 and MX25L1605D have different write
901 * protection capabilities, but share IDs */
902 *w25q_ranges = mx25l1605d_ranges;
903 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
904 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800905 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800906 *w25q_ranges = mx25l3205d_ranges;
907 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700908 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800909 case MACRONIX_MX25U3235E:
910 *w25q_ranges = mx25u3235e_ranges;
911 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
912 break;
Jongpil66a96492014-08-14 17:59:06 +0900913 case MACRONIX_MX25U6435E:
914 *w25q_ranges = mx25u6435e_ranges;
915 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
916 break;
David Hendricksac72e362010-08-16 18:20:03 -0700917 default:
918 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
919 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100920 flash->chip->model_id);
David Hendricksac72e362010-08-16 18:20:03 -0700921 return -1;
922 }
923 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700924 case ST_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100925 switch(flash->chip->model_id) {
David Hendricksbfa624b2012-07-24 12:47:59 -0700926 case ST_N25Q064__1E:
927 case ST_N25Q064__3E:
928 *w25q_ranges = n25q064_ranges;
929 *num_entries = ARRAY_SIZE(n25q064_ranges);
930 break;
931 default:
932 msg_cerr("%s() %d: Micron flash chip mismatch"
933 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100934 flash->chip->model_id);
David Hendricksbfa624b2012-07-24 12:47:59 -0700935 return -1;
936 }
937 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700938 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100939 switch(flash->chip->model_id) {
Bryan Freed9a0051f2012-05-22 16:06:09 -0700940 case GIGADEVICE_GD25LQ32:
941 *w25q_ranges = w25q32_ranges;
942 *num_entries = ARRAY_SIZE(w25q32_ranges);
943 break;
Martin Rothf3c3d5f2017-04-28 14:56:41 -0600944 case GIGADEVICE_GD25Q40:
945 if (w25q_read_status_register_2(flash) & (1 << 6)) {
946 /* CMP == 1 */
947 *w25q_ranges = gd25q40_cmp1_ranges;
948 *num_entries = ARRAY_SIZE(gd25q40_cmp1_ranges);
949 } else {
950 *w25q_ranges = gd25q40_cmp0_ranges;
951 *num_entries = ARRAY_SIZE(gd25q40_cmp0_ranges);
952 }
953 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700954 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -0600955 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700956 *w25q_ranges = gd25q64_ranges;
957 *num_entries = ARRAY_SIZE(gd25q64_ranges);
958 break;
Martin Roth1fd87ed2017-02-27 20:50:50 -0700959 case GIGADEVICE_GD25Q128:
960 if (w25q_read_status_register_2(flash) & (1 << 6)) {
961 /* CMP == 1 */
962 *w25q_ranges = w25rq128_cmp1_ranges;
963 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
964 } else {
965 /* CMP == 0 */
966 *w25q_ranges = w25rq128_cmp0_ranges;
967 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
968 }
969 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700970 default:
971 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
972 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100973 flash->chip->model_id);
Bryan Freed9a0051f2012-05-22 16:06:09 -0700974 return -1;
975 }
976 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800977 case AMIC_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100978 switch(flash->chip->model_id) {
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800979 case AMIC_A25L040:
980 *w25q_ranges = a25l040_ranges;
981 *num_entries = ARRAY_SIZE(a25l040_ranges);
982 break;
983 default:
984 msg_cerr("%s() %d: AMIC flash chip mismatch"
985 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100986 flash->chip->model_id);
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800987 return -1;
988 }
989 break;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -0800990 case ATMEL_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100991 switch(flash->chip->model_id) {
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -0800992 case ATMEL_AT25SL128A:
993 if (w25q_read_status_register_2(flash) & (1 << 6)) {
994 /* CMP == 1 */
995 *w25q_ranges = w25rq128_cmp1_ranges;
996 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
997 } else {
998 /* CMP == 0 */
999 *w25q_ranges = w25rq128_cmp0_ranges;
1000 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
1001 }
1002 break;
1003 default:
1004 msg_cerr("%s() %d: Atmel flash chip mismatch"
1005 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001006 flash->chip->model_id);
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -08001007 return -1;
1008 }
1009 break;
David Hendricksf7924d12010-06-10 21:26:44 -07001010 default:
David Hendricksd494b0a2010-08-16 16:28:50 -07001011 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001012 __func__, flash->chip->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -07001013 return -1;
1014 }
1015
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001016 return 0;
1017}
1018
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001019int w25_range_to_status(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001020 unsigned int start, unsigned int len,
1021 struct w25q_status *status)
1022{
1023 struct w25q_range *w25q_ranges;
1024 int i, range_found = 0;
1025 int num_entries;
1026
1027 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001028 for (i = 0; i < num_entries; i++) {
1029 struct wp_range *r = &w25q_ranges[i].range;
1030
1031 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1032 start, len, r->start, r->len);
1033 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -07001034 status->bp0 = w25q_ranges[i].bp & 1;
1035 status->bp1 = w25q_ranges[i].bp >> 1;
1036 status->bp2 = w25q_ranges[i].bp >> 2;
1037 status->tb = w25q_ranges[i].tb;
1038 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -07001039
1040 range_found = 1;
1041 break;
1042 }
1043 }
1044
1045 if (!range_found) {
1046 msg_cerr("matching range not found\n");
1047 return -1;
1048 }
David Hendricksd494b0a2010-08-16 16:28:50 -07001049 return 0;
1050}
1051
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001052int w25_status_to_range(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001053 const struct w25q_status *status,
1054 unsigned int *start, unsigned int *len)
1055{
1056 struct w25q_range *w25q_ranges;
1057 int i, status_found = 0;
1058 int num_entries;
1059
1060 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1061 for (i = 0; i < num_entries; i++) {
1062 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001063 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001064
1065 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
1066 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
1067 bp, w25q_ranges[i].bp,
1068 status->tb, w25q_ranges[i].tb,
1069 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001070 table_bp = w25q_ranges[i].bp;
1071 table_tb = w25q_ranges[i].tb;
1072 table_sec = w25q_ranges[i].sec;
1073 if ((bp == table_bp || table_bp == X) &&
1074 (status->tb == table_tb || table_tb == X) &&
1075 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001076 *start = w25q_ranges[i].range.start;
1077 *len = w25q_ranges[i].range.len;
1078
1079 status_found = 1;
1080 break;
1081 }
1082 }
1083
1084 if (!status_found) {
1085 msg_cerr("matching status not found\n");
1086 return -1;
1087 }
1088 return 0;
1089}
1090
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001091/* Given a [start, len], this function calls w25_range_to_status() to convert
1092 * it to flash-chip-specific range bits, then sets into status register.
1093 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001094static int w25_set_range(const struct flashctx *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -07001095 unsigned int start, unsigned int len)
1096{
1097 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001098 int tmp = 0;
1099 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -07001100
1101 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301102 tmp = do_read_status(flash);
David Hendricksd494b0a2010-08-16 16:28:50 -07001103 memcpy(&status, &tmp, 1);
1104 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1105
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001106 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001107
1108 msg_cdbg("status.busy: %x\n", status.busy);
1109 msg_cdbg("status.wel: %x\n", status.wel);
1110 msg_cdbg("status.bp0: %x\n", status.bp0);
1111 msg_cdbg("status.bp1: %x\n", status.bp1);
1112 msg_cdbg("status.bp2: %x\n", status.bp2);
1113 msg_cdbg("status.tb: %x\n", status.tb);
1114 msg_cdbg("status.sec: %x\n", status.sec);
1115 msg_cdbg("status.srp0: %x\n", status.srp0);
1116
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001117 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301118 do_write_status(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -07001119
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301120 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001121 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1122 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001123 return 0;
1124 } else {
David Hendricksc801adb2010-12-09 16:58:56 -08001125 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001126 expected, tmp);
1127 return 1;
1128 }
David Hendricksf7924d12010-06-10 21:26:44 -07001129}
1130
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001131/* Print out the current status register value with human-readable text. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001132static int w25_wp_status(const struct flashctx *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001133{
1134 struct w25q_status status;
1135 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -07001136 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001137 int ret = 0;
1138
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001139 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301140 tmp = do_read_status(flash);
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001141 memcpy(&status, &tmp, 1);
1142 msg_cinfo("WP: status: 0x%02x\n", tmp);
1143 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
1144 msg_cinfo("WP: write protect is %s.\n",
1145 status.srp0 ? "enabled" : "disabled");
1146
1147 msg_cinfo("WP: write protect range: ");
1148 if (w25_status_to_range(flash, &status, &start, &len)) {
1149 msg_cinfo("(cannot resolve the range)\n");
1150 ret = -1;
1151 } else {
1152 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1153 }
1154
1155 return ret;
1156}
1157
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001158/* Set/clear the SRP0 bit in the status register. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001159static int w25_set_srp0(const struct flashctx *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -07001160{
1161 struct w25q_status status;
1162 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001163 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001164
1165 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301166 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001167 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -07001168 memcpy(&status, &tmp, 1);
1169 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1170
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001171 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001172 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301173 do_write_status(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001174
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301175 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001176 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1177 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1178 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -07001179
1180 return 0;
1181}
1182
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001183static int w25_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001184 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001185{
1186 int ret;
1187
David Hendricks1c09f802012-10-03 11:03:48 -07001188 switch (wp_mode) {
1189 case WP_MODE_HARDWARE:
1190 ret = w25_set_srp0(flash, 1);
1191 break;
1192 default:
1193 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1194 return 1;
1195 }
1196
David Hendricksc801adb2010-12-09 16:58:56 -08001197 if (ret)
1198 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001199 return ret;
1200}
1201
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001202static int w25_disable_writeprotect(const struct flashctx *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001203{
1204 int ret;
1205
1206 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001207 if (ret)
1208 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001209 return ret;
1210}
1211
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001212static int w25_list_ranges(const struct flashctx *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001213{
1214 struct w25q_range *w25q_ranges;
1215 int i, num_entries;
1216
1217 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1218 for (i = 0; i < num_entries; i++) {
1219 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1220 w25q_ranges[i].range.start,
1221 w25q_ranges[i].range.len);
1222 }
1223
1224 return 0;
1225}
1226
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001227static int w25q_wp_status(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001228{
1229 struct w25q_status sr1;
1230 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001231 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001232 unsigned int start, len;
1233 int ret = 0;
1234
1235 memset(&sr1, 0, sizeof(sr1));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301236 tmp[0] = do_read_status(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001237 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001238
David Hendricksf1bd8802012-10-30 11:37:57 -07001239 memset(&sr2, 0, sizeof(sr2));
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001240 tmp[1] = w25q_read_status_register_2(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001241 memcpy(&sr2, &tmp[1], 1);
1242
1243 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001244 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1245 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1246 msg_cinfo("WP: write protect is %s.\n",
1247 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1248
1249 msg_cinfo("WP: write protect range: ");
1250 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1251 msg_cinfo("(cannot resolve the range)\n");
1252 ret = -1;
1253 } else {
1254 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1255 }
1256
1257 return ret;
1258}
1259
1260/*
1261 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1262 * de-asserted after the first byte, then it acts like a JEDEC-standard
1263 * WRSR command. if /CS is asserted, then the next data byte is written
1264 * into status register 2.
1265 */
1266#define W25Q_WRSR_OUTSIZE 0x03
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001267static int w25q_write_status_register_WREN(const struct flashctx *flash, uint8_t s1, uint8_t s2)
David Hendricks1c09f802012-10-03 11:03:48 -07001268{
1269 int result;
1270 struct spi_command cmds[] = {
1271 {
1272 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1273 .writecnt = JEDEC_WREN_OUTSIZE,
1274 .writearr = (const unsigned char[]){ JEDEC_WREN },
1275 .readcnt = 0,
1276 .readarr = NULL,
1277 }, {
1278 .writecnt = W25Q_WRSR_OUTSIZE,
1279 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1280 .readcnt = 0,
1281 .readarr = NULL,
1282 }, {
1283 .writecnt = 0,
1284 .writearr = NULL,
1285 .readcnt = 0,
1286 .readarr = NULL,
1287 }};
1288
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001289 result = spi_send_multicommand(flash, cmds);
David Hendricks1c09f802012-10-03 11:03:48 -07001290 if (result) {
1291 msg_cerr("%s failed during command execution\n",
1292 __func__);
1293 }
1294
1295 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001296 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001297
1298 return result;
1299}
1300
1301/*
1302 * Set/clear the SRP1 bit in status register 2.
1303 * FIXME: make this more generic if other chips use the same SR2 layout
1304 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001305static int w25q_set_srp1(const struct flashctx *flash, int enable)
David Hendricks1c09f802012-10-03 11:03:48 -07001306{
1307 struct w25q_status sr1;
1308 struct w25q_status_2 sr2;
1309 uint8_t tmp, expected;
1310
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301311 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001312 memcpy(&sr1, &tmp, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001313 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001314 memcpy(&sr2, &tmp, 1);
1315
1316 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1317
1318 sr2.srp1 = enable ? 1 : 0;
1319
1320 memcpy(&expected, &sr2, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001321 w25q_write_status_register_WREN(flash, *((uint8_t *)&sr1), *((uint8_t *)&sr2));
David Hendricks1c09f802012-10-03 11:03:48 -07001322
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001323 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001324 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1325 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1326 return 1;
1327
1328 return 0;
1329}
1330
1331enum wp_mode get_wp_mode(const char *mode_str)
1332{
1333 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1334
1335 if (!strcasecmp(mode_str, "hardware"))
1336 wp_mode = WP_MODE_HARDWARE;
1337 else if (!strcasecmp(mode_str, "power_cycle"))
1338 wp_mode = WP_MODE_POWER_CYCLE;
1339 else if (!strcasecmp(mode_str, "permanent"))
1340 wp_mode = WP_MODE_PERMANENT;
1341
1342 return wp_mode;
1343}
1344
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001345static int w25q_disable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001346 enum wp_mode wp_mode)
1347{
1348 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001349 struct w25q_status_2 sr2;
1350 uint8_t tmp;
1351
1352 switch (wp_mode) {
1353 case WP_MODE_HARDWARE:
1354 ret = w25_set_srp0(flash, 0);
1355 break;
1356 case WP_MODE_POWER_CYCLE:
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001357 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001358 memcpy(&sr2, &tmp, 1);
1359 if (sr2.srp1) {
1360 msg_cerr("%s(): must disconnect power to disable "
1361 "write-protection\n", __func__);
1362 } else {
1363 ret = 0;
1364 }
1365 break;
1366 case WP_MODE_PERMANENT:
1367 msg_cerr("%s(): cannot disable permanent write-protection\n",
1368 __func__);
1369 break;
1370 default:
1371 msg_cerr("%s(): invalid mode specified\n", __func__);
1372 break;
1373 }
1374
1375 if (ret)
1376 msg_cerr("%s(): error=%d.\n", __func__, ret);
1377 return ret;
1378}
1379
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001380static int w25q_disable_writeprotect_default(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001381{
1382 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1383}
1384
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001385static int w25q_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001386 enum wp_mode wp_mode)
1387{
1388 int ret = 1;
1389 struct w25q_status sr1;
1390 struct w25q_status_2 sr2;
1391 uint8_t tmp;
1392
1393 switch (wp_mode) {
1394 case WP_MODE_HARDWARE:
1395 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1396 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1397 __func__);
1398 break;
1399 }
1400
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301401 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001402 memcpy(&sr1, &tmp, 1);
1403 if (sr1.srp0)
1404 ret = 0;
1405 else
1406 ret = w25_set_srp0(flash, 1);
1407
1408 break;
1409 case WP_MODE_POWER_CYCLE:
1410 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1411 msg_cerr("%s(): cannot disable hardware WP mode\n",
1412 __func__);
1413 break;
1414 }
1415
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001416 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001417 memcpy(&sr2, &tmp, 1);
1418 if (sr2.srp1)
1419 ret = 0;
1420 else
1421 ret = w25q_set_srp1(flash, 1);
1422
1423 break;
1424 case WP_MODE_PERMANENT:
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301425 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001426 memcpy(&sr1, &tmp, 1);
1427 if (sr1.srp0 == 0) {
1428 ret = w25_set_srp0(flash, 1);
1429 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001430 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001431 "permanent WP\n", __func__);
1432 break;
1433 }
1434 }
1435
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001436 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001437 memcpy(&sr2, &tmp, 1);
1438 if (sr2.srp1 == 0) {
1439 ret = w25q_set_srp1(flash, 1);
1440 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001441 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001442 "permanent WP\n", __func__);
1443 break;
1444 }
1445 }
1446
1447 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001448 default:
1449 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1450 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001451 }
1452
1453 if (ret)
1454 msg_cerr("%s(): error=%d.\n", __func__, ret);
1455 return ret;
1456}
1457
David Hendricksc3496092014-11-13 17:20:55 -08001458/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001459uint8_t mx25l_read_config_register(const struct flashctx *flash)
David Hendricksc3496092014-11-13 17:20:55 -08001460{
1461 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
1462 unsigned char readarr[2]; /* leave room for dummy byte */
1463 int ret;
1464
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001465 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
David Hendricksc3496092014-11-13 17:20:55 -08001466 if (ret) {
1467 msg_cerr("RDCR failed!\n");
1468 readarr[0] = 0x00;
1469 }
1470
1471 return readarr[0];
1472}
David Hendricks1c09f802012-10-03 11:03:48 -07001473/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001474struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001475 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001476 .set_range = w25_set_range,
1477 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001478 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001479 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001480
1481};
1482
1483/* W25Q series has features such as a second status register and SFDP */
1484struct wp wp_w25q = {
1485 .list_ranges = w25_list_ranges,
1486 .set_range = w25_set_range,
1487 .enable = w25q_enable_writeprotect,
1488 /*
1489 * By default, disable hardware write-protection. We may change
1490 * this later if we want to add fine-grained write-protect disable
1491 * as a command-line option.
1492 */
1493 .disable = w25q_disable_writeprotect_default,
1494 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001495};
David Hendrickse0512a72014-07-15 20:30:47 -07001496
David Hendricksaf3944a2014-07-28 18:37:40 -07001497struct generic_range gd25q32_cmp0_ranges[] = {
1498 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001499 { { }, 0x00, {0, 0} },
1500 { { }, 0x08, {0, 0} },
1501 { { }, 0x10, {0, 0} },
1502 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001503
David Hendricks148a4bf2015-03-13 21:02:42 -07001504 { { }, 0x01, {0x3f0000, 64 * 1024} },
1505 { { }, 0x02, {0x3e0000, 128 * 1024} },
1506 { { }, 0x03, {0x3c0000, 256 * 1024} },
1507 { { }, 0x04, {0x380000, 512 * 1024} },
1508 { { }, 0x05, {0x300000, 1024 * 1024} },
1509 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001510
David Hendricks148a4bf2015-03-13 21:02:42 -07001511 { { }, 0x09, {0x000000, 64 * 1024} },
1512 { { }, 0x0a, {0x000000, 128 * 1024} },
1513 { { }, 0x0b, {0x000000, 256 * 1024} },
1514 { { }, 0x0c, {0x000000, 512 * 1024} },
1515 { { }, 0x0d, {0x000000, 1024 * 1024} },
1516 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001517
1518 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001519 { { }, 0x07, {0x000000, 4096 * 1024} },
1520 { { }, 0x0f, {0x000000, 4096 * 1024} },
1521 { { }, 0x17, {0x000000, 4096 * 1024} },
1522 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001523
David Hendricks148a4bf2015-03-13 21:02:42 -07001524 { { }, 0x11, {0x3ff000, 4 * 1024} },
1525 { { }, 0x12, {0x3fe000, 8 * 1024} },
1526 { { }, 0x13, {0x3fc000, 16 * 1024} },
1527 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1528 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1529 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001530
David Hendricks148a4bf2015-03-13 21:02:42 -07001531 { { }, 0x19, {0x000000, 4 * 1024} },
1532 { { }, 0x1a, {0x000000, 8 * 1024} },
1533 { { }, 0x1b, {0x000000, 16 * 1024} },
1534 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1535 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1536 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001537};
1538
1539struct generic_range gd25q32_cmp1_ranges[] = {
Martin Roth563a1fe2017-04-18 14:26:27 -06001540 /* All, bp4 and bp3 => don't care */
1541 { { }, 0x00, {0x000000, 4096 * 1024} }, /* All */
1542 { { }, 0x08, {0x000000, 4096 * 1024} },
1543 { { }, 0x10, {0x000000, 4096 * 1024} },
1544 { { }, 0x18, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001545
David Hendricks148a4bf2015-03-13 21:02:42 -07001546 { { }, 0x01, {0x000000, 4032 * 1024} },
1547 { { }, 0x02, {0x000000, 3968 * 1024} },
1548 { { }, 0x03, {0x000000, 3840 * 1024} },
1549 { { }, 0x04, {0x000000, 3584 * 1024} },
1550 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
1551 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001552
David Hendricks148a4bf2015-03-13 21:02:42 -07001553 { { }, 0x09, {0x010000, 4032 * 1024} },
1554 { { }, 0x0a, {0x020000, 3968 * 1024} },
1555 { { }, 0x0b, {0x040000, 3840 * 1024} },
1556 { { }, 0x0c, {0x080000, 3584 * 1024} },
1557 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
1558 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001559
Martin Roth563a1fe2017-04-18 14:26:27 -06001560 /* None, bp4 and bp3 => don't care */
1561 { { }, 0x07, {0, 0} }, /* None */
1562 { { }, 0x0f, {0, 0} },
1563 { { }, 0x17, {0, 0} },
1564 { { }, 0x1f, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001565
David Hendricks148a4bf2015-03-13 21:02:42 -07001566 { { }, 0x11, {0x000000, 4092 * 1024} },
1567 { { }, 0x12, {0x000000, 4088 * 1024} },
1568 { { }, 0x13, {0x000000, 4080 * 1024} },
1569 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1570 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1571 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001572
David Hendricks148a4bf2015-03-13 21:02:42 -07001573 { { }, 0x19, {0x001000, 4092 * 1024} },
1574 { { }, 0x1a, {0x002000, 4088 * 1024} },
1575 { { }, 0x1b, {0x040000, 4080 * 1024} },
1576 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1577 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1578 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001579};
1580
1581static struct generic_wp gd25q32_wp = {
1582 /* TODO: map second status register */
1583 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1584};
1585
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001586struct generic_range gd25q128_cmp0_ranges[] = {
1587 /* none, bp4 and bp3 => don't care, others = 0 */
1588 { { .tb = 0 }, 0x00, {0, 0} },
1589 { { .tb = 0 }, 0x08, {0, 0} },
1590 { { .tb = 0 }, 0x10, {0, 0} },
1591 { { .tb = 0 }, 0x18, {0, 0} },
1592
1593 { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} },
1594 { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} },
1595 { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} },
1596 { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} },
1597 { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} },
1598 { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} },
1599
1600 { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} },
1601 { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} },
1602 { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} },
1603 { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} },
1604 { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} },
1605 { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} },
1606
1607 /* all, bp4 and bp3 => don't care, others = 1 */
1608 { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} },
1609 { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} },
1610 { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} },
1611 { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} },
1612
1613 { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} },
1614 { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} },
1615 { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} },
1616 { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1617 { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1618
1619 { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} },
1620 { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} },
1621 { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} },
1622 { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1623 { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1624 { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} },
1625};
1626
1627struct generic_range gd25q128_cmp1_ranges[] = {
1628 /* none, bp4 and bp3 => don't care, others = 0 */
1629 { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} },
1630 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1631 { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} },
1632 { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} },
1633
1634 { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} },
1635 { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} },
1636 { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} },
1637 { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} },
1638 { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} },
1639 { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} },
1640
1641 { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} },
1642 { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} },
1643 { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} },
1644 { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} },
1645 { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} },
1646 { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} },
1647
1648 /* none, bp4 and bp3 => don't care, others = 1 */
1649 { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} },
1650 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1651 { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} },
1652 { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} },
1653 { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} },
1654
1655 { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} },
1656 { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} },
1657 { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} },
1658 { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1659 { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1660
1661 { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} },
1662 { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} },
1663 { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} },
1664 { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1665 { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1666 { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} },
1667};
1668
1669static struct generic_wp gd25q128_wp = {
1670 /* TODO: map second and third status registers */
1671 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1672};
1673
David Hendricks83541d32014-07-15 20:58:21 -07001674#if 0
1675/* FIXME: MX25L6405D has same ID as MX25L6406 */
1676static struct w25q_range mx25l6405d_ranges[] = {
1677 { X, 0, 0, {0, 0} }, /* none */
1678 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1679 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1680 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1681 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1682 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1683 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1684 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1685
1686 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1687 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1688 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1689 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1690 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1691 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1692 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1693 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1694};
1695#endif
1696
1697/* FIXME: MX25L6406 has same ID as MX25L6405D */
1698struct generic_range mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001699 { { }, 0, {0, 0} }, /* none */
1700 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1701 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1702 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1703 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1704 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1705 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07001706
David Hendricks148a4bf2015-03-13 21:02:42 -07001707 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1708 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1709 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1710 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1711 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1712 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1713 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1714 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1715 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07001716};
1717
1718static struct generic_wp mx25l6406e_wp = {
1719 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1720 .ranges = &mx25l6406e_ranges[0],
1721};
David Hendrickse0512a72014-07-15 20:30:47 -07001722
David Hendricksc3496092014-11-13 17:20:55 -08001723struct generic_range mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001724 { { }, 0, {0, 0} }, /* none */
1725 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
1726 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1727 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08001728
David Hendricks148a4bf2015-03-13 21:02:42 -07001729 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
1730 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1731 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1732 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1733 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1734 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1735 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1736 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1737 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1738 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1739 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1740 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001741};
1742
1743struct generic_range mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001744 { { }, 0, {0, 0} }, /* none */
1745 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
1746 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
1747 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
1748 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
1749 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
1750 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
1751 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1752 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1753 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1754 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1755 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1756 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1757 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1758 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1759 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001760};
1761
1762static struct generic_wp mx25l6495f_wp = {
1763 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1764};
1765
David Hendricks148a4bf2015-03-13 21:02:42 -07001766struct generic_range s25fs128s_ranges[] = {
1767 { { .tb = 1 }, 0, {0, 0} }, /* none */
1768 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
1769 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
1770 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
1771 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
1772 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
1773 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
1774 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001775
David Hendricks148a4bf2015-03-13 21:02:42 -07001776 { { .tb = 0 }, 0, {0, 0} }, /* none */
1777 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
1778 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
1779 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
1780 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
1781 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
1782 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
1783 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001784};
1785
1786static struct generic_wp s25fs128s_wp = {
1787 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001788 .get_modifier_bits = s25f_get_modifier_bits,
1789 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08001790};
1791
David Hendricksc694bb82015-02-25 14:52:17 -08001792
David Hendricks148a4bf2015-03-13 21:02:42 -07001793struct generic_range s25fl256s_ranges[] = {
1794 { { .tb = 1 }, 0, {0, 0} }, /* none */
1795 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
1796 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
1797 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
1798 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
1799 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
1800 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
1801 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
1802
1803 { { .tb = 0 }, 0, {0, 0} }, /* none */
1804 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
1805 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
1806 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
1807 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
1808 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
1809 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
1810 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08001811};
1812
1813static struct generic_wp s25fl256s_wp = {
1814 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001815 .get_modifier_bits = s25f_get_modifier_bits,
1816 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08001817};
1818
David Hendrickse0512a72014-07-15 20:30:47 -07001819/* Given a flash chip, this function returns its writeprotect info. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001820static int generic_range_table(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07001821 struct generic_wp **wp,
1822 int *num_entries)
1823{
1824 *wp = NULL;
1825 *num_entries = 0;
1826
Patrick Georgif3fa2992017-02-02 16:24:44 +01001827 switch (flash->chip->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07001828 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001829 switch(flash->chip->model_id) {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001830
Martin Roth563a1fe2017-04-18 14:26:27 -06001831 case GIGADEVICE_GD25LQ32:
David Hendricksaf3944a2014-07-28 18:37:40 -07001832 case GIGADEVICE_GD25Q32: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001833 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricksaf3944a2014-07-28 18:37:40 -07001834 *wp = &gd25q32_wp;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001835
David Hendricksaf3944a2014-07-28 18:37:40 -07001836 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1837 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
1838 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
1839 } else { /* CMP == 1 */
1840 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
1841 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
1842 }
1843
1844 break;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001845 }
Furquan Shaikh62cd8102016-07-17 23:04:06 -07001846 case GIGADEVICE_GD25Q128:
1847 case GIGADEVICE_GD25LQ128C: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001848 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001849 *wp = &gd25q128_wp;
1850
1851 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1852 (*wp)->ranges = &gd25q128_cmp0_ranges[0];
1853 *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges);
1854 } else { /* CMP == 1 */
1855 (*wp)->ranges = &gd25q128_cmp1_ranges[0];
1856 *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges);
1857 }
1858
1859 break;
David Hendricksaf3944a2014-07-28 18:37:40 -07001860 }
1861 default:
1862 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1863 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001864 flash->chip->model_id);
David Hendricksaf3944a2014-07-28 18:37:40 -07001865 return -1;
1866 }
1867 break;
David Hendricks83541d32014-07-15 20:58:21 -07001868 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001869 switch (flash->chip->model_id) {
David Hendricks83541d32014-07-15 20:58:21 -07001870 case MACRONIX_MX25L6405:
1871 /* FIXME: MX25L64* chips have mixed capabilities and
1872 share IDs */
1873 *wp = &mx25l6406e_wp;
1874 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
1875 break;
David Hendricksc3496092014-11-13 17:20:55 -08001876 case MACRONIX_MX25L6495F: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001877 uint8_t cr = mx25l_read_config_register(flash);
David Hendricksc3496092014-11-13 17:20:55 -08001878
1879 *wp = &mx25l6495f_wp;
1880 if (!(cr & (1 << 3))) { /* T/B == 0 */
1881 (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
1882 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
1883 } else { /* T/B == 1 */
1884 (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
1885 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
1886 }
1887 break;
1888 }
David Hendricks83541d32014-07-15 20:58:21 -07001889 default:
1890 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1891 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001892 flash->chip->model_id);
David Hendricks83541d32014-07-15 20:58:21 -07001893 return -1;
1894 }
1895 break;
David Hendricksa9884852014-12-11 15:31:12 -08001896 case SPANSION_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001897 switch (flash->chip->model_id) {
David Hendricksa9884852014-12-11 15:31:12 -08001898 case SPANSION_S25FS128S_L:
1899 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08001900 *wp = &s25fs128s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001901 (*wp)->ranges = s25fs128s_ranges;
1902 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08001903 break;
1904 }
David Hendricksc694bb82015-02-25 14:52:17 -08001905 case SPANSION_S25FL256S_UL:
1906 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08001907 *wp = &s25fl256s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001908 (*wp)->ranges = s25fl256s_ranges;
1909 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08001910 break;
1911 }
David Hendricksa9884852014-12-11 15:31:12 -08001912 default:
1913 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
Patrick Georgif3fa2992017-02-02 16:24:44 +01001914 ", aborting\n", __func__, __LINE__,
1915 flash->chip->model_id);
David Hendricksa9884852014-12-11 15:31:12 -08001916 return -1;
1917 }
1918 break;
David Hendrickse0512a72014-07-15 20:30:47 -07001919 default:
1920 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001921 __func__, flash->chip->manufacture_id);
David Hendrickse0512a72014-07-15 20:30:47 -07001922 return -1;
1923 }
1924
1925 return 0;
1926}
1927
1928/* Given a [start, len], this function finds a block protect bit combination
1929 * (if possible) and sets the corresponding bits in "status". Remaining bits
1930 * are preserved. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001931static int generic_range_to_status(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07001932 unsigned int start, unsigned int len,
1933 uint8_t *status)
1934{
1935 struct generic_wp *wp;
1936 struct generic_range *r;
1937 int i, range_found = 0, num_entries;
1938 uint8_t bp_mask;
1939
1940 if (generic_range_table(flash, &wp, &num_entries))
1941 return -1;
1942
1943 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
1944 ((1 << wp->sr1.bp0_pos) - 1);
1945
1946 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1947 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1948 start, len, r->range.start, r->range.len);
1949 if ((start == r->range.start) && (len == r->range.len)) {
1950 *status &= ~(bp_mask);
1951 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07001952
1953 if (wp->set_modifier_bits) {
1954 if (wp->set_modifier_bits(flash, &r->m) < 0) {
1955 msg_cerr("error setting modifier "
1956 "bits for range.\n");
1957 return -1;
1958 }
1959 }
1960
David Hendrickse0512a72014-07-15 20:30:47 -07001961 range_found = 1;
1962 break;
1963 }
1964 }
1965
1966 if (!range_found) {
1967 msg_cerr("matching range not found\n");
1968 return -1;
1969 }
1970 return 0;
1971}
1972
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001973static int generic_status_to_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07001974 const uint8_t sr1, unsigned int *start, unsigned int *len)
1975{
1976 struct generic_wp *wp;
1977 struct generic_range *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07001978 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07001979 uint8_t sr1_bp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001980 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07001981
1982 if (generic_range_table(flash, &wp, &num_entries))
1983 return -1;
1984
David Hendricks148a4bf2015-03-13 21:02:42 -07001985 /* modifier bits may be compared more than once, so get them here */
1986 if (wp->get_modifier_bits) {
1987 if (wp->get_modifier_bits(flash, &m) < 0)
1988 return -1;
1989 }
1990
David Hendrickse0512a72014-07-15 20:30:47 -07001991 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
1992
1993 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07001994 if (wp->get_modifier_bits) {
1995 if (memcmp(&m, &r->m, sizeof(m)))
1996 continue;
1997 }
David Hendrickse0512a72014-07-15 20:30:47 -07001998 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
1999 if (sr1_bp == r->bp) {
2000 *start = r->range.start;
2001 *len = r->range.len;
2002 status_found = 1;
2003 break;
2004 }
2005 }
2006
2007 if (!status_found) {
2008 msg_cerr("matching status not found\n");
2009 return -1;
2010 }
2011 return 0;
2012}
2013
2014/* Given a [start, len], this function calls generic_range_to_status() to
2015 * convert it to flash-chip-specific range bits, then sets into status register.
2016 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002017static int generic_set_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002018 unsigned int start, unsigned int len)
2019{
2020 uint8_t status, expected;
2021
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302022 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002023 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
2024
2025 expected = status; /* preserve non-bp bits */
2026 if (generic_range_to_status(flash, start, len, &expected))
2027 return -1;
2028
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302029 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002030
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302031 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002032 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
2033 if (status != expected) {
2034 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
2035 expected, status);
2036 return 1;
2037 }
2038
2039 return 0;
2040}
2041
2042/* Set/clear the status regsiter write protect bit in SR1. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002043static int generic_set_srp0(const struct flashctx *flash, int enable)
David Hendrickse0512a72014-07-15 20:30:47 -07002044{
2045 uint8_t status, expected;
2046 struct generic_wp *wp;
2047 int num_entries;
2048
2049 if (generic_range_table(flash, &wp, &num_entries))
2050 return -1;
2051
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302052 expected = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002053 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
2054
2055 if (enable)
2056 expected |= 1 << wp->sr1.srp_pos;
2057 else
2058 expected &= ~(1 << wp->sr1.srp_pos);
2059
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302060 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07002061
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302062 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002063 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
2064 if (status != expected)
2065 return -1;
2066
2067 return 0;
2068}
2069
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002070static int generic_enable_writeprotect(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002071 enum wp_mode wp_mode)
2072{
2073 int ret;
2074
2075 switch (wp_mode) {
2076 case WP_MODE_HARDWARE:
2077 ret = generic_set_srp0(flash, 1);
2078 break;
2079 default:
2080 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
2081 return 1;
2082 }
2083
2084 if (ret)
2085 msg_cerr("%s(): error=%d.\n", __func__, ret);
2086 return ret;
2087}
2088
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002089static int generic_disable_writeprotect(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002090{
2091 int ret;
2092
2093 ret = generic_set_srp0(flash, 0);
2094 if (ret)
2095 msg_cerr("%s(): error=%d.\n", __func__, ret);
2096 return ret;
2097}
2098
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002099static int generic_list_ranges(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002100{
2101 struct generic_wp *wp;
2102 struct generic_range *r;
2103 int i, num_entries;
2104
2105 if (generic_range_table(flash, &wp, &num_entries))
2106 return -1;
2107
2108 r = &wp->ranges[0];
2109 for (i = 0; i < num_entries; i++) {
2110 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
2111 r->range.start, r->range.len);
2112 r++;
2113 }
2114
2115 return 0;
2116}
2117
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002118static int generic_wp_status(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002119{
2120 uint8_t sr1;
2121 unsigned int start, len;
2122 int ret = 0;
2123 struct generic_wp *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002124 int num_entries, wp_en;
2125
2126 if (generic_range_table(flash, &wp, &num_entries))
2127 return -1;
2128
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302129 sr1 = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002130 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
2131
2132 msg_cinfo("WP: status: 0x%04x\n", sr1);
2133 msg_cinfo("WP: status.srp0: %x\n", wp_en);
2134 /* FIXME: SRP1 is not really generic, but we probably should print
2135 * it anyway to have consistent output. #legacycruft */
2136 msg_cinfo("WP: status.srp1: %x\n", 0);
2137 msg_cinfo("WP: write protect is %s.\n",
2138 wp_en ? "enabled" : "disabled");
2139
2140 msg_cinfo("WP: write protect range: ");
2141 if (generic_status_to_range(flash, sr1, &start, &len)) {
2142 msg_cinfo("(cannot resolve the range)\n");
2143 ret = -1;
2144 } else {
2145 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
2146 }
2147
2148 return ret;
2149}
2150
2151struct wp wp_generic = {
2152 .list_ranges = generic_list_ranges,
2153 .set_range = generic_set_range,
2154 .enable = generic_enable_writeprotect,
2155 .disable = generic_disable_writeprotect,
2156 .wp_status = generic_wp_status,
2157};