blob: f995a11e39595281277cd725125ae28488a5dba3 [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 */
77 int (*get_modifier_bits)(const struct flashchip *flash,
78 struct generic_modifier_bits *m);
79 int (*set_modifier_bits)(const struct flashchip *flash,
80 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} },
620};
621
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800622struct w25q_range w25x80_ranges[] = {
623 { X, X, 0, {0, 0} }, /* none */
624 { 0, 0, 0x1, {0x0F0000, 64 * 1024} },
625 { 0, 0, 0x2, {0x0E0000, 128 * 1024} },
626 { 0, 0, 0x3, {0x0C0000, 256 * 1024} },
627 { 0, 0, 0x4, {0x080000, 512 * 1024} },
628 { 0, 1, 0x1, {0x000000, 64 * 1024} },
629 { 0, 1, 0x2, {0x000000, 128 * 1024} },
630 { 0, 1, 0x3, {0x000000, 256 * 1024} },
631 { 0, 1, 0x4, {0x000000, 512 * 1024} },
632 { 0, X, 0x5, {0x000000, 1024 * 1024} },
633 { 0, X, 0x6, {0x000000, 1024 * 1024} },
634 { 0, X, 0x7, {0x000000, 1024 * 1024} },
635};
636
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700637static struct w25q_range gd25q64_ranges[] = {
638 { X, X, 0, {0, 0} }, /* none */
639 { 0, 0, 0x1, {0x7e0000, 128 * 1024} },
640 { 0, 0, 0x2, {0x7c0000, 256 * 1024} },
641 { 0, 0, 0x3, {0x780000, 512 * 1024} },
642 { 0, 0, 0x4, {0x700000, 1024 * 1024} },
643 { 0, 0, 0x5, {0x600000, 2048 * 1024} },
644 { 0, 0, 0x6, {0x400000, 4096 * 1024} },
645
646 { 0, 1, 0x1, {0x000000, 128 * 1024} },
647 { 0, 1, 0x2, {0x000000, 256 * 1024} },
648 { 0, 1, 0x3, {0x000000, 512 * 1024} },
649 { 0, 1, 0x4, {0x000000, 1024 * 1024} },
650 { 0, 1, 0x5, {0x000000, 2048 * 1024} },
651 { 0, 1, 0x6, {0x000000, 4096 * 1024} },
652 { X, X, 0x7, {0x000000, 8192 * 1024} },
653
654 { 1, 0, 0x1, {0x7ff000, 4 * 1024} },
655 { 1, 0, 0x2, {0x7fe000, 8 * 1024} },
656 { 1, 0, 0x3, {0x7fc000, 16 * 1024} },
657 { 1, 0, 0x4, {0x7f8000, 32 * 1024} },
658 { 1, 0, 0x5, {0x7f8000, 32 * 1024} },
659 { 1, 0, 0x6, {0x7f8000, 32 * 1024} },
660
661 { 1, 1, 0x1, {0x000000, 4 * 1024} },
662 { 1, 1, 0x2, {0x000000, 8 * 1024} },
663 { 1, 1, 0x3, {0x000000, 16 * 1024} },
664 { 1, 1, 0x4, {0x000000, 32 * 1024} },
665 { 1, 1, 0x5, {0x000000, 32 * 1024} },
666 { 1, 1, 0x6, {0x000000, 32 * 1024} },
667};
668
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800669static struct w25q_range a25l040_ranges[] = {
670 { X, X, 0x0, {0, 0} }, /* none */
671 { X, X, 0x1, {0x70000, 64 * 1024} },
672 { X, X, 0x2, {0x60000, 128 * 1024} },
673 { X, X, 0x3, {0x40000, 256 * 1024} },
674 { X, X, 0x4, {0x00000, 512 * 1024} },
675 { X, X, 0x5, {0x00000, 512 * 1024} },
676 { X, X, 0x6, {0x00000, 512 * 1024} },
677 { X, X, 0x7, {0x00000, 512 * 1024} },
678};
679
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530680static uint8_t do_read_status(const struct flashchip *flash)
681{
682 if (flash->read_status)
683 return flash->read_status(flash);
684 else
685 return spi_read_status_register(flash);
686}
687
688static int do_write_status(const struct flashchip *flash, int status)
689{
690 if (flash->write_status)
691 return flash->write_status(flash, status);
692 else
693 return spi_write_status_register(flash, status);
694}
695
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700696/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
697static uint8_t w25q_read_status_register_2(void)
698{
699 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x35 };
700 unsigned char readarr[2];
701 int ret;
702
703 /* Read Status Register */
704 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
705 if (ret) {
706 /*
707 * FIXME: make this a benign failure for now in case we are
708 * unable to execute the opcode
709 */
710 msg_cdbg("RDSR2 failed!\n");
711 readarr[0] = 0x00;
712 }
713
714 return readarr[0];
715}
716
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800717/* Given a flash chip, this function returns its range table. */
718static int w25_range_table(const struct flashchip *flash,
719 struct w25q_range **w25q_ranges,
720 int *num_entries)
David Hendricksf7924d12010-06-10 21:26:44 -0700721{
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800722 *w25q_ranges = 0;
723 *num_entries = 0;
David Hendricksf7924d12010-06-10 21:26:44 -0700724
David Hendricksd494b0a2010-08-16 16:28:50 -0700725 switch (flash->manufacture_id) {
726 case WINBOND_NEX_ID:
727 switch(flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800728 case WINBOND_NEX_W25X10:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800729 *w25q_ranges = w25x10_ranges;
730 *num_entries = ARRAY_SIZE(w25x10_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800731 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800732 case WINBOND_NEX_W25X20:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800733 *w25q_ranges = w25x20_ranges;
734 *num_entries = ARRAY_SIZE(w25x20_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800735 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800736 case WINBOND_NEX_W25X40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800737 *w25q_ranges = w25x40_ranges;
738 *num_entries = ARRAY_SIZE(w25x40_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700739 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800740 case WINBOND_NEX_W25X80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800741 *w25q_ranges = w25x80_ranges;
742 *num_entries = ARRAY_SIZE(w25x80_ranges);
Louis Yung-Chieh Lo232951f2010-09-16 11:30:00 +0800743 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800744 case WINBOND_NEX_W25Q80:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800745 *w25q_ranges = w25q80_ranges;
746 *num_entries = ARRAY_SIZE(w25q80_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700747 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800748 case WINBOND_NEX_W25Q16:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800749 *w25q_ranges = w25q16_ranges;
750 *num_entries = ARRAY_SIZE(w25q16_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700751 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800752 case WINBOND_NEX_W25Q32:
Louis Yung-Chieh Lo469707f2012-05-18 16:38:37 +0800753 case WINBOND_NEX_W25Q32DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800754 *w25q_ranges = w25q32_ranges;
755 *num_entries = ARRAY_SIZE(w25q32_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700756 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800757 case WINBOND_NEX_W25Q64:
AdamTsai141a2622013-12-31 14:07:15 +0800758 case WINBOND_NEX_W25Q64DW:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800759 *w25q_ranges = w25q64_ranges;
760 *num_entries = ARRAY_SIZE(w25q64_ranges);
David Hendricksd494b0a2010-08-16 16:28:50 -0700761 break;
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700762 case WINBOND_NEX_W25Q128:
Furquan Shaikh712fe922015-09-01 01:10:45 -0700763 case WINBOND_NEX_W25Q128FW:
Duncan Laurieed32d7b2015-05-27 11:28:18 -0700764 if (w25q_read_status_register_2() & (1 << 6)) {
765 /* CMP == 1 */
766 *w25q_ranges = w25rq128_cmp1_ranges;
767 *num_entries = ARRAY_SIZE(w25rq128_cmp1_ranges);
768 } else {
769 /* CMP == 0 */
770 *w25q_ranges = w25rq128_cmp0_ranges;
771 *num_entries = ARRAY_SIZE(w25rq128_cmp0_ranges);
772 }
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +0530773 break;
David Hendricksd494b0a2010-08-16 16:28:50 -0700774 default:
775 msg_cerr("%s() %d: WINBOND flash chip mismatch (0x%04x)"
776 ", aborting\n", __func__, __LINE__,
777 flash->model_id);
778 return -1;
779 }
David Hendricks2c4a76c2010-06-28 14:00:43 -0700780 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700781 case EON_ID_NOPREFIX:
782 switch (flash->model_id) {
David Hendricksc801adb2010-12-09 16:58:56 -0800783 case EON_EN25F40:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800784 *w25q_ranges = en25f40_ranges;
785 *num_entries = ARRAY_SIZE(en25f40_ranges);
David Hendricks57566ed2010-08-16 18:24:45 -0700786 break;
David Hendrickse185bf22011-05-24 15:34:18 -0700787 case EON_EN25Q40:
788 *w25q_ranges = en25q40_ranges;
789 *num_entries = ARRAY_SIZE(en25q40_ranges);
790 break;
791 case EON_EN25Q80:
792 *w25q_ranges = en25q80_ranges;
793 *num_entries = ARRAY_SIZE(en25q80_ranges);
794 break;
795 case EON_EN25Q32:
796 *w25q_ranges = en25q32_ranges;
797 *num_entries = ARRAY_SIZE(en25q32_ranges);
798 break;
799 case EON_EN25Q64:
800 *w25q_ranges = en25q64_ranges;
801 *num_entries = ARRAY_SIZE(en25q64_ranges);
802 break;
803 case EON_EN25Q128:
804 *w25q_ranges = en25q128_ranges;
805 *num_entries = ARRAY_SIZE(en25q128_ranges);
806 break;
Marc Jonesb2f90022014-04-29 17:37:23 -0600807 case EON_EN25S64:
808 *w25q_ranges = en25s64_ranges;
809 *num_entries = ARRAY_SIZE(en25s64_ranges);
810 break;
David Hendricks57566ed2010-08-16 18:24:45 -0700811 default:
812 msg_cerr("%s():%d: EON flash chip mismatch (0x%04x)"
813 ", aborting\n", __func__, __LINE__,
814 flash->model_id);
815 return -1;
816 }
817 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800818 case MACRONIX_ID:
David Hendricksac72e362010-08-16 18:20:03 -0700819 switch (flash->model_id) {
David Hendricksf8f00c72011-02-01 12:39:46 -0800820 case MACRONIX_MX25L1005:
821 *w25q_ranges = mx25l1005_ranges;
822 *num_entries = ARRAY_SIZE(mx25l1005_ranges);
823 break;
824 case MACRONIX_MX25L2005:
825 *w25q_ranges = mx25l2005_ranges;
826 *num_entries = ARRAY_SIZE(mx25l2005_ranges);
827 break;
828 case MACRONIX_MX25L4005:
829 *w25q_ranges = mx25l4005_ranges;
830 *num_entries = ARRAY_SIZE(mx25l4005_ranges);
831 break;
832 case MACRONIX_MX25L8005:
833 *w25q_ranges = mx25l8005_ranges;
834 *num_entries = ARRAY_SIZE(mx25l8005_ranges);
835 break;
836 case MACRONIX_MX25L1605:
837 /* FIXME: MX25L1605 and MX25L1605D have different write
838 * protection capabilities, but share IDs */
839 *w25q_ranges = mx25l1605d_ranges;
840 *num_entries = ARRAY_SIZE(mx25l1605d_ranges);
841 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800842 case MACRONIX_MX25L3205:
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800843 *w25q_ranges = mx25l3205d_ranges;
844 *num_entries = ARRAY_SIZE(mx25l3205d_ranges);
David Hendricksac72e362010-08-16 18:20:03 -0700845 break;
Vincent Palatin87e092a2013-02-28 15:46:14 -0800846 case MACRONIX_MX25U3235E:
847 *w25q_ranges = mx25u3235e_ranges;
848 *num_entries = ARRAY_SIZE(mx25u3235e_ranges);
849 break;
Jongpil66a96492014-08-14 17:59:06 +0900850 case MACRONIX_MX25U6435E:
851 *w25q_ranges = mx25u6435e_ranges;
852 *num_entries = ARRAY_SIZE(mx25u6435e_ranges);
853 break;
David Hendricksac72e362010-08-16 18:20:03 -0700854 default:
855 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
856 ", aborting\n", __func__, __LINE__,
857 flash->model_id);
858 return -1;
859 }
860 break;
David Hendricksbfa624b2012-07-24 12:47:59 -0700861 case ST_ID:
862 switch(flash->model_id) {
863 case ST_N25Q064__1E:
864 case ST_N25Q064__3E:
865 *w25q_ranges = n25q064_ranges;
866 *num_entries = ARRAY_SIZE(n25q064_ranges);
867 break;
868 default:
869 msg_cerr("%s() %d: Micron flash chip mismatch"
870 " (0x%04x), aborting\n", __func__, __LINE__,
871 flash->model_id);
872 return -1;
873 }
874 break;
Bryan Freed9a0051f2012-05-22 16:06:09 -0700875 case GIGADEVICE_ID:
876 switch(flash->model_id) {
877 case GIGADEVICE_GD25LQ32:
878 *w25q_ranges = w25q32_ranges;
879 *num_entries = ARRAY_SIZE(w25q32_ranges);
880 break;
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700881 case GIGADEVICE_GD25Q64:
Marc Jonesb18734f2014-04-03 16:19:47 -0600882 case GIGADEVICE_GD25LQ64:
Shawn Nematbakhsh9e8ef492012-09-01 21:58:03 -0700883 *w25q_ranges = gd25q64_ranges;
884 *num_entries = ARRAY_SIZE(gd25q64_ranges);
885 break;
886 /* TODO(shawnn): add support for other GD parts */
Bryan Freed9a0051f2012-05-22 16:06:09 -0700887 default:
888 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
889 " (0x%04x), aborting\n", __func__, __LINE__,
890 flash->model_id);
891 return -1;
892 }
893 break;
Louis Yung-Chieh Loc8ec7152012-09-17 17:38:35 +0800894 case AMIC_ID_NOPREFIX:
895 switch(flash->model_id) {
896 case AMIC_A25L040:
897 *w25q_ranges = a25l040_ranges;
898 *num_entries = ARRAY_SIZE(a25l040_ranges);
899 break;
900 default:
901 msg_cerr("%s() %d: AMIC flash chip mismatch"
902 " (0x%04x), aborting\n", __func__, __LINE__,
903 flash->model_id);
904 return -1;
905 }
906 break;
David Hendricksf7924d12010-06-10 21:26:44 -0700907 default:
David Hendricksd494b0a2010-08-16 16:28:50 -0700908 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
909 __func__, flash->manufacture_id);
David Hendricksf7924d12010-06-10 21:26:44 -0700910 return -1;
911 }
912
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800913 return 0;
914}
915
916int w25_range_to_status(const struct flashchip *flash,
917 unsigned int start, unsigned int len,
918 struct w25q_status *status)
919{
920 struct w25q_range *w25q_ranges;
921 int i, range_found = 0;
922 int num_entries;
923
924 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -0700925 for (i = 0; i < num_entries; i++) {
926 struct wp_range *r = &w25q_ranges[i].range;
927
928 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
929 start, len, r->start, r->len);
930 if ((start == r->start) && (len == r->len)) {
David Hendricksd494b0a2010-08-16 16:28:50 -0700931 status->bp0 = w25q_ranges[i].bp & 1;
932 status->bp1 = w25q_ranges[i].bp >> 1;
933 status->bp2 = w25q_ranges[i].bp >> 2;
934 status->tb = w25q_ranges[i].tb;
935 status->sec = w25q_ranges[i].sec;
David Hendricksf7924d12010-06-10 21:26:44 -0700936
937 range_found = 1;
938 break;
939 }
940 }
941
942 if (!range_found) {
943 msg_cerr("matching range not found\n");
944 return -1;
945 }
David Hendricksd494b0a2010-08-16 16:28:50 -0700946 return 0;
947}
948
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800949int w25_status_to_range(const struct flashchip *flash,
950 const struct w25q_status *status,
951 unsigned int *start, unsigned int *len)
952{
953 struct w25q_range *w25q_ranges;
954 int i, status_found = 0;
955 int num_entries;
956
957 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
958 for (i = 0; i < num_entries; i++) {
959 int bp;
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800960 int table_bp, table_tb, table_sec;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800961
962 bp = status->bp0 | (status->bp1 << 1) | (status->bp2 << 2);
963 msg_cspew("comparing 0x%x 0x%x / 0x%x 0x%x / 0x%x 0x%x\n",
964 bp, w25q_ranges[i].bp,
965 status->tb, w25q_ranges[i].tb,
966 status->sec, w25q_ranges[i].sec);
Louis Yung-Chieh Loedd39302011-11-10 15:43:06 +0800967 table_bp = w25q_ranges[i].bp;
968 table_tb = w25q_ranges[i].tb;
969 table_sec = w25q_ranges[i].sec;
970 if ((bp == table_bp || table_bp == X) &&
971 (status->tb == table_tb || table_tb == X) &&
972 (status->sec == table_sec || table_sec == X)) {
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +0800973 *start = w25q_ranges[i].range.start;
974 *len = w25q_ranges[i].range.len;
975
976 status_found = 1;
977 break;
978 }
979 }
980
981 if (!status_found) {
982 msg_cerr("matching status not found\n");
983 return -1;
984 }
985 return 0;
986}
987
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800988/* Given a [start, len], this function calls w25_range_to_status() to convert
989 * it to flash-chip-specific range bits, then sets into status register.
990 */
David Hendricks91040832011-07-08 20:01:09 -0700991static int w25_set_range(const struct flashchip *flash,
David Hendricksd494b0a2010-08-16 16:28:50 -0700992 unsigned int start, unsigned int len)
993{
994 struct w25q_status status;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +0800995 int tmp = 0;
996 int expected = 0;
David Hendricksd494b0a2010-08-16 16:28:50 -0700997
998 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +0530999 tmp = do_read_status(flash);
David Hendricksd494b0a2010-08-16 16:28:50 -07001000 memcpy(&status, &tmp, 1);
1001 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1002
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001003 if (w25_range_to_status(flash, start, len, &status)) return -1;
David Hendricksf7924d12010-06-10 21:26:44 -07001004
1005 msg_cdbg("status.busy: %x\n", status.busy);
1006 msg_cdbg("status.wel: %x\n", status.wel);
1007 msg_cdbg("status.bp0: %x\n", status.bp0);
1008 msg_cdbg("status.bp1: %x\n", status.bp1);
1009 msg_cdbg("status.bp2: %x\n", status.bp2);
1010 msg_cdbg("status.tb: %x\n", status.tb);
1011 msg_cdbg("status.sec: %x\n", status.sec);
1012 msg_cdbg("status.srp0: %x\n", status.srp0);
1013
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001014 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301015 do_write_status(flash, expected);
David Hendricksf7924d12010-06-10 21:26:44 -07001016
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301017 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001018 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1019 if ((tmp & MASK_WP_AREA) == (expected & MASK_WP_AREA)) {
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001020 return 0;
1021 } else {
David Hendricksc801adb2010-12-09 16:58:56 -08001022 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001023 expected, tmp);
1024 return 1;
1025 }
David Hendricksf7924d12010-06-10 21:26:44 -07001026}
1027
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001028/* Print out the current status register value with human-readable text. */
David Hendricks91040832011-07-08 20:01:09 -07001029static int w25_wp_status(const struct flashchip *flash)
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001030{
1031 struct w25q_status status;
1032 int tmp;
David Hendricksce8ded32010-10-08 11:23:38 -07001033 unsigned int start, len;
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001034 int ret = 0;
1035
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001036 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301037 tmp = do_read_status(flash);
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001038 memcpy(&status, &tmp, 1);
1039 msg_cinfo("WP: status: 0x%02x\n", tmp);
1040 msg_cinfo("WP: status.srp0: %x\n", status.srp0);
1041 msg_cinfo("WP: write protect is %s.\n",
1042 status.srp0 ? "enabled" : "disabled");
1043
1044 msg_cinfo("WP: write protect range: ");
1045 if (w25_status_to_range(flash, &status, &start, &len)) {
1046 msg_cinfo("(cannot resolve the range)\n");
1047 ret = -1;
1048 } else {
1049 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1050 }
1051
1052 return ret;
1053}
1054
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001055/* Set/clear the SRP0 bit in the status register. */
David Hendricks91040832011-07-08 20:01:09 -07001056static int w25_set_srp0(const struct flashchip *flash, int enable)
David Hendricksf7924d12010-06-10 21:26:44 -07001057{
1058 struct w25q_status status;
1059 int tmp = 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001060 int expected = 0;
David Hendricksf7924d12010-06-10 21:26:44 -07001061
1062 memset(&status, 0, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301063 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001064 /* FIXME: this is NOT endian-free copy. */
David Hendricksf7924d12010-06-10 21:26:44 -07001065 memcpy(&status, &tmp, 1);
1066 msg_cdbg("%s: old status: 0x%02x\n", __func__, tmp);
1067
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001068 status.srp0 = enable ? 1 : 0;
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001069 memcpy(&expected, &status, sizeof(status));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301070 do_write_status(flash, expected);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001071
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301072 tmp = do_read_status(flash);
Louis Yung-Chieh Lo165b4642010-11-26 16:35:26 +08001073 msg_cdbg("%s: new status: 0x%02x\n", __func__, tmp);
1074 if ((tmp & MASK_WP_AREA) != (expected & MASK_WP_AREA))
1075 return 1;
David Hendricksf7924d12010-06-10 21:26:44 -07001076
1077 return 0;
1078}
1079
David Hendricks1c09f802012-10-03 11:03:48 -07001080static int w25_enable_writeprotect(const struct flashchip *flash,
1081 enum wp_mode wp_mode)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001082{
1083 int ret;
1084
David Hendricks1c09f802012-10-03 11:03:48 -07001085 switch (wp_mode) {
1086 case WP_MODE_HARDWARE:
1087 ret = w25_set_srp0(flash, 1);
1088 break;
1089 default:
1090 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1091 return 1;
1092 }
1093
David Hendricksc801adb2010-12-09 16:58:56 -08001094 if (ret)
1095 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001096 return ret;
1097}
1098
David Hendricks91040832011-07-08 20:01:09 -07001099static int w25_disable_writeprotect(const struct flashchip *flash)
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001100{
1101 int ret;
1102
1103 ret = w25_set_srp0(flash, 0);
David Hendricksc801adb2010-12-09 16:58:56 -08001104 if (ret)
1105 msg_cerr("%s(): error=%d.\n", __func__, ret);
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001106 return ret;
1107}
1108
David Hendricks91040832011-07-08 20:01:09 -07001109static int w25_list_ranges(const struct flashchip *flash)
David Hendricks0f7f5382011-02-11 18:12:31 -08001110{
1111 struct w25q_range *w25q_ranges;
1112 int i, num_entries;
1113
1114 if (w25_range_table(flash, &w25q_ranges, &num_entries)) return -1;
1115 for (i = 0; i < num_entries; i++) {
1116 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
1117 w25q_ranges[i].range.start,
1118 w25q_ranges[i].range.len);
1119 }
1120
1121 return 0;
1122}
1123
David Hendricks1c09f802012-10-03 11:03:48 -07001124static int w25q_wp_status(const struct flashchip *flash)
1125{
1126 struct w25q_status sr1;
1127 struct w25q_status_2 sr2;
David Hendricksf1bd8802012-10-30 11:37:57 -07001128 uint8_t tmp[2];
David Hendricks1c09f802012-10-03 11:03:48 -07001129 unsigned int start, len;
1130 int ret = 0;
1131
1132 memset(&sr1, 0, sizeof(sr1));
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301133 tmp[0] = do_read_status(flash);
David Hendricksf1bd8802012-10-30 11:37:57 -07001134 memcpy(&sr1, &tmp[0], 1);
David Hendricks1c09f802012-10-03 11:03:48 -07001135
David Hendricksf1bd8802012-10-30 11:37:57 -07001136 memset(&sr2, 0, sizeof(sr2));
1137 tmp[1] = w25q_read_status_register_2();
1138 memcpy(&sr2, &tmp[1], 1);
1139
1140 msg_cinfo("WP: status: 0x%02x%02x\n", tmp[1], tmp[0]);
David Hendricks1c09f802012-10-03 11:03:48 -07001141 msg_cinfo("WP: status.srp0: %x\n", sr1.srp0);
1142 msg_cinfo("WP: status.srp1: %x\n", sr2.srp1);
1143 msg_cinfo("WP: write protect is %s.\n",
1144 (sr1.srp0 || sr2.srp1) ? "enabled" : "disabled");
1145
1146 msg_cinfo("WP: write protect range: ");
1147 if (w25_status_to_range(flash, &sr1, &start, &len)) {
1148 msg_cinfo("(cannot resolve the range)\n");
1149 ret = -1;
1150 } else {
1151 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
1152 }
1153
1154 return ret;
1155}
1156
1157/*
1158 * W25Q adds an optional byte to the standard WRSR opcode. If /CS is
1159 * de-asserted after the first byte, then it acts like a JEDEC-standard
1160 * WRSR command. if /CS is asserted, then the next data byte is written
1161 * into status register 2.
1162 */
1163#define W25Q_WRSR_OUTSIZE 0x03
1164static int w25q_write_status_register_WREN(uint8_t s1, uint8_t s2)
1165{
1166 int result;
1167 struct spi_command cmds[] = {
1168 {
1169 /* FIXME: WRSR requires either EWSR or WREN depending on chip type. */
1170 .writecnt = JEDEC_WREN_OUTSIZE,
1171 .writearr = (const unsigned char[]){ JEDEC_WREN },
1172 .readcnt = 0,
1173 .readarr = NULL,
1174 }, {
1175 .writecnt = W25Q_WRSR_OUTSIZE,
1176 .writearr = (const unsigned char[]){ JEDEC_WRSR, s1, s2 },
1177 .readcnt = 0,
1178 .readarr = NULL,
1179 }, {
1180 .writecnt = 0,
1181 .writearr = NULL,
1182 .readcnt = 0,
1183 .readarr = NULL,
1184 }};
1185
1186 result = spi_send_multicommand(cmds);
1187 if (result) {
1188 msg_cerr("%s failed during command execution\n",
1189 __func__);
1190 }
1191
1192 /* WRSR performs a self-timed erase before the changes take effect. */
David Hendricks60824042014-12-11 17:22:06 -08001193 programmer_delay(100 * 1000);
David Hendricks1c09f802012-10-03 11:03:48 -07001194
1195 return result;
1196}
1197
1198/*
1199 * Set/clear the SRP1 bit in status register 2.
1200 * FIXME: make this more generic if other chips use the same SR2 layout
1201 */
1202static int w25q_set_srp1(const struct flashchip *flash, int enable)
1203{
1204 struct w25q_status sr1;
1205 struct w25q_status_2 sr2;
1206 uint8_t tmp, expected;
1207
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301208 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001209 memcpy(&sr1, &tmp, 1);
1210 tmp = w25q_read_status_register_2();
1211 memcpy(&sr2, &tmp, 1);
1212
1213 msg_cdbg("%s: old status 2: 0x%02x\n", __func__, tmp);
1214
1215 sr2.srp1 = enable ? 1 : 0;
1216
1217 memcpy(&expected, &sr2, 1);
1218 w25q_write_status_register_WREN(*((uint8_t *)&sr1), *((uint8_t *)&sr2));
1219
1220 tmp = w25q_read_status_register_2();
1221 msg_cdbg("%s: new status 2: 0x%02x\n", __func__, tmp);
1222 if ((tmp & MASK_WP2_AREA) != (expected & MASK_WP2_AREA))
1223 return 1;
1224
1225 return 0;
1226}
1227
1228enum wp_mode get_wp_mode(const char *mode_str)
1229{
1230 enum wp_mode wp_mode = WP_MODE_UNKNOWN;
1231
1232 if (!strcasecmp(mode_str, "hardware"))
1233 wp_mode = WP_MODE_HARDWARE;
1234 else if (!strcasecmp(mode_str, "power_cycle"))
1235 wp_mode = WP_MODE_POWER_CYCLE;
1236 else if (!strcasecmp(mode_str, "permanent"))
1237 wp_mode = WP_MODE_PERMANENT;
1238
1239 return wp_mode;
1240}
1241
1242static int w25q_disable_writeprotect(const struct flashchip *flash,
1243 enum wp_mode wp_mode)
1244{
1245 int ret = 1;
David Hendricks1c09f802012-10-03 11:03:48 -07001246 struct w25q_status_2 sr2;
1247 uint8_t tmp;
1248
1249 switch (wp_mode) {
1250 case WP_MODE_HARDWARE:
1251 ret = w25_set_srp0(flash, 0);
1252 break;
1253 case WP_MODE_POWER_CYCLE:
1254 tmp = w25q_read_status_register_2();
1255 memcpy(&sr2, &tmp, 1);
1256 if (sr2.srp1) {
1257 msg_cerr("%s(): must disconnect power to disable "
1258 "write-protection\n", __func__);
1259 } else {
1260 ret = 0;
1261 }
1262 break;
1263 case WP_MODE_PERMANENT:
1264 msg_cerr("%s(): cannot disable permanent write-protection\n",
1265 __func__);
1266 break;
1267 default:
1268 msg_cerr("%s(): invalid mode specified\n", __func__);
1269 break;
1270 }
1271
1272 if (ret)
1273 msg_cerr("%s(): error=%d.\n", __func__, ret);
1274 return ret;
1275}
1276
1277static int w25q_disable_writeprotect_default(const struct flashchip *flash)
1278{
1279 return w25q_disable_writeprotect(flash, WP_MODE_HARDWARE);
1280}
1281
1282static int w25q_enable_writeprotect(const struct flashchip *flash,
1283 enum wp_mode wp_mode)
1284{
1285 int ret = 1;
1286 struct w25q_status sr1;
1287 struct w25q_status_2 sr2;
1288 uint8_t tmp;
1289
1290 switch (wp_mode) {
1291 case WP_MODE_HARDWARE:
1292 if (w25q_disable_writeprotect(flash, WP_MODE_POWER_CYCLE)) {
1293 msg_cerr("%s(): cannot disable power cycle WP mode\n",
1294 __func__);
1295 break;
1296 }
1297
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301298 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001299 memcpy(&sr1, &tmp, 1);
1300 if (sr1.srp0)
1301 ret = 0;
1302 else
1303 ret = w25_set_srp0(flash, 1);
1304
1305 break;
1306 case WP_MODE_POWER_CYCLE:
1307 if (w25q_disable_writeprotect(flash, WP_MODE_HARDWARE)) {
1308 msg_cerr("%s(): cannot disable hardware WP mode\n",
1309 __func__);
1310 break;
1311 }
1312
1313 tmp = w25q_read_status_register_2();
1314 memcpy(&sr2, &tmp, 1);
1315 if (sr2.srp1)
1316 ret = 0;
1317 else
1318 ret = w25q_set_srp1(flash, 1);
1319
1320 break;
1321 case WP_MODE_PERMANENT:
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301322 tmp = do_read_status(flash);
David Hendricks1c09f802012-10-03 11:03:48 -07001323 memcpy(&sr1, &tmp, 1);
1324 if (sr1.srp0 == 0) {
1325 ret = w25_set_srp0(flash, 1);
1326 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001327 msg_perr("%s(): cannot enable SRP0 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001328 "permanent WP\n", __func__);
1329 break;
1330 }
1331 }
1332
1333 tmp = w25q_read_status_register_2();
1334 memcpy(&sr2, &tmp, 1);
1335 if (sr2.srp1 == 0) {
1336 ret = w25q_set_srp1(flash, 1);
1337 if (ret) {
David Hendricksf1bd8802012-10-30 11:37:57 -07001338 msg_perr("%s(): cannot enable SRP1 for "
David Hendricks1c09f802012-10-03 11:03:48 -07001339 "permanent WP\n", __func__);
1340 break;
1341 }
1342 }
1343
1344 break;
David Hendricksf1bd8802012-10-30 11:37:57 -07001345 default:
1346 msg_perr("%s(): invalid mode %d\n", __func__, wp_mode);
1347 break;
David Hendricks1c09f802012-10-03 11:03:48 -07001348 }
1349
1350 if (ret)
1351 msg_cerr("%s(): error=%d.\n", __func__, ret);
1352 return ret;
1353}
1354
David Hendricksc3496092014-11-13 17:20:55 -08001355/* FIXME: Move to spi25.c if it's a JEDEC standard opcode */
1356uint8_t mx25l_read_config_register(void)
1357{
1358 static const unsigned char cmd[JEDEC_RDSR_OUTSIZE] = { 0x15 };
1359 unsigned char readarr[2]; /* leave room for dummy byte */
1360 int ret;
1361
1362 ret = spi_send_command(sizeof(cmd), sizeof(readarr), cmd, readarr);
1363 if (ret) {
1364 msg_cerr("RDCR failed!\n");
1365 readarr[0] = 0x00;
1366 }
1367
1368 return readarr[0];
1369}
David Hendricks1c09f802012-10-03 11:03:48 -07001370/* W25P, W25X, and many flash chips from various vendors */
David Hendricksf7924d12010-06-10 21:26:44 -07001371struct wp wp_w25 = {
David Hendricks0f7f5382011-02-11 18:12:31 -08001372 .list_ranges = w25_list_ranges,
David Hendricksf7924d12010-06-10 21:26:44 -07001373 .set_range = w25_set_range,
1374 .enable = w25_enable_writeprotect,
Louis Yung-Chieh Loc19d3c52010-10-08 11:59:16 +08001375 .disable = w25_disable_writeprotect,
Louis Yung-Chieh Loa92e8b22010-10-08 13:31:27 +08001376 .wp_status = w25_wp_status,
David Hendricks1c09f802012-10-03 11:03:48 -07001377
1378};
1379
1380/* W25Q series has features such as a second status register and SFDP */
1381struct wp wp_w25q = {
1382 .list_ranges = w25_list_ranges,
1383 .set_range = w25_set_range,
1384 .enable = w25q_enable_writeprotect,
1385 /*
1386 * By default, disable hardware write-protection. We may change
1387 * this later if we want to add fine-grained write-protect disable
1388 * as a command-line option.
1389 */
1390 .disable = w25q_disable_writeprotect_default,
1391 .wp_status = w25q_wp_status,
David Hendricksf7924d12010-06-10 21:26:44 -07001392};
David Hendrickse0512a72014-07-15 20:30:47 -07001393
David Hendricksaf3944a2014-07-28 18:37:40 -07001394struct generic_range gd25q32_cmp0_ranges[] = {
1395 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001396 { { }, 0x00, {0, 0} },
1397 { { }, 0x08, {0, 0} },
1398 { { }, 0x10, {0, 0} },
1399 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001400
David Hendricks148a4bf2015-03-13 21:02:42 -07001401 { { }, 0x01, {0x3f0000, 64 * 1024} },
1402 { { }, 0x02, {0x3e0000, 128 * 1024} },
1403 { { }, 0x03, {0x3c0000, 256 * 1024} },
1404 { { }, 0x04, {0x380000, 512 * 1024} },
1405 { { }, 0x05, {0x300000, 1024 * 1024} },
1406 { { }, 0x06, {0x200000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001407
David Hendricks148a4bf2015-03-13 21:02:42 -07001408 { { }, 0x09, {0x000000, 64 * 1024} },
1409 { { }, 0x0a, {0x000000, 128 * 1024} },
1410 { { }, 0x0b, {0x000000, 256 * 1024} },
1411 { { }, 0x0c, {0x000000, 512 * 1024} },
1412 { { }, 0x0d, {0x000000, 1024 * 1024} },
1413 { { }, 0x0e, {0x000000, 2048 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001414
1415 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001416 { { }, 0x07, {0x000000, 4096 * 1024} },
1417 { { }, 0x0f, {0x000000, 4096 * 1024} },
1418 { { }, 0x17, {0x000000, 4096 * 1024} },
1419 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001420
David Hendricks148a4bf2015-03-13 21:02:42 -07001421 { { }, 0x11, {0x3ff000, 4 * 1024} },
1422 { { }, 0x12, {0x3fe000, 8 * 1024} },
1423 { { }, 0x13, {0x3fc000, 16 * 1024} },
1424 { { }, 0x14, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1425 { { }, 0x15, {0x3f8000, 32 * 1024} }, /* bp0 => don't care */
1426 { { }, 0x16, {0x3f8000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001427
David Hendricks148a4bf2015-03-13 21:02:42 -07001428 { { }, 0x19, {0x000000, 4 * 1024} },
1429 { { }, 0x1a, {0x000000, 8 * 1024} },
1430 { { }, 0x1b, {0x000000, 16 * 1024} },
1431 { { }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1432 { { }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1433 { { }, 0x1e, {0x000000, 32 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001434};
1435
1436struct generic_range gd25q32_cmp1_ranges[] = {
1437 /* none, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001438 { { }, 0x00, {0, 0} },
1439 { { }, 0x08, {0, 0} },
1440 { { }, 0x10, {0, 0} },
1441 { { }, 0x18, {0, 0} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001442
David Hendricks148a4bf2015-03-13 21:02:42 -07001443 { { }, 0x01, {0x000000, 4032 * 1024} },
1444 { { }, 0x02, {0x000000, 3968 * 1024} },
1445 { { }, 0x03, {0x000000, 3840 * 1024} },
1446 { { }, 0x04, {0x000000, 3584 * 1024} },
1447 { { }, 0x05, {0x000000, 3 * 1024 * 1024} },
1448 { { }, 0x06, {0x000000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001449
David Hendricks148a4bf2015-03-13 21:02:42 -07001450 { { }, 0x09, {0x010000, 4032 * 1024} },
1451 { { }, 0x0a, {0x020000, 3968 * 1024} },
1452 { { }, 0x0b, {0x040000, 3840 * 1024} },
1453 { { }, 0x0c, {0x080000, 3584 * 1024} },
1454 { { }, 0x0d, {0x100000, 3 * 1024 * 1024} },
1455 { { }, 0x0e, {0x200000, 2 * 1024 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001456
1457 /* all, bp4 and bp3 => don't care */
David Hendricks148a4bf2015-03-13 21:02:42 -07001458 { { }, 0x07, {0x000000, 4096 * 1024} },
1459 { { }, 0x0f, {0x000000, 4096 * 1024} },
1460 { { }, 0x17, {0x000000, 4096 * 1024} },
1461 { { }, 0x1f, {0x000000, 4096 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001462
David Hendricks148a4bf2015-03-13 21:02:42 -07001463 { { }, 0x11, {0x000000, 4092 * 1024} },
1464 { { }, 0x12, {0x000000, 4088 * 1024} },
1465 { { }, 0x13, {0x000000, 4080 * 1024} },
1466 { { }, 0x14, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1467 { { }, 0x15, {0x000000, 4064 * 1024} }, /* bp0 => don't care */
1468 { { }, 0x16, {0x000000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001469
David Hendricks148a4bf2015-03-13 21:02:42 -07001470 { { }, 0x19, {0x001000, 4092 * 1024} },
1471 { { }, 0x1a, {0x002000, 4088 * 1024} },
1472 { { }, 0x1b, {0x040000, 4080 * 1024} },
1473 { { }, 0x1c, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1474 { { }, 0x1d, {0x080000, 4064 * 1024} }, /* bp0 => don't care */
1475 { { }, 0x1e, {0x080000, 4064 * 1024} },
David Hendricksaf3944a2014-07-28 18:37:40 -07001476};
1477
1478static struct generic_wp gd25q32_wp = {
1479 /* TODO: map second status register */
1480 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1481};
1482
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001483struct generic_range gd25q128_cmp0_ranges[] = {
1484 /* none, bp4 and bp3 => don't care, others = 0 */
1485 { { .tb = 0 }, 0x00, {0, 0} },
1486 { { .tb = 0 }, 0x08, {0, 0} },
1487 { { .tb = 0 }, 0x10, {0, 0} },
1488 { { .tb = 0 }, 0x18, {0, 0} },
1489
1490 { { .tb = 0 }, 0x01, {0xfc0000, 256 * 1024} },
1491 { { .tb = 0 }, 0x02, {0xf80000, 512 * 1024} },
1492 { { .tb = 0 }, 0x03, {0xf00000, 1024 * 1024} },
1493 { { .tb = 0 }, 0x04, {0xe00000, 2048 * 1024} },
1494 { { .tb = 0 }, 0x05, {0xc00000, 4096 * 1024} },
1495 { { .tb = 0 }, 0x06, {0x800000, 8192 * 1024} },
1496
1497 { { .tb = 0 }, 0x09, {0x000000, 256 * 1024} },
1498 { { .tb = 0 }, 0x0a, {0x000000, 512 * 1024} },
1499 { { .tb = 0 }, 0x0b, {0x000000, 1024 * 1024} },
1500 { { .tb = 0 }, 0x0c, {0x000000, 2048 * 1024} },
1501 { { .tb = 0 }, 0x0d, {0x000000, 4096 * 1024} },
1502 { { .tb = 0 }, 0x0e, {0x000000, 8192 * 1024} },
1503
1504 /* all, bp4 and bp3 => don't care, others = 1 */
1505 { { .tb = 0 }, 0x07, {0x000000, 16384 * 1024} },
1506 { { .tb = 0 }, 0x0f, {0x000000, 16384 * 1024} },
1507 { { .tb = 0 }, 0x17, {0x000000, 16384 * 1024} },
1508 { { .tb = 0 }, 0x1f, {0x000000, 16384 * 1024} },
1509
1510 { { .tb = 0 }, 0x11, {0xfff000, 4 * 1024} },
1511 { { .tb = 0 }, 0x12, {0xffe000, 8 * 1024} },
1512 { { .tb = 0 }, 0x13, {0xffc000, 16 * 1024} },
1513 { { .tb = 0 }, 0x14, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1514 { { .tb = 0 }, 0x15, {0xff8000, 32 * 1024} }, /* bp0 => don't care */
1515
1516 { { .tb = 0 }, 0x19, {0x000000, 4 * 1024} },
1517 { { .tb = 0 }, 0x1a, {0x000000, 8 * 1024} },
1518 { { .tb = 0 }, 0x1b, {0x000000, 16 * 1024} },
1519 { { .tb = 0 }, 0x1c, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1520 { { .tb = 0 }, 0x1d, {0x000000, 32 * 1024} }, /* bp0 => don't care */
1521 { { .tb = 0 }, 0x1e, {0x000000, 32 * 1024} },
1522};
1523
1524struct generic_range gd25q128_cmp1_ranges[] = {
1525 /* none, bp4 and bp3 => don't care, others = 0 */
1526 { { .tb = 1 }, 0x00, {0x000000, 16384 * 1024} },
1527 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1528 { { .tb = 1 }, 0x10, {0x000000, 16384 * 1024} },
1529 { { .tb = 1 }, 0x18, {0x000000, 16384 * 1024} },
1530
1531 { { .tb = 1 }, 0x01, {0x000000, 16128 * 1024} },
1532 { { .tb = 1 }, 0x02, {0x000000, 15872 * 1024} },
1533 { { .tb = 1 }, 0x03, {0x000000, 15360 * 1024} },
1534 { { .tb = 1 }, 0x04, {0x000000, 14336 * 1024} },
1535 { { .tb = 1 }, 0x05, {0x000000, 12288 * 1024} },
1536 { { .tb = 1 }, 0x06, {0x000000, 8192 * 1024} },
1537
1538 { { .tb = 1 }, 0x09, {0x000000, 16128 * 1024} },
1539 { { .tb = 1 }, 0x0a, {0x000000, 15872 * 1024} },
1540 { { .tb = 1 }, 0x0b, {0x000000, 15360 * 1024} },
1541 { { .tb = 1 }, 0x0c, {0x000000, 14336 * 1024} },
1542 { { .tb = 1 }, 0x0d, {0x000000, 12288 * 1024} },
1543 { { .tb = 1 }, 0x0e, {0x000000, 8192 * 1024} },
1544
1545 /* none, bp4 and bp3 => don't care, others = 1 */
1546 { { .tb = 1 }, 0x07, {0x000000, 16384 * 1024} },
1547 { { .tb = 1 }, 0x08, {0x000000, 16384 * 1024} },
1548 { { .tb = 1 }, 0x0f, {0x000000, 16384 * 1024} },
1549 { { .tb = 1 }, 0x17, {0x000000, 16384 * 1024} },
1550 { { .tb = 1 }, 0x1f, {0x000000, 16384 * 1024} },
1551
1552 { { .tb = 1 }, 0x11, {0x000000, 16380 * 1024} },
1553 { { .tb = 1 }, 0x12, {0x000000, 16376 * 1024} },
1554 { { .tb = 1 }, 0x13, {0x000000, 16368 * 1024} },
1555 { { .tb = 1 }, 0x14, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1556 { { .tb = 1 }, 0x15, {0x000000, 16352 * 1024} }, /* bp0 => don't care */
1557
1558 { { .tb = 1 }, 0x19, {0x001000, 16380 * 1024} },
1559 { { .tb = 1 }, 0x1a, {0x002000, 16376 * 1024} },
1560 { { .tb = 1 }, 0x1b, {0x004000, 16368 * 1024} },
1561 { { .tb = 1 }, 0x1c, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1562 { { .tb = 1 }, 0x1d, {0x008000, 16352 * 1024} }, /* bp0 => don't care */
1563 { { .tb = 1 }, 0x1e, {0x008000, 16352 * 1024} },
1564};
1565
1566static struct generic_wp gd25q128_wp = {
1567 /* TODO: map second and third status registers */
1568 .sr1 = { .bp0_pos = 2, .bp_bits = 5, .srp_pos = 7 },
1569};
1570
David Hendricks83541d32014-07-15 20:58:21 -07001571#if 0
1572/* FIXME: MX25L6405D has same ID as MX25L6406 */
1573static struct w25q_range mx25l6405d_ranges[] = {
1574 { X, 0, 0, {0, 0} }, /* none */
1575 { X, 0, 0x1, {0x7e0000, 2 * 64 * 1024} }, /* blocks 126-127 */
1576 { X, 0, 0x2, {0x7c0000, 4 * 64 * 1024} }, /* blocks 124-127 */
1577 { X, 0, 0x3, {0x780000, 8 * 64 * 1024} }, /* blocks 120-127 */
1578 { X, 0, 0x4, {0x700000, 16 * 64 * 1024} }, /* blocks 112-127 */
1579 { X, 0, 0x5, {0x600000, 32 * 64 * 1024} }, /* blocks 96-127 */
1580 { X, 0, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1581 { X, 0, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1582
1583 { X, 1, 0x0, {0x000000, 8192 * 1024} },
1584 { X, 1, 0x1, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1585 { X, 1, 0x2, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1586 { X, 1, 0x3, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1587 { X, 1, 0x4, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1588 { X, 1, 0x5, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1589 { X, 1, 0x6, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1590 { X, 1, 0x7, {0x000000, 64 * 128 * 1024} }, /* blocks 0-127 */
1591};
1592#endif
1593
1594/* FIXME: MX25L6406 has same ID as MX25L6405D */
1595struct generic_range mx25l6406e_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001596 { { }, 0, {0, 0} }, /* none */
1597 { { }, 0x1, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1598 { { }, 0x2, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
1599 { { }, 0x3, {0x7a0000, 64 * 8 * 1024} }, /* blocks 120-127 */
1600 { { }, 0x4, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1601 { { }, 0x5, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1602 { { }, 0x6, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
David Hendricks83541d32014-07-15 20:58:21 -07001603
David Hendricks148a4bf2015-03-13 21:02:42 -07001604 { { }, 0x7, {0x000000, 64 * 128 * 1024} }, /* all */
1605 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1606 { { }, 0x9, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1607 { { }, 0xa, {0x000000, 64 * 96 * 1024} }, /* blocks 0-95 */
1608 { { }, 0xb, {0x000000, 64 * 112 * 1024} }, /* blocks 0-111 */
1609 { { }, 0xc, {0x000000, 64 * 120 * 1024} }, /* blocks 0-119 */
1610 { { }, 0xd, {0x000000, 64 * 124 * 1024} }, /* blocks 0-123 */
1611 { { }, 0xe, {0x000000, 64 * 126 * 1024} }, /* blocks 0-125 */
1612 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricks83541d32014-07-15 20:58:21 -07001613};
1614
1615static struct generic_wp mx25l6406e_wp = {
1616 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1617 .ranges = &mx25l6406e_ranges[0],
1618};
David Hendrickse0512a72014-07-15 20:30:47 -07001619
David Hendricksc3496092014-11-13 17:20:55 -08001620struct generic_range mx25l6495f_tb0_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001621 { { }, 0, {0, 0} }, /* none */
1622 { { }, 0x1, {0x7f0000, 64 * 1 * 1024} }, /* block 127 */
1623 { { }, 0x2, {0x7e0000, 64 * 2 * 1024} }, /* blocks 126-127 */
1624 { { }, 0x3, {0x7c0000, 64 * 4 * 1024} }, /* blocks 124-127 */
David Hendricksc3496092014-11-13 17:20:55 -08001625
David Hendricks148a4bf2015-03-13 21:02:42 -07001626 { { }, 0x4, {0x780000, 64 * 8 * 1024} }, /* blocks 120-127 */
1627 { { }, 0x5, {0x700000, 64 * 16 * 1024} }, /* blocks 112-127 */
1628 { { }, 0x6, {0x600000, 64 * 32 * 1024} }, /* blocks 96-127 */
1629 { { }, 0x7, {0x400000, 64 * 64 * 1024} }, /* blocks 64-127 */
1630 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1631 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1632 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1633 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1634 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1635 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1636 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1637 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001638};
1639
1640struct generic_range mx25l6495f_tb1_ranges[] = {
David Hendricks148a4bf2015-03-13 21:02:42 -07001641 { { }, 0, {0, 0} }, /* none */
1642 { { }, 0x1, {0x000000, 64 * 1 * 1024} }, /* block 0 */
1643 { { }, 0x2, {0x000000, 64 * 2 * 1024} }, /* blocks 0-1 */
1644 { { }, 0x3, {0x000000, 64 * 4 * 1024} }, /* blocks 0-3 */
1645 { { }, 0x4, {0x000000, 64 * 8 * 1024} }, /* blocks 0-7 */
1646 { { }, 0x5, {0x000000, 64 * 16 * 1024} }, /* blocks 0-15 */
1647 { { }, 0x6, {0x000000, 64 * 32 * 1024} }, /* blocks 0-31 */
1648 { { }, 0x7, {0x000000, 64 * 64 * 1024} }, /* blocks 0-63 */
1649 { { }, 0x8, {0x000000, 64 * 128 * 1024} }, /* all */
1650 { { }, 0x9, {0x000000, 64 * 128 * 1024} }, /* all */
1651 { { }, 0xa, {0x000000, 64 * 128 * 1024} }, /* all */
1652 { { }, 0xb, {0x000000, 64 * 128 * 1024} }, /* all */
1653 { { }, 0xc, {0x000000, 64 * 128 * 1024} }, /* all */
1654 { { }, 0xd, {0x000000, 64 * 128 * 1024} }, /* all */
1655 { { }, 0xe, {0x000000, 64 * 128 * 1024} }, /* all */
1656 { { }, 0xf, {0x000000, 64 * 128 * 1024} }, /* all */
David Hendricksc3496092014-11-13 17:20:55 -08001657};
1658
1659static struct generic_wp mx25l6495f_wp = {
1660 .sr1 = { .bp0_pos = 2, .bp_bits = 4, .srp_pos = 7 },
1661};
1662
David Hendricks148a4bf2015-03-13 21:02:42 -07001663struct generic_range s25fs128s_ranges[] = {
1664 { { .tb = 1 }, 0, {0, 0} }, /* none */
1665 { { .tb = 1 }, 0x1, {0x000000, 256 * 1024} }, /* lower 64th */
1666 { { .tb = 1 }, 0x2, {0x000000, 512 * 1024} }, /* lower 32nd */
1667 { { .tb = 1 }, 0x3, {0x000000, 1024 * 1024} }, /* lower 16th */
1668 { { .tb = 1 }, 0x4, {0x000000, 2048 * 1024} }, /* lower 8th */
1669 { { .tb = 1 }, 0x5, {0x000000, 4096 * 1024} }, /* lower 4th */
1670 { { .tb = 1 }, 0x6, {0x000000, 8192 * 1024} }, /* lower half */
1671 { { .tb = 1 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001672
David Hendricks148a4bf2015-03-13 21:02:42 -07001673 { { .tb = 0 }, 0, {0, 0} }, /* none */
1674 { { .tb = 0 }, 0x1, {0xfc0000, 256 * 1024} }, /* upper 64th */
1675 { { .tb = 0 }, 0x2, {0xf80000, 512 * 1024} }, /* upper 32nd */
1676 { { .tb = 0 }, 0x3, {0xf00000, 1024 * 1024} }, /* upper 16th */
1677 { { .tb = 0 }, 0x4, {0xe00000, 2048 * 1024} }, /* upper 8th */
1678 { { .tb = 0 }, 0x5, {0xc00000, 4096 * 1024} }, /* upper 4th */
1679 { { .tb = 0 }, 0x6, {0x800000, 8192 * 1024} }, /* upper half */
1680 { { .tb = 0 }, 0x7, {0x000000, 16384 * 1024} }, /* all */
David Hendricksa9884852014-12-11 15:31:12 -08001681};
1682
1683static struct generic_wp s25fs128s_wp = {
1684 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001685 .get_modifier_bits = s25f_get_modifier_bits,
1686 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksa9884852014-12-11 15:31:12 -08001687};
1688
David Hendricksc694bb82015-02-25 14:52:17 -08001689
David Hendricks148a4bf2015-03-13 21:02:42 -07001690struct generic_range s25fl256s_ranges[] = {
1691 { { .tb = 1 }, 0, {0, 0} }, /* none */
1692 { { .tb = 1 }, 0x1, {0x000000, 512 * 1024} }, /* lower 64th */
1693 { { .tb = 1 }, 0x2, {0x000000, 1024 * 1024} }, /* lower 32nd */
1694 { { .tb = 1 }, 0x3, {0x000000, 2048 * 1024} }, /* lower 16th */
1695 { { .tb = 1 }, 0x4, {0x000000, 4096 * 1024} }, /* lower 8th */
1696 { { .tb = 1 }, 0x5, {0x000000, 8192 * 1024} }, /* lower 4th */
1697 { { .tb = 1 }, 0x6, {0x000000, 16384 * 1024} }, /* lower half */
1698 { { .tb = 1 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
1699
1700 { { .tb = 0 }, 0, {0, 0} }, /* none */
1701 { { .tb = 0 }, 0x1, {0x1f80000, 512 * 1024} }, /* upper 64th */
1702 { { .tb = 0 }, 0x2, {0x1f00000, 1024 * 1024} }, /* upper 32nd */
1703 { { .tb = 0 }, 0x3, {0x1e00000, 2048 * 1024} }, /* upper 16th */
1704 { { .tb = 0 }, 0x4, {0x1c00000, 4096 * 1024} }, /* upper 8th */
1705 { { .tb = 0 }, 0x5, {0x1800000, 8192 * 1024} }, /* upper 4th */
1706 { { .tb = 0 }, 0x6, {0x1000000, 16384 * 1024} }, /* upper half */
1707 { { .tb = 0 }, 0x7, {0x000000, 32768 * 1024} }, /* all */
David Hendricksc694bb82015-02-25 14:52:17 -08001708};
1709
1710static struct generic_wp s25fl256s_wp = {
1711 .sr1 = { .bp0_pos = 2, .bp_bits = 3, .srp_pos = 7 },
David Hendricks148a4bf2015-03-13 21:02:42 -07001712 .get_modifier_bits = s25f_get_modifier_bits,
1713 .set_modifier_bits = s25f_set_modifier_bits,
David Hendricksc694bb82015-02-25 14:52:17 -08001714};
1715
David Hendrickse0512a72014-07-15 20:30:47 -07001716/* Given a flash chip, this function returns its writeprotect info. */
1717static int generic_range_table(const struct flashchip *flash,
1718 struct generic_wp **wp,
1719 int *num_entries)
1720{
1721 *wp = NULL;
1722 *num_entries = 0;
1723
1724 switch (flash->manufacture_id) {
David Hendricksaf3944a2014-07-28 18:37:40 -07001725 case GIGADEVICE_ID:
1726 switch(flash->model_id) {
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001727
David Hendricksaf3944a2014-07-28 18:37:40 -07001728 case GIGADEVICE_GD25Q32: {
1729 uint8_t sr1 = w25q_read_status_register_2();
David Hendricksaf3944a2014-07-28 18:37:40 -07001730 *wp = &gd25q32_wp;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001731
David Hendricksaf3944a2014-07-28 18:37:40 -07001732 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1733 (*wp)->ranges = &gd25q32_cmp0_ranges[0];
1734 *num_entries = ARRAY_SIZE(gd25q32_cmp0_ranges);
1735 } else { /* CMP == 1 */
1736 (*wp)->ranges = &gd25q32_cmp1_ranges[0];
1737 *num_entries = ARRAY_SIZE(gd25q32_cmp1_ranges);
1738 }
1739
1740 break;
David Hendricks1e9d7ca2016-03-14 15:50:34 -07001741 }
1742 case GIGADEVICE_GD25Q128: {
1743 uint8_t sr1 = w25q_read_status_register_2();
1744 *wp = &gd25q128_wp;
1745
1746 if (!(sr1 & (1 << 6))) { /* CMP == 0 */
1747 (*wp)->ranges = &gd25q128_cmp0_ranges[0];
1748 *num_entries = ARRAY_SIZE(gd25q128_cmp0_ranges);
1749 } else { /* CMP == 1 */
1750 (*wp)->ranges = &gd25q128_cmp1_ranges[0];
1751 *num_entries = ARRAY_SIZE(gd25q128_cmp1_ranges);
1752 }
1753
1754 break;
David Hendricksaf3944a2014-07-28 18:37:40 -07001755 }
1756 default:
1757 msg_cerr("%s() %d: GigaDevice flash chip mismatch"
1758 " (0x%04x), aborting\n", __func__, __LINE__,
1759 flash->model_id);
1760 return -1;
1761 }
1762 break;
David Hendricks83541d32014-07-15 20:58:21 -07001763 case MACRONIX_ID:
1764 switch (flash->model_id) {
1765 case MACRONIX_MX25L6405:
1766 /* FIXME: MX25L64* chips have mixed capabilities and
1767 share IDs */
1768 *wp = &mx25l6406e_wp;
1769 *num_entries = ARRAY_SIZE(mx25l6406e_ranges);
1770 break;
David Hendricksc3496092014-11-13 17:20:55 -08001771 case MACRONIX_MX25L6495F: {
1772 uint8_t cr = mx25l_read_config_register();
1773
1774 *wp = &mx25l6495f_wp;
1775 if (!(cr & (1 << 3))) { /* T/B == 0 */
1776 (*wp)->ranges = &mx25l6495f_tb0_ranges[0];
1777 *num_entries = ARRAY_SIZE(mx25l6495f_tb0_ranges);
1778 } else { /* T/B == 1 */
1779 (*wp)->ranges = &mx25l6495f_tb1_ranges[0];
1780 *num_entries = ARRAY_SIZE(mx25l6495f_tb1_ranges);
1781 }
1782 break;
1783 }
David Hendricks83541d32014-07-15 20:58:21 -07001784 default:
1785 msg_cerr("%s():%d: MXIC flash chip mismatch (0x%04x)"
1786 ", aborting\n", __func__, __LINE__,
1787 flash->model_id);
1788 return -1;
1789 }
1790 break;
David Hendricksa9884852014-12-11 15:31:12 -08001791 case SPANSION_ID:
1792 switch (flash->model_id) {
1793 case SPANSION_S25FS128S_L:
1794 case SPANSION_S25FS128S_S: {
David Hendricksa9884852014-12-11 15:31:12 -08001795 *wp = &s25fs128s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001796 (*wp)->ranges = s25fs128s_ranges;
1797 *num_entries = ARRAY_SIZE(s25fs128s_ranges);
David Hendricksa9884852014-12-11 15:31:12 -08001798 break;
1799 }
David Hendricksc694bb82015-02-25 14:52:17 -08001800 case SPANSION_S25FL256S_UL:
1801 case SPANSION_S25FL256S_US: {
David Hendricksc694bb82015-02-25 14:52:17 -08001802 *wp = &s25fl256s_wp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001803 (*wp)->ranges = s25fl256s_ranges;
1804 *num_entries = ARRAY_SIZE(s25fl256s_ranges);
David Hendricksc694bb82015-02-25 14:52:17 -08001805 break;
1806 }
David Hendricksa9884852014-12-11 15:31:12 -08001807 default:
1808 msg_cerr("%s():%d Spansion flash chip mismatch (0x%04x)"
1809 ", aborting\n", __func__, __LINE__, flash->model_id);
1810 return -1;
1811 }
1812 break;
David Hendrickse0512a72014-07-15 20:30:47 -07001813 default:
1814 msg_cerr("%s: flash vendor (0x%x) not found, aborting\n",
1815 __func__, flash->manufacture_id);
1816 return -1;
1817 }
1818
1819 return 0;
1820}
1821
1822/* Given a [start, len], this function finds a block protect bit combination
1823 * (if possible) and sets the corresponding bits in "status". Remaining bits
1824 * are preserved. */
1825static int generic_range_to_status(const struct flashchip *flash,
1826 unsigned int start, unsigned int len,
1827 uint8_t *status)
1828{
1829 struct generic_wp *wp;
1830 struct generic_range *r;
1831 int i, range_found = 0, num_entries;
1832 uint8_t bp_mask;
1833
1834 if (generic_range_table(flash, &wp, &num_entries))
1835 return -1;
1836
1837 bp_mask = ((1 << (wp->sr1.bp0_pos + wp->sr1.bp_bits)) - 1) - \
1838 ((1 << wp->sr1.bp0_pos) - 1);
1839
1840 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
1841 msg_cspew("comparing range 0x%x 0x%x / 0x%x 0x%x\n",
1842 start, len, r->range.start, r->range.len);
1843 if ((start == r->range.start) && (len == r->range.len)) {
1844 *status &= ~(bp_mask);
1845 *status |= r->bp << (wp->sr1.bp0_pos);
David Hendricks148a4bf2015-03-13 21:02:42 -07001846
1847 if (wp->set_modifier_bits) {
1848 if (wp->set_modifier_bits(flash, &r->m) < 0) {
1849 msg_cerr("error setting modifier "
1850 "bits for range.\n");
1851 return -1;
1852 }
1853 }
1854
David Hendrickse0512a72014-07-15 20:30:47 -07001855 range_found = 1;
1856 break;
1857 }
1858 }
1859
1860 if (!range_found) {
1861 msg_cerr("matching range not found\n");
1862 return -1;
1863 }
1864 return 0;
1865}
1866
1867static int generic_status_to_range(const struct flashchip *flash,
1868 const uint8_t sr1, unsigned int *start, unsigned int *len)
1869{
1870 struct generic_wp *wp;
1871 struct generic_range *r;
Duncan Laurie04ca1172015-03-12 09:25:34 -07001872 int num_entries, i, status_found = 0;
David Hendrickse0512a72014-07-15 20:30:47 -07001873 uint8_t sr1_bp;
David Hendricks148a4bf2015-03-13 21:02:42 -07001874 struct generic_modifier_bits m;
David Hendrickse0512a72014-07-15 20:30:47 -07001875
1876 if (generic_range_table(flash, &wp, &num_entries))
1877 return -1;
1878
David Hendricks148a4bf2015-03-13 21:02:42 -07001879 /* modifier bits may be compared more than once, so get them here */
1880 if (wp->get_modifier_bits) {
1881 if (wp->get_modifier_bits(flash, &m) < 0)
1882 return -1;
1883 }
1884
David Hendrickse0512a72014-07-15 20:30:47 -07001885 sr1_bp = (sr1 >> wp->sr1.bp0_pos) & ((1 << wp->sr1.bp_bits) - 1);
1886
1887 for (i = 0, r = &wp->ranges[0]; i < num_entries; i++, r++) {
David Hendricks148a4bf2015-03-13 21:02:42 -07001888 if (wp->get_modifier_bits) {
1889 if (memcmp(&m, &r->m, sizeof(m)))
1890 continue;
1891 }
David Hendrickse0512a72014-07-15 20:30:47 -07001892 msg_cspew("comparing 0x%02x 0x%02x\n", sr1_bp, r->bp);
1893 if (sr1_bp == r->bp) {
1894 *start = r->range.start;
1895 *len = r->range.len;
1896 status_found = 1;
1897 break;
1898 }
1899 }
1900
1901 if (!status_found) {
1902 msg_cerr("matching status not found\n");
1903 return -1;
1904 }
1905 return 0;
1906}
1907
1908/* Given a [start, len], this function calls generic_range_to_status() to
1909 * convert it to flash-chip-specific range bits, then sets into status register.
1910 */
1911static int generic_set_range(const struct flashchip *flash,
1912 unsigned int start, unsigned int len)
1913{
1914 uint8_t status, expected;
1915
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301916 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07001917 msg_cdbg("%s: old status: 0x%02x\n", __func__, status);
1918
1919 expected = status; /* preserve non-bp bits */
1920 if (generic_range_to_status(flash, start, len, &expected))
1921 return -1;
1922
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301923 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001924
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301925 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07001926 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1927 if (status != expected) {
1928 msg_cerr("expected=0x%02x, but actual=0x%02x.\n",
1929 expected, status);
1930 return 1;
1931 }
1932
1933 return 0;
1934}
1935
1936/* Set/clear the status regsiter write protect bit in SR1. */
1937static int generic_set_srp0(const struct flashchip *flash, int enable)
1938{
1939 uint8_t status, expected;
1940 struct generic_wp *wp;
1941 int num_entries;
1942
1943 if (generic_range_table(flash, &wp, &num_entries))
1944 return -1;
1945
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301946 expected = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07001947 msg_cdbg("%s: old status: 0x%02x\n", __func__, expected);
1948
1949 if (enable)
1950 expected |= 1 << wp->sr1.srp_pos;
1951 else
1952 expected &= ~(1 << wp->sr1.srp_pos);
1953
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301954 do_write_status(flash, expected);
David Hendrickse0512a72014-07-15 20:30:47 -07001955
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05301956 status = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07001957 msg_cdbg("%s: new status: 0x%02x\n", __func__, status);
1958 if (status != expected)
1959 return -1;
1960
1961 return 0;
1962}
1963
1964static int generic_enable_writeprotect(const struct flashchip *flash,
1965 enum wp_mode wp_mode)
1966{
1967 int ret;
1968
1969 switch (wp_mode) {
1970 case WP_MODE_HARDWARE:
1971 ret = generic_set_srp0(flash, 1);
1972 break;
1973 default:
1974 msg_cerr("%s(): unsupported write-protect mode\n", __func__);
1975 return 1;
1976 }
1977
1978 if (ret)
1979 msg_cerr("%s(): error=%d.\n", __func__, ret);
1980 return ret;
1981}
1982
1983static int generic_disable_writeprotect(const struct flashchip *flash)
1984{
1985 int ret;
1986
1987 ret = generic_set_srp0(flash, 0);
1988 if (ret)
1989 msg_cerr("%s(): error=%d.\n", __func__, ret);
1990 return ret;
1991}
1992
1993static int generic_list_ranges(const struct flashchip *flash)
1994{
1995 struct generic_wp *wp;
1996 struct generic_range *r;
1997 int i, num_entries;
1998
1999 if (generic_range_table(flash, &wp, &num_entries))
2000 return -1;
2001
2002 r = &wp->ranges[0];
2003 for (i = 0; i < num_entries; i++) {
2004 msg_cinfo("start: 0x%06x, length: 0x%06x\n",
2005 r->range.start, r->range.len);
2006 r++;
2007 }
2008
2009 return 0;
2010}
2011
2012static int generic_wp_status(const struct flashchip *flash)
2013{
2014 uint8_t sr1;
2015 unsigned int start, len;
2016 int ret = 0;
2017 struct generic_wp *wp;
David Hendrickse0512a72014-07-15 20:30:47 -07002018 int num_entries, wp_en;
2019
2020 if (generic_range_table(flash, &wp, &num_entries))
2021 return -1;
2022
Ramya Vijaykumar4af3f822016-01-27 11:51:27 +05302023 sr1 = do_read_status(flash);
David Hendrickse0512a72014-07-15 20:30:47 -07002024 wp_en = (sr1 >> wp->sr1.srp_pos) & 1;
2025
2026 msg_cinfo("WP: status: 0x%04x\n", sr1);
2027 msg_cinfo("WP: status.srp0: %x\n", wp_en);
2028 /* FIXME: SRP1 is not really generic, but we probably should print
2029 * it anyway to have consistent output. #legacycruft */
2030 msg_cinfo("WP: status.srp1: %x\n", 0);
2031 msg_cinfo("WP: write protect is %s.\n",
2032 wp_en ? "enabled" : "disabled");
2033
2034 msg_cinfo("WP: write protect range: ");
2035 if (generic_status_to_range(flash, sr1, &start, &len)) {
2036 msg_cinfo("(cannot resolve the range)\n");
2037 ret = -1;
2038 } else {
2039 msg_cinfo("start=0x%08x, len=0x%08x\n", start, len);
2040 }
2041
2042 return ret;
2043}
2044
2045struct wp wp_generic = {
2046 .list_ranges = generic_list_ranges,
2047 .set_range = generic_set_range,
2048 .enable = generic_enable_writeprotect,
2049 .disable = generic_disable_writeprotect,
2050 .wp_status = generic_wp_status,
2051};