blob: dfcc6cff96def33b69473e67e3d0be7f65123116 [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
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700640static struct w25q_range gd25q64_ranges[] = {
641 { X, X, 0, {0, 0} }, /* none */
642 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
643 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
644 { 0, 0, 0x3, {0x780000, 512 * 1024} },
645 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
646 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
647 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
648
649 { 0, 1, 0x1, {0x000000, 128 * 1024} },
650 { 0, 1, 0x2, {0x000000, 256 * 1024} },
651 { 0, 1, 0x3, {0x000000, 512 * 1024} },
652 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
653 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
654 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
655 { X, X, 0x7, {0x000000, 8192 * 1024} },
656
657 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
658 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
659 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
660 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
661 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
662 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
663
664 { 1, 1, 0x1, {0x000000, 4 * 1024} },
665 { 1, 1, 0x2, {0x000000, 8 * 1024} },
666 { 1, 1, 0x3, {0x000000, 16 * 1024} },
667 { 1, 1, 0x4, {0x000000, 32 * 1024} },
668 { 1, 1, 0x5, {0x000000, 32 * 1024} },
669 { 1, 1, 0x6, {0x000000, 32 * 1024} },
670};
671
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800672static struct w25q_range a25l040_ranges[] = {
673 { X, X, 0x0, {0, 0} }, /* none */
674 { X, X, 0x1, {0x70000, 64 * 1024} },
675 { X, X, 0x2, {0x60000, 128 * 1024} },
676 { X, X, 0x3, {0x40000, 256 * 1024} },
677 { X, X, 0x4, {0x00000, 512 * 1024} },
678 { X, X, 0x5, {0x00000, 512 * 1024} },
679 { X, X, 0x6, {0x00000, 512 * 1024} },
680 { X, X, 0x7, {0x00000, 512 * 1024} },
681};
682
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700683static uint8_t do_read_status(const struct flashctx *flash)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530684{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100685 if (flash->chip->read_status)
686 return flash->chip->read_status(flash);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530687 else
688 return spi_read_status_register(flash);
689}
690
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700691static int do_write_status(const struct flashctx *flash, int status)
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530692{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100693 if (flash->chip->write_status)
694 return flash->chip->write_status(flash, status);
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530695 else
696 return spi_write_status_register(flash, status);
697}
698
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700699/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700700static uint8_t w25q_read_status_register_2(const struct flashctx *flash)
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700701{
702 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
703 unsigned char readarr[2];
704 int ret;
705
706 /* Read Status Register */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700707 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700708 if (ret) {
709 /*
710 * FIXME: make this a benign failure for now in case we are
711 * unable to execute the opcode
712 */
713 msg_cdbg("RDSR2 failed!\n");
714 readarr[0] = 0x00;
715 }
716
717 return readarr[0];
718}
719
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800720/* Given a flash chip, this function returns its range table. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700721static int w25_range_table(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800722 struct w25q_range **w25q_ranges,
723 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700724{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800725 *w25q_ranges = 0;
726 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700727
Patrick Georgif3fa2992017-02-02 16:24:44 +0100728 switch (flash->chip->manufacture_id) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700729 case WINBOND_NEX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100730 switch(flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800731 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800732 *w25q_ranges = w25x10_ranges;
733 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800734 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800735 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800736 *w25q_ranges = w25x20_ranges;
737 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800738 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800739 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800740 *w25q_ranges = w25x40_ranges;
741 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700742 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800743 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800744 *w25q_ranges = w25x80_ranges;
745 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800746 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100747 case WINBOND_NEX_W25Q80_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800748 *w25q_ranges = w25q80_ranges;
749 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700750 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100751 case WINBOND_NEX_W25Q16_V:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800752 *w25q_ranges = w25q16_ranges;
753 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700754 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100755 case WINBOND_NEX_W25Q32_V:
756 case WINBOND_NEX_W25Q32_W:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800757 *w25q_ranges = w25q32_ranges;
758 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700759 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100760 case WINBOND_NEX_W25Q64_V:
761 case WINBOND_NEX_W25Q64_W:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800762 *w25q_ranges = w25q64_ranges;
763 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700764 break;
Patrick Georgicc04a452017-02-06 12:14:43 +0100765 case WINBOND_NEX_W25Q128_V:
766 case WINBOND_NEX_W25Q128_W:
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700767 if (w25q_read_status_register_2(flash) & (1 << 6)) {
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700768 /* CMP == 1 */
769 *w25q_ranges = w25rq128_cmp1_ranges;
770 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
771 } else {
772 /* CMP == 0 */
773 *w25q_ranges = w25rq128_cmp0_ranges;
774 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
775 }
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530776 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700777 default:
778 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
779 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100780 flash->chip->model_id);
David Hendricksd494b0a2010-08-16 16:28:50 -0700781 return -1;
782 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700783 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700784 case EON_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100785 switch (flash->chip->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800786 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800787 *w25q_ranges = en25f40_ranges;
788 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700789 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700790 case EON_EN25Q40:
791 *w25q_ranges = en25q40_ranges;
792 *num_entries = ARRAY_SIZE(en25q40_ranges);
793 break;
794 case EON_EN25Q80:
795 *w25q_ranges = en25q80_ranges;
796 *num_entries = ARRAY_SIZE(en25q80_ranges);
797 break;
798 case EON_EN25Q32:
799 *w25q_ranges = en25q32_ranges;
800 *num_entries = ARRAY_SIZE(en25q32_ranges);
801 break;
802 case EON_EN25Q64:
803 *w25q_ranges = en25q64_ranges;
804 *num_entries = ARRAY_SIZE(en25q64_ranges);
805 break;
806 case EON_EN25Q128:
807 *w25q_ranges = en25q128_ranges;
808 *num_entries = ARRAY_SIZE(en25q128_ranges);
809 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600810 case EON_EN25S64:
811 *w25q_ranges = en25s64_ranges;
812 *num_entries = ARRAY_SIZE(en25s64_ranges);
813 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700814 default:
815 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
816 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100817 flash->chip->model_id);
David Hendricks57566ed2010-08-16 18:24:45 -0700818 return -1;
819 }
820 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800821 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100822 switch (flash->chip->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800823 case MACRONIX_MX25L1005:
824 *w25q_ranges = mx25l1005_ranges;
825 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
826 break;
827 case MACRONIX_MX25L2005:
828 *w25q_ranges = mx25l2005_ranges;
829 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
830 break;
831 case MACRONIX_MX25L4005:
832 *w25q_ranges = mx25l4005_ranges;
833 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
834 break;
835 case MACRONIX_MX25L8005:
836 *w25q_ranges = mx25l8005_ranges;
837 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
838 break;
839 case MACRONIX_MX25L1605:
840 /* FIXME: MX25L1605 and MX25L1605D have different write
841 * protection capabilities, but share IDs */
842 *w25q_ranges = mx25l1605d_ranges;
843 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
844 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800845 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800846 *w25q_ranges = mx25l3205d_ranges;
847 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700848 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800849 case MACRONIX_MX25U3235E:
850 *w25q_ranges = mx25u3235e_ranges;
851 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
852 break;
Jongpil66a96492014-08-14 17:59:06 +0900853 case MACRONIX_MX25U6435E:
854 *w25q_ranges = mx25u6435e_ranges;
855 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
856 break;
David Hendricksac72e362010-08-16 18:20:03 -0700857 default:
858 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
859 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100860 flash->chip->model_id);
David Hendricksac72e362010-08-16 18:20:03 -0700861 return -1;
862 }
863 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700864 case ST_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100865 switch(flash->chip->model_id) {
David Hendricksbfa624b2012-07-24 12:47:59 -0700866 case ST_N25Q064__1E:
867 case ST_N25Q064__3E:
868 *w25q_ranges = n25q064_ranges;
869 *num_entries = ARRAY_SIZE(n25q064_ranges);
870 break;
871 default:
872 msg_cerr("%s() %d: Micron flash chip mismatch"
873 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100874 flash->chip->model_id);
David Hendricksbfa624b2012-07-24 12:47:59 -0700875 return -1;
876 }
877 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700878 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100879 switch(flash->chip->model_id) {
Bryan Freed9a0051f2012-05-22 16:06:09 -0700880 case GIGADEVICE_GD25LQ32:
881 *w25q_ranges = w25q32_ranges;
882 *num_entries = ARRAY_SIZE(w25q32_ranges);
883 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700884 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -0600885 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700886 *w25q_ranges = gd25q64_ranges;
887 *num_entries = ARRAY_SIZE(gd25q64_ranges);
888 break;
Martin Roth1fd87ed2017-02-27 20:50:50 -0700889 case GIGADEVICE_GD25Q128:
890 if (w25q_read_status_register_2(flash) & (1 << 6)) {
891 /* CMP == 1 */
892 *w25q_ranges = w25rq128_cmp1_ranges;
893 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
894 } else {
895 /* CMP == 0 */
896 *w25q_ranges = w25rq128_cmp0_ranges;
897 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
898 }
899 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700900 default:
901 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
902 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100903 flash->chip->model_id);
Bryan Freed9a0051f2012-05-22 16:06:09 -0700904 return -1;
905 }
906 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800907 case AMIC_ID_NOPREFIX:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100908 switch(flash->chip->model_id) {
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800909 case AMIC_A25L040:
910 *w25q_ranges = a25l040_ranges;
911 *num_entries = ARRAY_SIZE(a25l040_ranges);
912 break;
913 default:
914 msg_cerr("%s() %d: AMIC flash chip mismatch"
915 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100916 flash->chip->model_id);
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800917 return -1;
918 }
919 break;
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -0800920 case ATMEL_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +0100921 switch(flash->chip->model_id) {
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -0800922 case ATMEL_AT25SL128A:
923 if (w25q_read_status_register_2(flash) & (1 << 6)) {
924 /* CMP == 1 */
925 *w25q_ranges = w25rq128_cmp1_ranges;
926 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
927 } else {
928 /* CMP == 0 */
929 *w25q_ranges = w25rq128_cmp0_ranges;
930 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
931 }
932 break;
933 default:
934 msg_cerr("%s() %d: Atmel flash chip mismatch"
935 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100936 flash->chip->model_id);
Furquan Shaikhb4df8ef2017-01-05 15:05:35 -0800937 return -1;
938 }
939 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700940 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700941 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +0100942 __func__, flash->chip->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700943 return -1;
944 }
945
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800946 return 0;
947}
948
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700949int w25_range_to_status(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800950 unsigned int start, unsigned int len,
951 struct w25q_status *status)
952{
953 struct w25q_range *w25q_ranges;
954 int i, range_found = 0;
955 int num_entries;
956
957 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700958 for (i = 0; i < num_entries; i++) {
959 struct wp_range *r = &w25q_ranges[i].range;
960
961 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
962 start, len, r->start, r->len);
963 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700964 status->bp0 = w25q_ranges[i].bp & 1;
965 status->bp1 = w25q_ranges[i].bp >> 1;
966 status->bp2 = w25q_ranges[i].bp >> 2;
967 status->tb = w25q_ranges[i].tb;
968 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700969
970 range_found = 1;
971 break;
972 }
973 }
974
975 if (!range_found) {
976 msg_cerr("matching range not found\n");
977 return -1;
978 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700979 return 0;
980}
981
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700982int w25_status_to_range(const struct flashctx *flash,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800983 const struct w25q_status *status,
984 unsigned int *start, unsigned int *len)
985{
986 struct w25q_range *w25q_ranges;
987 int i, status_found = 0;
988 int num_entries;
989
990 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
991 for (i = 0; i < num_entries; i++) {
992 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800993 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800994
995 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
996 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
997 bp, w25q_ranges[i].bp,
998 status->tb, w25q_ranges[i].tb,
999 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +08001000 table_bp = w25q_ranges[i].bp;
1001 table_tb = w25q_ranges[i].tb;
1002 table_sec = w25q_ranges[i].sec;
1003 if ((bp == table_bp || table_bp == X) &&
1004 (status->tb == table_tb || table_tb == X) &&
1005 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001006 *start = w25q_ranges[i].range.start;
1007 *len = w25q_ranges[i].range.len;
1008
1009 status_found = 1;
1010 break;
1011 }
1012 }
1013
1014 if (!status_found) {
1015 msg_cerr("matching status not found\n");
1016 return -1;
1017 }
1018 return 0;
1019}
1020
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001021/* Given a [start, len], this function calls w25_range_to_status() to convert
1022 * it to flash-chip-specific range bits, then sets into status register.
1023 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001024static int w25_set_range(const struct flashctx *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -07001025 unsigned int start, unsigned int len)
1026{
1027 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001028 int tmp = 0;
1029 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -07001030
1031 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301032 tmp = do_read_status(flash);
David Hendricksd494b0a2010-08-16 16:28:50 -07001033 memcpy(&status, &tmp, 1);
1034 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1035
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001036 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001037
1038 msg_cdbg("status.busy: %x\n", status.busy);
1039 msg_cdbg("status.wel: %x\n", status.wel);
1040 msg_cdbg("status.bp0: %x\n", status.bp0);
1041 msg_cdbg("status.bp1: %x\n", status.bp1);
1042 msg_cdbg("status.bp2: %x\n", status.bp2);
1043 msg_cdbg("status.tb: %x\n", status.tb);
1044 msg_cdbg("status.sec: %x\n", status.sec);
1045 msg_cdbg("status.srp0: %x\n", status.srp0);
1046
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001047 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301048 do_write_status(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -07001049
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301050 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001051 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1052 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001053 return 0;
1054 } else {
David Hendricksc801adb2010-12-09 16:58:56 -08001055 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001056 expected, tmp);
1057 return 1;
1058 }
David Hendricksf7924d12010-06-10 21:26:44 -07001059}
1060
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001061/* Print out the current status register value with human-readable text. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001062static int w25_wp_status(const struct flashctx *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001063{
1064 struct w25q_status status;
1065 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -07001066 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001067 int ret = 0;
1068
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001069 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301070 tmp = do_read_status(flash);
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001071 memcpy(&status, &tmp, 1);
1072 msg_cinfo("WP: status: 0x%02x\n", tmp);
1073 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
1074 msg_cinfo("WP: write protect is %s.\n",
1075 status.srp0 ? "enabled" : "disabled");
1076
1077 msg_cinfo("WP: write protect range: ");
1078 if (w25_status_to_range(flash, &status, &start, &len)) {
1079 msg_cinfo("(cannot resolve the range)\n");
1080 ret = -1;
1081 } else {
1082 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1083 }
1084
1085 return ret;
1086}
1087
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001088/* Set/clear the SRP0 bit in the status register. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001089static int w25_set_srp0(const struct flashctx *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -07001090{
1091 struct w25q_status status;
1092 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001093 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001094
1095 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301096 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001097 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -07001098 memcpy(&status, &tmp, 1);
1099 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1100
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001101 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001102 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301103 do_write_status(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001104
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301105 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001106 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1107 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1108 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -07001109
1110 return 0;
1111}
1112
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001113static int w25_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001114 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001115{
1116 int ret;
1117
David Hendricks1c09f802012-10-03 11:03:48 -07001118 switch (wp_mode) {
1119 case WP_MODE_HARDWARE:
1120 ret = w25_set_srp0(flash, 1);
1121 break;
1122 default:
1123 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1124 return 1;
1125 }
1126
David Hendricksc801adb2010-12-09 16:58:56 -08001127 if (ret)
1128 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001129 return ret;
1130}
1131
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001132static int w25_disable_writeprotect(const struct flashctx *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001133{
1134 int ret;
1135
1136 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001137 if (ret)
1138 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001139 return ret;
1140}
1141
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001142static int w25_list_ranges(const struct flashctx *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001143{
1144 struct w25q_range *w25q_ranges;
1145 int i, num_entries;
1146
1147 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1148 for (i = 0; i < num_entries; i++) {
1149 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1150 w25q_ranges[i].range.start,
1151 w25q_ranges[i].range.len);
1152 }
1153
1154 return 0;
1155}
1156
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001157static int w25q_wp_status(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001158{
1159 struct w25q_status sr1;
1160 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001161 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001162 unsigned int start, len;
1163 int ret = 0;
1164
1165 memset(&sr1, 0, sizeof(sr1));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301166 tmp[0] = do_read_status(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001167 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001168
David Hendricksf1bd8802012-10-30 11:37:57 -07001169 memset(&sr2, 0, sizeof(sr2));
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001170 tmp[1] = w25q_read_status_register_2(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001171 memcpy(&sr2, &tmp[1], 1);
1172
1173 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001174 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1175 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1176 msg_cinfo("WP: write protect is %s.\n",
1177 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1178
1179 msg_cinfo("WP: write protect range: ");
1180 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1181 msg_cinfo("(cannot resolve the range)\n");
1182 ret = -1;
1183 } else {
1184 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1185 }
1186
1187 return ret;
1188}
1189
1190/*
1191 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1192 * de-asserted after the first byte, then it acts like a JEDEC-standard
1193 * WRSR command. if /CS is asserted, then the next data byte is written
1194 * into status register 2.
1195 */
1196#define W25Q_WRSR_OUTSIZE 0x03
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001197static int w25q_write_status_register_WREN(const struct flashctx *flash, uint8_t s1, uint8_t s2)
David Hendricks1c09f802012-10-03 11:03:48 -07001198{
1199 int result;
1200 struct spi_command cmds[] = {
1201 {
1202 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1203 .writecnt = JEDEC_WREN_OUTSIZE,
1204 .writearr = (const unsigned char[]){ JEDEC_WREN },
1205 .readcnt = 0,
1206 .readarr = NULL,
1207 }, {
1208 .writecnt = W25Q_WRSR_OUTSIZE,
1209 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1210 .readcnt = 0,
1211 .readarr = NULL,
1212 }, {
1213 .writecnt = 0,
1214 .writearr = NULL,
1215 .readcnt = 0,
1216 .readarr = NULL,
1217 }};
1218
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001219 result = spi_send_multicommand(flash, cmds);
David Hendricks1c09f802012-10-03 11:03:48 -07001220 if (result) {
1221 msg_cerr("%s failed during command execution\n",
1222 __func__);
1223 }
1224
1225 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001226 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001227
1228 return result;
1229}
1230
1231/*
1232 * Set/clear the SRP1 bit in status register 2.
1233 * FIXME: make this more generic if other chips use the same SR2 layout
1234 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001235static int w25q_set_srp1(const struct flashctx *flash, int enable)
David Hendricks1c09f802012-10-03 11:03:48 -07001236{
1237 struct w25q_status sr1;
1238 struct w25q_status_2 sr2;
1239 uint8_t tmp, expected;
1240
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301241 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001242 memcpy(&sr1, &tmp, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001243 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001244 memcpy(&sr2, &tmp, 1);
1245
1246 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1247
1248 sr2.srp1 = enable ? 1 : 0;
1249
1250 memcpy(&expected, &sr2, 1);
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001251 w25q_write_status_register_WREN(flash, *((uint8_t *)&sr1), *((uint8_t *)&sr2));
David Hendricks1c09f802012-10-03 11:03:48 -07001252
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001253 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001254 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1255 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1256 return 1;
1257
1258 return 0;
1259}
1260
1261enum wp_mode get_wp_mode(const char *mode_str)
1262{
1263 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1264
1265 if (!strcasecmp(mode_str, "hardware"))
1266 wp_mode = WP_MODE_HARDWARE;
1267 else if (!strcasecmp(mode_str, "power_cycle"))
1268 wp_mode = WP_MODE_POWER_CYCLE;
1269 else if (!strcasecmp(mode_str, "permanent"))
1270 wp_mode = WP_MODE_PERMANENT;
1271
1272 return wp_mode;
1273}
1274
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001275static int w25q_disable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001276 enum wp_mode wp_mode)
1277{
1278 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001279 struct w25q_status_2 sr2;
1280 uint8_t tmp;
1281
1282 switch (wp_mode) {
1283 case WP_MODE_HARDWARE:
1284 ret = w25_set_srp0(flash, 0);
1285 break;
1286 case WP_MODE_POWER_CYCLE:
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001287 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001288 memcpy(&sr2, &tmp, 1);
1289 if (sr2.srp1) {
1290 msg_cerr("%s(): must disconnect power to disable "
1291 "write-protection\n", __func__);
1292 } else {
1293 ret = 0;
1294 }
1295 break;
1296 case WP_MODE_PERMANENT:
1297 msg_cerr("%s(): cannot disable permanent write-protection\n",
1298 __func__);
1299 break;
1300 default:
1301 msg_cerr("%s(): invalid mode specified\n", __func__);
1302 break;
1303 }
1304
1305 if (ret)
1306 msg_cerr("%s(): error=%d.\n", __func__, ret);
1307 return ret;
1308}
1309
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001310static int w25q_disable_writeprotect_default(const struct flashctx *flash)
David Hendricks1c09f802012-10-03 11:03:48 -07001311{
1312 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1313}
1314
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001315static int w25q_enable_writeprotect(const struct flashctx *flash,
David Hendricks1c09f802012-10-03 11:03:48 -07001316 enum wp_mode wp_mode)
1317{
1318 int ret = 1;
1319 struct w25q_status sr1;
1320 struct w25q_status_2 sr2;
1321 uint8_t tmp;
1322
1323 switch (wp_mode) {
1324 case WP_MODE_HARDWARE:
1325 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1326 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1327 __func__);
1328 break;
1329 }
1330
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301331 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001332 memcpy(&sr1, &tmp, 1);
1333 if (sr1.srp0)
1334 ret = 0;
1335 else
1336 ret = w25_set_srp0(flash, 1);
1337
1338 break;
1339 case WP_MODE_POWER_CYCLE:
1340 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1341 msg_cerr("%s(): cannot disable hardware WP mode\n",
1342 __func__);
1343 break;
1344 }
1345
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001346 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001347 memcpy(&sr2, &tmp, 1);
1348 if (sr2.srp1)
1349 ret = 0;
1350 else
1351 ret = w25q_set_srp1(flash, 1);
1352
1353 break;
1354 case WP_MODE_PERMANENT:
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301355 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001356 memcpy(&sr1, &tmp, 1);
1357 if (sr1.srp0 == 0) {
1358 ret = w25_set_srp0(flash, 1);
1359 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001360 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001361 "permanent WP\n", __func__);
1362 break;
1363 }
1364 }
1365
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001366 tmp = w25q_read_status_register_2(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001367 memcpy(&sr2, &tmp, 1);
1368 if (sr2.srp1 == 0) {
1369 ret = w25q_set_srp1(flash, 1);
1370 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001371 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001372 "permanent WP\n", __func__);
1373 break;
1374 }
1375 }
1376
1377 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001378 default:
1379 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1380 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001381 }
1382
1383 if (ret)
1384 msg_cerr("%s(): error=%d.\n", __func__, ret);
1385 return ret;
1386}
1387
David Hendricksc3496092014-11-13 17:20:55 -08001388/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001389uint8_t mx25l_read_config_register(const struct flashctx *flash)
David Hendricksc3496092014-11-13 17:20:55 -08001390{
1391 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
1392 unsigned char readarr[2]; /* leave room for dummy byte */
1393 int ret;
1394
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001395 ret = spi_send_command(flash, sizeof(cmd), sizeof(readarr), cmd, readarr);
David Hendricksc3496092014-11-13 17:20:55 -08001396 if (ret) {
1397 msg_cerr("RDCR failed!\n");
1398 readarr[0] = 0x00;
1399 }
1400
1401 return readarr[0];
1402}
David Hendricks1c09f802012-10-03 11:03:48 -07001403/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001404struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001405 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001406 .set_range = w25_set_range,
1407 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001408 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001409 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001410
1411};
1412
1413/* W25Q series has features such as a second status register and SFDP */
1414struct wp wp_w25q = {
1415 .list_ranges = w25_list_ranges,
1416 .set_range = w25_set_range,
1417 .enable = w25q_enable_writeprotect,
1418 /*
1419 * By default, disable hardware write-protection. We may change
1420 * this later if we want to add fine-grained write-protect disable
1421 * as a command-line option.
1422 */
1423 .disable = w25q_disable_writeprotect_default,
1424 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001425};
David Hendrickse0512a72014-07-15 20:30:47 -07001426
David Hendricksaf3944a2014-07-28 18:37:40 -07001427struct generic_range gd25q32_cmp0_ranges[] = {
1428 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001429 { { }, 0x00, {0, 0} },
1430 { { }, 0x08, {0, 0} },
1431 { { }, 0x10, {0, 0} },
1432 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001433
David Hendricks148a4bf2015-03-13 21:02:42 -07001434 { { }, 0x01, {0x3f0000, 64 * 1024} },
1435 { { }, 0x02, {0x3e0000, 128 * 1024} },
1436 { { }, 0x03, {0x3c0000, 256 * 1024} },
1437 { { }, 0x04, {0x380000, 512 * 1024} },
1438 { { }, 0x05, {0x300000, 1024 * 1024} },
1439 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001440
David Hendricks148a4bf2015-03-13 21:02:42 -07001441 { { }, 0x09, {0x000000, 64 * 1024} },
1442 { { }, 0x0a, {0x000000, 128 * 1024} },
1443 { { }, 0x0b, {0x000000, 256 * 1024} },
1444 { { }, 0x0c, {0x000000, 512 * 1024} },
1445 { { }, 0x0d, {0x000000, 1024 * 1024} },
1446 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001447
1448 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001449 { { }, 0x07, {0x000000, 4096 * 1024} },
1450 { { }, 0x0f, {0x000000, 4096 * 1024} },
1451 { { }, 0x17, {0x000000, 4096 * 1024} },
1452 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001453
David Hendricks148a4bf2015-03-13 21:02:42 -07001454 { { }, 0x11, {0x3ff000, 4 * 1024} },
1455 { { }, 0x12, {0x3fe000, 8 * 1024} },
1456 { { }, 0x13, {0x3fc000, 16 * 1024} },
1457 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1458 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1459 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001460
David Hendricks148a4bf2015-03-13 21:02:42 -07001461 { { }, 0x19, {0x000000, 4 * 1024} },
1462 { { }, 0x1a, {0x000000, 8 * 1024} },
1463 { { }, 0x1b, {0x000000, 16 * 1024} },
1464 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1465 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1466 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001467};
1468
1469struct generic_range gd25q32_cmp1_ranges[] = {
Martin Roth563a1fe2017-04-18 14:26:27 -06001470 /* All, bp4 and bp3 => don't care */
1471 { { }, 0x00, {0x000000, 4096 * 1024} }, /* All */
1472 { { }, 0x08, {0x000000, 4096 * 1024} },
1473 { { }, 0x10, {0x000000, 4096 * 1024} },
1474 { { }, 0x18, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001475
David Hendricks148a4bf2015-03-13 21:02:42 -07001476 { { }, 0x01, {0x000000, 4032 * 1024} },
1477 { { }, 0x02, {0x000000, 3968 * 1024} },
1478 { { }, 0x03, {0x000000, 3840 * 1024} },
1479 { { }, 0x04, {0x000000, 3584 * 1024} },
1480 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
1481 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001482
David Hendricks148a4bf2015-03-13 21:02:42 -07001483 { { }, 0x09, {0x010000, 4032 * 1024} },
1484 { { }, 0x0a, {0x020000, 3968 * 1024} },
1485 { { }, 0x0b, {0x040000, 3840 * 1024} },
1486 { { }, 0x0c, {0x080000, 3584 * 1024} },
1487 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
1488 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001489
Martin Roth563a1fe2017-04-18 14:26:27 -06001490 /* None, bp4 and bp3 => don't care */
1491 { { }, 0x07, {0, 0} }, /* None */
1492 { { }, 0x0f, {0, 0} },
1493 { { }, 0x17, {0, 0} },
1494 { { }, 0x1f, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001495
David Hendricks148a4bf2015-03-13 21:02:42 -07001496 { { }, 0x11, {0x000000, 4092 * 1024} },
1497 { { }, 0x12, {0x000000, 4088 * 1024} },
1498 { { }, 0x13, {0x000000, 4080 * 1024} },
1499 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1500 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1501 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001502
David Hendricks148a4bf2015-03-13 21:02:42 -07001503 { { }, 0x19, {0x001000, 4092 * 1024} },
1504 { { }, 0x1a, {0x002000, 4088 * 1024} },
1505 { { }, 0x1b, {0x040000, 4080 * 1024} },
1506 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1507 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1508 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001509};
1510
1511static struct generic_wp gd25q32_wp = {
1512 /* TODO: map second status register */
1513 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1514};
1515
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001516struct generic_range gd25q128_cmp0_ranges[] = {
1517 /* none, bp4 and bp3 => don't care, others = 0 */
1518 { { .tb = 0 }, 0x00, {0, 0} },
1519 { { .tb = 0 }, 0x08, {0, 0} },
1520 { { .tb = 0 }, 0x10, {0, 0} },
1521 { { .tb = 0 }, 0x18, {0, 0} },
1522
1523 { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} },
1524 { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} },
1525 { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} },
1526 { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} },
1527 { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} },
1528 { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} },
1529
1530 { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} },
1531 { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} },
1532 { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} },
1533 { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} },
1534 { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} },
1535 { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} },
1536
1537 /* all, bp4 and bp3 => don't care, others = 1 */
1538 { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} },
1539 { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} },
1540 { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} },
1541 { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} },
1542
1543 { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} },
1544 { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} },
1545 { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} },
1546 { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1547 { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1548
1549 { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} },
1550 { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} },
1551 { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} },
1552 { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1553 { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1554 { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} },
1555};
1556
1557struct generic_range gd25q128_cmp1_ranges[] = {
1558 /* none, bp4 and bp3 => don't care, others = 0 */
1559 { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} },
1560 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1561 { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} },
1562 { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} },
1563
1564 { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} },
1565 { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} },
1566 { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} },
1567 { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} },
1568 { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} },
1569 { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} },
1570
1571 { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} },
1572 { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} },
1573 { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} },
1574 { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} },
1575 { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} },
1576 { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} },
1577
1578 /* none, bp4 and bp3 => don't care, others = 1 */
1579 { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} },
1580 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1581 { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} },
1582 { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} },
1583 { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} },
1584
1585 { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} },
1586 { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} },
1587 { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} },
1588 { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1589 { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1590
1591 { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} },
1592 { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} },
1593 { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} },
1594 { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1595 { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1596 { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} },
1597};
1598
1599static struct generic_wp gd25q128_wp = {
1600 /* TODO: map second and third status registers */
1601 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1602};
1603
David Hendricks83541d32014-07-15 20:58:21 -07001604#if 0
1605/* FIXME: MX25L6405D has same ID as MX25L6406 */
1606static struct w25q_range mx25l6405d_ranges[] = {
1607 { X, 0, 0, {0, 0} }, /* none */
1608 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1609 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1610 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1611 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1612 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1613 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1614 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1615
1616 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1617 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1618 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1619 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1620 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1621 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1622 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1623 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1624};
1625#endif
1626
1627/* FIXME: MX25L6406 has same ID as MX25L6405D */
1628struct generic_range mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001629 { { }, 0, {0, 0} }, /* none */
1630 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1631 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1632 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1633 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1634 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1635 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07001636
David Hendricks148a4bf2015-03-13 21:02:42 -07001637 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1638 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1639 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1640 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1641 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1642 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1643 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1644 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1645 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07001646};
1647
1648static struct generic_wp mx25l6406e_wp = {
1649 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1650 .ranges = &mx25l6406e_ranges[0],
1651};
David Hendrickse0512a72014-07-15 20:30:47 -07001652
David Hendricksc3496092014-11-13 17:20:55 -08001653struct generic_range mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001654 { { }, 0, {0, 0} }, /* none */
1655 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
1656 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1657 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08001658
David Hendricks148a4bf2015-03-13 21:02:42 -07001659 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
1660 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1661 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1662 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1663 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1664 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1665 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1666 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1667 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1668 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1669 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1670 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001671};
1672
1673struct generic_range mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001674 { { }, 0, {0, 0} }, /* none */
1675 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
1676 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
1677 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
1678 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
1679 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
1680 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
1681 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1682 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1683 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1684 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1685 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1686 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1687 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1688 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1689 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001690};
1691
1692static struct generic_wp mx25l6495f_wp = {
1693 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1694};
1695
David Hendricks148a4bf2015-03-13 21:02:42 -07001696struct generic_range s25fs128s_ranges[] = {
1697 { { .tb = 1 }, 0, {0, 0} }, /* none */
1698 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
1699 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
1700 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
1701 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
1702 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
1703 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
1704 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001705
David Hendricks148a4bf2015-03-13 21:02:42 -07001706 { { .tb = 0 }, 0, {0, 0} }, /* none */
1707 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
1708 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
1709 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
1710 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
1711 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
1712 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
1713 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001714};
1715
1716static struct generic_wp s25fs128s_wp = {
1717 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001718 .get_modifier_bits = s25f_get_modifier_bits,
1719 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08001720};
1721
David Hendricksc694bb82015-02-25 14:52:17 -08001722
David Hendricks148a4bf2015-03-13 21:02:42 -07001723struct generic_range s25fl256s_ranges[] = {
1724 { { .tb = 1 }, 0, {0, 0} }, /* none */
1725 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
1726 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
1727 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
1728 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
1729 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
1730 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
1731 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
1732
1733 { { .tb = 0 }, 0, {0, 0} }, /* none */
1734 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
1735 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
1736 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
1737 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
1738 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
1739 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
1740 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08001741};
1742
1743static struct generic_wp s25fl256s_wp = {
1744 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001745 .get_modifier_bits = s25f_get_modifier_bits,
1746 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08001747};
1748
David Hendrickse0512a72014-07-15 20:30:47 -07001749/* Given a flash chip, this function returns its writeprotect info. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001750static int generic_range_table(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07001751 struct generic_wp **wp,
1752 int *num_entries)
1753{
1754 *wp = NULL;
1755 *num_entries = 0;
1756
Patrick Georgif3fa2992017-02-02 16:24:44 +01001757 switch (flash->chip->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07001758 case GIGADEVICE_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001759 switch(flash->chip->model_id) {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001760
Martin Roth563a1fe2017-04-18 14:26:27 -06001761 case GIGADEVICE_GD25LQ32:
David Hendricksaf3944a2014-07-28 18:37:40 -07001762 case GIGADEVICE_GD25Q32: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001763 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricksaf3944a2014-07-28 18:37:40 -07001764 *wp = &gd25q32_wp;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001765
David Hendricksaf3944a2014-07-28 18:37:40 -07001766 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1767 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
1768 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
1769 } else { /* CMP == 1 */
1770 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
1771 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
1772 }
1773
1774 break;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001775 }
Furquan Shaikh62cd8102016-07-17 23:04:06 -07001776 case GIGADEVICE_GD25Q128:
1777 case GIGADEVICE_GD25LQ128C: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001778 uint8_t sr1 = w25q_read_status_register_2(flash);
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001779 *wp = &gd25q128_wp;
1780
1781 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1782 (*wp)->ranges = &gd25q128_cmp0_ranges[0];
1783 *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges);
1784 } else { /* CMP == 1 */
1785 (*wp)->ranges = &gd25q128_cmp1_ranges[0];
1786 *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges);
1787 }
1788
1789 break;
David Hendricksaf3944a2014-07-28 18:37:40 -07001790 }
1791 default:
1792 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1793 " (0x%04x), aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001794 flash->chip->model_id);
David Hendricksaf3944a2014-07-28 18:37:40 -07001795 return -1;
1796 }
1797 break;
David Hendricks83541d32014-07-15 20:58:21 -07001798 case MACRONIX_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001799 switch (flash->chip->model_id) {
David Hendricks83541d32014-07-15 20:58:21 -07001800 case MACRONIX_MX25L6405:
1801 /* FIXME: MX25L64* chips have mixed capabilities and
1802 share IDs */
1803 *wp = &mx25l6406e_wp;
1804 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
1805 break;
David Hendricksc3496092014-11-13 17:20:55 -08001806 case MACRONIX_MX25L6495F: {
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001807 uint8_t cr = mx25l_read_config_register(flash);
David Hendricksc3496092014-11-13 17:20:55 -08001808
1809 *wp = &mx25l6495f_wp;
1810 if (!(cr & (1 << 3))) { /* T/B == 0 */
1811 (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
1812 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
1813 } else { /* T/B == 1 */
1814 (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
1815 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
1816 }
1817 break;
1818 }
David Hendricks83541d32014-07-15 20:58:21 -07001819 default:
1820 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1821 ", aborting\n", __func__, __LINE__,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001822 flash->chip->model_id);
David Hendricks83541d32014-07-15 20:58:21 -07001823 return -1;
1824 }
1825 break;
David Hendricksa9884852014-12-11 15:31:12 -08001826 case SPANSION_ID:
Patrick Georgif3fa2992017-02-02 16:24:44 +01001827 switch (flash->chip->model_id) {
David Hendricksa9884852014-12-11 15:31:12 -08001828 case SPANSION_S25FS128S_L:
1829 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08001830 *wp = &s25fs128s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001831 (*wp)->ranges = s25fs128s_ranges;
1832 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08001833 break;
1834 }
David Hendricksc694bb82015-02-25 14:52:17 -08001835 case SPANSION_S25FL256S_UL:
1836 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08001837 *wp = &s25fl256s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001838 (*wp)->ranges = s25fl256s_ranges;
1839 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08001840 break;
1841 }
David Hendricksa9884852014-12-11 15:31:12 -08001842 default:
1843 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
Patrick Georgif3fa2992017-02-02 16:24:44 +01001844 ", aborting\n", __func__, __LINE__,
1845 flash->chip->model_id);
David Hendricksa9884852014-12-11 15:31:12 -08001846 return -1;
1847 }
1848 break;
David Hendrickse0512a72014-07-15 20:30:47 -07001849 default:
1850 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001851 __func__, flash->chip->manufacture_id);
David Hendrickse0512a72014-07-15 20:30:47 -07001852 return -1;
1853 }
1854
1855 return 0;
1856}
1857
1858/* Given a [start, len], this function finds a block protect bit combination
1859 * (if possible) and sets the corresponding bits in "status". Remaining bits
1860 * are preserved. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001861static int generic_range_to_status(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07001862 unsigned int start, unsigned int len,
1863 uint8_t *status)
1864{
1865 struct generic_wp *wp;
1866 struct generic_range *r;
1867 int i, range_found = 0, num_entries;
1868 uint8_t bp_mask;
1869
1870 if (generic_range_table(flash, &wp, &num_entries))
1871 return -1;
1872
1873 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
1874 ((1 << wp->sr1.bp0_pos) - 1);
1875
1876 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1877 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1878 start, len, r->range.start, r->range.len);
1879 if ((start == r->range.start) && (len == r->range.len)) {
1880 *status &= ~(bp_mask);
1881 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07001882
1883 if (wp->set_modifier_bits) {
1884 if (wp->set_modifier_bits(flash, &r->m) < 0) {
1885 msg_cerr("error setting modifier "
1886 "bits for range.\n");
1887 return -1;
1888 }
1889 }
1890
David Hendrickse0512a72014-07-15 20:30:47 -07001891 range_found = 1;
1892 break;
1893 }
1894 }
1895
1896 if (!range_found) {
1897 msg_cerr("matching range not found\n");
1898 return -1;
1899 }
1900 return 0;
1901}
1902
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001903static int generic_status_to_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07001904 const uint8_t sr1, unsigned int *start, unsigned int *len)
1905{
1906 struct generic_wp *wp;
1907 struct generic_range *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07001908 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07001909 uint8_t sr1_bp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001910 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07001911
1912 if (generic_range_table(flash, &wp, &num_entries))
1913 return -1;
1914
David Hendricks148a4bf2015-03-13 21:02:42 -07001915 /* modifier bits may be compared more than once, so get them here */
1916 if (wp->get_modifier_bits) {
1917 if (wp->get_modifier_bits(flash, &m) < 0)
1918 return -1;
1919 }
1920
David Hendrickse0512a72014-07-15 20:30:47 -07001921 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
1922
1923 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07001924 if (wp->get_modifier_bits) {
1925 if (memcmp(&m, &r->m, sizeof(m)))
1926 continue;
1927 }
David Hendrickse0512a72014-07-15 20:30:47 -07001928 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
1929 if (sr1_bp == r->bp) {
1930 *start = r->range.start;
1931 *len = r->range.len;
1932 status_found = 1;
1933 break;
1934 }
1935 }
1936
1937 if (!status_found) {
1938 msg_cerr("matching status not found\n");
1939 return -1;
1940 }
1941 return 0;
1942}
1943
1944/* Given a [start, len], this function calls generic_range_to_status() to
1945 * convert it to flash-chip-specific range bits, then sets into status register.
1946 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001947static int generic_set_range(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07001948 unsigned int start, unsigned int len)
1949{
1950 uint8_t status, expected;
1951
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301952 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07001953 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
1954
1955 expected = status; /* preserve non-bp bits */
1956 if (generic_range_to_status(flash, start, len, &expected))
1957 return -1;
1958
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301959 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001960
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301961 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07001962 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1963 if (status != expected) {
1964 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1965 expected, status);
1966 return 1;
1967 }
1968
1969 return 0;
1970}
1971
1972/* Set/clear the status regsiter write protect bit in SR1. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001973static int generic_set_srp0(const struct flashctx *flash, int enable)
David Hendrickse0512a72014-07-15 20:30:47 -07001974{
1975 uint8_t status, expected;
1976 struct generic_wp *wp;
1977 int num_entries;
1978
1979 if (generic_range_table(flash, &wp, &num_entries))
1980 return -1;
1981
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301982 expected = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07001983 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
1984
1985 if (enable)
1986 expected |= 1 << wp->sr1.srp_pos;
1987 else
1988 expected &= ~(1 << wp->sr1.srp_pos);
1989
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301990 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001991
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301992 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07001993 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1994 if (status != expected)
1995 return -1;
1996
1997 return 0;
1998}
1999
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002000static int generic_enable_writeprotect(const struct flashctx *flash,
David Hendrickse0512a72014-07-15 20:30:47 -07002001 enum wp_mode wp_mode)
2002{
2003 int ret;
2004
2005 switch (wp_mode) {
2006 case WP_MODE_HARDWARE:
2007 ret = generic_set_srp0(flash, 1);
2008 break;
2009 default:
2010 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
2011 return 1;
2012 }
2013
2014 if (ret)
2015 msg_cerr("%s(): error=%d.\n", __func__, ret);
2016 return ret;
2017}
2018
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002019static int generic_disable_writeprotect(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002020{
2021 int ret;
2022
2023 ret = generic_set_srp0(flash, 0);
2024 if (ret)
2025 msg_cerr("%s(): error=%d.\n", __func__, ret);
2026 return ret;
2027}
2028
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002029static int generic_list_ranges(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002030{
2031 struct generic_wp *wp;
2032 struct generic_range *r;
2033 int i, num_entries;
2034
2035 if (generic_range_table(flash, &wp, &num_entries))
2036 return -1;
2037
2038 r = &wp->ranges[0];
2039 for (i = 0; i < num_entries; i++) {
2040 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
2041 r->range.start, r->range.len);
2042 r++;
2043 }
2044
2045 return 0;
2046}
2047
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002048static int generic_wp_status(const struct flashctx *flash)
David Hendrickse0512a72014-07-15 20:30:47 -07002049{
2050 uint8_t sr1;
2051 unsigned int start, len;
2052 int ret = 0;
2053 struct generic_wp *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002054 int num_entries, wp_en;
2055
2056 if (generic_range_table(flash, &wp, &num_entries))
2057 return -1;
2058
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302059 sr1 = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002060 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
2061
2062 msg_cinfo("WP: status: 0x%04x\n", sr1);
2063 msg_cinfo("WP: status.srp0: %x\n", wp_en);
2064 /* FIXME: SRP1 is not really generic, but we probably should print
2065 * it anyway to have consistent output. #legacycruft */
2066 msg_cinfo("WP: status.srp1: %x\n", 0);
2067 msg_cinfo("WP: write protect is %s.\n",
2068 wp_en ? "enabled" : "disabled");
2069
2070 msg_cinfo("WP: write protect range: ");
2071 if (generic_status_to_range(flash, sr1, &start, &len)) {
2072 msg_cinfo("(cannot resolve the range)\n");
2073 ret = -1;
2074 } else {
2075 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
2076 }
2077
2078 return ret;
2079}
2080
2081struct wp wp_generic = {
2082 .list_ranges = generic_list_ranges,
2083 .set_range = generic_set_range,
2084 .enable = generic_enable_writeprotect,
2085 .disable = generic_disable_writeprotect,
2086 .wp_status = generic_wp_status,
2087};